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