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 util library"""
16 from time import time, sleep
17 from robot.api import logger
18 from resources.libraries.python.ssh import exec_cmd_no_error
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
23 class InterfaceUtil(object):
24 """General utilities for managing interfaces"""
27 def set_interface_state(node, interface, state):
28 """Set interface state on a node.
30 Function can be used for DUTs as well as for TGs.
32 :param node: node where the interface is
33 :param interface: interface name
34 :param state: one of 'up' or 'down'
40 if node['type'] == NodeType.DUT:
46 raise ValueError('Unexpected interface state: {}'.format(state))
48 sw_if_index = Topology.get_interface_sw_index(node, interface)
49 VatExecutor.cmd_from_template(node, 'set_if_state.vat',
50 sw_if_index=sw_if_index, state=state)
52 elif node['type'] == NodeType.TG:
53 cmd = 'ip link set {} {}'.format(interface, state)
54 exec_cmd_no_error(node, cmd, sudo=True)
56 raise Exception('Node {} has unknown NodeType: "{}"'.
57 format(node['host'], node['type']))
60 def set_interface_ethernet_mtu(node, interface, mtu):
61 """Set Ethernet MTU for specified interface.
63 Function can be used only for TGs.
65 :param node: node where the interface is
66 :param interface: interface name
67 :param mtu: MTU to set
73 if node['type'] == NodeType.DUT:
74 ValueError('Node {}: Setting Ethernet MTU for interface '
75 'on DUT nodes not supported', node['host'])
76 elif node['type'] == NodeType.TG:
77 cmd = 'ip link set {} mtu {}'.format(interface, mtu)
78 exec_cmd_no_error(node, cmd, sudo=True)
80 raise ValueError('Node {} has unknown NodeType: "{}"'.
81 format(node['host'], node['type']))
84 def set_default_ethernet_mtu_on_all_interfaces_on_node(node):
85 """Set default Ethernet MTU on all interfaces on node.
87 Function can be used only for TGs.
89 :param node: node where to set default MTU
93 for ifc in node['interfaces'].values():
94 InterfaceUtil.set_interface_ethernet_mtu(node, ifc['name'], 1500)
97 def vpp_node_interfaces_ready_wait(node, timeout=10):
98 """Wait until all interfaces with admin-up are in link-up state.
100 :param node: Node to wait on.
101 :param timeout: Waiting timeout in seconds (optional, default 10s)
104 :raises: RuntimeError if the timeout period value has elapsed.
107 with VatTerminal(node) as vat:
111 out = vat.vat_terminal_exec_cmd('sw_interface_dump')
112 if time() - start > timeout:
113 for interface in out:
114 if interface.get('admin_up_down') == 1:
115 if interface.get('link_up_down') != 1:
116 logger.debug('{0} link-down'.format(
117 interface.get('interface_name')))
118 raise RuntimeError('timeout, not up {0}'.format(not_ready))
120 for interface in out:
121 if interface.get('admin_up_down') == 1:
122 if interface.get('link_up_down') != 1:
123 not_ready.append(interface.get('interface_name'))
127 logger.debug('Interfaces still in link-down state: {0}, '
128 'waiting...'.format(not_ready))
132 def vpp_nodes_interfaces_ready_wait(nodes, timeout=10):
133 """Wait until all interfaces with admin-up are in link-up state for
136 :param nodes: List of nodes to wait on.
137 :param timeout: Seconds to wait per node for all interfaces to come up.
140 :raises: RuntimeError if the timeout period value has elapsed.
143 InterfaceUtil.vpp_node_interfaces_ready_wait(node, timeout)
146 def all_vpp_interfaces_ready_wait(nodes, timeout=10):
147 """Wait until all interfaces with admin-up are in link-up state for all
148 nodes in the topology.
150 :param nodes: Nodes in the topology.
151 :param timeout: Seconds to wait per node for all interfaces to come up.
154 :raises: RuntimeError if the timeout period value has elapsed.
156 for node in nodes.values():
157 if node['type'] == NodeType.DUT:
158 InterfaceUtil.vpp_node_interfaces_ready_wait(node, timeout)