@@ -1246,10 +1246,141 @@ def test_live_migration_with_vcpu_pinning(self):
12461246 assert int (taskset_vcpu0_controller , 16 ) == int (taskset_vcpu0_compute , 16 )
12471247 assert int (taskset_vcpu2_controller , 16 ) == int (taskset_vcpu2_compute , 16 )
12481248
1249+ def test_bdf_implicit_assignment (self ):
1250+ """
1251+ Test if all BDFs are correctly assigned in a scenario where some
1252+ are fixed in the XML and some are assigned by libvirt.
1253+
1254+ The domain XML we use here leaves a slot ID 0x03 free, but
1255+ allocates IDs 0x01, 0x02 and 0x04. 0x01 and 0x02 are dynamically
1256+ assigned by libvirt and not given in the domain XML. As 0x04 is
1257+ the first free ID, we expect this to be selected for the first
1258+ device we add to show that libvirt uses gaps. We add another
1259+ disk to show that all succeeding BDFs would be allocated
1260+ dynamically. Moreover, we show that all BDF assignments
1261+ survive live migration.
1262+ """
1263+
1264+ VIRTIO_NETWORK_DEVICE = "1af4:1041"
1265+ VIRTIO_BLOCK_DEVICE = "1af4:1042"
1266+ VIRTIO_ENTROPY_SOURCE = "1af4:1044"
1267+
1268+ controllerVM .succeed ("virsh define /etc/domain-chv.xml" )
1269+ controllerVM .succeed ("virsh start testvm" )
1270+ controllerVM .succeed ("virsh attach-device testvm /etc/new_interface.xml" )
1271+ assert wait_for_ssh (controllerVM )
1272+
1273+ # Add a disk that receive the first free BDF
1274+ controllerVM .succeed (
1275+ "qemu-img create -f raw /var/lib/libvirt/storage-pools/nfs-share/vdb.img 5M"
1276+ )
1277+ controllerVM .succeed (
1278+ "virsh attach-disk --domain testvm --target vdb --source /var/lib/libvirt/storage-pools/nfs-share/vdb.img"
1279+ )
1280+ controllerVM .succeed (
1281+ "qemu-img create -f raw /var/lib/libvirt/storage-pools/nfs-share/vdc.img 5M"
1282+ )
1283+ controllerVM .succeed (
1284+ "virsh attach-disk --domain testvm --target vdc --source /var/lib/libvirt/storage-pools/nfs-share/vdc.img"
1285+ )
1286+ devices = pci_devices_by_bdf (controllerVM )
1287+ # Implicitly added fixed to 0x01
1288+ assert devices ["00:01.0" ] == VIRTIO_ENTROPY_SOURCE
1289+ # Added by XML; dynamic BDF
1290+ assert devices ["00:02.0" ] == VIRTIO_BLOCK_DEVICE
1291+ # Added by this test
1292+ assert devices ["00:03.0" ] == VIRTIO_BLOCK_DEVICE
1293+ # Defined fixed BDF in XML
1294+ assert devices ["00:04.0" ] == VIRTIO_NETWORK_DEVICE
1295+ # Added by this test
1296+ assert devices ["00:05.0" ] == VIRTIO_BLOCK_DEVICE
1297+ # Defined in 1d to fill gab, no explicit BDF
1298+ assert devices ["00:10.0" ] == VIRTIO_NETWORK_DEVICE
1299+
1300+ # Check that we can reuse the same non-statically allocated BDF
1301+ controllerVM .wait_until_succeeds (
1302+ "virsh detach-disk --domain testvm --target vdb"
1303+ )
1304+ assert pci_devices_by_bdf (controllerVM ).get ("00:03.0" ) is None
1305+ controllerVM .succeed (
1306+ "virsh attach-disk --domain testvm --target vdb --source /var/lib/libvirt/storage-pools/nfs-share/vdb.img"
1307+ )
1308+ assert pci_devices_by_bdf (controllerVM ).get ("00:03.0" ) == VIRTIO_BLOCK_DEVICE
1309+
1310+ devices_before_livemig = pci_devices_by_bdf (controllerVM )
1311+ # Check that BDFs stay the same after migration
1312+ controllerVM .succeed (
1313+ "virsh migrate --domain testvm --desturi ch+tcp://computeVM/session --live --p2p"
1314+ )
1315+ assert wait_for_ssh (computeVM )
1316+ devices_after_livemig = pci_devices_by_bdf (computeVM )
1317+ assert devices_before_livemig == devices_after_livemig
1318+
1319+ def test_bdf_explicit_assignment (self ):
1320+ """
1321+ Test if all BDFs are correctly assigned when binding them
1322+ explicitly to BDFs.
1323+
1324+ This test also shows that we can freely define the BDF that is
1325+ given to the RNG device. Moreover, we show that all BDF
1326+ assignments survive live migration, that allocating the same
1327+ BDF twice fails and that we can reuse BDFs if the respective
1328+ device was detached.
1329+ """
1330+ VIRTIO_NETWORK_DEVICE = "1af4:1041"
1331+ VIRTIO_BLOCK_DEVICE = "1af4:1042"
1332+ VIRTIO_ENTROPY_SOURCE = "1af4:1044"
1333+
1334+ controllerVM .succeed ("virsh define /etc/domain-chv-static-bdf.xml" )
1335+ controllerVM .succeed ("virsh start testvm" )
1336+ controllerVM .succeed ("virsh attach-device testvm /etc/new_interface.xml" )
1337+ controllerVM .succeed (
1338+ "virsh attach-disk --domain testvm --target vdb --source /var/lib/libvirt/storage-pools/nfs-share/cirros.img --address pci:0.0.17.0"
1339+ )
1340+
1341+ assert wait_for_ssh (controllerVM )
1342+
1343+ devices = pci_devices_by_bdf (controllerVM )
1344+ # Defined in 1a (entropy device is implicitly added by libvirt/CHV)
1345+ assert devices ["00:01.0" ] == VIRTIO_BLOCK_DEVICE
1346+ assert devices ["00:04.0" ] == VIRTIO_NETWORK_DEVICE
1347+ assert devices ["00:05.0" ] == VIRTIO_ENTROPY_SOURCE
1348+ assert devices ["00:10.0" ] == VIRTIO_NETWORK_DEVICE
1349+ assert devices ["00:17.0" ] == VIRTIO_BLOCK_DEVICE
1350+
1351+ # Check that BDF is freed and can be reallocated when de-/attaching a (entirely different) device
1352+ controllerVM .wait_until_succeeds (
1353+ "virsh detach-device testvm /etc/new_interface.xml"
1354+ )
1355+ controllerVM .wait_until_succeeds (
1356+ "virsh detach-disk --domain testvm --target vdb"
1357+ )
1358+ assert pci_devices_by_bdf (controllerVM ).get ("00:10.0" ) is None
1359+ assert pci_devices_by_bdf (controllerVM ).get ("00:17.0" ) is None
1360+ controllerVM .succeed (
1361+ "virsh attach-disk --domain testvm --target vdb --source /var/lib/libvirt/storage-pools/nfs-share/cirros.img --address pci:0.0.10.0"
1362+ )
1363+ devices_before_livemig = pci_devices_by_bdf (controllerVM )
1364+ assert devices_before_livemig .get ("00:10.0" ) == VIRTIO_BLOCK_DEVICE
1365+
1366+ # Adding to the same bdf twice fails
1367+ controllerVM .wait_until_fails (
1368+ "virsh attach-device testvm /etc/new_interface.xml"
1369+ )
1370+
1371+ # Check that BDFs stay the same after migration
1372+ controllerVM .succeed (
1373+ "virsh migrate --domain testvm --desturi ch+tcp://computeVM/session --live --p2p"
1374+ )
1375+ assert wait_for_ssh (computeVM )
1376+ devices_after_livemig = pci_devices_by_bdf (computeVM )
1377+ assert devices_before_livemig == devices_after_livemig
1378+
12491379
12501380def suite ():
12511381 # Test cases in alphabetical order
12521382 testcases = [
1383+ LibvirtTests .test_bdf_assignment ,
12531384 LibvirtTests .test_disk_is_locked ,
12541385 LibvirtTests .test_disk_resize_qcow2 ,
12551386 LibvirtTests .test_disk_resize_raw ,
0 commit comments