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."""
19 class InterfaceSetup(object):
20 """Interface setup utilities."""
22 __UDEV_IF_RULES_FILE = '/etc/udev/rules.d/10-network.rules'
28 def tg_set_interface_driver(node, pci_addr, driver):
29 """Set interface driver on the TG node.
31 :param node: Node to set interface driver on (must be TG node).
32 :param interface: Interface name.
33 :param driver: Driver name.
38 old_driver = InterfaceSetup.tg_get_interface_driver(node, pci_addr)
39 if old_driver == driver:
45 # Unbind from current driver
46 if old_driver is not None:
47 cmd = 'sh -c "echo {0} > /sys/bus/pci/drivers/{1}/unbind"'.format(
49 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
50 if int(ret_code) != 0:
51 raise Exception("'{0}' failed on '{1}'".format(cmd,
54 # Bind to the new driver
55 cmd = 'sh -c "echo {0} > /sys/bus/pci/drivers/{1}/bind"'.format(
57 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
58 if int(ret_code) != 0:
59 raise Exception("'{0}' failed on '{1}'".format(cmd, node['host']))
62 def tg_get_interface_driver(node, pci_addr):
63 """Get interface driver from the TG node.
65 :param node: Node to get interface driver on (must be TG node).
66 :param interface: Interface name.
69 :return: Interface driver or None if not found.
73 # lspci -vmmks 0000:00:05.0
75 Class: Ethernet controller
77 Device: Virtio network device
86 cmd = 'lspci -vmmks {0}'.format(pci_addr)
88 (ret_code, stdout, _) = ssh.exec_command(cmd)
89 if int(ret_code) != 0:
90 raise Exception("'{0}' failed on '{1}'".format(cmd, node['host']))
92 for line in stdout.splitlines():
95 (name, value) = line.split("\t", 1)
102 def tg_set_interfaces_udev_rules(node):
103 """Set udev rules for interfaces.
105 Create udev rules file in /etc/udev/rules.d where are rules for each
106 interface used by TG node, based on MAC interface has specific name.
107 So after unbind and bind again to kernel driver interface has same
108 name as before. This must be called after TG has set name for each
109 port in topology dictionary.
111 SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="52:54:00:e1:8a:0f",
114 :param node: Node to set udev rules on (must be TG node).
120 cmd = 'rm -f {0}'.format(InterfaceSetup.__UDEV_IF_RULES_FILE)
121 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
122 if int(ret_code) != 0:
123 raise Exception("'{0}' failed on '{1}'".format(cmd, node['host']))
125 for if_k, if_v in node['interfaces'].items():
128 rule = 'SUBSYSTEM==\\"net\\", ACTION==\\"add\\", ATTR{address}' + \
129 '==\\"' + if_v['mac_address'] + '\\", NAME=\\"' + \
131 cmd = 'sh -c "echo \'{0}\' >> {1}"'.format(
132 rule, InterfaceSetup.__UDEV_IF_RULES_FILE)
133 (ret_code, _, _) = ssh.exec_command_sudo(cmd)
134 if int(ret_code) != 0:
135 raise Exception("'{0}' failed on '{1}'".format(cmd,
138 cmd = '/etc/init.d/udev restart'
139 ssh.exec_command_sudo(cmd)
142 def tg_set_interfaces_default_driver(node):
143 """Set interfaces default driver specified in topology yaml file.
145 :param node: Node to setup interfaces driver on (must be TG node).
148 for if_k, if_v in node['interfaces'].items():
151 InterfaceSetup.tg_set_interface_driver(node, if_v['pci_address'],