0c7a79e266e0557f5f1c62eb9421092b6cb44f2a
[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 PapiExecutor
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.InterfaceUtil import InterfaceUtil
21
22
23 class VhostUser(object):
24     """Vhost-user interfaces L1 library."""
25
26     @staticmethod
27     def _sw_interface_vhost_user_dump(node):
28         """Get the Vhost-user dump on the given node.
29
30         :param node: Given node to get Vhost dump from.
31         :type node: dict
32         :returns: List of Vhost-user interfaces data extracted from Papi
33             response.
34         :rtype: list
35         """
36         with PapiExecutor(node) as papi_exec:
37             dump = papi_exec.add("sw_interface_vhost_user_dump").get_dump()
38
39         key = "sw_interface_vhost_user_details"
40         data = list()
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')
46             data.append(item)
47
48         logger.debug("VhostUser data:\n{data}".format(data=data))
49
50         return data
51
52     @staticmethod
53     def vpp_create_vhost_user_interface(node, socket):
54         """Create Vhost-user interface on VPP node.
55
56         :param node: Node to create Vhost-user interface on.
57         :param socket: Vhost-user interface socket path.
58         :type node: dict
59         :type socket: str
60         :returns: SW interface index.
61         :rtype: int
62         """
63         cmd = 'create_vhost_user_if'
64         err_msg = 'Failed to create Vhost-user interface on host {host}'.format(
65             host=node['host'])
66         args = dict(
67             sock_filename=str(socket)
68         )
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)
72
73         # Extract sw_if_idx:
74         sw_if_idx = data["sw_if_index"]
75
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)
79
80         ifc_name = InterfaceUtil.vpp_get_interface_name(node, sw_if_idx)
81         Topology.update_interface_name(node, if_key, ifc_name)
82
83         ifc_mac = InterfaceUtil.vpp_get_interface_mac(node, sw_if_idx)
84         Topology.update_interface_mac_address(node, if_key, ifc_mac)
85
86         Topology.update_interface_vhost_socket(node, if_key, socket)
87
88         return sw_if_idx
89
90     @staticmethod
91     def get_vhost_user_if_name_by_sock(node, socket):
92         """Get Vhost-user interface name by socket.
93
94         :param node: Node to get Vhost-user interface name on.
95         :param socket: Vhost-user interface socket path.
96         :type node: dict
97         :type socket: str
98         :returns: Interface name or None if not found.
99         :rtype: str
100         """
101         for interface in node['interfaces'].values():
102             if interface.get('socket') == socket:
103                 return interface.get('name')
104         return None
105
106     @staticmethod
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
109         interface dump.
110
111         :param node: VPP node to get interface data from.
112         :param sw_if_index: SW index of the specific interface.
113         :type node: dict
114         :type sw_if_index: str
115         :returns: l2_address of the given interface.
116         :rtype: str
117         """
118
119         return InterfaceUtil.vpp_get_interface_mac(node, sw_if_index)
120
121     @staticmethod
122     def vpp_show_vhost(node):
123         """Get Vhost-user data for the given node.
124
125         :param node: VPP node to get interface data from.
126         :type node: dict
127         """
128         VhostUser._sw_interface_vhost_user_dump(node)
129
130     @staticmethod
131     def show_vpp_vhost_on_all_duts(nodes):
132         """Show Vhost-user on all DUTs.
133
134         :param nodes: VPP nodes.
135         :type nodes: dict
136         """
137         for node in nodes.values():
138             if node['type'] == NodeType.DUT:
139                 VhostUser.vpp_show_vhost(node)