]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_dplane.c
Merge pull request #6789 from volta-networks/feat_ldp_igp_sync
[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 (zebra_nhg_kernel_nexthops_enabled()
1915 && (((op == DPLANE_OP_ROUTE_INSTALL)
1916 || (op == DPLANE_OP_ROUTE_UPDATE))
1917 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
1918 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED))) {
1919 ret = ENOENT;
1920 goto done;
1921 }
1922 }
1923 #endif /* HAVE_NETLINK */
1924
1925 /* Trying out the sequence number idea, so we can try to detect
1926 * when a result is stale.
1927 */
1928 re->dplane_sequence = zebra_router_get_next_sequence();
1929 ctx->zd_seq = re->dplane_sequence;
1930
1931 ret = AOK;
1932
1933 done:
1934 return ret;
1935 }
1936
1937 /**
1938 * dplane_ctx_nexthop_init() - Initialize a context block for a nexthop update
1939 *
1940 * @ctx: Dataplane context to init
1941 * @op: Operation being performed
1942 * @nhe: Nexthop group hash entry
1943 *
1944 * Return: Result status
1945 */
1946 int dplane_ctx_nexthop_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
1947 struct nhg_hash_entry *nhe)
1948 {
1949 struct zebra_vrf *zvrf = NULL;
1950 struct zebra_ns *zns = NULL;
1951 int ret = EINVAL;
1952
1953 if (!ctx || !nhe)
1954 goto done;
1955
1956 ctx->zd_op = op;
1957 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1958
1959 /* Copy over nhe info */
1960 ctx->u.rinfo.nhe.id = nhe->id;
1961 ctx->u.rinfo.nhe.afi = nhe->afi;
1962 ctx->u.rinfo.nhe.vrf_id = nhe->vrf_id;
1963 ctx->u.rinfo.nhe.type = nhe->type;
1964
1965 nexthop_group_copy(&(ctx->u.rinfo.nhe.ng), &(nhe->nhg));
1966
1967 /* If this is a group, convert it to a grp array of ids */
1968 if (!zebra_nhg_depends_is_empty(nhe)
1969 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE))
1970 ctx->u.rinfo.nhe.nh_grp_count = zebra_nhg_nhe2grp(
1971 ctx->u.rinfo.nhe.nh_grp, nhe, MULTIPATH_NUM);
1972
1973 zvrf = vrf_info_lookup(nhe->vrf_id);
1974
1975 /*
1976 * Fallback to default namespace if the vrf got ripped out from under
1977 * us.
1978 */
1979 zns = zvrf ? zvrf->zns : zebra_ns_lookup(NS_DEFAULT);
1980
1981 /*
1982 * TODO: Might not need to mark this as an update, since
1983 * it probably won't require two messages
1984 */
1985 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_NH_UPDATE));
1986 ctx->zd_is_update = (op == DPLANE_OP_NH_UPDATE);
1987
1988 ret = AOK;
1989
1990 done:
1991 return ret;
1992 }
1993
1994 /*
1995 * Capture information for an LSP update in a dplane context.
1996 */
1997 int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
1998 zebra_lsp_t *lsp)
1999 {
2000 int ret = AOK;
2001 zebra_nhlfe_t *nhlfe, *new_nhlfe;
2002
2003 ctx->zd_op = op;
2004 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2005
2006 /* Capture namespace info */
2007 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2008 (op == DPLANE_OP_LSP_UPDATE));
2009 ctx->zd_is_update = (op == DPLANE_OP_LSP_UPDATE);
2010
2011 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
2012
2013 nhlfe_list_init(&(ctx->u.lsp.nhlfe_list));
2014 nhlfe_list_init(&(ctx->u.lsp.backup_nhlfe_list));
2015
2016 /* This may be called to create/init a dplane context, not necessarily
2017 * to copy an lsp object.
2018 */
2019 if (lsp == NULL) {
2020 ret = AOK;
2021 goto done;
2022 }
2023
2024 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2025 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
2026 dplane_op2str(op), lsp->ile.in_label,
2027 lsp->num_ecmp);
2028
2029 ctx->u.lsp.ile = lsp->ile;
2030 ctx->u.lsp.addr_family = lsp->addr_family;
2031 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
2032 ctx->u.lsp.flags = lsp->flags;
2033
2034 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
2035 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
2036 /* Not sure if this is meaningful... */
2037 if (nhlfe->nexthop == NULL)
2038 continue;
2039
2040 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp), nhlfe->type,
2041 nhlfe->nexthop);
2042 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2043 ret = ENOMEM;
2044 break;
2045 }
2046
2047 /* Need to copy flags and backup info too */
2048 new_nhlfe->flags = nhlfe->flags;
2049 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2050
2051 if (CHECK_FLAG(new_nhlfe->nexthop->flags,
2052 NEXTHOP_FLAG_HAS_BACKUP)) {
2053 new_nhlfe->nexthop->backup_num =
2054 nhlfe->nexthop->backup_num;
2055 memcpy(new_nhlfe->nexthop->backup_idx,
2056 nhlfe->nexthop->backup_idx,
2057 new_nhlfe->nexthop->backup_num);
2058 }
2059
2060 if (nhlfe == lsp->best_nhlfe)
2061 ctx->u.lsp.best_nhlfe = new_nhlfe;
2062 }
2063
2064 if (ret != AOK)
2065 goto done;
2066
2067 /* Capture backup nhlfes/nexthops */
2068 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
2069 /* Not sure if this is meaningful... */
2070 if (nhlfe->nexthop == NULL)
2071 continue;
2072
2073 new_nhlfe = zebra_mpls_lsp_add_backup_nh(&(ctx->u.lsp),
2074 nhlfe->type,
2075 nhlfe->nexthop);
2076 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2077 ret = ENOMEM;
2078 break;
2079 }
2080
2081 /* Need to copy flags too */
2082 new_nhlfe->flags = nhlfe->flags;
2083 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2084 }
2085
2086 /* On error the ctx will be cleaned-up, so we don't need to
2087 * deal with any allocated nhlfe or nexthop structs here.
2088 */
2089 done:
2090
2091 return ret;
2092 }
2093
2094 /*
2095 * Capture information for an LSP update in a dplane context.
2096 */
2097 static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
2098 enum dplane_op_e op,
2099 struct zebra_pw *pw)
2100 {
2101 struct prefix p;
2102 afi_t afi;
2103 struct route_table *table;
2104 struct route_node *rn;
2105 struct route_entry *re;
2106 const struct nexthop_group *nhg;
2107
2108 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2109 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
2110 dplane_op2str(op), pw->ifname, pw->local_label,
2111 pw->remote_label);
2112
2113 ctx->zd_op = op;
2114 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2115
2116 /* Capture namespace info: no netlink support as of 12/18,
2117 * but just in case...
2118 */
2119 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2120
2121 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
2122
2123 /* This name appears to be c-string, so we use string copy. */
2124 strlcpy(ctx->zd_ifname, pw->ifname, sizeof(ctx->zd_ifname));
2125
2126 ctx->zd_vrf_id = pw->vrf_id;
2127 ctx->zd_ifindex = pw->ifindex;
2128 ctx->u.pw.type = pw->type;
2129 ctx->u.pw.af = pw->af;
2130 ctx->u.pw.local_label = pw->local_label;
2131 ctx->u.pw.remote_label = pw->remote_label;
2132 ctx->u.pw.flags = pw->flags;
2133
2134 ctx->u.pw.dest = pw->nexthop;
2135
2136 ctx->u.pw.fields = pw->data;
2137
2138 /* Capture nexthop info for the pw destination. We need to look
2139 * up and use zebra datastructs, but we're running in the zebra
2140 * pthread here so that should be ok.
2141 */
2142 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
2143 p.family = pw->af;
2144 p.prefixlen = ((pw->af == AF_INET) ?
2145 IPV4_MAX_PREFIXLEN : IPV6_MAX_PREFIXLEN);
2146
2147 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
2148 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
2149 if (table) {
2150 rn = route_node_match(table, &p);
2151 if (rn) {
2152 RNODE_FOREACH_RE(rn, re) {
2153 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
2154 break;
2155 }
2156
2157 if (re) {
2158 nhg = rib_get_fib_nhg(re);
2159 if (nhg && nhg->nexthop)
2160 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
2161 nhg->nexthop, NULL);
2162
2163 /* Include any installed backup nexthops */
2164 nhg = rib_get_fib_backup_nhg(re);
2165 if (nhg && nhg->nexthop)
2166 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
2167 nhg->nexthop, NULL);
2168 }
2169 route_unlock_node(rn);
2170 }
2171 }
2172
2173 return AOK;
2174 }
2175
2176 /**
2177 * dplane_ctx_rule_init_single() - Initialize a dataplane representation of a
2178 * PBR rule.
2179 *
2180 * @dplane_rule: Dataplane internal representation of a rule
2181 * @rule: PBR rule
2182 */
2183 static void dplane_ctx_rule_init_single(struct dplane_ctx_rule *dplane_rule,
2184 struct zebra_pbr_rule *rule)
2185 {
2186 dplane_rule->priority = rule->rule.priority;
2187 dplane_rule->table = rule->rule.action.table;
2188
2189 dplane_rule->filter_bm = rule->rule.filter.filter_bm;
2190 dplane_rule->fwmark = rule->rule.filter.fwmark;
2191 dplane_rule->dsfield = rule->rule.filter.dsfield;
2192 prefix_copy(&(dplane_rule->dst_ip), &rule->rule.filter.dst_ip);
2193 prefix_copy(&(dplane_rule->src_ip), &rule->rule.filter.src_ip);
2194 }
2195
2196 /**
2197 * dplane_ctx_rule_init() - Initialize a context block for a PBR rule update.
2198 *
2199 * @ctx: Dataplane context to init
2200 * @op: Operation being performed
2201 * @new_rule: PBR rule
2202 *
2203 * Return: Result status
2204 */
2205 static int dplane_ctx_rule_init(struct zebra_dplane_ctx *ctx,
2206 enum dplane_op_e op,
2207 struct zebra_pbr_rule *new_rule,
2208 struct zebra_pbr_rule *old_rule)
2209 {
2210 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2211 char buf1[PREFIX_STRLEN];
2212 char buf2[PREFIX_STRLEN];
2213
2214 zlog_debug(
2215 "init dplane ctx %s: IF %s(%u) Prio %u Fwmark %u Src %s Dst %s Table %u",
2216 dplane_op2str(op), new_rule->ifname,
2217 new_rule->rule.ifindex, new_rule->rule.priority,
2218 new_rule->rule.filter.fwmark,
2219 prefix2str(&new_rule->rule.filter.src_ip, buf1,
2220 sizeof(buf1)),
2221 prefix2str(&new_rule->rule.filter.dst_ip, buf2,
2222 sizeof(buf2)),
2223 new_rule->rule.action.table);
2224 }
2225
2226 ctx->zd_op = op;
2227 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2228
2229 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2230 op == DPLANE_OP_RULE_UPDATE);
2231 ctx->zd_is_update = (op == DPLANE_OP_RULE_UPDATE);
2232
2233 ctx->zd_vrf_id = new_rule->vrf_id;
2234 memcpy(ctx->zd_ifname, new_rule->ifname, sizeof(new_rule->ifname));
2235 ctx->zd_ifindex = new_rule->rule.ifindex;
2236
2237 ctx->u.rule.sock = new_rule->sock;
2238 ctx->u.rule.unique = new_rule->rule.unique;
2239 ctx->u.rule.seq = new_rule->rule.seq;
2240
2241 dplane_ctx_rule_init_single(&ctx->u.rule.new, new_rule);
2242 if (op == DPLANE_OP_RULE_UPDATE)
2243 dplane_ctx_rule_init_single(&ctx->u.rule.old, old_rule);
2244
2245 return AOK;
2246 }
2247
2248 /*
2249 * Enqueue a new update,
2250 * and ensure an event is active for the dataplane pthread.
2251 */
2252 static int dplane_update_enqueue(struct zebra_dplane_ctx *ctx)
2253 {
2254 int ret = EINVAL;
2255 uint32_t high, curr;
2256
2257 /* Enqueue for processing by the dataplane pthread */
2258 DPLANE_LOCK();
2259 {
2260 TAILQ_INSERT_TAIL(&zdplane_info.dg_update_ctx_q, ctx,
2261 zd_q_entries);
2262 }
2263 DPLANE_UNLOCK();
2264
2265 curr = atomic_fetch_add_explicit(
2266 &(zdplane_info.dg_routes_queued),
2267 1, memory_order_seq_cst);
2268
2269 curr++; /* We got the pre-incremented value */
2270
2271 /* Maybe update high-water counter also */
2272 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
2273 memory_order_seq_cst);
2274 while (high < curr) {
2275 if (atomic_compare_exchange_weak_explicit(
2276 &zdplane_info.dg_routes_queued_max,
2277 &high, curr,
2278 memory_order_seq_cst,
2279 memory_order_seq_cst))
2280 break;
2281 }
2282
2283 /* Ensure that an event for the dataplane thread is active */
2284 ret = dplane_provider_work_ready();
2285
2286 return ret;
2287 }
2288
2289 /*
2290 * Utility that prepares a route update and enqueues it for processing
2291 */
2292 static enum zebra_dplane_result
2293 dplane_route_update_internal(struct route_node *rn,
2294 struct route_entry *re,
2295 struct route_entry *old_re,
2296 enum dplane_op_e op)
2297 {
2298 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2299 int ret = EINVAL;
2300 struct zebra_dplane_ctx *ctx = NULL;
2301
2302 /* Obtain context block */
2303 ctx = dplane_ctx_alloc();
2304
2305 /* Init context with info from zebra data structs */
2306 ret = dplane_ctx_route_init(ctx, op, rn, re);
2307 if (ret == AOK) {
2308 /* Capture some extra info for update case
2309 * where there's a different 'old' route.
2310 */
2311 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
2312 old_re && (old_re != re)) {
2313 ctx->zd_is_update = true;
2314
2315 old_re->dplane_sequence =
2316 zebra_router_get_next_sequence();
2317 ctx->zd_old_seq = old_re->dplane_sequence;
2318
2319 ctx->u.rinfo.zd_old_tag = old_re->tag;
2320 ctx->u.rinfo.zd_old_type = old_re->type;
2321 ctx->u.rinfo.zd_old_instance = old_re->instance;
2322 ctx->u.rinfo.zd_old_distance = old_re->distance;
2323 ctx->u.rinfo.zd_old_metric = old_re->metric;
2324
2325 #ifndef HAVE_NETLINK
2326 /* For bsd, capture previous re's nexthops too, sigh.
2327 * We'll need these to do per-nexthop deletes.
2328 */
2329 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
2330 old_re->nhe->nhg.nexthop, NULL);
2331
2332 if (zebra_nhg_get_backup_nhg(old_re->nhe) != NULL) {
2333 struct nexthop_group *nhg;
2334 struct nexthop **nh;
2335
2336 nhg = zebra_nhg_get_backup_nhg(old_re->nhe);
2337 nh = &(ctx->u.rinfo.old_backup_ng.nexthop);
2338
2339 if (nhg->nexthop)
2340 copy_nexthops(nh, nhg->nexthop, NULL);
2341 }
2342 #endif /* !HAVE_NETLINK */
2343 }
2344
2345 /* Enqueue context for processing */
2346 ret = dplane_update_enqueue(ctx);
2347 }
2348
2349 /* Update counter */
2350 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
2351 memory_order_relaxed);
2352
2353 if (ret == AOK)
2354 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2355 else {
2356 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors, 1,
2357 memory_order_relaxed);
2358 if (ctx)
2359 dplane_ctx_free(&ctx);
2360 }
2361
2362 return result;
2363 }
2364
2365 /**
2366 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
2367 *
2368 * @nhe: Nexthop group hash entry where the change occured
2369 * @op: The operation to be enqued
2370 *
2371 * Return: Result of the change
2372 */
2373 static enum zebra_dplane_result
2374 dplane_nexthop_update_internal(struct nhg_hash_entry *nhe, enum dplane_op_e op)
2375 {
2376 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2377 int ret = EINVAL;
2378 struct zebra_dplane_ctx *ctx = NULL;
2379
2380 /* Obtain context block */
2381 ctx = dplane_ctx_alloc();
2382 if (!ctx) {
2383 ret = ENOMEM;
2384 goto done;
2385 }
2386
2387 ret = dplane_ctx_nexthop_init(ctx, op, nhe);
2388 if (ret == AOK)
2389 ret = dplane_update_enqueue(ctx);
2390
2391 done:
2392 /* Update counter */
2393 atomic_fetch_add_explicit(&zdplane_info.dg_nexthops_in, 1,
2394 memory_order_relaxed);
2395
2396 if (ret == AOK)
2397 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2398 else {
2399 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
2400 memory_order_relaxed);
2401 if (ctx)
2402 dplane_ctx_free(&ctx);
2403 }
2404
2405 return result;
2406 }
2407
2408 /*
2409 * Enqueue a route 'add' for the dataplane.
2410 */
2411 enum zebra_dplane_result dplane_route_add(struct route_node *rn,
2412 struct route_entry *re)
2413 {
2414 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2415
2416 if (rn == NULL || re == NULL)
2417 goto done;
2418
2419 ret = dplane_route_update_internal(rn, re, NULL,
2420 DPLANE_OP_ROUTE_INSTALL);
2421
2422 done:
2423 return ret;
2424 }
2425
2426 /*
2427 * Enqueue a route update for the dataplane.
2428 */
2429 enum zebra_dplane_result dplane_route_update(struct route_node *rn,
2430 struct route_entry *re,
2431 struct route_entry *old_re)
2432 {
2433 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2434
2435 if (rn == NULL || re == NULL)
2436 goto done;
2437
2438 ret = dplane_route_update_internal(rn, re, old_re,
2439 DPLANE_OP_ROUTE_UPDATE);
2440 done:
2441 return ret;
2442 }
2443
2444 /*
2445 * Enqueue a route removal for the dataplane.
2446 */
2447 enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
2448 struct route_entry *re)
2449 {
2450 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2451
2452 if (rn == NULL || re == NULL)
2453 goto done;
2454
2455 ret = dplane_route_update_internal(rn, re, NULL,
2456 DPLANE_OP_ROUTE_DELETE);
2457
2458 done:
2459 return ret;
2460 }
2461
2462 /*
2463 * Notify the dplane when system/connected routes change.
2464 */
2465 enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
2466 struct route_entry *re)
2467 {
2468 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2469
2470 /* Ignore this event unless a provider plugin has requested it. */
2471 if (!zdplane_info.dg_sys_route_notifs) {
2472 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2473 goto done;
2474 }
2475
2476 if (rn == NULL || re == NULL)
2477 goto done;
2478
2479 ret = dplane_route_update_internal(rn, re, NULL,
2480 DPLANE_OP_SYS_ROUTE_ADD);
2481
2482 done:
2483 return ret;
2484 }
2485
2486 /*
2487 * Notify the dplane when system/connected routes are deleted.
2488 */
2489 enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
2490 struct route_entry *re)
2491 {
2492 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2493
2494 /* Ignore this event unless a provider plugin has requested it. */
2495 if (!zdplane_info.dg_sys_route_notifs) {
2496 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2497 goto done;
2498 }
2499
2500 if (rn == NULL || re == NULL)
2501 goto done;
2502
2503 ret = dplane_route_update_internal(rn, re, NULL,
2504 DPLANE_OP_SYS_ROUTE_DELETE);
2505
2506 done:
2507 return ret;
2508 }
2509
2510 /*
2511 * Update from an async notification, to bring other fibs up-to-date.
2512 */
2513 enum zebra_dplane_result
2514 dplane_route_notif_update(struct route_node *rn,
2515 struct route_entry *re,
2516 enum dplane_op_e op,
2517 struct zebra_dplane_ctx *ctx)
2518 {
2519 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2520 int ret = EINVAL;
2521 struct zebra_dplane_ctx *new_ctx = NULL;
2522 struct nexthop *nexthop;
2523 struct nexthop_group *nhg;
2524
2525 if (rn == NULL || re == NULL)
2526 goto done;
2527
2528 new_ctx = dplane_ctx_alloc();
2529 if (new_ctx == NULL)
2530 goto done;
2531
2532 /* Init context with info from zebra data structs */
2533 dplane_ctx_route_init(new_ctx, op, rn, re);
2534
2535 /* For add/update, need to adjust the nexthops so that we match
2536 * the notification state, which may not be the route-entry/RIB
2537 * state.
2538 */
2539 if (op == DPLANE_OP_ROUTE_UPDATE ||
2540 op == DPLANE_OP_ROUTE_INSTALL) {
2541
2542 nexthops_free(new_ctx->u.rinfo.zd_ng.nexthop);
2543 new_ctx->u.rinfo.zd_ng.nexthop = NULL;
2544
2545 nhg = rib_get_fib_nhg(re);
2546 if (nhg && nhg->nexthop)
2547 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
2548 nhg->nexthop, NULL);
2549
2550 /* Check for installed backup nexthops also */
2551 nhg = rib_get_fib_backup_nhg(re);
2552 if (nhg && nhg->nexthop) {
2553 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
2554 nhg->nexthop, NULL);
2555 }
2556
2557 for (ALL_NEXTHOPS(new_ctx->u.rinfo.zd_ng, nexthop))
2558 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2559
2560 }
2561
2562 /* Capture info about the source of the notification, in 'ctx' */
2563 dplane_ctx_set_notif_provider(new_ctx,
2564 dplane_ctx_get_notif_provider(ctx));
2565
2566 ret = dplane_update_enqueue(new_ctx);
2567
2568 done:
2569 if (ret == AOK)
2570 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2571 else if (new_ctx)
2572 dplane_ctx_free(&new_ctx);
2573
2574 return result;
2575 }
2576
2577 /*
2578 * Enqueue a nexthop add for the dataplane.
2579 */
2580 enum zebra_dplane_result dplane_nexthop_add(struct nhg_hash_entry *nhe)
2581 {
2582 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2583
2584 if (nhe)
2585 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_INSTALL);
2586 return ret;
2587 }
2588
2589 /*
2590 * Enqueue a nexthop update for the dataplane.
2591 *
2592 * Might not need this func since zebra's nexthop objects should be immutable?
2593 */
2594 enum zebra_dplane_result dplane_nexthop_update(struct nhg_hash_entry *nhe)
2595 {
2596 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2597
2598 if (nhe)
2599 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_UPDATE);
2600 return ret;
2601 }
2602
2603 /*
2604 * Enqueue a nexthop removal for the dataplane.
2605 */
2606 enum zebra_dplane_result dplane_nexthop_delete(struct nhg_hash_entry *nhe)
2607 {
2608 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2609
2610 if (nhe)
2611 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_DELETE);
2612
2613 return ret;
2614 }
2615
2616 /*
2617 * Enqueue LSP add for the dataplane.
2618 */
2619 enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp)
2620 {
2621 enum zebra_dplane_result ret =
2622 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
2623
2624 return ret;
2625 }
2626
2627 /*
2628 * Enqueue LSP update for the dataplane.
2629 */
2630 enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp)
2631 {
2632 enum zebra_dplane_result ret =
2633 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
2634
2635 return ret;
2636 }
2637
2638 /*
2639 * Enqueue LSP delete for the dataplane.
2640 */
2641 enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp)
2642 {
2643 enum zebra_dplane_result ret =
2644 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
2645
2646 return ret;
2647 }
2648
2649 /* Update or un-install resulting from an async notification */
2650 enum zebra_dplane_result
2651 dplane_lsp_notif_update(zebra_lsp_t *lsp,
2652 enum dplane_op_e op,
2653 struct zebra_dplane_ctx *notif_ctx)
2654 {
2655 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2656 int ret = EINVAL;
2657 struct zebra_dplane_ctx *ctx = NULL;
2658 struct nhlfe_list_head *head;
2659 zebra_nhlfe_t *nhlfe, *new_nhlfe;
2660
2661 /* Obtain context block */
2662 ctx = dplane_ctx_alloc();
2663 if (ctx == NULL) {
2664 ret = ENOMEM;
2665 goto done;
2666 }
2667
2668 /* Copy info from zebra LSP */
2669 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2670 if (ret != AOK)
2671 goto done;
2672
2673 /* Add any installed backup nhlfes */
2674 head = &(ctx->u.lsp.backup_nhlfe_list);
2675 frr_each(nhlfe_list, head, nhlfe) {
2676
2677 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) &&
2678 CHECK_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_FIB)) {
2679 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp),
2680 nhlfe->type,
2681 nhlfe->nexthop);
2682
2683 /* Need to copy flags too */
2684 new_nhlfe->flags = nhlfe->flags;
2685 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2686 }
2687 }
2688
2689 /* Capture info about the source of the notification */
2690 dplane_ctx_set_notif_provider(
2691 ctx,
2692 dplane_ctx_get_notif_provider(notif_ctx));
2693
2694 ret = dplane_update_enqueue(ctx);
2695
2696 done:
2697 /* Update counter */
2698 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2699 memory_order_relaxed);
2700
2701 if (ret == AOK)
2702 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2703 else {
2704 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2705 memory_order_relaxed);
2706 if (ctx)
2707 dplane_ctx_free(&ctx);
2708 }
2709 return result;
2710 }
2711
2712 /*
2713 * Enqueue pseudowire install for the dataplane.
2714 */
2715 enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
2716 {
2717 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
2718 }
2719
2720 /*
2721 * Enqueue pseudowire un-install for the dataplane.
2722 */
2723 enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
2724 {
2725 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
2726 }
2727
2728 /*
2729 * Common internal LSP update utility
2730 */
2731 static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
2732 enum dplane_op_e op)
2733 {
2734 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2735 int ret = EINVAL;
2736 struct zebra_dplane_ctx *ctx = NULL;
2737
2738 /* Obtain context block */
2739 ctx = dplane_ctx_alloc();
2740
2741 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2742 if (ret != AOK)
2743 goto done;
2744
2745 ret = dplane_update_enqueue(ctx);
2746
2747 done:
2748 /* Update counter */
2749 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2750 memory_order_relaxed);
2751
2752 if (ret == AOK)
2753 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2754 else {
2755 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2756 memory_order_relaxed);
2757 dplane_ctx_free(&ctx);
2758 }
2759
2760 return result;
2761 }
2762
2763 /*
2764 * Internal, common handler for pseudowire updates.
2765 */
2766 static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
2767 enum dplane_op_e op)
2768 {
2769 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2770 int ret;
2771 struct zebra_dplane_ctx *ctx = NULL;
2772
2773 ctx = dplane_ctx_alloc();
2774
2775 ret = dplane_ctx_pw_init(ctx, op, pw);
2776 if (ret != AOK)
2777 goto done;
2778
2779 ret = dplane_update_enqueue(ctx);
2780
2781 done:
2782 /* Update counter */
2783 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
2784 memory_order_relaxed);
2785
2786 if (ret == AOK)
2787 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2788 else {
2789 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
2790 memory_order_relaxed);
2791 dplane_ctx_free(&ctx);
2792 }
2793
2794 return result;
2795 }
2796
2797 /*
2798 * Enqueue interface address add for the dataplane.
2799 */
2800 enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
2801 const struct connected *ifc)
2802 {
2803 #if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2804 /* Extra checks for this OS path. */
2805
2806 /* Don't configure PtP addresses on broadcast ifs or reverse */
2807 if (!(ifp->flags & IFF_POINTOPOINT) != !CONNECTED_PEER(ifc)) {
2808 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_DPLANE)
2809 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2810
2811 return ZEBRA_DPLANE_REQUEST_FAILURE;
2812 }
2813
2814 /* Ensure that no existing installed v4 route conflicts with
2815 * the new interface prefix. This check must be done in the
2816 * zebra pthread context, and any route delete (if needed)
2817 * is enqueued before the interface address programming attempt.
2818 */
2819 if (ifc->address->family == AF_INET) {
2820 struct prefix_ipv4 *p;
2821
2822 p = (struct prefix_ipv4 *)ifc->address;
2823 rib_lookup_and_pushup(p, ifp->vrf_id);
2824 }
2825 #endif
2826
2827 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_INSTALL);
2828 }
2829
2830 /*
2831 * Enqueue interface address remove/uninstall for the dataplane.
2832 */
2833 enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
2834 const struct connected *ifc)
2835 {
2836 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_UNINSTALL);
2837 }
2838
2839 static enum zebra_dplane_result intf_addr_update_internal(
2840 const struct interface *ifp, const struct connected *ifc,
2841 enum dplane_op_e op)
2842 {
2843 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2844 int ret = EINVAL;
2845 struct zebra_dplane_ctx *ctx = NULL;
2846 struct zebra_ns *zns;
2847
2848 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2849 char addr_str[PREFIX_STRLEN];
2850
2851 prefix2str(ifc->address, addr_str, sizeof(addr_str));
2852
2853 zlog_debug("init intf ctx %s: idx %d, addr %u:%s",
2854 dplane_op2str(op), ifp->ifindex, ifp->vrf_id,
2855 addr_str);
2856 }
2857
2858 ctx = dplane_ctx_alloc();
2859
2860 ctx->zd_op = op;
2861 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2862 ctx->zd_vrf_id = ifp->vrf_id;
2863
2864 zns = zebra_ns_lookup(ifp->vrf_id);
2865 dplane_ctx_ns_init(ctx, zns, false);
2866
2867 /* Init the interface-addr-specific area */
2868 memset(&ctx->u.intf, 0, sizeof(ctx->u.intf));
2869
2870 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2871 ctx->zd_ifindex = ifp->ifindex;
2872 ctx->u.intf.prefix = *(ifc->address);
2873
2874 if (if_is_broadcast(ifp))
2875 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
2876
2877 if (CONNECTED_PEER(ifc)) {
2878 ctx->u.intf.dest_prefix = *(ifc->destination);
2879 ctx->u.intf.flags |=
2880 (DPLANE_INTF_CONNECTED | DPLANE_INTF_HAS_DEST);
2881 }
2882
2883 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
2884 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
2885
2886 if (ifc->label) {
2887 size_t len;
2888
2889 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
2890
2891 /* Use embedded buffer if it's adequate; else allocate. */
2892 len = strlen(ifc->label);
2893
2894 if (len < sizeof(ctx->u.intf.label_buf)) {
2895 strlcpy(ctx->u.intf.label_buf, ifc->label,
2896 sizeof(ctx->u.intf.label_buf));
2897 ctx->u.intf.label = ctx->u.intf.label_buf;
2898 } else {
2899 ctx->u.intf.label = strdup(ifc->label);
2900 }
2901 }
2902
2903 ret = dplane_update_enqueue(ctx);
2904
2905 /* Increment counter */
2906 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addrs_in, 1,
2907 memory_order_relaxed);
2908
2909 if (ret == AOK)
2910 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2911 else {
2912 /* Error counter */
2913 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
2914 1, memory_order_relaxed);
2915 dplane_ctx_free(&ctx);
2916 }
2917
2918 return result;
2919 }
2920
2921 /*
2922 * Enqueue vxlan/evpn mac add (or update).
2923 */
2924 enum zebra_dplane_result dplane_rem_mac_add(const struct interface *ifp,
2925 const struct interface *bridge_ifp,
2926 vlanid_t vid,
2927 const struct ethaddr *mac,
2928 struct in_addr vtep_ip,
2929 bool sticky,
2930 uint32_t nhg_id,
2931 bool was_static)
2932 {
2933 enum zebra_dplane_result result;
2934 uint32_t update_flags = 0;
2935
2936 update_flags |= DPLANE_MAC_REMOTE;
2937 if (was_static)
2938 update_flags |= DPLANE_MAC_WAS_STATIC;
2939
2940 /* Use common helper api */
2941 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
2942 vid, mac, vtep_ip, sticky, nhg_id, update_flags);
2943 return result;
2944 }
2945
2946 /*
2947 * Enqueue vxlan/evpn mac delete.
2948 */
2949 enum zebra_dplane_result dplane_rem_mac_del(const struct interface *ifp,
2950 const struct interface *bridge_ifp,
2951 vlanid_t vid,
2952 const struct ethaddr *mac,
2953 struct in_addr vtep_ip)
2954 {
2955 enum zebra_dplane_result result;
2956 uint32_t update_flags = 0;
2957
2958 update_flags |= DPLANE_MAC_REMOTE;
2959
2960 /* Use common helper api */
2961 result = mac_update_common(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp,
2962 vid, mac, vtep_ip, false, 0, update_flags);
2963 return result;
2964 }
2965
2966 /*
2967 * Enqueue local mac add (or update).
2968 */
2969 enum zebra_dplane_result dplane_local_mac_add(const struct interface *ifp,
2970 const struct interface *bridge_ifp,
2971 vlanid_t vid,
2972 const struct ethaddr *mac,
2973 bool sticky,
2974 uint32_t set_static,
2975 uint32_t set_inactive)
2976 {
2977 enum zebra_dplane_result result;
2978 uint32_t update_flags = 0;
2979 struct in_addr vtep_ip;
2980
2981 if (set_static)
2982 update_flags |= DPLANE_MAC_SET_STATIC;
2983
2984 if (set_inactive)
2985 update_flags |= DPLANE_MAC_SET_INACTIVE;
2986
2987 vtep_ip.s_addr = 0;
2988
2989 /* Use common helper api */
2990 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
2991 vid, mac, vtep_ip, sticky, 0,
2992 update_flags);
2993 return result;
2994 }
2995
2996 /*
2997 * Public api to init an empty context - either newly-allocated or
2998 * reset/cleared - for a MAC update.
2999 */
3000 void dplane_mac_init(struct zebra_dplane_ctx *ctx,
3001 const struct interface *ifp,
3002 const struct interface *br_ifp,
3003 vlanid_t vid,
3004 const struct ethaddr *mac,
3005 struct in_addr vtep_ip,
3006 bool sticky,
3007 uint32_t nhg_id,
3008 uint32_t update_flags)
3009 {
3010 struct zebra_ns *zns;
3011
3012 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3013 ctx->zd_vrf_id = ifp->vrf_id;
3014
3015 zns = zebra_ns_lookup(ifp->vrf_id);
3016 dplane_ctx_ns_init(ctx, zns, false);
3017
3018 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3019 ctx->zd_ifindex = ifp->ifindex;
3020
3021 /* Init the mac-specific data area */
3022 memset(&ctx->u.macinfo, 0, sizeof(ctx->u.macinfo));
3023
3024 ctx->u.macinfo.br_ifindex = br_ifp->ifindex;
3025 ctx->u.macinfo.vtep_ip = vtep_ip;
3026 ctx->u.macinfo.mac = *mac;
3027 ctx->u.macinfo.vid = vid;
3028 ctx->u.macinfo.is_sticky = sticky;
3029 ctx->u.macinfo.nhg_id = nhg_id;
3030 ctx->u.macinfo.update_flags = update_flags;
3031 }
3032
3033 /*
3034 * Common helper api for MAC address/vxlan updates
3035 */
3036 static enum zebra_dplane_result
3037 mac_update_common(enum dplane_op_e op,
3038 const struct interface *ifp,
3039 const struct interface *br_ifp,
3040 vlanid_t vid,
3041 const struct ethaddr *mac,
3042 struct in_addr vtep_ip,
3043 bool sticky,
3044 uint32_t nhg_id,
3045 uint32_t update_flags)
3046 {
3047 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3048 int ret;
3049 struct zebra_dplane_ctx *ctx = NULL;
3050
3051 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3052 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
3053
3054 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
3055 dplane_op2str(op),
3056 prefix_mac2str(mac, buf1, sizeof(buf1)),
3057 ifp->name,
3058 inet_ntop(AF_INET, &vtep_ip, buf2, sizeof(buf2)));
3059 }
3060
3061 ctx = dplane_ctx_alloc();
3062 ctx->zd_op = op;
3063
3064 /* Common init for the ctx */
3065 dplane_mac_init(ctx, ifp, br_ifp, vid, mac, vtep_ip, sticky,
3066 nhg_id, update_flags);
3067
3068 /* Enqueue for processing on the dplane pthread */
3069 ret = dplane_update_enqueue(ctx);
3070
3071 /* Increment counter */
3072 atomic_fetch_add_explicit(&zdplane_info.dg_macs_in, 1,
3073 memory_order_relaxed);
3074
3075 if (ret == AOK)
3076 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3077 else {
3078 /* Error counter */
3079 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors, 1,
3080 memory_order_relaxed);
3081 dplane_ctx_free(&ctx);
3082 }
3083
3084 return result;
3085 }
3086
3087 /*
3088 * Enqueue evpn neighbor add for the dataplane.
3089 */
3090 enum zebra_dplane_result dplane_rem_neigh_add(const struct interface *ifp,
3091 const struct ipaddr *ip,
3092 const struct ethaddr *mac,
3093 uint32_t flags, bool was_static)
3094 {
3095 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3096 uint32_t update_flags = 0;
3097
3098 update_flags |= DPLANE_NEIGH_REMOTE;
3099
3100 if (was_static)
3101 update_flags |= DPLANE_NEIGH_WAS_STATIC;
3102
3103 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
3104 ifp, mac, ip, flags, DPLANE_NUD_NOARP,
3105 update_flags);
3106
3107 return result;
3108 }
3109
3110 /*
3111 * Enqueue local neighbor add for the dataplane.
3112 */
3113 enum zebra_dplane_result dplane_local_neigh_add(const struct interface *ifp,
3114 const struct ipaddr *ip,
3115 const struct ethaddr *mac,
3116 bool set_router, bool set_static,
3117 bool set_inactive)
3118 {
3119 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3120 uint32_t update_flags = 0;
3121 uint32_t ntf = 0;
3122 uint16_t state;
3123
3124 if (set_static)
3125 update_flags |= DPLANE_NEIGH_SET_STATIC;
3126
3127 if (set_inactive) {
3128 update_flags |= DPLANE_NEIGH_SET_INACTIVE;
3129 state = DPLANE_NUD_STALE;
3130 } else {
3131 state = DPLANE_NUD_REACHABLE;
3132 }
3133
3134 if (set_router)
3135 ntf |= DPLANE_NTF_ROUTER;
3136
3137 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
3138 ifp, mac, ip, ntf,
3139 state, update_flags);
3140
3141 return result;
3142 }
3143
3144 /*
3145 * Enqueue evpn neighbor delete for the dataplane.
3146 */
3147 enum zebra_dplane_result dplane_rem_neigh_delete(const struct interface *ifp,
3148 const struct ipaddr *ip)
3149 {
3150 enum zebra_dplane_result result;
3151 uint32_t update_flags = 0;
3152
3153 update_flags |= DPLANE_NEIGH_REMOTE;
3154
3155 result = neigh_update_internal(DPLANE_OP_NEIGH_DELETE,
3156 ifp, NULL, ip, 0, 0, update_flags);
3157
3158 return result;
3159 }
3160
3161 /*
3162 * Enqueue evpn VTEP add for the dataplane.
3163 */
3164 enum zebra_dplane_result dplane_vtep_add(const struct interface *ifp,
3165 const struct in_addr *ip,
3166 vni_t vni)
3167 {
3168 enum zebra_dplane_result result;
3169 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3170 struct ipaddr addr;
3171
3172 if (IS_ZEBRA_DEBUG_VXLAN)
3173 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
3174 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3175
3176 SET_IPADDR_V4(&addr);
3177 addr.ipaddr_v4 = *ip;
3178
3179 result = neigh_update_internal(DPLANE_OP_VTEP_ADD,
3180 ifp, &mac, &addr, 0, 0, 0);
3181
3182 return result;
3183 }
3184
3185 /*
3186 * Enqueue evpn VTEP add for the dataplane.
3187 */
3188 enum zebra_dplane_result dplane_vtep_delete(const struct interface *ifp,
3189 const struct in_addr *ip,
3190 vni_t vni)
3191 {
3192 enum zebra_dplane_result result;
3193 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3194 struct ipaddr addr;
3195
3196 if (IS_ZEBRA_DEBUG_VXLAN)
3197 zlog_debug(
3198 "Uninstall %s from flood list for VNI %u intf %s(%u)",
3199 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3200
3201 SET_IPADDR_V4(&addr);
3202 addr.ipaddr_v4 = *ip;
3203
3204 result = neigh_update_internal(DPLANE_OP_VTEP_DELETE,
3205 ifp, &mac, &addr, 0, 0, 0);
3206
3207 return result;
3208 }
3209
3210 enum zebra_dplane_result dplane_neigh_discover(const struct interface *ifp,
3211 const struct ipaddr *ip)
3212 {
3213 enum zebra_dplane_result result;
3214
3215 result = neigh_update_internal(DPLANE_OP_NEIGH_DISCOVER, ifp, NULL, ip,
3216 DPLANE_NTF_USE, DPLANE_NUD_INCOMPLETE, 0);
3217
3218 return result;
3219 }
3220
3221 /*
3222 * Common helper api for neighbor updates
3223 */
3224 static enum zebra_dplane_result
3225 neigh_update_internal(enum dplane_op_e op,
3226 const struct interface *ifp,
3227 const struct ethaddr *mac,
3228 const struct ipaddr *ip,
3229 uint32_t flags, uint16_t state,
3230 uint32_t update_flags)
3231 {
3232 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3233 int ret;
3234 struct zebra_dplane_ctx *ctx = NULL;
3235 struct zebra_ns *zns;
3236
3237 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3238 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
3239
3240 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
3241 dplane_op2str(op), ifp->name,
3242 prefix_mac2str(mac, buf1, sizeof(buf1)),
3243 ipaddr2str(ip, buf2, sizeof(buf2)));
3244 }
3245
3246 ctx = dplane_ctx_alloc();
3247
3248 ctx->zd_op = op;
3249 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3250 ctx->zd_vrf_id = ifp->vrf_id;
3251
3252 zns = zebra_ns_lookup(ifp->vrf_id);
3253 dplane_ctx_ns_init(ctx, zns, false);
3254
3255 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3256 ctx->zd_ifindex = ifp->ifindex;
3257
3258 /* Init the neighbor-specific data area */
3259 memset(&ctx->u.neigh, 0, sizeof(ctx->u.neigh));
3260
3261 ctx->u.neigh.ip_addr = *ip;
3262 if (mac)
3263 ctx->u.neigh.mac = *mac;
3264 ctx->u.neigh.flags = flags;
3265 ctx->u.neigh.state = state;
3266 ctx->u.neigh.update_flags = update_flags;
3267
3268 /* Enqueue for processing on the dplane pthread */
3269 ret = dplane_update_enqueue(ctx);
3270
3271 /* Increment counter */
3272 atomic_fetch_add_explicit(&zdplane_info.dg_neighs_in, 1,
3273 memory_order_relaxed);
3274
3275 if (ret == AOK)
3276 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3277 else {
3278 /* Error counter */
3279 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors, 1,
3280 memory_order_relaxed);
3281 dplane_ctx_free(&ctx);
3282 }
3283
3284 return result;
3285 }
3286
3287 /*
3288 * Common helper api for PBR rule updates
3289 */
3290 static enum zebra_dplane_result
3291 rule_update_internal(enum dplane_op_e op, struct zebra_pbr_rule *new_rule,
3292 struct zebra_pbr_rule *old_rule)
3293 {
3294 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3295 struct zebra_dplane_ctx *ctx;
3296 int ret;
3297
3298 ctx = dplane_ctx_alloc();
3299
3300 ret = dplane_ctx_rule_init(ctx, op, new_rule, old_rule);
3301 if (ret != AOK)
3302 goto done;
3303
3304 ret = dplane_update_enqueue(ctx);
3305
3306 done:
3307 atomic_fetch_add_explicit(&zdplane_info.dg_rules_in, 1,
3308 memory_order_relaxed);
3309
3310 if (ret == AOK)
3311 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3312 else {
3313 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors, 1,
3314 memory_order_relaxed);
3315 dplane_ctx_free(&ctx);
3316 }
3317
3318 return result;
3319 }
3320
3321 enum zebra_dplane_result dplane_pbr_rule_add(struct zebra_pbr_rule *rule)
3322 {
3323 return rule_update_internal(DPLANE_OP_RULE_ADD, rule, NULL);
3324 }
3325
3326 enum zebra_dplane_result dplane_pbr_rule_delete(struct zebra_pbr_rule *rule)
3327 {
3328 return rule_update_internal(DPLANE_OP_RULE_DELETE, rule, NULL);
3329 }
3330
3331 enum zebra_dplane_result dplane_pbr_rule_update(struct zebra_pbr_rule *old_rule,
3332 struct zebra_pbr_rule *new_rule)
3333 {
3334 return rule_update_internal(DPLANE_OP_RULE_UPDATE, new_rule, old_rule);
3335 }
3336
3337 /*
3338 * Handler for 'show dplane'
3339 */
3340 int dplane_show_helper(struct vty *vty, bool detailed)
3341 {
3342 uint64_t queued, queue_max, limit, errs, incoming, yields,
3343 other_errs;
3344
3345 /* Using atomics because counters are being changed in different
3346 * pthread contexts.
3347 */
3348 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
3349 memory_order_relaxed);
3350 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
3351 memory_order_relaxed);
3352 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
3353 memory_order_relaxed);
3354 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
3355 memory_order_relaxed);
3356 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
3357 memory_order_relaxed);
3358 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
3359 memory_order_relaxed);
3360 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
3361 memory_order_relaxed);
3362
3363 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
3364 incoming);
3365 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
3366 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
3367 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
3368 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
3369 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
3370 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
3371
3372 incoming = atomic_load_explicit(&zdplane_info.dg_lsps_in,
3373 memory_order_relaxed);
3374 errs = atomic_load_explicit(&zdplane_info.dg_lsp_errors,
3375 memory_order_relaxed);
3376 vty_out(vty, "LSP updates: %"PRIu64"\n", incoming);
3377 vty_out(vty, "LSP update errors: %"PRIu64"\n", errs);
3378
3379 incoming = atomic_load_explicit(&zdplane_info.dg_pws_in,
3380 memory_order_relaxed);
3381 errs = atomic_load_explicit(&zdplane_info.dg_pw_errors,
3382 memory_order_relaxed);
3383 vty_out(vty, "PW updates: %"PRIu64"\n", incoming);
3384 vty_out(vty, "PW update errors: %"PRIu64"\n", errs);
3385
3386 incoming = atomic_load_explicit(&zdplane_info.dg_intf_addrs_in,
3387 memory_order_relaxed);
3388 errs = atomic_load_explicit(&zdplane_info.dg_intf_addr_errors,
3389 memory_order_relaxed);
3390 vty_out(vty, "Intf addr updates: %"PRIu64"\n", incoming);
3391 vty_out(vty, "Intf addr errors: %"PRIu64"\n", errs);
3392
3393 incoming = atomic_load_explicit(&zdplane_info.dg_macs_in,
3394 memory_order_relaxed);
3395 errs = atomic_load_explicit(&zdplane_info.dg_mac_errors,
3396 memory_order_relaxed);
3397 vty_out(vty, "EVPN MAC updates: %"PRIu64"\n", incoming);
3398 vty_out(vty, "EVPN MAC errors: %"PRIu64"\n", errs);
3399
3400 incoming = atomic_load_explicit(&zdplane_info.dg_neighs_in,
3401 memory_order_relaxed);
3402 errs = atomic_load_explicit(&zdplane_info.dg_neigh_errors,
3403 memory_order_relaxed);
3404 vty_out(vty, "EVPN neigh updates: %"PRIu64"\n", incoming);
3405 vty_out(vty, "EVPN neigh errors: %"PRIu64"\n", errs);
3406
3407 incoming = atomic_load_explicit(&zdplane_info.dg_rules_in,
3408 memory_order_relaxed);
3409 errs = atomic_load_explicit(&zdplane_info.dg_rule_errors,
3410 memory_order_relaxed);
3411 vty_out(vty, "Rule updates: %" PRIu64 "\n", incoming);
3412 vty_out(vty, "Rule errors: %" PRIu64 "\n", errs);
3413
3414 return CMD_SUCCESS;
3415 }
3416
3417 /*
3418 * Handler for 'show dplane providers'
3419 */
3420 int dplane_show_provs_helper(struct vty *vty, bool detailed)
3421 {
3422 struct zebra_dplane_provider *prov;
3423 uint64_t in, in_max, out, out_max;
3424
3425 vty_out(vty, "Zebra dataplane providers:\n");
3426
3427 DPLANE_LOCK();
3428 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
3429 DPLANE_UNLOCK();
3430
3431 /* Show counters, useful info from each registered provider */
3432 while (prov) {
3433
3434 in = atomic_load_explicit(&prov->dp_in_counter,
3435 memory_order_relaxed);
3436 in_max = atomic_load_explicit(&prov->dp_in_max,
3437 memory_order_relaxed);
3438 out = atomic_load_explicit(&prov->dp_out_counter,
3439 memory_order_relaxed);
3440 out_max = atomic_load_explicit(&prov->dp_out_max,
3441 memory_order_relaxed);
3442
3443 vty_out(vty,
3444 "%s (%u): in: %" PRIu64 ", q_max: %" PRIu64
3445 ", out: %" PRIu64 ", q_max: %" PRIu64 "\n",
3446 prov->dp_name, prov->dp_id, in, in_max, out, out_max);
3447
3448 DPLANE_LOCK();
3449 prov = TAILQ_NEXT(prov, dp_prov_link);
3450 DPLANE_UNLOCK();
3451 }
3452
3453 return CMD_SUCCESS;
3454 }
3455
3456 /*
3457 * Helper for 'show run' etc.
3458 */
3459 int dplane_config_write_helper(struct vty *vty)
3460 {
3461 if (zdplane_info.dg_max_queued_updates != DPLANE_DEFAULT_MAX_QUEUED)
3462 vty_out(vty, "zebra dplane limit %u\n",
3463 zdplane_info.dg_max_queued_updates);
3464
3465 return 0;
3466 }
3467
3468 /*
3469 * Provider registration
3470 */
3471 int dplane_provider_register(const char *name,
3472 enum dplane_provider_prio prio,
3473 int flags,
3474 int (*start_fp)(struct zebra_dplane_provider *),
3475 int (*fp)(struct zebra_dplane_provider *),
3476 int (*fini_fp)(struct zebra_dplane_provider *,
3477 bool early),
3478 void *data,
3479 struct zebra_dplane_provider **prov_p)
3480 {
3481 int ret = 0;
3482 struct zebra_dplane_provider *p = NULL, *last;
3483
3484 /* Validate */
3485 if (fp == NULL) {
3486 ret = EINVAL;
3487 goto done;
3488 }
3489
3490 if (prio <= DPLANE_PRIO_NONE ||
3491 prio > DPLANE_PRIO_LAST) {
3492 ret = EINVAL;
3493 goto done;
3494 }
3495
3496 /* Allocate and init new provider struct */
3497 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
3498
3499 pthread_mutex_init(&(p->dp_mutex), NULL);
3500 TAILQ_INIT(&(p->dp_ctx_in_q));
3501 TAILQ_INIT(&(p->dp_ctx_out_q));
3502
3503 p->dp_flags = flags;
3504 p->dp_priority = prio;
3505 p->dp_fp = fp;
3506 p->dp_start = start_fp;
3507 p->dp_fini = fini_fp;
3508 p->dp_data = data;
3509
3510 /* Lock - the dplane pthread may be running */
3511 DPLANE_LOCK();
3512
3513 p->dp_id = ++zdplane_info.dg_provider_id;
3514
3515 if (name)
3516 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
3517 else
3518 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
3519 "provider-%u", p->dp_id);
3520
3521 /* Insert into list ordered by priority */
3522 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
3523 if (last->dp_priority > p->dp_priority)
3524 break;
3525 }
3526
3527 if (last)
3528 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
3529 else
3530 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
3531 dp_prov_link);
3532
3533 /* And unlock */
3534 DPLANE_UNLOCK();
3535
3536 if (IS_ZEBRA_DEBUG_DPLANE)
3537 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
3538 p->dp_name, p->dp_id, p->dp_priority);
3539
3540 done:
3541 if (prov_p)
3542 *prov_p = p;
3543
3544 return ret;
3545 }
3546
3547 /* Accessors for provider attributes */
3548 const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
3549 {
3550 return prov->dp_name;
3551 }
3552
3553 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
3554 {
3555 return prov->dp_id;
3556 }
3557
3558 void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
3559 {
3560 return prov->dp_data;
3561 }
3562
3563 int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
3564 {
3565 return zdplane_info.dg_updates_per_cycle;
3566 }
3567
3568 /* Lock/unlock a provider's mutex - iff the provider was registered with
3569 * the THREADED flag.
3570 */
3571 void dplane_provider_lock(struct zebra_dplane_provider *prov)
3572 {
3573 if (dplane_provider_is_threaded(prov))
3574 DPLANE_PROV_LOCK(prov);
3575 }
3576
3577 void dplane_provider_unlock(struct zebra_dplane_provider *prov)
3578 {
3579 if (dplane_provider_is_threaded(prov))
3580 DPLANE_PROV_UNLOCK(prov);
3581 }
3582
3583 /*
3584 * Dequeue and maintain associated counter
3585 */
3586 struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
3587 struct zebra_dplane_provider *prov)
3588 {
3589 struct zebra_dplane_ctx *ctx = NULL;
3590
3591 dplane_provider_lock(prov);
3592
3593 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3594 if (ctx) {
3595 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
3596
3597 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
3598 memory_order_relaxed);
3599 }
3600
3601 dplane_provider_unlock(prov);
3602
3603 return ctx;
3604 }
3605
3606 /*
3607 * Dequeue work to a list, return count
3608 */
3609 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
3610 struct dplane_ctx_q *listp)
3611 {
3612 int limit, ret;
3613 struct zebra_dplane_ctx *ctx;
3614
3615 limit = zdplane_info.dg_updates_per_cycle;
3616
3617 dplane_provider_lock(prov);
3618
3619 for (ret = 0; ret < limit; ret++) {
3620 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3621 if (ctx) {
3622 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
3623
3624 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
3625 } else {
3626 break;
3627 }
3628 }
3629
3630 if (ret > 0)
3631 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
3632 memory_order_relaxed);
3633
3634 dplane_provider_unlock(prov);
3635
3636 return ret;
3637 }
3638
3639 /*
3640 * Enqueue and maintain associated counter
3641 */
3642 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
3643 struct zebra_dplane_ctx *ctx)
3644 {
3645 dplane_provider_lock(prov);
3646
3647 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
3648 zd_q_entries);
3649
3650 dplane_provider_unlock(prov);
3651
3652 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
3653 memory_order_relaxed);
3654 }
3655
3656 /*
3657 * Accessor for provider object
3658 */
3659 bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
3660 {
3661 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
3662 }
3663
3664 /*
3665 * Internal helper that copies information from a zebra ns object; this is
3666 * called in the zebra main pthread context as part of dplane ctx init.
3667 */
3668 static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
3669 struct zebra_ns *zns)
3670 {
3671 ns_info->ns_id = zns->ns_id;
3672
3673 #if defined(HAVE_NETLINK)
3674 ns_info->is_cmd = true;
3675 ns_info->nls = zns->netlink_dplane;
3676 #endif /* NETLINK */
3677 }
3678
3679 /*
3680 * Provider api to signal that work/events are available
3681 * for the dataplane pthread.
3682 */
3683 int dplane_provider_work_ready(void)
3684 {
3685 /* Note that during zebra startup, we may be offered work before
3686 * the dataplane pthread (and thread-master) are ready. We want to
3687 * enqueue the work, but the event-scheduling machinery may not be
3688 * available.
3689 */
3690 if (zdplane_info.dg_run) {
3691 thread_add_event(zdplane_info.dg_master,
3692 dplane_thread_loop, NULL, 0,
3693 &zdplane_info.dg_t_update);
3694 }
3695
3696 return AOK;
3697 }
3698
3699 /*
3700 * Enqueue a context directly to zebra main.
3701 */
3702 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx)
3703 {
3704 struct dplane_ctx_q temp_list;
3705
3706 /* Zebra's api takes a list, so we need to use a temporary list */
3707 TAILQ_INIT(&temp_list);
3708
3709 TAILQ_INSERT_TAIL(&temp_list, ctx, zd_q_entries);
3710 (zdplane_info.dg_results_cb)(&temp_list);
3711 }
3712
3713 /*
3714 * Kernel dataplane provider
3715 */
3716
3717 static void kernel_dplane_log_detail(struct zebra_dplane_ctx *ctx)
3718 {
3719 char buf[PREFIX_STRLEN];
3720
3721 switch (dplane_ctx_get_op(ctx)) {
3722
3723 case DPLANE_OP_ROUTE_INSTALL:
3724 case DPLANE_OP_ROUTE_UPDATE:
3725 case DPLANE_OP_ROUTE_DELETE:
3726 prefix2str(dplane_ctx_get_dest(ctx), buf, sizeof(buf));
3727
3728 zlog_debug("%u:%s Dplane route update ctx %p op %s",
3729 dplane_ctx_get_vrf(ctx), buf, ctx,
3730 dplane_op2str(dplane_ctx_get_op(ctx)));
3731 break;
3732
3733 case DPLANE_OP_NH_INSTALL:
3734 case DPLANE_OP_NH_UPDATE:
3735 case DPLANE_OP_NH_DELETE:
3736 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
3737 dplane_ctx_get_nhe_id(ctx), ctx,
3738 dplane_op2str(dplane_ctx_get_op(ctx)));
3739 break;
3740
3741 case DPLANE_OP_LSP_INSTALL:
3742 case DPLANE_OP_LSP_UPDATE:
3743 case DPLANE_OP_LSP_DELETE:
3744 break;
3745
3746 case DPLANE_OP_PW_INSTALL:
3747 case DPLANE_OP_PW_UNINSTALL:
3748 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
3749 dplane_ctx_get_ifname(ctx),
3750 dplane_op2str(ctx->zd_op), dplane_ctx_get_pw_af(ctx),
3751 dplane_ctx_get_pw_local_label(ctx),
3752 dplane_ctx_get_pw_remote_label(ctx));
3753 break;
3754
3755 case DPLANE_OP_ADDR_INSTALL:
3756 case DPLANE_OP_ADDR_UNINSTALL:
3757 prefix2str(dplane_ctx_get_intf_addr(ctx), buf, sizeof(buf));
3758
3759 zlog_debug("Dplane intf %s, idx %u, addr %s",
3760 dplane_op2str(dplane_ctx_get_op(ctx)),
3761 dplane_ctx_get_ifindex(ctx), buf);
3762 break;
3763
3764 case DPLANE_OP_MAC_INSTALL:
3765 case DPLANE_OP_MAC_DELETE:
3766 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf,
3767 sizeof(buf));
3768
3769 zlog_debug("Dplane %s, mac %s, ifindex %u",
3770 dplane_op2str(dplane_ctx_get_op(ctx)),
3771 buf, dplane_ctx_get_ifindex(ctx));
3772 break;
3773
3774 case DPLANE_OP_NEIGH_INSTALL:
3775 case DPLANE_OP_NEIGH_UPDATE:
3776 case DPLANE_OP_NEIGH_DELETE:
3777 case DPLANE_OP_VTEP_ADD:
3778 case DPLANE_OP_VTEP_DELETE:
3779 case DPLANE_OP_NEIGH_DISCOVER:
3780 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx), buf,
3781 sizeof(buf));
3782
3783 zlog_debug("Dplane %s, ip %s, ifindex %u",
3784 dplane_op2str(dplane_ctx_get_op(ctx)),
3785 buf, dplane_ctx_get_ifindex(ctx));
3786 break;
3787
3788 case DPLANE_OP_RULE_ADD:
3789 case DPLANE_OP_RULE_DELETE:
3790 case DPLANE_OP_RULE_UPDATE:
3791 zlog_debug("Dplane rule update op %s, if %s(%u), ctx %p",
3792 dplane_op2str(dplane_ctx_get_op(ctx)),
3793 dplane_ctx_get_ifname(ctx),
3794 dplane_ctx_get_ifindex(ctx), ctx);
3795 break;
3796
3797 case DPLANE_OP_SYS_ROUTE_ADD:
3798 case DPLANE_OP_SYS_ROUTE_DELETE:
3799 case DPLANE_OP_ROUTE_NOTIFY:
3800 case DPLANE_OP_LSP_NOTIFY:
3801
3802 case DPLANE_OP_NONE:
3803 break;
3804 }
3805 }
3806
3807 static void kernel_dplane_handle_result(struct zebra_dplane_ctx *ctx)
3808 {
3809 enum zebra_dplane_result res = dplane_ctx_get_status(ctx);
3810
3811 switch (dplane_ctx_get_op(ctx)) {
3812
3813 case DPLANE_OP_ROUTE_INSTALL:
3814 case DPLANE_OP_ROUTE_UPDATE:
3815 case DPLANE_OP_ROUTE_DELETE:
3816 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3817 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
3818 1, memory_order_relaxed);
3819
3820 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE)
3821 && (res == ZEBRA_DPLANE_REQUEST_SUCCESS)) {
3822 struct nexthop *nexthop;
3823
3824 /* Update installed nexthops to signal which have been
3825 * installed.
3826 */
3827 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
3828 nexthop)) {
3829 if (CHECK_FLAG(nexthop->flags,
3830 NEXTHOP_FLAG_RECURSIVE))
3831 continue;
3832
3833 if (CHECK_FLAG(nexthop->flags,
3834 NEXTHOP_FLAG_ACTIVE)) {
3835 SET_FLAG(nexthop->flags,
3836 NEXTHOP_FLAG_FIB);
3837 }
3838 }
3839 }
3840 break;
3841
3842 case DPLANE_OP_NH_INSTALL:
3843 case DPLANE_OP_NH_UPDATE:
3844 case DPLANE_OP_NH_DELETE:
3845 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3846 atomic_fetch_add_explicit(
3847 &zdplane_info.dg_nexthop_errors, 1,
3848 memory_order_relaxed);
3849 break;
3850
3851 case DPLANE_OP_LSP_INSTALL:
3852 case DPLANE_OP_LSP_UPDATE:
3853 case DPLANE_OP_LSP_DELETE:
3854 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3855 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors,
3856 1, memory_order_relaxed);
3857 break;
3858
3859 case DPLANE_OP_PW_INSTALL:
3860 case DPLANE_OP_PW_UNINSTALL:
3861 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3862 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
3863 memory_order_relaxed);
3864 break;
3865
3866 case DPLANE_OP_ADDR_INSTALL:
3867 case DPLANE_OP_ADDR_UNINSTALL:
3868 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3869 atomic_fetch_add_explicit(
3870 &zdplane_info.dg_intf_addr_errors, 1,
3871 memory_order_relaxed);
3872 break;
3873
3874 case DPLANE_OP_MAC_INSTALL:
3875 case DPLANE_OP_MAC_DELETE:
3876 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3877 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors,
3878 1, memory_order_relaxed);
3879 break;
3880
3881 case DPLANE_OP_NEIGH_INSTALL:
3882 case DPLANE_OP_NEIGH_UPDATE:
3883 case DPLANE_OP_NEIGH_DELETE:
3884 case DPLANE_OP_VTEP_ADD:
3885 case DPLANE_OP_VTEP_DELETE:
3886 case DPLANE_OP_NEIGH_DISCOVER:
3887 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3888 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors,
3889 1, memory_order_relaxed);
3890 break;
3891
3892 case DPLANE_OP_RULE_ADD:
3893 case DPLANE_OP_RULE_DELETE:
3894 case DPLANE_OP_RULE_UPDATE:
3895 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3896 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors,
3897 1, memory_order_relaxed);
3898 break;
3899
3900 /* Ignore 'notifications' - no-op */
3901 case DPLANE_OP_SYS_ROUTE_ADD:
3902 case DPLANE_OP_SYS_ROUTE_DELETE:
3903 case DPLANE_OP_ROUTE_NOTIFY:
3904 case DPLANE_OP_LSP_NOTIFY:
3905 break;
3906
3907 case DPLANE_OP_NONE:
3908 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3909 atomic_fetch_add_explicit(&zdplane_info.dg_other_errors,
3910 1, memory_order_relaxed);
3911 break;
3912 }
3913 }
3914
3915 /*
3916 * Kernel provider callback
3917 */
3918 static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
3919 {
3920 struct zebra_dplane_ctx *ctx, *tctx;
3921 struct dplane_ctx_q work_list;
3922 int counter, limit;
3923
3924 TAILQ_INIT(&work_list);
3925
3926 limit = dplane_provider_get_work_limit(prov);
3927
3928 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3929 zlog_debug("dplane provider '%s': processing",
3930 dplane_provider_get_name(prov));
3931
3932 for (counter = 0; counter < limit; counter++) {
3933 ctx = dplane_provider_dequeue_in_ctx(prov);
3934 if (ctx == NULL)
3935 break;
3936
3937 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3938 kernel_dplane_log_detail(ctx);
3939
3940 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
3941 }
3942
3943 kernel_update_multi(&work_list);
3944
3945 TAILQ_FOREACH_SAFE (ctx, &work_list, zd_q_entries, tctx) {
3946 kernel_dplane_handle_result(ctx);
3947
3948 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
3949 dplane_provider_enqueue_out_ctx(prov, ctx);
3950 }
3951
3952 /* Ensure that we'll run the work loop again if there's still
3953 * more work to do.
3954 */
3955 if (counter >= limit) {
3956 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3957 zlog_debug("dplane provider '%s' reached max updates %d",
3958 dplane_provider_get_name(prov), counter);
3959
3960 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
3961 1, memory_order_relaxed);
3962
3963 dplane_provider_work_ready();
3964 }
3965
3966 return 0;
3967 }
3968
3969 #ifdef DPLANE_TEST_PROVIDER
3970
3971 /*
3972 * Test dataplane provider plugin
3973 */
3974
3975 /*
3976 * Test provider process callback
3977 */
3978 static int test_dplane_process_func(struct zebra_dplane_provider *prov)
3979 {
3980 struct zebra_dplane_ctx *ctx;
3981 int counter, limit;
3982
3983 /* Just moving from 'in' queue to 'out' queue */
3984
3985 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3986 zlog_debug("dplane provider '%s': processing",
3987 dplane_provider_get_name(prov));
3988
3989 limit = dplane_provider_get_work_limit(prov);
3990
3991 for (counter = 0; counter < limit; counter++) {
3992 ctx = dplane_provider_dequeue_in_ctx(prov);
3993 if (ctx == NULL)
3994 break;
3995
3996 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3997 zlog_debug("dplane provider '%s': op %s",
3998 dplane_provider_get_name(prov),
3999 dplane_op2str(dplane_ctx_get_op(ctx)));
4000
4001 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
4002
4003 dplane_provider_enqueue_out_ctx(prov, ctx);
4004 }
4005
4006 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4007 zlog_debug("dplane provider '%s': processed %d",
4008 dplane_provider_get_name(prov), counter);
4009
4010 /* Ensure that we'll run the work loop again if there's still
4011 * more work to do.
4012 */
4013 if (counter >= limit)
4014 dplane_provider_work_ready();
4015
4016 return 0;
4017 }
4018
4019 /*
4020 * Test provider shutdown/fini callback
4021 */
4022 static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
4023 bool early)
4024 {
4025 if (IS_ZEBRA_DEBUG_DPLANE)
4026 zlog_debug("dplane provider '%s': %sshutdown",
4027 dplane_provider_get_name(prov),
4028 early ? "early " : "");
4029
4030 return 0;
4031 }
4032 #endif /* DPLANE_TEST_PROVIDER */
4033
4034 /*
4035 * Register default kernel provider
4036 */
4037 static void dplane_provider_init(void)
4038 {
4039 int ret;
4040
4041 ret = dplane_provider_register("Kernel",
4042 DPLANE_PRIO_KERNEL,
4043 DPLANE_PROV_FLAGS_DEFAULT, NULL,
4044 kernel_dplane_process_func,
4045 NULL,
4046 NULL, NULL);
4047
4048 if (ret != AOK)
4049 zlog_err("Unable to register kernel dplane provider: %d",
4050 ret);
4051
4052 #ifdef DPLANE_TEST_PROVIDER
4053 /* Optional test provider ... */
4054 ret = dplane_provider_register("Test",
4055 DPLANE_PRIO_PRE_KERNEL,
4056 DPLANE_PROV_FLAGS_DEFAULT, NULL,
4057 test_dplane_process_func,
4058 test_dplane_shutdown_func,
4059 NULL /* data */, NULL);
4060
4061 if (ret != AOK)
4062 zlog_err("Unable to register test dplane provider: %d",
4063 ret);
4064 #endif /* DPLANE_TEST_PROVIDER */
4065 }
4066
4067 /* Indicates zebra shutdown/exit is in progress. Some operations may be
4068 * simplified or skipped during shutdown processing.
4069 */
4070 bool dplane_is_in_shutdown(void)
4071 {
4072 return zdplane_info.dg_is_shutdown;
4073 }
4074
4075 /*
4076 * Early or pre-shutdown, de-init notification api. This runs pretty
4077 * early during zebra shutdown, as a signal to stop new work and prepare
4078 * for updates generated by shutdown/cleanup activity, as zebra tries to
4079 * remove everything it's responsible for.
4080 * NB: This runs in the main zebra pthread context.
4081 */
4082 void zebra_dplane_pre_finish(void)
4083 {
4084 struct zebra_dplane_provider *prov;
4085
4086 if (IS_ZEBRA_DEBUG_DPLANE)
4087 zlog_debug("Zebra dataplane pre-finish called");
4088
4089 zdplane_info.dg_is_shutdown = true;
4090
4091 /* Notify provider(s) of pending shutdown. */
4092 TAILQ_FOREACH(prov, &zdplane_info.dg_providers_q, dp_prov_link) {
4093 if (prov->dp_fini == NULL)
4094 continue;
4095
4096 prov->dp_fini(prov, true /* early */);
4097 }
4098 }
4099
4100 /*
4101 * Utility to determine whether work remains enqueued within the dplane;
4102 * used during system shutdown processing.
4103 */
4104 static bool dplane_work_pending(void)
4105 {
4106 bool ret = false;
4107 struct zebra_dplane_ctx *ctx;
4108 struct zebra_dplane_provider *prov;
4109
4110 /* TODO -- just checking incoming/pending work for now, must check
4111 * providers
4112 */
4113 DPLANE_LOCK();
4114 {
4115 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
4116 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4117 }
4118 DPLANE_UNLOCK();
4119
4120 if (ctx != NULL) {
4121 ret = true;
4122 goto done;
4123 }
4124
4125 while (prov) {
4126
4127 dplane_provider_lock(prov);
4128
4129 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
4130 if (ctx == NULL)
4131 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4132
4133 dplane_provider_unlock(prov);
4134
4135 if (ctx != NULL)
4136 break;
4137
4138 DPLANE_LOCK();
4139 prov = TAILQ_NEXT(prov, dp_prov_link);
4140 DPLANE_UNLOCK();
4141 }
4142
4143 if (ctx != NULL)
4144 ret = true;
4145
4146 done:
4147 return ret;
4148 }
4149
4150 /*
4151 * Shutdown-time intermediate callback, used to determine when all pending
4152 * in-flight updates are done. If there's still work to do, reschedules itself.
4153 * If all work is done, schedules an event to the main zebra thread for
4154 * final zebra shutdown.
4155 * This runs in the dplane pthread context.
4156 */
4157 static int dplane_check_shutdown_status(struct thread *event)
4158 {
4159 if (IS_ZEBRA_DEBUG_DPLANE)
4160 zlog_debug("Zebra dataplane shutdown status check called");
4161
4162 if (dplane_work_pending()) {
4163 /* Reschedule dplane check on a short timer */
4164 thread_add_timer_msec(zdplane_info.dg_master,
4165 dplane_check_shutdown_status,
4166 NULL, 100,
4167 &zdplane_info.dg_t_shutdown_check);
4168
4169 /* TODO - give up and stop waiting after a short time? */
4170
4171 } else {
4172 /* We appear to be done - schedule a final callback event
4173 * for the zebra main pthread.
4174 */
4175 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
4176 }
4177
4178 return 0;
4179 }
4180
4181 /*
4182 * Shutdown, de-init api. This runs pretty late during shutdown,
4183 * after zebra has tried to free/remove/uninstall all routes during shutdown.
4184 * At this point, dplane work may still remain to be done, so we can't just
4185 * blindly terminate. If there's still work to do, we'll periodically check
4186 * and when done, we'll enqueue a task to the zebra main thread for final
4187 * termination processing.
4188 *
4189 * NB: This runs in the main zebra thread context.
4190 */
4191 void zebra_dplane_finish(void)
4192 {
4193 if (IS_ZEBRA_DEBUG_DPLANE)
4194 zlog_debug("Zebra dataplane fini called");
4195
4196 thread_add_event(zdplane_info.dg_master,
4197 dplane_check_shutdown_status, NULL, 0,
4198 &zdplane_info.dg_t_shutdown_check);
4199 }
4200
4201 /*
4202 * Main dataplane pthread event loop. The thread takes new incoming work
4203 * and offers it to the first provider. It then iterates through the
4204 * providers, taking complete work from each one and offering it
4205 * to the next in order. At each step, a limited number of updates are
4206 * processed during a cycle in order to provide some fairness.
4207 *
4208 * This loop through the providers is only run once, so that the dataplane
4209 * pthread can look for other pending work - such as i/o work on behalf of
4210 * providers.
4211 */
4212 static int dplane_thread_loop(struct thread *event)
4213 {
4214 struct dplane_ctx_q work_list;
4215 struct dplane_ctx_q error_list;
4216 struct zebra_dplane_provider *prov;
4217 struct zebra_dplane_ctx *ctx, *tctx;
4218 int limit, counter, error_counter;
4219 uint64_t curr, high;
4220
4221 /* Capture work limit per cycle */
4222 limit = zdplane_info.dg_updates_per_cycle;
4223
4224 /* Init temporary lists used to move contexts among providers */
4225 TAILQ_INIT(&work_list);
4226 TAILQ_INIT(&error_list);
4227 error_counter = 0;
4228
4229 /* Check for zebra shutdown */
4230 if (!zdplane_info.dg_run)
4231 goto done;
4232
4233 /* Dequeue some incoming work from zebra (if any) onto the temporary
4234 * working list.
4235 */
4236 DPLANE_LOCK();
4237
4238 /* Locate initial registered provider */
4239 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4240
4241 /* Move new work from incoming list to temp list */
4242 for (counter = 0; counter < limit; counter++) {
4243 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
4244 if (ctx) {
4245 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
4246 zd_q_entries);
4247
4248 ctx->zd_provider = prov->dp_id;
4249
4250 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
4251 } else {
4252 break;
4253 }
4254 }
4255
4256 DPLANE_UNLOCK();
4257
4258 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
4259 memory_order_relaxed);
4260
4261 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4262 zlog_debug("dplane: incoming new work counter: %d", counter);
4263
4264 /* Iterate through the registered providers, offering new incoming
4265 * work. If the provider has outgoing work in its queue, take that
4266 * work for the next provider
4267 */
4268 while (prov) {
4269
4270 /* At each iteration, the temporary work list has 'counter'
4271 * items.
4272 */
4273 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4274 zlog_debug("dplane enqueues %d new work to provider '%s'",
4275 counter, dplane_provider_get_name(prov));
4276
4277 /* Capture current provider id in each context; check for
4278 * error status.
4279 */
4280 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
4281 if (dplane_ctx_get_status(ctx) ==
4282 ZEBRA_DPLANE_REQUEST_SUCCESS) {
4283 ctx->zd_provider = prov->dp_id;
4284 } else {
4285 /*
4286 * TODO -- improve error-handling: recirc
4287 * errors backwards so that providers can
4288 * 'undo' their work (if they want to)
4289 */
4290
4291 /* Move to error list; will be returned
4292 * zebra main.
4293 */
4294 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
4295 TAILQ_INSERT_TAIL(&error_list,
4296 ctx, zd_q_entries);
4297 error_counter++;
4298 }
4299 }
4300
4301 /* Enqueue new work to the provider */
4302 dplane_provider_lock(prov);
4303
4304 if (TAILQ_FIRST(&work_list))
4305 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
4306 zd_q_entries);
4307
4308 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
4309 memory_order_relaxed);
4310 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
4311 memory_order_relaxed);
4312 curr = atomic_load_explicit(&prov->dp_in_queued,
4313 memory_order_relaxed);
4314 high = atomic_load_explicit(&prov->dp_in_max,
4315 memory_order_relaxed);
4316 if (curr > high)
4317 atomic_store_explicit(&prov->dp_in_max, curr,
4318 memory_order_relaxed);
4319
4320 dplane_provider_unlock(prov);
4321
4322 /* Reset the temp list (though the 'concat' may have done this
4323 * already), and the counter
4324 */
4325 TAILQ_INIT(&work_list);
4326 counter = 0;
4327
4328 /* Call into the provider code. Note that this is
4329 * unconditional: we offer to do work even if we don't enqueue
4330 * any _new_ work.
4331 */
4332 (*prov->dp_fp)(prov);
4333
4334 /* Check for zebra shutdown */
4335 if (!zdplane_info.dg_run)
4336 break;
4337
4338 /* Dequeue completed work from the provider */
4339 dplane_provider_lock(prov);
4340
4341 while (counter < limit) {
4342 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4343 if (ctx) {
4344 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
4345 zd_q_entries);
4346
4347 TAILQ_INSERT_TAIL(&work_list,
4348 ctx, zd_q_entries);
4349 counter++;
4350 } else
4351 break;
4352 }
4353
4354 dplane_provider_unlock(prov);
4355
4356 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4357 zlog_debug("dplane dequeues %d completed work from provider %s",
4358 counter, dplane_provider_get_name(prov));
4359
4360 /* Locate next provider */
4361 DPLANE_LOCK();
4362 prov = TAILQ_NEXT(prov, dp_prov_link);
4363 DPLANE_UNLOCK();
4364 }
4365
4366 /* After all providers have been serviced, enqueue any completed
4367 * work and any errors back to zebra so it can process the results.
4368 */
4369 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4370 zlog_debug("dplane has %d completed, %d errors, for zebra main",
4371 counter, error_counter);
4372
4373 /*
4374 * Hand lists through the api to zebra main,
4375 * to reduce the number of lock/unlock cycles
4376 */
4377
4378 /* Call through to zebra main */
4379 (zdplane_info.dg_results_cb)(&error_list);
4380
4381 TAILQ_INIT(&error_list);
4382
4383 /* Call through to zebra main */
4384 (zdplane_info.dg_results_cb)(&work_list);
4385
4386 TAILQ_INIT(&work_list);
4387
4388 done:
4389 return 0;
4390 }
4391
4392 /*
4393 * Final phase of shutdown, after all work enqueued to dplane has been
4394 * processed. This is called from the zebra main pthread context.
4395 */
4396 void zebra_dplane_shutdown(void)
4397 {
4398 struct zebra_dplane_provider *dp;
4399
4400 if (IS_ZEBRA_DEBUG_DPLANE)
4401 zlog_debug("Zebra dataplane shutdown called");
4402
4403 /* Stop dplane thread, if it's running */
4404
4405 zdplane_info.dg_run = false;
4406
4407 if (zdplane_info.dg_t_update)
4408 thread_cancel_async(zdplane_info.dg_t_update->master,
4409 &zdplane_info.dg_t_update, NULL);
4410
4411 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
4412
4413 /* Destroy pthread */
4414 frr_pthread_destroy(zdplane_info.dg_pthread);
4415 zdplane_info.dg_pthread = NULL;
4416 zdplane_info.dg_master = NULL;
4417
4418 /* Notify provider(s) of final shutdown.
4419 * Note that this call is in the main pthread, so providers must
4420 * be prepared for that.
4421 */
4422 TAILQ_FOREACH(dp, &zdplane_info.dg_providers_q, dp_prov_link) {
4423 if (dp->dp_fini == NULL)
4424 continue;
4425
4426 dp->dp_fini(dp, false);
4427 }
4428
4429 /* TODO -- Clean-up provider objects */
4430
4431 /* TODO -- Clean queue(s), free memory */
4432 }
4433
4434 /*
4435 * Initialize the dataplane module during startup, internal/private version
4436 */
4437 static void zebra_dplane_init_internal(void)
4438 {
4439 memset(&zdplane_info, 0, sizeof(zdplane_info));
4440
4441 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
4442
4443 TAILQ_INIT(&zdplane_info.dg_update_ctx_q);
4444 TAILQ_INIT(&zdplane_info.dg_providers_q);
4445
4446 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
4447
4448 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
4449
4450 /* Register default kernel 'provider' during init */
4451 dplane_provider_init();
4452 }
4453
4454 /*
4455 * Start the dataplane pthread. This step needs to be run later than the
4456 * 'init' step, in case zebra has fork-ed.
4457 */
4458 void zebra_dplane_start(void)
4459 {
4460 struct zebra_dplane_provider *prov;
4461 struct frr_pthread_attr pattr = {
4462 .start = frr_pthread_attr_default.start,
4463 .stop = frr_pthread_attr_default.stop
4464 };
4465
4466 /* Start dataplane pthread */
4467
4468 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
4469 "zebra_dplane");
4470
4471 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
4472
4473 zdplane_info.dg_run = true;
4474
4475 /* Enqueue an initial event for the dataplane pthread */
4476 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
4477 &zdplane_info.dg_t_update);
4478
4479 /* Call start callbacks for registered providers */
4480
4481 DPLANE_LOCK();
4482 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4483 DPLANE_UNLOCK();
4484
4485 while (prov) {
4486
4487 if (prov->dp_start)
4488 (prov->dp_start)(prov);
4489
4490 /* Locate next provider */
4491 DPLANE_LOCK();
4492 prov = TAILQ_NEXT(prov, dp_prov_link);
4493 DPLANE_UNLOCK();
4494 }
4495
4496 frr_pthread_run(zdplane_info.dg_pthread, NULL);
4497 }
4498
4499 /*
4500 * Initialize the dataplane module at startup; called by zebra rib_init()
4501 */
4502 void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
4503 {
4504 zebra_dplane_init_internal();
4505 zdplane_info.dg_results_cb = results_fp;
4506 }