X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FInterfaceUtil.py;h=795bb52933994c45450c9ca126ed7da0fa7d29da;hp=16acdf3214fb31aa155755195879df26316a9a11;hb=9a261ea61549fc6a5c23369d2e236b002dc35038;hpb=6dd47ed60a15c9f4c86d095844839610783a8393 diff --git a/resources/libraries/python/InterfaceUtil.py b/resources/libraries/python/InterfaceUtil.py index 16acdf3214..795bb52933 100644 --- a/resources/libraries/python/InterfaceUtil.py +++ b/resources/libraries/python/InterfaceUtil.py @@ -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,21 +348,35 @@ class InterfaceUtil(object): ssh = SSH() ssh.connect(node) - 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}'" - .format(cmd, node['host'])) + for i in range(3): + logger.trace('Try {}: Get interface driver'.format(i)) + 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'])) - for line in stdout.splitlines(): - if len(line) == 0: - continue - (name, value) = line.split("\t", 1) - if name == 'Driver:': - return value + 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}'" + .format(cmd, node['host'])) - return None + for line in stdout.splitlines(): + if len(line) == 0: + continue + try: + (name, value) = line.split("\t", 1) + except ValueError: + if name != "Driver:": + pass + else: + return None + if name == 'Driver:': + return value if value else None + else: + raise RuntimeError('Get interface driver for: {0}' + .format(pci_addr)) @staticmethod def tg_set_interfaces_udev_rules(node): @@ -427,6 +449,47 @@ class InterfaceUtil(object): VatJsonUtil.update_vpp_interface_data_from_json(node, interface_dump_json) + @staticmethod + def update_nic_interface_names(node): + """Update interface names based on nic type and PCI address. + + This method updates interface names in the same format as VPP does. + + :param node: Node dictionary. + :type node: dict + """ + for ifc in node['interfaces'].values(): + if_pci = ifc['pci_address'].replace('.', ':').split(':') + bus = '{:x}'.format(int(if_pci[1], 16)) + dev = '{:x}'.format(int(if_pci[2], 16)) + fun = '{:x}'.format(int(if_pci[3], 16)) + loc = '{bus}/{dev}/{fun}'.format(bus=bus, dev=dev, fun=fun) + if ifc['model'] == 'Intel-XL710': + ifc['name'] = 'FortyGigabitEthernet{loc}'.format(loc=loc) + elif ifc['model'] == 'Intel-X710': + ifc['name'] = 'TenGigabitEthernet{loc}'.format(loc=loc) + elif ifc['model'] == 'Intel-X520-DA2': + ifc['name'] = 'TenGigabitEthernet{loc}'.format(loc=loc) + elif ifc['model'] == 'Cisco-VIC-1385': + ifc['name'] = 'FortyGigabitEthernet{loc}'.format(loc=loc) + elif ifc['model'] == 'Cisco-VIC-1227': + ifc['name'] = 'TenGigabitEthernet{loc}'.format(loc=loc) + else: + ifc['name'] = 'UnknownEthernet{loc}'.format(loc=loc) + + @staticmethod + def update_nic_interface_names_on_all_duts(nodes): + """Update interface names based on nic type and PCI address on all DUTs. + + This method updates interface names in the same format as VPP does. + + :param nodes: Topology nodes. + :type nodes: dict + """ + for node in nodes.values(): + if node['type'] == NodeType.DUT: + InterfaceUtil.update_nic_interface_names(node) + @staticmethod def update_tg_interface_data_on_node(node): """Update interface name for TG/linux node in DICT__nodes. @@ -847,6 +910,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. @@ -950,10 +1036,14 @@ class InterfaceUtil(object): ipv6 = 'ipv6' if ipv6 else '' with VatTerminal(node) as vat: - vat.vat_terminal_exec_cmd_from_template("set_fib_to_interface.vat", - sw_index=sw_if_index, - vrf=table_id, - ipv6=ipv6) + ret = vat.vat_terminal_exec_cmd_from_template( + "set_fib_to_interface.vat", + sw_index=sw_if_index, vrf=table_id, ipv6=ipv6) + + if ret[0]["retval"] != 0: + raise RuntimeError('Unable to assign interface to FIB node {}.' + .format(node)) + @staticmethod def set_linux_interface_mac(node, interface, mac, namespace=None):