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