@@ -299,7 +299,7 @@ def __init__(
299299 self , dev_type , bus , slot , function , iommu_group , numa_node , * ,
300300 vf_ratio = None , multiple_gpu_types = False , generic_types = False ,
301301 parent = None , vend_id = None , vend_name = None , prod_id = None ,
302- prod_name = None , driver_name = None ,
302+ prod_name = None , driver_name = None , mac_address = None ,
303303 ):
304304 """Populate pci devices
305305
@@ -321,6 +321,8 @@ def __init__(
321321 :param prod_id: (str) The product ID.
322322 :param prod_name: (str) The product name.
323323 :param driver_name: (str) The driver name.
324+ :param mac_address: (str) The MAC of the device.
325+ Used in case of SRIOV PFs
324326 """
325327
326328 self .dev_type = dev_type
@@ -339,6 +341,7 @@ def __init__(
339341 self .prod_id = prod_id
340342 self .prod_name = prod_name
341343 self .driver_name = driver_name
344+ self .mac_address = mac_address
342345
343346 self .generate_xml ()
344347
@@ -352,7 +355,9 @@ def generate_xml(self, skip_capability=False):
352355 assert not self .vf_ratio , 'vf_ratio does not apply for PCI devices'
353356
354357 if self .dev_type in ('PF' , 'VF' ):
355- assert self .vf_ratio , 'require vf_ratio for PFs and VFs'
358+ assert (
359+ self .vf_ratio is not None
360+ ), 'require vf_ratio for PFs and VFs'
356361
357362 if self .dev_type == 'VF' :
358363 assert self .parent , 'require parent for VFs'
@@ -460,6 +465,10 @@ def generate_xml(self, skip_capability=False):
460465 def XMLDesc (self , flags ):
461466 return self .pci_device
462467
468+ @property
469+ def address (self ):
470+ return "0000:%02x:%02x.%1x" % (self .bus , self .slot , self .function )
471+
463472
464473# TODO(stephenfin): Remove all of these HostFooDevicesInfo objects in favour of
465474# a unified devices object
@@ -572,7 +581,7 @@ def add_device(
572581 self , dev_type , bus , slot , function , iommu_group , numa_node ,
573582 vf_ratio = None , multiple_gpu_types = False , generic_types = False ,
574583 parent = None , vend_id = None , vend_name = None , prod_id = None ,
575- prod_name = None , driver_name = None ,
584+ prod_name = None , driver_name = None , mac_address = None ,
576585 ):
577586 pci_dev_name = _get_libvirt_nodedev_name (bus , slot , function )
578587
@@ -593,7 +602,9 @@ def add_device(
593602 vend_name = vend_name ,
594603 prod_id = prod_id ,
595604 prod_name = prod_name ,
596- driver_name = driver_name )
605+ driver_name = driver_name ,
606+ mac_address = mac_address ,
607+ )
597608 self .devices [pci_dev_name ] = dev
598609 return dev
599610
@@ -612,6 +623,13 @@ def get_all_mdev_capable_devices(self):
612623 return [dev for dev in self .devices
613624 if self .devices [dev ].is_capable_of_mdevs ]
614625
626+ def get_pci_address_mac_mapping (self ):
627+ return {
628+ device .address : device .mac_address
629+ for dev_addr , device in self .devices .items ()
630+ if device .mac_address
631+ }
632+
615633
616634class FakeMdevDevice (object ):
617635 template = """
@@ -2141,6 +2159,15 @@ class LibvirtFixture(fixtures.Fixture):
21412159 """
21422160 def __init__ (self , stub_os_vif = True ):
21432161 self .stub_os_vif = stub_os_vif
2162+ self .pci_address_to_mac_map = collections .defaultdict (
2163+ lambda : '52:54:00:1e:59:c6' )
2164+
2165+ def update_sriov_mac_address_mapping (self , pci_address_to_mac_map ):
2166+ self .pci_address_to_mac_map .update (pci_address_to_mac_map )
2167+
2168+ def fake_get_mac_by_pci_address (self , pci_addr , pf_interface = False ):
2169+ res = self .pci_address_to_mac_map [pci_addr ]
2170+ return res
21442171
21452172 def setUp (self ):
21462173 super ().setUp ()
@@ -2162,6 +2189,10 @@ def setUp(self):
21622189 'nova.pci.utils.get_ifname_by_pci_address' ,
21632190 return_value = 'fake_pf_interface_name' ))
21642191
2192+ self .useFixture (fixtures .MockPatch (
2193+ 'nova.pci.utils.get_mac_by_pci_address' ,
2194+ new = self .fake_get_mac_by_pci_address ))
2195+
21652196 # libvirt calls out to sysfs to get the vfs ID during macvtap plug
21662197 self .useFixture (fixtures .MockPatch (
21672198 'nova.pci.utils.get_vf_num_by_pci_address' , return_value = 1 ))
0 commit comments