From: Lucas Meneghel Rodrigues Subject: [PATCH] KVM test: Add PCI device assignment support Date: Tue, 12 Jan 2010 16:28:13 -0200 Message-ID: <1263320893-6219-1-git-send-email-lmr@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: kvm@vger.kernel.org, mgoldish@redhat.com, Lucas Meneghel Rodrigues , Yolkfull Chow To: autotest@test.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:58212 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750929Ab0ALS2T (ORCPT ); Tue, 12 Jan 2010 13:28:19 -0500 Sender: kvm-owner@vger.kernel.org List-ID: Add support to PCI device assignment on the kvm test. It supports both SR-IOV virtual functions and physical NIC card device assignment. Single Root I/O Virtualization (SR-IOV) allows a single PCI device to be shared amongst multiple virtual machines while retaining the performance benefit of assigning a PCI device to a virtual machine. A common example is where a single SR-IOV capable NIC - with perhaps only a single physical network port - might be shared with multiple virtual machines by assigning a virtual function to each VM. SR-IOV support is implemented in the kernel. The core implementation is contained in the PCI subsystem, but there must also be driver suppor= t for both the Physical Function (PF) and Virtual Function (VF) devices. With an SR-IOV capable device one can allocate VFs from a PF. The VFs surface as PCI devices which are backed on the physical PCI device by resources (queues, and register sets). Device support: In 2.6.30, the Intel=C2=AE 82576 Gigabit Ethernet Controller is the onl= y SR-IOV capable device supported. The igb driver has PF support and the igbvf has VF support. In 2.6.31 the Neterion=C2=AE X3100=E2=84=A2 is supported as well. This = device uses the same vxge driver for the PF as well as the VFs. In order to configure the test: * For SR-IOV virtual functions passthrough, we could specify the module parameter 'max_vfs' in config file. * For physical NIC card pass through, we should specify the device name(s). Signed-off-by: Yolkfull Chow Signed-off-by: Lucas Meneghel Rodrigues --- client/tests/kvm/kvm_utils.py | 284 ++++++++++++++++++++++++= ++++++++ client/tests/kvm/kvm_vm.py | 60 +++++++ client/tests/kvm/tests_base.cfg.sample | 22 +++- 3 files changed, 365 insertions(+), 1 deletions(-) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils= =2Epy index 2bbbe22..a2d9607 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -924,3 +924,287 @@ def create_report(report_dir, results_dir): reporter =3D os.path.join(report_dir, 'html_report.py') html_file =3D os.path.join(results_dir, 'results.html') os.system('%s -r %s -f %s -R' % (reporter, results_dir, html_file)= ) + + +def get_full_pci_id(pci_id): + """ + Get full PCI ID of pci_id. + + @param pci_id: PCI ID of a device. + """ + cmd =3D "lspci -D | awk '/%s/ {print $1}'" % pci_id + status, full_id =3D commands.getstatusoutput(cmd) + if status !=3D 0: + return None + return full_id + + +def get_vendor_from_pci_id(pci_id): + """ + Check out the device vendor ID according to pci_id. + + @param pci_id: PCI ID of a device. + """ + cmd =3D "lspci -n | awk '/%s/ {print $3}'" % pci_id + return re.sub(":", " ", commands.getoutput(cmd)) + + +class PciAssignable(object): + """ + Request PCI assignable devices on host. It will check whether to r= equest + PF (physical Functions) or VF (Virtual Functions). + """ + def __init__(self, type=3D"nic_vf", driver=3DNone, driver_option=3D= None, + names=3DNone, devices_requested=3DNone): + """ + Initialize parameter 'type' which could be: + nic_vf: Virtual Functions + nic_pf: Physical Function (actual hardware) + mixed: Both includes VFs and PFs + + If pass through Physical NIC cards, we need to specify which d= evices + to be assigned, e.g. 'eth1 eth2'. + + If pass through Virtual Functions, we need to specify how many= vfs + are going to be assigned, e.g. passthrough_count =3D 8 and max= _vfs in + config file. + + @param type: PCI device type. + @param driver: Kernel module for the PCI assignable device. + @param driver_option: Module option to specify the maximum num= ber of + VFs (eg 'max_vfs=3D7') + @param names: Physical NIC cards correspondent network interfa= ces, + e.g.'eth1 eth2 ...' + @param devices_requested: Number of devices being requested. + """ + self.type =3D type + self.driver =3D driver + self.driver_option =3D driver_option + if names: + self.name_list =3D names.split() + if devices_requested: + self.devices_requested =3D int(devices_requested) + else: + self.devices_requested =3D None + + + def _get_pf_pci_id(self, name, search_str): + """ + Get the PF PCI ID according to name. + + @param name: Name of the PCI device. + @param search_str: Search string to be used on lspci. + """ + cmd =3D "ethtool -i %s | awk '/bus-info/ {print $2}'" % name + s, pci_id =3D commands.getstatusoutput(cmd) + if not (s or "Cannot get driver information" in pci_id): + return pci_id[5:] + cmd =3D "lspci | awk '/%s/ {print $1}'" % search_str + pci_ids =3D [id for id in commands.getoutput(cmd).splitlines()= ] + nic_id =3D int(re.search('[0-9]+', name).group(0)) + if (len(pci_ids) - 1) < nic_id: + return None + return pci_ids[nic_id] + + + def _release_dev(self, pci_id): + """ + Release a single PCI device. + + @param pci_id: PCI ID of a given PCI device. + """ + base_dir =3D "/sys/bus/pci" + full_id =3D get_full_pci_id(pci_id) + vendor_id =3D get_vendor_from_pci_id(pci_id) + drv_path =3D os.path.join(base_dir, "devices/%s/driver" % full= _id) + if 'pci-stub' in os.readlink(drv_path): + cmd =3D "echo '%s' > %s/new_id" % (vendor_id, drv_path) + if os.system(cmd): + return False + + stub_path =3D os.path.join(base_dir, "drivers/pci-stub") + cmd =3D "echo '%s' > %s/unbind" % (full_id, stub_path) + if os.system(cmd): + return False + + driver =3D self.dev_drivers[pci_id] + cmd =3D "echo '%s' > %s/bind" % (full_id, driver) + if os.system(cmd): + return False + + return True + + + def get_vf_devs(self): + """ + Catch all VFs PCI IDs. + + @return: List with all PCI IDs for the Virtual Functions avali= able + """ + if not self.sr_iov_setup(): + return [] + + cmd =3D "lspci | awk '/Virtual Function/ {print $1}'" + return commands.getoutput(cmd).split() + + + def get_pf_devs(self): + """ + Catch all PFs PCI IDs. + + @return: List with all PCI IDs for the physical hardware reque= sted + """ + pf_ids =3D [] + for name in self.name_list: + pf_id =3D self._get_pf_pci_id(name, "Ethernet") + if not pf_id: + continue + pf_ids.append(pf_id) + return pf_ids + + + def get_devs(self, count): + """ + Check out all devices' PCI IDs according to their name. + + @param count: count number of PCI devices needed for pass thro= ugh + @return: a list of all devices' PCI IDs + """ + if self.type =3D=3D "nic_vf": + vf_ids =3D self.get_vf_devs() + elif self.type =3D=3D "nic_pf": + vf_ids =3D self.get_pf_devs() + elif self.type =3D=3D "mixed": + vf_ids =3D self.get_vf_devs() + vf_ids.extend(self.get_pf_devs()) + return vf_ids[0:count] + + + def get_vfs_count(self): + """ + Get VFs count number according to lspci. + """ + cmd =3D "lspci | grep 'Virtual Function' | wc -l" + # For each VF we'll see 2 prints of 'Virtual Function', so let= 's + # divide the result per 2 + return int(commands.getoutput(cmd)) / 2 + + + def check_vfs_count(self): + """ + Check VFs count number according to the parameter driver_optio= ns. + """ + return (self.get_vfs_count =3D=3D self.devices_requested) + + + def is_binded_to_stub(self, full_id): + """ + Verify whether the device with full_id is already binded to pc= i-stub. + + @param full_id: Full ID for the given PCI device + """ + base_dir =3D "/sys/bus/pci" + stub_path =3D os.path.join(base_dir, "drivers/pci-stub") + if os.path.exists(os.path.join(stub_path, full_id)): + return True + return False + + + def sr_iov_setup(self): + """ + Ensure the PCI device is working in sr_iov mode. + + Check if the PCI hardware device drive is loaded with the appr= opriate, + parameters (number of VFs), and if it's not, perform setup. + + @return: True, if the setup was completed successfuly, False o= therwise. + """ + re_probe =3D False + s, o =3D commands.getstatusoutput('lsmod | grep %s' % self.dri= ver) + if s: + re_probe =3D True + elif not self.check_vfs_count(): + os.system("modprobe -r %s" % self.driver) + re_probe =3D True + + # Re-probe driver with proper number of VFs + if re_probe: + cmd =3D "modprobe %s %s" % (self.driver, self.driver_optio= n) + s, o =3D commands.getstatusoutput(cmd) + if s: + return False + if not self.check_vfs_count(): + return False + return True + + + def request_devs(self): + """ + Implement setup process: unbind the PCI device and then bind i= t + to the pci-stub driver. + + @return: a list of successfully requested devices' PCI IDs. + """ + base_dir =3D "/sys/bus/pci" + stub_path =3D os.path.join(base_dir, "drivers/pci-stub") + + self.pci_ids =3D self.get_devs(self.devices_requested) + logging.debug("The following pci_ids were found: %s" % self.pc= i_ids) + requested_pci_ids =3D [] + self.dev_drivers =3D {} + + # Setup all devices specified for assignment to guest + for pci_id in self.pci_ids: + full_id =3D get_full_pci_id(pci_id) + if not full_id: + continue + drv_path =3D os.path.join(base_dir, "devices/%s/driver" % = full_id) + dev_prev_driver=3D os.path.realpath(os.path.join(drv_path, + os.readlink(drv_path))) + self.dev_drivers[pci_id] =3D dev_prev_driver + + # Judge whether the device driver has been binded to stub + if not self.is_binded_to_stub(full_id): + logging.debug("Binding device %s to stub" % full_id) + vendor_id =3D get_vendor_from_pci_id(pci_id) + stub_new_id =3D os.path.join(stub_path, 'new_id') + unbind_dev =3D os.path.join(drv_path, 'unbind') + stub_bind =3D os.path.join(stub_path, 'bind') + + info_write_to_files =3D [(vendor_id, stub_new_id), + (full_id, unbind_dev), + (full_id, stub_bind)] + + for content, file in info_write_to_files: + try: + utils.open_write_close(content, file) + except IOError: + logging.debug("Failed to write %s to file %s" = % + (content, file)) + continue + + if not self.is_binded_to_stub(full_id): + logging.error("Binding device %s to stub failed" % + pci_id) + continue + else: + logging.debug("Device %s already binded to stub" % pci= _id) + requested_pci_ids.append(pci_id) + self.pci_ids =3D requested_pci_ids + return self.pci_ids + + + def release_devs(self): + """ + Release all PCI devices currently assigned to VMs back to the + virtualization host. + """ + try: + for pci_id in self.dev_drivers: + if not self._release_dev(pci_id): + logging.error("Failed to release device %s to host= " % + pci_id) + else: + logging.info("Released device %s successfully" % p= ci_id) + except: + return diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py index 7229b79..78a16ac 100755 --- a/client/tests/kvm/kvm_vm.py +++ b/client/tests/kvm/kvm_vm.py @@ -119,6 +119,7 @@ class VM: self.params =3D params self.root_dir =3D root_dir self.address_cache =3D address_cache + self.pci_assignable =3D None =20 # Find available monitor filename while True: @@ -304,6 +305,12 @@ class VM: elif params.get("uuid"): qemu_cmd +=3D " -uuid %s" % params.get("uuid") =20 + # If the PCI assignment step went OK, add each one of the PCI = assigned + # devices to the qemu command line. + if self.pci_assignable: + for pci_id in self.pa_pci_ids: + qemu_cmd +=3D " -pcidevice host=3D%s" % pci_id + return qemu_cmd =20 =20 @@ -392,6 +399,50 @@ class VM: self.uuid =3D f.read().strip() f.close() =20 + if not params.get("pci_assignable") =3D=3D "no": + pa_type =3D params.get("pci_assignable") + pa_devices_requested =3D params.get("devices_requested= ") + + # Virtual Functions (VF) assignable devices + if pa_type =3D=3D "vf": + pa_driver =3D params.get("driver") + pa_driver_option =3D params.get("driver_option") + self.pci_assignable =3D kvm_utils.PciAssignable(ty= pe=3Dpa_type, + driver=3Dpa_driver, + driver_option=3Dpa_driver_opti= on, + devices_requested=3Dpa_devices= _requested) + # Physical NIC (PF) assignable devices + elif pa_type =3D=3D "pf": + pa_device_names =3D params.get("device_names") + self.pci_assignable =3D kvm_utils.PciAssignable(ty= pe=3Dpa_type, + names=3Dpa_device_names, + devices_requested=3Dpa_device= s_requested) + # Working with both VF and PF + elif pa_type =3D=3D "mixed": + pa_device_names =3D params.get("device_names") + pa_driver =3D params.get("driver") + pa_driver_option =3D params.get("driver_option") + self.pci_assignable =3D kvm_utils.PciAssignable(ty= pe=3Dpa_type, + driver=3Dpa_driver, + driver_option=3Dpa_driver_opti= on, + names=3Dpa_device_names, + devices_requested=3Dpa_devices= _requested) + + self.pa_pci_ids =3D self.pci_assignable.request_devs() + + if self.pa_pci_ids: + logging.debug("Successfuly assigned devices: %s" % + self.pa_pci_ids) + else: + logging.error("No PCI assignable devices were assi= gned " + "and 'pci_assignable' is defined to = %s " + "on your config file. Aborting VM cr= eation." % + pa_type) + return False + + else: + self.pci_assignable =3D None + # Make qemu command qemu_command =3D self.make_qemu_command() =20 @@ -537,6 +588,8 @@ class VM: # Is it already dead? if self.is_dead(): logging.debug("VM is already down") + if self.pci_assignable: + self.pci_assignable.release_devs() return =20 logging.debug("Destroying VM with PID %d..." % @@ -557,6 +610,9 @@ class VM: return finally: session.close() + if self.pci_assignable: + self.pci_assignable.release_devs() + =20 # Try to destroy with a monitor command logging.debug("Trying to kill VM with monitor command...") @@ -566,6 +622,8 @@ class VM: # Wait for the VM to be really dead if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5): logging.debug("VM is down") + if self.pci_assignable: + self.pci_assignable.release_devs() return =20 # If the VM isn't dead yet... @@ -575,6 +633,8 @@ class VM: # Wait for the VM to be really dead if kvm_utils.wait_for(self.is_dead, 5, 0.5, 0.5): logging.debug("VM is down") + if self.pci_assignable: + self.pci_assignable.release_devs() return =20 logging.error("Process %s is a zombie!" % self.process.get= _pid()) diff --git a/client/tests/kvm/tests_base.cfg.sample b/client/tests/kvm/= tests_base.cfg.sample index b8f25f4..49b6d6b 100644 --- a/client/tests/kvm/tests_base.cfg.sample +++ b/client/tests/kvm/tests_base.cfg.sample @@ -889,7 +889,27 @@ variants: extra_params +=3D " -mem-path /mnt/kvm_hugepage" =20 =20 +variants: + - @no_pci_assignable: + pci_assignable =3D no + - pf_assignable: + pci_assignable =3D pf + device_names =3D eth1 + - vf_assignable: + pci_assignable =3D vf + # Driver (kernel module) that supports SR-IOV hardware. + # As of today (30-11-2009), we have 2 drivers for this type of= hardware: + # Intel=C2=AE 82576 Gigabit Ethernet Controller - igb + # Neterion=C2=AE X3100=E2=84=A2 - vxge + driver =3D igb + # Driver option to specify the maximum number of virtual funct= ions + # (on vxge the option is , for example, is max_config_dev) + # the default below is for the igb driver + driver_option =3D "max_vfs=3D7" + # Number of devices that are going to be requested. + devices_requested =3D 7 + + image_name.* ?<=3D images/ cdrom.* ?<=3D isos/ steps ?<=3D steps/ - --=20 1.6.5.2