blob: 23a108d9ca22febf371b60206a518726f22406a0 [file] [log] [blame]
Klement Sekera277b89c2016-10-28 13:20:27 +02001import os
Klement Sekeraf62ae122016-10-11 11:47:09 +02002from logging import error
3from hook import Hook
4
Klement Sekera277b89c2016-10-28 13:20:27 +02005do_import = True
6try:
7 no_vpp_papi = os.getenv("NO_VPP_PAPI")
8 if no_vpp_papi == "1":
9 do_import = False
10except:
11 pass
12
13if do_import:
14 import vpp_papi
15
16
Klement Sekeraf62ae122016-10-11 11:47:09 +020017# from vnet/vnet/mpls/mpls_types.h
18MPLS_IETF_MAX_LABEL = 0xfffff
19MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
20
Neale Ranns177bbdc2016-11-15 09:46:51 +000021class L2_VTR_OP:
22 L2_POP_1 = 3
23
Klement Sekeraf62ae122016-10-11 11:47:09 +020024
25class VppPapiProvider(object):
26 """VPP-api provider using vpp-papi
27
28 @property hook: hook object providing before and after api/cli hooks
29
30
31 """
32
33 def __init__(self, name, shm_prefix):
Klement Sekera277b89c2016-10-28 13:20:27 +020034 self.hook = Hook("vpp-papi-provider")
Klement Sekeraf62ae122016-10-11 11:47:09 +020035 self.name = name
36 self.shm_prefix = shm_prefix
37
38 def register_hook(self, hook):
39 """Replace hook registration with new hook
40
41 :param hook:
42
43 """
44 self.hook = hook
45
46 def connect(self):
47 """Connect the API to VPP"""
48 vpp_papi.connect(self.name, self.shm_prefix)
49
50 def disconnect(self):
51 """Disconnect the API from VPP"""
52 vpp_papi.disconnect()
53
54 def api(self, api_fn, api_args, expected_retval=0):
55 """Call API function and check it's return value
56 Call the appropriate hooks before and after the API call
57
58 :param api_fn: API function to call
59 :param api_args: tuple of API function arguments
60 :param expected_retval: Expected return value (Default value = 0)
61 :returns: reply from the API
62
63 """
64 self.hook.before_api(api_fn.__name__, api_args)
65 reply = api_fn(*api_args)
66 if hasattr(reply, 'retval') and reply.retval != expected_retval:
67 msg = "API call failed, expected retval == %d, got %s" % (
68 expected_retval, repr(reply))
69 error(msg)
70 raise Exception(msg)
71 self.hook.after_api(api_fn.__name__, api_args)
72 return reply
73
74 def cli(self, cli):
Jan49c0fca2016-10-26 15:44:27 +020075 """
76 Execute a CLI, calling the before/after hooks appropriately.
Klement Sekeraf62ae122016-10-11 11:47:09 +020077
78 :param cli: CLI to execute
79 :returns: CLI output
80
81 """
82 self.hook.before_cli(cli)
83 cli += '\n'
84 r = vpp_papi.cli_inband(len(cli), cli)
85 self.hook.after_cli(cli)
Jan49c0fca2016-10-26 15:44:27 +020086 if hasattr(r, 'reply'):
Klement Sekeraf62ae122016-10-11 11:47:09 +020087 return r.reply[0].decode().rstrip('\x00')
88
Jan49c0fca2016-10-26 15:44:27 +020089 def ppcli(self, cli):
90 """
91 Helping method to print CLI command in case of info logging level.
92
93 :param cli: CLI to execute
94 :returns: CLI output
95 """
96 return cli + "\n" + self.cli(cli)
97
Klement Sekeraf62ae122016-10-11 11:47:09 +020098 def show_version(self):
99 """ """
100 return vpp_papi.show_version()
101
102 def pg_create_interface(self, pg_index):
103 """
104
105 :param pg_index:
106
107 """
108 return self.api(vpp_papi.pg_create_interface, (pg_index, ))
109
110 def sw_interface_dump(self, filter=None):
111 """
112
113 :param filter: (Default value = None)
114
115 """
116 if filter is not None:
117 args = (1, filter)
118 else:
119 args = (0, b'')
120 return self.api(vpp_papi.sw_interface_dump, args)
121
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000122 def sw_interface_set_table(self, sw_if_index, is_ipv6, table_id):
123 """
124 Set the IPvX Table-id for the Interface
125
126 :param sw_if_index:
127 :param is_ipv6:
128 :param table_id:
129
130 """
131 return self.api(vpp_papi.sw_interface_set_table,
132 (sw_if_index, is_ipv6, table_id))
133
Klement Sekeraf62ae122016-10-11 11:47:09 +0200134 def sw_interface_add_del_address(self, sw_if_index, addr, addr_len,
135 is_ipv6=0, is_add=1, del_all=0):
136 """
137
138 :param addr: param is_ipv6: (Default value = 0)
139 :param sw_if_index:
140 :param addr_len:
141 :param is_ipv6: (Default value = 0)
142 :param is_add: (Default value = 1)
143 :param del_all: (Default value = 0)
144
145 """
146 return self.api(vpp_papi.sw_interface_add_del_address,
147 (sw_if_index, is_add, is_ipv6, del_all, addr_len, addr))
148
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000149 def sw_interface_enable_disable_mpls(self, sw_if_index,
150 is_enable=1):
151 """
152 Enable/Disable MPLS on the interface
153 :param sw_if_index:
154 :param is_enable: (Default value = 1)
155
156 """
157 return self.api(vpp_papi.sw_interface_set_mpls_enable,
158 (sw_if_index, is_enable))
159
Klement Sekeraf62ae122016-10-11 11:47:09 +0200160 def sw_interface_ra_suppress(self, sw_if_index):
161 suppress = 1
162 managed = 0
163 other = 0
164 ll_option = 0
165 send_unicast = 0
166 cease = 0
167 is_no = 0
168 default_router = 0
169 max_interval = 0
170 min_interval = 0
171 lifetime = 0
172 initial_count = 0
173 initial_interval = 0
174 async = False
175 return self.api(vpp_papi.sw_interface_ip6nd_ra_config,
176 (sw_if_index, suppress, managed, other,
177 ll_option, send_unicast, cease, is_no,
178 default_router, max_interval, min_interval,
179 lifetime, initial_count, initial_interval, async))
180
Klement Sekeraf62ae122016-10-11 11:47:09 +0200181 def vxlan_add_del_tunnel(
182 self,
183 src_addr,
184 dst_addr,
185 is_add=1,
186 is_ipv6=0,
187 encap_vrf_id=0,
188 decap_next_index=0xFFFFFFFF,
189 vni=0):
190 """
191
192 :param dst_addr:
193 :param src_addr:
194 :param is_add: (Default value = 1)
195 :param is_ipv6: (Default value = 0)
196 :param encap_vrf_id: (Default value = 0)
197 :param decap_next_index: (Default value = 0xFFFFFFFF)
198 :param vni: (Default value = 0)
199
200 """
201 return self.api(vpp_papi.vxlan_add_del_tunnel,
202 (is_add, is_ipv6, src_addr, dst_addr, encap_vrf_id,
203 decap_next_index, vni))
204
205 def sw_interface_set_l2_bridge(self, sw_if_index, bd_id,
206 shg=0, bvi=0, enable=1):
207 """
208
209 :param bd_id:
210 :param sw_if_index:
211 :param shg: (Default value = 0)
212 :param bvi: (Default value = 0)
213 :param enable: (Default value = 1)
214
215 """
216 return self.api(vpp_papi.sw_interface_set_l2_bridge,
217 (sw_if_index, bd_id, shg, bvi, enable))
218
219 def sw_interface_set_l2_xconnect(self, rx_sw_if_index, tx_sw_if_index,
220 enable):
221 """Create or delete unidirectional cross-connect from Tx interface to
222 Rx interface.
223
224 :param rx_sw_if_index: Software interface index of Rx interface.
225 :param tx_sw_if_index: Software interface index of Tx interface.
226 :param enable: Create cross-connect if equal to 1, delete cross-connect
Klement Sekera277b89c2016-10-28 13:20:27 +0200227 if equal to 0.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200228 :type rx_sw_if_index: str or int
229 :type rx_sw_if_index: str or int
230 :type enable: int
231
232 """
233 return self.api(vpp_papi.sw_interface_set_l2_xconnect,
234 (rx_sw_if_index, tx_sw_if_index, enable))
235
Neale Ranns177bbdc2016-11-15 09:46:51 +0000236 def sw_interface_set_l2_tag_rewrite(self, sw_if_index, vtr_oper, push=0, tag1=0, tag2=0):
237 """L2 interface vlan tag rewrite configure request
238 :param client_index - opaque cookie to identify the sender
239 :param context - sender context, to match reply w/ request
240 :param sw_if_index - interface the operation is applied to
241 :param vtr_op - Choose from l2_vtr_op_t enum values
242 :param push_dot1q - first pushed flag dot1q id set, else dot1ad
243 :param tag1 - Needed for any push or translate vtr op
244 :param tag2 - Needed for any push 2 or translate x-2 vtr ops
245
246 """
247 return self.api(vpp_papi.l2_interface_vlan_tag_rewrite,
248 (sw_if_index, vtr_oper, push, tag1, tag2))
249
Klement Sekeraf62ae122016-10-11 11:47:09 +0200250 def sw_interface_set_flags(self, sw_if_index, admin_up_down,
251 link_up_down=0, deleted=0):
252 """
253
254 :param admin_up_down:
255 :param sw_if_index:
256 :param link_up_down: (Default value = 0)
257 :param deleted: (Default value = 0)
258
259 """
260 return self.api(vpp_papi.sw_interface_set_flags,
261 (sw_if_index, admin_up_down, link_up_down, deleted))
262
263 def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
264 no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
265 default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):
266 """Create subinterface
267 from vpe.api: set dot1ad = 0 for dot1q, set dot1ad = 1 for dot1ad
268
269 :param sub_id: param inner_vlan:
270 :param sw_if_index:
271 :param outer_vlan:
272 :param inner_vlan:
273 :param no_tags: (Default value = 0)
274 :param one_tag: (Default value = 0)
275 :param two_tags: (Default value = 0)
276 :param dot1ad: (Default value = 0)
277 :param exact_match: (Default value = 0)
278 :param default_sub: (Default value = 0)
279 :param outer_vlan_id_any: (Default value = 0)
280 :param inner_vlan_id_any: (Default value = 0)
281
282 """
283 return self.api(
284 vpp_papi.create_subif,
285 (sw_if_index,
286 sub_id,
287 no_tags,
288 one_tag,
289 two_tags,
290 dot1ad,
291 exact_match,
292 default_sub,
293 outer_vlan_id_any,
294 inner_vlan_id_any,
295 outer_vlan,
296 inner_vlan))
297
Neale Ranns177bbdc2016-11-15 09:46:51 +0000298 def delete_subif(self, sw_if_index):
299 """Delete subinterface
300
301 :param sw_if_index:
302 """
303 return self.api(vpp_papi.delete_subif, ([sw_if_index]))
304
Klement Sekeraf62ae122016-10-11 11:47:09 +0200305 def create_vlan_subif(self, sw_if_index, vlan):
306 """
307
308 :param vlan:
309 :param sw_if_index:
310
311 """
312 return self.api(vpp_papi.create_vlan_subif, (sw_if_index, vlan))
313
Matej Klotton0178d522016-11-04 11:11:44 +0100314 def create_loopback(self, mac=''):
315 """
316
317 :param mac: (Optional)
318 """
319 return self.api(vpp_papi.create_loopback, (mac,))
320
Klement Sekeraf62ae122016-10-11 11:47:09 +0200321 def ip_add_del_route(
322 self,
323 dst_address,
324 dst_address_length,
325 next_hop_address,
326 next_hop_sw_if_index=0xFFFFFFFF,
327 table_id=0,
328 resolve_attempts=0,
329 classify_table_index=0xFFFFFFFF,
330 next_hop_out_label=MPLS_LABEL_INVALID,
331 next_hop_table_id=0,
332 create_vrf_if_needed=0,
333 resolve_if_needed=0,
334 is_add=1,
335 is_drop=0,
Juraj Sloboda86a2c572016-10-27 10:44:25 +0200336 is_unreach=0,
337 is_prohibit=0,
Klement Sekeraf62ae122016-10-11 11:47:09 +0200338 is_ipv6=0,
339 is_local=0,
340 is_classify=0,
341 is_multipath=0,
342 is_resolve_host=0,
343 is_resolve_attached=0,
344 not_last=0,
345 next_hop_weight=1):
346 """
347
348 :param dst_address_length:
349 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
350 :param dst_address:
351 :param next_hop_address:
352 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
353 :param vrf_id: (Default value = 0)
354 :param lookup_in_vrf: (Default value = 0)
355 :param resolve_attempts: (Default value = 0)
356 :param classify_table_index: (Default value = 0xFFFFFFFF)
357 :param create_vrf_if_needed: (Default value = 0)
358 :param resolve_if_needed: (Default value = 0)
359 :param is_add: (Default value = 1)
360 :param is_drop: (Default value = 0)
361 :param is_ipv6: (Default value = 0)
362 :param is_local: (Default value = 0)
363 :param is_classify: (Default value = 0)
364 :param is_multipath: (Default value = 0)
365 :param is_resolve_host: (Default value = 0)
366 :param is_resolve_attached: (Default value = 0)
367 :param not_last: (Default value = 0)
368 :param next_hop_weight: (Default value = 1)
369
370 """
371 return self.api(
372 vpp_papi.ip_add_del_route,
373 (next_hop_sw_if_index,
374 table_id,
375 resolve_attempts,
376 classify_table_index,
377 next_hop_out_label,
378 next_hop_table_id,
379 create_vrf_if_needed,
380 resolve_if_needed,
381 is_add,
382 is_drop,
Juraj Sloboda86a2c572016-10-27 10:44:25 +0200383 is_unreach,
384 is_prohibit,
Klement Sekeraf62ae122016-10-11 11:47:09 +0200385 is_ipv6,
386 is_local,
387 is_classify,
388 is_multipath,
389 is_resolve_host,
390 is_resolve_attached,
391 not_last,
392 next_hop_weight,
393 dst_address_length,
394 dst_address,
395 next_hop_address))
Matej Klotton0178d522016-11-04 11:11:44 +0100396
397 def ip_neighbor_add_del(self,
398 sw_if_index,
399 mac_address,
400 dst_address,
401 vrf_id=0,
402 is_add=1,
403 is_ipv6=0,
404 is_static=0,
405 ):
406 """ Add neighbor MAC to IPv4 or IPv6 address.
407
408 :param sw_if_index:
409 :param mac_address:
410 :param dst_address:
411 :param vrf_id: (Default value = 0)
412 :param is_add: (Default value = 1)
413 :param is_ipv6: (Default value = 0)
414 :param is_static: (Default value = 0)
415 """
416
417 return self.api(
418 vpp_papi.ip_neighbor_add_del,
419 (vrf_id,
420 sw_if_index,
421 is_add,
422 is_ipv6,
423 is_static,
424 mac_address,
425 dst_address
426 )
427 )
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100428
429 def sw_interface_span_enable_disable(self, sw_if_index_from, sw_if_index_to, enable=1):
430 """
431
432 :param sw_if_index_from:
433 :param sw_if_index_to:
434 :param enable
435
436 """
437 return self.api(vpp_papi.sw_interface_span_enable_disable, (sw_if_index_from, sw_if_index_to, enable ))
Neale Ranns177bbdc2016-11-15 09:46:51 +0000438
439 def gre_tunnel_add_del(self,
440 src_address,
441 dst_address,
442 outer_fib_id=0,
443 is_teb=0,
444 is_add=1,
445 is_ip6=0):
446 """ Add a GRE tunnel
447
448 :param src_address:
449 :param dst_address:
450 :param outer_fib_id: (Default value = 0)
451 :param is_add: (Default value = 1)
452 :param is_ipv6: (Default value = 0)
453 :param is_teb: (Default value = 0)
454 """
455
456 return self.api(
457 vpp_papi.gre_add_del_tunnel,
458 (is_add,
459 is_ip6,
460 is_teb,
461 src_address,
462 dst_address,
463 outer_fib_id)
464 )