X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FIPUtil.py;h=4a5a413fc8f09730ce6f37f9affe2fcdabb4eadb;hp=946f2f7b72386c608bcbfa3bb928988f954365ec;hb=b6606e7625e308a66bdfb9d5a9c065b58e429a99;hpb=a0d8705217e6f480d103e16d00fa63289019848f diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py index 946f2f7b72..4a5a413fc8 100644 --- a/resources/libraries/python/IPUtil.py +++ b/resources/libraries/python/IPUtil.py @@ -1,4 +1,5 @@ -# Copyright (c) 2020 Cisco and/or its affiliates. +# Copyright (c) 2021 Cisco and/or its affiliates. +# Copyright (c) 2021 PANTHEON.tech s.r.o. # 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: @@ -12,19 +13,22 @@ # limitations under the License. """Common IP utilities library.""" + import re +import os from enum import IntEnum -from ipaddress import ip_address +from ipaddress import ip_address, ip_network from resources.libraries.python.Constants import Constants +from resources.libraries.python.IncrementUtil import ObjIncrement from resources.libraries.python.InterfaceUtil import InterfaceUtil from resources.libraries.python.IPAddress import IPAddress 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 resources.libraries.python.VatExecutor import VatExecutor from resources.libraries.python.Namespaces import Namespaces @@ -51,7 +55,6 @@ class FibPathType(IntEnum): class FibPathFlags(IntEnum): """FIB path flags.""" FIB_PATH_FLAG_NONE = 0 - # TODO: Name too long for pylint, fix in VPP. FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1 FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2 @@ -65,6 +68,97 @@ class FibPathNhProto(IntEnum): FIB_PATH_NH_PROTO_BIER = 4 +class IpDscp(IntEnum): + """DSCP code points.""" + IP_API_DSCP_CS0 = 0 + IP_API_DSCP_CS1 = 8 + IP_API_DSCP_AF11 = 10 + IP_API_DSCP_AF12 = 12 + IP_API_DSCP_AF13 = 14 + IP_API_DSCP_CS2 = 16 + IP_API_DSCP_AF21 = 18 + IP_API_DSCP_AF22 = 20 + IP_API_DSCP_AF23 = 22 + IP_API_DSCP_CS3 = 24 + IP_API_DSCP_AF31 = 26 + IP_API_DSCP_AF32 = 28 + IP_API_DSCP_AF33 = 30 + IP_API_DSCP_CS4 = 32 + IP_API_DSCP_AF41 = 34 + IP_API_DSCP_AF42 = 36 + IP_API_DSCP_AF43 = 38 + IP_API_DSCP_CS5 = 40 + IP_API_DSCP_EF = 46 + IP_API_DSCP_CS6 = 48 + IP_API_DSCP_CS7 = 50 + + +class NetworkIncrement(ObjIncrement): + """ + An iterator object which accepts an IPv4Network or IPv6Network and + returns a new network, its address part incremented by the increment + number of network sizes, each time it is iterated or when inc_fmt is called. + The increment may be positive, negative or 0 + (in which case the network is always the same). + + Both initial and subsequent IP address can have host bits set, + check the initial value before creating instance if needed. + String formatting is configurable via constructor argument. + """ + def __init__(self, initial_value, increment=1, format=u"dash"): + """ + :param initial_value: The initial network. Can have host bits set. + :param increment: The current network will be incremented by this + amount of network sizes in each iteration/var_str call. + :param format: Type of formatting to use, "dash" or "slash" or "addr". + :type initial_value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network] + :type increment: int + :type format: str + """ + super().__init__(initial_value, increment) + self._prefix_len = self._value.prefixlen + host_len = self._value.max_prefixlen - self._prefix_len + self._net_increment = self._increment * (1 << host_len) + self._format = str(format).lower() + + def _incr(self): + """ + Increment the network, e.g.: + '30.0.0.0/24' incremented by 1 (the next network) is '30.0.1.0/24'. + '30.0.0.0/24' incremented by 2 is '30.0.2.0/24'. + """ + self._value = ip_network( + f"{self._value.network_address + self._net_increment}" + f"/{self._prefix_len}", strict=False + ) + + def _str_fmt(self): + """ + The string representation of the network depends on format. + + Dash format is ' - ', + useful for 'ipsec policy add spd' CLI. + + Slash format is '/', + useful for other CLI. + + Addr format is '', useful for PAPI. + + :returns: Current value converted to string according to format. + :rtype: str + :raises RuntimeError: If the format is not supported. + """ + if self._format == u"dash": + return f"{self._value.network_address} - " \ + f"{self._value.broadcast_address}" + elif self._format == u"slash": + return f"{self._value.network_address}/{self._prefix_len}" + elif self._format == u"addr": + return f"{self._value.network_address}" + + raise RuntimeError(f"Unsupported format {self._format}") + + class IPUtil: """Common IP utilities""" @@ -122,9 +216,6 @@ class IPUtil: 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 @@ -139,6 +230,24 @@ class IPUtil: PapiSocketExecutor.run_cli_cmd(node, u"show ip6 fib") PapiSocketExecutor.run_cli_cmd(node, u"show ip6 fib summary") + @staticmethod + def vpp_get_ip_table_summary(node): + """Get IPv4 FIB table summary on a VPP node. + + :param node: VPP node. + :type node: dict + """ + PapiSocketExecutor.run_cli_cmd(node, u"show ip fib summary") + + @staticmethod + def vpp_get_ip_table(node): + """Get IPv4 FIB table on a VPP node. + + :param node: VPP node. + :type node: dict + """ + PapiSocketExecutor.run_cli_cmd(node, u"show ip fib") + @staticmethod def vpp_get_ip_tables_prefix(node, address): """Get dump of all IP FIB tables on a VPP node. @@ -308,18 +417,24 @@ class IPUtil: return None @staticmethod - def set_linux_interface_up(node, interface): + def set_linux_interface_up( + node, interface, namespace=None): """Set the specified interface up. - :param node: VPP/TG node. :param interface: Interface in namespace. + :param namespace: Execute command in namespace. Optional :type node: dict :type interface: str + :type namespace: str :raises RuntimeError: If the interface could not be set up. """ - cmd = f"ip link set {interface} up" + if namespace is not None: + cmd = f"ip netns exec {namespace} ip link set dev {interface} up" + else: + cmd = f"ip link set dev {interface} up" exec_cmd_no_error(node, cmd, timeout=30, sudo=True) + @staticmethod def set_linux_interface_ip( node, interface, ip_addr, prefix, namespace=None): @@ -362,8 +477,6 @@ class IPUtil: :type namespace: str :raises RuntimeError: IP could not be deleted. """ - # TODO: Refactor command execution in namespaces into central - # methods (e.g. Namespace.exec_cmd_in_namespace) if namespace is not None: cmd = f"ip netns exec {namespace} ip addr del " \ f"{ip_addr}/{prefix_length} dev {interface}" @@ -387,7 +500,7 @@ class IPUtil: :type ip_addr: str :type prefix_length: int :type namespace: str - :rtype boolean + :rtype: boolean :raises RuntimeError: Request fails. """ ip_addr_with_prefix = f"{ip_addr}/{prefix_length}" @@ -539,10 +652,10 @@ class IPUtil: 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) + 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) + (Multipath value enters at higher level.) :type node: dict :type network: str @@ -597,68 +710,98 @@ class IPUtil: return route @staticmethod - def vpp_route_add(node, network, prefix_len, **kwargs): - """Add route to the VPP node. + def vpp_route_add(node, network, prefix_len, strict=True, **kwargs): + """Add route to the VPP node. Prefer multipath behavior. :param node: VPP node. :param network: Route destination network address. :param prefix_len: Route destination network prefix length. + :param strict: If true, fail if address has host bits set. :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) + local: The route is local with same prefix (increment is 1 network) + If None, then is not used. (bool) lookup_vrf: VRF table ID for lookup. (int) - multipath: Enable multipath routing. (bool) + multipath: Enable multipath routing. (bool) Default: True. weight: Weight value for unequal cost multipath routing. (int) :type node: dict :type network: str :type prefix_len: int + :type strict: bool :type kwargs: dict + :raises RuntimeError: If the argument combination is not supported. """ count = kwargs.get(u"count", 1) if count > 100: - gateway = kwargs.get(u"gateway", '') - interface = kwargs.get(u"interface", '') - vrf = kwargs.get(u"vrf", None) - multipath = kwargs.get(u"multipath", False) - - with VatTerminal(node, json_param=False) as vat: - - vat.vat_terminal_exec_cmd_from_template( - u"vpp_route_add.vat", - network=network, - prefix_length=prefix_len, - via=f"via {gateway}" if gateway else u"", - sw_if_index=f"sw_if_index " - f"{InterfaceUtil.get_interface_index(node, interface)}" - if interface else u"", - vrf=f"vrf {vrf}" if vrf else u"", - count=f"count {count}" if count else u"", - multipath=u"multipath" if multipath else u"" + if not kwargs.get(u"multipath", True): + raise RuntimeError(u"VAT exec supports only multipath behavior") + gateway = kwargs.get(u"gateway", u"") + interface = kwargs.get(u"interface", u"") + local = kwargs.get(u"local", u"") + if interface: + interface = InterfaceUtil.vpp_get_interface_name( + node, InterfaceUtil.get_interface_index( + node, interface + ) ) + vrf = kwargs.get(u"vrf", None) + trailers = list() + if vrf: + trailers.append(f"table {vrf}") + if gateway: + trailers.append(f"via {gateway}") + if interface: + trailers.append(interface) + elif interface: + trailers.append(f"via {interface}") + if local: + if gateway or interface: + raise RuntimeError(u"Unsupported combination with local.") + trailers.append(u"local") + trailer = u" ".join(trailers) + command_parts = [u"exec ip route add", u"network goes here"] + if trailer: + command_parts.append(trailer) + netiter = NetworkIncrement( + ip_network(f"{network}/{prefix_len}", strict=strict), + format=u"slash" + ) + tmp_filename = u"/tmp/routes.config" + with open(tmp_filename, u"w") as tmp_file: + for _ in range(count): + command_parts[1] = netiter.inc_fmt() + print(u" ".join(command_parts), file=tmp_file) + VatExecutor().execute_script( + tmp_filename, node, timeout=1800, json_out=False, + copy_on_execute=True, history=False + ) + os.remove(tmp_filename) return - net_addr = ip_address(network) cmd = u"ip_route_add_del" args = dict( is_add=True, - is_multipath=kwargs.get(u"multipath", False), + is_multipath=kwargs.get(u"multipath", True), route=None ) err_msg = f"Failed to add route(s) on host {node[u'host']}" + netiter = NetworkIncrement( + ip_network(f"{network}/{prefix_len}", strict=strict), + format=u"addr" + ) with PapiSocketExecutor(node) as papi_exec: - for i in range(kwargs.get(u"count", 1)): + for i in range(count): args[u"route"] = IPUtil.compose_vpp_route_structure( - node, net_addr + i, prefix_len, **kwargs + node, netiter.inc_fmt(), prefix_len, **kwargs ) - history = bool(not 1 < i < kwargs.get(u"count", 1)) + history = bool(not 0 < i < count - 1) papi_exec.add(cmd, history=history, **args) papi_exec.get_replies(err_msg)