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 """Routing utilities library."""
16 from resources.libraries.python.VatExecutor import VatTerminal
17 from resources.libraries.python.topology import Topology
18 from resources.libraries.python.ssh import exec_cmd_no_error
21 class Routing(object):
22 """Routing utilities."""
24 # pylint: disable=too-many-arguments
26 def vpp_route_add(node, network, prefix_len, gateway=None,
27 interface=None, use_sw_index=True, resolve_attempts=10,
28 count=1, vrf=None, lookup_vrf=None, multipath=False,
30 """Add route to the VPP node.
32 :param node: Node to add route on.
33 :param network: Route destination network address.
34 :param prefix_len: Route destination network prefix length.
35 :param gateway: Route gateway address.
36 :param interface: Route interface.
37 :param vrf: VRF table ID (Optional).
38 :param use_sw_index: Use sw_if_index in VAT command.
39 :param resolve_attempts: Resolve attempts IP route add parameter.
40 :param count: number of IP addresses to add starting from network IP
41 with same prefix (increment is 1). If None, then is not used.
42 :param lookup_vrf: VRF table ID for lookup.
43 :param multipath: Enable multipath routing.
44 :param weight: Weight value for unequal cost multipath routing.
50 :type use_sw_index: bool
51 :type resolve_attempts: int
59 int_cmd = ('sw_if_index {}'.
60 format(Topology.get_interface_sw_index(node, interface)))
64 rap = 'resolve-attempts {}'.format(resolve_attempts) \
65 if resolve_attempts else ''
67 via = 'via {}'.format(gateway) if gateway else ''
69 cnt = 'count {}'.format(count) \
72 vrf = 'vrf {}'.format(vrf) if vrf else ''
74 lookup_vrf = 'lookup-in-vrf {}'.format(lookup_vrf) if lookup_vrf else ''
76 multipath = 'multipath' if multipath else ''
78 weight = 'weight {}'.format(weight) if weight else ''
80 with VatTerminal(node, json_param=False) as vat:
81 vat.vat_terminal_exec_cmd_from_template('add_route.vat',
83 prefix_length=prefix_len,
89 lookup_vrf=lookup_vrf,
94 def add_fib_table(node, network, prefix_len, fib_id, place):
95 """Create new FIB table according to ID.
97 :param node: Node to add FIB on.
98 :param network: IP address to add to the FIB table.
99 :param prefix_len: IP address prefix length.
100 :param fib_id: FIB table ID.
101 :param place: Possible variants are local, drop.
104 :type prefix_len: int
108 with VatTerminal(node) as vat:
109 vat.vat_terminal_exec_cmd_from_template('add_fib_table.vat',
111 prefix_length=prefix_len,
116 def add_route(node, ip_addr, prefix, gateway, namespace=None):
117 """Add route in namespace.
119 :param node: Node where to execute command.
120 :param ip_addr: Route destination IP address.
121 :param prefix: IP prefix.
122 :param namespace: Execute command in namespace. Optional.
123 :param gateway: Gateway address.
130 if namespace is not None:
131 cmd = 'ip netns exec {} ip route add {}/{} via {}'.format(
132 namespace, ip_addr, prefix, gateway)
134 cmd = 'ip route add {}/{} via {}'.format(ip_addr, prefix, gateway)
135 exec_cmd_no_error(node, cmd, sudo=True)