DHCPv[46] proxy tests
[vpp.git] / test / vpp_papi_provider.py
1 import os
2 import fnmatch
3 import time
4 from hook import Hook
5 from collections import deque
6
7 # Sphinx creates auto-generated documentation by importing the python source
8 # files and collecting the docstrings from them. The NO_VPP_PAPI flag allows
9 # the vpp_papi_provider.py file to be importable without having to build
10 # the whole vpp api if the user only wishes to generate the test documentation.
11 do_import = True
12 try:
13     no_vpp_papi = os.getenv("NO_VPP_PAPI")
14     if no_vpp_papi == "1":
15         do_import = False
16 except:
17     pass
18
19 if do_import:
20     from vpp_papi import VPP
21
22 # from vnet/vnet/mpls/mpls_types.h
23 MPLS_IETF_MAX_LABEL = 0xfffff
24 MPLS_LABEL_INVALID = MPLS_IETF_MAX_LABEL + 1
25
26
27 class L2_VTR_OP:
28     L2_POP_1 = 3
29
30
31 class VppPapiProvider(object):
32     """VPP-api provider using vpp-papi
33
34     @property hook: hook object providing before and after api/cli hooks
35     """
36
37     _zero, _negative = range(2)
38
39     def __init__(self, name, shm_prefix, test_class):
40         self.hook = Hook("vpp-papi-provider")
41         self.name = name
42         self.shm_prefix = shm_prefix
43         self.test_class = test_class
44         self._expect_api_retval = self._zero
45         self._expect_stack = []
46         jsonfiles = []
47
48         install_dir = os.getenv('VPP_TEST_INSTALL_PATH')
49         for root, dirnames, filenames in os.walk(install_dir):
50             for filename in fnmatch.filter(filenames, '*.api.json'):
51                 jsonfiles.append(os.path.join(root, filename))
52
53         self.papi = VPP(jsonfiles)
54         self._events = deque()
55
56     def __enter__(self):
57         return self
58
59     def expect_negative_api_retval(self):
60         """ Expect API failure """
61         self._expect_stack.append(self._expect_api_retval)
62         self._expect_api_retval = self._negative
63         return self
64
65     def expect_zero_api_retval(self):
66         """ Expect API success """
67         self._expect_stack.append(self._expect_api_retval)
68         self._expect_api_retval = self._zero
69         return self
70
71     def __exit__(self, exc_type, exc_value, traceback):
72         self._expect_api_retval = self._expect_stack.pop()
73
74     def register_hook(self, hook):
75         """Replace hook registration with new hook
76
77         :param hook:
78
79         """
80         self.hook = hook
81
82     def collect_events(self):
83         """ Collect all events from the internal queue and clear the queue. """
84         e = self._events
85         self._events = deque()
86         return e
87
88     def wait_for_event(self, timeout, name=None):
89         """ Wait for and return next event. """
90         if self._events:
91             self.test_class.logger.debug("Not waiting, event already queued")
92         limit = time.time() + timeout
93         while time.time() < limit:
94             if self._events:
95                 e = self._events.popleft()
96                 if name and type(e).__name__ != name:
97                     raise Exception(
98                         "Unexpected event received: %s, expected: %s" %
99                         (type(e).__name__, name))
100                 self.test_class.logger.debug("Returning event %s:%s" %
101                                              (name, e))
102                 return e
103             time.sleep(0)  # yield
104         if name is not None:
105             raise Exception("Event %s did not occur within timeout" % name)
106         raise Exception("Event did not occur within timeout")
107
108     def __call__(self, name, event):
109         """ Enqueue event in the internal event queue. """
110         # FIXME use the name instead of relying on type(e).__name__ ?
111         # FIXME #2 if this throws, it is eaten silently, Ole?
112         self.test_class.logger.debug("New event: %s: %s" % (name, event))
113         self._events.append(event)
114
115     def connect(self):
116         """Connect the API to VPP"""
117         self.papi.connect(self.name, self.shm_prefix)
118         self.papi.register_event_callback(self)
119
120     def disconnect(self):
121         """Disconnect the API from VPP"""
122         self.papi.disconnect()
123
124     def api(self, api_fn, api_args, expected_retval=0):
125         """ Call API function and check it's return value.
126         Call the appropriate hooks before and after the API call
127
128         :param api_fn: API function to call
129         :param api_args: tuple of API function arguments
130         :param expected_retval: Expected return value (Default value = 0)
131         :returns: reply from the API
132
133         """
134         self.hook.before_api(api_fn.__name__, api_args)
135         reply = api_fn(**api_args)
136         if self._expect_api_retval == self._negative:
137             if hasattr(reply, 'retval') and reply.retval >= 0:
138                 msg = "API call passed unexpectedly: expected negative "\
139                     "return value instead of %d in %s" % \
140                     (reply.retval, repr(reply))
141                 self.test_class.logger.info(msg)
142                 raise Exception(msg)
143         elif self._expect_api_retval == self._zero:
144             if hasattr(reply, 'retval') and reply.retval != expected_retval:
145                 msg = "API call failed, expected zero return value instead "\
146                     "of %d in %s" % (expected_retval, repr(reply))
147                 self.test_class.logger.info(msg)
148                 raise Exception(msg)
149         else:
150             raise Exception("Internal error, unexpected value for "
151                             "self._expect_api_retval %s" %
152                             self._expect_api_retval)
153         self.hook.after_api(api_fn.__name__, api_args)
154         return reply
155
156     def cli(self, cli):
157         """ Execute a CLI, calling the before/after hooks appropriately.
158
159         :param cli: CLI to execute
160         :returns: CLI output
161
162         """
163         self.hook.before_cli(cli)
164         cli += '\n'
165         r = self.papi.cli_inband(length=len(cli), cmd=cli)
166         self.hook.after_cli(cli)
167         if hasattr(r, 'reply'):
168             return r.reply.decode().rstrip('\x00')
169
170     def ppcli(self, cli):
171         """ Helper method to print CLI command in case of info logging level.
172
173         :param cli: CLI to execute
174         :returns: CLI output
175         """
176         return cli + "\n" + str(self.cli(cli))
177
178     def _convert_mac(self, mac):
179         return int(mac.replace(":", ""), 16) << 16
180
181     def show_version(self):
182         """ """
183         return self.papi.show_version()
184
185     def pg_create_interface(self, pg_index):
186         """
187
188         :param pg_index:
189
190         """
191         return self.api(self.papi.pg_create_interface,
192                         {"interface_id": pg_index})
193
194     def sw_interface_dump(self, filter=None):
195         """
196
197         :param filter:  (Default value = None)
198
199         """
200         if filter is not None:
201             args = {"name_filter_valid": 1, "name_filter": filter}
202         else:
203             args = {}
204         return self.api(self.papi.sw_interface_dump, args)
205
206     def sw_interface_set_table(self, sw_if_index, is_ipv6, table_id):
207         """ Set the IPvX Table-id for the Interface
208
209         :param sw_if_index:
210         :param is_ipv6:
211         :param table_id:
212
213         """
214         return self.api(self.papi.sw_interface_set_table,
215                         {'sw_if_index': sw_if_index, 'is_ipv6': is_ipv6,
216                          'vrf_id': table_id})
217
218     def sw_interface_add_del_address(self, sw_if_index, addr, addr_len,
219                                      is_ipv6=0, is_add=1, del_all=0):
220         """
221
222         :param addr: param is_ipv6:  (Default value = 0)
223         :param sw_if_index:
224         :param addr_len:
225         :param is_ipv6:  (Default value = 0)
226         :param is_add:  (Default value = 1)
227         :param del_all:  (Default value = 0)
228
229         """
230         return self.api(self.papi.sw_interface_add_del_address,
231                         {'sw_if_index': sw_if_index,
232                          'is_add': is_add,
233                          'is_ipv6': is_ipv6,
234                          'del_all': del_all,
235                          'address_length': addr_len,
236                          'address': addr})
237
238     def sw_interface_enable_disable_mpls(self, sw_if_index,
239                                          is_enable=1):
240         """
241         Enable/Disable MPLS on the interface
242         :param sw_if_index:
243         :param is_enable:  (Default value = 1)
244
245         """
246         return self.api(self.papi.sw_interface_set_mpls_enable,
247                         {'sw_if_index': sw_if_index,
248                          'enable': is_enable})
249
250     def sw_interface_ra_suppress(self, sw_if_index):
251         return self.api(self.papi.sw_interface_ip6nd_ra_config,
252                         {'sw_if_index': sw_if_index})
253
254     def ip6_sw_interface_ra_config(self, sw_if_index,
255                                    suppress,
256                                    send_unicast,):
257         return self.api(self.papi.sw_interface_ip6nd_ra_config,
258                         {'sw_if_index': sw_if_index,
259                          'suppress': suppress,
260                          'send_unicast': send_unicast})
261
262     def ip6_sw_interface_enable_disable(self, sw_if_index, enable):
263         """
264         Enable/Disable An interface for IPv6
265         """
266         return self.api(self.papi.sw_interface_ip6_enable_disable,
267                         {'sw_if_index': sw_if_index,
268                          'enable': enable})
269
270     def vxlan_add_del_tunnel(
271             self,
272             src_addr,
273             dst_addr,
274             mcast_sw_if_index=0xFFFFFFFF,
275             is_add=1,
276             is_ipv6=0,
277             encap_vrf_id=0,
278             decap_next_index=0xFFFFFFFF,
279             vni=0):
280         """
281
282         :param dst_addr:
283         :param src_addr:
284         :param is_add:  (Default value = 1)
285         :param is_ipv6:  (Default value = 0)
286         :param encap_vrf_id:  (Default value = 0)
287         :param decap_next_index:  (Default value = 0xFFFFFFFF)
288         :param mcast_sw_if_index:  (Default value = 0xFFFFFFFF)
289         :param vni:  (Default value = 0)
290
291         """
292         return self.api(self.papi.vxlan_add_del_tunnel,
293                         {'is_add': is_add,
294                          'is_ipv6': is_ipv6,
295                          'src_address': src_addr,
296                          'dst_address': dst_addr,
297                          'mcast_sw_if_index': mcast_sw_if_index,
298                          'encap_vrf_id': encap_vrf_id,
299                          'decap_next_index': decap_next_index,
300                          'vni': vni})
301
302     def bridge_domain_add_del(self, bd_id, flood=1, uu_flood=1, forward=1,
303                               learn=1, arp_term=0, is_add=1):
304         """Create/delete bridge domain.
305
306         :param int bd_id: Bridge domain index.
307         :param int flood: Enable/disable bcast/mcast flooding in the BD.
308             (Default value = 1)
309         :param int uu_flood: Enable/disable unknown unicast flood in the BD.
310             (Default value = 1)
311         :param int forward: Enable/disable forwarding on all interfaces in
312             the BD. (Default value = 1)
313         :param int learn: Enable/disable learning on all interfaces in the BD.
314             (Default value = 1)
315         :param int arp_term: Enable/disable arp termination in the BD.
316             (Default value = 1)
317         :param int is_add: Add or delete flag. (Default value = 1)
318         """
319         return self.api(self.papi.bridge_domain_add_del,
320                         {'bd_id': bd_id,
321                          'flood': flood,
322                          'uu_flood': uu_flood,
323                          'forward': forward,
324                          'learn': learn,
325                          'arp_term': arp_term,
326                          'is_add': is_add})
327
328     def l2fib_add_del(self, mac, bd_id, sw_if_index, is_add=1, static_mac=0,
329                       filter_mac=0, bvi_mac=0):
330         """Create/delete L2 FIB entry.
331
332         :param str mac: MAC address to create FIB entry for.
333         :param int bd_id: Bridge domain index.
334         :param int sw_if_index: Software interface index of the interface.
335         :param int is_add: Add or delete flag. (Default value = 1)
336         :param int static_mac: Set to 1 to create static MAC entry.
337             (Default value = 0)
338         :param int filter_mac: Set to 1 to drop packet that's source or
339             destination MAC address contains defined MAC address.
340             (Default value = 0)
341         :param int bvi_mac: Set to 1 to create entry that points to BVI
342             interface. (Default value = 0)
343         """
344         return self.api(self.papi.l2fib_add_del,
345                         {'mac': self._convert_mac(mac),
346                          'bd_id': bd_id,
347                          'sw_if_index': sw_if_index,
348                          'is_add': is_add,
349                          'static_mac': static_mac,
350                          'filter_mac': filter_mac,
351                          'bvi_mac': bvi_mac})
352
353     def sw_interface_set_l2_bridge(self, sw_if_index, bd_id,
354                                    shg=0, bvi=0, enable=1):
355         """Add/remove interface to/from bridge domain.
356
357         :param int sw_if_index: Software interface index of the interface.
358         :param int bd_id: Bridge domain index.
359         :param int shg: Split-horizon group index. (Default value = 0)
360         :param int bvi: Set interface as a bridge group virtual interface.
361             (Default value = 0)
362         :param int enable: Add or remove interface. (Default value = 1)
363         """
364         return self.api(self.papi.sw_interface_set_l2_bridge,
365                         {'rx_sw_if_index': sw_if_index,
366                          'bd_id': bd_id,
367                          'shg': shg,
368                          'bvi': bvi,
369                          'enable': enable})
370
371     def bridge_flags(self, bd_id, is_set, feature_bitmap):
372         """Enable/disable required feature of the bridge domain with defined ID.
373
374         :param int bd_id: Bridge domain ID.
375         :param int is_set: Set to 1 to enable, set to 0 to disable the feature.
376         :param int feature_bitmap: Bitmap value of the feature to be set:
377             - learn (1 << 0),
378             - forward (1 << 1),
379             - flood (1 << 2),
380             - uu-flood (1 << 3) or
381             - arp-term (1 << 4).
382         """
383         return self.api(self.papi.bridge_flags,
384                         {'bd_id': bd_id,
385                          'is_set': is_set,
386                          'feature_bitmap': feature_bitmap})
387
388     def bridge_domain_dump(self, bd_id=0):
389         """
390
391         :param int bd_id: Bridge domain ID. (Default value = 0 => dump of all
392             existing bridge domains returned)
393         :return: Dictionary of bridge domain(s) data.
394         """
395         return self.api(self.papi.bridge_domain_dump,
396                         {'bd_id': bd_id})
397
398     def sw_interface_set_l2_xconnect(self, rx_sw_if_index, tx_sw_if_index,
399                                      enable):
400         """Create or delete unidirectional cross-connect from Tx interface to
401         Rx interface.
402
403         :param int rx_sw_if_index: Software interface index of Rx interface.
404         :param int tx_sw_if_index: Software interface index of Tx interface.
405         :param int enable: Create cross-connect if equal to 1, delete
406             cross-connect if equal to 0.
407
408         """
409         return self.api(self.papi.sw_interface_set_l2_xconnect,
410                         {'rx_sw_if_index': rx_sw_if_index,
411                          'tx_sw_if_index': tx_sw_if_index,
412                          'enable': enable})
413
414     def sw_interface_set_l2_tag_rewrite(
415             self,
416             sw_if_index,
417             vtr_oper,
418             push=0,
419             tag1=0,
420             tag2=0):
421         """L2 interface vlan tag rewrite configure request
422         :param client_index - opaque cookie to identify the sender
423         :param context - sender context, to match reply w/ request
424         :param sw_if_index - interface the operation is applied to
425         :param vtr_op - Choose from l2_vtr_op_t enum values
426         :param push_dot1q - first pushed flag dot1q id set, else dot1ad
427         :param tag1 - Needed for any push or translate vtr op
428         :param tag2 - Needed for any push 2 or translate x-2 vtr ops
429
430         """
431         return self.api(self.papi.l2_interface_vlan_tag_rewrite,
432                         {'sw_if_index': sw_if_index,
433                          'vtr_op': vtr_oper,
434                          'push_dot1q': push,
435                          'tag1': tag1,
436                          'tag2': tag2})
437
438     def sw_interface_set_flags(self, sw_if_index, admin_up_down,
439                                link_up_down=0, deleted=0):
440         """
441
442         :param admin_up_down:
443         :param sw_if_index:
444         :param link_up_down:  (Default value = 0)
445         :param deleted:  (Default value = 0)
446
447         """
448         return self.api(self.papi.sw_interface_set_flags,
449                         {'sw_if_index': sw_if_index,
450                          'admin_up_down': admin_up_down,
451                          'link_up_down': link_up_down,
452                          'deleted': deleted})
453
454     def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
455                      no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
456                      default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):
457         """Create subinterface
458         from vpe.api: set dot1ad = 0 for dot1q, set dot1ad = 1 for dot1ad
459
460         :param sub_id: param inner_vlan:
461         :param sw_if_index:
462         :param outer_vlan:
463         :param inner_vlan:
464         :param no_tags:  (Default value = 0)
465         :param one_tag:  (Default value = 0)
466         :param two_tags:  (Default value = 0)
467         :param dot1ad:  (Default value = 0)
468         :param exact_match:  (Default value = 0)
469         :param default_sub:  (Default value = 0)
470         :param outer_vlan_id_any:  (Default value = 0)
471         :param inner_vlan_id_any:  (Default value = 0)
472
473         """
474         return self.api(
475             self.papi.create_subif,
476             {'sw_if_index': sw_if_index,
477              'sub_id': sub_id,
478              'no_tags': no_tags,
479              'one_tag': one_tag,
480              'two_tags': two_tags,
481              'dot1ad': dot1ad,
482              'exact_match': exact_match,
483              'default_sub': default_sub,
484              'outer_vlan_id_any': outer_vlan_id_any,
485              'inner_vlan_id_any': inner_vlan_id_any,
486              'outer_vlan_id': outer_vlan,
487              'inner_vlan_id': inner_vlan})
488
489     def delete_subif(self, sw_if_index):
490         """Delete subinterface
491
492         :param sw_if_index:
493         """
494         return self.api(self.papi.delete_subif,
495                         {'sw_if_index': sw_if_index})
496
497     def create_vlan_subif(self, sw_if_index, vlan):
498         """
499
500         :param vlan:
501         :param sw_if_index:
502
503         """
504         return self.api(self.papi.create_vlan_subif,
505                         {'sw_if_index': sw_if_index,
506                          'vlan_id': vlan})
507
508     def create_loopback(self, mac=''):
509         """
510
511         :param mac: (Optional)
512         """
513         return self.api(self.papi.create_loopback,
514                         {'mac_address': mac})
515
516     def delete_loopback(self, sw_if_index):
517         return self.api(self.papi.delete_loopback,
518                         {'sw_if_index': sw_if_index, })
519
520     def ip_add_del_route(
521             self,
522             dst_address,
523             dst_address_length,
524             next_hop_address,
525             next_hop_sw_if_index=0xFFFFFFFF,
526             table_id=0,
527             next_hop_table_id=0,
528             next_hop_weight=1,
529             next_hop_n_out_labels=0,
530             next_hop_out_label_stack=[],
531             next_hop_via_label=MPLS_LABEL_INVALID,
532             create_vrf_if_needed=0,
533             is_resolve_host=0,
534             is_resolve_attached=0,
535             classify_table_index=0xFFFFFFFF,
536             is_add=1,
537             is_drop=0,
538             is_unreach=0,
539             is_prohibit=0,
540             is_ipv6=0,
541             is_local=0,
542             is_classify=0,
543             is_multipath=0,
544             not_last=0):
545         """
546
547         :param dst_address_length:
548         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
549         :param dst_address:
550         :param next_hop_address:
551         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
552         :param vrf_id:  (Default value = 0)
553         :param lookup_in_vrf:  (Default value = 0)
554         :param classify_table_index:  (Default value = 0xFFFFFFFF)
555         :param create_vrf_if_needed:  (Default value = 0)
556         :param is_add:  (Default value = 1)
557         :param is_drop:  (Default value = 0)
558         :param is_ipv6:  (Default value = 0)
559         :param is_local:  (Default value = 0)
560         :param is_classify:  (Default value = 0)
561         :param is_multipath:  (Default value = 0)
562         :param is_resolve_host:  (Default value = 0)
563         :param is_resolve_attached:  (Default value = 0)
564         :param not_last:  (Default value = 0)
565         :param next_hop_weight:  (Default value = 1)
566
567         """
568
569         return self.api(
570             self.papi.ip_add_del_route,
571             {'next_hop_sw_if_index': next_hop_sw_if_index,
572              'table_id': table_id,
573              'classify_table_index': classify_table_index,
574              'next_hop_table_id': next_hop_table_id,
575              'create_vrf_if_needed': create_vrf_if_needed,
576              'is_add': is_add,
577              'is_drop': is_drop,
578              'is_unreach': is_unreach,
579              'is_prohibit': is_prohibit,
580              'is_ipv6': is_ipv6,
581              'is_local': is_local,
582              'is_classify': is_classify,
583              'is_multipath': is_multipath,
584              'is_resolve_host': is_resolve_host,
585              'is_resolve_attached': is_resolve_attached,
586              'not_last': not_last,
587              'next_hop_weight': next_hop_weight,
588              'dst_address_length': dst_address_length,
589              'dst_address': dst_address,
590              'next_hop_address': next_hop_address,
591              'next_hop_n_out_labels': next_hop_n_out_labels,
592              'next_hop_via_label': next_hop_via_label,
593              'next_hop_out_label_stack': next_hop_out_label_stack})
594
595     def ip_fib_dump(self):
596         return self.api(self.papi.ip_fib_dump, {})
597
598     def ip_neighbor_add_del(self,
599                             sw_if_index,
600                             mac_address,
601                             dst_address,
602                             vrf_id=0,
603                             is_add=1,
604                             is_ipv6=0,
605                             is_static=0,
606                             ):
607         """ Add neighbor MAC to IPv4 or IPv6 address.
608
609         :param sw_if_index:
610         :param mac_address:
611         :param dst_address:
612         :param vrf_id:  (Default value = 0)
613         :param is_add:  (Default value = 1)
614         :param is_ipv6:  (Default value = 0)
615         :param is_static:  (Default value = 0)
616         """
617
618         return self.api(
619             self.papi.ip_neighbor_add_del,
620             {'vrf_id': vrf_id,
621              'sw_if_index': sw_if_index,
622              'is_add': is_add,
623              'is_ipv6': is_ipv6,
624              'is_static': is_static,
625              'mac_address': mac_address,
626              'dst_address': dst_address
627              }
628         )
629
630     def reset_vrf(self,
631                   vrf_id,
632                   is_ipv6=0,
633                   ):
634         """ Reset VRF (remove all routes etc.) request.
635
636         :param int vrf_id: ID of the FIB table / VRF to reset.
637         :param int is_ipv6: 1 for IPv6 neighbor, 0 for IPv4. (Default = 0)
638         """
639
640         return self.api(
641             self.papi.reset_vrf,
642             {'vrf_id': vrf_id,
643              'is_ipv6': is_ipv6,
644              }
645         )
646
647     def reset_fib(self,
648                   vrf_id,
649                   is_ipv6=0,
650                   ):
651         """ Reset VRF (remove all routes etc.) request.
652
653         :param int vrf_id: ID of the FIB table / VRF to reset.
654         :param int is_ipv6: 1 for IPv6 neighbor, 0 for IPv4. (Default = 0)
655         """
656
657         return self.api(
658             self.papi.reset_fib,
659             {'vrf_id': vrf_id,
660              'is_ipv6': is_ipv6,
661              }
662         )
663
664     def ip_dump(self,
665                 is_ipv6=0,
666                 ):
667         """ Return IP dump.
668
669         :param int is_ipv6: 1 for IPv6 neighbor, 0 for IPv4. (Default = 0)
670         """
671
672         return self.api(
673             self.papi.ip_dump,
674             {'is_ipv6': is_ipv6,
675              }
676         )
677
678     def sw_interface_span_enable_disable(
679             self, sw_if_index_from, sw_if_index_to, state=1):
680         """
681
682         :param sw_if_index_from:
683         :param sw_if_index_to:
684         :param state:
685         """
686         return self.api(self.papi.sw_interface_span_enable_disable,
687                         {'sw_if_index_from': sw_if_index_from,
688                          'sw_if_index_to': sw_if_index_to,
689                          'state': state})
690
691     def gre_tunnel_add_del(self,
692                            src_address,
693                            dst_address,
694                            outer_fib_id=0,
695                            is_teb=0,
696                            is_add=1,
697                            is_ip6=0):
698         """ Add a GRE tunnel
699
700         :param src_address:
701         :param dst_address:
702         :param outer_fib_id:  (Default value = 0)
703         :param is_add:  (Default value = 1)
704         :param is_ipv6:  (Default value = 0)
705         :param is_teb:  (Default value = 0)
706         """
707
708         return self.api(
709             self.papi.gre_add_del_tunnel,
710             {'is_add': is_add,
711              'is_ipv6': is_ip6,
712              'teb': is_teb,
713              'src_address': src_address,
714              'dst_address': dst_address,
715              'outer_fib_id': outer_fib_id}
716         )
717
718     def mpls_route_add_del(
719             self,
720             label,
721             eos,
722             next_hop_proto_is_ip4,
723             next_hop_address,
724             next_hop_sw_if_index=0xFFFFFFFF,
725             table_id=0,
726             next_hop_table_id=0,
727             next_hop_weight=1,
728             next_hop_n_out_labels=0,
729             next_hop_out_label_stack=[],
730             next_hop_via_label=MPLS_LABEL_INVALID,
731             create_vrf_if_needed=0,
732             is_resolve_host=0,
733             is_resolve_attached=0,
734             is_add=1,
735             is_drop=0,
736             is_multipath=0,
737             classify_table_index=0xFFFFFFFF,
738             is_classify=0,
739             not_last=0):
740         """
741
742         :param dst_address_length:
743         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
744         :param dst_address:
745         :param next_hop_address:
746         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
747         :param vrf_id:  (Default value = 0)
748         :param lookup_in_vrf:  (Default value = 0)
749         :param classify_table_index:  (Default value = 0xFFFFFFFF)
750         :param create_vrf_if_needed:  (Default value = 0)
751         :param is_add:  (Default value = 1)
752         :param is_drop:  (Default value = 0)
753         :param is_ipv6:  (Default value = 0)
754         :param is_local:  (Default value = 0)
755         :param is_classify:  (Default value = 0)
756         :param is_multipath:  (Default value = 0)
757         :param is_resolve_host:  (Default value = 0)
758         :param is_resolve_attached:  (Default value = 0)
759         :param not_last:  (Default value = 0)
760         :param next_hop_weight:  (Default value = 1)
761
762         """
763
764         return self.api(
765             self.papi.mpls_route_add_del,
766             {'mr_label': label,
767              'mr_eos': eos,
768              'mr_table_id': table_id,
769              'mr_classify_table_index': classify_table_index,
770              'mr_create_table_if_needed': create_vrf_if_needed,
771              'mr_is_add': is_add,
772              'mr_is_classify': is_classify,
773              'mr_is_multipath': is_multipath,
774              'mr_is_resolve_host': is_resolve_host,
775              'mr_is_resolve_attached': is_resolve_attached,
776              'mr_next_hop_proto_is_ip4': next_hop_proto_is_ip4,
777              'mr_next_hop_weight': next_hop_weight,
778              'mr_next_hop': next_hop_address,
779              'mr_next_hop_n_out_labels': next_hop_n_out_labels,
780              'mr_next_hop_sw_if_index': next_hop_sw_if_index,
781              'mr_next_hop_table_id': next_hop_table_id,
782              'mr_next_hop_via_label': next_hop_via_label,
783              'mr_next_hop_out_label_stack': next_hop_out_label_stack})
784
785     def mpls_ip_bind_unbind(
786             self,
787             label,
788             dst_address,
789             dst_address_length,
790             table_id=0,
791             ip_table_id=0,
792             is_ip4=1,
793             create_vrf_if_needed=0,
794             is_bind=1):
795         """
796         """
797         return self.api(
798             self.papi.mpls_ip_bind_unbind,
799             {'mb_mpls_table_id': table_id,
800              'mb_label': label,
801              'mb_ip_table_id': ip_table_id,
802              'mb_create_table_if_needed': create_vrf_if_needed,
803              'mb_is_bind': is_bind,
804              'mb_is_ip4': is_ip4,
805              'mb_address_length': dst_address_length,
806              'mb_address': dst_address})
807
808     def mpls_tunnel_add_del(
809             self,
810             tun_sw_if_index,
811             next_hop_proto_is_ip4,
812             next_hop_address,
813             next_hop_sw_if_index=0xFFFFFFFF,
814             next_hop_table_id=0,
815             next_hop_weight=1,
816             next_hop_n_out_labels=0,
817             next_hop_out_label_stack=[],
818             next_hop_via_label=MPLS_LABEL_INVALID,
819             create_vrf_if_needed=0,
820             is_add=1,
821             l2_only=0):
822         """
823
824         :param dst_address_length:
825         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
826         :param dst_address:
827         :param next_hop_address:
828         :param next_hop_sw_if_index:  (Default value = 0xFFFFFFFF)
829         :param vrf_id:  (Default value = 0)
830         :param lookup_in_vrf:  (Default value = 0)
831         :param classify_table_index:  (Default value = 0xFFFFFFFF)
832         :param create_vrf_if_needed:  (Default value = 0)
833         :param is_add:  (Default value = 1)
834         :param is_drop:  (Default value = 0)
835         :param is_ipv6:  (Default value = 0)
836         :param is_local:  (Default value = 0)
837         :param is_classify:  (Default value = 0)
838         :param is_multipath:  (Default value = 0)
839         :param is_resolve_host:  (Default value = 0)
840         :param is_resolve_attached:  (Default value = 0)
841         :param not_last:  (Default value = 0)
842         :param next_hop_weight:  (Default value = 1)
843
844         """
845         return self.api(
846             self.papi.mpls_tunnel_add_del,
847             {'mt_sw_if_index': tun_sw_if_index,
848              'mt_is_add': is_add,
849              'mt_l2_only': l2_only,
850              'mt_next_hop_proto_is_ip4': next_hop_proto_is_ip4,
851              'mt_next_hop_weight': next_hop_weight,
852              'mt_next_hop': next_hop_address,
853              'mt_next_hop_n_out_labels': next_hop_n_out_labels,
854              'mt_next_hop_sw_if_index': next_hop_sw_if_index,
855              'mt_next_hop_table_id': next_hop_table_id,
856              'mt_next_hop_out_label_stack': next_hop_out_label_stack})
857
858     def snat_interface_add_del_feature(
859             self,
860             sw_if_index,
861             is_inside=1,
862             is_add=1):
863         """Enable/disable S-NAT feature on the interface
864
865         :param sw_if_index: Software index of the interface
866         :param is_inside: 1 if inside, 0 if outside (Default value = 1)
867         :param is_add: 1 if add, 0 if delete (Default value = 1)
868         """
869         return self.api(
870             self.papi.snat_interface_add_del_feature,
871             {'is_add': is_add,
872              'is_inside': is_inside,
873              'sw_if_index': sw_if_index})
874
875     def snat_add_static_mapping(
876             self,
877             local_ip,
878             external_ip=0,
879             external_sw_if_index=0xFFFFFFFF,
880             local_port=0,
881             external_port=0,
882             addr_only=1,
883             vrf_id=0,
884             is_add=1,
885             is_ip4=1):
886         """Add/delete S-NAT static mapping
887
888         :param local_ip: Local IP address
889         :param external_ip: External IP address
890         :param external_sw_if_index: External interface instead of IP address
891         :param local_port: Local port number (Default value = 0)
892         :param external_port: External port number (Default value = 0)
893         :param addr_only: 1 if address only mapping, 0 if address and port
894         :param vrf_id: VRF ID
895         :param is_add: 1 if add, 0 if delete (Default value = 1)
896         :param is_ip4: 1 if address type is IPv4 (Default value = 1)
897         """
898         return self.api(
899             self.papi.snat_add_static_mapping,
900             {'is_add': is_add,
901              'is_ip4': is_ip4,
902              'addr_only': addr_only,
903              'local_ip_address': local_ip,
904              'external_ip_address': external_ip,
905              'local_port': local_port,
906              'external_port': external_port,
907              'external_sw_if_index': external_sw_if_index,
908              'vrf_id': vrf_id})
909
910     def snat_add_address_range(
911             self,
912             first_ip_address,
913             last_ip_address,
914             is_add=1,
915             is_ip4=1):
916         """Add/del S-NAT address range
917
918         :param first_ip_address: First IP address
919         :param last_ip_address: Last IP address
920         :param is_add: 1 if add, 0 if delete (Default value = 1)
921         :param is_ip4: 1 if address type is IPv4 (Default value = 1)
922         """
923         return self.api(
924             self.papi.snat_add_address_range,
925             {'is_ip4': is_ip4,
926              'first_ip_address': first_ip_address,
927              'last_ip_address': last_ip_address,
928              'is_add': is_add})
929
930     def snat_address_dump(self):
931         """Dump S-NAT addresses
932         :return: Dictionary of S-NAT addresses
933         """
934         return self.api(self.papi.snat_address_dump, {})
935
936     def snat_interface_dump(self):
937         """Dump interfaces with S-NAT feature
938         :return: Dictionary of interfaces with S-NAT feature
939         """
940         return self.api(self.papi.snat_interface_dump, {})
941
942     def snat_static_mapping_dump(self):
943         """Dump S-NAT static mappings
944         :return: Dictionary of S-NAT static mappings
945         """
946         return self.api(self.papi.snat_static_mapping_dump, {})
947
948     def snat_show_config(self):
949         """Show S-NAT config
950         :return: S-NAT config parameters
951         """
952         return self.api(self.papi.snat_show_config, {})
953
954     def snat_add_interface_addr(
955             self,
956             sw_if_index,
957             is_add=1):
958         """Add/del S-NAT address from interface
959
960         :param sw_if_index: Software index of the interface
961         :param is_add: 1 if add, 0 if delete (Default value = 1)
962         """
963         return self.api(self.papi.snat_add_del_interface_addr,
964                         {'is_add': is_add, 'sw_if_index': sw_if_index})
965
966     def snat_interface_addr_dump(self):
967         """Dump S-NAT addresses interfaces
968         :return: Dictionary of S-NAT addresses interfaces
969         """
970         return self.api(self.papi.snat_interface_addr_dump, {})
971
972     def snat_ipfix(
973             self,
974             domain_id=1,
975             src_port=4739,
976             enable=1):
977         """Enable/disable S-NAT IPFIX logging
978
979         :param domain_id: Observation domain ID (Default value = 1)
980         :param src_port: Source port number (Default value = 4739)
981         :param enable: 1 if enable, 0 if disable (Default value = 1)
982         """
983         return self.api(
984             self.papi.snat_ipfix_enable_disable,
985             {'domain_id': domain_id,
986              'src_port': src_port,
987              'enable': enable})
988
989     def control_ping(self):
990         self.api(self.papi.control_ping)
991
992     def bfd_udp_add(self, sw_if_index, desired_min_tx, required_min_rx,
993                     detect_mult, local_addr, peer_addr, is_ipv6=0):
994         return self.api(self.papi.bfd_udp_add,
995                         {
996                             'sw_if_index': sw_if_index,
997                             'desired_min_tx': desired_min_tx,
998                             'required_min_rx': required_min_rx,
999                             'local_addr': local_addr,
1000                             'peer_addr': peer_addr,
1001                             'is_ipv6': is_ipv6,
1002                             'detect_mult': detect_mult,
1003                         })
1004
1005     def bfd_udp_del(self, sw_if_index, local_addr, peer_addr, is_ipv6=0):
1006         return self.api(self.papi.bfd_udp_del,
1007                         {
1008                             'sw_if_index': sw_if_index,
1009                             'local_addr': local_addr,
1010                             'peer_addr': peer_addr,
1011                             'is_ipv6': is_ipv6,
1012                         })
1013
1014     def bfd_udp_session_dump(self):
1015         return self.api(self.papi.bfd_udp_session_dump, {})
1016
1017     def bfd_session_set_flags(self, bs_idx, admin_up_down):
1018         return self.api(self.papi.bfd_session_set_flags, {
1019             'bs_index': bs_idx,
1020             'admin_up_down': admin_up_down,
1021         })
1022
1023     def want_bfd_events(self, enable_disable=1):
1024         return self.api(self.papi.want_bfd_events, {
1025             'enable_disable': enable_disable,
1026             'pid': os.getpid(),
1027         })
1028
1029     def classify_add_del_table(
1030             self,
1031             is_add,
1032             mask,
1033             match_n_vectors=1,
1034             table_index=0xFFFFFFFF,
1035             nbuckets=2,
1036             memory_size=2097152,
1037             skip_n_vectors=0,
1038             next_table_index=0xFFFFFFFF,
1039             miss_next_index=0xFFFFFFFF,
1040             current_data_flag=0,
1041             current_data_offset=0):
1042         """
1043         :param is_add:
1044         :param mask:
1045         :param match_n_vectors: (Default value = 1)
1046         :param table_index: (Default value = 0xFFFFFFFF)
1047         :param nbuckets:  (Default value = 2)
1048         :param memory_size:  (Default value = 2097152)
1049         :param skip_n_vectors:  (Default value = 0)
1050         :param next_table_index:  (Default value = 0xFFFFFFFF)
1051         :param miss_next_index:  (Default value = 0xFFFFFFFF)
1052         :param current_data_flag:  (Default value = 0)
1053         :param current_data_offset:  (Default value = 0)
1054         """
1055
1056         return self.api(
1057             self.papi.classify_add_del_table,
1058             {'is_add': is_add,
1059              'table_index': table_index,
1060              'nbuckets': nbuckets,
1061              'memory_size': memory_size,
1062              'skip_n_vectors': skip_n_vectors,
1063              'match_n_vectors': match_n_vectors,
1064              'next_table_index': next_table_index,
1065              'miss_next_index': miss_next_index,
1066              'current_data_flag': current_data_flag,
1067              'current_data_offset': current_data_offset,
1068              'mask': mask})
1069
1070     def classify_add_del_session(
1071             self,
1072             is_add,
1073             table_index,
1074             match,
1075             opaque_index=0xFFFFFFFF,
1076             hit_next_index=0xFFFFFFFF,
1077             advance=0,
1078             action=0,
1079             metadata=0):
1080         """
1081         :param is_add:
1082         :param table_index:
1083         :param match:
1084         :param opaque_index:  (Default value = 0xFFFFFFFF)
1085         :param hit_next_index:  (Default value = 0xFFFFFFFF)
1086         :param advance:  (Default value = 0)
1087         :param action:  (Default value = 0)
1088         :param metadata:  (Default value = 0)
1089         """
1090
1091         return self.api(
1092             self.papi.classify_add_del_session,
1093             {'is_add': is_add,
1094              'table_index': table_index,
1095              'hit_next_index': hit_next_index,
1096              'opaque_index': opaque_index,
1097              'advance': advance,
1098              'action': action,
1099              'metadata': metadata,
1100              'match': match})
1101
1102     def input_acl_set_interface(
1103             self,
1104             is_add,
1105             sw_if_index,
1106             ip4_table_index=0xFFFFFFFF,
1107             ip6_table_index=0xFFFFFFFF,
1108             l2_table_index=0xFFFFFFFF):
1109         """
1110         :param is_add:
1111         :param sw_if_index:
1112         :param ip4_table_index:  (Default value = 0xFFFFFFFF)
1113         :param ip6_table_index:  (Default value = 0xFFFFFFFF)
1114         :param l2_table_index:  (Default value = 0xFFFFFFFF)
1115         """
1116
1117         return self.api(
1118             self.papi.input_acl_set_interface,
1119             {'sw_if_index': sw_if_index,
1120              'ip4_table_index': ip4_table_index,
1121              'ip6_table_index': ip6_table_index,
1122              'l2_table_index': l2_table_index,
1123              'is_add': is_add})
1124
1125     def set_ipfix_exporter(
1126             self,
1127             collector_address,
1128             src_address,
1129             path_mtu,
1130             template_interval,
1131             vrf_id=0,
1132             collector_port=4739,
1133             udp_checksum=0):
1134         return self.api(
1135             self.papi.set_ipfix_exporter,
1136             {
1137                 'collector_address': collector_address,
1138                 'collector_port': collector_port,
1139                 'src_address': src_address,
1140                 'vrf_id': vrf_id,
1141                 'path_mtu': path_mtu,
1142                 'template_interval': template_interval,
1143                 'udp_checksum': udp_checksum,
1144             })
1145
1146     def dhcp_proxy_config(self,
1147                           dhcp_server,
1148                           dhcp_src_address,
1149                           rx_table_id=0,
1150                           server_table_id=0,
1151                           is_add=1,
1152                           is_ipv6=0,
1153                           insert_circuit_id=0):
1154         return self.api(
1155             self.papi.dhcp_proxy_config_2,
1156             {
1157                 'rx_vrf_id': rx_table_id,
1158                 'server_vrf_id': server_table_id,
1159                 'is_ipv6': is_ipv6,
1160                 'is_add': is_add,
1161                 'insert_circuit_id': insert_circuit_id,
1162                 'dhcp_server': dhcp_server,
1163                 'dhcp_src_address': dhcp_src_address,
1164             })
1165
1166     def dhcp_proxy_set_vss(self,
1167                            table_id,
1168                            fib_id,
1169                            oui,
1170                            is_add=1,
1171                            is_ip6=0):
1172         return self.api(
1173             self.papi.dhcp_proxy_set_vss,
1174             {
1175                 'tbl_id': table_id,
1176                 'fib_id': fib_id,
1177                 'is_ipv6': is_ip6,
1178                 'is_add': is_add,
1179                 'oui': oui,
1180             })