blob: 148eca2053bebf729468f91cee9c9e9f291844b0 [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
Paul Vinciguerra19542292019-03-17 17:34:46 -0700239 # install_dir is a class attribute. We need to set it before
240 # calling the constructor.
Klement Sekerab23ffd72021-05-31 16:08:53 +0200241 VPPApiClient.apidir = config.vpp_install_dir
Paul Vinciguerra04575c52019-01-13 10:26:05 -0800242
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200243 self.vpp = VPPApiClient(
244 logger=test_class.logger,
245 read_timeout=read_timeout,
246 use_socket=True,
247 server_address=test_class.get_api_sock_path(),
248 )
Ole Troan4376ab22021-03-03 10:40:05 +0100249 self._events = queue.Queue()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200250
Klement Sekerae0545ef2017-01-25 08:00:40 +0100251 def __enter__(self):
252 return self
253
Klement Sekera7d6afb32018-11-08 11:52:04 +0100254 def assert_negative_api_retval(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200255 """Expect API failure - used with with, e.g.::
Dave Wallaced1706812021-08-12 18:36:02 -0400256
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200257 with self.vapi.assert_negative_api_retval():
258 self.vapi.<api call expected to fail>
Dave Wallaced1706812021-08-12 18:36:02 -0400259
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200260 ..
Klement Sekera7d6afb32018-11-08 11:52:04 +0100261 """
Klement Sekerae0545ef2017-01-25 08:00:40 +0100262 self._expect_stack.append(self._expect_api_retval)
263 self._expect_api_retval = self._negative
264 return self
265
Klement Sekera7d6afb32018-11-08 11:52:04 +0100266 def assert_zero_api_retval(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200267 """Expect API success - used with with, e.g.::
Klement Sekera7d6afb32018-11-08 11:52:04 +0100268
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200269 with self.vapi.assert_negative_api_retval():
270 self.vapi.<api call expected to succeed>
Dave Wallaced1706812021-08-12 18:36:02 -0400271
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200272 :note: this is useful only inside another with block
273 as success is the default expected value
Klement Sekera7d6afb32018-11-08 11:52:04 +0100274 """
Klement Sekerae0545ef2017-01-25 08:00:40 +0100275 self._expect_stack.append(self._expect_api_retval)
276 self._expect_api_retval = self._zero
277 return self
278
279 def __exit__(self, exc_type, exc_value, traceback):
280 self._expect_api_retval = self._expect_stack.pop()
281
Klement Sekeraf62ae122016-10-11 11:47:09 +0200282 def register_hook(self, hook):
283 """Replace hook registration with new hook
284
285 :param hook:
286
287 """
288 self.hook = hook
289
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200290 def collect_events(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200291 """Collect all events from the internal queue and clear the queue."""
Ole Troan4376ab22021-03-03 10:40:05 +0100292 result = []
293 while True:
294 try:
295 e = self._events.get(block=False)
296 result.append(e)
297 except queue.Empty:
298 return result
299 return result
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200300
301 def wait_for_event(self, timeout, name=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200302 """Wait for and return next event."""
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100303 if name:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200304 self.test_class.logger.debug(
305 "Expecting event '%s' within %ss", name, timeout
306 )
Eyal Bari20197482017-09-13 12:29:08 +0300307 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200308 self.test_class.logger.debug("Expecting event within %ss", timeout)
Ole Troan4376ab22021-03-03 10:40:05 +0100309 try:
310 e = self._events.get(timeout=timeout)
311 except queue.Empty:
312 raise Exception("Event did not occur within timeout")
313 msgname = type(e).__name__
314 if name and msgname != name:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200315 raise Exception("Unexpected event received: %s, expected: %s" % msgname)
Ole Troan4376ab22021-03-03 10:40:05 +0100316 self.test_class.logger.debug("Returning event %s:%s" % (name, e))
317 return e
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200318
319 def __call__(self, name, event):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200320 """Enqueue event in the internal event queue."""
Klement Sekerae4504c62016-12-08 10:16:41 +0100321 self.test_class.logger.debug("New event: %s: %s" % (name, event))
Ole Troan4376ab22021-03-03 10:40:05 +0100322 self._events.put(event)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200323
Ole Troanf159f582019-02-28 20:20:47 +0100324 def factory(self, name, apifn):
325 def f(*a, **ka):
326 fields = apifn._func.msg.fields
327
328 # add positional and kw arguments
329 d = ka
330 for i, o in enumerate(fields[3:]):
331 try:
332 d[o] = a[i]
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200333 except BaseException:
Ole Troanf159f582019-02-28 20:20:47 +0100334 break
335
336 # Default override
337 if name in defaultmapping:
338 for k, v in iteritems(defaultmapping[name]):
339 if k in d:
340 continue
341 d[k] = v
342 return self.api(apifn, d)
Ole Troan9a475372019-03-05 16:58:24 +0100343
Ole Troanf159f582019-02-28 20:20:47 +0100344 return f
345
Paul Vinciguerra7ab99702019-03-05 04:30:04 -0800346 def __getattribute__(self, name):
Ole Troanf159f582019-02-28 20:20:47 +0100347 try:
Paul Vinciguerra7ab99702019-03-05 04:30:04 -0800348 method = super(VppPapiProvider, self).__getattribute__(name)
349 except AttributeError:
350 method = self.factory(name, getattr(self.papi, name))
351 # lazily load the method so we don't need to call factory
352 # again for this name.
353 setattr(self, name, method)
354 return method
Ole Troanf159f582019-02-28 20:20:47 +0100355
Klement Sekeraf62ae122016-10-11 11:47:09 +0200356 def connect(self):
357 """Connect the API to VPP"""
Ole Troan4376ab22021-03-03 10:40:05 +0100358 # This might be called before VPP is prepared to listen to the socket
359 retries = 0
Klement Sekerae2636852021-03-16 12:52:12 +0100360 while not os.path.exists(self.test_class.get_api_sock_path()):
Ole Troan4376ab22021-03-03 10:40:05 +0100361 time.sleep(0.5)
362 retries += 1
363 if retries > 120:
364 break
365 self.vpp.connect(self.name[:63])
Klement Sekera7112c542017-03-01 09:53:19 +0100366 self.papi = self.vpp.api
367 self.vpp.register_event_callback(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200368
369 def disconnect(self):
370 """Disconnect the API from VPP"""
Klement Sekera7112c542017-03-01 09:53:19 +0100371 self.vpp.disconnect()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200372
373 def api(self, api_fn, api_args, expected_retval=0):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200374 """Call API function and check it's return value.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200375 Call the appropriate hooks before and after the API call
376
377 :param api_fn: API function to call
378 :param api_args: tuple of API function arguments
379 :param expected_retval: Expected return value (Default value = 0)
380 :returns: reply from the API
381
382 """
383 self.hook.before_api(api_fn.__name__, api_args)
Ole Troan7e3a8752016-12-05 10:27:09 +0100384 reply = api_fn(**api_args)
Klement Sekerae0545ef2017-01-25 08:00:40 +0100385 if self._expect_api_retval == self._negative:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200386 if hasattr(reply, "retval") and reply.retval >= 0:
387 msg = (
388 "%s(%s) passed unexpectedly: expected negative "
389 "return value instead of %d in %s"
390 % (
391 api_fn.__name__,
392 as_fn_signature(api_args),
393 reply.retval,
394 moves.reprlib.repr(reply),
395 )
396 )
Klement Sekerae0545ef2017-01-25 08:00:40 +0100397 self.test_class.logger.info(msg)
Klement Sekera77c821c2022-04-14 16:29:07 +0200398 raise UnexpectedApiReturnValueError(reply.retval, msg)
Klement Sekerae0545ef2017-01-25 08:00:40 +0100399 elif self._expect_api_retval == self._zero:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200400 if hasattr(reply, "retval") and reply.retval != expected_retval:
401 msg = (
402 "%s(%s) failed, expected %d return value instead "
403 "of %d in %s"
404 % (
405 api_fn.__name__,
406 as_fn_signature(api_args),
407 expected_retval,
408 reply.retval,
409 repr(reply),
410 )
411 )
Klement Sekerae0545ef2017-01-25 08:00:40 +0100412 self.test_class.logger.info(msg)
Klement Sekera77c821c2022-04-14 16:29:07 +0200413 raise UnexpectedApiReturnValueError(reply.retval, msg)
Klement Sekerae0545ef2017-01-25 08:00:40 +0100414 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200415 raise Exception(
416 "Internal error, unexpected value for "
417 "self._expect_api_retval %s" % self._expect_api_retval
418 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200419 self.hook.after_api(api_fn.__name__, api_args)
420 return reply
421
Dave Barach5932ce12019-10-09 17:28:04 -0400422 def cli_return_response(self, cli):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200423 """Execute a CLI, calling the before/after hooks appropriately.
Dave Barach5932ce12019-10-09 17:28:04 -0400424 Return the reply without examining it
425
426 :param cli: CLI to execute
427 :returns: response object
428
429 """
430 self.hook.before_cli(cli)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200431 cli += "\n"
Dave Barach5932ce12019-10-09 17:28:04 -0400432 r = self.papi.cli_inband(cmd=cli)
433 self.hook.after_cli(cli)
434 return r
435
Klement Sekeraf62ae122016-10-11 11:47:09 +0200436 def cli(self, cli):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200437 """Execute a CLI, calling the before/after hooks appropriately.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200438
439 :param cli: CLI to execute
440 :returns: CLI output
441
442 """
Dave Barach5932ce12019-10-09 17:28:04 -0400443 r = self.cli_return_response(cli)
Paul Vinciguerra9673e3e2019-05-10 20:41:08 -0400444 if r.retval == -156:
445 raise CliSyntaxError(r.reply)
446 if r.retval != 0:
447 raise CliFailedCommandError(r.reply)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200448 if hasattr(r, "reply"):
Ole Troan413f4a52018-11-28 11:36:05 +0100449 return r.reply
Klement Sekeraf62ae122016-10-11 11:47:09 +0200450
Jan49c0fca2016-10-26 15:44:27 +0200451 def ppcli(self, cli):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200452 """Helper method to print CLI command in case of info logging level.
Jan49c0fca2016-10-26 15:44:27 +0200453
454 :param cli: CLI to execute
455 :returns: CLI output
456 """
Ole Troan6ed154f2019-10-15 19:31:55 +0200457 return cli + "\n" + self.cli(cli)
Jan49c0fca2016-10-26 15:44:27 +0200458
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200459 def ip6nd_send_router_solicitation(self, sw_if_index, irt=1, mrt=120, mrc=0, mrd=0):
460 return self.api(
461 self.papi.ip6nd_send_router_solicitation,
462 {
463 "irt": irt,
464 "mrt": mrt,
465 "mrc": mrc,
466 "mrd": mrd,
467 "sw_if_index": sw_if_index,
468 },
469 )
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100470
Juraj Slobodab3f90502018-10-04 15:15:16 +0200471 def want_interface_events(self, enable_disable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200472 return self.api(
473 self.papi.want_interface_events,
474 {
475 "enable_disable": enable_disable,
476 "pid": os.getpid(),
477 },
478 )
Juraj Slobodab3f90502018-10-04 15:15:16 +0200479
Neale Rannsc819fc62018-02-16 02:44:05 -0800480 def sw_interface_set_mac_address(self, sw_if_index, mac):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200481 return self.api(
482 self.papi.sw_interface_set_mac_address,
483 {"sw_if_index": sw_if_index, "mac_address": mac},
484 )
Neale Rannsc819fc62018-02-16 02:44:05 -0800485
Ole Troane1ade682019-03-04 23:55:43 +0100486 def p2p_ethernet_add(self, sw_if_index, remote_mac, subif_id):
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200487 """Create p2p ethernet subinterface
488
489 :param sw_if_index: main (parent) interface
490 :param remote_mac: client (remote) mac address
491
492 """
493 return self.api(
494 self.papi.p2p_ethernet_add,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200495 {
496 "parent_if_index": sw_if_index,
497 "remote_mac": remote_mac,
498 "subif_id": subif_id,
499 },
500 )
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200501
Ole Troane1ade682019-03-04 23:55:43 +0100502 def p2p_ethernet_del(self, sw_if_index, remote_mac):
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200503 """Delete p2p ethernet subinterface
504
505 :param sw_if_index: main (parent) interface
506 :param remote_mac: client (remote) mac address
507
508 """
509 return self.api(
510 self.papi.p2p_ethernet_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200511 {"parent_if_index": sw_if_index, "remote_mac": remote_mac},
512 )
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200513
Klement Sekeraf62ae122016-10-11 11:47:09 +0200514 def create_vlan_subif(self, sw_if_index, vlan):
515 """
516
517 :param vlan:
518 :param sw_if_index:
519
520 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200521 return self.api(
522 self.papi.create_vlan_subif, {"sw_if_index": sw_if_index, "vlan_id": vlan}
523 )
Klement Sekeraf62ae122016-10-11 11:47:09 +0200524
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200525 def create_loopback(self, mac=""):
Matej Klotton0178d522016-11-04 11:11:44 +0100526 """
527
528 :param mac: (Optional)
529 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200530 return self.api(self.papi.create_loopback, {"mac_address": mac})
Matej Klotton0178d522016-11-04 11:11:44 +0100531
Neale Ranns097fa662018-05-01 05:17:55 -0700532 def ip_route_dump(self, table_id, is_ip6=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200533 return self.api(
534 self.papi.ip_route_dump, {"table": {"table_id": table_id, "is_ip6": is_ip6}}
535 )
Neale Ranns097fa662018-05-01 05:17:55 -0700536
Neale Ranns976b2592019-12-04 06:11:00 +0000537 def ip_route_v2_dump(self, table_id, is_ip6=False, src=0):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200538 return self.api(
539 self.papi.ip_route_v2_dump,
540 {"src": src, "table": {"table_id": table_id, "is_ip6": is_ip6}},
541 )
Neale Ranns976b2592019-12-04 06:11:00 +0000542
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200543 def ip_neighbor_add_del(
544 self, sw_if_index, mac_address, ip_address, is_add=1, flags=0
545 ):
546 """Add neighbor MAC to IPv4 or IPv6 address.
Matej Klotton0178d522016-11-04 11:11:44 +0100547
548 :param sw_if_index:
549 :param mac_address:
550 :param dst_address:
Matej Klotton0178d522016-11-04 11:11:44 +0100551 :param is_add: (Default value = 1)
Neale Ranns37029302018-08-10 05:30:06 -0700552 :param flags: (Default value = 0/NONE)
Matej Klotton0178d522016-11-04 11:11:44 +0100553 """
Matej Klotton0178d522016-11-04 11:11:44 +0100554 return self.api(
Ole Troan7e3a8752016-12-05 10:27:09 +0100555 self.papi.ip_neighbor_add_del,
Neale Ranns37029302018-08-10 05:30:06 -0700556 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200557 "is_add": is_add,
558 "neighbor": {
559 "sw_if_index": sw_if_index,
560 "flags": flags,
561 "mac_address": mac_address,
562 "ip_address": ip_address,
563 },
564 },
Matej Klotton0178d522016-11-04 11:11:44 +0100565 )
Pavel Kotucekf6e3dc42016-11-04 09:58:01 +0100566
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200567 def udp_encap_add(self, src_ip, dst_ip, src_port, dst_port, table_id=0):
568 """Add a GRE tunnel
Neale Ranns810086d2017-11-05 16:26:46 -0800569 :param src_ip:
570 :param dst_ip:
571 :param src_port:
572 :param dst_port:
573 :param outer_fib_id: (Default value = 0)
Neale Ranns810086d2017-11-05 16:26:46 -0800574 """
575
576 return self.api(
Neale Rannsd0df49f2018-08-08 01:06:40 -0700577 self.papi.udp_encap_add,
578 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200579 "udp_encap": {
580 "src_ip": src_ip,
581 "dst_ip": dst_ip,
582 "src_port": src_port,
583 "dst_port": dst_port,
584 "table_id": table_id,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700585 }
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200586 },
587 )
Neale Rannsd0df49f2018-08-08 01:06:40 -0700588
Neale Ranns097fa662018-05-01 05:17:55 -0700589 def udp_encap_del(self, id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200590 return self.api(self.papi.udp_encap_del, {"id": id})
Neale Ranns097fa662018-05-01 05:17:55 -0700591
592 def udp_encap_dump(self):
593 return self.api(self.papi.udp_encap_dump, {})
594
595 def want_udp_encap_stats(self, enable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200596 return self.api(
597 self.papi.want_udp_encap_stats, {"enable": enable, "pid": os.getpid()}
598 )
Neale Ranns097fa662018-05-01 05:17:55 -0700599
600 def mpls_route_dump(self, table_id):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200601 return self.api(self.papi.mpls_route_dump, {"table": {"mt_table_id": table_id}})
Neale Ranns097fa662018-05-01 05:17:55 -0700602
603 def mpls_table_dump(self):
604 return self.api(self.papi.mpls_table_dump, {})
605
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200606 def mpls_table_add_del(self, table_id, is_add=1):
Neale Ranns15002542017-09-10 04:39:11 -0700607 """
608
609 :param table_id
610 :param is_add: (Default value = 1)
611
612 """
613
614 return self.api(
615 self.papi.mpls_table_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200616 {
617 "mt_table": {
618 "mt_table_id": table_id,
619 },
620 "mt_is_add": is_add,
621 },
622 )
Neale Ranns15002542017-09-10 04:39:11 -0700623
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200624 def mpls_route_add_del(
625 self, table_id, label, eos, eos_proto, is_multicast, paths, is_add, is_multipath
626 ):
627 """MPLS Route add/del"""
Neale Ranns097fa662018-05-01 05:17:55 -0700628 return self.api(
629 self.papi.mpls_route_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200630 {
631 "mr_route": {
632 "mr_table_id": table_id,
633 "mr_label": label,
634 "mr_eos": eos,
635 "mr_eos_proto": eos_proto,
636 "mr_is_multicast": is_multicast,
637 "mr_n_paths": len(paths),
638 "mr_paths": paths,
639 },
640 "mr_is_add": is_add,
641 "mr_is_multipath": is_multipath,
642 },
643 )
Neale Ranns097fa662018-05-01 05:17:55 -0700644
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200645 def mpls_ip_bind_unbind(self, label, prefix, table_id=0, ip_table_id=0, is_bind=1):
646 """ """
Neale Rannsad422ed2016-11-02 14:20:04 +0000647 return self.api(
Ole Troan7e3a8752016-12-05 10:27:09 +0100648 self.papi.mpls_ip_bind_unbind,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200649 {
650 "mb_mpls_table_id": table_id,
651 "mb_label": label,
652 "mb_ip_table_id": ip_table_id,
653 "mb_is_bind": is_bind,
654 "mb_prefix": prefix,
655 },
656 )
Neale Rannsad422ed2016-11-02 14:20:04 +0000657
658 def mpls_tunnel_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200659 self, tun_sw_if_index, paths, is_add=1, l2_only=0, is_multicast=0
660 ):
661 """ """
Neale Rannsad422ed2016-11-02 14:20:04 +0000662 return self.api(
Ole Troan7e3a8752016-12-05 10:27:09 +0100663 self.papi.mpls_tunnel_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200664 {
665 "mt_is_add": is_add,
666 "mt_tunnel": {
667 "mt_sw_if_index": tun_sw_if_index,
668 "mt_l2_only": l2_only,
669 "mt_is_multicast": is_multicast,
670 "mt_n_paths": len(paths),
671 "mt_paths": paths,
672 },
673 },
674 )
Matus Fabiande886752016-12-07 03:38:19 -0800675
Steve Shin7957d6e2016-12-19 09:24:50 -0800676 def input_acl_set_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200677 self,
678 is_add,
679 sw_if_index,
680 ip4_table_index=0xFFFFFFFF,
681 ip6_table_index=0xFFFFFFFF,
682 l2_table_index=0xFFFFFFFF,
683 ):
Steve Shin7957d6e2016-12-19 09:24:50 -0800684 """
685 :param is_add:
686 :param sw_if_index:
687 :param ip4_table_index: (Default value = 0xFFFFFFFF)
688 :param ip6_table_index: (Default value = 0xFFFFFFFF)
689 :param l2_table_index: (Default value = 0xFFFFFFFF)
690 """
691
692 return self.api(
693 self.papi.input_acl_set_interface,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200694 {
695 "sw_if_index": sw_if_index,
696 "ip4_table_index": ip4_table_index,
697 "ip6_table_index": ip6_table_index,
698 "l2_table_index": l2_table_index,
699 "is_add": is_add,
700 },
701 )
Klement Sekera77fabdb2017-01-02 07:46:14 +0100702
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100703 def output_acl_set_interface(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200704 self,
705 is_add,
706 sw_if_index,
707 ip4_table_index=0xFFFFFFFF,
708 ip6_table_index=0xFFFFFFFF,
709 l2_table_index=0xFFFFFFFF,
710 ):
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100711 """
712 :param is_add:
713 :param sw_if_index:
714 :param ip4_table_index: (Default value = 0xFFFFFFFF)
715 :param ip6_table_index: (Default value = 0xFFFFFFFF)
716 :param l2_table_index: (Default value = 0xFFFFFFFF)
717 """
718
719 return self.api(
720 self.papi.output_acl_set_interface,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200721 {
722 "sw_if_index": sw_if_index,
723 "ip4_table_index": ip4_table_index,
724 "ip6_table_index": ip6_table_index,
725 "l2_table_index": l2_table_index,
726 "is_add": is_add,
727 },
728 )
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100729
Klement Sekera77fabdb2017-01-02 07:46:14 +0100730 def set_ipfix_exporter(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200731 self,
732 collector_address,
733 src_address,
734 path_mtu,
735 template_interval,
736 vrf_id=0,
737 collector_port=4739,
738 udp_checksum=0,
739 ):
Klement Sekera77fabdb2017-01-02 07:46:14 +0100740 return self.api(
741 self.papi.set_ipfix_exporter,
742 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200743 "collector_address": collector_address,
744 "collector_port": collector_port,
745 "src_address": src_address,
746 "vrf_id": vrf_id,
747 "path_mtu": path_mtu,
748 "template_interval": template_interval,
749 "udp_checksum": udp_checksum,
750 },
751 )
Neale Rannsfca0c242017-01-13 07:57:46 -0800752
Neale Ranns097fa662018-05-01 05:17:55 -0700753 def mfib_signal_dump(self):
754 return self.api(self.papi.mfib_signal_dump, {})
755
756 def ip_mroute_dump(self, table_id, is_ip6=False):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200757 return self.api(
758 self.papi.ip_mroute_dump,
759 {"table": {"table_id": table_id, "is_ip6": is_ip6}},
760 )
Neale Ranns32e1c012016-11-22 17:07:28 +0000761
Hongjun Ni62f9cdd2017-07-04 20:11:57 +0800762 def pppoe_add_del_session(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200763 self, client_ip, client_mac, session_id=0, is_add=1, decap_vrf_id=0
764 ):
Hongjun Ni62f9cdd2017-07-04 20:11:57 +0800765 """
766
767 :param is_add: (Default value = 1)
768 :param is_ipv6: (Default value = 0)
769 :param client_ip:
770 :param session_id: (Default value = 0)
771 :param client_mac:
772 :param decap_vrf_id: (Default value = 0)
773
774 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200775 return self.api(
776 self.papi.pppoe_add_del_session,
777 {
778 "is_add": is_add,
779 "session_id": session_id,
780 "client_ip": client_ip,
781 "decap_vrf_id": decap_vrf_id,
782 "client_mac": client_mac,
783 },
784 )
Kris Michielsen91074432017-06-22 13:00:20 +0200785
Neale Ranns7c922dc2018-08-30 06:12:27 -0700786 def sr_mpls_policy_add(self, bsid, weight, type, segments):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200787 return self.api(
788 self.papi.sr_mpls_policy_add,
789 {
790 "bsid": bsid,
791 "weight": weight,
792 "is_spray": type,
793 "n_segments": len(segments),
794 "segments": segments,
795 },
796 )
Neale Ranns7c922dc2018-08-30 06:12:27 -0700797
798 def sr_mpls_policy_del(self, bsid):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200799 return self.api(self.papi.sr_mpls_policy_del, {"bsid": bsid})
Neale Ranns7c922dc2018-08-30 06:12:27 -0700800
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200801 def bier_table_add_del(self, bti, mpls_label, is_add=1):
802 """BIER Table add/del"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700803 return self.api(
804 self.papi.bier_table_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200805 {
806 "bt_tbl_id": {
807 "bt_set": bti.set_id,
808 "bt_sub_domain": bti.sub_domain_id,
809 "bt_hdr_len_id": bti.hdr_len_id,
810 },
811 "bt_label": mpls_label,
812 "bt_is_add": is_add,
813 },
814 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700815
816 def bier_table_dump(self):
817 return self.api(self.papi.bier_table_dump, {})
818
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200819 def bier_route_add_del(self, bti, bp, paths, is_add=1, is_replace=0):
820 """BIER Route add/del"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700821 return self.api(
822 self.papi.bier_route_add_del,
Neale Ranns097fa662018-05-01 05:17:55 -0700823 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200824 "br_route": {
825 "br_tbl_id": {
826 "bt_set": bti.set_id,
827 "bt_sub_domain": bti.sub_domain_id,
828 "bt_hdr_len_id": bti.hdr_len_id,
829 },
830 "br_bp": bp,
831 "br_n_paths": len(paths),
832 "br_paths": paths,
Neale Ranns097fa662018-05-01 05:17:55 -0700833 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200834 "br_is_add": is_add,
835 "br_is_replace": is_replace,
836 },
837 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700838
839 def bier_route_dump(self, bti):
840 return self.api(
841 self.papi.bier_route_dump,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200842 {
843 "br_tbl_id": {
844 "bt_set": bti.set_id,
845 "bt_sub_domain": bti.sub_domain_id,
846 "bt_hdr_len_id": bti.hdr_len_id,
847 }
848 },
849 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700850
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200851 def bier_imp_add(self, bti, src, ibytes, is_add=1):
852 """BIER Imposition Add"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700853 return self.api(
854 self.papi.bier_imp_add,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200855 {
856 "bi_tbl_id": {
857 "bt_set": bti.set_id,
858 "bt_sub_domain": bti.sub_domain_id,
859 "bt_hdr_len_id": bti.hdr_len_id,
860 },
861 "bi_src": src,
862 "bi_n_bytes": len(ibytes),
863 "bi_bytes": ibytes,
864 },
865 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700866
867 def bier_imp_del(self, bi_index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200868 """BIER Imposition del"""
869 return self.api(self.papi.bier_imp_del, {"bi_index": bi_index})
Neale Rannsd792d9c2017-10-21 10:53:20 -0700870
871 def bier_imp_dump(self):
872 return self.api(self.papi.bier_imp_dump, {})
873
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200874 def bier_disp_table_add_del(self, bdti, is_add=1):
875 """BIER Disposition Table add/del"""
Neale Rannsd792d9c2017-10-21 10:53:20 -0700876 return self.api(
877 self.papi.bier_disp_table_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200878 {"bdt_tbl_id": bdti, "bdt_is_add": is_add},
879 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700880
881 def bier_disp_table_dump(self):
882 return self.api(self.papi.bier_disp_table_dump, {})
883
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200884 def bier_disp_entry_add_del(
885 self,
886 bdti,
887 bp,
888 payload_proto,
889 next_hop_afi,
890 next_hop,
891 next_hop_tbl_id=0,
892 next_hop_rpf_id=~0,
893 next_hop_is_ip4=1,
894 is_add=1,
895 ):
896 """BIER Route add/del"""
Neale Ranns31ed7442018-02-23 05:29:09 -0800897 lstack = []
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200898 while len(lstack) < 16:
Neale Ranns31ed7442018-02-23 05:29:09 -0800899 lstack.append({})
Neale Rannsd792d9c2017-10-21 10:53:20 -0700900 return self.api(
901 self.papi.bier_disp_entry_add_del,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200902 {
903 "bde_tbl_id": bdti,
904 "bde_bp": bp,
905 "bde_payload_proto": payload_proto,
906 "bde_n_paths": 1,
907 "bde_paths": [
908 {
909 "table_id": next_hop_tbl_id,
910 "rpf_id": next_hop_rpf_id,
911 "n_labels": 0,
912 "label_stack": lstack,
913 }
914 ],
915 "bde_is_add": is_add,
916 },
917 )
Neale Rannsd792d9c2017-10-21 10:53:20 -0700918
919 def bier_disp_entry_dump(self, bdti):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200920 return self.api(self.papi.bier_disp_entry_dump, {"bde_tbl_id": bdti})
Gabriel Ganne8527f122017-10-02 11:41:24 +0200921
“mystarrocks”23f0c452017-12-11 07:11:51 -0800922 def ipsec_spd_add_del(self, spd_id, is_add=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200923 """SPD add/del - Wrapper to add or del ipsec SPD
“mystarrocks”23f0c452017-12-11 07:11:51 -0800924 Sample CLI : 'ipsec spd add 1'
925
926 :param spd_id - SPD ID to be created in the vpp . mandatory
927 :param is_add - create (1) or delete(0) SPD (Default 1 - add) .
Dave Wallaced1706812021-08-12 18:36:02 -0400928 optional
“mystarrocks”23f0c452017-12-11 07:11:51 -0800929 :returns: reply from the API
930 """
931 return self.api(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200932 self.papi.ipsec_spd_add_del, {"spd_id": spd_id, "is_add": is_add}
933 )
“mystarrocks”23f0c452017-12-11 07:11:51 -0800934
Neale Ranns311124e2019-01-24 04:52:25 -0800935 def ipsec_spds_dump(self):
936 return self.api(self.papi.ipsec_spds_dump, {})
937
“mystarrocks”23f0c452017-12-11 07:11:51 -0800938 def ipsec_interface_add_del_spd(self, spd_id, sw_if_index, is_add=1):
939 """ IPSEC interface SPD add/del - \
940 Wrapper to associate/disassociate SPD to interface in VPP
941 Sample CLI : 'set interface ipsec spd GigabitEthernet0/6/0 1'
942
943 :param spd_id - SPD ID to associate with the interface . mandatory
944 :param sw_if_index - Interface Index which needs to ipsec \
945 association mandatory
946 :param is_add - add(1) or del(0) association with interface \
947 (Default 1 - add) . optional
948 :returns: reply from the API
949 """
950 return self.api(
Klement Sekera4b089f22018-04-17 18:04:57 +0200951 self.papi.ipsec_interface_add_del_spd,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200952 {"spd_id": spd_id, "sw_if_index": sw_if_index, "is_add": is_add},
953 )
“mystarrocks”23f0c452017-12-11 07:11:51 -0800954
Neale Ranns311124e2019-01-24 04:52:25 -0800955 def ipsec_spd_interface_dump(self, spd_index=None):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200956 return self.api(
957 self.papi.ipsec_spd_interface_dump,
958 {
959 "spd_index": spd_index if spd_index else 0,
960 "spd_index_valid": 1 if spd_index else 0,
961 },
962 )
Neale Ranns311124e2019-01-24 04:52:25 -0800963
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200964 def ipsec_spd_entry_add_del(
965 self,
966 spd_id,
967 sa_id,
968 local_address_start,
969 local_address_stop,
970 remote_address_start,
971 remote_address_stop,
972 local_port_start=0,
973 local_port_stop=65535,
974 remote_port_start=0,
975 remote_port_stop=65535,
Piotr Bronowski815c6a42022-06-09 09:09:28 +0000976 protocol=socket.IPPROTO_RAW,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200977 policy=0,
978 priority=100,
979 is_outbound=1,
980 is_add=1,
981 is_ipv6=0,
982 is_ip_any=0,
983 ):
984 """IPSEC policy SPD add/del -
“mystarrocks”23f0c452017-12-11 07:11:51 -0800985 Wrapper to configure ipsec SPD policy entries in VPP
Dave Wallaced1706812021-08-12 18:36:02 -0400986
Klement Sekera31da2e32018-06-24 22:49:55 +0200987 :param spd_id: SPD ID for the policy
988 :param local_address_start: local-ip-range start address
Dave Wallaced1706812021-08-12 18:36:02 -0400989 :param local_address_stop: local-ip-range stop address
Klement Sekera31da2e32018-06-24 22:49:55 +0200990 :param remote_address_start: remote-ip-range start address
Dave Wallaced1706812021-08-12 18:36:02 -0400991 :param remote_address_stop: remote-ip-range stop address
Klement Sekera31da2e32018-06-24 22:49:55 +0200992 :param local_port_start: (Default value = 0)
993 :param local_port_stop: (Default value = 65535)
994 :param remote_port_start: (Default value = 0)
995 :param remote_port_stop: (Default value = 65535)
996 :param protocol: Any(0), AH(51) & ESP(50) protocol (Default value = 0)
997 :param sa_id: Security Association ID for mapping it to SPD
998 :param policy: bypass(0), discard(1), resolve(2) or protect(3) action
Dave Wallaced1706812021-08-12 18:36:02 -0400999 (Default value = 0)
Klement Sekera31da2e32018-06-24 22:49:55 +02001000 :param priority: value for the spd action (Default value = 100)
1001 :param is_outbound: flag for inbound(0) or outbound(1)
Dave Wallaced1706812021-08-12 18:36:02 -04001002 (Default value = 1)
Klement Sekera31da2e32018-06-24 22:49:55 +02001003 :param is_add: (Default value = 1)
“mystarrocks”23f0c452017-12-11 07:11:51 -08001004 """
1005 return self.api(
Piotr Bronowski815c6a42022-06-09 09:09:28 +00001006 self.papi.ipsec_spd_entry_add_del_v2,
Neale Ranns17dcec02019-01-09 21:22:20 -08001007 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001008 "is_add": is_add,
1009 "entry": {
1010 "spd_id": spd_id,
1011 "sa_id": sa_id,
1012 "local_address_start": local_address_start,
1013 "local_address_stop": local_address_stop,
1014 "remote_address_start": remote_address_start,
1015 "remote_address_stop": remote_address_stop,
1016 "local_port_start": local_port_start,
1017 "local_port_stop": local_port_stop,
1018 "remote_port_start": remote_port_start,
1019 "remote_port_stop": remote_port_stop,
1020 "protocol": protocol,
1021 "policy": policy,
1022 "priority": priority,
1023 "is_outbound": is_outbound,
1024 },
1025 },
1026 )
Florin Corasb795bd02017-12-14 11:30:48 -08001027
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001028 def ipsec_spd_dump(self, spd_id, sa_id=0xFFFFFFFF):
1029 return self.api(self.papi.ipsec_spd_dump, {"spd_id": spd_id, "sa_id": sa_id})
Neale Ranns311124e2019-01-24 04:52:25 -08001030
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001031 def ipsec_tunnel_if_add_del(
1032 self,
1033 local_ip,
1034 remote_ip,
1035 local_spi,
1036 remote_spi,
1037 crypto_alg,
1038 local_crypto_key,
1039 remote_crypto_key,
1040 integ_alg,
1041 local_integ_key,
1042 remote_integ_key,
1043 is_add=1,
1044 esn=0,
1045 salt=0,
1046 anti_replay=1,
1047 renumber=0,
1048 udp_encap=0,
1049 show_instance=0xFFFFFFFF,
1050 ):
Klement Sekera31da2e32018-06-24 22:49:55 +02001051 return self.api(
1052 self.papi.ipsec_tunnel_if_add_del,
Kingwel Xie1ba5bc82019-03-20 07:21:58 -04001053 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001054 "local_ip": local_ip,
1055 "remote_ip": remote_ip,
1056 "local_spi": local_spi,
1057 "remote_spi": remote_spi,
1058 "crypto_alg": crypto_alg,
1059 "local_crypto_key_len": len(local_crypto_key),
1060 "local_crypto_key": local_crypto_key,
1061 "remote_crypto_key_len": len(remote_crypto_key),
1062 "remote_crypto_key": remote_crypto_key,
1063 "integ_alg": integ_alg,
1064 "local_integ_key_len": len(local_integ_key),
1065 "local_integ_key": local_integ_key,
1066 "remote_integ_key_len": len(remote_integ_key),
1067 "remote_integ_key": remote_integ_key,
1068 "is_add": is_add,
1069 "esn": esn,
1070 "anti_replay": anti_replay,
1071 "renumber": renumber,
1072 "show_instance": show_instance,
1073 "udp_encap": udp_encap,
1074 "salt": salt,
1075 },
1076 )
Klement Sekera31da2e32018-06-24 22:49:55 +02001077
Klement Sekerab4d30532018-11-08 13:00:02 +01001078 def ipsec_select_backend(self, protocol, index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001079 return self.api(
1080 self.papi.ipsec_select_backend, {"protocol": protocol, "index": index}
1081 )
Klement Sekerab4d30532018-11-08 13:00:02 +01001082
1083 def ipsec_backend_dump(self):
1084 return self.api(self.papi.ipsec_backend_dump, {})
1085
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001086 def punt_socket_register(self, reg, pathname, header_version=1):
1087 """Register punt socket"""
1088 return self.api(
1089 self.papi.punt_socket_register,
1090 {"header_version": header_version, "punt": reg, "pathname": pathname},
1091 )
Pavel Kotuceke88865d2018-11-28 07:42:11 +01001092
Neale Ranns50f0ac02019-05-15 02:13:37 -07001093 def punt_socket_deregister(self, reg):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001094 """Unregister punt socket"""
1095 return self.api(self.papi.punt_socket_deregister, {"punt": reg})
Klement Sekera75e7d132017-09-20 08:26:30 +02001096
Neale Ranns947ea622018-06-07 23:48:20 -07001097 def igmp_enable_disable(self, sw_if_index, enable, host):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001098 """Enable/disable IGMP on a given interface"""
1099 return self.api(
1100 self.papi.igmp_enable_disable,
1101 {"enable": enable, "mode": host, "sw_if_index": sw_if_index},
1102 )
Neale Ranns947ea622018-06-07 23:48:20 -07001103
Jakub Grajciar97748ca2018-10-04 11:05:35 +02001104 def igmp_proxy_device_add_del(self, vrf_id, sw_if_index, add):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001105 """Add/del IGMP proxy device"""
1106 return self.api(
1107 self.papi.igmp_proxy_device_add_del,
1108 {"vrf_id": vrf_id, "sw_if_index": sw_if_index, "add": add},
1109 )
Jakub Grajciar97748ca2018-10-04 11:05:35 +02001110
1111 def igmp_proxy_device_add_del_interface(self, vrf_id, sw_if_index, add):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001112 """Add/del interface to/from IGMP proxy device"""
1113 return self.api(
1114 self.papi.igmp_proxy_device_add_del_interface,
1115 {"vrf_id": vrf_id, "sw_if_index": sw_if_index, "add": add},
1116 )
Jakub Grajciar97748ca2018-10-04 11:05:35 +02001117
Neale Ranns947ea622018-06-07 23:48:20 -07001118 def igmp_listen(self, filter, sw_if_index, saddrs, gaddr):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001119 """Listen for new (S,G) on specified interface
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001120
Dave Wallaced1706812021-08-12 18:36:02 -04001121 :param enable: add/delas
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001122 :param sw_if_index: interface sw index
1123 :param saddr: source ip4 addr
1124 :param gaddr: group ip4 addr
1125 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001126 return self.api(
1127 self.papi.igmp_listen,
1128 {
1129 "group": {
1130 "filter": filter,
1131 "sw_if_index": sw_if_index,
1132 "n_srcs": len(saddrs),
1133 "saddrs": saddrs,
1134 "gaddr": gaddr,
1135 }
1136 },
1137 )
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001138
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001139 def igmp_clear_interface(self, sw_if_index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001140 """Remove all (S,G)s from specified interface
1141 doesn't send IGMP report!
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001142 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001143 return self.api(self.papi.igmp_clear_interface, {"sw_if_index": sw_if_index})
Jakub Grajciar7b867a82017-12-08 16:28:42 +01001144
1145 def want_igmp_events(self, enable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02001146 return self.api(
1147 self.papi.want_igmp_events, {"enable": enable, "pid": os.getpid()}
1148 )