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