]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_dplane.c
zebra: debug flags for evpn-mh ES and MAC-ECMP
[mirror_frr.git] / zebra / zebra_dplane.c
CommitLineData
ea1c14f6
MS
1/*
2 * Zebra dataplane layer.
3 * Copyright (c) 2018 Volta Networks, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
2618a52e
DL
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
18c37974 24#include "lib/libfrr.h"
1d11b21f
MS
25#include "lib/debug.h"
26#include "lib/frratomic.h"
7cdb1a84 27#include "lib/frr_pthread.h"
1d11b21f 28#include "lib/memory.h"
7cdb1a84 29#include "lib/queue.h"
1d11b21f 30#include "lib/zebra.h"
7cdb1a84 31#include "zebra/zebra_memory.h"
3801e764 32#include "zebra/zebra_router.h"
7cdb1a84 33#include "zebra/zebra_dplane.h"
f2a0ba3a 34#include "zebra/zebra_vxlan_private.h"
ee70f629 35#include "zebra/zebra_mpls.h"
7cdb1a84
MS
36#include "zebra/rt.h"
37#include "zebra/debug.h"
f62e5480 38#include "zebra/zebra_pbr.h"
7cdb1a84
MS
39
40/* Memory type for context blocks */
c1344b54
DL
41DEFINE_MTYPE_STATIC(ZEBRA, DP_CTX, "Zebra DPlane Ctx")
42DEFINE_MTYPE_STATIC(ZEBRA, DP_PROV, "Zebra DPlane Provider")
7cdb1a84
MS
43
44#ifndef AOK
45# define AOK 0
46#endif
47
e5a60d82
MS
48/* Enable test dataplane provider */
49/*#define DPLANE_TEST_PROVIDER 1 */
50
91f16812
MS
51/* Default value for max queued incoming updates */
52const uint32_t DPLANE_DEFAULT_MAX_QUEUED = 200;
53
c831033f
MS
54/* Default value for new work per cycle */
55const uint32_t DPLANE_DEFAULT_NEW_WORK = 100;
56
7cdb1a84
MS
57/* Validation check macro for context blocks */
58/* #define DPLANE_DEBUG 1 */
59
60#ifdef DPLANE_DEBUG
61
25779064
MS
62# define DPLANE_CTX_VALID(p) \
63 assert((p) != NULL)
7cdb1a84
MS
64
65#else
66
5709131c 67# define DPLANE_CTX_VALID(p)
7cdb1a84
MS
68
69#endif /* DPLANE_DEBUG */
70
0c8215cb
SW
71/*
72 * Nexthop information captured for nexthop/nexthop group updates
73 */
74struct dplane_nexthop_info {
75 uint32_t id;
76 afi_t afi;
77 vrf_id_t vrf_id;
38e40db1 78 int type;
0c8215cb
SW
79
80 struct nexthop_group ng;
e22e8001
SW
81 struct nh_grp nh_grp[MULTIPATH_NUM];
82 uint8_t nh_grp_count;
0c8215cb
SW
83};
84
7cdb1a84 85/*
0f461727 86 * Route information captured for route updates.
7cdb1a84 87 */
0f461727 88struct dplane_route_info {
7cdb1a84
MS
89
90 /* Dest and (optional) source prefixes */
91 struct prefix zd_dest;
92 struct prefix zd_src;
93
0f461727
MS
94 afi_t zd_afi;
95 safi_t zd_safi;
7cdb1a84
MS
96
97 int zd_type;
98 int zd_old_type;
99
7cdb1a84
MS
100 route_tag_t zd_tag;
101 route_tag_t zd_old_tag;
102 uint32_t zd_metric;
01ce7cba 103 uint32_t zd_old_metric;
0f461727 104
7cdb1a84
MS
105 uint16_t zd_instance;
106 uint16_t zd_old_instance;
107
108 uint8_t zd_distance;
109 uint8_t zd_old_distance;
110
111 uint32_t zd_mtu;
112 uint32_t zd_nexthop_mtu;
113
0c8215cb
SW
114 /* Nexthop hash entry info */
115 struct dplane_nexthop_info nhe;
f820d025 116
7cdb1a84 117 /* Nexthops */
1d48702e 118 uint32_t zd_nhg_id;
7cdb1a84
MS
119 struct nexthop_group zd_ng;
120
1d48702e
MS
121 /* Backup nexthops (if present) */
122 struct nexthop_group backup_ng;
123
4dfd7a02 124 /* "Previous" nexthops, used only in route updates without netlink */
01ce7cba 125 struct nexthop_group zd_old_ng;
1d48702e 126 struct nexthop_group old_backup_ng;
01ce7cba 127
b8e0423d
MS
128 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
129
0f461727
MS
130};
131
d613b8e1
MS
132/*
133 * Pseudowire info for the dataplane
134 */
135struct dplane_pw_info {
d613b8e1
MS
136 int type;
137 int af;
138 int status;
139 uint32_t flags;
16d69787 140 union g_addr dest;
d613b8e1
MS
141 mpls_label_t local_label;
142 mpls_label_t remote_label;
143
16d69787
MS
144 /* Nexthops */
145 struct nexthop_group nhg;
146
d613b8e1
MS
147 union pw_protocol_fields fields;
148};
149
a4a4802a
MS
150/*
151 * Interface/prefix info for the dataplane
152 */
153struct dplane_intf_info {
154
a4a4802a
MS
155 uint32_t metric;
156 uint32_t flags;
157
158#define DPLANE_INTF_CONNECTED (1 << 0) /* Connected peer, p2p */
159#define DPLANE_INTF_SECONDARY (1 << 1)
64168803 160#define DPLANE_INTF_BROADCAST (1 << 2)
0f3af738 161#define DPLANE_INTF_HAS_DEST DPLANE_INTF_CONNECTED
64168803 162#define DPLANE_INTF_HAS_LABEL (1 << 4)
a4a4802a
MS
163
164 /* Interface address/prefix */
165 struct prefix prefix;
166
167 /* Dest address, for p2p, or broadcast prefix */
168 struct prefix dest_prefix;
169
170 char *label;
171 char label_buf[32];
172};
173
7597ac7b 174/*
0bbd4ff4 175 * EVPN MAC address info for the dataplane.
7597ac7b
MS
176 */
177struct dplane_mac_info {
178 vlanid_t vid;
478566d6 179 ifindex_t br_ifindex;
7597ac7b
MS
180 struct ethaddr mac;
181 struct in_addr vtep_ip;
182 bool is_sticky;
7597ac7b
MS
183};
184
931fa60c
MS
185/*
186 * EVPN neighbor info for the dataplane
187 */
188struct dplane_neigh_info {
189 struct ipaddr ip_addr;
190 struct ethaddr mac;
191 uint32_t flags;
192 uint16_t state;
193};
194
60d8d43b
JU
195/*
196 * Policy based routing rule info for the dataplane
197 */
198struct dplane_ctx_rule {
199 uint32_t priority;
200
201 /* The route table pointed by this rule */
202 uint32_t table;
203
204 /* Filter criteria */
205 uint32_t filter_bm;
206 uint32_t fwmark;
01f23aff 207 uint8_t dsfield;
60d8d43b
JU
208 struct prefix src_ip;
209 struct prefix dst_ip;
210};
211
212struct dplane_rule_info {
213 /*
214 * Originating zclient sock fd, so we can know who to send
215 * back to.
216 */
217 int sock;
218
219 int unique;
220 int seq;
221
222 struct dplane_ctx_rule new;
223 struct dplane_ctx_rule old;
224};
225
0f461727
MS
226/*
227 * The context block used to exchange info about route updates across
228 * the boundary between the zebra main context (and pthread) and the
229 * dataplane layer (and pthread).
230 */
231struct zebra_dplane_ctx {
232
233 /* Operation code */
234 enum dplane_op_e zd_op;
235
236 /* Status on return */
237 enum zebra_dplane_result zd_status;
238
239 /* Dplane provider id */
240 uint32_t zd_provider;
241
242 /* Flags - used by providers, e.g. */
243 int zd_flags;
244
245 bool zd_is_update;
246
247 uint32_t zd_seq;
248 uint32_t zd_old_seq;
249
0024a559
MS
250 /* Some updates may be generated by notifications: allow the
251 * plugin to notice and ignore results from its own notifications.
252 */
253 uint32_t zd_notif_provider;
254
0f461727
MS
255 /* TODO -- internal/sub-operation status? */
256 enum zebra_dplane_result zd_remote_status;
257 enum zebra_dplane_result zd_kernel_status;
258
259 vrf_id_t zd_vrf_id;
260 uint32_t zd_table_id;
261
7c7ef4a8
MS
262 char zd_ifname[INTERFACE_NAMSIZ];
263 ifindex_t zd_ifindex;
264
a4a4802a 265 /* Support info for different kinds of updates */
0f461727
MS
266 union {
267 struct dplane_route_info rinfo;
268 zebra_lsp_t lsp;
d613b8e1 269 struct dplane_pw_info pw;
a4a4802a 270 struct dplane_intf_info intf;
7597ac7b 271 struct dplane_mac_info macinfo;
931fa60c 272 struct dplane_neigh_info neigh;
60d8d43b 273 struct dplane_rule_info rule;
0f461727
MS
274 } u;
275
276 /* Namespace info, used especially for netlink kernel communication */
277 struct zebra_dplane_info zd_ns_info;
278
7cdb1a84 279 /* Embedded list linkage */
25779064 280 TAILQ_ENTRY(zebra_dplane_ctx) zd_q_entries;
7cdb1a84
MS
281};
282
c831033f
MS
283/* Flag that can be set by a pre-kernel provider as a signal that an update
284 * should bypass the kernel.
285 */
286#define DPLANE_CTX_FLAG_NO_KERNEL 0x01
287
288
7cdb1a84
MS
289/*
290 * Registration block for one dataplane provider.
291 */
25779064 292struct zebra_dplane_provider {
7cdb1a84
MS
293 /* Name */
294 char dp_name[DPLANE_PROVIDER_NAMELEN + 1];
295
296 /* Priority, for ordering among providers */
297 uint8_t dp_priority;
298
299 /* Id value */
300 uint32_t dp_id;
301
c831033f
MS
302 /* Mutex */
303 pthread_mutex_t dp_mutex;
304
305 /* Plugin-provided extra data */
306 void *dp_data;
307
308 /* Flags */
309 int dp_flags;
310
1dd4ea8a
MS
311 int (*dp_start)(struct zebra_dplane_provider *prov);
312
4c206c8f 313 int (*dp_fp)(struct zebra_dplane_provider *prov);
7cdb1a84 314
4c206c8f 315 int (*dp_fini)(struct zebra_dplane_provider *prov, bool early_p);
18c37974 316
0545c373 317 _Atomic uint32_t dp_in_counter;
c9d17fe8 318 _Atomic uint32_t dp_in_queued;
c831033f
MS
319 _Atomic uint32_t dp_in_max;
320 _Atomic uint32_t dp_out_counter;
c9d17fe8 321 _Atomic uint32_t dp_out_queued;
c831033f 322 _Atomic uint32_t dp_out_max;
0545c373 323 _Atomic uint32_t dp_error_counter;
1d11b21f 324
c831033f
MS
325 /* Queue of contexts inbound to the provider */
326 struct dplane_ctx_q dp_ctx_in_q;
327
328 /* Queue of completed contexts outbound from the provider back
329 * towards the dataplane module.
330 */
331 struct dplane_ctx_q dp_ctx_out_q;
7cdb1a84 332
c831033f
MS
333 /* Embedded list linkage for provider objects */
334 TAILQ_ENTRY(zebra_dplane_provider) dp_prov_link;
7cdb1a84
MS
335};
336
337/*
338 * Globals
339 */
25779064 340static struct zebra_dplane_globals {
7cdb1a84
MS
341 /* Mutex to control access to dataplane components */
342 pthread_mutex_t dg_mutex;
343
344 /* Results callback registered by zebra 'core' */
4c206c8f 345 int (*dg_results_cb)(struct dplane_ctx_q *ctxlist);
7cdb1a84 346
4dfd7a02
MS
347 /* Sentinel for beginning of shutdown */
348 volatile bool dg_is_shutdown;
349
350 /* Sentinel for end of shutdown */
1d11b21f
MS
351 volatile bool dg_run;
352
3fe4ccc4
MS
353 /* Update context queue inbound to the dataplane */
354 TAILQ_HEAD(zdg_ctx_q, zebra_dplane_ctx) dg_update_ctx_q;
7cdb1a84
MS
355
356 /* Ordered list of providers */
25779064 357 TAILQ_HEAD(zdg_prov_q, zebra_dplane_provider) dg_providers_q;
7cdb1a84 358
1d11b21f 359 /* Counter used to assign internal ids to providers */
b8e0423d
MS
360 uint32_t dg_provider_id;
361
91f16812
MS
362 /* Limit number of pending, unprocessed updates */
363 _Atomic uint32_t dg_max_queued_updates;
364
cf363e1b
MS
365 /* Control whether system route notifications should be produced. */
366 bool dg_sys_route_notifs;
367
c831033f
MS
368 /* Limit number of new updates dequeued at once, to pace an
369 * incoming burst.
370 */
371 uint32_t dg_updates_per_cycle;
372
0545c373 373 _Atomic uint32_t dg_routes_in;
1d11b21f 374 _Atomic uint32_t dg_routes_queued;
4dfd7a02 375 _Atomic uint32_t dg_routes_queued_max;
0545c373 376 _Atomic uint32_t dg_route_errors;
16c628de
MS
377 _Atomic uint32_t dg_other_errors;
378
f820d025
SW
379 _Atomic uint32_t dg_nexthops_in;
380 _Atomic uint32_t dg_nexthop_errors;
381
16c628de 382 _Atomic uint32_t dg_lsps_in;
16c628de
MS
383 _Atomic uint32_t dg_lsp_errors;
384
97d8d05a
MS
385 _Atomic uint32_t dg_pws_in;
386 _Atomic uint32_t dg_pw_errors;
387
64168803
MS
388 _Atomic uint32_t dg_intf_addrs_in;
389 _Atomic uint32_t dg_intf_addr_errors;
390
7597ac7b
MS
391 _Atomic uint32_t dg_macs_in;
392 _Atomic uint32_t dg_mac_errors;
393
931fa60c
MS
394 _Atomic uint32_t dg_neighs_in;
395 _Atomic uint32_t dg_neigh_errors;
396
60d8d43b
JU
397 _Atomic uint32_t dg_rules_in;
398 _Atomic uint32_t dg_rule_errors;
399
c831033f 400 _Atomic uint32_t dg_update_yields;
1d11b21f 401
d8c16a95
MS
402 /* Dataplane pthread */
403 struct frr_pthread *dg_pthread;
404
7cdb1a84
MS
405 /* Event-delivery context 'master' for the dplane */
406 struct thread_master *dg_master;
407
408 /* Event/'thread' pointer for queued updates */
409 struct thread *dg_t_update;
410
4dfd7a02
MS
411 /* Event pointer for pending shutdown check loop */
412 struct thread *dg_t_shutdown_check;
413
25779064 414} zdplane_info;
7cdb1a84
MS
415
416/*
c831033f 417 * Lock and unlock for interactions with the zebra 'core' pthread
7cdb1a84 418 */
25779064 419#define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
25779064 420#define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
7cdb1a84 421
c831033f
MS
422
423/*
424 * Lock and unlock for individual providers
425 */
426#define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
427#define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
428
7cdb1a84 429/* Prototypes */
c831033f 430static int dplane_thread_loop(struct thread *event);
62b8bb7a
MS
431static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
432 struct zebra_ns *zns);
16c628de
MS
433static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
434 enum dplane_op_e op);
97d8d05a
MS
435static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
436 enum dplane_op_e op);
64168803
MS
437static enum zebra_dplane_result intf_addr_update_internal(
438 const struct interface *ifp, const struct connected *ifc,
439 enum dplane_op_e op);
f73a8467 440static enum zebra_dplane_result mac_update_common(
7597ac7b 441 enum dplane_op_e op, const struct interface *ifp,
478566d6 442 const struct interface *br_ifp,
7597ac7b
MS
443 vlanid_t vid, const struct ethaddr *mac,
444 struct in_addr vtep_ip, bool sticky);
931fa60c
MS
445static enum zebra_dplane_result neigh_update_internal(
446 enum dplane_op_e op,
447 const struct interface *ifp,
448 const struct ethaddr *mac,
449 const struct ipaddr *ip,
450 uint32_t flags, uint16_t state);
7cdb1a84
MS
451
452/*
453 * Public APIs
454 */
455
ad6aad4d
MS
456/* Obtain thread_master for dataplane thread */
457struct thread_master *dplane_get_thread_master(void)
458{
459 return zdplane_info.dg_master;
460}
461
7cdb1a84 462/*
b8e0423d 463 * Allocate a dataplane update context
7cdb1a84 464 */
593e4eb1 465struct zebra_dplane_ctx *dplane_ctx_alloc(void)
7cdb1a84 466{
25779064 467 struct zebra_dplane_ctx *p;
7cdb1a84 468
b8e0423d
MS
469 /* TODO -- just alloc'ing memory, but would like to maintain
470 * a pool
471 */
25779064 472 p = XCALLOC(MTYPE_DP_CTX, sizeof(struct zebra_dplane_ctx));
7cdb1a84 473
5709131c 474 return p;
7cdb1a84
MS
475}
476
cf363e1b
MS
477/* Enable system route notifications */
478void dplane_enable_sys_route_notifs(void)
479{
480 zdplane_info.dg_sys_route_notifs = true;
481}
482
7cdb1a84 483/*
f73a8467 484 * Clean up dependent/internal allocations inside a context object
7cdb1a84 485 */
f73a8467 486static void dplane_ctx_free_internal(struct zebra_dplane_ctx *ctx)
7cdb1a84 487{
f73a8467
MS
488 /*
489 * Some internal allocations may need to be freed, depending on
16c628de
MS
490 * the type of info captured in the ctx.
491 */
f73a8467 492 switch (ctx->zd_op) {
16c628de
MS
493 case DPLANE_OP_ROUTE_INSTALL:
494 case DPLANE_OP_ROUTE_UPDATE:
495 case DPLANE_OP_ROUTE_DELETE:
cf363e1b
MS
496 case DPLANE_OP_SYS_ROUTE_ADD:
497 case DPLANE_OP_SYS_ROUTE_DELETE:
54818e3b 498 case DPLANE_OP_ROUTE_NOTIFY:
b8e0423d 499
16c628de 500 /* Free allocated nexthops */
f73a8467 501 if (ctx->u.rinfo.zd_ng.nexthop) {
7cdb1a84 502 /* This deals with recursive nexthops too */
f73a8467 503 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
16c628de 504
f73a8467 505 ctx->u.rinfo.zd_ng.nexthop = NULL;
7cdb1a84
MS
506 }
507
1d48702e 508 /* Free backup info also (if present) */
f73a8467 509 if (ctx->u.rinfo.backup_ng.nexthop) {
1d48702e 510 /* This deals with recursive nexthops too */
f73a8467 511 nexthops_free(ctx->u.rinfo.backup_ng.nexthop);
1d48702e 512
f73a8467 513 ctx->u.rinfo.backup_ng.nexthop = NULL;
1d48702e
MS
514 }
515
f73a8467 516 if (ctx->u.rinfo.zd_old_ng.nexthop) {
4dfd7a02 517 /* This deals with recursive nexthops too */
f73a8467 518 nexthops_free(ctx->u.rinfo.zd_old_ng.nexthop);
16c628de 519
f73a8467 520 ctx->u.rinfo.zd_old_ng.nexthop = NULL;
16c628de
MS
521 }
522
f73a8467 523 if (ctx->u.rinfo.old_backup_ng.nexthop) {
1d48702e 524 /* This deals with recursive nexthops too */
f73a8467 525 nexthops_free(ctx->u.rinfo.old_backup_ng.nexthop);
1d48702e 526
f73a8467 527 ctx->u.rinfo.old_backup_ng.nexthop = NULL;
1d48702e
MS
528 }
529
16c628de
MS
530 break;
531
f820d025
SW
532 case DPLANE_OP_NH_INSTALL:
533 case DPLANE_OP_NH_UPDATE:
534 case DPLANE_OP_NH_DELETE: {
f73a8467 535 if (ctx->u.rinfo.nhe.ng.nexthop) {
0c8215cb 536 /* This deals with recursive nexthops too */
f73a8467 537 nexthops_free(ctx->u.rinfo.nhe.ng.nexthop);
0c8215cb 538
f73a8467 539 ctx->u.rinfo.nhe.ng.nexthop = NULL;
0c8215cb 540 }
f820d025
SW
541 break;
542 }
543
16c628de
MS
544 case DPLANE_OP_LSP_INSTALL:
545 case DPLANE_OP_LSP_UPDATE:
546 case DPLANE_OP_LSP_DELETE:
104e3ad9 547 case DPLANE_OP_LSP_NOTIFY:
16c628de 548 {
ee70f629 549 zebra_nhlfe_t *nhlfe;
16c628de 550
cd4bb96f
MS
551 /* Unlink and free allocated NHLFEs */
552 frr_each_safe(nhlfe_list, &ctx->u.lsp.nhlfe_list, nhlfe) {
553 nhlfe_list_del(&ctx->u.lsp.nhlfe_list, nhlfe);
554 zebra_mpls_nhlfe_free(nhlfe);
555 }
556
557 /* Unlink and free allocated backup NHLFEs, if present */
558 frr_each_safe(nhlfe_list,
559 &(ctx->u.lsp.backup_nhlfe_list), nhlfe) {
560 nhlfe_list_del(&ctx->u.lsp.backup_nhlfe_list,
561 nhlfe);
562 zebra_mpls_nhlfe_free(nhlfe);
563 }
01ce7cba 564
cd4bb96f 565 /* Clear pointers in lsp struct, in case we're caching
16c628de
MS
566 * free context structs.
567 */
ee70f629 568 nhlfe_list_init(&ctx->u.lsp.nhlfe_list);
f73a8467 569 ctx->u.lsp.best_nhlfe = NULL;
cd4bb96f 570 nhlfe_list_init(&ctx->u.lsp.backup_nhlfe_list);
16c628de
MS
571
572 break;
573 }
574
97d8d05a
MS
575 case DPLANE_OP_PW_INSTALL:
576 case DPLANE_OP_PW_UNINSTALL:
16d69787 577 /* Free allocated nexthops */
f73a8467 578 if (ctx->u.pw.nhg.nexthop) {
16d69787 579 /* This deals with recursive nexthops too */
f73a8467 580 nexthops_free(ctx->u.pw.nhg.nexthop);
16d69787 581
f73a8467 582 ctx->u.pw.nhg.nexthop = NULL;
16d69787
MS
583 }
584 break;
585
a4a4802a
MS
586 case DPLANE_OP_ADDR_INSTALL:
587 case DPLANE_OP_ADDR_UNINSTALL:
64168803 588 /* Maybe free label string, if allocated */
f73a8467
MS
589 if (ctx->u.intf.label != NULL &&
590 ctx->u.intf.label != ctx->u.intf.label_buf) {
591 free(ctx->u.intf.label);
592 ctx->u.intf.label = NULL;
64168803
MS
593 }
594 break;
595
7597ac7b
MS
596 case DPLANE_OP_MAC_INSTALL:
597 case DPLANE_OP_MAC_DELETE:
f2412b2d 598 case DPLANE_OP_NEIGH_INSTALL:
931fa60c 599 case DPLANE_OP_NEIGH_UPDATE:
f2412b2d 600 case DPLANE_OP_NEIGH_DELETE:
0bbd4ff4
MS
601 case DPLANE_OP_VTEP_ADD:
602 case DPLANE_OP_VTEP_DELETE:
60d8d43b
JU
603 case DPLANE_OP_RULE_ADD:
604 case DPLANE_OP_RULE_DELETE:
605 case DPLANE_OP_RULE_UPDATE:
16c628de
MS
606 case DPLANE_OP_NONE:
607 break;
7cdb1a84 608 }
f73a8467
MS
609}
610
611/*
612 * Free a dataplane results context.
613 */
614static void dplane_ctx_free(struct zebra_dplane_ctx **pctx)
615{
616 if (pctx == NULL)
617 return;
618
619 DPLANE_CTX_VALID(*pctx);
620
621 /* TODO -- just freeing memory, but would like to maintain
622 * a pool
623 */
624
625 /* Some internal allocations may need to be freed, depending on
626 * the type of info captured in the ctx.
627 */
628 dplane_ctx_free_internal(*pctx);
16c628de
MS
629
630 XFREE(MTYPE_DP_CTX, *pctx);
7cdb1a84
MS
631}
632
f73a8467
MS
633/*
634 * Reset an allocated context object for re-use. All internal allocations are
635 * freed and the context is memset.
636 */
637void dplane_ctx_reset(struct zebra_dplane_ctx *ctx)
638{
639 dplane_ctx_free_internal(ctx);
640 memset(ctx, 0, sizeof(*ctx));
641}
642
7cdb1a84
MS
643/*
644 * Return a context block to the dplane module after processing
645 */
25779064 646void dplane_ctx_fini(struct zebra_dplane_ctx **pctx)
7cdb1a84 647{
14b0bc8e 648 /* TODO -- maintain pool; for now, just free */
7cdb1a84
MS
649 dplane_ctx_free(pctx);
650}
651
652/* Enqueue a context block */
25779064
MS
653void dplane_ctx_enqueue_tail(struct dplane_ctx_q *q,
654 const struct zebra_dplane_ctx *ctx)
7cdb1a84 655{
25779064 656 TAILQ_INSERT_TAIL(q, (struct zebra_dplane_ctx *)ctx, zd_q_entries);
7cdb1a84
MS
657}
658
14b0bc8e
MS
659/* Append a list of context blocks to another list */
660void dplane_ctx_list_append(struct dplane_ctx_q *to_list,
661 struct dplane_ctx_q *from_list)
662{
663 if (TAILQ_FIRST(from_list)) {
664 TAILQ_CONCAT(to_list, from_list, zd_q_entries);
665
666 /* And clear 'from' list */
667 TAILQ_INIT(from_list);
668 }
669}
670
7cdb1a84 671/* Dequeue a context block from the head of a list */
68b375e0 672struct zebra_dplane_ctx *dplane_ctx_dequeue(struct dplane_ctx_q *q)
7cdb1a84 673{
25779064 674 struct zebra_dplane_ctx *ctx = TAILQ_FIRST(q);
5709131c
MS
675
676 if (ctx)
7cdb1a84 677 TAILQ_REMOVE(q, ctx, zd_q_entries);
7cdb1a84 678
68b375e0 679 return ctx;
7cdb1a84
MS
680}
681
682/*
683 * Accessors for information from the context object
684 */
25779064
MS
685enum zebra_dplane_result dplane_ctx_get_status(
686 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
687{
688 DPLANE_CTX_VALID(ctx);
689
5709131c 690 return ctx->zd_status;
7cdb1a84
MS
691}
692
c831033f
MS
693void dplane_ctx_set_status(struct zebra_dplane_ctx *ctx,
694 enum zebra_dplane_result status)
695{
696 DPLANE_CTX_VALID(ctx);
697
698 ctx->zd_status = status;
699}
700
701/* Retrieve last/current provider id */
702uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx *ctx)
703{
704 DPLANE_CTX_VALID(ctx);
705 return ctx->zd_provider;
706}
707
708/* Providers run before the kernel can control whether a kernel
709 * update should be done.
710 */
711void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx *ctx)
712{
713 DPLANE_CTX_VALID(ctx);
714
715 SET_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
716}
717
718bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx *ctx)
719{
720 DPLANE_CTX_VALID(ctx);
721
722 return CHECK_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
723}
724
593e4eb1
MS
725void dplane_ctx_set_op(struct zebra_dplane_ctx *ctx, enum dplane_op_e op)
726{
727 DPLANE_CTX_VALID(ctx);
728 ctx->zd_op = op;
729}
730
25779064 731enum dplane_op_e dplane_ctx_get_op(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
732{
733 DPLANE_CTX_VALID(ctx);
734
5709131c 735 return ctx->zd_op;
7cdb1a84
MS
736}
737
5709131c 738const char *dplane_op2str(enum dplane_op_e op)
7cdb1a84
MS
739{
740 const char *ret = "UNKNOWN";
741
5709131c 742 switch (op) {
7cdb1a84
MS
743 case DPLANE_OP_NONE:
744 ret = "NONE";
745 break;
746
747 /* Route update */
748 case DPLANE_OP_ROUTE_INSTALL:
749 ret = "ROUTE_INSTALL";
750 break;
751 case DPLANE_OP_ROUTE_UPDATE:
752 ret = "ROUTE_UPDATE";
753 break;
754 case DPLANE_OP_ROUTE_DELETE:
755 ret = "ROUTE_DELETE";
756 break;
54818e3b
MS
757 case DPLANE_OP_ROUTE_NOTIFY:
758 ret = "ROUTE_NOTIFY";
759 break;
7cdb1a84 760
f820d025
SW
761 /* Nexthop update */
762 case DPLANE_OP_NH_INSTALL:
763 ret = "NH_INSTALL";
764 break;
765 case DPLANE_OP_NH_UPDATE:
766 ret = "NH_UPDATE";
767 break;
768 case DPLANE_OP_NH_DELETE:
769 ret = "NH_DELETE";
770 break;
771
16c628de
MS
772 case DPLANE_OP_LSP_INSTALL:
773 ret = "LSP_INSTALL";
774 break;
775 case DPLANE_OP_LSP_UPDATE:
776 ret = "LSP_UPDATE";
777 break;
778 case DPLANE_OP_LSP_DELETE:
779 ret = "LSP_DELETE";
780 break;
104e3ad9
MS
781 case DPLANE_OP_LSP_NOTIFY:
782 ret = "LSP_NOTIFY";
783 break;
16c628de 784
97d8d05a
MS
785 case DPLANE_OP_PW_INSTALL:
786 ret = "PW_INSTALL";
787 break;
788 case DPLANE_OP_PW_UNINSTALL:
789 ret = "PW_UNINSTALL";
790 break;
791
cf363e1b
MS
792 case DPLANE_OP_SYS_ROUTE_ADD:
793 ret = "SYS_ROUTE_ADD";
794 break;
795 case DPLANE_OP_SYS_ROUTE_DELETE:
796 ret = "SYS_ROUTE_DEL";
797 break;
a4a4802a
MS
798
799 case DPLANE_OP_ADDR_INSTALL:
800 ret = "ADDR_INSTALL";
801 break;
802 case DPLANE_OP_ADDR_UNINSTALL:
803 ret = "ADDR_UNINSTALL";
804 break;
805
7597ac7b
MS
806 case DPLANE_OP_MAC_INSTALL:
807 ret = "MAC_INSTALL";
808 break;
809 case DPLANE_OP_MAC_DELETE:
810 ret = "MAC_DELETE";
811 break;
f2412b2d
MS
812
813 case DPLANE_OP_NEIGH_INSTALL:
814 ret = "NEIGH_INSTALL";
815 break;
931fa60c
MS
816 case DPLANE_OP_NEIGH_UPDATE:
817 ret = "NEIGH_UPDATE";
818 break;
f2412b2d
MS
819 case DPLANE_OP_NEIGH_DELETE:
820 ret = "NEIGH_DELETE";
821 break;
0bbd4ff4
MS
822 case DPLANE_OP_VTEP_ADD:
823 ret = "VTEP_ADD";
824 break;
825 case DPLANE_OP_VTEP_DELETE:
826 ret = "VTEP_DELETE";
827 break;
60d8d43b
JU
828
829 case DPLANE_OP_RULE_ADD:
830 ret = "RULE_ADD";
831 break;
832 case DPLANE_OP_RULE_DELETE:
833 ret = "RULE_DELETE";
834 break;
835 case DPLANE_OP_RULE_UPDATE:
836 ret = "RULE_UPDATE";
837 break;
5b94ec50 838 }
7cdb1a84 839
5709131c 840 return ret;
7cdb1a84
MS
841}
842
f183e380
MS
843const char *dplane_res2str(enum zebra_dplane_result res)
844{
845 const char *ret = "<Unknown>";
846
847 switch (res) {
848 case ZEBRA_DPLANE_REQUEST_FAILURE:
849 ret = "FAILURE";
850 break;
851 case ZEBRA_DPLANE_REQUEST_QUEUED:
852 ret = "QUEUED";
853 break;
854 case ZEBRA_DPLANE_REQUEST_SUCCESS:
855 ret = "SUCCESS";
856 break;
2f74a82a
JU
857 case ZEBRA_DPLANE_REQUEST_PENDING:
858 ret = "PENDING";
5b94ec50 859 }
f183e380
MS
860
861 return ret;
862}
863
593e4eb1
MS
864void dplane_ctx_set_dest(struct zebra_dplane_ctx *ctx,
865 const struct prefix *dest)
866{
867 DPLANE_CTX_VALID(ctx);
868
869 prefix_copy(&(ctx->u.rinfo.zd_dest), dest);
870}
871
25779064 872const struct prefix *dplane_ctx_get_dest(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
873{
874 DPLANE_CTX_VALID(ctx);
875
0f461727 876 return &(ctx->u.rinfo.zd_dest);
7cdb1a84
MS
877}
878
593e4eb1
MS
879void dplane_ctx_set_src(struct zebra_dplane_ctx *ctx, const struct prefix *src)
880{
881 DPLANE_CTX_VALID(ctx);
882
883 if (src)
884 prefix_copy(&(ctx->u.rinfo.zd_src), src);
885 else
886 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(struct prefix));
887}
888
5709131c 889/* Source prefix is a little special - return NULL for "no src prefix" */
25779064 890const struct prefix *dplane_ctx_get_src(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
891{
892 DPLANE_CTX_VALID(ctx);
893
0f461727
MS
894 if (ctx->u.rinfo.zd_src.prefixlen == 0 &&
895 IN6_IS_ADDR_UNSPECIFIED(&(ctx->u.rinfo.zd_src.u.prefix6))) {
5709131c 896 return NULL;
7cdb1a84 897 } else {
0f461727 898 return &(ctx->u.rinfo.zd_src);
7cdb1a84
MS
899 }
900}
901
25779064 902bool dplane_ctx_is_update(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
903{
904 DPLANE_CTX_VALID(ctx);
905
5709131c 906 return ctx->zd_is_update;
7cdb1a84
MS
907}
908
25779064 909uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
910{
911 DPLANE_CTX_VALID(ctx);
912
5709131c 913 return ctx->zd_seq;
7cdb1a84
MS
914}
915
25779064 916uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
917{
918 DPLANE_CTX_VALID(ctx);
919
5709131c 920 return ctx->zd_old_seq;
7cdb1a84
MS
921}
922
593e4eb1
MS
923void dplane_ctx_set_vrf(struct zebra_dplane_ctx *ctx, vrf_id_t vrf)
924{
925 DPLANE_CTX_VALID(ctx);
926
927 ctx->zd_vrf_id = vrf;
928}
929
25779064 930vrf_id_t dplane_ctx_get_vrf(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
931{
932 DPLANE_CTX_VALID(ctx);
933
5709131c 934 return ctx->zd_vrf_id;
7cdb1a84
MS
935}
936
0024a559
MS
937bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx *ctx)
938{
939 DPLANE_CTX_VALID(ctx);
940
941 return (ctx->zd_notif_provider != 0);
942}
943
944uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx *ctx)
945{
946 DPLANE_CTX_VALID(ctx);
947
948 return ctx->zd_notif_provider;
949}
950
9651af61
MS
951void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx *ctx,
952 uint32_t id)
953{
954 DPLANE_CTX_VALID(ctx);
955
956 ctx->zd_notif_provider = id;
957}
fd563cc7 958
7c7ef4a8
MS
959const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx *ctx)
960{
961 DPLANE_CTX_VALID(ctx);
962
963 return ctx->zd_ifname;
964}
965
fd563cc7
KS
966void dplane_ctx_set_ifname(struct zebra_dplane_ctx *ctx, const char *ifname)
967{
968 DPLANE_CTX_VALID(ctx);
969
970 if (!ifname)
971 return;
972
973 strlcpy(ctx->zd_ifname, ifname, sizeof(ctx->zd_ifname));
974}
975
7c7ef4a8
MS
976ifindex_t dplane_ctx_get_ifindex(const struct zebra_dplane_ctx *ctx)
977{
978 DPLANE_CTX_VALID(ctx);
979
980 return ctx->zd_ifindex;
981}
9651af61 982
593e4eb1
MS
983void dplane_ctx_set_type(struct zebra_dplane_ctx *ctx, int type)
984{
985 DPLANE_CTX_VALID(ctx);
986
987 ctx->u.rinfo.zd_type = type;
988}
989
25779064 990int dplane_ctx_get_type(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
991{
992 DPLANE_CTX_VALID(ctx);
993
0f461727 994 return ctx->u.rinfo.zd_type;
7cdb1a84
MS
995}
996
25779064 997int dplane_ctx_get_old_type(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
998{
999 DPLANE_CTX_VALID(ctx);
1000
0f461727 1001 return ctx->u.rinfo.zd_old_type;
7cdb1a84
MS
1002}
1003
593e4eb1
MS
1004void dplane_ctx_set_afi(struct zebra_dplane_ctx *ctx, afi_t afi)
1005{
1006 DPLANE_CTX_VALID(ctx);
1007
1008 ctx->u.rinfo.zd_afi = afi;
1009}
1010
25779064 1011afi_t dplane_ctx_get_afi(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1012{
1013 DPLANE_CTX_VALID(ctx);
1014
0f461727 1015 return ctx->u.rinfo.zd_afi;
7cdb1a84
MS
1016}
1017
593e4eb1
MS
1018void dplane_ctx_set_safi(struct zebra_dplane_ctx *ctx, safi_t safi)
1019{
1020 DPLANE_CTX_VALID(ctx);
1021
1022 ctx->u.rinfo.zd_safi = safi;
1023}
1024
25779064 1025safi_t dplane_ctx_get_safi(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1026{
1027 DPLANE_CTX_VALID(ctx);
1028
0f461727 1029 return ctx->u.rinfo.zd_safi;
7cdb1a84
MS
1030}
1031
593e4eb1
MS
1032void dplane_ctx_set_table(struct zebra_dplane_ctx *ctx, uint32_t table)
1033{
1034 DPLANE_CTX_VALID(ctx);
1035
1036 ctx->zd_table_id = table;
1037}
1038
25779064 1039uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1040{
1041 DPLANE_CTX_VALID(ctx);
1042
5709131c 1043 return ctx->zd_table_id;
7cdb1a84
MS
1044}
1045
25779064 1046route_tag_t dplane_ctx_get_tag(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1047{
1048 DPLANE_CTX_VALID(ctx);
1049
0f461727 1050 return ctx->u.rinfo.zd_tag;
7cdb1a84
MS
1051}
1052
6a91ae98
MS
1053void dplane_ctx_set_tag(struct zebra_dplane_ctx *ctx, route_tag_t tag)
1054{
1055 DPLANE_CTX_VALID(ctx);
1056
1057 ctx->u.rinfo.zd_tag = tag;
1058}
1059
25779064 1060route_tag_t dplane_ctx_get_old_tag(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1061{
1062 DPLANE_CTX_VALID(ctx);
1063
0f461727 1064 return ctx->u.rinfo.zd_old_tag;
7cdb1a84
MS
1065}
1066
25779064 1067uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1068{
1069 DPLANE_CTX_VALID(ctx);
1070
0f461727 1071 return ctx->u.rinfo.zd_instance;
7cdb1a84
MS
1072}
1073
6a91ae98
MS
1074void dplane_ctx_set_instance(struct zebra_dplane_ctx *ctx, uint16_t instance)
1075{
1076 DPLANE_CTX_VALID(ctx);
1077
1078 ctx->u.rinfo.zd_instance = instance;
1079}
1080
25779064 1081uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1082{
1083 DPLANE_CTX_VALID(ctx);
1084
0f461727 1085 return ctx->u.rinfo.zd_old_instance;
7cdb1a84
MS
1086}
1087
25779064 1088uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1089{
1090 DPLANE_CTX_VALID(ctx);
1091
0f461727 1092 return ctx->u.rinfo.zd_metric;
7cdb1a84
MS
1093}
1094
25779064 1095uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx *ctx)
01ce7cba
MS
1096{
1097 DPLANE_CTX_VALID(ctx);
1098
0f461727 1099 return ctx->u.rinfo.zd_old_metric;
01ce7cba
MS
1100}
1101
25779064 1102uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1103{
1104 DPLANE_CTX_VALID(ctx);
1105
0f461727 1106 return ctx->u.rinfo.zd_mtu;
7cdb1a84
MS
1107}
1108
25779064 1109uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1110{
1111 DPLANE_CTX_VALID(ctx);
1112
0f461727 1113 return ctx->u.rinfo.zd_nexthop_mtu;
7cdb1a84
MS
1114}
1115
25779064 1116uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1117{
1118 DPLANE_CTX_VALID(ctx);
1119
0f461727 1120 return ctx->u.rinfo.zd_distance;
7cdb1a84
MS
1121}
1122
6a91ae98
MS
1123void dplane_ctx_set_distance(struct zebra_dplane_ctx *ctx, uint8_t distance)
1124{
1125 DPLANE_CTX_VALID(ctx);
1126
1127 ctx->u.rinfo.zd_distance = distance;
1128}
1129
25779064 1130uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1131{
1132 DPLANE_CTX_VALID(ctx);
1133
0f461727 1134 return ctx->u.rinfo.zd_old_distance;
7cdb1a84
MS
1135}
1136
e1f3a8eb
MS
1137/*
1138 * Set the nexthops associated with a context: note that processing code
1139 * may well expect that nexthops are in canonical (sorted) order, so we
1140 * will enforce that here.
1141 */
593e4eb1
MS
1142void dplane_ctx_set_nexthops(struct zebra_dplane_ctx *ctx, struct nexthop *nh)
1143{
1144 DPLANE_CTX_VALID(ctx);
1145
1146 if (ctx->u.rinfo.zd_ng.nexthop) {
1147 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
1148 ctx->u.rinfo.zd_ng.nexthop = NULL;
1149 }
e1f3a8eb 1150 nexthop_group_copy_nh_sorted(&(ctx->u.rinfo.zd_ng), nh);
593e4eb1
MS
1151}
1152
928f94a9
MS
1153/*
1154 * Set the list of backup nexthops; their ordering is preserved (they're not
1155 * re-sorted.)
1156 */
1157void dplane_ctx_set_backup_nhg(struct zebra_dplane_ctx *ctx,
1158 const struct nexthop_group *nhg)
1159{
1160 struct nexthop *nh, *last_nh, *nexthop;
1161
1162 DPLANE_CTX_VALID(ctx);
1163
1164 if (ctx->u.rinfo.backup_ng.nexthop) {
1165 nexthops_free(ctx->u.rinfo.backup_ng.nexthop);
1166 ctx->u.rinfo.backup_ng.nexthop = NULL;
1167 }
1168
1169 last_nh = NULL;
1170
1171 /* Be careful to preserve the order of the backup list */
1172 for (nh = nhg->nexthop; nh; nh = nh->next) {
1173 nexthop = nexthop_dup(nh, NULL);
1174
1175 if (last_nh)
1176 NEXTHOP_APPEND(last_nh, nexthop);
1177 else
1178 ctx->u.rinfo.backup_ng.nexthop = nexthop;
1179
1180 last_nh = nexthop;
1181 }
1182}
1183
1d48702e
MS
1184uint32_t dplane_ctx_get_nhg_id(const struct zebra_dplane_ctx *ctx)
1185{
1186 DPLANE_CTX_VALID(ctx);
1187 return ctx->u.rinfo.zd_nhg_id;
1188}
1189
25779064
MS
1190const struct nexthop_group *dplane_ctx_get_ng(
1191 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1192{
1193 DPLANE_CTX_VALID(ctx);
1194
0f461727 1195 return &(ctx->u.rinfo.zd_ng);
7cdb1a84
MS
1196}
1197
1d48702e
MS
1198const struct nexthop_group *
1199dplane_ctx_get_backup_ng(const struct zebra_dplane_ctx *ctx)
1200{
1201 DPLANE_CTX_VALID(ctx);
1202
1203 return &(ctx->u.rinfo.backup_ng);
1204}
1205
1206const struct nexthop_group *
1207dplane_ctx_get_old_ng(const struct zebra_dplane_ctx *ctx)
01ce7cba
MS
1208{
1209 DPLANE_CTX_VALID(ctx);
1210
0f461727 1211 return &(ctx->u.rinfo.zd_old_ng);
01ce7cba
MS
1212}
1213
1d48702e
MS
1214const struct nexthop_group *
1215dplane_ctx_get_old_backup_ng(const struct zebra_dplane_ctx *ctx)
1216{
1217 DPLANE_CTX_VALID(ctx);
1218
1219 return &(ctx->u.rinfo.old_backup_ng);
1220}
1221
25779064
MS
1222const struct zebra_dplane_info *dplane_ctx_get_ns(
1223 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1224{
1225 DPLANE_CTX_VALID(ctx);
1226
5709131c 1227 return &(ctx->zd_ns_info);
7cdb1a84
MS
1228}
1229
f820d025 1230/* Accessors for nexthop information */
0c8215cb
SW
1231uint32_t dplane_ctx_get_nhe_id(const struct zebra_dplane_ctx *ctx)
1232{
1233 DPLANE_CTX_VALID(ctx);
1234 return ctx->u.rinfo.nhe.id;
1235}
1236
1237afi_t dplane_ctx_get_nhe_afi(const struct zebra_dplane_ctx *ctx)
1238{
1239 DPLANE_CTX_VALID(ctx);
1240 return ctx->u.rinfo.nhe.afi;
1241}
1242
1243vrf_id_t dplane_ctx_get_nhe_vrf_id(const struct zebra_dplane_ctx *ctx)
f820d025
SW
1244{
1245 DPLANE_CTX_VALID(ctx);
0c8215cb
SW
1246 return ctx->u.rinfo.nhe.vrf_id;
1247}
1248
38e40db1 1249int dplane_ctx_get_nhe_type(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1250{
1251 DPLANE_CTX_VALID(ctx);
38e40db1 1252 return ctx->u.rinfo.nhe.type;
0c8215cb
SW
1253}
1254
1255const struct nexthop_group *
1256dplane_ctx_get_nhe_ng(const struct zebra_dplane_ctx *ctx)
1257{
1258 DPLANE_CTX_VALID(ctx);
1259 return &(ctx->u.rinfo.nhe.ng);
1260}
1261
e22e8001
SW
1262const struct nh_grp *
1263dplane_ctx_get_nhe_nh_grp(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1264{
1265 DPLANE_CTX_VALID(ctx);
e22e8001 1266 return ctx->u.rinfo.nhe.nh_grp;
0c8215cb
SW
1267}
1268
e22e8001 1269uint8_t dplane_ctx_get_nhe_nh_grp_count(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1270{
1271 DPLANE_CTX_VALID(ctx);
e22e8001 1272 return ctx->u.rinfo.nhe.nh_grp_count;
f820d025
SW
1273}
1274
0f461727
MS
1275/* Accessors for LSP information */
1276
1277mpls_label_t dplane_ctx_get_in_label(const struct zebra_dplane_ctx *ctx)
1278{
1279 DPLANE_CTX_VALID(ctx);
1280
1281 return ctx->u.lsp.ile.in_label;
1282}
1283
3ab54059
MS
1284void dplane_ctx_set_in_label(struct zebra_dplane_ctx *ctx, mpls_label_t label)
1285{
1286 DPLANE_CTX_VALID(ctx);
1287
1288 ctx->u.lsp.ile.in_label = label;
1289}
1290
0f461727
MS
1291uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx *ctx)
1292{
1293 DPLANE_CTX_VALID(ctx);
1294
1295 return ctx->u.lsp.addr_family;
1296}
1297
3ab54059
MS
1298void dplane_ctx_set_addr_family(struct zebra_dplane_ctx *ctx,
1299 uint8_t family)
1300{
1301 DPLANE_CTX_VALID(ctx);
1302
1303 ctx->u.lsp.addr_family = family;
1304}
1305
0f461727
MS
1306uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx *ctx)
1307{
1308 DPLANE_CTX_VALID(ctx);
1309
1310 return ctx->u.lsp.flags;
1311}
1312
3ab54059
MS
1313void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx *ctx,
1314 uint32_t flags)
1315{
1316 DPLANE_CTX_VALID(ctx);
1317
1318 ctx->u.lsp.flags = flags;
1319}
1320
ee70f629
MS
1321const struct nhlfe_list_head *dplane_ctx_get_nhlfe_list(
1322 const struct zebra_dplane_ctx *ctx)
0f461727
MS
1323{
1324 DPLANE_CTX_VALID(ctx);
ee70f629 1325 return &(ctx->u.lsp.nhlfe_list);
0f461727
MS
1326}
1327
cd4bb96f
MS
1328const struct nhlfe_list_head *dplane_ctx_get_backup_nhlfe_list(
1329 const struct zebra_dplane_ctx *ctx)
1330{
1331 DPLANE_CTX_VALID(ctx);
1332 return &(ctx->u.lsp.backup_nhlfe_list);
1333}
1334
3ab54059
MS
1335zebra_nhlfe_t *dplane_ctx_add_nhlfe(struct zebra_dplane_ctx *ctx,
1336 enum lsp_types_t lsp_type,
1337 enum nexthop_types_t nh_type,
963a9803 1338 const union g_addr *gate,
3ab54059 1339 ifindex_t ifindex,
5065db0a 1340 uint8_t num_labels,
ee70f629 1341 mpls_label_t *out_labels)
3ab54059
MS
1342{
1343 zebra_nhlfe_t *nhlfe;
1344
1345 DPLANE_CTX_VALID(ctx);
1346
1347 nhlfe = zebra_mpls_lsp_add_nhlfe(&(ctx->u.lsp),
1348 lsp_type, nh_type, gate,
5065db0a 1349 ifindex, num_labels, out_labels);
3ab54059
MS
1350
1351 return nhlfe;
1352}
1353
cd4bb96f
MS
1354zebra_nhlfe_t *dplane_ctx_add_backup_nhlfe(struct zebra_dplane_ctx *ctx,
1355 enum lsp_types_t lsp_type,
1356 enum nexthop_types_t nh_type,
963a9803 1357 const union g_addr *gate,
cd4bb96f
MS
1358 ifindex_t ifindex,
1359 uint8_t num_labels,
1360 mpls_label_t *out_labels)
1361{
1362 zebra_nhlfe_t *nhlfe;
1363
1364 DPLANE_CTX_VALID(ctx);
1365
1366 nhlfe = zebra_mpls_lsp_add_backup_nhlfe(&(ctx->u.lsp),
1367 lsp_type, nh_type, gate,
1368 ifindex, num_labels,
1369 out_labels);
1370
1371 return nhlfe;
1372}
1373
81793ac1
MS
1374const zebra_nhlfe_t *
1375dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx *ctx)
0f461727
MS
1376{
1377 DPLANE_CTX_VALID(ctx);
1378
1379 return ctx->u.lsp.best_nhlfe;
1380}
1381
3ab54059
MS
1382const zebra_nhlfe_t *
1383dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx *ctx,
1384 zebra_nhlfe_t *nhlfe)
1385{
1386 DPLANE_CTX_VALID(ctx);
1387
1388 ctx->u.lsp.best_nhlfe = nhlfe;
1389 return ctx->u.lsp.best_nhlfe;
1390}
1391
0f461727
MS
1392uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx *ctx)
1393{
1394 DPLANE_CTX_VALID(ctx);
1395
1396 return ctx->u.lsp.num_ecmp;
1397}
1398
d613b8e1
MS
1399mpls_label_t dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx *ctx)
1400{
1401 DPLANE_CTX_VALID(ctx);
1402
1403 return ctx->u.pw.local_label;
1404}
1405
1406mpls_label_t dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx *ctx)
1407{
1408 DPLANE_CTX_VALID(ctx);
1409
1410 return ctx->u.pw.remote_label;
1411}
1412
1413int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx *ctx)
1414{
1415 DPLANE_CTX_VALID(ctx);
1416
1417 return ctx->u.pw.type;
1418}
1419
1420int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx *ctx)
1421{
1422 DPLANE_CTX_VALID(ctx);
1423
1424 return ctx->u.pw.af;
1425}
1426
1427uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx *ctx)
1428{
1429 DPLANE_CTX_VALID(ctx);
1430
1431 return ctx->u.pw.flags;
1432}
1433
1434int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx *ctx)
1435{
1436 DPLANE_CTX_VALID(ctx);
1437
1438 return ctx->u.pw.status;
1439}
1440
fd563cc7
KS
1441void dplane_ctx_set_pw_status(struct zebra_dplane_ctx *ctx, int status)
1442{
1443 DPLANE_CTX_VALID(ctx);
1444
1445 ctx->u.pw.status = status;
1446}
1447
16d69787 1448const union g_addr *dplane_ctx_get_pw_dest(
d613b8e1
MS
1449 const struct zebra_dplane_ctx *ctx)
1450{
1451 DPLANE_CTX_VALID(ctx);
1452
16d69787 1453 return &(ctx->u.pw.dest);
d613b8e1
MS
1454}
1455
1456const union pw_protocol_fields *dplane_ctx_get_pw_proto(
1457 const struct zebra_dplane_ctx *ctx)
1458{
1459 DPLANE_CTX_VALID(ctx);
1460
1461 return &(ctx->u.pw.fields);
1462}
1463
09cd307c
MS
1464const struct nexthop_group *
1465dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx *ctx)
1466{
1467 DPLANE_CTX_VALID(ctx);
1468
1469 return &(ctx->u.pw.nhg);
1470}
1471
a4a4802a 1472/* Accessors for interface information */
a4a4802a
MS
1473uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx *ctx)
1474{
1475 DPLANE_CTX_VALID(ctx);
1476
1477 return ctx->u.intf.metric;
1478}
1479
1480/* Is interface addr p2p? */
1481bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx *ctx)
1482{
1483 DPLANE_CTX_VALID(ctx);
1484
1485 return (ctx->u.intf.flags & DPLANE_INTF_CONNECTED);
1486}
1487
1488bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx *ctx)
1489{
1490 DPLANE_CTX_VALID(ctx);
1491
1492 return (ctx->u.intf.flags & DPLANE_INTF_SECONDARY);
1493}
1494
64168803
MS
1495bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx *ctx)
1496{
1497 DPLANE_CTX_VALID(ctx);
1498
1499 return (ctx->u.intf.flags & DPLANE_INTF_BROADCAST);
1500}
1501
a4a4802a
MS
1502const struct prefix *dplane_ctx_get_intf_addr(
1503 const struct zebra_dplane_ctx *ctx)
1504{
1505 DPLANE_CTX_VALID(ctx);
1506
1507 return &(ctx->u.intf.prefix);
1508}
1509
1510bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx *ctx)
1511{
1512 DPLANE_CTX_VALID(ctx);
1513
1514 return (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST);
1515}
1516
1517const struct prefix *dplane_ctx_get_intf_dest(
1518 const struct zebra_dplane_ctx *ctx)
1519{
1520 DPLANE_CTX_VALID(ctx);
1521
1522 if (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST)
1523 return &(ctx->u.intf.dest_prefix);
1524 else
1525 return NULL;
1526}
1527
1528bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx *ctx)
1529{
1530 DPLANE_CTX_VALID(ctx);
1531
1532 return (ctx->u.intf.flags & DPLANE_INTF_HAS_LABEL);
1533}
1534
1535const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx *ctx)
1536{
1537 DPLANE_CTX_VALID(ctx);
1538
1539 return ctx->u.intf.label;
1540}
1541
7597ac7b
MS
1542/* Accessors for MAC information */
1543vlanid_t dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx *ctx)
1544{
1545 DPLANE_CTX_VALID(ctx);
1546 return ctx->u.macinfo.vid;
1547}
1548
1549bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx *ctx)
1550{
1551 DPLANE_CTX_VALID(ctx);
1552 return ctx->u.macinfo.is_sticky;
1553}
1554
1555const struct ethaddr *dplane_ctx_mac_get_addr(
1556 const struct zebra_dplane_ctx *ctx)
1557{
1558 DPLANE_CTX_VALID(ctx);
1559 return &(ctx->u.macinfo.mac);
1560}
1561
1562const struct in_addr *dplane_ctx_mac_get_vtep_ip(
1563 const struct zebra_dplane_ctx *ctx)
1564{
1565 DPLANE_CTX_VALID(ctx);
1566 return &(ctx->u.macinfo.vtep_ip);
1567}
1568
478566d6
MS
1569ifindex_t dplane_ctx_mac_get_br_ifindex(const struct zebra_dplane_ctx *ctx)
1570{
1571 DPLANE_CTX_VALID(ctx);
1572 return ctx->u.macinfo.br_ifindex;
1573}
1574
931fa60c
MS
1575/* Accessors for neighbor information */
1576const struct ipaddr *dplane_ctx_neigh_get_ipaddr(
1577 const struct zebra_dplane_ctx *ctx)
1578{
1579 DPLANE_CTX_VALID(ctx);
1580 return &(ctx->u.neigh.ip_addr);
1581}
1582
1583const struct ethaddr *dplane_ctx_neigh_get_mac(
1584 const struct zebra_dplane_ctx *ctx)
1585{
1586 DPLANE_CTX_VALID(ctx);
1587 return &(ctx->u.neigh.mac);
1588}
1589
1590uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx *ctx)
1591{
1592 DPLANE_CTX_VALID(ctx);
1593 return ctx->u.neigh.flags;
1594}
1595
1596uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx *ctx)
1597{
1598 DPLANE_CTX_VALID(ctx);
1599 return ctx->u.neigh.state;
1600}
1601
60d8d43b
JU
1602/* Accessors for PBR rule information */
1603int dplane_ctx_rule_get_sock(const struct zebra_dplane_ctx *ctx)
1604{
1605 DPLANE_CTX_VALID(ctx);
1606
1607 return ctx->u.rule.sock;
1608}
1609
1610int dplane_ctx_rule_get_unique(const struct zebra_dplane_ctx *ctx)
1611{
1612 DPLANE_CTX_VALID(ctx);
1613
1614 return ctx->u.rule.unique;
1615}
1616
1617int dplane_ctx_rule_get_seq(const struct zebra_dplane_ctx *ctx)
1618{
1619 DPLANE_CTX_VALID(ctx);
1620
1621 return ctx->u.rule.seq;
1622}
1623
1624uint32_t dplane_ctx_rule_get_priority(const struct zebra_dplane_ctx *ctx)
1625{
1626 DPLANE_CTX_VALID(ctx);
1627
1628 return ctx->u.rule.new.priority;
1629}
1630
1631uint32_t dplane_ctx_rule_get_old_priority(const struct zebra_dplane_ctx *ctx)
1632{
1633 DPLANE_CTX_VALID(ctx);
1634
1635 return ctx->u.rule.old.priority;
1636}
1637
1638uint32_t dplane_ctx_rule_get_table(const struct zebra_dplane_ctx *ctx)
1639{
1640 DPLANE_CTX_VALID(ctx);
1641
1642 return ctx->u.rule.new.table;
1643}
1644
1645uint32_t dplane_ctx_rule_get_old_table(const struct zebra_dplane_ctx *ctx)
1646{
1647 DPLANE_CTX_VALID(ctx);
1648
1649 return ctx->u.rule.old.table;
1650}
1651
1652uint32_t dplane_ctx_rule_get_filter_bm(const struct zebra_dplane_ctx *ctx)
1653{
1654 DPLANE_CTX_VALID(ctx);
1655
1656 return ctx->u.rule.new.filter_bm;
1657}
1658
1659uint32_t dplane_ctx_rule_get_old_filter_bm(const struct zebra_dplane_ctx *ctx)
1660{
1661 DPLANE_CTX_VALID(ctx);
1662
1663 return ctx->u.rule.old.filter_bm;
1664}
1665
1666uint32_t dplane_ctx_rule_get_fwmark(const struct zebra_dplane_ctx *ctx)
1667{
1668 DPLANE_CTX_VALID(ctx);
1669
1670 return ctx->u.rule.new.fwmark;
1671}
1672
1673uint32_t dplane_ctx_rule_get_old_fwmark(const struct zebra_dplane_ctx *ctx)
1674{
1675 DPLANE_CTX_VALID(ctx);
1676
1677 return ctx->u.rule.old.fwmark;
1678}
1679
01f23aff
WC
1680uint8_t dplane_ctx_rule_get_dsfield(const struct zebra_dplane_ctx *ctx)
1681{
1682 DPLANE_CTX_VALID(ctx);
1683
1684 return ctx->u.rule.new.dsfield;
1685}
1686
1687uint8_t dplane_ctx_rule_get_old_dsfield(const struct zebra_dplane_ctx *ctx)
1688{
1689 DPLANE_CTX_VALID(ctx);
1690
1691 return ctx->u.rule.old.dsfield;
1692}
1693
60d8d43b
JU
1694const struct prefix *
1695dplane_ctx_rule_get_src_ip(const struct zebra_dplane_ctx *ctx)
1696{
1697 DPLANE_CTX_VALID(ctx);
1698
1699 return &(ctx->u.rule.new.src_ip);
1700}
1701
1702const struct prefix *
1703dplane_ctx_rule_get_old_src_ip(const struct zebra_dplane_ctx *ctx)
1704{
1705 DPLANE_CTX_VALID(ctx);
1706
1707 return &(ctx->u.rule.old.src_ip);
1708}
1709
1710const struct prefix *
1711dplane_ctx_rule_get_dst_ip(const struct zebra_dplane_ctx *ctx)
1712{
1713 DPLANE_CTX_VALID(ctx);
1714
1715 return &(ctx->u.rule.new.dst_ip);
1716}
1717
1718const struct prefix *
1719dplane_ctx_rule_get_old_dst_ip(const struct zebra_dplane_ctx *ctx)
1720{
1721 DPLANE_CTX_VALID(ctx);
1722
1723 return &(ctx->u.rule.old.dst_ip);
1724}
1725
7cdb1a84
MS
1726/*
1727 * End of dplane context accessors
1728 */
1729
c831033f 1730
91f16812
MS
1731/*
1732 * Retrieve the limit on the number of pending, unprocessed updates.
1733 */
1734uint32_t dplane_get_in_queue_limit(void)
1735{
1736 return atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
1737 memory_order_relaxed);
1738}
1739
1740/*
1741 * Configure limit on the number of pending, queued updates.
1742 */
1743void dplane_set_in_queue_limit(uint32_t limit, bool set)
1744{
1745 /* Reset to default on 'unset' */
1746 if (!set)
1747 limit = DPLANE_DEFAULT_MAX_QUEUED;
1748
1749 atomic_store_explicit(&zdplane_info.dg_max_queued_updates, limit,
1750 memory_order_relaxed);
1751}
1752
1753/*
1754 * Retrieve the current queue depth of incoming, unprocessed updates
1755 */
1756uint32_t dplane_get_in_queue_len(void)
1757{
1758 return atomic_load_explicit(&zdplane_info.dg_routes_queued,
1759 memory_order_seq_cst);
1760}
1761
16c628de
MS
1762/*
1763 * Common dataplane context init with zebra namespace info.
1764 */
1765static int dplane_ctx_ns_init(struct zebra_dplane_ctx *ctx,
1766 struct zebra_ns *zns,
1767 bool is_update)
1768{
1769 dplane_info_from_zns(&(ctx->zd_ns_info), zns);
1770
1771#if defined(HAVE_NETLINK)
1772 /* Increment message counter after copying to context struct - may need
1773 * two messages in some 'update' cases.
1774 */
1775 if (is_update)
1776 zns->netlink_dplane.seq += 2;
1777 else
1778 zns->netlink_dplane.seq++;
1779#endif /* HAVE_NETLINK */
1780
1781 return AOK;
1782}
1783
7cdb1a84
MS
1784/*
1785 * Initialize a context block for a route update from zebra data structs.
1786 */
018e77bc
RZ
1787int dplane_ctx_route_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
1788 struct route_node *rn, struct route_entry *re)
7cdb1a84
MS
1789{
1790 int ret = EINVAL;
1791 const struct route_table *table = NULL;
630d5962 1792 const struct rib_table_info *info;
7cdb1a84
MS
1793 const struct prefix *p, *src_p;
1794 struct zebra_ns *zns;
1795 struct zebra_vrf *zvrf;
f183e380 1796 struct nexthop *nexthop;
f2a0ba3a 1797 zebra_l3vni_t *zl3vni;
7cdb1a84 1798
5709131c 1799 if (!ctx || !rn || !re)
7cdb1a84 1800 goto done;
7cdb1a84
MS
1801
1802 ctx->zd_op = op;
14b0bc8e 1803 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
7cdb1a84 1804
0f461727
MS
1805 ctx->u.rinfo.zd_type = re->type;
1806 ctx->u.rinfo.zd_old_type = re->type;
7cdb1a84
MS
1807
1808 /* Prefixes: dest, and optional source */
1809 srcdest_rnode_prefixes(rn, &p, &src_p);
1810
0f461727 1811 prefix_copy(&(ctx->u.rinfo.zd_dest), p);
7cdb1a84 1812
5709131c 1813 if (src_p)
0f461727 1814 prefix_copy(&(ctx->u.rinfo.zd_src), src_p);
5709131c 1815 else
0f461727 1816 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(ctx->u.rinfo.zd_src));
7cdb1a84
MS
1817
1818 ctx->zd_table_id = re->table;
1819
0f461727
MS
1820 ctx->u.rinfo.zd_metric = re->metric;
1821 ctx->u.rinfo.zd_old_metric = re->metric;
7cdb1a84 1822 ctx->zd_vrf_id = re->vrf_id;
0f461727
MS
1823 ctx->u.rinfo.zd_mtu = re->mtu;
1824 ctx->u.rinfo.zd_nexthop_mtu = re->nexthop_mtu;
1825 ctx->u.rinfo.zd_instance = re->instance;
1826 ctx->u.rinfo.zd_tag = re->tag;
1827 ctx->u.rinfo.zd_old_tag = re->tag;
1828 ctx->u.rinfo.zd_distance = re->distance;
7cdb1a84
MS
1829
1830 table = srcdest_rnode_table(rn);
1831 info = table->info;
1832
0f461727
MS
1833 ctx->u.rinfo.zd_afi = info->afi;
1834 ctx->u.rinfo.zd_safi = info->safi;
7cdb1a84 1835
7cdb1a84 1836 /* Copy nexthops; recursive info is included too */
0eb97b86 1837 copy_nexthops(&(ctx->u.rinfo.zd_ng.nexthop),
c415d895 1838 re->nhe->nhg.nexthop, NULL);
1d48702e
MS
1839 ctx->u.rinfo.zd_nhg_id = re->nhe->id;
1840
1841 /* Copy backup nexthop info, if present */
1842 if (re->nhe->backup_info && re->nhe->backup_info->nhe) {
1843 copy_nexthops(&(ctx->u.rinfo.backup_ng.nexthop),
1844 re->nhe->backup_info->nhe->nhg.nexthop, NULL);
1845 }
7cdb1a84 1846
f2a0ba3a
RZ
1847 /*
1848 * Ensure that the dplane nexthops' flags are clear and copy
1849 * encapsulation information.
1850 */
1851 for (ALL_NEXTHOPS(ctx->u.rinfo.zd_ng, nexthop)) {
f183e380 1852 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
f183e380 1853
f2a0ba3a
RZ
1854 /* Check for available encapsulations. */
1855 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE))
1856 continue;
1857
1858 zl3vni = zl3vni_from_vrf(nexthop->vrf_id);
1859 if (zl3vni && is_l3vni_oper_up(zl3vni)) {
1860 nexthop->nh_encap_type = NET_VXLAN;
1861 nexthop->nh_encap.vni = zl3vni->vni;
1862 }
1863 }
1864
cf363e1b
MS
1865 /* Don't need some info when capturing a system notification */
1866 if (op == DPLANE_OP_SYS_ROUTE_ADD ||
1867 op == DPLANE_OP_SYS_ROUTE_DELETE) {
1868 ret = AOK;
1869 goto done;
1870 }
1871
1872 /* Extract ns info - can't use pointers to 'core' structs */
1873 zvrf = vrf_info_lookup(re->vrf_id);
1874 zns = zvrf->zns;
1875 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_ROUTE_UPDATE));
1876
6df59152 1877#ifdef HAVE_NETLINK
f924db49 1878 {
1d48702e 1879 struct nhg_hash_entry *nhe = zebra_nhg_resolve(re->nhe);
6df59152 1880
ae9bfa06 1881 ctx->u.rinfo.nhe.id = nhe->id;
6df59152 1882 /*
ae9bfa06
SW
1883 * Check if the nhe is installed/queued before doing anything
1884 * with this route.
08c51a38
SW
1885 *
1886 * If its a delete we only use the prefix anyway, so this only
1887 * matters for INSTALL/UPDATE.
6df59152 1888 */
08c51a38
SW
1889 if (((op == DPLANE_OP_ROUTE_INSTALL)
1890 || (op == DPLANE_OP_ROUTE_UPDATE))
1891 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
ae9bfa06 1892 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) {
6df59152
SW
1893 ret = ENOENT;
1894 goto done;
1895 }
98cda54a 1896 }
6df59152 1897#endif /* HAVE_NETLINK */
de3f5488 1898
7cdb1a84
MS
1899 /* Trying out the sequence number idea, so we can try to detect
1900 * when a result is stale.
1901 */
1485bbe7 1902 re->dplane_sequence = zebra_router_get_next_sequence();
7cdb1a84
MS
1903 ctx->zd_seq = re->dplane_sequence;
1904
1905 ret = AOK;
1906
1907done:
1908 return ret;
1909}
1910
f820d025
SW
1911/**
1912 * dplane_ctx_nexthop_init() - Initialize a context block for a nexthop update
1913 *
1914 * @ctx: Dataplane context to init
1915 * @op: Operation being performed
1916 * @nhe: Nexthop group hash entry
1917 *
1918 * Return: Result status
1919 */
a2072e71 1920int dplane_ctx_nexthop_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
981ca597 1921 struct nhg_hash_entry *nhe)
f820d025 1922{
a7df21c4 1923 struct zebra_vrf *zvrf = NULL;
8e401b25 1924 struct zebra_ns *zns = NULL;
f820d025
SW
1925 int ret = EINVAL;
1926
1927 if (!ctx || !nhe)
1928 goto done;
1929
1930 ctx->zd_op = op;
1931 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1932
1933 /* Copy over nhe info */
0c8215cb
SW
1934 ctx->u.rinfo.nhe.id = nhe->id;
1935 ctx->u.rinfo.nhe.afi = nhe->afi;
1936 ctx->u.rinfo.nhe.vrf_id = nhe->vrf_id;
38e40db1 1937 ctx->u.rinfo.nhe.type = nhe->type;
0c8215cb 1938
c415d895 1939 nexthop_group_copy(&(ctx->u.rinfo.nhe.ng), &(nhe->nhg));
0c8215cb 1940
c415d895 1941 /* If this is a group, convert it to a grp array of ids */
98cda54a
SW
1942 if (!zebra_nhg_depends_is_empty(nhe)
1943 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE))
8dbc800f
SW
1944 ctx->u.rinfo.nhe.nh_grp_count = zebra_nhg_nhe2grp(
1945 ctx->u.rinfo.nhe.nh_grp, nhe, MULTIPATH_NUM);
f820d025 1946
a7df21c4
SW
1947 zvrf = vrf_info_lookup(nhe->vrf_id);
1948
1949 /*
1950 * Fallback to default namespace if the vrf got ripped out from under
1951 * us.
1952 */
1953 zns = zvrf ? zvrf->zns : zebra_ns_lookup(NS_DEFAULT);
1909e63a 1954
2d3c57e6
SW
1955 /*
1956 * TODO: Might not need to mark this as an update, since
1957 * it probably won't require two messages
1958 */
1909e63a
SW
1959 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_NH_UPDATE));
1960
f820d025
SW
1961 ret = AOK;
1962
1963done:
1964 return ret;
1965}
1966
16c628de
MS
1967/*
1968 * Capture information for an LSP update in a dplane context.
1969 */
65f264cf
MS
1970int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
1971 zebra_lsp_t *lsp)
16c628de
MS
1972{
1973 int ret = AOK;
1974 zebra_nhlfe_t *nhlfe, *new_nhlfe;
1975
16c628de
MS
1976 ctx->zd_op = op;
1977 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1978
1979 /* Capture namespace info */
1980 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
1981 (op == DPLANE_OP_LSP_UPDATE));
1982
1983 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
1984
ee70f629 1985 nhlfe_list_init(&(ctx->u.lsp.nhlfe_list));
cd4bb96f 1986 nhlfe_list_init(&(ctx->u.lsp.backup_nhlfe_list));
65f264cf
MS
1987
1988 /* This may be called to create/init a dplane context, not necessarily
1989 * to copy an lsp object.
1990 */
1991 if (lsp == NULL) {
1992 ret = AOK;
1993 goto done;
1994 }
1995
1996 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1997 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1998 dplane_op2str(op), lsp->ile.in_label,
1999 lsp->num_ecmp);
2000
16c628de
MS
2001 ctx->u.lsp.ile = lsp->ile;
2002 ctx->u.lsp.addr_family = lsp->addr_family;
2003 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
2004 ctx->u.lsp.flags = lsp->flags;
2005
2006 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
ee70f629 2007 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
16c628de
MS
2008 /* Not sure if this is meaningful... */
2009 if (nhlfe->nexthop == NULL)
2010 continue;
2011
cd4bb96f
MS
2012 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp), nhlfe->type,
2013 nhlfe->nexthop);
16c628de
MS
2014 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2015 ret = ENOMEM;
2016 break;
2017 }
2018
3c0e1622 2019 /* Need to copy flags and backup info too */
16c628de
MS
2020 new_nhlfe->flags = nhlfe->flags;
2021 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2022
3c0e1622
MS
2023 if (CHECK_FLAG(new_nhlfe->nexthop->flags,
2024 NEXTHOP_FLAG_HAS_BACKUP)) {
2025 new_nhlfe->nexthop->backup_num =
2026 nhlfe->nexthop->backup_num;
2027 memcpy(new_nhlfe->nexthop->backup_idx,
2028 nhlfe->nexthop->backup_idx,
2029 new_nhlfe->nexthop->backup_num);
2030 }
2031
16c628de
MS
2032 if (nhlfe == lsp->best_nhlfe)
2033 ctx->u.lsp.best_nhlfe = new_nhlfe;
2034 }
2035
cd4bb96f
MS
2036 if (ret != AOK)
2037 goto done;
2038
2039 /* Capture backup nhlfes/nexthops */
2040 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
2041 /* Not sure if this is meaningful... */
2042 if (nhlfe->nexthop == NULL)
2043 continue;
2044
2045 new_nhlfe = zebra_mpls_lsp_add_backup_nh(&(ctx->u.lsp),
2046 nhlfe->type,
2047 nhlfe->nexthop);
2048 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2049 ret = ENOMEM;
2050 break;
2051 }
2052
2053 /* Need to copy flags too */
2054 new_nhlfe->flags = nhlfe->flags;
2055 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2056 }
2057
16c628de
MS
2058 /* On error the ctx will be cleaned-up, so we don't need to
2059 * deal with any allocated nhlfe or nexthop structs here.
2060 */
cd4bb96f 2061done:
16c628de
MS
2062
2063 return ret;
2064}
2065
97d8d05a
MS
2066/*
2067 * Capture information for an LSP update in a dplane context.
2068 */
2069static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
2070 enum dplane_op_e op,
2071 struct zebra_pw *pw)
2072{
09cd307c
MS
2073 struct prefix p;
2074 afi_t afi;
2075 struct route_table *table;
2076 struct route_node *rn;
2077 struct route_entry *re;
0024ea8e 2078 const struct nexthop_group *nhg;
09cd307c 2079
97d8d05a
MS
2080 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2081 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
2082 dplane_op2str(op), pw->ifname, pw->local_label,
2083 pw->remote_label);
2084
2085 ctx->zd_op = op;
2086 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2087
2088 /* Capture namespace info: no netlink support as of 12/18,
2089 * but just in case...
2090 */
2091 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2092
2093 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
2094
2095 /* This name appears to be c-string, so we use string copy. */
7c7ef4a8 2096 strlcpy(ctx->zd_ifname, pw->ifname, sizeof(ctx->zd_ifname));
16d69787 2097
9bd9717b 2098 ctx->zd_vrf_id = pw->vrf_id;
7c7ef4a8 2099 ctx->zd_ifindex = pw->ifindex;
97d8d05a
MS
2100 ctx->u.pw.type = pw->type;
2101 ctx->u.pw.af = pw->af;
2102 ctx->u.pw.local_label = pw->local_label;
2103 ctx->u.pw.remote_label = pw->remote_label;
2104 ctx->u.pw.flags = pw->flags;
2105
16d69787 2106 ctx->u.pw.dest = pw->nexthop;
97d8d05a
MS
2107
2108 ctx->u.pw.fields = pw->data;
2109
09cd307c
MS
2110 /* Capture nexthop info for the pw destination. We need to look
2111 * up and use zebra datastructs, but we're running in the zebra
2112 * pthread here so that should be ok.
2113 */
2114 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
2115 p.family = pw->af;
2116 p.prefixlen = ((pw->af == AF_INET) ?
2117 IPV4_MAX_PREFIXLEN : IPV6_MAX_PREFIXLEN);
2118
2119 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
2120 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
2121 if (table) {
2122 rn = route_node_match(table, &p);
2123 if (rn) {
2124 RNODE_FOREACH_RE(rn, re) {
2125 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
2126 break;
2127 }
2128
0024ea8e
MS
2129 if (re) {
2130 nhg = rib_get_fib_nhg(re);
3c0e1622
MS
2131 if (nhg && nhg->nexthop)
2132 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
2133 nhg->nexthop, NULL);
8b117ff0
MS
2134
2135 /* Include any installed backup nexthops */
2136 nhg = rib_get_fib_backup_nhg(re);
2137 if (nhg && nhg->nexthop)
2138 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
2139 nhg->nexthop, NULL);
0024ea8e 2140 }
09cd307c
MS
2141 route_unlock_node(rn);
2142 }
2143 }
2144
9f2d0354 2145 return AOK;
97d8d05a
MS
2146}
2147
f62e5480
JU
2148/**
2149 * dplane_ctx_rule_init_single() - Initialize a dataplane representation of a
2150 * PBR rule.
2151 *
2152 * @dplane_rule: Dataplane internal representation of a rule
2153 * @rule: PBR rule
2154 */
2155static void dplane_ctx_rule_init_single(struct dplane_ctx_rule *dplane_rule,
2156 struct zebra_pbr_rule *rule)
2157{
2158 dplane_rule->priority = rule->rule.priority;
2159 dplane_rule->table = rule->rule.action.table;
2160
2161 dplane_rule->filter_bm = rule->rule.filter.filter_bm;
2162 dplane_rule->fwmark = rule->rule.filter.fwmark;
01f23aff 2163 dplane_rule->dsfield = rule->rule.filter.dsfield;
f62e5480
JU
2164 prefix_copy(&(dplane_rule->dst_ip), &rule->rule.filter.dst_ip);
2165 prefix_copy(&(dplane_rule->src_ip), &rule->rule.filter.src_ip);
2166}
2167
2168/**
2169 * dplane_ctx_rule_init() - Initialize a context block for a PBR rule update.
2170 *
2171 * @ctx: Dataplane context to init
2172 * @op: Operation being performed
2173 * @new_rule: PBR rule
2174 *
2175 * Return: Result status
2176 */
2177static int dplane_ctx_rule_init(struct zebra_dplane_ctx *ctx,
2178 enum dplane_op_e op,
2179 struct zebra_pbr_rule *new_rule,
2180 struct zebra_pbr_rule *old_rule)
2181{
2182 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2183 char buf1[PREFIX_STRLEN];
2184 char buf2[PREFIX_STRLEN];
2185
2186 zlog_debug(
2187 "init dplane ctx %s: IF %s(%u) Prio %u Fwmark %u Src %s Dst %s Table %u",
2188 dplane_op2str(op), new_rule->ifname,
2189 new_rule->rule.ifindex, new_rule->rule.priority,
2190 new_rule->rule.filter.fwmark,
2191 prefix2str(&new_rule->rule.filter.src_ip, buf1,
2192 sizeof(buf1)),
2193 prefix2str(&new_rule->rule.filter.dst_ip, buf2,
2194 sizeof(buf2)),
2195 new_rule->rule.action.table);
2196 }
2197
2198 ctx->zd_op = op;
2199 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2200
2201 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2202 op == DPLANE_OP_RULE_UPDATE);
2203
2204 ctx->zd_vrf_id = new_rule->vrf_id;
2205 memcpy(ctx->zd_ifname, new_rule->ifname, sizeof(new_rule->ifname));
2206 ctx->zd_ifindex = new_rule->rule.ifindex;
2207
2208 ctx->u.rule.sock = new_rule->sock;
2209 ctx->u.rule.unique = new_rule->rule.unique;
2210 ctx->u.rule.seq = new_rule->rule.seq;
2211
2212 dplane_ctx_rule_init_single(&ctx->u.rule.new, new_rule);
2213 if (op == DPLANE_OP_RULE_UPDATE)
2214 dplane_ctx_rule_init_single(&ctx->u.rule.old, old_rule);
2215
2216 return AOK;
2217}
2218
7cdb1a84 2219/*
3fe4ccc4 2220 * Enqueue a new update,
16c628de 2221 * and ensure an event is active for the dataplane pthread.
7cdb1a84 2222 */
3fe4ccc4 2223static int dplane_update_enqueue(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
2224{
2225 int ret = EINVAL;
91f16812 2226 uint32_t high, curr;
7cdb1a84 2227
16c628de 2228 /* Enqueue for processing by the dataplane pthread */
7cdb1a84
MS
2229 DPLANE_LOCK();
2230 {
3fe4ccc4 2231 TAILQ_INSERT_TAIL(&zdplane_info.dg_update_ctx_q, ctx,
25779064 2232 zd_q_entries);
7cdb1a84
MS
2233 }
2234 DPLANE_UNLOCK();
2235
e3d9208a 2236 curr = atomic_fetch_add_explicit(
e07e9549 2237 &(zdplane_info.dg_routes_queued),
e07e9549 2238 1, memory_order_seq_cst);
91f16812 2239
e3d9208a
MS
2240 curr++; /* We got the pre-incremented value */
2241
91f16812
MS
2242 /* Maybe update high-water counter also */
2243 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
2244 memory_order_seq_cst);
2245 while (high < curr) {
2246 if (atomic_compare_exchange_weak_explicit(
2247 &zdplane_info.dg_routes_queued_max,
2248 &high, curr,
2249 memory_order_seq_cst,
2250 memory_order_seq_cst))
2251 break;
2252 }
2253
7cdb1a84 2254 /* Ensure that an event for the dataplane thread is active */
c831033f 2255 ret = dplane_provider_work_ready();
7cdb1a84 2256
5709131c 2257 return ret;
7cdb1a84
MS
2258}
2259
7cdb1a84
MS
2260/*
2261 * Utility that prepares a route update and enqueues it for processing
2262 */
655d681a
MS
2263static enum zebra_dplane_result
2264dplane_route_update_internal(struct route_node *rn,
2265 struct route_entry *re,
2266 struct route_entry *old_re,
5709131c 2267 enum dplane_op_e op)
7cdb1a84 2268{
655d681a 2269 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2270 int ret = EINVAL;
25779064 2271 struct zebra_dplane_ctx *ctx = NULL;
7cdb1a84
MS
2272
2273 /* Obtain context block */
2274 ctx = dplane_ctx_alloc();
7cdb1a84
MS
2275
2276 /* Init context with info from zebra data structs */
2277 ret = dplane_ctx_route_init(ctx, op, rn, re);
2278 if (ret == AOK) {
2279 /* Capture some extra info for update case
2280 * where there's a different 'old' route.
2281 */
b8e0423d
MS
2282 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
2283 old_re && (old_re != re)) {
7cdb1a84
MS
2284 ctx->zd_is_update = true;
2285
1485bbe7
DS
2286 old_re->dplane_sequence =
2287 zebra_router_get_next_sequence();
7cdb1a84
MS
2288 ctx->zd_old_seq = old_re->dplane_sequence;
2289
0f461727
MS
2290 ctx->u.rinfo.zd_old_tag = old_re->tag;
2291 ctx->u.rinfo.zd_old_type = old_re->type;
2292 ctx->u.rinfo.zd_old_instance = old_re->instance;
2293 ctx->u.rinfo.zd_old_distance = old_re->distance;
2294 ctx->u.rinfo.zd_old_metric = old_re->metric;
01ce7cba
MS
2295
2296#ifndef HAVE_NETLINK
f183e380
MS
2297 /* For bsd, capture previous re's nexthops too, sigh.
2298 * We'll need these to do per-nexthop deletes.
2299 */
0f461727 2300 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
c415d895 2301 old_re->nhe->nhg.nexthop, NULL);
1d48702e
MS
2302
2303 if (zebra_nhg_get_backup_nhg(old_re->nhe) != NULL) {
2304 struct nexthop_group *nhg;
2305 struct nexthop **nh;
2306
2307 nhg = zebra_nhg_get_backup_nhg(old_re->nhe);
2308 nh = &(ctx->u.rinfo.old_backup_ng.nexthop);
2309
2310 if (nhg->nexthop)
2311 copy_nexthops(nh, nhg->nexthop, NULL);
2312 }
01ce7cba 2313#endif /* !HAVE_NETLINK */
7cdb1a84
MS
2314 }
2315
2316 /* Enqueue context for processing */
3fe4ccc4 2317 ret = dplane_update_enqueue(ctx);
7cdb1a84
MS
2318 }
2319
91f16812 2320 /* Update counter */
25779064 2321 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
1d11b21f
MS
2322 memory_order_relaxed);
2323
91f16812 2324 if (ret == AOK)
655d681a 2325 result = ZEBRA_DPLANE_REQUEST_QUEUED;
16c628de 2326 else {
6df59152
SW
2327 if (ret == ENOENT)
2328 result = ZEBRA_DPLANE_REQUEST_SUCCESS;
2329 else
2330 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
2331 1, memory_order_relaxed);
16c628de
MS
2332 if (ctx)
2333 dplane_ctx_free(&ctx);
1d11b21f 2334 }
7cdb1a84 2335
5709131c 2336 return result;
7cdb1a84
MS
2337}
2338
f820d025
SW
2339/**
2340 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
2341 *
2342 * @nhe: Nexthop group hash entry where the change occured
2343 * @op: The operation to be enqued
2344 *
2345 * Return: Result of the change
2346 */
2347static enum zebra_dplane_result
2348dplane_nexthop_update_internal(struct nhg_hash_entry *nhe, enum dplane_op_e op)
2349{
2350 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2351 int ret = EINVAL;
2352 struct zebra_dplane_ctx *ctx = NULL;
2353
2354 /* Obtain context block */
2355 ctx = dplane_ctx_alloc();
2356 if (!ctx) {
2357 ret = ENOMEM;
2358 goto done;
2359 }
2360
2361 ret = dplane_ctx_nexthop_init(ctx, op, nhe);
2d3c57e6 2362 if (ret == AOK)
f820d025 2363 ret = dplane_update_enqueue(ctx);
2d3c57e6 2364
f820d025
SW
2365done:
2366 /* Update counter */
2367 atomic_fetch_add_explicit(&zdplane_info.dg_nexthops_in, 1,
2368 memory_order_relaxed);
2369
2370 if (ret == AOK)
2371 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2372 else {
81505946
SW
2373 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
2374 memory_order_relaxed);
f820d025
SW
2375 if (ctx)
2376 dplane_ctx_free(&ctx);
2377 }
2378
2379 return result;
2380}
2381
7cdb1a84
MS
2382/*
2383 * Enqueue a route 'add' for the dataplane.
2384 */
655d681a
MS
2385enum zebra_dplane_result dplane_route_add(struct route_node *rn,
2386 struct route_entry *re)
7cdb1a84 2387{
655d681a 2388 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2389
5709131c 2390 if (rn == NULL || re == NULL)
7cdb1a84 2391 goto done;
7cdb1a84
MS
2392
2393 ret = dplane_route_update_internal(rn, re, NULL,
2394 DPLANE_OP_ROUTE_INSTALL);
2395
2396done:
5709131c 2397 return ret;
7cdb1a84
MS
2398}
2399
2400/*
2401 * Enqueue a route update for the dataplane.
2402 */
655d681a
MS
2403enum zebra_dplane_result dplane_route_update(struct route_node *rn,
2404 struct route_entry *re,
2405 struct route_entry *old_re)
7cdb1a84 2406{
655d681a 2407 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2408
5709131c 2409 if (rn == NULL || re == NULL)
7cdb1a84 2410 goto done;
7cdb1a84
MS
2411
2412 ret = dplane_route_update_internal(rn, re, old_re,
2413 DPLANE_OP_ROUTE_UPDATE);
7cdb1a84 2414done:
5709131c 2415 return ret;
7cdb1a84
MS
2416}
2417
2418/*
2419 * Enqueue a route removal for the dataplane.
2420 */
655d681a
MS
2421enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
2422 struct route_entry *re)
7cdb1a84 2423{
655d681a 2424 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2425
5709131c 2426 if (rn == NULL || re == NULL)
7cdb1a84 2427 goto done;
7cdb1a84
MS
2428
2429 ret = dplane_route_update_internal(rn, re, NULL,
2430 DPLANE_OP_ROUTE_DELETE);
2431
2432done:
5709131c 2433 return ret;
7cdb1a84
MS
2434}
2435
cf363e1b
MS
2436/*
2437 * Notify the dplane when system/connected routes change.
2438 */
2439enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
2440 struct route_entry *re)
2441{
2442 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2443
2444 /* Ignore this event unless a provider plugin has requested it. */
2445 if (!zdplane_info.dg_sys_route_notifs) {
2446 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2447 goto done;
2448 }
2449
2450 if (rn == NULL || re == NULL)
2451 goto done;
2452
2453 ret = dplane_route_update_internal(rn, re, NULL,
2454 DPLANE_OP_SYS_ROUTE_ADD);
2455
2456done:
2457 return ret;
2458}
2459
2460/*
2461 * Notify the dplane when system/connected routes are deleted.
2462 */
2463enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
2464 struct route_entry *re)
2465{
2466 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2467
2468 /* Ignore this event unless a provider plugin has requested it. */
2469 if (!zdplane_info.dg_sys_route_notifs) {
2470 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2471 goto done;
2472 }
2473
2474 if (rn == NULL || re == NULL)
2475 goto done;
2476
2477 ret = dplane_route_update_internal(rn, re, NULL,
2478 DPLANE_OP_SYS_ROUTE_DELETE);
2479
2480done:
2481 return ret;
2482}
2483
188a00e0
MS
2484/*
2485 * Update from an async notification, to bring other fibs up-to-date.
2486 */
2487enum zebra_dplane_result
2488dplane_route_notif_update(struct route_node *rn,
2489 struct route_entry *re,
2490 enum dplane_op_e op,
2491 struct zebra_dplane_ctx *ctx)
2492{
54d321aa
MS
2493 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2494 int ret = EINVAL;
188a00e0
MS
2495 struct zebra_dplane_ctx *new_ctx = NULL;
2496 struct nexthop *nexthop;
3c0e1622 2497 struct nexthop_group *nhg;
188a00e0
MS
2498
2499 if (rn == NULL || re == NULL)
2500 goto done;
2501
2502 new_ctx = dplane_ctx_alloc();
2503 if (new_ctx == NULL)
2504 goto done;
2505
2506 /* Init context with info from zebra data structs */
2507 dplane_ctx_route_init(new_ctx, op, rn, re);
2508
2509 /* For add/update, need to adjust the nexthops so that we match
2510 * the notification state, which may not be the route-entry/RIB
2511 * state.
2512 */
2513 if (op == DPLANE_OP_ROUTE_UPDATE ||
2514 op == DPLANE_OP_ROUTE_INSTALL) {
2515
2516 nexthops_free(new_ctx->u.rinfo.zd_ng.nexthop);
2517 new_ctx->u.rinfo.zd_ng.nexthop = NULL;
2518
3c0e1622
MS
2519 nhg = rib_get_fib_nhg(re);
2520 if (nhg && nhg->nexthop)
2521 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
2522 nhg->nexthop, NULL);
188a00e0 2523
3c0e1622
MS
2524 /* Check for installed backup nexthops also */
2525 nhg = rib_get_fib_backup_nhg(re);
2526 if (nhg && nhg->nexthop) {
00a9b150 2527 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
3c0e1622 2528 nhg->nexthop, NULL);
00a9b150 2529 }
188a00e0
MS
2530
2531 for (ALL_NEXTHOPS(new_ctx->u.rinfo.zd_ng, nexthop))
2532 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2533
2534 }
2535
2536 /* Capture info about the source of the notification, in 'ctx' */
2537 dplane_ctx_set_notif_provider(new_ctx,
2538 dplane_ctx_get_notif_provider(ctx));
2539
54d321aa 2540 ret = dplane_update_enqueue(new_ctx);
188a00e0
MS
2541
2542done:
54d321aa
MS
2543 if (ret == AOK)
2544 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2545 else if (ctx)
2546 dplane_ctx_free(&ctx);
2547
2548 return result;
188a00e0
MS
2549}
2550
f820d025
SW
2551/*
2552 * Enqueue a nexthop add for the dataplane.
2553 */
2554enum zebra_dplane_result dplane_nexthop_add(struct nhg_hash_entry *nhe)
2555{
2556 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2557
2558 if (nhe)
2559 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_INSTALL);
2560 return ret;
2561}
2562
2563/*
2564 * Enqueue a nexthop update for the dataplane.
81505946
SW
2565 *
2566 * Might not need this func since zebra's nexthop objects should be immutable?
f820d025
SW
2567 */
2568enum zebra_dplane_result dplane_nexthop_update(struct nhg_hash_entry *nhe)
2569{
2570 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2571
2572 if (nhe)
2573 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_UPDATE);
2574 return ret;
2575}
2576
2577/*
2578 * Enqueue a nexthop removal for the dataplane.
2579 */
2580enum zebra_dplane_result dplane_nexthop_delete(struct nhg_hash_entry *nhe)
2581{
2582 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2583
2584 if (nhe)
2585 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_DELETE);
2586
2587 return ret;
2588}
2589
16c628de
MS
2590/*
2591 * Enqueue LSP add for the dataplane.
2592 */
2593enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp)
2594{
2595 enum zebra_dplane_result ret =
2596 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
2597
2598 return ret;
2599}
2600
2601/*
2602 * Enqueue LSP update for the dataplane.
2603 */
2604enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp)
2605{
2606 enum zebra_dplane_result ret =
2607 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
2608
2609 return ret;
2610}
2611
2612/*
2613 * Enqueue LSP delete for the dataplane.
2614 */
2615enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp)
2616{
2617 enum zebra_dplane_result ret =
2618 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
2619
2620 return ret;
2621}
2622
188a00e0
MS
2623/* Update or un-install resulting from an async notification */
2624enum zebra_dplane_result
2625dplane_lsp_notif_update(zebra_lsp_t *lsp,
2626 enum dplane_op_e op,
2627 struct zebra_dplane_ctx *notif_ctx)
2628{
2629 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2630 int ret = EINVAL;
2631 struct zebra_dplane_ctx *ctx = NULL;
00a9b150
MS
2632 struct nhlfe_list_head *head;
2633 zebra_nhlfe_t *nhlfe, *new_nhlfe;
188a00e0
MS
2634
2635 /* Obtain context block */
2636 ctx = dplane_ctx_alloc();
2637 if (ctx == NULL) {
2638 ret = ENOMEM;
2639 goto done;
2640 }
2641
00a9b150 2642 /* Copy info from zebra LSP */
188a00e0
MS
2643 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2644 if (ret != AOK)
2645 goto done;
2646
00a9b150
MS
2647 /* Add any installed backup nhlfes */
2648 head = &(ctx->u.lsp.backup_nhlfe_list);
2649 frr_each(nhlfe_list, head, nhlfe) {
2650
2651 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) &&
2652 CHECK_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_FIB)) {
2653 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp),
2654 nhlfe->type,
2655 nhlfe->nexthop);
2656
2657 /* Need to copy flags too */
2658 new_nhlfe->flags = nhlfe->flags;
2659 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2660 }
2661 }
2662
188a00e0
MS
2663 /* Capture info about the source of the notification */
2664 dplane_ctx_set_notif_provider(
2665 ctx,
2666 dplane_ctx_get_notif_provider(notif_ctx));
2667
3fe4ccc4 2668 ret = dplane_update_enqueue(ctx);
188a00e0
MS
2669
2670done:
2671 /* Update counter */
2672 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2673 memory_order_relaxed);
2674
2675 if (ret == AOK)
2676 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2677 else {
2678 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2679 memory_order_relaxed);
2680 if (ctx)
2681 dplane_ctx_free(&ctx);
2682 }
2683 return result;
2684}
2685
97d8d05a
MS
2686/*
2687 * Enqueue pseudowire install for the dataplane.
2688 */
2689enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
2690{
2691 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
2692}
2693
2694/*
2695 * Enqueue pseudowire un-install for the dataplane.
2696 */
2697enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
2698{
2699 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
2700}
2701
16c628de
MS
2702/*
2703 * Common internal LSP update utility
2704 */
2705static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
2706 enum dplane_op_e op)
2707{
2708 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2709 int ret = EINVAL;
2710 struct zebra_dplane_ctx *ctx = NULL;
2711
2712 /* Obtain context block */
2713 ctx = dplane_ctx_alloc();
16c628de
MS
2714
2715 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2716 if (ret != AOK)
2717 goto done;
2718
3fe4ccc4 2719 ret = dplane_update_enqueue(ctx);
16c628de
MS
2720
2721done:
2722 /* Update counter */
2723 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2724 memory_order_relaxed);
2725
2726 if (ret == AOK)
2727 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2728 else {
2729 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2730 memory_order_relaxed);
d8e479a3 2731 dplane_ctx_free(&ctx);
16c628de
MS
2732 }
2733
2734 return result;
2735}
2736
97d8d05a
MS
2737/*
2738 * Internal, common handler for pseudowire updates.
2739 */
2740static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
2741 enum dplane_op_e op)
2742{
2743 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2744 int ret;
2745 struct zebra_dplane_ctx *ctx = NULL;
2746
2747 ctx = dplane_ctx_alloc();
97d8d05a
MS
2748
2749 ret = dplane_ctx_pw_init(ctx, op, pw);
2750 if (ret != AOK)
2751 goto done;
2752
3fe4ccc4 2753 ret = dplane_update_enqueue(ctx);
97d8d05a
MS
2754
2755done:
2756 /* Update counter */
2757 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
2758 memory_order_relaxed);
2759
2760 if (ret == AOK)
2761 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2762 else {
2763 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
2764 memory_order_relaxed);
d8e479a3 2765 dplane_ctx_free(&ctx);
97d8d05a
MS
2766 }
2767
2768 return result;
2769}
2770
a4a4802a
MS
2771/*
2772 * Enqueue interface address add for the dataplane.
2773 */
2774enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
2775 const struct connected *ifc)
2776{
64168803
MS
2777#if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2778 /* Extra checks for this OS path. */
2779
2780 /* Don't configure PtP addresses on broadcast ifs or reverse */
2781 if (!(ifp->flags & IFF_POINTOPOINT) != !CONNECTED_PEER(ifc)) {
2782 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_DPLANE)
2783 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2784
2785 return ZEBRA_DPLANE_REQUEST_FAILURE;
2786 }
2787
2788 /* Ensure that no existing installed v4 route conflicts with
2789 * the new interface prefix. This check must be done in the
2790 * zebra pthread context, and any route delete (if needed)
2791 * is enqueued before the interface address programming attempt.
2792 */
2793 if (ifc->address->family == AF_INET) {
2794 struct prefix_ipv4 *p;
2795
2796 p = (struct prefix_ipv4 *)ifc->address;
a36898e7 2797 rib_lookup_and_pushup(p, ifp->vrf_id);
64168803
MS
2798 }
2799#endif
2800
2801 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_INSTALL);
a4a4802a
MS
2802}
2803
2804/*
2805 * Enqueue interface address remove/uninstall for the dataplane.
2806 */
2807enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
2808 const struct connected *ifc)
2809{
64168803
MS
2810 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_UNINSTALL);
2811}
2812
2813static enum zebra_dplane_result intf_addr_update_internal(
2814 const struct interface *ifp, const struct connected *ifc,
2815 enum dplane_op_e op)
2816{
2817 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2818 int ret = EINVAL;
2819 struct zebra_dplane_ctx *ctx = NULL;
2820 struct zebra_ns *zns;
2821
2822 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2823 char addr_str[PREFIX_STRLEN];
2824
2825 prefix2str(ifc->address, addr_str, sizeof(addr_str));
2826
2827 zlog_debug("init intf ctx %s: idx %d, addr %u:%s",
a36898e7 2828 dplane_op2str(op), ifp->ifindex, ifp->vrf_id,
64168803
MS
2829 addr_str);
2830 }
2831
2832 ctx = dplane_ctx_alloc();
64168803
MS
2833
2834 ctx->zd_op = op;
2835 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
a36898e7 2836 ctx->zd_vrf_id = ifp->vrf_id;
64168803 2837
a36898e7 2838 zns = zebra_ns_lookup(ifp->vrf_id);
64168803
MS
2839 dplane_ctx_ns_init(ctx, zns, false);
2840
2841 /* Init the interface-addr-specific area */
2842 memset(&ctx->u.intf, 0, sizeof(ctx->u.intf));
2843
7c7ef4a8
MS
2844 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2845 ctx->zd_ifindex = ifp->ifindex;
64168803
MS
2846 ctx->u.intf.prefix = *(ifc->address);
2847
2848 if (if_is_broadcast(ifp))
2849 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
2850
2851 if (CONNECTED_PEER(ifc)) {
2852 ctx->u.intf.dest_prefix = *(ifc->destination);
2853 ctx->u.intf.flags |=
2854 (DPLANE_INTF_CONNECTED | DPLANE_INTF_HAS_DEST);
64168803
MS
2855 }
2856
2857 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
2858 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
2859
2860 if (ifc->label) {
2861 size_t len;
2862
2863 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
2864
2865 /* Use embedded buffer if it's adequate; else allocate. */
2866 len = strlen(ifc->label);
2867
2868 if (len < sizeof(ctx->u.intf.label_buf)) {
b7b7bf31 2869 strlcpy(ctx->u.intf.label_buf, ifc->label,
64168803
MS
2870 sizeof(ctx->u.intf.label_buf));
2871 ctx->u.intf.label = ctx->u.intf.label_buf;
2872 } else {
2873 ctx->u.intf.label = strdup(ifc->label);
2874 }
2875 }
2876
3fe4ccc4 2877 ret = dplane_update_enqueue(ctx);
64168803 2878
64168803
MS
2879 /* Increment counter */
2880 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addrs_in, 1,
2881 memory_order_relaxed);
2882
2883 if (ret == AOK)
2884 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2885 else {
2886 /* Error counter */
2887 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
2888 1, memory_order_relaxed);
d8e479a3 2889 dplane_ctx_free(&ctx);
64168803
MS
2890 }
2891
2892 return result;
a4a4802a
MS
2893}
2894
7597ac7b
MS
2895/*
2896 * Enqueue vxlan/evpn mac add (or update).
2897 */
2898enum zebra_dplane_result dplane_mac_add(const struct interface *ifp,
478566d6 2899 const struct interface *bridge_ifp,
7597ac7b
MS
2900 vlanid_t vid,
2901 const struct ethaddr *mac,
2902 struct in_addr vtep_ip,
2903 bool sticky)
2904{
2905 enum zebra_dplane_result result;
2906
2907 /* Use common helper api */
f73a8467
MS
2908 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
2909 vid, mac, vtep_ip, sticky);
7597ac7b
MS
2910 return result;
2911}
2912
2913/*
2914 * Enqueue vxlan/evpn mac delete.
2915 */
2916enum zebra_dplane_result dplane_mac_del(const struct interface *ifp,
478566d6 2917 const struct interface *bridge_ifp,
7597ac7b
MS
2918 vlanid_t vid,
2919 const struct ethaddr *mac,
2920 struct in_addr vtep_ip)
2921{
2922 enum zebra_dplane_result result;
2923
2924 /* Use common helper api */
f73a8467
MS
2925 result = mac_update_common(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp,
2926 vid, mac, vtep_ip, false);
7597ac7b
MS
2927 return result;
2928}
2929
2930/*
f73a8467
MS
2931 * Public api to init an empty context - either newly-allocated or
2932 * reset/cleared - for a MAC update.
7597ac7b 2933 */
f73a8467
MS
2934void dplane_mac_init(struct zebra_dplane_ctx *ctx,
2935 const struct interface *ifp,
2936 const struct interface *br_ifp,
2937 vlanid_t vid,
2938 const struct ethaddr *mac,
2939 struct in_addr vtep_ip,
2940 bool sticky)
7597ac7b 2941{
7597ac7b
MS
2942 struct zebra_ns *zns;
2943
7597ac7b
MS
2944 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2945 ctx->zd_vrf_id = ifp->vrf_id;
2946
2947 zns = zebra_ns_lookup(ifp->vrf_id);
2948 dplane_ctx_ns_init(ctx, zns, false);
2949
2950 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2951 ctx->zd_ifindex = ifp->ifindex;
2952
2953 /* Init the mac-specific data area */
2954 memset(&ctx->u.macinfo, 0, sizeof(ctx->u.macinfo));
2955
478566d6 2956 ctx->u.macinfo.br_ifindex = br_ifp->ifindex;
7597ac7b
MS
2957 ctx->u.macinfo.vtep_ip = vtep_ip;
2958 ctx->u.macinfo.mac = *mac;
2959 ctx->u.macinfo.vid = vid;
2960 ctx->u.macinfo.is_sticky = sticky;
f73a8467
MS
2961}
2962
2963/*
2964 * Common helper api for MAC address/vxlan updates
2965 */
2966static enum zebra_dplane_result
2967mac_update_common(enum dplane_op_e op,
2968 const struct interface *ifp,
2969 const struct interface *br_ifp,
2970 vlanid_t vid,
2971 const struct ethaddr *mac,
2972 struct in_addr vtep_ip,
2973 bool sticky)
2974{
2975 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2976 int ret;
2977 struct zebra_dplane_ctx *ctx = NULL;
2978
2979 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2980 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
2981
2982 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
2983 dplane_op2str(op),
2984 prefix_mac2str(mac, buf1, sizeof(buf1)),
2985 ifp->name,
2986 inet_ntop(AF_INET, &vtep_ip, buf2, sizeof(buf2)));
2987 }
2988
2989 ctx = dplane_ctx_alloc();
2990 ctx->zd_op = op;
2991
2992 /* Common init for the ctx */
2993 dplane_mac_init(ctx, ifp, br_ifp, vid, mac, vtep_ip, sticky);
7597ac7b
MS
2994
2995 /* Enqueue for processing on the dplane pthread */
2996 ret = dplane_update_enqueue(ctx);
2997
2998 /* Increment counter */
2999 atomic_fetch_add_explicit(&zdplane_info.dg_macs_in, 1,
3000 memory_order_relaxed);
3001
3002 if (ret == AOK)
3003 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3004 else {
3005 /* Error counter */
3006 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors, 1,
3007 memory_order_relaxed);
3008 dplane_ctx_free(&ctx);
3009 }
3010
3011 return result;
3012}
3013
931fa60c
MS
3014/*
3015 * Enqueue evpn neighbor add for the dataplane.
3016 */
3017enum zebra_dplane_result dplane_neigh_add(const struct interface *ifp,
3018 const struct ipaddr *ip,
3019 const struct ethaddr *mac,
3020 uint32_t flags)
3021{
3022 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3023
3024 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
4dd9d11c 3025 ifp, mac, ip, flags, DPLANE_NUD_NOARP);
931fa60c
MS
3026
3027 return result;
3028}
3029
3030/*
3031 * Enqueue evpn neighbor update for the dataplane.
3032 */
3033enum zebra_dplane_result dplane_neigh_update(const struct interface *ifp,
3034 const struct ipaddr *ip,
3035 const struct ethaddr *mac)
3036{
0bbd4ff4 3037 enum zebra_dplane_result result;
931fa60c
MS
3038
3039 result = neigh_update_internal(DPLANE_OP_NEIGH_UPDATE,
3040 ifp, mac, ip, 0, DPLANE_NUD_PROBE);
3041
3042 return result;
3043}
3044
3045/*
3046 * Enqueue evpn neighbor delete for the dataplane.
3047 */
3048enum zebra_dplane_result dplane_neigh_delete(const struct interface *ifp,
3049 const struct ipaddr *ip)
3050{
0bbd4ff4 3051 enum zebra_dplane_result result;
931fa60c
MS
3052
3053 result = neigh_update_internal(DPLANE_OP_NEIGH_DELETE,
3054 ifp, NULL, ip, 0, 0);
3055
3056 return result;
3057}
3058
0bbd4ff4
MS
3059/*
3060 * Enqueue evpn VTEP add for the dataplane.
3061 */
3062enum zebra_dplane_result dplane_vtep_add(const struct interface *ifp,
3063 const struct in_addr *ip,
3064 vni_t vni)
3065{
3066 enum zebra_dplane_result result;
3067 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3068 struct ipaddr addr;
3069
3070 if (IS_ZEBRA_DEBUG_VXLAN)
3071 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
3072 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3073
3074 SET_IPADDR_V4(&addr);
3075 addr.ipaddr_v4 = *ip;
3076
3077 result = neigh_update_internal(DPLANE_OP_VTEP_ADD,
3078 ifp, &mac, &addr, 0, 0);
3079
3080 return result;
3081}
3082
3083/*
3084 * Enqueue evpn VTEP add for the dataplane.
3085 */
3086enum zebra_dplane_result dplane_vtep_delete(const struct interface *ifp,
3087 const struct in_addr *ip,
3088 vni_t vni)
3089{
3090 enum zebra_dplane_result result;
3091 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3092 struct ipaddr addr;
3093
3094 if (IS_ZEBRA_DEBUG_VXLAN)
3095 zlog_debug(
3096 "Uninstall %s from flood list for VNI %u intf %s(%u)",
3097 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3098
3099 SET_IPADDR_V4(&addr);
3100 addr.ipaddr_v4 = *ip;
3101
3102 result = neigh_update_internal(DPLANE_OP_VTEP_DELETE,
3103 ifp, &mac, &addr, 0, 0);
3104
3105 return result;
3106}
3107
931fa60c
MS
3108/*
3109 * Common helper api for evpn neighbor updates
3110 */
3111static enum zebra_dplane_result
3112neigh_update_internal(enum dplane_op_e op,
3113 const struct interface *ifp,
3114 const struct ethaddr *mac,
3115 const struct ipaddr *ip,
3116 uint32_t flags, uint16_t state)
3117{
3118 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3119 int ret;
3120 struct zebra_dplane_ctx *ctx = NULL;
3121 struct zebra_ns *zns;
3122
3123 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3124 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
3125
3126 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
3127 dplane_op2str(op),
3128 prefix_mac2str(mac, buf1, sizeof(buf1)),
3129 ifp->name,
3130 ipaddr2str(ip, buf2, sizeof(buf2)));
3131 }
3132
3133 ctx = dplane_ctx_alloc();
3134
3135 ctx->zd_op = op;
3136 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3137 ctx->zd_vrf_id = ifp->vrf_id;
3138
3139 zns = zebra_ns_lookup(ifp->vrf_id);
3140 dplane_ctx_ns_init(ctx, zns, false);
3141
3142 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3143 ctx->zd_ifindex = ifp->ifindex;
3144
3145 /* Init the neighbor-specific data area */
3146 memset(&ctx->u.neigh, 0, sizeof(ctx->u.neigh));
3147
3148 ctx->u.neigh.ip_addr = *ip;
3149 if (mac)
3150 ctx->u.neigh.mac = *mac;
3151 ctx->u.neigh.flags = flags;
3152 ctx->u.neigh.state = state;
3153
3154 /* Enqueue for processing on the dplane pthread */
3155 ret = dplane_update_enqueue(ctx);
3156
3157 /* Increment counter */
3158 atomic_fetch_add_explicit(&zdplane_info.dg_neighs_in, 1,
3159 memory_order_relaxed);
3160
3161 if (ret == AOK)
3162 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3163 else {
3164 /* Error counter */
3165 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors, 1,
3166 memory_order_relaxed);
3167 dplane_ctx_free(&ctx);
3168 }
3169
3170 return result;
3171}
3172
f62e5480
JU
3173/*
3174 * Common helper api for PBR rule updates
3175 */
3176static enum zebra_dplane_result
3177rule_update_internal(enum dplane_op_e op, struct zebra_pbr_rule *new_rule,
3178 struct zebra_pbr_rule *old_rule)
3179{
3180 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3181 struct zebra_dplane_ctx *ctx;
3182 int ret;
3183
3184 ctx = dplane_ctx_alloc();
3185
3186 ret = dplane_ctx_rule_init(ctx, op, new_rule, old_rule);
3187 if (ret != AOK)
3188 goto done;
3189
3190 ret = dplane_update_enqueue(ctx);
3191
3192done:
3193 atomic_fetch_add_explicit(&zdplane_info.dg_rules_in, 1,
3194 memory_order_relaxed);
3195
3196 if (ret == AOK)
3197 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3198 else {
3199 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors, 1,
3200 memory_order_relaxed);
3201 dplane_ctx_free(&ctx);
3202 }
3203
3204 return result;
3205}
3206
3207enum zebra_dplane_result dplane_pbr_rule_add(struct zebra_pbr_rule *rule)
3208{
3209 return rule_update_internal(DPLANE_OP_RULE_ADD, rule, NULL);
3210}
3211
3212enum zebra_dplane_result dplane_pbr_rule_delete(struct zebra_pbr_rule *rule)
3213{
3214 return rule_update_internal(DPLANE_OP_RULE_DELETE, rule, NULL);
3215}
3216
3217enum zebra_dplane_result dplane_pbr_rule_update(struct zebra_pbr_rule *old_rule,
3218 struct zebra_pbr_rule *new_rule)
3219{
3220 return rule_update_internal(DPLANE_OP_RULE_UPDATE, new_rule, old_rule);
3221}
3222
1d11b21f
MS
3223/*
3224 * Handler for 'show dplane'
3225 */
3226int dplane_show_helper(struct vty *vty, bool detailed)
3227{
16c628de
MS
3228 uint64_t queued, queue_max, limit, errs, incoming, yields,
3229 other_errs;
1d11b21f 3230
4dfd7a02 3231 /* Using atomics because counters are being changed in different
c831033f 3232 * pthread contexts.
4dfd7a02 3233 */
25779064 3234 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
1d11b21f 3235 memory_order_relaxed);
91f16812
MS
3236 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
3237 memory_order_relaxed);
25779064 3238 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
1d11b21f 3239 memory_order_relaxed);
25779064 3240 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
4dfd7a02 3241 memory_order_relaxed);
25779064 3242 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
1d11b21f 3243 memory_order_relaxed);
c831033f
MS
3244 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
3245 memory_order_relaxed);
16c628de
MS
3246 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
3247 memory_order_relaxed);
1d11b21f 3248
c831033f
MS
3249 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
3250 incoming);
1d11b21f 3251 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
16c628de 3252 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
91f16812 3253 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
1d11b21f 3254 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
4dfd7a02 3255 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
4280d91c
MS
3256 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
3257
3258 incoming = atomic_load_explicit(&zdplane_info.dg_lsps_in,
3259 memory_order_relaxed);
3260 errs = atomic_load_explicit(&zdplane_info.dg_lsp_errors,
3261 memory_order_relaxed);
3262 vty_out(vty, "LSP updates: %"PRIu64"\n", incoming);
3263 vty_out(vty, "LSP update errors: %"PRIu64"\n", errs);
3264
3265 incoming = atomic_load_explicit(&zdplane_info.dg_pws_in,
3266 memory_order_relaxed);
3267 errs = atomic_load_explicit(&zdplane_info.dg_pw_errors,
3268 memory_order_relaxed);
3269 vty_out(vty, "PW updates: %"PRIu64"\n", incoming);
3270 vty_out(vty, "PW update errors: %"PRIu64"\n", errs);
3271
3272 incoming = atomic_load_explicit(&zdplane_info.dg_intf_addrs_in,
3273 memory_order_relaxed);
3274 errs = atomic_load_explicit(&zdplane_info.dg_intf_addr_errors,
3275 memory_order_relaxed);
3276 vty_out(vty, "Intf addr updates: %"PRIu64"\n", incoming);
3277 vty_out(vty, "Intf addr errors: %"PRIu64"\n", errs);
3278
3279 incoming = atomic_load_explicit(&zdplane_info.dg_macs_in,
3280 memory_order_relaxed);
3281 errs = atomic_load_explicit(&zdplane_info.dg_mac_errors,
3282 memory_order_relaxed);
3283 vty_out(vty, "EVPN MAC updates: %"PRIu64"\n", incoming);
3284 vty_out(vty, "EVPN MAC errors: %"PRIu64"\n", errs);
1d11b21f 3285
931fa60c
MS
3286 incoming = atomic_load_explicit(&zdplane_info.dg_neighs_in,
3287 memory_order_relaxed);
3288 errs = atomic_load_explicit(&zdplane_info.dg_neigh_errors,
3289 memory_order_relaxed);
3290 vty_out(vty, "EVPN neigh updates: %"PRIu64"\n", incoming);
3291 vty_out(vty, "EVPN neigh errors: %"PRIu64"\n", errs);
3292
60d8d43b
JU
3293 incoming = atomic_load_explicit(&zdplane_info.dg_rules_in,
3294 memory_order_relaxed);
3295 errs = atomic_load_explicit(&zdplane_info.dg_rule_errors,
3296 memory_order_relaxed);
3297 vty_out(vty, "Rule updates: %" PRIu64 "\n", incoming);
3298 vty_out(vty, "Rule errors: %" PRIu64 "\n", errs);
3299
1d11b21f
MS
3300 return CMD_SUCCESS;
3301}
3302
3303/*
3304 * Handler for 'show dplane providers'
3305 */
3306int dplane_show_provs_helper(struct vty *vty, bool detailed)
3307{
c831033f
MS
3308 struct zebra_dplane_provider *prov;
3309 uint64_t in, in_max, out, out_max;
3310
3311 vty_out(vty, "Zebra dataplane providers:\n");
3312
3313 DPLANE_LOCK();
3314 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
3315 DPLANE_UNLOCK();
3316
3317 /* Show counters, useful info from each registered provider */
3318 while (prov) {
3319
3320 in = atomic_load_explicit(&prov->dp_in_counter,
3321 memory_order_relaxed);
3322 in_max = atomic_load_explicit(&prov->dp_in_max,
3323 memory_order_relaxed);
3324 out = atomic_load_explicit(&prov->dp_out_counter,
3325 memory_order_relaxed);
3326 out_max = atomic_load_explicit(&prov->dp_out_max,
3327 memory_order_relaxed);
3328
3efd0893 3329 vty_out(vty, "%s (%u): in: %"PRIu64", q_max: %"PRIu64", out: %"PRIu64", q_max: %"PRIu64"\n",
c831033f
MS
3330 prov->dp_name, prov->dp_id, in, in_max, out, out_max);
3331
3332 DPLANE_LOCK();
3333 prov = TAILQ_NEXT(prov, dp_prov_link);
3334 DPLANE_UNLOCK();
3335 }
1d11b21f
MS
3336
3337 return CMD_SUCCESS;
3338}
3339
f26730e1
MS
3340/*
3341 * Helper for 'show run' etc.
3342 */
3343int dplane_config_write_helper(struct vty *vty)
3344{
3345 if (zdplane_info.dg_max_queued_updates != DPLANE_DEFAULT_MAX_QUEUED)
3346 vty_out(vty, "zebra dplane limit %u\n",
3347 zdplane_info.dg_max_queued_updates);
3348
3349 return 0;
3350}
3351
b8e0423d
MS
3352/*
3353 * Provider registration
3354 */
3355int dplane_provider_register(const char *name,
c831033f
MS
3356 enum dplane_provider_prio prio,
3357 int flags,
1dd4ea8a 3358 int (*start_fp)(struct zebra_dplane_provider *),
4c206c8f
MS
3359 int (*fp)(struct zebra_dplane_provider *),
3360 int (*fini_fp)(struct zebra_dplane_provider *,
3361 bool early),
1ff8a248
MS
3362 void *data,
3363 struct zebra_dplane_provider **prov_p)
b8e0423d
MS
3364{
3365 int ret = 0;
6fb51ccb 3366 struct zebra_dplane_provider *p = NULL, *last;
b8e0423d
MS
3367
3368 /* Validate */
3369 if (fp == NULL) {
3370 ret = EINVAL;
3371 goto done;
3372 }
3373
3374 if (prio <= DPLANE_PRIO_NONE ||
1bcea841 3375 prio > DPLANE_PRIO_LAST) {
b8e0423d
MS
3376 ret = EINVAL;
3377 goto done;
3378 }
3379
3380 /* Allocate and init new provider struct */
25779064 3381 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
b8e0423d 3382
c831033f
MS
3383 pthread_mutex_init(&(p->dp_mutex), NULL);
3384 TAILQ_INIT(&(p->dp_ctx_in_q));
3385 TAILQ_INIT(&(p->dp_ctx_out_q));
b8e0423d 3386
932dbb4d 3387 p->dp_flags = flags;
b8e0423d
MS
3388 p->dp_priority = prio;
3389 p->dp_fp = fp;
1dd4ea8a 3390 p->dp_start = start_fp;
18c37974 3391 p->dp_fini = fini_fp;
c831033f 3392 p->dp_data = data;
18c37974 3393
c831033f 3394 /* Lock - the dplane pthread may be running */
18c37974 3395 DPLANE_LOCK();
b8e0423d 3396
25779064 3397 p->dp_id = ++zdplane_info.dg_provider_id;
b8e0423d 3398
c831033f
MS
3399 if (name)
3400 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
3401 else
3402 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
3403 "provider-%u", p->dp_id);
3404
b8e0423d 3405 /* Insert into list ordered by priority */
c831033f 3406 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
5709131c 3407 if (last->dp_priority > p->dp_priority)
b8e0423d 3408 break;
b8e0423d
MS
3409 }
3410
5709131c 3411 if (last)
c831033f 3412 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
5709131c 3413 else
25779064 3414 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
c831033f 3415 dp_prov_link);
b8e0423d 3416
18c37974
MS
3417 /* And unlock */
3418 DPLANE_UNLOCK();
3419
c831033f
MS
3420 if (IS_ZEBRA_DEBUG_DPLANE)
3421 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
3422 p->dp_name, p->dp_id, p->dp_priority);
3423
b8e0423d 3424done:
1ff8a248
MS
3425 if (prov_p)
3426 *prov_p = p;
3427
5709131c 3428 return ret;
b8e0423d
MS
3429}
3430
c831033f
MS
3431/* Accessors for provider attributes */
3432const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
3433{
3434 return prov->dp_name;
3435}
3436
3437uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
3438{
3439 return prov->dp_id;
3440}
3441
3442void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
3443{
3444 return prov->dp_data;
3445}
3446
3447int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
3448{
3449 return zdplane_info.dg_updates_per_cycle;
3450}
3451
ad6aad4d
MS
3452/* Lock/unlock a provider's mutex - iff the provider was registered with
3453 * the THREADED flag.
3454 */
3455void dplane_provider_lock(struct zebra_dplane_provider *prov)
3456{
3457 if (dplane_provider_is_threaded(prov))
3458 DPLANE_PROV_LOCK(prov);
3459}
3460
3461void dplane_provider_unlock(struct zebra_dplane_provider *prov)
3462{
3463 if (dplane_provider_is_threaded(prov))
3464 DPLANE_PROV_UNLOCK(prov);
3465}
3466
c831033f
MS
3467/*
3468 * Dequeue and maintain associated counter
3469 */
3470struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
3471 struct zebra_dplane_provider *prov)
3472{
3473 struct zebra_dplane_ctx *ctx = NULL;
3474
ad6aad4d 3475 dplane_provider_lock(prov);
c831033f
MS
3476
3477 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3478 if (ctx) {
3479 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
c9d17fe8
MS
3480
3481 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
3482 memory_order_relaxed);
c831033f
MS
3483 }
3484
ad6aad4d 3485 dplane_provider_unlock(prov);
c831033f
MS
3486
3487 return ctx;
3488}
3489
3490/*
3491 * Dequeue work to a list, return count
3492 */
3493int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
3494 struct dplane_ctx_q *listp)
3495{
3496 int limit, ret;
3497 struct zebra_dplane_ctx *ctx;
3498
3499 limit = zdplane_info.dg_updates_per_cycle;
3500
ad6aad4d 3501 dplane_provider_lock(prov);
c831033f
MS
3502
3503 for (ret = 0; ret < limit; ret++) {
3504 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3505 if (ctx) {
3506 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
3507
3508 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
3509 } else {
3510 break;
3511 }
3512 }
3513
c9d17fe8
MS
3514 if (ret > 0)
3515 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
3516 memory_order_relaxed);
3517
ad6aad4d 3518 dplane_provider_unlock(prov);
c831033f
MS
3519
3520 return ret;
3521}
3522
3523/*
3524 * Enqueue and maintain associated counter
3525 */
3526void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
3527 struct zebra_dplane_ctx *ctx)
3528{
ad6aad4d 3529 dplane_provider_lock(prov);
c831033f
MS
3530
3531 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
3532 zd_q_entries);
3533
ad6aad4d 3534 dplane_provider_unlock(prov);
c831033f
MS
3535
3536 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
3537 memory_order_relaxed);
3538}
3539
62b8bb7a
MS
3540/*
3541 * Accessor for provider object
3542 */
c831033f
MS
3543bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
3544{
3545 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
3546}
3547
62b8bb7a
MS
3548/*
3549 * Internal helper that copies information from a zebra ns object; this is
3550 * called in the zebra main pthread context as part of dplane ctx init.
3551 */
3552static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
3553 struct zebra_ns *zns)
3554{
3555 ns_info->ns_id = zns->ns_id;
3556
3557#if defined(HAVE_NETLINK)
3558 ns_info->is_cmd = true;
3559 ns_info->nls = zns->netlink_dplane;
3560#endif /* NETLINK */
3561}
3562
c831033f
MS
3563/*
3564 * Provider api to signal that work/events are available
3565 * for the dataplane pthread.
3566 */
3567int dplane_provider_work_ready(void)
3568{
e5a60d82
MS
3569 /* Note that during zebra startup, we may be offered work before
3570 * the dataplane pthread (and thread-master) are ready. We want to
3571 * enqueue the work, but the event-scheduling machinery may not be
3572 * available.
3573 */
3574 if (zdplane_info.dg_run) {
3575 thread_add_event(zdplane_info.dg_master,
3576 dplane_thread_loop, NULL, 0,
3577 &zdplane_info.dg_t_update);
3578 }
c831033f
MS
3579
3580 return AOK;
3581}
3582
593e4eb1
MS
3583/*
3584 * Enqueue a context directly to zebra main.
3585 */
3586void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx)
3587{
3588 struct dplane_ctx_q temp_list;
3589
3590 /* Zebra's api takes a list, so we need to use a temporary list */
3591 TAILQ_INIT(&temp_list);
3592
3593 TAILQ_INSERT_TAIL(&temp_list, ctx, zd_q_entries);
3594 (zdplane_info.dg_results_cb)(&temp_list);
3595}
3596
7cdb1a84 3597/*
c831033f 3598 * Kernel dataplane provider
7cdb1a84 3599 */
c831033f 3600
16c628de
MS
3601/*
3602 * Handler for kernel LSP updates
3603 */
3604static enum zebra_dplane_result
3605kernel_dplane_lsp_update(struct zebra_dplane_ctx *ctx)
3606{
2f74a82a 3607 return kernel_lsp_update(ctx);
16c628de
MS
3608}
3609
c10a646d
MS
3610/*
3611 * Handler for kernel pseudowire updates
3612 */
3613static enum zebra_dplane_result
3614kernel_dplane_pw_update(struct zebra_dplane_ctx *ctx)
3615{
c10a646d
MS
3616 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3617 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
7c7ef4a8 3618 dplane_ctx_get_ifname(ctx),
c10a646d
MS
3619 dplane_op2str(ctx->zd_op),
3620 dplane_ctx_get_pw_af(ctx),
3621 dplane_ctx_get_pw_local_label(ctx),
3622 dplane_ctx_get_pw_remote_label(ctx));
3623
2f74a82a 3624 return kernel_pw_update(ctx);
c10a646d
MS
3625}
3626
16c628de
MS
3627/*
3628 * Handler for kernel route updates
3629 */
3630static enum zebra_dplane_result
3631kernel_dplane_route_update(struct zebra_dplane_ctx *ctx)
3632{
16c628de
MS
3633 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3634 char dest_str[PREFIX_STRLEN];
3635
3636 prefix2str(dplane_ctx_get_dest(ctx),
3637 dest_str, sizeof(dest_str));
3638
3639 zlog_debug("%u:%s Dplane route update ctx %p op %s",
3640 dplane_ctx_get_vrf(ctx), dest_str,
3641 ctx, dplane_op2str(dplane_ctx_get_op(ctx)));
3642 }
3643
2f74a82a 3644 return kernel_route_update(ctx);
16c628de
MS
3645}
3646
64168803
MS
3647/*
3648 * Handler for kernel-facing interface address updates
3649 */
3650static enum zebra_dplane_result
3651kernel_dplane_address_update(struct zebra_dplane_ctx *ctx)
3652{
64168803
MS
3653 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3654 char dest_str[PREFIX_STRLEN];
3655
3656 prefix2str(dplane_ctx_get_intf_addr(ctx), dest_str,
3657 sizeof(dest_str));
3658
3659 zlog_debug("Dplane intf %s, idx %u, addr %s",
3660 dplane_op2str(dplane_ctx_get_op(ctx)),
3661 dplane_ctx_get_ifindex(ctx), dest_str);
3662 }
3663
2f74a82a 3664 return kernel_address_update_ctx(ctx);
64168803
MS
3665}
3666
f820d025
SW
3667/**
3668 * kernel_dplane_nexthop_update() - Handler for kernel nexthop updates
3669 *
3670 * @ctx: Dataplane context
3671 *
3672 * Return: Dataplane result flag
3673 */
3674static enum zebra_dplane_result
3675kernel_dplane_nexthop_update(struct zebra_dplane_ctx *ctx)
3676{
f820d025
SW
3677 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3678 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
0c8215cb 3679 dplane_ctx_get_nhe_id(ctx), ctx,
f820d025
SW
3680 dplane_op2str(dplane_ctx_get_op(ctx)));
3681 }
3682
2f74a82a 3683 return kernel_nexthop_update(ctx);
f820d025
SW
3684}
3685
036d93c0 3686/*
931fa60c 3687 * Handler for kernel-facing EVPN MAC address updates
036d93c0
MS
3688 */
3689static enum zebra_dplane_result
3690kernel_dplane_mac_update(struct zebra_dplane_ctx *ctx)
3691{
036d93c0
MS
3692 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3693 char buf[ETHER_ADDR_STRLEN];
3694
3695 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf,
3696 sizeof(buf));
3697
3698 zlog_debug("Dplane %s, mac %s, ifindex %u",
3699 dplane_op2str(dplane_ctx_get_op(ctx)),
3700 buf, dplane_ctx_get_ifindex(ctx));
3701 }
3702
2f74a82a 3703 return kernel_mac_update_ctx(ctx);
036d93c0
MS
3704}
3705
931fa60c
MS
3706/*
3707 * Handler for kernel-facing EVPN neighbor updates
3708 */
3709static enum zebra_dplane_result
3710kernel_dplane_neigh_update(struct zebra_dplane_ctx *ctx)
3711{
931fa60c
MS
3712 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3713 char buf[PREFIX_STRLEN];
3714
3715 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx), buf,
3716 sizeof(buf));
3717
3718 zlog_debug("Dplane %s, ip %s, ifindex %u",
3719 dplane_op2str(dplane_ctx_get_op(ctx)),
3720 buf, dplane_ctx_get_ifindex(ctx));
3721 }
3722
2f74a82a 3723 return kernel_neigh_update_ctx(ctx);
931fa60c
MS
3724}
3725
f62e5480
JU
3726/*
3727 * Handler for kernel PBR rule updates
3728 */
3729static enum zebra_dplane_result
3730kernel_dplane_rule_update(struct zebra_dplane_ctx *ctx)
3731{
f62e5480
JU
3732 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3733 zlog_debug("Dplane rule update op %s, if %s(%u), ctx %p",
3734 dplane_op2str(dplane_ctx_get_op(ctx)),
3735 dplane_ctx_get_ifname(ctx),
3736 dplane_ctx_get_ifindex(ctx), ctx);
3737
2f74a82a
JU
3738 return kernel_pbr_rule_update(ctx);
3739}
f62e5480 3740
2f74a82a
JU
3741static void kernel_dplane_handle_result(struct zebra_dplane_ctx *ctx,
3742 enum zebra_dplane_result res)
3743{
3744 switch (dplane_ctx_get_op(ctx)) {
3745
3746 case DPLANE_OP_ROUTE_INSTALL:
3747 case DPLANE_OP_ROUTE_UPDATE:
3748 case DPLANE_OP_ROUTE_DELETE:
3749 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3750 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
3751 1, memory_order_relaxed);
3752 break;
3753
3754 case DPLANE_OP_NH_INSTALL:
3755 case DPLANE_OP_NH_UPDATE:
3756 case DPLANE_OP_NH_DELETE:
3757 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3758 atomic_fetch_add_explicit(
3759 &zdplane_info.dg_nexthop_errors, 1,
3760 memory_order_relaxed);
3761 break;
3762
3763 case DPLANE_OP_LSP_INSTALL:
3764 case DPLANE_OP_LSP_UPDATE:
3765 case DPLANE_OP_LSP_DELETE:
3766 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3767 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors,
3768 1, memory_order_relaxed);
3769 break;
3770
3771 case DPLANE_OP_PW_INSTALL:
3772 case DPLANE_OP_PW_UNINSTALL:
3773 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3774 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
3775 memory_order_relaxed);
3776 break;
f62e5480 3777
2f74a82a
JU
3778 case DPLANE_OP_ADDR_INSTALL:
3779 case DPLANE_OP_ADDR_UNINSTALL:
3780 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3781 atomic_fetch_add_explicit(
3782 &zdplane_info.dg_intf_addr_errors, 1,
3783 memory_order_relaxed);
3784 break;
3785
3786 case DPLANE_OP_MAC_INSTALL:
3787 case DPLANE_OP_MAC_DELETE:
3788 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3789 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors,
3790 1, memory_order_relaxed);
3791 break;
3792
3793 case DPLANE_OP_NEIGH_INSTALL:
3794 case DPLANE_OP_NEIGH_UPDATE:
3795 case DPLANE_OP_NEIGH_DELETE:
3796 case DPLANE_OP_VTEP_ADD:
3797 case DPLANE_OP_VTEP_DELETE:
3798 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3799 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors,
3800 1, memory_order_relaxed);
3801 break;
3802
3803 case DPLANE_OP_RULE_ADD:
3804 case DPLANE_OP_RULE_DELETE:
3805 case DPLANE_OP_RULE_UPDATE:
3806 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3807 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors,
3808 1, memory_order_relaxed);
3809 break;
3810
3811 /* Ignore 'notifications' - no-op */
3812 case DPLANE_OP_SYS_ROUTE_ADD:
3813 case DPLANE_OP_SYS_ROUTE_DELETE:
3814 case DPLANE_OP_ROUTE_NOTIFY:
3815 case DPLANE_OP_LSP_NOTIFY:
3816 case DPLANE_OP_NONE:
3817 break;
3818 }
3819
3820 dplane_ctx_set_status(ctx, res);
f62e5480
JU
3821}
3822
c831033f
MS
3823/*
3824 * Kernel provider callback
3825 */
3826static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
7cdb1a84 3827{
c831033f 3828 enum zebra_dplane_result res;
2f74a82a
JU
3829 struct zebra_dplane_ctx *ctx, *tctx;
3830 struct dplane_ctx_q work_list;
c831033f 3831 int counter, limit;
7cdb1a84 3832
2f74a82a
JU
3833 TAILQ_INIT(&work_list);
3834
c831033f 3835 limit = dplane_provider_get_work_limit(prov);
7cdb1a84 3836
c831033f
MS
3837 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3838 zlog_debug("dplane provider '%s': processing",
3839 dplane_provider_get_name(prov));
7cdb1a84 3840
c831033f 3841 for (counter = 0; counter < limit; counter++) {
91f16812 3842
c831033f
MS
3843 ctx = dplane_provider_dequeue_in_ctx(prov);
3844 if (ctx == NULL)
3845 break;
d8c16a95 3846
9677961e
MS
3847 /* A previous provider plugin may have asked to skip the
3848 * kernel update.
3849 */
3850 if (dplane_ctx_is_skip_kernel(ctx)) {
3851 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3852 goto skip_one;
3853 }
3854
16c628de
MS
3855 /* Dispatch to appropriate kernel-facing apis */
3856 switch (dplane_ctx_get_op(ctx)) {
1d11b21f 3857
16c628de
MS
3858 case DPLANE_OP_ROUTE_INSTALL:
3859 case DPLANE_OP_ROUTE_UPDATE:
3860 case DPLANE_OP_ROUTE_DELETE:
3861 res = kernel_dplane_route_update(ctx);
3862 break;
d8c16a95 3863
f820d025
SW
3864 case DPLANE_OP_NH_INSTALL:
3865 case DPLANE_OP_NH_UPDATE:
3866 case DPLANE_OP_NH_DELETE:
3867 res = kernel_dplane_nexthop_update(ctx);
3868 break;
3869
16c628de
MS
3870 case DPLANE_OP_LSP_INSTALL:
3871 case DPLANE_OP_LSP_UPDATE:
3872 case DPLANE_OP_LSP_DELETE:
3873 res = kernel_dplane_lsp_update(ctx);
3874 break;
d8c16a95 3875
c10a646d
MS
3876 case DPLANE_OP_PW_INSTALL:
3877 case DPLANE_OP_PW_UNINSTALL:
3878 res = kernel_dplane_pw_update(ctx);
3879 break;
3880
64168803
MS
3881 case DPLANE_OP_ADDR_INSTALL:
3882 case DPLANE_OP_ADDR_UNINSTALL:
3883 res = kernel_dplane_address_update(ctx);
3884 break;
3885
036d93c0
MS
3886 case DPLANE_OP_MAC_INSTALL:
3887 case DPLANE_OP_MAC_DELETE:
3888 res = kernel_dplane_mac_update(ctx);
3889 break;
3890
931fa60c
MS
3891 case DPLANE_OP_NEIGH_INSTALL:
3892 case DPLANE_OP_NEIGH_UPDATE:
3893 case DPLANE_OP_NEIGH_DELETE:
0bbd4ff4
MS
3894 case DPLANE_OP_VTEP_ADD:
3895 case DPLANE_OP_VTEP_DELETE:
931fa60c
MS
3896 res = kernel_dplane_neigh_update(ctx);
3897 break;
3898
f62e5480
JU
3899 case DPLANE_OP_RULE_ADD:
3900 case DPLANE_OP_RULE_DELETE:
3901 case DPLANE_OP_RULE_UPDATE:
3902 res = kernel_dplane_rule_update(ctx);
3903 break;
3904
104e3ad9 3905 /* Ignore 'notifications' - no-op */
cf363e1b
MS
3906 case DPLANE_OP_SYS_ROUTE_ADD:
3907 case DPLANE_OP_SYS_ROUTE_DELETE:
54818e3b 3908 case DPLANE_OP_ROUTE_NOTIFY:
104e3ad9 3909 case DPLANE_OP_LSP_NOTIFY:
cf363e1b
MS
3910 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3911 break;
3912
16c628de 3913 default:
c831033f 3914 atomic_fetch_add_explicit(
16c628de 3915 &zdplane_info.dg_other_errors, 1,
c831033f 3916 memory_order_relaxed);
d8c16a95 3917
16c628de
MS
3918 res = ZEBRA_DPLANE_REQUEST_FAILURE;
3919 break;
3920 }
3921
2f74a82a
JU
3922 skip_one:
3923 /* If the request isn't pending, we can handle the result right
3924 * away.
3925 */
3926 if (res != ZEBRA_DPLANE_REQUEST_PENDING)
3927 kernel_dplane_handle_result(ctx, res);
3928
3929 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
3930 }
c831033f 3931
2f74a82a
JU
3932 TAILQ_FOREACH_SAFE (ctx, &work_list, zd_q_entries, tctx) {
3933 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
c831033f
MS
3934 dplane_provider_enqueue_out_ctx(prov, ctx);
3935 }
3936
3937 /* Ensure that we'll run the work loop again if there's still
3938 * more work to do.
3939 */
3940 if (counter >= limit) {
3941 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3942 zlog_debug("dplane provider '%s' reached max updates %d",
3943 dplane_provider_get_name(prov), counter);
3944
3945 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
3946 1, memory_order_relaxed);
3947
3948 dplane_provider_work_ready();
3949 }
3950
3951 return 0;
3952}
3953
1e20238a 3954#ifdef DPLANE_TEST_PROVIDER
e5a60d82 3955
c831033f
MS
3956/*
3957 * Test dataplane provider plugin
3958 */
3959
3960/*
3961 * Test provider process callback
3962 */
3963static int test_dplane_process_func(struct zebra_dplane_provider *prov)
3964{
3965 struct zebra_dplane_ctx *ctx;
3966 int counter, limit;
3967
3968 /* Just moving from 'in' queue to 'out' queue */
3969
3970 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3971 zlog_debug("dplane provider '%s': processing",
3972 dplane_provider_get_name(prov));
3973
3974 limit = dplane_provider_get_work_limit(prov);
3975
3976 for (counter = 0; counter < limit; counter++) {
3977
3978 ctx = dplane_provider_dequeue_in_ctx(prov);
3979 if (ctx == NULL)
3980 break;
3981
cf363e1b
MS
3982 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3983 zlog_debug("dplane provider '%s': op %s",
3984 dplane_provider_get_name(prov),
3985 dplane_op2str(dplane_ctx_get_op(ctx)));
3986
c831033f
MS
3987 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
3988
3989 dplane_provider_enqueue_out_ctx(prov, ctx);
3990 }
3991
c9d17fe8
MS
3992 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3993 zlog_debug("dplane provider '%s': processed %d",
3994 dplane_provider_get_name(prov), counter);
3995
c831033f
MS
3996 /* Ensure that we'll run the work loop again if there's still
3997 * more work to do.
3998 */
3999 if (counter >= limit)
4000 dplane_provider_work_ready();
4001
4002 return 0;
4003}
4004
4005/*
4006 * Test provider shutdown/fini callback
4007 */
4008static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
4009 bool early)
4010{
4011 if (IS_ZEBRA_DEBUG_DPLANE)
4012 zlog_debug("dplane provider '%s': %sshutdown",
4013 dplane_provider_get_name(prov),
4014 early ? "early " : "");
4015
4016 return 0;
4017}
e5a60d82 4018#endif /* DPLANE_TEST_PROVIDER */
c831033f
MS
4019
4020/*
4021 * Register default kernel provider
4022 */
4023static void dplane_provider_init(void)
4024{
4025 int ret;
4026
4027 ret = dplane_provider_register("Kernel",
4028 DPLANE_PRIO_KERNEL,
1dd4ea8a 4029 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
4030 kernel_dplane_process_func,
4031 NULL,
1ff8a248 4032 NULL, NULL);
c831033f
MS
4033
4034 if (ret != AOK)
4035 zlog_err("Unable to register kernel dplane provider: %d",
4036 ret);
4037
1e20238a 4038#ifdef DPLANE_TEST_PROVIDER
e5a60d82 4039 /* Optional test provider ... */
c831033f
MS
4040 ret = dplane_provider_register("Test",
4041 DPLANE_PRIO_PRE_KERNEL,
1dd4ea8a 4042 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
4043 test_dplane_process_func,
4044 test_dplane_shutdown_func,
1ff8a248 4045 NULL /* data */, NULL);
c831033f
MS
4046
4047 if (ret != AOK)
4048 zlog_err("Unable to register test dplane provider: %d",
4049 ret);
e5a60d82 4050#endif /* DPLANE_TEST_PROVIDER */
7cdb1a84
MS
4051}
4052
4dfd7a02
MS
4053/* Indicates zebra shutdown/exit is in progress. Some operations may be
4054 * simplified or skipped during shutdown processing.
4055 */
4056bool dplane_is_in_shutdown(void)
4057{
25779064 4058 return zdplane_info.dg_is_shutdown;
4dfd7a02
MS
4059}
4060
4061/*
4062 * Early or pre-shutdown, de-init notification api. This runs pretty
4063 * early during zebra shutdown, as a signal to stop new work and prepare
4064 * for updates generated by shutdown/cleanup activity, as zebra tries to
4065 * remove everything it's responsible for.
c9d17fe8 4066 * NB: This runs in the main zebra pthread context.
4dfd7a02
MS
4067 */
4068void zebra_dplane_pre_finish(void)
4069{
3c0e1622 4070 struct zebra_dplane_provider *prov;
6ba8db21 4071
4dfd7a02 4072 if (IS_ZEBRA_DEBUG_DPLANE)
3c0e1622 4073 zlog_debug("Zebra dataplane pre-finish called");
4dfd7a02 4074
25779064 4075 zdplane_info.dg_is_shutdown = true;
4dfd7a02 4076
6ba8db21 4077 /* Notify provider(s) of pending shutdown. */
3c0e1622
MS
4078 TAILQ_FOREACH(prov, &zdplane_info.dg_providers_q, dp_prov_link) {
4079 if (prov->dp_fini == NULL)
6ba8db21
RZ
4080 continue;
4081
3c0e1622 4082 prov->dp_fini(prov, true /* early */);
6ba8db21 4083 }
4dfd7a02
MS
4084}
4085
4086/*
4087 * Utility to determine whether work remains enqueued within the dplane;
4088 * used during system shutdown processing.
4089 */
4090static bool dplane_work_pending(void)
4091{
c9d17fe8 4092 bool ret = false;
25779064 4093 struct zebra_dplane_ctx *ctx;
c9d17fe8 4094 struct zebra_dplane_provider *prov;
4dfd7a02 4095
c831033f
MS
4096 /* TODO -- just checking incoming/pending work for now, must check
4097 * providers
4098 */
4dfd7a02
MS
4099 DPLANE_LOCK();
4100 {
3fe4ccc4 4101 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c9d17fe8 4102 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4dfd7a02
MS
4103 }
4104 DPLANE_UNLOCK();
4105
c9d17fe8
MS
4106 if (ctx != NULL) {
4107 ret = true;
4108 goto done;
4109 }
4110
4111 while (prov) {
4112
ad6aad4d 4113 dplane_provider_lock(prov);
c9d17fe8
MS
4114
4115 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
4116 if (ctx == NULL)
4117 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4118
ad6aad4d 4119 dplane_provider_unlock(prov);
c9d17fe8
MS
4120
4121 if (ctx != NULL)
4122 break;
4123
4124 DPLANE_LOCK();
4125 prov = TAILQ_NEXT(prov, dp_prov_link);
4126 DPLANE_UNLOCK();
4127 }
4128
4129 if (ctx != NULL)
4130 ret = true;
4131
4132done:
4133 return ret;
4dfd7a02
MS
4134}
4135
4136/*
4137 * Shutdown-time intermediate callback, used to determine when all pending
4138 * in-flight updates are done. If there's still work to do, reschedules itself.
4139 * If all work is done, schedules an event to the main zebra thread for
4140 * final zebra shutdown.
4141 * This runs in the dplane pthread context.
4142 */
4143static int dplane_check_shutdown_status(struct thread *event)
4144{
4145 if (IS_ZEBRA_DEBUG_DPLANE)
4146 zlog_debug("Zebra dataplane shutdown status check called");
4147
4148 if (dplane_work_pending()) {
4149 /* Reschedule dplane check on a short timer */
25779064 4150 thread_add_timer_msec(zdplane_info.dg_master,
4dfd7a02
MS
4151 dplane_check_shutdown_status,
4152 NULL, 100,
25779064 4153 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
4154
4155 /* TODO - give up and stop waiting after a short time? */
4156
4157 } else {
4158 /* We appear to be done - schedule a final callback event
4159 * for the zebra main pthread.
4160 */
3801e764 4161 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
4dfd7a02
MS
4162 }
4163
4164 return 0;
4165}
4166
18c37974 4167/*
1d11b21f 4168 * Shutdown, de-init api. This runs pretty late during shutdown,
4dfd7a02
MS
4169 * after zebra has tried to free/remove/uninstall all routes during shutdown.
4170 * At this point, dplane work may still remain to be done, so we can't just
4171 * blindly terminate. If there's still work to do, we'll periodically check
4172 * and when done, we'll enqueue a task to the zebra main thread for final
4173 * termination processing.
4174 *
1d11b21f 4175 * NB: This runs in the main zebra thread context.
18c37974 4176 */
1d11b21f 4177void zebra_dplane_finish(void)
18c37974 4178{
4dfd7a02
MS
4179 if (IS_ZEBRA_DEBUG_DPLANE)
4180 zlog_debug("Zebra dataplane fini called");
4181
25779064 4182 thread_add_event(zdplane_info.dg_master,
4dfd7a02 4183 dplane_check_shutdown_status, NULL, 0,
25779064 4184 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
4185}
4186
c831033f
MS
4187/*
4188 * Main dataplane pthread event loop. The thread takes new incoming work
4189 * and offers it to the first provider. It then iterates through the
4190 * providers, taking complete work from each one and offering it
4191 * to the next in order. At each step, a limited number of updates are
4192 * processed during a cycle in order to provide some fairness.
14b0bc8e
MS
4193 *
4194 * This loop through the providers is only run once, so that the dataplane
4195 * pthread can look for other pending work - such as i/o work on behalf of
4196 * providers.
c831033f
MS
4197 */
4198static int dplane_thread_loop(struct thread *event)
4199{
4200 struct dplane_ctx_q work_list;
4201 struct dplane_ctx_q error_list;
4202 struct zebra_dplane_provider *prov;
4203 struct zebra_dplane_ctx *ctx, *tctx;
4204 int limit, counter, error_counter;
c9d17fe8 4205 uint64_t curr, high;
c831033f
MS
4206
4207 /* Capture work limit per cycle */
4208 limit = zdplane_info.dg_updates_per_cycle;
4209
14b0bc8e 4210 /* Init temporary lists used to move contexts among providers */
c831033f 4211 TAILQ_INIT(&work_list);
14b0bc8e
MS
4212 TAILQ_INIT(&error_list);
4213 error_counter = 0;
c831033f
MS
4214
4215 /* Check for zebra shutdown */
4216 if (!zdplane_info.dg_run)
4217 goto done;
4218
4219 /* Dequeue some incoming work from zebra (if any) onto the temporary
4220 * working list.
4221 */
4222 DPLANE_LOCK();
4223
4224 /* Locate initial registered provider */
4225 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4226
14b0bc8e 4227 /* Move new work from incoming list to temp list */
c831033f 4228 for (counter = 0; counter < limit; counter++) {
3fe4ccc4 4229 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c831033f 4230 if (ctx) {
3fe4ccc4 4231 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
c831033f
MS
4232 zd_q_entries);
4233
c831033f
MS
4234 ctx->zd_provider = prov->dp_id;
4235
4236 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
4237 } else {
4238 break;
4239 }
4240 }
4241
4242 DPLANE_UNLOCK();
4243
14b0bc8e
MS
4244 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
4245 memory_order_relaxed);
4246
c831033f
MS
4247 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4248 zlog_debug("dplane: incoming new work counter: %d", counter);
4249
4250 /* Iterate through the registered providers, offering new incoming
4251 * work. If the provider has outgoing work in its queue, take that
4252 * work for the next provider
4253 */
4254 while (prov) {
4255
14b0bc8e
MS
4256 /* At each iteration, the temporary work list has 'counter'
4257 * items.
4258 */
c831033f
MS
4259 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4260 zlog_debug("dplane enqueues %d new work to provider '%s'",
4261 counter, dplane_provider_get_name(prov));
4262
4263 /* Capture current provider id in each context; check for
4264 * error status.
4265 */
4266 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
4267 if (dplane_ctx_get_status(ctx) ==
4268 ZEBRA_DPLANE_REQUEST_SUCCESS) {
4269 ctx->zd_provider = prov->dp_id;
4270 } else {
4271 /*
4272 * TODO -- improve error-handling: recirc
4273 * errors backwards so that providers can
4274 * 'undo' their work (if they want to)
4275 */
4276
4277 /* Move to error list; will be returned
4278 * zebra main.
4279 */
4280 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
4281 TAILQ_INSERT_TAIL(&error_list,
4282 ctx, zd_q_entries);
4283 error_counter++;
4284 }
4285 }
4286
4287 /* Enqueue new work to the provider */
ad6aad4d 4288 dplane_provider_lock(prov);
c831033f
MS
4289
4290 if (TAILQ_FIRST(&work_list))
4291 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
4292 zd_q_entries);
4293
c9d17fe8
MS
4294 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
4295 memory_order_relaxed);
4296 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
4297 memory_order_relaxed);
4298 curr = atomic_load_explicit(&prov->dp_in_queued,
4299 memory_order_relaxed);
4300 high = atomic_load_explicit(&prov->dp_in_max,
4301 memory_order_relaxed);
4302 if (curr > high)
4303 atomic_store_explicit(&prov->dp_in_max, curr,
c831033f
MS
4304 memory_order_relaxed);
4305
ad6aad4d 4306 dplane_provider_unlock(prov);
c831033f 4307
14b0bc8e
MS
4308 /* Reset the temp list (though the 'concat' may have done this
4309 * already), and the counter
4310 */
c831033f
MS
4311 TAILQ_INIT(&work_list);
4312 counter = 0;
4313
14b0bc8e
MS
4314 /* Call into the provider code. Note that this is
4315 * unconditional: we offer to do work even if we don't enqueue
4316 * any _new_ work.
4317 */
c831033f
MS
4318 (*prov->dp_fp)(prov);
4319
4320 /* Check for zebra shutdown */
4321 if (!zdplane_info.dg_run)
4322 break;
4323
4324 /* Dequeue completed work from the provider */
ad6aad4d 4325 dplane_provider_lock(prov);
c831033f
MS
4326
4327 while (counter < limit) {
4328 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4329 if (ctx) {
4330 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
4331 zd_q_entries);
4332
4333 TAILQ_INSERT_TAIL(&work_list,
4334 ctx, zd_q_entries);
4335 counter++;
4336 } else
4337 break;
4338 }
4339
ad6aad4d 4340 dplane_provider_unlock(prov);
c831033f
MS
4341
4342 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4343 zlog_debug("dplane dequeues %d completed work from provider %s",
4344 counter, dplane_provider_get_name(prov));
4345
4346 /* Locate next provider */
4347 DPLANE_LOCK();
4348 prov = TAILQ_NEXT(prov, dp_prov_link);
4349 DPLANE_UNLOCK();
c831033f
MS
4350 }
4351
4352 /* After all providers have been serviced, enqueue any completed
14b0bc8e 4353 * work and any errors back to zebra so it can process the results.
c831033f 4354 */
c831033f 4355 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
14b0bc8e
MS
4356 zlog_debug("dplane has %d completed, %d errors, for zebra main",
4357 counter, error_counter);
c831033f
MS
4358
4359 /*
4c206c8f 4360 * Hand lists through the api to zebra main,
c831033f
MS
4361 * to reduce the number of lock/unlock cycles
4362 */
14b0bc8e 4363
4c206c8f
MS
4364 /* Call through to zebra main */
4365 (zdplane_info.dg_results_cb)(&error_list);
14b0bc8e 4366
4c206c8f 4367 TAILQ_INIT(&error_list);
14b0bc8e 4368
4c206c8f
MS
4369 /* Call through to zebra main */
4370 (zdplane_info.dg_results_cb)(&work_list);
c831033f 4371
4c206c8f 4372 TAILQ_INIT(&work_list);
c831033f
MS
4373
4374done:
4375 return 0;
4376}
4377
4dfd7a02
MS
4378/*
4379 * Final phase of shutdown, after all work enqueued to dplane has been
4380 * processed. This is called from the zebra main pthread context.
4381 */
4382void zebra_dplane_shutdown(void)
4383{
6ba8db21
RZ
4384 struct zebra_dplane_provider *dp;
4385
4dfd7a02
MS
4386 if (IS_ZEBRA_DEBUG_DPLANE)
4387 zlog_debug("Zebra dataplane shutdown called");
1d11b21f
MS
4388
4389 /* Stop dplane thread, if it's running */
4390
25779064 4391 zdplane_info.dg_run = false;
1d11b21f 4392
9344d3fc
SW
4393 if (zdplane_info.dg_t_update)
4394 thread_cancel_async(zdplane_info.dg_t_update->master,
4395 &zdplane_info.dg_t_update, NULL);
1d11b21f 4396
d8c16a95
MS
4397 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
4398
4399 /* Destroy pthread */
4400 frr_pthread_destroy(zdplane_info.dg_pthread);
4401 zdplane_info.dg_pthread = NULL;
4402 zdplane_info.dg_master = NULL;
4dfd7a02 4403
3c0e1622
MS
4404 /* Notify provider(s) of final shutdown.
4405 * Note that this call is in the main pthread, so providers must
4406 * be prepared for that.
4407 */
6ba8db21
RZ
4408 TAILQ_FOREACH(dp, &zdplane_info.dg_providers_q, dp_prov_link) {
4409 if (dp->dp_fini == NULL)
4410 continue;
4411
4412 dp->dp_fini(dp, false);
4413 }
c831033f
MS
4414
4415 /* TODO -- Clean-up provider objects */
4416
4417 /* TODO -- Clean queue(s), free memory */
4418}
4419
4420/*
4421 * Initialize the dataplane module during startup, internal/private version
4422 */
2561d12e 4423static void zebra_dplane_init_internal(void)
c831033f
MS
4424{
4425 memset(&zdplane_info, 0, sizeof(zdplane_info));
4426
4427 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
1d11b21f 4428
3fe4ccc4 4429 TAILQ_INIT(&zdplane_info.dg_update_ctx_q);
c831033f 4430 TAILQ_INIT(&zdplane_info.dg_providers_q);
1d11b21f 4431
c831033f
MS
4432 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
4433
4434 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
4435
4436 /* Register default kernel 'provider' during init */
4437 dplane_provider_init();
e5a60d82 4438}
c831033f 4439
e5a60d82
MS
4440/*
4441 * Start the dataplane pthread. This step needs to be run later than the
4442 * 'init' step, in case zebra has fork-ed.
4443 */
4444void zebra_dplane_start(void)
4445{
1dd4ea8a 4446 struct zebra_dplane_provider *prov;
c831033f
MS
4447 struct frr_pthread_attr pattr = {
4448 .start = frr_pthread_attr_default.start,
4449 .stop = frr_pthread_attr_default.stop
4450 };
4451
1dd4ea8a
MS
4452 /* Start dataplane pthread */
4453
c831033f 4454 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
0eca319c 4455 "zebra_dplane");
c831033f
MS
4456
4457 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
4458
e5a60d82
MS
4459 zdplane_info.dg_run = true;
4460
c831033f
MS
4461 /* Enqueue an initial event for the dataplane pthread */
4462 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
4463 &zdplane_info.dg_t_update);
4464
1dd4ea8a
MS
4465 /* Call start callbacks for registered providers */
4466
4467 DPLANE_LOCK();
4468 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4469 DPLANE_UNLOCK();
4470
4471 while (prov) {
4472
4473 if (prov->dp_start)
4474 (prov->dp_start)(prov);
4475
4476 /* Locate next provider */
4477 DPLANE_LOCK();
4478 prov = TAILQ_NEXT(prov, dp_prov_link);
4479 DPLANE_UNLOCK();
4480 }
4481
c831033f 4482 frr_pthread_run(zdplane_info.dg_pthread, NULL);
18c37974
MS
4483}
4484
7cdb1a84 4485/*
b8e0423d 4486 * Initialize the dataplane module at startup; called by zebra rib_init()
7cdb1a84 4487 */
4c206c8f 4488void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
7cdb1a84 4489{
2561d12e 4490 zebra_dplane_init_internal();
4c206c8f 4491 zdplane_info.dg_results_cb = results_fp;
7cdb1a84 4492}