Add VM to NodeType
[csit.git] / resources / libraries / python / InterfaceSetup.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 """Interface setup library."""
15
16 from ssh import SSH
17 from robot.api.deco import keyword
18 from resources.libraries.python.VatExecutor import VatExecutor
19
20
21 class InterfaceSetup(object):
22     """Interface setup utilities."""
23
24     __UDEV_IF_RULES_FILE = '/etc/udev/rules.d/10-network.rules'
25
26     @staticmethod
27     def tg_set_interface_driver(node, pci_addr, driver):
28         """Set interface driver on the TG node.
29
30         :param node: Node to set interface driver on (must be TG node).
31         :param pci_addr: PCI address of the interface.
32         :param driver: Driver name.
33         :type node: dict
34         :type pci_addr: str
35         :type driver: str
36         """
37         old_driver = InterfaceSetup.tg_get_interface_driver(node, pci_addr)
38         if old_driver == driver:
39             return
40
41         ssh = SSH()
42         ssh.connect(node)
43
44         # Unbind from current driver
45         if old_driver is not None:
46             cmd = 'sh -c "echo {0} > /sys/bus/pci/drivers/{1}/unbind"'.format(
47                 pci_addr, old_driver)
48             (ret_code, _, _) = ssh.exec_command_sudo(cmd)
49             if int(ret_code) != 0:
50                 raise Exception("'{0}' failed on '{1}'".format(cmd,
51                                                                node['host']))
52
53         # Bind to the new driver
54         cmd = 'sh -c "echo {0} > /sys/bus/pci/drivers/{1}/bind"'.format(
55             pci_addr, driver)
56         (ret_code, _, _) = ssh.exec_command_sudo(cmd)
57         if int(ret_code) != 0:
58             raise Exception("'{0}' failed on '{1}'".format(cmd, node['host']))
59
60     @staticmethod
61     def tg_get_interface_driver(node, pci_addr):
62         """Get interface driver from the TG node.
63
64         :param node: Node to get interface driver on (must be TG node).
65         :param pci_addr: PCI address of the interface.
66         :type node: dict
67         :type pci_addr: str
68         :return: Interface driver or None if not found.
69         :rtype: str
70
71         .. note::
72             # lspci -vmmks 0000:00:05.0
73             Slot:   00:05.0
74             Class:  Ethernet controller
75             Vendor: Red Hat, Inc
76             Device: Virtio network device
77             SVendor:        Red Hat, Inc
78             SDevice:        Device 0001
79             PhySlot:        5
80             Driver: virtio-pci
81         """
82         ssh = SSH()
83         ssh.connect(node)
84
85         cmd = 'lspci -vmmks {0}'.format(pci_addr)
86
87         (ret_code, stdout, _) = ssh.exec_command(cmd)
88         if int(ret_code) != 0:
89             raise Exception("'{0}' failed on '{1}'".format(cmd, node['host']))
90
91         for line in stdout.splitlines():
92             if len(line) == 0:
93                 continue
94             (name, value) = line.split("\t", 1)
95             if name == 'Driver:':
96                 return value
97
98         return None
99
100     @staticmethod
101     def tg_set_interfaces_udev_rules(node):
102         """Set udev rules for interfaces.
103
104         Create udev rules file in /etc/udev/rules.d where are rules for each
105         interface used by TG node, based on MAC interface has specific name.
106         So after unbind and bind again to kernel driver interface has same
107         name as before. This must be called after TG has set name for each
108         port in topology dictionary.
109         udev rule example
110         SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="52:54:00:e1:8a:0f",
111         NAME="eth1"
112
113         :param node: Node to set udev rules on (must be TG node).
114         :type node: dict
115         """
116         ssh = SSH()
117         ssh.connect(node)
118
119         cmd = 'rm -f {0}'.format(InterfaceSetup.__UDEV_IF_RULES_FILE)
120         (ret_code, _, _) = ssh.exec_command_sudo(cmd)
121         if int(ret_code) != 0:
122             raise Exception("'{0}' failed on '{1}'".format(cmd, node['host']))
123
124         for if_k, if_v in node['interfaces'].items():
125             if if_k == 'mgmt':
126                 continue
127             rule = 'SUBSYSTEM==\\"net\\", ACTION==\\"add\\", ATTR{address}' + \
128                 '==\\"' + if_v['mac_address'] + '\\", NAME=\\"' + \
129                 if_v['name'] + '\\"'
130             cmd = 'sh -c "echo \'{0}\' >> {1}"'.format(
131                 rule, InterfaceSetup.__UDEV_IF_RULES_FILE)
132             (ret_code, _, _) = ssh.exec_command_sudo(cmd)
133             if int(ret_code) != 0:
134                 raise Exception("'{0}' failed on '{1}'".format(cmd,
135                                                                node['host']))
136
137         cmd = '/etc/init.d/udev restart'
138         ssh.exec_command_sudo(cmd)
139
140     @staticmethod
141     def tg_set_interfaces_default_driver(node):
142         """Set interfaces default driver specified in topology yaml file.
143
144         :param node: Node to setup interfaces driver on (must be TG node).
145         :type node: dict
146         """
147         for if_k, if_v in node['interfaces'].items():
148             if if_k == 'mgmt':
149                 continue
150             InterfaceSetup.tg_set_interface_driver(node, if_v['pci_address'],
151                                                    if_v['driver'])
152
153     @staticmethod
154     @keyword('Create VXLAN interface on "${DUT}" with VNI "${VNI}"'
155              ' from "${SRC_IP}" to "${DST_IP}"')
156     def create_vxlan_interface(node, vni, source_ip, destination_ip):
157         """Create VXLAN interface and return index of created interface
158
159         Executes "vxlan_add_del_tunnel src {src} dst {dst} vni {vni}" VAT
160         command on the node.
161
162         :param node: Node where to create VXLAN interface
163         :param vni: VXLAN Network Identifier
164         :param source_ip: Source IP of a VXLAN Tunnel End Point
165         :param destination_ip: Destination IP of a VXLAN Tunnel End Point
166         :type node: dict
167         :type vni: int
168         :type source_ip: str
169         :type destination_ip: str
170         :return: SW IF INDEX of created interface
171         :rtype: int
172         """
173
174         output = VatExecutor.cmd_from_template(node, "vxlan_create.vat",
175                                                src=source_ip,
176                                                dst=destination_ip,
177                                                vni=vni)
178         output = output[0]
179
180         if output["retval"] == 0:
181             return output["sw_if_index"]
182         else:
183             raise RuntimeError('Unable to create VXLAN interface on node {}'.\
184                                format(node))