diff --git a/include/HostSharePCI.h b/include/HostSharePCI.h index 4c08f71941..aeb98e8ba6 100644 --- a/include/HostSharePCI.h +++ b/include/HostSharePCI.h @@ -78,7 +78,7 @@ public: * @param devs list of requested PCI devices, will include address of * assigned devices. * @param vmid of the VM - * + * * @return true if the devices where added * * NOTE THIS FUNCTION DOES NOT PERFORM ANY ROLLBACK @@ -117,7 +117,7 @@ public: /** * Gets a 4 hex digits value from attribute * @param name of the attribute - * @pci_device VectorAttribute representing the device + * @param pci_device VectorAttribute representing the device * @return the 0 if not found, -1 syntax error, >0 valid hex value */ static int get_pci_value(const char * name, diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index d08c0dc24c..e89b6a26cf 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -1414,6 +1414,12 @@ public: // ------------------------------------------------------------------------ // NIC Hotplug related functions // ------------------------------------------------------------------------ + + /** + * Checks the attributes of a PCI device + */ + int check_pci_attributes(VectorAttribute * pci, std::string& err); + /** * Generate and attach a new NIC attribute to the VM. This method check * that the NIC is compatible with the VM cluster allocation and fills SG diff --git a/include/VirtualMachineNic.h b/include/VirtualMachineNic.h index 84caa7d5ec..7e5e899b88 100644 --- a/include/VirtualMachineNic.h +++ b/include/VirtualMachineNic.h @@ -114,7 +114,7 @@ public: void to_xml_short(std::ostringstream& oss) const; /** - * Check is a nic is alias or not + * Check if a nic is alias or not */ bool is_alias() const { @@ -122,7 +122,7 @@ public: } /** - * Check is a nic is a PCI + * Check if a nic is a PCI */ bool is_pci() const { diff --git a/share/etc/oned.conf b/share/etc/oned.conf index ea2022b33f..2e4e4bbaac 100644 --- a/share/etc/oned.conf +++ b/share/etc/oned.conf @@ -1116,6 +1116,8 @@ INHERIT_VNET_ATTR = "VXLAN_TEP" INHERIT_VNET_ATTR = "VXLAN_MC" INHERIT_VNET_ATTR = "CVLANS" INHERIT_VNET_ATTR = "QINQ_TYPE" +INHERIT_VNET_ATTR = "TRUST" +INHERIT_VNET_ATTR = "SPOOFCHK" INHERIT_VNET_ATTR = "VCENTER_NET_REF" INHERIT_VNET_ATTR = "VCENTER_SWITCH_NAME" diff --git a/src/host/HostSharePCI.cc b/src/host/HostSharePCI.cc index 4183f6a33b..4bbcfc3f35 100644 --- a/src/host/HostSharePCI.cc +++ b/src/host/HostSharePCI.cc @@ -144,14 +144,20 @@ bool HostSharePCI::test(const vector &devs) const { std::set assigned; std::set tested; + unsigned int vendor_id, device_id, class_id; - // Test for "SHORT_ADDRESS" PCI selectio + // Test for "SHORT_ADDRESS" PCI selection // and pre-allocated these first for (const auto& device : devs) { string short_addr = device->vector_value("SHORT_ADDRESS"); - if (short_addr.empty()) + // Be aware of special case after migration, when + // !short_addr.empty() and at least one of vendor/device/class is set + if (short_addr.empty() || + get_pci_value("VENDOR", device, vendor_id) > 0 || + get_pci_value("DEVICE", device, device_id) > 0 || + get_pci_value("CLASS" , device, class_id) > 0) { continue; } @@ -301,12 +307,16 @@ bool HostSharePCI::add_by_name(VectorAttribute *device, int vmid) bool HostSharePCI::add(vector &devs, int vmid) { std::set added; + unsigned int vendor_id, device_id, class_id; for (auto& device : devs) { string short_addr = device->vector_value("SHORT_ADDRESS"); - if (short_addr.empty()) + if (short_addr.empty() || + get_pci_value("VENDOR", device, vendor_id) > 0 || + get_pci_value("DEVICE", device, device_id) > 0 || + get_pci_value("CLASS" , device, class_id) > 0) { continue; } diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 07f3b76012..7b63f220ac 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -2546,6 +2546,8 @@ Request::ErrorCode VirtualMachineAttachNic::request_execute(int id, // ------------------------------------------------------------------------- // Authorize the operation, restricted attributes & check quotas // ------------------------------------------------------------------------- + VectorAttribute * pci = tmpl.get("PCI"); + if (auto vm = vmpool->get_ro(id)) { vm->get_permissions(vm_perms); @@ -2554,6 +2556,11 @@ Request::ErrorCode VirtualMachineAttachNic::request_execute(int id, { hid = vm->get_hid(); } + + if (pci != nullptr && vm->check_pci_attributes(pci, att.resp_msg) != 0) + { + return ACTION; + } } else { @@ -2596,8 +2603,6 @@ Request::ErrorCode VirtualMachineAttachNic::request_execute(int id, // ------------------------------------------------------------------------- // PCI test and set // ------------------------------------------------------------------------- - - VectorAttribute * pci = tmpl.get("PCI"); HostShareCapacity sr; if ( pci != nullptr && hid != -1 ) diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 5635ef2f83..eb5bfc8c4c 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -3413,7 +3413,7 @@ int VirtualMachine::set_up_attach_nic(VirtualMachineTemplate * tmpl, string& err if ( is_pci ) { Nebula& nd = Nebula::instance(); - string default_bus; + string bus; std::vector pcis; @@ -3435,9 +3435,9 @@ int VirtualMachine::set_up_attach_nic(VirtualMachineTemplate * tmpl, string& err _new_nic->replace("PCI_ID", max_pci_id + 1); - nd.get_configuration_attribute("PCI_PASSTHROUGH_BUS", default_bus); + nd.get_configuration_attribute("PCI_PASSTHROUGH_BUS", bus); - if ( HostSharePCI::set_pci_address(_new_nic.get(), default_bus, false) != 0 ) + if ( HostSharePCI::set_pci_address(_new_nic.get(), bus, false) != 0 ) { err = "Wrong BUS in PCI attribute"; return -1; diff --git a/src/vm/VirtualMachineParser.cc b/src/vm/VirtualMachineParser.cc index 3342369323..06589fd1c8 100644 --- a/src/vm/VirtualMachineParser.cc +++ b/src/vm/VirtualMachineParser.cc @@ -315,8 +315,7 @@ int VirtualMachine::parse_vrouter(string& error_str, Template * tmpl) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -static int check_pci_attributes(VectorAttribute * pci, const string& default_bus, - string& error_str) +int VirtualMachine::check_pci_attributes(VectorAttribute * pci, string& error_str) { static std::vector attrs = {"VENDOR", "DEVICE", "CLASS"}; bool found = false; @@ -383,10 +382,16 @@ int VirtualMachine::parse_pci(string& error_str, Template * tmpl) for (auto& attr : array_pci) { - if ( check_pci_attributes(attr, default_bus, error_str) != 0 ) + if ( check_pci_attributes(attr, error_str) != 0 ) { return -1; } + + if ( HostSharePCI::set_pci_address(attr, default_bus, true) != 0 ) + { + error_str = "Wrong BUS in PCI attribute"; + return -1; + } } return 0; diff --git a/src/vmm_mad/remotes/kvm/attach_nic b/src/vmm_mad/remotes/kvm/attach_nic index 436ca78dd7..e8a20a0095 100755 --- a/src/vmm_mad/remotes/kvm/attach_nic +++ b/src/vmm_mad/remotes/kvm/attach_nic @@ -65,7 +65,8 @@ domain = ARGV[0] vm = KvmVM.new(STDIN.read) if vm.pci_attach? - dev_xml = vm.hostdev_xml + pci1 = vm.dumpxml_regexp(domain, "") + dev_xml = vm.hostdev_xml(:pci => pci1) else dev_xml = vm.interface_xml end diff --git a/src/vmm_mad/remotes/kvm/detach_nic b/src/vmm_mad/remotes/kvm/detach_nic index 48fd2c83ed..eb33d5421e 100755 --- a/src/vmm_mad/remotes/kvm/detach_nic +++ b/src/vmm_mad/remotes/kvm/detach_nic @@ -87,7 +87,7 @@ def detach_nic(dom, mac) end def detach_pci(dom, vm) - dev_xml = vm.hostdev_xml(true) + dev_xml = vm.hostdev_xml(:force_hostdev => true) cmd =<<~EOS #{virsh} detach-device #{dom} <( @@ -105,6 +105,7 @@ def detach_pci(dom, vm) rc end + # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ diff --git a/src/vmm_mad/remotes/lib/kvm/opennebula_vm.rb b/src/vmm_mad/remotes/lib/kvm/opennebula_vm.rb index c1144feeb7..82b2bd14bb 100644 --- a/src/vmm_mad/remotes/lib/kvm/opennebula_vm.rb +++ b/src/vmm_mad/remotes/lib/kvm/opennebula_vm.rb @@ -119,7 +119,7 @@ module VirtualMachineManagerKVM dev << '' end - dev << xputs('', 'BRIDGE') + dev << xputs("", 'BRIDGE') else dev = '' end @@ -144,10 +144,10 @@ module VirtualMachineManagerKVM end inb_keys = %w[INBOUND_AVG_BW INBOUND_PEAK_BW INBOUND_PEAK_KB] - inbound = inb_keys.any? {|e| exist? e } + inbound = inb_keys.any? {|e| exist? e } outb_keys = %w[OUTBOUND_AVG_BW OUTBOUND_PEAK_BW OUTBOUND_PEAK_KB] - outbound = outb_keys.any? {|e| exist? e } + outbound = outb_keys.any? {|e| exist? e } if inbound || outbound dev << '' @@ -191,6 +191,18 @@ module VirtualMachineManagerKVM !out.match(regexp).nil? end + def dumpxml_regexp(domain, str_exp) + cmd = "#{virsh} dumpxml #{domain}" + + out, _err, _rc = Open3.capture3(cmd) + + return false if out.nil? || out.empty? + + regexp = Regexp.new(str_exp) + + !out.match(regexp).nil? + end + #----------------------------------------------------------------------- # This function generates a XML document to attach a new device # to the VM. The specification supports the same OpenNebula attributes. @@ -198,20 +210,24 @@ module VirtualMachineManagerKVM # Example: # # - # - #
- # - #
+ # + #
+ # + #
# # # NOTE: Libvirt/QEMU seems to have a race condition accesing vfio device # and the permission check/set that makes not work for VF. # - # NOTE: On detach (as we are managing MAC/VLAN through ip link vf) - # devices needs to use format + # NOTE: On detach (as we are manging MAC/VLAN through ip link vf) devices + # needs to use format #----------------------------------------------------------------------- - def hostdev_xml(force_hostdev = false) + def hostdev_xml(_opts = {}) + opts = { + :force_hostdev => false, + :pci => false + }.merge(_opts) + prefix_old = @xpath_prefix @xpath_prefix = "TEMPLATE/PCI[ATTACH='YES']/" @@ -220,7 +236,7 @@ module VirtualMachineManagerKVM dev << xputs('
', 'UUID') dev << '' else - if force_hostdev + if opts[:force_hostdev] is_vf = false else is_vf = vf?(@xml["#{@xpath_prefix}SHORT_ADDRESS"]) @@ -241,6 +257,22 @@ module VirtualMachineManagerKVM dev << xputs(' slot=%s', 'SLOT', :hex => true) dev << xputs(' function=%s', 'FUNCTION', :hex => true) dev << '/>' + + #Setting Bus address needs to check that a PCI contoller is + #present for Bus 1 + vm_addr = %w[VM_DOMAIN VM_BUS VM_SLOT VM_FUNCTION].all? {|e| + exist? e + } + + if vm_addr && opts[:pci] + dev << '
' + end + dev << dev_end end diff --git a/src/vnm_mad/remotes/lib/vf.rb b/src/vnm_mad/remotes/lib/vf.rb index e5d0f636b3..e95eed6ec9 100644 --- a/src/vnm_mad/remotes/lib/vf.rb +++ b/src/vnm_mad/remotes/lib/vf.rb @@ -87,7 +87,6 @@ module VNMMAD::VirtualFunction # virtfn /sys/devices/pci0000:80/0000:80:03.2/0000:85:00.0/virtfn3 # _vf /sys/devices/pci0000:80/0000:80:03.2/0000:85:02.3 # rubocop:enable Layout/LineLength - m = virtfn.match(/virtfn([0-9]+)/) next if m.nil? @@ -102,6 +101,8 @@ module VNMMAD::VirtualFunction cmd = "#{command(:ip)} link set #{pf_dev} vf #{m[1]}" cmd << " mac #{pci[:mac]}" if pci[:mac] cmd << " vlan #{pci[:vlan_id]}" if pci[:vlan_id] + cmd << " spoofchk #{on_off(pci[:spoofchk])}" if pci[:spoofchk] + cmd << " trust #{on_off(pci[:trust])}" if pci[:trust] OpenNebula.exec_and_log(cmd) end @@ -109,5 +110,12 @@ module VNMMAD::VirtualFunction # rubocop:enable Style/CombinableLoops end + def on_off(option) + if option.match(/^yes$|^on$/i) + "on" + else + "off" + end + end end # rubocop:enable Style/ClassAndModuleChildren