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
{
155 ifindex_t br_ifindex
;
157 struct in_addr vtep_ip
;
163 * EVPN neighbor info for the dataplane
165 struct dplane_neigh_info
{
166 struct ipaddr ip_addr
;
173 * The context block used to exchange info about route updates across
174 * the boundary between the zebra main context (and pthread) and the
175 * dataplane layer (and pthread).
177 struct zebra_dplane_ctx
{
180 enum dplane_op_e zd_op
;
182 /* Status on return */
183 enum zebra_dplane_result zd_status
;
185 /* Dplane provider id */
186 uint32_t zd_provider
;
188 /* Flags - used by providers, e.g. */
196 /* Some updates may be generated by notifications: allow the
197 * plugin to notice and ignore results from its own notifications.
199 uint32_t zd_notif_provider
;
201 /* TODO -- internal/sub-operation status? */
202 enum zebra_dplane_result zd_remote_status
;
203 enum zebra_dplane_result zd_kernel_status
;
206 uint32_t zd_table_id
;
208 char zd_ifname
[INTERFACE_NAMSIZ
];
209 ifindex_t zd_ifindex
;
211 /* Support info for different kinds of updates */
213 struct dplane_route_info rinfo
;
215 struct dplane_pw_info pw
;
216 struct dplane_intf_info intf
;
217 struct dplane_mac_info macinfo
;
218 struct dplane_neigh_info neigh
;
221 /* Namespace info, used especially for netlink kernel communication */
222 struct zebra_dplane_info zd_ns_info
;
224 /* Embedded list linkage */
225 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
228 /* Flag that can be set by a pre-kernel provider as a signal that an update
229 * should bypass the kernel.
231 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
235 * Registration block for one dataplane provider.
237 struct zebra_dplane_provider
{
239 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
241 /* Priority, for ordering among providers */
248 pthread_mutex_t dp_mutex
;
250 /* Plugin-provided extra data */
256 int (*dp_start
)(struct zebra_dplane_provider
*prov
);
258 int (*dp_fp
)(struct zebra_dplane_provider
*prov
);
260 int (*dp_fini
)(struct zebra_dplane_provider
*prov
, bool early_p
);
262 _Atomic
uint32_t dp_in_counter
;
263 _Atomic
uint32_t dp_in_queued
;
264 _Atomic
uint32_t dp_in_max
;
265 _Atomic
uint32_t dp_out_counter
;
266 _Atomic
uint32_t dp_out_queued
;
267 _Atomic
uint32_t dp_out_max
;
268 _Atomic
uint32_t dp_error_counter
;
270 /* Queue of contexts inbound to the provider */
271 struct dplane_ctx_q dp_ctx_in_q
;
273 /* Queue of completed contexts outbound from the provider back
274 * towards the dataplane module.
276 struct dplane_ctx_q dp_ctx_out_q
;
278 /* Embedded list linkage for provider objects */
279 TAILQ_ENTRY(zebra_dplane_provider
) dp_prov_link
;
285 static struct zebra_dplane_globals
{
286 /* Mutex to control access to dataplane components */
287 pthread_mutex_t dg_mutex
;
289 /* Results callback registered by zebra 'core' */
290 int (*dg_results_cb
)(struct dplane_ctx_q
*ctxlist
);
292 /* Sentinel for beginning of shutdown */
293 volatile bool dg_is_shutdown
;
295 /* Sentinel for end of shutdown */
296 volatile bool dg_run
;
298 /* Update context queue inbound to the dataplane */
299 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_update_ctx_q
;
301 /* Ordered list of providers */
302 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
304 /* Counter used to assign internal ids to providers */
305 uint32_t dg_provider_id
;
307 /* Limit number of pending, unprocessed updates */
308 _Atomic
uint32_t dg_max_queued_updates
;
310 /* Control whether system route notifications should be produced. */
311 bool dg_sys_route_notifs
;
313 /* Limit number of new updates dequeued at once, to pace an
316 uint32_t dg_updates_per_cycle
;
318 _Atomic
uint32_t dg_routes_in
;
319 _Atomic
uint32_t dg_routes_queued
;
320 _Atomic
uint32_t dg_routes_queued_max
;
321 _Atomic
uint32_t dg_route_errors
;
322 _Atomic
uint32_t dg_other_errors
;
324 _Atomic
uint32_t dg_lsps_in
;
325 _Atomic
uint32_t dg_lsp_errors
;
327 _Atomic
uint32_t dg_pws_in
;
328 _Atomic
uint32_t dg_pw_errors
;
330 _Atomic
uint32_t dg_intf_addrs_in
;
331 _Atomic
uint32_t dg_intf_addr_errors
;
333 _Atomic
uint32_t dg_macs_in
;
334 _Atomic
uint32_t dg_mac_errors
;
336 _Atomic
uint32_t dg_neighs_in
;
337 _Atomic
uint32_t dg_neigh_errors
;
339 _Atomic
uint32_t dg_update_yields
;
341 /* Dataplane pthread */
342 struct frr_pthread
*dg_pthread
;
344 /* Event-delivery context 'master' for the dplane */
345 struct thread_master
*dg_master
;
347 /* Event/'thread' pointer for queued updates */
348 struct thread
*dg_t_update
;
350 /* Event pointer for pending shutdown check loop */
351 struct thread
*dg_t_shutdown_check
;
356 * Lock and unlock for interactions with the zebra 'core' pthread
358 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
359 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
363 * Lock and unlock for individual providers
365 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
366 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
369 static int dplane_thread_loop(struct thread
*event
);
370 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
371 struct zebra_ns
*zns
);
372 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
373 enum dplane_op_e op
);
374 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
375 enum dplane_op_e op
);
376 static enum zebra_dplane_result
intf_addr_update_internal(
377 const struct interface
*ifp
, const struct connected
*ifc
,
378 enum dplane_op_e op
);
379 static enum zebra_dplane_result
mac_update_internal(
380 enum dplane_op_e op
, const struct interface
*ifp
,
381 const struct interface
*br_ifp
,
382 vlanid_t vid
, const struct ethaddr
*mac
,
383 struct in_addr vtep_ip
, bool sticky
);
384 static enum zebra_dplane_result
neigh_update_internal(
386 const struct interface
*ifp
,
387 const struct ethaddr
*mac
,
388 const struct ipaddr
*ip
,
389 uint32_t flags
, uint16_t state
);
395 /* Obtain thread_master for dataplane thread */
396 struct thread_master
*dplane_get_thread_master(void)
398 return zdplane_info
.dg_master
;
402 * Allocate a dataplane update context
404 struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
406 struct zebra_dplane_ctx
*p
;
408 /* TODO -- just alloc'ing memory, but would like to maintain
411 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
416 /* Enable system route notifications */
417 void dplane_enable_sys_route_notifs(void)
419 zdplane_info
.dg_sys_route_notifs
= true;
423 * Free a dataplane results context.
425 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
430 DPLANE_CTX_VALID(*pctx
);
432 /* TODO -- just freeing memory, but would like to maintain
436 /* Some internal allocations may need to be freed, depending on
437 * the type of info captured in the ctx.
439 switch ((*pctx
)->zd_op
) {
440 case DPLANE_OP_ROUTE_INSTALL
:
441 case DPLANE_OP_ROUTE_UPDATE
:
442 case DPLANE_OP_ROUTE_DELETE
:
443 case DPLANE_OP_SYS_ROUTE_ADD
:
444 case DPLANE_OP_SYS_ROUTE_DELETE
:
445 case DPLANE_OP_ROUTE_NOTIFY
:
447 /* Free allocated nexthops */
448 if ((*pctx
)->u
.rinfo
.zd_ng
.nexthop
) {
449 /* This deals with recursive nexthops too */
450 nexthops_free((*pctx
)->u
.rinfo
.zd_ng
.nexthop
);
452 (*pctx
)->u
.rinfo
.zd_ng
.nexthop
= NULL
;
455 if ((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
) {
456 /* This deals with recursive nexthops too */
457 nexthops_free((*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
);
459 (*pctx
)->u
.rinfo
.zd_old_ng
.nexthop
= NULL
;
464 case DPLANE_OP_LSP_INSTALL
:
465 case DPLANE_OP_LSP_UPDATE
:
466 case DPLANE_OP_LSP_DELETE
:
467 case DPLANE_OP_LSP_NOTIFY
:
469 zebra_nhlfe_t
*nhlfe
, *next
;
471 /* Free allocated NHLFEs */
472 for (nhlfe
= (*pctx
)->u
.lsp
.nhlfe_list
; nhlfe
; nhlfe
= next
) {
475 zebra_mpls_nhlfe_del(nhlfe
);
478 /* Clear pointers in lsp struct, in case we're cacheing
479 * free context structs.
481 (*pctx
)->u
.lsp
.nhlfe_list
= NULL
;
482 (*pctx
)->u
.lsp
.best_nhlfe
= NULL
;
487 case DPLANE_OP_PW_INSTALL
:
488 case DPLANE_OP_PW_UNINSTALL
:
489 /* Free allocated nexthops */
490 if ((*pctx
)->u
.pw
.nhg
.nexthop
) {
491 /* This deals with recursive nexthops too */
492 nexthops_free((*pctx
)->u
.pw
.nhg
.nexthop
);
494 (*pctx
)->u
.pw
.nhg
.nexthop
= NULL
;
498 case DPLANE_OP_ADDR_INSTALL
:
499 case DPLANE_OP_ADDR_UNINSTALL
:
500 /* Maybe free label string, if allocated */
501 if ((*pctx
)->u
.intf
.label
!= NULL
&&
502 (*pctx
)->u
.intf
.label
!= (*pctx
)->u
.intf
.label_buf
) {
503 free((*pctx
)->u
.intf
.label
);
504 (*pctx
)->u
.intf
.label
= NULL
;
508 case DPLANE_OP_MAC_INSTALL
:
509 case DPLANE_OP_MAC_DELETE
:
510 case DPLANE_OP_NEIGH_INSTALL
:
511 case DPLANE_OP_NEIGH_UPDATE
:
512 case DPLANE_OP_NEIGH_DELETE
:
513 case DPLANE_OP_VTEP_ADD
:
514 case DPLANE_OP_VTEP_DELETE
:
519 XFREE(MTYPE_DP_CTX
, *pctx
);
524 * Return a context block to the dplane module after processing
526 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
528 /* TODO -- maintain pool; for now, just free */
529 dplane_ctx_free(pctx
);
532 /* Enqueue a context block */
533 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
534 const struct zebra_dplane_ctx
*ctx
)
536 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
539 /* Append a list of context blocks to another list */
540 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
541 struct dplane_ctx_q
*from_list
)
543 if (TAILQ_FIRST(from_list
)) {
544 TAILQ_CONCAT(to_list
, from_list
, zd_q_entries
);
546 /* And clear 'from' list */
547 TAILQ_INIT(from_list
);
551 /* Dequeue a context block from the head of a list */
552 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
)
554 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
557 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
563 * Accessors for information from the context object
565 enum zebra_dplane_result
dplane_ctx_get_status(
566 const struct zebra_dplane_ctx
*ctx
)
568 DPLANE_CTX_VALID(ctx
);
570 return ctx
->zd_status
;
573 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
574 enum zebra_dplane_result status
)
576 DPLANE_CTX_VALID(ctx
);
578 ctx
->zd_status
= status
;
581 /* Retrieve last/current provider id */
582 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
)
584 DPLANE_CTX_VALID(ctx
);
585 return ctx
->zd_provider
;
588 /* Providers run before the kernel can control whether a kernel
589 * update should be done.
591 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
)
593 DPLANE_CTX_VALID(ctx
);
595 SET_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
598 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
)
600 DPLANE_CTX_VALID(ctx
);
602 return CHECK_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
605 void dplane_ctx_set_op(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
)
607 DPLANE_CTX_VALID(ctx
);
611 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
613 DPLANE_CTX_VALID(ctx
);
618 const char *dplane_op2str(enum dplane_op_e op
)
620 const char *ret
= "UNKNOWN";
628 case DPLANE_OP_ROUTE_INSTALL
:
629 ret
= "ROUTE_INSTALL";
631 case DPLANE_OP_ROUTE_UPDATE
:
632 ret
= "ROUTE_UPDATE";
634 case DPLANE_OP_ROUTE_DELETE
:
635 ret
= "ROUTE_DELETE";
637 case DPLANE_OP_ROUTE_NOTIFY
:
638 ret
= "ROUTE_NOTIFY";
641 case DPLANE_OP_LSP_INSTALL
:
644 case DPLANE_OP_LSP_UPDATE
:
647 case DPLANE_OP_LSP_DELETE
:
650 case DPLANE_OP_LSP_NOTIFY
:
654 case DPLANE_OP_PW_INSTALL
:
657 case DPLANE_OP_PW_UNINSTALL
:
658 ret
= "PW_UNINSTALL";
661 case DPLANE_OP_SYS_ROUTE_ADD
:
662 ret
= "SYS_ROUTE_ADD";
664 case DPLANE_OP_SYS_ROUTE_DELETE
:
665 ret
= "SYS_ROUTE_DEL";
668 case DPLANE_OP_ADDR_INSTALL
:
669 ret
= "ADDR_INSTALL";
671 case DPLANE_OP_ADDR_UNINSTALL
:
672 ret
= "ADDR_UNINSTALL";
675 case DPLANE_OP_MAC_INSTALL
:
678 case DPLANE_OP_MAC_DELETE
:
682 case DPLANE_OP_NEIGH_INSTALL
:
683 ret
= "NEIGH_INSTALL";
685 case DPLANE_OP_NEIGH_UPDATE
:
686 ret
= "NEIGH_UPDATE";
688 case DPLANE_OP_NEIGH_DELETE
:
689 ret
= "NEIGH_DELETE";
691 case DPLANE_OP_VTEP_ADD
:
694 case DPLANE_OP_VTEP_DELETE
:
702 const char *dplane_res2str(enum zebra_dplane_result res
)
704 const char *ret
= "<Unknown>";
707 case ZEBRA_DPLANE_REQUEST_FAILURE
:
710 case ZEBRA_DPLANE_REQUEST_QUEUED
:
713 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
721 void dplane_ctx_set_dest(struct zebra_dplane_ctx
*ctx
,
722 const struct prefix
*dest
)
724 DPLANE_CTX_VALID(ctx
);
726 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), dest
);
729 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
731 DPLANE_CTX_VALID(ctx
);
733 return &(ctx
->u
.rinfo
.zd_dest
);
736 void dplane_ctx_set_src(struct zebra_dplane_ctx
*ctx
, const struct prefix
*src
)
738 DPLANE_CTX_VALID(ctx
);
741 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src
);
743 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(struct prefix
));
746 /* Source prefix is a little special - return NULL for "no src prefix" */
747 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
749 DPLANE_CTX_VALID(ctx
);
751 if (ctx
->u
.rinfo
.zd_src
.prefixlen
== 0 &&
752 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->u
.rinfo
.zd_src
.u
.prefix6
))) {
755 return &(ctx
->u
.rinfo
.zd_src
);
759 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
761 DPLANE_CTX_VALID(ctx
);
763 return ctx
->zd_is_update
;
766 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
768 DPLANE_CTX_VALID(ctx
);
773 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
775 DPLANE_CTX_VALID(ctx
);
777 return ctx
->zd_old_seq
;
780 void dplane_ctx_set_vrf(struct zebra_dplane_ctx
*ctx
, vrf_id_t vrf
)
782 DPLANE_CTX_VALID(ctx
);
784 ctx
->zd_vrf_id
= vrf
;
787 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
789 DPLANE_CTX_VALID(ctx
);
791 return ctx
->zd_vrf_id
;
794 bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx
*ctx
)
796 DPLANE_CTX_VALID(ctx
);
798 return (ctx
->zd_notif_provider
!= 0);
801 uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx
*ctx
)
803 DPLANE_CTX_VALID(ctx
);
805 return ctx
->zd_notif_provider
;
808 void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx
*ctx
,
811 DPLANE_CTX_VALID(ctx
);
813 ctx
->zd_notif_provider
= id
;
815 const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx
*ctx
)
817 DPLANE_CTX_VALID(ctx
);
819 return ctx
->zd_ifname
;
822 ifindex_t
dplane_ctx_get_ifindex(const struct zebra_dplane_ctx
*ctx
)
824 DPLANE_CTX_VALID(ctx
);
826 return ctx
->zd_ifindex
;
829 void dplane_ctx_set_type(struct zebra_dplane_ctx
*ctx
, int type
)
831 DPLANE_CTX_VALID(ctx
);
833 ctx
->u
.rinfo
.zd_type
= type
;
836 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
838 DPLANE_CTX_VALID(ctx
);
840 return ctx
->u
.rinfo
.zd_type
;
843 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
845 DPLANE_CTX_VALID(ctx
);
847 return ctx
->u
.rinfo
.zd_old_type
;
850 void dplane_ctx_set_afi(struct zebra_dplane_ctx
*ctx
, afi_t afi
)
852 DPLANE_CTX_VALID(ctx
);
854 ctx
->u
.rinfo
.zd_afi
= afi
;
857 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
859 DPLANE_CTX_VALID(ctx
);
861 return ctx
->u
.rinfo
.zd_afi
;
864 void dplane_ctx_set_safi(struct zebra_dplane_ctx
*ctx
, safi_t safi
)
866 DPLANE_CTX_VALID(ctx
);
868 ctx
->u
.rinfo
.zd_safi
= safi
;
871 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
873 DPLANE_CTX_VALID(ctx
);
875 return ctx
->u
.rinfo
.zd_safi
;
878 void dplane_ctx_set_table(struct zebra_dplane_ctx
*ctx
, uint32_t table
)
880 DPLANE_CTX_VALID(ctx
);
882 ctx
->zd_table_id
= table
;
885 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
887 DPLANE_CTX_VALID(ctx
);
889 return ctx
->zd_table_id
;
892 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
894 DPLANE_CTX_VALID(ctx
);
896 return ctx
->u
.rinfo
.zd_tag
;
899 void dplane_ctx_set_tag(struct zebra_dplane_ctx
*ctx
, route_tag_t tag
)
901 DPLANE_CTX_VALID(ctx
);
903 ctx
->u
.rinfo
.zd_tag
= tag
;
906 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
908 DPLANE_CTX_VALID(ctx
);
910 return ctx
->u
.rinfo
.zd_old_tag
;
913 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
915 DPLANE_CTX_VALID(ctx
);
917 return ctx
->u
.rinfo
.zd_instance
;
920 void dplane_ctx_set_instance(struct zebra_dplane_ctx
*ctx
, uint16_t instance
)
922 DPLANE_CTX_VALID(ctx
);
924 ctx
->u
.rinfo
.zd_instance
= instance
;
927 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
929 DPLANE_CTX_VALID(ctx
);
931 return ctx
->u
.rinfo
.zd_old_instance
;
934 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
936 DPLANE_CTX_VALID(ctx
);
938 return ctx
->u
.rinfo
.zd_metric
;
941 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
943 DPLANE_CTX_VALID(ctx
);
945 return ctx
->u
.rinfo
.zd_old_metric
;
948 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
950 DPLANE_CTX_VALID(ctx
);
952 return ctx
->u
.rinfo
.zd_mtu
;
955 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
957 DPLANE_CTX_VALID(ctx
);
959 return ctx
->u
.rinfo
.zd_nexthop_mtu
;
962 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
964 DPLANE_CTX_VALID(ctx
);
966 return ctx
->u
.rinfo
.zd_distance
;
969 void dplane_ctx_set_distance(struct zebra_dplane_ctx
*ctx
, uint8_t distance
)
971 DPLANE_CTX_VALID(ctx
);
973 ctx
->u
.rinfo
.zd_distance
= distance
;
976 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
978 DPLANE_CTX_VALID(ctx
);
980 return ctx
->u
.rinfo
.zd_old_distance
;
983 void dplane_ctx_set_nexthops(struct zebra_dplane_ctx
*ctx
, struct nexthop
*nh
)
985 DPLANE_CTX_VALID(ctx
);
987 if (ctx
->u
.rinfo
.zd_ng
.nexthop
) {
988 nexthops_free(ctx
->u
.rinfo
.zd_ng
.nexthop
);
989 ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
991 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), nh
, NULL
);
994 const struct nexthop_group
*dplane_ctx_get_ng(
995 const struct zebra_dplane_ctx
*ctx
)
997 DPLANE_CTX_VALID(ctx
);
999 return &(ctx
->u
.rinfo
.zd_ng
);
1002 const struct nexthop_group
*dplane_ctx_get_old_ng(
1003 const struct zebra_dplane_ctx
*ctx
)
1005 DPLANE_CTX_VALID(ctx
);
1007 return &(ctx
->u
.rinfo
.zd_old_ng
);
1010 const struct zebra_dplane_info
*dplane_ctx_get_ns(
1011 const struct zebra_dplane_ctx
*ctx
)
1013 DPLANE_CTX_VALID(ctx
);
1015 return &(ctx
->zd_ns_info
);
1018 /* Accessors for LSP information */
1020 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
)
1022 DPLANE_CTX_VALID(ctx
);
1024 return ctx
->u
.lsp
.ile
.in_label
;
1027 void dplane_ctx_set_in_label(struct zebra_dplane_ctx
*ctx
, mpls_label_t label
)
1029 DPLANE_CTX_VALID(ctx
);
1031 ctx
->u
.lsp
.ile
.in_label
= label
;
1034 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
)
1036 DPLANE_CTX_VALID(ctx
);
1038 return ctx
->u
.lsp
.addr_family
;
1041 void dplane_ctx_set_addr_family(struct zebra_dplane_ctx
*ctx
,
1044 DPLANE_CTX_VALID(ctx
);
1046 ctx
->u
.lsp
.addr_family
= family
;
1049 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
)
1051 DPLANE_CTX_VALID(ctx
);
1053 return ctx
->u
.lsp
.flags
;
1056 void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx
*ctx
,
1059 DPLANE_CTX_VALID(ctx
);
1061 ctx
->u
.lsp
.flags
= flags
;
1064 const zebra_nhlfe_t
*dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx
*ctx
)
1066 DPLANE_CTX_VALID(ctx
);
1068 return ctx
->u
.lsp
.nhlfe_list
;
1071 zebra_nhlfe_t
*dplane_ctx_add_nhlfe(struct zebra_dplane_ctx
*ctx
,
1072 enum lsp_types_t lsp_type
,
1073 enum nexthop_types_t nh_type
,
1076 mpls_label_t out_label
)
1078 zebra_nhlfe_t
*nhlfe
;
1080 DPLANE_CTX_VALID(ctx
);
1082 nhlfe
= zebra_mpls_lsp_add_nhlfe(&(ctx
->u
.lsp
),
1083 lsp_type
, nh_type
, gate
,
1084 ifindex
, out_label
);
1089 const zebra_nhlfe_t
*
1090 dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx
*ctx
)
1092 DPLANE_CTX_VALID(ctx
);
1094 return ctx
->u
.lsp
.best_nhlfe
;
1097 const zebra_nhlfe_t
*
1098 dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx
*ctx
,
1099 zebra_nhlfe_t
*nhlfe
)
1101 DPLANE_CTX_VALID(ctx
);
1103 ctx
->u
.lsp
.best_nhlfe
= nhlfe
;
1104 return ctx
->u
.lsp
.best_nhlfe
;
1107 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
)
1109 DPLANE_CTX_VALID(ctx
);
1111 return ctx
->u
.lsp
.num_ecmp
;
1114 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
)
1116 DPLANE_CTX_VALID(ctx
);
1118 return ctx
->u
.pw
.local_label
;
1121 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
)
1123 DPLANE_CTX_VALID(ctx
);
1125 return ctx
->u
.pw
.remote_label
;
1128 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
)
1130 DPLANE_CTX_VALID(ctx
);
1132 return ctx
->u
.pw
.type
;
1135 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
)
1137 DPLANE_CTX_VALID(ctx
);
1139 return ctx
->u
.pw
.af
;
1142 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
)
1144 DPLANE_CTX_VALID(ctx
);
1146 return ctx
->u
.pw
.flags
;
1149 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
)
1151 DPLANE_CTX_VALID(ctx
);
1153 return ctx
->u
.pw
.status
;
1156 const union g_addr
*dplane_ctx_get_pw_dest(
1157 const struct zebra_dplane_ctx
*ctx
)
1159 DPLANE_CTX_VALID(ctx
);
1161 return &(ctx
->u
.pw
.dest
);
1164 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
1165 const struct zebra_dplane_ctx
*ctx
)
1167 DPLANE_CTX_VALID(ctx
);
1169 return &(ctx
->u
.pw
.fields
);
1172 const struct nexthop_group
*
1173 dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx
*ctx
)
1175 DPLANE_CTX_VALID(ctx
);
1177 return &(ctx
->u
.pw
.nhg
);
1180 /* Accessors for interface information */
1181 uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx
*ctx
)
1183 DPLANE_CTX_VALID(ctx
);
1185 return ctx
->u
.intf
.metric
;
1188 /* Is interface addr p2p? */
1189 bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx
*ctx
)
1191 DPLANE_CTX_VALID(ctx
);
1193 return (ctx
->u
.intf
.flags
& DPLANE_INTF_CONNECTED
);
1196 bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx
*ctx
)
1198 DPLANE_CTX_VALID(ctx
);
1200 return (ctx
->u
.intf
.flags
& DPLANE_INTF_SECONDARY
);
1203 bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx
*ctx
)
1205 DPLANE_CTX_VALID(ctx
);
1207 return (ctx
->u
.intf
.flags
& DPLANE_INTF_BROADCAST
);
1210 const struct prefix
*dplane_ctx_get_intf_addr(
1211 const struct zebra_dplane_ctx
*ctx
)
1213 DPLANE_CTX_VALID(ctx
);
1215 return &(ctx
->u
.intf
.prefix
);
1218 bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx
*ctx
)
1220 DPLANE_CTX_VALID(ctx
);
1222 return (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_DEST
);
1225 const struct prefix
*dplane_ctx_get_intf_dest(
1226 const struct zebra_dplane_ctx
*ctx
)
1228 DPLANE_CTX_VALID(ctx
);
1230 if (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_DEST
)
1231 return &(ctx
->u
.intf
.dest_prefix
);
1236 bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx
*ctx
)
1238 DPLANE_CTX_VALID(ctx
);
1240 return (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_LABEL
);
1243 const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx
*ctx
)
1245 DPLANE_CTX_VALID(ctx
);
1247 return ctx
->u
.intf
.label
;
1250 /* Accessors for MAC information */
1251 vlanid_t
dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx
*ctx
)
1253 DPLANE_CTX_VALID(ctx
);
1254 return ctx
->u
.macinfo
.vid
;
1257 bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx
*ctx
)
1259 DPLANE_CTX_VALID(ctx
);
1260 return ctx
->u
.macinfo
.is_sticky
;
1263 const struct ethaddr
*dplane_ctx_mac_get_addr(
1264 const struct zebra_dplane_ctx
*ctx
)
1266 DPLANE_CTX_VALID(ctx
);
1267 return &(ctx
->u
.macinfo
.mac
);
1270 const struct in_addr
*dplane_ctx_mac_get_vtep_ip(
1271 const struct zebra_dplane_ctx
*ctx
)
1273 DPLANE_CTX_VALID(ctx
);
1274 return &(ctx
->u
.macinfo
.vtep_ip
);
1277 ifindex_t
dplane_ctx_mac_get_br_ifindex(const struct zebra_dplane_ctx
*ctx
)
1279 DPLANE_CTX_VALID(ctx
);
1280 return ctx
->u
.macinfo
.br_ifindex
;
1283 /* Accessors for neighbor information */
1284 const struct ipaddr
*dplane_ctx_neigh_get_ipaddr(
1285 const struct zebra_dplane_ctx
*ctx
)
1287 DPLANE_CTX_VALID(ctx
);
1288 return &(ctx
->u
.neigh
.ip_addr
);
1291 const struct ethaddr
*dplane_ctx_neigh_get_mac(
1292 const struct zebra_dplane_ctx
*ctx
)
1294 DPLANE_CTX_VALID(ctx
);
1295 return &(ctx
->u
.neigh
.mac
);
1298 uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx
*ctx
)
1300 DPLANE_CTX_VALID(ctx
);
1301 return ctx
->u
.neigh
.flags
;
1304 uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx
*ctx
)
1306 DPLANE_CTX_VALID(ctx
);
1307 return ctx
->u
.neigh
.state
;
1311 * End of dplane context accessors
1316 * Retrieve the limit on the number of pending, unprocessed updates.
1318 uint32_t dplane_get_in_queue_limit(void)
1320 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
1321 memory_order_relaxed
);
1325 * Configure limit on the number of pending, queued updates.
1327 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
1329 /* Reset to default on 'unset' */
1331 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
1333 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
1334 memory_order_relaxed
);
1338 * Retrieve the current queue depth of incoming, unprocessed updates
1340 uint32_t dplane_get_in_queue_len(void)
1342 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
1343 memory_order_seq_cst
);
1347 * Common dataplane context init with zebra namespace info.
1349 static int dplane_ctx_ns_init(struct zebra_dplane_ctx
*ctx
,
1350 struct zebra_ns
*zns
,
1353 dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
);
1355 #if defined(HAVE_NETLINK)
1356 /* Increment message counter after copying to context struct - may need
1357 * two messages in some 'update' cases.
1360 zns
->netlink_dplane
.seq
+= 2;
1362 zns
->netlink_dplane
.seq
++;
1363 #endif /* HAVE_NETLINK */
1369 * Initialize a context block for a route update from zebra data structs.
1371 static int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
,
1372 enum dplane_op_e op
,
1373 struct route_node
*rn
,
1374 struct route_entry
*re
)
1377 const struct route_table
*table
= NULL
;
1378 const rib_table_info_t
*info
;
1379 const struct prefix
*p
, *src_p
;
1380 struct zebra_ns
*zns
;
1381 struct zebra_vrf
*zvrf
;
1382 struct nexthop
*nexthop
;
1384 if (!ctx
|| !rn
|| !re
)
1388 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1390 ctx
->u
.rinfo
.zd_type
= re
->type
;
1391 ctx
->u
.rinfo
.zd_old_type
= re
->type
;
1393 /* Prefixes: dest, and optional source */
1394 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1396 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), p
);
1399 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src_p
);
1401 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(ctx
->u
.rinfo
.zd_src
));
1403 ctx
->zd_table_id
= re
->table
;
1405 ctx
->u
.rinfo
.zd_metric
= re
->metric
;
1406 ctx
->u
.rinfo
.zd_old_metric
= re
->metric
;
1407 ctx
->zd_vrf_id
= re
->vrf_id
;
1408 ctx
->u
.rinfo
.zd_mtu
= re
->mtu
;
1409 ctx
->u
.rinfo
.zd_nexthop_mtu
= re
->nexthop_mtu
;
1410 ctx
->u
.rinfo
.zd_instance
= re
->instance
;
1411 ctx
->u
.rinfo
.zd_tag
= re
->tag
;
1412 ctx
->u
.rinfo
.zd_old_tag
= re
->tag
;
1413 ctx
->u
.rinfo
.zd_distance
= re
->distance
;
1415 table
= srcdest_rnode_table(rn
);
1418 ctx
->u
.rinfo
.zd_afi
= info
->afi
;
1419 ctx
->u
.rinfo
.zd_safi
= info
->safi
;
1421 /* Copy nexthops; recursive info is included too */
1422 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
1424 /* Ensure that the dplane's nexthops flags are clear. */
1425 for (ALL_NEXTHOPS(ctx
->u
.rinfo
.zd_ng
, nexthop
))
1426 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1428 /* Don't need some info when capturing a system notification */
1429 if (op
== DPLANE_OP_SYS_ROUTE_ADD
||
1430 op
== DPLANE_OP_SYS_ROUTE_DELETE
) {
1435 /* Extract ns info - can't use pointers to 'core' structs */
1436 zvrf
= vrf_info_lookup(re
->vrf_id
);
1438 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_ROUTE_UPDATE
));
1440 /* Trying out the sequence number idea, so we can try to detect
1441 * when a result is stale.
1443 re
->dplane_sequence
= zebra_router_get_next_sequence();
1444 ctx
->zd_seq
= re
->dplane_sequence
;
1453 * Capture information for an LSP update in a dplane context.
1455 static int dplane_ctx_lsp_init(struct zebra_dplane_ctx
*ctx
,
1456 enum dplane_op_e op
,
1460 zebra_nhlfe_t
*nhlfe
, *new_nhlfe
;
1462 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1463 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1464 dplane_op2str(op
), lsp
->ile
.in_label
,
1468 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1470 /* Capture namespace info */
1471 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
1472 (op
== DPLANE_OP_LSP_UPDATE
));
1474 memset(&ctx
->u
.lsp
, 0, sizeof(ctx
->u
.lsp
));
1476 ctx
->u
.lsp
.ile
= lsp
->ile
;
1477 ctx
->u
.lsp
.addr_family
= lsp
->addr_family
;
1478 ctx
->u
.lsp
.num_ecmp
= lsp
->num_ecmp
;
1479 ctx
->u
.lsp
.flags
= lsp
->flags
;
1481 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
1482 for (nhlfe
= lsp
->nhlfe_list
; nhlfe
; nhlfe
= nhlfe
->next
) {
1483 /* Not sure if this is meaningful... */
1484 if (nhlfe
->nexthop
== NULL
)
1488 zebra_mpls_lsp_add_nhlfe(
1491 nhlfe
->nexthop
->type
,
1492 &(nhlfe
->nexthop
->gate
),
1493 nhlfe
->nexthop
->ifindex
,
1494 nhlfe
->nexthop
->nh_label
->label
[0]);
1496 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
1501 /* Need to copy flags too */
1502 new_nhlfe
->flags
= nhlfe
->flags
;
1503 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
1505 if (nhlfe
== lsp
->best_nhlfe
)
1506 ctx
->u
.lsp
.best_nhlfe
= new_nhlfe
;
1509 /* On error the ctx will be cleaned-up, so we don't need to
1510 * deal with any allocated nhlfe or nexthop structs here.
1517 * Capture information for an LSP update in a dplane context.
1519 static int dplane_ctx_pw_init(struct zebra_dplane_ctx
*ctx
,
1520 enum dplane_op_e op
,
1521 struct zebra_pw
*pw
)
1525 struct route_table
*table
;
1526 struct route_node
*rn
;
1527 struct route_entry
*re
;
1529 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1530 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
1531 dplane_op2str(op
), pw
->ifname
, pw
->local_label
,
1535 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1537 /* Capture namespace info: no netlink support as of 12/18,
1538 * but just in case...
1540 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
1542 memset(&ctx
->u
.pw
, 0, sizeof(ctx
->u
.pw
));
1544 /* This name appears to be c-string, so we use string copy. */
1545 strlcpy(ctx
->zd_ifname
, pw
->ifname
, sizeof(ctx
->zd_ifname
));
1547 ctx
->zd_vrf_id
= pw
->vrf_id
;
1548 ctx
->zd_ifindex
= pw
->ifindex
;
1549 ctx
->u
.pw
.type
= pw
->type
;
1550 ctx
->u
.pw
.af
= pw
->af
;
1551 ctx
->u
.pw
.local_label
= pw
->local_label
;
1552 ctx
->u
.pw
.remote_label
= pw
->remote_label
;
1553 ctx
->u
.pw
.flags
= pw
->flags
;
1555 ctx
->u
.pw
.dest
= pw
->nexthop
;
1557 ctx
->u
.pw
.fields
= pw
->data
;
1559 /* Capture nexthop info for the pw destination. We need to look
1560 * up and use zebra datastructs, but we're running in the zebra
1561 * pthread here so that should be ok.
1563 memcpy(&p
.u
, &pw
->nexthop
, sizeof(pw
->nexthop
));
1565 p
.prefixlen
= ((pw
->af
== AF_INET
) ?
1566 IPV4_MAX_PREFIXLEN
: IPV6_MAX_PREFIXLEN
);
1568 afi
= (pw
->af
== AF_INET
) ? AFI_IP
: AFI_IP6
;
1569 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, pw
->vrf_id
);
1571 rn
= route_node_match(table
, &p
);
1573 RNODE_FOREACH_RE(rn
, re
) {
1574 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
1579 copy_nexthops(&(ctx
->u
.pw
.nhg
.nexthop
),
1580 re
->ng
.nexthop
, NULL
);
1582 route_unlock_node(rn
);
1590 * Enqueue a new update,
1591 * and ensure an event is active for the dataplane pthread.
1593 static int dplane_update_enqueue(struct zebra_dplane_ctx
*ctx
)
1596 uint32_t high
, curr
;
1598 /* Enqueue for processing by the dataplane pthread */
1601 TAILQ_INSERT_TAIL(&zdplane_info
.dg_update_ctx_q
, ctx
,
1606 curr
= atomic_add_fetch_explicit(
1608 /* TODO -- issue with the clang atomic/intrinsics currently;
1609 * casting away the 'Atomic'-ness of the variable works.
1611 (uint32_t *)&(zdplane_info
.dg_routes_queued
),
1613 &(zdplane_info
.dg_routes_queued
),
1615 1, memory_order_seq_cst
);
1617 /* Maybe update high-water counter also */
1618 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
1619 memory_order_seq_cst
);
1620 while (high
< curr
) {
1621 if (atomic_compare_exchange_weak_explicit(
1622 &zdplane_info
.dg_routes_queued_max
,
1624 memory_order_seq_cst
,
1625 memory_order_seq_cst
))
1629 /* Ensure that an event for the dataplane thread is active */
1630 ret
= dplane_provider_work_ready();
1636 * Utility that prepares a route update and enqueues it for processing
1638 static enum zebra_dplane_result
1639 dplane_route_update_internal(struct route_node
*rn
,
1640 struct route_entry
*re
,
1641 struct route_entry
*old_re
,
1642 enum dplane_op_e op
)
1644 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1646 struct zebra_dplane_ctx
*ctx
= NULL
;
1648 /* Obtain context block */
1649 ctx
= dplane_ctx_alloc();
1651 /* Init context with info from zebra data structs */
1652 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
1654 /* Capture some extra info for update case
1655 * where there's a different 'old' route.
1657 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
1658 old_re
&& (old_re
!= re
)) {
1659 ctx
->zd_is_update
= true;
1661 old_re
->dplane_sequence
=
1662 zebra_router_get_next_sequence();
1663 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
1665 ctx
->u
.rinfo
.zd_old_tag
= old_re
->tag
;
1666 ctx
->u
.rinfo
.zd_old_type
= old_re
->type
;
1667 ctx
->u
.rinfo
.zd_old_instance
= old_re
->instance
;
1668 ctx
->u
.rinfo
.zd_old_distance
= old_re
->distance
;
1669 ctx
->u
.rinfo
.zd_old_metric
= old_re
->metric
;
1671 #ifndef HAVE_NETLINK
1672 /* For bsd, capture previous re's nexthops too, sigh.
1673 * We'll need these to do per-nexthop deletes.
1675 copy_nexthops(&(ctx
->u
.rinfo
.zd_old_ng
.nexthop
),
1676 old_re
->ng
.nexthop
, NULL
);
1677 #endif /* !HAVE_NETLINK */
1680 /* Enqueue context for processing */
1681 ret
= dplane_update_enqueue(ctx
);
1684 /* Update counter */
1685 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
1686 memory_order_relaxed
);
1689 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1691 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
1692 memory_order_relaxed
);
1694 dplane_ctx_free(&ctx
);
1701 * Enqueue a route 'add' for the dataplane.
1703 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
1704 struct route_entry
*re
)
1706 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1708 if (rn
== NULL
|| re
== NULL
)
1711 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1712 DPLANE_OP_ROUTE_INSTALL
);
1719 * Enqueue a route update for the dataplane.
1721 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
1722 struct route_entry
*re
,
1723 struct route_entry
*old_re
)
1725 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1727 if (rn
== NULL
|| re
== NULL
)
1730 ret
= dplane_route_update_internal(rn
, re
, old_re
,
1731 DPLANE_OP_ROUTE_UPDATE
);
1737 * Enqueue a route removal for the dataplane.
1739 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
1740 struct route_entry
*re
)
1742 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1744 if (rn
== NULL
|| re
== NULL
)
1747 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1748 DPLANE_OP_ROUTE_DELETE
);
1755 * Notify the dplane when system/connected routes change.
1757 enum zebra_dplane_result
dplane_sys_route_add(struct route_node
*rn
,
1758 struct route_entry
*re
)
1760 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1762 /* Ignore this event unless a provider plugin has requested it. */
1763 if (!zdplane_info
.dg_sys_route_notifs
) {
1764 ret
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1768 if (rn
== NULL
|| re
== NULL
)
1771 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1772 DPLANE_OP_SYS_ROUTE_ADD
);
1779 * Notify the dplane when system/connected routes are deleted.
1781 enum zebra_dplane_result
dplane_sys_route_del(struct route_node
*rn
,
1782 struct route_entry
*re
)
1784 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1786 /* Ignore this event unless a provider plugin has requested it. */
1787 if (!zdplane_info
.dg_sys_route_notifs
) {
1788 ret
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
1792 if (rn
== NULL
|| re
== NULL
)
1795 ret
= dplane_route_update_internal(rn
, re
, NULL
,
1796 DPLANE_OP_SYS_ROUTE_DELETE
);
1803 * Update from an async notification, to bring other fibs up-to-date.
1805 enum zebra_dplane_result
1806 dplane_route_notif_update(struct route_node
*rn
,
1807 struct route_entry
*re
,
1808 enum dplane_op_e op
,
1809 struct zebra_dplane_ctx
*ctx
)
1811 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1812 struct zebra_dplane_ctx
*new_ctx
= NULL
;
1813 struct nexthop
*nexthop
;
1815 if (rn
== NULL
|| re
== NULL
)
1818 new_ctx
= dplane_ctx_alloc();
1819 if (new_ctx
== NULL
)
1822 /* Init context with info from zebra data structs */
1823 dplane_ctx_route_init(new_ctx
, op
, rn
, re
);
1825 /* For add/update, need to adjust the nexthops so that we match
1826 * the notification state, which may not be the route-entry/RIB
1829 if (op
== DPLANE_OP_ROUTE_UPDATE
||
1830 op
== DPLANE_OP_ROUTE_INSTALL
) {
1832 nexthops_free(new_ctx
->u
.rinfo
.zd_ng
.nexthop
);
1833 new_ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
1835 copy_nexthops(&(new_ctx
->u
.rinfo
.zd_ng
.nexthop
),
1836 (rib_active_nhg(re
))->nexthop
, NULL
);
1838 for (ALL_NEXTHOPS(new_ctx
->u
.rinfo
.zd_ng
, nexthop
))
1839 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1843 /* Capture info about the source of the notification, in 'ctx' */
1844 dplane_ctx_set_notif_provider(new_ctx
,
1845 dplane_ctx_get_notif_provider(ctx
));
1847 dplane_update_enqueue(new_ctx
);
1849 ret
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1856 * Enqueue LSP add for the dataplane.
1858 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
)
1860 enum zebra_dplane_result ret
=
1861 lsp_update_internal(lsp
, DPLANE_OP_LSP_INSTALL
);
1867 * Enqueue LSP update for the dataplane.
1869 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
)
1871 enum zebra_dplane_result ret
=
1872 lsp_update_internal(lsp
, DPLANE_OP_LSP_UPDATE
);
1878 * Enqueue LSP delete for the dataplane.
1880 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
)
1882 enum zebra_dplane_result ret
=
1883 lsp_update_internal(lsp
, DPLANE_OP_LSP_DELETE
);
1888 /* Update or un-install resulting from an async notification */
1889 enum zebra_dplane_result
1890 dplane_lsp_notif_update(zebra_lsp_t
*lsp
,
1891 enum dplane_op_e op
,
1892 struct zebra_dplane_ctx
*notif_ctx
)
1894 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1896 struct zebra_dplane_ctx
*ctx
= NULL
;
1898 /* Obtain context block */
1899 ctx
= dplane_ctx_alloc();
1905 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1909 /* Capture info about the source of the notification */
1910 dplane_ctx_set_notif_provider(
1912 dplane_ctx_get_notif_provider(notif_ctx
));
1914 ret
= dplane_update_enqueue(ctx
);
1917 /* Update counter */
1918 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1919 memory_order_relaxed
);
1922 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1924 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1925 memory_order_relaxed
);
1927 dplane_ctx_free(&ctx
);
1933 * Enqueue pseudowire install for the dataplane.
1935 enum zebra_dplane_result
dplane_pw_install(struct zebra_pw
*pw
)
1937 return pw_update_internal(pw
, DPLANE_OP_PW_INSTALL
);
1941 * Enqueue pseudowire un-install for the dataplane.
1943 enum zebra_dplane_result
dplane_pw_uninstall(struct zebra_pw
*pw
)
1945 return pw_update_internal(pw
, DPLANE_OP_PW_UNINSTALL
);
1949 * Common internal LSP update utility
1951 static enum zebra_dplane_result
lsp_update_internal(zebra_lsp_t
*lsp
,
1952 enum dplane_op_e op
)
1954 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1956 struct zebra_dplane_ctx
*ctx
= NULL
;
1958 /* Obtain context block */
1959 ctx
= dplane_ctx_alloc();
1961 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
1965 ret
= dplane_update_enqueue(ctx
);
1968 /* Update counter */
1969 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
1970 memory_order_relaxed
);
1973 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
1975 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
1976 memory_order_relaxed
);
1977 dplane_ctx_free(&ctx
);
1984 * Internal, common handler for pseudowire updates.
1986 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
1987 enum dplane_op_e op
)
1989 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
1991 struct zebra_dplane_ctx
*ctx
= NULL
;
1993 ctx
= dplane_ctx_alloc();
1995 ret
= dplane_ctx_pw_init(ctx
, op
, pw
);
1999 ret
= dplane_update_enqueue(ctx
);
2002 /* Update counter */
2003 atomic_fetch_add_explicit(&zdplane_info
.dg_pws_in
, 1,
2004 memory_order_relaxed
);
2007 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2009 atomic_fetch_add_explicit(&zdplane_info
.dg_pw_errors
, 1,
2010 memory_order_relaxed
);
2011 dplane_ctx_free(&ctx
);
2018 * Enqueue interface address add for the dataplane.
2020 enum zebra_dplane_result
dplane_intf_addr_set(const struct interface
*ifp
,
2021 const struct connected
*ifc
)
2023 #if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2024 /* Extra checks for this OS path. */
2026 /* Don't configure PtP addresses on broadcast ifs or reverse */
2027 if (!(ifp
->flags
& IFF_POINTOPOINT
) != !CONNECTED_PEER(ifc
)) {
2028 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_DPLANE
)
2029 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2031 return ZEBRA_DPLANE_REQUEST_FAILURE
;
2034 /* Ensure that no existing installed v4 route conflicts with
2035 * the new interface prefix. This check must be done in the
2036 * zebra pthread context, and any route delete (if needed)
2037 * is enqueued before the interface address programming attempt.
2039 if (ifc
->address
->family
== AF_INET
) {
2040 struct prefix_ipv4
*p
;
2042 p
= (struct prefix_ipv4
*)ifc
->address
;
2043 rib_lookup_and_pushup(p
, ifp
->vrf_id
);
2047 return intf_addr_update_internal(ifp
, ifc
, DPLANE_OP_ADDR_INSTALL
);
2051 * Enqueue interface address remove/uninstall for the dataplane.
2053 enum zebra_dplane_result
dplane_intf_addr_unset(const struct interface
*ifp
,
2054 const struct connected
*ifc
)
2056 return intf_addr_update_internal(ifp
, ifc
, DPLANE_OP_ADDR_UNINSTALL
);
2059 static enum zebra_dplane_result
intf_addr_update_internal(
2060 const struct interface
*ifp
, const struct connected
*ifc
,
2061 enum dplane_op_e op
)
2063 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2065 struct zebra_dplane_ctx
*ctx
= NULL
;
2066 struct zebra_ns
*zns
;
2068 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2069 char addr_str
[PREFIX_STRLEN
];
2071 prefix2str(ifc
->address
, addr_str
, sizeof(addr_str
));
2073 zlog_debug("init intf ctx %s: idx %d, addr %u:%s",
2074 dplane_op2str(op
), ifp
->ifindex
, ifp
->vrf_id
,
2078 ctx
= dplane_ctx_alloc();
2081 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2082 ctx
->zd_vrf_id
= ifp
->vrf_id
;
2084 zns
= zebra_ns_lookup(ifp
->vrf_id
);
2085 dplane_ctx_ns_init(ctx
, zns
, false);
2087 /* Init the interface-addr-specific area */
2088 memset(&ctx
->u
.intf
, 0, sizeof(ctx
->u
.intf
));
2090 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
2091 ctx
->zd_ifindex
= ifp
->ifindex
;
2092 ctx
->u
.intf
.prefix
= *(ifc
->address
);
2094 if (if_is_broadcast(ifp
))
2095 ctx
->u
.intf
.flags
|= DPLANE_INTF_BROADCAST
;
2097 if (CONNECTED_PEER(ifc
)) {
2098 ctx
->u
.intf
.dest_prefix
= *(ifc
->destination
);
2099 ctx
->u
.intf
.flags
|=
2100 (DPLANE_INTF_CONNECTED
| DPLANE_INTF_HAS_DEST
);
2103 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
2104 ctx
->u
.intf
.flags
|= DPLANE_INTF_SECONDARY
;
2109 ctx
->u
.intf
.flags
|= DPLANE_INTF_HAS_LABEL
;
2111 /* Use embedded buffer if it's adequate; else allocate. */
2112 len
= strlen(ifc
->label
);
2114 if (len
< sizeof(ctx
->u
.intf
.label_buf
)) {
2115 strlcpy(ctx
->u
.intf
.label_buf
, ifc
->label
,
2116 sizeof(ctx
->u
.intf
.label_buf
));
2117 ctx
->u
.intf
.label
= ctx
->u
.intf
.label_buf
;
2119 ctx
->u
.intf
.label
= strdup(ifc
->label
);
2123 ret
= dplane_update_enqueue(ctx
);
2125 /* Increment counter */
2126 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addrs_in
, 1,
2127 memory_order_relaxed
);
2130 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2133 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addr_errors
,
2134 1, memory_order_relaxed
);
2135 dplane_ctx_free(&ctx
);
2142 * Enqueue vxlan/evpn mac add (or update).
2144 enum zebra_dplane_result
dplane_mac_add(const struct interface
*ifp
,
2145 const struct interface
*bridge_ifp
,
2147 const struct ethaddr
*mac
,
2148 struct in_addr vtep_ip
,
2151 enum zebra_dplane_result result
;
2153 /* Use common helper api */
2154 result
= mac_update_internal(DPLANE_OP_MAC_INSTALL
, ifp
, bridge_ifp
,
2155 vid
, mac
, vtep_ip
, sticky
);
2160 * Enqueue vxlan/evpn mac delete.
2162 enum zebra_dplane_result
dplane_mac_del(const struct interface
*ifp
,
2163 const struct interface
*bridge_ifp
,
2165 const struct ethaddr
*mac
,
2166 struct in_addr vtep_ip
)
2168 enum zebra_dplane_result result
;
2170 /* Use common helper api */
2171 result
= mac_update_internal(DPLANE_OP_MAC_DELETE
, ifp
, bridge_ifp
,
2172 vid
, mac
, vtep_ip
, false);
2177 * Common helper api for MAC address/vxlan updates
2179 static enum zebra_dplane_result
2180 mac_update_internal(enum dplane_op_e op
,
2181 const struct interface
*ifp
,
2182 const struct interface
*br_ifp
,
2184 const struct ethaddr
*mac
,
2185 struct in_addr vtep_ip
,
2188 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2190 struct zebra_dplane_ctx
*ctx
= NULL
;
2191 struct zebra_ns
*zns
;
2193 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2194 char buf1
[ETHER_ADDR_STRLEN
], buf2
[PREFIX_STRLEN
];
2196 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
2198 prefix_mac2str(mac
, buf1
, sizeof(buf1
)),
2200 inet_ntop(AF_INET
, &vtep_ip
, buf2
, sizeof(buf2
)));
2203 ctx
= dplane_ctx_alloc();
2206 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2207 ctx
->zd_vrf_id
= ifp
->vrf_id
;
2209 zns
= zebra_ns_lookup(ifp
->vrf_id
);
2210 dplane_ctx_ns_init(ctx
, zns
, false);
2212 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
2213 ctx
->zd_ifindex
= ifp
->ifindex
;
2215 /* Init the mac-specific data area */
2216 memset(&ctx
->u
.macinfo
, 0, sizeof(ctx
->u
.macinfo
));
2218 ctx
->u
.macinfo
.br_ifindex
= br_ifp
->ifindex
;
2219 ctx
->u
.macinfo
.vtep_ip
= vtep_ip
;
2220 ctx
->u
.macinfo
.mac
= *mac
;
2221 ctx
->u
.macinfo
.vid
= vid
;
2222 ctx
->u
.macinfo
.is_sticky
= sticky
;
2224 /* Enqueue for processing on the dplane pthread */
2225 ret
= dplane_update_enqueue(ctx
);
2227 /* Increment counter */
2228 atomic_fetch_add_explicit(&zdplane_info
.dg_macs_in
, 1,
2229 memory_order_relaxed
);
2232 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2235 atomic_fetch_add_explicit(&zdplane_info
.dg_mac_errors
, 1,
2236 memory_order_relaxed
);
2237 dplane_ctx_free(&ctx
);
2244 * Enqueue evpn neighbor add for the dataplane.
2246 enum zebra_dplane_result
dplane_neigh_add(const struct interface
*ifp
,
2247 const struct ipaddr
*ip
,
2248 const struct ethaddr
*mac
,
2251 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2253 result
= neigh_update_internal(DPLANE_OP_NEIGH_INSTALL
,
2254 ifp
, mac
, ip
, flags
, 0);
2260 * Enqueue evpn neighbor update for the dataplane.
2262 enum zebra_dplane_result
dplane_neigh_update(const struct interface
*ifp
,
2263 const struct ipaddr
*ip
,
2264 const struct ethaddr
*mac
)
2266 enum zebra_dplane_result result
;
2268 result
= neigh_update_internal(DPLANE_OP_NEIGH_UPDATE
,
2269 ifp
, mac
, ip
, 0, DPLANE_NUD_PROBE
);
2275 * Enqueue evpn neighbor delete for the dataplane.
2277 enum zebra_dplane_result
dplane_neigh_delete(const struct interface
*ifp
,
2278 const struct ipaddr
*ip
)
2280 enum zebra_dplane_result result
;
2282 result
= neigh_update_internal(DPLANE_OP_NEIGH_DELETE
,
2283 ifp
, NULL
, ip
, 0, 0);
2289 * Enqueue evpn VTEP add for the dataplane.
2291 enum zebra_dplane_result
dplane_vtep_add(const struct interface
*ifp
,
2292 const struct in_addr
*ip
,
2295 enum zebra_dplane_result result
;
2296 struct ethaddr mac
= { {0, 0, 0, 0, 0, 0} };
2299 if (IS_ZEBRA_DEBUG_VXLAN
)
2300 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
2301 inet_ntoa(*ip
), vni
, ifp
->name
, ifp
->ifindex
);
2303 SET_IPADDR_V4(&addr
);
2304 addr
.ipaddr_v4
= *ip
;
2306 result
= neigh_update_internal(DPLANE_OP_VTEP_ADD
,
2307 ifp
, &mac
, &addr
, 0, 0);
2313 * Enqueue evpn VTEP add for the dataplane.
2315 enum zebra_dplane_result
dplane_vtep_delete(const struct interface
*ifp
,
2316 const struct in_addr
*ip
,
2319 enum zebra_dplane_result result
;
2320 struct ethaddr mac
= { {0, 0, 0, 0, 0, 0} };
2323 if (IS_ZEBRA_DEBUG_VXLAN
)
2325 "Uninstall %s from flood list for VNI %u intf %s(%u)",
2326 inet_ntoa(*ip
), vni
, ifp
->name
, ifp
->ifindex
);
2328 SET_IPADDR_V4(&addr
);
2329 addr
.ipaddr_v4
= *ip
;
2331 result
= neigh_update_internal(DPLANE_OP_VTEP_DELETE
,
2332 ifp
, &mac
, &addr
, 0, 0);
2338 * Common helper api for evpn neighbor updates
2340 static enum zebra_dplane_result
2341 neigh_update_internal(enum dplane_op_e op
,
2342 const struct interface
*ifp
,
2343 const struct ethaddr
*mac
,
2344 const struct ipaddr
*ip
,
2345 uint32_t flags
, uint16_t state
)
2347 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
2349 struct zebra_dplane_ctx
*ctx
= NULL
;
2350 struct zebra_ns
*zns
;
2352 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2353 char buf1
[ETHER_ADDR_STRLEN
], buf2
[PREFIX_STRLEN
];
2355 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
2357 prefix_mac2str(mac
, buf1
, sizeof(buf1
)),
2359 ipaddr2str(ip
, buf2
, sizeof(buf2
)));
2362 ctx
= dplane_ctx_alloc();
2365 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2366 ctx
->zd_vrf_id
= ifp
->vrf_id
;
2368 zns
= zebra_ns_lookup(ifp
->vrf_id
);
2369 dplane_ctx_ns_init(ctx
, zns
, false);
2371 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
2372 ctx
->zd_ifindex
= ifp
->ifindex
;
2374 /* Init the neighbor-specific data area */
2375 memset(&ctx
->u
.neigh
, 0, sizeof(ctx
->u
.neigh
));
2377 ctx
->u
.neigh
.ip_addr
= *ip
;
2379 ctx
->u
.neigh
.mac
= *mac
;
2380 ctx
->u
.neigh
.flags
= flags
;
2381 ctx
->u
.neigh
.state
= state
;
2383 /* Enqueue for processing on the dplane pthread */
2384 ret
= dplane_update_enqueue(ctx
);
2386 /* Increment counter */
2387 atomic_fetch_add_explicit(&zdplane_info
.dg_neighs_in
, 1,
2388 memory_order_relaxed
);
2391 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
2394 atomic_fetch_add_explicit(&zdplane_info
.dg_neigh_errors
, 1,
2395 memory_order_relaxed
);
2396 dplane_ctx_free(&ctx
);
2403 * Handler for 'show dplane'
2405 int dplane_show_helper(struct vty
*vty
, bool detailed
)
2407 uint64_t queued
, queue_max
, limit
, errs
, incoming
, yields
,
2410 /* Using atomics because counters are being changed in different
2413 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
2414 memory_order_relaxed
);
2415 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
2416 memory_order_relaxed
);
2417 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
2418 memory_order_relaxed
);
2419 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
2420 memory_order_relaxed
);
2421 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
2422 memory_order_relaxed
);
2423 yields
= atomic_load_explicit(&zdplane_info
.dg_update_yields
,
2424 memory_order_relaxed
);
2425 other_errs
= atomic_load_explicit(&zdplane_info
.dg_other_errors
,
2426 memory_order_relaxed
);
2428 vty_out(vty
, "Zebra dataplane:\nRoute updates: %"PRIu64
"\n",
2430 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
2431 vty_out(vty
, "Other errors : %"PRIu64
"\n", other_errs
);
2432 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
2433 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
2434 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
2435 vty_out(vty
, "Dplane update yields: %"PRIu64
"\n", yields
);
2437 incoming
= atomic_load_explicit(&zdplane_info
.dg_lsps_in
,
2438 memory_order_relaxed
);
2439 errs
= atomic_load_explicit(&zdplane_info
.dg_lsp_errors
,
2440 memory_order_relaxed
);
2441 vty_out(vty
, "LSP updates: %"PRIu64
"\n", incoming
);
2442 vty_out(vty
, "LSP update errors: %"PRIu64
"\n", errs
);
2444 incoming
= atomic_load_explicit(&zdplane_info
.dg_pws_in
,
2445 memory_order_relaxed
);
2446 errs
= atomic_load_explicit(&zdplane_info
.dg_pw_errors
,
2447 memory_order_relaxed
);
2448 vty_out(vty
, "PW updates: %"PRIu64
"\n", incoming
);
2449 vty_out(vty
, "PW update errors: %"PRIu64
"\n", errs
);
2451 incoming
= atomic_load_explicit(&zdplane_info
.dg_intf_addrs_in
,
2452 memory_order_relaxed
);
2453 errs
= atomic_load_explicit(&zdplane_info
.dg_intf_addr_errors
,
2454 memory_order_relaxed
);
2455 vty_out(vty
, "Intf addr updates: %"PRIu64
"\n", incoming
);
2456 vty_out(vty
, "Intf addr errors: %"PRIu64
"\n", errs
);
2458 incoming
= atomic_load_explicit(&zdplane_info
.dg_macs_in
,
2459 memory_order_relaxed
);
2460 errs
= atomic_load_explicit(&zdplane_info
.dg_mac_errors
,
2461 memory_order_relaxed
);
2462 vty_out(vty
, "EVPN MAC updates: %"PRIu64
"\n", incoming
);
2463 vty_out(vty
, "EVPN MAC errors: %"PRIu64
"\n", errs
);
2465 incoming
= atomic_load_explicit(&zdplane_info
.dg_neighs_in
,
2466 memory_order_relaxed
);
2467 errs
= atomic_load_explicit(&zdplane_info
.dg_neigh_errors
,
2468 memory_order_relaxed
);
2469 vty_out(vty
, "EVPN neigh updates: %"PRIu64
"\n", incoming
);
2470 vty_out(vty
, "EVPN neigh errors: %"PRIu64
"\n", errs
);
2476 * Handler for 'show dplane providers'
2478 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
2480 struct zebra_dplane_provider
*prov
;
2481 uint64_t in
, in_max
, out
, out_max
;
2483 vty_out(vty
, "Zebra dataplane providers:\n");
2486 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
2489 /* Show counters, useful info from each registered provider */
2492 in
= atomic_load_explicit(&prov
->dp_in_counter
,
2493 memory_order_relaxed
);
2494 in_max
= atomic_load_explicit(&prov
->dp_in_max
,
2495 memory_order_relaxed
);
2496 out
= atomic_load_explicit(&prov
->dp_out_counter
,
2497 memory_order_relaxed
);
2498 out_max
= atomic_load_explicit(&prov
->dp_out_max
,
2499 memory_order_relaxed
);
2501 vty_out(vty
, "%s (%u): in: %"PRIu64
", q_max: %"PRIu64
", "
2502 "out: %"PRIu64
", q_max: %"PRIu64
"\n",
2503 prov
->dp_name
, prov
->dp_id
, in
, in_max
, out
, out_max
);
2506 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
2514 * Helper for 'show run' etc.
2516 int dplane_config_write_helper(struct vty
*vty
)
2518 if (zdplane_info
.dg_max_queued_updates
!= DPLANE_DEFAULT_MAX_QUEUED
)
2519 vty_out(vty
, "zebra dplane limit %u\n",
2520 zdplane_info
.dg_max_queued_updates
);
2526 * Provider registration
2528 int dplane_provider_register(const char *name
,
2529 enum dplane_provider_prio prio
,
2531 int (*start_fp
)(struct zebra_dplane_provider
*),
2532 int (*fp
)(struct zebra_dplane_provider
*),
2533 int (*fini_fp
)(struct zebra_dplane_provider
*,
2536 struct zebra_dplane_provider
**prov_p
)
2539 struct zebra_dplane_provider
*p
= NULL
, *last
;
2547 if (prio
<= DPLANE_PRIO_NONE
||
2548 prio
> DPLANE_PRIO_LAST
) {
2553 /* Allocate and init new provider struct */
2554 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
2556 pthread_mutex_init(&(p
->dp_mutex
), NULL
);
2557 TAILQ_INIT(&(p
->dp_ctx_in_q
));
2558 TAILQ_INIT(&(p
->dp_ctx_out_q
));
2560 p
->dp_priority
= prio
;
2562 p
->dp_start
= start_fp
;
2563 p
->dp_fini
= fini_fp
;
2566 /* Lock - the dplane pthread may be running */
2569 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
2572 strlcpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
2574 snprintf(p
->dp_name
, DPLANE_PROVIDER_NAMELEN
,
2575 "provider-%u", p
->dp_id
);
2577 /* Insert into list ordered by priority */
2578 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
2579 if (last
->dp_priority
> p
->dp_priority
)
2584 TAILQ_INSERT_BEFORE(last
, p
, dp_prov_link
);
2586 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
2592 if (IS_ZEBRA_DEBUG_DPLANE
)
2593 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
2594 p
->dp_name
, p
->dp_id
, p
->dp_priority
);
2603 /* Accessors for provider attributes */
2604 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
)
2606 return prov
->dp_name
;
2609 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
)
2614 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
)
2616 return prov
->dp_data
;
2619 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
)
2621 return zdplane_info
.dg_updates_per_cycle
;
2624 /* Lock/unlock a provider's mutex - iff the provider was registered with
2625 * the THREADED flag.
2627 void dplane_provider_lock(struct zebra_dplane_provider
*prov
)
2629 if (dplane_provider_is_threaded(prov
))
2630 DPLANE_PROV_LOCK(prov
);
2633 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
)
2635 if (dplane_provider_is_threaded(prov
))
2636 DPLANE_PROV_UNLOCK(prov
);
2640 * Dequeue and maintain associated counter
2642 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
2643 struct zebra_dplane_provider
*prov
)
2645 struct zebra_dplane_ctx
*ctx
= NULL
;
2647 dplane_provider_lock(prov
);
2649 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
2651 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
2653 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, 1,
2654 memory_order_relaxed
);
2657 dplane_provider_unlock(prov
);
2663 * Dequeue work to a list, return count
2665 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
2666 struct dplane_ctx_q
*listp
)
2669 struct zebra_dplane_ctx
*ctx
;
2671 limit
= zdplane_info
.dg_updates_per_cycle
;
2673 dplane_provider_lock(prov
);
2675 for (ret
= 0; ret
< limit
; ret
++) {
2676 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
2678 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
2680 TAILQ_INSERT_TAIL(listp
, ctx
, zd_q_entries
);
2687 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, ret
,
2688 memory_order_relaxed
);
2690 dplane_provider_unlock(prov
);
2696 * Enqueue and maintain associated counter
2698 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
2699 struct zebra_dplane_ctx
*ctx
)
2701 dplane_provider_lock(prov
);
2703 TAILQ_INSERT_TAIL(&(prov
->dp_ctx_out_q
), ctx
,
2706 dplane_provider_unlock(prov
);
2708 atomic_fetch_add_explicit(&(prov
->dp_out_counter
), 1,
2709 memory_order_relaxed
);
2713 * Accessor for provider object
2715 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
)
2717 return (prov
->dp_flags
& DPLANE_PROV_FLAG_THREADED
);
2721 * Internal helper that copies information from a zebra ns object; this is
2722 * called in the zebra main pthread context as part of dplane ctx init.
2724 static void dplane_info_from_zns(struct zebra_dplane_info
*ns_info
,
2725 struct zebra_ns
*zns
)
2727 ns_info
->ns_id
= zns
->ns_id
;
2729 #if defined(HAVE_NETLINK)
2730 ns_info
->is_cmd
= true;
2731 ns_info
->nls
= zns
->netlink_dplane
;
2732 #endif /* NETLINK */
2736 * Provider api to signal that work/events are available
2737 * for the dataplane pthread.
2739 int dplane_provider_work_ready(void)
2741 /* Note that during zebra startup, we may be offered work before
2742 * the dataplane pthread (and thread-master) are ready. We want to
2743 * enqueue the work, but the event-scheduling machinery may not be
2746 if (zdplane_info
.dg_run
) {
2747 thread_add_event(zdplane_info
.dg_master
,
2748 dplane_thread_loop
, NULL
, 0,
2749 &zdplane_info
.dg_t_update
);
2756 * Enqueue a context directly to zebra main.
2758 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx
*ctx
)
2760 struct dplane_ctx_q temp_list
;
2762 /* Zebra's api takes a list, so we need to use a temporary list */
2763 TAILQ_INIT(&temp_list
);
2765 TAILQ_INSERT_TAIL(&temp_list
, ctx
, zd_q_entries
);
2766 (zdplane_info
.dg_results_cb
)(&temp_list
);
2770 * Kernel dataplane provider
2774 * Handler for kernel LSP updates
2776 static enum zebra_dplane_result
2777 kernel_dplane_lsp_update(struct zebra_dplane_ctx
*ctx
)
2779 enum zebra_dplane_result res
;
2781 /* Call into the synchronous kernel-facing code here */
2782 res
= kernel_lsp_update(ctx
);
2784 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2785 atomic_fetch_add_explicit(
2786 &zdplane_info
.dg_lsp_errors
, 1,
2787 memory_order_relaxed
);
2793 * Handler for kernel pseudowire updates
2795 static enum zebra_dplane_result
2796 kernel_dplane_pw_update(struct zebra_dplane_ctx
*ctx
)
2798 enum zebra_dplane_result res
;
2800 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2801 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
2802 dplane_ctx_get_ifname(ctx
),
2803 dplane_op2str(ctx
->zd_op
),
2804 dplane_ctx_get_pw_af(ctx
),
2805 dplane_ctx_get_pw_local_label(ctx
),
2806 dplane_ctx_get_pw_remote_label(ctx
));
2808 res
= kernel_pw_update(ctx
);
2810 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2811 atomic_fetch_add_explicit(
2812 &zdplane_info
.dg_pw_errors
, 1,
2813 memory_order_relaxed
);
2819 * Handler for kernel route updates
2821 static enum zebra_dplane_result
2822 kernel_dplane_route_update(struct zebra_dplane_ctx
*ctx
)
2824 enum zebra_dplane_result res
;
2826 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2827 char dest_str
[PREFIX_STRLEN
];
2829 prefix2str(dplane_ctx_get_dest(ctx
),
2830 dest_str
, sizeof(dest_str
));
2832 zlog_debug("%u:%s Dplane route update ctx %p op %s",
2833 dplane_ctx_get_vrf(ctx
), dest_str
,
2834 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
2837 /* Call into the synchronous kernel-facing code here */
2838 res
= kernel_route_update(ctx
);
2840 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2841 atomic_fetch_add_explicit(
2842 &zdplane_info
.dg_route_errors
, 1,
2843 memory_order_relaxed
);
2849 * Handler for kernel-facing interface address updates
2851 static enum zebra_dplane_result
2852 kernel_dplane_address_update(struct zebra_dplane_ctx
*ctx
)
2854 enum zebra_dplane_result res
;
2856 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2857 char dest_str
[PREFIX_STRLEN
];
2859 prefix2str(dplane_ctx_get_intf_addr(ctx
), dest_str
,
2862 zlog_debug("Dplane intf %s, idx %u, addr %s",
2863 dplane_op2str(dplane_ctx_get_op(ctx
)),
2864 dplane_ctx_get_ifindex(ctx
), dest_str
);
2867 res
= kernel_address_update_ctx(ctx
);
2869 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2870 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addr_errors
,
2871 1, memory_order_relaxed
);
2877 * Handler for kernel-facing EVPN MAC address updates
2879 static enum zebra_dplane_result
2880 kernel_dplane_mac_update(struct zebra_dplane_ctx
*ctx
)
2882 enum zebra_dplane_result res
;
2884 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2885 char buf
[ETHER_ADDR_STRLEN
];
2887 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
,
2890 zlog_debug("Dplane %s, mac %s, ifindex %u",
2891 dplane_op2str(dplane_ctx_get_op(ctx
)),
2892 buf
, dplane_ctx_get_ifindex(ctx
));
2895 res
= kernel_mac_update_ctx(ctx
);
2897 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2898 atomic_fetch_add_explicit(&zdplane_info
.dg_mac_errors
,
2899 1, memory_order_relaxed
);
2905 * Handler for kernel-facing EVPN neighbor updates
2907 static enum zebra_dplane_result
2908 kernel_dplane_neigh_update(struct zebra_dplane_ctx
*ctx
)
2910 enum zebra_dplane_result res
;
2912 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2913 char buf
[PREFIX_STRLEN
];
2915 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx
), buf
,
2918 zlog_debug("Dplane %s, ip %s, ifindex %u",
2919 dplane_op2str(dplane_ctx_get_op(ctx
)),
2920 buf
, dplane_ctx_get_ifindex(ctx
));
2923 res
= kernel_neigh_update_ctx(ctx
);
2925 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
2926 atomic_fetch_add_explicit(&zdplane_info
.dg_neigh_errors
,
2927 1, memory_order_relaxed
);
2933 * Kernel provider callback
2935 static int kernel_dplane_process_func(struct zebra_dplane_provider
*prov
)
2937 enum zebra_dplane_result res
;
2938 struct zebra_dplane_ctx
*ctx
;
2941 limit
= dplane_provider_get_work_limit(prov
);
2943 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2944 zlog_debug("dplane provider '%s': processing",
2945 dplane_provider_get_name(prov
));
2947 for (counter
= 0; counter
< limit
; counter
++) {
2949 ctx
= dplane_provider_dequeue_in_ctx(prov
);
2953 /* A previous provider plugin may have asked to skip the
2956 if (dplane_ctx_is_skip_kernel(ctx
)) {
2957 res
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2961 /* Dispatch to appropriate kernel-facing apis */
2962 switch (dplane_ctx_get_op(ctx
)) {
2964 case DPLANE_OP_ROUTE_INSTALL
:
2965 case DPLANE_OP_ROUTE_UPDATE
:
2966 case DPLANE_OP_ROUTE_DELETE
:
2967 res
= kernel_dplane_route_update(ctx
);
2970 case DPLANE_OP_LSP_INSTALL
:
2971 case DPLANE_OP_LSP_UPDATE
:
2972 case DPLANE_OP_LSP_DELETE
:
2973 res
= kernel_dplane_lsp_update(ctx
);
2976 case DPLANE_OP_PW_INSTALL
:
2977 case DPLANE_OP_PW_UNINSTALL
:
2978 res
= kernel_dplane_pw_update(ctx
);
2981 case DPLANE_OP_ADDR_INSTALL
:
2982 case DPLANE_OP_ADDR_UNINSTALL
:
2983 res
= kernel_dplane_address_update(ctx
);
2986 case DPLANE_OP_MAC_INSTALL
:
2987 case DPLANE_OP_MAC_DELETE
:
2988 res
= kernel_dplane_mac_update(ctx
);
2991 case DPLANE_OP_NEIGH_INSTALL
:
2992 case DPLANE_OP_NEIGH_UPDATE
:
2993 case DPLANE_OP_NEIGH_DELETE
:
2994 case DPLANE_OP_VTEP_ADD
:
2995 case DPLANE_OP_VTEP_DELETE
:
2996 res
= kernel_dplane_neigh_update(ctx
);
2999 /* Ignore 'notifications' - no-op */
3000 case DPLANE_OP_SYS_ROUTE_ADD
:
3001 case DPLANE_OP_SYS_ROUTE_DELETE
:
3002 case DPLANE_OP_ROUTE_NOTIFY
:
3003 case DPLANE_OP_LSP_NOTIFY
:
3004 res
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3008 atomic_fetch_add_explicit(
3009 &zdplane_info
.dg_other_errors
, 1,
3010 memory_order_relaxed
);
3012 res
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3017 dplane_ctx_set_status(ctx
, res
);
3019 dplane_provider_enqueue_out_ctx(prov
, ctx
);
3022 /* Ensure that we'll run the work loop again if there's still
3025 if (counter
>= limit
) {
3026 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3027 zlog_debug("dplane provider '%s' reached max updates %d",
3028 dplane_provider_get_name(prov
), counter
);
3030 atomic_fetch_add_explicit(&zdplane_info
.dg_update_yields
,
3031 1, memory_order_relaxed
);
3033 dplane_provider_work_ready();
3039 #if DPLANE_TEST_PROVIDER
3042 * Test dataplane provider plugin
3046 * Test provider process callback
3048 static int test_dplane_process_func(struct zebra_dplane_provider
*prov
)
3050 struct zebra_dplane_ctx
*ctx
;
3053 /* Just moving from 'in' queue to 'out' queue */
3055 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3056 zlog_debug("dplane provider '%s': processing",
3057 dplane_provider_get_name(prov
));
3059 limit
= dplane_provider_get_work_limit(prov
);
3061 for (counter
= 0; counter
< limit
; counter
++) {
3063 ctx
= dplane_provider_dequeue_in_ctx(prov
);
3067 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3068 zlog_debug("dplane provider '%s': op %s",
3069 dplane_provider_get_name(prov
),
3070 dplane_op2str(dplane_ctx_get_op(ctx
)));
3072 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
3074 dplane_provider_enqueue_out_ctx(prov
, ctx
);
3077 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3078 zlog_debug("dplane provider '%s': processed %d",
3079 dplane_provider_get_name(prov
), counter
);
3081 /* Ensure that we'll run the work loop again if there's still
3084 if (counter
>= limit
)
3085 dplane_provider_work_ready();
3091 * Test provider shutdown/fini callback
3093 static int test_dplane_shutdown_func(struct zebra_dplane_provider
*prov
,
3096 if (IS_ZEBRA_DEBUG_DPLANE
)
3097 zlog_debug("dplane provider '%s': %sshutdown",
3098 dplane_provider_get_name(prov
),
3099 early
? "early " : "");
3103 #endif /* DPLANE_TEST_PROVIDER */
3106 * Register default kernel provider
3108 static void dplane_provider_init(void)
3112 ret
= dplane_provider_register("Kernel",
3114 DPLANE_PROV_FLAGS_DEFAULT
, NULL
,
3115 kernel_dplane_process_func
,
3120 zlog_err("Unable to register kernel dplane provider: %d",
3123 #if DPLANE_TEST_PROVIDER
3124 /* Optional test provider ... */
3125 ret
= dplane_provider_register("Test",
3126 DPLANE_PRIO_PRE_KERNEL
,
3127 DPLANE_PROV_FLAGS_DEFAULT
, NULL
,
3128 test_dplane_process_func
,
3129 test_dplane_shutdown_func
,
3130 NULL
/* data */, NULL
);
3133 zlog_err("Unable to register test dplane provider: %d",
3135 #endif /* DPLANE_TEST_PROVIDER */
3138 /* Indicates zebra shutdown/exit is in progress. Some operations may be
3139 * simplified or skipped during shutdown processing.
3141 bool dplane_is_in_shutdown(void)
3143 return zdplane_info
.dg_is_shutdown
;
3147 * Early or pre-shutdown, de-init notification api. This runs pretty
3148 * early during zebra shutdown, as a signal to stop new work and prepare
3149 * for updates generated by shutdown/cleanup activity, as zebra tries to
3150 * remove everything it's responsible for.
3151 * NB: This runs in the main zebra pthread context.
3153 void zebra_dplane_pre_finish(void)
3155 if (IS_ZEBRA_DEBUG_DPLANE
)
3156 zlog_debug("Zebra dataplane pre-fini called");
3158 zdplane_info
.dg_is_shutdown
= true;
3160 /* TODO -- Notify provider(s) of pending shutdown */
3164 * Utility to determine whether work remains enqueued within the dplane;
3165 * used during system shutdown processing.
3167 static bool dplane_work_pending(void)
3170 struct zebra_dplane_ctx
*ctx
;
3171 struct zebra_dplane_provider
*prov
;
3173 /* TODO -- just checking incoming/pending work for now, must check
3178 ctx
= TAILQ_FIRST(&zdplane_info
.dg_update_ctx_q
);
3179 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
3190 dplane_provider_lock(prov
);
3192 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
3194 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
3196 dplane_provider_unlock(prov
);
3202 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
3214 * Shutdown-time intermediate callback, used to determine when all pending
3215 * in-flight updates are done. If there's still work to do, reschedules itself.
3216 * If all work is done, schedules an event to the main zebra thread for
3217 * final zebra shutdown.
3218 * This runs in the dplane pthread context.
3220 static int dplane_check_shutdown_status(struct thread
*event
)
3222 if (IS_ZEBRA_DEBUG_DPLANE
)
3223 zlog_debug("Zebra dataplane shutdown status check called");
3225 if (dplane_work_pending()) {
3226 /* Reschedule dplane check on a short timer */
3227 thread_add_timer_msec(zdplane_info
.dg_master
,
3228 dplane_check_shutdown_status
,
3230 &zdplane_info
.dg_t_shutdown_check
);
3232 /* TODO - give up and stop waiting after a short time? */
3235 /* We appear to be done - schedule a final callback event
3236 * for the zebra main pthread.
3238 thread_add_event(zrouter
.master
, zebra_finalize
, NULL
, 0, NULL
);
3245 * Shutdown, de-init api. This runs pretty late during shutdown,
3246 * after zebra has tried to free/remove/uninstall all routes during shutdown.
3247 * At this point, dplane work may still remain to be done, so we can't just
3248 * blindly terminate. If there's still work to do, we'll periodically check
3249 * and when done, we'll enqueue a task to the zebra main thread for final
3250 * termination processing.
3252 * NB: This runs in the main zebra thread context.
3254 void zebra_dplane_finish(void)
3256 if (IS_ZEBRA_DEBUG_DPLANE
)
3257 zlog_debug("Zebra dataplane fini called");
3259 thread_add_event(zdplane_info
.dg_master
,
3260 dplane_check_shutdown_status
, NULL
, 0,
3261 &zdplane_info
.dg_t_shutdown_check
);
3265 * Main dataplane pthread event loop. The thread takes new incoming work
3266 * and offers it to the first provider. It then iterates through the
3267 * providers, taking complete work from each one and offering it
3268 * to the next in order. At each step, a limited number of updates are
3269 * processed during a cycle in order to provide some fairness.
3271 * This loop through the providers is only run once, so that the dataplane
3272 * pthread can look for other pending work - such as i/o work on behalf of
3275 static int dplane_thread_loop(struct thread
*event
)
3277 struct dplane_ctx_q work_list
;
3278 struct dplane_ctx_q error_list
;
3279 struct zebra_dplane_provider
*prov
;
3280 struct zebra_dplane_ctx
*ctx
, *tctx
;
3281 int limit
, counter
, error_counter
;
3282 uint64_t curr
, high
;
3284 /* Capture work limit per cycle */
3285 limit
= zdplane_info
.dg_updates_per_cycle
;
3287 /* Init temporary lists used to move contexts among providers */
3288 TAILQ_INIT(&work_list
);
3289 TAILQ_INIT(&error_list
);
3292 /* Check for zebra shutdown */
3293 if (!zdplane_info
.dg_run
)
3296 /* Dequeue some incoming work from zebra (if any) onto the temporary
3301 /* Locate initial registered provider */
3302 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
3304 /* Move new work from incoming list to temp list */
3305 for (counter
= 0; counter
< limit
; counter
++) {
3306 ctx
= TAILQ_FIRST(&zdplane_info
.dg_update_ctx_q
);
3308 TAILQ_REMOVE(&zdplane_info
.dg_update_ctx_q
, ctx
,
3311 ctx
->zd_provider
= prov
->dp_id
;
3313 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
3321 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, counter
,
3322 memory_order_relaxed
);
3324 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3325 zlog_debug("dplane: incoming new work counter: %d", counter
);
3327 /* Iterate through the registered providers, offering new incoming
3328 * work. If the provider has outgoing work in its queue, take that
3329 * work for the next provider
3333 /* At each iteration, the temporary work list has 'counter'
3336 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3337 zlog_debug("dplane enqueues %d new work to provider '%s'",
3338 counter
, dplane_provider_get_name(prov
));
3340 /* Capture current provider id in each context; check for
3343 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, tctx
) {
3344 if (dplane_ctx_get_status(ctx
) ==
3345 ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3346 ctx
->zd_provider
= prov
->dp_id
;
3349 * TODO -- improve error-handling: recirc
3350 * errors backwards so that providers can
3351 * 'undo' their work (if they want to)
3354 /* Move to error list; will be returned
3357 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
3358 TAILQ_INSERT_TAIL(&error_list
,
3364 /* Enqueue new work to the provider */
3365 dplane_provider_lock(prov
);
3367 if (TAILQ_FIRST(&work_list
))
3368 TAILQ_CONCAT(&(prov
->dp_ctx_in_q
), &work_list
,
3371 atomic_fetch_add_explicit(&prov
->dp_in_counter
, counter
,
3372 memory_order_relaxed
);
3373 atomic_fetch_add_explicit(&prov
->dp_in_queued
, counter
,
3374 memory_order_relaxed
);
3375 curr
= atomic_load_explicit(&prov
->dp_in_queued
,
3376 memory_order_relaxed
);
3377 high
= atomic_load_explicit(&prov
->dp_in_max
,
3378 memory_order_relaxed
);
3380 atomic_store_explicit(&prov
->dp_in_max
, curr
,
3381 memory_order_relaxed
);
3383 dplane_provider_unlock(prov
);
3385 /* Reset the temp list (though the 'concat' may have done this
3386 * already), and the counter
3388 TAILQ_INIT(&work_list
);
3391 /* Call into the provider code. Note that this is
3392 * unconditional: we offer to do work even if we don't enqueue
3395 (*prov
->dp_fp
)(prov
);
3397 /* Check for zebra shutdown */
3398 if (!zdplane_info
.dg_run
)
3401 /* Dequeue completed work from the provider */
3402 dplane_provider_lock(prov
);
3404 while (counter
< limit
) {
3405 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
3407 TAILQ_REMOVE(&(prov
->dp_ctx_out_q
), ctx
,
3410 TAILQ_INSERT_TAIL(&work_list
,
3417 dplane_provider_unlock(prov
);
3419 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3420 zlog_debug("dplane dequeues %d completed work from provider %s",
3421 counter
, dplane_provider_get_name(prov
));
3423 /* Locate next provider */
3425 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
3429 /* After all providers have been serviced, enqueue any completed
3430 * work and any errors back to zebra so it can process the results.
3432 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3433 zlog_debug("dplane has %d completed, %d errors, for zebra main",
3434 counter
, error_counter
);
3437 * Hand lists through the api to zebra main,
3438 * to reduce the number of lock/unlock cycles
3441 /* Call through to zebra main */
3442 (zdplane_info
.dg_results_cb
)(&error_list
);
3444 TAILQ_INIT(&error_list
);
3446 /* Call through to zebra main */
3447 (zdplane_info
.dg_results_cb
)(&work_list
);
3449 TAILQ_INIT(&work_list
);
3456 * Final phase of shutdown, after all work enqueued to dplane has been
3457 * processed. This is called from the zebra main pthread context.
3459 void zebra_dplane_shutdown(void)
3461 if (IS_ZEBRA_DEBUG_DPLANE
)
3462 zlog_debug("Zebra dataplane shutdown called");
3464 /* Stop dplane thread, if it's running */
3466 zdplane_info
.dg_run
= false;
3468 THREAD_OFF(zdplane_info
.dg_t_update
);
3470 frr_pthread_stop(zdplane_info
.dg_pthread
, NULL
);
3472 /* Destroy pthread */
3473 frr_pthread_destroy(zdplane_info
.dg_pthread
);
3474 zdplane_info
.dg_pthread
= NULL
;
3475 zdplane_info
.dg_master
= NULL
;
3477 /* TODO -- Notify provider(s) of final shutdown */
3479 /* TODO -- Clean-up provider objects */
3481 /* TODO -- Clean queue(s), free memory */
3485 * Initialize the dataplane module during startup, internal/private version
3487 static void zebra_dplane_init_internal(void)
3489 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
3491 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
3493 TAILQ_INIT(&zdplane_info
.dg_update_ctx_q
);
3494 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
3496 zdplane_info
.dg_updates_per_cycle
= DPLANE_DEFAULT_NEW_WORK
;
3498 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
3500 /* Register default kernel 'provider' during init */
3501 dplane_provider_init();
3505 * Start the dataplane pthread. This step needs to be run later than the
3506 * 'init' step, in case zebra has fork-ed.
3508 void zebra_dplane_start(void)
3510 struct zebra_dplane_provider
*prov
;
3511 struct frr_pthread_attr pattr
= {
3512 .start
= frr_pthread_attr_default
.start
,
3513 .stop
= frr_pthread_attr_default
.stop
3516 /* Start dataplane pthread */
3518 zdplane_info
.dg_pthread
= frr_pthread_new(&pattr
, "Zebra dplane thread",
3521 zdplane_info
.dg_master
= zdplane_info
.dg_pthread
->master
;
3523 zdplane_info
.dg_run
= true;
3525 /* Enqueue an initial event for the dataplane pthread */
3526 thread_add_event(zdplane_info
.dg_master
, dplane_thread_loop
, NULL
, 0,
3527 &zdplane_info
.dg_t_update
);
3529 /* Call start callbacks for registered providers */
3532 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
3538 (prov
->dp_start
)(prov
);
3540 /* Locate next provider */
3542 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
3546 frr_pthread_run(zdplane_info
.dg_pthread
, NULL
);
3550 * Initialize the dataplane module at startup; called by zebra rib_init()
3552 void zebra_dplane_init(int (*results_fp
)(struct dplane_ctx_q
*))
3554 zebra_dplane_init_internal();
3555 zdplane_info
.dg_results_cb
= results_fp
;