X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FIPUtil.py;h=de018b9565daf0ebf8487e1b34a335423221294e;hb=aafa2efa63b891e85ff13255691aae4909bc503c;hp=b78450a9f8c110aebb11fc0e347de73f59a17c29;hpb=56c1c013bd0d632256efa4cb6b0224bf9c608afa;p=csit.git diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py index b78450a9f8..de018b9565 100644 --- a/resources/libraries/python/IPUtil.py +++ b/resources/libraries/python/IPUtil.py @@ -15,17 +15,15 @@ import re -from socket import AF_INET, AF_INET6, inet_pton - from enum import IntEnum from ipaddress import ip_address -from ipaddress import IPv4Network, IPv6Network from resources.libraries.python.Constants import Constants from resources.libraries.python.InterfaceUtil import InterfaceUtil -from resources.libraries.python.PapiExecutor import PapiExecutor +from resources.libraries.python.PapiExecutor import PapiSocketExecutor from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd from resources.libraries.python.topology import Topology +from resources.libraries.python.VatExecutor import VatTerminal # from vpp/src/vnet/vnet/mpls/mpls_types.h @@ -57,7 +55,7 @@ class FibPathType(IntEnum): class FibPathFlags(IntEnum): """FIB path flags.""" FIB_PATH_FLAG_NONE = 0 - FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1 + FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1 #pylint: disable=invalid-name FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2 @@ -114,34 +112,54 @@ class IPUtil(object): """ sw_if_index = InterfaceUtil.get_interface_index(node, interface) - data = list() - if sw_if_index: - is_ipv6 = 1 if ip_version == 'ipv6' else 0 - - cmd = 'ip_address_dump' - cmd_reply = 'ip_address_details' - args = dict(sw_if_index=sw_if_index, - is_ipv6=is_ipv6) - err_msg = 'Failed to get L2FIB dump on host {host}'.format( - host=node['host']) - - with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_dump(err_msg) - - for item in papi_resp.reply[0]['api_reply']: - item[cmd_reply]['ip'] = item[cmd_reply]['prefix'].split('/')[0] - item[cmd_reply]['prefix_length'] = int( - item[cmd_reply]['prefix'].split('/')[1]) - item[cmd_reply]['is_ipv6'] = is_ipv6 - item[cmd_reply]['netmask'] = \ - str(IPv6Network(unicode('::/{pl}'.format( - pl=item[cmd_reply]['prefix_length']))).netmask) \ - if is_ipv6 \ - else str(IPv4Network(unicode('0.0.0.0/{pl}'.format( - pl=item[cmd_reply]['prefix_length']))).netmask) - data.append(item[cmd_reply]) - - return data + if not sw_if_index: + return list() + + is_ipv6 = 1 if ip_version == 'ipv6' else 0 + + cmd = 'ip_address_dump' + args = dict(sw_if_index=sw_if_index, + is_ipv6=is_ipv6) + err_msg = 'Failed to get L2FIB dump on host {host}'.format( + host=node['host']) + + with PapiSocketExecutor(node) as papi_exec: + details = papi_exec.add(cmd, **args).get_details(err_msg) + + # TODO: CSIT currently looks only whether the list is empty. + # Add proper value processing if values become important. + + return details + + @staticmethod + def vpp_get_ip_tables(node): + """Get dump of all IP FIB tables on a VPP node. + + :param node: VPP node. + :type node: dict + """ + + PapiSocketExecutor.run_cli_cmd(node, 'show ip fib') + PapiSocketExecutor.run_cli_cmd(node, 'show ip fib summary') + PapiSocketExecutor.run_cli_cmd(node, 'show ip6 fib') + PapiSocketExecutor.run_cli_cmd(node, 'show ip6 fib summary') + + @staticmethod + def vpp_get_ip_tables_prefix(node, address): + """Get dump of all IP FIB tables on a VPP node. + + :param node: VPP node. + :param address: IP address. + :type node: dict + :type address: str + """ + addr = ip_address(unicode(address)) + + PapiSocketExecutor.run_cli_cmd( + node, 'show {ip_ver} fib {addr}/{addr_len}'.format( + ip_ver='ip6' if addr.version == 6 else 'ip', + addr=addr, + addr_len=addr.max_prefixlen)) @staticmethod def get_interface_vrf_table(node, interface, ip_version='ipv4'): @@ -158,19 +176,18 @@ class IPUtil(object): """ sw_if_index = InterfaceUtil.get_interface_index(node, interface) - is_ipv6 = 1 if ip_version == 'ipv6' else 0 - cmd = 'sw_interface_get_table' - args = dict(sw_if_index=sw_if_index, - is_ipv6=is_ipv6) + args = dict( + sw_if_index=sw_if_index, + is_ipv6=True if ip_version == 'ipv6' else False + ) err_msg = 'Failed to get VRF id assigned to interface {ifc}'.format( ifc=interface) - with PapiExecutor(node) as papi_exec: - papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + reply = papi_exec.add(cmd, **args).get_reply(err_msg) - return papi_resp['vrf_id'] + return reply['vrf_id'] @staticmethod def vpp_ip_source_check_setup(node, if_name): @@ -185,12 +202,12 @@ class IPUtil(object): args = dict( sw_if_index=InterfaceUtil.get_interface_index(node, if_name), is_add=1, - loose=0) + loose=0 + ) err_msg = 'Failed to enable source check on interface {ifc}'.format( ifc=if_name) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_ip_probe(node, interface, addr): @@ -204,16 +221,14 @@ class IPUtil(object): :type addr: str """ cmd = 'ip_probe_neighbor' - cmd_reply = 'proxy_arp_intfc_enable_disable_reply' args = dict( sw_if_index=InterfaceUtil.get_interface_index(node, interface), dst=str(addr)) err_msg = 'VPP ip probe {dev} {ip} failed on {h}'.format( dev=interface, ip=addr, h=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(cmd_reply=cmd_reply, err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def ip_addresses_should_be_equal(ip1, ip2): @@ -381,18 +396,17 @@ class IPUtil(object): cmd = 'sw_interface_add_del_address' args = dict( sw_if_index=InterfaceUtil.get_interface_index(node, interface), - is_add=1, - is_ipv6=1 if ip_addr.version == 6 else 0, - del_all=0, - address_length=int(prefix_length) if prefix_length else 128 - if ip_addr.version == 6 else 32, - address=inet_pton( - AF_INET6 if ip_addr.version == 6 else AF_INET, str(ip_addr))) + is_add=True, + del_all=False, + prefix=IPUtil.create_prefix_object( + ip_addr, + prefix_length if prefix_length else 128 + if ip_addr.version == 6 else 32) + ) err_msg = 'Failed to add IP address on interface {ifc}'.format( ifc=interface) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address): @@ -420,13 +434,57 @@ class IPUtil(object): neighbor=neighbor) err_msg = 'Failed to add IP neighbor on interface {ifc}'.format( ifc=iface_key) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod - def vpp_route_add(node, network, prefix_len, **kwargs): - """Add route to the VPP node. + def union_addr(ip_addr): + """Creates union IP address. + + :param ip_addr: IPv4 or IPv6 address. + :type ip_addr: IPv4Address or IPv6Address + :returns: Union IP address. + :rtype: dict + """ + return dict(ip6=ip_addr.packed) if ip_addr.version == 6 \ + else dict(ip4=ip_addr.packed) + + @staticmethod + def create_ip_address_object(ip_addr): + """Create IP address object. + + :param ip_addr: IPv4 or IPv6 address + :type ip_addr: IPv4Address or IPv6Address + :returns: IP address object. + :rtype: dict + """ + return dict( + af=getattr( + AddressFamily, 'ADDRESS_IP6' if ip_addr.version == 6 + else 'ADDRESS_IP4').value, + un=IPUtil.union_addr(ip_addr)) + + @staticmethod + def create_prefix_object(ip_addr, addr_len): + """Create prefix object. + + :param ip_addr: IPv4 or IPv6 address. + :para, addr_len: Length of IP address. + :type ip_addr: IPv4Address or IPv6Address + :type addr_len: int + :returns: Prefix object. + :rtype: dict + """ + addr = IPUtil.create_ip_address_object(ip_addr) + + return dict( + len=int(addr_len), + address=addr + ) + + @staticmethod + def compose_vpp_route_structure(node, network, prefix_len, **kwargs): + """Create route object for ip_route_add_del api call. :param node: VPP node. :param network: Route destination network address. @@ -447,35 +505,23 @@ class IPUtil(object): :type network: str :type prefix_len: int :type kwargs: dict + :returns: route parameter basic structure + :rtype: dict """ interface = kwargs.get('interface', '') gateway = kwargs.get('gateway', '') net_addr = ip_address(unicode(network)) - def union_addr(ip_addr): - """Creates union IP address. - - :param ip_addr: IPv4 or IPv6 address. - :type ip_addr: IPv4Address or IPv6Address - :returns: Union IP address. - :rtype: dict - """ - return dict(ip6=inet_pton(AF_INET6, str(ip_addr))) \ - if ip_addr.version == 6 \ - else dict(ip4=inet_pton(AF_INET, str(ip_addr))) - - addr = dict( - af=getattr( - AddressFamily, 'ADDRESS_IP6' if net_addr.version == 6 - else 'ADDRESS_IP4').value) - prefix = dict(address_length=int(prefix_len)) + prefix = IPUtil.create_prefix_object(net_addr, prefix_len) paths = list() n_hop = dict( - address=union_addr(ip_address(unicode(gateway))) if gateway else 0, + address=IPUtil.union_addr(ip_address(unicode(gateway))) if gateway + else 0, via_label=MPLS_LABEL_INVALID, - obj_id=Constants.BITWISE_NON_ZERO) + obj_id=Constants.BITWISE_NON_ZERO + ) path = dict( sw_if_index=InterfaceUtil.get_interface_index(node, interface) if interface else Constants.BITWISE_NON_ZERO, @@ -494,31 +540,81 @@ class IPUtil(object): else 'FIB_PATH_NH_PROTO_IP4').value, nh=n_hop, n_labels=0, - label_stack=list(0 for _ in range(16))) + label_stack=list(0 for _ in range(16)) + ) paths.append(path) route = dict( table_id=int(kwargs.get('vrf', 0)), + prefix=prefix, n_paths=len(paths), - paths=paths) + paths=paths + ) + return route + + @staticmethod + def vpp_route_add(node, network, prefix_len, **kwargs): + """Add route to the VPP node. + + :param node: VPP node. + :param network: Route destination network address. + :param prefix_len: Route destination network prefix length. + :param kwargs: Optional key-value arguments: + + gateway: Route gateway address. (str) + interface: Route interface. (str) + vrf: VRF table ID. (int) + count: number of IP addresses to add starting from network IP (int) + local: The route is local with same prefix (increment is 1). + If None, then is not used. (bool) + lookup_vrf: VRF table ID for lookup. (int) + multipath: Enable multipath routing. (bool) + weight: Weight value for unequal cost multipath routing. (int) + + :type node: dict + :type network: str + :type prefix_len: int + :type kwargs: dict + """ + count = kwargs.get("count", 1) + + if count > 100: + gateway = kwargs.get("gateway", '') + interface = kwargs.get("interface", '') + vrf = kwargs.get("vrf", None) + multipath = kwargs.get("multipath", False) + + with VatTerminal(node, json_param=False) as vat: + vat.vat_terminal_exec_cmd_from_template( + 'vpp_route_add.vat', + network=network, + prefix_length=prefix_len, + via='via {}'.format(gateway) if gateway else '', + sw_if_index='sw_if_index {}'.format( + InterfaceUtil.get_interface_index(node, interface)) + if interface else '', + vrf='vrf {}'.format(vrf) if vrf else '', + count='count {}'.format(count) if count else '', + multipath='multipath' if multipath else '') + return + + net_addr = ip_address(unicode(network)) cmd = 'ip_route_add_del' args = dict( is_add=1, - is_multipath=int(kwargs.get('multipath', False))) + is_multipath=int(kwargs.get('multipath', False)), + route=None + ) err_msg = 'Failed to add route(s) on host {host}'.format( host=node['host']) - with PapiExecutor(node) as papi_exec: + with PapiSocketExecutor(node) as papi_exec: for i in xrange(kwargs.get('count', 1)): - addr['un'] = union_addr(net_addr + i) - prefix['address'] = addr - route['prefix'] = prefix + args['route'] = IPUtil.compose_vpp_route_structure( + node, net_addr + i, prefix_len, **kwargs) history = False if 1 < i < kwargs.get('count', 1) else True - papi_exec.add(cmd, history=history, route=route, **args) - if i > 0 and i % Constants.PAPI_MAX_API_BULK == 0: - papi_exec.get_replies(err_msg).verify_replies( - err_msg=err_msg) - papi_exec.get_replies(err_msg).verify_replies(err_msg=err_msg) + papi_exec.add(cmd, history=history, **args) + papi_exec.get_replies(err_msg) @staticmethod def flush_ip_addresses(node, interface): @@ -532,12 +628,13 @@ class IPUtil(object): cmd = 'sw_interface_add_del_address' args = dict( sw_if_index=InterfaceUtil.get_interface_index(node, interface), - del_all=1) + is_add=False, + del_all=True + ) err_msg = 'Failed to flush IP address on interface {ifc}'.format( ifc=interface) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def add_fib_table(node, table_id, ipv6=False): @@ -559,6 +656,5 @@ class IPUtil(object): is_add=1) err_msg = 'Failed to add FIB table on host {host}'.format( host=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg)