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_router.h"
28 #include "zebra/zebra_memory.h"
29 #include "zebra/zserv.h"
30 #include "zebra/zebra_dplane.h"
32 #include "zebra/debug.h"
34 /* Memory type for context blocks */
35 DEFINE_MTYPE(ZEBRA
, DP_CTX
, "Zebra DPlane Ctx")
36 DEFINE_MTYPE(ZEBRA
, DP_PROV
, "Zebra DPlane Provider")
42 /* Enable test dataplane provider */
43 /*#define DPLANE_TEST_PROVIDER 1 */
45 /* Default value for max queued incoming updates */
46 const uint32_t DPLANE_DEFAULT_MAX_QUEUED
= 200;
48 /* Default value for new work per cycle */
49 const uint32_t DPLANE_DEFAULT_NEW_WORK
= 100;
51 /* Validation check macro for context blocks */
52 /* #define DPLANE_DEBUG 1 */
56 # define DPLANE_CTX_VALID(p) \
61 # define DPLANE_CTX_VALID(p)
63 #endif /* DPLANE_DEBUG */
66 * Route information captured for route updates.
68 struct dplane_route_info
{
70 /* Dest and (optional) source prefixes */
71 struct prefix zd_dest
;
81 route_tag_t zd_old_tag
;
83 uint32_t zd_old_metric
;
86 uint16_t zd_old_instance
;
89 uint8_t zd_old_distance
;
92 uint32_t zd_nexthop_mtu
;
95 struct nexthop_group zd_ng
;
97 /* "Previous" nexthops, used only in route updates without netlink */
98 struct nexthop_group zd_old_ng
;
100 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
105 * The context block used to exchange info about route updates across
106 * the boundary between the zebra main context (and pthread) and the
107 * dataplane layer (and pthread).
109 struct zebra_dplane_ctx
{
112 enum dplane_op_e zd_op
;
114 /* Status on return */
115 enum zebra_dplane_result zd_status
;
117 /* Dplane provider id */
118 uint32_t zd_provider
;
120 /* Flags - used by providers, e.g. */
128 /* TODO -- internal/sub-operation status? */
129 enum zebra_dplane_result zd_remote_status
;
130 enum zebra_dplane_result zd_kernel_status
;
133 uint32_t zd_table_id
;
135 /* Support info for either route or LSP update */
137 struct dplane_route_info rinfo
;
141 /* Namespace info, used especially for netlink kernel communication */
142 struct zebra_dplane_info zd_ns_info
;
144 /* Embedded list linkage */
145 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
148 /* Flag that can be set by a pre-kernel provider as a signal that an update
149 * should bypass the kernel.
151 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
155 * Registration block for one dataplane provider.
157 struct zebra_dplane_provider
{
159 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
161 /* Priority, for ordering among providers */
168 pthread_mutex_t dp_mutex
;
170 /* Plugin-provided extra data */
176 int (*dp_fp
)(struct zebra_dplane_provider
*prov
);
178 int (*dp_fini
)(struct zebra_dplane_provider
*prov
, bool early_p
);
180 _Atomic
uint32_t dp_in_counter
;
181 _Atomic
uint32_t dp_in_queued
;
182 _Atomic
uint32_t dp_in_max
;
183 _Atomic
uint32_t dp_out_counter
;
184 _Atomic
uint32_t dp_out_queued
;
185 _Atomic
uint32_t dp_out_max
;
186 _Atomic
uint32_t dp_error_counter
;
188 /* Queue of contexts inbound to the provider */
189 struct dplane_ctx_q dp_ctx_in_q
;
191 /* Queue of completed contexts outbound from the provider back
192 * towards the dataplane module.
194 struct dplane_ctx_q dp_ctx_out_q
;
196 /* Embedded list linkage for provider objects */
197 TAILQ_ENTRY(zebra_dplane_provider
) dp_prov_link
;
203 static struct zebra_dplane_globals
{
204 /* Mutex to control access to dataplane components */
205 pthread_mutex_t dg_mutex
;
207 /* Results callback registered by zebra 'core' */
208 int (*dg_results_cb
)(struct dplane_ctx_q
*ctxlist
);
210 /* Sentinel for beginning of shutdown */
211 volatile bool dg_is_shutdown
;
213 /* Sentinel for end of shutdown */
214 volatile bool dg_run
;
216 /* Route-update context queue inbound to the dataplane */
217 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_route_ctx_q
;
219 /* Ordered list of providers */
220 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
222 /* Counter used to assign internal ids to providers */
223 uint32_t dg_provider_id
;
225 /* Limit number of pending, unprocessed updates */
226 _Atomic
uint32_t dg_max_queued_updates
;
228 /* Limit number of new updates dequeued at once, to pace an
231 uint32_t dg_updates_per_cycle
;
233 _Atomic
uint32_t dg_routes_in
;
234 _Atomic
uint32_t dg_routes_queued
;
235 _Atomic
uint32_t dg_routes_queued_max
;
236 _Atomic
uint32_t dg_route_errors
;
237 _Atomic
uint32_t dg_other_errors
;
239 _Atomic
uint32_t dg_lsps_in
;
240 _Atomic
uint32_t dg_lsps_queued
;
241 _Atomic
uint32_t dg_lsps_queued_max
;
242 _Atomic
uint32_t dg_lsp_errors
;
244 _Atomic
uint32_t dg_update_yields
;
246 /* Dataplane pthread */
247 struct frr_pthread
*dg_pthread
;
249 /* Event-delivery context 'master' for the dplane */
250 struct thread_master
*dg_master
;
252 /* Event/'thread' pointer for queued updates */
253 struct thread
*dg_t_update
;
255 /* Event pointer for pending shutdown check loop */
256 struct thread
*dg_t_shutdown_check
;
261 * Lock and unlock for interactions with the zebra 'core' pthread
263 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
264 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
268 * Lock and unlock for individual providers
270 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
271 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
274 static int dplane_thread_loop(struct thread
*event
);
275 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
276 struct zebra_ns
*zns
);
277 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
278 enum dplane_op_e op
);
284 /* Obtain thread_master for dataplane thread */
285 struct thread_master
*dplane_get_thread_master(void)
287 return zdplane_info
.dg_master
;
291 * Allocate a dataplane update context
293 static struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
295 struct zebra_dplane_ctx
*p
;
297 /* TODO -- just alloc'ing memory, but would like to maintain
300 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
306 * Free a dataplane results context.
308 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
313 DPLANE_CTX_VALID(*pctx
);
315 /* TODO -- just freeing memory, but would like to maintain
319 /* Some internal allocations may need to be freed, depending on
320 * the type of info captured in the ctx.
322 switch ((*pctx
)->zd_op
) {
323 case DPLANE_OP_ROUTE_INSTALL
:
324 case DPLANE_OP_ROUTE_UPDATE
:
325 case DPLANE_OP_ROUTE_DELETE
:
327 /* Free allocated nexthops */
328 if ((*pctx
)->u
.rinfo
.zd_ng
.nexthop
) {
329 /* This deals with recursive nexthops too */
330 nexthops_free((*pctx
)->u
.rinfo
.zd_ng
.nexthop
);
332 (*pctx
)->u
.rinfo
.zd_ng
.nexthop
= NULL
;
335 if ((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
) {
336 /* This deals with recursive nexthops too */
337 nexthops_free((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
);
339 (*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
= NULL
;
344 case DPLANE_OP_LSP_INSTALL
:
345 case DPLANE_OP_LSP_UPDATE
:
346 case DPLANE_OP_LSP_DELETE
:
348 zebra_nhlfe_t
*nhlfe
, *next
;
350 /* Free allocated NHLFEs */
351 for (nhlfe
= (*pctx
)->u
.lsp
.nhlfe_list
; nhlfe
; nhlfe
= next
) {
354 zebra_mpls_nhlfe_del(nhlfe
);
357 /* Clear pointers in lsp struct, in case we're cacheing
358 * free context structs.
360 (*pctx
)->u
.lsp
.nhlfe_list
= NULL
;
361 (*pctx
)->u
.lsp
.best_nhlfe
= NULL
;
370 XFREE(MTYPE_DP_CTX
, *pctx
);
375 * Return a context block to the dplane module after processing
377 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
379 /* TODO -- maintain pool; for now, just free */
380 dplane_ctx_free(pctx
);
383 /* Enqueue a context block */
384 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
385 const struct zebra_dplane_ctx
*ctx
)
387 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
390 /* Append a list of context blocks to another list */
391 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
392 struct dplane_ctx_q
*from_list
)
394 if (TAILQ_FIRST(from_list
)) {
395 TAILQ_CONCAT(to_list
, from_list
, zd_q_entries
);
397 /* And clear 'from' list */
398 TAILQ_INIT(from_list
);
402 /* Dequeue a context block from the head of a list */
403 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
)
405 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
408 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
414 * Accessors for information from the context object
416 enum zebra_dplane_result
dplane_ctx_get_status(
417 const struct zebra_dplane_ctx
*ctx
)
419 DPLANE_CTX_VALID(ctx
);
421 return ctx
->zd_status
;
424 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
425 enum zebra_dplane_result status
)
427 DPLANE_CTX_VALID(ctx
);
429 ctx
->zd_status
= status
;
432 /* Retrieve last/current provider id */
433 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
)
435 DPLANE_CTX_VALID(ctx
);
436 return ctx
->zd_provider
;
439 /* Providers run before the kernel can control whether a kernel
440 * update should be done.
442 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
)
444 DPLANE_CTX_VALID(ctx
);
446 SET_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
449 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
)
451 DPLANE_CTX_VALID(ctx
);
453 return CHECK_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
456 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
458 DPLANE_CTX_VALID(ctx
);
463 const char *dplane_op2str(enum dplane_op_e op
)
465 const char *ret
= "UNKNOWN";
473 case DPLANE_OP_ROUTE_INSTALL
:
474 ret
= "ROUTE_INSTALL";
476 case DPLANE_OP_ROUTE_UPDATE
:
477 ret
= "ROUTE_UPDATE";
479 case DPLANE_OP_ROUTE_DELETE
:
480 ret
= "ROUTE_DELETE";
483 case DPLANE_OP_LSP_INSTALL
:
486 case DPLANE_OP_LSP_UPDATE
:
489 case DPLANE_OP_LSP_DELETE
:
498 const char *dplane_res2str(enum zebra_dplane_result res
)
500 const char *ret
= "<Unknown>";
503 case ZEBRA_DPLANE_REQUEST_FAILURE
:
506 case ZEBRA_DPLANE_REQUEST_QUEUED
:
509 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
517 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
519 DPLANE_CTX_VALID(ctx
);
521 return &(ctx
->u
.rinfo
.zd_dest
);
524 /* Source prefix is a little special - return NULL for "no src prefix" */
525 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
527 DPLANE_CTX_VALID(ctx
);
529 if (ctx
->u
.rinfo
.zd_src
.prefixlen
== 0 &&
530 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->u
.rinfo
.zd_src
.u
.prefix6
))) {
533 return &(ctx
->u
.rinfo
.zd_src
);
537 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
539 DPLANE_CTX_VALID(ctx
);
541 return ctx
->zd_is_update
;
544 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
546 DPLANE_CTX_VALID(ctx
);
551 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
553 DPLANE_CTX_VALID(ctx
);
555 return ctx
->zd_old_seq
;
558 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
560 DPLANE_CTX_VALID(ctx
);
562 return ctx
->zd_vrf_id
;
565 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
567 DPLANE_CTX_VALID(ctx
);
569 return ctx
->u
.rinfo
.zd_type
;
572 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
574 DPLANE_CTX_VALID(ctx
);
576 return ctx
->u
.rinfo
.zd_old_type
;
579 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
581 DPLANE_CTX_VALID(ctx
);
583 return ctx
->u
.rinfo
.zd_afi
;
586 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
588 DPLANE_CTX_VALID(ctx
);
590 return ctx
->u
.rinfo
.zd_safi
;
593 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
595 DPLANE_CTX_VALID(ctx
);
597 return ctx
->zd_table_id
;
600 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
602 DPLANE_CTX_VALID(ctx
);
604 return ctx
->u
.rinfo
.zd_tag
;
607 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
609 DPLANE_CTX_VALID(ctx
);
611 return ctx
->u
.rinfo
.zd_old_tag
;
614 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
616 DPLANE_CTX_VALID(ctx
);
618 return ctx
->u
.rinfo
.zd_instance
;
621 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
623 DPLANE_CTX_VALID(ctx
);
625 return ctx
->u
.rinfo
.zd_old_instance
;
628 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
630 DPLANE_CTX_VALID(ctx
);
632 return ctx
->u
.rinfo
.zd_metric
;
635 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
637 DPLANE_CTX_VALID(ctx
);
639 return ctx
->u
.rinfo
.zd_old_metric
;
642 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
644 DPLANE_CTX_VALID(ctx
);
646 return ctx
->u
.rinfo
.zd_mtu
;
649 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
651 DPLANE_CTX_VALID(ctx
);
653 return ctx
->u
.rinfo
.zd_nexthop_mtu
;
656 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
658 DPLANE_CTX_VALID(ctx
);
660 return ctx
->u
.rinfo
.zd_distance
;
663 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
665 DPLANE_CTX_VALID(ctx
);
667 return ctx
->u
.rinfo
.zd_old_distance
;
670 const struct nexthop_group
*dplane_ctx_get_ng(
671 const struct zebra_dplane_ctx
*ctx
)
673 DPLANE_CTX_VALID(ctx
);
675 return &(ctx
->u
.rinfo
.zd_ng
);
678 const struct nexthop_group
*dplane_ctx_get_old_ng(
679 const struct zebra_dplane_ctx
*ctx
)
681 DPLANE_CTX_VALID(ctx
);
683 return &(ctx
->u
.rinfo
.zd_old_ng
);
686 const struct zebra_dplane_info
*dplane_ctx_get_ns(
687 const struct zebra_dplane_ctx
*ctx
)
689 DPLANE_CTX_VALID(ctx
);
691 return &(ctx
->zd_ns_info
);
694 /* Accessors for LSP information */
696 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
)
698 DPLANE_CTX_VALID(ctx
);
700 return ctx
->u
.lsp
.ile
.in_label
;
703 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
)
705 DPLANE_CTX_VALID(ctx
);
707 return ctx
->u
.lsp
.addr_family
;
710 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
)
712 DPLANE_CTX_VALID(ctx
);
714 return ctx
->u
.lsp
.flags
;
717 zebra_nhlfe_t
*dplane_ctx_get_nhlfe(struct zebra_dplane_ctx
*ctx
)
719 DPLANE_CTX_VALID(ctx
);
721 return ctx
->u
.lsp
.nhlfe_list
;
724 zebra_nhlfe_t
*dplane_ctx_get_best_nhlfe(struct zebra_dplane_ctx
*ctx
)
726 DPLANE_CTX_VALID(ctx
);
728 return ctx
->u
.lsp
.best_nhlfe
;
731 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
)
733 DPLANE_CTX_VALID(ctx
);
735 return ctx
->u
.lsp
.num_ecmp
;
739 * End of dplane context accessors
744 * Retrieve the limit on the number of pending, unprocessed updates.
746 uint32_t dplane_get_in_queue_limit(void)
748 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
749 memory_order_relaxed
);
753 * Configure limit on the number of pending, queued updates.
755 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
757 /* Reset to default on 'unset' */
759 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
761 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
762 memory_order_relaxed
);
766 * Retrieve the current queue depth of incoming, unprocessed updates
768 uint32_t dplane_get_in_queue_len(void)
770 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
771 memory_order_seq_cst
);
775 * Common dataplane context init with zebra namespace info.
777 static int dplane_ctx_ns_init(struct zebra_dplane_ctx
*ctx
,
778 struct zebra_ns
*zns
,
781 dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
);
783 #if defined(HAVE_NETLINK)
784 /* Increment message counter after copying to context struct - may need
785 * two messages in some 'update' cases.
788 zns
->netlink_dplane
.seq
+= 2;
790 zns
->netlink_dplane
.seq
++;
791 #endif /* HAVE_NETLINK */
797 * Initialize a context block for a route update from zebra data structs.
799 static int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
,
801 struct route_node
*rn
,
802 struct route_entry
*re
)
805 const struct route_table
*table
= NULL
;
806 const rib_table_info_t
*info
;
807 const struct prefix
*p
, *src_p
;
808 struct zebra_ns
*zns
;
809 struct zebra_vrf
*zvrf
;
810 struct nexthop
*nexthop
;
812 if (!ctx
|| !rn
|| !re
)
816 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
818 ctx
->u
.rinfo
.zd_type
= re
->type
;
819 ctx
->u
.rinfo
.zd_old_type
= re
->type
;
821 /* Prefixes: dest, and optional source */
822 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
824 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), p
);
827 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src_p
);
829 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(ctx
->u
.rinfo
.zd_src
));
831 ctx
->zd_table_id
= re
->table
;
833 ctx
->u
.rinfo
.zd_metric
= re
->metric
;
834 ctx
->u
.rinfo
.zd_old_metric
= re
->metric
;
835 ctx
->zd_vrf_id
= re
->vrf_id
;
836 ctx
->u
.rinfo
.zd_mtu
= re
->mtu
;
837 ctx
->u
.rinfo
.zd_nexthop_mtu
= re
->nexthop_mtu
;
838 ctx
->u
.rinfo
.zd_instance
= re
->instance
;
839 ctx
->u
.rinfo
.zd_tag
= re
->tag
;
840 ctx
->u
.rinfo
.zd_old_tag
= re
->tag
;
841 ctx
->u
.rinfo
.zd_distance
= re
->distance
;
843 table
= srcdest_rnode_table(rn
);
846 ctx
->u
.rinfo
.zd_afi
= info
->afi
;
847 ctx
->u
.rinfo
.zd_safi
= info
->safi
;
849 /* Extract ns info - can't use pointers to 'core' structs */
850 zvrf
= vrf_info_lookup(re
->vrf_id
);
853 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_ROUTE_UPDATE
));
855 /* Copy nexthops; recursive info is included too */
856 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
858 /* TODO -- maybe use array of nexthops to avoid allocs? */
860 /* Ensure that the dplane's nexthops flags are clear. */
861 for (ALL_NEXTHOPS(ctx
->u
.rinfo
.zd_ng
, nexthop
))
862 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
864 /* Trying out the sequence number idea, so we can try to detect
865 * when a result is stale.
867 re
->dplane_sequence
= zebra_router_get_next_sequence();
868 ctx
->zd_seq
= re
->dplane_sequence
;
877 * Capture information for an LSP update in a dplane context.
879 static int dplane_ctx_lsp_init(struct zebra_dplane_ctx
*ctx
,
884 zebra_nhlfe_t
*nhlfe
, *new_nhlfe
;
886 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
887 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
888 dplane_op2str(op
), lsp
->ile
.in_label
,
892 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
894 /* Capture namespace info */
895 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
896 (op
== DPLANE_OP_LSP_UPDATE
));
898 memset(&ctx
->u
.lsp
, 0, sizeof(ctx
->u
.lsp
));
900 ctx
->u
.lsp
.ile
= lsp
->ile
;
901 ctx
->u
.lsp
.addr_family
= lsp
->addr_family
;
902 ctx
->u
.lsp
.num_ecmp
= lsp
->num_ecmp
;
903 ctx
->u
.lsp
.flags
= lsp
->flags
;
905 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
906 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
907 /* Not sure if this is meaningful... */
908 if (nhlfe
->nexthop
== NULL
)
912 zebra_mpls_lsp_add_nhlfe(
915 nhlfe
->nexthop
->type
,
916 &(nhlfe
->nexthop
->gate
),
917 nhlfe
->nexthop
->ifindex
,
918 nhlfe
->nexthop
->nh_label
->label
[0]);
920 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
925 /* Need to copy flags too */
926 new_nhlfe
->flags
= nhlfe
->flags
;
927 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
929 if (nhlfe
== lsp
->best_nhlfe
)
930 ctx
->u
.lsp
.best_nhlfe
= new_nhlfe
;
933 /* On error the ctx will be cleaned-up, so we don't need to
934 * deal with any allocated nhlfe or nexthop structs here.
941 * Enqueue a new route update,
942 * and ensure an event is active for the dataplane pthread.
944 static int dplane_route_enqueue(struct zebra_dplane_ctx
*ctx
)
949 /* Enqueue for processing by the dataplane pthread */
952 TAILQ_INSERT_TAIL(&zdplane_info
.dg_route_ctx_q
, ctx
,
957 curr
= atomic_add_fetch_explicit(
959 /* TODO -- issue with the clang atomic/intrinsics currently;
960 * casting away the 'Atomic'-ness of the variable works.
962 (uint32_t *)&(zdplane_info
.dg_routes_queued
),
964 &(zdplane_info
.dg_routes_queued
),
966 1, memory_order_seq_cst
);
968 /* Maybe update high-water counter also */
969 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
970 memory_order_seq_cst
);
971 while (high
< curr
) {
972 if (atomic_compare_exchange_weak_explicit(
973 &zdplane_info
.dg_routes_queued_max
,
975 memory_order_seq_cst
,
976 memory_order_seq_cst
))
980 /* Ensure that an event for the dataplane thread is active */
981 ret
= dplane_provider_work_ready();
987 * Utility that prepares a route update and enqueues it for processing
989 static enum zebra_dplane_result
990 dplane_route_update_internal(struct route_node
*rn
,
991 struct route_entry
*re
,
992 struct route_entry
*old_re
,
995 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
997 struct zebra_dplane_ctx
*ctx
= NULL
;
999 /* Obtain context block */
1000 ctx
= dplane_ctx_alloc();
1006 /* Init context with info from zebra data structs */
1007 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
1009 /* Capture some extra info for update case
1010 * where there's a different 'old' route.
1012 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
1013 old_re
&& (old_re
!= re
)) {
1014 ctx
->zd_is_update
= true;
1016 old_re
->dplane_sequence
=
1017 zebra_router_get_next_sequence();
1018 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
1020 ctx
->u
.rinfo
.zd_old_tag
= old_re
->tag
;
1021 ctx
->u
.rinfo
.zd_old_type
= old_re
->type
;
1022 ctx
->u
.rinfo
.zd_old_instance
= old_re
->instance
;
1023 ctx
->u
.rinfo
.zd_old_distance
= old_re
->distance
;
1024 ctx
->u
.rinfo
.zd_old_metric
= old_re
->metric
;
1026 #ifndef HAVE_NETLINK
1027 /* For bsd, capture previous re's nexthops too, sigh.
1028 * We'll need these to do per-nexthop deletes.
1030 copy_nexthops(&(ctx
->u
.rinfo
.zd_old_ng
.nexthop
),
1031 old_re
->ng
.nexthop
, NULL
);
1032 #endif /* !HAVE_NETLINK */
1035 /* Enqueue context for processing */
1036 ret
= dplane_route_enqueue(ctx
);
1040 /* Update counter */
1041 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
1042 memory_order_relaxed
);
1045 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1047 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
1048 memory_order_relaxed
);
1050 dplane_ctx_free(&ctx
);
1057 * Enqueue a route 'add' for the dataplane.
1059 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
1060 struct route_entry
*re
)
1062 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1064 if (rn
== NULL
|| re
== NULL
)
1067 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1068 DPLANE_OP_ROUTE_INSTALL
);
1075 * Enqueue a route update for the dataplane.
1077 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
1078 struct route_entry
*re
,
1079 struct route_entry
*old_re
)
1081 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1083 if (rn
== NULL
|| re
== NULL
)
1086 ret
= dplane_route_update_internal(rn
, re
, old_re
,
1087 DPLANE_OP_ROUTE_UPDATE
);
1093 * Enqueue a route removal for the dataplane.
1095 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
1096 struct route_entry
*re
)
1098 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1100 if (rn
== NULL
|| re
== NULL
)
1103 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1104 DPLANE_OP_ROUTE_DELETE
);
1111 * Enqueue LSP add for the dataplane.
1113 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
)
1115 enum zebra_dplane_result ret
=
1116 lsp_update_internal(lsp
, DPLANE_OP_LSP_INSTALL
);
1122 * Enqueue LSP update for the dataplane.
1124 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
)
1126 enum zebra_dplane_result ret
=
1127 lsp_update_internal(lsp
, DPLANE_OP_LSP_UPDATE
);
1133 * Enqueue LSP delete for the dataplane.
1135 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
)
1137 enum zebra_dplane_result ret
=
1138 lsp_update_internal(lsp
, DPLANE_OP_LSP_DELETE
);
1144 * Common internal LSP update utility
1146 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
1147 enum dplane_op_e op
)
1149 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1151 struct zebra_dplane_ctx
*ctx
= NULL
;
1153 /* Obtain context block */
1154 ctx
= dplane_ctx_alloc();
1160 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1164 ret
= dplane_route_enqueue(ctx
);
1167 /* Update counter */
1168 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1169 memory_order_relaxed
);
1172 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1174 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1175 memory_order_relaxed
);
1177 dplane_ctx_free(&ctx
);
1184 * Handler for 'show dplane'
1186 int dplane_show_helper(struct vty
*vty
, bool detailed
)
1188 uint64_t queued
, queue_max
, limit
, errs
, incoming
, yields
,
1191 /* Using atomics because counters are being changed in different
1194 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
1195 memory_order_relaxed
);
1196 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
1197 memory_order_relaxed
);
1198 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
1199 memory_order_relaxed
);
1200 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1201 memory_order_relaxed
);
1202 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
1203 memory_order_relaxed
);
1204 yields
= atomic_load_explicit(&zdplane_info
.dg_update_yields
,
1205 memory_order_relaxed
);
1206 other_errs
= atomic_load_explicit(&zdplane_info
.dg_other_errors
,
1207 memory_order_relaxed
);
1209 vty_out(vty
, "Zebra dataplane:\nRoute updates: %"PRIu64
"\n",
1211 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
1212 vty_out(vty
, "Other errors : %"PRIu64
"\n", other_errs
);
1213 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
1214 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
1215 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
1216 vty_out(vty
, "Dplane update yields: %"PRIu64
"\n", yields
);
1222 * Handler for 'show dplane providers'
1224 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
1226 struct zebra_dplane_provider
*prov
;
1227 uint64_t in
, in_max
, out
, out_max
;
1229 vty_out(vty
, "Zebra dataplane providers:\n");
1232 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1235 /* Show counters, useful info from each registered provider */
1238 in
= atomic_load_explicit(&prov
->dp_in_counter
,
1239 memory_order_relaxed
);
1240 in_max
= atomic_load_explicit(&prov
->dp_in_max
,
1241 memory_order_relaxed
);
1242 out
= atomic_load_explicit(&prov
->dp_out_counter
,
1243 memory_order_relaxed
);
1244 out_max
= atomic_load_explicit(&prov
->dp_out_max
,
1245 memory_order_relaxed
);
1247 vty_out(vty
, "%s (%u): in: %"PRIu64
", q_max: %"PRIu64
", "
1248 "out: %"PRIu64
", q_max: %"PRIu64
"\n",
1249 prov
->dp_name
, prov
->dp_id
, in
, in_max
, out
, out_max
);
1252 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
1260 * Provider registration
1262 int dplane_provider_register(const char *name
,
1263 enum dplane_provider_prio prio
,
1265 int (*fp
)(struct zebra_dplane_provider
*),
1266 int (*fini_fp
)(struct zebra_dplane_provider
*,
1269 struct zebra_dplane_provider
**prov_p
)
1272 struct zebra_dplane_provider
*p
= NULL
, *last
;
1280 if (prio
<= DPLANE_PRIO_NONE
||
1281 prio
> DPLANE_PRIO_LAST
) {
1286 /* Allocate and init new provider struct */
1287 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
1293 pthread_mutex_init(&(p
->dp_mutex
), NULL
);
1294 TAILQ_INIT(&(p
->dp_ctx_in_q
));
1295 TAILQ_INIT(&(p
->dp_ctx_out_q
));
1297 p
->dp_priority
= prio
;
1299 p
->dp_fini
= fini_fp
;
1302 /* Lock - the dplane pthread may be running */
1305 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
1308 strlcpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
1310 snprintf(p
->dp_name
, DPLANE_PROVIDER_NAMELEN
,
1311 "provider-%u", p
->dp_id
);
1313 /* Insert into list ordered by priority */
1314 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
1315 if (last
->dp_priority
> p
->dp_priority
)
1320 TAILQ_INSERT_BEFORE(last
, p
, dp_prov_link
);
1322 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
1328 if (IS_ZEBRA_DEBUG_DPLANE
)
1329 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
1330 p
->dp_name
, p
->dp_id
, p
->dp_priority
);
1339 /* Accessors for provider attributes */
1340 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
)
1342 return prov
->dp_name
;
1345 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
)
1350 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
)
1352 return prov
->dp_data
;
1355 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
)
1357 return zdplane_info
.dg_updates_per_cycle
;
1360 /* Lock/unlock a provider's mutex - iff the provider was registered with
1361 * the THREADED flag.
1363 void dplane_provider_lock(struct zebra_dplane_provider
*prov
)
1365 if (dplane_provider_is_threaded(prov
))
1366 DPLANE_PROV_LOCK(prov
);
1369 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
)
1371 if (dplane_provider_is_threaded(prov
))
1372 DPLANE_PROV_UNLOCK(prov
);
1376 * Dequeue and maintain associated counter
1378 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
1379 struct zebra_dplane_provider
*prov
)
1381 struct zebra_dplane_ctx
*ctx
= NULL
;
1383 dplane_provider_lock(prov
);
1385 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1387 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
1389 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, 1,
1390 memory_order_relaxed
);
1393 dplane_provider_unlock(prov
);
1399 * Dequeue work to a list, return count
1401 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
1402 struct dplane_ctx_q
*listp
)
1405 struct zebra_dplane_ctx
*ctx
;
1407 limit
= zdplane_info
.dg_updates_per_cycle
;
1409 dplane_provider_lock(prov
);
1411 for (ret
= 0; ret
< limit
; ret
++) {
1412 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1414 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
1416 TAILQ_INSERT_TAIL(listp
, ctx
, zd_q_entries
);
1423 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, ret
,
1424 memory_order_relaxed
);
1426 dplane_provider_unlock(prov
);
1432 * Enqueue and maintain associated counter
1434 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
1435 struct zebra_dplane_ctx
*ctx
)
1437 dplane_provider_lock(prov
);
1439 TAILQ_INSERT_TAIL(&(prov
->dp_ctx_out_q
), ctx
,
1442 dplane_provider_unlock(prov
);
1444 atomic_fetch_add_explicit(&(prov
->dp_out_counter
), 1,
1445 memory_order_relaxed
);
1449 * Accessor for provider object
1451 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
)
1453 return (prov
->dp_flags
& DPLANE_PROV_FLAG_THREADED
);
1457 * Internal helper that copies information from a zebra ns object; this is
1458 * called in the zebra main pthread context as part of dplane ctx init.
1460 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
1461 struct zebra_ns
*zns
)
1463 ns_info
->ns_id
= zns
->ns_id
;
1465 #if defined(HAVE_NETLINK)
1466 ns_info
->is_cmd
= true;
1467 ns_info
->nls
= zns
->netlink_dplane
;
1468 #endif /* NETLINK */
1472 * Provider api to signal that work/events are available
1473 * for the dataplane pthread.
1475 int dplane_provider_work_ready(void)
1477 /* Note that during zebra startup, we may be offered work before
1478 * the dataplane pthread (and thread-master) are ready. We want to
1479 * enqueue the work, but the event-scheduling machinery may not be
1482 if (zdplane_info
.dg_run
) {
1483 thread_add_event(zdplane_info
.dg_master
,
1484 dplane_thread_loop
, NULL
, 0,
1485 &zdplane_info
.dg_t_update
);
1492 * Kernel dataplane provider
1496 * Handler for kernel LSP updates
1498 static enum zebra_dplane_result
1499 kernel_dplane_lsp_update(struct zebra_dplane_ctx
*ctx
)
1501 enum zebra_dplane_result res
;
1503 /* Call into the synchronous kernel-facing code here */
1504 res
= kernel_lsp_update(ctx
);
1506 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1507 atomic_fetch_add_explicit(
1508 &zdplane_info
.dg_lsp_errors
, 1,
1509 memory_order_relaxed
);
1515 * Handler for kernel route updates
1517 static enum zebra_dplane_result
1518 kernel_dplane_route_update(struct zebra_dplane_ctx
*ctx
)
1520 enum zebra_dplane_result res
;
1522 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1523 char dest_str
[PREFIX_STRLEN
];
1525 prefix2str(dplane_ctx_get_dest(ctx
),
1526 dest_str
, sizeof(dest_str
));
1528 zlog_debug("%u:%s Dplane route update ctx %p op %s",
1529 dplane_ctx_get_vrf(ctx
), dest_str
,
1530 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
1533 /* Call into the synchronous kernel-facing code here */
1534 res
= kernel_route_update(ctx
);
1536 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1537 atomic_fetch_add_explicit(
1538 &zdplane_info
.dg_route_errors
, 1,
1539 memory_order_relaxed
);
1545 * Kernel provider callback
1547 static int kernel_dplane_process_func(struct zebra_dplane_provider
*prov
)
1549 enum zebra_dplane_result res
;
1550 struct zebra_dplane_ctx
*ctx
;
1553 limit
= dplane_provider_get_work_limit(prov
);
1555 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1556 zlog_debug("dplane provider '%s': processing",
1557 dplane_provider_get_name(prov
));
1559 for (counter
= 0; counter
< limit
; counter
++) {
1561 ctx
= dplane_provider_dequeue_in_ctx(prov
);
1565 /* Dispatch to appropriate kernel-facing apis */
1566 switch (dplane_ctx_get_op(ctx
)) {
1568 case DPLANE_OP_ROUTE_INSTALL
:
1569 case DPLANE_OP_ROUTE_UPDATE
:
1570 case DPLANE_OP_ROUTE_DELETE
:
1571 res
= kernel_dplane_route_update(ctx
);
1574 case DPLANE_OP_LSP_INSTALL
:
1575 case DPLANE_OP_LSP_UPDATE
:
1576 case DPLANE_OP_LSP_DELETE
:
1577 res
= kernel_dplane_lsp_update(ctx
);
1581 atomic_fetch_add_explicit(
1582 &zdplane_info
.dg_other_errors
, 1,
1583 memory_order_relaxed
);
1585 res
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1589 dplane_ctx_set_status(ctx
, res
);
1591 dplane_provider_enqueue_out_ctx(prov
, ctx
);
1594 /* Ensure that we'll run the work loop again if there's still
1597 if (counter
>= limit
) {
1598 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1599 zlog_debug("dplane provider '%s' reached max updates %d",
1600 dplane_provider_get_name(prov
), counter
);
1602 atomic_fetch_add_explicit(&zdplane_info
.dg_update_yields
,
1603 1, memory_order_relaxed
);
1605 dplane_provider_work_ready();
1611 #if DPLANE_TEST_PROVIDER
1614 * Test dataplane provider plugin
1618 * Test provider process callback
1620 static int test_dplane_process_func(struct zebra_dplane_provider
*prov
)
1622 struct zebra_dplane_ctx
*ctx
;
1625 /* Just moving from 'in' queue to 'out' queue */
1627 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1628 zlog_debug("dplane provider '%s': processing",
1629 dplane_provider_get_name(prov
));
1631 limit
= dplane_provider_get_work_limit(prov
);
1633 for (counter
= 0; counter
< limit
; counter
++) {
1635 ctx
= dplane_provider_dequeue_in_ctx(prov
);
1639 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1641 dplane_provider_enqueue_out_ctx(prov
, ctx
);
1644 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1645 zlog_debug("dplane provider '%s': processed %d",
1646 dplane_provider_get_name(prov
), counter
);
1648 /* Ensure that we'll run the work loop again if there's still
1651 if (counter
>= limit
)
1652 dplane_provider_work_ready();
1658 * Test provider shutdown/fini callback
1660 static int test_dplane_shutdown_func(struct zebra_dplane_provider
*prov
,
1663 if (IS_ZEBRA_DEBUG_DPLANE
)
1664 zlog_debug("dplane provider '%s': %sshutdown",
1665 dplane_provider_get_name(prov
),
1666 early
? "early " : "");
1670 #endif /* DPLANE_TEST_PROVIDER */
1673 * Register default kernel provider
1675 static void dplane_provider_init(void)
1679 ret
= dplane_provider_register("Kernel",
1681 DPLANE_PROV_FLAGS_DEFAULT
,
1682 kernel_dplane_process_func
,
1687 zlog_err("Unable to register kernel dplane provider: %d",
1690 #if DPLANE_TEST_PROVIDER
1691 /* Optional test provider ... */
1692 ret
= dplane_provider_register("Test",
1693 DPLANE_PRIO_PRE_KERNEL
,
1694 DPLANE_PROV_FLAGS_DEFAULT
,
1695 test_dplane_process_func
,
1696 test_dplane_shutdown_func
,
1697 NULL
/* data */, NULL
);
1700 zlog_err("Unable to register test dplane provider: %d",
1702 #endif /* DPLANE_TEST_PROVIDER */
1705 /* Indicates zebra shutdown/exit is in progress. Some operations may be
1706 * simplified or skipped during shutdown processing.
1708 bool dplane_is_in_shutdown(void)
1710 return zdplane_info
.dg_is_shutdown
;
1714 * Early or pre-shutdown, de-init notification api. This runs pretty
1715 * early during zebra shutdown, as a signal to stop new work and prepare
1716 * for updates generated by shutdown/cleanup activity, as zebra tries to
1717 * remove everything it's responsible for.
1718 * NB: This runs in the main zebra pthread context.
1720 void zebra_dplane_pre_finish(void)
1722 if (IS_ZEBRA_DEBUG_DPLANE
)
1723 zlog_debug("Zebra dataplane pre-fini called");
1725 zdplane_info
.dg_is_shutdown
= true;
1727 /* TODO -- Notify provider(s) of pending shutdown */
1731 * Utility to determine whether work remains enqueued within the dplane;
1732 * used during system shutdown processing.
1734 static bool dplane_work_pending(void)
1737 struct zebra_dplane_ctx
*ctx
;
1738 struct zebra_dplane_provider
*prov
;
1740 /* TODO -- just checking incoming/pending work for now, must check
1745 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
1746 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1757 dplane_provider_lock(prov
);
1759 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1761 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
1763 dplane_provider_unlock(prov
);
1769 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
1781 * Shutdown-time intermediate callback, used to determine when all pending
1782 * in-flight updates are done. If there's still work to do, reschedules itself.
1783 * If all work is done, schedules an event to the main zebra thread for
1784 * final zebra shutdown.
1785 * This runs in the dplane pthread context.
1787 static int dplane_check_shutdown_status(struct thread
*event
)
1789 if (IS_ZEBRA_DEBUG_DPLANE
)
1790 zlog_debug("Zebra dataplane shutdown status check called");
1792 if (dplane_work_pending()) {
1793 /* Reschedule dplane check on a short timer */
1794 thread_add_timer_msec(zdplane_info
.dg_master
,
1795 dplane_check_shutdown_status
,
1797 &zdplane_info
.dg_t_shutdown_check
);
1799 /* TODO - give up and stop waiting after a short time? */
1802 /* We appear to be done - schedule a final callback event
1803 * for the zebra main pthread.
1805 thread_add_event(zebrad
.master
, zebra_finalize
, NULL
, 0, NULL
);
1812 * Shutdown, de-init api. This runs pretty late during shutdown,
1813 * after zebra has tried to free/remove/uninstall all routes during shutdown.
1814 * At this point, dplane work may still remain to be done, so we can't just
1815 * blindly terminate. If there's still work to do, we'll periodically check
1816 * and when done, we'll enqueue a task to the zebra main thread for final
1817 * termination processing.
1819 * NB: This runs in the main zebra thread context.
1821 void zebra_dplane_finish(void)
1823 if (IS_ZEBRA_DEBUG_DPLANE
)
1824 zlog_debug("Zebra dataplane fini called");
1826 thread_add_event(zdplane_info
.dg_master
,
1827 dplane_check_shutdown_status
, NULL
, 0,
1828 &zdplane_info
.dg_t_shutdown_check
);
1832 * Main dataplane pthread event loop. The thread takes new incoming work
1833 * and offers it to the first provider. It then iterates through the
1834 * providers, taking complete work from each one and offering it
1835 * to the next in order. At each step, a limited number of updates are
1836 * processed during a cycle in order to provide some fairness.
1838 * This loop through the providers is only run once, so that the dataplane
1839 * pthread can look for other pending work - such as i/o work on behalf of
1842 static int dplane_thread_loop(struct thread
*event
)
1844 struct dplane_ctx_q work_list
;
1845 struct dplane_ctx_q error_list
;
1846 struct zebra_dplane_provider
*prov
;
1847 struct zebra_dplane_ctx
*ctx
, *tctx
;
1848 int limit
, counter
, error_counter
;
1849 uint64_t curr
, high
;
1851 /* Capture work limit per cycle */
1852 limit
= zdplane_info
.dg_updates_per_cycle
;
1854 /* Init temporary lists used to move contexts among providers */
1855 TAILQ_INIT(&work_list
);
1856 TAILQ_INIT(&error_list
);
1859 /* Check for zebra shutdown */
1860 if (!zdplane_info
.dg_run
)
1863 /* Dequeue some incoming work from zebra (if any) onto the temporary
1868 /* Locate initial registered provider */
1869 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1871 /* Move new work from incoming list to temp list */
1872 for (counter
= 0; counter
< limit
; counter
++) {
1873 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
1875 TAILQ_REMOVE(&zdplane_info
.dg_route_ctx_q
, ctx
,
1878 ctx
->zd_provider
= prov
->dp_id
;
1880 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
1888 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, counter
,
1889 memory_order_relaxed
);
1891 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1892 zlog_debug("dplane: incoming new work counter: %d", counter
);
1894 /* Iterate through the registered providers, offering new incoming
1895 * work. If the provider has outgoing work in its queue, take that
1896 * work for the next provider
1900 /* At each iteration, the temporary work list has 'counter'
1903 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1904 zlog_debug("dplane enqueues %d new work to provider '%s'",
1905 counter
, dplane_provider_get_name(prov
));
1907 /* Capture current provider id in each context; check for
1910 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, tctx
) {
1911 if (dplane_ctx_get_status(ctx
) ==
1912 ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1913 ctx
->zd_provider
= prov
->dp_id
;
1916 * TODO -- improve error-handling: recirc
1917 * errors backwards so that providers can
1918 * 'undo' their work (if they want to)
1921 /* Move to error list; will be returned
1924 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
1925 TAILQ_INSERT_TAIL(&error_list
,
1931 /* Enqueue new work to the provider */
1932 dplane_provider_lock(prov
);
1934 if (TAILQ_FIRST(&work_list
))
1935 TAILQ_CONCAT(&(prov
->dp_ctx_in_q
), &work_list
,
1938 atomic_fetch_add_explicit(&prov
->dp_in_counter
, counter
,
1939 memory_order_relaxed
);
1940 atomic_fetch_add_explicit(&prov
->dp_in_queued
, counter
,
1941 memory_order_relaxed
);
1942 curr
= atomic_load_explicit(&prov
->dp_in_queued
,
1943 memory_order_relaxed
);
1944 high
= atomic_load_explicit(&prov
->dp_in_max
,
1945 memory_order_relaxed
);
1947 atomic_store_explicit(&prov
->dp_in_max
, curr
,
1948 memory_order_relaxed
);
1950 dplane_provider_unlock(prov
);
1952 /* Reset the temp list (though the 'concat' may have done this
1953 * already), and the counter
1955 TAILQ_INIT(&work_list
);
1958 /* Call into the provider code. Note that this is
1959 * unconditional: we offer to do work even if we don't enqueue
1962 (*prov
->dp_fp
)(prov
);
1964 /* Check for zebra shutdown */
1965 if (!zdplane_info
.dg_run
)
1968 /* Dequeue completed work from the provider */
1969 dplane_provider_lock(prov
);
1971 while (counter
< limit
) {
1972 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
1974 TAILQ_REMOVE(&(prov
->dp_ctx_out_q
), ctx
,
1977 TAILQ_INSERT_TAIL(&work_list
,
1984 dplane_provider_unlock(prov
);
1986 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1987 zlog_debug("dplane dequeues %d completed work from provider %s",
1988 counter
, dplane_provider_get_name(prov
));
1990 /* Locate next provider */
1992 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
1996 /* After all providers have been serviced, enqueue any completed
1997 * work and any errors back to zebra so it can process the results.
1999 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2000 zlog_debug("dplane has %d completed, %d errors, for zebra main",
2001 counter
, error_counter
);
2004 * Hand lists through the api to zebra main,
2005 * to reduce the number of lock/unlock cycles
2008 /* Call through to zebra main */
2009 (zdplane_info
.dg_results_cb
)(&error_list
);
2011 TAILQ_INIT(&error_list
);
2014 /* Call through to zebra main */
2015 (zdplane_info
.dg_results_cb
)(&work_list
);
2017 TAILQ_INIT(&work_list
);
2024 * Final phase of shutdown, after all work enqueued to dplane has been
2025 * processed. This is called from the zebra main pthread context.
2027 void zebra_dplane_shutdown(void)
2029 if (IS_ZEBRA_DEBUG_DPLANE
)
2030 zlog_debug("Zebra dataplane shutdown called");
2032 /* Stop dplane thread, if it's running */
2034 zdplane_info
.dg_run
= false;
2036 THREAD_OFF(zdplane_info
.dg_t_update
);
2038 frr_pthread_stop(zdplane_info
.dg_pthread
, NULL
);
2040 /* Destroy pthread */
2041 frr_pthread_destroy(zdplane_info
.dg_pthread
);
2042 zdplane_info
.dg_pthread
= NULL
;
2043 zdplane_info
.dg_master
= NULL
;
2045 /* TODO -- Notify provider(s) of final shutdown */
2047 /* TODO -- Clean-up provider objects */
2049 /* TODO -- Clean queue(s), free memory */
2053 * Initialize the dataplane module during startup, internal/private version
2055 static void zebra_dplane_init_internal(struct zebra_t
*zebra
)
2057 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
2059 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
2061 TAILQ_INIT(&zdplane_info
.dg_route_ctx_q
);
2062 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
2064 zdplane_info
.dg_updates_per_cycle
= DPLANE_DEFAULT_NEW_WORK
;
2066 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
2068 /* Register default kernel 'provider' during init */
2069 dplane_provider_init();
2073 * Start the dataplane pthread. This step needs to be run later than the
2074 * 'init' step, in case zebra has fork-ed.
2076 void zebra_dplane_start(void)
2078 /* Start dataplane pthread */
2080 struct frr_pthread_attr pattr
= {
2081 .start
= frr_pthread_attr_default
.start
,
2082 .stop
= frr_pthread_attr_default
.stop
2085 zdplane_info
.dg_pthread
= frr_pthread_new(&pattr
, "Zebra dplane thread",
2088 zdplane_info
.dg_master
= zdplane_info
.dg_pthread
->master
;
2090 zdplane_info
.dg_run
= true;
2092 /* Enqueue an initial event for the dataplane pthread */
2093 thread_add_event(zdplane_info
.dg_master
, dplane_thread_loop
, NULL
, 0,
2094 &zdplane_info
.dg_t_update
);
2096 frr_pthread_run(zdplane_info
.dg_pthread
, NULL
);
2100 * Initialize the dataplane module at startup; called by zebra rib_init()
2102 void zebra_dplane_init(int (*results_fp
)(struct dplane_ctx_q
*))
2104 zebra_dplane_init_internal(&zebrad
);
2105 zdplane_info
.dg_results_cb
= results_fp
;