CSIT-843: Update actual topology in case of new/updated/deleted interface
[csit.git] / resources / libraries / python / VhostUser.py
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:
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 resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
17 from resources.libraries.python.topology import NodeType, Topology
18 from resources.libraries.python.InterfaceUtil import InterfaceUtil
19
20
21 class VhostUser(object):
22     """Vhost-user interfaces"""
23
24     @staticmethod
25     def vpp_create_vhost_user_interface(node, socket):
26         """Create Vhost-user interface on VPP node.
27
28         :param node: Node to create Vhost-user interface on.
29         :param socket: Vhost-user interface socket path.
30         :type node: dict
31         :type socket: str
32         :returns: SW interface index.
33         :rtype: int
34         :raises RuntimeError: If Vhost-user interface creation failed.
35         """
36         out = VatExecutor.cmd_from_template(node, 'create_vhost_user_if.vat',
37                                             sock=socket)
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)
47             return sw_if_idx
48         else:
49             raise RuntimeError('Create Vhost-user interface failed on node '
50                                '"{}"'.format(node['host']))
51
52     @staticmethod
53     def get_vhost_user_if_name_by_sock(node, socket):
54         """Get Vhost-user interface name by socket.
55
56         :param node: Node to get Vhost-user interface name on.
57         :param socket: Vhost-user interface socket path.
58         :type node: dict
59         :type socket: str
60         :returns: Interface name or None if not found.
61         :rtype: str
62         """
63         for interface in node['interfaces'].values():
64             if interface.get('socket') == socket:
65                 return interface.get('name')
66         return None
67
68     @staticmethod
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
71         interface dump.
72
73         :param node: VPP node to get interface data from.
74         :param sw_if_index: Idx of the specific interface.
75         :type node: dict
76         :type sw_if_index: str
77         :returns: l2_address of the given interface.
78         :rtype: str
79         """
80
81         with VatTerminal(node) as vat:
82             if_data = vat.vat_terminal_exec_cmd_from_template(
83                 "interface_dump.vat")
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])
87
88         return None
89
90     @staticmethod
91     def vpp_show_vhost(node):
92         """Get vhost-user data for the given node.
93
94         :param node: VPP node to get interface data from.
95         :type node: dict
96         :returns: nothing
97         """
98         vat = VatExecutor()
99         vat.execute_script("show_vhost.vat", node, json_out=False)
100
101     @staticmethod
102     def show_vpp_vhost_on_all_duts(nodes):
103         """Show Vhost User on all DUTs.
104
105         :param nodes: VPP nodes.
106         :type nodes: dict
107         """
108         for node in nodes.values():
109             if node['type'] == NodeType.DUT:
110                 VhostUser.vpp_show_vhost(node)