2013-03-17 17:06:52 -04:00
#
# Base class for all VM devices
#
2013-10-27 21:59:47 +01:00
# Copyright 2008, 2013 Red Hat, Inc.
2013-03-17 17:06:52 -04:00
# Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
2013-10-27 21:59:47 +01:00
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
2013-03-17 17:06:52 -04:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
2014-09-12 15:59:22 -04:00
from . xmlbuilder import XMLBuilder , XMLChildProperty , XMLProperty
2013-09-11 11:47:09 -04:00
class VirtualDeviceAlias ( XMLBuilder ) :
_XML_ROOT_NAME = " alias "
2013-09-19 13:27:30 -04:00
name = XMLProperty ( " ./@name " )
2013-09-11 11:47:09 -04:00
2014-02-09 13:36:12 -05:00
class VirtualDeviceBoot ( XMLBuilder ) :
_XML_ROOT_NAME = " boot "
order = XMLProperty ( " ./@order " , is_int = True )
2013-09-11 11:47:09 -04:00
class VirtualDeviceAddress ( XMLBuilder ) :
"""
Examples :
< address type = ' pci ' domain = ' 0x0000 ' bus = ' 0x00 ' slot = ' 0x04 ' function = ' 0x0 ' / >
< address type = ' drive ' controller = ' 0 ' bus = ' 0 ' unit = ' 0 ' / >
< address type = ' ccid ' controller = ' 0 ' slot = ' 0 ' / >
< address type = ' virtio-serial ' controller = ' 1 ' bus = ' 0 ' port = ' 4 ' / >
"""
ADDRESS_TYPE_PCI = " pci "
ADDRESS_TYPE_DRIVE = " drive "
ADDRESS_TYPE_VIRTIO_SERIAL = " virtio-serial "
ADDRESS_TYPE_CCID = " ccid "
ADDRESS_TYPE_SPAPR_VIO = " spapr-vio "
TYPES = [ ADDRESS_TYPE_PCI , ADDRESS_TYPE_DRIVE ,
ADDRESS_TYPE_VIRTIO_SERIAL , ADDRESS_TYPE_CCID ,
ADDRESS_TYPE_SPAPR_VIO ]
_XML_ROOT_NAME = " address "
_XML_PROP_ORDER = [ " type " , " domain " , " controller " , " bus " , " slot " ,
" function " , " target " , " unit " , " multifunction " ]
def set_addrstr ( self , addrstr ) :
if addrstr is None :
return
if addrstr . count ( " : " ) in [ 1 , 2 ] and addrstr . count ( " . " ) :
self . type = self . ADDRESS_TYPE_PCI
addrstr , self . function = addrstr . split ( " . " , 1 )
addrstr , self . slot = addrstr . rsplit ( " : " , 1 )
self . domain = " 0 "
if addrstr . count ( " : " ) :
self . domain , self . bus = addrstr . split ( " : " , 1 )
elif addrstr == " spapr-vio " :
self . type = self . ADDRESS_TYPE_SPAPR_VIO
else :
raise ValueError ( _ ( " Could not determine or unsupported "
" format of ' %s ' " ) % addrstr )
2017-11-23 17:32:45 +08:00
def compare_controller ( self , controller , dev_bus ) :
if ( controller . type == dev_bus and
controller . index == self . controller ) :
return True
return False
2013-09-11 11:47:09 -04:00
2013-09-19 13:27:30 -04:00
type = XMLProperty ( " ./@type " )
2016-06-11 12:36:45 -04:00
# type=pci
2013-09-19 13:27:30 -04:00
domain = XMLProperty ( " ./@domain " , is_int = True )
bus = XMLProperty ( " ./@bus " , is_int = True )
slot = XMLProperty ( " ./@slot " , is_int = True )
function = XMLProperty ( " ./@function " , is_int = True )
2016-06-11 12:36:45 -04:00
multifunction = XMLProperty ( " ./@multifunction " , is_onoff = True )
# type=drive
2013-09-19 13:27:30 -04:00
controller = XMLProperty ( " ./@controller " , is_int = True )
unit = XMLProperty ( " ./@unit " , is_int = True )
port = XMLProperty ( " ./@port " , is_int = True )
target = XMLProperty ( " ./@target " , is_int = True )
2016-06-11 12:36:45 -04:00
# type=spapr-vio
reg = XMLProperty ( " ./@reg " )
# type=ccw
cssid = XMLProperty ( " ./@cssid " )
ssid = XMLProperty ( " ./@ssid " )
devno = XMLProperty ( " ./@devno " )
# type=isa
iobase = XMLProperty ( " ./@iobase " )
irq = XMLProperty ( " ./@irq " )
# type=dimm
base = XMLProperty ( " ./@base " )
2013-03-17 17:06:52 -04:00
2013-04-13 14:34:52 -04:00
2013-07-13 18:56:09 -04:00
class VirtualDevice ( XMLBuilder ) :
2013-03-17 17:06:52 -04:00
"""
Base class for all domain xml device objects .
"""
VIRTUAL_DEV_DISK = " disk "
VIRTUAL_DEV_NET = " interface "
VIRTUAL_DEV_INPUT = " input "
VIRTUAL_DEV_GRAPHICS = " graphics "
VIRTUAL_DEV_AUDIO = " sound "
VIRTUAL_DEV_HOSTDEV = " hostdev "
VIRTUAL_DEV_SERIAL = " serial "
VIRTUAL_DEV_PARALLEL = " parallel "
VIRTUAL_DEV_CHANNEL = " channel "
VIRTUAL_DEV_CONSOLE = " console "
VIRTUAL_DEV_VIDEO = " video "
VIRTUAL_DEV_CONTROLLER = " controller "
VIRTUAL_DEV_WATCHDOG = " watchdog "
VIRTUAL_DEV_FILESYSTEM = " filesystem "
VIRTUAL_DEV_SMARTCARD = " smartcard "
VIRTUAL_DEV_REDIRDEV = " redirdev "
VIRTUAL_DEV_MEMBALLOON = " memballoon "
2013-06-25 21:45:06 -04:00
VIRTUAL_DEV_TPM = " tpm "
2013-09-18 15:29:28 +02:00
VIRTUAL_DEV_RNG = " rng "
2014-01-10 17:37:54 +08:00
VIRTUAL_DEV_PANIC = " panic "
2017-05-05 11:50:06 +02:00
VIRTUAL_DEV_MEMORY = " memory "
2013-03-17 17:06:52 -04:00
# Ordering in this list is important: it will be the order the
# Guest class outputs XML. So changing this may upset the test suite
virtual_device_types = [ VIRTUAL_DEV_DISK ,
VIRTUAL_DEV_CONTROLLER ,
VIRTUAL_DEV_FILESYSTEM ,
VIRTUAL_DEV_NET ,
VIRTUAL_DEV_INPUT ,
VIRTUAL_DEV_GRAPHICS ,
VIRTUAL_DEV_SERIAL ,
VIRTUAL_DEV_PARALLEL ,
VIRTUAL_DEV_CONSOLE ,
VIRTUAL_DEV_CHANNEL ,
VIRTUAL_DEV_AUDIO ,
VIRTUAL_DEV_VIDEO ,
VIRTUAL_DEV_HOSTDEV ,
VIRTUAL_DEV_WATCHDOG ,
VIRTUAL_DEV_SMARTCARD ,
VIRTUAL_DEV_REDIRDEV ,
2013-06-25 21:45:06 -04:00
VIRTUAL_DEV_MEMBALLOON ,
2013-09-18 15:29:28 +02:00
VIRTUAL_DEV_TPM ,
2014-01-10 17:37:54 +08:00
VIRTUAL_DEV_RNG ,
2017-05-05 11:50:06 +02:00
VIRTUAL_DEV_PANIC ,
VIRTUAL_DEV_MEMORY ]
2013-03-17 17:06:52 -04:00
2013-07-24 08:46:55 -04:00
virtual_device_classes = { }
@classmethod
2013-07-24 11:32:30 -04:00
def register_type ( cls ) :
2013-09-11 11:47:09 -04:00
cls . _XML_ROOT_NAME = cls . virtual_device_type
2013-07-24 11:32:30 -04:00
VirtualDevice . virtual_device_classes [ cls . virtual_device_type ] = cls
2013-07-24 08:46:55 -04:00
2013-03-17 17:06:52 -04:00
# General device type (disk, interface, etc.)
2013-07-16 09:14:37 -04:00
virtual_device_type = None
2013-03-17 17:06:52 -04:00
2013-09-11 11:47:09 -04:00
def __init__ ( self , * args , * * kwargs ) :
2013-03-17 17:06:52 -04:00
"""
Initialize device state
@param conn : libvirt connection to validate device against
"""
2013-09-11 11:47:09 -04:00
XMLBuilder . __init__ ( self , * args , * * kwargs )
2013-07-17 17:58:24 -04:00
self . _XML_PROP_ORDER = self . _XML_PROP_ORDER + [ " alias " , " address " ]
2013-03-17 17:06:52 -04:00
2013-07-16 09:14:37 -04:00
if not self . virtual_device_type :
2013-03-17 17:06:52 -04:00
raise ValueError ( _ ( " Virtual device type must be set in subclass. " ) )
2013-07-16 09:14:37 -04:00
if self . virtual_device_type not in self . virtual_device_types :
2013-03-17 17:06:52 -04:00
raise ValueError ( _ ( " Unknown virtual device type ' %s ' . " ) %
2013-07-16 09:14:37 -04:00
self . virtual_device_type )
2013-03-17 17:06:52 -04:00
2013-09-11 11:47:09 -04:00
alias = XMLChildProperty ( VirtualDeviceAlias , is_single = True )
address = XMLChildProperty ( VirtualDeviceAddress , is_single = True )
2014-02-09 13:36:12 -05:00
boot = XMLChildProperty ( VirtualDeviceBoot , is_single = True )
2013-09-11 11:47:09 -04:00
2013-03-17 17:06:52 -04:00
2013-07-05 20:14:57 -04:00
def setup ( self , meter = None ) :
2013-03-17 17:06:52 -04:00
"""
Perform potentially hazardous device initialization , like
storage creation or host device reset
@param meter : Optional progress meter to use
"""
# Will be overwritten by subclasses if necessary.
ignore = meter
return