Python3: resources and libraries
[csit.git] / resources / libraries / python / IPUtil.py
1 # Copyright (c) 2019 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Common IP utilities library."""
15
16 import re
17
18 from enum import IntEnum
19
20 from ipaddress import ip_address
21
22 from resources.libraries.python.Constants import Constants
23 from resources.libraries.python.InterfaceUtil import InterfaceUtil
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
29
30 # from vpp/src/vnet/vnet/mpls/mpls_types.h
31 MPLS_IETF_MAX_LABEL = 0xfffff
32 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
33
34
35 class AddressFamily(IntEnum):
36     """IP address family."""
37     ADDRESS_IP4 = 0
38     ADDRESS_IP6 = 1
39
40
41 class FibPathType(IntEnum):
42     """FIB path types."""
43     FIB_PATH_TYPE_NORMAL = 0
44     FIB_PATH_TYPE_LOCAL = 1
45     FIB_PATH_TYPE_DROP = 2
46     FIB_PATH_TYPE_UDP_ENCAP = 3
47     FIB_PATH_TYPE_BIER_IMP = 4
48     FIB_PATH_TYPE_ICMP_UNREACH = 5
49     FIB_PATH_TYPE_ICMP_PROHIBIT = 6
50     FIB_PATH_TYPE_SOURCE_LOOKUP = 7
51     FIB_PATH_TYPE_DVR = 8
52     FIB_PATH_TYPE_INTERFACE_RX = 9
53     FIB_PATH_TYPE_CLASSIFY = 10
54
55
56 class FibPathFlags(IntEnum):
57     """FIB path flags."""
58     FIB_PATH_FLAG_NONE = 0
59     FIB_PATH_FLAG_RESOLVE_VIA_ATTACHED = 1  # pylint: disable=invalid-name
60     FIB_PATH_FLAG_RESOLVE_VIA_HOST = 2
61
62
63 class FibPathNhProto(IntEnum):
64     """FIB path next-hop protocol."""
65     FIB_PATH_NH_PROTO_IP4 = 0
66     FIB_PATH_NH_PROTO_IP6 = 1
67     FIB_PATH_NH_PROTO_MPLS = 2
68     FIB_PATH_NH_PROTO_ETHERNET = 3
69     FIB_PATH_NH_PROTO_BIER = 4
70
71
72 class IPUtil:
73     """Common IP utilities"""
74
75     @staticmethod
76     def ip_to_int(ip_str):
77         """Convert IP address from string format (e.g. 10.0.0.1) to integer
78         representation (167772161).
79
80         :param ip_str: IP address in string representation.
81         :type ip_str: str
82         :returns: Integer representation of IP address.
83         :rtype: int
84         """
85         return int(ip_address(ip_str))
86
87     @staticmethod
88     def int_to_ip(ip_int):
89         """Convert IP address from integer representation (e.g. 167772161) to
90         string format (10.0.0.1).
91
92         :param ip_int: IP address in integer representation.
93         :type ip_int: int
94         :returns: String representation of IP address.
95         :rtype: str
96         """
97         return str(ip_address(ip_int))
98
99     @staticmethod
100     def vpp_get_interface_ip_addresses(node, interface, ip_version):
101         """Get list of IP addresses from an interface on a VPP node.
102
103         :param node: VPP node.
104         :param interface: Name of an interface on the VPP node.
105         :param ip_version: IP protocol version (ipv4 or ipv6).
106         :type node: dict
107         :type interface: str
108         :type ip_version: str
109         :returns: List of dictionaries, each containing IP address, subnet
110             prefix length and also the subnet mask for ipv4 addresses.
111             Note: A single interface may have multiple IP addresses assigned.
112         :rtype: list
113         """
114         sw_if_index = InterfaceUtil.get_interface_index(node, interface)
115
116         if not sw_if_index:
117             return list()
118
119         is_ipv6 = 1 if ip_version == u"ipv6" else 0
120
121         cmd = u"ip_address_dump"
122         args = dict(
123             sw_if_index=sw_if_index,
124             is_ipv6=is_ipv6
125         )
126         err_msg = f"Failed to get L2FIB dump on host {node[u'host']}"
127
128         with PapiSocketExecutor(node) as papi_exec:
129             details = papi_exec.add(cmd, **args).get_details(err_msg)
130
131         # TODO: CSIT currently looks only whether the list is empty.
132         # Add proper value processing if values become important.
133
134         return details
135
136     @staticmethod
137     def vpp_get_ip_tables(node):
138         """Get dump of all IP FIB tables on a VPP node.
139
140         :param node: VPP node.
141         :type node: dict
142         """
143         PapiSocketExecutor.run_cli_cmd(node, u"show ip fib")
144         PapiSocketExecutor.run_cli_cmd(node, u"show ip fib summary")
145         PapiSocketExecutor.run_cli_cmd(node, u"show ip6 fib")
146         PapiSocketExecutor.run_cli_cmd(node, u"show ip6 fib summary")
147
148     @staticmethod
149     def vpp_get_ip_tables_prefix(node, address):
150         """Get dump of all IP FIB tables on a VPP node.
151
152         :param node: VPP node.
153         :param address: IP address.
154         :type node: dict
155         :type address: str
156         """
157         addr = ip_address(address)
158         ip_ver = u"ip6" if addr.version == 6 else u"ip"
159
160         PapiSocketExecutor.run_cli_cmd(
161             node, f"show {ip_ver} fib {addr}/{addr.max_prefixlen}"
162         )
163
164     @staticmethod
165     def get_interface_vrf_table(node, interface, ip_version='ipv4'):
166         """Get vrf ID for the given interface.
167
168         :param node: VPP node.
169         :param interface: Name or sw_if_index of a specific interface.
170         :type node: dict
171         :param ip_version: IP protocol version (ipv4 or ipv6).
172         :type interface: str or int
173         :type ip_version: str
174         :returns: vrf ID of the specified interface.
175         :rtype: int
176         """
177         sw_if_index = InterfaceUtil.get_interface_index(node, interface)
178
179         cmd = u"sw_interface_get_table"
180         args = dict(
181             sw_if_index=sw_if_index,
182             is_ipv6=bool(ip_version == u"ipv6")
183         )
184         err_msg = f"Failed to get VRF id assigned to interface {interface}"
185
186         with PapiSocketExecutor(node) as papi_exec:
187             reply = papi_exec.add(cmd, **args).get_reply(err_msg)
188
189         return reply[u"vrf_id"]
190
191     @staticmethod
192     def vpp_ip_source_check_setup(node, if_name):
193         """Setup Reverse Path Forwarding source check on interface.
194
195         :param node: VPP node.
196         :param if_name: Interface name to setup RPF source check.
197         :type node: dict
198         :type if_name: str
199         """
200         cmd = u"ip_source_check_interface_add_del"
201         args = dict(
202             sw_if_index=InterfaceUtil.get_interface_index(node, if_name),
203             is_add=1,
204             loose=0
205         )
206         err_msg = f"Failed to enable source check on interface {if_name}"
207         with PapiSocketExecutor(node) as papi_exec:
208             papi_exec.add(cmd, **args).get_reply(err_msg)
209
210     @staticmethod
211     def vpp_ip_probe(node, interface, addr):
212         """Run ip probe on VPP node.
213
214         :param node: VPP node.
215         :param interface: Interface key or name.
216         :param addr: IPv4/IPv6 address.
217         :type node: dict
218         :type interface: str
219         :type addr: str
220         """
221         cmd = u"ip_probe_neighbor"
222         args = dict(
223             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
224             dst=str(addr)
225         )
226         err_msg = f"VPP ip probe {interface} {addr} failed on {node[u'host']}"
227
228         with PapiSocketExecutor(node) as papi_exec:
229             papi_exec.add(cmd, **args).get_reply(err_msg)
230
231     @staticmethod
232     def ip_addresses_should_be_equal(ip1, ip2):
233         """Fails if the given IP addresses are unequal.
234
235         :param ip1: IPv4 or IPv6 address.
236         :param ip2: IPv4 or IPv6 address.
237         :type ip1: str
238         :type ip2: str
239         """
240         addr1 = ip_address(ip1)
241         addr2 = ip_address(ip2)
242
243         if addr1 != addr2:
244             raise AssertionError(f"IP addresses are not equal: {ip1} != {ip2}")
245
246     @staticmethod
247     def setup_network_namespace(
248             node, namespace_name, interface_name, ip_addr, prefix):
249         """Setup namespace on given node and attach interface and IP to
250         this namespace. Applicable also on TG node.
251
252         :param node: VPP node.
253         :param namespace_name: Namespace name.
254         :param interface_name: Interface name.
255         :param ip_addr: IP address of namespace's interface.
256         :param prefix: IP address prefix length.
257         :type node: dict
258         :type namespace_name: str
259         :type interface_name: str
260         :type ip_addr: str
261         :type prefix: int
262         """
263         cmd = f"ip netns add {namespace_name}"
264         exec_cmd_no_error(node, cmd, sudo=True)
265
266         cmd = f"ip link set dev {interface_name} up netns {namespace_name}"
267         exec_cmd_no_error(node, cmd, sudo=True)
268
269         cmd = f"ip netns exec {namespace_name} ip addr add {ip_addr}/{prefix}" \
270             f" dev {interface_name}"
271         exec_cmd_no_error(node, cmd, sudo=True)
272
273     @staticmethod
274     def linux_enable_forwarding(node, ip_ver=u"ipv4"):
275         """Enable forwarding on a Linux node, e.g. VM.
276
277         :param node: VPP node.
278         :param ip_ver: IP version, 'ipv4' or 'ipv6'.
279         :type node: dict
280         :type ip_ver: str
281         """
282         cmd = f"sysctl -w net.{ip_ver}.ip_forward=1"
283         exec_cmd_no_error(node, cmd, sudo=True)
284
285     @staticmethod
286     def get_linux_interface_name(node, pci_addr):
287         """Get the interface name.
288
289         :param node: VPP/TG node.
290         :param pci_addr: PCI address
291         :type node: dict
292         :type pci_addr: str
293         :returns: Interface name
294         :rtype: str
295         :raises RuntimeError: If cannot get the information about interfaces.
296         """
297         regex_intf_info = \
298             r"pci@([0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}.[0-9a-f])\s" \
299             r"*([a-zA-Z0-9]*)\s*network"
300
301         cmd = u"lshw -class network -businfo"
302         ret_code, stdout, stderr = exec_cmd(node, cmd, timeout=30, sudo=True)
303         if ret_code != 0:
304             raise RuntimeError(
305                 f"Could not get information about interfaces:\n{stderr}"
306             )
307
308         for line in stdout.splitlines()[2:]:
309             try:
310                 if re.search(regex_intf_info, line).group(1) == pci_addr:
311                     return re.search(regex_intf_info, line).group(2)
312             except AttributeError:
313                 continue
314         return None
315
316     @staticmethod
317     def set_linux_interface_up(node, interface):
318         """Set the specified interface up.
319
320         :param node: VPP/TG node.
321         :param interface: Interface in namespace.
322         :type node: dict
323         :type interface: str
324         :raises RuntimeError: If the interface could not be set up.
325         """
326         cmd = f"ip link set {interface} up"
327         exec_cmd_no_error(node, cmd, timeout=30, sudo=True)
328
329     @staticmethod
330     def set_linux_interface_ip(
331             node, interface, ip_addr, prefix, namespace=None):
332         """Set IP address to interface in linux.
333
334         :param node: VPP/TG node.
335         :param interface: Interface in namespace.
336         :param ip_addr: IP to be set on interface.
337         :param prefix: IP prefix.
338         :param namespace: Execute command in namespace. Optional
339         :type node: dict
340         :type interface: str
341         :type ip_addr: str
342         :type prefix: int
343         :type namespace: str
344         :raises RuntimeError: IP could not be set.
345         """
346         if namespace is not None:
347             cmd = f"ip netns exec {namespace} ip addr add {ip_addr}/{prefix}" \
348                 f" dev {interface}"
349         else:
350             cmd = f"ip addr add {ip_addr}/{prefix} dev {interface}"
351
352         exec_cmd_no_error(node, cmd, timeout=5, sudo=True)
353
354     @staticmethod
355     def add_linux_route(node, ip_addr, prefix, gateway, namespace=None):
356         """Add linux route in namespace.
357
358         :param node: Node where to execute command.
359         :param ip_addr: Route destination IP address.
360         :param prefix: IP prefix.
361         :param namespace: Execute command in namespace. Optional.
362         :param gateway: Gateway address.
363         :type node: dict
364         :type ip_addr: str
365         :type prefix: int
366         :type gateway: str
367         :type namespace: str
368         """
369         if namespace is not None:
370             cmd = f"ip netns exec {namespace} ip route add {ip_addr}/{prefix}" \
371                 f" via {gateway}"
372         else:
373             cmd = f"ip route add {ip_addr}/{prefix} via {gateway}"
374
375         exec_cmd_no_error(node, cmd, sudo=True)
376
377     @staticmethod
378     def vpp_interface_set_ip_address(
379             node, interface, address, prefix_length=None):
380         """Set IP address to VPP interface.
381
382         :param node: VPP node.
383         :param interface: Interface name.
384         :param address: IP address.
385         :param prefix_length: Prefix length.
386         :type node: dict
387         :type interface: str
388         :type address: str
389         :type prefix_length: int
390         """
391         ip_addr = ip_address(address)
392
393         cmd = u"sw_interface_add_del_address"
394         args = dict(
395             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
396             is_add=True,
397             del_all=False,
398             prefix=IPUtil.create_prefix_object(
399                 ip_addr,
400                 prefix_length if prefix_length else 128
401                 if ip_addr.version == 6 else 32
402             )
403         )
404         err_msg = f"Failed to add IP address on interface {interface}"
405
406         with PapiSocketExecutor(node) as papi_exec:
407             papi_exec.add(cmd, **args).get_reply(err_msg)
408
409     @staticmethod
410     def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address):
411         """Add IP neighbor on DUT node.
412
413         :param node: VPP node.
414         :param iface_key: Interface key.
415         :param ip_addr: IP address of the interface.
416         :param mac_address: MAC address of the interface.
417         :type node: dict
418         :type iface_key: str
419         :type ip_addr: str
420         :type mac_address: str
421         """
422         dst_ip = ip_address(ip_addr)
423
424         neighbor = dict(
425             sw_if_index=Topology.get_interface_sw_index(node, iface_key),
426             flags=0,
427             mac_address=str(mac_address),
428             ip_address=str(dst_ip)
429         )
430         cmd = u"ip_neighbor_add_del"
431         args = dict(
432             is_add=1,
433             neighbor=neighbor
434         )
435         err_msg = f"Failed to add IP neighbor on interface {iface_key}"
436
437         with PapiSocketExecutor(node) as papi_exec:
438             papi_exec.add(cmd, **args).get_reply(err_msg)
439
440     @staticmethod
441     def union_addr(ip_addr):
442         """Creates union IP address.
443
444         :param ip_addr: IPv4 or IPv6 address.
445         :type ip_addr: IPv4Address or IPv6Address
446         :returns: Union IP address.
447         :rtype: dict
448         """
449         return dict(ip6=ip_addr.packed) if ip_addr.version == 6 \
450             else dict(ip4=ip_addr.packed)
451
452     @staticmethod
453     def create_ip_address_object(ip_addr):
454         """Create IP address object.
455
456         :param ip_addr: IPv4 or IPv6 address
457         :type ip_addr: IPv4Address or IPv6Address
458         :returns: IP address object.
459         :rtype: dict
460         """
461         return dict(
462             af=getattr(
463                 AddressFamily, u"ADDRESS_IP6" if ip_addr.version == 6
464                 else u"ADDRESS_IP4"
465             ).value,
466             un=IPUtil.union_addr(ip_addr)
467         )
468
469     @staticmethod
470     def create_prefix_object(ip_addr, addr_len):
471         """Create prefix object.
472
473         :param ip_addr: IPv4 or IPv6 address.
474         :para, addr_len: Length of IP address.
475         :type ip_addr: IPv4Address or IPv6Address
476         :type addr_len: int
477         :returns: Prefix object.
478         :rtype: dict
479         """
480         addr = IPUtil.create_ip_address_object(ip_addr)
481
482         return dict(
483             len=int(addr_len),
484             address=addr
485         )
486
487     @staticmethod
488     def compose_vpp_route_structure(node, network, prefix_len, **kwargs):
489         """Create route object for ip_route_add_del api call.
490
491         :param node: VPP node.
492         :param network: Route destination network address.
493         :param prefix_len: Route destination network prefix length.
494         :param kwargs: Optional key-value arguments:
495
496             gateway: Route gateway address. (str)
497             interface: Route interface. (str)
498             vrf: VRF table ID. (int)
499             count: number of IP addresses to add starting from network IP (int)
500             local: The route is local with same prefix (increment is 1).
501                 If None, then is not used. (bool)
502             lookup_vrf: VRF table ID for lookup. (int)
503             multipath: Enable multipath routing. (bool)
504             weight: Weight value for unequal cost multipath routing. (int)
505
506         :type node: dict
507         :type network: str
508         :type prefix_len: int
509         :type kwargs: dict
510         :returns: route parameter basic structure
511         :rtype: dict
512         """
513         interface = kwargs.get(u"interface", u"")
514         gateway = kwargs.get(u"gateway", u"")
515
516         net_addr = ip_address(network)
517
518         prefix = IPUtil.create_prefix_object(net_addr, prefix_len)
519
520         paths = list()
521         n_hop = dict(
522             address=IPUtil.union_addr(ip_address(gateway)) if gateway else 0,
523             via_label=MPLS_LABEL_INVALID,
524             obj_id=Constants.BITWISE_NON_ZERO
525         )
526         path = dict(
527             sw_if_index=InterfaceUtil.get_interface_index(node, interface)
528             if interface else Constants.BITWISE_NON_ZERO,
529             table_id=int(kwargs.get(u"lookup_vrf", 0)),
530             rpf_id=Constants.BITWISE_NON_ZERO,
531             weight=int(kwargs.get(u"weight", 1)),
532             preference=1,
533             type=getattr(
534                 FibPathType, u"FIB_PATH_TYPE_LOCAL"
535                 if kwargs.get(u"local", False)
536                 else u"FIB_PATH_TYPE_NORMAL"
537             ).value,
538             flags=getattr(FibPathFlags, u"FIB_PATH_FLAG_NONE").value,
539             proto=getattr(
540                 FibPathNhProto, u"FIB_PATH_NH_PROTO_IP6"
541                 if net_addr.version == 6
542                 else u"FIB_PATH_NH_PROTO_IP4"
543             ).value,
544             nh=n_hop,
545             n_labels=0,
546             label_stack=list(0 for _ in range(16))
547         )
548         paths.append(path)
549
550         route = dict(
551             table_id=int(kwargs.get(u"vrf", 0)),
552             prefix=prefix,
553             n_paths=len(paths),
554             paths=paths
555         )
556         return route
557
558     @staticmethod
559     def vpp_route_add(node, network, prefix_len, **kwargs):
560         """Add route to the VPP node.
561
562         :param node: VPP node.
563         :param network: Route destination network address.
564         :param prefix_len: Route destination network prefix length.
565         :param kwargs: Optional key-value arguments:
566
567             gateway: Route gateway address. (str)
568             interface: Route interface. (str)
569             vrf: VRF table ID. (int)
570             count: number of IP addresses to add starting from network IP (int)
571             local: The route is local with same prefix (increment is 1).
572                 If None, then is not used. (bool)
573             lookup_vrf: VRF table ID for lookup. (int)
574             multipath: Enable multipath routing. (bool)
575             weight: Weight value for unequal cost multipath routing. (int)
576
577         :type node: dict
578         :type network: str
579         :type prefix_len: int
580         :type kwargs: dict
581         """
582         count = kwargs.get(u"count", 1)
583
584         if count > 100:
585             gateway = kwargs.get(u"gateway", '')
586             interface = kwargs.get(u"interface", '')
587             vrf = kwargs.get(u"vrf", None)
588             multipath = kwargs.get(u"multipath", False)
589
590             with VatTerminal(node, json_param=False) as vat:
591
592                 vat.vat_terminal_exec_cmd_from_template(
593                     u"vpp_route_add.vat",
594                     network=network,
595                     prefix_length=prefix_len,
596                     via=f"via {gateway}" if gateway else u"",
597                     sw_if_index=f"sw_if_index "
598                     f"{InterfaceUtil.get_interface_index(node, interface)}"
599                     if interface else u"",
600                     vrf=f"vrf {vrf}" if vrf else u"",
601                     count=f"count {count}" if count else u"",
602                     multipath=u"multipath" if multipath else u""
603                 )
604             return
605
606         net_addr = ip_address(network)
607         cmd = u"ip_route_add_del"
608         args = dict(
609             is_add=1,
610             is_multipath=int(kwargs.get(u"multipath", False)),
611             route=None
612         )
613         err_msg = f"Failed to add route(s) on host {node[u'host']}"
614
615         with PapiSocketExecutor(node) as papi_exec:
616             for i in range(kwargs.get(u"count", 1)):
617                 args[u"route"] = IPUtil.compose_vpp_route_structure(
618                     node, net_addr + i, prefix_len, **kwargs
619                 )
620                 history = bool(not 1 < i < kwargs.get(u"count", 1))
621                 papi_exec.add(cmd, history=history, **args)
622             papi_exec.get_replies(err_msg)
623
624     @staticmethod
625     def flush_ip_addresses(node, interface):
626         """Flush all IP addresses from specified interface.
627
628         :param node: VPP node.
629         :param interface: Interface name.
630         :type node: dict
631         :type interface: str
632         """
633         cmd = u"sw_interface_add_del_address"
634         args = dict(
635             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
636             is_add=False,
637             del_all=True
638         )
639         err_msg = f"Failed to flush IP address on interface {interface}"
640
641         with PapiSocketExecutor(node) as papi_exec:
642             papi_exec.add(cmd, **args).get_reply(err_msg)
643
644     @staticmethod
645     def add_fib_table(node, table_id, ipv6=False):
646         """Create new FIB table according to ID.
647
648         :param node: Node to add FIB on.
649         :param table_id: FIB table ID.
650         :param ipv6: Is this an IPv6 table
651         :type node: dict
652         :type table_id: int
653         :type ipv6: bool
654         """
655         cmd = u"ip_table_add_del"
656         table = dict(
657             table_id=int(table_id),
658             is_ip6=int(ipv6)
659         )
660         args = dict(
661             table=table,
662             is_add=1
663         )
664         err_msg = f"Failed to add FIB table on host {node[u'host']}"
665
666         with PapiSocketExecutor(node) as papi_exec:
667             papi_exec.add(cmd, **args).get_reply(err_msg)