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/zebra_router.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
];
115 mpls_label_t local_label
;
116 mpls_label_t remote_label
;
119 struct nexthop_group nhg
;
121 union pw_protocol_fields fields
;
125 * The context block used to exchange info about route updates across
126 * the boundary between the zebra main context (and pthread) and the
127 * dataplane layer (and pthread).
129 struct zebra_dplane_ctx
{
132 enum dplane_op_e zd_op
;
134 /* Status on return */
135 enum zebra_dplane_result zd_status
;
137 /* Dplane provider id */
138 uint32_t zd_provider
;
140 /* Flags - used by providers, e.g. */
148 /* TODO -- internal/sub-operation status? */
149 enum zebra_dplane_result zd_remote_status
;
150 enum zebra_dplane_result zd_kernel_status
;
153 uint32_t zd_table_id
;
155 /* Support info for either route or LSP update */
157 struct dplane_route_info rinfo
;
159 struct dplane_pw_info pw
;
162 /* Namespace info, used especially for netlink kernel communication */
163 struct zebra_dplane_info zd_ns_info
;
165 /* Embedded list linkage */
166 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
169 /* Flag that can be set by a pre-kernel provider as a signal that an update
170 * should bypass the kernel.
172 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
176 * Registration block for one dataplane provider.
178 struct zebra_dplane_provider
{
180 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
182 /* Priority, for ordering among providers */
189 pthread_mutex_t dp_mutex
;
191 /* Plugin-provided extra data */
197 int (*dp_fp
)(struct zebra_dplane_provider
*prov
);
199 int (*dp_fini
)(struct zebra_dplane_provider
*prov
, bool early_p
);
201 _Atomic
uint32_t dp_in_counter
;
202 _Atomic
uint32_t dp_in_queued
;
203 _Atomic
uint32_t dp_in_max
;
204 _Atomic
uint32_t dp_out_counter
;
205 _Atomic
uint32_t dp_out_queued
;
206 _Atomic
uint32_t dp_out_max
;
207 _Atomic
uint32_t dp_error_counter
;
209 /* Queue of contexts inbound to the provider */
210 struct dplane_ctx_q dp_ctx_in_q
;
212 /* Queue of completed contexts outbound from the provider back
213 * towards the dataplane module.
215 struct dplane_ctx_q dp_ctx_out_q
;
217 /* Embedded list linkage for provider objects */
218 TAILQ_ENTRY(zebra_dplane_provider
) dp_prov_link
;
224 static struct zebra_dplane_globals
{
225 /* Mutex to control access to dataplane components */
226 pthread_mutex_t dg_mutex
;
228 /* Results callback registered by zebra 'core' */
229 int (*dg_results_cb
)(struct dplane_ctx_q
*ctxlist
);
231 /* Sentinel for beginning of shutdown */
232 volatile bool dg_is_shutdown
;
234 /* Sentinel for end of shutdown */
235 volatile bool dg_run
;
237 /* Route-update context queue inbound to the dataplane */
238 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_route_ctx_q
;
240 /* Ordered list of providers */
241 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
243 /* Counter used to assign internal ids to providers */
244 uint32_t dg_provider_id
;
246 /* Limit number of pending, unprocessed updates */
247 _Atomic
uint32_t dg_max_queued_updates
;
249 /* Limit number of new updates dequeued at once, to pace an
252 uint32_t dg_updates_per_cycle
;
254 _Atomic
uint32_t dg_routes_in
;
255 _Atomic
uint32_t dg_routes_queued
;
256 _Atomic
uint32_t dg_routes_queued_max
;
257 _Atomic
uint32_t dg_route_errors
;
258 _Atomic
uint32_t dg_other_errors
;
260 _Atomic
uint32_t dg_lsps_in
;
261 _Atomic
uint32_t dg_lsp_errors
;
263 _Atomic
uint32_t dg_pws_in
;
264 _Atomic
uint32_t dg_pw_errors
;
266 _Atomic
uint32_t dg_update_yields
;
268 /* Dataplane pthread */
269 struct frr_pthread
*dg_pthread
;
271 /* Event-delivery context 'master' for the dplane */
272 struct thread_master
*dg_master
;
274 /* Event/'thread' pointer for queued updates */
275 struct thread
*dg_t_update
;
277 /* Event pointer for pending shutdown check loop */
278 struct thread
*dg_t_shutdown_check
;
283 * Lock and unlock for interactions with the zebra 'core' pthread
285 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
286 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
290 * Lock and unlock for individual providers
292 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
293 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
296 static int dplane_thread_loop(struct thread
*event
);
297 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
298 struct zebra_ns
*zns
);
299 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
300 enum dplane_op_e op
);
301 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
302 enum dplane_op_e op
);
308 /* Obtain thread_master for dataplane thread */
309 struct thread_master
*dplane_get_thread_master(void)
311 return zdplane_info
.dg_master
;
315 * Allocate a dataplane update context
317 static struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
319 struct zebra_dplane_ctx
*p
;
321 /* TODO -- just alloc'ing memory, but would like to maintain
324 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
330 * Free a dataplane results context.
332 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
337 DPLANE_CTX_VALID(*pctx
);
339 /* TODO -- just freeing memory, but would like to maintain
343 /* Some internal allocations may need to be freed, depending on
344 * the type of info captured in the ctx.
346 switch ((*pctx
)->zd_op
) {
347 case DPLANE_OP_ROUTE_INSTALL
:
348 case DPLANE_OP_ROUTE_UPDATE
:
349 case DPLANE_OP_ROUTE_DELETE
:
351 /* Free allocated nexthops */
352 if ((*pctx
)->u
.rinfo
.zd_ng
.nexthop
) {
353 /* This deals with recursive nexthops too */
354 nexthops_free((*pctx
)->u
.rinfo
.zd_ng
.nexthop
);
356 (*pctx
)->u
.rinfo
.zd_ng
.nexthop
= NULL
;
359 if ((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
) {
360 /* This deals with recursive nexthops too */
361 nexthops_free((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
);
363 (*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
= NULL
;
368 case DPLANE_OP_LSP_INSTALL
:
369 case DPLANE_OP_LSP_UPDATE
:
370 case DPLANE_OP_LSP_DELETE
:
372 zebra_nhlfe_t
*nhlfe
, *next
;
374 /* Free allocated NHLFEs */
375 for (nhlfe
= (*pctx
)->u
.lsp
.nhlfe_list
; nhlfe
; nhlfe
= next
) {
378 zebra_mpls_nhlfe_del(nhlfe
);
381 /* Clear pointers in lsp struct, in case we're cacheing
382 * free context structs.
384 (*pctx
)->u
.lsp
.nhlfe_list
= NULL
;
385 (*pctx
)->u
.lsp
.best_nhlfe
= NULL
;
390 case DPLANE_OP_PW_INSTALL
:
391 case DPLANE_OP_PW_UNINSTALL
:
392 /* Free allocated nexthops */
393 if ((*pctx
)->u
.pw
.nhg
.nexthop
) {
394 /* This deals with recursive nexthops too */
395 nexthops_free((*pctx
)->u
.pw
.nhg
.nexthop
);
397 (*pctx
)->u
.pw
.nhg
.nexthop
= NULL
;
405 XFREE(MTYPE_DP_CTX
, *pctx
);
410 * Return a context block to the dplane module after processing
412 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
414 /* TODO -- maintain pool; for now, just free */
415 dplane_ctx_free(pctx
);
418 /* Enqueue a context block */
419 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
420 const struct zebra_dplane_ctx
*ctx
)
422 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
425 /* Append a list of context blocks to another list */
426 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
427 struct dplane_ctx_q
*from_list
)
429 if (TAILQ_FIRST(from_list
)) {
430 TAILQ_CONCAT(to_list
, from_list
, zd_q_entries
);
432 /* And clear 'from' list */
433 TAILQ_INIT(from_list
);
437 /* Dequeue a context block from the head of a list */
438 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
)
440 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
443 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
449 * Accessors for information from the context object
451 enum zebra_dplane_result
dplane_ctx_get_status(
452 const struct zebra_dplane_ctx
*ctx
)
454 DPLANE_CTX_VALID(ctx
);
456 return ctx
->zd_status
;
459 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
460 enum zebra_dplane_result status
)
462 DPLANE_CTX_VALID(ctx
);
464 ctx
->zd_status
= status
;
467 /* Retrieve last/current provider id */
468 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
)
470 DPLANE_CTX_VALID(ctx
);
471 return ctx
->zd_provider
;
474 /* Providers run before the kernel can control whether a kernel
475 * update should be done.
477 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
)
479 DPLANE_CTX_VALID(ctx
);
481 SET_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
484 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
)
486 DPLANE_CTX_VALID(ctx
);
488 return CHECK_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
491 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
493 DPLANE_CTX_VALID(ctx
);
498 const char *dplane_op2str(enum dplane_op_e op
)
500 const char *ret
= "UNKNOWN";
508 case DPLANE_OP_ROUTE_INSTALL
:
509 ret
= "ROUTE_INSTALL";
511 case DPLANE_OP_ROUTE_UPDATE
:
512 ret
= "ROUTE_UPDATE";
514 case DPLANE_OP_ROUTE_DELETE
:
515 ret
= "ROUTE_DELETE";
518 case DPLANE_OP_LSP_INSTALL
:
521 case DPLANE_OP_LSP_UPDATE
:
524 case DPLANE_OP_LSP_DELETE
:
528 case DPLANE_OP_PW_INSTALL
:
531 case DPLANE_OP_PW_UNINSTALL
:
532 ret
= "PW_UNINSTALL";
540 const char *dplane_res2str(enum zebra_dplane_result res
)
542 const char *ret
= "<Unknown>";
545 case ZEBRA_DPLANE_REQUEST_FAILURE
:
548 case ZEBRA_DPLANE_REQUEST_QUEUED
:
551 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
559 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
561 DPLANE_CTX_VALID(ctx
);
563 return &(ctx
->u
.rinfo
.zd_dest
);
566 /* Source prefix is a little special - return NULL for "no src prefix" */
567 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
569 DPLANE_CTX_VALID(ctx
);
571 if (ctx
->u
.rinfo
.zd_src
.prefixlen
== 0 &&
572 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->u
.rinfo
.zd_src
.u
.prefix6
))) {
575 return &(ctx
->u
.rinfo
.zd_src
);
579 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
581 DPLANE_CTX_VALID(ctx
);
583 return ctx
->zd_is_update
;
586 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
588 DPLANE_CTX_VALID(ctx
);
593 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
595 DPLANE_CTX_VALID(ctx
);
597 return ctx
->zd_old_seq
;
600 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
602 DPLANE_CTX_VALID(ctx
);
604 return ctx
->zd_vrf_id
;
607 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
609 DPLANE_CTX_VALID(ctx
);
611 return ctx
->u
.rinfo
.zd_type
;
614 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
616 DPLANE_CTX_VALID(ctx
);
618 return ctx
->u
.rinfo
.zd_old_type
;
621 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
623 DPLANE_CTX_VALID(ctx
);
625 return ctx
->u
.rinfo
.zd_afi
;
628 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
630 DPLANE_CTX_VALID(ctx
);
632 return ctx
->u
.rinfo
.zd_safi
;
635 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
637 DPLANE_CTX_VALID(ctx
);
639 return ctx
->zd_table_id
;
642 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
644 DPLANE_CTX_VALID(ctx
);
646 return ctx
->u
.rinfo
.zd_tag
;
649 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
651 DPLANE_CTX_VALID(ctx
);
653 return ctx
->u
.rinfo
.zd_old_tag
;
656 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
658 DPLANE_CTX_VALID(ctx
);
660 return ctx
->u
.rinfo
.zd_instance
;
663 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
665 DPLANE_CTX_VALID(ctx
);
667 return ctx
->u
.rinfo
.zd_old_instance
;
670 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
672 DPLANE_CTX_VALID(ctx
);
674 return ctx
->u
.rinfo
.zd_metric
;
677 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
679 DPLANE_CTX_VALID(ctx
);
681 return ctx
->u
.rinfo
.zd_old_metric
;
684 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
686 DPLANE_CTX_VALID(ctx
);
688 return ctx
->u
.rinfo
.zd_mtu
;
691 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
693 DPLANE_CTX_VALID(ctx
);
695 return ctx
->u
.rinfo
.zd_nexthop_mtu
;
698 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
700 DPLANE_CTX_VALID(ctx
);
702 return ctx
->u
.rinfo
.zd_distance
;
705 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
707 DPLANE_CTX_VALID(ctx
);
709 return ctx
->u
.rinfo
.zd_old_distance
;
712 const struct nexthop_group
*dplane_ctx_get_ng(
713 const struct zebra_dplane_ctx
*ctx
)
715 DPLANE_CTX_VALID(ctx
);
717 return &(ctx
->u
.rinfo
.zd_ng
);
720 const struct nexthop_group
*dplane_ctx_get_old_ng(
721 const struct zebra_dplane_ctx
*ctx
)
723 DPLANE_CTX_VALID(ctx
);
725 return &(ctx
->u
.rinfo
.zd_old_ng
);
728 const struct zebra_dplane_info
*dplane_ctx_get_ns(
729 const struct zebra_dplane_ctx
*ctx
)
731 DPLANE_CTX_VALID(ctx
);
733 return &(ctx
->zd_ns_info
);
736 /* Accessors for LSP information */
738 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
)
740 DPLANE_CTX_VALID(ctx
);
742 return ctx
->u
.lsp
.ile
.in_label
;
745 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
)
747 DPLANE_CTX_VALID(ctx
);
749 return ctx
->u
.lsp
.addr_family
;
752 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
)
754 DPLANE_CTX_VALID(ctx
);
756 return ctx
->u
.lsp
.flags
;
759 const zebra_nhlfe_t
*dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx
*ctx
)
761 DPLANE_CTX_VALID(ctx
);
763 return ctx
->u
.lsp
.nhlfe_list
;
766 const zebra_nhlfe_t
*
767 dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx
*ctx
)
769 DPLANE_CTX_VALID(ctx
);
771 return ctx
->u
.lsp
.best_nhlfe
;
774 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
)
776 DPLANE_CTX_VALID(ctx
);
778 return ctx
->u
.lsp
.num_ecmp
;
781 const char *dplane_ctx_get_pw_ifname(const struct zebra_dplane_ctx
*ctx
)
783 DPLANE_CTX_VALID(ctx
);
785 return ctx
->u
.pw
.ifname
;
788 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
)
790 DPLANE_CTX_VALID(ctx
);
792 return ctx
->u
.pw
.local_label
;
795 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
)
797 DPLANE_CTX_VALID(ctx
);
799 return ctx
->u
.pw
.remote_label
;
802 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
)
804 DPLANE_CTX_VALID(ctx
);
806 return ctx
->u
.pw
.type
;
809 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
)
811 DPLANE_CTX_VALID(ctx
);
816 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
)
818 DPLANE_CTX_VALID(ctx
);
820 return ctx
->u
.pw
.flags
;
823 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
)
825 DPLANE_CTX_VALID(ctx
);
827 return ctx
->u
.pw
.status
;
830 const union g_addr
*dplane_ctx_get_pw_dest(
831 const struct zebra_dplane_ctx
*ctx
)
833 DPLANE_CTX_VALID(ctx
);
835 return &(ctx
->u
.pw
.dest
);
838 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
839 const struct zebra_dplane_ctx
*ctx
)
841 DPLANE_CTX_VALID(ctx
);
843 return &(ctx
->u
.pw
.fields
);
846 const struct nexthop_group
*
847 dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx
*ctx
)
849 DPLANE_CTX_VALID(ctx
);
851 return &(ctx
->u
.pw
.nhg
);
855 * End of dplane context accessors
860 * Retrieve the limit on the number of pending, unprocessed updates.
862 uint32_t dplane_get_in_queue_limit(void)
864 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
865 memory_order_relaxed
);
869 * Configure limit on the number of pending, queued updates.
871 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
873 /* Reset to default on 'unset' */
875 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
877 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
878 memory_order_relaxed
);
882 * Retrieve the current queue depth of incoming, unprocessed updates
884 uint32_t dplane_get_in_queue_len(void)
886 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
887 memory_order_seq_cst
);
891 * Common dataplane context init with zebra namespace info.
893 static int dplane_ctx_ns_init(struct zebra_dplane_ctx
*ctx
,
894 struct zebra_ns
*zns
,
897 dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
);
899 #if defined(HAVE_NETLINK)
900 /* Increment message counter after copying to context struct - may need
901 * two messages in some 'update' cases.
904 zns
->netlink_dplane
.seq
+= 2;
906 zns
->netlink_dplane
.seq
++;
907 #endif /* HAVE_NETLINK */
913 * Initialize a context block for a route update from zebra data structs.
915 static int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
,
917 struct route_node
*rn
,
918 struct route_entry
*re
)
921 const struct route_table
*table
= NULL
;
922 const rib_table_info_t
*info
;
923 const struct prefix
*p
, *src_p
;
924 struct zebra_ns
*zns
;
925 struct zebra_vrf
*zvrf
;
926 struct nexthop
*nexthop
;
928 if (!ctx
|| !rn
|| !re
)
932 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
934 ctx
->u
.rinfo
.zd_type
= re
->type
;
935 ctx
->u
.rinfo
.zd_old_type
= re
->type
;
937 /* Prefixes: dest, and optional source */
938 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
940 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), p
);
943 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src_p
);
945 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(ctx
->u
.rinfo
.zd_src
));
947 ctx
->zd_table_id
= re
->table
;
949 ctx
->u
.rinfo
.zd_metric
= re
->metric
;
950 ctx
->u
.rinfo
.zd_old_metric
= re
->metric
;
951 ctx
->zd_vrf_id
= re
->vrf_id
;
952 ctx
->u
.rinfo
.zd_mtu
= re
->mtu
;
953 ctx
->u
.rinfo
.zd_nexthop_mtu
= re
->nexthop_mtu
;
954 ctx
->u
.rinfo
.zd_instance
= re
->instance
;
955 ctx
->u
.rinfo
.zd_tag
= re
->tag
;
956 ctx
->u
.rinfo
.zd_old_tag
= re
->tag
;
957 ctx
->u
.rinfo
.zd_distance
= re
->distance
;
959 table
= srcdest_rnode_table(rn
);
962 ctx
->u
.rinfo
.zd_afi
= info
->afi
;
963 ctx
->u
.rinfo
.zd_safi
= info
->safi
;
965 /* Extract ns info - can't use pointers to 'core' structs */
966 zvrf
= vrf_info_lookup(re
->vrf_id
);
969 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_ROUTE_UPDATE
));
971 /* Copy nexthops; recursive info is included too */
972 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
974 /* TODO -- maybe use array of nexthops to avoid allocs? */
976 /* Ensure that the dplane's nexthops flags are clear. */
977 for (ALL_NEXTHOPS(ctx
->u
.rinfo
.zd_ng
, nexthop
))
978 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
980 /* Trying out the sequence number idea, so we can try to detect
981 * when a result is stale.
983 re
->dplane_sequence
= zebra_router_get_next_sequence();
984 ctx
->zd_seq
= re
->dplane_sequence
;
993 * Capture information for an LSP update in a dplane context.
995 static int dplane_ctx_lsp_init(struct zebra_dplane_ctx
*ctx
,
1000 zebra_nhlfe_t
*nhlfe
, *new_nhlfe
;
1002 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1003 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1004 dplane_op2str(op
), lsp
->ile
.in_label
,
1008 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1010 /* Capture namespace info */
1011 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
1012 (op
== DPLANE_OP_LSP_UPDATE
));
1014 memset(&ctx
->u
.lsp
, 0, sizeof(ctx
->u
.lsp
));
1016 ctx
->u
.lsp
.ile
= lsp
->ile
;
1017 ctx
->u
.lsp
.addr_family
= lsp
->addr_family
;
1018 ctx
->u
.lsp
.num_ecmp
= lsp
->num_ecmp
;
1019 ctx
->u
.lsp
.flags
= lsp
->flags
;
1021 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
1022 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1023 /* Not sure if this is meaningful... */
1024 if (nhlfe
->nexthop
== NULL
)
1028 zebra_mpls_lsp_add_nhlfe(
1031 nhlfe
->nexthop
->type
,
1032 &(nhlfe
->nexthop
->gate
),
1033 nhlfe
->nexthop
->ifindex
,
1034 nhlfe
->nexthop
->nh_label
->label
[0]);
1036 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
1041 /* Need to copy flags too */
1042 new_nhlfe
->flags
= nhlfe
->flags
;
1043 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
1045 if (nhlfe
== lsp
->best_nhlfe
)
1046 ctx
->u
.lsp
.best_nhlfe
= new_nhlfe
;
1049 /* On error the ctx will be cleaned-up, so we don't need to
1050 * deal with any allocated nhlfe or nexthop structs here.
1057 * Capture information for an LSP update in a dplane context.
1059 static int dplane_ctx_pw_init(struct zebra_dplane_ctx
*ctx
,
1060 enum dplane_op_e op
,
1061 struct zebra_pw
*pw
)
1065 struct route_table
*table
;
1066 struct route_node
*rn
;
1067 struct route_entry
*re
;
1069 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1070 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
1071 dplane_op2str(op
), pw
->ifname
, pw
->local_label
,
1075 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1077 /* Capture namespace info: no netlink support as of 12/18,
1078 * but just in case...
1080 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
1082 memset(&ctx
->u
.pw
, 0, sizeof(ctx
->u
.pw
));
1084 /* This name appears to be c-string, so we use string copy. */
1085 strlcpy(ctx
->u
.pw
.ifname
, pw
->ifname
, sizeof(ctx
->u
.pw
.ifname
));
1087 ctx
->zd_vrf_id
= pw
->vrf_id
;
1088 ctx
->u
.pw
.ifindex
= pw
->ifindex
;
1089 ctx
->u
.pw
.type
= pw
->type
;
1090 ctx
->u
.pw
.af
= pw
->af
;
1091 ctx
->u
.pw
.local_label
= pw
->local_label
;
1092 ctx
->u
.pw
.remote_label
= pw
->remote_label
;
1093 ctx
->u
.pw
.flags
= pw
->flags
;
1095 ctx
->u
.pw
.dest
= pw
->nexthop
;
1097 ctx
->u
.pw
.fields
= pw
->data
;
1099 /* Capture nexthop info for the pw destination. We need to look
1100 * up and use zebra datastructs, but we're running in the zebra
1101 * pthread here so that should be ok.
1103 memcpy(&p
.u
, &pw
->nexthop
, sizeof(pw
->nexthop
));
1105 p
.prefixlen
= ((pw
->af
== AF_INET
) ?
1106 IPV4_MAX_PREFIXLEN
: IPV6_MAX_PREFIXLEN
);
1108 afi
= (pw
->af
== AF_INET
) ? AFI_IP
: AFI_IP6
;
1109 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, pw
->vrf_id
);
1111 rn
= route_node_match(table
, &p
);
1113 RNODE_FOREACH_RE(rn
, re
) {
1114 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
1119 copy_nexthops(&(ctx
->u
.pw
.nhg
.nexthop
),
1120 re
->ng
.nexthop
, NULL
);
1122 route_unlock_node(rn
);
1130 * Enqueue a new route update,
1131 * and ensure an event is active for the dataplane pthread.
1133 static int dplane_route_enqueue(struct zebra_dplane_ctx
*ctx
)
1136 uint32_t high
, curr
;
1138 /* Enqueue for processing by the dataplane pthread */
1141 TAILQ_INSERT_TAIL(&zdplane_info
.dg_route_ctx_q
, ctx
,
1146 curr
= atomic_add_fetch_explicit(
1148 /* TODO -- issue with the clang atomic/intrinsics currently;
1149 * casting away the 'Atomic'-ness of the variable works.
1151 (uint32_t *)&(zdplane_info
.dg_routes_queued
),
1153 &(zdplane_info
.dg_routes_queued
),
1155 1, memory_order_seq_cst
);
1157 /* Maybe update high-water counter also */
1158 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1159 memory_order_seq_cst
);
1160 while (high
< curr
) {
1161 if (atomic_compare_exchange_weak_explicit(
1162 &zdplane_info
.dg_routes_queued_max
,
1164 memory_order_seq_cst
,
1165 memory_order_seq_cst
))
1169 /* Ensure that an event for the dataplane thread is active */
1170 ret
= dplane_provider_work_ready();
1176 * Utility that prepares a route update and enqueues it for processing
1178 static enum zebra_dplane_result
1179 dplane_route_update_internal(struct route_node
*rn
,
1180 struct route_entry
*re
,
1181 struct route_entry
*old_re
,
1182 enum dplane_op_e op
)
1184 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1186 struct zebra_dplane_ctx
*ctx
= NULL
;
1188 /* Obtain context block */
1189 ctx
= dplane_ctx_alloc();
1195 /* Init context with info from zebra data structs */
1196 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
1198 /* Capture some extra info for update case
1199 * where there's a different 'old' route.
1201 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
1202 old_re
&& (old_re
!= re
)) {
1203 ctx
->zd_is_update
= true;
1205 old_re
->dplane_sequence
=
1206 zebra_router_get_next_sequence();
1207 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
1209 ctx
->u
.rinfo
.zd_old_tag
= old_re
->tag
;
1210 ctx
->u
.rinfo
.zd_old_type
= old_re
->type
;
1211 ctx
->u
.rinfo
.zd_old_instance
= old_re
->instance
;
1212 ctx
->u
.rinfo
.zd_old_distance
= old_re
->distance
;
1213 ctx
->u
.rinfo
.zd_old_metric
= old_re
->metric
;
1215 #ifndef HAVE_NETLINK
1216 /* For bsd, capture previous re's nexthops too, sigh.
1217 * We'll need these to do per-nexthop deletes.
1219 copy_nexthops(&(ctx
->u
.rinfo
.zd_old_ng
.nexthop
),
1220 old_re
->ng
.nexthop
, NULL
);
1221 #endif /* !HAVE_NETLINK */
1224 /* Enqueue context for processing */
1225 ret
= dplane_route_enqueue(ctx
);
1229 /* Update counter */
1230 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
1231 memory_order_relaxed
);
1234 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1236 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
1237 memory_order_relaxed
);
1239 dplane_ctx_free(&ctx
);
1246 * Enqueue a route 'add' for the dataplane.
1248 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
1249 struct route_entry
*re
)
1251 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1253 if (rn
== NULL
|| re
== NULL
)
1256 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1257 DPLANE_OP_ROUTE_INSTALL
);
1264 * Enqueue a route update for the dataplane.
1266 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
1267 struct route_entry
*re
,
1268 struct route_entry
*old_re
)
1270 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1272 if (rn
== NULL
|| re
== NULL
)
1275 ret
= dplane_route_update_internal(rn
, re
, old_re
,
1276 DPLANE_OP_ROUTE_UPDATE
);
1282 * Enqueue a route removal for the dataplane.
1284 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
1285 struct route_entry
*re
)
1287 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1289 if (rn
== NULL
|| re
== NULL
)
1292 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1293 DPLANE_OP_ROUTE_DELETE
);
1300 * Enqueue LSP add for the dataplane.
1302 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
)
1304 enum zebra_dplane_result ret
=
1305 lsp_update_internal(lsp
, DPLANE_OP_LSP_INSTALL
);
1311 * Enqueue LSP update for the dataplane.
1313 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
)
1315 enum zebra_dplane_result ret
=
1316 lsp_update_internal(lsp
, DPLANE_OP_LSP_UPDATE
);
1322 * Enqueue LSP delete for the dataplane.
1324 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
)
1326 enum zebra_dplane_result ret
=
1327 lsp_update_internal(lsp
, DPLANE_OP_LSP_DELETE
);
1333 * Enqueue pseudowire install for the dataplane.
1335 enum zebra_dplane_result
dplane_pw_install(struct zebra_pw
*pw
)
1337 return pw_update_internal(pw
, DPLANE_OP_PW_INSTALL
);
1341 * Enqueue pseudowire un-install for the dataplane.
1343 enum zebra_dplane_result
dplane_pw_uninstall(struct zebra_pw
*pw
)
1345 return pw_update_internal(pw
, DPLANE_OP_PW_UNINSTALL
);
1349 * Common internal LSP update utility
1351 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
1352 enum dplane_op_e op
)
1354 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1356 struct zebra_dplane_ctx
*ctx
= NULL
;
1358 /* Obtain context block */
1359 ctx
= dplane_ctx_alloc();
1365 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1369 ret
= dplane_route_enqueue(ctx
);
1372 /* Update counter */
1373 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1374 memory_order_relaxed
);
1377 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1379 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1380 memory_order_relaxed
);
1382 dplane_ctx_free(&ctx
);
1389 * Internal, common handler for pseudowire updates.
1391 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
1392 enum dplane_op_e op
)
1394 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1396 struct zebra_dplane_ctx
*ctx
= NULL
;
1398 ctx
= dplane_ctx_alloc();
1404 ret
= dplane_ctx_pw_init(ctx
, op
, pw
);
1408 ret
= dplane_route_enqueue(ctx
);
1411 /* Update counter */
1412 atomic_fetch_add_explicit(&zdplane_info
.dg_pws_in
, 1,
1413 memory_order_relaxed
);
1416 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1418 atomic_fetch_add_explicit(&zdplane_info
.dg_pw_errors
, 1,
1419 memory_order_relaxed
);
1421 dplane_ctx_free(&ctx
);
1428 * Handler for 'show dplane'
1430 int dplane_show_helper(struct vty
*vty
, bool detailed
)
1432 uint64_t queued
, queue_max
, limit
, errs
, incoming
, yields
,
1435 /* Using atomics because counters are being changed in different
1438 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
1439 memory_order_relaxed
);
1440 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
1441 memory_order_relaxed
);
1442 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
1443 memory_order_relaxed
);
1444 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1445 memory_order_relaxed
);
1446 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
1447 memory_order_relaxed
);
1448 yields
= atomic_load_explicit(&zdplane_info
.dg_update_yields
,
1449 memory_order_relaxed
);
1450 other_errs
= atomic_load_explicit(&zdplane_info
.dg_other_errors
,
1451 memory_order_relaxed
);
1453 vty_out(vty
, "Zebra dataplane:\nRoute updates: %"PRIu64
"\n",
1455 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
1456 vty_out(vty
, "Other errors : %"PRIu64
"\n", other_errs
);
1457 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
1458 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
1459 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
1460 vty_out(vty
, "Dplane update yields: %"PRIu64
"\n", yields
);
1466 * Handler for 'show dplane providers'
1468 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
1470 struct zebra_dplane_provider
*prov
;
1471 uint64_t in
, in_max
, out
, out_max
;
1473 vty_out(vty
, "Zebra dataplane providers:\n");
1476 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
1479 /* Show counters, useful info from each registered provider */
1482 in
= atomic_load_explicit(&prov
->dp_in_counter
,
1483 memory_order_relaxed
);
1484 in_max
= atomic_load_explicit(&prov
->dp_in_max
,
1485 memory_order_relaxed
);
1486 out
= atomic_load_explicit(&prov
->dp_out_counter
,
1487 memory_order_relaxed
);
1488 out_max
= atomic_load_explicit(&prov
->dp_out_max
,
1489 memory_order_relaxed
);
1491 vty_out(vty
, "%s (%u): in: %"PRIu64
", q_max: %"PRIu64
", "
1492 "out: %"PRIu64
", q_max: %"PRIu64
"\n",
1493 prov
->dp_name
, prov
->dp_id
, in
, in_max
, out
, out_max
);
1496 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
1504 * Provider registration
1506 int dplane_provider_register(const char *name
,
1507 enum dplane_provider_prio prio
,
1509 int (*fp
)(struct zebra_dplane_provider
*),
1510 int (*fini_fp
)(struct zebra_dplane_provider
*,
1513 struct zebra_dplane_provider
**prov_p
)
1516 struct zebra_dplane_provider
*p
= NULL
, *last
;
1524 if (prio
<= DPLANE_PRIO_NONE
||
1525 prio
> DPLANE_PRIO_LAST
) {
1530 /* Allocate and init new provider struct */
1531 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
1533 pthread_mutex_init(&(p
->dp_mutex
), NULL
);
1534 TAILQ_INIT(&(p
->dp_ctx_in_q
));
1535 TAILQ_INIT(&(p
->dp_ctx_out_q
));
1537 p
->dp_priority
= prio
;
1539 p
->dp_fini
= fini_fp
;
1542 /* Lock - the dplane pthread may be running */
1545 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
1548 strlcpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
1550 snprintf(p
->dp_name
, DPLANE_PROVIDER_NAMELEN
,
1551 "provider-%u", p
->dp_id
);
1553 /* Insert into list ordered by priority */
1554 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
1555 if (last
->dp_priority
> p
->dp_priority
)
1560 TAILQ_INSERT_BEFORE(last
, p
, dp_prov_link
);
1562 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
1568 if (IS_ZEBRA_DEBUG_DPLANE
)
1569 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
1570 p
->dp_name
, p
->dp_id
, p
->dp_priority
);
1579 /* Accessors for provider attributes */
1580 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
)
1582 return prov
->dp_name
;
1585 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
)
1590 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
)
1592 return prov
->dp_data
;
1595 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
)
1597 return zdplane_info
.dg_updates_per_cycle
;
1600 /* Lock/unlock a provider's mutex - iff the provider was registered with
1601 * the THREADED flag.
1603 void dplane_provider_lock(struct zebra_dplane_provider
*prov
)
1605 if (dplane_provider_is_threaded(prov
))
1606 DPLANE_PROV_LOCK(prov
);
1609 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
)
1611 if (dplane_provider_is_threaded(prov
))
1612 DPLANE_PROV_UNLOCK(prov
);
1616 * Dequeue and maintain associated counter
1618 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
1619 struct zebra_dplane_provider
*prov
)
1621 struct zebra_dplane_ctx
*ctx
= NULL
;
1623 dplane_provider_lock(prov
);
1625 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1627 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
1629 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, 1,
1630 memory_order_relaxed
);
1633 dplane_provider_unlock(prov
);
1639 * Dequeue work to a list, return count
1641 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
1642 struct dplane_ctx_q
*listp
)
1645 struct zebra_dplane_ctx
*ctx
;
1647 limit
= zdplane_info
.dg_updates_per_cycle
;
1649 dplane_provider_lock(prov
);
1651 for (ret
= 0; ret
< limit
; ret
++) {
1652 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
1654 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
1656 TAILQ_INSERT_TAIL(listp
, ctx
, zd_q_entries
);
1663 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, ret
,
1664 memory_order_relaxed
);
1666 dplane_provider_unlock(prov
);
1672 * Enqueue and maintain associated counter
1674 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
1675 struct zebra_dplane_ctx
*ctx
)
1677 dplane_provider_lock(prov
);
1679 TAILQ_INSERT_TAIL(&(prov
->dp_ctx_out_q
), ctx
,
1682 dplane_provider_unlock(prov
);
1684 atomic_fetch_add_explicit(&(prov
->dp_out_counter
), 1,
1685 memory_order_relaxed
);
1689 * Accessor for provider object
1691 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
)
1693 return (prov
->dp_flags
& DPLANE_PROV_FLAG_THREADED
);
1697 * Internal helper that copies information from a zebra ns object; this is
1698 * called in the zebra main pthread context as part of dplane ctx init.
1700 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
1701 struct zebra_ns
*zns
)
1703 ns_info
->ns_id
= zns
->ns_id
;
1705 #if defined(HAVE_NETLINK)
1706 ns_info
->is_cmd
= true;
1707 ns_info
->nls
= zns
->netlink_dplane
;
1708 #endif /* NETLINK */
1712 * Provider api to signal that work/events are available
1713 * for the dataplane pthread.
1715 int dplane_provider_work_ready(void)
1717 /* Note that during zebra startup, we may be offered work before
1718 * the dataplane pthread (and thread-master) are ready. We want to
1719 * enqueue the work, but the event-scheduling machinery may not be
1722 if (zdplane_info
.dg_run
) {
1723 thread_add_event(zdplane_info
.dg_master
,
1724 dplane_thread_loop
, NULL
, 0,
1725 &zdplane_info
.dg_t_update
);
1732 * Kernel dataplane provider
1736 * Handler for kernel LSP updates
1738 static enum zebra_dplane_result
1739 kernel_dplane_lsp_update(struct zebra_dplane_ctx
*ctx
)
1741 enum zebra_dplane_result res
;
1743 /* Call into the synchronous kernel-facing code here */
1744 res
= kernel_lsp_update(ctx
);
1746 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1747 atomic_fetch_add_explicit(
1748 &zdplane_info
.dg_lsp_errors
, 1,
1749 memory_order_relaxed
);
1755 * Handler for kernel pseudowire updates
1757 static enum zebra_dplane_result
1758 kernel_dplane_pw_update(struct zebra_dplane_ctx
*ctx
)
1760 enum zebra_dplane_result res
;
1762 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1763 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
1764 dplane_ctx_get_pw_ifname(ctx
),
1765 dplane_op2str(ctx
->zd_op
),
1766 dplane_ctx_get_pw_af(ctx
),
1767 dplane_ctx_get_pw_local_label(ctx
),
1768 dplane_ctx_get_pw_remote_label(ctx
));
1770 res
= kernel_pw_update(ctx
);
1772 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1773 atomic_fetch_add_explicit(
1774 &zdplane_info
.dg_pw_errors
, 1,
1775 memory_order_relaxed
);
1781 * Handler for kernel route updates
1783 static enum zebra_dplane_result
1784 kernel_dplane_route_update(struct zebra_dplane_ctx
*ctx
)
1786 enum zebra_dplane_result res
;
1788 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1789 char dest_str
[PREFIX_STRLEN
];
1791 prefix2str(dplane_ctx_get_dest(ctx
),
1792 dest_str
, sizeof(dest_str
));
1794 zlog_debug("%u:%s Dplane route update ctx %p op %s",
1795 dplane_ctx_get_vrf(ctx
), dest_str
,
1796 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
1799 /* Call into the synchronous kernel-facing code here */
1800 res
= kernel_route_update(ctx
);
1802 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
1803 atomic_fetch_add_explicit(
1804 &zdplane_info
.dg_route_errors
, 1,
1805 memory_order_relaxed
);
1811 * Kernel provider callback
1813 static int kernel_dplane_process_func(struct zebra_dplane_provider
*prov
)
1815 enum zebra_dplane_result res
;
1816 struct zebra_dplane_ctx
*ctx
;
1819 limit
= dplane_provider_get_work_limit(prov
);
1821 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1822 zlog_debug("dplane provider '%s': processing",
1823 dplane_provider_get_name(prov
));
1825 for (counter
= 0; counter
< limit
; counter
++) {
1827 ctx
= dplane_provider_dequeue_in_ctx(prov
);
1831 /* Dispatch to appropriate kernel-facing apis */
1832 switch (dplane_ctx_get_op(ctx
)) {
1834 case DPLANE_OP_ROUTE_INSTALL
:
1835 case DPLANE_OP_ROUTE_UPDATE
:
1836 case DPLANE_OP_ROUTE_DELETE
:
1837 res
= kernel_dplane_route_update(ctx
);
1840 case DPLANE_OP_LSP_INSTALL
:
1841 case DPLANE_OP_LSP_UPDATE
:
1842 case DPLANE_OP_LSP_DELETE
:
1843 res
= kernel_dplane_lsp_update(ctx
);
1846 case DPLANE_OP_PW_INSTALL
:
1847 case DPLANE_OP_PW_UNINSTALL
:
1848 res
= kernel_dplane_pw_update(ctx
);
1852 atomic_fetch_add_explicit(
1853 &zdplane_info
.dg_other_errors
, 1,
1854 memory_order_relaxed
);
1856 res
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1860 dplane_ctx_set_status(ctx
, res
);
1862 dplane_provider_enqueue_out_ctx(prov
, ctx
);
1865 /* Ensure that we'll run the work loop again if there's still
1868 if (counter
>= limit
) {
1869 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1870 zlog_debug("dplane provider '%s' reached max updates %d",
1871 dplane_provider_get_name(prov
), counter
);
1873 atomic_fetch_add_explicit(&zdplane_info
.dg_update_yields
,
1874 1, memory_order_relaxed
);
1876 dplane_provider_work_ready();
1882 #if DPLANE_TEST_PROVIDER
1885 * Test dataplane provider plugin
1889 * Test provider process callback
1891 static int test_dplane_process_func(struct zebra_dplane_provider
*prov
)
1893 struct zebra_dplane_ctx
*ctx
;
1896 /* Just moving from 'in' queue to 'out' queue */
1898 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1899 zlog_debug("dplane provider '%s': processing",
1900 dplane_provider_get_name(prov
));
1902 limit
= dplane_provider_get_work_limit(prov
);
1904 for (counter
= 0; counter
< limit
; counter
++) {
1906 ctx
= dplane_provider_dequeue_in_ctx(prov
);
1910 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1912 dplane_provider_enqueue_out_ctx(prov
, ctx
);
1915 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1916 zlog_debug("dplane provider '%s': processed %d",
1917 dplane_provider_get_name(prov
), counter
);
1919 /* Ensure that we'll run the work loop again if there's still
1922 if (counter
>= limit
)
1923 dplane_provider_work_ready();
1929 * Test provider shutdown/fini callback
1931 static int test_dplane_shutdown_func(struct zebra_dplane_provider
*prov
,
1934 if (IS_ZEBRA_DEBUG_DPLANE
)
1935 zlog_debug("dplane provider '%s': %sshutdown",
1936 dplane_provider_get_name(prov
),
1937 early
? "early " : "");
1941 #endif /* DPLANE_TEST_PROVIDER */
1944 * Register default kernel provider
1946 static void dplane_provider_init(void)
1950 ret
= dplane_provider_register("Kernel",
1952 DPLANE_PROV_FLAGS_DEFAULT
,
1953 kernel_dplane_process_func
,
1958 zlog_err("Unable to register kernel dplane provider: %d",
1961 #if DPLANE_TEST_PROVIDER
1962 /* Optional test provider ... */
1963 ret
= dplane_provider_register("Test",
1964 DPLANE_PRIO_PRE_KERNEL
,
1965 DPLANE_PROV_FLAGS_DEFAULT
,
1966 test_dplane_process_func
,
1967 test_dplane_shutdown_func
,
1968 NULL
/* data */, NULL
);
1971 zlog_err("Unable to register test dplane provider: %d",
1973 #endif /* DPLANE_TEST_PROVIDER */
1976 /* Indicates zebra shutdown/exit is in progress. Some operations may be
1977 * simplified or skipped during shutdown processing.
1979 bool dplane_is_in_shutdown(void)
1981 return zdplane_info
.dg_is_shutdown
;
1985 * Early or pre-shutdown, de-init notification api. This runs pretty
1986 * early during zebra shutdown, as a signal to stop new work and prepare
1987 * for updates generated by shutdown/cleanup activity, as zebra tries to
1988 * remove everything it's responsible for.
1989 * NB: This runs in the main zebra pthread context.
1991 void zebra_dplane_pre_finish(void)
1993 if (IS_ZEBRA_DEBUG_DPLANE
)
1994 zlog_debug("Zebra dataplane pre-fini called");
1996 zdplane_info
.dg_is_shutdown
= true;
1998 /* TODO -- Notify provider(s) of pending shutdown */
2002 * Utility to determine whether work remains enqueued within the dplane;
2003 * used during system shutdown processing.
2005 static bool dplane_work_pending(void)
2008 struct zebra_dplane_ctx
*ctx
;
2009 struct zebra_dplane_provider
*prov
;
2011 /* TODO -- just checking incoming/pending work for now, must check
2016 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
2017 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
2028 dplane_provider_lock(prov
);
2030 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
2032 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
2034 dplane_provider_unlock(prov
);
2040 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
2052 * Shutdown-time intermediate callback, used to determine when all pending
2053 * in-flight updates are done. If there's still work to do, reschedules itself.
2054 * If all work is done, schedules an event to the main zebra thread for
2055 * final zebra shutdown.
2056 * This runs in the dplane pthread context.
2058 static int dplane_check_shutdown_status(struct thread
*event
)
2060 if (IS_ZEBRA_DEBUG_DPLANE
)
2061 zlog_debug("Zebra dataplane shutdown status check called");
2063 if (dplane_work_pending()) {
2064 /* Reschedule dplane check on a short timer */
2065 thread_add_timer_msec(zdplane_info
.dg_master
,
2066 dplane_check_shutdown_status
,
2068 &zdplane_info
.dg_t_shutdown_check
);
2070 /* TODO - give up and stop waiting after a short time? */
2073 /* We appear to be done - schedule a final callback event
2074 * for the zebra main pthread.
2076 thread_add_event(zrouter
.master
, zebra_finalize
, NULL
, 0, NULL
);
2083 * Shutdown, de-init api. This runs pretty late during shutdown,
2084 * after zebra has tried to free/remove/uninstall all routes during shutdown.
2085 * At this point, dplane work may still remain to be done, so we can't just
2086 * blindly terminate. If there's still work to do, we'll periodically check
2087 * and when done, we'll enqueue a task to the zebra main thread for final
2088 * termination processing.
2090 * NB: This runs in the main zebra thread context.
2092 void zebra_dplane_finish(void)
2094 if (IS_ZEBRA_DEBUG_DPLANE
)
2095 zlog_debug("Zebra dataplane fini called");
2097 thread_add_event(zdplane_info
.dg_master
,
2098 dplane_check_shutdown_status
, NULL
, 0,
2099 &zdplane_info
.dg_t_shutdown_check
);
2103 * Main dataplane pthread event loop. The thread takes new incoming work
2104 * and offers it to the first provider. It then iterates through the
2105 * providers, taking complete work from each one and offering it
2106 * to the next in order. At each step, a limited number of updates are
2107 * processed during a cycle in order to provide some fairness.
2109 * This loop through the providers is only run once, so that the dataplane
2110 * pthread can look for other pending work - such as i/o work on behalf of
2113 static int dplane_thread_loop(struct thread
*event
)
2115 struct dplane_ctx_q work_list
;
2116 struct dplane_ctx_q error_list
;
2117 struct zebra_dplane_provider
*prov
;
2118 struct zebra_dplane_ctx
*ctx
, *tctx
;
2119 int limit
, counter
, error_counter
;
2120 uint64_t curr
, high
;
2122 /* Capture work limit per cycle */
2123 limit
= zdplane_info
.dg_updates_per_cycle
;
2125 /* Init temporary lists used to move contexts among providers */
2126 TAILQ_INIT(&work_list
);
2127 TAILQ_INIT(&error_list
);
2130 /* Check for zebra shutdown */
2131 if (!zdplane_info
.dg_run
)
2134 /* Dequeue some incoming work from zebra (if any) onto the temporary
2139 /* Locate initial registered provider */
2140 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
2142 /* Move new work from incoming list to temp list */
2143 for (counter
= 0; counter
< limit
; counter
++) {
2144 ctx
= TAILQ_FIRST(&zdplane_info
.dg_route_ctx_q
);
2146 TAILQ_REMOVE(&zdplane_info
.dg_route_ctx_q
, ctx
,
2149 ctx
->zd_provider
= prov
->dp_id
;
2151 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
2159 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, counter
,
2160 memory_order_relaxed
);
2162 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2163 zlog_debug("dplane: incoming new work counter: %d", counter
);
2165 /* Iterate through the registered providers, offering new incoming
2166 * work. If the provider has outgoing work in its queue, take that
2167 * work for the next provider
2171 /* At each iteration, the temporary work list has 'counter'
2174 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2175 zlog_debug("dplane enqueues %d new work to provider '%s'",
2176 counter
, dplane_provider_get_name(prov
));
2178 /* Capture current provider id in each context; check for
2181 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, tctx
) {
2182 if (dplane_ctx_get_status(ctx
) ==
2183 ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2184 ctx
->zd_provider
= prov
->dp_id
;
2187 * TODO -- improve error-handling: recirc
2188 * errors backwards so that providers can
2189 * 'undo' their work (if they want to)
2192 /* Move to error list; will be returned
2195 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
2196 TAILQ_INSERT_TAIL(&error_list
,
2202 /* Enqueue new work to the provider */
2203 dplane_provider_lock(prov
);
2205 if (TAILQ_FIRST(&work_list
))
2206 TAILQ_CONCAT(&(prov
->dp_ctx_in_q
), &work_list
,
2209 atomic_fetch_add_explicit(&prov
->dp_in_counter
, counter
,
2210 memory_order_relaxed
);
2211 atomic_fetch_add_explicit(&prov
->dp_in_queued
, counter
,
2212 memory_order_relaxed
);
2213 curr
= atomic_load_explicit(&prov
->dp_in_queued
,
2214 memory_order_relaxed
);
2215 high
= atomic_load_explicit(&prov
->dp_in_max
,
2216 memory_order_relaxed
);
2218 atomic_store_explicit(&prov
->dp_in_max
, curr
,
2219 memory_order_relaxed
);
2221 dplane_provider_unlock(prov
);
2223 /* Reset the temp list (though the 'concat' may have done this
2224 * already), and the counter
2226 TAILQ_INIT(&work_list
);
2229 /* Call into the provider code. Note that this is
2230 * unconditional: we offer to do work even if we don't enqueue
2233 (*prov
->dp_fp
)(prov
);
2235 /* Check for zebra shutdown */
2236 if (!zdplane_info
.dg_run
)
2239 /* Dequeue completed work from the provider */
2240 dplane_provider_lock(prov
);
2242 while (counter
< limit
) {
2243 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
2245 TAILQ_REMOVE(&(prov
->dp_ctx_out_q
), ctx
,
2248 TAILQ_INSERT_TAIL(&work_list
,
2255 dplane_provider_unlock(prov
);
2257 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2258 zlog_debug("dplane dequeues %d completed work from provider %s",
2259 counter
, dplane_provider_get_name(prov
));
2261 /* Locate next provider */
2263 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
2267 /* After all providers have been serviced, enqueue any completed
2268 * work and any errors back to zebra so it can process the results.
2270 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2271 zlog_debug("dplane has %d completed, %d errors, for zebra main",
2272 counter
, error_counter
);
2275 * Hand lists through the api to zebra main,
2276 * to reduce the number of lock/unlock cycles
2279 /* Call through to zebra main */
2280 (zdplane_info
.dg_results_cb
)(&error_list
);
2282 TAILQ_INIT(&error_list
);
2285 /* Call through to zebra main */
2286 (zdplane_info
.dg_results_cb
)(&work_list
);
2288 TAILQ_INIT(&work_list
);
2295 * Final phase of shutdown, after all work enqueued to dplane has been
2296 * processed. This is called from the zebra main pthread context.
2298 void zebra_dplane_shutdown(void)
2300 if (IS_ZEBRA_DEBUG_DPLANE
)
2301 zlog_debug("Zebra dataplane shutdown called");
2303 /* Stop dplane thread, if it's running */
2305 zdplane_info
.dg_run
= false;
2307 THREAD_OFF(zdplane_info
.dg_t_update
);
2309 frr_pthread_stop(zdplane_info
.dg_pthread
, NULL
);
2311 /* Destroy pthread */
2312 frr_pthread_destroy(zdplane_info
.dg_pthread
);
2313 zdplane_info
.dg_pthread
= NULL
;
2314 zdplane_info
.dg_master
= NULL
;
2316 /* TODO -- Notify provider(s) of final shutdown */
2318 /* TODO -- Clean-up provider objects */
2320 /* TODO -- Clean queue(s), free memory */
2324 * Initialize the dataplane module during startup, internal/private version
2326 static void zebra_dplane_init_internal(void)
2328 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
2330 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
2332 TAILQ_INIT(&zdplane_info
.dg_route_ctx_q
);
2333 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
2335 zdplane_info
.dg_updates_per_cycle
= DPLANE_DEFAULT_NEW_WORK
;
2337 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
2339 /* Register default kernel 'provider' during init */
2340 dplane_provider_init();
2344 * Start the dataplane pthread. This step needs to be run later than the
2345 * 'init' step, in case zebra has fork-ed.
2347 void zebra_dplane_start(void)
2349 /* Start dataplane pthread */
2351 struct frr_pthread_attr pattr
= {
2352 .start
= frr_pthread_attr_default
.start
,
2353 .stop
= frr_pthread_attr_default
.stop
2356 zdplane_info
.dg_pthread
= frr_pthread_new(&pattr
, "Zebra dplane thread",
2359 zdplane_info
.dg_master
= zdplane_info
.dg_pthread
->master
;
2361 zdplane_info
.dg_run
= true;
2363 /* Enqueue an initial event for the dataplane pthread */
2364 thread_add_event(zdplane_info
.dg_master
, dplane_thread_loop
, NULL
, 0,
2365 &zdplane_info
.dg_t_update
);
2367 frr_pthread_run(zdplane_info
.dg_pthread
, NULL
);
2371 * Initialize the dataplane module at startup; called by zebra rib_init()
2373 void zebra_dplane_init(int (*results_fp
)(struct dplane_ctx_q
*))
2375 zebra_dplane_init_internal();
2376 zdplane_info
.dg_results_cb
= results_fp
;