1 # Copyright (c) 2016 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 """Tap utilities library."""
16 from resources.libraries.python.VatExecutor import VatTerminal
17 from resources.libraries.python.InterfaceUtil import InterfaceUtil
18 from resources.libraries.python.topology import Topology
25 def add_tap_interface(node, tap_name, mac=None):
26 """Add tap interface with name and optionally with MAC.
28 :param node: Node to add tap on.
29 :param tap_name: Tap interface name for linux tap.
30 :param mac: Optional MAC address for VPP tap.
34 :returns: Returns a interface index.
39 args = 'tapname {} mac {}'.format(tap_name, mac)
41 args = 'tapname {}'.format(tap_name)
42 with VatTerminal(node) as vat:
43 resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
46 sw_if_idx = resp[0]['sw_if_index']
47 if_key = Topology.add_new_port(node, 'tap')
48 Topology.update_interface_sw_if_index(node, if_key, sw_if_idx)
49 ifc_name = Tap.vpp_get_tap_interface_name(node, sw_if_idx)
50 Topology.update_interface_name(node, if_key, ifc_name)
52 mac = Tap.vpp_get_tap_interface_mac(node, sw_if_idx)
53 Topology.update_interface_mac_address(node, if_key, mac)
54 Topology.update_interface_tap_dev_name(node, if_key, tap_name)
59 def modify_tap_interface(node, if_index, tap_name, mac=None):
60 """Modify tap interface like linux interface name or VPP MAC.
62 :param node: Node to modify tap on.
63 :param if_index: Index of tap interface to be modified.
64 :param tap_name: Tap interface name for linux tap.
65 :param mac: Optional MAC address for VPP tap.
70 :return: Returns a interface index.
75 args = 'sw_if_index {} tapname {} mac {}'.format(
76 if_index, tap_name, mac)
78 args = 'sw_if_index {} tapname {}'.format(if_index, tap_name)
79 with VatTerminal(node) as vat:
80 resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
83 if_key = Topology.get_interface_by_sw_index(node, if_index)
84 Topology.update_interface_tap_dev_name(node, if_key, tap_name)
86 Topology.update_interface_mac_address(node, if_key, mac)
88 return resp[0]['sw_if_index']
91 def delete_tap_interface(node, if_index):
92 """Delete tap interface.
94 :param node: Node to delete tap on.
95 :param if_index: Index of tap interface to be deleted.
98 :raises RuntimeError: Deletion was not successful.
101 args = 'sw_if_index {}'.format(if_index)
102 with VatTerminal(node) as vat:
103 resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
106 if int(resp[0]['retval']) != 0:
108 'Could not remove tap interface: {}'.format(resp))
109 if_key = Topology.get_interface_sw_index(node, if_index)
110 Topology.remove_port(node, if_key)
113 def check_tap_present(node, tap_name):
114 """Check whether specific tap interface exists.
116 :param node: Node to check tap on.
117 :param tap_name: Tap interface name for linux tap.
120 :raises RuntimeError: Specified interface was not found.
122 tap_if = InterfaceUtil.tap_dump(node, tap_name)
125 'Tap interface :{} does not exist'.format(tap_name))
128 def vpp_get_tap_interface_name(node, sw_if_idx):
129 """Get VPP tap interface name from hardware interfaces dump.
131 :param node: DUT node.
132 :param sw_if_idx: DUT node.
135 :returns: VPP tap interface name.
138 with VatTerminal(node, json_param=False) as vat:
139 response = vat.vat_terminal_exec_cmd_from_template(
140 'show_hardware_detail.vat')
142 for line in str(response[0]).splitlines():
143 if line.startswith('tap-'):
144 line_split = line.split()
145 if line_split[1] == sw_if_idx:
151 def vpp_get_tap_interface_mac(node, sw_if_idx):
152 """Get tap interface MAC address from hardware interfaces dump.
154 :param node: DUT node.
155 :param sw_if_idx: DUT node.
158 :returns: Tap interface MAC address.
161 with VatTerminal(node, json_param=False) as vat:
162 response = vat.vat_terminal_exec_cmd_from_template(
163 'show_hardware_detail.vat')
166 for line in str(response[0]).splitlines():
168 line_split = line.split()
169 return line_split[-1]
170 if line.startswith('tap-'):
171 line_split = line.split()
172 if line_split[1] == sw_if_idx: