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:
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 """Interface setup library."""
17 from robot.api.deco import keyword
18 from resources.libraries.python.VatExecutor import VatExecutor
21 class InterfaceSetup(object):
22 """Interface setup utilities."""
24 __UDEV_IF_RULES_FILE = '/etc/udev/rules.d/10-network.rules'
27 def tg_set_interface_driver(node, pci_addr, driver):
28 """Set interface driver on the TG node.
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.
37 old_driver = InterfaceSetup.tg_get_interface_driver(node, pci_addr)
38 if old_driver == driver:
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(
48 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
49 if int(ret_code) != 0:
50 raise Exception("'{0}' failed on '{1}'".format(cmd,
53 # Bind to the new driver
54 cmd = 'sh -c "echo {0} > /sys/bus/pci/drivers/{1}/bind"'.format(
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']))
61 def tg_get_interface_driver(node, pci_addr):
62 """Get interface driver from the TG node.
64 :param node: Node to get interface driver on (must be TG node).
65 :param pci_addr: PCI address of the interface.
68 :return: Interface driver or None if not found.
72 # lspci -vmmks 0000:00:05.0
74 Class: Ethernet controller
76 Device: Virtio network device
85 cmd = 'lspci -vmmks {0}'.format(pci_addr)
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']))
91 for line in stdout.splitlines():
94 (name, value) = line.split("\t", 1)
101 def tg_set_interfaces_udev_rules(node):
102 """Set udev rules for interfaces.
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.
110 SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="52:54:00:e1:8a:0f",
113 :param node: Node to set udev rules on (must be TG node).
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']))
124 for if_k, if_v in node['interfaces'].items():
127 rule = 'SUBSYSTEM==\\"net\\", ACTION==\\"add\\", ATTR{address}' + \
128 '==\\"' + if_v['mac_address'] + '\\", 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,
137 cmd = '/etc/init.d/udev restart'
138 ssh.exec_command_sudo(cmd)
141 def tg_set_interfaces_default_driver(node):
142 """Set interfaces default driver specified in topology yaml file.
144 :param node: Node to setup interfaces driver on (must be TG node).
147 for if_k, if_v in node['interfaces'].items():
150 InterfaceSetup.tg_set_interface_driver(node, if_v['pci_address'],
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
159 Executes "vxlan_add_del_tunnel src {src} dst {dst} vni {vni}" VAT
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
169 :type destination_ip: str
170 :return: SW IF INDEX of created interface
174 output = VatExecutor.cmd_from_template(node, "vxlan_create.vat",
180 if output["retval"] == 0:
181 return output["sw_if_index"]
183 raise RuntimeError('Unable to create VXLAN interface on node {}'.\