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, Topology
18 from resources.libraries.python.InterfaceUtil import InterfaceUtil
21 class VhostUser(object):
22 """Vhost-user interfaces"""
25 def vpp_create_vhost_user_interface(node, socket):
26 """Create Vhost-user interface on VPP node.
28 :param node: Node to create Vhost-user interface on.
29 :param socket: Vhost-user interface socket path.
32 :returns: SW interface index.
34 :raises RuntimeError: If Vhost-user interface creation failed.
36 out = VatExecutor.cmd_from_template(node, 'create_vhost_user_if.vat',
38 if out[0].get('retval') == 0:
39 sw_if_idx = int(out[0].get('sw_if_index'))
40 if_key = Topology.add_new_port(node, 'vhost')
41 Topology.update_interface_sw_if_index(node, if_key, sw_if_idx)
42 ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx)
43 Topology.update_interface_name(node, if_key, ifc_name)
44 ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_idx)
45 Topology.update_interface_mac_address(node, if_key, ifc_mac)
46 Topology.update_interface_vhost_socket(node, if_key, socket)
49 raise RuntimeError('Create Vhost-user interface failed on node '
50 '"{}"'.format(node['host']))
53 def get_vhost_user_if_name_by_sock(node, socket):
54 """Get Vhost-user interface name by socket.
56 :param node: Node to get Vhost-user interface name on.
57 :param socket: Vhost-user interface socket path.
60 :returns: Interface name or None if not found.
63 for interface in node['interfaces'].values():
64 if interface.get('socket') == socket:
65 return interface.get('name')
69 def get_vhost_user_mac_by_sw_index(node, sw_if_index):
70 """Get Vhost-user l2_address for the given interface from actual
73 :param node: VPP node to get interface data from.
74 :param sw_if_index: Idx of the specific interface.
76 :type sw_if_index: str
77 :returns: l2_address of the given interface.
81 with VatTerminal(node) as vat:
82 if_data = vat.vat_terminal_exec_cmd_from_template(
84 for iface in if_data[0]:
85 if iface["sw_if_index"] == sw_if_index:
86 return ':'.join("%02x" % (b) for b in iface["l2_address"][:6])
91 def vpp_show_vhost(node):
92 """Get vhost-user data for the given node.
94 :param node: VPP node to get interface data from.
99 vat.execute_script("show_vhost.vat", node, json_out=False)
102 def show_vpp_vhost_on_all_duts(nodes):
103 """Show Vhost User on all DUTs.
105 :param nodes: VPP nodes.
108 for node in nodes.values():
109 if node['type'] == NodeType.DUT:
110 VhostUser.vpp_show_vhost(node)