blob: aec052c2f40185aeedc61738d2b4cf35b1c75a4a [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 """
Ed Warnickeb8ff5d62016-11-28 13:59:22 -060096 return cli + "\n" + str(self.cli(cli))
Jan49c0fca2016-10-26 15:44:27 +020097
Jan4af521d2016-11-15 17:05:00 +010098 def _convert_mac(self, mac):
99 return int(mac.replace(":", ""), 16) << 16
100
Klement Sekeraf62ae122016-10-11 11:47:09 +0200101 def show_version(self):
102 """ """
103 return vpp_papi.show_version()
104
105 def pg_create_interface(self, pg_index):
106 """
107
108 :param pg_index:
109
110 """
111 return self.api(vpp_papi.pg_create_interface, (pg_index, ))
112
113 def sw_interface_dump(self, filter=None):
114 """
115
116 :param filter: (Default value = None)
117
118 """
119 if filter is not None:
120 args = (1, filter)
121 else:
122 args = (0, b'')
123 return self.api(vpp_papi.sw_interface_dump, args)
124
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000125 def sw_interface_set_table(self, sw_if_index, is_ipv6, table_id):
126 """
127 Set the IPvX Table-id for the Interface
128
129 :param sw_if_index:
130 :param is_ipv6:
131 :param table_id:
132
133 """
134 return self.api(vpp_papi.sw_interface_set_table,
135 (sw_if_index, is_ipv6, table_id))
136
Klement Sekeraf62ae122016-10-11 11:47:09 +0200137 def sw_interface_add_del_address(self, sw_if_index, addr, addr_len,
138 is_ipv6=0, is_add=1, del_all=0):
139 """
140
141 :param addr: param is_ipv6: (Default value = 0)
142 :param sw_if_index:
143 :param addr_len:
144 :param is_ipv6: (Default value = 0)
145 :param is_add: (Default value = 1)
146 :param del_all: (Default value = 0)
147
148 """
149 return self.api(vpp_papi.sw_interface_add_del_address,
150 (sw_if_index, is_add, is_ipv6, del_all, addr_len, addr))
151
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000152 def sw_interface_enable_disable_mpls(self, sw_if_index,
153 is_enable=1):
154 """
155 Enable/Disable MPLS on the interface
156 :param sw_if_index:
157 :param is_enable: (Default value = 1)
158
159 """
160 return self.api(vpp_papi.sw_interface_set_mpls_enable,
161 (sw_if_index, is_enable))
162
Klement Sekeraf62ae122016-10-11 11:47:09 +0200163 def sw_interface_ra_suppress(self, sw_if_index):
164 suppress = 1
165 managed = 0
166 other = 0
167 ll_option = 0
168 send_unicast = 0
169 cease = 0
170 is_no = 0
171 default_router = 0
172 max_interval = 0
173 min_interval = 0
174 lifetime = 0
175 initial_count = 0
176 initial_interval = 0
177 async = False
178 return self.api(vpp_papi.sw_interface_ip6nd_ra_config,
179 (sw_if_index, suppress, managed, other,
180 ll_option, send_unicast, cease, is_no,
181 default_router, max_interval, min_interval,
182 lifetime, initial_count, initial_interval, async))
183
Klement Sekeraf62ae122016-10-11 11:47:09 +0200184 def vxlan_add_del_tunnel(
185 self,
186 src_addr,
187 dst_addr,
188 is_add=1,
189 is_ipv6=0,
190 encap_vrf_id=0,
191 decap_next_index=0xFFFFFFFF,
192 vni=0):
193 """
194
195 :param dst_addr:
196 :param src_addr:
197 :param is_add: (Default value = 1)
198 :param is_ipv6: (Default value = 0)
199 :param encap_vrf_id: (Default value = 0)
200 :param decap_next_index: (Default value = 0xFFFFFFFF)
201 :param vni: (Default value = 0)
202
203 """
204 return self.api(vpp_papi.vxlan_add_del_tunnel,
205 (is_add, is_ipv6, src_addr, dst_addr, encap_vrf_id,
206 decap_next_index, vni))
207
Jan4af521d2016-11-15 17:05:00 +0100208 def bridge_domain_add_del(self, bd_id, flood=1, uu_flood=1, forward=1,
209 learn=1, arp_term=0, is_add=1):
210 """Create/delete bridge domain.
211
212 :param int bd_id: Bridge domain index.
213 :param int flood: Enable/disable bcast/mcast flooding in the BD.
214 (Default value = 1)
215 :param int uu_flood: Enable/disable unknown unicast flood in the BD.
216 (Default value = 1)
217 :param int forward: Enable/disable forwarding on all interfaces in
218 the BD. (Default value = 1)
219 :param int learn: Enable/disable learning on all interfaces in the BD.
220 (Default value = 1)
221 :param int arp_term: Enable/disable arp termination in the BD.
222 (Default value = 1)
223 :param int is_add: Add or delete flag. (Default value = 1)
224 """
225 return self.api(vpp_papi.bridge_domain_add_del,
226 (bd_id, flood, uu_flood, forward, learn, arp_term,
227 is_add))
228
229 def l2fib_add_del(self, mac, bd_id, sw_if_index, is_add=1, static_mac=0,
230 filter_mac=0, bvi_mac=0):
231 """Create/delete L2 FIB entry.
232
233 :param str mac: MAC address to create FIB entry for.
234 :param int bd_id: Bridge domain index.
235 :param int sw_if_index: Software interface index of the interface.
236 :param int is_add: Add or delete flag. (Default value = 1)
237 :param int static_mac: Set to 1 to create static MAC entry.
238 (Default value = 0)
239 :param int filter_mac: Set to 1 to drop packet that's source or
240 destination MAC address contains defined MAC address.
241 (Default value = 0)
242 :param int bvi_mac: Set to 1 to create entry that points to BVI
243 interface. (Default value = 0)
244 """
245 return self.api(vpp_papi.l2fib_add_del,
246 (self._convert_mac(mac), bd_id, sw_if_index, is_add,
247 static_mac, filter_mac, bvi_mac))
248
Klement Sekeraf62ae122016-10-11 11:47:09 +0200249 def sw_interface_set_l2_bridge(self, sw_if_index, bd_id,
250 shg=0, bvi=0, enable=1):
Jan4af521d2016-11-15 17:05:00 +0100251 """Add/remove interface to/from bridge domain.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200252
Jan4af521d2016-11-15 17:05:00 +0100253 :param int sw_if_index: Software interface index of the interface.
254 :param int bd_id: Bridge domain index.
255 :param int shg: Split-horizon group index. (Default value = 0)
256 :param int bvi: Set interface as a bridge group virtual interface.
257 (Default value = 0)
258 :param int enable: Add or remove interface. (Default value = 1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200259 """
260 return self.api(vpp_papi.sw_interface_set_l2_bridge,
261 (sw_if_index, bd_id, shg, bvi, enable))
262
263 def sw_interface_set_l2_xconnect(self, rx_sw_if_index, tx_sw_if_index,
264 enable):
265 """Create or delete unidirectional cross-connect from Tx interface to
266 Rx interface.
267
Jan4af521d2016-11-15 17:05:00 +0100268 :param int rx_sw_if_index: Software interface index of Rx interface.
269 :param int tx_sw_if_index: Software interface index of Tx interface.
270 :param int enable: Create cross-connect if equal to 1, delete
271 cross-connect if equal to 0.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200272
273 """
274 return self.api(vpp_papi.sw_interface_set_l2_xconnect,
275 (rx_sw_if_index, tx_sw_if_index, enable))
276
Neale Ranns177bbdc2016-11-15 09:46:51 +0000277 def sw_interface_set_l2_tag_rewrite(self, sw_if_index, vtr_oper, push=0, tag1=0, tag2=0):
278 """L2 interface vlan tag rewrite configure request
279 :param client_index - opaque cookie to identify the sender
280 :param context - sender context, to match reply w/ request
281 :param sw_if_index - interface the operation is applied to
282 :param vtr_op - Choose from l2_vtr_op_t enum values
283 :param push_dot1q - first pushed flag dot1q id set, else dot1ad
284 :param tag1 - Needed for any push or translate vtr op
285 :param tag2 - Needed for any push 2 or translate x-2 vtr ops
286
287 """
288 return self.api(vpp_papi.l2_interface_vlan_tag_rewrite,
289 (sw_if_index, vtr_oper, push, tag1, tag2))
290
Klement Sekeraf62ae122016-10-11 11:47:09 +0200291 def sw_interface_set_flags(self, sw_if_index, admin_up_down,
292 link_up_down=0, deleted=0):
293 """
294
295 :param admin_up_down:
296 :param sw_if_index:
297 :param link_up_down: (Default value = 0)
298 :param deleted: (Default value = 0)
299
300 """
301 return self.api(vpp_papi.sw_interface_set_flags,
302 (sw_if_index, admin_up_down, link_up_down, deleted))
303
304 def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
305 no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
306 default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):
307 """Create subinterface
308 from vpe.api: set dot1ad = 0 for dot1q, set dot1ad = 1 for dot1ad
309
310 :param sub_id: param inner_vlan:
311 :param sw_if_index:
312 :param outer_vlan:
313 :param inner_vlan:
314 :param no_tags: (Default value = 0)
315 :param one_tag: (Default value = 0)
316 :param two_tags: (Default value = 0)
317 :param dot1ad: (Default value = 0)
318 :param exact_match: (Default value = 0)
319 :param default_sub: (Default value = 0)
320 :param outer_vlan_id_any: (Default value = 0)
321 :param inner_vlan_id_any: (Default value = 0)
322
323 """
324 return self.api(
325 vpp_papi.create_subif,
326 (sw_if_index,
327 sub_id,
328 no_tags,
329 one_tag,
330 two_tags,
331 dot1ad,
332 exact_match,
333 default_sub,
334 outer_vlan_id_any,
335 inner_vlan_id_any,
336 outer_vlan,
337 inner_vlan))
338
Neale Ranns177bbdc2016-11-15 09:46:51 +0000339 def delete_subif(self, sw_if_index):
340 """Delete subinterface
341
342 :param sw_if_index:
343 """
344 return self.api(vpp_papi.delete_subif, ([sw_if_index]))
345
Klement Sekeraf62ae122016-10-11 11:47:09 +0200346 def create_vlan_subif(self, sw_if_index, vlan):
347 """
348
349 :param vlan:
350 :param sw_if_index:
351
352 """
353 return self.api(vpp_papi.create_vlan_subif, (sw_if_index, vlan))
354
Matej Klotton0178d522016-11-04 11:11:44 +0100355 def create_loopback(self, mac=''):
356 """
357
358 :param mac: (Optional)
359 """
360 return self.api(vpp_papi.create_loopback, (mac,))
361
Klement Sekeraf62ae122016-10-11 11:47:09 +0200362 def ip_add_del_route(
363 self,
364 dst_address,
365 dst_address_length,
366 next_hop_address,
367 next_hop_sw_if_index=0xFFFFFFFF,
368 table_id=0,
369 resolve_attempts=0,
370 classify_table_index=0xFFFFFFFF,
371 next_hop_out_label=MPLS_LABEL_INVALID,
372 next_hop_table_id=0,
373 create_vrf_if_needed=0,
374 resolve_if_needed=0,
375 is_add=1,
376 is_drop=0,
Juraj Sloboda86a2c572016-10-27 10:44:25 +0200377 is_unreach=0,
378 is_prohibit=0,
Klement Sekeraf62ae122016-10-11 11:47:09 +0200379 is_ipv6=0,
380 is_local=0,
381 is_classify=0,
382 is_multipath=0,
383 is_resolve_host=0,
384 is_resolve_attached=0,
385 not_last=0,
386 next_hop_weight=1):
387 """
388
389 :param dst_address_length:
390 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
391 :param dst_address:
392 :param next_hop_address:
393 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
394 :param vrf_id: (Default value = 0)
395 :param lookup_in_vrf: (Default value = 0)
396 :param resolve_attempts: (Default value = 0)
397 :param classify_table_index: (Default value = 0xFFFFFFFF)
398 :param create_vrf_if_needed: (Default value = 0)
399 :param resolve_if_needed: (Default value = 0)
400 :param is_add: (Default value = 1)
401 :param is_drop: (Default value = 0)
402 :param is_ipv6: (Default value = 0)
403 :param is_local: (Default value = 0)
404 :param is_classify: (Default value = 0)
405 :param is_multipath: (Default value = 0)
406 :param is_resolve_host: (Default value = 0)
407 :param is_resolve_attached: (Default value = 0)
408 :param not_last: (Default value = 0)
409 :param next_hop_weight: (Default value = 1)
410
411 """
412 return self.api(
413 vpp_papi.ip_add_del_route,
414 (next_hop_sw_if_index,
415 table_id,
416 resolve_attempts,
417 classify_table_index,
418 next_hop_out_label,
419 next_hop_table_id,
420 create_vrf_if_needed,
421 resolve_if_needed,
422 is_add,
423 is_drop,
Juraj Sloboda86a2c572016-10-27 10:44:25 +0200424 is_unreach,
425 is_prohibit,
Klement Sekeraf62ae122016-10-11 11:47:09 +0200426 is_ipv6,
427 is_local,
428 is_classify,
429 is_multipath,
430 is_resolve_host,
431 is_resolve_attached,
432 not_last,
433 next_hop_weight,
434 dst_address_length,
435 dst_address,
436 next_hop_address))
Matej Klotton0178d522016-11-04 11:11:44 +0100437
438 def ip_neighbor_add_del(self,
439 sw_if_index,
440 mac_address,
441 dst_address,
442 vrf_id=0,
443 is_add=1,
444 is_ipv6=0,
445 is_static=0,
446 ):
447 """ Add neighbor MAC to IPv4 or IPv6 address.
448
449 :param sw_if_index:
450 :param mac_address:
451 :param dst_address:
452 :param vrf_id: (Default value = 0)
453 :param is_add: (Default value = 1)
454 :param is_ipv6: (Default value = 0)
455 :param is_static: (Default value = 0)
456 """
457
458 return self.api(
459 vpp_papi.ip_neighbor_add_del,
460 (vrf_id,
461 sw_if_index,
462 is_add,
463 is_ipv6,
464 is_static,
465 mac_address,
466 dst_address
467 )
468 )
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100469
470 def sw_interface_span_enable_disable(self, sw_if_index_from, sw_if_index_to, enable=1):
471 """
472
473 :param sw_if_index_from:
474 :param sw_if_index_to:
475 :param enable
476
477 """
478 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 +0000479
480 def gre_tunnel_add_del(self,
481 src_address,
482 dst_address,
483 outer_fib_id=0,
484 is_teb=0,
485 is_add=1,
486 is_ip6=0):
487 """ Add a GRE tunnel
488
489 :param src_address:
490 :param dst_address:
491 :param outer_fib_id: (Default value = 0)
492 :param is_add: (Default value = 1)
493 :param is_ipv6: (Default value = 0)
494 :param is_teb: (Default value = 0)
495 """
496
497 return self.api(
498 vpp_papi.gre_add_del_tunnel,
499 (is_add,
500 is_ip6,
501 is_teb,
502 src_address,
503 dst_address,
504 outer_fib_id)
505 )