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