7 no_vpp_papi = os.getenv("NO_VPP_PAPI")
17 # from vnet/vnet/mpls/mpls_types.h
18 MPLS_IETF_MAX_LABEL = 0xfffff
19 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
24 need_swap = True if os.sys.byteorder == 'little' else False
26 class VppPapiProvider(object):
27 """VPP-api provider using vpp-papi
29 @property hook: hook object providing before and after api/cli hooks
34 def __init__(self, name, shm_prefix, test_class):
35 self.hook = Hook("vpp-papi-provider")
37 self.shm_prefix = shm_prefix
38 self.test_class = test_class
40 def register_hook(self, hook):
41 """Replace hook registration with new hook
49 """Connect the API to VPP"""
50 vpp_papi.connect(self.name, self.shm_prefix)
53 """Disconnect the API from VPP"""
56 def api(self, api_fn, api_args, expected_retval=0):
57 """Call API function and check it's return value
58 Call the appropriate hooks before and after the API call
60 :param api_fn: API function to call
61 :param api_args: tuple of API function arguments
62 :param expected_retval: Expected return value (Default value = 0)
63 :returns: reply from the API
66 self.hook.before_api(api_fn.__name__, api_args)
67 reply = api_fn(*api_args)
68 if hasattr(reply, 'retval') and reply.retval != expected_retval:
69 msg = "API call failed, expected retval == %d, got %s" % (
70 expected_retval, repr(reply))
71 self.test_class.test_instance.logger.error(msg)
73 self.hook.after_api(api_fn.__name__, api_args)
78 Execute a CLI, calling the before/after hooks appropriately.
80 :param cli: CLI to execute
84 self.hook.before_cli(cli)
86 r = vpp_papi.cli_inband(len(cli), cli)
87 self.hook.after_cli(cli)
88 if hasattr(r, 'reply'):
89 return r.reply[0].decode().rstrip('\x00')
93 Helping method to print CLI command in case of info logging level.
95 :param cli: CLI to execute
98 return cli + "\n" + str(self.cli(cli))
100 def _convert_mac(self, mac):
101 return int(mac.replace(":", ""), 16) << 16
103 def show_version(self):
105 return vpp_papi.show_version()
107 def pg_create_interface(self, pg_index):
113 return self.api(vpp_papi.pg_create_interface, (pg_index, ))
115 def sw_interface_dump(self, filter=None):
118 :param filter: (Default value = None)
121 if filter is not None:
125 return self.api(vpp_papi.sw_interface_dump, args)
127 def sw_interface_set_table(self, sw_if_index, is_ipv6, table_id):
129 Set the IPvX Table-id for the Interface
136 return self.api(vpp_papi.sw_interface_set_table,
137 (sw_if_index, is_ipv6, table_id))
139 def sw_interface_add_del_address(self, sw_if_index, addr, addr_len,
140 is_ipv6=0, is_add=1, del_all=0):
143 :param addr: param is_ipv6: (Default value = 0)
146 :param is_ipv6: (Default value = 0)
147 :param is_add: (Default value = 1)
148 :param del_all: (Default value = 0)
151 return self.api(vpp_papi.sw_interface_add_del_address,
152 (sw_if_index, is_add, is_ipv6, del_all, addr_len, addr))
154 def sw_interface_enable_disable_mpls(self, sw_if_index,
157 Enable/Disable MPLS on the interface
159 :param is_enable: (Default value = 1)
162 return self.api(vpp_papi.sw_interface_set_mpls_enable,
163 (sw_if_index, is_enable))
165 def sw_interface_ra_suppress(self, sw_if_index):
180 return self.api(vpp_papi.sw_interface_ip6nd_ra_config,
181 (sw_if_index, suppress, managed, other,
182 ll_option, send_unicast, cease, is_no,
183 default_router, max_interval, min_interval,
184 lifetime, initial_count, initial_interval, async))
186 def vxlan_add_del_tunnel(
190 mcast_sw_if_index=0xFFFFFFFF,
194 decap_next_index=0xFFFFFFFF,
200 :param is_add: (Default value = 1)
201 :param is_ipv6: (Default value = 0)
202 :param encap_vrf_id: (Default value = 0)
203 :param decap_next_index: (Default value = 0xFFFFFFFF)
204 :param mcast_sw_if_index: (Default value = 0xFFFFFFFF)
205 :param vni: (Default value = 0)
208 return self.api(vpp_papi.vxlan_add_del_tunnel,
209 (is_add, is_ipv6, src_addr, dst_addr, mcast_sw_if_index,
210 encap_vrf_id, decap_next_index, vni))
212 def bridge_domain_add_del(self, bd_id, flood=1, uu_flood=1, forward=1,
213 learn=1, arp_term=0, is_add=1):
214 """Create/delete bridge domain.
216 :param int bd_id: Bridge domain index.
217 :param int flood: Enable/disable bcast/mcast flooding in the BD.
219 :param int uu_flood: Enable/disable unknown unicast flood in the BD.
221 :param int forward: Enable/disable forwarding on all interfaces in
222 the BD. (Default value = 1)
223 :param int learn: Enable/disable learning on all interfaces in the BD.
225 :param int arp_term: Enable/disable arp termination in the BD.
227 :param int is_add: Add or delete flag. (Default value = 1)
229 return self.api(vpp_papi.bridge_domain_add_del,
230 (bd_id, flood, uu_flood, forward, learn, arp_term,
233 def l2fib_add_del(self, mac, bd_id, sw_if_index, is_add=1, static_mac=0,
234 filter_mac=0, bvi_mac=0):
235 """Create/delete L2 FIB entry.
237 :param str mac: MAC address to create FIB entry for.
238 :param int bd_id: Bridge domain index.
239 :param int sw_if_index: Software interface index of the interface.
240 :param int is_add: Add or delete flag. (Default value = 1)
241 :param int static_mac: Set to 1 to create static MAC entry.
243 :param int filter_mac: Set to 1 to drop packet that's source or
244 destination MAC address contains defined MAC address.
246 :param int bvi_mac: Set to 1 to create entry that points to BVI
247 interface. (Default value = 0)
249 return self.api(vpp_papi.l2fib_add_del,
250 (self._convert_mac(mac), bd_id, sw_if_index, is_add,
251 static_mac, filter_mac, bvi_mac))
253 def sw_interface_set_l2_bridge(self, sw_if_index, bd_id,
254 shg=0, bvi=0, enable=1):
255 """Add/remove interface to/from bridge domain.
257 :param int sw_if_index: Software interface index of the interface.
258 :param int bd_id: Bridge domain index.
259 :param int shg: Split-horizon group index. (Default value = 0)
260 :param int bvi: Set interface as a bridge group virtual interface.
262 :param int enable: Add or remove interface. (Default value = 1)
264 return self.api(vpp_papi.sw_interface_set_l2_bridge,
265 (sw_if_index, bd_id, shg, bvi, enable))
267 def bridge_flags(self, bd_id, is_set, feature_bitmap):
268 """Enable/disable required feature of the bridge domain with defined ID.
270 :param int bd_id: Bridge domain ID.
271 :param int is_set: Set to 1 to enable, set to 0 to disable the feature.
272 :param int feature_bitmap: Bitmap value of the feature to be set:
276 - uu-flood (1 << 3) or
279 return self.api(vpp_papi.bridge_flags, (bd_id, is_set, feature_bitmap))
281 def bridge_domain_dump(self, bd_id=0):
284 :param int bd_id: Bridge domain ID. (Default value = 0 => dump of all
285 existing bridge domains returned)
286 :return: Dictionary of bridge domain(s) data.
288 return self.api(vpp_papi.bridge_domain_dump, (bd_id, ))
290 def sw_interface_set_l2_xconnect(self, rx_sw_if_index, tx_sw_if_index,
292 """Create or delete unidirectional cross-connect from Tx interface to
295 :param int rx_sw_if_index: Software interface index of Rx interface.
296 :param int tx_sw_if_index: Software interface index of Tx interface.
297 :param int enable: Create cross-connect if equal to 1, delete
298 cross-connect if equal to 0.
301 return self.api(vpp_papi.sw_interface_set_l2_xconnect,
302 (rx_sw_if_index, tx_sw_if_index, enable))
304 def sw_interface_set_l2_tag_rewrite(self, sw_if_index, vtr_oper, push=0, tag1=0, tag2=0):
305 """L2 interface vlan tag rewrite configure request
306 :param client_index - opaque cookie to identify the sender
307 :param context - sender context, to match reply w/ request
308 :param sw_if_index - interface the operation is applied to
309 :param vtr_op - Choose from l2_vtr_op_t enum values
310 :param push_dot1q - first pushed flag dot1q id set, else dot1ad
311 :param tag1 - Needed for any push or translate vtr op
312 :param tag2 - Needed for any push 2 or translate x-2 vtr ops
315 return self.api(vpp_papi.l2_interface_vlan_tag_rewrite,
316 (sw_if_index, vtr_oper, push, tag1, tag2))
318 def sw_interface_set_flags(self, sw_if_index, admin_up_down,
319 link_up_down=0, deleted=0):
322 :param admin_up_down:
324 :param link_up_down: (Default value = 0)
325 :param deleted: (Default value = 0)
328 return self.api(vpp_papi.sw_interface_set_flags,
329 (sw_if_index, admin_up_down, link_up_down, deleted))
331 def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
332 no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
333 default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):
334 """Create subinterface
335 from vpe.api: set dot1ad = 0 for dot1q, set dot1ad = 1 for dot1ad
337 :param sub_id: param inner_vlan:
341 :param no_tags: (Default value = 0)
342 :param one_tag: (Default value = 0)
343 :param two_tags: (Default value = 0)
344 :param dot1ad: (Default value = 0)
345 :param exact_match: (Default value = 0)
346 :param default_sub: (Default value = 0)
347 :param outer_vlan_id_any: (Default value = 0)
348 :param inner_vlan_id_any: (Default value = 0)
352 vpp_papi.create_subif,
366 def delete_subif(self, sw_if_index):
367 """Delete subinterface
371 return self.api(vpp_papi.delete_subif, ([sw_if_index]))
373 def create_vlan_subif(self, sw_if_index, vlan):
380 return self.api(vpp_papi.create_vlan_subif, (sw_if_index, vlan))
382 def create_loopback(self, mac=''):
385 :param mac: (Optional)
387 return self.api(vpp_papi.create_loopback, (mac,))
389 def ip_add_del_route(
394 next_hop_sw_if_index=0xFFFFFFFF,
398 next_hop_n_out_labels = 0,
399 next_hop_out_label_stack = [],
400 next_hop_via_label = MPLS_LABEL_INVALID,
401 create_vrf_if_needed=0,
403 is_resolve_attached=0,
404 classify_table_index=0xFFFFFFFF,
416 :param dst_address_length:
417 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
419 :param next_hop_address:
420 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
421 :param vrf_id: (Default value = 0)
422 :param lookup_in_vrf: (Default value = 0)
423 :param classify_table_index: (Default value = 0xFFFFFFFF)
424 :param create_vrf_if_needed: (Default value = 0)
425 :param is_add: (Default value = 1)
426 :param is_drop: (Default value = 0)
427 :param is_ipv6: (Default value = 0)
428 :param is_local: (Default value = 0)
429 :param is_classify: (Default value = 0)
430 :param is_multipath: (Default value = 0)
431 :param is_resolve_host: (Default value = 0)
432 :param is_resolve_attached: (Default value = 0)
433 :param not_last: (Default value = 0)
434 :param next_hop_weight: (Default value = 1)
437 stack = array.array('I', next_hop_out_label_stack)
440 stack = stack.tostring()
443 vpp_papi.ip_add_del_route,
444 (next_hop_sw_if_index,
446 classify_table_index,
448 create_vrf_if_needed,
464 next_hop_n_out_labels,
468 def ip_neighbor_add_del(self,
477 """ Add neighbor MAC to IPv4 or IPv6 address.
482 :param vrf_id: (Default value = 0)
483 :param is_add: (Default value = 1)
484 :param is_ipv6: (Default value = 0)
485 :param is_static: (Default value = 0)
489 vpp_papi.ip_neighbor_add_del,
500 def sw_interface_span_enable_disable(
501 self, sw_if_index_from, sw_if_index_to, enable=1):
504 :param sw_if_index_from:
505 :param sw_if_index_to:
509 return self.api(vpp_papi.sw_interface_span_enable_disable, (sw_if_index_from, sw_if_index_to, enable ))
511 def gre_tunnel_add_del(self,
522 :param outer_fib_id: (Default value = 0)
523 :param is_add: (Default value = 1)
524 :param is_ipv6: (Default value = 0)
525 :param is_teb: (Default value = 0)
529 vpp_papi.gre_add_del_tunnel,
538 def mpls_route_add_del(
542 next_hop_proto_is_ip4,
544 next_hop_sw_if_index=0xFFFFFFFF,
548 next_hop_n_out_labels = 0,
549 next_hop_out_label_stack = [],
550 next_hop_via_label = MPLS_LABEL_INVALID,
551 create_vrf_if_needed=0,
553 is_resolve_attached=0,
557 classify_table_index=0xFFFFFFFF,
562 :param dst_address_length:
563 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
565 :param next_hop_address:
566 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
567 :param vrf_id: (Default value = 0)
568 :param lookup_in_vrf: (Default value = 0)
569 :param classify_table_index: (Default value = 0xFFFFFFFF)
570 :param create_vrf_if_needed: (Default value = 0)
571 :param is_add: (Default value = 1)
572 :param is_drop: (Default value = 0)
573 :param is_ipv6: (Default value = 0)
574 :param is_local: (Default value = 0)
575 :param is_classify: (Default value = 0)
576 :param is_multipath: (Default value = 0)
577 :param is_resolve_host: (Default value = 0)
578 :param is_resolve_attached: (Default value = 0)
579 :param not_last: (Default value = 0)
580 :param next_hop_weight: (Default value = 1)
583 stack = array.array('I', next_hop_out_label_stack)
586 stack = stack.tostring()
589 vpp_papi.mpls_route_add_del,
593 classify_table_index,
594 create_vrf_if_needed,
600 next_hop_proto_is_ip4,
603 next_hop_n_out_labels,
604 next_hop_sw_if_index,
609 def mpls_ip_bind_unbind(
617 create_vrf_if_needed=0,
622 vpp_papi.mpls_ip_bind_unbind,
626 create_vrf_if_needed,
632 def mpls_tunnel_add_del(
635 next_hop_proto_is_ip4,
637 next_hop_sw_if_index=0xFFFFFFFF,
640 next_hop_n_out_labels = 0,
641 next_hop_out_label_stack = [],
642 next_hop_via_label = MPLS_LABEL_INVALID,
643 create_vrf_if_needed=0,
648 :param dst_address_length:
649 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
651 :param next_hop_address:
652 :param next_hop_sw_if_index: (Default value = 0xFFFFFFFF)
653 :param vrf_id: (Default value = 0)
654 :param lookup_in_vrf: (Default value = 0)
655 :param classify_table_index: (Default value = 0xFFFFFFFF)
656 :param create_vrf_if_needed: (Default value = 0)
657 :param is_add: (Default value = 1)
658 :param is_drop: (Default value = 0)
659 :param is_ipv6: (Default value = 0)
660 :param is_local: (Default value = 0)
661 :param is_classify: (Default value = 0)
662 :param is_multipath: (Default value = 0)
663 :param is_resolve_host: (Default value = 0)
664 :param is_resolve_attached: (Default value = 0)
665 :param not_last: (Default value = 0)
666 :param next_hop_weight: (Default value = 1)
669 stack = array.array('I', next_hop_out_label_stack)
672 stack = stack.tostring()
675 vpp_papi.mpls_tunnel_add_del,
679 next_hop_proto_is_ip4,
682 next_hop_n_out_labels,
683 next_hop_sw_if_index,
687 return self.api(vpp_papi.sw_interface_span_enable_disable,
688 (sw_if_index_from, sw_if_index_to, enable))