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
24 def add_tap_interface(node, tap_name, mac=None):
25 """Add tap interface with name and optionally with MAC.
27 :param node: Node to add tap on.
28 :param tap_name: Tap interface name for linux tap.
29 :param mac: Optional MAC address for VPP tap.
33 :return: Returns a interface index.
38 args = 'tapname {} mac {}'.format(tap_name, mac)
40 args = 'tapname {}'.format(tap_name)
41 with VatTerminal(node) as vat:
42 resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
45 return resp[0]['sw_if_index']
48 def modify_tap_interface(node, if_index, tap_name, mac=None):
49 """Modify tap interface like linux interface name or VPP MAC.
51 :param node: Node to modify tap on.
52 :param if_index: Index of tap interface to be modified.
53 :param tap_name: Tap interface name for linux tap.
54 :param mac: Optional MAC address for VPP tap.
59 :return: Returns a interface index.
64 args = 'sw_if_index {} tapname {} mac {}'.format(
65 if_index, tap_name, mac)
67 args = 'sw_if_index {} tapname {}'.format(if_index, tap_name)
68 with VatTerminal(node) as vat:
69 resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
72 return resp[0]['sw_if_index']
75 def delete_tap_interface(node, if_index):
76 """Delete tap interface.
78 :param node: Node to delete tap on.
79 :param if_index: Index of tap interface to be deleted.
82 :raises RuntimeError: Deletion was not successful.
85 args = 'sw_if_index {}'.format(if_index)
86 with VatTerminal(node) as vat:
87 resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
90 if int(resp[0]['retval']) != 0:
92 'Could not remove tap interface: {}'.format(resp))
95 def check_tap_present(node, tap_name):
96 """Check whether specific tap interface exists.
98 :param node: Node to check tap on.
99 :param tap_name: Tap interface name for linux tap.
102 :raises RuntimeError: Specified interface was not found.
104 tap_if = InterfaceUtil.tap_dump(node, tap_name)
107 'Tap interface :{} does not exist'.format(tap_name))