1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Common IP utilities library."""
17 from enum import IntEnum
19 from ipaddress import ip_address
21 from resources.libraries.python.Constants import Constants
22 from resources.libraries.python.InterfaceUtil import InterfaceUtil
23 from resources.libraries.python.IPAddress import IPAddress
24 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
25 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
26 from resources.libraries.python.topology import Topology
27 from resources.libraries.python.VatExecutor import VatTerminal
28 from resources.libraries.python.Namespaces import Namespaces
31 # from vpp/src/vnet/vnet/mpls/mpls_types.h
32 MPLS_IETF_MAX_LABEL = 0xfffff
33 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
36 class FibPathType(IntEnum):
38 FIB_PATH_TYPE_NORMAL = 0
39 FIB_PATH_TYPE_LOCAL = 1
40 FIB_PATH_TYPE_DROP = 2
41 FIB_PATH_TYPE_UDP_ENCAP = 3
42 FIB_PATH_TYPE_BIER_IMP = 4
43 FIB_PATH_TYPE_ICMP_UNREACH = 5
44 FIB_PATH_TYPE_ICMP_PROHIBIT = 6
45 FIB_PATH_TYPE_SOURCE_LOOKUP = 7
47 FIB_PATH_TYPE_INTERFACE_RX = 9
48 FIB_PATH_TYPE_CLASSIFY = 10
51 class FibPathFlags(IntEnum):
53 FIB_PATH_FLAG_NONE = 0
54 # TODO: Name too long for pylint, fix in VPP.
55 FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1
56 FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2
59 class FibPathNhProto(IntEnum):
60 """FIB path next-hop protocol."""
61 FIB_PATH_NH_PROTO_IP4 = 0
62 FIB_PATH_NH_PROTO_IP6 = 1
63 FIB_PATH_NH_PROTO_MPLS = 2
64 FIB_PATH_NH_PROTO_ETHERNET = 3
65 FIB_PATH_NH_PROTO_BIER = 4
69 """Common IP utilities"""
72 def ip_to_int(ip_str):
73 """Convert IP address from string format (e.g. 10.0.0.1) to integer
74 representation (167772161).
76 :param ip_str: IP address in string representation.
78 :returns: Integer representation of IP address.
81 return int(ip_address(ip_str))
84 def int_to_ip(ip_int):
85 """Convert IP address from integer representation (e.g. 167772161) to
86 string format (10.0.0.1).
88 :param ip_int: IP address in integer representation.
90 :returns: String representation of IP address.
93 return str(ip_address(ip_int))
96 def vpp_get_interface_ip_addresses(node, interface, ip_version):
97 """Get list of IP addresses from an interface on a VPP node.
99 :param node: VPP node.
100 :param interface: Name of an interface on the VPP node.
101 :param ip_version: IP protocol version (ipv4 or ipv6).
104 :type ip_version: str
105 :returns: List of dictionaries, each containing IP address, subnet
106 prefix length and also the subnet mask for ipv4 addresses.
107 Note: A single interface may have multiple IP addresses assigned.
110 sw_if_index = InterfaceUtil.get_interface_index(node, interface)
115 cmd = u"ip_address_dump"
117 sw_if_index=sw_if_index,
118 is_ipv6=bool(ip_version == u"ipv6")
120 err_msg = f"Failed to get L2FIB dump on host {node[u'host']}"
122 with PapiSocketExecutor(node) as papi_exec:
123 details = papi_exec.add(cmd, **args).get_details(err_msg)
125 # TODO: CSIT currently looks only whether the list is empty.
126 # Add proper value processing if values become important.
131 def vpp_get_ip_tables(node):
132 """Get dump of all IP FIB tables on a VPP node.
134 :param node: VPP node.
137 PapiSocketExecutor.run_cli_cmd(node, u"show ip fib")
138 PapiSocketExecutor.run_cli_cmd(node, u"show ip fib summary")
139 PapiSocketExecutor.run_cli_cmd(node, u"show ip6 fib")
140 PapiSocketExecutor.run_cli_cmd(node, u"show ip6 fib summary")
143 def vpp_get_ip_table_summary(node):
144 """Get IPv4 FIB table summary on a VPP node.
146 :param node: VPP node.
149 PapiSocketExecutor.run_cli_cmd(node, u"show ip fib summary")
152 def vpp_get_ip_table(node):
153 """Get IPv4 FIB table on a VPP node.
155 :param node: VPP node.
158 PapiSocketExecutor.run_cli_cmd(node, u"show ip fib")
161 def vpp_get_ip_tables_prefix(node, address):
162 """Get dump of all IP FIB tables on a VPP node.
164 :param node: VPP node.
165 :param address: IP address.
169 addr = ip_address(address)
170 ip_ver = u"ip6" if addr.version == 6 else u"ip"
172 PapiSocketExecutor.run_cli_cmd(
173 node, f"show {ip_ver} fib {addr}/{addr.max_prefixlen}"
177 def get_interface_vrf_table(node, interface, ip_version='ipv4'):
178 """Get vrf ID for the given interface.
180 :param node: VPP node.
181 :param interface: Name or sw_if_index of a specific interface.
183 :param ip_version: IP protocol version (ipv4 or ipv6).
184 :type interface: str or int
185 :type ip_version: str
186 :returns: vrf ID of the specified interface.
189 sw_if_index = InterfaceUtil.get_interface_index(node, interface)
191 cmd = u"sw_interface_get_table"
193 sw_if_index=sw_if_index,
194 is_ipv6=bool(ip_version == u"ipv6")
196 err_msg = f"Failed to get VRF id assigned to interface {interface}"
198 with PapiSocketExecutor(node) as papi_exec:
199 reply = papi_exec.add(cmd, **args).get_reply(err_msg)
201 return reply[u"vrf_id"]
204 def vpp_ip_source_check_setup(node, if_name):
205 """Setup Reverse Path Forwarding source check on interface.
207 :param node: VPP node.
208 :param if_name: Interface name to setup RPF source check.
212 cmd = u"ip_source_check_interface_add_del"
214 sw_if_index=InterfaceUtil.get_interface_index(node, if_name),
218 err_msg = f"Failed to enable source check on interface {if_name}"
219 with PapiSocketExecutor(node) as papi_exec:
220 papi_exec.add(cmd, **args).get_reply(err_msg)
223 def vpp_ip_probe(node, interface, addr):
224 """Run ip probe on VPP node.
226 :param node: VPP node.
227 :param interface: Interface key or name.
228 :param addr: IPv4/IPv6 address.
233 cmd = u"ip_probe_neighbor"
235 sw_if_index=InterfaceUtil.get_interface_index(node, interface),
238 err_msg = f"VPP ip probe {interface} {addr} failed on {node[u'host']}"
240 with PapiSocketExecutor(node) as papi_exec:
241 papi_exec.add(cmd, **args).get_reply(err_msg)
244 def ip_addresses_should_be_equal(ip1, ip2):
245 """Fails if the given IP addresses are unequal.
247 :param ip1: IPv4 or IPv6 address.
248 :param ip2: IPv4 or IPv6 address.
252 addr1 = ip_address(ip1)
253 addr2 = ip_address(ip2)
256 raise AssertionError(f"IP addresses are not equal: {ip1} != {ip2}")
259 def setup_network_namespace(node, namespace_name, interface_name,
260 ip_addr_list, prefix_length):
261 """Setup namespace on given node and attach interface and IP to
262 this namespace. Applicable also on TG node.
264 :param node: VPP node.
265 :param namespace_name: Namespace name.
266 :param interface_name: Interface name.
267 :param ip_addr_list: List of IP addresses of namespace's interface.
268 :param prefix_length: IP address prefix length.
270 :type namespace_name: str
271 :type interface_name: str
272 :type ip_addr_list: list
273 :type prefix_length: int
275 Namespaces.create_namespace(node, namespace_name)
277 cmd = f"ip netns exec {namespace_name} ip link set {interface_name} up"
278 exec_cmd_no_error(node, cmd, sudo=True)
280 for ip_addr in ip_addr_list:
281 cmd = f"ip netns exec {namespace_name} ip addr add " \
282 f"{ip_addr}/{prefix_length} dev {interface_name}"
283 exec_cmd_no_error(node, cmd, sudo=True)
286 def linux_enable_forwarding(node, ip_ver=u"ipv4"):
287 """Enable forwarding on a Linux node, e.g. VM.
289 :param node: VPP node.
290 :param ip_ver: IP version, 'ipv4' or 'ipv6'.
294 cmd = f"sysctl -w net.{ip_ver}.ip_forward=1"
295 exec_cmd_no_error(node, cmd, sudo=True)
298 def get_linux_interface_name(node, pci_addr):
299 """Get the interface name.
301 :param node: VPP/TG node.
302 :param pci_addr: PCI address
305 :returns: Interface name
307 :raises RuntimeError: If cannot get the information about interfaces.
310 r"pci@([0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}.[0-9a-f])\s" \
311 r"*([a-zA-Z0-9]*)\s*network"
313 cmd = u"lshw -class network -businfo"
314 ret_code, stdout, stderr = exec_cmd(node, cmd, timeout=30, sudo=True)
317 f"Could not get information about interfaces:\n{stderr}"
320 for line in stdout.splitlines()[2:]:
322 if re.search(regex_intf_info, line).group(1) == pci_addr:
323 return re.search(regex_intf_info, line).group(2)
324 except AttributeError:
329 def set_linux_interface_up(node, interface):
330 """Set the specified interface up.
332 :param node: VPP/TG node.
333 :param interface: Interface in namespace.
336 :raises RuntimeError: If the interface could not be set up.
338 cmd = f"ip link set {interface} up"
339 exec_cmd_no_error(node, cmd, timeout=30, sudo=True)
342 def set_linux_interface_ip(
343 node, interface, ip_addr, prefix, namespace=None):
344 """Set IP address to interface in linux.
346 :param node: VPP/TG node.
347 :param interface: Interface in namespace.
348 :param ip_addr: IP to be set on interface.
349 :param prefix: IP prefix.
350 :param namespace: Execute command in namespace. Optional
356 :raises RuntimeError: IP could not be set.
358 if namespace is not None:
359 cmd = f"ip netns exec {namespace} ip addr add {ip_addr}/{prefix}" \
362 cmd = f"ip addr add {ip_addr}/{prefix} dev {interface}"
364 exec_cmd_no_error(node, cmd, timeout=5, sudo=True)
367 def delete_linux_interface_ip(
368 node, interface, ip_addr, prefix_length, namespace=None):
369 """Delete IP address from interface in linux.
371 :param node: VPP/TG node.
372 :param interface: Interface in namespace.
373 :param ip_addr: IP to be deleted from interface.
374 :param prefix_length: IP prefix length.
375 :param namespace: Execute command in namespace. Optional
379 :type prefix_length: int
381 :raises RuntimeError: IP could not be deleted.
383 # TODO: Refactor command execution in namespaces into central
384 # methods (e.g. Namespace.exec_cmd_in_namespace)
385 if namespace is not None:
386 cmd = f"ip netns exec {namespace} ip addr del " \
387 f"{ip_addr}/{prefix_length} dev {interface}"
389 cmd = f"ip addr del {ip_addr}/{prefix_length} dev {interface}"
391 exec_cmd_no_error(node, cmd, timeout=5, sudo=True)
394 def linux_interface_has_ip(
395 node, interface, ip_addr, prefix_length, namespace=None):
396 """Return True if interface in linux has IP address.
398 :param node: VPP/TG node.
399 :param interface: Interface in namespace.
400 :param ip_addr: IP to be queried on interface.
401 :param prefix_length: IP prefix length.
402 :param namespace: Execute command in namespace. Optional
406 :type prefix_length: int
409 :raises RuntimeError: Request fails.
411 ip_addr_with_prefix = f"{ip_addr}/{prefix_length}"
412 if namespace is not None:
413 cmd = f"ip netns exec {namespace} ip addr show dev {interface}"
415 cmd = f"ip addr show dev {interface}"
417 cmd += u" | grep 'inet ' | awk -e '{print $2}'"
418 cmd += f" | grep '{ip_addr_with_prefix}'"
419 _, stdout, _ = exec_cmd(node, cmd, timeout=5, sudo=True)
421 has_ip = stdout.rstrip()
422 return bool(has_ip == ip_addr_with_prefix)
425 def add_linux_route(node, ip_addr, prefix, gateway, namespace=None):
426 """Add linux route in namespace.
428 :param node: Node where to execute command.
429 :param ip_addr: Route destination IP address.
430 :param prefix: IP prefix.
431 :param namespace: Execute command in namespace. Optional.
432 :param gateway: Gateway address.
439 if namespace is not None:
440 cmd = f"ip netns exec {namespace} ip route add {ip_addr}/{prefix}" \
443 cmd = f"ip route add {ip_addr}/{prefix} via {gateway}"
445 exec_cmd_no_error(node, cmd, sudo=True)
448 def vpp_interface_set_ip_address(
449 node, interface, address, prefix_length=None):
450 """Set IP address to VPP interface.
452 :param node: VPP node.
453 :param interface: Interface name.
454 :param address: IP address.
455 :param prefix_length: Prefix length.
459 :type prefix_length: int
461 ip_addr = ip_address(address)
463 cmd = u"sw_interface_add_del_address"
465 sw_if_index=InterfaceUtil.get_interface_index(node, interface),
468 prefix=IPUtil.create_prefix_object(
470 prefix_length if prefix_length else 128
471 if ip_addr.version == 6 else 32
474 err_msg = f"Failed to add IP address on interface {interface}"
476 with PapiSocketExecutor(node) as papi_exec:
477 papi_exec.add(cmd, **args).get_reply(err_msg)
480 def vpp_interface_set_ip_addresses(node, interface, ip_addr_list,
482 """Set IP addresses to VPP interface.
484 :param node: VPP node.
485 :param interface: Interface name.
486 :param ip_addr_list: IP addresses.
487 :param prefix_length: Prefix length.
490 :type ip_addr_list: list
491 :type prefix_length: int
493 for ip_addr in ip_addr_list:
494 IPUtil.vpp_interface_set_ip_address(node, interface, ip_addr,
498 def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address):
499 """Add IP neighbor on DUT node.
501 :param node: VPP node.
502 :param iface_key: Interface key.
503 :param ip_addr: IP address of the interface.
504 :param mac_address: MAC address of the interface.
508 :type mac_address: str
510 dst_ip = ip_address(ip_addr)
513 sw_if_index=Topology.get_interface_sw_index(node, iface_key),
515 mac_address=str(mac_address),
516 ip_address=str(dst_ip)
518 cmd = u"ip_neighbor_add_del"
523 err_msg = f"Failed to add IP neighbor on interface {iface_key}"
525 with PapiSocketExecutor(node) as papi_exec:
526 papi_exec.add(cmd, **args).get_reply(err_msg)
529 def create_prefix_object(ip_addr, addr_len):
530 """Create prefix object.
532 :param ip_addr: IPv4 or IPv6 address.
533 :param addr_len: Length of IP address.
534 :type ip_addr: IPv4Address or IPv6Address
536 :returns: Prefix object.
539 addr = IPAddress.create_ip_address_object(ip_addr)
547 def compose_vpp_route_structure(node, network, prefix_len, **kwargs):
548 """Create route object for ip_route_add_del api call.
550 :param node: VPP node.
551 :param network: Route destination network address.
552 :param prefix_len: Route destination network prefix length.
553 :param kwargs: Optional key-value arguments:
555 gateway: Route gateway address. (str)
556 interface: Route interface. (str)
557 vrf: VRF table ID. (int)
558 count: number of IP addresses to add starting from network IP (int)
559 local: The route is local with same prefix (increment is 1).
560 If None, then is not used. (bool)
561 lookup_vrf: VRF table ID for lookup. (int)
562 multipath: Enable multipath routing. (bool)
563 weight: Weight value for unequal cost multipath routing. (int)
567 :type prefix_len: int
569 :returns: route parameter basic structure
572 interface = kwargs.get(u"interface", u"")
573 gateway = kwargs.get(u"gateway", u"")
575 net_addr = ip_address(network)
577 prefix = IPUtil.create_prefix_object(net_addr, prefix_len)
581 address=IPAddress.union_addr(ip_address(gateway)) if gateway else 0,
582 via_label=MPLS_LABEL_INVALID,
583 obj_id=Constants.BITWISE_NON_ZERO
586 sw_if_index=InterfaceUtil.get_interface_index(node, interface)
587 if interface else Constants.BITWISE_NON_ZERO,
588 table_id=int(kwargs.get(u"lookup_vrf", 0)),
589 rpf_id=Constants.BITWISE_NON_ZERO,
590 weight=int(kwargs.get(u"weight", 1)),
593 FibPathType, u"FIB_PATH_TYPE_LOCAL"
594 if kwargs.get(u"local", False)
595 else u"FIB_PATH_TYPE_NORMAL"
597 flags=getattr(FibPathFlags, u"FIB_PATH_FLAG_NONE").value,
599 FibPathNhProto, u"FIB_PATH_NH_PROTO_IP6"
600 if net_addr.version == 6
601 else u"FIB_PATH_NH_PROTO_IP4"
605 label_stack=list(0 for _ in range(16))
610 table_id=int(kwargs.get(u"vrf", 0)),
618 def vpp_route_add(node, network, prefix_len, **kwargs):
619 """Add route to the VPP node.
621 :param node: VPP node.
622 :param network: Route destination network address.
623 :param prefix_len: Route destination network prefix length.
624 :param kwargs: Optional key-value arguments:
626 gateway: Route gateway address. (str)
627 interface: Route interface. (str)
628 vrf: VRF table ID. (int)
629 count: number of IP addresses to add starting from network IP (int)
630 local: The route is local with same prefix (increment is 1).
631 If None, then is not used. (bool)
632 lookup_vrf: VRF table ID for lookup. (int)
633 multipath: Enable multipath routing. (bool)
634 weight: Weight value for unequal cost multipath routing. (int)
638 :type prefix_len: int
641 count = kwargs.get(u"count", 1)
644 gateway = kwargs.get(u"gateway", '')
645 interface = kwargs.get(u"interface", '')
646 vrf = kwargs.get(u"vrf", None)
647 multipath = kwargs.get(u"multipath", False)
649 with VatTerminal(node, json_param=False) as vat:
651 vat.vat_terminal_exec_cmd_from_template(
652 u"vpp_route_add.vat",
654 prefix_length=prefix_len,
655 via=f"via {gateway}" if gateway else u"",
656 sw_if_index=f"sw_if_index "
657 f"{InterfaceUtil.get_interface_index(node, interface)}"
658 if interface else u"",
659 vrf=f"vrf {vrf}" if vrf else u"",
660 count=f"count {count}" if count else u"",
661 multipath=u"multipath" if multipath else u""
665 net_addr = ip_address(network)
666 cmd = u"ip_route_add_del"
669 is_multipath=kwargs.get(u"multipath", False),
672 err_msg = f"Failed to add route(s) on host {node[u'host']}"
674 with PapiSocketExecutor(node) as papi_exec:
675 for i in range(kwargs.get(u"count", 1)):
676 args[u"route"] = IPUtil.compose_vpp_route_structure(
677 node, net_addr + i, prefix_len, **kwargs
679 history = bool(not 1 < i < kwargs.get(u"count", 1))
680 papi_exec.add(cmd, history=history, **args)
681 papi_exec.get_replies(err_msg)
684 def flush_ip_addresses(node, interface):
685 """Flush all IP addresses from specified interface.
687 :param node: VPP node.
688 :param interface: Interface name.
692 cmd = u"sw_interface_add_del_address"
694 sw_if_index=InterfaceUtil.get_interface_index(node, interface),
698 err_msg = f"Failed to flush IP address on interface {interface}"
700 with PapiSocketExecutor(node) as papi_exec:
701 papi_exec.add(cmd, **args).get_reply(err_msg)
704 def add_fib_table(node, table_id, ipv6=False):
705 """Create new FIB table according to ID.
707 :param node: Node to add FIB on.
708 :param table_id: FIB table ID.
709 :param ipv6: Is this an IPv6 table
714 cmd = u"ip_table_add_del"
716 table_id=int(table_id),
723 err_msg = f"Failed to add FIB table on host {node[u'host']}"
725 with PapiSocketExecutor(node) as papi_exec:
726 papi_exec.add(cmd, **args).get_reply(err_msg)