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 """Vhost-user interfaces library."""
16 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
17 from resources.libraries.python.topology import NodeType
20 class VhostUser(object):
21 """Vhost-user interfaces"""
24 def vpp_create_vhost_user_interface(node, socket):
25 """Create Vhost-user interface on VPP node.
27 :param node: Node to create Vhost-user interface on.
28 :param socket: Vhost-user interface socket path.
31 :return: SW interface index.
34 out = VatExecutor.cmd_from_template(node, "create_vhost_user_if.vat",
36 if out[0].get('retval') == 0:
37 return out[0].get('sw_if_index')
39 raise RuntimeError('Create Vhost-user interface failed on node '
40 '"{}"'.format(node['host']))
43 def get_vhost_user_if_name_by_sock(node, socket):
44 """Get Vhost-user interface name by socket.
46 :param node: Node to get Vhost-user interface name on.
47 :param socket: Vhost-user interface socket path.
50 :return: Interface name or None if not found.
53 for interface in node['interfaces'].values():
54 if interface.get('socket') == socket:
55 return interface.get('name')
59 def get_vhost_user_mac_by_sw_index(node, sw_if_index):
60 """Get Vhost-user l2_address for the given interface from actual
63 :param node: VPP node to get interface data from.
64 :param sw_if_index: Idx of the specific interface.
66 :type sw_if_index: str
67 :return: l2_address of the given interface.
71 with VatTerminal(node) as vat:
72 if_data = vat.vat_terminal_exec_cmd_from_template(
74 for iface in if_data[0]:
75 if iface["sw_if_index"] == sw_if_index:
76 return ':'.join("%02x" % (b) for b in iface["l2_address"][:6])
81 def vpp_show_vhost(node):
82 """Get vhost-user data for the given node.
84 :param node: VPP node to get interface data from.
89 vat.execute_script("show_vhost.vat", node, json_out=False)
92 def show_vpp_vhost_on_all_duts(nodes):
93 """Show Vhost User on all DUTs.
95 :param nodes: VPP nodes.
98 for node in nodes.values():
99 if node['type'] == NodeType.DUT:
100 VhostUser.vpp_show_vhost(node)