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
24 #include "lib/libfrr.h"
25 #include "lib/debug.h"
26 #include "lib/frratomic.h"
27 #include "lib/frr_pthread.h"
28 #include "lib/memory.h"
29 #include "lib/queue.h"
30 #include "lib/zebra.h"
31 #include "zebra/zebra_router.h"
32 #include "zebra/zebra_memory.h"
33 #include "zebra/zebra_router.h"
34 #include "zebra/zebra_dplane.h"
36 #include "zebra/debug.h"
38 /* Memory type for context blocks */
39 DEFINE_MTYPE_STATIC(ZEBRA
, DP_CTX
, "Zebra DPlane Ctx")
40 DEFINE_MTYPE_STATIC(ZEBRA
, DP_PROV
, "Zebra DPlane Provider")
46 /* Enable test dataplane provider */
47 /*#define DPLANE_TEST_PROVIDER 1 */
49 /* Default value for max queued incoming updates */
50 const uint32_t DPLANE_DEFAULT_MAX_QUEUED
= 200;
52 /* Default value for new work per cycle */
53 const uint32_t DPLANE_DEFAULT_NEW_WORK
= 100;
55 /* Validation check macro for context blocks */
56 /* #define DPLANE_DEBUG 1 */
60 # define DPLANE_CTX_VALID(p) \
65 # define DPLANE_CTX_VALID(p)
67 #endif /* DPLANE_DEBUG */
70 * Route information captured for route updates.
72 struct dplane_route_info
{
74 /* Dest and (optional) source prefixes */
75 struct prefix zd_dest
;
85 route_tag_t zd_old_tag
;
87 uint32_t zd_old_metric
;
90 uint16_t zd_old_instance
;
93 uint8_t zd_old_distance
;
96 uint32_t zd_nexthop_mtu
;
99 struct nexthop_group zd_ng
;
101 /* "Previous" nexthops, used only in route updates without netlink */
102 struct nexthop_group zd_old_ng
;
104 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
109 * Pseudowire info for the dataplane
111 struct dplane_pw_info
{
117 mpls_label_t local_label
;
118 mpls_label_t remote_label
;
121 struct nexthop_group nhg
;
123 union pw_protocol_fields fields
;
127 * Interface/prefix info for the dataplane
129 struct dplane_intf_info
{
134 #define DPLANE_INTF_CONNECTED (1 << 0) /* Connected peer, p2p */
135 #define DPLANE_INTF_SECONDARY (1 << 1)
136 #define DPLANE_INTF_BROADCAST (1 << 2)
137 #define DPLANE_INTF_HAS_DEST DPLANE_INTF_CONNECTED
138 #define DPLANE_INTF_HAS_LABEL (1 << 4)
140 /* Interface address/prefix */
141 struct prefix prefix
;
143 /* Dest address, for p2p, or broadcast prefix */
144 struct prefix dest_prefix
;
151 * EVPN MAC address info for the dataplane.
153 struct dplane_mac_info
{
156 struct in_addr vtep_ip
;
162 * EVPN neighbor info for the dataplane
164 struct dplane_neigh_info
{
165 struct ipaddr ip_addr
;
172 * The context block used to exchange info about route updates across
173 * the boundary between the zebra main context (and pthread) and the
174 * dataplane layer (and pthread).
176 struct zebra_dplane_ctx
{
179 enum dplane_op_e zd_op
;
181 /* Status on return */
182 enum zebra_dplane_result zd_status
;
184 /* Dplane provider id */
185 uint32_t zd_provider
;
187 /* Flags - used by providers, e.g. */
195 /* Some updates may be generated by notifications: allow the
196 * plugin to notice and ignore results from its own notifications.
198 uint32_t zd_notif_provider
;
200 /* TODO -- internal/sub-operation status? */
201 enum zebra_dplane_result zd_remote_status
;
202 enum zebra_dplane_result zd_kernel_status
;
205 uint32_t zd_table_id
;
207 char zd_ifname
[INTERFACE_NAMSIZ
];
208 ifindex_t zd_ifindex
;
210 /* Support info for different kinds of updates */
212 struct dplane_route_info rinfo
;
214 struct dplane_pw_info pw
;
215 struct dplane_intf_info intf
;
216 struct dplane_mac_info macinfo
;
217 struct dplane_neigh_info neigh
;
220 /* Namespace info, used especially for netlink kernel communication */
221 struct zebra_dplane_info zd_ns_info
;
223 /* Embedded list linkage */
224 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
227 /* Flag that can be set by a pre-kernel provider as a signal that an update
228 * should bypass the kernel.
230 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
234 * Registration block for one dataplane provider.
236 struct zebra_dplane_provider
{
238 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
240 /* Priority, for ordering among providers */
247 pthread_mutex_t dp_mutex
;
249 /* Plugin-provided extra data */
255 int (*dp_start
)(struct zebra_dplane_provider
*prov
);
257 int (*dp_fp
)(struct zebra_dplane_provider
*prov
);
259 int (*dp_fini
)(struct zebra_dplane_provider
*prov
, bool early_p
);
261 _Atomic
uint32_t dp_in_counter
;
262 _Atomic
uint32_t dp_in_queued
;
263 _Atomic
uint32_t dp_in_max
;
264 _Atomic
uint32_t dp_out_counter
;
265 _Atomic
uint32_t dp_out_queued
;
266 _Atomic
uint32_t dp_out_max
;
267 _Atomic
uint32_t dp_error_counter
;
269 /* Queue of contexts inbound to the provider */
270 struct dplane_ctx_q dp_ctx_in_q
;
272 /* Queue of completed contexts outbound from the provider back
273 * towards the dataplane module.
275 struct dplane_ctx_q dp_ctx_out_q
;
277 /* Embedded list linkage for provider objects */
278 TAILQ_ENTRY(zebra_dplane_provider
) dp_prov_link
;
284 static struct zebra_dplane_globals
{
285 /* Mutex to control access to dataplane components */
286 pthread_mutex_t dg_mutex
;
288 /* Results callback registered by zebra 'core' */
289 int (*dg_results_cb
)(struct dplane_ctx_q
*ctxlist
);
291 /* Sentinel for beginning of shutdown */
292 volatile bool dg_is_shutdown
;
294 /* Sentinel for end of shutdown */
295 volatile bool dg_run
;
297 /* Update context queue inbound to the dataplane */
298 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_update_ctx_q
;
300 /* Ordered list of providers */
301 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
303 /* Counter used to assign internal ids to providers */
304 uint32_t dg_provider_id
;
306 /* Limit number of pending, unprocessed updates */
307 _Atomic
uint32_t dg_max_queued_updates
;
309 /* Control whether system route notifications should be produced. */
310 bool dg_sys_route_notifs
;
312 /* Limit number of new updates dequeued at once, to pace an
315 uint32_t dg_updates_per_cycle
;
317 _Atomic
uint32_t dg_routes_in
;
318 _Atomic
uint32_t dg_routes_queued
;
319 _Atomic
uint32_t dg_routes_queued_max
;
320 _Atomic
uint32_t dg_route_errors
;
321 _Atomic
uint32_t dg_other_errors
;
323 _Atomic
uint32_t dg_lsps_in
;
324 _Atomic
uint32_t dg_lsp_errors
;
326 _Atomic
uint32_t dg_pws_in
;
327 _Atomic
uint32_t dg_pw_errors
;
329 _Atomic
uint32_t dg_intf_addrs_in
;
330 _Atomic
uint32_t dg_intf_addr_errors
;
332 _Atomic
uint32_t dg_macs_in
;
333 _Atomic
uint32_t dg_mac_errors
;
335 _Atomic
uint32_t dg_neighs_in
;
336 _Atomic
uint32_t dg_neigh_errors
;
338 _Atomic
uint32_t dg_update_yields
;
340 /* Dataplane pthread */
341 struct frr_pthread
*dg_pthread
;
343 /* Event-delivery context 'master' for the dplane */
344 struct thread_master
*dg_master
;
346 /* Event/'thread' pointer for queued updates */
347 struct thread
*dg_t_update
;
349 /* Event pointer for pending shutdown check loop */
350 struct thread
*dg_t_shutdown_check
;
355 * Lock and unlock for interactions with the zebra 'core' pthread
357 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
358 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
362 * Lock and unlock for individual providers
364 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
365 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
368 static int dplane_thread_loop(struct thread
*event
);
369 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
370 struct zebra_ns
*zns
);
371 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
372 enum dplane_op_e op
);
373 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
374 enum dplane_op_e op
);
375 static enum zebra_dplane_result
intf_addr_update_internal(
376 const struct interface
*ifp
, const struct connected
*ifc
,
377 enum dplane_op_e op
);
378 static enum zebra_dplane_result
mac_update_internal(
379 enum dplane_op_e op
, const struct interface
*ifp
,
380 vlanid_t vid
, const struct ethaddr
*mac
,
381 struct in_addr vtep_ip
, bool sticky
);
382 static enum zebra_dplane_result
neigh_update_internal(
384 const struct interface
*ifp
,
385 const struct ethaddr
*mac
,
386 const struct ipaddr
*ip
,
387 uint32_t flags
, uint16_t state
);
393 /* Obtain thread_master for dataplane thread */
394 struct thread_master
*dplane_get_thread_master(void)
396 return zdplane_info
.dg_master
;
400 * Allocate a dataplane update context
402 struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
404 struct zebra_dplane_ctx
*p
;
406 /* TODO -- just alloc'ing memory, but would like to maintain
409 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
414 /* Enable system route notifications */
415 void dplane_enable_sys_route_notifs(void)
417 zdplane_info
.dg_sys_route_notifs
= true;
421 * Free a dataplane results context.
423 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
428 DPLANE_CTX_VALID(*pctx
);
430 /* TODO -- just freeing memory, but would like to maintain
434 /* Some internal allocations may need to be freed, depending on
435 * the type of info captured in the ctx.
437 switch ((*pctx
)->zd_op
) {
438 case DPLANE_OP_ROUTE_INSTALL
:
439 case DPLANE_OP_ROUTE_UPDATE
:
440 case DPLANE_OP_ROUTE_DELETE
:
441 case DPLANE_OP_SYS_ROUTE_ADD
:
442 case DPLANE_OP_SYS_ROUTE_DELETE
:
443 case DPLANE_OP_ROUTE_NOTIFY
:
445 /* Free allocated nexthops */
446 if ((*pctx
)->u
.rinfo
.zd_ng
.nexthop
) {
447 /* This deals with recursive nexthops too */
448 nexthops_free((*pctx
)->u
.rinfo
.zd_ng
.nexthop
);
450 (*pctx
)->u
.rinfo
.zd_ng
.nexthop
= NULL
;
453 if ((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
) {
454 /* This deals with recursive nexthops too */
455 nexthops_free((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
);
457 (*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
= NULL
;
462 case DPLANE_OP_LSP_INSTALL
:
463 case DPLANE_OP_LSP_UPDATE
:
464 case DPLANE_OP_LSP_DELETE
:
465 case DPLANE_OP_LSP_NOTIFY
:
467 zebra_nhlfe_t
*nhlfe
, *next
;
469 /* Free allocated NHLFEs */
470 for (nhlfe
= (*pctx
)->u
.lsp
.nhlfe_list
; nhlfe
; nhlfe
= next
) {
473 zebra_mpls_nhlfe_del(nhlfe
);
476 /* Clear pointers in lsp struct, in case we're cacheing
477 * free context structs.
479 (*pctx
)->u
.lsp
.nhlfe_list
= NULL
;
480 (*pctx
)->u
.lsp
.best_nhlfe
= NULL
;
485 case DPLANE_OP_PW_INSTALL
:
486 case DPLANE_OP_PW_UNINSTALL
:
487 /* Free allocated nexthops */
488 if ((*pctx
)->u
.pw
.nhg
.nexthop
) {
489 /* This deals with recursive nexthops too */
490 nexthops_free((*pctx
)->u
.pw
.nhg
.nexthop
);
492 (*pctx
)->u
.pw
.nhg
.nexthop
= NULL
;
496 case DPLANE_OP_ADDR_INSTALL
:
497 case DPLANE_OP_ADDR_UNINSTALL
:
498 /* Maybe free label string, if allocated */
499 if ((*pctx
)->u
.intf
.label
!= NULL
&&
500 (*pctx
)->u
.intf
.label
!= (*pctx
)->u
.intf
.label_buf
) {
501 free((*pctx
)->u
.intf
.label
);
502 (*pctx
)->u
.intf
.label
= NULL
;
506 case DPLANE_OP_MAC_INSTALL
:
507 case DPLANE_OP_MAC_DELETE
:
508 case DPLANE_OP_NEIGH_INSTALL
:
509 case DPLANE_OP_NEIGH_UPDATE
:
510 case DPLANE_OP_NEIGH_DELETE
:
511 case DPLANE_OP_VTEP_ADD
:
512 case DPLANE_OP_VTEP_DELETE
:
517 XFREE(MTYPE_DP_CTX
, *pctx
);
522 * Return a context block to the dplane module after processing
524 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
526 /* TODO -- maintain pool; for now, just free */
527 dplane_ctx_free(pctx
);
530 /* Enqueue a context block */
531 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
532 const struct zebra_dplane_ctx
*ctx
)
534 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
537 /* Append a list of context blocks to another list */
538 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
539 struct dplane_ctx_q
*from_list
)
541 if (TAILQ_FIRST(from_list
)) {
542 TAILQ_CONCAT(to_list
, from_list
, zd_q_entries
);
544 /* And clear 'from' list */
545 TAILQ_INIT(from_list
);
549 /* Dequeue a context block from the head of a list */
550 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
)
552 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
555 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
561 * Accessors for information from the context object
563 enum zebra_dplane_result
dplane_ctx_get_status(
564 const struct zebra_dplane_ctx
*ctx
)
566 DPLANE_CTX_VALID(ctx
);
568 return ctx
->zd_status
;
571 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
572 enum zebra_dplane_result status
)
574 DPLANE_CTX_VALID(ctx
);
576 ctx
->zd_status
= status
;
579 /* Retrieve last/current provider id */
580 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
)
582 DPLANE_CTX_VALID(ctx
);
583 return ctx
->zd_provider
;
586 /* Providers run before the kernel can control whether a kernel
587 * update should be done.
589 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
)
591 DPLANE_CTX_VALID(ctx
);
593 SET_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
596 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
)
598 DPLANE_CTX_VALID(ctx
);
600 return CHECK_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
603 void dplane_ctx_set_op(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
)
605 DPLANE_CTX_VALID(ctx
);
609 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
611 DPLANE_CTX_VALID(ctx
);
616 const char *dplane_op2str(enum dplane_op_e op
)
618 const char *ret
= "UNKNOWN";
626 case DPLANE_OP_ROUTE_INSTALL
:
627 ret
= "ROUTE_INSTALL";
629 case DPLANE_OP_ROUTE_UPDATE
:
630 ret
= "ROUTE_UPDATE";
632 case DPLANE_OP_ROUTE_DELETE
:
633 ret
= "ROUTE_DELETE";
635 case DPLANE_OP_ROUTE_NOTIFY
:
636 ret
= "ROUTE_NOTIFY";
639 case DPLANE_OP_LSP_INSTALL
:
642 case DPLANE_OP_LSP_UPDATE
:
645 case DPLANE_OP_LSP_DELETE
:
648 case DPLANE_OP_LSP_NOTIFY
:
652 case DPLANE_OP_PW_INSTALL
:
655 case DPLANE_OP_PW_UNINSTALL
:
656 ret
= "PW_UNINSTALL";
659 case DPLANE_OP_SYS_ROUTE_ADD
:
660 ret
= "SYS_ROUTE_ADD";
662 case DPLANE_OP_SYS_ROUTE_DELETE
:
663 ret
= "SYS_ROUTE_DEL";
666 case DPLANE_OP_ADDR_INSTALL
:
667 ret
= "ADDR_INSTALL";
669 case DPLANE_OP_ADDR_UNINSTALL
:
670 ret
= "ADDR_UNINSTALL";
673 case DPLANE_OP_MAC_INSTALL
:
676 case DPLANE_OP_MAC_DELETE
:
680 case DPLANE_OP_NEIGH_INSTALL
:
681 ret
= "NEIGH_INSTALL";
683 case DPLANE_OP_NEIGH_UPDATE
:
684 ret
= "NEIGH_UPDATE";
686 case DPLANE_OP_NEIGH_DELETE
:
687 ret
= "NEIGH_DELETE";
689 case DPLANE_OP_VTEP_ADD
:
692 case DPLANE_OP_VTEP_DELETE
:
700 const char *dplane_res2str(enum zebra_dplane_result res
)
702 const char *ret
= "<Unknown>";
705 case ZEBRA_DPLANE_REQUEST_FAILURE
:
708 case ZEBRA_DPLANE_REQUEST_QUEUED
:
711 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
719 void dplane_ctx_set_dest(struct zebra_dplane_ctx
*ctx
,
720 const struct prefix
*dest
)
722 DPLANE_CTX_VALID(ctx
);
724 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), dest
);
727 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
729 DPLANE_CTX_VALID(ctx
);
731 return &(ctx
->u
.rinfo
.zd_dest
);
734 void dplane_ctx_set_src(struct zebra_dplane_ctx
*ctx
, const struct prefix
*src
)
736 DPLANE_CTX_VALID(ctx
);
739 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src
);
741 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(struct prefix
));
744 /* Source prefix is a little special - return NULL for "no src prefix" */
745 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
747 DPLANE_CTX_VALID(ctx
);
749 if (ctx
->u
.rinfo
.zd_src
.prefixlen
== 0 &&
750 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->u
.rinfo
.zd_src
.u
.prefix6
))) {
753 return &(ctx
->u
.rinfo
.zd_src
);
757 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
759 DPLANE_CTX_VALID(ctx
);
761 return ctx
->zd_is_update
;
764 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
766 DPLANE_CTX_VALID(ctx
);
771 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
773 DPLANE_CTX_VALID(ctx
);
775 return ctx
->zd_old_seq
;
778 void dplane_ctx_set_vrf(struct zebra_dplane_ctx
*ctx
, vrf_id_t vrf
)
780 DPLANE_CTX_VALID(ctx
);
782 ctx
->zd_vrf_id
= vrf
;
785 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
787 DPLANE_CTX_VALID(ctx
);
789 return ctx
->zd_vrf_id
;
792 bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx
*ctx
)
794 DPLANE_CTX_VALID(ctx
);
796 return (ctx
->zd_notif_provider
!= 0);
799 uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx
*ctx
)
801 DPLANE_CTX_VALID(ctx
);
803 return ctx
->zd_notif_provider
;
806 void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx
*ctx
,
809 DPLANE_CTX_VALID(ctx
);
811 ctx
->zd_notif_provider
= id
;
813 const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx
*ctx
)
815 DPLANE_CTX_VALID(ctx
);
817 return ctx
->zd_ifname
;
820 ifindex_t
dplane_ctx_get_ifindex(const struct zebra_dplane_ctx
*ctx
)
822 DPLANE_CTX_VALID(ctx
);
824 return ctx
->zd_ifindex
;
827 void dplane_ctx_set_type(struct zebra_dplane_ctx
*ctx
, int type
)
829 DPLANE_CTX_VALID(ctx
);
831 ctx
->u
.rinfo
.zd_type
= type
;
834 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
836 DPLANE_CTX_VALID(ctx
);
838 return ctx
->u
.rinfo
.zd_type
;
841 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
843 DPLANE_CTX_VALID(ctx
);
845 return ctx
->u
.rinfo
.zd_old_type
;
848 void dplane_ctx_set_afi(struct zebra_dplane_ctx
*ctx
, afi_t afi
)
850 DPLANE_CTX_VALID(ctx
);
852 ctx
->u
.rinfo
.zd_afi
= afi
;
855 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
857 DPLANE_CTX_VALID(ctx
);
859 return ctx
->u
.rinfo
.zd_afi
;
862 void dplane_ctx_set_safi(struct zebra_dplane_ctx
*ctx
, safi_t safi
)
864 DPLANE_CTX_VALID(ctx
);
866 ctx
->u
.rinfo
.zd_safi
= safi
;
869 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
871 DPLANE_CTX_VALID(ctx
);
873 return ctx
->u
.rinfo
.zd_safi
;
876 void dplane_ctx_set_table(struct zebra_dplane_ctx
*ctx
, uint32_t table
)
878 DPLANE_CTX_VALID(ctx
);
880 ctx
->zd_table_id
= table
;
883 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
885 DPLANE_CTX_VALID(ctx
);
887 return ctx
->zd_table_id
;
890 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
892 DPLANE_CTX_VALID(ctx
);
894 return ctx
->u
.rinfo
.zd_tag
;
897 void dplane_ctx_set_tag(struct zebra_dplane_ctx
*ctx
, route_tag_t tag
)
899 DPLANE_CTX_VALID(ctx
);
901 ctx
->u
.rinfo
.zd_tag
= tag
;
904 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
906 DPLANE_CTX_VALID(ctx
);
908 return ctx
->u
.rinfo
.zd_old_tag
;
911 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
913 DPLANE_CTX_VALID(ctx
);
915 return ctx
->u
.rinfo
.zd_instance
;
918 void dplane_ctx_set_instance(struct zebra_dplane_ctx
*ctx
, uint16_t instance
)
920 DPLANE_CTX_VALID(ctx
);
922 ctx
->u
.rinfo
.zd_instance
= instance
;
925 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
927 DPLANE_CTX_VALID(ctx
);
929 return ctx
->u
.rinfo
.zd_old_instance
;
932 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
934 DPLANE_CTX_VALID(ctx
);
936 return ctx
->u
.rinfo
.zd_metric
;
939 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
941 DPLANE_CTX_VALID(ctx
);
943 return ctx
->u
.rinfo
.zd_old_metric
;
946 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
948 DPLANE_CTX_VALID(ctx
);
950 return ctx
->u
.rinfo
.zd_mtu
;
953 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
955 DPLANE_CTX_VALID(ctx
);
957 return ctx
->u
.rinfo
.zd_nexthop_mtu
;
960 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
962 DPLANE_CTX_VALID(ctx
);
964 return ctx
->u
.rinfo
.zd_distance
;
967 void dplane_ctx_set_distance(struct zebra_dplane_ctx
*ctx
, uint8_t distance
)
969 DPLANE_CTX_VALID(ctx
);
971 ctx
->u
.rinfo
.zd_distance
= distance
;
974 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
976 DPLANE_CTX_VALID(ctx
);
978 return ctx
->u
.rinfo
.zd_old_distance
;
981 void dplane_ctx_set_nexthops(struct zebra_dplane_ctx
*ctx
, struct nexthop
*nh
)
983 DPLANE_CTX_VALID(ctx
);
985 if (ctx
->u
.rinfo
.zd_ng
.nexthop
) {
986 nexthops_free(ctx
->u
.rinfo
.zd_ng
.nexthop
);
987 ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
989 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), nh
, NULL
);
992 const struct nexthop_group
*dplane_ctx_get_ng(
993 const struct zebra_dplane_ctx
*ctx
)
995 DPLANE_CTX_VALID(ctx
);
997 return &(ctx
->u
.rinfo
.zd_ng
);
1000 const struct nexthop_group
*dplane_ctx_get_old_ng(
1001 const struct zebra_dplane_ctx
*ctx
)
1003 DPLANE_CTX_VALID(ctx
);
1005 return &(ctx
->u
.rinfo
.zd_old_ng
);
1008 const struct zebra_dplane_info
*dplane_ctx_get_ns(
1009 const struct zebra_dplane_ctx
*ctx
)
1011 DPLANE_CTX_VALID(ctx
);
1013 return &(ctx
->zd_ns_info
);
1016 /* Accessors for LSP information */
1018 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
)
1020 DPLANE_CTX_VALID(ctx
);
1022 return ctx
->u
.lsp
.ile
.in_label
;
1025 void dplane_ctx_set_in_label(struct zebra_dplane_ctx
*ctx
, mpls_label_t label
)
1027 DPLANE_CTX_VALID(ctx
);
1029 ctx
->u
.lsp
.ile
.in_label
= label
;
1032 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
)
1034 DPLANE_CTX_VALID(ctx
);
1036 return ctx
->u
.lsp
.addr_family
;
1039 void dplane_ctx_set_addr_family(struct zebra_dplane_ctx
*ctx
,
1042 DPLANE_CTX_VALID(ctx
);
1044 ctx
->u
.lsp
.addr_family
= family
;
1047 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
)
1049 DPLANE_CTX_VALID(ctx
);
1051 return ctx
->u
.lsp
.flags
;
1054 void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx
*ctx
,
1057 DPLANE_CTX_VALID(ctx
);
1059 ctx
->u
.lsp
.flags
= flags
;
1062 const zebra_nhlfe_t
*dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx
*ctx
)
1064 DPLANE_CTX_VALID(ctx
);
1066 return ctx
->u
.lsp
.nhlfe_list
;
1069 zebra_nhlfe_t
*dplane_ctx_add_nhlfe(struct zebra_dplane_ctx
*ctx
,
1070 enum lsp_types_t lsp_type
,
1071 enum nexthop_types_t nh_type
,
1074 mpls_label_t out_label
)
1076 zebra_nhlfe_t
*nhlfe
;
1078 DPLANE_CTX_VALID(ctx
);
1080 nhlfe
= zebra_mpls_lsp_add_nhlfe(&(ctx
->u
.lsp
),
1081 lsp_type
, nh_type
, gate
,
1082 ifindex
, out_label
);
1087 const zebra_nhlfe_t
*
1088 dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx
*ctx
)
1090 DPLANE_CTX_VALID(ctx
);
1092 return ctx
->u
.lsp
.best_nhlfe
;
1095 const zebra_nhlfe_t
*
1096 dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx
*ctx
,
1097 zebra_nhlfe_t
*nhlfe
)
1099 DPLANE_CTX_VALID(ctx
);
1101 ctx
->u
.lsp
.best_nhlfe
= nhlfe
;
1102 return ctx
->u
.lsp
.best_nhlfe
;
1105 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
)
1107 DPLANE_CTX_VALID(ctx
);
1109 return ctx
->u
.lsp
.num_ecmp
;
1112 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
)
1114 DPLANE_CTX_VALID(ctx
);
1116 return ctx
->u
.pw
.local_label
;
1119 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
)
1121 DPLANE_CTX_VALID(ctx
);
1123 return ctx
->u
.pw
.remote_label
;
1126 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
)
1128 DPLANE_CTX_VALID(ctx
);
1130 return ctx
->u
.pw
.type
;
1133 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
)
1135 DPLANE_CTX_VALID(ctx
);
1137 return ctx
->u
.pw
.af
;
1140 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
)
1142 DPLANE_CTX_VALID(ctx
);
1144 return ctx
->u
.pw
.flags
;
1147 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
)
1149 DPLANE_CTX_VALID(ctx
);
1151 return ctx
->u
.pw
.status
;
1154 const union g_addr
*dplane_ctx_get_pw_dest(
1155 const struct zebra_dplane_ctx
*ctx
)
1157 DPLANE_CTX_VALID(ctx
);
1159 return &(ctx
->u
.pw
.dest
);
1162 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
1163 const struct zebra_dplane_ctx
*ctx
)
1165 DPLANE_CTX_VALID(ctx
);
1167 return &(ctx
->u
.pw
.fields
);
1170 const struct nexthop_group
*
1171 dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx
*ctx
)
1173 DPLANE_CTX_VALID(ctx
);
1175 return &(ctx
->u
.pw
.nhg
);
1178 /* Accessors for interface information */
1179 uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx
*ctx
)
1181 DPLANE_CTX_VALID(ctx
);
1183 return ctx
->u
.intf
.metric
;
1186 /* Is interface addr p2p? */
1187 bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx
*ctx
)
1189 DPLANE_CTX_VALID(ctx
);
1191 return (ctx
->u
.intf
.flags
& DPLANE_INTF_CONNECTED
);
1194 bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx
*ctx
)
1196 DPLANE_CTX_VALID(ctx
);
1198 return (ctx
->u
.intf
.flags
& DPLANE_INTF_SECONDARY
);
1201 bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx
*ctx
)
1203 DPLANE_CTX_VALID(ctx
);
1205 return (ctx
->u
.intf
.flags
& DPLANE_INTF_BROADCAST
);
1208 const struct prefix
*dplane_ctx_get_intf_addr(
1209 const struct zebra_dplane_ctx
*ctx
)
1211 DPLANE_CTX_VALID(ctx
);
1213 return &(ctx
->u
.intf
.prefix
);
1216 bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx
*ctx
)
1218 DPLANE_CTX_VALID(ctx
);
1220 return (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_DEST
);
1223 const struct prefix
*dplane_ctx_get_intf_dest(
1224 const struct zebra_dplane_ctx
*ctx
)
1226 DPLANE_CTX_VALID(ctx
);
1228 if (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_DEST
)
1229 return &(ctx
->u
.intf
.dest_prefix
);
1234 bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx
*ctx
)
1236 DPLANE_CTX_VALID(ctx
);
1238 return (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_LABEL
);
1241 const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx
*ctx
)
1243 DPLANE_CTX_VALID(ctx
);
1245 return ctx
->u
.intf
.label
;
1248 /* Accessors for MAC information */
1249 vlanid_t
dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx
*ctx
)
1251 DPLANE_CTX_VALID(ctx
);
1252 return ctx
->u
.macinfo
.vid
;
1255 bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx
*ctx
)
1257 DPLANE_CTX_VALID(ctx
);
1258 return ctx
->u
.macinfo
.is_sticky
;
1261 const struct ethaddr
*dplane_ctx_mac_get_addr(
1262 const struct zebra_dplane_ctx
*ctx
)
1264 DPLANE_CTX_VALID(ctx
);
1265 return &(ctx
->u
.macinfo
.mac
);
1268 const struct in_addr
*dplane_ctx_mac_get_vtep_ip(
1269 const struct zebra_dplane_ctx
*ctx
)
1271 DPLANE_CTX_VALID(ctx
);
1272 return &(ctx
->u
.macinfo
.vtep_ip
);
1275 /* Accessors for neighbor information */
1276 const struct ipaddr
*dplane_ctx_neigh_get_ipaddr(
1277 const struct zebra_dplane_ctx
*ctx
)
1279 DPLANE_CTX_VALID(ctx
);
1280 return &(ctx
->u
.neigh
.ip_addr
);
1283 const struct ethaddr
*dplane_ctx_neigh_get_mac(
1284 const struct zebra_dplane_ctx
*ctx
)
1286 DPLANE_CTX_VALID(ctx
);
1287 return &(ctx
->u
.neigh
.mac
);
1290 uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx
*ctx
)
1292 DPLANE_CTX_VALID(ctx
);
1293 return ctx
->u
.neigh
.flags
;
1296 uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx
*ctx
)
1298 DPLANE_CTX_VALID(ctx
);
1299 return ctx
->u
.neigh
.state
;
1303 * End of dplane context accessors
1308 * Retrieve the limit on the number of pending, unprocessed updates.
1310 uint32_t dplane_get_in_queue_limit(void)
1312 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
1313 memory_order_relaxed
);
1317 * Configure limit on the number of pending, queued updates.
1319 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
1321 /* Reset to default on 'unset' */
1323 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
1325 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
1326 memory_order_relaxed
);
1330 * Retrieve the current queue depth of incoming, unprocessed updates
1332 uint32_t dplane_get_in_queue_len(void)
1334 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
1335 memory_order_seq_cst
);
1339 * Common dataplane context init with zebra namespace info.
1341 static int dplane_ctx_ns_init(struct zebra_dplane_ctx
*ctx
,
1342 struct zebra_ns
*zns
,
1345 dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
);
1347 #if defined(HAVE_NETLINK)
1348 /* Increment message counter after copying to context struct - may need
1349 * two messages in some 'update' cases.
1352 zns
->netlink_dplane
.seq
+= 2;
1354 zns
->netlink_dplane
.seq
++;
1355 #endif /* HAVE_NETLINK */
1361 * Initialize a context block for a route update from zebra data structs.
1363 static int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
,
1364 enum dplane_op_e op
,
1365 struct route_node
*rn
,
1366 struct route_entry
*re
)
1369 const struct route_table
*table
= NULL
;
1370 const rib_table_info_t
*info
;
1371 const struct prefix
*p
, *src_p
;
1372 struct zebra_ns
*zns
;
1373 struct zebra_vrf
*zvrf
;
1374 struct nexthop
*nexthop
;
1376 if (!ctx
|| !rn
|| !re
)
1380 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1382 ctx
->u
.rinfo
.zd_type
= re
->type
;
1383 ctx
->u
.rinfo
.zd_old_type
= re
->type
;
1385 /* Prefixes: dest, and optional source */
1386 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1388 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), p
);
1391 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src_p
);
1393 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(ctx
->u
.rinfo
.zd_src
));
1395 ctx
->zd_table_id
= re
->table
;
1397 ctx
->u
.rinfo
.zd_metric
= re
->metric
;
1398 ctx
->u
.rinfo
.zd_old_metric
= re
->metric
;
1399 ctx
->zd_vrf_id
= re
->vrf_id
;
1400 ctx
->u
.rinfo
.zd_mtu
= re
->mtu
;
1401 ctx
->u
.rinfo
.zd_nexthop_mtu
= re
->nexthop_mtu
;
1402 ctx
->u
.rinfo
.zd_instance
= re
->instance
;
1403 ctx
->u
.rinfo
.zd_tag
= re
->tag
;
1404 ctx
->u
.rinfo
.zd_old_tag
= re
->tag
;
1405 ctx
->u
.rinfo
.zd_distance
= re
->distance
;
1407 table
= srcdest_rnode_table(rn
);
1410 ctx
->u
.rinfo
.zd_afi
= info
->afi
;
1411 ctx
->u
.rinfo
.zd_safi
= info
->safi
;
1413 /* Copy nexthops; recursive info is included too */
1414 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
1416 /* Ensure that the dplane's nexthops flags are clear. */
1417 for (ALL_NEXTHOPS(ctx
->u
.rinfo
.zd_ng
, nexthop
))
1418 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1420 /* Don't need some info when capturing a system notification */
1421 if (op
== DPLANE_OP_SYS_ROUTE_ADD
||
1422 op
== DPLANE_OP_SYS_ROUTE_DELETE
) {
1427 /* Extract ns info - can't use pointers to 'core' structs */
1428 zvrf
= vrf_info_lookup(re
->vrf_id
);
1430 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_ROUTE_UPDATE
));
1432 /* Trying out the sequence number idea, so we can try to detect
1433 * when a result is stale.
1435 re
->dplane_sequence
= zebra_router_get_next_sequence();
1436 ctx
->zd_seq
= re
->dplane_sequence
;
1445 * Capture information for an LSP update in a dplane context.
1447 static int dplane_ctx_lsp_init(struct zebra_dplane_ctx
*ctx
,
1448 enum dplane_op_e op
,
1452 zebra_nhlfe_t
*nhlfe
, *new_nhlfe
;
1454 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1455 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1456 dplane_op2str(op
), lsp
->ile
.in_label
,
1460 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1462 /* Capture namespace info */
1463 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
1464 (op
== DPLANE_OP_LSP_UPDATE
));
1466 memset(&ctx
->u
.lsp
, 0, sizeof(ctx
->u
.lsp
));
1468 ctx
->u
.lsp
.ile
= lsp
->ile
;
1469 ctx
->u
.lsp
.addr_family
= lsp
->addr_family
;
1470 ctx
->u
.lsp
.num_ecmp
= lsp
->num_ecmp
;
1471 ctx
->u
.lsp
.flags
= lsp
->flags
;
1473 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
1474 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1475 /* Not sure if this is meaningful... */
1476 if (nhlfe
->nexthop
== NULL
)
1480 zebra_mpls_lsp_add_nhlfe(
1483 nhlfe
->nexthop
->type
,
1484 &(nhlfe
->nexthop
->gate
),
1485 nhlfe
->nexthop
->ifindex
,
1486 nhlfe
->nexthop
->nh_label
->label
[0]);
1488 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
1493 /* Need to copy flags too */
1494 new_nhlfe
->flags
= nhlfe
->flags
;
1495 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
1497 if (nhlfe
== lsp
->best_nhlfe
)
1498 ctx
->u
.lsp
.best_nhlfe
= new_nhlfe
;
1501 /* On error the ctx will be cleaned-up, so we don't need to
1502 * deal with any allocated nhlfe or nexthop structs here.
1509 * Capture information for an LSP update in a dplane context.
1511 static int dplane_ctx_pw_init(struct zebra_dplane_ctx
*ctx
,
1512 enum dplane_op_e op
,
1513 struct zebra_pw
*pw
)
1517 struct route_table
*table
;
1518 struct route_node
*rn
;
1519 struct route_entry
*re
;
1521 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1522 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
1523 dplane_op2str(op
), pw
->ifname
, pw
->local_label
,
1527 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1529 /* Capture namespace info: no netlink support as of 12/18,
1530 * but just in case...
1532 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
1534 memset(&ctx
->u
.pw
, 0, sizeof(ctx
->u
.pw
));
1536 /* This name appears to be c-string, so we use string copy. */
1537 strlcpy(ctx
->zd_ifname
, pw
->ifname
, sizeof(ctx
->zd_ifname
));
1539 ctx
->zd_vrf_id
= pw
->vrf_id
;
1540 ctx
->zd_ifindex
= pw
->ifindex
;
1541 ctx
->u
.pw
.type
= pw
->type
;
1542 ctx
->u
.pw
.af
= pw
->af
;
1543 ctx
->u
.pw
.local_label
= pw
->local_label
;
1544 ctx
->u
.pw
.remote_label
= pw
->remote_label
;
1545 ctx
->u
.pw
.flags
= pw
->flags
;
1547 ctx
->u
.pw
.dest
= pw
->nexthop
;
1549 ctx
->u
.pw
.fields
= pw
->data
;
1551 /* Capture nexthop info for the pw destination. We need to look
1552 * up and use zebra datastructs, but we're running in the zebra
1553 * pthread here so that should be ok.
1555 memcpy(&p
.u
, &pw
->nexthop
, sizeof(pw
->nexthop
));
1557 p
.prefixlen
= ((pw
->af
== AF_INET
) ?
1558 IPV4_MAX_PREFIXLEN
: IPV6_MAX_PREFIXLEN
);
1560 afi
= (pw
->af
== AF_INET
) ? AFI_IP
: AFI_IP6
;
1561 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, pw
->vrf_id
);
1563 rn
= route_node_match(table
, &p
);
1565 RNODE_FOREACH_RE(rn
, re
) {
1566 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
1571 copy_nexthops(&(ctx
->u
.pw
.nhg
.nexthop
),
1572 re
->ng
.nexthop
, NULL
);
1574 route_unlock_node(rn
);
1582 * Enqueue a new update,
1583 * and ensure an event is active for the dataplane pthread.
1585 static int dplane_update_enqueue(struct zebra_dplane_ctx
*ctx
)
1588 uint32_t high
, curr
;
1590 /* Enqueue for processing by the dataplane pthread */
1593 TAILQ_INSERT_TAIL(&zdplane_info
.dg_update_ctx_q
, ctx
,
1598 curr
= atomic_add_fetch_explicit(
1600 /* TODO -- issue with the clang atomic/intrinsics currently;
1601 * casting away the 'Atomic'-ness of the variable works.
1603 (uint32_t *)&(zdplane_info
.dg_routes_queued
),
1605 &(zdplane_info
.dg_routes_queued
),
1607 1, memory_order_seq_cst
);
1609 /* Maybe update high-water counter also */
1610 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1611 memory_order_seq_cst
);
1612 while (high
< curr
) {
1613 if (atomic_compare_exchange_weak_explicit(
1614 &zdplane_info
.dg_routes_queued_max
,
1616 memory_order_seq_cst
,
1617 memory_order_seq_cst
))
1621 /* Ensure that an event for the dataplane thread is active */
1622 ret
= dplane_provider_work_ready();
1628 * Utility that prepares a route update and enqueues it for processing
1630 static enum zebra_dplane_result
1631 dplane_route_update_internal(struct route_node
*rn
,
1632 struct route_entry
*re
,
1633 struct route_entry
*old_re
,
1634 enum dplane_op_e op
)
1636 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1638 struct zebra_dplane_ctx
*ctx
= NULL
;
1640 /* Obtain context block */
1641 ctx
= dplane_ctx_alloc();
1643 /* Init context with info from zebra data structs */
1644 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
1646 /* Capture some extra info for update case
1647 * where there's a different 'old' route.
1649 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
1650 old_re
&& (old_re
!= re
)) {
1651 ctx
->zd_is_update
= true;
1653 old_re
->dplane_sequence
=
1654 zebra_router_get_next_sequence();
1655 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
1657 ctx
->u
.rinfo
.zd_old_tag
= old_re
->tag
;
1658 ctx
->u
.rinfo
.zd_old_type
= old_re
->type
;
1659 ctx
->u
.rinfo
.zd_old_instance
= old_re
->instance
;
1660 ctx
->u
.rinfo
.zd_old_distance
= old_re
->distance
;
1661 ctx
->u
.rinfo
.zd_old_metric
= old_re
->metric
;
1663 #ifndef HAVE_NETLINK
1664 /* For bsd, capture previous re's nexthops too, sigh.
1665 * We'll need these to do per-nexthop deletes.
1667 copy_nexthops(&(ctx
->u
.rinfo
.zd_old_ng
.nexthop
),
1668 old_re
->ng
.nexthop
, NULL
);
1669 #endif /* !HAVE_NETLINK */
1672 /* Enqueue context for processing */
1673 ret
= dplane_update_enqueue(ctx
);
1676 /* Update counter */
1677 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
1678 memory_order_relaxed
);
1681 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1683 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
1684 memory_order_relaxed
);
1686 dplane_ctx_free(&ctx
);
1693 * Enqueue a route 'add' for the dataplane.
1695 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
1696 struct route_entry
*re
)
1698 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1700 if (rn
== NULL
|| re
== NULL
)
1703 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1704 DPLANE_OP_ROUTE_INSTALL
);
1711 * Enqueue a route update for the dataplane.
1713 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
1714 struct route_entry
*re
,
1715 struct route_entry
*old_re
)
1717 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1719 if (rn
== NULL
|| re
== NULL
)
1722 ret
= dplane_route_update_internal(rn
, re
, old_re
,
1723 DPLANE_OP_ROUTE_UPDATE
);
1729 * Enqueue a route removal for the dataplane.
1731 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
1732 struct route_entry
*re
)
1734 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1736 if (rn
== NULL
|| re
== NULL
)
1739 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1740 DPLANE_OP_ROUTE_DELETE
);
1747 * Notify the dplane when system/connected routes change.
1749 enum zebra_dplane_result
dplane_sys_route_add(struct route_node
*rn
,
1750 struct route_entry
*re
)
1752 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1754 /* Ignore this event unless a provider plugin has requested it. */
1755 if (!zdplane_info
.dg_sys_route_notifs
) {
1756 ret
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1760 if (rn
== NULL
|| re
== NULL
)
1763 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1764 DPLANE_OP_SYS_ROUTE_ADD
);
1771 * Notify the dplane when system/connected routes are deleted.
1773 enum zebra_dplane_result
dplane_sys_route_del(struct route_node
*rn
,
1774 struct route_entry
*re
)
1776 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1778 /* Ignore this event unless a provider plugin has requested it. */
1779 if (!zdplane_info
.dg_sys_route_notifs
) {
1780 ret
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1784 if (rn
== NULL
|| re
== NULL
)
1787 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1788 DPLANE_OP_SYS_ROUTE_DELETE
);
1795 * Update from an async notification, to bring other fibs up-to-date.
1797 enum zebra_dplane_result
1798 dplane_route_notif_update(struct route_node
*rn
,
1799 struct route_entry
*re
,
1800 enum dplane_op_e op
,
1801 struct zebra_dplane_ctx
*ctx
)
1803 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1804 struct zebra_dplane_ctx
*new_ctx
= NULL
;
1805 struct nexthop
*nexthop
;
1807 if (rn
== NULL
|| re
== NULL
)
1810 new_ctx
= dplane_ctx_alloc();
1811 if (new_ctx
== NULL
)
1814 /* Init context with info from zebra data structs */
1815 dplane_ctx_route_init(new_ctx
, op
, rn
, re
);
1817 /* For add/update, need to adjust the nexthops so that we match
1818 * the notification state, which may not be the route-entry/RIB
1821 if (op
== DPLANE_OP_ROUTE_UPDATE
||
1822 op
== DPLANE_OP_ROUTE_INSTALL
) {
1824 nexthops_free(new_ctx
->u
.rinfo
.zd_ng
.nexthop
);
1825 new_ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
1827 copy_nexthops(&(new_ctx
->u
.rinfo
.zd_ng
.nexthop
),
1828 (rib_active_nhg(re
))->nexthop
, NULL
);
1830 for (ALL_NEXTHOPS(new_ctx
->u
.rinfo
.zd_ng
, nexthop
))
1831 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1835 /* Capture info about the source of the notification, in 'ctx' */
1836 dplane_ctx_set_notif_provider(new_ctx
,
1837 dplane_ctx_get_notif_provider(ctx
));
1839 dplane_update_enqueue(new_ctx
);
1841 ret
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1848 * Enqueue LSP add for the dataplane.
1850 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
)
1852 enum zebra_dplane_result ret
=
1853 lsp_update_internal(lsp
, DPLANE_OP_LSP_INSTALL
);
1859 * Enqueue LSP update for the dataplane.
1861 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
)
1863 enum zebra_dplane_result ret
=
1864 lsp_update_internal(lsp
, DPLANE_OP_LSP_UPDATE
);
1870 * Enqueue LSP delete for the dataplane.
1872 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
)
1874 enum zebra_dplane_result ret
=
1875 lsp_update_internal(lsp
, DPLANE_OP_LSP_DELETE
);
1880 /* Update or un-install resulting from an async notification */
1881 enum zebra_dplane_result
1882 dplane_lsp_notif_update(zebra_lsp_t
*lsp
,
1883 enum dplane_op_e op
,
1884 struct zebra_dplane_ctx
*notif_ctx
)
1886 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1888 struct zebra_dplane_ctx
*ctx
= NULL
;
1890 /* Obtain context block */
1891 ctx
= dplane_ctx_alloc();
1897 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1901 /* Capture info about the source of the notification */
1902 dplane_ctx_set_notif_provider(
1904 dplane_ctx_get_notif_provider(notif_ctx
));
1906 ret
= dplane_update_enqueue(ctx
);
1909 /* Update counter */
1910 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1911 memory_order_relaxed
);
1914 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1916 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1917 memory_order_relaxed
);
1919 dplane_ctx_free(&ctx
);
1925 * Enqueue pseudowire install for the dataplane.
1927 enum zebra_dplane_result
dplane_pw_install(struct zebra_pw
*pw
)
1929 return pw_update_internal(pw
, DPLANE_OP_PW_INSTALL
);
1933 * Enqueue pseudowire un-install for the dataplane.
1935 enum zebra_dplane_result
dplane_pw_uninstall(struct zebra_pw
*pw
)
1937 return pw_update_internal(pw
, DPLANE_OP_PW_UNINSTALL
);
1941 * Common internal LSP update utility
1943 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
1944 enum dplane_op_e op
)
1946 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1948 struct zebra_dplane_ctx
*ctx
= NULL
;
1950 /* Obtain context block */
1951 ctx
= dplane_ctx_alloc();
1953 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1957 ret
= dplane_update_enqueue(ctx
);
1960 /* Update counter */
1961 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1962 memory_order_relaxed
);
1965 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1967 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1968 memory_order_relaxed
);
1969 dplane_ctx_free(&ctx
);
1976 * Internal, common handler for pseudowire updates.
1978 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
1979 enum dplane_op_e op
)
1981 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1983 struct zebra_dplane_ctx
*ctx
= NULL
;
1985 ctx
= dplane_ctx_alloc();
1987 ret
= dplane_ctx_pw_init(ctx
, op
, pw
);
1991 ret
= dplane_update_enqueue(ctx
);
1994 /* Update counter */
1995 atomic_fetch_add_explicit(&zdplane_info
.dg_pws_in
, 1,
1996 memory_order_relaxed
);
1999 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2001 atomic_fetch_add_explicit(&zdplane_info
.dg_pw_errors
, 1,
2002 memory_order_relaxed
);
2003 dplane_ctx_free(&ctx
);
2010 * Enqueue interface address add for the dataplane.
2012 enum zebra_dplane_result
dplane_intf_addr_set(const struct interface
*ifp
,
2013 const struct connected
*ifc
)
2015 #if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2016 /* Extra checks for this OS path. */
2018 /* Don't configure PtP addresses on broadcast ifs or reverse */
2019 if (!(ifp
->flags
& IFF_POINTOPOINT
) != !CONNECTED_PEER(ifc
)) {
2020 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_DPLANE
)
2021 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2023 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2026 /* Ensure that no existing installed v4 route conflicts with
2027 * the new interface prefix. This check must be done in the
2028 * zebra pthread context, and any route delete (if needed)
2029 * is enqueued before the interface address programming attempt.
2031 if (ifc
->address
->family
== AF_INET
) {
2032 struct prefix_ipv4
*p
;
2034 p
= (struct prefix_ipv4
*)ifc
->address
;
2035 rib_lookup_and_pushup(p
, ifp
->vrf_id
);
2039 return intf_addr_update_internal(ifp
, ifc
, DPLANE_OP_ADDR_INSTALL
);
2043 * Enqueue interface address remove/uninstall for the dataplane.
2045 enum zebra_dplane_result
dplane_intf_addr_unset(const struct interface
*ifp
,
2046 const struct connected
*ifc
)
2048 return intf_addr_update_internal(ifp
, ifc
, DPLANE_OP_ADDR_UNINSTALL
);
2051 static enum zebra_dplane_result
intf_addr_update_internal(
2052 const struct interface
*ifp
, const struct connected
*ifc
,
2053 enum dplane_op_e op
)
2055 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2057 struct zebra_dplane_ctx
*ctx
= NULL
;
2058 struct zebra_ns
*zns
;
2060 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2061 char addr_str
[PREFIX_STRLEN
];
2063 prefix2str(ifc
->address
, addr_str
, sizeof(addr_str
));
2065 zlog_debug("init intf ctx %s: idx %d, addr %u:%s",
2066 dplane_op2str(op
), ifp
->ifindex
, ifp
->vrf_id
,
2070 ctx
= dplane_ctx_alloc();
2073 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2074 ctx
->zd_vrf_id
= ifp
->vrf_id
;
2076 zns
= zebra_ns_lookup(ifp
->vrf_id
);
2077 dplane_ctx_ns_init(ctx
, zns
, false);
2079 /* Init the interface-addr-specific area */
2080 memset(&ctx
->u
.intf
, 0, sizeof(ctx
->u
.intf
));
2082 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
2083 ctx
->zd_ifindex
= ifp
->ifindex
;
2084 ctx
->u
.intf
.prefix
= *(ifc
->address
);
2086 if (if_is_broadcast(ifp
))
2087 ctx
->u
.intf
.flags
|= DPLANE_INTF_BROADCAST
;
2089 if (CONNECTED_PEER(ifc
)) {
2090 ctx
->u
.intf
.dest_prefix
= *(ifc
->destination
);
2091 ctx
->u
.intf
.flags
|=
2092 (DPLANE_INTF_CONNECTED
| DPLANE_INTF_HAS_DEST
);
2095 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
2096 ctx
->u
.intf
.flags
|= DPLANE_INTF_SECONDARY
;
2101 ctx
->u
.intf
.flags
|= DPLANE_INTF_HAS_LABEL
;
2103 /* Use embedded buffer if it's adequate; else allocate. */
2104 len
= strlen(ifc
->label
);
2106 if (len
< sizeof(ctx
->u
.intf
.label_buf
)) {
2107 strlcpy(ctx
->u
.intf
.label_buf
, ifc
->label
,
2108 sizeof(ctx
->u
.intf
.label_buf
));
2109 ctx
->u
.intf
.label
= ctx
->u
.intf
.label_buf
;
2111 ctx
->u
.intf
.label
= strdup(ifc
->label
);
2115 ret
= dplane_update_enqueue(ctx
);
2117 /* Increment counter */
2118 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addrs_in
, 1,
2119 memory_order_relaxed
);
2122 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2125 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addr_errors
,
2126 1, memory_order_relaxed
);
2127 dplane_ctx_free(&ctx
);
2134 * Enqueue vxlan/evpn mac add (or update).
2136 enum zebra_dplane_result
dplane_mac_add(const struct interface
*ifp
,
2138 const struct ethaddr
*mac
,
2139 struct in_addr vtep_ip
,
2142 enum zebra_dplane_result result
;
2144 /* Use common helper api */
2145 result
= mac_update_internal(DPLANE_OP_MAC_INSTALL
, ifp
, vid
,
2146 mac
, vtep_ip
, sticky
);
2151 * Enqueue vxlan/evpn mac delete.
2153 enum zebra_dplane_result
dplane_mac_del(const struct interface
*ifp
,
2155 const struct ethaddr
*mac
,
2156 struct in_addr vtep_ip
)
2158 enum zebra_dplane_result result
;
2160 /* Use common helper api */
2161 result
= mac_update_internal(DPLANE_OP_MAC_DELETE
, ifp
, vid
, mac
,
2167 * Common helper api for MAC address/vxlan updates
2169 static enum zebra_dplane_result
2170 mac_update_internal(enum dplane_op_e op
,
2171 const struct interface
*ifp
,
2173 const struct ethaddr
*mac
,
2174 struct in_addr vtep_ip
,
2177 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2179 struct zebra_dplane_ctx
*ctx
= NULL
;
2180 struct zebra_ns
*zns
;
2182 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2183 char buf1
[ETHER_ADDR_STRLEN
], buf2
[PREFIX_STRLEN
];
2185 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
2187 prefix_mac2str(mac
, buf1
, sizeof(buf1
)),
2189 inet_ntop(AF_INET
, &vtep_ip
, buf2
, sizeof(buf2
)));
2192 ctx
= dplane_ctx_alloc();
2195 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2196 ctx
->zd_vrf_id
= ifp
->vrf_id
;
2198 zns
= zebra_ns_lookup(ifp
->vrf_id
);
2199 dplane_ctx_ns_init(ctx
, zns
, false);
2201 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
2202 ctx
->zd_ifindex
= ifp
->ifindex
;
2204 /* Init the mac-specific data area */
2205 memset(&ctx
->u
.macinfo
, 0, sizeof(ctx
->u
.macinfo
));
2207 ctx
->u
.macinfo
.vtep_ip
= vtep_ip
;
2208 ctx
->u
.macinfo
.mac
= *mac
;
2209 ctx
->u
.macinfo
.vid
= vid
;
2210 ctx
->u
.macinfo
.is_sticky
= sticky
;
2212 /* Enqueue for processing on the dplane pthread */
2213 ret
= dplane_update_enqueue(ctx
);
2215 /* Increment counter */
2216 atomic_fetch_add_explicit(&zdplane_info
.dg_macs_in
, 1,
2217 memory_order_relaxed
);
2220 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2223 atomic_fetch_add_explicit(&zdplane_info
.dg_mac_errors
, 1,
2224 memory_order_relaxed
);
2225 dplane_ctx_free(&ctx
);
2232 * Enqueue evpn neighbor add for the dataplane.
2234 enum zebra_dplane_result
dplane_neigh_add(const struct interface
*ifp
,
2235 const struct ipaddr
*ip
,
2236 const struct ethaddr
*mac
,
2239 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2241 result
= neigh_update_internal(DPLANE_OP_NEIGH_INSTALL
,
2242 ifp
, mac
, ip
, flags
, DPLANE_NUD_NOARP
);
2248 * Enqueue evpn neighbor update for the dataplane.
2250 enum zebra_dplane_result
dplane_neigh_update(const struct interface
*ifp
,
2251 const struct ipaddr
*ip
,
2252 const struct ethaddr
*mac
)
2254 enum zebra_dplane_result result
;
2256 result
= neigh_update_internal(DPLANE_OP_NEIGH_UPDATE
,
2257 ifp
, mac
, ip
, 0, DPLANE_NUD_PROBE
);
2263 * Enqueue evpn neighbor delete for the dataplane.
2265 enum zebra_dplane_result
dplane_neigh_delete(const struct interface
*ifp
,
2266 const struct ipaddr
*ip
)
2268 enum zebra_dplane_result result
;
2270 result
= neigh_update_internal(DPLANE_OP_NEIGH_DELETE
,
2271 ifp
, NULL
, ip
, 0, 0);
2277 * Enqueue evpn VTEP add for the dataplane.
2279 enum zebra_dplane_result
dplane_vtep_add(const struct interface
*ifp
,
2280 const struct in_addr
*ip
,
2283 enum zebra_dplane_result result
;
2284 struct ethaddr mac
= { {0, 0, 0, 0, 0, 0} };
2287 if (IS_ZEBRA_DEBUG_VXLAN
)
2288 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
2289 inet_ntoa(*ip
), vni
, ifp
->name
, ifp
->ifindex
);
2291 SET_IPADDR_V4(&addr
);
2292 addr
.ipaddr_v4
= *ip
;
2294 result
= neigh_update_internal(DPLANE_OP_VTEP_ADD
,
2295 ifp
, &mac
, &addr
, 0, 0);
2301 * Enqueue evpn VTEP add for the dataplane.
2303 enum zebra_dplane_result
dplane_vtep_delete(const struct interface
*ifp
,
2304 const struct in_addr
*ip
,
2307 enum zebra_dplane_result result
;
2308 struct ethaddr mac
= { {0, 0, 0, 0, 0, 0} };
2311 if (IS_ZEBRA_DEBUG_VXLAN
)
2313 "Uninstall %s from flood list for VNI %u intf %s(%u)",
2314 inet_ntoa(*ip
), vni
, ifp
->name
, ifp
->ifindex
);
2316 SET_IPADDR_V4(&addr
);
2317 addr
.ipaddr_v4
= *ip
;
2319 result
= neigh_update_internal(DPLANE_OP_VTEP_DELETE
,
2320 ifp
, &mac
, &addr
, 0, 0);
2326 * Common helper api for evpn neighbor updates
2328 static enum zebra_dplane_result
2329 neigh_update_internal(enum dplane_op_e op
,
2330 const struct interface
*ifp
,
2331 const struct ethaddr
*mac
,
2332 const struct ipaddr
*ip
,
2333 uint32_t flags
, uint16_t state
)
2335 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2337 struct zebra_dplane_ctx
*ctx
= NULL
;
2338 struct zebra_ns
*zns
;
2340 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2341 char buf1
[ETHER_ADDR_STRLEN
], buf2
[PREFIX_STRLEN
];
2343 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
2345 prefix_mac2str(mac
, buf1
, sizeof(buf1
)),
2347 ipaddr2str(ip
, buf2
, sizeof(buf2
)));
2350 ctx
= dplane_ctx_alloc();
2353 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2354 ctx
->zd_vrf_id
= ifp
->vrf_id
;
2356 zns
= zebra_ns_lookup(ifp
->vrf_id
);
2357 dplane_ctx_ns_init(ctx
, zns
, false);
2359 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
2360 ctx
->zd_ifindex
= ifp
->ifindex
;
2362 /* Init the neighbor-specific data area */
2363 memset(&ctx
->u
.neigh
, 0, sizeof(ctx
->u
.neigh
));
2365 ctx
->u
.neigh
.ip_addr
= *ip
;
2367 ctx
->u
.neigh
.mac
= *mac
;
2368 ctx
->u
.neigh
.flags
= flags
;
2369 ctx
->u
.neigh
.state
= state
;
2371 /* Enqueue for processing on the dplane pthread */
2372 ret
= dplane_update_enqueue(ctx
);
2374 /* Increment counter */
2375 atomic_fetch_add_explicit(&zdplane_info
.dg_neighs_in
, 1,
2376 memory_order_relaxed
);
2379 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2382 atomic_fetch_add_explicit(&zdplane_info
.dg_neigh_errors
, 1,
2383 memory_order_relaxed
);
2384 dplane_ctx_free(&ctx
);
2391 * Handler for 'show dplane'
2393 int dplane_show_helper(struct vty
*vty
, bool detailed
)
2395 uint64_t queued
, queue_max
, limit
, errs
, incoming
, yields
,
2398 /* Using atomics because counters are being changed in different
2401 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
2402 memory_order_relaxed
);
2403 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
2404 memory_order_relaxed
);
2405 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
2406 memory_order_relaxed
);
2407 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
2408 memory_order_relaxed
);
2409 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
2410 memory_order_relaxed
);
2411 yields
= atomic_load_explicit(&zdplane_info
.dg_update_yields
,
2412 memory_order_relaxed
);
2413 other_errs
= atomic_load_explicit(&zdplane_info
.dg_other_errors
,
2414 memory_order_relaxed
);
2416 vty_out(vty
, "Zebra dataplane:\nRoute updates: %"PRIu64
"\n",
2418 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
2419 vty_out(vty
, "Other errors : %"PRIu64
"\n", other_errs
);
2420 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
2421 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
2422 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
2423 vty_out(vty
, "Dplane update yields: %"PRIu64
"\n", yields
);
2425 incoming
= atomic_load_explicit(&zdplane_info
.dg_lsps_in
,
2426 memory_order_relaxed
);
2427 errs
= atomic_load_explicit(&zdplane_info
.dg_lsp_errors
,
2428 memory_order_relaxed
);
2429 vty_out(vty
, "LSP updates: %"PRIu64
"\n", incoming
);
2430 vty_out(vty
, "LSP update errors: %"PRIu64
"\n", errs
);
2432 incoming
= atomic_load_explicit(&zdplane_info
.dg_pws_in
,
2433 memory_order_relaxed
);
2434 errs
= atomic_load_explicit(&zdplane_info
.dg_pw_errors
,
2435 memory_order_relaxed
);
2436 vty_out(vty
, "PW updates: %"PRIu64
"\n", incoming
);
2437 vty_out(vty
, "PW update errors: %"PRIu64
"\n", errs
);
2439 incoming
= atomic_load_explicit(&zdplane_info
.dg_intf_addrs_in
,
2440 memory_order_relaxed
);
2441 errs
= atomic_load_explicit(&zdplane_info
.dg_intf_addr_errors
,
2442 memory_order_relaxed
);
2443 vty_out(vty
, "Intf addr updates: %"PRIu64
"\n", incoming
);
2444 vty_out(vty
, "Intf addr errors: %"PRIu64
"\n", errs
);
2446 incoming
= atomic_load_explicit(&zdplane_info
.dg_macs_in
,
2447 memory_order_relaxed
);
2448 errs
= atomic_load_explicit(&zdplane_info
.dg_mac_errors
,
2449 memory_order_relaxed
);
2450 vty_out(vty
, "EVPN MAC updates: %"PRIu64
"\n", incoming
);
2451 vty_out(vty
, "EVPN MAC errors: %"PRIu64
"\n", errs
);
2453 incoming
= atomic_load_explicit(&zdplane_info
.dg_neighs_in
,
2454 memory_order_relaxed
);
2455 errs
= atomic_load_explicit(&zdplane_info
.dg_neigh_errors
,
2456 memory_order_relaxed
);
2457 vty_out(vty
, "EVPN neigh updates: %"PRIu64
"\n", incoming
);
2458 vty_out(vty
, "EVPN neigh errors: %"PRIu64
"\n", errs
);
2464 * Handler for 'show dplane providers'
2466 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
2468 struct zebra_dplane_provider
*prov
;
2469 uint64_t in
, in_max
, out
, out_max
;
2471 vty_out(vty
, "Zebra dataplane providers:\n");
2474 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
2477 /* Show counters, useful info from each registered provider */
2480 in
= atomic_load_explicit(&prov
->dp_in_counter
,
2481 memory_order_relaxed
);
2482 in_max
= atomic_load_explicit(&prov
->dp_in_max
,
2483 memory_order_relaxed
);
2484 out
= atomic_load_explicit(&prov
->dp_out_counter
,
2485 memory_order_relaxed
);
2486 out_max
= atomic_load_explicit(&prov
->dp_out_max
,
2487 memory_order_relaxed
);
2489 vty_out(vty
, "%s (%u): in: %"PRIu64
", q_max: %"PRIu64
", "
2490 "out: %"PRIu64
", q_max: %"PRIu64
"\n",
2491 prov
->dp_name
, prov
->dp_id
, in
, in_max
, out
, out_max
);
2494 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
2502 * Helper for 'show run' etc.
2504 int dplane_config_write_helper(struct vty
*vty
)
2506 if (zdplane_info
.dg_max_queued_updates
!= DPLANE_DEFAULT_MAX_QUEUED
)
2507 vty_out(vty
, "zebra dplane limit %u\n",
2508 zdplane_info
.dg_max_queued_updates
);
2514 * Provider registration
2516 int dplane_provider_register(const char *name
,
2517 enum dplane_provider_prio prio
,
2519 int (*start_fp
)(struct zebra_dplane_provider
*),
2520 int (*fp
)(struct zebra_dplane_provider
*),
2521 int (*fini_fp
)(struct zebra_dplane_provider
*,
2524 struct zebra_dplane_provider
**prov_p
)
2527 struct zebra_dplane_provider
*p
= NULL
, *last
;
2535 if (prio
<= DPLANE_PRIO_NONE
||
2536 prio
> DPLANE_PRIO_LAST
) {
2541 /* Allocate and init new provider struct */
2542 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
2544 pthread_mutex_init(&(p
->dp_mutex
), NULL
);
2545 TAILQ_INIT(&(p
->dp_ctx_in_q
));
2546 TAILQ_INIT(&(p
->dp_ctx_out_q
));
2548 p
->dp_flags
= flags
;
2549 p
->dp_priority
= prio
;
2551 p
->dp_start
= start_fp
;
2552 p
->dp_fini
= fini_fp
;
2555 /* Lock - the dplane pthread may be running */
2558 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
2561 strlcpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
2563 snprintf(p
->dp_name
, DPLANE_PROVIDER_NAMELEN
,
2564 "provider-%u", p
->dp_id
);
2566 /* Insert into list ordered by priority */
2567 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
2568 if (last
->dp_priority
> p
->dp_priority
)
2573 TAILQ_INSERT_BEFORE(last
, p
, dp_prov_link
);
2575 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
2581 if (IS_ZEBRA_DEBUG_DPLANE
)
2582 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
2583 p
->dp_name
, p
->dp_id
, p
->dp_priority
);
2592 /* Accessors for provider attributes */
2593 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
)
2595 return prov
->dp_name
;
2598 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
)
2603 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
)
2605 return prov
->dp_data
;
2608 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
)
2610 return zdplane_info
.dg_updates_per_cycle
;
2613 /* Lock/unlock a provider's mutex - iff the provider was registered with
2614 * the THREADED flag.
2616 void dplane_provider_lock(struct zebra_dplane_provider
*prov
)
2618 if (dplane_provider_is_threaded(prov
))
2619 DPLANE_PROV_LOCK(prov
);
2622 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
)
2624 if (dplane_provider_is_threaded(prov
))
2625 DPLANE_PROV_UNLOCK(prov
);
2629 * Dequeue and maintain associated counter
2631 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
2632 struct zebra_dplane_provider
*prov
)
2634 struct zebra_dplane_ctx
*ctx
= NULL
;
2636 dplane_provider_lock(prov
);
2638 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
2640 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
2642 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, 1,
2643 memory_order_relaxed
);
2646 dplane_provider_unlock(prov
);
2652 * Dequeue work to a list, return count
2654 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
2655 struct dplane_ctx_q
*listp
)
2658 struct zebra_dplane_ctx
*ctx
;
2660 limit
= zdplane_info
.dg_updates_per_cycle
;
2662 dplane_provider_lock(prov
);
2664 for (ret
= 0; ret
< limit
; ret
++) {
2665 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
2667 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
2669 TAILQ_INSERT_TAIL(listp
, ctx
, zd_q_entries
);
2676 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, ret
,
2677 memory_order_relaxed
);
2679 dplane_provider_unlock(prov
);
2685 * Enqueue and maintain associated counter
2687 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
2688 struct zebra_dplane_ctx
*ctx
)
2690 dplane_provider_lock(prov
);
2692 TAILQ_INSERT_TAIL(&(prov
->dp_ctx_out_q
), ctx
,
2695 dplane_provider_unlock(prov
);
2697 atomic_fetch_add_explicit(&(prov
->dp_out_counter
), 1,
2698 memory_order_relaxed
);
2702 * Accessor for provider object
2704 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
)
2706 return (prov
->dp_flags
& DPLANE_PROV_FLAG_THREADED
);
2710 * Internal helper that copies information from a zebra ns object; this is
2711 * called in the zebra main pthread context as part of dplane ctx init.
2713 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
2714 struct zebra_ns
*zns
)
2716 ns_info
->ns_id
= zns
->ns_id
;
2718 #if defined(HAVE_NETLINK)
2719 ns_info
->is_cmd
= true;
2720 ns_info
->nls
= zns
->netlink_dplane
;
2721 #endif /* NETLINK */
2725 * Provider api to signal that work/events are available
2726 * for the dataplane pthread.
2728 int dplane_provider_work_ready(void)
2730 /* Note that during zebra startup, we may be offered work before
2731 * the dataplane pthread (and thread-master) are ready. We want to
2732 * enqueue the work, but the event-scheduling machinery may not be
2735 if (zdplane_info
.dg_run
) {
2736 thread_add_event(zdplane_info
.dg_master
,
2737 dplane_thread_loop
, NULL
, 0,
2738 &zdplane_info
.dg_t_update
);
2745 * Enqueue a context directly to zebra main.
2747 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx
*ctx
)
2749 struct dplane_ctx_q temp_list
;
2751 /* Zebra's api takes a list, so we need to use a temporary list */
2752 TAILQ_INIT(&temp_list
);
2754 TAILQ_INSERT_TAIL(&temp_list
, ctx
, zd_q_entries
);
2755 (zdplane_info
.dg_results_cb
)(&temp_list
);
2759 * Kernel dataplane provider
2763 * Handler for kernel LSP updates
2765 static enum zebra_dplane_result
2766 kernel_dplane_lsp_update(struct zebra_dplane_ctx
*ctx
)
2768 enum zebra_dplane_result res
;
2770 /* Call into the synchronous kernel-facing code here */
2771 res
= kernel_lsp_update(ctx
);
2773 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2774 atomic_fetch_add_explicit(
2775 &zdplane_info
.dg_lsp_errors
, 1,
2776 memory_order_relaxed
);
2782 * Handler for kernel pseudowire updates
2784 static enum zebra_dplane_result
2785 kernel_dplane_pw_update(struct zebra_dplane_ctx
*ctx
)
2787 enum zebra_dplane_result res
;
2789 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2790 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
2791 dplane_ctx_get_ifname(ctx
),
2792 dplane_op2str(ctx
->zd_op
),
2793 dplane_ctx_get_pw_af(ctx
),
2794 dplane_ctx_get_pw_local_label(ctx
),
2795 dplane_ctx_get_pw_remote_label(ctx
));
2797 res
= kernel_pw_update(ctx
);
2799 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2800 atomic_fetch_add_explicit(
2801 &zdplane_info
.dg_pw_errors
, 1,
2802 memory_order_relaxed
);
2808 * Handler for kernel route updates
2810 static enum zebra_dplane_result
2811 kernel_dplane_route_update(struct zebra_dplane_ctx
*ctx
)
2813 enum zebra_dplane_result res
;
2815 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2816 char dest_str
[PREFIX_STRLEN
];
2818 prefix2str(dplane_ctx_get_dest(ctx
),
2819 dest_str
, sizeof(dest_str
));
2821 zlog_debug("%u:%s Dplane route update ctx %p op %s",
2822 dplane_ctx_get_vrf(ctx
), dest_str
,
2823 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
2826 /* Call into the synchronous kernel-facing code here */
2827 res
= kernel_route_update(ctx
);
2829 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2830 atomic_fetch_add_explicit(
2831 &zdplane_info
.dg_route_errors
, 1,
2832 memory_order_relaxed
);
2838 * Handler for kernel-facing interface address updates
2840 static enum zebra_dplane_result
2841 kernel_dplane_address_update(struct zebra_dplane_ctx
*ctx
)
2843 enum zebra_dplane_result res
;
2845 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2846 char dest_str
[PREFIX_STRLEN
];
2848 prefix2str(dplane_ctx_get_intf_addr(ctx
), dest_str
,
2851 zlog_debug("Dplane intf %s, idx %u, addr %s",
2852 dplane_op2str(dplane_ctx_get_op(ctx
)),
2853 dplane_ctx_get_ifindex(ctx
), dest_str
);
2856 res
= kernel_address_update_ctx(ctx
);
2858 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2859 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addr_errors
,
2860 1, memory_order_relaxed
);
2866 * Handler for kernel-facing EVPN MAC address updates
2868 static enum zebra_dplane_result
2869 kernel_dplane_mac_update(struct zebra_dplane_ctx
*ctx
)
2871 enum zebra_dplane_result res
;
2873 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2874 char buf
[ETHER_ADDR_STRLEN
];
2876 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
,
2879 zlog_debug("Dplane %s, mac %s, ifindex %u",
2880 dplane_op2str(dplane_ctx_get_op(ctx
)),
2881 buf
, dplane_ctx_get_ifindex(ctx
));
2884 res
= kernel_mac_update_ctx(ctx
);
2886 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2887 atomic_fetch_add_explicit(&zdplane_info
.dg_mac_errors
,
2888 1, memory_order_relaxed
);
2894 * Handler for kernel-facing EVPN neighbor updates
2896 static enum zebra_dplane_result
2897 kernel_dplane_neigh_update(struct zebra_dplane_ctx
*ctx
)
2899 enum zebra_dplane_result res
;
2901 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2902 char buf
[PREFIX_STRLEN
];
2904 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx
), buf
,
2907 zlog_debug("Dplane %s, ip %s, ifindex %u",
2908 dplane_op2str(dplane_ctx_get_op(ctx
)),
2909 buf
, dplane_ctx_get_ifindex(ctx
));
2912 res
= kernel_neigh_update_ctx(ctx
);
2914 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2915 atomic_fetch_add_explicit(&zdplane_info
.dg_neigh_errors
,
2916 1, memory_order_relaxed
);
2922 * Kernel provider callback
2924 static int kernel_dplane_process_func(struct zebra_dplane_provider
*prov
)
2926 enum zebra_dplane_result res
;
2927 struct zebra_dplane_ctx
*ctx
;
2930 limit
= dplane_provider_get_work_limit(prov
);
2932 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2933 zlog_debug("dplane provider '%s': processing",
2934 dplane_provider_get_name(prov
));
2936 for (counter
= 0; counter
< limit
; counter
++) {
2938 ctx
= dplane_provider_dequeue_in_ctx(prov
);
2942 /* A previous provider plugin may have asked to skip the
2945 if (dplane_ctx_is_skip_kernel(ctx
)) {
2946 res
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2950 /* Dispatch to appropriate kernel-facing apis */
2951 switch (dplane_ctx_get_op(ctx
)) {
2953 case DPLANE_OP_ROUTE_INSTALL
:
2954 case DPLANE_OP_ROUTE_UPDATE
:
2955 case DPLANE_OP_ROUTE_DELETE
:
2956 res
= kernel_dplane_route_update(ctx
);
2959 case DPLANE_OP_LSP_INSTALL
:
2960 case DPLANE_OP_LSP_UPDATE
:
2961 case DPLANE_OP_LSP_DELETE
:
2962 res
= kernel_dplane_lsp_update(ctx
);
2965 case DPLANE_OP_PW_INSTALL
:
2966 case DPLANE_OP_PW_UNINSTALL
:
2967 res
= kernel_dplane_pw_update(ctx
);
2970 case DPLANE_OP_ADDR_INSTALL
:
2971 case DPLANE_OP_ADDR_UNINSTALL
:
2972 res
= kernel_dplane_address_update(ctx
);
2975 case DPLANE_OP_MAC_INSTALL
:
2976 case DPLANE_OP_MAC_DELETE
:
2977 res
= kernel_dplane_mac_update(ctx
);
2980 case DPLANE_OP_NEIGH_INSTALL
:
2981 case DPLANE_OP_NEIGH_UPDATE
:
2982 case DPLANE_OP_NEIGH_DELETE
:
2983 case DPLANE_OP_VTEP_ADD
:
2984 case DPLANE_OP_VTEP_DELETE
:
2985 res
= kernel_dplane_neigh_update(ctx
);
2988 /* Ignore 'notifications' - no-op */
2989 case DPLANE_OP_SYS_ROUTE_ADD
:
2990 case DPLANE_OP_SYS_ROUTE_DELETE
:
2991 case DPLANE_OP_ROUTE_NOTIFY
:
2992 case DPLANE_OP_LSP_NOTIFY
:
2993 res
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2997 atomic_fetch_add_explicit(
2998 &zdplane_info
.dg_other_errors
, 1,
2999 memory_order_relaxed
);
3001 res
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3006 dplane_ctx_set_status(ctx
, res
);
3008 dplane_provider_enqueue_out_ctx(prov
, ctx
);
3011 /* Ensure that we'll run the work loop again if there's still
3014 if (counter
>= limit
) {
3015 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3016 zlog_debug("dplane provider '%s' reached max updates %d",
3017 dplane_provider_get_name(prov
), counter
);
3019 atomic_fetch_add_explicit(&zdplane_info
.dg_update_yields
,
3020 1, memory_order_relaxed
);
3022 dplane_provider_work_ready();
3028 #if DPLANE_TEST_PROVIDER
3031 * Test dataplane provider plugin
3035 * Test provider process callback
3037 static int test_dplane_process_func(struct zebra_dplane_provider
*prov
)
3039 struct zebra_dplane_ctx
*ctx
;
3042 /* Just moving from 'in' queue to 'out' queue */
3044 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3045 zlog_debug("dplane provider '%s': processing",
3046 dplane_provider_get_name(prov
));
3048 limit
= dplane_provider_get_work_limit(prov
);
3050 for (counter
= 0; counter
< limit
; counter
++) {
3052 ctx
= dplane_provider_dequeue_in_ctx(prov
);
3056 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3057 zlog_debug("dplane provider '%s': op %s",
3058 dplane_provider_get_name(prov
),
3059 dplane_op2str(dplane_ctx_get_op(ctx
)));
3061 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
3063 dplane_provider_enqueue_out_ctx(prov
, ctx
);
3066 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3067 zlog_debug("dplane provider '%s': processed %d",
3068 dplane_provider_get_name(prov
), counter
);
3070 /* Ensure that we'll run the work loop again if there's still
3073 if (counter
>= limit
)
3074 dplane_provider_work_ready();
3080 * Test provider shutdown/fini callback
3082 static int test_dplane_shutdown_func(struct zebra_dplane_provider
*prov
,
3085 if (IS_ZEBRA_DEBUG_DPLANE
)
3086 zlog_debug("dplane provider '%s': %sshutdown",
3087 dplane_provider_get_name(prov
),
3088 early
? "early " : "");
3092 #endif /* DPLANE_TEST_PROVIDER */
3095 * Register default kernel provider
3097 static void dplane_provider_init(void)
3101 ret
= dplane_provider_register("Kernel",
3103 DPLANE_PROV_FLAGS_DEFAULT
, NULL
,
3104 kernel_dplane_process_func
,
3109 zlog_err("Unable to register kernel dplane provider: %d",
3112 #if DPLANE_TEST_PROVIDER
3113 /* Optional test provider ... */
3114 ret
= dplane_provider_register("Test",
3115 DPLANE_PRIO_PRE_KERNEL
,
3116 DPLANE_PROV_FLAGS_DEFAULT
, NULL
,
3117 test_dplane_process_func
,
3118 test_dplane_shutdown_func
,
3119 NULL
/* data */, NULL
);
3122 zlog_err("Unable to register test dplane provider: %d",
3124 #endif /* DPLANE_TEST_PROVIDER */
3127 /* Indicates zebra shutdown/exit is in progress. Some operations may be
3128 * simplified or skipped during shutdown processing.
3130 bool dplane_is_in_shutdown(void)
3132 return zdplane_info
.dg_is_shutdown
;
3136 * Early or pre-shutdown, de-init notification api. This runs pretty
3137 * early during zebra shutdown, as a signal to stop new work and prepare
3138 * for updates generated by shutdown/cleanup activity, as zebra tries to
3139 * remove everything it's responsible for.
3140 * NB: This runs in the main zebra pthread context.
3142 void zebra_dplane_pre_finish(void)
3144 if (IS_ZEBRA_DEBUG_DPLANE
)
3145 zlog_debug("Zebra dataplane pre-fini called");
3147 zdplane_info
.dg_is_shutdown
= true;
3149 /* TODO -- Notify provider(s) of pending shutdown */
3153 * Utility to determine whether work remains enqueued within the dplane;
3154 * used during system shutdown processing.
3156 static bool dplane_work_pending(void)
3159 struct zebra_dplane_ctx
*ctx
;
3160 struct zebra_dplane_provider
*prov
;
3162 /* TODO -- just checking incoming/pending work for now, must check
3167 ctx
= TAILQ_FIRST(&zdplane_info
.dg_update_ctx_q
);
3168 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
3179 dplane_provider_lock(prov
);
3181 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
3183 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
3185 dplane_provider_unlock(prov
);
3191 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
3203 * Shutdown-time intermediate callback, used to determine when all pending
3204 * in-flight updates are done. If there's still work to do, reschedules itself.
3205 * If all work is done, schedules an event to the main zebra thread for
3206 * final zebra shutdown.
3207 * This runs in the dplane pthread context.
3209 static int dplane_check_shutdown_status(struct thread
*event
)
3211 if (IS_ZEBRA_DEBUG_DPLANE
)
3212 zlog_debug("Zebra dataplane shutdown status check called");
3214 if (dplane_work_pending()) {
3215 /* Reschedule dplane check on a short timer */
3216 thread_add_timer_msec(zdplane_info
.dg_master
,
3217 dplane_check_shutdown_status
,
3219 &zdplane_info
.dg_t_shutdown_check
);
3221 /* TODO - give up and stop waiting after a short time? */
3224 /* We appear to be done - schedule a final callback event
3225 * for the zebra main pthread.
3227 thread_add_event(zrouter
.master
, zebra_finalize
, NULL
, 0, NULL
);
3234 * Shutdown, de-init api. This runs pretty late during shutdown,
3235 * after zebra has tried to free/remove/uninstall all routes during shutdown.
3236 * At this point, dplane work may still remain to be done, so we can't just
3237 * blindly terminate. If there's still work to do, we'll periodically check
3238 * and when done, we'll enqueue a task to the zebra main thread for final
3239 * termination processing.
3241 * NB: This runs in the main zebra thread context.
3243 void zebra_dplane_finish(void)
3245 if (IS_ZEBRA_DEBUG_DPLANE
)
3246 zlog_debug("Zebra dataplane fini called");
3248 thread_add_event(zdplane_info
.dg_master
,
3249 dplane_check_shutdown_status
, NULL
, 0,
3250 &zdplane_info
.dg_t_shutdown_check
);
3254 * Main dataplane pthread event loop. The thread takes new incoming work
3255 * and offers it to the first provider. It then iterates through the
3256 * providers, taking complete work from each one and offering it
3257 * to the next in order. At each step, a limited number of updates are
3258 * processed during a cycle in order to provide some fairness.
3260 * This loop through the providers is only run once, so that the dataplane
3261 * pthread can look for other pending work - such as i/o work on behalf of
3264 static int dplane_thread_loop(struct thread
*event
)
3266 struct dplane_ctx_q work_list
;
3267 struct dplane_ctx_q error_list
;
3268 struct zebra_dplane_provider
*prov
;
3269 struct zebra_dplane_ctx
*ctx
, *tctx
;
3270 int limit
, counter
, error_counter
;
3271 uint64_t curr
, high
;
3273 /* Capture work limit per cycle */
3274 limit
= zdplane_info
.dg_updates_per_cycle
;
3276 /* Init temporary lists used to move contexts among providers */
3277 TAILQ_INIT(&work_list
);
3278 TAILQ_INIT(&error_list
);
3281 /* Check for zebra shutdown */
3282 if (!zdplane_info
.dg_run
)
3285 /* Dequeue some incoming work from zebra (if any) onto the temporary
3290 /* Locate initial registered provider */
3291 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
3293 /* Move new work from incoming list to temp list */
3294 for (counter
= 0; counter
< limit
; counter
++) {
3295 ctx
= TAILQ_FIRST(&zdplane_info
.dg_update_ctx_q
);
3297 TAILQ_REMOVE(&zdplane_info
.dg_update_ctx_q
, ctx
,
3300 ctx
->zd_provider
= prov
->dp_id
;
3302 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
3310 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, counter
,
3311 memory_order_relaxed
);
3313 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3314 zlog_debug("dplane: incoming new work counter: %d", counter
);
3316 /* Iterate through the registered providers, offering new incoming
3317 * work. If the provider has outgoing work in its queue, take that
3318 * work for the next provider
3322 /* At each iteration, the temporary work list has 'counter'
3325 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3326 zlog_debug("dplane enqueues %d new work to provider '%s'",
3327 counter
, dplane_provider_get_name(prov
));
3329 /* Capture current provider id in each context; check for
3332 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, tctx
) {
3333 if (dplane_ctx_get_status(ctx
) ==
3334 ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3335 ctx
->zd_provider
= prov
->dp_id
;
3338 * TODO -- improve error-handling: recirc
3339 * errors backwards so that providers can
3340 * 'undo' their work (if they want to)
3343 /* Move to error list; will be returned
3346 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
3347 TAILQ_INSERT_TAIL(&error_list
,
3353 /* Enqueue new work to the provider */
3354 dplane_provider_lock(prov
);
3356 if (TAILQ_FIRST(&work_list
))
3357 TAILQ_CONCAT(&(prov
->dp_ctx_in_q
), &work_list
,
3360 atomic_fetch_add_explicit(&prov
->dp_in_counter
, counter
,
3361 memory_order_relaxed
);
3362 atomic_fetch_add_explicit(&prov
->dp_in_queued
, counter
,
3363 memory_order_relaxed
);
3364 curr
= atomic_load_explicit(&prov
->dp_in_queued
,
3365 memory_order_relaxed
);
3366 high
= atomic_load_explicit(&prov
->dp_in_max
,
3367 memory_order_relaxed
);
3369 atomic_store_explicit(&prov
->dp_in_max
, curr
,
3370 memory_order_relaxed
);
3372 dplane_provider_unlock(prov
);
3374 /* Reset the temp list (though the 'concat' may have done this
3375 * already), and the counter
3377 TAILQ_INIT(&work_list
);
3380 /* Call into the provider code. Note that this is
3381 * unconditional: we offer to do work even if we don't enqueue
3384 (*prov
->dp_fp
)(prov
);
3386 /* Check for zebra shutdown */
3387 if (!zdplane_info
.dg_run
)
3390 /* Dequeue completed work from the provider */
3391 dplane_provider_lock(prov
);
3393 while (counter
< limit
) {
3394 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
3396 TAILQ_REMOVE(&(prov
->dp_ctx_out_q
), ctx
,
3399 TAILQ_INSERT_TAIL(&work_list
,
3406 dplane_provider_unlock(prov
);
3408 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3409 zlog_debug("dplane dequeues %d completed work from provider %s",
3410 counter
, dplane_provider_get_name(prov
));
3412 /* Locate next provider */
3414 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
3418 /* After all providers have been serviced, enqueue any completed
3419 * work and any errors back to zebra so it can process the results.
3421 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3422 zlog_debug("dplane has %d completed, %d errors, for zebra main",
3423 counter
, error_counter
);
3426 * Hand lists through the api to zebra main,
3427 * to reduce the number of lock/unlock cycles
3430 /* Call through to zebra main */
3431 (zdplane_info
.dg_results_cb
)(&error_list
);
3433 TAILQ_INIT(&error_list
);
3435 /* Call through to zebra main */
3436 (zdplane_info
.dg_results_cb
)(&work_list
);
3438 TAILQ_INIT(&work_list
);
3445 * Final phase of shutdown, after all work enqueued to dplane has been
3446 * processed. This is called from the zebra main pthread context.
3448 void zebra_dplane_shutdown(void)
3450 if (IS_ZEBRA_DEBUG_DPLANE
)
3451 zlog_debug("Zebra dataplane shutdown called");
3453 /* Stop dplane thread, if it's running */
3455 zdplane_info
.dg_run
= false;
3457 THREAD_OFF(zdplane_info
.dg_t_update
);
3459 frr_pthread_stop(zdplane_info
.dg_pthread
, NULL
);
3461 /* Destroy pthread */
3462 frr_pthread_destroy(zdplane_info
.dg_pthread
);
3463 zdplane_info
.dg_pthread
= NULL
;
3464 zdplane_info
.dg_master
= NULL
;
3466 /* TODO -- Notify provider(s) of final shutdown */
3468 /* TODO -- Clean-up provider objects */
3470 /* TODO -- Clean queue(s), free memory */
3474 * Initialize the dataplane module during startup, internal/private version
3476 static void zebra_dplane_init_internal(void)
3478 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
3480 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
3482 TAILQ_INIT(&zdplane_info
.dg_update_ctx_q
);
3483 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
3485 zdplane_info
.dg_updates_per_cycle
= DPLANE_DEFAULT_NEW_WORK
;
3487 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
3489 /* Register default kernel 'provider' during init */
3490 dplane_provider_init();
3494 * Start the dataplane pthread. This step needs to be run later than the
3495 * 'init' step, in case zebra has fork-ed.
3497 void zebra_dplane_start(void)
3499 struct zebra_dplane_provider
*prov
;
3500 struct frr_pthread_attr pattr
= {
3501 .start
= frr_pthread_attr_default
.start
,
3502 .stop
= frr_pthread_attr_default
.stop
3505 /* Start dataplane pthread */
3507 zdplane_info
.dg_pthread
= frr_pthread_new(&pattr
, "Zebra dplane thread",
3510 zdplane_info
.dg_master
= zdplane_info
.dg_pthread
->master
;
3512 zdplane_info
.dg_run
= true;
3514 /* Enqueue an initial event for the dataplane pthread */
3515 thread_add_event(zdplane_info
.dg_master
, dplane_thread_loop
, NULL
, 0,
3516 &zdplane_info
.dg_t_update
);
3518 /* Call start callbacks for registered providers */
3521 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
3527 (prov
->dp_start
)(prov
);
3529 /* Locate next provider */
3531 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
3535 frr_pthread_run(zdplane_info
.dg_pthread
, NULL
);
3539 * Initialize the dataplane module at startup; called by zebra rib_init()
3541 void zebra_dplane_init(int (*results_fp
)(struct dplane_ctx_q
*))
3543 zebra_dplane_init_internal();
3544 zdplane_info
.dg_results_cb
= results_fp
;