2013-03-18 01:06:52 +04:00
#
# Copyright 2009 Red Hat, Inc.
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
2013-08-09 04:47:17 +04:00
from virtinst import VirtualDevice
2013-07-16 01:45:43 +04:00
from virtinst import NodeDeviceParser
2013-08-09 04:47:17 +04:00
from virtinst . xmlbuilder import XMLProperty
2013-03-18 01:06:52 +04:00
2013-04-13 22:34:52 +04:00
2013-04-11 03:48:07 +04:00
class VirtualHostDevice ( VirtualDevice ) :
2013-07-16 17:14:37 +04:00
virtual_device_type = VirtualDevice . VIRTUAL_DEV_HOSTDEV
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
@staticmethod
def device_from_node ( conn , name = None , nodedev = None , is_dup = False ,
dev = None ) :
2013-03-18 01:06:52 +04:00
"""
Convert the passed device name to a VirtualHostDevice
instance , with proper error reporting . Name can be any of the
values accepted by NodeDeviceParser . lookupNodeName . If a node
device name is not specified , a virtinst . NodeDevice instance can
be passed in to create a dev from .
@param conn : libvirt . virConnect instance to perform the lookup on
@param name : optional libvirt node device name to lookup
@param nodedev : optional L { virtinst . NodeDevice } instance to use
@rtype : L { virtinst . VirtualHostDevice } instance
"""
if not name and not nodedev :
2013-07-16 01:45:43 +04:00
raise ValueError ( " ' name ' or ' nodedev ' required. " )
2013-03-18 01:06:52 +04:00
if nodedev :
nodeinst = nodedev
else :
2013-04-30 18:53:11 +04:00
nodeinst , addr_type = NodeDeviceParser . lookupNodeName ( conn , name )
if addr_type == NodeDeviceParser . HOSTDEV_ADDR_TYPE_USB_BUSADDR :
is_dup = True
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
if isinstance ( nodeinst , NodeDeviceParser . NetDevice ) :
2013-03-18 01:06:52 +04:00
parentname = nodeinst . parent
2013-07-16 01:45:43 +04:00
return VirtualHostDevice . device_from_node ( conn ,
name = parentname )
if not dev :
dev = VirtualHostDevice ( conn )
dev . set_from_nodedev ( nodeinst , is_dup )
return dev
def set_from_nodedev ( self , nodedev , is_dup = False ) :
if isinstance ( nodedev , NodeDeviceParser . PCIDevice ) :
self . type = " pci "
self . domain = nodedev . domain
2013-03-18 01:06:52 +04:00
self . bus = nodedev . bus
2013-07-16 01:45:43 +04:00
self . slot = nodedev . slot
self . function = nodedev . function
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
elif isinstance ( nodedev , NodeDeviceParser . USBDevice ) :
self . type = " usb "
self . vendor = nodedev . vendor_id
self . product = nodedev . product_id
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
if is_dup :
self . bus = nodedev . bus
self . device = nodedev . device
else :
raise ValueError ( " Unknown node device type %s " % nodedev )
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
_XML_PROP_ORDER = [ " mode " , " type " , " managed " , " vendor " , " product " ,
" domain " , " bus " , " slot " , " function " ]
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
mode = XMLProperty ( xpath = " ./@mode " , default_cb = lambda s : " subsystem " )
type = XMLProperty ( xpath = " ./@type " )
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
def _get_default_managed ( self ) :
return self . conn . is_xen ( ) and " no " or " yes "
2013-07-16 21:04:24 +04:00
managed = XMLProperty ( xpath = " ./@managed " , is_yesno = True ,
default_cb = _get_default_managed )
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
vendor = XMLProperty ( xpath = " ./source/vendor/@id " )
product = XMLProperty ( xpath = " ./source/product/@id " )
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
device = XMLProperty ( xpath = " ./source/address/@device " )
bus = XMLProperty ( xpath = " ./source/address/@bus " )
2013-03-18 01:06:52 +04:00
2013-07-16 01:45:43 +04:00
def _get_default_domain ( self ) :
if self . type == " pci " :
return " 0x0 "
return None
domain = XMLProperty ( xpath = " ./source/address/@domain " ,
default_cb = _get_default_domain )
function = XMLProperty ( xpath = " ./source/address/@function " )
slot = XMLProperty ( xpath = " ./source/address/@slot " )
2013-07-24 16:46:55 +04:00
VirtualHostDevice . register_type ( )