2013-03-17 17:06:52 -04:00
#
2013-10-27 21:59:46 +01:00
# Copyright 2009, 2013 Red Hat, Inc.
2013-03-17 17:06:52 -04:00
#
2018-04-04 14:35:41 +01:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 15:00:02 -04:00
# See the COPYING file in the top-level directory.
2013-03-17 17:06:52 -04:00
2019-05-13 20:13:42 -04:00
from . device import Device , DeviceSeclabel
2019-05-13 14:23:27 -04:00
from . . xmlbuilder import XMLBuilder , XMLChildProperty , XMLProperty
2019-06-07 18:21:24 -04:00
from . . import xmlutil
2019-05-13 14:23:27 -04:00
def _set_host_helper ( obj , hostparam , portparam , val ) :
def parse_host ( val ) :
host , ignore , port = ( val or " " ) . partition ( " : " )
return host or None , port or None
host , port = parse_host ( val )
2019-05-15 17:21:26 -04:00
if port and not host :
2019-05-13 14:23:27 -04:00
host = " 127.0.0.1 "
2019-06-07 18:21:24 -04:00
xmlutil . set_prop_path ( obj , hostparam , host )
2019-05-13 14:23:27 -04:00
if port :
2019-06-07 18:21:24 -04:00
xmlutil . set_prop_path ( obj , portparam , port )
2019-05-13 14:23:27 -04:00
class CharSource ( XMLBuilder ) :
XML_NAME = " source "
_XML_PROP_ORDER = [ " bind_host " , " bind_service " ,
" mode " , " connect_host " , " connect_service " ,
" path " , " channel " ]
def set_friendly_connect ( self , val ) :
_set_host_helper ( self , " connect_host " , " connect_service " , val )
def set_friendly_bind ( self , val ) :
_set_host_helper ( self , " bind_host " , " bind_service " , val )
2019-05-13 14:59:33 -04:00
def set_friendly_host ( self , val ) :
_set_host_helper ( self , " host " , " service " , val )
2019-05-13 20:13:42 -04:00
seclabels = XMLChildProperty ( DeviceSeclabel )
2019-05-13 14:23:27 -04:00
2019-05-13 14:59:33 -04:00
host = XMLProperty ( " ./@host " )
service = XMLProperty ( " ./@service " , is_int = True )
2019-05-13 14:23:27 -04:00
path = XMLProperty ( " ./@path " )
channel = XMLProperty ( " ./@channel " )
master = XMLProperty ( " ./@master " )
slave = XMLProperty ( " ./@slave " )
mode = XMLProperty ( " ./@mode " )
2019-05-13 15:14:25 -04:00
# It's weird to track these properties here, since the XML is set on
# the parent, but this is how libvirt does it internally, which means
# everything that shares a charsource has these values too.
protocol = XMLProperty ( " ./../protocol/@type " )
log_file = XMLProperty ( " ./../log/@file " )
log_append = XMLProperty ( " ./../log/@append " , is_onoff = True )
2019-05-13 14:23:27 -04:00
# Convenience source helpers for setting connect/bind host and service
connect_host = XMLProperty ( " ./../source[@mode= ' connect ' ]/@host " )
connect_service = XMLProperty (
" ./../source[@mode= ' connect ' ]/@service " , is_int = True )
bind_host = XMLProperty ( " ./../source[@mode= ' bind ' ]/@host " )
bind_service = XMLProperty ( " ./../source[@mode= ' bind ' ]/@service " , is_int = True )
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class _DeviceChar ( Device ) :
2013-03-17 17:06:52 -04:00
"""
Base class for all character devices . Shouldn ' t be instantiated
directly .
"""
2013-07-16 09:14:37 -04:00
TYPE_PTY = " pty "
TYPE_DEV = " dev "
TYPE_STDIO = " stdio "
TYPE_PIPE = " pipe "
TYPE_FILE = " file "
TYPE_VC = " vc "
TYPE_NULL = " null "
TYPE_TCP = " tcp "
TYPE_UDP = " udp "
TYPE_UNIX = " unix "
TYPE_SPICEVMC = " spicevmc "
2014-03-25 15:42:33 +01:00
TYPE_SPICEPORT = " spiceport "
2016-08-24 16:37:36 -04:00
TYPE_NMDM = " nmdm "
2014-03-25 15:42:33 +01:00
2013-10-05 13:27:11 -04:00
CHANNEL_NAME_SPICE = " com.redhat.spice.0 "
CHANNEL_NAME_QEMUGA = " org.qemu.guest_agent.0 "
CHANNEL_NAME_LIBGUESTFS = " org.libguestfs.channel.0 "
2014-03-25 15:42:33 +01:00
CHANNEL_NAME_SPICE_WEBDAV = " org.spice-space.webdav.0 "
2013-10-05 13:27:11 -04:00
CHANNEL_NAMES = [ CHANNEL_NAME_SPICE ,
CHANNEL_NAME_QEMUGA ,
2014-03-25 15:42:33 +01:00
CHANNEL_NAME_LIBGUESTFS ,
CHANNEL_NAME_SPICE_WEBDAV ]
2013-10-05 13:27:11 -04:00
2014-01-19 13:56:06 -05:00
def set_friendly_target ( self , val ) :
2019-05-13 14:23:27 -04:00
_set_host_helper ( self , " target_address " , " target_port " , val )
2013-07-16 09:14:37 -04:00
2019-05-13 14:23:27 -04:00
_XML_PROP_ORDER = [ " type " , " source " ,
2018-07-04 16:10:38 +08:00
" target_type " , " target_name " , " target_state " ]
2013-07-16 09:14:37 -04:00
2018-02-22 20:44:09 -05:00
type = XMLProperty ( " ./@type " )
2019-05-13 14:23:27 -04:00
source = XMLChildProperty ( CharSource , is_single = True )
2013-09-24 09:25:05 -04:00
2019-05-13 11:31:18 -04:00
target_address = XMLProperty ( " ./target/@address " )
target_port = XMLProperty ( " ./target/@port " , is_int = True )
target_type = XMLProperty ( " ./target/@type " )
target_name = XMLProperty ( " ./target/@name " )
target_state = XMLProperty ( " ./target/@state " )
2019-05-14 11:32:18 -04:00
target_model_name = XMLProperty ( " ./target/model/@name " )
2019-05-13 11:31:18 -04:00
2013-04-13 14:34:52 -04:00
2018-09-03 16:18:03 -04:00
##################
# Default config #
##################
def set_defaults ( self , _guest ) :
2019-05-13 14:23:27 -04:00
if ( not self . source . mode and
2019-05-13 13:18:41 -04:00
self . type in [ self . TYPE_UNIX , self . TYPE_TCP ] ) :
2019-05-13 14:23:27 -04:00
self . source . mode = " bind "
2018-09-03 16:18:03 -04:00
if not self . target_type and self . DEVICE_TYPE == " channel " :
2018-09-06 11:49:09 -04:00
self . target_type = " virtio "
2018-09-03 16:18:03 -04:00
if not self . target_name and self . type == self . TYPE_SPICEVMC :
self . target_name = self . CHANNEL_NAME_SPICE
2018-03-20 12:18:35 -04:00
class DeviceConsole ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " console "
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class DeviceSerial ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " serial "
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class DeviceParallel ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " parallel "
2013-04-13 14:34:52 -04:00
2018-03-20 12:18:35 -04:00
class DeviceChannel ( _DeviceChar ) :
2018-03-21 10:53:34 -04:00
XML_NAME = " channel "