blob: d1c40a97bcec2bfe7edf59eeb05ae16e315af1e9 [file] [log] [blame]
Ole Troan9a475372019-03-05 16:58:24 +01001# NB NB NB NB NB NB NB NB NB NB NB
2#
3# NOTE: The API binary wrappers in this file are in the process of being
4# deprecated. DO NOT ADD NEW WRAPPERS HERE. Call the functions using
5# named arguments directly instead.
6#
7
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08008import os
Klement Sekera0e3c0de2016-09-29 14:43:44 +02009import time
Ole Troan4376ab22021-03-03 10:40:05 +010010import queue
Ole Troanf159f582019-02-28 20:20:47 +010011from six import moves, iteritems
Klement Sekerab23ffd72021-05-31 16:08:53 +020012from config import config
Klement Sekera77c821c2022-04-14 16:29:07 +020013from vpp_papi import VPPApiClient
Paul Vinciguerra00671cf2018-11-25 12:47:04 -080014from hook import Hook
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015from vpp_papi_exceptions import (
16 CliFailedCommandError,
17 CliSyntaxError,
18 UnexpectedApiReturnValueError,
19)
Klement Sekeraf62ae122016-10-11 11:47:09 +020020
Ole Troanf159f582019-02-28 20:20:47 +010021#
22# Dictionary keyed on message name to override default values for
23# named parameters
24#
Ole Troan9a475372019-03-05 16:58:24 +010025defaultmapping = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020026 "acl_interface_add_del": {"is_add": 1, "is_input": 1},
27 "bd_ip_mac_add_del": {
28 "is_add": 1,
29 },
30 "bfd_udp_add": {"is_authenticated": False, "bfd_key_id": None, "conf_key_id": None},
31 "bfd_udp_auth_activate": {
32 "bfd_key_id": None,
33 "conf_key_id": None,
34 "is_delayed": False,
35 },
36 "bier_disp_entry_add_del": {
37 "next_hop_rpf_id": -1,
38 "next_hop_is_ip4": 1,
39 "is_add": 1,
40 },
41 "bier_disp_table_add_del": {
42 "is_add": 1,
43 },
44 "bier_imp_add": {
45 "is_add": 1,
46 },
47 "bier_route_add_del": {
48 "is_add": 1,
49 },
50 "bier_table_add_del": {
51 "is_add": 1,
52 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020053 "bvi_delete": {},
54 "geneve_add_del_tunnel": {
55 "mcast_sw_if_index": 4294967295,
56 "is_add": 1,
57 "decap_next_index": 4294967295,
58 },
59 "input_acl_set_interface": {
60 "ip4_table_index": 4294967295,
61 "ip6_table_index": 4294967295,
62 "l2_table_index": 4294967295,
63 },
64 "ip6_add_del_address_using_prefix": {
65 "is_add": 1,
66 },
67 "ip6nd_send_router_solicitation": {
68 "irt": 1,
69 "mrt": 120,
70 },
71 "ip_add_del_route": {
72 "next_hop_sw_if_index": 4294967295,
73 "next_hop_weight": 1,
74 "next_hop_via_label": 1048576,
75 "classify_table_index": 4294967295,
76 "is_add": 1,
77 },
78 "ip_mroute_add_del": {
79 "is_add": 1,
80 },
81 "ip_neighbor_add_del": {
82 "is_add": 1,
83 },
84 "ipsec_interface_add_del_spd": {
85 "is_add": 1,
86 },
87 "ipsec_spd_add_del": {
88 "is_add": 1,
89 },
90 "ipsec_spd_dump": {
91 "sa_id": 4294967295,
92 },
93 "ipsec_spd_entry_add_del": {
94 "local_port_stop": 65535,
95 "remote_port_stop": 65535,
96 "priority": 100,
97 "is_outbound": 1,
98 "is_add": 1,
99 },
100 "ipsec_tunnel_if_add_del": {
101 "is_add": 1,
102 "anti_replay": 1,
103 },
104 "l2_emulation": {
105 "enable": 1,
106 },
107 "l2fib_add_del": {
108 "is_add": 1,
109 },
110 "lisp_add_del_adjacency": {
111 "is_add": 1,
112 },
113 "lisp_add_del_local_eid": {
114 "is_add": 1,
115 },
116 "lisp_add_del_locator": {
117 "priority": 1,
118 "weight": 1,
119 "is_add": 1,
120 },
121 "lisp_add_del_locator_set": {
122 "is_add": 1,
123 },
124 "lisp_add_del_remote_mapping": {
125 "is_add": 1,
126 },
127 "macip_acl_interface_add_del": {
128 "is_add": 1,
129 },
130 "mpls_ip_bind_unbind": {
131 "is_ip4": 1,
132 "is_bind": 1,
133 },
134 "mpls_route_add_del": {
135 "mr_next_hop_sw_if_index": 4294967295,
136 "mr_next_hop_weight": 1,
137 "mr_next_hop_via_label": 1048576,
138 "mr_is_add": 1,
139 "mr_classify_table_index": 4294967295,
140 },
141 "mpls_table_add_del": {
142 "is_add": 1,
143 },
144 "mpls_tunnel_add_del": {
145 "next_hop_sw_if_index": 4294967295,
146 "next_hop_weight": 1,
147 "next_hop_via_label": 1048576,
148 "is_add": 1,
149 },
150 "output_acl_set_interface": {
151 "ip4_table_index": 4294967295,
152 "ip6_table_index": 4294967295,
153 "l2_table_index": 4294967295,
154 },
155 "pppoe_add_del_session": {
156 "is_add": 1,
157 },
158 "policer_add_del": {
159 "is_add": 1,
160 "conform_action": {"type": 1},
161 },
162 "set_ipfix_exporter": {
163 "collector_port": 4739,
164 },
165 "sr_policy_add": {
166 "weight": 1,
167 "is_encap": 1,
168 },
169 "sw_interface_add_del_address": {
170 "is_add": 1,
171 },
172 "sw_interface_ip6nd_ra_prefix": {
173 "val_lifetime": 4294967295,
174 "pref_lifetime": 4294967295,
175 },
176 "sw_interface_set_ip_directed_broadcast": {
177 "enable": 1,
178 },
179 "sw_interface_set_l2_bridge": {
180 "enable": 1,
181 },
182 "sw_interface_set_mpls_enable": {
183 "enable": 1,
184 },
185 "sw_interface_set_mtu": {
186 "mtu": [0, 0, 0, 0],
187 },
188 "sw_interface_set_unnumbered": {
189 "is_add": 1,
190 },
191 "sw_interface_span_enable_disable": {
192 "state": 1,
193 },
194 "vxlan_add_del_tunnel": {
195 "mcast_sw_if_index": 4294967295,
196 "is_add": 1,
197 "decap_next_index": 4294967295,
198 "instance": 4294967295,
199 },
200 "want_bfd_events": {
201 "enable_disable": 1,
202 },
203 "want_igmp_events": {
204 "enable": 1,
205 },
206 "want_interface_events": {
207 "enable_disable": 1,
208 },
209 "want_l2_macs_events": {
210 "enable_disable": 1,
211 "pid": os.getpid(),
212 },
213 "want_l2_macs_events2": {
214 "enable_disable": 1,
215 "pid": os.getpid(),
216 },
Ole Troan9a475372019-03-05 16:58:24 +0100217}
Ole Troanf159f582019-02-28 20:20:47 +0100218
Matus Fabianb4515b42018-11-19 04:25:32 -0800219
Paul Vinciguerra0b0a84c2020-05-02 10:40:33 -0400220def as_fn_signature(d):
221 return ", ".join(f"{k}={v}" for k, v in d.items())
222
223
Klement Sekeraf62ae122016-10-11 11:47:09 +0200224class VppPapiProvider(object):
225 """VPP-api provider using vpp-papi
226
227 @property hook: hook object providing before and after api/cli hooks
Klement Sekeraf62ae122016-10-11 11:47:09 +0200228 """
229
Klement Sekerae0545ef2017-01-25 08:00:40 +0100230 _zero, _negative = range(2)
231
Ole Troan4376ab22021-03-03 10:40:05 +0100232 def __init__(self, name, test_class, read_timeout):
Paul Vinciguerra895e2f82019-01-08 20:37:40 -0800233 self.hook = Hook(test_class)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200234 self.name = name
Klement Sekera7bb873a2016-11-18 07:38:42 +0100235 self.test_class = test_class
Klement Sekerae0545ef2017-01-25 08:00:40 +0100236 self._expect_api_retval = self._zero
237 self._expect_stack = []
Ole Troan7e3a8752016-12-05 10:27:09 +0100238
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200239 self.vpp = VPPApiClient(
Maxime Peim77caeb12023-11-14 15:26:41 +0100240 apidir=config.extern_apidir + [config.vpp_install_dir],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200241 logger=test_class.logger,
242 read_timeout=read_timeout,
243 use_socket=True,
244 server_address=test_class.get_api_sock_path(),
245 )
Ole Troan4376ab22021-03-03 10:40:05 +0100246 self._events = queue.Queue()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200247
Klement Sekerae0545ef2017-01-25 08:00:40 +0100248 def __enter__(self):
249 return self
250
Klement Sekera7d6afb32018-11-08 11:52:04 +0100251 def assert_negative_api_retval(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200252 """Expect API failure - used with with, e.g.::
Dave Wallaced1706812021-08-12 18:36:02 -0400253
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200254 with self.vapi.assert_negative_api_retval():
255 self.vapi.<api call expected to fail>
Dave Wallaced1706812021-08-12 18:36:02 -0400256
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200257 ..
Klement Sekera7d6afb32018-11-08 11:52:04 +0100258 """
Klement Sekerae0545ef2017-01-25 08:00:40 +0100259 self._expect_stack.append(self._expect_api_retval)
260 self._expect_api_retval = self._negative
261 return self
262
Klement Sekera7d6afb32018-11-08 11:52:04 +0100263 def assert_zero_api_retval(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200264 """Expect API success - used with with, e.g.::
Klement Sekera7d6afb32018-11-08 11:52:04 +0100265
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200266 with self.vapi.assert_negative_api_retval():
267 self.vapi.<api call expected to succeed>
Dave Wallaced1706812021-08-12 18:36:02 -0400268
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200269 :note: this is useful only inside another with block
270 as success is the default expected value
Klement Sekera7d6afb32018-11-08 11:52:04 +0100271 """
Klement Sekerae0545ef2017-01-25 08:00:40 +0100272 self._expect_stack.append(self._expect_api_retval)
273 self._expect_api_retval = self._zero
274 return self
275
276 def __exit__(self, exc_type, exc_value, traceback):
277 self._expect_api_retval = self._expect_stack.pop()
278
Klement Sekeraf62ae122016-10-11 11:47:09 +0200279 def register_hook(self, hook):
280 """Replace hook registration with new hook
281
282 :param hook:
283
284 """
285 self.hook = hook
286
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200287 def collect_events(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288 """Collect all events from the internal queue and clear the queue."""
Ole Troan4376ab22021-03-03 10:40:05 +0100289 result = []
290 while True:
291 try:
292 e = self._events.get(block=False)
293 result.append(e)
294 except queue.Empty:
295 return result
296 return result
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200297
298 def wait_for_event(self, timeout, name=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200299 """Wait for and return next event."""
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100300 if name:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200301 self.test_class.logger.debug(
302 "Expecting event '%s' within %ss", name, timeout
303 )
Eyal Bari20197482017-09-13 12:29:08 +0300304 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200305 self.test_class.logger.debug("Expecting event within %ss", timeout)
Ole Troan4376ab22021-03-03 10:40:05 +0100306 try:
307 e = self._events.get(timeout=timeout)
308 except queue.Empty:
309 raise Exception("Event did not occur within timeout")
310 msgname = type(e).__name__
311 if name and msgname != name:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200312 raise Exception("Unexpected event received: %s, expected: %s" % msgname)
Ole Troan4376ab22021-03-03 10:40:05 +0100313 self.test_class.logger.debug("Returning event %s:%s" % (name, e))
314 return e
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200315
316 def __call__(self, name, event):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200317 """Enqueue event in the internal event queue."""
Klement Sekerae4504c62016-12-08 10:16:41 +0100318 self.test_class.logger.debug("New event: %s: %s" % (name, event))
Ole Troan4376ab22021-03-03 10:40:05 +0100319 self._events.put(event)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200320
Ole Troanf159f582019-02-28 20:20:47 +0100321 def factory(self, name, apifn):
322 def f(*a, **ka):
323 fields = apifn._func.msg.fields
324
325 # add positional and kw arguments
326 d = ka
327 for i, o in enumerate(fields[3:]):
328 try:
329 d[o] = a[i]
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200330 except BaseException:
Ole Troanf159f582019-02-28 20:20:47 +0100331 break
332
333 # Default override
334 if name in defaultmapping:
335 for k, v in iteritems(defaultmapping[name]):
336 if k in d:
337 continue
338 d[k] = v
339 return self.api(apifn, d)
Ole Troan9a475372019-03-05 16:58:24 +0100340
Ole Troanf159f582019-02-28 20:20:47 +0100341 return f
342
Paul Vinciguerra7ab99702019-03-05 04:30:04 -0800343 def __getattribute__(self, name):
Ole Troanf159f582019-02-28 20:20:47 +0100344 try:
Paul Vinciguerra7ab99702019-03-05 04:30:04 -0800345 method = super(VppPapiProvider, self).__getattribute__(name)
346 except AttributeError:
347 method = self.factory(name, getattr(self.papi, name))
348 # lazily load the method so we don't need to call factory
349 # again for this name.
350 setattr(self, name, method)
351 return method
Ole Troanf159f582019-02-28 20:20:47 +0100352
Klement Sekeraf62ae122016-10-11 11:47:09 +0200353 def connect(self):
354 """Connect the API to VPP"""
Ole Troan4376ab22021-03-03 10:40:05 +0100355 # This might be called before VPP is prepared to listen to the socket
356 retries = 0
Klement Sekerae2636852021-03-16 12:52:12 +0100357 while not os.path.exists(self.test_class.get_api_sock_path()):
Ole Troan4376ab22021-03-03 10:40:05 +0100358 time.sleep(0.5)
359 retries += 1
360 if retries > 120:
361 break
362 self.vpp.connect(self.name[:63])
Klement Sekera7112c542017-03-01 09:53:19 +0100363 self.papi = self.vpp.api
364 self.vpp.register_event_callback(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200365
366 def disconnect(self):
367 """Disconnect the API from VPP"""
Klement Sekera7112c542017-03-01 09:53:19 +0100368 self.vpp.disconnect()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200369
370 def api(self, api_fn, api_args, expected_retval=0):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200371 """Call API function and check it's return value.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200372 Call the appropriate hooks before and after the API call
373
374 :param api_fn: API function to call
375 :param api_args: tuple of API function arguments
376 :param expected_retval: Expected return value (Default value = 0)
377 :returns: reply from the API
378
379 """
380 self.hook.before_api(api_fn.__name__, api_args)
Ole Troan7e3a8752016-12-05 10:27:09 +0100381 reply = api_fn(**api_args)
Klement Sekerae0545ef2017-01-25 08:00:40 +0100382 if self._expect_api_retval == self._negative:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200383 if hasattr(reply, "retval") and reply.retval >= 0:
384 msg = (
385 "%s(%s) passed unexpectedly: expected negative "
386 "return value instead of %d in %s"
387 % (
388 api_fn.__name__,
389 as_fn_signature(api_args),
390 reply.retval,
391 moves.reprlib.repr(reply),
392 )
393 )
Klement Sekerae0545ef2017-01-25 08:00:40 +0100394 self.test_class.logger.info(msg)
Klement Sekera77c821c2022-04-14 16:29:07 +0200395 raise UnexpectedApiReturnValueError(reply.retval, msg)
Klement Sekerae0545ef2017-01-25 08:00:40 +0100396 elif self._expect_api_retval == self._zero:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200397 if hasattr(reply, "retval") and reply.retval != expected_retval:
398 msg = (
399 "%s(%s) failed, expected %d return value instead "
400 "of %d in %s"
401 % (
402 api_fn.__name__,
403 as_fn_signature(api_args),
404 expected_retval,
405 reply.retval,
406 repr(reply),
407 )
408 )
Klement Sekerae0545ef2017-01-25 08:00:40 +0100409 self.test_class.logger.info(msg)
Klement Sekera77c821c2022-04-14 16:29:07 +0200410 raise UnexpectedApiReturnValueError(reply.retval, msg)
Klement Sekerae0545ef2017-01-25 08:00:40 +0100411 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200412 raise Exception(
413 "Internal error, unexpected value for "
414 "self._expect_api_retval %s" % self._expect_api_retval
415 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200416 self.hook.after_api(api_fn.__name__, api_args)
417 return reply
418
Dave Barach5932ce12019-10-09 17:28:04 -0400419 def cli_return_response(self, cli):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200420 """Execute a CLI, calling the before/after hooks appropriately.
Dave Barach5932ce12019-10-09 17:28:04 -0400421 Return the reply without examining it
422
423 :param cli: CLI to execute
424 :returns: response object
425
426 """
427 self.hook.before_cli(cli)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200428 cli += "\n"
Dave Barach5932ce12019-10-09 17:28:04 -0400429 r = self.papi.cli_inband(cmd=cli)
430 self.hook.after_cli(cli)
431 return r
432
Klement Sekeraf62ae122016-10-11 11:47:09 +0200433 def cli(self, cli):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200434 """Execute a CLI, calling the before/after hooks appropriately.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200435
436 :param cli: CLI to execute
437 :returns: CLI output
438
439 """
Dave Barach5932ce12019-10-09 17:28:04 -0400440 r = self.cli_return_response(cli)
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400441 if r.retval == -156:
442 raise CliSyntaxError(r.reply)
443 if r.retval != 0:
444 raise CliFailedCommandError(r.reply)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200445 if hasattr(r, "reply"):
Ole Troan413f4a52018-11-28 11:36:05 +0100446 return r.reply
Klement Sekeraf62ae122016-10-11 11:47:09 +0200447
Jan49c0fca2016-10-26 15:44:27 +0200448 def ppcli(self, cli):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200449 """Helper method to print CLI command in case of info logging level.
Jan49c0fca2016-10-26 15:44:27 +0200450
451 :param cli: CLI to execute
452 :returns: CLI output
453 """
Ole Troan6ed154f2019-10-15 19:31:55 +0200454 return cli + "\n" + self.cli(cli)
Jan49c0fca2016-10-26 15:44:27 +0200455
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200456 def ip6nd_send_router_solicitation(self, sw_if_index, irt=1, mrt=120, mrc=0, mrd=0):
457 return self.api(
458 self.papi.ip6nd_send_router_solicitation,
459 {
460 "irt": irt,
461 "mrt": mrt,
462 "mrc": mrc,
463 "mrd": mrd,
464 "sw_if_index": sw_if_index,
465 },
466 )
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100467
Juraj Slobodab3f90502018-10-04 15:15:16 +0200468 def want_interface_events(self, enable_disable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200469 return self.api(
470 self.papi.want_interface_events,
471 {
472 "enable_disable": enable_disable,
473 "pid": os.getpid(),
474 },
475 )
Juraj Slobodab3f90502018-10-04 15:15:16 +0200476
Neale Rannsc819fc62018-02-16 02:44:05 -0800477 def sw_interface_set_mac_address(self, sw_if_index, mac):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200478 return self.api(
479 self.papi.sw_interface_set_mac_address,
480 {"sw_if_index": sw_if_index, "mac_address": mac},
481 )
Neale Rannsc819fc62018-02-16 02:44:05 -0800482
Ole Troane1ade682019-03-04 23:55:43 +0100483 def p2p_ethernet_add(self, sw_if_index, remote_mac, subif_id):
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200484 """Create p2p ethernet subinterface
485
486 :param sw_if_index: main (parent) interface
487 :param remote_mac: client (remote) mac address
488
489 """
490 return self.api(
491 self.papi.p2p_ethernet_add,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200492 {
493 "parent_if_index": sw_if_index,
494 "remote_mac": remote_mac,
495 "subif_id": subif_id,
496 },
497 )
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200498
Ole Troane1ade682019-03-04 23:55:43 +0100499 def p2p_ethernet_del(self, sw_if_index, remote_mac):
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200500 """Delete p2p ethernet subinterface
501
502 :param sw_if_index: main (parent) interface
503 :param remote_mac: client (remote) mac address
504
505 """
506 return self.api(
507 self.papi.p2p_ethernet_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200508 {"parent_if_index": sw_if_index, "remote_mac": remote_mac},
509 )
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200510
Klement Sekeraf62ae122016-10-11 11:47:09 +0200511 def create_vlan_subif(self, sw_if_index, vlan):
512 """
513
514 :param vlan:
515 :param sw_if_index:
516
517 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200518 return self.api(
519 self.papi.create_vlan_subif, {"sw_if_index": sw_if_index, "vlan_id": vlan}
520 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200521
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200522 def create_loopback(self, mac=""):
Matej Klotton0178d522016-11-04 11:11:44 +0100523 """
524
525 :param mac: (Optional)
526 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200527 return self.api(self.papi.create_loopback, {"mac_address": mac})
Matej Klotton0178d522016-11-04 11:11:44 +0100528
Neale Ranns097fa662018-05-01 05:17:55 -0700529 def ip_route_dump(self, table_id, is_ip6=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200530 return self.api(
531 self.papi.ip_route_dump, {"table": {"table_id": table_id, "is_ip6": is_ip6}}
532 )
Neale Ranns097fa662018-05-01 05:17:55 -0700533
Neale Ranns976b2592019-12-04 06:11:00 +0000534 def ip_route_v2_dump(self, table_id, is_ip6=False, src=0):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200535 return self.api(
536 self.papi.ip_route_v2_dump,
537 {"src": src, "table": {"table_id": table_id, "is_ip6": is_ip6}},
538 )
Neale Ranns976b2592019-12-04 06:11:00 +0000539
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200540 def ip_neighbor_add_del(
541 self, sw_if_index, mac_address, ip_address, is_add=1, flags=0
542 ):
543 """Add neighbor MAC to IPv4 or IPv6 address.
Matej Klotton0178d522016-11-04 11:11:44 +0100544
545 :param sw_if_index:
546 :param mac_address:
547 :param dst_address:
Matej Klotton0178d522016-11-04 11:11:44 +0100548 :param is_add: (Default value = 1)
Neale Ranns37029302018-08-10 05:30:06 -0700549 :param flags: (Default value = 0/NONE)
Matej Klotton0178d522016-11-04 11:11:44 +0100550 """
Matej Klotton0178d522016-11-04 11:11:44 +0100551 return self.api(
Ole Troan7e3a8752016-12-05 10:27:09 +0100552 self.papi.ip_neighbor_add_del,
Neale Ranns37029302018-08-10 05:30:06 -0700553 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200554 "is_add": is_add,
555 "neighbor": {
556 "sw_if_index": sw_if_index,
557 "flags": flags,
558 "mac_address": mac_address,
559 "ip_address": ip_address,
560 },
561 },
Matej Klotton0178d522016-11-04 11:11:44 +0100562 )
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100563
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200564 def udp_encap_add(self, src_ip, dst_ip, src_port, dst_port, table_id=0):
565 """Add a GRE tunnel
Neale Ranns810086d2017-11-05 16:26:46 -0800566 :param src_ip:
567 :param dst_ip:
568 :param src_port:
569 :param dst_port:
570 :param outer_fib_id: (Default value = 0)
Neale Ranns810086d2017-11-05 16:26:46 -0800571 """
572
573 return self.api(
Neale Rannsd0df49f2018-08-08 01:06:40 -0700574 self.papi.udp_encap_add,
575 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200576 "udp_encap": {
577 "src_ip": src_ip,
578 "dst_ip": dst_ip,
579 "src_port": src_port,
580 "dst_port": dst_port,
581 "table_id": table_id,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700582 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200583 },
584 )
Neale Rannsd0df49f2018-08-08 01:06:40 -0700585
Neale Ranns097fa662018-05-01 05:17:55 -0700586 def udp_encap_del(self, id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200587 return self.api(self.papi.udp_encap_del, {"id": id})
Neale Ranns097fa662018-05-01 05:17:55 -0700588
589 def udp_encap_dump(self):
590 return self.api(self.papi.udp_encap_dump, {})
591
592 def want_udp_encap_stats(self, enable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200593 return self.api(
594 self.papi.want_udp_encap_stats, {"enable": enable, "pid": os.getpid()}
595 )
Neale Ranns097fa662018-05-01 05:17:55 -0700596
597 def mpls_route_dump(self, table_id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200598 return self.api(self.papi.mpls_route_dump, {"table": {"mt_table_id": table_id}})
Neale Ranns097fa662018-05-01 05:17:55 -0700599
600 def mpls_table_dump(self):
601 return self.api(self.papi.mpls_table_dump, {})
602
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200603 def mpls_table_add_del(self, table_id, is_add=1):
Neale Ranns15002542017-09-10 04:39:11 -0700604 """
605
606 :param table_id
607 :param is_add: (Default value = 1)
608
609 """
610
611 return self.api(
612 self.papi.mpls_table_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200613 {
614 "mt_table": {
615 "mt_table_id": table_id,
616 },
617 "mt_is_add": is_add,
618 },
619 )
Neale Ranns15002542017-09-10 04:39:11 -0700620
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200621 def mpls_route_add_del(
622 self, table_id, label, eos, eos_proto, is_multicast, paths, is_add, is_multipath
623 ):
624 """MPLS Route add/del"""
Neale Ranns097fa662018-05-01 05:17:55 -0700625 return self.api(
626 self.papi.mpls_route_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200627 {
628 "mr_route": {
629 "mr_table_id": table_id,
630 "mr_label": label,
631 "mr_eos": eos,
632 "mr_eos_proto": eos_proto,
633 "mr_is_multicast": is_multicast,
634 "mr_n_paths": len(paths),
635 "mr_paths": paths,
636 },
637 "mr_is_add": is_add,
638 "mr_is_multipath": is_multipath,
639 },
640 )
Neale Ranns097fa662018-05-01 05:17:55 -0700641
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200642 def mpls_ip_bind_unbind(self, label, prefix, table_id=0, ip_table_id=0, is_bind=1):
643 """ """
Neale Rannsad422ed2016-11-02 14:20:04 +0000644 return self.api(
Ole Troan7e3a8752016-12-05 10:27:09 +0100645 self.papi.mpls_ip_bind_unbind,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200646 {
647 "mb_mpls_table_id": table_id,
648 "mb_label": label,
649 "mb_ip_table_id": ip_table_id,
650 "mb_is_bind": is_bind,
651 "mb_prefix": prefix,
652 },
653 )
Neale Rannsad422ed2016-11-02 14:20:04 +0000654
655 def mpls_tunnel_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200656 self, tun_sw_if_index, paths, is_add=1, l2_only=0, is_multicast=0
657 ):
658 """ """
Neale Rannsad422ed2016-11-02 14:20:04 +0000659 return self.api(
Ole Troan7e3a8752016-12-05 10:27:09 +0100660 self.papi.mpls_tunnel_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200661 {
662 "mt_is_add": is_add,
663 "mt_tunnel": {
664 "mt_sw_if_index": tun_sw_if_index,
665 "mt_l2_only": l2_only,
666 "mt_is_multicast": is_multicast,
667 "mt_n_paths": len(paths),
668 "mt_paths": paths,
669 },
670 },
671 )
Matus Fabiande886752016-12-07 03:38:19 -0800672
Steve Shin7957d6e2016-12-19 09:24:50 -0800673 def input_acl_set_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200674 self,
675 is_add,
676 sw_if_index,
677 ip4_table_index=0xFFFFFFFF,
678 ip6_table_index=0xFFFFFFFF,
679 l2_table_index=0xFFFFFFFF,
680 ):
Steve Shin7957d6e2016-12-19 09:24:50 -0800681 """
682 :param is_add:
683 :param sw_if_index:
684 :param ip4_table_index: (Default value = 0xFFFFFFFF)
685 :param ip6_table_index: (Default value = 0xFFFFFFFF)
686 :param l2_table_index: (Default value = 0xFFFFFFFF)
687 """
688
689 return self.api(
690 self.papi.input_acl_set_interface,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200691 {
692 "sw_if_index": sw_if_index,
693 "ip4_table_index": ip4_table_index,
694 "ip6_table_index": ip6_table_index,
695 "l2_table_index": l2_table_index,
696 "is_add": is_add,
697 },
698 )
Klement Sekera77fabdb2017-01-02 07:46:14 +0100699
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100700 def output_acl_set_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200701 self,
702 is_add,
703 sw_if_index,
704 ip4_table_index=0xFFFFFFFF,
705 ip6_table_index=0xFFFFFFFF,
706 l2_table_index=0xFFFFFFFF,
707 ):
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100708 """
709 :param is_add:
710 :param sw_if_index:
711 :param ip4_table_index: (Default value = 0xFFFFFFFF)
712 :param ip6_table_index: (Default value = 0xFFFFFFFF)
713 :param l2_table_index: (Default value = 0xFFFFFFFF)
714 """
715
716 return self.api(
717 self.papi.output_acl_set_interface,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200718 {
719 "sw_if_index": sw_if_index,
720 "ip4_table_index": ip4_table_index,
721 "ip6_table_index": ip6_table_index,
722 "l2_table_index": l2_table_index,
723 "is_add": is_add,
724 },
725 )
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100726
Klement Sekera77fabdb2017-01-02 07:46:14 +0100727 def set_ipfix_exporter(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200728 self,
729 collector_address,
730 src_address,
731 path_mtu,
732 template_interval,
733 vrf_id=0,
734 collector_port=4739,
735 udp_checksum=0,
736 ):
Klement Sekera77fabdb2017-01-02 07:46:14 +0100737 return self.api(
738 self.papi.set_ipfix_exporter,
739 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200740 "collector_address": collector_address,
741 "collector_port": collector_port,
742 "src_address": src_address,
743 "vrf_id": vrf_id,
744 "path_mtu": path_mtu,
745 "template_interval": template_interval,
746 "udp_checksum": udp_checksum,
747 },
748 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800749
Neale Ranns097fa662018-05-01 05:17:55 -0700750 def mfib_signal_dump(self):
751 return self.api(self.papi.mfib_signal_dump, {})
752
753 def ip_mroute_dump(self, table_id, is_ip6=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200754 return self.api(
755 self.papi.ip_mroute_dump,
756 {"table": {"table_id": table_id, "is_ip6": is_ip6}},
757 )
Neale Ranns32e1c012016-11-22 17:07:28 +0000758
Hongjun Ni62f9cdd2017-07-04 20:11:57 +0800759 def pppoe_add_del_session(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200760 self, client_ip, client_mac, session_id=0, is_add=1, decap_vrf_id=0
761 ):
Hongjun Ni62f9cdd2017-07-04 20:11:57 +0800762 """
763
764 :param is_add: (Default value = 1)
765 :param is_ipv6: (Default value = 0)
766 :param client_ip:
767 :param session_id: (Default value = 0)
768 :param client_mac:
769 :param decap_vrf_id: (Default value = 0)
770
771 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200772 return self.api(
773 self.papi.pppoe_add_del_session,
774 {
775 "is_add": is_add,
776 "session_id": session_id,
777 "client_ip": client_ip,
778 "decap_vrf_id": decap_vrf_id,
779 "client_mac": client_mac,
780 },
781 )
Kris Michielsen91074432017-06-22 13:00:20 +0200782
Neale Ranns7c922dc2018-08-30 06:12:27 -0700783 def sr_mpls_policy_add(self, bsid, weight, type, segments):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200784 return self.api(
785 self.papi.sr_mpls_policy_add,
786 {
787 "bsid": bsid,
788 "weight": weight,
789 "is_spray": type,
790 "n_segments": len(segments),
791 "segments": segments,
792 },
793 )
Neale Ranns7c922dc2018-08-30 06:12:27 -0700794
795 def sr_mpls_policy_del(self, bsid):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200796 return self.api(self.papi.sr_mpls_policy_del, {"bsid": bsid})
Neale Ranns7c922dc2018-08-30 06:12:27 -0700797
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200798 def bier_table_add_del(self, bti, mpls_label, is_add=1):
799 """BIER Table add/del"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700800 return self.api(
801 self.papi.bier_table_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200802 {
803 "bt_tbl_id": {
804 "bt_set": bti.set_id,
805 "bt_sub_domain": bti.sub_domain_id,
806 "bt_hdr_len_id": bti.hdr_len_id,
807 },
808 "bt_label": mpls_label,
809 "bt_is_add": is_add,
810 },
811 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700812
813 def bier_table_dump(self):
814 return self.api(self.papi.bier_table_dump, {})
815
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200816 def bier_route_add_del(self, bti, bp, paths, is_add=1, is_replace=0):
817 """BIER Route add/del"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700818 return self.api(
819 self.papi.bier_route_add_del,
Neale Ranns097fa662018-05-01 05:17:55 -0700820 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200821 "br_route": {
822 "br_tbl_id": {
823 "bt_set": bti.set_id,
824 "bt_sub_domain": bti.sub_domain_id,
825 "bt_hdr_len_id": bti.hdr_len_id,
826 },
827 "br_bp": bp,
828 "br_n_paths": len(paths),
829 "br_paths": paths,
Neale Ranns097fa662018-05-01 05:17:55 -0700830 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200831 "br_is_add": is_add,
832 "br_is_replace": is_replace,
833 },
834 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700835
836 def bier_route_dump(self, bti):
837 return self.api(
838 self.papi.bier_route_dump,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200839 {
840 "br_tbl_id": {
841 "bt_set": bti.set_id,
842 "bt_sub_domain": bti.sub_domain_id,
843 "bt_hdr_len_id": bti.hdr_len_id,
844 }
845 },
846 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700847
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200848 def bier_imp_add(self, bti, src, ibytes, is_add=1):
849 """BIER Imposition Add"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700850 return self.api(
851 self.papi.bier_imp_add,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200852 {
853 "bi_tbl_id": {
854 "bt_set": bti.set_id,
855 "bt_sub_domain": bti.sub_domain_id,
856 "bt_hdr_len_id": bti.hdr_len_id,
857 },
858 "bi_src": src,
859 "bi_n_bytes": len(ibytes),
860 "bi_bytes": ibytes,
861 },
862 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700863
864 def bier_imp_del(self, bi_index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200865 """BIER Imposition del"""
866 return self.api(self.papi.bier_imp_del, {"bi_index": bi_index})
Neale Rannsd792d9c2017-10-21 10:53:20 -0700867
868 def bier_imp_dump(self):
869 return self.api(self.papi.bier_imp_dump, {})
870
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200871 def bier_disp_table_add_del(self, bdti, is_add=1):
872 """BIER Disposition Table add/del"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700873 return self.api(
874 self.papi.bier_disp_table_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200875 {"bdt_tbl_id": bdti, "bdt_is_add": is_add},
876 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700877
878 def bier_disp_table_dump(self):
879 return self.api(self.papi.bier_disp_table_dump, {})
880
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200881 def bier_disp_entry_add_del(
882 self,
883 bdti,
884 bp,
885 payload_proto,
886 next_hop_afi,
887 next_hop,
888 next_hop_tbl_id=0,
889 next_hop_rpf_id=~0,
890 next_hop_is_ip4=1,
891 is_add=1,
892 ):
893 """BIER Route add/del"""
Neale Ranns31ed7442018-02-23 05:29:09 -0800894 lstack = []
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200895 while len(lstack) < 16:
Neale Ranns31ed7442018-02-23 05:29:09 -0800896 lstack.append({})
Neale Rannsd792d9c2017-10-21 10:53:20 -0700897 return self.api(
898 self.papi.bier_disp_entry_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200899 {
900 "bde_tbl_id": bdti,
901 "bde_bp": bp,
902 "bde_payload_proto": payload_proto,
903 "bde_n_paths": 1,
904 "bde_paths": [
905 {
906 "table_id": next_hop_tbl_id,
907 "rpf_id": next_hop_rpf_id,
908 "n_labels": 0,
909 "label_stack": lstack,
910 }
911 ],
912 "bde_is_add": is_add,
913 },
914 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700915
916 def bier_disp_entry_dump(self, bdti):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200917 return self.api(self.papi.bier_disp_entry_dump, {"bde_tbl_id": bdti})
Gabriel Ganne8527f122017-10-02 11:41:24 +0200918
“mystarrocks”23f0c452017-12-11 07:11:51 -0800919 def ipsec_spd_add_del(self, spd_id, is_add=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200920 """SPD add/del - Wrapper to add or del ipsec SPD
“mystarrocks”23f0c452017-12-11 07:11:51 -0800921 Sample CLI : 'ipsec spd add 1'
922
923 :param spd_id - SPD ID to be created in the vpp . mandatory
924 :param is_add - create (1) or delete(0) SPD (Default 1 - add) .
Dave Wallaced1706812021-08-12 18:36:02 -0400925 optional
“mystarrocks”23f0c452017-12-11 07:11:51 -0800926 :returns: reply from the API
927 """
928 return self.api(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200929 self.papi.ipsec_spd_add_del, {"spd_id": spd_id, "is_add": is_add}
930 )
“mystarrocks”23f0c452017-12-11 07:11:51 -0800931
Neale Ranns311124e2019-01-24 04:52:25 -0800932 def ipsec_spds_dump(self):
933 return self.api(self.papi.ipsec_spds_dump, {})
934
“mystarrocks”23f0c452017-12-11 07:11:51 -0800935 def ipsec_interface_add_del_spd(self, spd_id, sw_if_index, is_add=1):
936 """ IPSEC interface SPD add/del - \
937 Wrapper to associate/disassociate SPD to interface in VPP
938 Sample CLI : 'set interface ipsec spd GigabitEthernet0/6/0 1'
939
940 :param spd_id - SPD ID to associate with the interface . mandatory
941 :param sw_if_index - Interface Index which needs to ipsec \
942 association mandatory
943 :param is_add - add(1) or del(0) association with interface \
944 (Default 1 - add) . optional
945 :returns: reply from the API
946 """
947 return self.api(
Klement Sekera4b089f22018-04-17 18:04:57 +0200948 self.papi.ipsec_interface_add_del_spd,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200949 {"spd_id": spd_id, "sw_if_index": sw_if_index, "is_add": is_add},
950 )
“mystarrocks”23f0c452017-12-11 07:11:51 -0800951
Neale Ranns311124e2019-01-24 04:52:25 -0800952 def ipsec_spd_interface_dump(self, spd_index=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200953 return self.api(
954 self.papi.ipsec_spd_interface_dump,
955 {
956 "spd_index": spd_index if spd_index else 0,
957 "spd_index_valid": 1 if spd_index else 0,
958 },
959 )
Neale Ranns311124e2019-01-24 04:52:25 -0800960
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200961 def ipsec_spd_entry_add_del(
962 self,
963 spd_id,
964 sa_id,
965 local_address_start,
966 local_address_stop,
967 remote_address_start,
968 remote_address_stop,
969 local_port_start=0,
970 local_port_stop=65535,
971 remote_port_start=0,
972 remote_port_stop=65535,
Piotr Bronowski815c6a42022-06-09 09:09:28 +0000973 protocol=socket.IPPROTO_RAW,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200974 policy=0,
975 priority=100,
976 is_outbound=1,
977 is_add=1,
978 is_ipv6=0,
979 is_ip_any=0,
980 ):
981 """IPSEC policy SPD add/del -
“mystarrocks”23f0c452017-12-11 07:11:51 -0800982 Wrapper to configure ipsec SPD policy entries in VPP
Dave Wallaced1706812021-08-12 18:36:02 -0400983
Klement Sekera31da2e32018-06-24 22:49:55 +0200984 :param spd_id: SPD ID for the policy
985 :param local_address_start: local-ip-range start address
Dave Wallaced1706812021-08-12 18:36:02 -0400986 :param local_address_stop: local-ip-range stop address
Klement Sekera31da2e32018-06-24 22:49:55 +0200987 :param remote_address_start: remote-ip-range start address
Dave Wallaced1706812021-08-12 18:36:02 -0400988 :param remote_address_stop: remote-ip-range stop address
Klement Sekera31da2e32018-06-24 22:49:55 +0200989 :param local_port_start: (Default value = 0)
990 :param local_port_stop: (Default value = 65535)
991 :param remote_port_start: (Default value = 0)
992 :param remote_port_stop: (Default value = 65535)
993 :param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0)
994 :param sa_id: Security Association ID for mapping it to SPD
995 :param policy: bypass(0), discard(1), resolve(2) or protect(3) action
Dave Wallaced1706812021-08-12 18:36:02 -0400996 (Default value = 0)
Klement Sekera31da2e32018-06-24 22:49:55 +0200997 :param priority: value for the spd action (Default value = 100)
998 :param is_outbound: flag for inbound(0) or outbound(1)
Dave Wallaced1706812021-08-12 18:36:02 -0400999 (Default value = 1)
Klement Sekera31da2e32018-06-24 22:49:55 +02001000 :param is_add: (Default value = 1)
“mystarrocks”23f0c452017-12-11 07:11:51 -08001001 """
1002 return self.api(
Piotr Bronowski815c6a42022-06-09 09:09:28 +00001003 self.papi.ipsec_spd_entry_add_del_v2,
Neale Ranns17dcec02019-01-09 21:22:20 -08001004 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001005 "is_add": is_add,
1006 "entry": {
1007 "spd_id": spd_id,
1008 "sa_id": sa_id,
1009 "local_address_start": local_address_start,
1010 "local_address_stop": local_address_stop,
1011 "remote_address_start": remote_address_start,
1012 "remote_address_stop": remote_address_stop,
1013 "local_port_start": local_port_start,
1014 "local_port_stop": local_port_stop,
1015 "remote_port_start": remote_port_start,
1016 "remote_port_stop": remote_port_stop,
1017 "protocol": protocol,
1018 "policy": policy,
1019 "priority": priority,
1020 "is_outbound": is_outbound,
1021 },
1022 },
1023 )
Florin Corasb795bd02017-12-14 11:30:48 -08001024
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001025 def ipsec_spd_dump(self, spd_id, sa_id=0xFFFFFFFF):
1026 return self.api(self.papi.ipsec_spd_dump, {"spd_id": spd_id, "sa_id": sa_id})
Neale Ranns311124e2019-01-24 04:52:25 -08001027
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001028 def ipsec_tunnel_if_add_del(
1029 self,
1030 local_ip,
1031 remote_ip,
1032 local_spi,
1033 remote_spi,
1034 crypto_alg,
1035 local_crypto_key,
1036 remote_crypto_key,
1037 integ_alg,
1038 local_integ_key,
1039 remote_integ_key,
1040 is_add=1,
1041 esn=0,
1042 salt=0,
1043 anti_replay=1,
1044 renumber=0,
1045 udp_encap=0,
1046 show_instance=0xFFFFFFFF,
1047 ):
Klement Sekera31da2e32018-06-24 22:49:55 +02001048 return self.api(
1049 self.papi.ipsec_tunnel_if_add_del,
Kingwel Xie1ba5bc82019-03-20 07:21:58 -04001050 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001051 "local_ip": local_ip,
1052 "remote_ip": remote_ip,
1053 "local_spi": local_spi,
1054 "remote_spi": remote_spi,
1055 "crypto_alg": crypto_alg,
1056 "local_crypto_key_len": len(local_crypto_key),
1057 "local_crypto_key": local_crypto_key,
1058 "remote_crypto_key_len": len(remote_crypto_key),
1059 "remote_crypto_key": remote_crypto_key,
1060 "integ_alg": integ_alg,
1061 "local_integ_key_len": len(local_integ_key),
1062 "local_integ_key": local_integ_key,
1063 "remote_integ_key_len": len(remote_integ_key),
1064 "remote_integ_key": remote_integ_key,
1065 "is_add": is_add,
1066 "esn": esn,
1067 "anti_replay": anti_replay,
1068 "renumber": renumber,
1069 "show_instance": show_instance,
1070 "udp_encap": udp_encap,
1071 "salt": salt,
1072 },
1073 )
Klement Sekera31da2e32018-06-24 22:49:55 +02001074
Klement Sekerab4d30532018-11-08 13:00:02 +01001075 def ipsec_select_backend(self, protocol, index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001076 return self.api(
1077 self.papi.ipsec_select_backend, {"protocol": protocol, "index": index}
1078 )
Klement Sekerab4d30532018-11-08 13:00:02 +01001079
1080 def ipsec_backend_dump(self):
1081 return self.api(self.papi.ipsec_backend_dump, {})
1082
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001083 def punt_socket_register(self, reg, pathname, header_version=1):
1084 """Register punt socket"""
1085 return self.api(
1086 self.papi.punt_socket_register,
1087 {"header_version": header_version, "punt": reg, "pathname": pathname},
1088 )
Pavel Kotuceke88865d2018-11-28 07:42:11 +01001089
Neale Ranns50f0ac02019-05-15 02:13:37 -07001090 def punt_socket_deregister(self, reg):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001091 """Unregister punt socket"""
1092 return self.api(self.papi.punt_socket_deregister, {"punt": reg})
Klement Sekera75e7d132017-09-20 08:26:30 +02001093
Neale Ranns947ea622018-06-07 23:48:20 -07001094 def igmp_enable_disable(self, sw_if_index, enable, host):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001095 """Enable/disable IGMP on a given interface"""
1096 return self.api(
1097 self.papi.igmp_enable_disable,
1098 {"enable": enable, "mode": host, "sw_if_index": sw_if_index},
1099 )
Neale Ranns947ea622018-06-07 23:48:20 -07001100
Jakub Grajciar97748ca2018-10-04 11:05:35 +02001101 def igmp_proxy_device_add_del(self, vrf_id, sw_if_index, add):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001102 """Add/del IGMP proxy device"""
1103 return self.api(
1104 self.papi.igmp_proxy_device_add_del,
1105 {"vrf_id": vrf_id, "sw_if_index": sw_if_index, "add": add},
1106 )
Jakub Grajciar97748ca2018-10-04 11:05:35 +02001107
1108 def igmp_proxy_device_add_del_interface(self, vrf_id, sw_if_index, add):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001109 """Add/del interface to/from IGMP proxy device"""
1110 return self.api(
1111 self.papi.igmp_proxy_device_add_del_interface,
1112 {"vrf_id": vrf_id, "sw_if_index": sw_if_index, "add": add},
1113 )
Jakub Grajciar97748ca2018-10-04 11:05:35 +02001114
Neale Ranns947ea622018-06-07 23:48:20 -07001115 def igmp_listen(self, filter, sw_if_index, saddrs, gaddr):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001116 """Listen for new (S,G) on specified interface
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001117
Dave Wallaced1706812021-08-12 18:36:02 -04001118 :param enable: add/delas
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001119 :param sw_if_index: interface sw index
1120 :param saddr: source ip4 addr
1121 :param gaddr: group ip4 addr
1122 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001123 return self.api(
1124 self.papi.igmp_listen,
1125 {
1126 "group": {
1127 "filter": filter,
1128 "sw_if_index": sw_if_index,
1129 "n_srcs": len(saddrs),
1130 "saddrs": saddrs,
1131 "gaddr": gaddr,
1132 }
1133 },
1134 )
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001135
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001136 def igmp_clear_interface(self, sw_if_index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001137 """Remove all (S,G)s from specified interface
1138 doesn't send IGMP report!
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001139 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001140 return self.api(self.papi.igmp_clear_interface, {"sw_if_index": sw_if_index})
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001141
1142 def want_igmp_events(self, enable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001143 return self.api(
1144 self.papi.want_igmp_events, {"enable": enable, "pid": os.getpid()}
1145 )