diff --git a/include/HostSharePCI.h b/include/HostSharePCI.h index 35c5a6c1b9..7881894ad0 100644 --- a/include/HostSharePCI.h +++ b/include/HostSharePCI.h @@ -131,6 +131,11 @@ public: * - VM_FUNCTION: 0 * - VM_ADDRESS: BUS:SLOT.0 * + * When the machine type is q35, SLOT needs to be 0, and devices are attached + * to a different bus. Use param slot_index to select this behavior + * - VM_SLOT: 0 + * - VM_BUS: PCI_ID + 1 + * * Cleans internal attributes: * - NUMA_NODE * - UUID @@ -139,10 +144,11 @@ public: * @param pci_device to set the address in * @param default_bus if not set in PCI attribute (PCI_PASSTHROUGH_BUS * in oned.conf) + * @param bus_index when true devices uses slot = 0 and bus = pci_id + 1 * @return -1 if wrong bus 0 on success */ static int set_pci_address(VectorAttribute * pci_device, const std::string& dbus, - bool clean); + bool bus_index, bool clean); private: /** diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index 788960cf56..b2f49e6bb8 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -345,7 +345,7 @@ public: { VectorAttribute * os = obj_template->get("OS"); - if ( os == 0 ) + if ( os == nullptr ) { return; } @@ -362,7 +362,7 @@ public: { VectorAttribute * os = obj_template->get("OS"); - if ( os == 0 ) + if ( os == nullptr ) { return; } @@ -370,6 +370,20 @@ public: os->replace("INITRD", initrd); }; + bool test_machine_type(const std::string& machine_type) const + { + VectorAttribute * os = obj_template->get("OS"); + + if ( os == nullptr ) + { + return false; + } + + const std::string machine = os->vector_value("MACHINE"); + + return machine.find(machine_type) != std::string::npos; + } + // ------------------------------------------------------------------------ // Access to VM locations // ------------------------------------------------------------------------ diff --git a/src/host/HostSharePCI.cc b/src/host/HostSharePCI.cc index b78a936e0d..7e90890346 100644 --- a/src/host/HostSharePCI.cc +++ b/src/host/HostSharePCI.cc @@ -513,7 +513,7 @@ int HostSharePCI::get_pci_value(const char * name, /* ------------------------------------------------------------------------*/ int HostSharePCI::set_pci_address(VectorAttribute * pci_device, - const string& dbus, bool clean) + const string& dbus, bool bus_index, bool clean) { string bus; ostringstream oss; @@ -553,21 +553,28 @@ int HostSharePCI::set_pci_address(VectorAttribute * pci_device, return -1; } - oss << showbase << internal << setfill('0') << hex << setw(4) << ibus; - - pci_device->replace("VM_BUS", oss.str()); - // --------------------- SLOT (PCI_ID +1) ----------------------- - oss.str(""); - pci_device->vector_value("PCI_ID", slot); slot = slot + 1; + if ( bus_index ) + { + ibus = slot; + slot = 0; + } + + // Set PCI attributes oss << showbase << internal << setfill('0') << hex << setw(4) << slot; pci_device->replace("VM_SLOT", oss.str()); + oss.str(""); + + oss << showbase << internal << setfill('0') << hex << setw(4) << ibus; + + pci_device->replace("VM_BUS", oss.str()); + // ------------------- ADDRESS (BUS:SLOT.0) --------------------- oss.str(""); diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 0a634dd8f7..5d87553539 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -3678,7 +3678,8 @@ int VirtualMachine::set_up_attach_nic(VirtualMachineTemplate * tmpl, string& err nd.get_configuration_attribute("PCI_PASSTHROUGH_BUS", bus); - if ( HostSharePCI::set_pci_address(_new_nic.get(), bus, false) != 0 ) + if ( HostSharePCI::set_pci_address(_new_nic.get(), bus, + test_machine_type("q35"), false) != 0 ) { err = "Wrong BUS in PCI attribute"; return -1; @@ -3828,7 +3829,8 @@ int VirtualMachine::attach_pci(VectorAttribute * vpci, string& err) nd.get_configuration_attribute("PCI_PASSTHROUGH_BUS", bus); - if ( HostSharePCI::set_pci_address(_new_pci.get(), bus, false) != 0 ) + if ( HostSharePCI::set_pci_address(_new_pci.get(), bus, + test_machine_type("q35"), false) != 0 ) { err = "Wrong BUS in PCI attribute"; return -1; diff --git a/src/vm/VirtualMachineParser.cc b/src/vm/VirtualMachineParser.cc index 95e01ab72f..8d1e7b1171 100644 --- a/src/vm/VirtualMachineParser.cc +++ b/src/vm/VirtualMachineParser.cc @@ -381,7 +381,8 @@ int VirtualMachine::parse_pci(string& error_str, Template * tmpl) return -1; } - if ( HostSharePCI::set_pci_address(attr, default_bus, true) != 0 ) + if ( HostSharePCI::set_pci_address(attr, default_bus, + test_machine_type("q35"), true) != 0 ) { error_str = "Wrong BUS in PCI attribute"; return -1;