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 """Segment Routing over IPv6 dataplane utilities library."""
16 from resources.libraries.python.VatExecutor import VatTerminal
17 from resources.libraries.python.topology import Topology
20 # SRv6 LocalSID supported functions.
22 SRV6BEHAVIOUR_END = 'end'
23 # Endpoint function with Layer-3 cross-connect
24 SRV6BEHAVIOUR_END_X = 'end.x'
25 # Endpoint with decapsulation and Layer-2 cross-connect
26 SRV6BEHAVIOUR_END_DX2 = 'end.dx2'
27 # Endpoint with decapsulation and IPv4 cross-connect
28 SRV6BEHAVIOUR_END_DX4 = 'end.dx4'
29 # Endpoint with decapsulation and IPv4 table lookup
30 SRV6BEHAVIOUR_END_DT4 = 'end.dt4'
31 # Endpoint with decapsulation and IPv6 cross-connect
32 SRV6BEHAVIOUR_END_DX6 = 'end.dx6'
33 # Endpoint with decapsulation and IPv6 table lookup
34 SRV6BEHAVIOUR_END_DT6 = 'end.dt6'
44 def configure_sr_localsid(node, local_sid, behavior, interface=None,
45 next_hop=None, fib_table=None):
46 """Create SRv6 LocalSID and binds it to a particular behaviour on
49 :param node: Given node to create localSID on.
50 :param local_sid: LocalSID IPv6 address.
51 :param behavior: SRv6 LocalSID function.
52 :param interface: Interface name (Optional, required for
54 :param next_hop: Next hop IPv4/IPv6 address (Optional, required for L3
56 :param fib_table: FIB table for IPv4/IPv6 lookup (Optional, required for
64 :raises ValueError: If unsupported SRv6 LocalSID function used or
65 required parameter is missing.
67 if behavior == SRV6BEHAVIOUR_END:
69 elif behavior in [SRV6BEHAVIOUR_END_X, SRV6BEHAVIOUR_END_DX4,
70 SRV6BEHAVIOUR_END_DX6]:
71 if interface is None or next_hop is None:
72 raise ValueError('Required data missing.\ninterface:{0}\n'
73 'next_hop:{1}'.format(interface, next_hop))
74 interface_name = Topology.get_interface_name(node, interface)
75 params = '{0} {1}'.format(interface_name, next_hop)
76 elif behavior == SRV6BEHAVIOUR_END_DX2:
78 raise ValueError('Required data missing.\ninterface:{0}\n'.
80 params = '{0}'.format(interface)
81 elif behavior in [SRV6BEHAVIOUR_END_DT4, SRV6BEHAVIOUR_END_DT6]:
83 raise ValueError('Required data missing.\nfib_table:{0}\n'.
85 params = '{0}'.format(fib_table)
87 raise ValueError('Unsupported SRv6 LocalSID function: {0}'.
90 with VatTerminal(node, json_param=False) as vat:
91 vat.vat_terminal_exec_cmd_from_template(
92 'srv6/sr_localsid_add.vat', local_sid=local_sid,
93 behavior=behavior, params=params)
95 if "exec error: Misc" in vat.vat_stdout:
96 raise RuntimeError('Create SRv6 LocalSID {0} failed on node {1}'.
97 format(local_sid, node['host']))
100 def delete_sr_localsid(node, local_sid):
101 """Delete SRv6 LocalSID on the given node.
103 :param node: Given node to delete localSID on.
104 :param local_sid: LocalSID IPv6 address.
108 with VatTerminal(node, json_param=False) as vat:
109 vat.vat_terminal_exec_cmd_from_template(
110 'srv6/sr_localsid_del.vat', local_sid=local_sid)
112 if "exec error: Misc" in vat.vat_stdout:
113 raise RuntimeError('Delete SRv6 LocalSID {0} failed on node {1}'.
114 format(local_sid, node['host']))
117 def show_sr_localsids(node):
118 """Show SRv6 LocalSIDs on the given node.
120 :param node: Given node to show localSIDs on.
123 with VatTerminal(node, json_param=False) as vat:
124 vat.vat_terminal_exec_cmd_from_template(
125 'srv6/sr_localsids_show.vat')
128 def configure_sr_policy(node, bsid, sid_list, mode='encap'):
129 """Create SRv6 policy on the given node.
131 :param node: Given node to create SRv6 policy on.
132 :param bsid: BindingSID - local SID IPv6 address.
133 :param sid_list: SID list.
134 :param mode: Encapsulation / insertion mode.
140 sid_conf = 'next ' + ' next '.join(sid_list)
142 with VatTerminal(node, json_param=False) as vat:
143 vat.vat_terminal_exec_cmd_from_template(
144 'srv6/sr_policy_add.vat', bsid=bsid,
145 sid_conf=sid_conf, mode=mode)
147 if "exec error: Misc" in vat.vat_stdout:
148 raise RuntimeError('Create SRv6 policy for BindingSID {0} failed on'
149 ' node {1}'.format(bsid, node['host']))
152 def delete_sr_policy(node, bsid):
153 """Delete SRv6 policy on the given node.
155 :param node: Given node to delete SRv6 policy on.
156 :param bsid: BindingSID IPv6 address.
160 with VatTerminal(node, json_param=False) as vat:
161 vat.vat_terminal_exec_cmd_from_template(
162 'srv6/sr_policy_del.vat', bsid=bsid)
164 if "exec error: Misc" in vat.vat_stdout:
165 raise RuntimeError('Delete SRv6 policy for BindingSID {0} failed on'
166 ' node {1}'.format(bsid, node['host']))
169 def show_sr_policies(node):
170 """Show SRv6 policies on the given node.
172 :param node: Given node to show SRv6 policies on.
175 with VatTerminal(node, json_param=False) as vat:
176 vat.vat_terminal_exec_cmd_from_template(
177 'srv6/sr_policies_show.vat')
180 def configure_sr_steer(node, mode, bsid, interface=None, ip_addr=None,
182 """Create SRv6 steering policy on the given node.
184 :param node: Given node to create steering policy on.
185 :param mode: Mode of operation - L2 or L3.
186 :param bsid: BindingSID - local SID IPv6 address.
187 :param interface: Interface name (Optional, required in case of
189 :param ip_addr: IPv4/IPv6 address (Optional, required in case of L3
191 :param prefix: IP address prefix (Optional, required in case of L3
199 :raises ValueError: If unsupported mode used or required parameter
202 if mode.lower() == 'l2':
203 if interface is None:
204 raise ValueError('Required data missing.\ninterface:{0}\n'.
206 interface_name = Topology.get_interface_name(node, interface)
207 params = 'l2 {0}'.format(interface_name)
208 elif mode.lower() == 'l3':
209 if ip_addr is None or prefix is None:
210 raise ValueError('Required data missing.\nIP address:{0}\n'
211 'mask:{1}'.format(ip_addr, prefix))
212 params = 'l3 {0}/{1}'.format(ip_addr, prefix)
214 raise ValueError('Unsupported mode: {0}'.format(mode))
216 with VatTerminal(node, json_param=False) as vat:
217 vat.vat_terminal_exec_cmd_from_template(
218 'srv6/sr_steer_add_del.vat', params=params, bsid=bsid)
220 if "exec error: Misc" in vat.vat_stdout:
221 raise RuntimeError('Create SRv6 steering policy for BindingSID {0}'
222 ' failed on node {1}'.format(bsid, node['host']))
225 def delete_sr_steer(node, mode, bsid, interface=None, ip_addr=None,
227 """Delete SRv6 steering policy on the given node.
229 :param node: Given node to delete steering policy on.
230 :param mode: Mode of operation - L2 or L3.
231 :param bsid: BindingSID - local SID IPv6 address.
232 :param interface: Interface name (Optional, required in case of
234 :param ip_addr: IPv4/IPv6 address (Optional, required in case of L3
236 :param mask: IP address mask (Optional, required in case of L3 mode).
243 :raises ValueError: If unsupported mode used or required parameter
248 if interface is None:
249 raise ValueError('Required data missing.\ninterface:{0}\n'.
251 interface_name = Topology.get_interface_name(node, interface)
252 params += 'l2 {0}'.format(interface_name)
254 if ip_addr is None or mask is None:
255 raise ValueError('Required data missing.\nIP address:{0}\n'
256 'mask:{1}'.format(ip_addr, mask))
257 params += '{0}/{1}'.format(ip_addr, mask)
259 raise ValueError('Unsupported mode: {0}'.format(mode))
261 with VatTerminal(node, json_param=False) as vat:
262 vat.vat_terminal_exec_cmd_from_template(
263 'srv6/sr_steer_add_del.vat', params=params, bsid=bsid)
265 if "exec error: Misc" in vat.vat_stdout:
266 raise RuntimeError('Delete SRv6 policy for bsid {0} failed on node'
267 ' {1}'.format(bsid, node['host']))
270 def show_sr_steering_policies(node):
271 """Show SRv6 steering policies on the given node.
273 :param node: Given node to show SRv6 steering policies on.
276 with VatTerminal(node, json_param=False) as vat:
277 vat.vat_terminal_exec_cmd_from_template(
278 'srv6/sr_steer_policies_show.vat')
281 def set_sr_encaps_source_address(node, ip6_addr):
282 """Set SRv6 encapsulation source address on the given node.
284 :param node: Given node to set SRv6 encapsulation source address on.
285 :param ip6_addr: Local SID IPv6 address.
289 with VatTerminal(node, json_param=False) as vat:
290 vat.vat_terminal_exec_cmd_from_template(
291 'srv6/sr_set_encaps_source.vat', ip6_addr=ip6_addr)