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:
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 robot.api import logger
18 from resources.libraries.python.PapiExecutor import PapiExecutor
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
23 class VhostUser(object):
24 """Vhost-user interfaces L1 library."""
27 def _sw_interface_vhost_user_dump(node):
28 """Get the Vhost-user dump on the given node.
30 :param node: Given node to get Vhost dump from.
32 :returns: List of Vhost-user interfaces data extracted from Papi
36 with PapiExecutor(node) as papi_exec:
37 dump = papi_exec.add("sw_interface_vhost_user_dump").get_dump()
39 key = "sw_interface_vhost_user_details"
41 for item in dump.reply[0]["api_reply"]:
42 item[key]["interface_name"] = \
43 item[key]["interface_name"].rstrip('\x00')
44 item[key]["sock_filename"] = \
45 item[key]["sock_filename"].rstrip('\x00')
48 logger.debug("VhostUser data:\n{data}".format(data=data))
53 def vpp_create_vhost_user_interface(node, socket):
54 """Create Vhost-user interface on VPP node.
56 :param node: Node to create Vhost-user interface on.
57 :param socket: Vhost-user interface socket path.
60 :returns: SW interface index.
63 cmd = 'create_vhost_user_if'
64 err_msg = 'Failed to create Vhost-user interface on host {host}'.format(
67 sock_filename=str(socket)
69 with PapiExecutor(node) as papi_exec:
70 data = papi_exec.add(cmd, **args).get_replies(err_msg).\
71 verify_reply(err_msg=err_msg)
74 sw_if_idx = data["sw_if_index"]
76 # Update the Topology:
77 if_key = Topology.add_new_port(node, 'vhost')
78 Topology.update_interface_sw_if_index(node, if_key, sw_if_idx)
80 ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx)
81 Topology.update_interface_name(node, if_key, ifc_name)
83 ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_idx)
84 Topology.update_interface_mac_address(node, if_key, ifc_mac)
86 Topology.update_interface_vhost_socket(node, if_key, socket)
91 def get_vhost_user_if_name_by_sock(node, socket):
92 """Get Vhost-user interface name by socket.
94 :param node: Node to get Vhost-user interface name on.
95 :param socket: Vhost-user interface socket path.
98 :returns: Interface name or None if not found.
101 for interface in node['interfaces'].values():
102 if interface.get('socket') == socket:
103 return interface.get('name')
107 def get_vhost_user_mac_by_sw_index(node, sw_if_index):
108 """Get Vhost-user l2_address for the given interface from actual
111 :param node: VPP node to get interface data from.
112 :param sw_if_index: SW index of the specific interface.
114 :type sw_if_index: str
115 :returns: l2_address of the given interface.
119 return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
122 def vpp_show_vhost(node):
123 """Get Vhost-user data for the given node.
125 :param node: VPP node to get interface data from.
128 VhostUser._sw_interface_vhost_user_dump(node)
131 def show_vpp_vhost_on_all_duts(nodes):
132 """Show Vhost-user on all DUTs.
134 :param nodes: VPP nodes.
137 for node in nodes.values():
138 if node['type'] == NodeType.DUT:
139 VhostUser.vpp_show_vhost(node)