1 # Copyright (c) 2018 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 """IPv4 setup library"""
16 from socket import inet_ntoa
17 from struct import pack
18 from abc import ABCMeta, abstractmethod
20 from robot.api.deco import keyword
22 from resources.libraries.python.ssh import exec_cmd_no_error
23 from resources.libraries.python.Routing import Routing
24 from resources.libraries.python.topology import NodeType, Topology
25 from resources.libraries.python.VatExecutor import VatExecutor
28 class IPv4Node(object):
29 """Abstract class of a node in a topology."""
30 __metaclass__ = ABCMeta
32 def __init__(self, node_info):
33 self.node_info = node_info
36 def _get_netmask(prefix_length):
37 """Convert IPv4 network prefix length into IPV4 network mask.
39 :param prefix_length: Length of network prefix.
40 :type prefix_length: int
41 :returns: Network mask.
45 bits = 0xffffffff ^ (1 << 32 - prefix_length) - 1
46 return inet_ntoa(pack('>I', bits))
49 def set_ip(self, interface, address, prefix_length):
50 """Configure IPv4 address on interface.
52 :param interface: Interface name.
53 :param address: IPv4 address.
54 :param prefix_length: IPv4 prefix length.
57 :type prefix_length: int
63 def set_route(self, network, prefix_length, gateway, interface, count=1):
64 """Configure IPv4 route.
66 :param network: Network IPv4 address.
67 :param prefix_length: IPv4 prefix length.
68 :param gateway: IPv4 address of the gateway.
69 :param interface: Interface name.
70 :param count: Number of consecutive routes to add.
72 :type prefix_length: int
81 def unset_route(self, network, prefix_length, gateway, interface):
82 """Remove specified IPv4 route.
84 :param network: Network IPv4 address.
85 :param prefix_length: IPv4 prefix length.
86 :param gateway: IPv4 address of the gateway.
87 :param interface: Interface name.
89 :type prefix_length: int
97 def flush_ip_addresses(self, interface):
98 """Flush all IPv4 addresses from specified interface.
100 :param interface: Interface name.
107 def ping(self, destination_address, source_interface):
108 """Send an ICMP request to destination node.
110 :param destination_address: Address to send the ICMP request.
111 :param source_interface: Source interface name.
112 :type destination_address: str
113 :type source_interface: str
120 """Traffic generator node"""
121 def __init__(self, node_info):
122 super(Tg, self).__init__(node_info)
124 def _execute(self, cmd):
125 """Executes the specified command on TG using SSH.
127 :param cmd: Command to be executed.
129 :returns: Content of stdout and stderr returned by command.
132 return exec_cmd_no_error(self.node_info, cmd)
134 def _sudo_execute(self, cmd):
135 """Executes the specified command with sudo on TG using SSH.
137 :param cmd: Command to be executed.
139 :returns: Content of stdout and stderr returned by command.
142 return exec_cmd_no_error(self.node_info, cmd, sudo=True)
144 def set_ip(self, interface, address, prefix_length):
145 cmd = 'ip -4 addr flush dev {}'.format(interface)
146 self._sudo_execute(cmd)
147 cmd = 'ip addr add {}/{} dev {}'.format(address, prefix_length,
149 self._sudo_execute(cmd)
151 def set_route(self, network, prefix_length, gateway, interface, count=1):
152 netmask = self._get_netmask(prefix_length)
153 cmd = 'route add -net {} netmask {} gw {}'.\
154 format(network, netmask, gateway)
155 self._sudo_execute(cmd)
157 def unset_route(self, network, prefix_length, gateway, interface):
158 self._sudo_execute('ip route delete {}/{}'.
159 format(network, prefix_length))
161 def arp_ping(self, destination_address, source_interface):
162 """Execute 'arping' command to send one ARP packet from the TG node.
164 :param destination_address: Destination IP address for the ARP packet.
165 :param source_interface: Name of an interface to send ARP packet from.
166 :type destination_address: str
167 :type source_interface: str
169 self._sudo_execute('arping -c 1 -I {} {}'.format(source_interface,
170 destination_address))
172 def ping(self, destination_address, source_interface):
173 self._execute('ping -c 1 -w 5 -I {} {}'.format(source_interface,
174 destination_address))
176 def flush_ip_addresses(self, interface):
177 self._sudo_execute('ip addr flush dev {}'.format(interface))
181 """Device under test"""
182 def __init__(self, node_info):
183 super(Dut, self).__init__(node_info)
185 def get_sw_if_index(self, interface):
186 """Get sw_if_index of specified interface from current node.
188 :param interface: Interface name.
190 :returns: sw_if_index of the interface or None.
193 return Topology().get_interface_sw_index(self.node_info, interface)
195 def exec_vat(self, script, **args):
196 """Wrapper for VAT executor.
198 :param script: Script to execute.
199 :param args: Parameters to the script.
204 # TODO: check return value
205 VatExecutor.cmd_from_template(self.node_info, script, **args)
207 def set_arp(self, iface_key, ip_address, mac_address):
208 """Set entry in ARP cache.
210 :param iface_key: Interface key.
211 :param ip_address: IP address.
212 :param mac_address: MAC address.
214 :type ip_address: str
215 :type mac_address: str
217 self.exec_vat('add_ip_neighbor.vat',
218 sw_if_index=self.get_sw_if_index(iface_key),
219 ip_address=ip_address, mac_address=mac_address)
221 def set_ip(self, interface, address, prefix_length):
222 self.exec_vat('add_ip_address.vat',
223 sw_if_index=self.get_sw_if_index(interface),
224 address=address, prefix_length=prefix_length)
226 def set_route(self, network, prefix_length, gateway, interface, count=1):
227 Routing.vpp_route_add(self.node_info,
228 network=network, prefix_len=prefix_length,
229 gateway=gateway, interface=interface, count=count)
231 def unset_route(self, network, prefix_length, gateway, interface):
232 self.exec_vat('del_route.vat', network=network,
233 prefix_length=prefix_length, gateway=gateway,
234 sw_if_index=self.get_sw_if_index(interface))
236 def arp_ping(self, destination_address, source_interface):
240 def flush_ip_addresses(self, interface):
241 self.exec_vat('flush_ip_addresses.vat',
242 sw_if_index=self.get_sw_if_index(interface))
244 def ping(self, destination_address, source_interface):
248 def get_node(node_info):
249 """Creates a class instance derived from Node based on type.
251 :param node_info: Dictionary containing information on nodes in topology.
252 :type node_info: dict
253 :returns: Class instance that is derived from Node.
255 if node_info['type'] == NodeType.TG:
257 elif node_info['type'] == NodeType.DUT:
258 return Dut(node_info)
260 raise NotImplementedError('Node type "{}" unsupported!'.
261 format(node_info['type']))
264 class IPv4Setup(object):
265 """IPv4 setup in topology."""
268 def vpp_nodes_set_ipv4_addresses(nodes, nodes_addr):
269 """Set IPv4 addresses on all VPP nodes in topology.
271 :param nodes: Nodes of the test topology.
272 :param nodes_addr: Available nodes IPv4 addresses.
274 :type nodes_addr: dict
275 :returns: Affected interfaces as list of (node, interface) tuples.
279 for net in nodes_addr.values():
280 for port in net['ports'].values():
281 host = port.get('node')
285 node = topo.get_node_by_hostname(nodes, host)
288 if node['type'] != NodeType.DUT:
290 iface_key = topo.get_interface_by_name(node, port['if'])
291 get_node(node).set_ip(iface_key, port['addr'], net['prefix'])
292 interfaces.append((node, port['if']))
297 @keyword('Get IPv4 address of node "${node}" interface "${port}" '
298 'from "${nodes_addr}"')
299 def get_ip_addr(node, iface_key, nodes_addr):
300 """Return IPv4 address of the node port.
302 :param node: Node in the topology.
303 :param iface_key: Interface key of the node.
304 :param nodes_addr: Nodes IPv4 addresses.
307 :type nodes_addr: dict
308 :returns: IPv4 address.
311 interface = Topology.get_interface_name(node, iface_key)
312 for net in nodes_addr.values():
313 for port in net['ports'].values():
314 host = port.get('node')
316 if host == node['host'] and dev == interface:
317 ip_addr = port.get('addr')
318 if ip_addr is not None:
322 'Node {n} port {p} IPv4 address is not set'.format(
323 n=node['host'], p=interface))
325 raise Exception('Node {n} port {p} IPv4 address not found.'.format(
326 n=node['host'], p=interface))
329 def setup_arp_on_all_duts(nodes_info, nodes_addr):
330 """For all DUT nodes extract MAC and IP addresses of adjacent
331 interfaces from topology and use them to setup ARP entries.
333 :param nodes_info: Dictionary containing information on all nodes
335 :param nodes_addr: Nodes IPv4 addresses.
336 :type nodes_info: dict
337 :type nodes_addr: dict
339 for node in nodes_info.values():
340 if node['type'] == NodeType.TG:
342 for iface_key in node['interfaces'].keys():
343 adj_node, adj_int = Topology.\
344 get_adjacent_node_and_interface(nodes_info, node, iface_key)
345 ip_address = IPv4Setup.get_ip_addr(adj_node, adj_int,
347 mac_address = Topology.get_interface_mac(adj_node, adj_int)
348 get_node(node).set_arp(iface_key, ip_address, mac_address)
351 def add_arp_on_dut(node, iface_key, ip_address, mac_address):
352 """Set ARP cache entree on DUT node.
354 :param node: VPP Node in the topology.
355 :param iface_key: Interface key.
356 :param ip_address: IP address of the interface.
357 :param mac_address: MAC address of the interface.
360 :type ip_address: str
361 :type mac_address: str
363 get_node(node).set_arp(iface_key, ip_address, mac_address)