]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_dplane.c
Merge pull request #5706 from mjstapp/fix_nh_debug_show
[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
e1f3a8eb
MS
1026/*
1027 * Set the nexthops associated with a context: note that processing code
1028 * may well expect that nexthops are in canonical (sorted) order, so we
1029 * will enforce that here.
1030 */
593e4eb1
MS
1031void dplane_ctx_set_nexthops(struct zebra_dplane_ctx *ctx, struct nexthop *nh)
1032{
1033 DPLANE_CTX_VALID(ctx);
1034
1035 if (ctx->u.rinfo.zd_ng.nexthop) {
1036 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
1037 ctx->u.rinfo.zd_ng.nexthop = NULL;
1038 }
e1f3a8eb 1039 nexthop_group_copy_nh_sorted(&(ctx->u.rinfo.zd_ng), nh);
593e4eb1
MS
1040}
1041
25779064
MS
1042const struct nexthop_group *dplane_ctx_get_ng(
1043 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1044{
1045 DPLANE_CTX_VALID(ctx);
1046
0f461727 1047 return &(ctx->u.rinfo.zd_ng);
7cdb1a84
MS
1048}
1049
25779064
MS
1050const struct nexthop_group *dplane_ctx_get_old_ng(
1051 const struct zebra_dplane_ctx *ctx)
01ce7cba
MS
1052{
1053 DPLANE_CTX_VALID(ctx);
1054
0f461727 1055 return &(ctx->u.rinfo.zd_old_ng);
01ce7cba
MS
1056}
1057
25779064
MS
1058const struct zebra_dplane_info *dplane_ctx_get_ns(
1059 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1060{
1061 DPLANE_CTX_VALID(ctx);
1062
5709131c 1063 return &(ctx->zd_ns_info);
7cdb1a84
MS
1064}
1065
f820d025 1066/* Accessors for nexthop information */
0c8215cb
SW
1067uint32_t dplane_ctx_get_nhe_id(const struct zebra_dplane_ctx *ctx)
1068{
1069 DPLANE_CTX_VALID(ctx);
1070 return ctx->u.rinfo.nhe.id;
1071}
1072
1073afi_t dplane_ctx_get_nhe_afi(const struct zebra_dplane_ctx *ctx)
1074{
1075 DPLANE_CTX_VALID(ctx);
1076 return ctx->u.rinfo.nhe.afi;
1077}
1078
1079vrf_id_t dplane_ctx_get_nhe_vrf_id(const struct zebra_dplane_ctx *ctx)
f820d025
SW
1080{
1081 DPLANE_CTX_VALID(ctx);
0c8215cb
SW
1082 return ctx->u.rinfo.nhe.vrf_id;
1083}
1084
38e40db1 1085int dplane_ctx_get_nhe_type(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1086{
1087 DPLANE_CTX_VALID(ctx);
38e40db1 1088 return ctx->u.rinfo.nhe.type;
0c8215cb
SW
1089}
1090
1091const struct nexthop_group *
1092dplane_ctx_get_nhe_ng(const struct zebra_dplane_ctx *ctx)
1093{
1094 DPLANE_CTX_VALID(ctx);
1095 return &(ctx->u.rinfo.nhe.ng);
1096}
1097
e22e8001
SW
1098const struct nh_grp *
1099dplane_ctx_get_nhe_nh_grp(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1100{
1101 DPLANE_CTX_VALID(ctx);
e22e8001 1102 return ctx->u.rinfo.nhe.nh_grp;
0c8215cb
SW
1103}
1104
e22e8001 1105uint8_t dplane_ctx_get_nhe_nh_grp_count(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1106{
1107 DPLANE_CTX_VALID(ctx);
e22e8001 1108 return ctx->u.rinfo.nhe.nh_grp_count;
f820d025
SW
1109}
1110
0f461727
MS
1111/* Accessors for LSP information */
1112
1113mpls_label_t dplane_ctx_get_in_label(const struct zebra_dplane_ctx *ctx)
1114{
1115 DPLANE_CTX_VALID(ctx);
1116
1117 return ctx->u.lsp.ile.in_label;
1118}
1119
3ab54059
MS
1120void dplane_ctx_set_in_label(struct zebra_dplane_ctx *ctx, mpls_label_t label)
1121{
1122 DPLANE_CTX_VALID(ctx);
1123
1124 ctx->u.lsp.ile.in_label = label;
1125}
1126
0f461727
MS
1127uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx *ctx)
1128{
1129 DPLANE_CTX_VALID(ctx);
1130
1131 return ctx->u.lsp.addr_family;
1132}
1133
3ab54059
MS
1134void dplane_ctx_set_addr_family(struct zebra_dplane_ctx *ctx,
1135 uint8_t family)
1136{
1137 DPLANE_CTX_VALID(ctx);
1138
1139 ctx->u.lsp.addr_family = family;
1140}
1141
0f461727
MS
1142uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx *ctx)
1143{
1144 DPLANE_CTX_VALID(ctx);
1145
1146 return ctx->u.lsp.flags;
1147}
1148
3ab54059
MS
1149void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx *ctx,
1150 uint32_t flags)
1151{
1152 DPLANE_CTX_VALID(ctx);
1153
1154 ctx->u.lsp.flags = flags;
1155}
1156
81793ac1 1157const zebra_nhlfe_t *dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx *ctx)
0f461727
MS
1158{
1159 DPLANE_CTX_VALID(ctx);
1160
1161 return ctx->u.lsp.nhlfe_list;
1162}
1163
3ab54059
MS
1164zebra_nhlfe_t *dplane_ctx_add_nhlfe(struct zebra_dplane_ctx *ctx,
1165 enum lsp_types_t lsp_type,
1166 enum nexthop_types_t nh_type,
1167 union g_addr *gate,
1168 ifindex_t ifindex,
5065db0a
RW
1169 uint8_t num_labels,
1170 mpls_label_t out_labels[])
3ab54059
MS
1171{
1172 zebra_nhlfe_t *nhlfe;
1173
1174 DPLANE_CTX_VALID(ctx);
1175
1176 nhlfe = zebra_mpls_lsp_add_nhlfe(&(ctx->u.lsp),
1177 lsp_type, nh_type, gate,
5065db0a 1178 ifindex, num_labels, out_labels);
3ab54059
MS
1179
1180 return nhlfe;
1181}
1182
81793ac1
MS
1183const zebra_nhlfe_t *
1184dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx *ctx)
0f461727
MS
1185{
1186 DPLANE_CTX_VALID(ctx);
1187
1188 return ctx->u.lsp.best_nhlfe;
1189}
1190
3ab54059
MS
1191const zebra_nhlfe_t *
1192dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx *ctx,
1193 zebra_nhlfe_t *nhlfe)
1194{
1195 DPLANE_CTX_VALID(ctx);
1196
1197 ctx->u.lsp.best_nhlfe = nhlfe;
1198 return ctx->u.lsp.best_nhlfe;
1199}
1200
0f461727
MS
1201uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx *ctx)
1202{
1203 DPLANE_CTX_VALID(ctx);
1204
1205 return ctx->u.lsp.num_ecmp;
1206}
1207
d613b8e1
MS
1208mpls_label_t dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx *ctx)
1209{
1210 DPLANE_CTX_VALID(ctx);
1211
1212 return ctx->u.pw.local_label;
1213}
1214
1215mpls_label_t dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx *ctx)
1216{
1217 DPLANE_CTX_VALID(ctx);
1218
1219 return ctx->u.pw.remote_label;
1220}
1221
1222int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx *ctx)
1223{
1224 DPLANE_CTX_VALID(ctx);
1225
1226 return ctx->u.pw.type;
1227}
1228
1229int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx *ctx)
1230{
1231 DPLANE_CTX_VALID(ctx);
1232
1233 return ctx->u.pw.af;
1234}
1235
1236uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx *ctx)
1237{
1238 DPLANE_CTX_VALID(ctx);
1239
1240 return ctx->u.pw.flags;
1241}
1242
1243int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx *ctx)
1244{
1245 DPLANE_CTX_VALID(ctx);
1246
1247 return ctx->u.pw.status;
1248}
1249
16d69787 1250const union g_addr *dplane_ctx_get_pw_dest(
d613b8e1
MS
1251 const struct zebra_dplane_ctx *ctx)
1252{
1253 DPLANE_CTX_VALID(ctx);
1254
16d69787 1255 return &(ctx->u.pw.dest);
d613b8e1
MS
1256}
1257
1258const union pw_protocol_fields *dplane_ctx_get_pw_proto(
1259 const struct zebra_dplane_ctx *ctx)
1260{
1261 DPLANE_CTX_VALID(ctx);
1262
1263 return &(ctx->u.pw.fields);
1264}
1265
09cd307c
MS
1266const struct nexthop_group *
1267dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx *ctx)
1268{
1269 DPLANE_CTX_VALID(ctx);
1270
1271 return &(ctx->u.pw.nhg);
1272}
1273
a4a4802a 1274/* Accessors for interface information */
a4a4802a
MS
1275uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx *ctx)
1276{
1277 DPLANE_CTX_VALID(ctx);
1278
1279 return ctx->u.intf.metric;
1280}
1281
1282/* Is interface addr p2p? */
1283bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx *ctx)
1284{
1285 DPLANE_CTX_VALID(ctx);
1286
1287 return (ctx->u.intf.flags & DPLANE_INTF_CONNECTED);
1288}
1289
1290bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx *ctx)
1291{
1292 DPLANE_CTX_VALID(ctx);
1293
1294 return (ctx->u.intf.flags & DPLANE_INTF_SECONDARY);
1295}
1296
64168803
MS
1297bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx *ctx)
1298{
1299 DPLANE_CTX_VALID(ctx);
1300
1301 return (ctx->u.intf.flags & DPLANE_INTF_BROADCAST);
1302}
1303
a4a4802a
MS
1304const struct prefix *dplane_ctx_get_intf_addr(
1305 const struct zebra_dplane_ctx *ctx)
1306{
1307 DPLANE_CTX_VALID(ctx);
1308
1309 return &(ctx->u.intf.prefix);
1310}
1311
1312bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx *ctx)
1313{
1314 DPLANE_CTX_VALID(ctx);
1315
1316 return (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST);
1317}
1318
1319const struct prefix *dplane_ctx_get_intf_dest(
1320 const struct zebra_dplane_ctx *ctx)
1321{
1322 DPLANE_CTX_VALID(ctx);
1323
1324 if (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST)
1325 return &(ctx->u.intf.dest_prefix);
1326 else
1327 return NULL;
1328}
1329
1330bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx *ctx)
1331{
1332 DPLANE_CTX_VALID(ctx);
1333
1334 return (ctx->u.intf.flags & DPLANE_INTF_HAS_LABEL);
1335}
1336
1337const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx *ctx)
1338{
1339 DPLANE_CTX_VALID(ctx);
1340
1341 return ctx->u.intf.label;
1342}
1343
7597ac7b
MS
1344/* Accessors for MAC information */
1345vlanid_t dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx *ctx)
1346{
1347 DPLANE_CTX_VALID(ctx);
1348 return ctx->u.macinfo.vid;
1349}
1350
1351bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx *ctx)
1352{
1353 DPLANE_CTX_VALID(ctx);
1354 return ctx->u.macinfo.is_sticky;
1355}
1356
1357const struct ethaddr *dplane_ctx_mac_get_addr(
1358 const struct zebra_dplane_ctx *ctx)
1359{
1360 DPLANE_CTX_VALID(ctx);
1361 return &(ctx->u.macinfo.mac);
1362}
1363
1364const struct in_addr *dplane_ctx_mac_get_vtep_ip(
1365 const struct zebra_dplane_ctx *ctx)
1366{
1367 DPLANE_CTX_VALID(ctx);
1368 return &(ctx->u.macinfo.vtep_ip);
1369}
1370
478566d6
MS
1371ifindex_t dplane_ctx_mac_get_br_ifindex(const struct zebra_dplane_ctx *ctx)
1372{
1373 DPLANE_CTX_VALID(ctx);
1374 return ctx->u.macinfo.br_ifindex;
1375}
1376
931fa60c
MS
1377/* Accessors for neighbor information */
1378const struct ipaddr *dplane_ctx_neigh_get_ipaddr(
1379 const struct zebra_dplane_ctx *ctx)
1380{
1381 DPLANE_CTX_VALID(ctx);
1382 return &(ctx->u.neigh.ip_addr);
1383}
1384
1385const struct ethaddr *dplane_ctx_neigh_get_mac(
1386 const struct zebra_dplane_ctx *ctx)
1387{
1388 DPLANE_CTX_VALID(ctx);
1389 return &(ctx->u.neigh.mac);
1390}
1391
1392uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx *ctx)
1393{
1394 DPLANE_CTX_VALID(ctx);
1395 return ctx->u.neigh.flags;
1396}
1397
1398uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx *ctx)
1399{
1400 DPLANE_CTX_VALID(ctx);
1401 return ctx->u.neigh.state;
1402}
1403
7cdb1a84
MS
1404/*
1405 * End of dplane context accessors
1406 */
1407
c831033f 1408
91f16812
MS
1409/*
1410 * Retrieve the limit on the number of pending, unprocessed updates.
1411 */
1412uint32_t dplane_get_in_queue_limit(void)
1413{
1414 return atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
1415 memory_order_relaxed);
1416}
1417
1418/*
1419 * Configure limit on the number of pending, queued updates.
1420 */
1421void dplane_set_in_queue_limit(uint32_t limit, bool set)
1422{
1423 /* Reset to default on 'unset' */
1424 if (!set)
1425 limit = DPLANE_DEFAULT_MAX_QUEUED;
1426
1427 atomic_store_explicit(&zdplane_info.dg_max_queued_updates, limit,
1428 memory_order_relaxed);
1429}
1430
1431/*
1432 * Retrieve the current queue depth of incoming, unprocessed updates
1433 */
1434uint32_t dplane_get_in_queue_len(void)
1435{
1436 return atomic_load_explicit(&zdplane_info.dg_routes_queued,
1437 memory_order_seq_cst);
1438}
1439
16c628de
MS
1440/*
1441 * Common dataplane context init with zebra namespace info.
1442 */
1443static int dplane_ctx_ns_init(struct zebra_dplane_ctx *ctx,
1444 struct zebra_ns *zns,
1445 bool is_update)
1446{
1447 dplane_info_from_zns(&(ctx->zd_ns_info), zns);
1448
1449#if defined(HAVE_NETLINK)
1450 /* Increment message counter after copying to context struct - may need
1451 * two messages in some 'update' cases.
1452 */
1453 if (is_update)
1454 zns->netlink_dplane.seq += 2;
1455 else
1456 zns->netlink_dplane.seq++;
1457#endif /* HAVE_NETLINK */
1458
1459 return AOK;
1460}
1461
7cdb1a84
MS
1462/*
1463 * Initialize a context block for a route update from zebra data structs.
1464 */
25779064 1465static int dplane_ctx_route_init(struct zebra_dplane_ctx *ctx,
5709131c 1466 enum dplane_op_e op,
7cdb1a84
MS
1467 struct route_node *rn,
1468 struct route_entry *re)
1469{
1470 int ret = EINVAL;
1471 const struct route_table *table = NULL;
1472 const rib_table_info_t *info;
1473 const struct prefix *p, *src_p;
1474 struct zebra_ns *zns;
1475 struct zebra_vrf *zvrf;
f183e380 1476 struct nexthop *nexthop;
7cdb1a84 1477
5709131c 1478 if (!ctx || !rn || !re)
7cdb1a84 1479 goto done;
7cdb1a84
MS
1480
1481 ctx->zd_op = op;
14b0bc8e 1482 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
7cdb1a84 1483
0f461727
MS
1484 ctx->u.rinfo.zd_type = re->type;
1485 ctx->u.rinfo.zd_old_type = re->type;
7cdb1a84
MS
1486
1487 /* Prefixes: dest, and optional source */
1488 srcdest_rnode_prefixes(rn, &p, &src_p);
1489
0f461727 1490 prefix_copy(&(ctx->u.rinfo.zd_dest), p);
7cdb1a84 1491
5709131c 1492 if (src_p)
0f461727 1493 prefix_copy(&(ctx->u.rinfo.zd_src), src_p);
5709131c 1494 else
0f461727 1495 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(ctx->u.rinfo.zd_src));
7cdb1a84
MS
1496
1497 ctx->zd_table_id = re->table;
1498
0f461727
MS
1499 ctx->u.rinfo.zd_metric = re->metric;
1500 ctx->u.rinfo.zd_old_metric = re->metric;
7cdb1a84 1501 ctx->zd_vrf_id = re->vrf_id;
0f461727
MS
1502 ctx->u.rinfo.zd_mtu = re->mtu;
1503 ctx->u.rinfo.zd_nexthop_mtu = re->nexthop_mtu;
1504 ctx->u.rinfo.zd_instance = re->instance;
1505 ctx->u.rinfo.zd_tag = re->tag;
1506 ctx->u.rinfo.zd_old_tag = re->tag;
1507 ctx->u.rinfo.zd_distance = re->distance;
7cdb1a84
MS
1508
1509 table = srcdest_rnode_table(rn);
1510 info = table->info;
1511
0f461727
MS
1512 ctx->u.rinfo.zd_afi = info->afi;
1513 ctx->u.rinfo.zd_safi = info->safi;
7cdb1a84 1514
7cdb1a84 1515 /* Copy nexthops; recursive info is included too */
0eb97b86
MS
1516 copy_nexthops(&(ctx->u.rinfo.zd_ng.nexthop),
1517 re->nhe->nhg->nexthop, NULL);
7cdb1a84 1518
14b0bc8e 1519 /* Ensure that the dplane's nexthops flags are clear. */
0f461727 1520 for (ALL_NEXTHOPS(ctx->u.rinfo.zd_ng, nexthop))
f183e380 1521 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
f183e380 1522
cf363e1b
MS
1523 /* Don't need some info when capturing a system notification */
1524 if (op == DPLANE_OP_SYS_ROUTE_ADD ||
1525 op == DPLANE_OP_SYS_ROUTE_DELETE) {
1526 ret = AOK;
1527 goto done;
1528 }
1529
1530 /* Extract ns info - can't use pointers to 'core' structs */
1531 zvrf = vrf_info_lookup(re->vrf_id);
1532 zns = zvrf->zns;
1533 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_ROUTE_UPDATE));
1534
6df59152 1535#ifdef HAVE_NETLINK
81505946 1536 if (re->nhe_id) {
ae9bfa06
SW
1537 struct nhg_hash_entry *nhe =
1538 zebra_nhg_resolve(zebra_nhg_lookup_id(re->nhe_id));
6df59152 1539
ae9bfa06 1540 ctx->u.rinfo.nhe.id = nhe->id;
6df59152 1541 /*
ae9bfa06
SW
1542 * Check if the nhe is installed/queued before doing anything
1543 * with this route.
08c51a38
SW
1544 *
1545 * If its a delete we only use the prefix anyway, so this only
1546 * matters for INSTALL/UPDATE.
6df59152 1547 */
08c51a38
SW
1548 if (((op == DPLANE_OP_ROUTE_INSTALL)
1549 || (op == DPLANE_OP_ROUTE_UPDATE))
1550 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
ae9bfa06 1551 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED)) {
6df59152
SW
1552 ret = ENOENT;
1553 goto done;
1554 }
98cda54a 1555 }
6df59152 1556#endif /* HAVE_NETLINK */
de3f5488 1557
7cdb1a84
MS
1558 /* Trying out the sequence number idea, so we can try to detect
1559 * when a result is stale.
1560 */
1485bbe7 1561 re->dplane_sequence = zebra_router_get_next_sequence();
7cdb1a84
MS
1562 ctx->zd_seq = re->dplane_sequence;
1563
1564 ret = AOK;
1565
1566done:
1567 return ret;
1568}
1569
f820d025
SW
1570/**
1571 * dplane_ctx_nexthop_init() - Initialize a context block for a nexthop update
1572 *
1573 * @ctx: Dataplane context to init
1574 * @op: Operation being performed
1575 * @nhe: Nexthop group hash entry
1576 *
1577 * Return: Result status
1578 */
1579static int dplane_ctx_nexthop_init(struct zebra_dplane_ctx *ctx,
1580 enum dplane_op_e op,
1581 struct nhg_hash_entry *nhe)
1582{
a7df21c4 1583 struct zebra_vrf *zvrf = NULL;
8e401b25 1584 struct zebra_ns *zns = NULL;
1909e63a 1585
f820d025
SW
1586 int ret = EINVAL;
1587
1588 if (!ctx || !nhe)
1589 goto done;
1590
1591 ctx->zd_op = op;
1592 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1593
1594 /* Copy over nhe info */
0c8215cb
SW
1595 ctx->u.rinfo.nhe.id = nhe->id;
1596 ctx->u.rinfo.nhe.afi = nhe->afi;
1597 ctx->u.rinfo.nhe.vrf_id = nhe->vrf_id;
38e40db1 1598 ctx->u.rinfo.nhe.type = nhe->type;
0c8215cb
SW
1599
1600 nexthop_group_copy(&(ctx->u.rinfo.nhe.ng), nhe->nhg);
1601
98cda54a
SW
1602 /* If its a group, convert it to a grp array of ids */
1603 if (!zebra_nhg_depends_is_empty(nhe)
1604 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE))
8dbc800f
SW
1605 ctx->u.rinfo.nhe.nh_grp_count = zebra_nhg_nhe2grp(
1606 ctx->u.rinfo.nhe.nh_grp, nhe, MULTIPATH_NUM);
f820d025 1607
a7df21c4
SW
1608 zvrf = vrf_info_lookup(nhe->vrf_id);
1609
1610 /*
1611 * Fallback to default namespace if the vrf got ripped out from under
1612 * us.
1613 */
1614 zns = zvrf ? zvrf->zns : zebra_ns_lookup(NS_DEFAULT);
1909e63a 1615
2d3c57e6
SW
1616 /*
1617 * TODO: Might not need to mark this as an update, since
1618 * it probably won't require two messages
1619 */
1909e63a
SW
1620 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_NH_UPDATE));
1621
f820d025
SW
1622 ret = AOK;
1623
1624done:
1625 return ret;
1626}
1627
16c628de
MS
1628/*
1629 * Capture information for an LSP update in a dplane context.
1630 */
1631static int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx,
1632 enum dplane_op_e op,
1633 zebra_lsp_t *lsp)
1634{
1635 int ret = AOK;
1636 zebra_nhlfe_t *nhlfe, *new_nhlfe;
1637
1638 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1639 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1640 dplane_op2str(op), lsp->ile.in_label,
1641 lsp->num_ecmp);
1642
1643 ctx->zd_op = op;
1644 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1645
1646 /* Capture namespace info */
1647 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
1648 (op == DPLANE_OP_LSP_UPDATE));
1649
1650 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
1651
1652 ctx->u.lsp.ile = lsp->ile;
1653 ctx->u.lsp.addr_family = lsp->addr_family;
1654 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
1655 ctx->u.lsp.flags = lsp->flags;
1656
1657 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
1658 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
1659 /* Not sure if this is meaningful... */
1660 if (nhlfe->nexthop == NULL)
1661 continue;
1662
1663 new_nhlfe =
1664 zebra_mpls_lsp_add_nhlfe(
1665 &(ctx->u.lsp),
1666 nhlfe->type,
1667 nhlfe->nexthop->type,
1668 &(nhlfe->nexthop->gate),
1669 nhlfe->nexthop->ifindex,
5065db0a
RW
1670 nhlfe->nexthop->nh_label->num_labels,
1671 nhlfe->nexthop->nh_label->label);
16c628de
MS
1672
1673 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
1674 ret = ENOMEM;
1675 break;
1676 }
1677
1678 /* Need to copy flags too */
1679 new_nhlfe->flags = nhlfe->flags;
1680 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
1681
1682 if (nhlfe == lsp->best_nhlfe)
1683 ctx->u.lsp.best_nhlfe = new_nhlfe;
1684 }
1685
1686 /* On error the ctx will be cleaned-up, so we don't need to
1687 * deal with any allocated nhlfe or nexthop structs here.
1688 */
1689
1690 return ret;
1691}
1692
97d8d05a
MS
1693/*
1694 * Capture information for an LSP update in a dplane context.
1695 */
1696static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
1697 enum dplane_op_e op,
1698 struct zebra_pw *pw)
1699{
09cd307c
MS
1700 struct prefix p;
1701 afi_t afi;
1702 struct route_table *table;
1703 struct route_node *rn;
1704 struct route_entry *re;
1705
97d8d05a
MS
1706 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1707 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
1708 dplane_op2str(op), pw->ifname, pw->local_label,
1709 pw->remote_label);
1710
1711 ctx->zd_op = op;
1712 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1713
1714 /* Capture namespace info: no netlink support as of 12/18,
1715 * but just in case...
1716 */
1717 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
1718
1719 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
1720
1721 /* This name appears to be c-string, so we use string copy. */
7c7ef4a8 1722 strlcpy(ctx->zd_ifname, pw->ifname, sizeof(ctx->zd_ifname));
16d69787 1723
9bd9717b 1724 ctx->zd_vrf_id = pw->vrf_id;
7c7ef4a8 1725 ctx->zd_ifindex = pw->ifindex;
97d8d05a
MS
1726 ctx->u.pw.type = pw->type;
1727 ctx->u.pw.af = pw->af;
1728 ctx->u.pw.local_label = pw->local_label;
1729 ctx->u.pw.remote_label = pw->remote_label;
1730 ctx->u.pw.flags = pw->flags;
1731
16d69787 1732 ctx->u.pw.dest = pw->nexthop;
97d8d05a
MS
1733
1734 ctx->u.pw.fields = pw->data;
1735
09cd307c
MS
1736 /* Capture nexthop info for the pw destination. We need to look
1737 * up and use zebra datastructs, but we're running in the zebra
1738 * pthread here so that should be ok.
1739 */
1740 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
1741 p.family = pw->af;
1742 p.prefixlen = ((pw->af == AF_INET) ?
1743 IPV4_MAX_PREFIXLEN : IPV6_MAX_PREFIXLEN);
1744
1745 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
1746 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
1747 if (table) {
1748 rn = route_node_match(table, &p);
1749 if (rn) {
1750 RNODE_FOREACH_RE(rn, re) {
1751 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
1752 break;
1753 }
1754
1755 if (re)
1756 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
0eb97b86 1757 re->nhe->nhg->nexthop, NULL);
09cd307c
MS
1758
1759 route_unlock_node(rn);
1760 }
1761 }
1762
9f2d0354 1763 return AOK;
97d8d05a
MS
1764}
1765
7cdb1a84 1766/*
3fe4ccc4 1767 * Enqueue a new update,
16c628de 1768 * and ensure an event is active for the dataplane pthread.
7cdb1a84 1769 */
3fe4ccc4 1770static int dplane_update_enqueue(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1771{
1772 int ret = EINVAL;
91f16812 1773 uint32_t high, curr;
7cdb1a84 1774
16c628de 1775 /* Enqueue for processing by the dataplane pthread */
7cdb1a84
MS
1776 DPLANE_LOCK();
1777 {
3fe4ccc4 1778 TAILQ_INSERT_TAIL(&zdplane_info.dg_update_ctx_q, ctx,
25779064 1779 zd_q_entries);
7cdb1a84
MS
1780 }
1781 DPLANE_UNLOCK();
1782
e07e9549
MS
1783 curr = atomic_add_fetch_explicit(
1784#ifdef __clang__
1785 /* TODO -- issue with the clang atomic/intrinsics currently;
1786 * casting away the 'Atomic'-ness of the variable works.
1787 */
1788 (uint32_t *)&(zdplane_info.dg_routes_queued),
1789#else
1790 &(zdplane_info.dg_routes_queued),
1791#endif
1792 1, memory_order_seq_cst);
91f16812
MS
1793
1794 /* Maybe update high-water counter also */
1795 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
1796 memory_order_seq_cst);
1797 while (high < curr) {
1798 if (atomic_compare_exchange_weak_explicit(
1799 &zdplane_info.dg_routes_queued_max,
1800 &high, curr,
1801 memory_order_seq_cst,
1802 memory_order_seq_cst))
1803 break;
1804 }
1805
7cdb1a84 1806 /* Ensure that an event for the dataplane thread is active */
c831033f 1807 ret = dplane_provider_work_ready();
7cdb1a84 1808
5709131c 1809 return ret;
7cdb1a84
MS
1810}
1811
7cdb1a84
MS
1812/*
1813 * Utility that prepares a route update and enqueues it for processing
1814 */
655d681a
MS
1815static enum zebra_dplane_result
1816dplane_route_update_internal(struct route_node *rn,
1817 struct route_entry *re,
1818 struct route_entry *old_re,
5709131c 1819 enum dplane_op_e op)
7cdb1a84 1820{
655d681a 1821 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 1822 int ret = EINVAL;
25779064 1823 struct zebra_dplane_ctx *ctx = NULL;
7cdb1a84
MS
1824
1825 /* Obtain context block */
1826 ctx = dplane_ctx_alloc();
7cdb1a84
MS
1827
1828 /* Init context with info from zebra data structs */
1829 ret = dplane_ctx_route_init(ctx, op, rn, re);
1830 if (ret == AOK) {
1831 /* Capture some extra info for update case
1832 * where there's a different 'old' route.
1833 */
b8e0423d
MS
1834 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
1835 old_re && (old_re != re)) {
7cdb1a84
MS
1836 ctx->zd_is_update = true;
1837
1485bbe7
DS
1838 old_re->dplane_sequence =
1839 zebra_router_get_next_sequence();
7cdb1a84
MS
1840 ctx->zd_old_seq = old_re->dplane_sequence;
1841
0f461727
MS
1842 ctx->u.rinfo.zd_old_tag = old_re->tag;
1843 ctx->u.rinfo.zd_old_type = old_re->type;
1844 ctx->u.rinfo.zd_old_instance = old_re->instance;
1845 ctx->u.rinfo.zd_old_distance = old_re->distance;
1846 ctx->u.rinfo.zd_old_metric = old_re->metric;
01ce7cba
MS
1847
1848#ifndef HAVE_NETLINK
f183e380
MS
1849 /* For bsd, capture previous re's nexthops too, sigh.
1850 * We'll need these to do per-nexthop deletes.
1851 */
0f461727 1852 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
0eb97b86 1853 old_re->nhe->nhg->nexthop, NULL);
01ce7cba 1854#endif /* !HAVE_NETLINK */
7cdb1a84
MS
1855 }
1856
1857 /* Enqueue context for processing */
3fe4ccc4 1858 ret = dplane_update_enqueue(ctx);
7cdb1a84
MS
1859 }
1860
91f16812 1861 /* Update counter */
25779064 1862 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
1d11b21f
MS
1863 memory_order_relaxed);
1864
91f16812 1865 if (ret == AOK)
655d681a 1866 result = ZEBRA_DPLANE_REQUEST_QUEUED;
16c628de 1867 else {
6df59152
SW
1868 if (ret == ENOENT)
1869 result = ZEBRA_DPLANE_REQUEST_SUCCESS;
1870 else
1871 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
1872 1, memory_order_relaxed);
16c628de
MS
1873 if (ctx)
1874 dplane_ctx_free(&ctx);
1d11b21f 1875 }
7cdb1a84 1876
5709131c 1877 return result;
7cdb1a84
MS
1878}
1879
f820d025
SW
1880/**
1881 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
1882 *
1883 * @nhe: Nexthop group hash entry where the change occured
1884 * @op: The operation to be enqued
1885 *
1886 * Return: Result of the change
1887 */
1888static enum zebra_dplane_result
1889dplane_nexthop_update_internal(struct nhg_hash_entry *nhe, enum dplane_op_e op)
1890{
1891 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
1892 int ret = EINVAL;
1893 struct zebra_dplane_ctx *ctx = NULL;
1894
1895 /* Obtain context block */
1896 ctx = dplane_ctx_alloc();
1897 if (!ctx) {
1898 ret = ENOMEM;
1899 goto done;
1900 }
1901
1902 ret = dplane_ctx_nexthop_init(ctx, op, nhe);
2d3c57e6 1903 if (ret == AOK)
f820d025 1904 ret = dplane_update_enqueue(ctx);
2d3c57e6 1905
f820d025
SW
1906done:
1907 /* Update counter */
1908 atomic_fetch_add_explicit(&zdplane_info.dg_nexthops_in, 1,
1909 memory_order_relaxed);
1910
1911 if (ret == AOK)
1912 result = ZEBRA_DPLANE_REQUEST_QUEUED;
1913 else {
81505946
SW
1914 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
1915 memory_order_relaxed);
f820d025
SW
1916 if (ctx)
1917 dplane_ctx_free(&ctx);
1918 }
1919
1920 return result;
1921}
1922
7cdb1a84
MS
1923/*
1924 * Enqueue a route 'add' for the dataplane.
1925 */
655d681a
MS
1926enum zebra_dplane_result dplane_route_add(struct route_node *rn,
1927 struct route_entry *re)
7cdb1a84 1928{
655d681a 1929 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 1930
5709131c 1931 if (rn == NULL || re == NULL)
7cdb1a84 1932 goto done;
7cdb1a84
MS
1933
1934 ret = dplane_route_update_internal(rn, re, NULL,
1935 DPLANE_OP_ROUTE_INSTALL);
1936
1937done:
5709131c 1938 return ret;
7cdb1a84
MS
1939}
1940
1941/*
1942 * Enqueue a route update for the dataplane.
1943 */
655d681a
MS
1944enum zebra_dplane_result dplane_route_update(struct route_node *rn,
1945 struct route_entry *re,
1946 struct route_entry *old_re)
7cdb1a84 1947{
655d681a 1948 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 1949
5709131c 1950 if (rn == NULL || re == NULL)
7cdb1a84 1951 goto done;
7cdb1a84
MS
1952
1953 ret = dplane_route_update_internal(rn, re, old_re,
1954 DPLANE_OP_ROUTE_UPDATE);
7cdb1a84 1955done:
5709131c 1956 return ret;
7cdb1a84
MS
1957}
1958
1959/*
1960 * Enqueue a route removal for the dataplane.
1961 */
655d681a
MS
1962enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
1963 struct route_entry *re)
7cdb1a84 1964{
655d681a 1965 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 1966
5709131c 1967 if (rn == NULL || re == NULL)
7cdb1a84 1968 goto done;
7cdb1a84
MS
1969
1970 ret = dplane_route_update_internal(rn, re, NULL,
1971 DPLANE_OP_ROUTE_DELETE);
1972
1973done:
5709131c 1974 return ret;
7cdb1a84
MS
1975}
1976
cf363e1b
MS
1977/*
1978 * Notify the dplane when system/connected routes change.
1979 */
1980enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
1981 struct route_entry *re)
1982{
1983 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
1984
1985 /* Ignore this event unless a provider plugin has requested it. */
1986 if (!zdplane_info.dg_sys_route_notifs) {
1987 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
1988 goto done;
1989 }
1990
1991 if (rn == NULL || re == NULL)
1992 goto done;
1993
1994 ret = dplane_route_update_internal(rn, re, NULL,
1995 DPLANE_OP_SYS_ROUTE_ADD);
1996
1997done:
1998 return ret;
1999}
2000
2001/*
2002 * Notify the dplane when system/connected routes are deleted.
2003 */
2004enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
2005 struct route_entry *re)
2006{
2007 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2008
2009 /* Ignore this event unless a provider plugin has requested it. */
2010 if (!zdplane_info.dg_sys_route_notifs) {
2011 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2012 goto done;
2013 }
2014
2015 if (rn == NULL || re == NULL)
2016 goto done;
2017
2018 ret = dplane_route_update_internal(rn, re, NULL,
2019 DPLANE_OP_SYS_ROUTE_DELETE);
2020
2021done:
2022 return ret;
2023}
2024
188a00e0
MS
2025/*
2026 * Update from an async notification, to bring other fibs up-to-date.
2027 */
2028enum zebra_dplane_result
2029dplane_route_notif_update(struct route_node *rn,
2030 struct route_entry *re,
2031 enum dplane_op_e op,
2032 struct zebra_dplane_ctx *ctx)
2033{
2034 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2035 struct zebra_dplane_ctx *new_ctx = NULL;
2036 struct nexthop *nexthop;
2037
2038 if (rn == NULL || re == NULL)
2039 goto done;
2040
2041 new_ctx = dplane_ctx_alloc();
2042 if (new_ctx == NULL)
2043 goto done;
2044
2045 /* Init context with info from zebra data structs */
2046 dplane_ctx_route_init(new_ctx, op, rn, re);
2047
2048 /* For add/update, need to adjust the nexthops so that we match
2049 * the notification state, which may not be the route-entry/RIB
2050 * state.
2051 */
2052 if (op == DPLANE_OP_ROUTE_UPDATE ||
2053 op == DPLANE_OP_ROUTE_INSTALL) {
2054
2055 nexthops_free(new_ctx->u.rinfo.zd_ng.nexthop);
2056 new_ctx->u.rinfo.zd_ng.nexthop = NULL;
2057
2058 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
2059 (rib_active_nhg(re))->nexthop, NULL);
2060
2061 for (ALL_NEXTHOPS(new_ctx->u.rinfo.zd_ng, nexthop))
2062 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2063
2064 }
2065
2066 /* Capture info about the source of the notification, in 'ctx' */
2067 dplane_ctx_set_notif_provider(new_ctx,
2068 dplane_ctx_get_notif_provider(ctx));
2069
3fe4ccc4 2070 dplane_update_enqueue(new_ctx);
188a00e0
MS
2071
2072 ret = ZEBRA_DPLANE_REQUEST_QUEUED;
2073
2074done:
2075 return ret;
2076}
2077
f820d025
SW
2078/*
2079 * Enqueue a nexthop add for the dataplane.
2080 */
2081enum zebra_dplane_result dplane_nexthop_add(struct nhg_hash_entry *nhe)
2082{
2083 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2084
2085 if (nhe)
2086 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_INSTALL);
2087 return ret;
2088}
2089
2090/*
2091 * Enqueue a nexthop update for the dataplane.
81505946
SW
2092 *
2093 * Might not need this func since zebra's nexthop objects should be immutable?
f820d025
SW
2094 */
2095enum zebra_dplane_result dplane_nexthop_update(struct nhg_hash_entry *nhe)
2096{
2097 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2098
2099 if (nhe)
2100 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_UPDATE);
2101 return ret;
2102}
2103
2104/*
2105 * Enqueue a nexthop removal for the dataplane.
2106 */
2107enum zebra_dplane_result dplane_nexthop_delete(struct nhg_hash_entry *nhe)
2108{
2109 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2110
2111 if (nhe)
2112 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_DELETE);
2113
2114 return ret;
2115}
2116
16c628de
MS
2117/*
2118 * Enqueue LSP add for the dataplane.
2119 */
2120enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp)
2121{
2122 enum zebra_dplane_result ret =
2123 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
2124
2125 return ret;
2126}
2127
2128/*
2129 * Enqueue LSP update for the dataplane.
2130 */
2131enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp)
2132{
2133 enum zebra_dplane_result ret =
2134 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
2135
2136 return ret;
2137}
2138
2139/*
2140 * Enqueue LSP delete for the dataplane.
2141 */
2142enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp)
2143{
2144 enum zebra_dplane_result ret =
2145 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
2146
2147 return ret;
2148}
2149
188a00e0
MS
2150/* Update or un-install resulting from an async notification */
2151enum zebra_dplane_result
2152dplane_lsp_notif_update(zebra_lsp_t *lsp,
2153 enum dplane_op_e op,
2154 struct zebra_dplane_ctx *notif_ctx)
2155{
2156 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2157 int ret = EINVAL;
2158 struct zebra_dplane_ctx *ctx = NULL;
2159
2160 /* Obtain context block */
2161 ctx = dplane_ctx_alloc();
2162 if (ctx == NULL) {
2163 ret = ENOMEM;
2164 goto done;
2165 }
2166
2167 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2168 if (ret != AOK)
2169 goto done;
2170
2171 /* Capture info about the source of the notification */
2172 dplane_ctx_set_notif_provider(
2173 ctx,
2174 dplane_ctx_get_notif_provider(notif_ctx));
2175
3fe4ccc4 2176 ret = dplane_update_enqueue(ctx);
188a00e0
MS
2177
2178done:
2179 /* Update counter */
2180 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2181 memory_order_relaxed);
2182
2183 if (ret == AOK)
2184 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2185 else {
2186 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2187 memory_order_relaxed);
2188 if (ctx)
2189 dplane_ctx_free(&ctx);
2190 }
2191 return result;
2192}
2193
97d8d05a
MS
2194/*
2195 * Enqueue pseudowire install for the dataplane.
2196 */
2197enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
2198{
2199 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
2200}
2201
2202/*
2203 * Enqueue pseudowire un-install for the dataplane.
2204 */
2205enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
2206{
2207 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
2208}
2209
16c628de
MS
2210/*
2211 * Common internal LSP update utility
2212 */
2213static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
2214 enum dplane_op_e op)
2215{
2216 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2217 int ret = EINVAL;
2218 struct zebra_dplane_ctx *ctx = NULL;
2219
2220 /* Obtain context block */
2221 ctx = dplane_ctx_alloc();
16c628de
MS
2222
2223 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2224 if (ret != AOK)
2225 goto done;
2226
3fe4ccc4 2227 ret = dplane_update_enqueue(ctx);
16c628de
MS
2228
2229done:
2230 /* Update counter */
2231 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2232 memory_order_relaxed);
2233
2234 if (ret == AOK)
2235 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2236 else {
2237 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2238 memory_order_relaxed);
d8e479a3 2239 dplane_ctx_free(&ctx);
16c628de
MS
2240 }
2241
2242 return result;
2243}
2244
97d8d05a
MS
2245/*
2246 * Internal, common handler for pseudowire updates.
2247 */
2248static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
2249 enum dplane_op_e op)
2250{
2251 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2252 int ret;
2253 struct zebra_dplane_ctx *ctx = NULL;
2254
2255 ctx = dplane_ctx_alloc();
97d8d05a
MS
2256
2257 ret = dplane_ctx_pw_init(ctx, op, pw);
2258 if (ret != AOK)
2259 goto done;
2260
3fe4ccc4 2261 ret = dplane_update_enqueue(ctx);
97d8d05a
MS
2262
2263done:
2264 /* Update counter */
2265 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
2266 memory_order_relaxed);
2267
2268 if (ret == AOK)
2269 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2270 else {
2271 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
2272 memory_order_relaxed);
d8e479a3 2273 dplane_ctx_free(&ctx);
97d8d05a
MS
2274 }
2275
2276 return result;
2277}
2278
a4a4802a
MS
2279/*
2280 * Enqueue interface address add for the dataplane.
2281 */
2282enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
2283 const struct connected *ifc)
2284{
64168803
MS
2285#if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2286 /* Extra checks for this OS path. */
2287
2288 /* Don't configure PtP addresses on broadcast ifs or reverse */
2289 if (!(ifp->flags & IFF_POINTOPOINT) != !CONNECTED_PEER(ifc)) {
2290 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_DPLANE)
2291 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2292
2293 return ZEBRA_DPLANE_REQUEST_FAILURE;
2294 }
2295
2296 /* Ensure that no existing installed v4 route conflicts with
2297 * the new interface prefix. This check must be done in the
2298 * zebra pthread context, and any route delete (if needed)
2299 * is enqueued before the interface address programming attempt.
2300 */
2301 if (ifc->address->family == AF_INET) {
2302 struct prefix_ipv4 *p;
2303
2304 p = (struct prefix_ipv4 *)ifc->address;
a36898e7 2305 rib_lookup_and_pushup(p, ifp->vrf_id);
64168803
MS
2306 }
2307#endif
2308
2309 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_INSTALL);
a4a4802a
MS
2310}
2311
2312/*
2313 * Enqueue interface address remove/uninstall for the dataplane.
2314 */
2315enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
2316 const struct connected *ifc)
2317{
64168803
MS
2318 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_UNINSTALL);
2319}
2320
2321static enum zebra_dplane_result intf_addr_update_internal(
2322 const struct interface *ifp, const struct connected *ifc,
2323 enum dplane_op_e op)
2324{
2325 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2326 int ret = EINVAL;
2327 struct zebra_dplane_ctx *ctx = NULL;
2328 struct zebra_ns *zns;
2329
2330 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2331 char addr_str[PREFIX_STRLEN];
2332
2333 prefix2str(ifc->address, addr_str, sizeof(addr_str));
2334
2335 zlog_debug("init intf ctx %s: idx %d, addr %u:%s",
a36898e7 2336 dplane_op2str(op), ifp->ifindex, ifp->vrf_id,
64168803
MS
2337 addr_str);
2338 }
2339
2340 ctx = dplane_ctx_alloc();
64168803
MS
2341
2342 ctx->zd_op = op;
2343 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
a36898e7 2344 ctx->zd_vrf_id = ifp->vrf_id;
64168803 2345
a36898e7 2346 zns = zebra_ns_lookup(ifp->vrf_id);
64168803
MS
2347 dplane_ctx_ns_init(ctx, zns, false);
2348
2349 /* Init the interface-addr-specific area */
2350 memset(&ctx->u.intf, 0, sizeof(ctx->u.intf));
2351
7c7ef4a8
MS
2352 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2353 ctx->zd_ifindex = ifp->ifindex;
64168803
MS
2354 ctx->u.intf.prefix = *(ifc->address);
2355
2356 if (if_is_broadcast(ifp))
2357 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
2358
2359 if (CONNECTED_PEER(ifc)) {
2360 ctx->u.intf.dest_prefix = *(ifc->destination);
2361 ctx->u.intf.flags |=
2362 (DPLANE_INTF_CONNECTED | DPLANE_INTF_HAS_DEST);
64168803
MS
2363 }
2364
2365 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
2366 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
2367
2368 if (ifc->label) {
2369 size_t len;
2370
2371 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
2372
2373 /* Use embedded buffer if it's adequate; else allocate. */
2374 len = strlen(ifc->label);
2375
2376 if (len < sizeof(ctx->u.intf.label_buf)) {
b7b7bf31 2377 strlcpy(ctx->u.intf.label_buf, ifc->label,
64168803
MS
2378 sizeof(ctx->u.intf.label_buf));
2379 ctx->u.intf.label = ctx->u.intf.label_buf;
2380 } else {
2381 ctx->u.intf.label = strdup(ifc->label);
2382 }
2383 }
2384
3fe4ccc4 2385 ret = dplane_update_enqueue(ctx);
64168803 2386
64168803
MS
2387 /* Increment counter */
2388 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addrs_in, 1,
2389 memory_order_relaxed);
2390
2391 if (ret == AOK)
2392 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2393 else {
2394 /* Error counter */
2395 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
2396 1, memory_order_relaxed);
d8e479a3 2397 dplane_ctx_free(&ctx);
64168803
MS
2398 }
2399
2400 return result;
a4a4802a
MS
2401}
2402
7597ac7b
MS
2403/*
2404 * Enqueue vxlan/evpn mac add (or update).
2405 */
2406enum zebra_dplane_result dplane_mac_add(const struct interface *ifp,
478566d6 2407 const struct interface *bridge_ifp,
7597ac7b
MS
2408 vlanid_t vid,
2409 const struct ethaddr *mac,
2410 struct in_addr vtep_ip,
2411 bool sticky)
2412{
2413 enum zebra_dplane_result result;
2414
2415 /* Use common helper api */
478566d6
MS
2416 result = mac_update_internal(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
2417 vid, mac, vtep_ip, sticky);
7597ac7b
MS
2418 return result;
2419}
2420
2421/*
2422 * Enqueue vxlan/evpn mac delete.
2423 */
2424enum zebra_dplane_result dplane_mac_del(const struct interface *ifp,
478566d6 2425 const struct interface *bridge_ifp,
7597ac7b
MS
2426 vlanid_t vid,
2427 const struct ethaddr *mac,
2428 struct in_addr vtep_ip)
2429{
2430 enum zebra_dplane_result result;
2431
2432 /* Use common helper api */
478566d6
MS
2433 result = mac_update_internal(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp,
2434 vid, mac, vtep_ip, false);
7597ac7b
MS
2435 return result;
2436}
2437
2438/*
2439 * Common helper api for MAC address/vxlan updates
2440 */
2441static enum zebra_dplane_result
2442mac_update_internal(enum dplane_op_e op,
2443 const struct interface *ifp,
478566d6 2444 const struct interface *br_ifp,
7597ac7b
MS
2445 vlanid_t vid,
2446 const struct ethaddr *mac,
2447 struct in_addr vtep_ip,
2448 bool sticky)
2449{
2450 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2451 int ret;
2452 struct zebra_dplane_ctx *ctx = NULL;
2453 struct zebra_ns *zns;
2454
036d93c0
MS
2455 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2456 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
2457
2458 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
2459 dplane_op2str(op),
2460 prefix_mac2str(mac, buf1, sizeof(buf1)),
2461 ifp->name,
2462 inet_ntop(AF_INET, &vtep_ip, buf2, sizeof(buf2)));
2463 }
2464
7597ac7b
MS
2465 ctx = dplane_ctx_alloc();
2466
2467 ctx->zd_op = op;
2468 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2469 ctx->zd_vrf_id = ifp->vrf_id;
2470
2471 zns = zebra_ns_lookup(ifp->vrf_id);
2472 dplane_ctx_ns_init(ctx, zns, false);
2473
2474 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2475 ctx->zd_ifindex = ifp->ifindex;
2476
2477 /* Init the mac-specific data area */
2478 memset(&ctx->u.macinfo, 0, sizeof(ctx->u.macinfo));
2479
478566d6 2480 ctx->u.macinfo.br_ifindex = br_ifp->ifindex;
7597ac7b
MS
2481 ctx->u.macinfo.vtep_ip = vtep_ip;
2482 ctx->u.macinfo.mac = *mac;
2483 ctx->u.macinfo.vid = vid;
2484 ctx->u.macinfo.is_sticky = sticky;
2485
2486 /* Enqueue for processing on the dplane pthread */
2487 ret = dplane_update_enqueue(ctx);
2488
2489 /* Increment counter */
2490 atomic_fetch_add_explicit(&zdplane_info.dg_macs_in, 1,
2491 memory_order_relaxed);
2492
2493 if (ret == AOK)
2494 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2495 else {
2496 /* Error counter */
2497 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors, 1,
2498 memory_order_relaxed);
2499 dplane_ctx_free(&ctx);
2500 }
2501
2502 return result;
2503}
2504
931fa60c
MS
2505/*
2506 * Enqueue evpn neighbor add for the dataplane.
2507 */
2508enum zebra_dplane_result dplane_neigh_add(const struct interface *ifp,
2509 const struct ipaddr *ip,
2510 const struct ethaddr *mac,
2511 uint32_t flags)
2512{
2513 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2514
2515 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
4dd9d11c 2516 ifp, mac, ip, flags, DPLANE_NUD_NOARP);
931fa60c
MS
2517
2518 return result;
2519}
2520
2521/*
2522 * Enqueue evpn neighbor update for the dataplane.
2523 */
2524enum zebra_dplane_result dplane_neigh_update(const struct interface *ifp,
2525 const struct ipaddr *ip,
2526 const struct ethaddr *mac)
2527{
0bbd4ff4 2528 enum zebra_dplane_result result;
931fa60c
MS
2529
2530 result = neigh_update_internal(DPLANE_OP_NEIGH_UPDATE,
2531 ifp, mac, ip, 0, DPLANE_NUD_PROBE);
2532
2533 return result;
2534}
2535
2536/*
2537 * Enqueue evpn neighbor delete for the dataplane.
2538 */
2539enum zebra_dplane_result dplane_neigh_delete(const struct interface *ifp,
2540 const struct ipaddr *ip)
2541{
0bbd4ff4 2542 enum zebra_dplane_result result;
931fa60c
MS
2543
2544 result = neigh_update_internal(DPLANE_OP_NEIGH_DELETE,
2545 ifp, NULL, ip, 0, 0);
2546
2547 return result;
2548}
2549
0bbd4ff4
MS
2550/*
2551 * Enqueue evpn VTEP add for the dataplane.
2552 */
2553enum zebra_dplane_result dplane_vtep_add(const struct interface *ifp,
2554 const struct in_addr *ip,
2555 vni_t vni)
2556{
2557 enum zebra_dplane_result result;
2558 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
2559 struct ipaddr addr;
2560
2561 if (IS_ZEBRA_DEBUG_VXLAN)
2562 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
2563 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
2564
2565 SET_IPADDR_V4(&addr);
2566 addr.ipaddr_v4 = *ip;
2567
2568 result = neigh_update_internal(DPLANE_OP_VTEP_ADD,
2569 ifp, &mac, &addr, 0, 0);
2570
2571 return result;
2572}
2573
2574/*
2575 * Enqueue evpn VTEP add for the dataplane.
2576 */
2577enum zebra_dplane_result dplane_vtep_delete(const struct interface *ifp,
2578 const struct in_addr *ip,
2579 vni_t vni)
2580{
2581 enum zebra_dplane_result result;
2582 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
2583 struct ipaddr addr;
2584
2585 if (IS_ZEBRA_DEBUG_VXLAN)
2586 zlog_debug(
2587 "Uninstall %s from flood list for VNI %u intf %s(%u)",
2588 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
2589
2590 SET_IPADDR_V4(&addr);
2591 addr.ipaddr_v4 = *ip;
2592
2593 result = neigh_update_internal(DPLANE_OP_VTEP_DELETE,
2594 ifp, &mac, &addr, 0, 0);
2595
2596 return result;
2597}
2598
931fa60c
MS
2599/*
2600 * Common helper api for evpn neighbor updates
2601 */
2602static enum zebra_dplane_result
2603neigh_update_internal(enum dplane_op_e op,
2604 const struct interface *ifp,
2605 const struct ethaddr *mac,
2606 const struct ipaddr *ip,
2607 uint32_t flags, uint16_t state)
2608{
2609 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2610 int ret;
2611 struct zebra_dplane_ctx *ctx = NULL;
2612 struct zebra_ns *zns;
2613
2614 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2615 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
2616
2617 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
2618 dplane_op2str(op),
2619 prefix_mac2str(mac, buf1, sizeof(buf1)),
2620 ifp->name,
2621 ipaddr2str(ip, buf2, sizeof(buf2)));
2622 }
2623
2624 ctx = dplane_ctx_alloc();
2625
2626 ctx->zd_op = op;
2627 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2628 ctx->zd_vrf_id = ifp->vrf_id;
2629
2630 zns = zebra_ns_lookup(ifp->vrf_id);
2631 dplane_ctx_ns_init(ctx, zns, false);
2632
2633 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2634 ctx->zd_ifindex = ifp->ifindex;
2635
2636 /* Init the neighbor-specific data area */
2637 memset(&ctx->u.neigh, 0, sizeof(ctx->u.neigh));
2638
2639 ctx->u.neigh.ip_addr = *ip;
2640 if (mac)
2641 ctx->u.neigh.mac = *mac;
2642 ctx->u.neigh.flags = flags;
2643 ctx->u.neigh.state = state;
2644
2645 /* Enqueue for processing on the dplane pthread */
2646 ret = dplane_update_enqueue(ctx);
2647
2648 /* Increment counter */
2649 atomic_fetch_add_explicit(&zdplane_info.dg_neighs_in, 1,
2650 memory_order_relaxed);
2651
2652 if (ret == AOK)
2653 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2654 else {
2655 /* Error counter */
2656 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors, 1,
2657 memory_order_relaxed);
2658 dplane_ctx_free(&ctx);
2659 }
2660
2661 return result;
2662}
2663
1d11b21f
MS
2664/*
2665 * Handler for 'show dplane'
2666 */
2667int dplane_show_helper(struct vty *vty, bool detailed)
2668{
16c628de
MS
2669 uint64_t queued, queue_max, limit, errs, incoming, yields,
2670 other_errs;
1d11b21f 2671
4dfd7a02 2672 /* Using atomics because counters are being changed in different
c831033f 2673 * pthread contexts.
4dfd7a02 2674 */
25779064 2675 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
1d11b21f 2676 memory_order_relaxed);
91f16812
MS
2677 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
2678 memory_order_relaxed);
25779064 2679 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
1d11b21f 2680 memory_order_relaxed);
25779064 2681 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
4dfd7a02 2682 memory_order_relaxed);
25779064 2683 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
1d11b21f 2684 memory_order_relaxed);
c831033f
MS
2685 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
2686 memory_order_relaxed);
16c628de
MS
2687 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
2688 memory_order_relaxed);
1d11b21f 2689
c831033f
MS
2690 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
2691 incoming);
1d11b21f 2692 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
16c628de 2693 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
91f16812 2694 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
1d11b21f 2695 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
4dfd7a02 2696 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
4280d91c
MS
2697 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
2698
2699 incoming = atomic_load_explicit(&zdplane_info.dg_lsps_in,
2700 memory_order_relaxed);
2701 errs = atomic_load_explicit(&zdplane_info.dg_lsp_errors,
2702 memory_order_relaxed);
2703 vty_out(vty, "LSP updates: %"PRIu64"\n", incoming);
2704 vty_out(vty, "LSP update errors: %"PRIu64"\n", errs);
2705
2706 incoming = atomic_load_explicit(&zdplane_info.dg_pws_in,
2707 memory_order_relaxed);
2708 errs = atomic_load_explicit(&zdplane_info.dg_pw_errors,
2709 memory_order_relaxed);
2710 vty_out(vty, "PW updates: %"PRIu64"\n", incoming);
2711 vty_out(vty, "PW update errors: %"PRIu64"\n", errs);
2712
2713 incoming = atomic_load_explicit(&zdplane_info.dg_intf_addrs_in,
2714 memory_order_relaxed);
2715 errs = atomic_load_explicit(&zdplane_info.dg_intf_addr_errors,
2716 memory_order_relaxed);
2717 vty_out(vty, "Intf addr updates: %"PRIu64"\n", incoming);
2718 vty_out(vty, "Intf addr errors: %"PRIu64"\n", errs);
2719
2720 incoming = atomic_load_explicit(&zdplane_info.dg_macs_in,
2721 memory_order_relaxed);
2722 errs = atomic_load_explicit(&zdplane_info.dg_mac_errors,
2723 memory_order_relaxed);
2724 vty_out(vty, "EVPN MAC updates: %"PRIu64"\n", incoming);
2725 vty_out(vty, "EVPN MAC errors: %"PRIu64"\n", errs);
1d11b21f 2726
931fa60c
MS
2727 incoming = atomic_load_explicit(&zdplane_info.dg_neighs_in,
2728 memory_order_relaxed);
2729 errs = atomic_load_explicit(&zdplane_info.dg_neigh_errors,
2730 memory_order_relaxed);
2731 vty_out(vty, "EVPN neigh updates: %"PRIu64"\n", incoming);
2732 vty_out(vty, "EVPN neigh errors: %"PRIu64"\n", errs);
2733
1d11b21f
MS
2734 return CMD_SUCCESS;
2735}
2736
2737/*
2738 * Handler for 'show dplane providers'
2739 */
2740int dplane_show_provs_helper(struct vty *vty, bool detailed)
2741{
c831033f
MS
2742 struct zebra_dplane_provider *prov;
2743 uint64_t in, in_max, out, out_max;
2744
2745 vty_out(vty, "Zebra dataplane providers:\n");
2746
2747 DPLANE_LOCK();
2748 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
2749 DPLANE_UNLOCK();
2750
2751 /* Show counters, useful info from each registered provider */
2752 while (prov) {
2753
2754 in = atomic_load_explicit(&prov->dp_in_counter,
2755 memory_order_relaxed);
2756 in_max = atomic_load_explicit(&prov->dp_in_max,
2757 memory_order_relaxed);
2758 out = atomic_load_explicit(&prov->dp_out_counter,
2759 memory_order_relaxed);
2760 out_max = atomic_load_explicit(&prov->dp_out_max,
2761 memory_order_relaxed);
2762
c9d17fe8
MS
2763 vty_out(vty, "%s (%u): in: %"PRIu64", q_max: %"PRIu64", "
2764 "out: %"PRIu64", q_max: %"PRIu64"\n",
c831033f
MS
2765 prov->dp_name, prov->dp_id, in, in_max, out, out_max);
2766
2767 DPLANE_LOCK();
2768 prov = TAILQ_NEXT(prov, dp_prov_link);
2769 DPLANE_UNLOCK();
2770 }
1d11b21f
MS
2771
2772 return CMD_SUCCESS;
2773}
2774
f26730e1
MS
2775/*
2776 * Helper for 'show run' etc.
2777 */
2778int dplane_config_write_helper(struct vty *vty)
2779{
2780 if (zdplane_info.dg_max_queued_updates != DPLANE_DEFAULT_MAX_QUEUED)
2781 vty_out(vty, "zebra dplane limit %u\n",
2782 zdplane_info.dg_max_queued_updates);
2783
2784 return 0;
2785}
2786
b8e0423d
MS
2787/*
2788 * Provider registration
2789 */
2790int dplane_provider_register(const char *name,
c831033f
MS
2791 enum dplane_provider_prio prio,
2792 int flags,
1dd4ea8a 2793 int (*start_fp)(struct zebra_dplane_provider *),
4c206c8f
MS
2794 int (*fp)(struct zebra_dplane_provider *),
2795 int (*fini_fp)(struct zebra_dplane_provider *,
2796 bool early),
1ff8a248
MS
2797 void *data,
2798 struct zebra_dplane_provider **prov_p)
b8e0423d
MS
2799{
2800 int ret = 0;
6fb51ccb 2801 struct zebra_dplane_provider *p = NULL, *last;
b8e0423d
MS
2802
2803 /* Validate */
2804 if (fp == NULL) {
2805 ret = EINVAL;
2806 goto done;
2807 }
2808
2809 if (prio <= DPLANE_PRIO_NONE ||
1bcea841 2810 prio > DPLANE_PRIO_LAST) {
b8e0423d
MS
2811 ret = EINVAL;
2812 goto done;
2813 }
2814
2815 /* Allocate and init new provider struct */
25779064 2816 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
b8e0423d 2817
c831033f
MS
2818 pthread_mutex_init(&(p->dp_mutex), NULL);
2819 TAILQ_INIT(&(p->dp_ctx_in_q));
2820 TAILQ_INIT(&(p->dp_ctx_out_q));
b8e0423d 2821
932dbb4d 2822 p->dp_flags = flags;
b8e0423d
MS
2823 p->dp_priority = prio;
2824 p->dp_fp = fp;
1dd4ea8a 2825 p->dp_start = start_fp;
18c37974 2826 p->dp_fini = fini_fp;
c831033f 2827 p->dp_data = data;
18c37974 2828
c831033f 2829 /* Lock - the dplane pthread may be running */
18c37974 2830 DPLANE_LOCK();
b8e0423d 2831
25779064 2832 p->dp_id = ++zdplane_info.dg_provider_id;
b8e0423d 2833
c831033f
MS
2834 if (name)
2835 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
2836 else
2837 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
2838 "provider-%u", p->dp_id);
2839
b8e0423d 2840 /* Insert into list ordered by priority */
c831033f 2841 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
5709131c 2842 if (last->dp_priority > p->dp_priority)
b8e0423d 2843 break;
b8e0423d
MS
2844 }
2845
5709131c 2846 if (last)
c831033f 2847 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
5709131c 2848 else
25779064 2849 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
c831033f 2850 dp_prov_link);
b8e0423d 2851
18c37974
MS
2852 /* And unlock */
2853 DPLANE_UNLOCK();
2854
c831033f
MS
2855 if (IS_ZEBRA_DEBUG_DPLANE)
2856 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
2857 p->dp_name, p->dp_id, p->dp_priority);
2858
b8e0423d 2859done:
1ff8a248
MS
2860 if (prov_p)
2861 *prov_p = p;
2862
5709131c 2863 return ret;
b8e0423d
MS
2864}
2865
c831033f
MS
2866/* Accessors for provider attributes */
2867const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
2868{
2869 return prov->dp_name;
2870}
2871
2872uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
2873{
2874 return prov->dp_id;
2875}
2876
2877void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
2878{
2879 return prov->dp_data;
2880}
2881
2882int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
2883{
2884 return zdplane_info.dg_updates_per_cycle;
2885}
2886
ad6aad4d
MS
2887/* Lock/unlock a provider's mutex - iff the provider was registered with
2888 * the THREADED flag.
2889 */
2890void dplane_provider_lock(struct zebra_dplane_provider *prov)
2891{
2892 if (dplane_provider_is_threaded(prov))
2893 DPLANE_PROV_LOCK(prov);
2894}
2895
2896void dplane_provider_unlock(struct zebra_dplane_provider *prov)
2897{
2898 if (dplane_provider_is_threaded(prov))
2899 DPLANE_PROV_UNLOCK(prov);
2900}
2901
c831033f
MS
2902/*
2903 * Dequeue and maintain associated counter
2904 */
2905struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
2906 struct zebra_dplane_provider *prov)
2907{
2908 struct zebra_dplane_ctx *ctx = NULL;
2909
ad6aad4d 2910 dplane_provider_lock(prov);
c831033f
MS
2911
2912 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
2913 if (ctx) {
2914 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
c9d17fe8
MS
2915
2916 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
2917 memory_order_relaxed);
c831033f
MS
2918 }
2919
ad6aad4d 2920 dplane_provider_unlock(prov);
c831033f
MS
2921
2922 return ctx;
2923}
2924
2925/*
2926 * Dequeue work to a list, return count
2927 */
2928int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
2929 struct dplane_ctx_q *listp)
2930{
2931 int limit, ret;
2932 struct zebra_dplane_ctx *ctx;
2933
2934 limit = zdplane_info.dg_updates_per_cycle;
2935
ad6aad4d 2936 dplane_provider_lock(prov);
c831033f
MS
2937
2938 for (ret = 0; ret < limit; ret++) {
2939 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
2940 if (ctx) {
2941 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
2942
2943 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
2944 } else {
2945 break;
2946 }
2947 }
2948
c9d17fe8
MS
2949 if (ret > 0)
2950 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
2951 memory_order_relaxed);
2952
ad6aad4d 2953 dplane_provider_unlock(prov);
c831033f
MS
2954
2955 return ret;
2956}
2957
2958/*
2959 * Enqueue and maintain associated counter
2960 */
2961void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
2962 struct zebra_dplane_ctx *ctx)
2963{
ad6aad4d 2964 dplane_provider_lock(prov);
c831033f
MS
2965
2966 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
2967 zd_q_entries);
2968
ad6aad4d 2969 dplane_provider_unlock(prov);
c831033f
MS
2970
2971 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
2972 memory_order_relaxed);
2973}
2974
62b8bb7a
MS
2975/*
2976 * Accessor for provider object
2977 */
c831033f
MS
2978bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
2979{
2980 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
2981}
2982
62b8bb7a
MS
2983/*
2984 * Internal helper that copies information from a zebra ns object; this is
2985 * called in the zebra main pthread context as part of dplane ctx init.
2986 */
2987static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
2988 struct zebra_ns *zns)
2989{
2990 ns_info->ns_id = zns->ns_id;
2991
2992#if defined(HAVE_NETLINK)
2993 ns_info->is_cmd = true;
2994 ns_info->nls = zns->netlink_dplane;
2995#endif /* NETLINK */
2996}
2997
c831033f
MS
2998/*
2999 * Provider api to signal that work/events are available
3000 * for the dataplane pthread.
3001 */
3002int dplane_provider_work_ready(void)
3003{
e5a60d82
MS
3004 /* Note that during zebra startup, we may be offered work before
3005 * the dataplane pthread (and thread-master) are ready. We want to
3006 * enqueue the work, but the event-scheduling machinery may not be
3007 * available.
3008 */
3009 if (zdplane_info.dg_run) {
3010 thread_add_event(zdplane_info.dg_master,
3011 dplane_thread_loop, NULL, 0,
3012 &zdplane_info.dg_t_update);
3013 }
c831033f
MS
3014
3015 return AOK;
3016}
3017
593e4eb1
MS
3018/*
3019 * Enqueue a context directly to zebra main.
3020 */
3021void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx)
3022{
3023 struct dplane_ctx_q temp_list;
3024
3025 /* Zebra's api takes a list, so we need to use a temporary list */
3026 TAILQ_INIT(&temp_list);
3027
3028 TAILQ_INSERT_TAIL(&temp_list, ctx, zd_q_entries);
3029 (zdplane_info.dg_results_cb)(&temp_list);
3030}
3031
7cdb1a84 3032/*
c831033f 3033 * Kernel dataplane provider
7cdb1a84 3034 */
c831033f 3035
16c628de
MS
3036/*
3037 * Handler for kernel LSP updates
3038 */
3039static enum zebra_dplane_result
3040kernel_dplane_lsp_update(struct zebra_dplane_ctx *ctx)
3041{
3042 enum zebra_dplane_result res;
3043
3044 /* Call into the synchronous kernel-facing code here */
3045 res = kernel_lsp_update(ctx);
3046
3047 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3048 atomic_fetch_add_explicit(
3049 &zdplane_info.dg_lsp_errors, 1,
3050 memory_order_relaxed);
3051
3052 return res;
3053}
3054
c10a646d
MS
3055/*
3056 * Handler for kernel pseudowire updates
3057 */
3058static enum zebra_dplane_result
3059kernel_dplane_pw_update(struct zebra_dplane_ctx *ctx)
3060{
3061 enum zebra_dplane_result res;
3062
3063 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3064 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
7c7ef4a8 3065 dplane_ctx_get_ifname(ctx),
c10a646d
MS
3066 dplane_op2str(ctx->zd_op),
3067 dplane_ctx_get_pw_af(ctx),
3068 dplane_ctx_get_pw_local_label(ctx),
3069 dplane_ctx_get_pw_remote_label(ctx));
3070
3071 res = kernel_pw_update(ctx);
3072
3073 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3074 atomic_fetch_add_explicit(
3075 &zdplane_info.dg_pw_errors, 1,
3076 memory_order_relaxed);
3077
3078 return res;
3079}
3080
16c628de
MS
3081/*
3082 * Handler for kernel route updates
3083 */
3084static enum zebra_dplane_result
3085kernel_dplane_route_update(struct zebra_dplane_ctx *ctx)
3086{
3087 enum zebra_dplane_result res;
3088
3089 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3090 char dest_str[PREFIX_STRLEN];
3091
3092 prefix2str(dplane_ctx_get_dest(ctx),
3093 dest_str, sizeof(dest_str));
3094
3095 zlog_debug("%u:%s Dplane route update ctx %p op %s",
3096 dplane_ctx_get_vrf(ctx), dest_str,
3097 ctx, dplane_op2str(dplane_ctx_get_op(ctx)));
3098 }
3099
3100 /* Call into the synchronous kernel-facing code here */
3101 res = kernel_route_update(ctx);
3102
3103 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3104 atomic_fetch_add_explicit(
3105 &zdplane_info.dg_route_errors, 1,
3106 memory_order_relaxed);
3107
3108 return res;
3109}
3110
64168803
MS
3111/*
3112 * Handler for kernel-facing interface address updates
3113 */
3114static enum zebra_dplane_result
3115kernel_dplane_address_update(struct zebra_dplane_ctx *ctx)
3116{
3117 enum zebra_dplane_result res;
3118
64168803
MS
3119 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3120 char dest_str[PREFIX_STRLEN];
3121
3122 prefix2str(dplane_ctx_get_intf_addr(ctx), dest_str,
3123 sizeof(dest_str));
3124
3125 zlog_debug("Dplane intf %s, idx %u, addr %s",
3126 dplane_op2str(dplane_ctx_get_op(ctx)),
3127 dplane_ctx_get_ifindex(ctx), dest_str);
3128 }
3129
3130 res = kernel_address_update_ctx(ctx);
3131
3132 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3133 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
3134 1, memory_order_relaxed);
3135
3136 return res;
3137}
3138
f820d025
SW
3139/**
3140 * kernel_dplane_nexthop_update() - Handler for kernel nexthop updates
3141 *
3142 * @ctx: Dataplane context
3143 *
3144 * Return: Dataplane result flag
3145 */
3146static enum zebra_dplane_result
3147kernel_dplane_nexthop_update(struct zebra_dplane_ctx *ctx)
3148{
3149 enum zebra_dplane_result res;
3150
3151 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3152 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
0c8215cb 3153 dplane_ctx_get_nhe_id(ctx), ctx,
f820d025
SW
3154 dplane_op2str(dplane_ctx_get_op(ctx)));
3155 }
3156
3157 res = kernel_nexthop_update(ctx);
3158
3159 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3160 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
3161 memory_order_relaxed);
3162
3163 return res;
3164}
3165
036d93c0 3166/*
931fa60c 3167 * Handler for kernel-facing EVPN MAC address updates
036d93c0
MS
3168 */
3169static enum zebra_dplane_result
3170kernel_dplane_mac_update(struct zebra_dplane_ctx *ctx)
3171{
3172 enum zebra_dplane_result res;
3173
3174 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3175 char buf[ETHER_ADDR_STRLEN];
3176
3177 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf,
3178 sizeof(buf));
3179
3180 zlog_debug("Dplane %s, mac %s, ifindex %u",
3181 dplane_op2str(dplane_ctx_get_op(ctx)),
3182 buf, dplane_ctx_get_ifindex(ctx));
3183 }
3184
3185 res = kernel_mac_update_ctx(ctx);
3186
3187 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3188 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors,
3189 1, memory_order_relaxed);
3190
3191 return res;
3192}
3193
931fa60c
MS
3194/*
3195 * Handler for kernel-facing EVPN neighbor updates
3196 */
3197static enum zebra_dplane_result
3198kernel_dplane_neigh_update(struct zebra_dplane_ctx *ctx)
3199{
3200 enum zebra_dplane_result res;
3201
3202 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3203 char buf[PREFIX_STRLEN];
3204
3205 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx), buf,
3206 sizeof(buf));
3207
3208 zlog_debug("Dplane %s, ip %s, ifindex %u",
3209 dplane_op2str(dplane_ctx_get_op(ctx)),
3210 buf, dplane_ctx_get_ifindex(ctx));
3211 }
3212
3213 res = kernel_neigh_update_ctx(ctx);
3214
3215 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3216 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors,
3217 1, memory_order_relaxed);
3218
3219 return res;
3220}
3221
c831033f
MS
3222/*
3223 * Kernel provider callback
3224 */
3225static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
7cdb1a84 3226{
c831033f
MS
3227 enum zebra_dplane_result res;
3228 struct zebra_dplane_ctx *ctx;
3229 int counter, limit;
7cdb1a84 3230
c831033f 3231 limit = dplane_provider_get_work_limit(prov);
7cdb1a84 3232
c831033f
MS
3233 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3234 zlog_debug("dplane provider '%s': processing",
3235 dplane_provider_get_name(prov));
7cdb1a84 3236
c831033f 3237 for (counter = 0; counter < limit; counter++) {
91f16812 3238
c831033f
MS
3239 ctx = dplane_provider_dequeue_in_ctx(prov);
3240 if (ctx == NULL)
3241 break;
d8c16a95 3242
9677961e
MS
3243 /* A previous provider plugin may have asked to skip the
3244 * kernel update.
3245 */
3246 if (dplane_ctx_is_skip_kernel(ctx)) {
3247 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3248 goto skip_one;
3249 }
3250
16c628de
MS
3251 /* Dispatch to appropriate kernel-facing apis */
3252 switch (dplane_ctx_get_op(ctx)) {
1d11b21f 3253
16c628de
MS
3254 case DPLANE_OP_ROUTE_INSTALL:
3255 case DPLANE_OP_ROUTE_UPDATE:
3256 case DPLANE_OP_ROUTE_DELETE:
3257 res = kernel_dplane_route_update(ctx);
3258 break;
d8c16a95 3259
f820d025
SW
3260 case DPLANE_OP_NH_INSTALL:
3261 case DPLANE_OP_NH_UPDATE:
3262 case DPLANE_OP_NH_DELETE:
3263 res = kernel_dplane_nexthop_update(ctx);
3264 break;
3265
16c628de
MS
3266 case DPLANE_OP_LSP_INSTALL:
3267 case DPLANE_OP_LSP_UPDATE:
3268 case DPLANE_OP_LSP_DELETE:
3269 res = kernel_dplane_lsp_update(ctx);
3270 break;
d8c16a95 3271
c10a646d
MS
3272 case DPLANE_OP_PW_INSTALL:
3273 case DPLANE_OP_PW_UNINSTALL:
3274 res = kernel_dplane_pw_update(ctx);
3275 break;
3276
64168803
MS
3277 case DPLANE_OP_ADDR_INSTALL:
3278 case DPLANE_OP_ADDR_UNINSTALL:
3279 res = kernel_dplane_address_update(ctx);
3280 break;
3281
036d93c0
MS
3282 case DPLANE_OP_MAC_INSTALL:
3283 case DPLANE_OP_MAC_DELETE:
3284 res = kernel_dplane_mac_update(ctx);
3285 break;
3286
931fa60c
MS
3287 case DPLANE_OP_NEIGH_INSTALL:
3288 case DPLANE_OP_NEIGH_UPDATE:
3289 case DPLANE_OP_NEIGH_DELETE:
0bbd4ff4
MS
3290 case DPLANE_OP_VTEP_ADD:
3291 case DPLANE_OP_VTEP_DELETE:
931fa60c
MS
3292 res = kernel_dplane_neigh_update(ctx);
3293 break;
3294
104e3ad9 3295 /* Ignore 'notifications' - no-op */
cf363e1b
MS
3296 case DPLANE_OP_SYS_ROUTE_ADD:
3297 case DPLANE_OP_SYS_ROUTE_DELETE:
54818e3b 3298 case DPLANE_OP_ROUTE_NOTIFY:
104e3ad9 3299 case DPLANE_OP_LSP_NOTIFY:
cf363e1b
MS
3300 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3301 break;
3302
16c628de 3303 default:
c831033f 3304 atomic_fetch_add_explicit(
16c628de 3305 &zdplane_info.dg_other_errors, 1,
c831033f 3306 memory_order_relaxed);
d8c16a95 3307
16c628de
MS
3308 res = ZEBRA_DPLANE_REQUEST_FAILURE;
3309 break;
3310 }
3311
9677961e 3312skip_one:
c831033f
MS
3313 dplane_ctx_set_status(ctx, res);
3314
3315 dplane_provider_enqueue_out_ctx(prov, ctx);
3316 }
3317
3318 /* Ensure that we'll run the work loop again if there's still
3319 * more work to do.
3320 */
3321 if (counter >= limit) {
3322 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3323 zlog_debug("dplane provider '%s' reached max updates %d",
3324 dplane_provider_get_name(prov), counter);
3325
3326 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
3327 1, memory_order_relaxed);
3328
3329 dplane_provider_work_ready();
3330 }
3331
3332 return 0;
3333}
3334
e5a60d82
MS
3335#if DPLANE_TEST_PROVIDER
3336
c831033f
MS
3337/*
3338 * Test dataplane provider plugin
3339 */
3340
3341/*
3342 * Test provider process callback
3343 */
3344static int test_dplane_process_func(struct zebra_dplane_provider *prov)
3345{
3346 struct zebra_dplane_ctx *ctx;
3347 int counter, limit;
3348
3349 /* Just moving from 'in' queue to 'out' queue */
3350
3351 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3352 zlog_debug("dplane provider '%s': processing",
3353 dplane_provider_get_name(prov));
3354
3355 limit = dplane_provider_get_work_limit(prov);
3356
3357 for (counter = 0; counter < limit; counter++) {
3358
3359 ctx = dplane_provider_dequeue_in_ctx(prov);
3360 if (ctx == NULL)
3361 break;
3362
cf363e1b
MS
3363 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3364 zlog_debug("dplane provider '%s': op %s",
3365 dplane_provider_get_name(prov),
3366 dplane_op2str(dplane_ctx_get_op(ctx)));
3367
c831033f
MS
3368 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
3369
3370 dplane_provider_enqueue_out_ctx(prov, ctx);
3371 }
3372
c9d17fe8
MS
3373 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3374 zlog_debug("dplane provider '%s': processed %d",
3375 dplane_provider_get_name(prov), counter);
3376
c831033f
MS
3377 /* Ensure that we'll run the work loop again if there's still
3378 * more work to do.
3379 */
3380 if (counter >= limit)
3381 dplane_provider_work_ready();
3382
3383 return 0;
3384}
3385
3386/*
3387 * Test provider shutdown/fini callback
3388 */
3389static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
3390 bool early)
3391{
3392 if (IS_ZEBRA_DEBUG_DPLANE)
3393 zlog_debug("dplane provider '%s': %sshutdown",
3394 dplane_provider_get_name(prov),
3395 early ? "early " : "");
3396
3397 return 0;
3398}
e5a60d82 3399#endif /* DPLANE_TEST_PROVIDER */
c831033f
MS
3400
3401/*
3402 * Register default kernel provider
3403 */
3404static void dplane_provider_init(void)
3405{
3406 int ret;
3407
3408 ret = dplane_provider_register("Kernel",
3409 DPLANE_PRIO_KERNEL,
1dd4ea8a 3410 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
3411 kernel_dplane_process_func,
3412 NULL,
1ff8a248 3413 NULL, NULL);
c831033f
MS
3414
3415 if (ret != AOK)
3416 zlog_err("Unable to register kernel dplane provider: %d",
3417 ret);
3418
e5a60d82
MS
3419#if DPLANE_TEST_PROVIDER
3420 /* Optional test provider ... */
c831033f
MS
3421 ret = dplane_provider_register("Test",
3422 DPLANE_PRIO_PRE_KERNEL,
1dd4ea8a 3423 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
3424 test_dplane_process_func,
3425 test_dplane_shutdown_func,
1ff8a248 3426 NULL /* data */, NULL);
c831033f
MS
3427
3428 if (ret != AOK)
3429 zlog_err("Unable to register test dplane provider: %d",
3430 ret);
e5a60d82 3431#endif /* DPLANE_TEST_PROVIDER */
7cdb1a84
MS
3432}
3433
4dfd7a02
MS
3434/* Indicates zebra shutdown/exit is in progress. Some operations may be
3435 * simplified or skipped during shutdown processing.
3436 */
3437bool dplane_is_in_shutdown(void)
3438{
25779064 3439 return zdplane_info.dg_is_shutdown;
4dfd7a02
MS
3440}
3441
3442/*
3443 * Early or pre-shutdown, de-init notification api. This runs pretty
3444 * early during zebra shutdown, as a signal to stop new work and prepare
3445 * for updates generated by shutdown/cleanup activity, as zebra tries to
3446 * remove everything it's responsible for.
c9d17fe8 3447 * NB: This runs in the main zebra pthread context.
4dfd7a02
MS
3448 */
3449void zebra_dplane_pre_finish(void)
3450{
3451 if (IS_ZEBRA_DEBUG_DPLANE)
3452 zlog_debug("Zebra dataplane pre-fini called");
3453
25779064 3454 zdplane_info.dg_is_shutdown = true;
4dfd7a02 3455
c9d17fe8 3456 /* TODO -- Notify provider(s) of pending shutdown */
4dfd7a02
MS
3457}
3458
3459/*
3460 * Utility to determine whether work remains enqueued within the dplane;
3461 * used during system shutdown processing.
3462 */
3463static bool dplane_work_pending(void)
3464{
c9d17fe8 3465 bool ret = false;
25779064 3466 struct zebra_dplane_ctx *ctx;
c9d17fe8 3467 struct zebra_dplane_provider *prov;
4dfd7a02 3468
c831033f
MS
3469 /* TODO -- just checking incoming/pending work for now, must check
3470 * providers
3471 */
4dfd7a02
MS
3472 DPLANE_LOCK();
3473 {
3fe4ccc4 3474 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c9d17fe8 3475 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4dfd7a02
MS
3476 }
3477 DPLANE_UNLOCK();
3478
c9d17fe8
MS
3479 if (ctx != NULL) {
3480 ret = true;
3481 goto done;
3482 }
3483
3484 while (prov) {
3485
ad6aad4d 3486 dplane_provider_lock(prov);
c9d17fe8
MS
3487
3488 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3489 if (ctx == NULL)
3490 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
3491
ad6aad4d 3492 dplane_provider_unlock(prov);
c9d17fe8
MS
3493
3494 if (ctx != NULL)
3495 break;
3496
3497 DPLANE_LOCK();
3498 prov = TAILQ_NEXT(prov, dp_prov_link);
3499 DPLANE_UNLOCK();
3500 }
3501
3502 if (ctx != NULL)
3503 ret = true;
3504
3505done:
3506 return ret;
4dfd7a02
MS
3507}
3508
3509/*
3510 * Shutdown-time intermediate callback, used to determine when all pending
3511 * in-flight updates are done. If there's still work to do, reschedules itself.
3512 * If all work is done, schedules an event to the main zebra thread for
3513 * final zebra shutdown.
3514 * This runs in the dplane pthread context.
3515 */
3516static int dplane_check_shutdown_status(struct thread *event)
3517{
3518 if (IS_ZEBRA_DEBUG_DPLANE)
3519 zlog_debug("Zebra dataplane shutdown status check called");
3520
3521 if (dplane_work_pending()) {
3522 /* Reschedule dplane check on a short timer */
25779064 3523 thread_add_timer_msec(zdplane_info.dg_master,
4dfd7a02
MS
3524 dplane_check_shutdown_status,
3525 NULL, 100,
25779064 3526 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
3527
3528 /* TODO - give up and stop waiting after a short time? */
3529
3530 } else {
3531 /* We appear to be done - schedule a final callback event
3532 * for the zebra main pthread.
3533 */
3801e764 3534 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
4dfd7a02
MS
3535 }
3536
3537 return 0;
3538}
3539
18c37974 3540/*
1d11b21f 3541 * Shutdown, de-init api. This runs pretty late during shutdown,
4dfd7a02
MS
3542 * after zebra has tried to free/remove/uninstall all routes during shutdown.
3543 * At this point, dplane work may still remain to be done, so we can't just
3544 * blindly terminate. If there's still work to do, we'll periodically check
3545 * and when done, we'll enqueue a task to the zebra main thread for final
3546 * termination processing.
3547 *
1d11b21f 3548 * NB: This runs in the main zebra thread context.
18c37974 3549 */
1d11b21f 3550void zebra_dplane_finish(void)
18c37974 3551{
4dfd7a02
MS
3552 if (IS_ZEBRA_DEBUG_DPLANE)
3553 zlog_debug("Zebra dataplane fini called");
3554
25779064 3555 thread_add_event(zdplane_info.dg_master,
4dfd7a02 3556 dplane_check_shutdown_status, NULL, 0,
25779064 3557 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
3558}
3559
c831033f
MS
3560/*
3561 * Main dataplane pthread event loop. The thread takes new incoming work
3562 * and offers it to the first provider. It then iterates through the
3563 * providers, taking complete work from each one and offering it
3564 * to the next in order. At each step, a limited number of updates are
3565 * processed during a cycle in order to provide some fairness.
14b0bc8e
MS
3566 *
3567 * This loop through the providers is only run once, so that the dataplane
3568 * pthread can look for other pending work - such as i/o work on behalf of
3569 * providers.
c831033f
MS
3570 */
3571static int dplane_thread_loop(struct thread *event)
3572{
3573 struct dplane_ctx_q work_list;
3574 struct dplane_ctx_q error_list;
3575 struct zebra_dplane_provider *prov;
3576 struct zebra_dplane_ctx *ctx, *tctx;
3577 int limit, counter, error_counter;
c9d17fe8 3578 uint64_t curr, high;
c831033f
MS
3579
3580 /* Capture work limit per cycle */
3581 limit = zdplane_info.dg_updates_per_cycle;
3582
14b0bc8e 3583 /* Init temporary lists used to move contexts among providers */
c831033f 3584 TAILQ_INIT(&work_list);
14b0bc8e
MS
3585 TAILQ_INIT(&error_list);
3586 error_counter = 0;
c831033f
MS
3587
3588 /* Check for zebra shutdown */
3589 if (!zdplane_info.dg_run)
3590 goto done;
3591
3592 /* Dequeue some incoming work from zebra (if any) onto the temporary
3593 * working list.
3594 */
3595 DPLANE_LOCK();
3596
3597 /* Locate initial registered provider */
3598 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
3599
14b0bc8e 3600 /* Move new work from incoming list to temp list */
c831033f 3601 for (counter = 0; counter < limit; counter++) {
3fe4ccc4 3602 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c831033f 3603 if (ctx) {
3fe4ccc4 3604 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
c831033f
MS
3605 zd_q_entries);
3606
c831033f
MS
3607 ctx->zd_provider = prov->dp_id;
3608
3609 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
3610 } else {
3611 break;
3612 }
3613 }
3614
3615 DPLANE_UNLOCK();
3616
14b0bc8e
MS
3617 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
3618 memory_order_relaxed);
3619
c831033f
MS
3620 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3621 zlog_debug("dplane: incoming new work counter: %d", counter);
3622
3623 /* Iterate through the registered providers, offering new incoming
3624 * work. If the provider has outgoing work in its queue, take that
3625 * work for the next provider
3626 */
3627 while (prov) {
3628
14b0bc8e
MS
3629 /* At each iteration, the temporary work list has 'counter'
3630 * items.
3631 */
c831033f
MS
3632 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3633 zlog_debug("dplane enqueues %d new work to provider '%s'",
3634 counter, dplane_provider_get_name(prov));
3635
3636 /* Capture current provider id in each context; check for
3637 * error status.
3638 */
3639 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
3640 if (dplane_ctx_get_status(ctx) ==
3641 ZEBRA_DPLANE_REQUEST_SUCCESS) {
3642 ctx->zd_provider = prov->dp_id;
3643 } else {
3644 /*
3645 * TODO -- improve error-handling: recirc
3646 * errors backwards so that providers can
3647 * 'undo' their work (if they want to)
3648 */
3649
3650 /* Move to error list; will be returned
3651 * zebra main.
3652 */
3653 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
3654 TAILQ_INSERT_TAIL(&error_list,
3655 ctx, zd_q_entries);
3656 error_counter++;
3657 }
3658 }
3659
3660 /* Enqueue new work to the provider */
ad6aad4d 3661 dplane_provider_lock(prov);
c831033f
MS
3662
3663 if (TAILQ_FIRST(&work_list))
3664 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
3665 zd_q_entries);
3666
c9d17fe8
MS
3667 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
3668 memory_order_relaxed);
3669 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
3670 memory_order_relaxed);
3671 curr = atomic_load_explicit(&prov->dp_in_queued,
3672 memory_order_relaxed);
3673 high = atomic_load_explicit(&prov->dp_in_max,
3674 memory_order_relaxed);
3675 if (curr > high)
3676 atomic_store_explicit(&prov->dp_in_max, curr,
c831033f
MS
3677 memory_order_relaxed);
3678
ad6aad4d 3679 dplane_provider_unlock(prov);
c831033f 3680
14b0bc8e
MS
3681 /* Reset the temp list (though the 'concat' may have done this
3682 * already), and the counter
3683 */
c831033f
MS
3684 TAILQ_INIT(&work_list);
3685 counter = 0;
3686
14b0bc8e
MS
3687 /* Call into the provider code. Note that this is
3688 * unconditional: we offer to do work even if we don't enqueue
3689 * any _new_ work.
3690 */
c831033f
MS
3691 (*prov->dp_fp)(prov);
3692
3693 /* Check for zebra shutdown */
3694 if (!zdplane_info.dg_run)
3695 break;
3696
3697 /* Dequeue completed work from the provider */
ad6aad4d 3698 dplane_provider_lock(prov);
c831033f
MS
3699
3700 while (counter < limit) {
3701 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
3702 if (ctx) {
3703 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
3704 zd_q_entries);
3705
3706 TAILQ_INSERT_TAIL(&work_list,
3707 ctx, zd_q_entries);
3708 counter++;
3709 } else
3710 break;
3711 }
3712
ad6aad4d 3713 dplane_provider_unlock(prov);
c831033f
MS
3714
3715 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3716 zlog_debug("dplane dequeues %d completed work from provider %s",
3717 counter, dplane_provider_get_name(prov));
3718
3719 /* Locate next provider */
3720 DPLANE_LOCK();
3721 prov = TAILQ_NEXT(prov, dp_prov_link);
3722 DPLANE_UNLOCK();
c831033f
MS
3723 }
3724
3725 /* After all providers have been serviced, enqueue any completed
14b0bc8e 3726 * work and any errors back to zebra so it can process the results.
c831033f 3727 */
c831033f 3728 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
14b0bc8e
MS
3729 zlog_debug("dplane has %d completed, %d errors, for zebra main",
3730 counter, error_counter);
c831033f
MS
3731
3732 /*
4c206c8f 3733 * Hand lists through the api to zebra main,
c831033f
MS
3734 * to reduce the number of lock/unlock cycles
3735 */
14b0bc8e 3736
4c206c8f
MS
3737 /* Call through to zebra main */
3738 (zdplane_info.dg_results_cb)(&error_list);
14b0bc8e 3739
4c206c8f 3740 TAILQ_INIT(&error_list);
14b0bc8e 3741
4c206c8f
MS
3742 /* Call through to zebra main */
3743 (zdplane_info.dg_results_cb)(&work_list);
c831033f 3744
4c206c8f 3745 TAILQ_INIT(&work_list);
c831033f
MS
3746
3747done:
3748 return 0;
3749}
3750
4dfd7a02
MS
3751/*
3752 * Final phase of shutdown, after all work enqueued to dplane has been
3753 * processed. This is called from the zebra main pthread context.
3754 */
3755void zebra_dplane_shutdown(void)
3756{
3757 if (IS_ZEBRA_DEBUG_DPLANE)
3758 zlog_debug("Zebra dataplane shutdown called");
1d11b21f
MS
3759
3760 /* Stop dplane thread, if it's running */
3761
25779064 3762 zdplane_info.dg_run = false;
1d11b21f 3763
9344d3fc
SW
3764 if (zdplane_info.dg_t_update)
3765 thread_cancel_async(zdplane_info.dg_t_update->master,
3766 &zdplane_info.dg_t_update, NULL);
1d11b21f 3767
d8c16a95
MS
3768 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
3769
3770 /* Destroy pthread */
3771 frr_pthread_destroy(zdplane_info.dg_pthread);
3772 zdplane_info.dg_pthread = NULL;
3773 zdplane_info.dg_master = NULL;
4dfd7a02 3774
c831033f
MS
3775 /* TODO -- Notify provider(s) of final shutdown */
3776
3777 /* TODO -- Clean-up provider objects */
3778
3779 /* TODO -- Clean queue(s), free memory */
3780}
3781
3782/*
3783 * Initialize the dataplane module during startup, internal/private version
3784 */
2561d12e 3785static void zebra_dplane_init_internal(void)
c831033f
MS
3786{
3787 memset(&zdplane_info, 0, sizeof(zdplane_info));
3788
3789 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
1d11b21f 3790
3fe4ccc4 3791 TAILQ_INIT(&zdplane_info.dg_update_ctx_q);
c831033f 3792 TAILQ_INIT(&zdplane_info.dg_providers_q);
1d11b21f 3793
c831033f
MS
3794 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
3795
3796 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
3797
3798 /* Register default kernel 'provider' during init */
3799 dplane_provider_init();
e5a60d82 3800}
c831033f 3801
e5a60d82
MS
3802/*
3803 * Start the dataplane pthread. This step needs to be run later than the
3804 * 'init' step, in case zebra has fork-ed.
3805 */
3806void zebra_dplane_start(void)
3807{
1dd4ea8a 3808 struct zebra_dplane_provider *prov;
c831033f
MS
3809 struct frr_pthread_attr pattr = {
3810 .start = frr_pthread_attr_default.start,
3811 .stop = frr_pthread_attr_default.stop
3812 };
3813
1dd4ea8a
MS
3814 /* Start dataplane pthread */
3815
c831033f 3816 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
0eca319c 3817 "zebra_dplane");
c831033f
MS
3818
3819 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
3820
e5a60d82
MS
3821 zdplane_info.dg_run = true;
3822
c831033f
MS
3823 /* Enqueue an initial event for the dataplane pthread */
3824 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
3825 &zdplane_info.dg_t_update);
3826
1dd4ea8a
MS
3827 /* Call start callbacks for registered providers */
3828
3829 DPLANE_LOCK();
3830 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
3831 DPLANE_UNLOCK();
3832
3833 while (prov) {
3834
3835 if (prov->dp_start)
3836 (prov->dp_start)(prov);
3837
3838 /* Locate next provider */
3839 DPLANE_LOCK();
3840 prov = TAILQ_NEXT(prov, dp_prov_link);
3841 DPLANE_UNLOCK();
3842 }
3843
c831033f 3844 frr_pthread_run(zdplane_info.dg_pthread, NULL);
18c37974
MS
3845}
3846
7cdb1a84 3847/*
b8e0423d 3848 * Initialize the dataplane module at startup; called by zebra rib_init()
7cdb1a84 3849 */
4c206c8f 3850void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
7cdb1a84 3851{
2561d12e 3852 zebra_dplane_init_internal();
4c206c8f 3853 zdplane_info.dg_results_cb = results_fp;
7cdb1a84 3854}