feat(jobspec): Unify soak jobspecs
[csit.git] / resources / libraries / python / IPUtil.py
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:
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 import re
16
17 from enum import IntEnum
18
19 from ipaddress import ip_address
20
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
29
30
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
34
35
36 class FibPathType(IntEnum):
37     """FIB path types."""
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
46     FIB_PATH_TYPE_DVR = 8
47     FIB_PATH_TYPE_INTERFACE_RX = 9
48     FIB_PATH_TYPE_CLASSIFY = 10
49
50
51 class FibPathFlags(IntEnum):
52     """FIB path flags."""
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
57
58
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
66
67
68 class IPUtil:
69     """Common IP utilities"""
70
71     @staticmethod
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).
75
76         :param ip_str: IP address in string representation.
77         :type ip_str: str
78         :returns: Integer representation of IP address.
79         :rtype: int
80         """
81         return int(ip_address(ip_str))
82
83     @staticmethod
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).
87
88         :param ip_int: IP address in integer representation.
89         :type ip_int: int
90         :returns: String representation of IP address.
91         :rtype: str
92         """
93         return str(ip_address(ip_int))
94
95     @staticmethod
96     def vpp_get_interface_ip_addresses(node, interface, ip_version):
97         """Get list of IP addresses from an interface on a VPP node.
98
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).
102         :type node: dict
103         :type interface: str
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.
108         :rtype: list
109         """
110         sw_if_index = InterfaceUtil.get_interface_index(node, interface)
111
112         if not sw_if_index:
113             return list()
114
115         cmd = u"ip_address_dump"
116         args = dict(
117             sw_if_index=sw_if_index,
118             is_ipv6=bool(ip_version == u"ipv6")
119         )
120         err_msg = f"Failed to get L2FIB dump on host {node[u'host']}"
121
122         with PapiSocketExecutor(node) as papi_exec:
123             details = papi_exec.add(cmd, **args).get_details(err_msg)
124
125         # TODO: CSIT currently looks only whether the list is empty.
126         # Add proper value processing if values become important.
127
128         return details
129
130     @staticmethod
131     def vpp_get_ip_tables(node):
132         """Get dump of all IP FIB tables on a VPP node.
133
134         :param node: VPP node.
135         :type node: dict
136         """
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")
141
142     @staticmethod
143     def vpp_get_ip_table_summary(node):
144         """Get IPv4 FIB table summary on a VPP node.
145
146         :param node: VPP node.
147         :type node: dict
148         """
149         PapiSocketExecutor.run_cli_cmd(node, u"show ip fib summary")
150
151     @staticmethod
152     def vpp_get_ip_table(node):
153         """Get IPv4 FIB table on a VPP node.
154
155         :param node: VPP node.
156         :type node: dict
157         """
158         PapiSocketExecutor.run_cli_cmd(node, u"show ip fib")
159
160     @staticmethod
161     def vpp_get_ip_tables_prefix(node, address):
162         """Get dump of all IP FIB tables on a VPP node.
163
164         :param node: VPP node.
165         :param address: IP address.
166         :type node: dict
167         :type address: str
168         """
169         addr = ip_address(address)
170         ip_ver = u"ip6" if addr.version == 6 else u"ip"
171
172         PapiSocketExecutor.run_cli_cmd(
173             node, f"show {ip_ver} fib {addr}/{addr.max_prefixlen}"
174         )
175
176     @staticmethod
177     def get_interface_vrf_table(node, interface, ip_version='ipv4'):
178         """Get vrf ID for the given interface.
179
180         :param node: VPP node.
181         :param interface: Name or sw_if_index of a specific interface.
182         :type node: dict
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.
187         :rtype: int
188         """
189         sw_if_index = InterfaceUtil.get_interface_index(node, interface)
190
191         cmd = u"sw_interface_get_table"
192         args = dict(
193             sw_if_index=sw_if_index,
194             is_ipv6=bool(ip_version == u"ipv6")
195         )
196         err_msg = f"Failed to get VRF id assigned to interface {interface}"
197
198         with PapiSocketExecutor(node) as papi_exec:
199             reply = papi_exec.add(cmd, **args).get_reply(err_msg)
200
201         return reply[u"vrf_id"]
202
203     @staticmethod
204     def vpp_ip_source_check_setup(node, if_name):
205         """Setup Reverse Path Forwarding source check on interface.
206
207         :param node: VPP node.
208         :param if_name: Interface name to setup RPF source check.
209         :type node: dict
210         :type if_name: str
211         """
212         cmd = u"ip_source_check_interface_add_del"
213         args = dict(
214             sw_if_index=InterfaceUtil.get_interface_index(node, if_name),
215             is_add=1,
216             loose=0
217         )
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)
221
222     @staticmethod
223     def vpp_ip_probe(node, interface, addr):
224         """Run ip probe on VPP node.
225
226         :param node: VPP node.
227         :param interface: Interface key or name.
228         :param addr: IPv4/IPv6 address.
229         :type node: dict
230         :type interface: str
231         :type addr: str
232         """
233         cmd = u"ip_probe_neighbor"
234         args = dict(
235             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
236             dst=str(addr)
237         )
238         err_msg = f"VPP ip probe {interface} {addr} failed on {node[u'host']}"
239
240         with PapiSocketExecutor(node) as papi_exec:
241             papi_exec.add(cmd, **args).get_reply(err_msg)
242
243     @staticmethod
244     def ip_addresses_should_be_equal(ip1, ip2):
245         """Fails if the given IP addresses are unequal.
246
247         :param ip1: IPv4 or IPv6 address.
248         :param ip2: IPv4 or IPv6 address.
249         :type ip1: str
250         :type ip2: str
251         """
252         addr1 = ip_address(ip1)
253         addr2 = ip_address(ip2)
254
255         if addr1 != addr2:
256             raise AssertionError(f"IP addresses are not equal: {ip1} != {ip2}")
257
258     @staticmethod
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.
263
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.
269         :type node: dict
270         :type namespace_name: str
271         :type interface_name: str
272         :type ip_addr_list: list
273         :type prefix_length: int
274         """
275         Namespaces.create_namespace(node, namespace_name)
276
277         cmd = f"ip netns exec {namespace_name} ip link set {interface_name} up"
278         exec_cmd_no_error(node, cmd, sudo=True)
279
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)
284
285     @staticmethod
286     def linux_enable_forwarding(node, ip_ver=u"ipv4"):
287         """Enable forwarding on a Linux node, e.g. VM.
288
289         :param node: VPP node.
290         :param ip_ver: IP version, 'ipv4' or 'ipv6'.
291         :type node: dict
292         :type ip_ver: str
293         """
294         cmd = f"sysctl -w net.{ip_ver}.ip_forward=1"
295         exec_cmd_no_error(node, cmd, sudo=True)
296
297     @staticmethod
298     def get_linux_interface_name(node, pci_addr):
299         """Get the interface name.
300
301         :param node: VPP/TG node.
302         :param pci_addr: PCI address
303         :type node: dict
304         :type pci_addr: str
305         :returns: Interface name
306         :rtype: str
307         :raises RuntimeError: If cannot get the information about interfaces.
308         """
309         regex_intf_info = \
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"
312
313         cmd = u"lshw -class network -businfo"
314         ret_code, stdout, stderr = exec_cmd(node, cmd, timeout=30, sudo=True)
315         if ret_code != 0:
316             raise RuntimeError(
317                 f"Could not get information about interfaces:\n{stderr}"
318             )
319
320         for line in stdout.splitlines()[2:]:
321             try:
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:
325                 continue
326         return None
327
328     @staticmethod
329     def set_linux_interface_up(
330             node, interface, namespace=None):
331         """Set the specified interface up.
332         :param node: VPP/TG node.
333         :param interface: Interface in namespace.
334         :param namespace: Execute command in namespace. Optional
335         :type node: dict
336         :type interface: str
337         :type namespace: str
338         :raises RuntimeError: If the interface could not be set up.
339         """
340         if namespace is not None:
341             cmd = f"ip netns exec {namespace} ip link set dev {interface} up"
342         else:
343             cmd = f"ip link set dev {interface} up"
344         exec_cmd_no_error(node, cmd, timeout=30, sudo=True)
345
346
347     @staticmethod
348     def set_linux_interface_ip(
349             node, interface, ip_addr, prefix, namespace=None):
350         """Set IP address to interface in linux.
351
352         :param node: VPP/TG node.
353         :param interface: Interface in namespace.
354         :param ip_addr: IP to be set on interface.
355         :param prefix: IP prefix.
356         :param namespace: Execute command in namespace. Optional
357         :type node: dict
358         :type interface: str
359         :type ip_addr: str
360         :type prefix: int
361         :type namespace: str
362         :raises RuntimeError: IP could not be set.
363         """
364         if namespace is not None:
365             cmd = f"ip netns exec {namespace} ip addr add {ip_addr}/{prefix}" \
366                 f" dev {interface}"
367         else:
368             cmd = f"ip addr add {ip_addr}/{prefix} dev {interface}"
369
370         exec_cmd_no_error(node, cmd, timeout=5, sudo=True)
371
372     @staticmethod
373     def delete_linux_interface_ip(
374             node, interface, ip_addr, prefix_length, namespace=None):
375         """Delete IP address from interface in linux.
376
377         :param node: VPP/TG node.
378         :param interface: Interface in namespace.
379         :param ip_addr: IP to be deleted from interface.
380         :param prefix_length: IP prefix length.
381         :param namespace: Execute command in namespace. Optional
382         :type node: dict
383         :type interface: str
384         :type ip_addr: str
385         :type prefix_length: int
386         :type namespace: str
387         :raises RuntimeError: IP could not be deleted.
388         """
389         # TODO: Refactor command execution in namespaces into central
390         # methods (e.g. Namespace.exec_cmd_in_namespace)
391         if namespace is not None:
392             cmd = f"ip netns exec {namespace} ip addr del " \
393                 f"{ip_addr}/{prefix_length} dev {interface}"
394         else:
395             cmd = f"ip addr del {ip_addr}/{prefix_length} dev {interface}"
396
397         exec_cmd_no_error(node, cmd, timeout=5, sudo=True)
398
399     @staticmethod
400     def linux_interface_has_ip(
401             node, interface, ip_addr, prefix_length, namespace=None):
402         """Return True if interface in linux has IP address.
403
404         :param node: VPP/TG node.
405         :param interface: Interface in namespace.
406         :param ip_addr: IP to be queried on interface.
407         :param prefix_length: IP prefix length.
408         :param namespace: Execute command in namespace. Optional
409         :type node: dict
410         :type interface: str
411         :type ip_addr: str
412         :type prefix_length: int
413         :type namespace: str
414         :rtype boolean
415         :raises RuntimeError: Request fails.
416         """
417         ip_addr_with_prefix = f"{ip_addr}/{prefix_length}"
418         if namespace is not None:
419             cmd = f"ip netns exec {namespace} ip addr show dev {interface}"
420         else:
421             cmd = f"ip addr show dev {interface}"
422
423         cmd += u" | grep 'inet ' | awk -e '{print $2}'"
424         cmd += f" | grep '{ip_addr_with_prefix}'"
425         _, stdout, _ = exec_cmd(node, cmd, timeout=5, sudo=True)
426
427         has_ip = stdout.rstrip()
428         return bool(has_ip == ip_addr_with_prefix)
429
430     @staticmethod
431     def add_linux_route(node, ip_addr, prefix, gateway, namespace=None):
432         """Add linux route in namespace.
433
434         :param node: Node where to execute command.
435         :param ip_addr: Route destination IP address.
436         :param prefix: IP prefix.
437         :param namespace: Execute command in namespace. Optional.
438         :param gateway: Gateway address.
439         :type node: dict
440         :type ip_addr: str
441         :type prefix: int
442         :type gateway: str
443         :type namespace: str
444         """
445         if namespace is not None:
446             cmd = f"ip netns exec {namespace} ip route add {ip_addr}/{prefix}" \
447                 f" via {gateway}"
448         else:
449             cmd = f"ip route add {ip_addr}/{prefix} via {gateway}"
450
451         exec_cmd_no_error(node, cmd, sudo=True)
452
453     @staticmethod
454     def vpp_interface_set_ip_address(
455             node, interface, address, prefix_length=None):
456         """Set IP address to VPP interface.
457
458         :param node: VPP node.
459         :param interface: Interface name.
460         :param address: IP address.
461         :param prefix_length: Prefix length.
462         :type node: dict
463         :type interface: str
464         :type address: str
465         :type prefix_length: int
466         """
467         ip_addr = ip_address(address)
468
469         cmd = u"sw_interface_add_del_address"
470         args = dict(
471             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
472             is_add=True,
473             del_all=False,
474             prefix=IPUtil.create_prefix_object(
475                 ip_addr,
476                 prefix_length if prefix_length else 128
477                 if ip_addr.version == 6 else 32
478             )
479         )
480         err_msg = f"Failed to add IP address on interface {interface}"
481
482         with PapiSocketExecutor(node) as papi_exec:
483             papi_exec.add(cmd, **args).get_reply(err_msg)
484
485     @staticmethod
486     def vpp_interface_set_ip_addresses(node, interface, ip_addr_list,
487                                        prefix_length=None):
488         """Set IP addresses to VPP interface.
489
490         :param node: VPP node.
491         :param interface: Interface name.
492         :param ip_addr_list: IP addresses.
493         :param prefix_length: Prefix length.
494         :type node: dict
495         :type interface: str
496         :type ip_addr_list: list
497         :type prefix_length: int
498         """
499         for ip_addr in ip_addr_list:
500             IPUtil.vpp_interface_set_ip_address(node, interface, ip_addr,
501                                                 prefix_length)
502
503     @staticmethod
504     def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address):
505         """Add IP neighbor on DUT node.
506
507         :param node: VPP node.
508         :param iface_key: Interface key.
509         :param ip_addr: IP address of the interface.
510         :param mac_address: MAC address of the interface.
511         :type node: dict
512         :type iface_key: str
513         :type ip_addr: str
514         :type mac_address: str
515         """
516         dst_ip = ip_address(ip_addr)
517
518         neighbor = dict(
519             sw_if_index=Topology.get_interface_sw_index(node, iface_key),
520             flags=0,
521             mac_address=str(mac_address),
522             ip_address=str(dst_ip)
523         )
524         cmd = u"ip_neighbor_add_del"
525         args = dict(
526             is_add=True,
527             neighbor=neighbor
528         )
529         err_msg = f"Failed to add IP neighbor on interface {iface_key}"
530
531         with PapiSocketExecutor(node) as papi_exec:
532             papi_exec.add(cmd, **args).get_reply(err_msg)
533
534     @staticmethod
535     def create_prefix_object(ip_addr, addr_len):
536         """Create prefix object.
537
538         :param ip_addr: IPv4 or IPv6 address.
539         :param addr_len: Length of IP address.
540         :type ip_addr: IPv4Address or IPv6Address
541         :type addr_len: int
542         :returns: Prefix object.
543         :rtype: dict
544         """
545         addr = IPAddress.create_ip_address_object(ip_addr)
546
547         return dict(
548             len=int(addr_len),
549             address=addr
550         )
551
552     @staticmethod
553     def compose_vpp_route_structure(node, network, prefix_len, **kwargs):
554         """Create route object for ip_route_add_del api call.
555
556         :param node: VPP node.
557         :param network: Route destination network address.
558         :param prefix_len: Route destination network prefix length.
559         :param kwargs: Optional key-value arguments:
560
561             gateway: Route gateway address. (str)
562             interface: Route interface. (str)
563             vrf: VRF table ID. (int)
564             count: number of IP addresses to add starting from network IP (int)
565             local: The route is local with same prefix (increment is 1).
566                 If None, then is not used. (bool)
567             lookup_vrf: VRF table ID for lookup. (int)
568             multipath: Enable multipath routing. (bool)
569             weight: Weight value for unequal cost multipath routing. (int)
570
571         :type node: dict
572         :type network: str
573         :type prefix_len: int
574         :type kwargs: dict
575         :returns: route parameter basic structure
576         :rtype: dict
577         """
578         interface = kwargs.get(u"interface", u"")
579         gateway = kwargs.get(u"gateway", u"")
580
581         net_addr = ip_address(network)
582
583         prefix = IPUtil.create_prefix_object(net_addr, prefix_len)
584
585         paths = list()
586         n_hop = dict(
587             address=IPAddress.union_addr(ip_address(gateway)) if gateway else 0,
588             via_label=MPLS_LABEL_INVALID,
589             obj_id=Constants.BITWISE_NON_ZERO
590         )
591         path = dict(
592             sw_if_index=InterfaceUtil.get_interface_index(node, interface)
593             if interface else Constants.BITWISE_NON_ZERO,
594             table_id=int(kwargs.get(u"lookup_vrf", 0)),
595             rpf_id=Constants.BITWISE_NON_ZERO,
596             weight=int(kwargs.get(u"weight", 1)),
597             preference=1,
598             type=getattr(
599                 FibPathType, u"FIB_PATH_TYPE_LOCAL"
600                 if kwargs.get(u"local", False)
601                 else u"FIB_PATH_TYPE_NORMAL"
602             ).value,
603             flags=getattr(FibPathFlags, u"FIB_PATH_FLAG_NONE").value,
604             proto=getattr(
605                 FibPathNhProto, u"FIB_PATH_NH_PROTO_IP6"
606                 if net_addr.version == 6
607                 else u"FIB_PATH_NH_PROTO_IP4"
608             ).value,
609             nh=n_hop,
610             n_labels=0,
611             label_stack=list(0 for _ in range(16))
612         )
613         paths.append(path)
614
615         route = dict(
616             table_id=int(kwargs.get(u"vrf", 0)),
617             prefix=prefix,
618             n_paths=len(paths),
619             paths=paths
620         )
621         return route
622
623     @staticmethod
624     def vpp_route_add(node, network, prefix_len, **kwargs):
625         """Add route to the VPP node.
626
627         :param node: VPP node.
628         :param network: Route destination network address.
629         :param prefix_len: Route destination network prefix length.
630         :param kwargs: Optional key-value arguments:
631
632             gateway: Route gateway address. (str)
633             interface: Route interface. (str)
634             vrf: VRF table ID. (int)
635             count: number of IP addresses to add starting from network IP (int)
636             local: The route is local with same prefix (increment is 1).
637                 If None, then is not used. (bool)
638             lookup_vrf: VRF table ID for lookup. (int)
639             multipath: Enable multipath routing. (bool)
640             weight: Weight value for unequal cost multipath routing. (int)
641
642         :type node: dict
643         :type network: str
644         :type prefix_len: int
645         :type kwargs: dict
646         """
647         count = kwargs.get(u"count", 1)
648
649         if count > 100:
650             gateway = kwargs.get(u"gateway", '')
651             interface = kwargs.get(u"interface", '')
652             vrf = kwargs.get(u"vrf", None)
653             multipath = kwargs.get(u"multipath", False)
654
655             with VatTerminal(node, json_param=False) as vat:
656
657                 vat.vat_terminal_exec_cmd_from_template(
658                     u"vpp_route_add.vat",
659                     network=network,
660                     prefix_length=prefix_len,
661                     via=f"via {gateway}" if gateway else u"",
662                     sw_if_index=f"sw_if_index "
663                     f"{InterfaceUtil.get_interface_index(node, interface)}"
664                     if interface else u"",
665                     vrf=f"vrf {vrf}" if vrf else u"",
666                     count=f"count {count}" if count else u"",
667                     multipath=u"multipath" if multipath else u""
668                 )
669             return
670
671         net_addr = ip_address(network)
672         cmd = u"ip_route_add_del"
673         args = dict(
674             is_add=True,
675             is_multipath=kwargs.get(u"multipath", False),
676             route=None
677         )
678         err_msg = f"Failed to add route(s) on host {node[u'host']}"
679
680         with PapiSocketExecutor(node) as papi_exec:
681             for i in range(kwargs.get(u"count", 1)):
682                 args[u"route"] = IPUtil.compose_vpp_route_structure(
683                     node, net_addr + i, prefix_len, **kwargs
684                 )
685                 history = bool(not 1 < i < kwargs.get(u"count", 1))
686                 papi_exec.add(cmd, history=history, **args)
687             papi_exec.get_replies(err_msg)
688
689     @staticmethod
690     def flush_ip_addresses(node, interface):
691         """Flush all IP addresses from specified interface.
692
693         :param node: VPP node.
694         :param interface: Interface name.
695         :type node: dict
696         :type interface: str
697         """
698         cmd = u"sw_interface_add_del_address"
699         args = dict(
700             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
701             is_add=False,
702             del_all=True
703         )
704         err_msg = f"Failed to flush IP address on interface {interface}"
705
706         with PapiSocketExecutor(node) as papi_exec:
707             papi_exec.add(cmd, **args).get_reply(err_msg)
708
709     @staticmethod
710     def add_fib_table(node, table_id, ipv6=False):
711         """Create new FIB table according to ID.
712
713         :param node: Node to add FIB on.
714         :param table_id: FIB table ID.
715         :param ipv6: Is this an IPv6 table
716         :type node: dict
717         :type table_id: int
718         :type ipv6: bool
719         """
720         cmd = u"ip_table_add_del"
721         table = dict(
722             table_id=int(table_id),
723             is_ip6=ipv6
724         )
725         args = dict(
726             table=table,
727             is_add=True
728         )
729         err_msg = f"Failed to add FIB table on host {node[u'host']}"
730
731         with PapiSocketExecutor(node) as papi_exec:
732             papi_exec.add(cmd, **args).get_reply(err_msg)