Fix: use pci/rescan to avoid occasional bind issue on Centos7
[csit.git] / resources / libraries / python / InterfaceUtil.py
index 5d6d8f7..e5597b9 100644 (file)
@@ -260,7 +260,15 @@ class InterfaceUtil(object):
          Note: A single interface may have multiple IP addresses assigned.
          :rtype: list
         """
-        sw_if_index = InterfaceUtil.get_sw_if_index(node, interface)
+
+        try:
+            sw_if_index = Topology.convert_interface_reference(
+                node, interface, "sw_if_index")
+        except RuntimeError:
+            if isinstance(interface, basestring):
+                sw_if_index = InterfaceUtil.get_sw_if_index(node, interface)
+            else:
+                raise
 
         with VatTerminal(node) as vat:
             response = vat.vat_terminal_exec_cmd_from_template(
@@ -340,8 +348,14 @@ class InterfaceUtil(object):
         ssh = SSH()
         ssh.connect(node)
 
-        cmd = 'lspci -vmmks {0}'.format(pci_addr)
+        # First rescan PCI devices in the system
+        cmd = 'sh -c "echo 1 > /sys/bus/pci/rescan"'
+        (ret_code, _, _) = ssh.exec_command_sudo(cmd)
+        if int(ret_code) != 0:
+            raise RuntimeError("'{0}' failed on '{1}'"
+                               .format(cmd, node['host']))
 
+        cmd = 'lspci -vmmks {0}'.format(pci_addr)
         (ret_code, stdout, _) = ssh.exec_command(cmd)
         if int(ret_code) != 0:
             raise RuntimeError("'{0}' failed on '{1}'"
@@ -847,6 +861,29 @@ class InterfaceUtil(object):
                 sw_if_index=sw_if_index)
         return data[0]
 
+    @staticmethod
+    def get_interface_vrf_table(node, interface):
+        """Get vrf ID for the given interface.
+
+        :param node: VPP node.
+        :param interface: Name or sw_if_index of a specific interface.
+        :type node: dict
+        :type interface: str or int
+        :returns: vrf ID of the specified interface.
+        :rtype: int
+        """
+
+        if isinstance(interface, basestring):
+            sw_if_index = InterfaceUtil.get_sw_if_index(node, interface)
+        else:
+            sw_if_index = interface
+
+        with VatTerminal(node) as vat:
+            data = vat.vat_terminal_exec_cmd_from_template(
+                "interface_vrf_dump.vat",
+                sw_if_index=sw_if_index)
+        return data[0]["vrf_id"]
+
     @staticmethod
     def get_sw_if_index(node, interface_name):
         """Get sw_if_index for the given interface from actual interface dump.