X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FDUTSetup.py;h=c7a560262c5b675381524df2d1177b73c907ddf0;hp=396029a04f7164abf55418e44373ddc946505ae9;hb=3cba9eaf8dce261928939aa112fae4354b51e229;hpb=844698802b99b67237250fd31e416081680354c0 diff --git a/resources/libraries/python/DUTSetup.py b/resources/libraries/python/DUTSetup.py index 396029a04f..c7a560262c 100644 --- a/resources/libraries/python/DUTSetup.py +++ b/resources/libraries/python/DUTSetup.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020 Cisco and/or its affiliates. +# Copyright (c) 2021 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -400,6 +400,20 @@ class DUTSetup: :type numvfs: int :raises RuntimeError: Failed to create VFs on PCI. """ + cmd = f"test -f /sys/bus/pci/devices/{pf_pci_addr}/sriov_numvfs" + sriov_unsupported, _, _ = exec_cmd(node, cmd) + # if sriov_numvfs doesn't exist, then sriov_unsupported != 0 + if int(sriov_unsupported): + if numvfs == 0: + # sriov is not supported and we want 0 VFs + # no need to do anything + return + + raise RuntimeError( + f"Can't configure {numvfs} VFs on {pf_pci_addr} device " + f"on {node[u'host']} since it doesn't support SR-IOV." + ) + pci = pf_pci_addr.replace(u":", r"\:") command = f"sh -c \"echo {numvfs} | " \ f"tee /sys/bus/pci/devices/{pci}/sriov_numvfs\"" @@ -430,16 +444,21 @@ class DUTSetup: ) @staticmethod - def pci_driver_unbind_list(node, *pci_addrs): - """Unbind PCI devices from current driver on node. + def unbind_pci_devices_from_other_driver(node, driver, *pci_addrs): + """Unbind PCI devices from driver other than input driver on node. :param node: DUT node. + :param driver: Driver to not unbind from. If None or empty string, + will attempt to unbind from the current driver. :param pci_addrs: PCI device addresses. :type node: dict + :type driver: str :type pci_addrs: list """ for pci_addr in pci_addrs: - DUTSetup.pci_driver_unbind(node, pci_addr) + if not driver or \ + DUTSetup.get_pci_dev_driver(node, pci_addr) != driver: + DUTSetup.pci_driver_unbind(node, pci_addr) @staticmethod def pci_driver_bind(node, pci_addr, driver): @@ -543,61 +562,24 @@ class DUTSetup: def get_pci_dev_driver(node, pci_addr): """Get current PCI device driver on node. - .. note:: - # lspci -vmmks 0000:00:05.0 - Slot: 00:05.0 - Class: Ethernet controller - Vendor: Red Hat, Inc - Device: Virtio network device - SVendor: Red Hat, Inc - SDevice: Device 0001 - PhySlot: 5 - Driver: virtio-pci - :param node: DUT node. :param pci_addr: PCI device address. :type node: dict :type pci_addr: str :returns: Driver or None - :raises RuntimeError: If PCI rescan or lspci command execution failed. :raises RuntimeError: If it is not possible to get the interface driver information from the node. """ - ssh = SSH() - ssh.connect(node) - - for i in range(3): - logger.trace(f"Try number {i}: Get PCI device driver") - - cmd = f"lspci -vmmks {pci_addr}" - ret_code, stdout, _ = ssh.exec_command(cmd) - if int(ret_code): - raise RuntimeError(f"'{cmd}' failed on '{node[u'host']}'") - - for line in stdout.splitlines(): - if not line: - continue - name = None - value = None - try: - name, value = line.split(u"\t", 1) - except ValueError: - if name == u"Driver:": - return None - if name == u"Driver:": - return value - - if i < 2: - logger.trace( - f"Driver for PCI device {pci_addr} not found, " - f"executing pci rescan and retrying" - ) - cmd = u"sh -c \"echo 1 > /sys/bus/pci/rescan\"" - ret_code, _, _ = ssh.exec_command_sudo(cmd) - if int(ret_code) != 0: - raise RuntimeError(f"'{cmd}' failed on '{node[u'host']}'") - - return None + driver_path = f"/sys/bus/pci/devices/{pci_addr}/driver" + cmd = f"test -d {driver_path}" + ret_code, ret_val, _ = exec_cmd(node, cmd) + if int(ret_code): + # the directory doesn't exist which means the device is not bound + # to any driver + return None + cmd = f"basename $(readlink -f {driver_path})" + ret_val, _ = exec_cmd_no_error(node, cmd) + return ret_val.strip() @staticmethod def verify_kernel_module(node, module, force_load=False): @@ -699,7 +681,7 @@ class DUTSetup: ) # workaround to avoid installation of vpp-api-python exec_cmd_no_error( - node, u"rm -f {vpp_pkg_dir}vpp-api-python.deb", + node, f"rm -f {vpp_pkg_dir}vpp-api-python.deb", timeout=120, sudo=True ) exec_cmd_no_error( @@ -716,7 +698,7 @@ class DUTSetup: ) # workaround to avoid installation of vpp-api-python exec_cmd_no_error( - node, u"rm -f {vpp_pkg_dir}vpp-api-python.rpm", + node, f"rm -f {vpp_pkg_dir}vpp-api-python.rpm", timeout=120, sudo=True ) exec_cmd_no_error(