T-Rex: 2.82, core pin, 8 workers
[csit.git] / resources / libraries / python / VhostUser.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Vhost-user interfaces library."""
15
16 from robot.api import logger
17
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
21
22
23 class VhostUser:
24     """Vhost-user interfaces L1 library."""
25
26     @staticmethod
27     def vpp_create_vhost_user_interface(
28             node, socket, is_server=False, enable_gso=False):
29         """Create Vhost-user interface on VPP node.
30
31         :param node: Node to create Vhost-user interface on.
32         :param socket: Vhost-user interface socket path.
33         :param is_server: Server side of connection. Default: False
34         :param enable_gso: Generic segmentation offloading. Default: False
35         :type node: dict
36         :type socket: str
37         :type is_server: bool
38         :type enable_gso: bool
39         :returns: SW interface index.
40         :rtype: int
41         """
42         cmd = u"create_vhost_user_if"
43         err_msg = f"Failed to create Vhost-user interface " \
44             f"on host {node[u'host']}"
45         args = dict(
46             is_server=bool(is_server),
47             sock_filename=str(socket),
48             enable_gso=bool(enable_gso)
49         )
50
51         with PapiSocketExecutor(node) as papi_exec:
52             sw_if_index = papi_exec.add(cmd, **args).get_sw_if_index(err_msg)
53
54         # Update the Topology:
55         if_key = Topology.add_new_port(node, u"vhost")
56         Topology.update_interface_sw_if_index(node, if_key, sw_if_index)
57
58         ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_index)
59         Topology.update_interface_name(node, if_key, ifc_name)
60
61         ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
62         Topology.update_interface_mac_address(node, if_key, ifc_mac)
63
64         Topology.update_interface_vhost_socket(node, if_key, socket)
65
66         return sw_if_index
67
68     @staticmethod
69     def get_vhost_user_if_name_by_sock(node, socket):
70         """Get Vhost-user interface name by socket.
71
72         :param node: Node to get Vhost-user interface name on.
73         :param socket: Vhost-user interface socket path.
74         :type node: dict
75         :type socket: str
76         :returns: Interface name or None if not found.
77         :rtype: str
78         """
79         for interface in node[u"interfaces"].values():
80             if interface.get(u"socket") == socket:
81                 return interface.get(u"name")
82         return None
83
84     @staticmethod
85     def get_vhost_user_mac_by_sw_index(node, sw_if_index):
86         """Get Vhost-user l2_address for the given interface from actual
87         interface dump.
88
89         :param node: VPP node to get interface data from.
90         :param sw_if_index: SW index of the specific interface.
91         :type node: dict
92         :type sw_if_index: str
93         :returns: l2_address of the given interface.
94         :rtype: str
95         """
96         return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
97
98     @staticmethod
99     def show_vpp_vhost_on_all_duts(nodes):
100         """Show Vhost-user on all DUTs.
101
102         :param nodes: VPP nodes.
103         :type nodes: dict
104         """
105         for node in nodes.values():
106             if node[u"type"] == NodeType.DUT:
107                 VhostUser.vhost_user_dump(node)
108
109     @staticmethod
110     def vhost_user_dump(node):
111         """Get vhost-user data for the given node.
112
113         :param node: VPP node to get interface data from.
114         :type node: dict
115         :returns: List of dictionaries with all vhost-user interfaces.
116         :rtype: list
117         """
118         cmd = u"sw_interface_vhost_user_dump"
119         err_msg = f"Failed to get vhost-user dump on host {node['host']}"
120
121         with PapiSocketExecutor(node) as papi_exec:
122             details = papi_exec.add(cmd).get_details(err_msg)
123
124         logger.debug(f"Vhost-user details:\n{details}")
125         return details