diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index bdc87c7c2e..21e49f234d 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -2364,6 +2364,43 @@ qemuBuildControllerDevStr(const virDomainDef *domainDef, modelName, def->opts.pciopts.chassisNr, def->info.alias); break; + case VIR_DOMAIN_CONTROLLER_MODEL_PCI_EXPANDER_BUS: + if (def->opts.pciopts.modelName + == VIR_DOMAIN_CONTROLLER_PCI_MODEL_NAME_NONE || + def->opts.pciopts.busNr == -1) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("autogenerated pci-expander-bus options not set")); + goto error; + } + + modelName = virDomainControllerPCIModelNameTypeToString(def->opts.pciopts.modelName); + if (!modelName) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("unknown pci-expander-bus model name value %d"), + def->opts.pciopts.modelName); + goto error; + } + if (def->opts.pciopts.modelName + != VIR_DOMAIN_CONTROLLER_PCI_MODEL_NAME_PXB) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("PCI controller model name '%s' " + "is not valid for a pci-expander-bus"), + modelName); + goto error; + } + if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_PXB)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("the pxb controller " + "is not supported in this QEMU binary")); + goto error; + } + virBufferAsprintf(&buf, "%s,bus_nr=%d,id=%s", + modelName, def->opts.pciopts.busNr, + def->info.alias); + if (def->opts.pciopts.numaNode != -1) + virBufferAsprintf(&buf, ",numa_node=%d", + def->opts.pciopts.numaNode); + break; case VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE: if (def->opts.pciopts.modelName == VIR_DOMAIN_CONTROLLER_PCI_MODEL_NAME_NONE) { diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 5d54fffcfb..5e11e8553d 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -1887,6 +1887,39 @@ qemuDomainDeviceDefPostParse(virDomainDeviceDefPtr dev, dev->data.panic->model = VIR_DOMAIN_PANIC_MODEL_ISA; } + + if (dev->type == VIR_DOMAIN_DEVICE_CONTROLLER) { + virDomainControllerDefPtr cont = dev->data.controller; + + if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI && + cont->model == VIR_DOMAIN_CONTROLLER_MODEL_PCI_EXPANDER_BUS) { + + if (!qemuDomainMachineIsI440FX(def)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("pci-expander-bus controllers are only supported " + "on 440fx-based machinetypes")); + goto cleanup; + } + + /* if a PCI expander bus has a NUMA node set, make sure + * that NUMA node is configured in the guest + * array. NUMA cell id's in this array are numbered + * from 0 .. size-1. + */ + if ((int) virDomainNumaGetNodeCount(def->numa) + <= cont->opts.pciopts.numaNode) { + virReportError(VIR_ERR_XML_ERROR, + _("pci-expander-bus with index %d is " + "configured for a NUMA node (%d) " + "not present in the domain's " + " array (%zu)"), + cont->idx, cont->opts.pciopts.numaNode, + virDomainNumaGetNodeCount(def->numa)); + goto cleanup; + } + } + } + ret = 0; cleanup: diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c index d336b4b90f..a806355dda 100644 --- a/src/qemu/qemu_domain_address.c +++ b/src/qemu/qemu_domain_address.c @@ -1373,6 +1373,8 @@ qemuDomainPCIControllerSetDefaultModelName(virDomainControllerDefPtr cont) *modelName = VIR_DOMAIN_CONTROLLER_PCI_MODEL_NAME_XIO3130_DOWNSTREAM; break; case VIR_DOMAIN_CONTROLLER_MODEL_PCI_EXPANDER_BUS: + *modelName = VIR_DOMAIN_CONTROLLER_PCI_MODEL_NAME_PXB; + break; case VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT: case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT: case VIR_DOMAIN_CONTROLLER_MODEL_PCI_LAST: @@ -1381,6 +1383,74 @@ qemuDomainPCIControllerSetDefaultModelName(virDomainControllerDefPtr cont) } +static int +qemuDomainAddressFindNewBusNr(virDomainDefPtr def) +{ +/* Try to find a nice default for busNr for a new pci-expander-bus. + * This is a bit tricky, since you need to satisfy the following: + * + * 1) There need to be enough unused bus numbers between busNr of this + * bus and busNr of the next highest bus for the guest to assign a + * unique bus number to each PCI bus that is a child of this + * bus. Each PCI controller. On top of this, the pxb device (which + * implements the pci-extender-bus) includes a pci-bridge within + * it, and that bridge also uses one bus number (so each pxb device + * requires at least 2 bus numbers). + * + * 2) There need to be enough bus numbers *below* this for all the + * child controllers of the pci-expander-bus with the next lower + * busNr (or the pci-root bus if there are no lower + * pci-expander-buses). + * + * 3) If at all possible, we want to avoid needing to change the busNr + * of a bus in the future, as that changes the guest's device ABI, + * which could potentially lead to issues with a guest OS that is + * picky about such things. + * + * Due to the impossibility of predicting what might be added to the + * config in the future, we can't make a foolproof choice, but since + * a pci-expander-bus (pxb) has slots for 32 devices, and the only + * practical use for it is to assign real devices on a particular + * NUMA node in the host, it's reasonably safe to assume it should + * never need any additional child buses (probably only a few of the + * 32 will ever be used). So for pci-expander-bus we find the lowest + * existing busNr, and set this one to the current lowest - 2 (one + * for the pxb, one for the intergrated pci-bridge), thus leaving the + * maximum possible bus numbers available for other buses plugged + * into pci-root (i.e. pci-bridges and other + * pci-expander-buses). Anyone who needs more than 32 devices + * descended from one pci-expander-bus should set the busNr manually + * in the config. + * + * There is room for more error checking here - in particular we + * can/should determine the ultimate parent (root-bus) of each PCI + * controller and determine if there is enough space for all the + * buses within the current range allotted to the bus just prior to + * this one. + */ + size_t i; + int lowestBusNr = 256; + + for (i = 0; i < def->ncontrollers; i++) { + if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) { + int thisBusNr = def->controllers[i]->opts.pciopts.busNr; + + if (thisBusNr >= 0 && thisBusNr < lowestBusNr) + lowestBusNr = thisBusNr; + } + } + + /* If we already have a busNR = 1, then we can't auto-assign (0 is + * the pci[e]-root, and the others may have been assigned + * purposefully). + */ + if (lowestBusNr <= 2) + return -1; + + return lowestBusNr - 2; +} + + static int qemuDomainAssignPCIAddresses(virDomainDefPtr def, virQEMUCapsPtr qemuCaps, @@ -1509,6 +1579,18 @@ qemuDomainAssignPCIAddresses(virDomainDefPtr def, options->port = addr->slot; break; case VIR_DOMAIN_CONTROLLER_MODEL_PCI_EXPANDER_BUS: + if (options->busNr == -1) + options->busNr = qemuDomainAddressFindNewBusNr(def); + if (options->busNr == -1) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("No free busNr lower than current " + "lowest busNr is available to " + "auto-assign to bus %d. Must be " + "manually assigned"), + addr->bus); + goto cleanup; + } + break; case VIR_DOMAIN_CONTROLLER_MODEL_DMI_TO_PCI_BRIDGE: case VIR_DOMAIN_CONTROLLER_MODEL_PCIE_SWITCH_UPSTREAM_PORT: case VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT: diff --git a/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus-bad-machine.xml b/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus-bad-machine.xml new file mode 100644 index 0000000000..606ddfd73c --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus-bad-machine.xml @@ -0,0 +1,167 @@ + + expander-test + 3ec6cbe1-b5a2-4515-b800-31a61855df41 + 219100 + 219100 + 16 + + hvm + + + + + + + + + + /usr/bin/qemu-system-x86_64 + + +
+ + + + + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + diff --git a/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus-bad-node.xml b/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus-bad-node.xml new file mode 100644 index 0000000000..2c390eae8c --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus-bad-node.xml @@ -0,0 +1,160 @@ + + expander-test + 3ec6cbe1-b5a2-4515-b800-31a61855df41 + 219100 + 219100 + 16 + + hvm + + + /usr/bin/qemu-system-x86_64 + + +
+ + + + + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + diff --git a/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus.args b/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus.args new file mode 100644 index 0000000000..268a3014bd --- /dev/null +++ b/tests/qemuxml2argvdata/qemuxml2argv-pci-expander-bus.args @@ -0,0 +1,87 @@ +LC_ALL=C \ +PATH=/bin \ +HOME=/home/test \ +USER=test \ +LOGNAME=test \ +QEMU_AUDIO_DRV=none \ +/usr/bin/qemu-system-x86_64 \ +-name expander-test \ +-S \ +-M pc-i440fx-2.5 \ +-m 214 \ +-smp 16 \ +-numa node,nodeid=0,cpus=0-7,mem=107 \ +-numa node,nodeid=1,cpus=8-15,mem=107 \ +-uuid 3ec6cbe1-b5a2-4515-b800-31a61855df41 \ +-nographic \ +-nodefaults \ +-monitor unix:/tmp/lib/domain--1-expander-test/monitor.sock,server,nowait \ +-no-acpi \ +-boot c \ +-device pxb,bus_nr=254,id=pci.1,numa_node=1,bus=pci.0,addr=0x3 \ +-device pxb,bus_nr=252,id=pci.2,bus=pci.0,addr=0x4 \ +-device rtl8139,vlan=0,id=net0,mac=52:54:00:f1:95:51,bus=pci.0,addr=0x5 \ +-net user,vlan=0,name=hostnet0 \ +-device e1000,vlan=1,id=net1,mac=52:54:00:5c:c6:1a,bus=pci.0,addr=0x7 \ +-net user,vlan=1,name=hostnet1 \ +-device e1000,vlan=2,id=net2,mac=52:54:00:39:97:ac,bus=pci.0,addr=0x8 \ +-net user,vlan=2,name=hostnet2 \ +-device e1000,vlan=3,id=net3,mac=52:54:00:45:28:cb,bus=pci.0,addr=0x9 \ +-net user,vlan=3,name=hostnet3 \ +-device e1000,vlan=4,id=net4,mac=52:54:00:ee:b9:a8,bus=pci.0,addr=0xa \ +-net user,vlan=4,name=hostnet4 \ +-device e1000,vlan=5,id=net5,mac=52:54:00:a9:f7:17,bus=pci.0,addr=0xb \ +-net user,vlan=5,name=hostnet5 \ +-device e1000,vlan=6,id=net6,mac=52:54:00:df:2b:f3,bus=pci.0,addr=0xc \ +-net user,vlan=6,name=hostnet6 \ +-device e1000,vlan=7,id=net7,mac=52:54:00:78:94:b4,bus=pci.0,addr=0xd \ +-net user,vlan=7,name=hostnet7 \ +-device e1000,vlan=8,id=net8,mac=52:54:00:6b:9b:06,bus=pci.0,addr=0xe \ +-net user,vlan=8,name=hostnet8 \ +-device e1000,vlan=9,id=net9,mac=52:54:00:17:df:bc,bus=pci.0,addr=0xf \ +-net user,vlan=9,name=hostnet9 \ +-device e1000,vlan=10,id=net10,mac=52:54:00:3b:d0:51,bus=pci.0,addr=0x10 \ +-net user,vlan=10,name=hostnet10 \ +-device e1000,vlan=11,id=net11,mac=52:54:00:8d:2d:17,bus=pci.0,addr=0x11 \ +-net user,vlan=11,name=hostnet11 \ +-device e1000,vlan=12,id=net12,mac=52:54:00:a7:66:af,bus=pci.0,addr=0x12 \ +-net user,vlan=12,name=hostnet12 \ +-device e1000,vlan=13,id=net13,mac=52:54:00:54:ab:d7,bus=pci.0,addr=0x13 \ +-net user,vlan=13,name=hostnet13 \ +-device e1000,vlan=14,id=net14,mac=52:54:00:1f:99:90,bus=pci.0,addr=0x14 \ +-net user,vlan=14,name=hostnet14 \ +-device e1000,vlan=15,id=net15,mac=52:54:00:c8:43:87,bus=pci.0,addr=0x15 \ +-net user,vlan=15,name=hostnet15 \ +-device e1000,vlan=16,id=net16,mac=52:54:00:df:22:b2,bus=pci.0,addr=0x16 \ +-net user,vlan=16,name=hostnet16 \ +-device e1000,vlan=17,id=net17,mac=52:54:00:d2:9a:47,bus=pci.0,addr=0x17 \ +-net user,vlan=17,name=hostnet17 \ +-device e1000,vlan=18,id=net18,mac=52:54:00:86:05:e2,bus=pci.0,addr=0x18 \ +-net user,vlan=18,name=hostnet18 \ +-device e1000,vlan=19,id=net19,mac=52:54:00:8c:1c:c2,bus=pci.0,addr=0x19 \ +-net user,vlan=19,name=hostnet19 \ +-device e1000,vlan=20,id=net20,mac=52:54:00:48:58:92,bus=pci.0,addr=0x1a \ +-net user,vlan=20,name=hostnet20 \ +-device e1000,vlan=21,id=net21,mac=52:54:00:99:e5:bf,bus=pci.0,addr=0x1b \ +-net user,vlan=21,name=hostnet21 \ +-device e1000,vlan=22,id=net22,mac=52:54:00:b1:8c:25,bus=pci.0,addr=0x1c \ +-net user,vlan=22,name=hostnet22 \ +-device e1000,vlan=23,id=net23,mac=52:54:00:60:e0:d0,bus=pci.0,addr=0x1d \ +-net user,vlan=23,name=hostnet23 \ +-device e1000,vlan=24,id=net24,mac=52:54:00:37:00:6a,bus=pci.0,addr=0x1e \ +-net user,vlan=24,name=hostnet24 \ +-device e1000,vlan=25,id=net25,mac=52:54:00:c7:c8:ad,bus=pci.0,addr=0x1f \ +-net user,vlan=25,name=hostnet25 \ +-device e1000,vlan=26,id=net26,mac=52:54:00:4e:a7:cf,bus=pci.1,addr=0x0 \ +-net user,vlan=26,name=hostnet26 \ +-device e1000,vlan=27,id=net27,mac=52:54:00:00:79:69,bus=pci.1,addr=0x1 \ +-net user,vlan=27,name=hostnet27 \ +-device e1000,vlan=28,id=net28,mac=52:54:00:47:00:6f,bus=pci.1,addr=0x2 \ +-net user,vlan=28,name=hostnet28 \ +-device e1000,vlan=29,id=net29,mac=52:54:00:2a:8c:8b,bus=pci.1,addr=0x3 \ +-net user,vlan=29,name=hostnet29 \ +-device e1000,vlan=30,id=net30,mac=52:54:00:ec:d5:e3,bus=pci.1,addr=0x4 \ +-net user,vlan=30,name=hostnet30 \ +-device e1000,vlan=31,id=net31,mac=52:54:00:7e:6e:c8,bus=pci.1,addr=0x5 \ +-net user,vlan=31,name=hostnet31 \ +-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6 diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 39443e22d8..a47d653ef0 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -1562,6 +1562,16 @@ mymain(void) QEMU_CAPS_DEVICE_VIDEO_PRIMARY, QEMU_CAPS_VGA_QXL, QEMU_CAPS_DEVICE_QXL); + DO_TEST("pci-expander-bus", + QEMU_CAPS_DEVICE_PCI_BRIDGE, + QEMU_CAPS_DEVICE_PXB); + DO_TEST_PARSE_ERROR("pci-expander-bus-bad-node", + QEMU_CAPS_DEVICE_PCI_BRIDGE, + QEMU_CAPS_DEVICE_PXB); + DO_TEST_PARSE_ERROR("pci-expander-bus-bad-machine", + QEMU_CAPS_DEVICE_PCI_BRIDGE, + QEMU_CAPS_DEVICE_PXB); + DO_TEST("hostdev-scsi-lsi", QEMU_CAPS_VIRTIO_SCSI, QEMU_CAPS_SCSI_LSI, QEMU_CAPS_DEVICE_SCSI_GENERIC); diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-pci-expander-bus.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-pci-expander-bus.xml index 946e94f0f0..e0d787e57c 100644 --- a/tests/qemuxml2xmloutdata/qemuxml2xmlout-pci-expander-bus.xml +++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-pci-expander-bus.xml @@ -35,6 +35,7 @@ +