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 PapiSocketExecutor
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
24 """Vhost-user interfaces L1 library."""
27 def vpp_create_vhost_user_interface(node, socket):
28 """Create Vhost-user interface on VPP node.
30 :param node: Node to create Vhost-user interface on.
31 :param socket: Vhost-user interface socket path.
34 :returns: SW interface index.
37 cmd = u"create_vhost_user_if"
38 err_msg = f"Failed to create Vhost-user interface " \
39 f"on host {node[u'host']}"
41 sock_filename=str(socket)
44 with PapiSocketExecutor(node) as papi_exec:
45 sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg)
47 # Update the Topology:
48 if_key = Topology.add_new_port(node, u"vhost")
49 Topology.update_interface_sw_if_index(node, if_key, sw_if_index)
51 ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index)
52 Topology.update_interface_name(node, if_key, ifc_name)
54 ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
55 Topology.update_interface_mac_address(node, if_key, ifc_mac)
57 Topology.update_interface_vhost_socket(node, if_key, socket)
62 def get_vhost_user_if_name_by_sock(node, socket):
63 """Get Vhost-user interface name by socket.
65 :param node: Node to get Vhost-user interface name on.
66 :param socket: Vhost-user interface socket path.
69 :returns: Interface name or None if not found.
72 for interface in node[u"interfaces"].values():
73 if interface.get(u"socket") == socket:
74 return interface.get(u"name")
78 def get_vhost_user_mac_by_sw_index(node, sw_if_index):
79 """Get Vhost-user l2_address for the given interface from actual
82 :param node: VPP node to get interface data from.
83 :param sw_if_index: SW index of the specific interface.
85 :type sw_if_index: str
86 :returns: l2_address of the given interface.
89 return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
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[u"type"] == NodeType.DUT:
100 VhostUser.vhost_user_dump(node)
103 def vhost_user_dump(node):
104 """Get vhost-user data for the given node.
106 :param node: VPP node to get interface data from.
108 :returns: List of dictionaries with all vhost-user interfaces.
111 cmd = u"sw_interface_vhost_user_dump"
112 err_msg = f"Failed to get vhost-user dump on host {node['host']}"
114 with PapiSocketExecutor(node) as papi_exec:
115 details = papi_exec.add(cmd).get_details(err_msg)
117 logger.debug(f"Vhost-user details:\n{details}")