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