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 """IPv6 utilities library."""
18 from resources.libraries.python.ssh import SSH
19 from resources.libraries.python.VatExecutor import VatTerminal
20 from resources.libraries.python.topology import Topology
23 class IPv6Util(object):
27 def ipv6_ping(src_node, dst_addr, count=3, data_size=56, timeout=1):
30 :param src_node: Node where ping run.
31 :param dst_addr: Destination IPv6 address.
32 :param count: Number of echo requests. (Optional)
33 :param data_size: Number of the data bytes. (Optional)
34 :param timeout: Time to wait for a response, in seconds. (Optional)
40 :return: Number of lost packets.
46 cmd = "ping6 -c {c} -s {s} -W {W} {dst}".format(c=count, s=data_size,
49 (_, stdout, _) = ssh.exec_command(cmd)
51 regex = re.compile(r'(\d+) packets transmitted, (\d+) received')
52 match = regex.search(stdout)
53 sent, received = match.groups()
54 packet_lost = int(sent) - int(received)
59 def ipv6_ping_port(nodes_ip, src_node, dst_node, port, cnt=3,
61 """Send IPv6 ping to the node port.
63 :param nodes_ip: Nodes IPv6 addresses.
64 :param src_node: Node where ping run.
65 :param dst_node: Destination node.
66 :param port: Port on the destination node.
67 :param cnt: Number of echo requests. (Optional)
68 :param size: Number of the data bytes. (Optional)
69 :param timeout: Time to wait for a response, in seconds. (Optional)
77 :return: Number of lost packets.
80 dst_ip = IPv6Util.get_node_port_ipv6_address(dst_node, port, nodes_ip)
81 return IPv6Util.ipv6_ping(src_node, dst_ip, cnt, size, timeout)
84 def get_node_port_ipv6_address(node, iface_key, nodes_addr):
85 """Return IPv6 address of the node port.
87 :param node: Node in the topology.
88 :param iface_key: Interface key of the node.
89 :param nodes_addr: Nodes IPv6 addresses.
92 :type nodes_addr: dict
93 :return: IPv6 address string.
96 interface = Topology.get_interface_name(node, iface_key)
97 for net in nodes_addr.values():
98 for port in net['ports'].values():
99 host = port.get('node')
101 if host == node['host'] and dev == interface:
102 ip_addr = port.get('addr')
103 if ip_addr is not None:
107 'Node {n} port {p} IPv6 address is not set'.format(
108 n=node['host'], p=interface))
110 raise Exception('Node {n} port {p} IPv6 address not found.'.format(
111 n=node['host'], p=interface))
114 def add_ip_neighbor(node, interface, ip_address, mac_address):
117 :param node: VPP node to add ip neighbor.
118 :param interface: Interface name or sw_if_index.
119 :param ip_address: IP address.
120 :param mac_address: MAC address.
122 :type interface: str or int
123 :type ip_address: str
124 :type mac_address: str
126 if isinstance(interface, basestring):
127 sw_if_index = Topology.get_interface_sw_index(node, interface)
129 sw_if_index = interface
131 with VatTerminal(node) as vat:
132 vat.vat_terminal_exec_cmd_from_template("add_ip_neighbor.vat",
133 sw_if_index=sw_if_index,
134 ip_address=ip_address,
135 mac_address=mac_address)