Python3: resources and libraries
[csit.git] / resources / libraries / python / Tap.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 """Tap utilities library."""
15
16 from ipaddress import ip_address
17 from robot.api import logger
18
19 from resources.libraries.python.Constants import Constants
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
21 from resources.libraries.python.L2Util import L2Util
22 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
23 from resources.libraries.python.topology import Topology
24
25
26 class Tap:
27     """Tap utilities."""
28
29     @staticmethod
30     def add_tap_interface(node, tap_name, mac=None):
31         """Add tap interface with name and optionally with MAC.
32
33         :param node: Node to add tap on.
34         :param tap_name: Tap interface name for linux tap.
35         :param mac: Optional MAC address for VPP tap.
36         :type node: dict
37         :type tap_name: str
38         :type mac: str
39         :returns: Returns a interface index.
40         :rtype: int
41         """
42         cmd = u"tap_create_v2"
43         args = dict(
44             id=Constants.BITWISE_NON_ZERO,
45             use_random_mac=0 if mac else 1,
46             mac_address=L2Util.mac_to_bin(mac) if mac else 6 * b"\0",
47             host_namespace=64 * b"\0",
48             host_mac_addr=6 * b"\0",
49             host_if_name_set=1,
50             host_if_name=tap_name.encode(encoding=u"utf-8") +
51             (64 - len(tap_name)) * b"\0",
52             host_bridge=64 * b"\0",
53             host_ip4_addr=4 * b"\0",
54             host_ip6_addr=16 * b"\0",
55             host_ip4_gw=4 * b"\0",
56             host_ip6_gw=16 * b"\0"
57         )
58         err_msg = f"Failed to create tap interface {tap_name} " \
59             f"on host {node[u'host']}"
60
61         with PapiSocketExecutor(node) as papi_exec:
62             sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg)
63
64         if_key = Topology.add_new_port(node, u"tap")
65         Topology.update_interface_sw_if_index(node, if_key, sw_if_index)
66         Topology.update_interface_name(node, if_key, tap_name)
67         if mac is None:
68             mac = Tap.vpp_get_tap_interface_mac(node, tap_name)
69         Topology.update_interface_mac_address(node, if_key, mac)
70         tap_dev_name = Tap.vpp_get_tap_dev_name(node, tap_name)
71         Topology.update_interface_tap_dev_name(node, if_key, tap_dev_name)
72
73         return sw_if_index
74
75     @staticmethod
76     def vpp_get_tap_dev_name(node, host_if_name):
77         """Get VPP tap interface name from hardware interfaces dump.
78
79         :param node: DUT node.
80         :param host_if_name: Tap host interface name.
81         :type node: dict
82         :type host_if_name: str
83         :returns: VPP tap interface dev_name.
84         :rtype: str
85         """
86         return Tap.tap_dump(node, host_if_name).get(u"dev_name")
87
88     @staticmethod
89     def vpp_get_tap_interface_mac(node, interface_name):
90         """Get tap interface MAC address from interfaces dump.
91
92         :param node: DUT node.
93         :param interface_name: Tap interface name.
94         :type node: dict
95         :type interface_name: str
96         :returns: Tap interface MAC address.
97         :rtype: str
98         """
99         return InterfaceUtil.vpp_get_interface_mac(node, interface_name)
100
101     @staticmethod
102     def tap_dump(node, name=None):
103         """Get all TAP interface data from the given node, or data about
104         a specific TAP interface.
105
106         :param node: VPP node to get data from.
107         :param name: Optional name of a specific TAP interface.
108         :type node: dict
109         :type name: str
110         :returns: Dictionary of information about a specific TAP interface, or
111             a List of dictionaries containing all TAP data for the given node.
112         :rtype: dict or list
113         """
114         def process_tap_dump(tap_dump):
115             """Process tap dump.
116
117             :param tap_dump: Tap interface dump.
118             :type tap_dump: dict
119             :returns: Processed tap interface dump.
120             :rtype: dict
121             """
122             tap_dump[u"host_mac_addr"] = L2Util.bin_to_mac(
123                 tap_dump[u"host_mac_addr"]
124             )
125             tap_dump[u"host_ip4_addr"] = ip_address(tap_dump[u"host_ip4_addr"])
126             tap_dump[u"host_ip6_addr"] = ip_address(tap_dump[u"host_ip6_addr"])
127
128             return tap_dump
129
130         cmd = u"sw_interface_tap_v2_dump"
131         err_msg = f"Failed to get TAP dump on host {node[u'host']}"
132
133         with PapiSocketExecutor(node) as papi_exec:
134             details = papi_exec.add(cmd).get_details(err_msg)
135
136         data = list() if name is None else dict()
137         for dump in details:
138             if name is None:
139                 data.append(process_tap_dump(dump))
140             elif dump.get(u"host_if_name") == name:
141                 data = process_tap_dump(dump)
142                 break
143
144         logger.debug(f"TAP data:\n{data}")
145         return data