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 * Pseudowire info for the dataplane
107 struct dplane_pw_info
{
108 char ifname
[IF_NAMESIZE
];
114 union g_addr nexthop
;
115 mpls_label_t local_label
;
116 mpls_label_t remote_label
;
118 union pw_protocol_fields fields
;
122 * The context block used to exchange info about route updates across
123 * the boundary between the zebra main context (and pthread) and the
124 * dataplane layer (and pthread).
126 struct zebra_dplane_ctx
{
129 enum dplane_op_e zd_op
;
131 /* Status on return */
132 enum zebra_dplane_result zd_status
;
134 /* Dplane provider id */
135 uint32_t zd_provider
;
137 /* Flags - used by providers, e.g. */
145 /* TODO -- internal/sub-operation status? */
146 enum zebra_dplane_result zd_remote_status
;
147 enum zebra_dplane_result zd_kernel_status
;
150 uint32_t zd_table_id
;
152 /* Support info for either route or LSP update */
154 struct dplane_route_info rinfo
;
156 struct dplane_pw_info pw
;
159 /* Namespace info, used especially for netlink kernel communication */
160 struct zebra_dplane_info zd_ns_info
;
162 /* Embedded list linkage */
163 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
166 /* Flag that can be set by a pre-kernel provider as a signal that an update
167 * should bypass the kernel.
169 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
173 * Registration block for one dataplane provider.
175 struct zebra_dplane_provider
{
177 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
179 /* Priority, for ordering among providers */
186 pthread_mutex_t dp_mutex
;
188 /* Plugin-provided extra data */
194 int (*dp_fp
)(struct zebra_dplane_provider
*prov
);
196 int (*dp_fini
)(struct zebra_dplane_provider
*prov
, bool early_p
);
198 _Atomic
uint32_t dp_in_counter
;
199 _Atomic
uint32_t dp_in_queued
;
200 _Atomic
uint32_t dp_in_max
;
201 _Atomic
uint32_t dp_out_counter
;
202 _Atomic
uint32_t dp_out_queued
;
203 _Atomic
uint32_t dp_out_max
;
204 _Atomic
uint32_t dp_error_counter
;
206 /* Queue of contexts inbound to the provider */
207 struct dplane_ctx_q dp_ctx_in_q
;
209 /* Queue of completed contexts outbound from the provider back
210 * towards the dataplane module.
212 struct dplane_ctx_q dp_ctx_out_q
;
214 /* Embedded list linkage for provider objects */
215 TAILQ_ENTRY(zebra_dplane_provider
) dp_prov_link
;
221 static struct zebra_dplane_globals
{
222 /* Mutex to control access to dataplane components */
223 pthread_mutex_t dg_mutex
;
225 /* Results callback registered by zebra 'core' */
226 int (*dg_results_cb
)(struct dplane_ctx_q
*ctxlist
);
228 /* Sentinel for beginning of shutdown */
229 volatile bool dg_is_shutdown
;
231 /* Sentinel for end of shutdown */
232 volatile bool dg_run
;
234 /* Route-update context queue inbound to the dataplane */
235 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_route_ctx_q
;
237 /* Ordered list of providers */
238 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
240 /* Counter used to assign internal ids to providers */
241 uint32_t dg_provider_id
;
243 /* Limit number of pending, unprocessed updates */
244 _Atomic
uint32_t dg_max_queued_updates
;
246 /* Limit number of new updates dequeued at once, to pace an
249 uint32_t dg_updates_per_cycle
;
251 _Atomic
uint32_t dg_routes_in
;
252 _Atomic
uint32_t dg_routes_queued
;
253 _Atomic
uint32_t dg_routes_queued_max
;
254 _Atomic
uint32_t dg_route_errors
;
255 _Atomic
uint32_t dg_other_errors
;
257 _Atomic
uint32_t dg_lsps_in
;
258 _Atomic
uint32_t dg_lsps_queued
;
259 _Atomic
uint32_t dg_lsps_queued_max
;
260 _Atomic
uint32_t dg_lsp_errors
;
262 _Atomic
uint32_t dg_update_yields
;
264 /* Dataplane pthread */
265 struct frr_pthread
*dg_pthread
;
267 /* Event-delivery context 'master' for the dplane */
268 struct thread_master
*dg_master
;
270 /* Event/'thread' pointer for queued updates */
271 struct thread
*dg_t_update
;
273 /* Event pointer for pending shutdown check loop */
274 struct thread
*dg_t_shutdown_check
;
279 * Lock and unlock for interactions with the zebra 'core' pthread
281 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
282 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
286 * Lock and unlock for individual providers
288 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
289 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
292 static int dplane_thread_loop(struct thread
*event
);
293 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
294 struct zebra_ns
*zns
);
295 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
296 enum dplane_op_e op
);
302 /* Obtain thread_master for dataplane thread */
303 struct thread_master
*dplane_get_thread_master(void)
305 return zdplane_info
.dg_master
;
309 * Allocate a dataplane update context
311 static struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
313 struct zebra_dplane_ctx
*p
;
315 /* TODO -- just alloc'ing memory, but would like to maintain
318 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
324 * Free a dataplane results context.
326 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
331 DPLANE_CTX_VALID(*pctx
);
333 /* TODO -- just freeing memory, but would like to maintain
337 /* Some internal allocations may need to be freed, depending on
338 * the type of info captured in the ctx.
340 switch ((*pctx
)->zd_op
) {
341 case DPLANE_OP_ROUTE_INSTALL
:
342 case DPLANE_OP_ROUTE_UPDATE
:
343 case DPLANE_OP_ROUTE_DELETE
:
345 /* Free allocated nexthops */
346 if ((*pctx
)->u
.rinfo
.zd_ng
.nexthop
) {
347 /* This deals with recursive nexthops too */
348 nexthops_free((*pctx
)->u
.rinfo
.zd_ng
.nexthop
);
350 (*pctx
)->u
.rinfo
.zd_ng
.nexthop
= NULL
;
353 if ((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
) {
354 /* This deals with recursive nexthops too */
355 nexthops_free((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
);
357 (*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
= NULL
;
362 case DPLANE_OP_LSP_INSTALL
:
363 case DPLANE_OP_LSP_UPDATE
:
364 case DPLANE_OP_LSP_DELETE
:
366 zebra_nhlfe_t
*nhlfe
, *next
;
368 /* Free allocated NHLFEs */
369 for (nhlfe
= (*pctx
)->u
.lsp
.nhlfe_list
; nhlfe
; nhlfe
= next
) {
372 zebra_mpls_nhlfe_del(nhlfe
);
375 /* Clear pointers in lsp struct, in case we're cacheing
376 * free context structs.
378 (*pctx
)->u
.lsp
.nhlfe_list
= NULL
;
379 (*pctx
)->u
.lsp
.best_nhlfe
= NULL
;
388 XFREE(MTYPE_DP_CTX
, *pctx
);
393 * Return a context block to the dplane module after processing
395 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
397 /* TODO -- maintain pool; for now, just free */
398 dplane_ctx_free(pctx
);
401 /* Enqueue a context block */
402 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
403 const struct zebra_dplane_ctx
*ctx
)
405 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
408 /* Append a list of context blocks to another list */
409 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
410 struct dplane_ctx_q
*from_list
)
412 if (TAILQ_FIRST(from_list
)) {
413 TAILQ_CONCAT(to_list
, from_list
, zd_q_entries
);
415 /* And clear 'from' list */
416 TAILQ_INIT(from_list
);
420 /* Dequeue a context block from the head of a list */
421 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
)
423 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
426 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
432 * Accessors for information from the context object
434 enum zebra_dplane_result
dplane_ctx_get_status(
435 const struct zebra_dplane_ctx
*ctx
)
437 DPLANE_CTX_VALID(ctx
);
439 return ctx
->zd_status
;
442 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
443 enum zebra_dplane_result status
)
445 DPLANE_CTX_VALID(ctx
);
447 ctx
->zd_status
= status
;
450 /* Retrieve last/current provider id */
451 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
)
453 DPLANE_CTX_VALID(ctx
);
454 return ctx
->zd_provider
;
457 /* Providers run before the kernel can control whether a kernel
458 * update should be done.
460 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
)
462 DPLANE_CTX_VALID(ctx
);
464 SET_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
467 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
)
469 DPLANE_CTX_VALID(ctx
);
471 return CHECK_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
474 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
476 DPLANE_CTX_VALID(ctx
);
481 const char *dplane_op2str(enum dplane_op_e op
)
483 const char *ret
= "UNKNOWN";
491 case DPLANE_OP_ROUTE_INSTALL
:
492 ret
= "ROUTE_INSTALL";
494 case DPLANE_OP_ROUTE_UPDATE
:
495 ret
= "ROUTE_UPDATE";
497 case DPLANE_OP_ROUTE_DELETE
:
498 ret
= "ROUTE_DELETE";
501 case DPLANE_OP_LSP_INSTALL
:
504 case DPLANE_OP_LSP_UPDATE
:
507 case DPLANE_OP_LSP_DELETE
:
516 const char *dplane_res2str(enum zebra_dplane_result res
)
518 const char *ret
= "<Unknown>";
521 case ZEBRA_DPLANE_REQUEST_FAILURE
:
524 case ZEBRA_DPLANE_REQUEST_QUEUED
:
527 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
535 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
537 DPLANE_CTX_VALID(ctx
);
539 return &(ctx
->u
.rinfo
.zd_dest
);
542 /* Source prefix is a little special - return NULL for "no src prefix" */
543 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
545 DPLANE_CTX_VALID(ctx
);
547 if (ctx
->u
.rinfo
.zd_src
.prefixlen
== 0 &&
548 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->u
.rinfo
.zd_src
.u
.prefix6
))) {
551 return &(ctx
->u
.rinfo
.zd_src
);
555 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
557 DPLANE_CTX_VALID(ctx
);
559 return ctx
->zd_is_update
;
562 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
564 DPLANE_CTX_VALID(ctx
);
569 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
571 DPLANE_CTX_VALID(ctx
);
573 return ctx
->zd_old_seq
;
576 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
578 DPLANE_CTX_VALID(ctx
);
580 return ctx
->zd_vrf_id
;
583 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
585 DPLANE_CTX_VALID(ctx
);
587 return ctx
->u
.rinfo
.zd_type
;
590 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
592 DPLANE_CTX_VALID(ctx
);
594 return ctx
->u
.rinfo
.zd_old_type
;
597 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
599 DPLANE_CTX_VALID(ctx
);
601 return ctx
->u
.rinfo
.zd_afi
;
604 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
606 DPLANE_CTX_VALID(ctx
);
608 return ctx
->u
.rinfo
.zd_safi
;
611 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
613 DPLANE_CTX_VALID(ctx
);
615 return ctx
->zd_table_id
;
618 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
620 DPLANE_CTX_VALID(ctx
);
622 return ctx
->u
.rinfo
.zd_tag
;
625 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
627 DPLANE_CTX_VALID(ctx
);
629 return ctx
->u
.rinfo
.zd_old_tag
;
632 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
634 DPLANE_CTX_VALID(ctx
);
636 return ctx
->u
.rinfo
.zd_instance
;
639 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
641 DPLANE_CTX_VALID(ctx
);
643 return ctx
->u
.rinfo
.zd_old_instance
;
646 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
648 DPLANE_CTX_VALID(ctx
);
650 return ctx
->u
.rinfo
.zd_metric
;
653 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
655 DPLANE_CTX_VALID(ctx
);
657 return ctx
->u
.rinfo
.zd_old_metric
;
660 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
662 DPLANE_CTX_VALID(ctx
);
664 return ctx
->u
.rinfo
.zd_mtu
;
667 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
669 DPLANE_CTX_VALID(ctx
);
671 return ctx
->u
.rinfo
.zd_nexthop_mtu
;
674 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
676 DPLANE_CTX_VALID(ctx
);
678 return ctx
->u
.rinfo
.zd_distance
;
681 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
683 DPLANE_CTX_VALID(ctx
);
685 return ctx
->u
.rinfo
.zd_old_distance
;
688 const struct nexthop_group
*dplane_ctx_get_ng(
689 const struct zebra_dplane_ctx
*ctx
)
691 DPLANE_CTX_VALID(ctx
);
693 return &(ctx
->u
.rinfo
.zd_ng
);
696 const struct nexthop_group
*dplane_ctx_get_old_ng(
697 const struct zebra_dplane_ctx
*ctx
)
699 DPLANE_CTX_VALID(ctx
);
701 return &(ctx
->u
.rinfo
.zd_old_ng
);
704 const struct zebra_dplane_info
*dplane_ctx_get_ns(
705 const struct zebra_dplane_ctx
*ctx
)
707 DPLANE_CTX_VALID(ctx
);
709 return &(ctx
->zd_ns_info
);
712 /* Accessors for LSP information */
714 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
)
716 DPLANE_CTX_VALID(ctx
);
718 return ctx
->u
.lsp
.ile
.in_label
;
721 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
)
723 DPLANE_CTX_VALID(ctx
);
725 return ctx
->u
.lsp
.addr_family
;
728 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
)
730 DPLANE_CTX_VALID(ctx
);
732 return ctx
->u
.lsp
.flags
;
735 zebra_nhlfe_t
*dplane_ctx_get_nhlfe(struct zebra_dplane_ctx
*ctx
)
737 DPLANE_CTX_VALID(ctx
);
739 return ctx
->u
.lsp
.nhlfe_list
;
742 zebra_nhlfe_t
*dplane_ctx_get_best_nhlfe(struct zebra_dplane_ctx
*ctx
)
744 DPLANE_CTX_VALID(ctx
);
746 return ctx
->u
.lsp
.best_nhlfe
;
749 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
)
751 DPLANE_CTX_VALID(ctx
);
753 return ctx
->u
.lsp
.num_ecmp
;
756 const char *dplane_ctx_get_pw_ifname(const struct zebra_dplane_ctx
*ctx
)
758 DPLANE_CTX_VALID(ctx
);
760 return ctx
->u
.pw
.ifname
;
763 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
)
765 DPLANE_CTX_VALID(ctx
);
767 return ctx
->u
.pw
.local_label
;
770 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
)
772 DPLANE_CTX_VALID(ctx
);
774 return ctx
->u
.pw
.remote_label
;
777 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
)
779 DPLANE_CTX_VALID(ctx
);
781 return ctx
->u
.pw
.type
;
784 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
)
786 DPLANE_CTX_VALID(ctx
);
791 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
)
793 DPLANE_CTX_VALID(ctx
);
795 return ctx
->u
.pw
.flags
;
798 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
)
800 DPLANE_CTX_VALID(ctx
);
802 return ctx
->u
.pw
.status
;
805 const union g_addr
*dplane_ctx_get_pw_nexthop(
806 const struct zebra_dplane_ctx
*ctx
)
808 DPLANE_CTX_VALID(ctx
);
810 return &(ctx
->u
.pw
.nexthop
);
813 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
814 const struct zebra_dplane_ctx
*ctx
)
816 DPLANE_CTX_VALID(ctx
);
818 return &(ctx
->u
.pw
.fields
);
822 * End of dplane context accessors
827 * Retrieve the limit on the number of pending, unprocessed updates.
829 uint32_t dplane_get_in_queue_limit(void)
831 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
832 memory_order_relaxed
);
836 * Configure limit on the number of pending, queued updates.
838 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
840 /* Reset to default on 'unset' */
842 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
844 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
845 memory_order_relaxed
);
849 * Retrieve the current queue depth of incoming, unprocessed updates
851 uint32_t dplane_get_in_queue_len(void)
853 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
854 memory_order_seq_cst
);
858 * Common dataplane context init with zebra namespace info.
860 static int dplane_ctx_ns_init(struct zebra_dplane_ctx
*ctx
,
861 struct zebra_ns
*zns
,
864 dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
);
866 #if defined(HAVE_NETLINK)
867 /* Increment message counter after copying to context struct - may need
868 * two messages in some 'update' cases.
871 zns
->netlink_dplane
.seq
+= 2;
873 zns
->netlink_dplane
.seq
++;
874 #endif /* HAVE_NETLINK */
880 * Initialize a context block for a route update from zebra data structs.
882 static int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
,
884 struct route_node
*rn
,
885 struct route_entry
*re
)
888 const struct route_table
*table
= NULL
;
889 const rib_table_info_t
*info
;
890 const struct prefix
*p
, *src_p
;
891 struct zebra_ns
*zns
;
892 struct zebra_vrf
*zvrf
;
893 struct nexthop
*nexthop
;
895 if (!ctx
|| !rn
|| !re
)
899 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
901 ctx
->u
.rinfo
.zd_type
= re
->type
;
902 ctx
->u
.rinfo
.zd_old_type
= re
->type
;
904 /* Prefixes: dest, and optional source */
905 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
907 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), p
);
910 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src_p
);
912 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(ctx
->u
.rinfo
.zd_src
));
914 ctx
->zd_table_id
= re
->table
;
916 ctx
->u
.rinfo
.zd_metric
= re
->metric
;
917 ctx
->u
.rinfo
.zd_old_metric
= re
->metric
;
918 ctx
->zd_vrf_id
= re
->vrf_id
;
919 ctx
->u
.rinfo
.zd_mtu
= re
->mtu
;
920 ctx
->u
.rinfo
.zd_nexthop_mtu
= re
->nexthop_mtu
;
921 ctx
->u
.rinfo
.zd_instance
= re
->instance
;
922 ctx
->u
.rinfo
.zd_tag
= re
->tag
;
923 ctx
->u
.rinfo
.zd_old_tag
= re
->tag
;
924 ctx
->u
.rinfo
.zd_distance
= re
->distance
;
926 table
= srcdest_rnode_table(rn
);
929 ctx
->u
.rinfo
.zd_afi
= info
->afi
;
930 ctx
->u
.rinfo
.zd_safi
= info
->safi
;
932 /* Extract ns info - can't use pointers to 'core' structs */
933 zvrf
= vrf_info_lookup(re
->vrf_id
);
936 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_ROUTE_UPDATE
));
938 /* Copy nexthops; recursive info is included too */
939 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
941 /* TODO -- maybe use array of nexthops to avoid allocs? */
943 /* Ensure that the dplane's nexthops flags are clear. */
944 for (ALL_NEXTHOPS(ctx
->u
.rinfo
.zd_ng
, nexthop
))
945 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
947 /* Trying out the sequence number idea, so we can try to detect
948 * when a result is stale.
950 re
->dplane_sequence
= zebra_router_get_next_sequence();
951 ctx
->zd_seq
= re
->dplane_sequence
;
960 * Capture information for an LSP update in a dplane context.
962 static int dplane_ctx_lsp_init(struct zebra_dplane_ctx
*ctx
,
967 zebra_nhlfe_t
*nhlfe
, *new_nhlfe
;
969 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
970 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
971 dplane_op2str(op
), lsp
->ile
.in_label
,
975 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
977 /* Capture namespace info */
978 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
979 (op
== DPLANE_OP_LSP_UPDATE
));
981 memset(&ctx
->u
.lsp
, 0, sizeof(ctx
->u
.lsp
));
983 ctx
->u
.lsp
.ile
= lsp
->ile
;
984 ctx
->u
.lsp
.addr_family
= lsp
->addr_family
;
985 ctx
->u
.lsp
.num_ecmp
= lsp
->num_ecmp
;
986 ctx
->u
.lsp
.flags
= lsp
->flags
;
988 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
989 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
990 /* Not sure if this is meaningful... */
991 if (nhlfe
->nexthop
== NULL
)
995 zebra_mpls_lsp_add_nhlfe(
998 nhlfe
->nexthop
->type
,
999 &(nhlfe
->nexthop
->gate
),
1000 nhlfe
->nexthop
->ifindex
,
1001 nhlfe
->nexthop
->nh_label
->label
[0]);
1003 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
1008 /* Need to copy flags too */
1009 new_nhlfe
->flags
= nhlfe
->flags
;
1010 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
1012 if (nhlfe
== lsp
->best_nhlfe
)
1013 ctx
->u
.lsp
.best_nhlfe
= new_nhlfe
;
1016 /* On error the ctx will be cleaned-up, so we don't need to
1017 * deal with any allocated nhlfe or nexthop structs here.
1024 * Enqueue a new route update,
1025 * and ensure an event is active for the dataplane pthread.
1027 static int dplane_route_enqueue(struct zebra_dplane_ctx
*ctx
)
1030 uint32_t high
, curr
;
1032 /* Enqueue for processing by the dataplane pthread */
1035 TAILQ_INSERT_TAIL(&zdplane_info
.dg_route_ctx_q
, ctx
,
1040 curr
= atomic_add_fetch_explicit(
1042 /* TODO -- issue with the clang atomic/intrinsics currently;
1043 * casting away the 'Atomic'-ness of the variable works.
1045 (uint32_t *)&(zdplane_info
.dg_routes_queued
),
1047 &(zdplane_info
.dg_routes_queued
),
1049 1, memory_order_seq_cst
);
1051 /* Maybe update high-water counter also */
1052 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1053 memory_order_seq_cst
);
1054 while (high
< curr
) {
1055 if (atomic_compare_exchange_weak_explicit(
1056 &zdplane_info
.dg_routes_queued_max
,
1058 memory_order_seq_cst
,
1059 memory_order_seq_cst
))
1063 /* Ensure that an event for the dataplane thread is active */
1064 ret
= dplane_provider_work_ready();
1070 * Utility that prepares a route update and enqueues it for processing
1072 static enum zebra_dplane_result
1073 dplane_route_update_internal(struct route_node
*rn
,
1074 struct route_entry
*re
,
1075 struct route_entry
*old_re
,
1076 enum dplane_op_e op
)
1078 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1080 struct zebra_dplane_ctx
*ctx
= NULL
;
1082 /* Obtain context block */
1083 ctx
= dplane_ctx_alloc();
1089 /* Init context with info from zebra data structs */
1090 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
1092 /* Capture some extra info for update case
1093 * where there's a different 'old' route.
1095 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
1096 old_re
&& (old_re
!= re
)) {
1097 ctx
->zd_is_update
= true;
1099 old_re
->dplane_sequence
=
1100 zebra_router_get_next_sequence();
1101 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
1103 ctx
->u
.rinfo
.zd_old_tag
= old_re
->tag
;
1104 ctx
->u
.rinfo
.zd_old_type
= old_re
->type
;
1105 ctx
->u
.rinfo
.zd_old_instance
= old_re
->instance
;
1106 ctx
->u
.rinfo
.zd_old_distance
= old_re
->distance
;
1107 ctx
->u
.rinfo
.zd_old_metric
= old_re
->metric
;
1109 #ifndef HAVE_NETLINK
1110 /* For bsd, capture previous re's nexthops too, sigh.
1111 * We'll need these to do per-nexthop deletes.
1113 copy_nexthops(&(ctx
->u
.rinfo
.zd_old_ng
.nexthop
),
1114 old_re
->ng
.nexthop
, NULL
);
1115 #endif /* !HAVE_NETLINK */
1118 /* Enqueue context for processing */
1119 ret
= dplane_route_enqueue(ctx
);
1123 /* Update counter */
1124 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
1125 memory_order_relaxed
);
1128 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1130 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
1131 memory_order_relaxed
);
1133 dplane_ctx_free(&ctx
);
1140 * Enqueue a route 'add' for the dataplane.
1142 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
1143 struct route_entry
*re
)
1145 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1147 if (rn
== NULL
|| re
== NULL
)
1150 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1151 DPLANE_OP_ROUTE_INSTALL
);
1158 * Enqueue a route update for the dataplane.
1160 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
1161 struct route_entry
*re
,
1162 struct route_entry
*old_re
)
1164 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1166 if (rn
== NULL
|| re
== NULL
)
1169 ret
= dplane_route_update_internal(rn
, re
, old_re
,
1170 DPLANE_OP_ROUTE_UPDATE
);
1176 * Enqueue a route removal for the dataplane.
1178 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
1179 struct route_entry
*re
)
1181 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1183 if (rn
== NULL
|| re
== NULL
)
1186 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1187 DPLANE_OP_ROUTE_DELETE
);
1194 * Enqueue LSP add for the dataplane.
1196 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
)
1198 enum zebra_dplane_result ret
=
1199 lsp_update_internal(lsp
, DPLANE_OP_LSP_INSTALL
);
1205 * Enqueue LSP update for the dataplane.
1207 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
)
1209 enum zebra_dplane_result ret
=
1210 lsp_update_internal(lsp
, DPLANE_OP_LSP_UPDATE
);
1216 * Enqueue LSP delete for the dataplane.
1218 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
)
1220 enum zebra_dplane_result ret
=
1221 lsp_update_internal(lsp
, DPLANE_OP_LSP_DELETE
);
1227 * Common internal LSP update utility
1229 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
1230 enum dplane_op_e op
)
1232 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1234 struct zebra_dplane_ctx
*ctx
= NULL
;
1236 /* Obtain context block */
1237 ctx
= dplane_ctx_alloc();
1243 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1247 ret
= dplane_route_enqueue(ctx
);
1250 /* Update counter */
1251 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1252 memory_order_relaxed
);
1255 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1257 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1258 memory_order_relaxed
);
1260 dplane_ctx_free(&ctx
);
1267 * Handler for 'show dplane'
1269 int dplane_show_helper(struct vty
*vty
, bool detailed
)
1271 uint64_t queued
, queue_max
, limit
, errs
, incoming
, yields
,
1274 /* Using atomics because counters are being changed in different
1277 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
1278 memory_order_relaxed
);
1279 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
1280 memory_order_relaxed
);
1281 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
1282 memory_order_relaxed
);
1283 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1284 memory_order_relaxed
);
1285 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
1286 memory_order_relaxed
);
1287 yields
= atomic_load_explicit(&zdplane_info
.dg_update_yields
,
1288 memory_order_relaxed
);
1289 other_errs
= atomic_load_explicit(&zdplane_info
.dg_other_errors
,
1290 memory_order_relaxed
);
1292 vty_out(vty
, "Zebra dataplane:\nRoute updates: %"PRIu64
"\n",
1294 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
1295 vty_out(vty
, "Other errors : %"PRIu64
"\n", other_errs
);
1296 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
1297 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
1298 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
1299 vty_out(vty
, "Dplane update yields: %"PRIu64
"\n", yields
);
1305 * Handler for 'show dplane providers'
1307 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
1309 struct zebra_dplane_provider
*prov
;
1310 uint64_t in
, in_max
, out
, out_max
;
1312 vty_out(vty
, "Zebra dataplane providers:\n");
1315 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1318 /* Show counters, useful info from each registered provider */
1321 in
= atomic_load_explicit(&prov
->dp_in_counter
,
1322 memory_order_relaxed
);
1323 in_max
= atomic_load_explicit(&prov
->dp_in_max
,
1324 memory_order_relaxed
);
1325 out
= atomic_load_explicit(&prov
->dp_out_counter
,
1326 memory_order_relaxed
);
1327 out_max
= atomic_load_explicit(&prov
->dp_out_max
,
1328 memory_order_relaxed
);
1330 vty_out(vty
, "%s (%u): in: %"PRIu64
", q_max: %"PRIu64
", "
1331 "out: %"PRIu64
", q_max: %"PRIu64
"\n",
1332 prov
->dp_name
, prov
->dp_id
, in
, in_max
, out
, out_max
);
1335 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
1343 * Provider registration
1345 int dplane_provider_register(const char *name
,
1346 enum dplane_provider_prio prio
,
1348 int (*fp
)(struct zebra_dplane_provider
*),
1349 int (*fini_fp
)(struct zebra_dplane_provider
*,
1352 struct zebra_dplane_provider
**prov_p
)
1355 struct zebra_dplane_provider
*p
= NULL
, *last
;
1363 if (prio
<= DPLANE_PRIO_NONE
||
1364 prio
> DPLANE_PRIO_LAST
) {
1369 /* Allocate and init new provider struct */
1370 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
1376 pthread_mutex_init(&(p
->dp_mutex
), NULL
);
1377 TAILQ_INIT(&(p
->dp_ctx_in_q
));
1378 TAILQ_INIT(&(p
->dp_ctx_out_q
));
1380 p
->dp_priority
= prio
;
1382 p
->dp_fini
= fini_fp
;
1385 /* Lock - the dplane pthread may be running */
1388 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
1391 strlcpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
1393 snprintf(p
->dp_name
, DPLANE_PROVIDER_NAMELEN
,
1394 "provider-%u", p
->dp_id
);
1396 /* Insert into list ordered by priority */
1397 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
1398 if (last
->dp_priority
> p
->dp_priority
)
1403 TAILQ_INSERT_BEFORE(last
, p
, dp_prov_link
);
1405 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
1411 if (IS_ZEBRA_DEBUG_DPLANE
)
1412 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
1413 p
->dp_name
, p
->dp_id
, p
->dp_priority
);
1422 /* Accessors for provider attributes */
1423 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
)
1425 return prov
->dp_name
;
1428 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
)
1433 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
)
1435 return prov
->dp_data
;
1438 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
)
1440 return zdplane_info
.dg_updates_per_cycle
;
1443 /* Lock/unlock a provider's mutex - iff the provider was registered with
1444 * the THREADED flag.
1446 void dplane_provider_lock(struct zebra_dplane_provider
*prov
)
1448 if (dplane_provider_is_threaded(prov
))
1449 DPLANE_PROV_LOCK(prov
);
1452 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
)
1454 if (dplane_provider_is_threaded(prov
))
1455 DPLANE_PROV_UNLOCK(prov
);
1459 * Dequeue and maintain associated counter
1461 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
1462 struct zebra_dplane_provider
*prov
)
1464 struct zebra_dplane_ctx
*ctx
= NULL
;
1466 dplane_provider_lock(prov
);
1468 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1470 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
1472 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, 1,
1473 memory_order_relaxed
);
1476 dplane_provider_unlock(prov
);
1482 * Dequeue work to a list, return count
1484 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
1485 struct dplane_ctx_q
*listp
)
1488 struct zebra_dplane_ctx
*ctx
;
1490 limit
= zdplane_info
.dg_updates_per_cycle
;
1492 dplane_provider_lock(prov
);
1494 for (ret
= 0; ret
< limit
; ret
++) {
1495 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1497 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
1499 TAILQ_INSERT_TAIL(listp
, ctx
, zd_q_entries
);
1506 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, ret
,
1507 memory_order_relaxed
);
1509 dplane_provider_unlock(prov
);
1515 * Enqueue and maintain associated counter
1517 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
1518 struct zebra_dplane_ctx
*ctx
)
1520 dplane_provider_lock(prov
);
1522 TAILQ_INSERT_TAIL(&(prov
->dp_ctx_out_q
), ctx
,
1525 dplane_provider_unlock(prov
);
1527 atomic_fetch_add_explicit(&(prov
->dp_out_counter
), 1,
1528 memory_order_relaxed
);
1532 * Accessor for provider object
1534 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
)
1536 return (prov
->dp_flags
& DPLANE_PROV_FLAG_THREADED
);
1540 * Internal helper that copies information from a zebra ns object; this is
1541 * called in the zebra main pthread context as part of dplane ctx init.
1543 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
1544 struct zebra_ns
*zns
)
1546 ns_info
->ns_id
= zns
->ns_id
;
1548 #if defined(HAVE_NETLINK)
1549 ns_info
->is_cmd
= true;
1550 ns_info
->nls
= zns
->netlink_dplane
;
1551 #endif /* NETLINK */
1555 * Provider api to signal that work/events are available
1556 * for the dataplane pthread.
1558 int dplane_provider_work_ready(void)
1560 /* Note that during zebra startup, we may be offered work before
1561 * the dataplane pthread (and thread-master) are ready. We want to
1562 * enqueue the work, but the event-scheduling machinery may not be
1565 if (zdplane_info
.dg_run
) {
1566 thread_add_event(zdplane_info
.dg_master
,
1567 dplane_thread_loop
, NULL
, 0,
1568 &zdplane_info
.dg_t_update
);
1575 * Kernel dataplane provider
1579 * Handler for kernel LSP updates
1581 static enum zebra_dplane_result
1582 kernel_dplane_lsp_update(struct zebra_dplane_ctx
*ctx
)
1584 enum zebra_dplane_result res
;
1586 /* Call into the synchronous kernel-facing code here */
1587 res
= kernel_lsp_update(ctx
);
1589 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1590 atomic_fetch_add_explicit(
1591 &zdplane_info
.dg_lsp_errors
, 1,
1592 memory_order_relaxed
);
1598 * Handler for kernel route updates
1600 static enum zebra_dplane_result
1601 kernel_dplane_route_update(struct zebra_dplane_ctx
*ctx
)
1603 enum zebra_dplane_result res
;
1605 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1606 char dest_str
[PREFIX_STRLEN
];
1608 prefix2str(dplane_ctx_get_dest(ctx
),
1609 dest_str
, sizeof(dest_str
));
1611 zlog_debug("%u:%s Dplane route update ctx %p op %s",
1612 dplane_ctx_get_vrf(ctx
), dest_str
,
1613 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
1616 /* Call into the synchronous kernel-facing code here */
1617 res
= kernel_route_update(ctx
);
1619 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1620 atomic_fetch_add_explicit(
1621 &zdplane_info
.dg_route_errors
, 1,
1622 memory_order_relaxed
);
1628 * Kernel provider callback
1630 static int kernel_dplane_process_func(struct zebra_dplane_provider
*prov
)
1632 enum zebra_dplane_result res
;
1633 struct zebra_dplane_ctx
*ctx
;
1636 limit
= dplane_provider_get_work_limit(prov
);
1638 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1639 zlog_debug("dplane provider '%s': processing",
1640 dplane_provider_get_name(prov
));
1642 for (counter
= 0; counter
< limit
; counter
++) {
1644 ctx
= dplane_provider_dequeue_in_ctx(prov
);
1648 /* Dispatch to appropriate kernel-facing apis */
1649 switch (dplane_ctx_get_op(ctx
)) {
1651 case DPLANE_OP_ROUTE_INSTALL
:
1652 case DPLANE_OP_ROUTE_UPDATE
:
1653 case DPLANE_OP_ROUTE_DELETE
:
1654 res
= kernel_dplane_route_update(ctx
);
1657 case DPLANE_OP_LSP_INSTALL
:
1658 case DPLANE_OP_LSP_UPDATE
:
1659 case DPLANE_OP_LSP_DELETE
:
1660 res
= kernel_dplane_lsp_update(ctx
);
1664 atomic_fetch_add_explicit(
1665 &zdplane_info
.dg_other_errors
, 1,
1666 memory_order_relaxed
);
1668 res
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1672 dplane_ctx_set_status(ctx
, res
);
1674 dplane_provider_enqueue_out_ctx(prov
, ctx
);
1677 /* Ensure that we'll run the work loop again if there's still
1680 if (counter
>= limit
) {
1681 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1682 zlog_debug("dplane provider '%s' reached max updates %d",
1683 dplane_provider_get_name(prov
), counter
);
1685 atomic_fetch_add_explicit(&zdplane_info
.dg_update_yields
,
1686 1, memory_order_relaxed
);
1688 dplane_provider_work_ready();
1694 #if DPLANE_TEST_PROVIDER
1697 * Test dataplane provider plugin
1701 * Test provider process callback
1703 static int test_dplane_process_func(struct zebra_dplane_provider
*prov
)
1705 struct zebra_dplane_ctx
*ctx
;
1708 /* Just moving from 'in' queue to 'out' queue */
1710 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1711 zlog_debug("dplane provider '%s': processing",
1712 dplane_provider_get_name(prov
));
1714 limit
= dplane_provider_get_work_limit(prov
);
1716 for (counter
= 0; counter
< limit
; counter
++) {
1718 ctx
= dplane_provider_dequeue_in_ctx(prov
);
1722 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1724 dplane_provider_enqueue_out_ctx(prov
, ctx
);
1727 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1728 zlog_debug("dplane provider '%s': processed %d",
1729 dplane_provider_get_name(prov
), counter
);
1731 /* Ensure that we'll run the work loop again if there's still
1734 if (counter
>= limit
)
1735 dplane_provider_work_ready();
1741 * Test provider shutdown/fini callback
1743 static int test_dplane_shutdown_func(struct zebra_dplane_provider
*prov
,
1746 if (IS_ZEBRA_DEBUG_DPLANE
)
1747 zlog_debug("dplane provider '%s': %sshutdown",
1748 dplane_provider_get_name(prov
),
1749 early
? "early " : "");
1753 #endif /* DPLANE_TEST_PROVIDER */
1756 * Register default kernel provider
1758 static void dplane_provider_init(void)
1762 ret
= dplane_provider_register("Kernel",
1764 DPLANE_PROV_FLAGS_DEFAULT
,
1765 kernel_dplane_process_func
,
1770 zlog_err("Unable to register kernel dplane provider: %d",
1773 #if DPLANE_TEST_PROVIDER
1774 /* Optional test provider ... */
1775 ret
= dplane_provider_register("Test",
1776 DPLANE_PRIO_PRE_KERNEL
,
1777 DPLANE_PROV_FLAGS_DEFAULT
,
1778 test_dplane_process_func
,
1779 test_dplane_shutdown_func
,
1780 NULL
/* data */, NULL
);
1783 zlog_err("Unable to register test dplane provider: %d",
1785 #endif /* DPLANE_TEST_PROVIDER */
1788 /* Indicates zebra shutdown/exit is in progress. Some operations may be
1789 * simplified or skipped during shutdown processing.
1791 bool dplane_is_in_shutdown(void)
1793 return zdplane_info
.dg_is_shutdown
;
1797 * Early or pre-shutdown, de-init notification api. This runs pretty
1798 * early during zebra shutdown, as a signal to stop new work and prepare
1799 * for updates generated by shutdown/cleanup activity, as zebra tries to
1800 * remove everything it's responsible for.
1801 * NB: This runs in the main zebra pthread context.
1803 void zebra_dplane_pre_finish(void)
1805 if (IS_ZEBRA_DEBUG_DPLANE
)
1806 zlog_debug("Zebra dataplane pre-fini called");
1808 zdplane_info
.dg_is_shutdown
= true;
1810 /* TODO -- Notify provider(s) of pending shutdown */
1814 * Utility to determine whether work remains enqueued within the dplane;
1815 * used during system shutdown processing.
1817 static bool dplane_work_pending(void)
1820 struct zebra_dplane_ctx
*ctx
;
1821 struct zebra_dplane_provider
*prov
;
1823 /* TODO -- just checking incoming/pending work for now, must check
1828 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
1829 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1840 dplane_provider_lock(prov
);
1842 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1844 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
1846 dplane_provider_unlock(prov
);
1852 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
1864 * Shutdown-time intermediate callback, used to determine when all pending
1865 * in-flight updates are done. If there's still work to do, reschedules itself.
1866 * If all work is done, schedules an event to the main zebra thread for
1867 * final zebra shutdown.
1868 * This runs in the dplane pthread context.
1870 static int dplane_check_shutdown_status(struct thread
*event
)
1872 if (IS_ZEBRA_DEBUG_DPLANE
)
1873 zlog_debug("Zebra dataplane shutdown status check called");
1875 if (dplane_work_pending()) {
1876 /* Reschedule dplane check on a short timer */
1877 thread_add_timer_msec(zdplane_info
.dg_master
,
1878 dplane_check_shutdown_status
,
1880 &zdplane_info
.dg_t_shutdown_check
);
1882 /* TODO - give up and stop waiting after a short time? */
1885 /* We appear to be done - schedule a final callback event
1886 * for the zebra main pthread.
1888 thread_add_event(zebrad
.master
, zebra_finalize
, NULL
, 0, NULL
);
1895 * Shutdown, de-init api. This runs pretty late during shutdown,
1896 * after zebra has tried to free/remove/uninstall all routes during shutdown.
1897 * At this point, dplane work may still remain to be done, so we can't just
1898 * blindly terminate. If there's still work to do, we'll periodically check
1899 * and when done, we'll enqueue a task to the zebra main thread for final
1900 * termination processing.
1902 * NB: This runs in the main zebra thread context.
1904 void zebra_dplane_finish(void)
1906 if (IS_ZEBRA_DEBUG_DPLANE
)
1907 zlog_debug("Zebra dataplane fini called");
1909 thread_add_event(zdplane_info
.dg_master
,
1910 dplane_check_shutdown_status
, NULL
, 0,
1911 &zdplane_info
.dg_t_shutdown_check
);
1915 * Main dataplane pthread event loop. The thread takes new incoming work
1916 * and offers it to the first provider. It then iterates through the
1917 * providers, taking complete work from each one and offering it
1918 * to the next in order. At each step, a limited number of updates are
1919 * processed during a cycle in order to provide some fairness.
1921 * This loop through the providers is only run once, so that the dataplane
1922 * pthread can look for other pending work - such as i/o work on behalf of
1925 static int dplane_thread_loop(struct thread
*event
)
1927 struct dplane_ctx_q work_list
;
1928 struct dplane_ctx_q error_list
;
1929 struct zebra_dplane_provider
*prov
;
1930 struct zebra_dplane_ctx
*ctx
, *tctx
;
1931 int limit
, counter
, error_counter
;
1932 uint64_t curr
, high
;
1934 /* Capture work limit per cycle */
1935 limit
= zdplane_info
.dg_updates_per_cycle
;
1937 /* Init temporary lists used to move contexts among providers */
1938 TAILQ_INIT(&work_list
);
1939 TAILQ_INIT(&error_list
);
1942 /* Check for zebra shutdown */
1943 if (!zdplane_info
.dg_run
)
1946 /* Dequeue some incoming work from zebra (if any) onto the temporary
1951 /* Locate initial registered provider */
1952 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1954 /* Move new work from incoming list to temp list */
1955 for (counter
= 0; counter
< limit
; counter
++) {
1956 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
1958 TAILQ_REMOVE(&zdplane_info
.dg_route_ctx_q
, ctx
,
1961 ctx
->zd_provider
= prov
->dp_id
;
1963 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
1971 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, counter
,
1972 memory_order_relaxed
);
1974 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1975 zlog_debug("dplane: incoming new work counter: %d", counter
);
1977 /* Iterate through the registered providers, offering new incoming
1978 * work. If the provider has outgoing work in its queue, take that
1979 * work for the next provider
1983 /* At each iteration, the temporary work list has 'counter'
1986 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1987 zlog_debug("dplane enqueues %d new work to provider '%s'",
1988 counter
, dplane_provider_get_name(prov
));
1990 /* Capture current provider id in each context; check for
1993 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, tctx
) {
1994 if (dplane_ctx_get_status(ctx
) ==
1995 ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1996 ctx
->zd_provider
= prov
->dp_id
;
1999 * TODO -- improve error-handling: recirc
2000 * errors backwards so that providers can
2001 * 'undo' their work (if they want to)
2004 /* Move to error list; will be returned
2007 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
2008 TAILQ_INSERT_TAIL(&error_list
,
2014 /* Enqueue new work to the provider */
2015 dplane_provider_lock(prov
);
2017 if (TAILQ_FIRST(&work_list
))
2018 TAILQ_CONCAT(&(prov
->dp_ctx_in_q
), &work_list
,
2021 atomic_fetch_add_explicit(&prov
->dp_in_counter
, counter
,
2022 memory_order_relaxed
);
2023 atomic_fetch_add_explicit(&prov
->dp_in_queued
, counter
,
2024 memory_order_relaxed
);
2025 curr
= atomic_load_explicit(&prov
->dp_in_queued
,
2026 memory_order_relaxed
);
2027 high
= atomic_load_explicit(&prov
->dp_in_max
,
2028 memory_order_relaxed
);
2030 atomic_store_explicit(&prov
->dp_in_max
, curr
,
2031 memory_order_relaxed
);
2033 dplane_provider_unlock(prov
);
2035 /* Reset the temp list (though the 'concat' may have done this
2036 * already), and the counter
2038 TAILQ_INIT(&work_list
);
2041 /* Call into the provider code. Note that this is
2042 * unconditional: we offer to do work even if we don't enqueue
2045 (*prov
->dp_fp
)(prov
);
2047 /* Check for zebra shutdown */
2048 if (!zdplane_info
.dg_run
)
2051 /* Dequeue completed work from the provider */
2052 dplane_provider_lock(prov
);
2054 while (counter
< limit
) {
2055 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
2057 TAILQ_REMOVE(&(prov
->dp_ctx_out_q
), ctx
,
2060 TAILQ_INSERT_TAIL(&work_list
,
2067 dplane_provider_unlock(prov
);
2069 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2070 zlog_debug("dplane dequeues %d completed work from provider %s",
2071 counter
, dplane_provider_get_name(prov
));
2073 /* Locate next provider */
2075 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
2079 /* After all providers have been serviced, enqueue any completed
2080 * work and any errors back to zebra so it can process the results.
2082 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2083 zlog_debug("dplane has %d completed, %d errors, for zebra main",
2084 counter
, error_counter
);
2087 * Hand lists through the api to zebra main,
2088 * to reduce the number of lock/unlock cycles
2091 /* Call through to zebra main */
2092 (zdplane_info
.dg_results_cb
)(&error_list
);
2094 TAILQ_INIT(&error_list
);
2097 /* Call through to zebra main */
2098 (zdplane_info
.dg_results_cb
)(&work_list
);
2100 TAILQ_INIT(&work_list
);
2107 * Final phase of shutdown, after all work enqueued to dplane has been
2108 * processed. This is called from the zebra main pthread context.
2110 void zebra_dplane_shutdown(void)
2112 if (IS_ZEBRA_DEBUG_DPLANE
)
2113 zlog_debug("Zebra dataplane shutdown called");
2115 /* Stop dplane thread, if it's running */
2117 zdplane_info
.dg_run
= false;
2119 THREAD_OFF(zdplane_info
.dg_t_update
);
2121 frr_pthread_stop(zdplane_info
.dg_pthread
, NULL
);
2123 /* Destroy pthread */
2124 frr_pthread_destroy(zdplane_info
.dg_pthread
);
2125 zdplane_info
.dg_pthread
= NULL
;
2126 zdplane_info
.dg_master
= NULL
;
2128 /* TODO -- Notify provider(s) of final shutdown */
2130 /* TODO -- Clean-up provider objects */
2132 /* TODO -- Clean queue(s), free memory */
2136 * Initialize the dataplane module during startup, internal/private version
2138 static void zebra_dplane_init_internal(struct zebra_t
*zebra
)
2140 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
2142 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
2144 TAILQ_INIT(&zdplane_info
.dg_route_ctx_q
);
2145 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
2147 zdplane_info
.dg_updates_per_cycle
= DPLANE_DEFAULT_NEW_WORK
;
2149 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
2151 /* Register default kernel 'provider' during init */
2152 dplane_provider_init();
2156 * Start the dataplane pthread. This step needs to be run later than the
2157 * 'init' step, in case zebra has fork-ed.
2159 void zebra_dplane_start(void)
2161 /* Start dataplane pthread */
2163 struct frr_pthread_attr pattr
= {
2164 .start
= frr_pthread_attr_default
.start
,
2165 .stop
= frr_pthread_attr_default
.stop
2168 zdplane_info
.dg_pthread
= frr_pthread_new(&pattr
, "Zebra dplane thread",
2171 zdplane_info
.dg_master
= zdplane_info
.dg_pthread
->master
;
2173 zdplane_info
.dg_run
= true;
2175 /* Enqueue an initial event for the dataplane pthread */
2176 thread_add_event(zdplane_info
.dg_master
, dplane_thread_loop
, NULL
, 0,
2177 &zdplane_info
.dg_t_update
);
2179 frr_pthread_run(zdplane_info
.dg_pthread
, NULL
);
2183 * Initialize the dataplane module at startup; called by zebra rib_init()
2185 void zebra_dplane_init(int (*results_fp
)(struct dplane_ctx_q
*))
2187 zebra_dplane_init_internal(&zebrad
);
2188 zdplane_info
.dg_results_cb
= results_fp
;