1 # Copyright (c) 2017 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 """Keywords used for setup and testing of Honeycomb's control-plane interface
18 from resources.libraries.python.ssh import SSH
21 class IPv6Management(object):
22 """Utilities for managing IPv6 contol-plane interfaces."""
28 def get_interface_name_by_mac(node, mac):
29 """Get the name of an interface using its MAC address.
31 :param node: Node in topology.
32 :param mac: MAC address.
35 :returns: Name of the interface.
37 :raises RuntimeError: If no interface is found.
41 "fgrep -ls '{0}' /sys/class/net/*/address".format(mac),
42 "awk -F '/' '{print $5}'"
47 ret_code, stdout, _ = ssh.exec_command(cmd)
52 raise RuntimeError("No interface found using the specified MAC "
56 def clear_interface_configuration(node, interface):
57 """Remove all configured IP addresses from the specified interface
58 and set it into down state.
60 :param node: Node in topology.
61 :param interface: Name of an interface on the node.
64 :raises RuntimeError: If the configuration could not be cleared.
68 "sudo ip addr flush dev {interface}".format(interface=interface),
69 "sudo ip link set dev {interface} down".format(interface=interface)
74 ret_code, _, _ = ssh.exec_command(cmd)
76 raise RuntimeError("Could not clear interface configuration.")
79 def set_management_interface_address(node, interface, address, prefix):
80 """Configure an IP address on the specified interface.
82 :param node: Node in topology.
83 :param interface: Name of an interface on the node.
84 :param address: IP address to configure.
85 :param prefix: IP network prefix.
90 :raises RuntimeError: If the configuration fails.
96 # Enable IPv6 for only the specified interface
97 cmd = "sudo sysctl net.ipv6.conf.{0}.disable_ipv6=0".format(interface)
99 ret_code, _, _ = ssh.exec_command(cmd)
101 raise RuntimeError("Could not enable IPv6 on interface.")
103 # Configure IPv6 address on the interface
104 cmd = "sudo ip address add {address}/{prefix} dev {interface}".format(
109 ret_code, _, _ = ssh.exec_command(cmd)
111 raise RuntimeError("Could not configure IP address on interface.")
113 # Set the interface up
114 cmd = "sudo ip link set {interface} up".format(interface=interface)
116 ret_code, _, _ = ssh.exec_command(cmd)
118 raise RuntimeError("Could not change the interface to 'up' state.")
121 def configure_control_interface_tunnel(node, src_port, dst_ip, dst_port):
122 """Configure a tunnel on the specified node, tunelling any IPv4 traffic
123 from one port to the specified address.
125 :param node: Node in topology.
126 :param src_port: Port to tunnel traffic from.
127 :param dst_ip: IP address to tunnel traffic to.
128 :param dst_port: Port to tunnel traffic to.
133 :raises RuntimeError: If tunnel creation is not successful.
136 cmd = "nohup socat TCP4-LISTEN:{src_port},fork,su=nobody " \
137 "TCP6:[{dst_ip}]:{dst_port} $@ > " \
138 "/tmp/socat.log 2>&1 &".format(
145 ret_code, _, _ = ssh.exec_command_sudo(cmd)
147 raise RuntimeError("Could not configure tunnel.")