1 #!/usr/bin/env python2.7
2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """This executable python module gathers MAC address data from topology nodes.
16 It requires that all interfaces/port elements in topology have driver field.
17 This script binds the port in given node to set linux kernel driver and
18 extracts MAC address from it."""
24 from argparse import ArgumentParser
28 from resources.libraries.python.ssh import SSH
31 def load_topology(args):
32 """Load topology file referenced to by parameter passed to this script.
34 :param args: Arguments parsed from commandline.
35 :type args: ArgumentParser().parse_args()
36 :return: Python representation of topology YAML.
40 with open(args.topology, u"rt") as stream:
42 data = yaml.safe_load(stream)
43 except yaml.YAMLError as exc:
44 print(f"Failed to load topology file: {args.topology}")
51 def ssh_no_error(ssh, cmd):
52 """Execute a command over ssh channel, and log and exit if the command
55 :param ssh: SSH() object connected to a node.
56 :param cmd: Command line to execute on remote node.
57 :type ssh: SSH() object
59 :return: stdout from the SSH command.
62 ret, stdo, stde = ssh.exec_command(cmd)
64 print(f"Command execution failed: '{cmd}'")
65 print(f"stdout: {stdo}")
66 print(f"stderr: {stde}")
67 raise RuntimeError(u"Unexpected ssh command failure")
72 def update_mac_addresses_for_node(node):
73 """For given node loop over all ports with PCI address and look for its MAC
76 This function firstly unbinds the PCI device from its current driver
77 and binds it to linux kernel driver. After the device is bound to specific
78 linux kernel driver the MAC address is extracted from /sys/bus/pci location
79 and stored within the node dictionary that was passed to this function.
81 :param node: Node from topology.
84 for port_name, port in node[u"interfaces"].items():
85 if u"driver" not in port:
87 f"{node[u'host']} port {port_name} has no driver element, "
94 # TODO: make following SSH commands into one-liner to save on SSH opers
96 # First unbind from current driver
97 drvr_dir_path = f"/sys/bus/pci/devices/{port[u'pci_address']}/driver"
99 if [ -d {drvr_dir_path} ]; then
100 echo {port[u'pci_address']} | sudo tee {drvr_dir_path}/unbind ;
102 true Do not have to do anything, port already unbound ;
104 ssh_no_error(ssh, cmd)
106 # Then bind to the 'driver' from topology for given port
107 cmd = f"echo {port[u'pci_address']} | " \
108 f"sudo tee /sys/bus/pci/drivers/{port[u'driver']}/bind"
109 ssh_no_error(ssh, cmd)
111 # Then extract the mac address and store it in the topology
112 cmd = f"cat /sys/bus/pci/devices/{port['pci_address']}/net/*/address"
113 mac = ssh_no_error(ssh, cmd).strip()
114 pattern = re.compile(u"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$")
115 if not pattern.match(mac):
117 f"MAC address read from host {node[u'host']} "
118 f"{port[u'pci_address']} is in bad format '{mac}'"
121 f"{node[u'host']}: Found MAC address of PCI device "
122 f"{port[u'pci_address']}: {mac}"
124 port[u"mac_address"] = mac
127 def update_nodes_mac_addresses(topology):
128 """Loop over nodes in topology and get mac addresses for all listed ports
129 based on PCI addresses.
131 :param topology: Topology information with nodes.
134 for node in topology[u"nodes"].values():
135 update_mac_addresses_for_node(node)
138 def dump_updated_topology(topology, args):
139 """Writes or prints out updated topology file.
141 :param topology: Topology information with nodes.
142 :param args: Arguments parsed from command line.
144 :type args: ArgumentParser().parse_args()
145 :return: 1 if error occurred, 0 if successful.
150 if os.path.isfile(args.output_file):
152 f"File {args.output_file} already exists. If you want to "
153 f"overwrite this file, add -f as a parameter to this script"
156 with open(args.output_file, u"wt") as stream:
157 yaml.dump(topology, stream, default_flow_style=False)
159 print(yaml.dump(topology, default_flow_style=False))
165 parser = ArgumentParser()
166 parser.add_argument(u"topology", help=u"Topology yaml file to read")
167 parser.add_argument(u"--output-file", u"-o", help=u"Output file")
169 u"-f", u"--force", help=u"Overwrite existing file",
170 action=u"store_const", const=True
172 parser.add_argument(u"--verbose", u"-v", action=u"store_true")
173 args = parser.parse_args()
175 topology = load_topology(args)
176 update_nodes_mac_addresses(topology)
177 ret = dump_updated_topology(topology, args)
182 if __name__ == u"__main__":