2 * Zebra dataplane layer.
3 * Copyright (c) 2018 Volta Networks, Inc.
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.
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.
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
20 #include "lib/libfrr.h"
21 #include "lib/debug.h"
22 #include "lib/frratomic.h"
23 #include "lib/frr_pthread.h"
24 #include "lib/memory.h"
25 #include "lib/queue.h"
26 #include "lib/zebra.h"
27 #include "zebra/zebra_memory.h"
28 #include "zebra/zserv.h"
29 #include "zebra/zebra_dplane.h"
31 #include "zebra/debug.h"
33 /* Memory type for context blocks */
34 DEFINE_MTYPE(ZEBRA
, DP_CTX
, "Zebra DPlane Ctx")
35 DEFINE_MTYPE(ZEBRA
, DP_PROV
, "Zebra DPlane Provider")
41 /* Default value for max queued incoming updates */
42 const uint32_t DPLANE_DEFAULT_MAX_QUEUED
= 200;
45 /* Validation check macro for context blocks */
46 /* #define DPLANE_DEBUG 1 */
50 # define DPLANE_CTX_VALID(p) \
55 # define DPLANE_CTX_VALID(p)
57 #endif /* DPLANE_DEBUG */
60 * The context block used to exchange info about route updates across
61 * the boundary between the zebra main context (and pthread) and the
62 * dataplane layer (and pthread).
64 struct zebra_dplane_ctx
{
67 enum dplane_op_e zd_op
;
69 /* Status on return */
70 enum zebra_dplane_result zd_status
;
72 /* TODO -- internal/sub-operation status? */
73 enum zebra_dplane_status zd_remote_status
;
74 enum zebra_dplane_status zd_kernel_status
;
76 /* Dest and (optional) source prefixes */
77 struct prefix zd_dest
;
94 route_tag_t zd_old_tag
;
96 uint32_t zd_old_metric
;
98 uint16_t zd_old_instance
;
101 uint8_t zd_old_distance
;
104 uint32_t zd_nexthop_mtu
;
107 struct zebra_dplane_info zd_ns_info
;
110 struct nexthop_group zd_ng
;
112 /* "Previous" nexthops, used only in route updates without netlink */
113 struct nexthop_group zd_old_ng
;
115 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
117 /* Embedded list linkage */
118 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
122 * Registration block for one dataplane provider.
124 struct zebra_dplane_provider
{
126 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
128 /* Priority, for ordering among providers */
134 dplane_provider_process_fp dp_fp
;
136 dplane_provider_fini_fp dp_fini
;
138 _Atomic
uint64_t dp_in_counter
;
139 _Atomic
uint64_t dp_error_counter
;
141 /* Embedded list linkage */
142 TAILQ_ENTRY(zebra_dplane_provider
) dp_q_providers
;
149 static struct zebra_dplane_globals
{
150 /* Mutex to control access to dataplane components */
151 pthread_mutex_t dg_mutex
;
153 /* Results callback registered by zebra 'core' */
154 dplane_results_fp dg_results_cb
;
156 /* Sentinel for beginning of shutdown */
157 volatile bool dg_is_shutdown
;
159 /* Sentinel for end of shutdown */
160 volatile bool dg_run
;
162 /* Route-update context queue inbound to the dataplane */
163 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_route_ctx_q
;
165 /* Ordered list of providers */
166 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
168 /* Counter used to assign internal ids to providers */
169 uint32_t dg_provider_id
;
171 /* Limit number of pending, unprocessed updates */
172 _Atomic
uint32_t dg_max_queued_updates
;
174 _Atomic
uint64_t dg_routes_in
;
175 _Atomic
uint32_t dg_routes_queued
;
176 _Atomic
uint32_t dg_routes_queued_max
;
177 _Atomic
uint64_t dg_route_errors
;
179 /* Event-delivery context 'master' for the dplane */
180 struct thread_master
*dg_master
;
182 /* Event/'thread' pointer for queued updates */
183 struct thread
*dg_t_update
;
185 /* Event pointer for pending shutdown check loop */
186 struct thread
*dg_t_shutdown_check
;
191 * Lock and unlock for interactions with the zebra 'core'
193 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
195 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
198 static int dplane_route_process(struct thread
*event
);
205 * Allocate a dataplane update context
207 static struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
209 struct zebra_dplane_ctx
*p
;
211 /* TODO -- just alloc'ing memory, but would like to maintain
214 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
220 * Free a dataplane results context.
222 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
225 DPLANE_CTX_VALID(*pctx
);
227 /* TODO -- just freeing memory, but would like to maintain
231 /* Free embedded nexthops */
232 if ((*pctx
)->zd_ng
.nexthop
) {
233 /* This deals with recursive nexthops too */
234 nexthops_free((*pctx
)->zd_ng
.nexthop
);
237 if ((*pctx
)->zd_old_ng
.nexthop
) {
238 /* This deals with recursive nexthops too */
239 nexthops_free((*pctx
)->zd_old_ng
.nexthop
);
242 XFREE(MTYPE_DP_CTX
, *pctx
);
248 * Return a context block to the dplane module after processing
250 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
252 /* TODO -- enqueue for next provider; for now, just free */
253 dplane_ctx_free(pctx
);
256 /* Enqueue a context block */
257 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
258 const struct zebra_dplane_ctx
*ctx
)
260 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
263 /* Dequeue a context block from the head of a list */
264 void dplane_ctx_dequeue(struct dplane_ctx_q
*q
, struct zebra_dplane_ctx
**ctxp
)
266 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
269 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
275 * Accessors for information from the context object
277 enum zebra_dplane_result
dplane_ctx_get_status(
278 const struct zebra_dplane_ctx
*ctx
)
280 DPLANE_CTX_VALID(ctx
);
282 return ctx
->zd_status
;
285 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
287 DPLANE_CTX_VALID(ctx
);
292 const char *dplane_op2str(enum dplane_op_e op
)
294 const char *ret
= "UNKNOWN";
302 case DPLANE_OP_ROUTE_INSTALL
:
303 ret
= "ROUTE_INSTALL";
305 case DPLANE_OP_ROUTE_UPDATE
:
306 ret
= "ROUTE_UPDATE";
308 case DPLANE_OP_ROUTE_DELETE
:
309 ret
= "ROUTE_DELETE";
317 const char *dplane_res2str(enum zebra_dplane_result res
)
319 const char *ret
= "<Unknown>";
322 case ZEBRA_DPLANE_REQUEST_FAILURE
:
325 case ZEBRA_DPLANE_REQUEST_QUEUED
:
328 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
336 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
338 DPLANE_CTX_VALID(ctx
);
340 return &(ctx
->zd_dest
);
343 /* Source prefix is a little special - return NULL for "no src prefix" */
344 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
346 DPLANE_CTX_VALID(ctx
);
348 if (ctx
->zd_src
.prefixlen
== 0 &&
349 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->zd_src
.u
.prefix6
))) {
352 return &(ctx
->zd_src
);
356 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
358 DPLANE_CTX_VALID(ctx
);
360 return ctx
->zd_is_update
;
363 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
365 DPLANE_CTX_VALID(ctx
);
370 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
372 DPLANE_CTX_VALID(ctx
);
374 return ctx
->zd_old_seq
;
377 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
379 DPLANE_CTX_VALID(ctx
);
381 return ctx
->zd_vrf_id
;
384 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
386 DPLANE_CTX_VALID(ctx
);
391 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
393 DPLANE_CTX_VALID(ctx
);
395 return ctx
->zd_old_type
;
398 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
400 DPLANE_CTX_VALID(ctx
);
405 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
407 DPLANE_CTX_VALID(ctx
);
412 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
414 DPLANE_CTX_VALID(ctx
);
416 return ctx
->zd_table_id
;
419 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
421 DPLANE_CTX_VALID(ctx
);
426 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
428 DPLANE_CTX_VALID(ctx
);
430 return ctx
->zd_old_tag
;
433 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
435 DPLANE_CTX_VALID(ctx
);
437 return ctx
->zd_instance
;
440 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
442 DPLANE_CTX_VALID(ctx
);
444 return ctx
->zd_instance
;
447 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
449 DPLANE_CTX_VALID(ctx
);
451 return ctx
->zd_metric
;
454 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
456 DPLANE_CTX_VALID(ctx
);
458 return ctx
->zd_old_metric
;
461 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
463 DPLANE_CTX_VALID(ctx
);
468 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
470 DPLANE_CTX_VALID(ctx
);
472 return ctx
->zd_nexthop_mtu
;
475 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
477 DPLANE_CTX_VALID(ctx
);
479 return ctx
->zd_distance
;
482 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
484 DPLANE_CTX_VALID(ctx
);
486 return ctx
->zd_old_distance
;
489 const struct nexthop_group
*dplane_ctx_get_ng(
490 const struct zebra_dplane_ctx
*ctx
)
492 DPLANE_CTX_VALID(ctx
);
494 return &(ctx
->zd_ng
);
497 const struct nexthop_group
*dplane_ctx_get_old_ng(
498 const struct zebra_dplane_ctx
*ctx
)
500 DPLANE_CTX_VALID(ctx
);
502 return &(ctx
->zd_old_ng
);
505 const struct zebra_dplane_info
*dplane_ctx_get_ns(
506 const struct zebra_dplane_ctx
*ctx
)
508 DPLANE_CTX_VALID(ctx
);
510 return &(ctx
->zd_ns_info
);
514 * End of dplane context accessors
518 * Retrieve the limit on the number of pending, unprocessed updates.
520 uint32_t dplane_get_in_queue_limit(void)
522 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
523 memory_order_relaxed
);
527 * Configure limit on the number of pending, queued updates.
529 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
531 /* Reset to default on 'unset' */
533 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
535 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
536 memory_order_relaxed
);
540 * Retrieve the current queue depth of incoming, unprocessed updates
542 uint32_t dplane_get_in_queue_len(void)
544 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
545 memory_order_seq_cst
);
549 * Initialize a context block for a route update from zebra data structs.
551 static int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
,
553 struct route_node
*rn
,
554 struct route_entry
*re
)
557 const struct route_table
*table
= NULL
;
558 const rib_table_info_t
*info
;
559 const struct prefix
*p
, *src_p
;
560 struct zebra_ns
*zns
;
561 struct zebra_vrf
*zvrf
;
562 struct nexthop
*nexthop
;
564 if (!ctx
|| !rn
|| !re
)
569 ctx
->zd_type
= re
->type
;
570 ctx
->zd_old_type
= re
->type
;
572 /* Prefixes: dest, and optional source */
573 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
575 prefix_copy(&(ctx
->zd_dest
), p
);
578 prefix_copy(&(ctx
->zd_src
), src_p
);
580 memset(&(ctx
->zd_src
), 0, sizeof(ctx
->zd_src
));
582 ctx
->zd_table_id
= re
->table
;
584 ctx
->zd_metric
= re
->metric
;
585 ctx
->zd_old_metric
= re
->metric
;
586 ctx
->zd_vrf_id
= re
->vrf_id
;
587 ctx
->zd_mtu
= re
->mtu
;
588 ctx
->zd_nexthop_mtu
= re
->nexthop_mtu
;
589 ctx
->zd_instance
= re
->instance
;
590 ctx
->zd_tag
= re
->tag
;
591 ctx
->zd_old_tag
= re
->tag
;
592 ctx
->zd_distance
= re
->distance
;
594 table
= srcdest_rnode_table(rn
);
597 ctx
->zd_afi
= info
->afi
;
598 ctx
->zd_safi
= info
->safi
;
600 /* Extract ns info - can't use pointers to 'core' structs */
601 zvrf
= vrf_info_lookup(re
->vrf_id
);
604 zebra_dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
, true /*is_cmd*/);
606 #if defined(HAVE_NETLINK)
607 /* Increment message counter after copying to context struct - may need
608 * two messages in some 'update' cases.
610 if (op
== DPLANE_OP_ROUTE_UPDATE
)
611 zns
->netlink_cmd
.seq
+= 2;
613 zns
->netlink_cmd
.seq
++;
616 /* Copy nexthops; recursive info is included too */
617 copy_nexthops(&(ctx
->zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
619 /* TODO -- maybe use array of nexthops to avoid allocs? */
621 /* Ensure that the dplane's nexthop flag is clear. */
622 for (ALL_NEXTHOPS(ctx
->zd_ng
, nexthop
))
623 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
625 /* Trying out the sequence number idea, so we can try to detect
626 * when a result is stale.
628 re
->dplane_sequence
++;
629 ctx
->zd_seq
= re
->dplane_sequence
;
638 * Enqueue a new route update,
639 * and ensure an event is active for the dataplane thread.
641 static int dplane_route_enqueue(struct zebra_dplane_ctx
*ctx
)
646 /* Enqueue for processing by the dataplane thread */
649 TAILQ_INSERT_TAIL(&zdplane_info
.dg_route_ctx_q
, ctx
,
654 curr
= atomic_add_fetch_explicit(&zdplane_info
.dg_routes_queued
,
655 1, memory_order_seq_cst
);
657 /* Maybe update high-water counter also */
658 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
659 memory_order_seq_cst
);
660 while (high
< curr
) {
661 if (atomic_compare_exchange_weak_explicit(
662 &zdplane_info
.dg_routes_queued_max
,
664 memory_order_seq_cst
,
665 memory_order_seq_cst
))
669 /* Ensure that an event for the dataplane thread is active */
670 thread_add_event(zdplane_info
.dg_master
, dplane_route_process
, NULL
, 0,
671 &zdplane_info
.dg_t_update
);
679 * Attempt to dequeue a route-update block
681 static struct zebra_dplane_ctx
*dplane_route_dequeue(void)
683 struct zebra_dplane_ctx
*ctx
= NULL
;
687 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
689 TAILQ_REMOVE(&zdplane_info
.dg_route_ctx_q
,
699 * Utility that prepares a route update and enqueues it for processing
701 static enum zebra_dplane_result
702 dplane_route_update_internal(struct route_node
*rn
,
703 struct route_entry
*re
,
704 struct route_entry
*old_re
,
707 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
709 struct zebra_dplane_ctx
*ctx
= NULL
;
711 /* Obtain context block */
712 ctx
= dplane_ctx_alloc();
718 /* Init context with info from zebra data structs */
719 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
721 /* Capture some extra info for update case
722 * where there's a different 'old' route.
724 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
725 old_re
&& (old_re
!= re
)) {
726 ctx
->zd_is_update
= true;
728 old_re
->dplane_sequence
++;
729 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
731 ctx
->zd_old_tag
= old_re
->tag
;
732 ctx
->zd_old_type
= old_re
->type
;
733 ctx
->zd_old_instance
= old_re
->instance
;
734 ctx
->zd_old_distance
= old_re
->distance
;
735 ctx
->zd_old_metric
= old_re
->metric
;
738 /* For bsd, capture previous re's nexthops too, sigh.
739 * We'll need these to do per-nexthop deletes.
741 copy_nexthops(&(ctx
->zd_old_ng
.nexthop
),
742 old_re
->ng
.nexthop
, NULL
);
743 #endif /* !HAVE_NETLINK */
746 /* Enqueue context for processing */
747 ret
= dplane_route_enqueue(ctx
);
752 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
753 memory_order_relaxed
);
756 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
758 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
759 memory_order_relaxed
);
760 dplane_ctx_free(&ctx
);
767 * Enqueue a route 'add' for the dataplane.
769 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
770 struct route_entry
*re
)
772 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
774 if (rn
== NULL
|| re
== NULL
)
777 ret
= dplane_route_update_internal(rn
, re
, NULL
,
778 DPLANE_OP_ROUTE_INSTALL
);
785 * Enqueue a route update for the dataplane.
787 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
788 struct route_entry
*re
,
789 struct route_entry
*old_re
)
791 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
793 if (rn
== NULL
|| re
== NULL
)
796 ret
= dplane_route_update_internal(rn
, re
, old_re
,
797 DPLANE_OP_ROUTE_UPDATE
);
803 * Enqueue a route removal for the dataplane.
805 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
806 struct route_entry
*re
)
808 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
810 if (rn
== NULL
|| re
== NULL
)
813 ret
= dplane_route_update_internal(rn
, re
, NULL
,
814 DPLANE_OP_ROUTE_DELETE
);
821 * Event handler function for routing updates
823 static int dplane_route_process(struct thread
*event
)
825 enum zebra_dplane_result res
;
826 struct zebra_dplane_ctx
*ctx
;
829 /* Check for shutdown */
830 if (!zdplane_info
.dg_run
)
833 /* TODO -- limit number of updates per cycle? */
834 ctx
= dplane_route_dequeue();
839 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, 1,
840 memory_order_relaxed
);
842 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
843 char dest_str
[PREFIX_STRLEN
];
845 prefix2str(dplane_ctx_get_dest(ctx
),
846 dest_str
, sizeof(dest_str
));
848 zlog_debug("%u:%s Dplane route update ctx %p op %s",
849 dplane_ctx_get_vrf(ctx
), dest_str
,
850 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
853 /* TODO -- support series of providers */
855 /* Initially, just doing kernel-facing update here */
856 res
= kernel_route_update(ctx
);
858 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
859 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
,
860 1, memory_order_relaxed
);
862 ctx
->zd_status
= res
;
864 /* Enqueue result to zebra main context */
865 zdplane_info
.dg_results_cb(ctx
);
874 * Handler for 'show dplane'
876 int dplane_show_helper(struct vty
*vty
, bool detailed
)
878 uint64_t queued
, limit
, queue_max
, errs
, incoming
;
880 /* Using atomics because counters are being changed in different
883 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
884 memory_order_relaxed
);
885 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
886 memory_order_relaxed
);
887 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
888 memory_order_relaxed
);
889 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
890 memory_order_relaxed
);
891 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
892 memory_order_relaxed
);
894 vty_out(vty
, "Route updates: %"PRIu64
"\n", incoming
);
895 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
896 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
897 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
898 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
904 * Handler for 'show dplane providers'
906 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
908 vty_out(vty
, "Zebra dataplane providers:%s\n",
909 (detailed
? " (detailed)" : ""));
915 * Provider registration
917 int dplane_provider_register(const char *name
,
918 enum dplane_provider_prio_e prio
,
919 dplane_provider_process_fp fp
,
920 dplane_provider_fini_fp fini_fp
)
923 struct zebra_dplane_provider
*p
, *last
;
931 if (prio
<= DPLANE_PRIO_NONE
||
932 prio
> DPLANE_PRIO_LAST
) {
937 /* Allocate and init new provider struct */
938 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
944 strncpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
945 p
->dp_name
[DPLANE_PROVIDER_NAMELEN
] = '\0'; /* Belt-and-suspenders */
947 p
->dp_priority
= prio
;
949 p
->dp_fini
= fini_fp
;
951 /* Lock the lock - the dplane pthread may be running */
954 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
956 /* Insert into list ordered by priority */
957 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_q_providers
) {
958 if (last
->dp_priority
> p
->dp_priority
)
963 TAILQ_INSERT_BEFORE(last
, p
, dp_q_providers
);
965 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
976 * Zebra registers a results callback with the dataplane system
978 int dplane_results_register(dplane_results_fp fp
)
980 zdplane_info
.dg_results_cb
= fp
;
985 * Initialize the dataplane module during startup, internal/private version
987 static void zebra_dplane_init_internal(struct zebra_t
*zebra
)
989 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
991 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
993 TAILQ_INIT(&zdplane_info
.dg_route_ctx_q
);
994 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
996 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
998 /* TODO -- register default kernel 'provider' during init */
1000 zdplane_info
.dg_run
= true;
1002 /* TODO -- start dataplane pthread. We're using the zebra
1003 * core/main thread temporarily
1005 zdplane_info
.dg_master
= zebra
->master
;
1008 /* Indicates zebra shutdown/exit is in progress. Some operations may be
1009 * simplified or skipped during shutdown processing.
1011 bool dplane_is_in_shutdown(void)
1013 return zdplane_info
.dg_is_shutdown
;
1017 * Early or pre-shutdown, de-init notification api. This runs pretty
1018 * early during zebra shutdown, as a signal to stop new work and prepare
1019 * for updates generated by shutdown/cleanup activity, as zebra tries to
1020 * remove everything it's responsible for.
1021 * NB: This runs in the main zebra thread context.
1023 void zebra_dplane_pre_finish(void)
1025 if (IS_ZEBRA_DEBUG_DPLANE
)
1026 zlog_debug("Zebra dataplane pre-fini called");
1028 zdplane_info
.dg_is_shutdown
= true;
1030 /* Notify provider(s) of pending shutdown */
1034 * Utility to determine whether work remains enqueued within the dplane;
1035 * used during system shutdown processing.
1037 static bool dplane_work_pending(void)
1039 struct zebra_dplane_ctx
*ctx
;
1041 /* TODO -- just checking incoming/pending work for now */
1044 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
1048 return (ctx
!= NULL
);
1052 * Shutdown-time intermediate callback, used to determine when all pending
1053 * in-flight updates are done. If there's still work to do, reschedules itself.
1054 * If all work is done, schedules an event to the main zebra thread for
1055 * final zebra shutdown.
1056 * This runs in the dplane pthread context.
1058 static int dplane_check_shutdown_status(struct thread
*event
)
1060 if (IS_ZEBRA_DEBUG_DPLANE
)
1061 zlog_debug("Zebra dataplane shutdown status check called");
1063 if (dplane_work_pending()) {
1064 /* Reschedule dplane check on a short timer */
1065 thread_add_timer_msec(zdplane_info
.dg_master
,
1066 dplane_check_shutdown_status
,
1068 &zdplane_info
.dg_t_shutdown_check
);
1070 /* TODO - give up and stop waiting after a short time? */
1073 /* We appear to be done - schedule a final callback event
1074 * for the zebra main pthread.
1076 thread_add_event(zebrad
.master
, zebra_finalize
, NULL
, 0, NULL
);
1083 * Shutdown, de-init api. This runs pretty late during shutdown,
1084 * after zebra has tried to free/remove/uninstall all routes during shutdown.
1085 * At this point, dplane work may still remain to be done, so we can't just
1086 * blindly terminate. If there's still work to do, we'll periodically check
1087 * and when done, we'll enqueue a task to the zebra main thread for final
1088 * termination processing.
1090 * NB: This runs in the main zebra thread context.
1092 void zebra_dplane_finish(void)
1094 if (IS_ZEBRA_DEBUG_DPLANE
)
1095 zlog_debug("Zebra dataplane fini called");
1097 thread_add_event(zdplane_info
.dg_master
,
1098 dplane_check_shutdown_status
, NULL
, 0,
1099 &zdplane_info
.dg_t_shutdown_check
);
1103 * Final phase of shutdown, after all work enqueued to dplane has been
1104 * processed. This is called from the zebra main pthread context.
1106 void zebra_dplane_shutdown(void)
1108 if (IS_ZEBRA_DEBUG_DPLANE
)
1109 zlog_debug("Zebra dataplane shutdown called");
1111 /* Stop dplane thread, if it's running */
1113 zdplane_info
.dg_run
= false;
1115 THREAD_OFF(zdplane_info
.dg_t_update
);
1118 /* frr_pthread_stop(...) */
1120 /* Notify provider(s) of final shutdown */
1122 /* Clean-up provider objects */
1124 /* Clean queue(s) */
1128 * Initialize the dataplane module at startup; called by zebra rib_init()
1130 void zebra_dplane_init(void)
1132 zebra_dplane_init_internal(&zebrad
);