]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_dplane.c
zebra: Make netlink buffer reads resizeable when needed
[mirror_frr.git] / zebra / zebra_dplane.c
1 /*
2 * Zebra dataplane layer.
3 * Copyright (c) 2018 Volta Networks, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "lib/libfrr.h"
25 #include "lib/debug.h"
26 #include "lib/frratomic.h"
27 #include "lib/frr_pthread.h"
28 #include "lib/memory.h"
29 #include "lib/queue.h"
30 #include "lib/zebra.h"
31 #include "zebra/zebra_router.h"
32 #include "zebra/zebra_dplane.h"
33 #include "zebra/zebra_vxlan_private.h"
34 #include "zebra/zebra_mpls.h"
35 #include "zebra/rt.h"
36 #include "zebra/debug.h"
37 #include "zebra/zebra_pbr.h"
38 #include "printfrr.h"
39
40 /* Memory types */
41 DEFINE_MTYPE_STATIC(ZEBRA, DP_CTX, "Zebra DPlane Ctx");
42 DEFINE_MTYPE_STATIC(ZEBRA, DP_INTF, "Zebra DPlane Intf");
43 DEFINE_MTYPE_STATIC(ZEBRA, DP_PROV, "Zebra DPlane Provider");
44 DEFINE_MTYPE_STATIC(ZEBRA, DP_NETFILTER, "Zebra Netfilter Internal Object");
45 DEFINE_MTYPE_STATIC(ZEBRA, DP_NS, "DPlane NSes");
46
47 #ifndef AOK
48 # define AOK 0
49 #endif
50
51 /* Control for collection of extra interface info with route updates; a plugin
52 * can enable the extra info via a dplane api.
53 */
54 static bool dplane_collect_extra_intf_info;
55
56 /* Enable test dataplane provider */
57 /*#define DPLANE_TEST_PROVIDER 1 */
58
59 /* Default value for max queued incoming updates */
60 const uint32_t DPLANE_DEFAULT_MAX_QUEUED = 200;
61
62 /* Default value for new work per cycle */
63 const uint32_t DPLANE_DEFAULT_NEW_WORK = 100;
64
65 /* Validation check macro for context blocks */
66 /* #define DPLANE_DEBUG 1 */
67
68 #ifdef DPLANE_DEBUG
69
70 # define DPLANE_CTX_VALID(p) \
71 assert((p) != NULL)
72
73 #else
74
75 # define DPLANE_CTX_VALID(p)
76
77 #endif /* DPLANE_DEBUG */
78
79 /*
80 * Nexthop information captured for nexthop/nexthop group updates
81 */
82 struct dplane_nexthop_info {
83 uint32_t id;
84 uint32_t old_id;
85 afi_t afi;
86 vrf_id_t vrf_id;
87 int type;
88
89 struct nexthop_group ng;
90 struct nh_grp nh_grp[MULTIPATH_NUM];
91 uint8_t nh_grp_count;
92 };
93
94 /*
95 * Optional extra info about interfaces used in route updates' nexthops.
96 */
97 struct dplane_intf_extra {
98 vrf_id_t vrf_id;
99 uint32_t ifindex;
100 uint32_t flags;
101 uint32_t status;
102
103 TAILQ_ENTRY(dplane_intf_extra) link;
104 };
105
106 /*
107 * Route information captured for route updates.
108 */
109 struct dplane_route_info {
110
111 /* Dest and (optional) source prefixes */
112 struct prefix zd_dest;
113 struct prefix zd_src;
114
115 afi_t zd_afi;
116 safi_t zd_safi;
117
118 int zd_type;
119 int zd_old_type;
120
121 route_tag_t zd_tag;
122 route_tag_t zd_old_tag;
123 uint32_t zd_metric;
124 uint32_t zd_old_metric;
125
126 uint16_t zd_instance;
127 uint16_t zd_old_instance;
128
129 uint8_t zd_distance;
130 uint8_t zd_old_distance;
131
132 uint32_t zd_mtu;
133 uint32_t zd_nexthop_mtu;
134
135 /* Nexthop hash entry info */
136 struct dplane_nexthop_info nhe;
137
138 /* Nexthops */
139 uint32_t zd_nhg_id;
140 struct nexthop_group zd_ng;
141
142 /* Backup nexthops (if present) */
143 struct nexthop_group backup_ng;
144
145 /* "Previous" nexthops, used only in route updates without netlink */
146 struct nexthop_group zd_old_ng;
147 struct nexthop_group old_backup_ng;
148
149 /* Optional list of extra interface info */
150 TAILQ_HEAD(dp_intf_extra_q, dplane_intf_extra) intf_extra_q;
151 };
152
153 /*
154 * Pseudowire info for the dataplane
155 */
156 struct dplane_pw_info {
157 int type;
158 int af;
159 int status;
160 uint32_t flags;
161 uint32_t nhg_id;
162 union g_addr dest;
163 mpls_label_t local_label;
164 mpls_label_t remote_label;
165
166 /* Nexthops that are valid and installed */
167 struct nexthop_group fib_nhg;
168
169 /* Primary and backup nexthop sets, copied from the resolving route. */
170 struct nexthop_group primary_nhg;
171 struct nexthop_group backup_nhg;
172
173 union pw_protocol_fields fields;
174 };
175
176 /*
177 * Bridge port info for the dataplane
178 */
179 struct dplane_br_port_info {
180 uint32_t sph_filter_cnt;
181 struct in_addr sph_filters[ES_VTEP_MAX_CNT];
182 /* DPLANE_BR_PORT_XXX - see zebra_dplane.h*/
183 uint32_t flags;
184 uint32_t backup_nhg_id;
185 };
186
187 /*
188 * Interface/prefix info for the dataplane
189 */
190 struct dplane_intf_info {
191
192 uint32_t metric;
193 uint32_t flags;
194
195 #define DPLANE_INTF_CONNECTED (1 << 0) /* Connected peer, p2p */
196 #define DPLANE_INTF_SECONDARY (1 << 1)
197 #define DPLANE_INTF_BROADCAST (1 << 2)
198 #define DPLANE_INTF_HAS_DEST DPLANE_INTF_CONNECTED
199 #define DPLANE_INTF_HAS_LABEL (1 << 4)
200
201 /* Interface address/prefix */
202 struct prefix prefix;
203
204 /* Dest address, for p2p, or broadcast prefix */
205 struct prefix dest_prefix;
206
207 char *label;
208 char label_buf[32];
209 };
210
211 /*
212 * EVPN MAC address info for the dataplane.
213 */
214 struct dplane_mac_info {
215 vlanid_t vid;
216 ifindex_t br_ifindex;
217 struct ethaddr mac;
218 struct in_addr vtep_ip;
219 bool is_sticky;
220 uint32_t nhg_id;
221 uint32_t update_flags;
222 };
223
224 /*
225 * Neighbor info for the dataplane
226 */
227 struct dplane_neigh_info {
228 struct ipaddr ip_addr;
229 union {
230 struct ethaddr mac;
231 struct ipaddr ip_addr;
232 } link;
233 uint32_t flags;
234 uint16_t state;
235 uint32_t update_flags;
236 };
237
238 /*
239 * Neighbor Table
240 */
241 struct dplane_neigh_table {
242 uint8_t family;
243 uint32_t app_probes;
244 uint32_t ucast_probes;
245 uint32_t mcast_probes;
246 };
247
248 /*
249 * Policy based routing rule info for the dataplane
250 */
251 struct dplane_ctx_rule {
252 uint32_t priority;
253
254 /* The route table pointed by this rule */
255 uint32_t table;
256
257 /* Filter criteria */
258 uint32_t filter_bm;
259 uint32_t fwmark;
260 uint8_t dsfield;
261 struct prefix src_ip;
262 struct prefix dst_ip;
263 uint8_t ip_proto;
264
265 uint8_t action_pcp;
266 uint16_t action_vlan_id;
267 uint16_t action_vlan_flags;
268
269 uint32_t action_queue_id;
270
271 char ifname[INTERFACE_NAMSIZ + 1];
272 };
273
274 struct dplane_rule_info {
275 /*
276 * Originating zclient sock fd, so we can know who to send
277 * back to.
278 */
279 int sock;
280
281 int unique;
282 int seq;
283
284 struct dplane_ctx_rule new;
285 struct dplane_ctx_rule old;
286 };
287
288 struct dplane_gre_ctx {
289 uint32_t link_ifindex;
290 unsigned int mtu;
291 struct zebra_l2info_gre info;
292 };
293 /*
294 * The context block used to exchange info about route updates across
295 * the boundary between the zebra main context (and pthread) and the
296 * dataplane layer (and pthread).
297 */
298 struct zebra_dplane_ctx {
299
300 /* Operation code */
301 enum dplane_op_e zd_op;
302
303 /* Status on return */
304 enum zebra_dplane_result zd_status;
305
306 /* Dplane provider id */
307 uint32_t zd_provider;
308
309 /* Flags - used by providers, e.g. */
310 int zd_flags;
311
312 bool zd_is_update;
313
314 uint32_t zd_seq;
315 uint32_t zd_old_seq;
316
317 /* Some updates may be generated by notifications: allow the
318 * plugin to notice and ignore results from its own notifications.
319 */
320 uint32_t zd_notif_provider;
321
322 /* TODO -- internal/sub-operation status? */
323 enum zebra_dplane_result zd_remote_status;
324 enum zebra_dplane_result zd_kernel_status;
325
326 vrf_id_t zd_vrf_id;
327 uint32_t zd_table_id;
328
329 char zd_ifname[INTERFACE_NAMSIZ];
330 ifindex_t zd_ifindex;
331
332 /* Support info for different kinds of updates */
333 union {
334 struct dplane_route_info rinfo;
335 struct zebra_lsp lsp;
336 struct dplane_pw_info pw;
337 struct dplane_br_port_info br_port;
338 struct dplane_intf_info intf;
339 struct dplane_mac_info macinfo;
340 struct dplane_neigh_info neigh;
341 struct dplane_rule_info rule;
342 struct zebra_pbr_iptable iptable;
343 struct zebra_pbr_ipset ipset;
344 struct {
345 struct zebra_pbr_ipset_entry entry;
346 struct zebra_pbr_ipset_info info;
347 } ipset_entry;
348 struct dplane_neigh_table neightable;
349 struct dplane_gre_ctx gre;
350 } u;
351
352 /* Namespace info, used especially for netlink kernel communication */
353 struct zebra_dplane_info zd_ns_info;
354
355 /* Embedded list linkage */
356 TAILQ_ENTRY(zebra_dplane_ctx) zd_q_entries;
357 };
358
359 /* Flag that can be set by a pre-kernel provider as a signal that an update
360 * should bypass the kernel.
361 */
362 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
363
364
365 /*
366 * Registration block for one dataplane provider.
367 */
368 struct zebra_dplane_provider {
369 /* Name */
370 char dp_name[DPLANE_PROVIDER_NAMELEN + 1];
371
372 /* Priority, for ordering among providers */
373 uint8_t dp_priority;
374
375 /* Id value */
376 uint32_t dp_id;
377
378 /* Mutex */
379 pthread_mutex_t dp_mutex;
380
381 /* Plugin-provided extra data */
382 void *dp_data;
383
384 /* Flags */
385 int dp_flags;
386
387 int (*dp_start)(struct zebra_dplane_provider *prov);
388
389 int (*dp_fp)(struct zebra_dplane_provider *prov);
390
391 int (*dp_fini)(struct zebra_dplane_provider *prov, bool early_p);
392
393 _Atomic uint32_t dp_in_counter;
394 _Atomic uint32_t dp_in_queued;
395 _Atomic uint32_t dp_in_max;
396 _Atomic uint32_t dp_out_counter;
397 _Atomic uint32_t dp_out_queued;
398 _Atomic uint32_t dp_out_max;
399 _Atomic uint32_t dp_error_counter;
400
401 /* Queue of contexts inbound to the provider */
402 struct dplane_ctx_q dp_ctx_in_q;
403
404 /* Queue of completed contexts outbound from the provider back
405 * towards the dataplane module.
406 */
407 struct dplane_ctx_q dp_ctx_out_q;
408
409 /* Embedded list linkage for provider objects */
410 TAILQ_ENTRY(zebra_dplane_provider) dp_prov_link;
411 };
412
413 /* Declare types for list of zns info objects */
414 PREDECL_DLIST(zns_info_list);
415
416 struct dplane_zns_info {
417 struct zebra_dplane_info info;
418
419 /* Read event */
420 struct thread *t_read;
421
422 /* List linkage */
423 struct zns_info_list_item link;
424 };
425
426 /*
427 * Globals
428 */
429 static struct zebra_dplane_globals {
430 /* Mutex to control access to dataplane components */
431 pthread_mutex_t dg_mutex;
432
433 /* Results callback registered by zebra 'core' */
434 int (*dg_results_cb)(struct dplane_ctx_q *ctxlist);
435
436 /* Sentinel for beginning of shutdown */
437 volatile bool dg_is_shutdown;
438
439 /* Sentinel for end of shutdown */
440 volatile bool dg_run;
441
442 /* Update context queue inbound to the dataplane */
443 TAILQ_HEAD(zdg_ctx_q, zebra_dplane_ctx) dg_update_ctx_q;
444
445 /* Ordered list of providers */
446 TAILQ_HEAD(zdg_prov_q, zebra_dplane_provider) dg_providers_q;
447
448 /* List of info about each zns */
449 struct zns_info_list_head dg_zns_list;
450
451 /* Counter used to assign internal ids to providers */
452 uint32_t dg_provider_id;
453
454 /* Limit number of pending, unprocessed updates */
455 _Atomic uint32_t dg_max_queued_updates;
456
457 /* Control whether system route notifications should be produced. */
458 bool dg_sys_route_notifs;
459
460 /* Limit number of new updates dequeued at once, to pace an
461 * incoming burst.
462 */
463 uint32_t dg_updates_per_cycle;
464
465 _Atomic uint32_t dg_routes_in;
466 _Atomic uint32_t dg_routes_queued;
467 _Atomic uint32_t dg_routes_queued_max;
468 _Atomic uint32_t dg_route_errors;
469 _Atomic uint32_t dg_other_errors;
470
471 _Atomic uint32_t dg_nexthops_in;
472 _Atomic uint32_t dg_nexthop_errors;
473
474 _Atomic uint32_t dg_lsps_in;
475 _Atomic uint32_t dg_lsp_errors;
476
477 _Atomic uint32_t dg_pws_in;
478 _Atomic uint32_t dg_pw_errors;
479
480 _Atomic uint32_t dg_br_port_in;
481 _Atomic uint32_t dg_br_port_errors;
482
483 _Atomic uint32_t dg_intf_addrs_in;
484 _Atomic uint32_t dg_intf_addr_errors;
485
486 _Atomic uint32_t dg_macs_in;
487 _Atomic uint32_t dg_mac_errors;
488
489 _Atomic uint32_t dg_neighs_in;
490 _Atomic uint32_t dg_neigh_errors;
491
492 _Atomic uint32_t dg_rules_in;
493 _Atomic uint32_t dg_rule_errors;
494
495 _Atomic uint32_t dg_update_yields;
496
497 _Atomic uint32_t dg_iptable_in;
498 _Atomic uint32_t dg_iptable_errors;
499
500 _Atomic uint32_t dg_ipset_in;
501 _Atomic uint32_t dg_ipset_errors;
502 _Atomic uint32_t dg_ipset_entry_in;
503 _Atomic uint32_t dg_ipset_entry_errors;
504
505 _Atomic uint32_t dg_neightable_in;
506 _Atomic uint32_t dg_neightable_errors;
507
508 _Atomic uint32_t dg_gre_set_in;
509 _Atomic uint32_t dg_gre_set_errors;
510
511 /* Dataplane pthread */
512 struct frr_pthread *dg_pthread;
513
514 /* Event-delivery context 'master' for the dplane */
515 struct thread_master *dg_master;
516
517 /* Event/'thread' pointer for queued updates */
518 struct thread *dg_t_update;
519
520 /* Event pointer for pending shutdown check loop */
521 struct thread *dg_t_shutdown_check;
522
523 } zdplane_info;
524
525 /* Instantiate zns list type */
526 DECLARE_DLIST(zns_info_list, struct dplane_zns_info, link);
527
528 /*
529 * Lock and unlock for interactions with the zebra 'core' pthread
530 */
531 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
532 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
533
534
535 /*
536 * Lock and unlock for individual providers
537 */
538 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
539 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
540
541 /* Prototypes */
542 static int dplane_thread_loop(struct thread *event);
543 static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
544 struct zebra_ns *zns);
545 static enum zebra_dplane_result lsp_update_internal(struct zebra_lsp *lsp,
546 enum dplane_op_e op);
547 static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
548 enum dplane_op_e op);
549 static enum zebra_dplane_result intf_addr_update_internal(
550 const struct interface *ifp, const struct connected *ifc,
551 enum dplane_op_e op);
552 static enum zebra_dplane_result mac_update_common(
553 enum dplane_op_e op, const struct interface *ifp,
554 const struct interface *br_ifp,
555 vlanid_t vid, const struct ethaddr *mac,
556 struct in_addr vtep_ip, bool sticky, uint32_t nhg_id,
557 uint32_t update_flags);
558 static enum zebra_dplane_result
559 neigh_update_internal(enum dplane_op_e op, const struct interface *ifp,
560 const void *link, int link_family,
561 const struct ipaddr *ip, uint32_t flags, uint16_t state,
562 uint32_t update_flags, int protocol);
563
564 /*
565 * Public APIs
566 */
567
568 /* Obtain thread_master for dataplane thread */
569 struct thread_master *dplane_get_thread_master(void)
570 {
571 return zdplane_info.dg_master;
572 }
573
574 /*
575 * Allocate a dataplane update context
576 */
577 struct zebra_dplane_ctx *dplane_ctx_alloc(void)
578 {
579 struct zebra_dplane_ctx *p;
580
581 /* TODO -- just alloc'ing memory, but would like to maintain
582 * a pool
583 */
584 p = XCALLOC(MTYPE_DP_CTX, sizeof(struct zebra_dplane_ctx));
585
586 return p;
587 }
588
589 /* Enable system route notifications */
590 void dplane_enable_sys_route_notifs(void)
591 {
592 zdplane_info.dg_sys_route_notifs = true;
593 }
594
595 /*
596 * Clean up dependent/internal allocations inside a context object
597 */
598 static void dplane_ctx_free_internal(struct zebra_dplane_ctx *ctx)
599 {
600 struct dplane_intf_extra *if_extra, *if_tmp;
601
602 /*
603 * Some internal allocations may need to be freed, depending on
604 * the type of info captured in the ctx.
605 */
606 switch (ctx->zd_op) {
607 case DPLANE_OP_ROUTE_INSTALL:
608 case DPLANE_OP_ROUTE_UPDATE:
609 case DPLANE_OP_ROUTE_DELETE:
610 case DPLANE_OP_SYS_ROUTE_ADD:
611 case DPLANE_OP_SYS_ROUTE_DELETE:
612 case DPLANE_OP_ROUTE_NOTIFY:
613
614 /* Free allocated nexthops */
615 if (ctx->u.rinfo.zd_ng.nexthop) {
616 /* This deals with recursive nexthops too */
617 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
618
619 ctx->u.rinfo.zd_ng.nexthop = NULL;
620 }
621
622 /* Free backup info also (if present) */
623 if (ctx->u.rinfo.backup_ng.nexthop) {
624 /* This deals with recursive nexthops too */
625 nexthops_free(ctx->u.rinfo.backup_ng.nexthop);
626
627 ctx->u.rinfo.backup_ng.nexthop = NULL;
628 }
629
630 if (ctx->u.rinfo.zd_old_ng.nexthop) {
631 /* This deals with recursive nexthops too */
632 nexthops_free(ctx->u.rinfo.zd_old_ng.nexthop);
633
634 ctx->u.rinfo.zd_old_ng.nexthop = NULL;
635 }
636
637 if (ctx->u.rinfo.old_backup_ng.nexthop) {
638 /* This deals with recursive nexthops too */
639 nexthops_free(ctx->u.rinfo.old_backup_ng.nexthop);
640
641 ctx->u.rinfo.old_backup_ng.nexthop = NULL;
642 }
643
644 /* Optional extra interface info */
645 TAILQ_FOREACH_SAFE(if_extra, &ctx->u.rinfo.intf_extra_q,
646 link, if_tmp) {
647 TAILQ_REMOVE(&ctx->u.rinfo.intf_extra_q, if_extra,
648 link);
649 XFREE(MTYPE_DP_INTF, if_extra);
650 }
651
652 break;
653
654 case DPLANE_OP_NH_INSTALL:
655 case DPLANE_OP_NH_UPDATE:
656 case DPLANE_OP_NH_DELETE: {
657 if (ctx->u.rinfo.nhe.ng.nexthop) {
658 /* This deals with recursive nexthops too */
659 nexthops_free(ctx->u.rinfo.nhe.ng.nexthop);
660
661 ctx->u.rinfo.nhe.ng.nexthop = NULL;
662 }
663 break;
664 }
665
666 case DPLANE_OP_LSP_INSTALL:
667 case DPLANE_OP_LSP_UPDATE:
668 case DPLANE_OP_LSP_DELETE:
669 case DPLANE_OP_LSP_NOTIFY:
670 {
671 struct zebra_nhlfe *nhlfe;
672
673 /* Unlink and free allocated NHLFEs */
674 frr_each_safe(nhlfe_list, &ctx->u.lsp.nhlfe_list, nhlfe) {
675 nhlfe_list_del(&ctx->u.lsp.nhlfe_list, nhlfe);
676 zebra_mpls_nhlfe_free(nhlfe);
677 }
678
679 /* Unlink and free allocated backup NHLFEs, if present */
680 frr_each_safe(nhlfe_list,
681 &(ctx->u.lsp.backup_nhlfe_list), nhlfe) {
682 nhlfe_list_del(&ctx->u.lsp.backup_nhlfe_list,
683 nhlfe);
684 zebra_mpls_nhlfe_free(nhlfe);
685 }
686
687 /* Clear pointers in lsp struct, in case we're caching
688 * free context structs.
689 */
690 nhlfe_list_init(&ctx->u.lsp.nhlfe_list);
691 ctx->u.lsp.best_nhlfe = NULL;
692 nhlfe_list_init(&ctx->u.lsp.backup_nhlfe_list);
693
694 break;
695 }
696
697 case DPLANE_OP_PW_INSTALL:
698 case DPLANE_OP_PW_UNINSTALL:
699 /* Free allocated nexthops */
700 if (ctx->u.pw.fib_nhg.nexthop) {
701 /* This deals with recursive nexthops too */
702 nexthops_free(ctx->u.pw.fib_nhg.nexthop);
703
704 ctx->u.pw.fib_nhg.nexthop = NULL;
705 }
706 if (ctx->u.pw.primary_nhg.nexthop) {
707 nexthops_free(ctx->u.pw.primary_nhg.nexthop);
708
709 ctx->u.pw.primary_nhg.nexthop = NULL;
710 }
711 if (ctx->u.pw.backup_nhg.nexthop) {
712 nexthops_free(ctx->u.pw.backup_nhg.nexthop);
713
714 ctx->u.pw.backup_nhg.nexthop = NULL;
715 }
716 break;
717
718 case DPLANE_OP_ADDR_INSTALL:
719 case DPLANE_OP_ADDR_UNINSTALL:
720 case DPLANE_OP_INTF_ADDR_ADD:
721 case DPLANE_OP_INTF_ADDR_DEL:
722 /* Maybe free label string, if allocated */
723 if (ctx->u.intf.label != NULL &&
724 ctx->u.intf.label != ctx->u.intf.label_buf) {
725 free(ctx->u.intf.label);
726 ctx->u.intf.label = NULL;
727 }
728 break;
729
730 case DPLANE_OP_MAC_INSTALL:
731 case DPLANE_OP_MAC_DELETE:
732 case DPLANE_OP_NEIGH_INSTALL:
733 case DPLANE_OP_NEIGH_UPDATE:
734 case DPLANE_OP_NEIGH_DELETE:
735 case DPLANE_OP_VTEP_ADD:
736 case DPLANE_OP_VTEP_DELETE:
737 case DPLANE_OP_RULE_ADD:
738 case DPLANE_OP_RULE_DELETE:
739 case DPLANE_OP_RULE_UPDATE:
740 case DPLANE_OP_NEIGH_DISCOVER:
741 case DPLANE_OP_BR_PORT_UPDATE:
742 case DPLANE_OP_NEIGH_IP_INSTALL:
743 case DPLANE_OP_NEIGH_IP_DELETE:
744 case DPLANE_OP_NONE:
745 case DPLANE_OP_IPSET_ADD:
746 case DPLANE_OP_IPSET_DELETE:
747 break;
748
749 case DPLANE_OP_IPSET_ENTRY_ADD:
750 case DPLANE_OP_IPSET_ENTRY_DELETE:
751 break;
752 case DPLANE_OP_NEIGH_TABLE_UPDATE:
753 break;
754 case DPLANE_OP_IPTABLE_ADD:
755 case DPLANE_OP_IPTABLE_DELETE:
756 if (ctx->u.iptable.interface_name_list) {
757 struct listnode *node, *nnode;
758 char *ifname;
759
760 for (ALL_LIST_ELEMENTS(
761 ctx->u.iptable.interface_name_list, node,
762 nnode, ifname)) {
763 LISTNODE_DETACH(
764 ctx->u.iptable.interface_name_list,
765 node);
766 XFREE(MTYPE_DP_NETFILTER, ifname);
767 }
768 list_delete(&ctx->u.iptable.interface_name_list);
769 }
770 break;
771 case DPLANE_OP_GRE_SET:
772 break;
773 }
774 }
775
776 /*
777 * Free a dataplane results context.
778 */
779 static void dplane_ctx_free(struct zebra_dplane_ctx **pctx)
780 {
781 if (pctx == NULL)
782 return;
783
784 DPLANE_CTX_VALID(*pctx);
785
786 /* TODO -- just freeing memory, but would like to maintain
787 * a pool
788 */
789
790 /* Some internal allocations may need to be freed, depending on
791 * the type of info captured in the ctx.
792 */
793 dplane_ctx_free_internal(*pctx);
794
795 XFREE(MTYPE_DP_CTX, *pctx);
796 }
797
798 /*
799 * Reset an allocated context object for re-use. All internal allocations are
800 * freed and the context is memset.
801 */
802 void dplane_ctx_reset(struct zebra_dplane_ctx *ctx)
803 {
804 dplane_ctx_free_internal(ctx);
805 memset(ctx, 0, sizeof(*ctx));
806 }
807
808 /*
809 * Return a context block to the dplane module after processing
810 */
811 void dplane_ctx_fini(struct zebra_dplane_ctx **pctx)
812 {
813 /* TODO -- maintain pool; for now, just free */
814 dplane_ctx_free(pctx);
815 }
816
817 /* Enqueue a context block */
818 void dplane_ctx_enqueue_tail(struct dplane_ctx_q *q,
819 const struct zebra_dplane_ctx *ctx)
820 {
821 TAILQ_INSERT_TAIL(q, (struct zebra_dplane_ctx *)ctx, zd_q_entries);
822 }
823
824 /* Append a list of context blocks to another list */
825 void dplane_ctx_list_append(struct dplane_ctx_q *to_list,
826 struct dplane_ctx_q *from_list)
827 {
828 if (TAILQ_FIRST(from_list)) {
829 TAILQ_CONCAT(to_list, from_list, zd_q_entries);
830
831 /* And clear 'from' list */
832 TAILQ_INIT(from_list);
833 }
834 }
835
836 struct zebra_dplane_ctx *dplane_ctx_get_head(struct dplane_ctx_q *q)
837 {
838 struct zebra_dplane_ctx *ctx = TAILQ_FIRST(q);
839
840 return ctx;
841 }
842
843 /* Dequeue a context block from the head of a list */
844 struct zebra_dplane_ctx *dplane_ctx_dequeue(struct dplane_ctx_q *q)
845 {
846 struct zebra_dplane_ctx *ctx = TAILQ_FIRST(q);
847
848 if (ctx)
849 TAILQ_REMOVE(q, ctx, zd_q_entries);
850
851 return ctx;
852 }
853
854 /*
855 * Accessors for information from the context object
856 */
857 enum zebra_dplane_result dplane_ctx_get_status(
858 const struct zebra_dplane_ctx *ctx)
859 {
860 DPLANE_CTX_VALID(ctx);
861
862 return ctx->zd_status;
863 }
864
865 void dplane_ctx_set_status(struct zebra_dplane_ctx *ctx,
866 enum zebra_dplane_result status)
867 {
868 DPLANE_CTX_VALID(ctx);
869
870 ctx->zd_status = status;
871 }
872
873 /* Retrieve last/current provider id */
874 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx *ctx)
875 {
876 DPLANE_CTX_VALID(ctx);
877 return ctx->zd_provider;
878 }
879
880 /* Providers run before the kernel can control whether a kernel
881 * update should be done.
882 */
883 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx *ctx)
884 {
885 DPLANE_CTX_VALID(ctx);
886
887 SET_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
888 }
889
890 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx *ctx)
891 {
892 DPLANE_CTX_VALID(ctx);
893
894 return CHECK_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
895 }
896
897 void dplane_ctx_set_op(struct zebra_dplane_ctx *ctx, enum dplane_op_e op)
898 {
899 DPLANE_CTX_VALID(ctx);
900 ctx->zd_op = op;
901 }
902
903 enum dplane_op_e dplane_ctx_get_op(const struct zebra_dplane_ctx *ctx)
904 {
905 DPLANE_CTX_VALID(ctx);
906
907 return ctx->zd_op;
908 }
909
910 const char *dplane_op2str(enum dplane_op_e op)
911 {
912 const char *ret = "UNKNOWN";
913
914 switch (op) {
915 case DPLANE_OP_NONE:
916 ret = "NONE";
917 break;
918
919 /* Route update */
920 case DPLANE_OP_ROUTE_INSTALL:
921 ret = "ROUTE_INSTALL";
922 break;
923 case DPLANE_OP_ROUTE_UPDATE:
924 ret = "ROUTE_UPDATE";
925 break;
926 case DPLANE_OP_ROUTE_DELETE:
927 ret = "ROUTE_DELETE";
928 break;
929 case DPLANE_OP_ROUTE_NOTIFY:
930 ret = "ROUTE_NOTIFY";
931 break;
932
933 /* Nexthop update */
934 case DPLANE_OP_NH_INSTALL:
935 ret = "NH_INSTALL";
936 break;
937 case DPLANE_OP_NH_UPDATE:
938 ret = "NH_UPDATE";
939 break;
940 case DPLANE_OP_NH_DELETE:
941 ret = "NH_DELETE";
942 break;
943
944 case DPLANE_OP_LSP_INSTALL:
945 ret = "LSP_INSTALL";
946 break;
947 case DPLANE_OP_LSP_UPDATE:
948 ret = "LSP_UPDATE";
949 break;
950 case DPLANE_OP_LSP_DELETE:
951 ret = "LSP_DELETE";
952 break;
953 case DPLANE_OP_LSP_NOTIFY:
954 ret = "LSP_NOTIFY";
955 break;
956
957 case DPLANE_OP_PW_INSTALL:
958 ret = "PW_INSTALL";
959 break;
960 case DPLANE_OP_PW_UNINSTALL:
961 ret = "PW_UNINSTALL";
962 break;
963
964 case DPLANE_OP_SYS_ROUTE_ADD:
965 ret = "SYS_ROUTE_ADD";
966 break;
967 case DPLANE_OP_SYS_ROUTE_DELETE:
968 ret = "SYS_ROUTE_DEL";
969 break;
970
971 case DPLANE_OP_BR_PORT_UPDATE:
972 ret = "BR_PORT_UPDATE";
973 break;
974
975 case DPLANE_OP_ADDR_INSTALL:
976 ret = "ADDR_INSTALL";
977 break;
978 case DPLANE_OP_ADDR_UNINSTALL:
979 ret = "ADDR_UNINSTALL";
980 break;
981
982 case DPLANE_OP_MAC_INSTALL:
983 ret = "MAC_INSTALL";
984 break;
985 case DPLANE_OP_MAC_DELETE:
986 ret = "MAC_DELETE";
987 break;
988
989 case DPLANE_OP_NEIGH_INSTALL:
990 ret = "NEIGH_INSTALL";
991 break;
992 case DPLANE_OP_NEIGH_UPDATE:
993 ret = "NEIGH_UPDATE";
994 break;
995 case DPLANE_OP_NEIGH_DELETE:
996 ret = "NEIGH_DELETE";
997 break;
998 case DPLANE_OP_VTEP_ADD:
999 ret = "VTEP_ADD";
1000 break;
1001 case DPLANE_OP_VTEP_DELETE:
1002 ret = "VTEP_DELETE";
1003 break;
1004
1005 case DPLANE_OP_RULE_ADD:
1006 ret = "RULE_ADD";
1007 break;
1008 case DPLANE_OP_RULE_DELETE:
1009 ret = "RULE_DELETE";
1010 break;
1011 case DPLANE_OP_RULE_UPDATE:
1012 ret = "RULE_UPDATE";
1013 break;
1014
1015 case DPLANE_OP_NEIGH_DISCOVER:
1016 ret = "NEIGH_DISCOVER";
1017 break;
1018
1019 case DPLANE_OP_IPTABLE_ADD:
1020 ret = "IPTABLE_ADD";
1021 break;
1022 case DPLANE_OP_IPTABLE_DELETE:
1023 ret = "IPTABLE_DELETE";
1024 break;
1025 case DPLANE_OP_IPSET_ADD:
1026 ret = "IPSET_ADD";
1027 break;
1028 case DPLANE_OP_IPSET_DELETE:
1029 ret = "IPSET_DELETE";
1030 break;
1031 case DPLANE_OP_IPSET_ENTRY_ADD:
1032 ret = "IPSET_ENTRY_ADD";
1033 break;
1034 case DPLANE_OP_IPSET_ENTRY_DELETE:
1035 ret = "IPSET_ENTRY_DELETE";
1036 break;
1037 case DPLANE_OP_NEIGH_IP_INSTALL:
1038 ret = "NEIGH_IP_INSTALL";
1039 break;
1040 case DPLANE_OP_NEIGH_IP_DELETE:
1041 ret = "NEIGH_IP_DELETE";
1042 break;
1043 case DPLANE_OP_NEIGH_TABLE_UPDATE:
1044 ret = "NEIGH_TABLE_UPDATE";
1045 break;
1046
1047 case DPLANE_OP_GRE_SET:
1048 ret = "GRE_SET";
1049 break;
1050
1051 case DPLANE_OP_INTF_ADDR_ADD:
1052 return "INTF_ADDR_ADD";
1053
1054 case DPLANE_OP_INTF_ADDR_DEL:
1055 return "INTF_ADDR_DEL";
1056 }
1057
1058 return ret;
1059 }
1060
1061 const char *dplane_res2str(enum zebra_dplane_result res)
1062 {
1063 const char *ret = "<Unknown>";
1064
1065 switch (res) {
1066 case ZEBRA_DPLANE_REQUEST_FAILURE:
1067 ret = "FAILURE";
1068 break;
1069 case ZEBRA_DPLANE_REQUEST_QUEUED:
1070 ret = "QUEUED";
1071 break;
1072 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1073 ret = "SUCCESS";
1074 break;
1075 }
1076
1077 return ret;
1078 }
1079
1080 void dplane_ctx_set_dest(struct zebra_dplane_ctx *ctx,
1081 const struct prefix *dest)
1082 {
1083 DPLANE_CTX_VALID(ctx);
1084
1085 prefix_copy(&(ctx->u.rinfo.zd_dest), dest);
1086 }
1087
1088 const struct prefix *dplane_ctx_get_dest(const struct zebra_dplane_ctx *ctx)
1089 {
1090 DPLANE_CTX_VALID(ctx);
1091
1092 return &(ctx->u.rinfo.zd_dest);
1093 }
1094
1095 void dplane_ctx_set_src(struct zebra_dplane_ctx *ctx, const struct prefix *src)
1096 {
1097 DPLANE_CTX_VALID(ctx);
1098
1099 if (src)
1100 prefix_copy(&(ctx->u.rinfo.zd_src), src);
1101 else
1102 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(struct prefix));
1103 }
1104
1105 /* Source prefix is a little special - return NULL for "no src prefix" */
1106 const struct prefix *dplane_ctx_get_src(const struct zebra_dplane_ctx *ctx)
1107 {
1108 DPLANE_CTX_VALID(ctx);
1109
1110 if (ctx->u.rinfo.zd_src.prefixlen == 0 &&
1111 IN6_IS_ADDR_UNSPECIFIED(&(ctx->u.rinfo.zd_src.u.prefix6))) {
1112 return NULL;
1113 } else {
1114 return &(ctx->u.rinfo.zd_src);
1115 }
1116 }
1117
1118 bool dplane_ctx_is_update(const struct zebra_dplane_ctx *ctx)
1119 {
1120 DPLANE_CTX_VALID(ctx);
1121
1122 return ctx->zd_is_update;
1123 }
1124
1125 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx *ctx)
1126 {
1127 DPLANE_CTX_VALID(ctx);
1128
1129 return ctx->zd_seq;
1130 }
1131
1132 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx *ctx)
1133 {
1134 DPLANE_CTX_VALID(ctx);
1135
1136 return ctx->zd_old_seq;
1137 }
1138
1139 void dplane_ctx_set_vrf(struct zebra_dplane_ctx *ctx, vrf_id_t vrf)
1140 {
1141 DPLANE_CTX_VALID(ctx);
1142
1143 ctx->zd_vrf_id = vrf;
1144 }
1145
1146 vrf_id_t dplane_ctx_get_vrf(const struct zebra_dplane_ctx *ctx)
1147 {
1148 DPLANE_CTX_VALID(ctx);
1149
1150 return ctx->zd_vrf_id;
1151 }
1152
1153 /* In some paths we have only a namespace id */
1154 void dplane_ctx_set_ns_id(struct zebra_dplane_ctx *ctx, ns_id_t nsid)
1155 {
1156 DPLANE_CTX_VALID(ctx);
1157
1158 ctx->zd_ns_info.ns_id = nsid;
1159 }
1160
1161 ns_id_t dplane_ctx_get_ns_id(const struct zebra_dplane_ctx *ctx)
1162 {
1163 DPLANE_CTX_VALID(ctx);
1164
1165 return ctx->zd_ns_info.ns_id;
1166 }
1167
1168 bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx *ctx)
1169 {
1170 DPLANE_CTX_VALID(ctx);
1171
1172 return (ctx->zd_notif_provider != 0);
1173 }
1174
1175 uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx *ctx)
1176 {
1177 DPLANE_CTX_VALID(ctx);
1178
1179 return ctx->zd_notif_provider;
1180 }
1181
1182 void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx *ctx,
1183 uint32_t id)
1184 {
1185 DPLANE_CTX_VALID(ctx);
1186
1187 ctx->zd_notif_provider = id;
1188 }
1189
1190 const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx *ctx)
1191 {
1192 DPLANE_CTX_VALID(ctx);
1193
1194 return ctx->zd_ifname;
1195 }
1196
1197 void dplane_ctx_set_ifname(struct zebra_dplane_ctx *ctx, const char *ifname)
1198 {
1199 DPLANE_CTX_VALID(ctx);
1200
1201 if (!ifname)
1202 return;
1203
1204 strlcpy(ctx->zd_ifname, ifname, sizeof(ctx->zd_ifname));
1205 }
1206
1207 ifindex_t dplane_ctx_get_ifindex(const struct zebra_dplane_ctx *ctx)
1208 {
1209 DPLANE_CTX_VALID(ctx);
1210
1211 return ctx->zd_ifindex;
1212 }
1213
1214 void dplane_ctx_set_ifindex(struct zebra_dplane_ctx *ctx, ifindex_t ifindex)
1215 {
1216 DPLANE_CTX_VALID(ctx);
1217
1218 ctx->zd_ifindex = ifindex;
1219 }
1220
1221 void dplane_ctx_set_type(struct zebra_dplane_ctx *ctx, int type)
1222 {
1223 DPLANE_CTX_VALID(ctx);
1224
1225 ctx->u.rinfo.zd_type = type;
1226 }
1227
1228 int dplane_ctx_get_type(const struct zebra_dplane_ctx *ctx)
1229 {
1230 DPLANE_CTX_VALID(ctx);
1231
1232 return ctx->u.rinfo.zd_type;
1233 }
1234
1235 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx *ctx)
1236 {
1237 DPLANE_CTX_VALID(ctx);
1238
1239 return ctx->u.rinfo.zd_old_type;
1240 }
1241
1242 void dplane_ctx_set_afi(struct zebra_dplane_ctx *ctx, afi_t afi)
1243 {
1244 DPLANE_CTX_VALID(ctx);
1245
1246 ctx->u.rinfo.zd_afi = afi;
1247 }
1248
1249 afi_t dplane_ctx_get_afi(const struct zebra_dplane_ctx *ctx)
1250 {
1251 DPLANE_CTX_VALID(ctx);
1252
1253 return ctx->u.rinfo.zd_afi;
1254 }
1255
1256 void dplane_ctx_set_safi(struct zebra_dplane_ctx *ctx, safi_t safi)
1257 {
1258 DPLANE_CTX_VALID(ctx);
1259
1260 ctx->u.rinfo.zd_safi = safi;
1261 }
1262
1263 safi_t dplane_ctx_get_safi(const struct zebra_dplane_ctx *ctx)
1264 {
1265 DPLANE_CTX_VALID(ctx);
1266
1267 return ctx->u.rinfo.zd_safi;
1268 }
1269
1270 void dplane_ctx_set_table(struct zebra_dplane_ctx *ctx, uint32_t table)
1271 {
1272 DPLANE_CTX_VALID(ctx);
1273
1274 ctx->zd_table_id = table;
1275 }
1276
1277 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx *ctx)
1278 {
1279 DPLANE_CTX_VALID(ctx);
1280
1281 return ctx->zd_table_id;
1282 }
1283
1284 route_tag_t dplane_ctx_get_tag(const struct zebra_dplane_ctx *ctx)
1285 {
1286 DPLANE_CTX_VALID(ctx);
1287
1288 return ctx->u.rinfo.zd_tag;
1289 }
1290
1291 void dplane_ctx_set_tag(struct zebra_dplane_ctx *ctx, route_tag_t tag)
1292 {
1293 DPLANE_CTX_VALID(ctx);
1294
1295 ctx->u.rinfo.zd_tag = tag;
1296 }
1297
1298 route_tag_t dplane_ctx_get_old_tag(const struct zebra_dplane_ctx *ctx)
1299 {
1300 DPLANE_CTX_VALID(ctx);
1301
1302 return ctx->u.rinfo.zd_old_tag;
1303 }
1304
1305 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx *ctx)
1306 {
1307 DPLANE_CTX_VALID(ctx);
1308
1309 return ctx->u.rinfo.zd_instance;
1310 }
1311
1312 void dplane_ctx_set_instance(struct zebra_dplane_ctx *ctx, uint16_t instance)
1313 {
1314 DPLANE_CTX_VALID(ctx);
1315
1316 ctx->u.rinfo.zd_instance = instance;
1317 }
1318
1319 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx *ctx)
1320 {
1321 DPLANE_CTX_VALID(ctx);
1322
1323 return ctx->u.rinfo.zd_old_instance;
1324 }
1325
1326 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx *ctx)
1327 {
1328 DPLANE_CTX_VALID(ctx);
1329
1330 return ctx->u.rinfo.zd_metric;
1331 }
1332
1333 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx *ctx)
1334 {
1335 DPLANE_CTX_VALID(ctx);
1336
1337 return ctx->u.rinfo.zd_old_metric;
1338 }
1339
1340 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx *ctx)
1341 {
1342 DPLANE_CTX_VALID(ctx);
1343
1344 return ctx->u.rinfo.zd_mtu;
1345 }
1346
1347 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx *ctx)
1348 {
1349 DPLANE_CTX_VALID(ctx);
1350
1351 return ctx->u.rinfo.zd_nexthop_mtu;
1352 }
1353
1354 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx *ctx)
1355 {
1356 DPLANE_CTX_VALID(ctx);
1357
1358 return ctx->u.rinfo.zd_distance;
1359 }
1360
1361 void dplane_ctx_set_distance(struct zebra_dplane_ctx *ctx, uint8_t distance)
1362 {
1363 DPLANE_CTX_VALID(ctx);
1364
1365 ctx->u.rinfo.zd_distance = distance;
1366 }
1367
1368 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx *ctx)
1369 {
1370 DPLANE_CTX_VALID(ctx);
1371
1372 return ctx->u.rinfo.zd_old_distance;
1373 }
1374
1375 /*
1376 * Set the nexthops associated with a context: note that processing code
1377 * may well expect that nexthops are in canonical (sorted) order, so we
1378 * will enforce that here.
1379 */
1380 void dplane_ctx_set_nexthops(struct zebra_dplane_ctx *ctx, struct nexthop *nh)
1381 {
1382 DPLANE_CTX_VALID(ctx);
1383
1384 if (ctx->u.rinfo.zd_ng.nexthop) {
1385 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
1386 ctx->u.rinfo.zd_ng.nexthop = NULL;
1387 }
1388 nexthop_group_copy_nh_sorted(&(ctx->u.rinfo.zd_ng), nh);
1389 }
1390
1391 /*
1392 * Set the list of backup nexthops; their ordering is preserved (they're not
1393 * re-sorted.)
1394 */
1395 void dplane_ctx_set_backup_nhg(struct zebra_dplane_ctx *ctx,
1396 const struct nexthop_group *nhg)
1397 {
1398 struct nexthop *nh, *last_nh, *nexthop;
1399
1400 DPLANE_CTX_VALID(ctx);
1401
1402 if (ctx->u.rinfo.backup_ng.nexthop) {
1403 nexthops_free(ctx->u.rinfo.backup_ng.nexthop);
1404 ctx->u.rinfo.backup_ng.nexthop = NULL;
1405 }
1406
1407 last_nh = NULL;
1408
1409 /* Be careful to preserve the order of the backup list */
1410 for (nh = nhg->nexthop; nh; nh = nh->next) {
1411 nexthop = nexthop_dup(nh, NULL);
1412
1413 if (last_nh)
1414 NEXTHOP_APPEND(last_nh, nexthop);
1415 else
1416 ctx->u.rinfo.backup_ng.nexthop = nexthop;
1417
1418 last_nh = nexthop;
1419 }
1420 }
1421
1422 uint32_t dplane_ctx_get_nhg_id(const struct zebra_dplane_ctx *ctx)
1423 {
1424 DPLANE_CTX_VALID(ctx);
1425 return ctx->u.rinfo.zd_nhg_id;
1426 }
1427
1428 const struct nexthop_group *dplane_ctx_get_ng(
1429 const struct zebra_dplane_ctx *ctx)
1430 {
1431 DPLANE_CTX_VALID(ctx);
1432
1433 return &(ctx->u.rinfo.zd_ng);
1434 }
1435
1436 const struct nexthop_group *
1437 dplane_ctx_get_backup_ng(const struct zebra_dplane_ctx *ctx)
1438 {
1439 DPLANE_CTX_VALID(ctx);
1440
1441 return &(ctx->u.rinfo.backup_ng);
1442 }
1443
1444 const struct nexthop_group *
1445 dplane_ctx_get_old_ng(const struct zebra_dplane_ctx *ctx)
1446 {
1447 DPLANE_CTX_VALID(ctx);
1448
1449 return &(ctx->u.rinfo.zd_old_ng);
1450 }
1451
1452 const struct nexthop_group *
1453 dplane_ctx_get_old_backup_ng(const struct zebra_dplane_ctx *ctx)
1454 {
1455 DPLANE_CTX_VALID(ctx);
1456
1457 return &(ctx->u.rinfo.old_backup_ng);
1458 }
1459
1460 const struct zebra_dplane_info *dplane_ctx_get_ns(
1461 const struct zebra_dplane_ctx *ctx)
1462 {
1463 DPLANE_CTX_VALID(ctx);
1464
1465 return &(ctx->zd_ns_info);
1466 }
1467
1468 int dplane_ctx_get_ns_sock(const struct zebra_dplane_ctx *ctx)
1469 {
1470 DPLANE_CTX_VALID(ctx);
1471
1472 #ifdef HAVE_NETLINK
1473 return ctx->zd_ns_info.sock;
1474 #else
1475 return -1;
1476 #endif
1477 }
1478
1479 /* Accessors for nexthop information */
1480 uint32_t dplane_ctx_get_nhe_id(const struct zebra_dplane_ctx *ctx)
1481 {
1482 DPLANE_CTX_VALID(ctx);
1483 return ctx->u.rinfo.nhe.id;
1484 }
1485
1486 uint32_t dplane_ctx_get_old_nhe_id(const struct zebra_dplane_ctx *ctx)
1487 {
1488 DPLANE_CTX_VALID(ctx);
1489 return ctx->u.rinfo.nhe.old_id;
1490 }
1491
1492 afi_t dplane_ctx_get_nhe_afi(const struct zebra_dplane_ctx *ctx)
1493 {
1494 DPLANE_CTX_VALID(ctx);
1495 return ctx->u.rinfo.nhe.afi;
1496 }
1497
1498 vrf_id_t dplane_ctx_get_nhe_vrf_id(const struct zebra_dplane_ctx *ctx)
1499 {
1500 DPLANE_CTX_VALID(ctx);
1501 return ctx->u.rinfo.nhe.vrf_id;
1502 }
1503
1504 int dplane_ctx_get_nhe_type(const struct zebra_dplane_ctx *ctx)
1505 {
1506 DPLANE_CTX_VALID(ctx);
1507 return ctx->u.rinfo.nhe.type;
1508 }
1509
1510 const struct nexthop_group *
1511 dplane_ctx_get_nhe_ng(const struct zebra_dplane_ctx *ctx)
1512 {
1513 DPLANE_CTX_VALID(ctx);
1514 return &(ctx->u.rinfo.nhe.ng);
1515 }
1516
1517 const struct nh_grp *
1518 dplane_ctx_get_nhe_nh_grp(const struct zebra_dplane_ctx *ctx)
1519 {
1520 DPLANE_CTX_VALID(ctx);
1521 return ctx->u.rinfo.nhe.nh_grp;
1522 }
1523
1524 uint8_t dplane_ctx_get_nhe_nh_grp_count(const struct zebra_dplane_ctx *ctx)
1525 {
1526 DPLANE_CTX_VALID(ctx);
1527 return ctx->u.rinfo.nhe.nh_grp_count;
1528 }
1529
1530 /* Accessors for LSP information */
1531
1532 mpls_label_t dplane_ctx_get_in_label(const struct zebra_dplane_ctx *ctx)
1533 {
1534 DPLANE_CTX_VALID(ctx);
1535
1536 return ctx->u.lsp.ile.in_label;
1537 }
1538
1539 void dplane_ctx_set_in_label(struct zebra_dplane_ctx *ctx, mpls_label_t label)
1540 {
1541 DPLANE_CTX_VALID(ctx);
1542
1543 ctx->u.lsp.ile.in_label = label;
1544 }
1545
1546 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx *ctx)
1547 {
1548 DPLANE_CTX_VALID(ctx);
1549
1550 return ctx->u.lsp.addr_family;
1551 }
1552
1553 void dplane_ctx_set_addr_family(struct zebra_dplane_ctx *ctx,
1554 uint8_t family)
1555 {
1556 DPLANE_CTX_VALID(ctx);
1557
1558 ctx->u.lsp.addr_family = family;
1559 }
1560
1561 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx *ctx)
1562 {
1563 DPLANE_CTX_VALID(ctx);
1564
1565 return ctx->u.lsp.flags;
1566 }
1567
1568 void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx *ctx,
1569 uint32_t flags)
1570 {
1571 DPLANE_CTX_VALID(ctx);
1572
1573 ctx->u.lsp.flags = flags;
1574 }
1575
1576 const struct nhlfe_list_head *dplane_ctx_get_nhlfe_list(
1577 const struct zebra_dplane_ctx *ctx)
1578 {
1579 DPLANE_CTX_VALID(ctx);
1580 return &(ctx->u.lsp.nhlfe_list);
1581 }
1582
1583 const struct nhlfe_list_head *dplane_ctx_get_backup_nhlfe_list(
1584 const struct zebra_dplane_ctx *ctx)
1585 {
1586 DPLANE_CTX_VALID(ctx);
1587 return &(ctx->u.lsp.backup_nhlfe_list);
1588 }
1589
1590 struct zebra_nhlfe *dplane_ctx_add_nhlfe(struct zebra_dplane_ctx *ctx,
1591 enum lsp_types_t lsp_type,
1592 enum nexthop_types_t nh_type,
1593 const union g_addr *gate,
1594 ifindex_t ifindex, uint8_t num_labels,
1595 mpls_label_t *out_labels)
1596 {
1597 struct zebra_nhlfe *nhlfe;
1598
1599 DPLANE_CTX_VALID(ctx);
1600
1601 nhlfe = zebra_mpls_lsp_add_nhlfe(&(ctx->u.lsp),
1602 lsp_type, nh_type, gate,
1603 ifindex, num_labels, out_labels);
1604
1605 return nhlfe;
1606 }
1607
1608 struct zebra_nhlfe *dplane_ctx_add_backup_nhlfe(
1609 struct zebra_dplane_ctx *ctx, enum lsp_types_t lsp_type,
1610 enum nexthop_types_t nh_type, const union g_addr *gate,
1611 ifindex_t ifindex, uint8_t num_labels, mpls_label_t *out_labels)
1612 {
1613 struct zebra_nhlfe *nhlfe;
1614
1615 DPLANE_CTX_VALID(ctx);
1616
1617 nhlfe = zebra_mpls_lsp_add_backup_nhlfe(&(ctx->u.lsp),
1618 lsp_type, nh_type, gate,
1619 ifindex, num_labels,
1620 out_labels);
1621
1622 return nhlfe;
1623 }
1624
1625 const struct zebra_nhlfe *
1626 dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx *ctx)
1627 {
1628 DPLANE_CTX_VALID(ctx);
1629
1630 return ctx->u.lsp.best_nhlfe;
1631 }
1632
1633 const struct zebra_nhlfe *
1634 dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx *ctx,
1635 struct zebra_nhlfe *nhlfe)
1636 {
1637 DPLANE_CTX_VALID(ctx);
1638
1639 ctx->u.lsp.best_nhlfe = nhlfe;
1640 return ctx->u.lsp.best_nhlfe;
1641 }
1642
1643 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx *ctx)
1644 {
1645 DPLANE_CTX_VALID(ctx);
1646
1647 return ctx->u.lsp.num_ecmp;
1648 }
1649
1650 mpls_label_t dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx *ctx)
1651 {
1652 DPLANE_CTX_VALID(ctx);
1653
1654 return ctx->u.pw.local_label;
1655 }
1656
1657 mpls_label_t dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx *ctx)
1658 {
1659 DPLANE_CTX_VALID(ctx);
1660
1661 return ctx->u.pw.remote_label;
1662 }
1663
1664 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx *ctx)
1665 {
1666 DPLANE_CTX_VALID(ctx);
1667
1668 return ctx->u.pw.type;
1669 }
1670
1671 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx *ctx)
1672 {
1673 DPLANE_CTX_VALID(ctx);
1674
1675 return ctx->u.pw.af;
1676 }
1677
1678 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx *ctx)
1679 {
1680 DPLANE_CTX_VALID(ctx);
1681
1682 return ctx->u.pw.flags;
1683 }
1684
1685 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx *ctx)
1686 {
1687 DPLANE_CTX_VALID(ctx);
1688
1689 return ctx->u.pw.status;
1690 }
1691
1692 void dplane_ctx_set_pw_status(struct zebra_dplane_ctx *ctx, int status)
1693 {
1694 DPLANE_CTX_VALID(ctx);
1695
1696 ctx->u.pw.status = status;
1697 }
1698
1699 const union g_addr *dplane_ctx_get_pw_dest(
1700 const struct zebra_dplane_ctx *ctx)
1701 {
1702 DPLANE_CTX_VALID(ctx);
1703
1704 return &(ctx->u.pw.dest);
1705 }
1706
1707 const union pw_protocol_fields *dplane_ctx_get_pw_proto(
1708 const struct zebra_dplane_ctx *ctx)
1709 {
1710 DPLANE_CTX_VALID(ctx);
1711
1712 return &(ctx->u.pw.fields);
1713 }
1714
1715 const struct nexthop_group *
1716 dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx *ctx)
1717 {
1718 DPLANE_CTX_VALID(ctx);
1719
1720 return &(ctx->u.pw.fib_nhg);
1721 }
1722
1723 const struct nexthop_group *
1724 dplane_ctx_get_pw_primary_nhg(const struct zebra_dplane_ctx *ctx)
1725 {
1726 DPLANE_CTX_VALID(ctx);
1727
1728 return &(ctx->u.pw.primary_nhg);
1729 }
1730
1731 const struct nexthop_group *
1732 dplane_ctx_get_pw_backup_nhg(const struct zebra_dplane_ctx *ctx)
1733 {
1734 DPLANE_CTX_VALID(ctx);
1735
1736 return &(ctx->u.pw.backup_nhg);
1737 }
1738
1739 /* Accessors for interface information */
1740 uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx *ctx)
1741 {
1742 DPLANE_CTX_VALID(ctx);
1743
1744 return ctx->u.intf.metric;
1745 }
1746
1747 void dplane_ctx_set_intf_metric(struct zebra_dplane_ctx *ctx, uint32_t metric)
1748 {
1749 DPLANE_CTX_VALID(ctx);
1750
1751 ctx->u.intf.metric = metric;
1752 }
1753
1754 /* Is interface addr p2p? */
1755 bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx *ctx)
1756 {
1757 DPLANE_CTX_VALID(ctx);
1758
1759 return (ctx->u.intf.flags & DPLANE_INTF_CONNECTED);
1760 }
1761
1762 bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx *ctx)
1763 {
1764 DPLANE_CTX_VALID(ctx);
1765
1766 return (ctx->u.intf.flags & DPLANE_INTF_SECONDARY);
1767 }
1768
1769 bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx *ctx)
1770 {
1771 DPLANE_CTX_VALID(ctx);
1772
1773 return (ctx->u.intf.flags & DPLANE_INTF_BROADCAST);
1774 }
1775
1776 void dplane_ctx_intf_set_connected(struct zebra_dplane_ctx *ctx)
1777 {
1778 DPLANE_CTX_VALID(ctx);
1779
1780 ctx->u.intf.flags |= DPLANE_INTF_CONNECTED;
1781 }
1782
1783 void dplane_ctx_intf_set_secondary(struct zebra_dplane_ctx *ctx)
1784 {
1785 DPLANE_CTX_VALID(ctx);
1786
1787 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
1788 }
1789
1790 void dplane_ctx_intf_set_broadcast(struct zebra_dplane_ctx *ctx)
1791 {
1792 DPLANE_CTX_VALID(ctx);
1793
1794 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
1795 }
1796
1797 const struct prefix *dplane_ctx_get_intf_addr(
1798 const struct zebra_dplane_ctx *ctx)
1799 {
1800 DPLANE_CTX_VALID(ctx);
1801
1802 return &(ctx->u.intf.prefix);
1803 }
1804
1805 void dplane_ctx_set_intf_addr(struct zebra_dplane_ctx *ctx,
1806 const struct prefix *p)
1807 {
1808 DPLANE_CTX_VALID(ctx);
1809
1810 prefix_copy(&(ctx->u.intf.prefix), p);
1811 }
1812
1813 bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx *ctx)
1814 {
1815 DPLANE_CTX_VALID(ctx);
1816
1817 return (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST);
1818 }
1819
1820 const struct prefix *dplane_ctx_get_intf_dest(
1821 const struct zebra_dplane_ctx *ctx)
1822 {
1823 DPLANE_CTX_VALID(ctx);
1824
1825 return &(ctx->u.intf.dest_prefix);
1826 }
1827
1828 void dplane_ctx_set_intf_dest(struct zebra_dplane_ctx *ctx,
1829 const struct prefix *p)
1830 {
1831 DPLANE_CTX_VALID(ctx);
1832
1833 prefix_copy(&(ctx->u.intf.dest_prefix), p);
1834 }
1835
1836 bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx *ctx)
1837 {
1838 DPLANE_CTX_VALID(ctx);
1839
1840 return (ctx->u.intf.flags & DPLANE_INTF_HAS_LABEL);
1841 }
1842
1843 const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx *ctx)
1844 {
1845 DPLANE_CTX_VALID(ctx);
1846
1847 return ctx->u.intf.label;
1848 }
1849
1850 void dplane_ctx_set_intf_label(struct zebra_dplane_ctx *ctx, const char *label)
1851 {
1852 size_t len;
1853
1854 DPLANE_CTX_VALID(ctx);
1855
1856 if (ctx->u.intf.label && ctx->u.intf.label != ctx->u.intf.label_buf)
1857 free(ctx->u.intf.label);
1858
1859 ctx->u.intf.label = NULL;
1860
1861 if (label) {
1862 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
1863
1864 /* Use embedded buffer if it's adequate; else allocate. */
1865 len = strlen(label);
1866
1867 if (len < sizeof(ctx->u.intf.label_buf)) {
1868 strlcpy(ctx->u.intf.label_buf, label,
1869 sizeof(ctx->u.intf.label_buf));
1870 ctx->u.intf.label = ctx->u.intf.label_buf;
1871 } else {
1872 ctx->u.intf.label = strdup(label);
1873 }
1874 } else {
1875 ctx->u.intf.flags &= ~DPLANE_INTF_HAS_LABEL;
1876 }
1877 }
1878
1879 /* Accessors for MAC information */
1880 vlanid_t dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx *ctx)
1881 {
1882 DPLANE_CTX_VALID(ctx);
1883 return ctx->u.macinfo.vid;
1884 }
1885
1886 bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx *ctx)
1887 {
1888 DPLANE_CTX_VALID(ctx);
1889 return ctx->u.macinfo.is_sticky;
1890 }
1891
1892 uint32_t dplane_ctx_mac_get_nhg_id(const struct zebra_dplane_ctx *ctx)
1893 {
1894 DPLANE_CTX_VALID(ctx);
1895 return ctx->u.macinfo.nhg_id;
1896 }
1897
1898 uint32_t dplane_ctx_mac_get_update_flags(const struct zebra_dplane_ctx *ctx)
1899 {
1900 DPLANE_CTX_VALID(ctx);
1901 return ctx->u.macinfo.update_flags;
1902 }
1903
1904 const struct ethaddr *dplane_ctx_mac_get_addr(
1905 const struct zebra_dplane_ctx *ctx)
1906 {
1907 DPLANE_CTX_VALID(ctx);
1908 return &(ctx->u.macinfo.mac);
1909 }
1910
1911 const struct in_addr *dplane_ctx_mac_get_vtep_ip(
1912 const struct zebra_dplane_ctx *ctx)
1913 {
1914 DPLANE_CTX_VALID(ctx);
1915 return &(ctx->u.macinfo.vtep_ip);
1916 }
1917
1918 ifindex_t dplane_ctx_mac_get_br_ifindex(const struct zebra_dplane_ctx *ctx)
1919 {
1920 DPLANE_CTX_VALID(ctx);
1921 return ctx->u.macinfo.br_ifindex;
1922 }
1923
1924 /* Accessors for neighbor information */
1925 const struct ipaddr *dplane_ctx_neigh_get_ipaddr(
1926 const struct zebra_dplane_ctx *ctx)
1927 {
1928 DPLANE_CTX_VALID(ctx);
1929 return &(ctx->u.neigh.ip_addr);
1930 }
1931
1932 const struct ipaddr *
1933 dplane_ctx_neigh_get_link_ip(const struct zebra_dplane_ctx *ctx)
1934 {
1935 DPLANE_CTX_VALID(ctx);
1936 return &(ctx->u.neigh.link.ip_addr);
1937 }
1938
1939 const struct ethaddr *dplane_ctx_neigh_get_mac(
1940 const struct zebra_dplane_ctx *ctx)
1941 {
1942 DPLANE_CTX_VALID(ctx);
1943 return &(ctx->u.neigh.link.mac);
1944 }
1945
1946 uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx *ctx)
1947 {
1948 DPLANE_CTX_VALID(ctx);
1949 return ctx->u.neigh.flags;
1950 }
1951
1952 uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx *ctx)
1953 {
1954 DPLANE_CTX_VALID(ctx);
1955 return ctx->u.neigh.state;
1956 }
1957
1958 uint32_t dplane_ctx_neigh_get_update_flags(const struct zebra_dplane_ctx *ctx)
1959 {
1960 DPLANE_CTX_VALID(ctx);
1961 return ctx->u.neigh.update_flags;
1962 }
1963
1964 /* Accessor for GRE set */
1965 uint32_t
1966 dplane_ctx_gre_get_link_ifindex(const struct zebra_dplane_ctx *ctx)
1967 {
1968 DPLANE_CTX_VALID(ctx);
1969
1970 return ctx->u.gre.link_ifindex;
1971 }
1972
1973 unsigned int
1974 dplane_ctx_gre_get_mtu(const struct zebra_dplane_ctx *ctx)
1975 {
1976 DPLANE_CTX_VALID(ctx);
1977
1978 return ctx->u.gre.mtu;
1979 }
1980
1981 const struct zebra_l2info_gre *
1982 dplane_ctx_gre_get_info(const struct zebra_dplane_ctx *ctx)
1983 {
1984 DPLANE_CTX_VALID(ctx);
1985
1986 return &ctx->u.gre.info;
1987 }
1988
1989 /* Accessors for PBR rule information */
1990 int dplane_ctx_rule_get_sock(const struct zebra_dplane_ctx *ctx)
1991 {
1992 DPLANE_CTX_VALID(ctx);
1993
1994 return ctx->u.rule.sock;
1995 }
1996
1997 const char *dplane_ctx_rule_get_ifname(const struct zebra_dplane_ctx *ctx)
1998 {
1999 DPLANE_CTX_VALID(ctx);
2000
2001 return ctx->u.rule.new.ifname;
2002 }
2003
2004 int dplane_ctx_rule_get_unique(const struct zebra_dplane_ctx *ctx)
2005 {
2006 DPLANE_CTX_VALID(ctx);
2007
2008 return ctx->u.rule.unique;
2009 }
2010
2011 int dplane_ctx_rule_get_seq(const struct zebra_dplane_ctx *ctx)
2012 {
2013 DPLANE_CTX_VALID(ctx);
2014
2015 return ctx->u.rule.seq;
2016 }
2017
2018 uint32_t dplane_ctx_rule_get_priority(const struct zebra_dplane_ctx *ctx)
2019 {
2020 DPLANE_CTX_VALID(ctx);
2021
2022 return ctx->u.rule.new.priority;
2023 }
2024
2025 uint32_t dplane_ctx_rule_get_old_priority(const struct zebra_dplane_ctx *ctx)
2026 {
2027 DPLANE_CTX_VALID(ctx);
2028
2029 return ctx->u.rule.old.priority;
2030 }
2031
2032 uint32_t dplane_ctx_rule_get_table(const struct zebra_dplane_ctx *ctx)
2033 {
2034 DPLANE_CTX_VALID(ctx);
2035
2036 return ctx->u.rule.new.table;
2037 }
2038
2039 uint32_t dplane_ctx_rule_get_old_table(const struct zebra_dplane_ctx *ctx)
2040 {
2041 DPLANE_CTX_VALID(ctx);
2042
2043 return ctx->u.rule.old.table;
2044 }
2045
2046 uint32_t dplane_ctx_rule_get_filter_bm(const struct zebra_dplane_ctx *ctx)
2047 {
2048 DPLANE_CTX_VALID(ctx);
2049
2050 return ctx->u.rule.new.filter_bm;
2051 }
2052
2053 uint32_t dplane_ctx_rule_get_old_filter_bm(const struct zebra_dplane_ctx *ctx)
2054 {
2055 DPLANE_CTX_VALID(ctx);
2056
2057 return ctx->u.rule.old.filter_bm;
2058 }
2059
2060 uint32_t dplane_ctx_rule_get_fwmark(const struct zebra_dplane_ctx *ctx)
2061 {
2062 DPLANE_CTX_VALID(ctx);
2063
2064 return ctx->u.rule.new.fwmark;
2065 }
2066
2067 uint32_t dplane_ctx_rule_get_old_fwmark(const struct zebra_dplane_ctx *ctx)
2068 {
2069 DPLANE_CTX_VALID(ctx);
2070
2071 return ctx->u.rule.old.fwmark;
2072 }
2073
2074 uint8_t dplane_ctx_rule_get_ipproto(const struct zebra_dplane_ctx *ctx)
2075 {
2076 DPLANE_CTX_VALID(ctx);
2077
2078 return ctx->u.rule.new.ip_proto;
2079 }
2080
2081 uint8_t dplane_ctx_rule_get_old_ipproto(const struct zebra_dplane_ctx *ctx)
2082 {
2083 DPLANE_CTX_VALID(ctx);
2084
2085 return ctx->u.rule.old.ip_proto;
2086 }
2087
2088 uint8_t dplane_ctx_rule_get_dsfield(const struct zebra_dplane_ctx *ctx)
2089 {
2090 DPLANE_CTX_VALID(ctx);
2091
2092 return ctx->u.rule.new.dsfield;
2093 }
2094
2095 uint8_t dplane_ctx_rule_get_old_dsfield(const struct zebra_dplane_ctx *ctx)
2096 {
2097 DPLANE_CTX_VALID(ctx);
2098
2099 return ctx->u.rule.old.dsfield;
2100 }
2101
2102 const struct prefix *
2103 dplane_ctx_rule_get_src_ip(const struct zebra_dplane_ctx *ctx)
2104 {
2105 DPLANE_CTX_VALID(ctx);
2106
2107 return &(ctx->u.rule.new.src_ip);
2108 }
2109
2110 const struct prefix *
2111 dplane_ctx_rule_get_old_src_ip(const struct zebra_dplane_ctx *ctx)
2112 {
2113 DPLANE_CTX_VALID(ctx);
2114
2115 return &(ctx->u.rule.old.src_ip);
2116 }
2117
2118 const struct prefix *
2119 dplane_ctx_rule_get_dst_ip(const struct zebra_dplane_ctx *ctx)
2120 {
2121 DPLANE_CTX_VALID(ctx);
2122
2123 return &(ctx->u.rule.new.dst_ip);
2124 }
2125
2126 const struct prefix *
2127 dplane_ctx_rule_get_old_dst_ip(const struct zebra_dplane_ctx *ctx)
2128 {
2129 DPLANE_CTX_VALID(ctx);
2130
2131 return &(ctx->u.rule.old.dst_ip);
2132 }
2133
2134 uint32_t dplane_ctx_get_br_port_flags(const struct zebra_dplane_ctx *ctx)
2135 {
2136 DPLANE_CTX_VALID(ctx);
2137
2138 return ctx->u.br_port.flags;
2139 }
2140
2141 uint32_t
2142 dplane_ctx_get_br_port_sph_filter_cnt(const struct zebra_dplane_ctx *ctx)
2143 {
2144 DPLANE_CTX_VALID(ctx);
2145
2146 return ctx->u.br_port.sph_filter_cnt;
2147 }
2148
2149 const struct in_addr *
2150 dplane_ctx_get_br_port_sph_filters(const struct zebra_dplane_ctx *ctx)
2151 {
2152 DPLANE_CTX_VALID(ctx);
2153
2154 return ctx->u.br_port.sph_filters;
2155 }
2156
2157 uint32_t
2158 dplane_ctx_get_br_port_backup_nhg_id(const struct zebra_dplane_ctx *ctx)
2159 {
2160 DPLANE_CTX_VALID(ctx);
2161
2162 return ctx->u.br_port.backup_nhg_id;
2163 }
2164
2165 /* Accessors for PBR iptable information */
2166 void dplane_ctx_get_pbr_iptable(const struct zebra_dplane_ctx *ctx,
2167 struct zebra_pbr_iptable *table)
2168 {
2169 DPLANE_CTX_VALID(ctx);
2170
2171 memcpy(table, &ctx->u.iptable, sizeof(struct zebra_pbr_iptable));
2172 }
2173
2174 void dplane_ctx_get_pbr_ipset(const struct zebra_dplane_ctx *ctx,
2175 struct zebra_pbr_ipset *ipset)
2176 {
2177 DPLANE_CTX_VALID(ctx);
2178
2179 assert(ipset);
2180
2181 if (ctx->zd_op == DPLANE_OP_IPSET_ENTRY_ADD ||
2182 ctx->zd_op == DPLANE_OP_IPSET_ENTRY_DELETE) {
2183 memset(ipset, 0, sizeof(struct zebra_pbr_ipset));
2184 ipset->type = ctx->u.ipset_entry.info.type;
2185 ipset->family = ctx->u.ipset_entry.info.family;
2186 memcpy(&ipset->ipset_name, &ctx->u.ipset_entry.info.ipset_name,
2187 ZEBRA_IPSET_NAME_SIZE);
2188 } else
2189 memcpy(ipset, &ctx->u.ipset, sizeof(struct zebra_pbr_ipset));
2190 }
2191
2192 void dplane_ctx_get_pbr_ipset_entry(const struct zebra_dplane_ctx *ctx,
2193 struct zebra_pbr_ipset_entry *entry)
2194 {
2195 DPLANE_CTX_VALID(ctx);
2196
2197 assert(entry);
2198
2199 memcpy(entry, &ctx->u.ipset_entry.entry, sizeof(struct zebra_pbr_ipset_entry));
2200 }
2201
2202 /*
2203 * End of dplane context accessors
2204 */
2205
2206 /* Optional extra info about interfaces in nexthops - a plugin must enable
2207 * this extra info.
2208 */
2209 const struct dplane_intf_extra *
2210 dplane_ctx_get_intf_extra(const struct zebra_dplane_ctx *ctx)
2211 {
2212 return TAILQ_FIRST(&ctx->u.rinfo.intf_extra_q);
2213 }
2214
2215 const struct dplane_intf_extra *
2216 dplane_ctx_intf_extra_next(const struct zebra_dplane_ctx *ctx,
2217 const struct dplane_intf_extra *ptr)
2218 {
2219 return TAILQ_NEXT(ptr, link);
2220 }
2221
2222 vrf_id_t dplane_intf_extra_get_vrfid(const struct dplane_intf_extra *ptr)
2223 {
2224 return ptr->vrf_id;
2225 }
2226
2227 uint32_t dplane_intf_extra_get_ifindex(const struct dplane_intf_extra *ptr)
2228 {
2229 return ptr->ifindex;
2230 }
2231
2232 uint32_t dplane_intf_extra_get_flags(const struct dplane_intf_extra *ptr)
2233 {
2234 return ptr->flags;
2235 }
2236
2237 uint32_t dplane_intf_extra_get_status(const struct dplane_intf_extra *ptr)
2238 {
2239 return ptr->status;
2240 }
2241
2242 uint8_t dplane_ctx_neightable_get_family(const struct zebra_dplane_ctx *ctx)
2243 {
2244 DPLANE_CTX_VALID(ctx);
2245
2246 return ctx->u.neightable.family;
2247 }
2248
2249 uint32_t
2250 dplane_ctx_neightable_get_app_probes(const struct zebra_dplane_ctx *ctx)
2251 {
2252 DPLANE_CTX_VALID(ctx);
2253
2254 return ctx->u.neightable.app_probes;
2255 }
2256
2257 uint32_t
2258 dplane_ctx_neightable_get_ucast_probes(const struct zebra_dplane_ctx *ctx)
2259 {
2260 DPLANE_CTX_VALID(ctx);
2261
2262 return ctx->u.neightable.ucast_probes;
2263 }
2264
2265 uint32_t
2266 dplane_ctx_neightable_get_mcast_probes(const struct zebra_dplane_ctx *ctx)
2267 {
2268 DPLANE_CTX_VALID(ctx);
2269
2270 return ctx->u.neightable.mcast_probes;
2271 }
2272
2273 /*
2274 * End of interface extra info accessors
2275 */
2276
2277 /*
2278 * Retrieve the limit on the number of pending, unprocessed updates.
2279 */
2280 uint32_t dplane_get_in_queue_limit(void)
2281 {
2282 return atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
2283 memory_order_relaxed);
2284 }
2285
2286 /*
2287 * Configure limit on the number of pending, queued updates.
2288 */
2289 void dplane_set_in_queue_limit(uint32_t limit, bool set)
2290 {
2291 /* Reset to default on 'unset' */
2292 if (!set)
2293 limit = DPLANE_DEFAULT_MAX_QUEUED;
2294
2295 atomic_store_explicit(&zdplane_info.dg_max_queued_updates, limit,
2296 memory_order_relaxed);
2297 }
2298
2299 /*
2300 * Retrieve the current queue depth of incoming, unprocessed updates
2301 */
2302 uint32_t dplane_get_in_queue_len(void)
2303 {
2304 return atomic_load_explicit(&zdplane_info.dg_routes_queued,
2305 memory_order_seq_cst);
2306 }
2307
2308 /*
2309 * Common dataplane context init with zebra namespace info.
2310 */
2311 static int dplane_ctx_ns_init(struct zebra_dplane_ctx *ctx,
2312 struct zebra_ns *zns,
2313 bool is_update)
2314 {
2315 dplane_info_from_zns(&(ctx->zd_ns_info), zns);
2316
2317 ctx->zd_is_update = is_update;
2318
2319 #if defined(HAVE_NETLINK)
2320 /* Increment message counter after copying to context struct - may need
2321 * two messages in some 'update' cases.
2322 */
2323 if (is_update)
2324 zns->netlink_dplane_out.seq += 2;
2325 else
2326 zns->netlink_dplane_out.seq++;
2327 #endif /* HAVE_NETLINK */
2328
2329 return AOK;
2330 }
2331
2332 /*
2333 * Initialize a context block for a route update from zebra data structs.
2334 */
2335 int dplane_ctx_route_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
2336 struct route_node *rn, struct route_entry *re)
2337 {
2338 int ret = EINVAL;
2339 const struct route_table *table = NULL;
2340 const struct rib_table_info *info;
2341 const struct prefix *p, *src_p;
2342 struct zebra_ns *zns;
2343 struct zebra_vrf *zvrf;
2344 struct nexthop *nexthop;
2345 struct zebra_l3vni *zl3vni;
2346 const struct interface *ifp;
2347 struct dplane_intf_extra *if_extra;
2348
2349 if (!ctx || !rn || !re)
2350 goto done;
2351
2352 TAILQ_INIT(&ctx->u.rinfo.intf_extra_q);
2353
2354 ctx->zd_op = op;
2355 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2356
2357 ctx->u.rinfo.zd_type = re->type;
2358 ctx->u.rinfo.zd_old_type = re->type;
2359
2360 /* Prefixes: dest, and optional source */
2361 srcdest_rnode_prefixes(rn, &p, &src_p);
2362
2363 prefix_copy(&(ctx->u.rinfo.zd_dest), p);
2364
2365 if (src_p)
2366 prefix_copy(&(ctx->u.rinfo.zd_src), src_p);
2367 else
2368 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(ctx->u.rinfo.zd_src));
2369
2370 ctx->zd_table_id = re->table;
2371
2372 ctx->u.rinfo.zd_metric = re->metric;
2373 ctx->u.rinfo.zd_old_metric = re->metric;
2374 ctx->zd_vrf_id = re->vrf_id;
2375 ctx->u.rinfo.zd_mtu = re->mtu;
2376 ctx->u.rinfo.zd_nexthop_mtu = re->nexthop_mtu;
2377 ctx->u.rinfo.zd_instance = re->instance;
2378 ctx->u.rinfo.zd_tag = re->tag;
2379 ctx->u.rinfo.zd_old_tag = re->tag;
2380 ctx->u.rinfo.zd_distance = re->distance;
2381
2382 table = srcdest_rnode_table(rn);
2383 info = table->info;
2384
2385 ctx->u.rinfo.zd_afi = info->afi;
2386 ctx->u.rinfo.zd_safi = info->safi;
2387
2388 /* Copy nexthops; recursive info is included too */
2389 copy_nexthops(&(ctx->u.rinfo.zd_ng.nexthop),
2390 re->nhe->nhg.nexthop, NULL);
2391 ctx->u.rinfo.zd_nhg_id = re->nhe->id;
2392
2393 /* Copy backup nexthop info, if present */
2394 if (re->nhe->backup_info && re->nhe->backup_info->nhe) {
2395 copy_nexthops(&(ctx->u.rinfo.backup_ng.nexthop),
2396 re->nhe->backup_info->nhe->nhg.nexthop, NULL);
2397 }
2398
2399 /*
2400 * Ensure that the dplane nexthops' flags are clear and copy
2401 * encapsulation information.
2402 */
2403 for (ALL_NEXTHOPS(ctx->u.rinfo.zd_ng, nexthop)) {
2404 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2405
2406 /* Optionally capture extra interface info while we're in the
2407 * main zebra pthread - a plugin has to ask for this info.
2408 */
2409 if (dplane_collect_extra_intf_info) {
2410 ifp = if_lookup_by_index(nexthop->ifindex,
2411 nexthop->vrf_id);
2412
2413 if (ifp) {
2414 if_extra = XCALLOC(
2415 MTYPE_DP_INTF,
2416 sizeof(struct dplane_intf_extra));
2417 if_extra->vrf_id = nexthop->vrf_id;
2418 if_extra->ifindex = nexthop->ifindex;
2419 if_extra->flags = ifp->flags;
2420 if_extra->status = ifp->status;
2421
2422 TAILQ_INSERT_TAIL(&ctx->u.rinfo.intf_extra_q,
2423 if_extra, link);
2424 }
2425 }
2426
2427 /* Check for available evpn encapsulations. */
2428 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE))
2429 continue;
2430
2431 zl3vni = zl3vni_from_vrf(nexthop->vrf_id);
2432 if (zl3vni && is_l3vni_oper_up(zl3vni)) {
2433 nexthop->nh_encap_type = NET_VXLAN;
2434 nexthop->nh_encap.vni = zl3vni->vni;
2435 }
2436 }
2437
2438 /* Don't need some info when capturing a system notification */
2439 if (op == DPLANE_OP_SYS_ROUTE_ADD ||
2440 op == DPLANE_OP_SYS_ROUTE_DELETE) {
2441 ret = AOK;
2442 goto done;
2443 }
2444
2445 /* Extract ns info - can't use pointers to 'core' structs */
2446 zvrf = vrf_info_lookup(re->vrf_id);
2447 zns = zvrf->zns;
2448 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_ROUTE_UPDATE));
2449
2450 #ifdef HAVE_NETLINK
2451 {
2452 struct nhg_hash_entry *nhe = zebra_nhg_resolve(re->nhe);
2453
2454 ctx->u.rinfo.nhe.id = nhe->id;
2455 ctx->u.rinfo.nhe.old_id = 0;
2456 /*
2457 * Check if the nhe is installed/queued before doing anything
2458 * with this route.
2459 *
2460 * If its a delete we only use the prefix anyway, so this only
2461 * matters for INSTALL/UPDATE.
2462 */
2463 if (zebra_nhg_kernel_nexthops_enabled()
2464 && (((op == DPLANE_OP_ROUTE_INSTALL)
2465 || (op == DPLANE_OP_ROUTE_UPDATE))
2466 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
2467 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED))) {
2468 ret = ENOENT;
2469 goto done;
2470 }
2471
2472 re->nhe_installed_id = nhe->id;
2473 }
2474 #endif /* HAVE_NETLINK */
2475
2476 /* Trying out the sequence number idea, so we can try to detect
2477 * when a result is stale.
2478 */
2479 re->dplane_sequence = zebra_router_get_next_sequence();
2480 ctx->zd_seq = re->dplane_sequence;
2481
2482 ret = AOK;
2483
2484 done:
2485 return ret;
2486 }
2487
2488 /**
2489 * dplane_ctx_nexthop_init() - Initialize a context block for a nexthop update
2490 *
2491 * @ctx: Dataplane context to init
2492 * @op: Operation being performed
2493 * @nhe: Nexthop group hash entry
2494 *
2495 * Return: Result status
2496 */
2497 int dplane_ctx_nexthop_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
2498 struct nhg_hash_entry *nhe)
2499 {
2500 struct zebra_vrf *zvrf = NULL;
2501 struct zebra_ns *zns = NULL;
2502 int ret = EINVAL;
2503
2504 if (!ctx || !nhe)
2505 goto done;
2506
2507 ctx->zd_op = op;
2508 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2509
2510 /* Copy over nhe info */
2511 ctx->u.rinfo.nhe.id = nhe->id;
2512 ctx->u.rinfo.nhe.afi = nhe->afi;
2513 ctx->u.rinfo.nhe.vrf_id = nhe->vrf_id;
2514 ctx->u.rinfo.nhe.type = nhe->type;
2515
2516 nexthop_group_copy(&(ctx->u.rinfo.nhe.ng), &(nhe->nhg));
2517
2518 /* If this is a group, convert it to a grp array of ids */
2519 if (!zebra_nhg_depends_is_empty(nhe)
2520 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE))
2521 ctx->u.rinfo.nhe.nh_grp_count = zebra_nhg_nhe2grp(
2522 ctx->u.rinfo.nhe.nh_grp, nhe, MULTIPATH_NUM);
2523
2524 zvrf = vrf_info_lookup(nhe->vrf_id);
2525
2526 /*
2527 * Fallback to default namespace if the vrf got ripped out from under
2528 * us.
2529 */
2530 zns = zvrf ? zvrf->zns : zebra_ns_lookup(NS_DEFAULT);
2531
2532 /*
2533 * TODO: Might not need to mark this as an update, since
2534 * it probably won't require two messages
2535 */
2536 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_NH_UPDATE));
2537
2538 ret = AOK;
2539
2540 done:
2541 return ret;
2542 }
2543
2544 /*
2545 * Capture information for an LSP update in a dplane context.
2546 */
2547 int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
2548 struct zebra_lsp *lsp)
2549 {
2550 int ret = AOK;
2551 struct zebra_nhlfe *nhlfe, *new_nhlfe;
2552
2553 ctx->zd_op = op;
2554 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2555
2556 /* Capture namespace info */
2557 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2558 (op == DPLANE_OP_LSP_UPDATE));
2559
2560 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
2561
2562 nhlfe_list_init(&(ctx->u.lsp.nhlfe_list));
2563 nhlfe_list_init(&(ctx->u.lsp.backup_nhlfe_list));
2564
2565 /* This may be called to create/init a dplane context, not necessarily
2566 * to copy an lsp object.
2567 */
2568 if (lsp == NULL) {
2569 ret = AOK;
2570 goto done;
2571 }
2572
2573 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2574 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
2575 dplane_op2str(op), lsp->ile.in_label,
2576 lsp->num_ecmp);
2577
2578 ctx->u.lsp.ile = lsp->ile;
2579 ctx->u.lsp.addr_family = lsp->addr_family;
2580 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
2581 ctx->u.lsp.flags = lsp->flags;
2582
2583 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
2584 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
2585 /* Not sure if this is meaningful... */
2586 if (nhlfe->nexthop == NULL)
2587 continue;
2588
2589 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp), nhlfe->type,
2590 nhlfe->nexthop);
2591 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2592 ret = ENOMEM;
2593 break;
2594 }
2595
2596 /* Need to copy flags and backup info too */
2597 new_nhlfe->flags = nhlfe->flags;
2598 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2599
2600 if (CHECK_FLAG(new_nhlfe->nexthop->flags,
2601 NEXTHOP_FLAG_HAS_BACKUP)) {
2602 new_nhlfe->nexthop->backup_num =
2603 nhlfe->nexthop->backup_num;
2604 memcpy(new_nhlfe->nexthop->backup_idx,
2605 nhlfe->nexthop->backup_idx,
2606 new_nhlfe->nexthop->backup_num);
2607 }
2608
2609 if (nhlfe == lsp->best_nhlfe)
2610 ctx->u.lsp.best_nhlfe = new_nhlfe;
2611 }
2612
2613 if (ret != AOK)
2614 goto done;
2615
2616 /* Capture backup nhlfes/nexthops */
2617 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
2618 /* Not sure if this is meaningful... */
2619 if (nhlfe->nexthop == NULL)
2620 continue;
2621
2622 new_nhlfe = zebra_mpls_lsp_add_backup_nh(&(ctx->u.lsp),
2623 nhlfe->type,
2624 nhlfe->nexthop);
2625 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2626 ret = ENOMEM;
2627 break;
2628 }
2629
2630 /* Need to copy flags too */
2631 new_nhlfe->flags = nhlfe->flags;
2632 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2633 }
2634
2635 /* On error the ctx will be cleaned-up, so we don't need to
2636 * deal with any allocated nhlfe or nexthop structs here.
2637 */
2638 done:
2639
2640 return ret;
2641 }
2642
2643 /*
2644 * Capture information for an LSP update in a dplane context.
2645 */
2646 static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
2647 enum dplane_op_e op,
2648 struct zebra_pw *pw)
2649 {
2650 int ret = EINVAL;
2651 struct prefix p;
2652 afi_t afi;
2653 struct route_table *table;
2654 struct route_node *rn;
2655 struct route_entry *re;
2656 const struct nexthop_group *nhg;
2657 struct nexthop *nh, *newnh, *last_nh;
2658
2659 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2660 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
2661 dplane_op2str(op), pw->ifname, pw->local_label,
2662 pw->remote_label);
2663
2664 ctx->zd_op = op;
2665 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2666
2667 /* Capture namespace info: no netlink support as of 12/18,
2668 * but just in case...
2669 */
2670 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2671
2672 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
2673
2674 /* This name appears to be c-string, so we use string copy. */
2675 strlcpy(ctx->zd_ifname, pw->ifname, sizeof(ctx->zd_ifname));
2676
2677 ctx->zd_vrf_id = pw->vrf_id;
2678 ctx->zd_ifindex = pw->ifindex;
2679 ctx->u.pw.type = pw->type;
2680 ctx->u.pw.af = pw->af;
2681 ctx->u.pw.local_label = pw->local_label;
2682 ctx->u.pw.remote_label = pw->remote_label;
2683 ctx->u.pw.flags = pw->flags;
2684
2685 ctx->u.pw.dest = pw->nexthop;
2686
2687 ctx->u.pw.fields = pw->data;
2688
2689 /* Capture nexthop info for the pw destination. We need to look
2690 * up and use zebra datastructs, but we're running in the zebra
2691 * pthread here so that should be ok.
2692 */
2693 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
2694 p.family = pw->af;
2695 p.prefixlen = ((pw->af == AF_INET) ? IPV4_MAX_BITLEN : IPV6_MAX_BITLEN);
2696
2697 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
2698 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
2699 if (table == NULL)
2700 goto done;
2701
2702 rn = route_node_match(table, &p);
2703 if (rn == NULL)
2704 goto done;
2705
2706 re = NULL;
2707 RNODE_FOREACH_RE(rn, re) {
2708 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
2709 break;
2710 }
2711
2712 if (re) {
2713 /* We'll capture a 'fib' list of nexthops that meet our
2714 * criteria: installed, and labelled.
2715 */
2716 nhg = rib_get_fib_nhg(re);
2717 last_nh = NULL;
2718
2719 if (nhg && nhg->nexthop) {
2720 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
2721 if (!CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE)
2722 || CHECK_FLAG(nh->flags,
2723 NEXTHOP_FLAG_RECURSIVE)
2724 || nh->nh_label == NULL)
2725 continue;
2726
2727 newnh = nexthop_dup(nh, NULL);
2728
2729 if (last_nh)
2730 NEXTHOP_APPEND(last_nh, newnh);
2731 else
2732 ctx->u.pw.fib_nhg.nexthop = newnh;
2733 last_nh = newnh;
2734 }
2735 }
2736
2737 /* Include any installed backup nexthops also. */
2738 nhg = rib_get_fib_backup_nhg(re);
2739 if (nhg && nhg->nexthop) {
2740 for (ALL_NEXTHOPS_PTR(nhg, nh)) {
2741 if (!CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE)
2742 || CHECK_FLAG(nh->flags,
2743 NEXTHOP_FLAG_RECURSIVE)
2744 || nh->nh_label == NULL)
2745 continue;
2746
2747 newnh = nexthop_dup(nh, NULL);
2748
2749 if (last_nh)
2750 NEXTHOP_APPEND(last_nh, newnh);
2751 else
2752 ctx->u.pw.fib_nhg.nexthop = newnh;
2753 last_nh = newnh;
2754 }
2755 }
2756
2757 /* Copy primary nexthops; recursive info is included too */
2758 assert(re->nhe != NULL); /* SA warning */
2759 copy_nexthops(&(ctx->u.pw.primary_nhg.nexthop),
2760 re->nhe->nhg.nexthop, NULL);
2761 ctx->u.pw.nhg_id = re->nhe->id;
2762
2763 /* Copy backup nexthop info, if present */
2764 if (re->nhe->backup_info && re->nhe->backup_info->nhe) {
2765 copy_nexthops(&(ctx->u.pw.backup_nhg.nexthop),
2766 re->nhe->backup_info->nhe->nhg.nexthop,
2767 NULL);
2768 }
2769 }
2770 route_unlock_node(rn);
2771
2772 ret = AOK;
2773
2774 done:
2775 return ret;
2776 }
2777
2778 /**
2779 * dplane_ctx_rule_init_single() - Initialize a dataplane representation of a
2780 * PBR rule.
2781 *
2782 * @dplane_rule: Dataplane internal representation of a rule
2783 * @rule: PBR rule
2784 */
2785 static void dplane_ctx_rule_init_single(struct dplane_ctx_rule *dplane_rule,
2786 struct zebra_pbr_rule *rule)
2787 {
2788 dplane_rule->priority = rule->rule.priority;
2789 dplane_rule->table = rule->rule.action.table;
2790
2791 dplane_rule->filter_bm = rule->rule.filter.filter_bm;
2792 dplane_rule->fwmark = rule->rule.filter.fwmark;
2793 dplane_rule->dsfield = rule->rule.filter.dsfield;
2794 dplane_rule->ip_proto = rule->rule.filter.ip_proto;
2795 prefix_copy(&(dplane_rule->dst_ip), &rule->rule.filter.dst_ip);
2796 prefix_copy(&(dplane_rule->src_ip), &rule->rule.filter.src_ip);
2797
2798 dplane_rule->action_pcp = rule->rule.action.pcp;
2799 dplane_rule->action_vlan_flags = rule->rule.action.vlan_flags;
2800 dplane_rule->action_vlan_id = rule->rule.action.vlan_id;
2801 dplane_rule->action_queue_id = rule->rule.action.queue_id;
2802
2803 strlcpy(dplane_rule->ifname, rule->ifname, INTERFACE_NAMSIZ);
2804 }
2805
2806 /**
2807 * dplane_ctx_rule_init() - Initialize a context block for a PBR rule update.
2808 *
2809 * @ctx: Dataplane context to init
2810 * @op: Operation being performed
2811 * @new_rule: PBR rule
2812 *
2813 * Return: Result status
2814 */
2815 static int dplane_ctx_rule_init(struct zebra_dplane_ctx *ctx,
2816 enum dplane_op_e op,
2817 struct zebra_pbr_rule *new_rule,
2818 struct zebra_pbr_rule *old_rule)
2819 {
2820 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2821 zlog_debug(
2822 "init dplane ctx %s: IF %s Prio %u Fwmark %u Src %pFX Dst %pFX Table %u",
2823 dplane_op2str(op), new_rule->ifname,
2824 new_rule->rule.priority, new_rule->rule.filter.fwmark,
2825 &new_rule->rule.filter.src_ip,
2826 &new_rule->rule.filter.dst_ip,
2827 new_rule->rule.action.table);
2828
2829 ctx->zd_op = op;
2830 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2831
2832 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2833 op == DPLANE_OP_RULE_UPDATE);
2834
2835 ctx->zd_vrf_id = new_rule->vrf_id;
2836 strlcpy(ctx->zd_ifname, new_rule->ifname, sizeof(ctx->zd_ifname));
2837
2838 ctx->u.rule.sock = new_rule->sock;
2839 ctx->u.rule.unique = new_rule->rule.unique;
2840 ctx->u.rule.seq = new_rule->rule.seq;
2841
2842 dplane_ctx_rule_init_single(&ctx->u.rule.new, new_rule);
2843 if (op == DPLANE_OP_RULE_UPDATE)
2844 dplane_ctx_rule_init_single(&ctx->u.rule.old, old_rule);
2845
2846 return AOK;
2847 }
2848
2849 /**
2850 * dplane_ctx_iptable_init() - Initialize a context block for a PBR iptable
2851 * update.
2852 *
2853 * @ctx: Dataplane context to init
2854 * @op: Operation being performed
2855 * @new_rule: PBR iptable
2856 *
2857 * Return: Result status
2858 */
2859 static int dplane_ctx_iptable_init(struct zebra_dplane_ctx *ctx,
2860 enum dplane_op_e op,
2861 struct zebra_pbr_iptable *iptable)
2862 {
2863 char *ifname;
2864 struct listnode *node;
2865
2866 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2867 zlog_debug(
2868 "init dplane ctx %s: Unique %u Fwmark %u Family %s Action %s",
2869 dplane_op2str(op), iptable->unique, iptable->fwmark,
2870 family2str(iptable->family),
2871 iptable->action == ZEBRA_IPTABLES_DROP ? "Drop"
2872 : "Forward");
2873 }
2874
2875 ctx->zd_op = op;
2876 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2877
2878 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2879
2880 ctx->zd_vrf_id = iptable->vrf_id;
2881 memcpy(&ctx->u.iptable, iptable, sizeof(struct zebra_pbr_iptable));
2882 ctx->u.iptable.interface_name_list = NULL;
2883 if (iptable->nb_interface > 0) {
2884 ctx->u.iptable.interface_name_list = list_new();
2885 for (ALL_LIST_ELEMENTS_RO(iptable->interface_name_list, node,
2886 ifname)) {
2887 listnode_add(ctx->u.iptable.interface_name_list,
2888 XSTRDUP(MTYPE_DP_NETFILTER, ifname));
2889 }
2890 }
2891 return AOK;
2892 }
2893
2894 /**
2895 * dplane_ctx_ipset_init() - Initialize a context block for a PBR ipset update.
2896 *
2897 * @ctx: Dataplane context to init
2898 * @op: Operation being performed
2899 * @new_rule: PBR ipset
2900 *
2901 * Return: Result status
2902 */
2903 static int dplane_ctx_ipset_init(struct zebra_dplane_ctx *ctx,
2904 enum dplane_op_e op,
2905 struct zebra_pbr_ipset *ipset)
2906 {
2907 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2908 zlog_debug("init dplane ctx %s: %s Unique %u Family %s Type %s",
2909 dplane_op2str(op), ipset->ipset_name, ipset->unique,
2910 family2str(ipset->family),
2911 zebra_pbr_ipset_type2str(ipset->type));
2912 }
2913
2914 ctx->zd_op = op;
2915 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2916
2917 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2918
2919 ctx->zd_vrf_id = ipset->vrf_id;
2920
2921 memcpy(&ctx->u.ipset, ipset, sizeof(struct zebra_pbr_ipset));
2922 return AOK;
2923 }
2924
2925 /**
2926 * dplane_ctx_ipset_entry_init() - Initialize a context block for a PBR ipset
2927 * update.
2928 *
2929 * @ctx: Dataplane context to init
2930 * @op: Operation being performed
2931 * @new_rule: PBR ipset
2932 *
2933 * Return: Result status
2934 */
2935 static int
2936 dplane_ctx_ipset_entry_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
2937 struct zebra_pbr_ipset_entry *ipset_entry)
2938 {
2939 struct zebra_pbr_ipset *ipset;
2940
2941 ipset = ipset_entry->backpointer;
2942 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2943 zlog_debug("init dplane ctx %s: %s Unique %u filter %u",
2944 dplane_op2str(op), ipset->ipset_name,
2945 ipset_entry->unique, ipset_entry->filter_bm);
2946 }
2947
2948 ctx->zd_op = op;
2949 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2950
2951 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2952
2953 ctx->zd_vrf_id = ipset->vrf_id;
2954
2955 memcpy(&ctx->u.ipset_entry.entry, ipset_entry,
2956 sizeof(struct zebra_pbr_ipset_entry));
2957 ctx->u.ipset_entry.entry.backpointer = NULL;
2958 ctx->u.ipset_entry.info.type = ipset->type;
2959 ctx->u.ipset_entry.info.family = ipset->family;
2960 memcpy(&ctx->u.ipset_entry.info.ipset_name, &ipset->ipset_name,
2961 ZEBRA_IPSET_NAME_SIZE);
2962
2963 return AOK;
2964 }
2965
2966
2967 /*
2968 * Enqueue a new update,
2969 * and ensure an event is active for the dataplane pthread.
2970 */
2971 static int dplane_update_enqueue(struct zebra_dplane_ctx *ctx)
2972 {
2973 int ret = EINVAL;
2974 uint32_t high, curr;
2975
2976 /* Enqueue for processing by the dataplane pthread */
2977 DPLANE_LOCK();
2978 {
2979 TAILQ_INSERT_TAIL(&zdplane_info.dg_update_ctx_q, ctx,
2980 zd_q_entries);
2981 }
2982 DPLANE_UNLOCK();
2983
2984 curr = atomic_fetch_add_explicit(
2985 &(zdplane_info.dg_routes_queued),
2986 1, memory_order_seq_cst);
2987
2988 curr++; /* We got the pre-incremented value */
2989
2990 /* Maybe update high-water counter also */
2991 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
2992 memory_order_seq_cst);
2993 while (high < curr) {
2994 if (atomic_compare_exchange_weak_explicit(
2995 &zdplane_info.dg_routes_queued_max,
2996 &high, curr,
2997 memory_order_seq_cst,
2998 memory_order_seq_cst))
2999 break;
3000 }
3001
3002 /* Ensure that an event for the dataplane thread is active */
3003 ret = dplane_provider_work_ready();
3004
3005 return ret;
3006 }
3007
3008 /*
3009 * Utility that prepares a route update and enqueues it for processing
3010 */
3011 static enum zebra_dplane_result
3012 dplane_route_update_internal(struct route_node *rn,
3013 struct route_entry *re,
3014 struct route_entry *old_re,
3015 enum dplane_op_e op)
3016 {
3017 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3018 int ret = EINVAL;
3019 struct zebra_dplane_ctx *ctx = NULL;
3020
3021 /* Obtain context block */
3022 ctx = dplane_ctx_alloc();
3023
3024 /* Init context with info from zebra data structs */
3025 ret = dplane_ctx_route_init(ctx, op, rn, re);
3026 if (ret == AOK) {
3027 /* Capture some extra info for update case
3028 * where there's a different 'old' route.
3029 */
3030 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
3031 old_re && (old_re != re)) {
3032
3033 old_re->dplane_sequence =
3034 zebra_router_get_next_sequence();
3035 ctx->zd_old_seq = old_re->dplane_sequence;
3036
3037 ctx->u.rinfo.zd_old_tag = old_re->tag;
3038 ctx->u.rinfo.zd_old_type = old_re->type;
3039 ctx->u.rinfo.zd_old_instance = old_re->instance;
3040 ctx->u.rinfo.zd_old_distance = old_re->distance;
3041 ctx->u.rinfo.zd_old_metric = old_re->metric;
3042 ctx->u.rinfo.nhe.old_id = old_re->nhe->id;
3043
3044 #ifndef HAVE_NETLINK
3045 /* For bsd, capture previous re's nexthops too, sigh.
3046 * We'll need these to do per-nexthop deletes.
3047 */
3048 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
3049 old_re->nhe->nhg.nexthop, NULL);
3050
3051 if (zebra_nhg_get_backup_nhg(old_re->nhe) != NULL) {
3052 struct nexthop_group *nhg;
3053 struct nexthop **nh;
3054
3055 nhg = zebra_nhg_get_backup_nhg(old_re->nhe);
3056 nh = &(ctx->u.rinfo.old_backup_ng.nexthop);
3057
3058 if (nhg->nexthop)
3059 copy_nexthops(nh, nhg->nexthop, NULL);
3060 }
3061 #endif /* !HAVE_NETLINK */
3062 }
3063
3064 /*
3065 * If the old and new context type, and nexthop group id
3066 * are the same there is no need to send down a route replace
3067 * as that we know we have sent a nexthop group replace
3068 * or an upper level protocol has sent us the exact
3069 * same route again.
3070 */
3071 if ((dplane_ctx_get_type(ctx) == dplane_ctx_get_old_type(ctx))
3072 && (dplane_ctx_get_nhe_id(ctx)
3073 == dplane_ctx_get_old_nhe_id(ctx))
3074 && (dplane_ctx_get_nhe_id(ctx) >= ZEBRA_NHG_PROTO_LOWER)) {
3075 struct nexthop *nexthop;
3076
3077 if (IS_ZEBRA_DEBUG_DPLANE)
3078 zlog_debug(
3079 "%s: Ignoring Route exactly the same",
3080 __func__);
3081
3082 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
3083 nexthop)) {
3084 if (CHECK_FLAG(nexthop->flags,
3085 NEXTHOP_FLAG_RECURSIVE))
3086 continue;
3087
3088 if (CHECK_FLAG(nexthop->flags,
3089 NEXTHOP_FLAG_ACTIVE))
3090 SET_FLAG(nexthop->flags,
3091 NEXTHOP_FLAG_FIB);
3092 }
3093
3094 dplane_ctx_free(&ctx);
3095 return ZEBRA_DPLANE_REQUEST_SUCCESS;
3096 }
3097
3098 /* Enqueue context for processing */
3099 ret = dplane_update_enqueue(ctx);
3100 }
3101
3102 /* Update counter */
3103 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
3104 memory_order_relaxed);
3105
3106 if (ret == AOK)
3107 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3108 else {
3109 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors, 1,
3110 memory_order_relaxed);
3111 if (ctx)
3112 dplane_ctx_free(&ctx);
3113 }
3114
3115 return result;
3116 }
3117
3118 /**
3119 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
3120 *
3121 * @nhe: Nexthop group hash entry where the change occured
3122 * @op: The operation to be enqued
3123 *
3124 * Return: Result of the change
3125 */
3126 static enum zebra_dplane_result
3127 dplane_nexthop_update_internal(struct nhg_hash_entry *nhe, enum dplane_op_e op)
3128 {
3129 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3130 int ret = EINVAL;
3131 struct zebra_dplane_ctx *ctx = NULL;
3132
3133 /* Obtain context block */
3134 ctx = dplane_ctx_alloc();
3135 if (!ctx) {
3136 ret = ENOMEM;
3137 goto done;
3138 }
3139
3140 ret = dplane_ctx_nexthop_init(ctx, op, nhe);
3141 if (ret == AOK)
3142 ret = dplane_update_enqueue(ctx);
3143
3144 done:
3145 /* Update counter */
3146 atomic_fetch_add_explicit(&zdplane_info.dg_nexthops_in, 1,
3147 memory_order_relaxed);
3148
3149 if (ret == AOK)
3150 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3151 else {
3152 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
3153 memory_order_relaxed);
3154 if (ctx)
3155 dplane_ctx_free(&ctx);
3156 }
3157
3158 return result;
3159 }
3160
3161 /*
3162 * Enqueue a route 'add' for the dataplane.
3163 */
3164 enum zebra_dplane_result dplane_route_add(struct route_node *rn,
3165 struct route_entry *re)
3166 {
3167 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3168
3169 if (rn == NULL || re == NULL)
3170 goto done;
3171
3172 ret = dplane_route_update_internal(rn, re, NULL,
3173 DPLANE_OP_ROUTE_INSTALL);
3174
3175 done:
3176 return ret;
3177 }
3178
3179 /*
3180 * Enqueue a route update for the dataplane.
3181 */
3182 enum zebra_dplane_result dplane_route_update(struct route_node *rn,
3183 struct route_entry *re,
3184 struct route_entry *old_re)
3185 {
3186 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3187
3188 if (rn == NULL || re == NULL)
3189 goto done;
3190
3191 ret = dplane_route_update_internal(rn, re, old_re,
3192 DPLANE_OP_ROUTE_UPDATE);
3193 done:
3194 return ret;
3195 }
3196
3197 /*
3198 * Enqueue a route removal for the dataplane.
3199 */
3200 enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
3201 struct route_entry *re)
3202 {
3203 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3204
3205 if (rn == NULL || re == NULL)
3206 goto done;
3207
3208 ret = dplane_route_update_internal(rn, re, NULL,
3209 DPLANE_OP_ROUTE_DELETE);
3210
3211 done:
3212 return ret;
3213 }
3214
3215 /*
3216 * Notify the dplane when system/connected routes change.
3217 */
3218 enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
3219 struct route_entry *re)
3220 {
3221 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3222
3223 /* Ignore this event unless a provider plugin has requested it. */
3224 if (!zdplane_info.dg_sys_route_notifs) {
3225 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
3226 goto done;
3227 }
3228
3229 if (rn == NULL || re == NULL)
3230 goto done;
3231
3232 ret = dplane_route_update_internal(rn, re, NULL,
3233 DPLANE_OP_SYS_ROUTE_ADD);
3234
3235 done:
3236 return ret;
3237 }
3238
3239 /*
3240 * Notify the dplane when system/connected routes are deleted.
3241 */
3242 enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
3243 struct route_entry *re)
3244 {
3245 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3246
3247 /* Ignore this event unless a provider plugin has requested it. */
3248 if (!zdplane_info.dg_sys_route_notifs) {
3249 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
3250 goto done;
3251 }
3252
3253 if (rn == NULL || re == NULL)
3254 goto done;
3255
3256 ret = dplane_route_update_internal(rn, re, NULL,
3257 DPLANE_OP_SYS_ROUTE_DELETE);
3258
3259 done:
3260 return ret;
3261 }
3262
3263 /*
3264 * Update from an async notification, to bring other fibs up-to-date.
3265 */
3266 enum zebra_dplane_result
3267 dplane_route_notif_update(struct route_node *rn,
3268 struct route_entry *re,
3269 enum dplane_op_e op,
3270 struct zebra_dplane_ctx *ctx)
3271 {
3272 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3273 int ret = EINVAL;
3274 struct zebra_dplane_ctx *new_ctx = NULL;
3275 struct nexthop *nexthop;
3276 struct nexthop_group *nhg;
3277
3278 if (rn == NULL || re == NULL)
3279 goto done;
3280
3281 new_ctx = dplane_ctx_alloc();
3282 if (new_ctx == NULL)
3283 goto done;
3284
3285 /* Init context with info from zebra data structs */
3286 dplane_ctx_route_init(new_ctx, op, rn, re);
3287
3288 /* For add/update, need to adjust the nexthops so that we match
3289 * the notification state, which may not be the route-entry/RIB
3290 * state.
3291 */
3292 if (op == DPLANE_OP_ROUTE_UPDATE ||
3293 op == DPLANE_OP_ROUTE_INSTALL) {
3294
3295 nexthops_free(new_ctx->u.rinfo.zd_ng.nexthop);
3296 new_ctx->u.rinfo.zd_ng.nexthop = NULL;
3297
3298 nhg = rib_get_fib_nhg(re);
3299 if (nhg && nhg->nexthop)
3300 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
3301 nhg->nexthop, NULL);
3302
3303 /* Check for installed backup nexthops also */
3304 nhg = rib_get_fib_backup_nhg(re);
3305 if (nhg && nhg->nexthop) {
3306 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
3307 nhg->nexthop, NULL);
3308 }
3309
3310 for (ALL_NEXTHOPS(new_ctx->u.rinfo.zd_ng, nexthop))
3311 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
3312
3313 }
3314
3315 /* Capture info about the source of the notification, in 'ctx' */
3316 dplane_ctx_set_notif_provider(new_ctx,
3317 dplane_ctx_get_notif_provider(ctx));
3318
3319 ret = dplane_update_enqueue(new_ctx);
3320
3321 done:
3322 if (ret == AOK)
3323 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3324 else if (new_ctx)
3325 dplane_ctx_free(&new_ctx);
3326
3327 return result;
3328 }
3329
3330 /*
3331 * Enqueue a nexthop add for the dataplane.
3332 */
3333 enum zebra_dplane_result dplane_nexthop_add(struct nhg_hash_entry *nhe)
3334 {
3335 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3336
3337 if (nhe)
3338 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_INSTALL);
3339 return ret;
3340 }
3341
3342 /*
3343 * Enqueue a nexthop update for the dataplane.
3344 *
3345 * Might not need this func since zebra's nexthop objects should be immutable?
3346 */
3347 enum zebra_dplane_result dplane_nexthop_update(struct nhg_hash_entry *nhe)
3348 {
3349 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3350
3351 if (nhe)
3352 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_UPDATE);
3353 return ret;
3354 }
3355
3356 /*
3357 * Enqueue a nexthop removal for the dataplane.
3358 */
3359 enum zebra_dplane_result dplane_nexthop_delete(struct nhg_hash_entry *nhe)
3360 {
3361 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
3362
3363 if (nhe)
3364 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_DELETE);
3365
3366 return ret;
3367 }
3368
3369 /*
3370 * Enqueue LSP add for the dataplane.
3371 */
3372 enum zebra_dplane_result dplane_lsp_add(struct zebra_lsp *lsp)
3373 {
3374 enum zebra_dplane_result ret =
3375 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
3376
3377 return ret;
3378 }
3379
3380 /*
3381 * Enqueue LSP update for the dataplane.
3382 */
3383 enum zebra_dplane_result dplane_lsp_update(struct zebra_lsp *lsp)
3384 {
3385 enum zebra_dplane_result ret =
3386 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
3387
3388 return ret;
3389 }
3390
3391 /*
3392 * Enqueue LSP delete for the dataplane.
3393 */
3394 enum zebra_dplane_result dplane_lsp_delete(struct zebra_lsp *lsp)
3395 {
3396 enum zebra_dplane_result ret =
3397 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
3398
3399 return ret;
3400 }
3401
3402 /* Update or un-install resulting from an async notification */
3403 enum zebra_dplane_result
3404 dplane_lsp_notif_update(struct zebra_lsp *lsp, enum dplane_op_e op,
3405 struct zebra_dplane_ctx *notif_ctx)
3406 {
3407 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3408 int ret = EINVAL;
3409 struct zebra_dplane_ctx *ctx = NULL;
3410 struct nhlfe_list_head *head;
3411 struct zebra_nhlfe *nhlfe, *new_nhlfe;
3412
3413 /* Obtain context block */
3414 ctx = dplane_ctx_alloc();
3415 if (ctx == NULL) {
3416 ret = ENOMEM;
3417 goto done;
3418 }
3419
3420 /* Copy info from zebra LSP */
3421 ret = dplane_ctx_lsp_init(ctx, op, lsp);
3422 if (ret != AOK)
3423 goto done;
3424
3425 /* Add any installed backup nhlfes */
3426 head = &(ctx->u.lsp.backup_nhlfe_list);
3427 frr_each(nhlfe_list, head, nhlfe) {
3428
3429 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) &&
3430 CHECK_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_FIB)) {
3431 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp),
3432 nhlfe->type,
3433 nhlfe->nexthop);
3434
3435 /* Need to copy flags too */
3436 new_nhlfe->flags = nhlfe->flags;
3437 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
3438 }
3439 }
3440
3441 /* Capture info about the source of the notification */
3442 dplane_ctx_set_notif_provider(
3443 ctx,
3444 dplane_ctx_get_notif_provider(notif_ctx));
3445
3446 ret = dplane_update_enqueue(ctx);
3447
3448 done:
3449 /* Update counter */
3450 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
3451 memory_order_relaxed);
3452
3453 if (ret == AOK)
3454 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3455 else {
3456 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
3457 memory_order_relaxed);
3458 if (ctx)
3459 dplane_ctx_free(&ctx);
3460 }
3461 return result;
3462 }
3463
3464 /*
3465 * Enqueue pseudowire install for the dataplane.
3466 */
3467 enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
3468 {
3469 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
3470 }
3471
3472 /*
3473 * Enqueue pseudowire un-install for the dataplane.
3474 */
3475 enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
3476 {
3477 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
3478 }
3479
3480 /*
3481 * Common internal LSP update utility
3482 */
3483 static enum zebra_dplane_result lsp_update_internal(struct zebra_lsp *lsp,
3484 enum dplane_op_e op)
3485 {
3486 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3487 int ret = EINVAL;
3488 struct zebra_dplane_ctx *ctx = NULL;
3489
3490 /* Obtain context block */
3491 ctx = dplane_ctx_alloc();
3492
3493 ret = dplane_ctx_lsp_init(ctx, op, lsp);
3494 if (ret != AOK)
3495 goto done;
3496
3497 ret = dplane_update_enqueue(ctx);
3498
3499 done:
3500 /* Update counter */
3501 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
3502 memory_order_relaxed);
3503
3504 if (ret == AOK)
3505 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3506 else {
3507 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
3508 memory_order_relaxed);
3509 dplane_ctx_free(&ctx);
3510 }
3511
3512 return result;
3513 }
3514
3515 /*
3516 * Internal, common handler for pseudowire updates.
3517 */
3518 static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
3519 enum dplane_op_e op)
3520 {
3521 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3522 int ret;
3523 struct zebra_dplane_ctx *ctx = NULL;
3524
3525 ctx = dplane_ctx_alloc();
3526
3527 ret = dplane_ctx_pw_init(ctx, op, pw);
3528 if (ret != AOK)
3529 goto done;
3530
3531 ret = dplane_update_enqueue(ctx);
3532
3533 done:
3534 /* Update counter */
3535 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
3536 memory_order_relaxed);
3537
3538 if (ret == AOK)
3539 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3540 else {
3541 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
3542 memory_order_relaxed);
3543 dplane_ctx_free(&ctx);
3544 }
3545
3546 return result;
3547 }
3548
3549 /*
3550 * Enqueue access br_port update.
3551 */
3552 enum zebra_dplane_result
3553 dplane_br_port_update(const struct interface *ifp, bool non_df,
3554 uint32_t sph_filter_cnt,
3555 const struct in_addr *sph_filters, uint32_t backup_nhg_id)
3556 {
3557 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3558 uint32_t flags = 0;
3559 int ret;
3560 struct zebra_dplane_ctx *ctx = NULL;
3561 struct zebra_ns *zns;
3562 enum dplane_op_e op = DPLANE_OP_BR_PORT_UPDATE;
3563
3564 if (non_df)
3565 flags |= DPLANE_BR_PORT_NON_DF;
3566
3567 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL || IS_ZEBRA_DEBUG_EVPN_MH_ES) {
3568 uint32_t i;
3569 char vtep_str[ES_VTEP_LIST_STR_SZ];
3570
3571 vtep_str[0] = '\0';
3572 for (i = 0; i < sph_filter_cnt; ++i) {
3573 snprintfrr(vtep_str + strlen(vtep_str),
3574 sizeof(vtep_str) - strlen(vtep_str), "%pI4 ",
3575 &sph_filters[i]);
3576 }
3577 zlog_debug(
3578 "init br_port ctx %s: ifp %s, flags 0x%x backup_nhg 0x%x sph %s",
3579 dplane_op2str(op), ifp->name, flags, backup_nhg_id,
3580 vtep_str);
3581 }
3582
3583 ctx = dplane_ctx_alloc();
3584
3585 ctx->zd_op = op;
3586 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3587 ctx->zd_vrf_id = ifp->vrf->vrf_id;
3588
3589 zns = zebra_ns_lookup(ifp->vrf->vrf_id);
3590 dplane_ctx_ns_init(ctx, zns, false);
3591
3592 ctx->zd_ifindex = ifp->ifindex;
3593 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3594
3595 /* Init the br-port-specific data area */
3596 memset(&ctx->u.br_port, 0, sizeof(ctx->u.br_port));
3597
3598 ctx->u.br_port.flags = flags;
3599 ctx->u.br_port.backup_nhg_id = backup_nhg_id;
3600 ctx->u.br_port.sph_filter_cnt = sph_filter_cnt;
3601 memcpy(ctx->u.br_port.sph_filters, sph_filters,
3602 sizeof(ctx->u.br_port.sph_filters[0]) * sph_filter_cnt);
3603
3604 /* Enqueue for processing on the dplane pthread */
3605 ret = dplane_update_enqueue(ctx);
3606
3607 /* Increment counter */
3608 atomic_fetch_add_explicit(&zdplane_info.dg_br_port_in, 1,
3609 memory_order_relaxed);
3610
3611 if (ret == AOK) {
3612 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3613 } else {
3614 /* Error counter */
3615 atomic_fetch_add_explicit(&zdplane_info.dg_br_port_errors, 1,
3616 memory_order_relaxed);
3617 dplane_ctx_free(&ctx);
3618 }
3619
3620 return result;
3621 }
3622
3623 /*
3624 * Enqueue interface address add for the dataplane.
3625 */
3626 enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
3627 const struct connected *ifc)
3628 {
3629 #if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
3630 /* Extra checks for this OS path. */
3631
3632 /* Don't configure PtP addresses on broadcast ifs or reverse */
3633 if (!(ifp->flags & IFF_POINTOPOINT) != !CONNECTED_PEER(ifc)) {
3634 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_DPLANE)
3635 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
3636
3637 return ZEBRA_DPLANE_REQUEST_FAILURE;
3638 }
3639 #endif
3640
3641 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_INSTALL);
3642 }
3643
3644 /*
3645 * Enqueue interface address remove/uninstall for the dataplane.
3646 */
3647 enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
3648 const struct connected *ifc)
3649 {
3650 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_UNINSTALL);
3651 }
3652
3653 static enum zebra_dplane_result intf_addr_update_internal(
3654 const struct interface *ifp, const struct connected *ifc,
3655 enum dplane_op_e op)
3656 {
3657 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3658 int ret = EINVAL;
3659 struct zebra_dplane_ctx *ctx = NULL;
3660 struct zebra_ns *zns;
3661
3662 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3663 zlog_debug("init intf ctx %s: idx %d, addr %u:%pFX",
3664 dplane_op2str(op), ifp->ifindex, ifp->vrf->vrf_id,
3665 ifc->address);
3666
3667 ctx = dplane_ctx_alloc();
3668
3669 ctx->zd_op = op;
3670 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3671 ctx->zd_vrf_id = ifp->vrf->vrf_id;
3672
3673 zns = zebra_ns_lookup(ifp->vrf->vrf_id);
3674 dplane_ctx_ns_init(ctx, zns, false);
3675
3676 /* Init the interface-addr-specific area */
3677 memset(&ctx->u.intf, 0, sizeof(ctx->u.intf));
3678
3679 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3680 ctx->zd_ifindex = ifp->ifindex;
3681 ctx->u.intf.prefix = *(ifc->address);
3682
3683 if (if_is_broadcast(ifp))
3684 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
3685
3686 if (CONNECTED_PEER(ifc)) {
3687 ctx->u.intf.dest_prefix = *(ifc->destination);
3688 ctx->u.intf.flags |=
3689 (DPLANE_INTF_CONNECTED | DPLANE_INTF_HAS_DEST);
3690 }
3691
3692 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
3693 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
3694
3695 if (ifc->label) {
3696 size_t len;
3697
3698 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
3699
3700 /* Use embedded buffer if it's adequate; else allocate. */
3701 len = strlen(ifc->label);
3702
3703 if (len < sizeof(ctx->u.intf.label_buf)) {
3704 strlcpy(ctx->u.intf.label_buf, ifc->label,
3705 sizeof(ctx->u.intf.label_buf));
3706 ctx->u.intf.label = ctx->u.intf.label_buf;
3707 } else {
3708 ctx->u.intf.label = strdup(ifc->label);
3709 }
3710 }
3711
3712 ret = dplane_update_enqueue(ctx);
3713
3714 /* Increment counter */
3715 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addrs_in, 1,
3716 memory_order_relaxed);
3717
3718 if (ret == AOK)
3719 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3720 else {
3721 /* Error counter */
3722 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
3723 1, memory_order_relaxed);
3724 dplane_ctx_free(&ctx);
3725 }
3726
3727 return result;
3728 }
3729
3730 /*
3731 * Enqueue vxlan/evpn mac add (or update).
3732 */
3733 enum zebra_dplane_result dplane_rem_mac_add(const struct interface *ifp,
3734 const struct interface *bridge_ifp,
3735 vlanid_t vid,
3736 const struct ethaddr *mac,
3737 struct in_addr vtep_ip,
3738 bool sticky,
3739 uint32_t nhg_id,
3740 bool was_static)
3741 {
3742 enum zebra_dplane_result result;
3743 uint32_t update_flags = 0;
3744
3745 update_flags |= DPLANE_MAC_REMOTE;
3746 if (was_static)
3747 update_flags |= DPLANE_MAC_WAS_STATIC;
3748
3749 /* Use common helper api */
3750 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
3751 vid, mac, vtep_ip, sticky, nhg_id, update_flags);
3752 return result;
3753 }
3754
3755 /*
3756 * Enqueue vxlan/evpn mac delete.
3757 */
3758 enum zebra_dplane_result dplane_rem_mac_del(const struct interface *ifp,
3759 const struct interface *bridge_ifp,
3760 vlanid_t vid,
3761 const struct ethaddr *mac,
3762 struct in_addr vtep_ip)
3763 {
3764 enum zebra_dplane_result result;
3765 uint32_t update_flags = 0;
3766
3767 update_flags |= DPLANE_MAC_REMOTE;
3768
3769 /* Use common helper api */
3770 result = mac_update_common(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp,
3771 vid, mac, vtep_ip, false, 0, update_flags);
3772 return result;
3773 }
3774
3775 /*
3776 * API to configure link local with either MAC address or IP information
3777 */
3778 enum zebra_dplane_result dplane_neigh_ip_update(enum dplane_op_e op,
3779 const struct interface *ifp,
3780 struct ipaddr *link_ip,
3781 struct ipaddr *ip,
3782 uint32_t ndm_state, int protocol)
3783 {
3784 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3785 uint16_t state = 0;
3786 uint32_t update_flags;
3787
3788 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3789 zlog_debug("%s: init link ctx %s: ifp %s, link_ip %pIA ip %pIA",
3790 __func__, dplane_op2str(op), ifp->name, link_ip, ip);
3791
3792 if (ndm_state == ZEBRA_NEIGH_STATE_REACHABLE)
3793 state = DPLANE_NUD_REACHABLE;
3794 else if (ndm_state == ZEBRA_NEIGH_STATE_FAILED)
3795 state = DPLANE_NUD_FAILED;
3796
3797 update_flags = DPLANE_NEIGH_NO_EXTENSION;
3798
3799 result = neigh_update_internal(op, ifp, (const void *)link_ip,
3800 ipaddr_family(link_ip), ip, 0, state,
3801 update_flags, protocol);
3802
3803 return result;
3804 }
3805
3806 /*
3807 * Enqueue local mac add (or update).
3808 */
3809 enum zebra_dplane_result dplane_local_mac_add(const struct interface *ifp,
3810 const struct interface *bridge_ifp,
3811 vlanid_t vid,
3812 const struct ethaddr *mac,
3813 bool sticky,
3814 uint32_t set_static,
3815 uint32_t set_inactive)
3816 {
3817 enum zebra_dplane_result result;
3818 uint32_t update_flags = 0;
3819 struct in_addr vtep_ip;
3820
3821 if (set_static)
3822 update_flags |= DPLANE_MAC_SET_STATIC;
3823
3824 if (set_inactive)
3825 update_flags |= DPLANE_MAC_SET_INACTIVE;
3826
3827 vtep_ip.s_addr = 0;
3828
3829 /* Use common helper api */
3830 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
3831 vid, mac, vtep_ip, sticky, 0,
3832 update_flags);
3833 return result;
3834 }
3835
3836 /*
3837 * Enqueue local mac del
3838 */
3839 enum zebra_dplane_result
3840 dplane_local_mac_del(const struct interface *ifp,
3841 const struct interface *bridge_ifp, vlanid_t vid,
3842 const struct ethaddr *mac)
3843 {
3844 enum zebra_dplane_result result;
3845 struct in_addr vtep_ip;
3846
3847 vtep_ip.s_addr = 0;
3848
3849 /* Use common helper api */
3850 result = mac_update_common(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp, vid,
3851 mac, vtep_ip, false, 0, 0);
3852 return result;
3853 }
3854 /*
3855 * Public api to init an empty context - either newly-allocated or
3856 * reset/cleared - for a MAC update.
3857 */
3858 void dplane_mac_init(struct zebra_dplane_ctx *ctx,
3859 const struct interface *ifp,
3860 const struct interface *br_ifp,
3861 vlanid_t vid,
3862 const struct ethaddr *mac,
3863 struct in_addr vtep_ip,
3864 bool sticky,
3865 uint32_t nhg_id,
3866 uint32_t update_flags)
3867 {
3868 struct zebra_ns *zns;
3869
3870 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3871 ctx->zd_vrf_id = ifp->vrf->vrf_id;
3872
3873 zns = zebra_ns_lookup(ifp->vrf->vrf_id);
3874 dplane_ctx_ns_init(ctx, zns, false);
3875
3876 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3877 ctx->zd_ifindex = ifp->ifindex;
3878
3879 /* Init the mac-specific data area */
3880 memset(&ctx->u.macinfo, 0, sizeof(ctx->u.macinfo));
3881
3882 ctx->u.macinfo.br_ifindex = br_ifp->ifindex;
3883 ctx->u.macinfo.vtep_ip = vtep_ip;
3884 ctx->u.macinfo.mac = *mac;
3885 ctx->u.macinfo.vid = vid;
3886 ctx->u.macinfo.is_sticky = sticky;
3887 ctx->u.macinfo.nhg_id = nhg_id;
3888 ctx->u.macinfo.update_flags = update_flags;
3889 }
3890
3891 /*
3892 * Common helper api for MAC address/vxlan updates
3893 */
3894 static enum zebra_dplane_result
3895 mac_update_common(enum dplane_op_e op,
3896 const struct interface *ifp,
3897 const struct interface *br_ifp,
3898 vlanid_t vid,
3899 const struct ethaddr *mac,
3900 struct in_addr vtep_ip,
3901 bool sticky,
3902 uint32_t nhg_id,
3903 uint32_t update_flags)
3904 {
3905 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3906 int ret;
3907 struct zebra_dplane_ctx *ctx = NULL;
3908
3909 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3910 zlog_debug("init mac ctx %s: mac %pEA, ifp %s, vtep %pI4",
3911 dplane_op2str(op), mac, ifp->name, &vtep_ip);
3912
3913 ctx = dplane_ctx_alloc();
3914 ctx->zd_op = op;
3915
3916 /* Common init for the ctx */
3917 dplane_mac_init(ctx, ifp, br_ifp, vid, mac, vtep_ip, sticky,
3918 nhg_id, update_flags);
3919
3920 /* Enqueue for processing on the dplane pthread */
3921 ret = dplane_update_enqueue(ctx);
3922
3923 /* Increment counter */
3924 atomic_fetch_add_explicit(&zdplane_info.dg_macs_in, 1,
3925 memory_order_relaxed);
3926
3927 if (ret == AOK)
3928 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3929 else {
3930 /* Error counter */
3931 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors, 1,
3932 memory_order_relaxed);
3933 dplane_ctx_free(&ctx);
3934 }
3935
3936 return result;
3937 }
3938
3939 /*
3940 * Enqueue evpn neighbor add for the dataplane.
3941 */
3942 enum zebra_dplane_result dplane_rem_neigh_add(const struct interface *ifp,
3943 const struct ipaddr *ip,
3944 const struct ethaddr *mac,
3945 uint32_t flags, bool was_static)
3946 {
3947 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3948 uint32_t update_flags = 0;
3949
3950 update_flags |= DPLANE_NEIGH_REMOTE;
3951
3952 if (was_static)
3953 update_flags |= DPLANE_NEIGH_WAS_STATIC;
3954
3955 result = neigh_update_internal(
3956 DPLANE_OP_NEIGH_INSTALL, ifp, (const void *)mac, AF_ETHERNET,
3957 ip, flags, DPLANE_NUD_NOARP, update_flags, 0);
3958
3959 return result;
3960 }
3961
3962 /*
3963 * Enqueue local neighbor add for the dataplane.
3964 */
3965 enum zebra_dplane_result dplane_local_neigh_add(const struct interface *ifp,
3966 const struct ipaddr *ip,
3967 const struct ethaddr *mac,
3968 bool set_router, bool set_static,
3969 bool set_inactive)
3970 {
3971 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3972 uint32_t update_flags = 0;
3973 uint32_t ntf = 0;
3974 uint16_t state;
3975
3976 if (set_static)
3977 update_flags |= DPLANE_NEIGH_SET_STATIC;
3978
3979 if (set_inactive) {
3980 update_flags |= DPLANE_NEIGH_SET_INACTIVE;
3981 state = DPLANE_NUD_STALE;
3982 } else {
3983 state = DPLANE_NUD_REACHABLE;
3984 }
3985
3986 if (set_router)
3987 ntf |= DPLANE_NTF_ROUTER;
3988
3989 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL, ifp,
3990 (const void *)mac, AF_ETHERNET, ip, ntf,
3991 state, update_flags, 0);
3992
3993 return result;
3994 }
3995
3996 /*
3997 * Enqueue evpn neighbor delete for the dataplane.
3998 */
3999 enum zebra_dplane_result dplane_rem_neigh_delete(const struct interface *ifp,
4000 const struct ipaddr *ip)
4001 {
4002 enum zebra_dplane_result result;
4003 uint32_t update_flags = 0;
4004
4005 update_flags |= DPLANE_NEIGH_REMOTE;
4006
4007 result = neigh_update_internal(DPLANE_OP_NEIGH_DELETE, ifp, NULL,
4008 AF_ETHERNET, ip, 0, 0, update_flags, 0);
4009
4010 return result;
4011 }
4012
4013 /*
4014 * Enqueue evpn VTEP add for the dataplane.
4015 */
4016 enum zebra_dplane_result dplane_vtep_add(const struct interface *ifp,
4017 const struct in_addr *ip,
4018 vni_t vni)
4019 {
4020 enum zebra_dplane_result result;
4021 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
4022 struct ipaddr addr;
4023
4024 if (IS_ZEBRA_DEBUG_VXLAN)
4025 zlog_debug("Install %pI4 into flood list for VNI %u intf %s(%u)",
4026 ip, vni, ifp->name, ifp->ifindex);
4027
4028 SET_IPADDR_V4(&addr);
4029 addr.ipaddr_v4 = *ip;
4030
4031 result = neigh_update_internal(DPLANE_OP_VTEP_ADD, ifp, &mac,
4032 AF_ETHERNET, &addr, 0, 0, 0, 0);
4033
4034 return result;
4035 }
4036
4037 /*
4038 * Enqueue evpn VTEP add for the dataplane.
4039 */
4040 enum zebra_dplane_result dplane_vtep_delete(const struct interface *ifp,
4041 const struct in_addr *ip,
4042 vni_t vni)
4043 {
4044 enum zebra_dplane_result result;
4045 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
4046 struct ipaddr addr;
4047
4048 if (IS_ZEBRA_DEBUG_VXLAN)
4049 zlog_debug(
4050 "Uninstall %pI4 from flood list for VNI %u intf %s(%u)",
4051 ip, vni, ifp->name, ifp->ifindex);
4052
4053 SET_IPADDR_V4(&addr);
4054 addr.ipaddr_v4 = *ip;
4055
4056 result = neigh_update_internal(DPLANE_OP_VTEP_DELETE, ifp,
4057 (const void *)&mac, AF_ETHERNET, &addr,
4058 0, 0, 0, 0);
4059
4060 return result;
4061 }
4062
4063 enum zebra_dplane_result dplane_neigh_discover(const struct interface *ifp,
4064 const struct ipaddr *ip)
4065 {
4066 enum zebra_dplane_result result;
4067
4068 result = neigh_update_internal(DPLANE_OP_NEIGH_DISCOVER, ifp, NULL,
4069 AF_ETHERNET, ip, DPLANE_NTF_USE,
4070 DPLANE_NUD_INCOMPLETE, 0, 0);
4071
4072 return result;
4073 }
4074
4075 enum zebra_dplane_result dplane_neigh_table_update(const struct interface *ifp,
4076 const uint8_t family,
4077 const uint32_t app_probes,
4078 const uint32_t ucast_probes,
4079 const uint32_t mcast_probes)
4080 {
4081 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4082 int ret;
4083 struct zebra_dplane_ctx *ctx = NULL;
4084 struct zebra_ns *zns;
4085 enum dplane_op_e op = DPLANE_OP_NEIGH_TABLE_UPDATE;
4086
4087 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
4088 zlog_debug("set neigh ctx %s: ifp %s, family %s",
4089 dplane_op2str(op), ifp->name, family2str(family));
4090 }
4091
4092 ctx = dplane_ctx_alloc();
4093
4094 ctx->zd_op = op;
4095 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
4096 ctx->zd_vrf_id = ifp->vrf->vrf_id;
4097
4098 zns = zebra_ns_lookup(ifp->vrf->vrf_id);
4099 dplane_ctx_ns_init(ctx, zns, false);
4100
4101 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
4102 ctx->zd_ifindex = ifp->ifindex;
4103
4104 /* Init the neighbor-specific data area */
4105 memset(&ctx->u.neightable, 0, sizeof(ctx->u.neightable));
4106
4107 ctx->u.neightable.family = family;
4108 ctx->u.neightable.app_probes = app_probes;
4109 ctx->u.neightable.ucast_probes = ucast_probes;
4110 ctx->u.neightable.mcast_probes = mcast_probes;
4111
4112 /* Enqueue for processing on the dplane pthread */
4113 ret = dplane_update_enqueue(ctx);
4114
4115 /* Increment counter */
4116 atomic_fetch_add_explicit(&zdplane_info.dg_neightable_in, 1,
4117 memory_order_relaxed);
4118
4119 if (ret == AOK)
4120 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4121 else {
4122 /* Error counter */
4123 atomic_fetch_add_explicit(&zdplane_info.dg_neightable_errors, 1,
4124 memory_order_relaxed);
4125 dplane_ctx_free(&ctx);
4126 }
4127
4128 return result;
4129 }
4130
4131 /*
4132 * Common helper api for neighbor updates
4133 */
4134 static enum zebra_dplane_result
4135 neigh_update_internal(enum dplane_op_e op, const struct interface *ifp,
4136 const void *link, const int link_family,
4137 const struct ipaddr *ip, uint32_t flags, uint16_t state,
4138 uint32_t update_flags, int protocol)
4139 {
4140 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4141 int ret;
4142 struct zebra_dplane_ctx *ctx = NULL;
4143 struct zebra_ns *zns;
4144 const struct ethaddr *mac = NULL;
4145 const struct ipaddr *link_ip = NULL;
4146
4147 if (link_family == AF_ETHERNET)
4148 mac = (const struct ethaddr *)link;
4149 else
4150 link_ip = (const struct ipaddr *)link;
4151
4152 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
4153 char buf1[PREFIX_STRLEN];
4154
4155 buf1[0] = '\0';
4156 if (link_family == AF_ETHERNET)
4157 prefix_mac2str(mac, buf1, sizeof(buf1));
4158 else
4159 ipaddr2str(link_ip, buf1, sizeof(buf1));
4160 zlog_debug("init neigh ctx %s: ifp %s, %s %s, ip %pIA",
4161 dplane_op2str(op), ifp->name,
4162 link_family == AF_ETHERNET ? "mac " : "link ",
4163 buf1, ip);
4164 }
4165
4166 ctx = dplane_ctx_alloc();
4167
4168 ctx->zd_op = op;
4169 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
4170 ctx->zd_vrf_id = ifp->vrf->vrf_id;
4171 dplane_ctx_set_type(ctx, protocol);
4172
4173 zns = zebra_ns_lookup(ifp->vrf->vrf_id);
4174 dplane_ctx_ns_init(ctx, zns, false);
4175
4176 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
4177 ctx->zd_ifindex = ifp->ifindex;
4178
4179 /* Init the neighbor-specific data area */
4180 memset(&ctx->u.neigh, 0, sizeof(ctx->u.neigh));
4181
4182 ctx->u.neigh.ip_addr = *ip;
4183 if (mac)
4184 ctx->u.neigh.link.mac = *mac;
4185 else if (link_ip)
4186 ctx->u.neigh.link.ip_addr = *link_ip;
4187
4188 ctx->u.neigh.flags = flags;
4189 ctx->u.neigh.state = state;
4190 ctx->u.neigh.update_flags = update_flags;
4191
4192 /* Enqueue for processing on the dplane pthread */
4193 ret = dplane_update_enqueue(ctx);
4194
4195 /* Increment counter */
4196 atomic_fetch_add_explicit(&zdplane_info.dg_neighs_in, 1,
4197 memory_order_relaxed);
4198
4199 if (ret == AOK)
4200 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4201 else {
4202 /* Error counter */
4203 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors, 1,
4204 memory_order_relaxed);
4205 dplane_ctx_free(&ctx);
4206 }
4207
4208 return result;
4209 }
4210
4211 /*
4212 * Common helper api for PBR rule updates
4213 */
4214 static enum zebra_dplane_result
4215 rule_update_internal(enum dplane_op_e op, struct zebra_pbr_rule *new_rule,
4216 struct zebra_pbr_rule *old_rule)
4217 {
4218 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4219 struct zebra_dplane_ctx *ctx;
4220 int ret;
4221
4222 ctx = dplane_ctx_alloc();
4223
4224 ret = dplane_ctx_rule_init(ctx, op, new_rule, old_rule);
4225 if (ret != AOK)
4226 goto done;
4227
4228 ret = dplane_update_enqueue(ctx);
4229
4230 done:
4231 atomic_fetch_add_explicit(&zdplane_info.dg_rules_in, 1,
4232 memory_order_relaxed);
4233
4234 if (ret == AOK)
4235 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4236 else {
4237 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors, 1,
4238 memory_order_relaxed);
4239 dplane_ctx_free(&ctx);
4240 }
4241
4242 return result;
4243 }
4244
4245 enum zebra_dplane_result dplane_pbr_rule_add(struct zebra_pbr_rule *rule)
4246 {
4247 return rule_update_internal(DPLANE_OP_RULE_ADD, rule, NULL);
4248 }
4249
4250 enum zebra_dplane_result dplane_pbr_rule_delete(struct zebra_pbr_rule *rule)
4251 {
4252 return rule_update_internal(DPLANE_OP_RULE_DELETE, rule, NULL);
4253 }
4254
4255 enum zebra_dplane_result dplane_pbr_rule_update(struct zebra_pbr_rule *old_rule,
4256 struct zebra_pbr_rule *new_rule)
4257 {
4258 return rule_update_internal(DPLANE_OP_RULE_UPDATE, new_rule, old_rule);
4259 }
4260 /*
4261 * Common helper api for iptable updates
4262 */
4263 static enum zebra_dplane_result
4264 iptable_update_internal(enum dplane_op_e op, struct zebra_pbr_iptable *iptable)
4265 {
4266 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4267 struct zebra_dplane_ctx *ctx;
4268 int ret;
4269
4270 ctx = dplane_ctx_alloc();
4271
4272 ret = dplane_ctx_iptable_init(ctx, op, iptable);
4273 if (ret != AOK)
4274 goto done;
4275
4276 ret = dplane_update_enqueue(ctx);
4277
4278 done:
4279 atomic_fetch_add_explicit(&zdplane_info.dg_iptable_in, 1,
4280 memory_order_relaxed);
4281
4282 if (ret == AOK)
4283 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4284 else {
4285 atomic_fetch_add_explicit(&zdplane_info.dg_iptable_errors, 1,
4286 memory_order_relaxed);
4287 dplane_ctx_free(&ctx);
4288 }
4289
4290 return result;
4291 }
4292
4293 enum zebra_dplane_result
4294 dplane_pbr_iptable_add(struct zebra_pbr_iptable *iptable)
4295 {
4296 return iptable_update_internal(DPLANE_OP_IPTABLE_ADD, iptable);
4297 }
4298
4299 enum zebra_dplane_result
4300 dplane_pbr_iptable_delete(struct zebra_pbr_iptable *iptable)
4301 {
4302 return iptable_update_internal(DPLANE_OP_IPTABLE_DELETE, iptable);
4303 }
4304
4305 /*
4306 * Common helper api for ipset updates
4307 */
4308 static enum zebra_dplane_result
4309 ipset_update_internal(enum dplane_op_e op, struct zebra_pbr_ipset *ipset)
4310 {
4311 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4312 struct zebra_dplane_ctx *ctx;
4313 int ret;
4314
4315 ctx = dplane_ctx_alloc();
4316
4317 ret = dplane_ctx_ipset_init(ctx, op, ipset);
4318 if (ret != AOK)
4319 goto done;
4320
4321 ret = dplane_update_enqueue(ctx);
4322
4323 done:
4324 atomic_fetch_add_explicit(&zdplane_info.dg_ipset_in, 1,
4325 memory_order_relaxed);
4326
4327 if (ret == AOK)
4328 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4329 else {
4330 atomic_fetch_add_explicit(&zdplane_info.dg_ipset_errors, 1,
4331 memory_order_relaxed);
4332 dplane_ctx_free(&ctx);
4333 }
4334
4335 return result;
4336 }
4337
4338 enum zebra_dplane_result dplane_pbr_ipset_add(struct zebra_pbr_ipset *ipset)
4339 {
4340 return ipset_update_internal(DPLANE_OP_IPSET_ADD, ipset);
4341 }
4342
4343 enum zebra_dplane_result dplane_pbr_ipset_delete(struct zebra_pbr_ipset *ipset)
4344 {
4345 return ipset_update_internal(DPLANE_OP_IPSET_DELETE, ipset);
4346 }
4347
4348 /*
4349 * Common helper api for ipset updates
4350 */
4351 static enum zebra_dplane_result
4352 ipset_entry_update_internal(enum dplane_op_e op,
4353 struct zebra_pbr_ipset_entry *ipset_entry)
4354 {
4355 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4356 struct zebra_dplane_ctx *ctx;
4357 int ret;
4358
4359 ctx = dplane_ctx_alloc();
4360
4361 ret = dplane_ctx_ipset_entry_init(ctx, op, ipset_entry);
4362 if (ret != AOK)
4363 goto done;
4364
4365 ret = dplane_update_enqueue(ctx);
4366
4367 done:
4368 atomic_fetch_add_explicit(&zdplane_info.dg_ipset_entry_in, 1,
4369 memory_order_relaxed);
4370
4371 if (ret == AOK)
4372 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4373 else {
4374 atomic_fetch_add_explicit(&zdplane_info.dg_ipset_entry_errors,
4375 1, memory_order_relaxed);
4376 dplane_ctx_free(&ctx);
4377 }
4378
4379 return result;
4380 }
4381
4382 enum zebra_dplane_result
4383 dplane_pbr_ipset_entry_add(struct zebra_pbr_ipset_entry *ipset)
4384 {
4385 return ipset_entry_update_internal(DPLANE_OP_IPSET_ENTRY_ADD, ipset);
4386 }
4387
4388 enum zebra_dplane_result
4389 dplane_pbr_ipset_entry_delete(struct zebra_pbr_ipset_entry *ipset)
4390 {
4391 return ipset_entry_update_internal(DPLANE_OP_IPSET_ENTRY_DELETE, ipset);
4392 }
4393
4394 /*
4395 * Common helper api for GRE set
4396 */
4397 enum zebra_dplane_result
4398 dplane_gre_set(struct interface *ifp, struct interface *ifp_link,
4399 unsigned int mtu, const struct zebra_l2info_gre *gre_info)
4400 {
4401 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
4402 struct zebra_dplane_ctx *ctx;
4403 enum dplane_op_e op = DPLANE_OP_GRE_SET;
4404 int ret;
4405 struct zebra_ns *zns;
4406
4407 ctx = dplane_ctx_alloc();
4408
4409 if (!ifp)
4410 return result;
4411
4412 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
4413 zlog_debug("init dplane ctx %s: if %s link %s%s",
4414 dplane_op2str(op), ifp->name,
4415 ifp_link ? "set" : "unset", ifp_link ?
4416 ifp_link->name : "");
4417 }
4418
4419 ctx->zd_op = op;
4420 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
4421 zns = zebra_ns_lookup(ifp->vrf->vrf_id);
4422 if (!zns)
4423 return result;
4424 dplane_ctx_ns_init(ctx, zns, false);
4425
4426 dplane_ctx_set_ifname(ctx, ifp->name);
4427 ctx->zd_vrf_id = ifp->vrf->vrf_id;
4428 ctx->zd_ifindex = ifp->ifindex;
4429 if (ifp_link)
4430 ctx->u.gre.link_ifindex = ifp_link->ifindex;
4431 else
4432 ctx->u.gre.link_ifindex = 0;
4433 if (gre_info)
4434 memcpy(&ctx->u.gre.info, gre_info, sizeof(ctx->u.gre.info));
4435 ctx->u.gre.mtu = mtu;
4436
4437 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
4438
4439 /* Enqueue context for processing */
4440 ret = dplane_update_enqueue(ctx);
4441
4442 /* Update counter */
4443 atomic_fetch_add_explicit(&zdplane_info.dg_gre_set_in, 1,
4444 memory_order_relaxed);
4445
4446 if (ret == AOK)
4447 result = ZEBRA_DPLANE_REQUEST_QUEUED;
4448 else {
4449 atomic_fetch_add_explicit(
4450 &zdplane_info.dg_gre_set_errors, 1,
4451 memory_order_relaxed);
4452 if (ctx)
4453 dplane_ctx_free(&ctx);
4454 result = ZEBRA_DPLANE_REQUEST_FAILURE;
4455 }
4456 return result;
4457 }
4458
4459 /*
4460 * Handler for 'show dplane'
4461 */
4462 int dplane_show_helper(struct vty *vty, bool detailed)
4463 {
4464 uint64_t queued, queue_max, limit, errs, incoming, yields,
4465 other_errs;
4466
4467 /* Using atomics because counters are being changed in different
4468 * pthread contexts.
4469 */
4470 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
4471 memory_order_relaxed);
4472 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
4473 memory_order_relaxed);
4474 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
4475 memory_order_relaxed);
4476 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
4477 memory_order_relaxed);
4478 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
4479 memory_order_relaxed);
4480 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
4481 memory_order_relaxed);
4482 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
4483 memory_order_relaxed);
4484
4485 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
4486 incoming);
4487 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
4488 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
4489 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
4490 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
4491 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
4492 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
4493
4494 incoming = atomic_load_explicit(&zdplane_info.dg_lsps_in,
4495 memory_order_relaxed);
4496 errs = atomic_load_explicit(&zdplane_info.dg_lsp_errors,
4497 memory_order_relaxed);
4498 vty_out(vty, "LSP updates: %"PRIu64"\n", incoming);
4499 vty_out(vty, "LSP update errors: %"PRIu64"\n", errs);
4500
4501 incoming = atomic_load_explicit(&zdplane_info.dg_pws_in,
4502 memory_order_relaxed);
4503 errs = atomic_load_explicit(&zdplane_info.dg_pw_errors,
4504 memory_order_relaxed);
4505 vty_out(vty, "PW updates: %"PRIu64"\n", incoming);
4506 vty_out(vty, "PW update errors: %"PRIu64"\n", errs);
4507
4508 incoming = atomic_load_explicit(&zdplane_info.dg_intf_addrs_in,
4509 memory_order_relaxed);
4510 errs = atomic_load_explicit(&zdplane_info.dg_intf_addr_errors,
4511 memory_order_relaxed);
4512 vty_out(vty, "Intf addr updates: %"PRIu64"\n", incoming);
4513 vty_out(vty, "Intf addr errors: %"PRIu64"\n", errs);
4514
4515 incoming = atomic_load_explicit(&zdplane_info.dg_macs_in,
4516 memory_order_relaxed);
4517 errs = atomic_load_explicit(&zdplane_info.dg_mac_errors,
4518 memory_order_relaxed);
4519 vty_out(vty, "EVPN MAC updates: %"PRIu64"\n", incoming);
4520 vty_out(vty, "EVPN MAC errors: %"PRIu64"\n", errs);
4521
4522 incoming = atomic_load_explicit(&zdplane_info.dg_neighs_in,
4523 memory_order_relaxed);
4524 errs = atomic_load_explicit(&zdplane_info.dg_neigh_errors,
4525 memory_order_relaxed);
4526 vty_out(vty, "EVPN neigh updates: %"PRIu64"\n", incoming);
4527 vty_out(vty, "EVPN neigh errors: %"PRIu64"\n", errs);
4528
4529 incoming = atomic_load_explicit(&zdplane_info.dg_rules_in,
4530 memory_order_relaxed);
4531 errs = atomic_load_explicit(&zdplane_info.dg_rule_errors,
4532 memory_order_relaxed);
4533 vty_out(vty, "Rule updates: %" PRIu64 "\n", incoming);
4534 vty_out(vty, "Rule errors: %" PRIu64 "\n", errs);
4535
4536 incoming = atomic_load_explicit(&zdplane_info.dg_br_port_in,
4537 memory_order_relaxed);
4538 errs = atomic_load_explicit(&zdplane_info.dg_br_port_errors,
4539 memory_order_relaxed);
4540 vty_out(vty, "Bridge port updates: %" PRIu64 "\n", incoming);
4541 vty_out(vty, "Bridge port errors: %" PRIu64 "\n", errs);
4542
4543 incoming = atomic_load_explicit(&zdplane_info.dg_iptable_in,
4544 memory_order_relaxed);
4545 errs = atomic_load_explicit(&zdplane_info.dg_iptable_errors,
4546 memory_order_relaxed);
4547 vty_out(vty, "IPtable updates: %" PRIu64 "\n", incoming);
4548 vty_out(vty, "IPtable errors: %" PRIu64 "\n", errs);
4549 incoming = atomic_load_explicit(&zdplane_info.dg_ipset_in,
4550 memory_order_relaxed);
4551 errs = atomic_load_explicit(&zdplane_info.dg_ipset_errors,
4552 memory_order_relaxed);
4553 vty_out(vty, "IPset updates: %" PRIu64 "\n", incoming);
4554 vty_out(vty, "IPset errors: %" PRIu64 "\n", errs);
4555 incoming = atomic_load_explicit(&zdplane_info.dg_ipset_entry_in,
4556 memory_order_relaxed);
4557 errs = atomic_load_explicit(&zdplane_info.dg_ipset_entry_errors,
4558 memory_order_relaxed);
4559 vty_out(vty, "IPset entry updates: %" PRIu64 "\n", incoming);
4560 vty_out(vty, "IPset entry errors: %" PRIu64 "\n", errs);
4561
4562 incoming = atomic_load_explicit(&zdplane_info.dg_neightable_in,
4563 memory_order_relaxed);
4564 errs = atomic_load_explicit(&zdplane_info.dg_neightable_errors,
4565 memory_order_relaxed);
4566 vty_out(vty, "Neighbor Table updates: %"PRIu64"\n", incoming);
4567 vty_out(vty, "Neighbor Table errors: %"PRIu64"\n", errs);
4568
4569 incoming = atomic_load_explicit(&zdplane_info.dg_gre_set_in,
4570 memory_order_relaxed);
4571 errs = atomic_load_explicit(&zdplane_info.dg_gre_set_errors,
4572 memory_order_relaxed);
4573 vty_out(vty, "GRE set updates: %"PRIu64"\n", incoming);
4574 vty_out(vty, "GRE set errors: %"PRIu64"\n", errs);
4575 return CMD_SUCCESS;
4576 }
4577
4578 /*
4579 * Handler for 'show dplane providers'
4580 */
4581 int dplane_show_provs_helper(struct vty *vty, bool detailed)
4582 {
4583 struct zebra_dplane_provider *prov;
4584 uint64_t in, in_q, in_max, out, out_q, out_max;
4585
4586 vty_out(vty, "Zebra dataplane providers:\n");
4587
4588 DPLANE_LOCK();
4589 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4590 DPLANE_UNLOCK();
4591
4592 /* Show counters, useful info from each registered provider */
4593 while (prov) {
4594
4595 in = atomic_load_explicit(&prov->dp_in_counter,
4596 memory_order_relaxed);
4597 in_q = atomic_load_explicit(&prov->dp_in_queued,
4598 memory_order_relaxed);
4599 in_max = atomic_load_explicit(&prov->dp_in_max,
4600 memory_order_relaxed);
4601 out = atomic_load_explicit(&prov->dp_out_counter,
4602 memory_order_relaxed);
4603 out_q = atomic_load_explicit(&prov->dp_out_queued,
4604 memory_order_relaxed);
4605 out_max = atomic_load_explicit(&prov->dp_out_max,
4606 memory_order_relaxed);
4607
4608 vty_out(vty, "%s (%u): in: %"PRIu64", q: %"PRIu64", q_max: %"PRIu64", out: %"PRIu64", q: %"PRIu64", q_max: %"PRIu64"\n",
4609 prov->dp_name, prov->dp_id, in, in_q, in_max,
4610 out, out_q, out_max);
4611
4612 DPLANE_LOCK();
4613 prov = TAILQ_NEXT(prov, dp_prov_link);
4614 DPLANE_UNLOCK();
4615 }
4616
4617 return CMD_SUCCESS;
4618 }
4619
4620 /*
4621 * Helper for 'show run' etc.
4622 */
4623 int dplane_config_write_helper(struct vty *vty)
4624 {
4625 if (zdplane_info.dg_max_queued_updates != DPLANE_DEFAULT_MAX_QUEUED)
4626 vty_out(vty, "zebra dplane limit %u\n",
4627 zdplane_info.dg_max_queued_updates);
4628
4629 return 0;
4630 }
4631
4632 /*
4633 * Provider registration
4634 */
4635 int dplane_provider_register(const char *name,
4636 enum dplane_provider_prio prio,
4637 int flags,
4638 int (*start_fp)(struct zebra_dplane_provider *),
4639 int (*fp)(struct zebra_dplane_provider *),
4640 int (*fini_fp)(struct zebra_dplane_provider *,
4641 bool early),
4642 void *data,
4643 struct zebra_dplane_provider **prov_p)
4644 {
4645 int ret = 0;
4646 struct zebra_dplane_provider *p = NULL, *last;
4647
4648 /* Validate */
4649 if (fp == NULL) {
4650 ret = EINVAL;
4651 goto done;
4652 }
4653
4654 if (prio <= DPLANE_PRIO_NONE ||
4655 prio > DPLANE_PRIO_LAST) {
4656 ret = EINVAL;
4657 goto done;
4658 }
4659
4660 /* Allocate and init new provider struct */
4661 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
4662
4663 pthread_mutex_init(&(p->dp_mutex), NULL);
4664 TAILQ_INIT(&(p->dp_ctx_in_q));
4665 TAILQ_INIT(&(p->dp_ctx_out_q));
4666
4667 p->dp_flags = flags;
4668 p->dp_priority = prio;
4669 p->dp_fp = fp;
4670 p->dp_start = start_fp;
4671 p->dp_fini = fini_fp;
4672 p->dp_data = data;
4673
4674 /* Lock - the dplane pthread may be running */
4675 DPLANE_LOCK();
4676
4677 p->dp_id = ++zdplane_info.dg_provider_id;
4678
4679 if (name)
4680 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
4681 else
4682 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
4683 "provider-%u", p->dp_id);
4684
4685 /* Insert into list ordered by priority */
4686 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
4687 if (last->dp_priority > p->dp_priority)
4688 break;
4689 }
4690
4691 if (last)
4692 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
4693 else
4694 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
4695 dp_prov_link);
4696
4697 /* And unlock */
4698 DPLANE_UNLOCK();
4699
4700 if (IS_ZEBRA_DEBUG_DPLANE)
4701 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
4702 p->dp_name, p->dp_id, p->dp_priority);
4703
4704 done:
4705 if (prov_p)
4706 *prov_p = p;
4707
4708 return ret;
4709 }
4710
4711 /* Accessors for provider attributes */
4712 const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
4713 {
4714 return prov->dp_name;
4715 }
4716
4717 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
4718 {
4719 return prov->dp_id;
4720 }
4721
4722 void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
4723 {
4724 return prov->dp_data;
4725 }
4726
4727 int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
4728 {
4729 return zdplane_info.dg_updates_per_cycle;
4730 }
4731
4732 /* Lock/unlock a provider's mutex - iff the provider was registered with
4733 * the THREADED flag.
4734 */
4735 void dplane_provider_lock(struct zebra_dplane_provider *prov)
4736 {
4737 if (dplane_provider_is_threaded(prov))
4738 DPLANE_PROV_LOCK(prov);
4739 }
4740
4741 void dplane_provider_unlock(struct zebra_dplane_provider *prov)
4742 {
4743 if (dplane_provider_is_threaded(prov))
4744 DPLANE_PROV_UNLOCK(prov);
4745 }
4746
4747 /*
4748 * Dequeue and maintain associated counter
4749 */
4750 struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
4751 struct zebra_dplane_provider *prov)
4752 {
4753 struct zebra_dplane_ctx *ctx = NULL;
4754
4755 dplane_provider_lock(prov);
4756
4757 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
4758 if (ctx) {
4759 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
4760
4761 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
4762 memory_order_relaxed);
4763 }
4764
4765 dplane_provider_unlock(prov);
4766
4767 return ctx;
4768 }
4769
4770 /*
4771 * Dequeue work to a list, return count
4772 */
4773 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
4774 struct dplane_ctx_q *listp)
4775 {
4776 int limit, ret;
4777 struct zebra_dplane_ctx *ctx;
4778
4779 limit = zdplane_info.dg_updates_per_cycle;
4780
4781 dplane_provider_lock(prov);
4782
4783 for (ret = 0; ret < limit; ret++) {
4784 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
4785 if (ctx) {
4786 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
4787
4788 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
4789 } else {
4790 break;
4791 }
4792 }
4793
4794 if (ret > 0)
4795 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
4796 memory_order_relaxed);
4797
4798 dplane_provider_unlock(prov);
4799
4800 return ret;
4801 }
4802
4803 uint32_t dplane_provider_out_ctx_queue_len(struct zebra_dplane_provider *prov)
4804 {
4805 return atomic_load_explicit(&(prov->dp_out_counter),
4806 memory_order_relaxed);
4807 }
4808
4809 /*
4810 * Enqueue and maintain associated counter
4811 */
4812 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
4813 struct zebra_dplane_ctx *ctx)
4814 {
4815 uint64_t curr, high;
4816
4817 dplane_provider_lock(prov);
4818
4819 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
4820 zd_q_entries);
4821
4822 /* Maintain out-queue counters */
4823 atomic_fetch_add_explicit(&(prov->dp_out_queued), 1,
4824 memory_order_relaxed);
4825 curr = atomic_load_explicit(&prov->dp_out_queued,
4826 memory_order_relaxed);
4827 high = atomic_load_explicit(&prov->dp_out_max,
4828 memory_order_relaxed);
4829 if (curr > high)
4830 atomic_store_explicit(&prov->dp_out_max, curr,
4831 memory_order_relaxed);
4832
4833 dplane_provider_unlock(prov);
4834
4835 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
4836 memory_order_relaxed);
4837 }
4838
4839 /*
4840 * Accessor for provider object
4841 */
4842 bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
4843 {
4844 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
4845 }
4846
4847 /*
4848 * Internal helper that copies information from a zebra ns object; this is
4849 * called in the zebra main pthread context as part of dplane ctx init.
4850 */
4851 static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
4852 struct zebra_ns *zns)
4853 {
4854 ns_info->ns_id = zns->ns_id;
4855
4856 #if defined(HAVE_NETLINK)
4857 ns_info->is_cmd = true;
4858 ns_info->sock = zns->netlink_dplane_out.sock;
4859 #endif /* NETLINK */
4860 }
4861
4862 #ifdef HAVE_NETLINK
4863 /*
4864 * Callback when an OS (netlink) incoming event read is ready. This runs
4865 * in the dplane pthread.
4866 */
4867 static int dplane_incoming_read(struct thread *event)
4868 {
4869 struct dplane_zns_info *zi = THREAD_ARG(event);
4870
4871 kernel_dplane_read(&zi->info);
4872
4873 /* Re-start read task */
4874 thread_add_read(zdplane_info.dg_master, dplane_incoming_read, zi,
4875 zi->info.sock, &zi->t_read);
4876
4877 return 0;
4878 }
4879 #endif /* HAVE_NETLINK */
4880
4881 /*
4882 * Notify dplane when namespaces are enabled and disabled. The dplane
4883 * needs to start and stop reading incoming events from the zns. In the
4884 * common case where vrfs are _not_ namespaces, there will only be one
4885 * of these.
4886 *
4887 * This is called in the main pthread.
4888 */
4889 void zebra_dplane_ns_enable(struct zebra_ns *zns, bool enabled)
4890 {
4891 struct dplane_zns_info *zi;
4892
4893 if (IS_ZEBRA_DEBUG_DPLANE)
4894 zlog_debug("%s: %s for nsid %u", __func__,
4895 (enabled ? "ENABLED" : "DISABLED"), zns->ns_id);
4896
4897 /* Search for an existing zns info entry */
4898 frr_each (zns_info_list, &zdplane_info.dg_zns_list, zi) {
4899 if (zi->info.ns_id == zns->ns_id)
4900 break;
4901 }
4902
4903 if (enabled) {
4904 /* Create a new entry if necessary; start reading. */
4905 if (zi == NULL) {
4906 zi = XCALLOC(MTYPE_DP_NS, sizeof(*zi));
4907
4908 zi->info.ns_id = zns->ns_id;
4909
4910 zns_info_list_add_tail(&zdplane_info.dg_zns_list, zi);
4911
4912 if (IS_ZEBRA_DEBUG_DPLANE)
4913 zlog_debug("%s: nsid %u, new zi %p", __func__,
4914 zns->ns_id, zi);
4915 }
4916
4917 /* Make sure we're up-to-date with the zns object */
4918 #if defined(HAVE_NETLINK)
4919 zi->info.is_cmd = false;
4920 zi->info.sock = zns->netlink_dplane_in.sock;
4921
4922 /* Start read task for the dplane pthread. */
4923 if (zdplane_info.dg_master)
4924 thread_add_read(zdplane_info.dg_master,
4925 dplane_incoming_read, zi, zi->info.sock,
4926 &zi->t_read);
4927 #endif
4928 } else if (zi) {
4929 if (IS_ZEBRA_DEBUG_DPLANE)
4930 zlog_debug("%s: nsid %u, deleting zi %p", __func__,
4931 zns->ns_id, zi);
4932
4933 /* Stop reading, free memory */
4934 zns_info_list_del(&zdplane_info.dg_zns_list, zi);
4935
4936 if (zdplane_info.dg_master)
4937 thread_cancel_async(zdplane_info.dg_master, &zi->t_read,
4938 NULL);
4939
4940 XFREE(MTYPE_DP_NS, zi);
4941 }
4942 }
4943
4944 /*
4945 * Provider api to signal that work/events are available
4946 * for the dataplane pthread.
4947 */
4948 int dplane_provider_work_ready(void)
4949 {
4950 /* Note that during zebra startup, we may be offered work before
4951 * the dataplane pthread (and thread-master) are ready. We want to
4952 * enqueue the work, but the event-scheduling machinery may not be
4953 * available.
4954 */
4955 if (zdplane_info.dg_run) {
4956 thread_add_event(zdplane_info.dg_master,
4957 dplane_thread_loop, NULL, 0,
4958 &zdplane_info.dg_t_update);
4959 }
4960
4961 return AOK;
4962 }
4963
4964 /*
4965 * Enqueue a context directly to zebra main.
4966 */
4967 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx)
4968 {
4969 struct dplane_ctx_q temp_list;
4970
4971 /* Zebra's api takes a list, so we need to use a temporary list */
4972 TAILQ_INIT(&temp_list);
4973
4974 TAILQ_INSERT_TAIL(&temp_list, ctx, zd_q_entries);
4975 (zdplane_info.dg_results_cb)(&temp_list);
4976 }
4977
4978 /*
4979 * Kernel dataplane provider
4980 */
4981
4982 static void kernel_dplane_log_detail(struct zebra_dplane_ctx *ctx)
4983 {
4984 char buf[PREFIX_STRLEN];
4985
4986 switch (dplane_ctx_get_op(ctx)) {
4987
4988 case DPLANE_OP_ROUTE_INSTALL:
4989 case DPLANE_OP_ROUTE_UPDATE:
4990 case DPLANE_OP_ROUTE_DELETE:
4991 zlog_debug("%u:%pFX Dplane route update ctx %p op %s",
4992 dplane_ctx_get_vrf(ctx), dplane_ctx_get_dest(ctx),
4993 ctx, dplane_op2str(dplane_ctx_get_op(ctx)));
4994 break;
4995
4996 case DPLANE_OP_NH_INSTALL:
4997 case DPLANE_OP_NH_UPDATE:
4998 case DPLANE_OP_NH_DELETE:
4999 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
5000 dplane_ctx_get_nhe_id(ctx), ctx,
5001 dplane_op2str(dplane_ctx_get_op(ctx)));
5002 break;
5003
5004 case DPLANE_OP_LSP_INSTALL:
5005 case DPLANE_OP_LSP_UPDATE:
5006 case DPLANE_OP_LSP_DELETE:
5007 break;
5008
5009 case DPLANE_OP_PW_INSTALL:
5010 case DPLANE_OP_PW_UNINSTALL:
5011 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
5012 dplane_ctx_get_ifname(ctx),
5013 dplane_op2str(ctx->zd_op), dplane_ctx_get_pw_af(ctx),
5014 dplane_ctx_get_pw_local_label(ctx),
5015 dplane_ctx_get_pw_remote_label(ctx));
5016 break;
5017
5018 case DPLANE_OP_ADDR_INSTALL:
5019 case DPLANE_OP_ADDR_UNINSTALL:
5020 zlog_debug("Dplane intf %s, idx %u, addr %pFX",
5021 dplane_op2str(dplane_ctx_get_op(ctx)),
5022 dplane_ctx_get_ifindex(ctx),
5023 dplane_ctx_get_intf_addr(ctx));
5024 break;
5025
5026 case DPLANE_OP_MAC_INSTALL:
5027 case DPLANE_OP_MAC_DELETE:
5028 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf,
5029 sizeof(buf));
5030
5031 zlog_debug("Dplane %s, mac %s, ifindex %u",
5032 dplane_op2str(dplane_ctx_get_op(ctx)),
5033 buf, dplane_ctx_get_ifindex(ctx));
5034 break;
5035
5036 case DPLANE_OP_NEIGH_INSTALL:
5037 case DPLANE_OP_NEIGH_UPDATE:
5038 case DPLANE_OP_NEIGH_DELETE:
5039 case DPLANE_OP_VTEP_ADD:
5040 case DPLANE_OP_VTEP_DELETE:
5041 case DPLANE_OP_NEIGH_DISCOVER:
5042 case DPLANE_OP_NEIGH_IP_INSTALL:
5043 case DPLANE_OP_NEIGH_IP_DELETE:
5044 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx), buf,
5045 sizeof(buf));
5046
5047 zlog_debug("Dplane %s, ip %s, ifindex %u",
5048 dplane_op2str(dplane_ctx_get_op(ctx)),
5049 buf, dplane_ctx_get_ifindex(ctx));
5050 break;
5051
5052 case DPLANE_OP_RULE_ADD:
5053 case DPLANE_OP_RULE_DELETE:
5054 case DPLANE_OP_RULE_UPDATE:
5055 zlog_debug("Dplane rule update op %s, if %s(%u), ctx %p",
5056 dplane_op2str(dplane_ctx_get_op(ctx)),
5057 dplane_ctx_get_ifname(ctx),
5058 dplane_ctx_get_ifindex(ctx), ctx);
5059 break;
5060
5061 case DPLANE_OP_SYS_ROUTE_ADD:
5062 case DPLANE_OP_SYS_ROUTE_DELETE:
5063 case DPLANE_OP_ROUTE_NOTIFY:
5064 case DPLANE_OP_LSP_NOTIFY:
5065 case DPLANE_OP_BR_PORT_UPDATE:
5066
5067 case DPLANE_OP_NONE:
5068 break;
5069
5070 case DPLANE_OP_IPTABLE_ADD:
5071 case DPLANE_OP_IPTABLE_DELETE: {
5072 struct zebra_pbr_iptable ipt;
5073
5074 dplane_ctx_get_pbr_iptable(ctx, &ipt);
5075 zlog_debug("Dplane iptable update op %s, unique(%u), ctx %p",
5076 dplane_op2str(dplane_ctx_get_op(ctx)), ipt.unique,
5077 ctx);
5078 } break;
5079 case DPLANE_OP_IPSET_ADD:
5080 case DPLANE_OP_IPSET_DELETE: {
5081 struct zebra_pbr_ipset ipset;
5082
5083 dplane_ctx_get_pbr_ipset(ctx, &ipset);
5084 zlog_debug("Dplane ipset update op %s, unique(%u), ctx %p",
5085 dplane_op2str(dplane_ctx_get_op(ctx)), ipset.unique,
5086 ctx);
5087 } break;
5088 case DPLANE_OP_IPSET_ENTRY_ADD:
5089 case DPLANE_OP_IPSET_ENTRY_DELETE: {
5090 struct zebra_pbr_ipset_entry ipent;
5091
5092 dplane_ctx_get_pbr_ipset_entry(ctx, &ipent);
5093 zlog_debug(
5094 "Dplane ipset entry update op %s, unique(%u), ctx %p",
5095 dplane_op2str(dplane_ctx_get_op(ctx)), ipent.unique,
5096 ctx);
5097 } break;
5098 case DPLANE_OP_NEIGH_TABLE_UPDATE:
5099 zlog_debug("Dplane neigh table op %s, ifp %s, family %s",
5100 dplane_op2str(dplane_ctx_get_op(ctx)),
5101 dplane_ctx_get_ifname(ctx),
5102 family2str(dplane_ctx_neightable_get_family(ctx)));
5103 break;
5104 case DPLANE_OP_GRE_SET:
5105 zlog_debug("Dplane gre set op %s, ifp %s, link %u",
5106 dplane_op2str(dplane_ctx_get_op(ctx)),
5107 dplane_ctx_get_ifname(ctx),
5108 ctx->u.gre.link_ifindex);
5109 break;
5110
5111 case DPLANE_OP_INTF_ADDR_ADD:
5112 case DPLANE_OP_INTF_ADDR_DEL:
5113 zlog_debug("Dplane incoming op %s, intf %s, addr %pFX",
5114 dplane_op2str(dplane_ctx_get_op(ctx)),
5115 dplane_ctx_get_ifname(ctx),
5116 dplane_ctx_get_intf_addr(ctx));
5117 break;
5118 }
5119 }
5120
5121 static void kernel_dplane_handle_result(struct zebra_dplane_ctx *ctx)
5122 {
5123 enum zebra_dplane_result res = dplane_ctx_get_status(ctx);
5124
5125 switch (dplane_ctx_get_op(ctx)) {
5126
5127 case DPLANE_OP_ROUTE_INSTALL:
5128 case DPLANE_OP_ROUTE_UPDATE:
5129 case DPLANE_OP_ROUTE_DELETE:
5130 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5131 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
5132 1, memory_order_relaxed);
5133
5134 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE)
5135 && (res == ZEBRA_DPLANE_REQUEST_SUCCESS)) {
5136 struct nexthop *nexthop;
5137
5138 /* Update installed nexthops to signal which have been
5139 * installed.
5140 */
5141 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
5142 nexthop)) {
5143 if (CHECK_FLAG(nexthop->flags,
5144 NEXTHOP_FLAG_RECURSIVE))
5145 continue;
5146
5147 if (CHECK_FLAG(nexthop->flags,
5148 NEXTHOP_FLAG_ACTIVE)) {
5149 SET_FLAG(nexthop->flags,
5150 NEXTHOP_FLAG_FIB);
5151 }
5152 }
5153 }
5154 break;
5155
5156 case DPLANE_OP_NH_INSTALL:
5157 case DPLANE_OP_NH_UPDATE:
5158 case DPLANE_OP_NH_DELETE:
5159 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5160 atomic_fetch_add_explicit(
5161 &zdplane_info.dg_nexthop_errors, 1,
5162 memory_order_relaxed);
5163 break;
5164
5165 case DPLANE_OP_LSP_INSTALL:
5166 case DPLANE_OP_LSP_UPDATE:
5167 case DPLANE_OP_LSP_DELETE:
5168 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5169 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors,
5170 1, memory_order_relaxed);
5171 break;
5172
5173 case DPLANE_OP_PW_INSTALL:
5174 case DPLANE_OP_PW_UNINSTALL:
5175 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5176 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
5177 memory_order_relaxed);
5178 break;
5179
5180 case DPLANE_OP_ADDR_INSTALL:
5181 case DPLANE_OP_ADDR_UNINSTALL:
5182 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5183 atomic_fetch_add_explicit(
5184 &zdplane_info.dg_intf_addr_errors, 1,
5185 memory_order_relaxed);
5186 break;
5187
5188 case DPLANE_OP_MAC_INSTALL:
5189 case DPLANE_OP_MAC_DELETE:
5190 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5191 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors,
5192 1, memory_order_relaxed);
5193 break;
5194
5195 case DPLANE_OP_NEIGH_INSTALL:
5196 case DPLANE_OP_NEIGH_UPDATE:
5197 case DPLANE_OP_NEIGH_DELETE:
5198 case DPLANE_OP_VTEP_ADD:
5199 case DPLANE_OP_VTEP_DELETE:
5200 case DPLANE_OP_NEIGH_DISCOVER:
5201 case DPLANE_OP_NEIGH_IP_INSTALL:
5202 case DPLANE_OP_NEIGH_IP_DELETE:
5203 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5204 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors,
5205 1, memory_order_relaxed);
5206 break;
5207
5208 case DPLANE_OP_RULE_ADD:
5209 case DPLANE_OP_RULE_DELETE:
5210 case DPLANE_OP_RULE_UPDATE:
5211 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5212 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors,
5213 1, memory_order_relaxed);
5214 break;
5215
5216 case DPLANE_OP_IPTABLE_ADD:
5217 case DPLANE_OP_IPTABLE_DELETE:
5218 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5219 atomic_fetch_add_explicit(
5220 &zdplane_info.dg_iptable_errors, 1,
5221 memory_order_relaxed);
5222 break;
5223
5224 case DPLANE_OP_IPSET_ADD:
5225 case DPLANE_OP_IPSET_DELETE:
5226 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5227 atomic_fetch_add_explicit(&zdplane_info.dg_ipset_errors,
5228 1, memory_order_relaxed);
5229 break;
5230
5231 case DPLANE_OP_IPSET_ENTRY_ADD:
5232 case DPLANE_OP_IPSET_ENTRY_DELETE:
5233 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5234 atomic_fetch_add_explicit(
5235 &zdplane_info.dg_ipset_entry_errors, 1,
5236 memory_order_relaxed);
5237 break;
5238
5239 case DPLANE_OP_NEIGH_TABLE_UPDATE:
5240 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5241 atomic_fetch_add_explicit(
5242 &zdplane_info.dg_neightable_errors, 1,
5243 memory_order_relaxed);
5244 break;
5245
5246 case DPLANE_OP_GRE_SET:
5247 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5248 atomic_fetch_add_explicit(
5249 &zdplane_info.dg_gre_set_errors, 1,
5250 memory_order_relaxed);
5251 break;
5252 /* Ignore 'notifications' - no-op */
5253 case DPLANE_OP_SYS_ROUTE_ADD:
5254 case DPLANE_OP_SYS_ROUTE_DELETE:
5255 case DPLANE_OP_ROUTE_NOTIFY:
5256 case DPLANE_OP_LSP_NOTIFY:
5257 case DPLANE_OP_BR_PORT_UPDATE:
5258 break;
5259
5260 /* TODO -- error counters for incoming events? */
5261 case DPLANE_OP_INTF_ADDR_ADD:
5262 case DPLANE_OP_INTF_ADDR_DEL:
5263 break;
5264
5265 case DPLANE_OP_NONE:
5266 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
5267 atomic_fetch_add_explicit(&zdplane_info.dg_other_errors,
5268 1, memory_order_relaxed);
5269 break;
5270 }
5271 }
5272
5273 static void kernel_dplane_process_iptable(struct zebra_dplane_provider *prov,
5274 struct zebra_dplane_ctx *ctx)
5275 {
5276 zebra_pbr_process_iptable(ctx);
5277 dplane_provider_enqueue_out_ctx(prov, ctx);
5278 }
5279
5280 static void kernel_dplane_process_ipset(struct zebra_dplane_provider *prov,
5281 struct zebra_dplane_ctx *ctx)
5282 {
5283 zebra_pbr_process_ipset(ctx);
5284 dplane_provider_enqueue_out_ctx(prov, ctx);
5285 }
5286
5287 static void
5288 kernel_dplane_process_ipset_entry(struct zebra_dplane_provider *prov,
5289 struct zebra_dplane_ctx *ctx)
5290 {
5291 zebra_pbr_process_ipset_entry(ctx);
5292 dplane_provider_enqueue_out_ctx(prov, ctx);
5293 }
5294
5295 /*
5296 * Kernel provider callback
5297 */
5298 static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
5299 {
5300 struct zebra_dplane_ctx *ctx, *tctx;
5301 struct dplane_ctx_q work_list;
5302 int counter, limit;
5303
5304 TAILQ_INIT(&work_list);
5305
5306 limit = dplane_provider_get_work_limit(prov);
5307
5308 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5309 zlog_debug("dplane provider '%s': processing",
5310 dplane_provider_get_name(prov));
5311
5312 for (counter = 0; counter < limit; counter++) {
5313 ctx = dplane_provider_dequeue_in_ctx(prov);
5314 if (ctx == NULL)
5315 break;
5316 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5317 kernel_dplane_log_detail(ctx);
5318
5319 if ((dplane_ctx_get_op(ctx) == DPLANE_OP_IPTABLE_ADD
5320 || dplane_ctx_get_op(ctx) == DPLANE_OP_IPTABLE_DELETE))
5321 kernel_dplane_process_iptable(prov, ctx);
5322 else if ((dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_ADD
5323 || dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_DELETE))
5324 kernel_dplane_process_ipset(prov, ctx);
5325 else if ((dplane_ctx_get_op(ctx) == DPLANE_OP_IPSET_ENTRY_ADD
5326 || dplane_ctx_get_op(ctx)
5327 == DPLANE_OP_IPSET_ENTRY_DELETE))
5328 kernel_dplane_process_ipset_entry(prov, ctx);
5329 else
5330 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
5331 }
5332
5333 kernel_update_multi(&work_list);
5334
5335 TAILQ_FOREACH_SAFE (ctx, &work_list, zd_q_entries, tctx) {
5336 kernel_dplane_handle_result(ctx);
5337
5338 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
5339 dplane_provider_enqueue_out_ctx(prov, ctx);
5340 }
5341
5342 /* Ensure that we'll run the work loop again if there's still
5343 * more work to do.
5344 */
5345 if (counter >= limit) {
5346 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5347 zlog_debug("dplane provider '%s' reached max updates %d",
5348 dplane_provider_get_name(prov), counter);
5349
5350 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
5351 1, memory_order_relaxed);
5352
5353 dplane_provider_work_ready();
5354 }
5355
5356 return 0;
5357 }
5358
5359 #ifdef DPLANE_TEST_PROVIDER
5360
5361 /*
5362 * Test dataplane provider plugin
5363 */
5364
5365 /*
5366 * Test provider process callback
5367 */
5368 static int test_dplane_process_func(struct zebra_dplane_provider *prov)
5369 {
5370 struct zebra_dplane_ctx *ctx;
5371 int counter, limit;
5372
5373 /* Just moving from 'in' queue to 'out' queue */
5374
5375 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5376 zlog_debug("dplane provider '%s': processing",
5377 dplane_provider_get_name(prov));
5378
5379 limit = dplane_provider_get_work_limit(prov);
5380
5381 for (counter = 0; counter < limit; counter++) {
5382 ctx = dplane_provider_dequeue_in_ctx(prov);
5383 if (ctx == NULL)
5384 break;
5385
5386 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5387 zlog_debug("dplane provider '%s': op %s",
5388 dplane_provider_get_name(prov),
5389 dplane_op2str(dplane_ctx_get_op(ctx)));
5390
5391 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
5392
5393 dplane_provider_enqueue_out_ctx(prov, ctx);
5394 }
5395
5396 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5397 zlog_debug("dplane provider '%s': processed %d",
5398 dplane_provider_get_name(prov), counter);
5399
5400 /* Ensure that we'll run the work loop again if there's still
5401 * more work to do.
5402 */
5403 if (counter >= limit)
5404 dplane_provider_work_ready();
5405
5406 return 0;
5407 }
5408
5409 /*
5410 * Test provider shutdown/fini callback
5411 */
5412 static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
5413 bool early)
5414 {
5415 if (IS_ZEBRA_DEBUG_DPLANE)
5416 zlog_debug("dplane provider '%s': %sshutdown",
5417 dplane_provider_get_name(prov),
5418 early ? "early " : "");
5419
5420 return 0;
5421 }
5422 #endif /* DPLANE_TEST_PROVIDER */
5423
5424 /*
5425 * Register default kernel provider
5426 */
5427 static void dplane_provider_init(void)
5428 {
5429 int ret;
5430
5431 ret = dplane_provider_register("Kernel",
5432 DPLANE_PRIO_KERNEL,
5433 DPLANE_PROV_FLAGS_DEFAULT, NULL,
5434 kernel_dplane_process_func,
5435 NULL,
5436 NULL, NULL);
5437
5438 if (ret != AOK)
5439 zlog_err("Unable to register kernel dplane provider: %d",
5440 ret);
5441
5442 #ifdef DPLANE_TEST_PROVIDER
5443 /* Optional test provider ... */
5444 ret = dplane_provider_register("Test",
5445 DPLANE_PRIO_PRE_KERNEL,
5446 DPLANE_PROV_FLAGS_DEFAULT, NULL,
5447 test_dplane_process_func,
5448 test_dplane_shutdown_func,
5449 NULL /* data */, NULL);
5450
5451 if (ret != AOK)
5452 zlog_err("Unable to register test dplane provider: %d",
5453 ret);
5454 #endif /* DPLANE_TEST_PROVIDER */
5455 }
5456
5457 /*
5458 * Allow zebra code to walk the queue of pending contexts, evaluate each one
5459 * using a callback function. If the function returns 'true', the context
5460 * will be dequeued and freed without being processed.
5461 */
5462 int dplane_clean_ctx_queue(bool (*context_cb)(struct zebra_dplane_ctx *ctx,
5463 void *arg), void *val)
5464 {
5465 struct zebra_dplane_ctx *ctx, *temp;
5466 struct dplane_ctx_q work_list;
5467
5468 TAILQ_INIT(&work_list);
5469
5470 if (context_cb == NULL)
5471 goto done;
5472
5473 /* Walk the pending context queue under the dplane lock. */
5474 DPLANE_LOCK();
5475
5476 TAILQ_FOREACH_SAFE(ctx, &zdplane_info.dg_update_ctx_q, zd_q_entries,
5477 temp) {
5478 if (context_cb(ctx, val)) {
5479 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
5480 zd_q_entries);
5481 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
5482 }
5483 }
5484
5485 DPLANE_UNLOCK();
5486
5487 /* Now free any contexts selected by the caller, without holding
5488 * the lock.
5489 */
5490 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, temp) {
5491 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
5492 dplane_ctx_fini(&ctx);
5493 }
5494
5495 done:
5496
5497 return 0;
5498 }
5499
5500 /* Indicates zebra shutdown/exit is in progress. Some operations may be
5501 * simplified or skipped during shutdown processing.
5502 */
5503 bool dplane_is_in_shutdown(void)
5504 {
5505 return zdplane_info.dg_is_shutdown;
5506 }
5507
5508 /*
5509 * Enable collection of extra info about interfaces in route updates.
5510 */
5511 void dplane_enable_intf_extra_info(void)
5512 {
5513 dplane_collect_extra_intf_info = true;
5514 }
5515
5516 /*
5517 * Early or pre-shutdown, de-init notification api. This runs pretty
5518 * early during zebra shutdown, as a signal to stop new work and prepare
5519 * for updates generated by shutdown/cleanup activity, as zebra tries to
5520 * remove everything it's responsible for.
5521 * NB: This runs in the main zebra pthread context.
5522 */
5523 void zebra_dplane_pre_finish(void)
5524 {
5525 struct zebra_dplane_provider *prov;
5526
5527 if (IS_ZEBRA_DEBUG_DPLANE)
5528 zlog_debug("Zebra dataplane pre-finish called");
5529
5530 zdplane_info.dg_is_shutdown = true;
5531
5532 /* Notify provider(s) of pending shutdown. */
5533 TAILQ_FOREACH(prov, &zdplane_info.dg_providers_q, dp_prov_link) {
5534 if (prov->dp_fini == NULL)
5535 continue;
5536
5537 prov->dp_fini(prov, true /* early */);
5538 }
5539 }
5540
5541 /*
5542 * Utility to determine whether work remains enqueued within the dplane;
5543 * used during system shutdown processing.
5544 */
5545 static bool dplane_work_pending(void)
5546 {
5547 bool ret = false;
5548 struct zebra_dplane_ctx *ctx;
5549 struct zebra_dplane_provider *prov;
5550
5551 /* TODO -- just checking incoming/pending work for now, must check
5552 * providers
5553 */
5554 DPLANE_LOCK();
5555 {
5556 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
5557 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
5558 }
5559 DPLANE_UNLOCK();
5560
5561 if (ctx != NULL) {
5562 ret = true;
5563 goto done;
5564 }
5565
5566 while (prov) {
5567
5568 dplane_provider_lock(prov);
5569
5570 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
5571 if (ctx == NULL)
5572 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
5573
5574 dplane_provider_unlock(prov);
5575
5576 if (ctx != NULL)
5577 break;
5578
5579 DPLANE_LOCK();
5580 prov = TAILQ_NEXT(prov, dp_prov_link);
5581 DPLANE_UNLOCK();
5582 }
5583
5584 if (ctx != NULL)
5585 ret = true;
5586
5587 done:
5588 return ret;
5589 }
5590
5591 /*
5592 * Shutdown-time intermediate callback, used to determine when all pending
5593 * in-flight updates are done. If there's still work to do, reschedules itself.
5594 * If all work is done, schedules an event to the main zebra thread for
5595 * final zebra shutdown.
5596 * This runs in the dplane pthread context.
5597 */
5598 static int dplane_check_shutdown_status(struct thread *event)
5599 {
5600 struct dplane_zns_info *zi;
5601
5602 if (IS_ZEBRA_DEBUG_DPLANE)
5603 zlog_debug("Zebra dataplane shutdown status check called");
5604
5605 /* Remove any zns info entries as we stop the dplane pthread. */
5606 frr_each_safe (zns_info_list, &zdplane_info.dg_zns_list, zi) {
5607 zns_info_list_del(&zdplane_info.dg_zns_list, zi);
5608
5609 if (zdplane_info.dg_master)
5610 thread_cancel(&zi->t_read);
5611
5612 XFREE(MTYPE_DP_NS, zi);
5613 }
5614
5615 if (dplane_work_pending()) {
5616 /* Reschedule dplane check on a short timer */
5617 thread_add_timer_msec(zdplane_info.dg_master,
5618 dplane_check_shutdown_status,
5619 NULL, 100,
5620 &zdplane_info.dg_t_shutdown_check);
5621
5622 /* TODO - give up and stop waiting after a short time? */
5623
5624 } else {
5625 /* We appear to be done - schedule a final callback event
5626 * for the zebra main pthread.
5627 */
5628 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
5629 }
5630
5631 return 0;
5632 }
5633
5634 /*
5635 * Shutdown, de-init api. This runs pretty late during shutdown,
5636 * after zebra has tried to free/remove/uninstall all routes during shutdown.
5637 * At this point, dplane work may still remain to be done, so we can't just
5638 * blindly terminate. If there's still work to do, we'll periodically check
5639 * and when done, we'll enqueue a task to the zebra main thread for final
5640 * termination processing.
5641 *
5642 * NB: This runs in the main zebra thread context.
5643 */
5644 void zebra_dplane_finish(void)
5645 {
5646 if (IS_ZEBRA_DEBUG_DPLANE)
5647 zlog_debug("Zebra dataplane fini called");
5648
5649 thread_add_event(zdplane_info.dg_master,
5650 dplane_check_shutdown_status, NULL, 0,
5651 &zdplane_info.dg_t_shutdown_check);
5652 }
5653
5654 /*
5655 * Main dataplane pthread event loop. The thread takes new incoming work
5656 * and offers it to the first provider. It then iterates through the
5657 * providers, taking complete work from each one and offering it
5658 * to the next in order. At each step, a limited number of updates are
5659 * processed during a cycle in order to provide some fairness.
5660 *
5661 * This loop through the providers is only run once, so that the dataplane
5662 * pthread can look for other pending work - such as i/o work on behalf of
5663 * providers.
5664 */
5665 static int dplane_thread_loop(struct thread *event)
5666 {
5667 struct dplane_ctx_q work_list;
5668 struct dplane_ctx_q error_list;
5669 struct zebra_dplane_provider *prov;
5670 struct zebra_dplane_ctx *ctx, *tctx;
5671 int limit, counter, error_counter;
5672 uint64_t curr, high;
5673 bool reschedule = false;
5674
5675 /* Capture work limit per cycle */
5676 limit = zdplane_info.dg_updates_per_cycle;
5677
5678 /* Init temporary lists used to move contexts among providers */
5679 TAILQ_INIT(&work_list);
5680 TAILQ_INIT(&error_list);
5681 error_counter = 0;
5682
5683 /* Check for zebra shutdown */
5684 if (!zdplane_info.dg_run)
5685 goto done;
5686
5687 /* Dequeue some incoming work from zebra (if any) onto the temporary
5688 * working list.
5689 */
5690 DPLANE_LOCK();
5691
5692 /* Locate initial registered provider */
5693 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
5694
5695 /* Move new work from incoming list to temp list */
5696 for (counter = 0; counter < limit; counter++) {
5697 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
5698 if (ctx) {
5699 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
5700 zd_q_entries);
5701
5702 ctx->zd_provider = prov->dp_id;
5703
5704 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
5705 } else {
5706 break;
5707 }
5708 }
5709
5710 DPLANE_UNLOCK();
5711
5712 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
5713 memory_order_relaxed);
5714
5715 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5716 zlog_debug("dplane: incoming new work counter: %d", counter);
5717
5718 /* Iterate through the registered providers, offering new incoming
5719 * work. If the provider has outgoing work in its queue, take that
5720 * work for the next provider
5721 */
5722 while (prov) {
5723
5724 /* At each iteration, the temporary work list has 'counter'
5725 * items.
5726 */
5727 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5728 zlog_debug("dplane enqueues %d new work to provider '%s'",
5729 counter, dplane_provider_get_name(prov));
5730
5731 /* Capture current provider id in each context; check for
5732 * error status.
5733 */
5734 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
5735 if (dplane_ctx_get_status(ctx) ==
5736 ZEBRA_DPLANE_REQUEST_SUCCESS) {
5737 ctx->zd_provider = prov->dp_id;
5738 } else {
5739 /*
5740 * TODO -- improve error-handling: recirc
5741 * errors backwards so that providers can
5742 * 'undo' their work (if they want to)
5743 */
5744
5745 /* Move to error list; will be returned
5746 * zebra main.
5747 */
5748 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
5749 TAILQ_INSERT_TAIL(&error_list,
5750 ctx, zd_q_entries);
5751 error_counter++;
5752 }
5753 }
5754
5755 /* Enqueue new work to the provider */
5756 dplane_provider_lock(prov);
5757
5758 if (TAILQ_FIRST(&work_list))
5759 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
5760 zd_q_entries);
5761
5762 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
5763 memory_order_relaxed);
5764 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
5765 memory_order_relaxed);
5766 curr = atomic_load_explicit(&prov->dp_in_queued,
5767 memory_order_relaxed);
5768 high = atomic_load_explicit(&prov->dp_in_max,
5769 memory_order_relaxed);
5770 if (curr > high)
5771 atomic_store_explicit(&prov->dp_in_max, curr,
5772 memory_order_relaxed);
5773
5774 dplane_provider_unlock(prov);
5775
5776 /* Reset the temp list (though the 'concat' may have done this
5777 * already), and the counter
5778 */
5779 TAILQ_INIT(&work_list);
5780 counter = 0;
5781
5782 /* Call into the provider code. Note that this is
5783 * unconditional: we offer to do work even if we don't enqueue
5784 * any _new_ work.
5785 */
5786 (*prov->dp_fp)(prov);
5787
5788 /* Check for zebra shutdown */
5789 if (!zdplane_info.dg_run)
5790 break;
5791
5792 /* Dequeue completed work from the provider */
5793 dplane_provider_lock(prov);
5794
5795 while (counter < limit) {
5796 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
5797 if (ctx) {
5798 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
5799 zd_q_entries);
5800
5801 TAILQ_INSERT_TAIL(&work_list,
5802 ctx, zd_q_entries);
5803 counter++;
5804 } else
5805 break;
5806 }
5807
5808 dplane_provider_unlock(prov);
5809
5810 if (counter >= limit)
5811 reschedule = true;
5812
5813 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5814 zlog_debug("dplane dequeues %d completed work from provider %s",
5815 counter, dplane_provider_get_name(prov));
5816
5817 /* Locate next provider */
5818 DPLANE_LOCK();
5819 prov = TAILQ_NEXT(prov, dp_prov_link);
5820 DPLANE_UNLOCK();
5821 }
5822
5823 /*
5824 * We hit the work limit while processing at least one provider's
5825 * output queue - ensure we come back and finish it.
5826 */
5827 if (reschedule)
5828 dplane_provider_work_ready();
5829
5830 /* After all providers have been serviced, enqueue any completed
5831 * work and any errors back to zebra so it can process the results.
5832 */
5833 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
5834 zlog_debug("dplane has %d completed, %d errors, for zebra main",
5835 counter, error_counter);
5836
5837 /*
5838 * Hand lists through the api to zebra main,
5839 * to reduce the number of lock/unlock cycles
5840 */
5841
5842 /* Call through to zebra main */
5843 (zdplane_info.dg_results_cb)(&error_list);
5844
5845 TAILQ_INIT(&error_list);
5846
5847 /* Call through to zebra main */
5848 (zdplane_info.dg_results_cb)(&work_list);
5849
5850 TAILQ_INIT(&work_list);
5851
5852 done:
5853 return 0;
5854 }
5855
5856 /*
5857 * Final phase of shutdown, after all work enqueued to dplane has been
5858 * processed. This is called from the zebra main pthread context.
5859 */
5860 void zebra_dplane_shutdown(void)
5861 {
5862 struct zebra_dplane_provider *dp;
5863
5864 if (IS_ZEBRA_DEBUG_DPLANE)
5865 zlog_debug("Zebra dataplane shutdown called");
5866
5867 /* Stop dplane thread, if it's running */
5868
5869 zdplane_info.dg_run = false;
5870
5871 if (zdplane_info.dg_t_update)
5872 thread_cancel_async(zdplane_info.dg_t_update->master,
5873 &zdplane_info.dg_t_update, NULL);
5874
5875 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
5876
5877 /* Destroy pthread */
5878 frr_pthread_destroy(zdplane_info.dg_pthread);
5879 zdplane_info.dg_pthread = NULL;
5880 zdplane_info.dg_master = NULL;
5881
5882 /* Notify provider(s) of final shutdown.
5883 * Note that this call is in the main pthread, so providers must
5884 * be prepared for that.
5885 */
5886 TAILQ_FOREACH(dp, &zdplane_info.dg_providers_q, dp_prov_link) {
5887 if (dp->dp_fini == NULL)
5888 continue;
5889
5890 dp->dp_fini(dp, false);
5891 }
5892
5893 /* TODO -- Clean-up provider objects */
5894
5895 /* TODO -- Clean queue(s), free memory */
5896 }
5897
5898 /*
5899 * Initialize the dataplane module during startup, internal/private version
5900 */
5901 static void zebra_dplane_init_internal(void)
5902 {
5903 memset(&zdplane_info, 0, sizeof(zdplane_info));
5904
5905 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
5906
5907 TAILQ_INIT(&zdplane_info.dg_update_ctx_q);
5908 TAILQ_INIT(&zdplane_info.dg_providers_q);
5909 zns_info_list_init(&zdplane_info.dg_zns_list);
5910
5911 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
5912
5913 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
5914
5915 /* Register default kernel 'provider' during init */
5916 dplane_provider_init();
5917 }
5918
5919 /*
5920 * Start the dataplane pthread. This step needs to be run later than the
5921 * 'init' step, in case zebra has fork-ed.
5922 */
5923 void zebra_dplane_start(void)
5924 {
5925 struct dplane_zns_info *zi;
5926 struct zebra_dplane_provider *prov;
5927 struct frr_pthread_attr pattr = {
5928 .start = frr_pthread_attr_default.start,
5929 .stop = frr_pthread_attr_default.stop
5930 };
5931
5932 /* Start dataplane pthread */
5933
5934 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
5935 "zebra_dplane");
5936
5937 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
5938
5939 zdplane_info.dg_run = true;
5940
5941 /* Enqueue an initial event for the dataplane pthread */
5942 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
5943 &zdplane_info.dg_t_update);
5944
5945 /* Enqueue reads if necessary */
5946 frr_each (zns_info_list, &zdplane_info.dg_zns_list, zi) {
5947 #if defined(HAVE_NETLINK)
5948 thread_add_read(zdplane_info.dg_master, dplane_incoming_read,
5949 zi, zi->info.sock, &zi->t_read);
5950 #endif
5951 }
5952
5953 /* Call start callbacks for registered providers */
5954
5955 DPLANE_LOCK();
5956 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
5957 DPLANE_UNLOCK();
5958
5959 while (prov) {
5960
5961 if (prov->dp_start)
5962 (prov->dp_start)(prov);
5963
5964 /* Locate next provider */
5965 DPLANE_LOCK();
5966 prov = TAILQ_NEXT(prov, dp_prov_link);
5967 DPLANE_UNLOCK();
5968 }
5969
5970 frr_pthread_run(zdplane_info.dg_pthread, NULL);
5971 }
5972
5973 /*
5974 * Initialize the dataplane module at startup; called by zebra rib_init()
5975 */
5976 void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
5977 {
5978 zebra_dplane_init_internal();
5979 zdplane_info.dg_results_cb = results_fp;
5980 }