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