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/netconf_netlink.h"
32 #include "zebra/zebra_router.h"
33 #include "zebra/zebra_dplane.h"
34 #include "zebra/zebra_vxlan_private.h"
35 #include "zebra/zebra_mpls.h"
37 #include "zebra/debug.h"
38 #include "zebra/zebra_pbr.h"
42 DEFINE_MTYPE_STATIC(ZEBRA
, DP_CTX
, "Zebra DPlane Ctx");
43 DEFINE_MTYPE_STATIC(ZEBRA
, DP_INTF
, "Zebra DPlane Intf");
44 DEFINE_MTYPE_STATIC(ZEBRA
, DP_PROV
, "Zebra DPlane Provider");
45 DEFINE_MTYPE_STATIC(ZEBRA
, DP_NETFILTER
, "Zebra Netfilter Internal Object");
46 DEFINE_MTYPE_STATIC(ZEBRA
, DP_NS
, "DPlane NSes");
52 /* Control for collection of extra interface info with route updates; a plugin
53 * can enable the extra info via a dplane api.
55 static bool dplane_collect_extra_intf_info
;
57 /* Enable test dataplane provider */
58 /*#define DPLANE_TEST_PROVIDER 1 */
60 /* Default value for max queued incoming updates */
61 const uint32_t DPLANE_DEFAULT_MAX_QUEUED
= 200;
63 /* Default value for new work per cycle */
64 const uint32_t DPLANE_DEFAULT_NEW_WORK
= 100;
66 /* Validation check macro for context blocks */
67 /* #define DPLANE_DEBUG 1 */
71 # define DPLANE_CTX_VALID(p) \
76 # define DPLANE_CTX_VALID(p)
78 #endif /* DPLANE_DEBUG */
81 * Nexthop information captured for nexthop/nexthop group updates
83 struct dplane_nexthop_info
{
90 struct nexthop_group ng
;
91 struct nh_grp nh_grp
[MULTIPATH_NUM
];
96 * Optional extra info about interfaces used in route updates' nexthops.
98 struct dplane_intf_extra
{
104 TAILQ_ENTRY(dplane_intf_extra
) link
;
108 * Route information captured for route updates.
110 struct dplane_route_info
{
112 /* Dest and (optional) source prefixes */
113 struct prefix zd_dest
;
114 struct prefix zd_src
;
123 route_tag_t zd_old_tag
;
125 uint32_t zd_old_metric
;
127 uint16_t zd_instance
;
128 uint16_t zd_old_instance
;
131 uint8_t zd_old_distance
;
134 uint32_t zd_nexthop_mtu
;
136 /* Nexthop hash entry info */
137 struct dplane_nexthop_info nhe
;
141 struct nexthop_group zd_ng
;
143 /* Backup nexthops (if present) */
144 struct nexthop_group backup_ng
;
146 /* "Previous" nexthops, used only in route updates without netlink */
147 struct nexthop_group zd_old_ng
;
148 struct nexthop_group old_backup_ng
;
150 /* Optional list of extra interface info */
151 TAILQ_HEAD(dp_intf_extra_q
, dplane_intf_extra
) intf_extra_q
;
155 * Pseudowire info for the dataplane
157 struct dplane_pw_info
{
164 mpls_label_t local_label
;
165 mpls_label_t remote_label
;
167 /* Nexthops that are valid and installed */
168 struct nexthop_group fib_nhg
;
170 /* Primary and backup nexthop sets, copied from the resolving route. */
171 struct nexthop_group primary_nhg
;
172 struct nexthop_group backup_nhg
;
174 union pw_protocol_fields fields
;
178 * Bridge port info for the dataplane
180 struct dplane_br_port_info
{
181 uint32_t sph_filter_cnt
;
182 struct in_addr sph_filters
[ES_VTEP_MAX_CNT
];
183 /* DPLANE_BR_PORT_XXX - see zebra_dplane.h*/
185 uint32_t backup_nhg_id
;
189 * Interface/prefix info for the dataplane
191 struct dplane_intf_info
{
196 #define DPLANE_INTF_CONNECTED (1 << 0) /* Connected peer, p2p */
197 #define DPLANE_INTF_SECONDARY (1 << 1)
198 #define DPLANE_INTF_BROADCAST (1 << 2)
199 #define DPLANE_INTF_HAS_DEST DPLANE_INTF_CONNECTED
200 #define DPLANE_INTF_HAS_LABEL (1 << 4)
202 /* Interface address/prefix */
203 struct prefix prefix
;
205 /* Dest address, for p2p, or broadcast prefix */
206 struct prefix dest_prefix
;
213 * EVPN MAC address info for the dataplane.
215 struct dplane_mac_info
{
217 ifindex_t br_ifindex
;
219 struct in_addr vtep_ip
;
222 uint32_t update_flags
;
226 * Neighbor info for the dataplane
228 struct dplane_neigh_info
{
229 struct ipaddr ip_addr
;
232 struct ipaddr ip_addr
;
236 uint32_t update_flags
;
242 struct dplane_neigh_table
{
245 uint32_t ucast_probes
;
246 uint32_t mcast_probes
;
250 * Policy based routing rule info for the dataplane
252 struct dplane_ctx_rule
{
255 /* The route table pointed by this rule */
258 /* Filter criteria */
262 struct prefix src_ip
;
263 struct prefix dst_ip
;
267 uint16_t action_vlan_id
;
268 uint16_t action_vlan_flags
;
270 uint32_t action_queue_id
;
272 char ifname
[INTERFACE_NAMSIZ
+ 1];
275 struct dplane_rule_info
{
277 * Originating zclient sock fd, so we can know who to send
285 struct dplane_ctx_rule
new;
286 struct dplane_ctx_rule old
;
289 struct dplane_gre_ctx
{
290 uint32_t link_ifindex
;
292 struct zebra_l2info_gre info
;
297 * Network interface configuration info - aligned with netlink's NETCONF
298 * info. The flags values are public, in the dplane.h file...
300 struct dplane_netconf_info
{
303 enum dplane_netconf_status_e mpls_val
;
304 enum dplane_netconf_status_e mcast_val
;
308 * The context block used to exchange info about route updates across
309 * the boundary between the zebra main context (and pthread) and the
310 * dataplane layer (and pthread).
312 struct zebra_dplane_ctx
{
315 enum dplane_op_e zd_op
;
317 /* Status on return */
318 enum zebra_dplane_result zd_status
;
320 /* Dplane provider id */
321 uint32_t zd_provider
;
323 /* Flags - used by providers, e.g. */
331 /* Some updates may be generated by notifications: allow the
332 * plugin to notice and ignore results from its own notifications.
334 uint32_t zd_notif_provider
;
336 /* TODO -- internal/sub-operation status? */
337 enum zebra_dplane_result zd_remote_status
;
338 enum zebra_dplane_result zd_kernel_status
;
341 uint32_t zd_table_id
;
343 char zd_ifname
[INTERFACE_NAMSIZ
];
344 ifindex_t zd_ifindex
;
346 /* Support info for different kinds of updates */
348 struct dplane_route_info rinfo
;
349 struct zebra_lsp lsp
;
350 struct dplane_pw_info pw
;
351 struct dplane_br_port_info br_port
;
352 struct dplane_intf_info intf
;
353 struct dplane_mac_info macinfo
;
354 struct dplane_neigh_info neigh
;
355 struct dplane_rule_info rule
;
356 struct zebra_pbr_iptable iptable
;
357 struct zebra_pbr_ipset ipset
;
359 struct zebra_pbr_ipset_entry entry
;
360 struct zebra_pbr_ipset_info info
;
362 struct dplane_neigh_table neightable
;
363 struct dplane_gre_ctx gre
;
364 struct dplane_netconf_info netconf
;
367 /* Namespace info, used especially for netlink kernel communication */
368 struct zebra_dplane_info zd_ns_info
;
370 /* Embedded list linkage */
371 TAILQ_ENTRY(zebra_dplane_ctx
) zd_q_entries
;
374 /* Flag that can be set by a pre-kernel provider as a signal that an update
375 * should bypass the kernel.
377 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
381 * Registration block for one dataplane provider.
383 struct zebra_dplane_provider
{
385 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
387 /* Priority, for ordering among providers */
394 pthread_mutex_t dp_mutex
;
396 /* Plugin-provided extra data */
402 int (*dp_start
)(struct zebra_dplane_provider
*prov
);
404 int (*dp_fp
)(struct zebra_dplane_provider
*prov
);
406 int (*dp_fini
)(struct zebra_dplane_provider
*prov
, bool early_p
);
408 _Atomic
uint32_t dp_in_counter
;
409 _Atomic
uint32_t dp_in_queued
;
410 _Atomic
uint32_t dp_in_max
;
411 _Atomic
uint32_t dp_out_counter
;
412 _Atomic
uint32_t dp_out_queued
;
413 _Atomic
uint32_t dp_out_max
;
414 _Atomic
uint32_t dp_error_counter
;
416 /* Queue of contexts inbound to the provider */
417 struct dplane_ctx_q dp_ctx_in_q
;
419 /* Queue of completed contexts outbound from the provider back
420 * towards the dataplane module.
422 struct dplane_ctx_q dp_ctx_out_q
;
424 /* Embedded list linkage for provider objects */
425 TAILQ_ENTRY(zebra_dplane_provider
) dp_prov_link
;
428 /* Declare types for list of zns info objects */
429 PREDECL_DLIST(zns_info_list
);
431 struct dplane_zns_info
{
432 struct zebra_dplane_info info
;
434 /* Request data from the OS */
435 struct thread
*t_request
;
438 struct thread
*t_read
;
441 struct zns_info_list_item link
;
447 static struct zebra_dplane_globals
{
448 /* Mutex to control access to dataplane components */
449 pthread_mutex_t dg_mutex
;
451 /* Results callback registered by zebra 'core' */
452 int (*dg_results_cb
)(struct dplane_ctx_q
*ctxlist
);
454 /* Sentinel for beginning of shutdown */
455 volatile bool dg_is_shutdown
;
457 /* Sentinel for end of shutdown */
458 volatile bool dg_run
;
460 /* Update context queue inbound to the dataplane */
461 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx
) dg_update_ctx_q
;
463 /* Ordered list of providers */
464 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider
) dg_providers_q
;
466 /* List of info about each zns */
467 struct zns_info_list_head dg_zns_list
;
469 /* Counter used to assign internal ids to providers */
470 uint32_t dg_provider_id
;
472 /* Limit number of pending, unprocessed updates */
473 _Atomic
uint32_t dg_max_queued_updates
;
475 /* Control whether system route notifications should be produced. */
476 bool dg_sys_route_notifs
;
478 /* Limit number of new updates dequeued at once, to pace an
481 uint32_t dg_updates_per_cycle
;
483 _Atomic
uint32_t dg_routes_in
;
484 _Atomic
uint32_t dg_routes_queued
;
485 _Atomic
uint32_t dg_routes_queued_max
;
486 _Atomic
uint32_t dg_route_errors
;
487 _Atomic
uint32_t dg_other_errors
;
489 _Atomic
uint32_t dg_nexthops_in
;
490 _Atomic
uint32_t dg_nexthop_errors
;
492 _Atomic
uint32_t dg_lsps_in
;
493 _Atomic
uint32_t dg_lsp_errors
;
495 _Atomic
uint32_t dg_pws_in
;
496 _Atomic
uint32_t dg_pw_errors
;
498 _Atomic
uint32_t dg_br_port_in
;
499 _Atomic
uint32_t dg_br_port_errors
;
501 _Atomic
uint32_t dg_intf_addrs_in
;
502 _Atomic
uint32_t dg_intf_addr_errors
;
504 _Atomic
uint32_t dg_macs_in
;
505 _Atomic
uint32_t dg_mac_errors
;
507 _Atomic
uint32_t dg_neighs_in
;
508 _Atomic
uint32_t dg_neigh_errors
;
510 _Atomic
uint32_t dg_rules_in
;
511 _Atomic
uint32_t dg_rule_errors
;
513 _Atomic
uint32_t dg_update_yields
;
515 _Atomic
uint32_t dg_iptable_in
;
516 _Atomic
uint32_t dg_iptable_errors
;
518 _Atomic
uint32_t dg_ipset_in
;
519 _Atomic
uint32_t dg_ipset_errors
;
520 _Atomic
uint32_t dg_ipset_entry_in
;
521 _Atomic
uint32_t dg_ipset_entry_errors
;
523 _Atomic
uint32_t dg_neightable_in
;
524 _Atomic
uint32_t dg_neightable_errors
;
526 _Atomic
uint32_t dg_gre_set_in
;
527 _Atomic
uint32_t dg_gre_set_errors
;
529 /* Dataplane pthread */
530 struct frr_pthread
*dg_pthread
;
532 /* Event-delivery context 'master' for the dplane */
533 struct thread_master
*dg_master
;
535 /* Event/'thread' pointer for queued updates */
536 struct thread
*dg_t_update
;
538 /* Event pointer for pending shutdown check loop */
539 struct thread
*dg_t_shutdown_check
;
543 /* Instantiate zns list type */
544 DECLARE_DLIST(zns_info_list
, struct dplane_zns_info
, link
);
547 * Lock and unlock for interactions with the zebra 'core' pthread
549 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
550 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
554 * Lock and unlock for individual providers
556 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
557 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
560 static void dplane_thread_loop(struct thread
*event
);
561 static enum zebra_dplane_result
lsp_update_internal(struct zebra_lsp
*lsp
,
562 enum dplane_op_e op
);
563 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
564 enum dplane_op_e op
);
565 static enum zebra_dplane_result
intf_addr_update_internal(
566 const struct interface
*ifp
, const struct connected
*ifc
,
567 enum dplane_op_e op
);
568 static enum zebra_dplane_result
mac_update_common(
569 enum dplane_op_e op
, const struct interface
*ifp
,
570 const struct interface
*br_ifp
,
571 vlanid_t vid
, const struct ethaddr
*mac
,
572 struct in_addr vtep_ip
, bool sticky
, uint32_t nhg_id
,
573 uint32_t update_flags
);
574 static enum zebra_dplane_result
575 neigh_update_internal(enum dplane_op_e op
, const struct interface
*ifp
,
576 const void *link
, int link_family
,
577 const struct ipaddr
*ip
, uint32_t flags
, uint16_t state
,
578 uint32_t update_flags
, int protocol
);
584 /* Obtain thread_master for dataplane thread */
585 struct thread_master
*dplane_get_thread_master(void)
587 return zdplane_info
.dg_master
;
591 * Allocate a dataplane update context
593 struct zebra_dplane_ctx
*dplane_ctx_alloc(void)
595 struct zebra_dplane_ctx
*p
;
597 /* TODO -- just alloc'ing memory, but would like to maintain
600 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx
));
605 /* Enable system route notifications */
606 void dplane_enable_sys_route_notifs(void)
608 zdplane_info
.dg_sys_route_notifs
= true;
612 * Clean up dependent/internal allocations inside a context object
614 static void dplane_ctx_free_internal(struct zebra_dplane_ctx
*ctx
)
616 struct dplane_intf_extra
*if_extra
, *if_tmp
;
619 * Some internal allocations may need to be freed, depending on
620 * the type of info captured in the ctx.
622 switch (ctx
->zd_op
) {
623 case DPLANE_OP_ROUTE_INSTALL
:
624 case DPLANE_OP_ROUTE_UPDATE
:
625 case DPLANE_OP_ROUTE_DELETE
:
626 case DPLANE_OP_SYS_ROUTE_ADD
:
627 case DPLANE_OP_SYS_ROUTE_DELETE
:
628 case DPLANE_OP_ROUTE_NOTIFY
:
630 /* Free allocated nexthops */
631 if (ctx
->u
.rinfo
.zd_ng
.nexthop
) {
632 /* This deals with recursive nexthops too */
633 nexthops_free(ctx
->u
.rinfo
.zd_ng
.nexthop
);
635 ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
638 /* Free backup info also (if present) */
639 if (ctx
->u
.rinfo
.backup_ng
.nexthop
) {
640 /* This deals with recursive nexthops too */
641 nexthops_free(ctx
->u
.rinfo
.backup_ng
.nexthop
);
643 ctx
->u
.rinfo
.backup_ng
.nexthop
= NULL
;
646 if (ctx
->u
.rinfo
.zd_old_ng
.nexthop
) {
647 /* This deals with recursive nexthops too */
648 nexthops_free(ctx
->u
.rinfo
.zd_old_ng
.nexthop
);
650 ctx
->u
.rinfo
.zd_old_ng
.nexthop
= NULL
;
653 if (ctx
->u
.rinfo
.old_backup_ng
.nexthop
) {
654 /* This deals with recursive nexthops too */
655 nexthops_free(ctx
->u
.rinfo
.old_backup_ng
.nexthop
);
657 ctx
->u
.rinfo
.old_backup_ng
.nexthop
= NULL
;
660 /* Optional extra interface info */
661 TAILQ_FOREACH_SAFE(if_extra
, &ctx
->u
.rinfo
.intf_extra_q
,
663 TAILQ_REMOVE(&ctx
->u
.rinfo
.intf_extra_q
, if_extra
,
665 XFREE(MTYPE_DP_INTF
, if_extra
);
670 case DPLANE_OP_NH_INSTALL
:
671 case DPLANE_OP_NH_UPDATE
:
672 case DPLANE_OP_NH_DELETE
: {
673 if (ctx
->u
.rinfo
.nhe
.ng
.nexthop
) {
674 /* This deals with recursive nexthops too */
675 nexthops_free(ctx
->u
.rinfo
.nhe
.ng
.nexthop
);
677 ctx
->u
.rinfo
.nhe
.ng
.nexthop
= NULL
;
682 case DPLANE_OP_LSP_INSTALL
:
683 case DPLANE_OP_LSP_UPDATE
:
684 case DPLANE_OP_LSP_DELETE
:
685 case DPLANE_OP_LSP_NOTIFY
:
687 struct zebra_nhlfe
*nhlfe
;
689 /* Unlink and free allocated NHLFEs */
690 frr_each_safe(nhlfe_list
, &ctx
->u
.lsp
.nhlfe_list
, nhlfe
) {
691 nhlfe_list_del(&ctx
->u
.lsp
.nhlfe_list
, nhlfe
);
692 zebra_mpls_nhlfe_free(nhlfe
);
695 /* Unlink and free allocated backup NHLFEs, if present */
696 frr_each_safe(nhlfe_list
,
697 &(ctx
->u
.lsp
.backup_nhlfe_list
), nhlfe
) {
698 nhlfe_list_del(&ctx
->u
.lsp
.backup_nhlfe_list
,
700 zebra_mpls_nhlfe_free(nhlfe
);
703 /* Clear pointers in lsp struct, in case we're caching
704 * free context structs.
706 nhlfe_list_init(&ctx
->u
.lsp
.nhlfe_list
);
707 ctx
->u
.lsp
.best_nhlfe
= NULL
;
708 nhlfe_list_init(&ctx
->u
.lsp
.backup_nhlfe_list
);
713 case DPLANE_OP_PW_INSTALL
:
714 case DPLANE_OP_PW_UNINSTALL
:
715 /* Free allocated nexthops */
716 if (ctx
->u
.pw
.fib_nhg
.nexthop
) {
717 /* This deals with recursive nexthops too */
718 nexthops_free(ctx
->u
.pw
.fib_nhg
.nexthop
);
720 ctx
->u
.pw
.fib_nhg
.nexthop
= NULL
;
722 if (ctx
->u
.pw
.primary_nhg
.nexthop
) {
723 nexthops_free(ctx
->u
.pw
.primary_nhg
.nexthop
);
725 ctx
->u
.pw
.primary_nhg
.nexthop
= NULL
;
727 if (ctx
->u
.pw
.backup_nhg
.nexthop
) {
728 nexthops_free(ctx
->u
.pw
.backup_nhg
.nexthop
);
730 ctx
->u
.pw
.backup_nhg
.nexthop
= NULL
;
734 case DPLANE_OP_ADDR_INSTALL
:
735 case DPLANE_OP_ADDR_UNINSTALL
:
736 case DPLANE_OP_INTF_ADDR_ADD
:
737 case DPLANE_OP_INTF_ADDR_DEL
:
738 /* Maybe free label string, if allocated */
739 if (ctx
->u
.intf
.label
!= NULL
&&
740 ctx
->u
.intf
.label
!= ctx
->u
.intf
.label_buf
) {
741 XFREE(MTYPE_DP_CTX
, ctx
->u
.intf
.label
);
742 ctx
->u
.intf
.label
= NULL
;
746 case DPLANE_OP_MAC_INSTALL
:
747 case DPLANE_OP_MAC_DELETE
:
748 case DPLANE_OP_NEIGH_INSTALL
:
749 case DPLANE_OP_NEIGH_UPDATE
:
750 case DPLANE_OP_NEIGH_DELETE
:
751 case DPLANE_OP_VTEP_ADD
:
752 case DPLANE_OP_VTEP_DELETE
:
753 case DPLANE_OP_RULE_ADD
:
754 case DPLANE_OP_RULE_DELETE
:
755 case DPLANE_OP_RULE_UPDATE
:
756 case DPLANE_OP_NEIGH_DISCOVER
:
757 case DPLANE_OP_BR_PORT_UPDATE
:
758 case DPLANE_OP_NEIGH_IP_INSTALL
:
759 case DPLANE_OP_NEIGH_IP_DELETE
:
761 case DPLANE_OP_IPSET_ADD
:
762 case DPLANE_OP_IPSET_DELETE
:
765 case DPLANE_OP_IPSET_ENTRY_ADD
:
766 case DPLANE_OP_IPSET_ENTRY_DELETE
:
768 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
770 case DPLANE_OP_IPTABLE_ADD
:
771 case DPLANE_OP_IPTABLE_DELETE
:
772 if (ctx
->u
.iptable
.interface_name_list
) {
773 struct listnode
*node
, *nnode
;
776 for (ALL_LIST_ELEMENTS(
777 ctx
->u
.iptable
.interface_name_list
, node
,
780 ctx
->u
.iptable
.interface_name_list
,
782 XFREE(MTYPE_DP_NETFILTER
, ifname
);
784 list_delete(&ctx
->u
.iptable
.interface_name_list
);
787 case DPLANE_OP_GRE_SET
:
788 case DPLANE_OP_INTF_NETCONFIG
:
794 * Free a dataplane results context.
796 static void dplane_ctx_free(struct zebra_dplane_ctx
**pctx
)
801 DPLANE_CTX_VALID(*pctx
);
803 /* TODO -- just freeing memory, but would like to maintain
807 /* Some internal allocations may need to be freed, depending on
808 * the type of info captured in the ctx.
810 dplane_ctx_free_internal(*pctx
);
812 XFREE(MTYPE_DP_CTX
, *pctx
);
816 * Reset an allocated context object for re-use. All internal allocations are
817 * freed and the context is memset.
819 void dplane_ctx_reset(struct zebra_dplane_ctx
*ctx
)
821 dplane_ctx_free_internal(ctx
);
822 memset(ctx
, 0, sizeof(*ctx
));
826 * Return a context block to the dplane module after processing
828 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
)
830 /* TODO -- maintain pool; for now, just free */
831 dplane_ctx_free(pctx
);
834 /* Enqueue a context block */
835 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
836 const struct zebra_dplane_ctx
*ctx
)
838 TAILQ_INSERT_TAIL(q
, (struct zebra_dplane_ctx
*)ctx
, zd_q_entries
);
841 /* Append a list of context blocks to another list */
842 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
843 struct dplane_ctx_q
*from_list
)
845 if (TAILQ_FIRST(from_list
)) {
846 TAILQ_CONCAT(to_list
, from_list
, zd_q_entries
);
848 /* And clear 'from' list */
849 TAILQ_INIT(from_list
);
853 struct zebra_dplane_ctx
*dplane_ctx_get_head(struct dplane_ctx_q
*q
)
855 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
860 /* Dequeue a context block from the head of a list */
861 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
)
863 struct zebra_dplane_ctx
*ctx
= TAILQ_FIRST(q
);
866 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
872 * Accessors for information from the context object
874 enum zebra_dplane_result
dplane_ctx_get_status(
875 const struct zebra_dplane_ctx
*ctx
)
877 DPLANE_CTX_VALID(ctx
);
879 return ctx
->zd_status
;
882 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
883 enum zebra_dplane_result status
)
885 DPLANE_CTX_VALID(ctx
);
887 ctx
->zd_status
= status
;
890 /* Retrieve last/current provider id */
891 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
)
893 DPLANE_CTX_VALID(ctx
);
894 return ctx
->zd_provider
;
897 /* Providers run before the kernel can control whether a kernel
898 * update should be done.
900 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
)
902 DPLANE_CTX_VALID(ctx
);
904 SET_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
907 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
)
909 DPLANE_CTX_VALID(ctx
);
911 return CHECK_FLAG(ctx
->zd_flags
, DPLANE_CTX_FLAG_NO_KERNEL
);
914 void dplane_ctx_set_op(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
)
916 DPLANE_CTX_VALID(ctx
);
920 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
)
922 DPLANE_CTX_VALID(ctx
);
927 const char *dplane_op2str(enum dplane_op_e op
)
929 const char *ret
= "UNKNOWN";
937 case DPLANE_OP_ROUTE_INSTALL
:
938 ret
= "ROUTE_INSTALL";
940 case DPLANE_OP_ROUTE_UPDATE
:
941 ret
= "ROUTE_UPDATE";
943 case DPLANE_OP_ROUTE_DELETE
:
944 ret
= "ROUTE_DELETE";
946 case DPLANE_OP_ROUTE_NOTIFY
:
947 ret
= "ROUTE_NOTIFY";
951 case DPLANE_OP_NH_INSTALL
:
954 case DPLANE_OP_NH_UPDATE
:
957 case DPLANE_OP_NH_DELETE
:
961 case DPLANE_OP_LSP_INSTALL
:
964 case DPLANE_OP_LSP_UPDATE
:
967 case DPLANE_OP_LSP_DELETE
:
970 case DPLANE_OP_LSP_NOTIFY
:
974 case DPLANE_OP_PW_INSTALL
:
977 case DPLANE_OP_PW_UNINSTALL
:
978 ret
= "PW_UNINSTALL";
981 case DPLANE_OP_SYS_ROUTE_ADD
:
982 ret
= "SYS_ROUTE_ADD";
984 case DPLANE_OP_SYS_ROUTE_DELETE
:
985 ret
= "SYS_ROUTE_DEL";
988 case DPLANE_OP_BR_PORT_UPDATE
:
989 ret
= "BR_PORT_UPDATE";
992 case DPLANE_OP_ADDR_INSTALL
:
993 ret
= "ADDR_INSTALL";
995 case DPLANE_OP_ADDR_UNINSTALL
:
996 ret
= "ADDR_UNINSTALL";
999 case DPLANE_OP_MAC_INSTALL
:
1000 ret
= "MAC_INSTALL";
1002 case DPLANE_OP_MAC_DELETE
:
1006 case DPLANE_OP_NEIGH_INSTALL
:
1007 ret
= "NEIGH_INSTALL";
1009 case DPLANE_OP_NEIGH_UPDATE
:
1010 ret
= "NEIGH_UPDATE";
1012 case DPLANE_OP_NEIGH_DELETE
:
1013 ret
= "NEIGH_DELETE";
1015 case DPLANE_OP_VTEP_ADD
:
1018 case DPLANE_OP_VTEP_DELETE
:
1019 ret
= "VTEP_DELETE";
1022 case DPLANE_OP_RULE_ADD
:
1025 case DPLANE_OP_RULE_DELETE
:
1026 ret
= "RULE_DELETE";
1028 case DPLANE_OP_RULE_UPDATE
:
1029 ret
= "RULE_UPDATE";
1032 case DPLANE_OP_NEIGH_DISCOVER
:
1033 ret
= "NEIGH_DISCOVER";
1036 case DPLANE_OP_IPTABLE_ADD
:
1037 ret
= "IPTABLE_ADD";
1039 case DPLANE_OP_IPTABLE_DELETE
:
1040 ret
= "IPTABLE_DELETE";
1042 case DPLANE_OP_IPSET_ADD
:
1045 case DPLANE_OP_IPSET_DELETE
:
1046 ret
= "IPSET_DELETE";
1048 case DPLANE_OP_IPSET_ENTRY_ADD
:
1049 ret
= "IPSET_ENTRY_ADD";
1051 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1052 ret
= "IPSET_ENTRY_DELETE";
1054 case DPLANE_OP_NEIGH_IP_INSTALL
:
1055 ret
= "NEIGH_IP_INSTALL";
1057 case DPLANE_OP_NEIGH_IP_DELETE
:
1058 ret
= "NEIGH_IP_DELETE";
1060 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1061 ret
= "NEIGH_TABLE_UPDATE";
1064 case DPLANE_OP_GRE_SET
:
1068 case DPLANE_OP_INTF_ADDR_ADD
:
1069 return "INTF_ADDR_ADD";
1071 case DPLANE_OP_INTF_ADDR_DEL
:
1072 return "INTF_ADDR_DEL";
1074 case DPLANE_OP_INTF_NETCONFIG
:
1075 return "INTF_NETCONFIG";
1081 const char *dplane_res2str(enum zebra_dplane_result res
)
1083 const char *ret
= "<Unknown>";
1086 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1089 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1092 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1100 void dplane_ctx_set_dest(struct zebra_dplane_ctx
*ctx
,
1101 const struct prefix
*dest
)
1103 DPLANE_CTX_VALID(ctx
);
1105 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), dest
);
1108 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
)
1110 DPLANE_CTX_VALID(ctx
);
1112 return &(ctx
->u
.rinfo
.zd_dest
);
1115 void dplane_ctx_set_src(struct zebra_dplane_ctx
*ctx
, const struct prefix
*src
)
1117 DPLANE_CTX_VALID(ctx
);
1120 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src
);
1122 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(struct prefix
));
1125 /* Source prefix is a little special - return NULL for "no src prefix" */
1126 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
)
1128 DPLANE_CTX_VALID(ctx
);
1130 if (ctx
->u
.rinfo
.zd_src
.prefixlen
== 0 &&
1131 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->u
.rinfo
.zd_src
.u
.prefix6
))) {
1134 return &(ctx
->u
.rinfo
.zd_src
);
1138 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
)
1140 DPLANE_CTX_VALID(ctx
);
1142 return ctx
->zd_is_update
;
1145 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
)
1147 DPLANE_CTX_VALID(ctx
);
1152 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
)
1154 DPLANE_CTX_VALID(ctx
);
1156 return ctx
->zd_old_seq
;
1159 void dplane_ctx_set_vrf(struct zebra_dplane_ctx
*ctx
, vrf_id_t vrf
)
1161 DPLANE_CTX_VALID(ctx
);
1163 ctx
->zd_vrf_id
= vrf
;
1166 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
)
1168 DPLANE_CTX_VALID(ctx
);
1170 return ctx
->zd_vrf_id
;
1173 /* In some paths we have only a namespace id */
1174 void dplane_ctx_set_ns_id(struct zebra_dplane_ctx
*ctx
, ns_id_t nsid
)
1176 DPLANE_CTX_VALID(ctx
);
1178 ctx
->zd_ns_info
.ns_id
= nsid
;
1181 ns_id_t
dplane_ctx_get_ns_id(const struct zebra_dplane_ctx
*ctx
)
1183 DPLANE_CTX_VALID(ctx
);
1185 return ctx
->zd_ns_info
.ns_id
;
1188 bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx
*ctx
)
1190 DPLANE_CTX_VALID(ctx
);
1192 return (ctx
->zd_notif_provider
!= 0);
1195 uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx
*ctx
)
1197 DPLANE_CTX_VALID(ctx
);
1199 return ctx
->zd_notif_provider
;
1202 void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx
*ctx
,
1205 DPLANE_CTX_VALID(ctx
);
1207 ctx
->zd_notif_provider
= id
;
1210 const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx
*ctx
)
1212 DPLANE_CTX_VALID(ctx
);
1214 return ctx
->zd_ifname
;
1217 void dplane_ctx_set_ifname(struct zebra_dplane_ctx
*ctx
, const char *ifname
)
1219 DPLANE_CTX_VALID(ctx
);
1224 strlcpy(ctx
->zd_ifname
, ifname
, sizeof(ctx
->zd_ifname
));
1227 ifindex_t
dplane_ctx_get_ifindex(const struct zebra_dplane_ctx
*ctx
)
1229 DPLANE_CTX_VALID(ctx
);
1231 return ctx
->zd_ifindex
;
1234 void dplane_ctx_set_ifindex(struct zebra_dplane_ctx
*ctx
, ifindex_t ifindex
)
1236 DPLANE_CTX_VALID(ctx
);
1238 ctx
->zd_ifindex
= ifindex
;
1241 void dplane_ctx_set_type(struct zebra_dplane_ctx
*ctx
, int type
)
1243 DPLANE_CTX_VALID(ctx
);
1245 ctx
->u
.rinfo
.zd_type
= type
;
1248 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
)
1250 DPLANE_CTX_VALID(ctx
);
1252 return ctx
->u
.rinfo
.zd_type
;
1255 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
)
1257 DPLANE_CTX_VALID(ctx
);
1259 return ctx
->u
.rinfo
.zd_old_type
;
1262 void dplane_ctx_set_afi(struct zebra_dplane_ctx
*ctx
, afi_t afi
)
1264 DPLANE_CTX_VALID(ctx
);
1266 ctx
->u
.rinfo
.zd_afi
= afi
;
1269 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
)
1271 DPLANE_CTX_VALID(ctx
);
1273 return ctx
->u
.rinfo
.zd_afi
;
1276 void dplane_ctx_set_safi(struct zebra_dplane_ctx
*ctx
, safi_t safi
)
1278 DPLANE_CTX_VALID(ctx
);
1280 ctx
->u
.rinfo
.zd_safi
= safi
;
1283 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
)
1285 DPLANE_CTX_VALID(ctx
);
1287 return ctx
->u
.rinfo
.zd_safi
;
1290 void dplane_ctx_set_table(struct zebra_dplane_ctx
*ctx
, uint32_t table
)
1292 DPLANE_CTX_VALID(ctx
);
1294 ctx
->zd_table_id
= table
;
1297 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
)
1299 DPLANE_CTX_VALID(ctx
);
1301 return ctx
->zd_table_id
;
1304 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
)
1306 DPLANE_CTX_VALID(ctx
);
1308 return ctx
->u
.rinfo
.zd_tag
;
1311 void dplane_ctx_set_tag(struct zebra_dplane_ctx
*ctx
, route_tag_t tag
)
1313 DPLANE_CTX_VALID(ctx
);
1315 ctx
->u
.rinfo
.zd_tag
= tag
;
1318 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
)
1320 DPLANE_CTX_VALID(ctx
);
1322 return ctx
->u
.rinfo
.zd_old_tag
;
1325 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
)
1327 DPLANE_CTX_VALID(ctx
);
1329 return ctx
->u
.rinfo
.zd_instance
;
1332 void dplane_ctx_set_instance(struct zebra_dplane_ctx
*ctx
, uint16_t instance
)
1334 DPLANE_CTX_VALID(ctx
);
1336 ctx
->u
.rinfo
.zd_instance
= instance
;
1339 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
)
1341 DPLANE_CTX_VALID(ctx
);
1343 return ctx
->u
.rinfo
.zd_old_instance
;
1346 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
)
1348 DPLANE_CTX_VALID(ctx
);
1350 return ctx
->u
.rinfo
.zd_metric
;
1353 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
)
1355 DPLANE_CTX_VALID(ctx
);
1357 return ctx
->u
.rinfo
.zd_old_metric
;
1360 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
)
1362 DPLANE_CTX_VALID(ctx
);
1364 return ctx
->u
.rinfo
.zd_mtu
;
1367 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
)
1369 DPLANE_CTX_VALID(ctx
);
1371 return ctx
->u
.rinfo
.zd_nexthop_mtu
;
1374 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
)
1376 DPLANE_CTX_VALID(ctx
);
1378 return ctx
->u
.rinfo
.zd_distance
;
1381 void dplane_ctx_set_distance(struct zebra_dplane_ctx
*ctx
, uint8_t distance
)
1383 DPLANE_CTX_VALID(ctx
);
1385 ctx
->u
.rinfo
.zd_distance
= distance
;
1388 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
)
1390 DPLANE_CTX_VALID(ctx
);
1392 return ctx
->u
.rinfo
.zd_old_distance
;
1396 * Set the nexthops associated with a context: note that processing code
1397 * may well expect that nexthops are in canonical (sorted) order, so we
1398 * will enforce that here.
1400 void dplane_ctx_set_nexthops(struct zebra_dplane_ctx
*ctx
, struct nexthop
*nh
)
1402 DPLANE_CTX_VALID(ctx
);
1404 if (ctx
->u
.rinfo
.zd_ng
.nexthop
) {
1405 nexthops_free(ctx
->u
.rinfo
.zd_ng
.nexthop
);
1406 ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
1408 nexthop_group_copy_nh_sorted(&(ctx
->u
.rinfo
.zd_ng
), nh
);
1412 * Set the list of backup nexthops; their ordering is preserved (they're not
1415 void dplane_ctx_set_backup_nhg(struct zebra_dplane_ctx
*ctx
,
1416 const struct nexthop_group
*nhg
)
1418 struct nexthop
*nh
, *last_nh
, *nexthop
;
1420 DPLANE_CTX_VALID(ctx
);
1422 if (ctx
->u
.rinfo
.backup_ng
.nexthop
) {
1423 nexthops_free(ctx
->u
.rinfo
.backup_ng
.nexthop
);
1424 ctx
->u
.rinfo
.backup_ng
.nexthop
= NULL
;
1429 /* Be careful to preserve the order of the backup list */
1430 for (nh
= nhg
->nexthop
; nh
; nh
= nh
->next
) {
1431 nexthop
= nexthop_dup(nh
, NULL
);
1434 NEXTHOP_APPEND(last_nh
, nexthop
);
1436 ctx
->u
.rinfo
.backup_ng
.nexthop
= nexthop
;
1442 uint32_t dplane_ctx_get_nhg_id(const struct zebra_dplane_ctx
*ctx
)
1444 DPLANE_CTX_VALID(ctx
);
1445 return ctx
->u
.rinfo
.zd_nhg_id
;
1448 const struct nexthop_group
*dplane_ctx_get_ng(
1449 const struct zebra_dplane_ctx
*ctx
)
1451 DPLANE_CTX_VALID(ctx
);
1453 return &(ctx
->u
.rinfo
.zd_ng
);
1456 const struct nexthop_group
*
1457 dplane_ctx_get_backup_ng(const struct zebra_dplane_ctx
*ctx
)
1459 DPLANE_CTX_VALID(ctx
);
1461 return &(ctx
->u
.rinfo
.backup_ng
);
1464 const struct nexthop_group
*
1465 dplane_ctx_get_old_ng(const struct zebra_dplane_ctx
*ctx
)
1467 DPLANE_CTX_VALID(ctx
);
1469 return &(ctx
->u
.rinfo
.zd_old_ng
);
1472 const struct nexthop_group
*
1473 dplane_ctx_get_old_backup_ng(const struct zebra_dplane_ctx
*ctx
)
1475 DPLANE_CTX_VALID(ctx
);
1477 return &(ctx
->u
.rinfo
.old_backup_ng
);
1480 const struct zebra_dplane_info
*dplane_ctx_get_ns(
1481 const struct zebra_dplane_ctx
*ctx
)
1483 DPLANE_CTX_VALID(ctx
);
1485 return &(ctx
->zd_ns_info
);
1488 int dplane_ctx_get_ns_sock(const struct zebra_dplane_ctx
*ctx
)
1490 DPLANE_CTX_VALID(ctx
);
1493 return ctx
->zd_ns_info
.sock
;
1499 /* Accessors for nexthop information */
1500 uint32_t dplane_ctx_get_nhe_id(const struct zebra_dplane_ctx
*ctx
)
1502 DPLANE_CTX_VALID(ctx
);
1503 return ctx
->u
.rinfo
.nhe
.id
;
1506 uint32_t dplane_ctx_get_old_nhe_id(const struct zebra_dplane_ctx
*ctx
)
1508 DPLANE_CTX_VALID(ctx
);
1509 return ctx
->u
.rinfo
.nhe
.old_id
;
1512 afi_t
dplane_ctx_get_nhe_afi(const struct zebra_dplane_ctx
*ctx
)
1514 DPLANE_CTX_VALID(ctx
);
1515 return ctx
->u
.rinfo
.nhe
.afi
;
1518 vrf_id_t
dplane_ctx_get_nhe_vrf_id(const struct zebra_dplane_ctx
*ctx
)
1520 DPLANE_CTX_VALID(ctx
);
1521 return ctx
->u
.rinfo
.nhe
.vrf_id
;
1524 int dplane_ctx_get_nhe_type(const struct zebra_dplane_ctx
*ctx
)
1526 DPLANE_CTX_VALID(ctx
);
1527 return ctx
->u
.rinfo
.nhe
.type
;
1530 const struct nexthop_group
*
1531 dplane_ctx_get_nhe_ng(const struct zebra_dplane_ctx
*ctx
)
1533 DPLANE_CTX_VALID(ctx
);
1534 return &(ctx
->u
.rinfo
.nhe
.ng
);
1537 const struct nh_grp
*
1538 dplane_ctx_get_nhe_nh_grp(const struct zebra_dplane_ctx
*ctx
)
1540 DPLANE_CTX_VALID(ctx
);
1541 return ctx
->u
.rinfo
.nhe
.nh_grp
;
1544 uint8_t dplane_ctx_get_nhe_nh_grp_count(const struct zebra_dplane_ctx
*ctx
)
1546 DPLANE_CTX_VALID(ctx
);
1547 return ctx
->u
.rinfo
.nhe
.nh_grp_count
;
1550 /* Accessors for LSP information */
1552 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
)
1554 DPLANE_CTX_VALID(ctx
);
1556 return ctx
->u
.lsp
.ile
.in_label
;
1559 void dplane_ctx_set_in_label(struct zebra_dplane_ctx
*ctx
, mpls_label_t label
)
1561 DPLANE_CTX_VALID(ctx
);
1563 ctx
->u
.lsp
.ile
.in_label
= label
;
1566 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
)
1568 DPLANE_CTX_VALID(ctx
);
1570 return ctx
->u
.lsp
.addr_family
;
1573 void dplane_ctx_set_addr_family(struct zebra_dplane_ctx
*ctx
,
1576 DPLANE_CTX_VALID(ctx
);
1578 ctx
->u
.lsp
.addr_family
= family
;
1581 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
)
1583 DPLANE_CTX_VALID(ctx
);
1585 return ctx
->u
.lsp
.flags
;
1588 void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx
*ctx
,
1591 DPLANE_CTX_VALID(ctx
);
1593 ctx
->u
.lsp
.flags
= flags
;
1596 const struct nhlfe_list_head
*dplane_ctx_get_nhlfe_list(
1597 const struct zebra_dplane_ctx
*ctx
)
1599 DPLANE_CTX_VALID(ctx
);
1600 return &(ctx
->u
.lsp
.nhlfe_list
);
1603 const struct nhlfe_list_head
*dplane_ctx_get_backup_nhlfe_list(
1604 const struct zebra_dplane_ctx
*ctx
)
1606 DPLANE_CTX_VALID(ctx
);
1607 return &(ctx
->u
.lsp
.backup_nhlfe_list
);
1610 struct zebra_nhlfe
*dplane_ctx_add_nhlfe(struct zebra_dplane_ctx
*ctx
,
1611 enum lsp_types_t lsp_type
,
1612 enum nexthop_types_t nh_type
,
1613 const union g_addr
*gate
,
1614 ifindex_t ifindex
, uint8_t num_labels
,
1615 mpls_label_t
*out_labels
)
1617 struct zebra_nhlfe
*nhlfe
;
1619 DPLANE_CTX_VALID(ctx
);
1621 nhlfe
= zebra_mpls_lsp_add_nhlfe(&(ctx
->u
.lsp
),
1622 lsp_type
, nh_type
, gate
,
1623 ifindex
, num_labels
, out_labels
);
1628 struct zebra_nhlfe
*dplane_ctx_add_backup_nhlfe(
1629 struct zebra_dplane_ctx
*ctx
, enum lsp_types_t lsp_type
,
1630 enum nexthop_types_t nh_type
, const union g_addr
*gate
,
1631 ifindex_t ifindex
, uint8_t num_labels
, mpls_label_t
*out_labels
)
1633 struct zebra_nhlfe
*nhlfe
;
1635 DPLANE_CTX_VALID(ctx
);
1637 nhlfe
= zebra_mpls_lsp_add_backup_nhlfe(&(ctx
->u
.lsp
),
1638 lsp_type
, nh_type
, gate
,
1639 ifindex
, num_labels
,
1645 const struct zebra_nhlfe
*
1646 dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx
*ctx
)
1648 DPLANE_CTX_VALID(ctx
);
1650 return ctx
->u
.lsp
.best_nhlfe
;
1653 const struct zebra_nhlfe
*
1654 dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx
*ctx
,
1655 struct zebra_nhlfe
*nhlfe
)
1657 DPLANE_CTX_VALID(ctx
);
1659 ctx
->u
.lsp
.best_nhlfe
= nhlfe
;
1660 return ctx
->u
.lsp
.best_nhlfe
;
1663 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
)
1665 DPLANE_CTX_VALID(ctx
);
1667 return ctx
->u
.lsp
.num_ecmp
;
1670 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
)
1672 DPLANE_CTX_VALID(ctx
);
1674 return ctx
->u
.pw
.local_label
;
1677 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
)
1679 DPLANE_CTX_VALID(ctx
);
1681 return ctx
->u
.pw
.remote_label
;
1684 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
)
1686 DPLANE_CTX_VALID(ctx
);
1688 return ctx
->u
.pw
.type
;
1691 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
)
1693 DPLANE_CTX_VALID(ctx
);
1695 return ctx
->u
.pw
.af
;
1698 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
)
1700 DPLANE_CTX_VALID(ctx
);
1702 return ctx
->u
.pw
.flags
;
1705 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
)
1707 DPLANE_CTX_VALID(ctx
);
1709 return ctx
->u
.pw
.status
;
1712 void dplane_ctx_set_pw_status(struct zebra_dplane_ctx
*ctx
, int status
)
1714 DPLANE_CTX_VALID(ctx
);
1716 ctx
->u
.pw
.status
= status
;
1719 const union g_addr
*dplane_ctx_get_pw_dest(
1720 const struct zebra_dplane_ctx
*ctx
)
1722 DPLANE_CTX_VALID(ctx
);
1724 return &(ctx
->u
.pw
.dest
);
1727 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
1728 const struct zebra_dplane_ctx
*ctx
)
1730 DPLANE_CTX_VALID(ctx
);
1732 return &(ctx
->u
.pw
.fields
);
1735 const struct nexthop_group
*
1736 dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx
*ctx
)
1738 DPLANE_CTX_VALID(ctx
);
1740 return &(ctx
->u
.pw
.fib_nhg
);
1743 const struct nexthop_group
*
1744 dplane_ctx_get_pw_primary_nhg(const struct zebra_dplane_ctx
*ctx
)
1746 DPLANE_CTX_VALID(ctx
);
1748 return &(ctx
->u
.pw
.primary_nhg
);
1751 const struct nexthop_group
*
1752 dplane_ctx_get_pw_backup_nhg(const struct zebra_dplane_ctx
*ctx
)
1754 DPLANE_CTX_VALID(ctx
);
1756 return &(ctx
->u
.pw
.backup_nhg
);
1759 /* Accessors for interface information */
1760 uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx
*ctx
)
1762 DPLANE_CTX_VALID(ctx
);
1764 return ctx
->u
.intf
.metric
;
1767 void dplane_ctx_set_intf_metric(struct zebra_dplane_ctx
*ctx
, uint32_t metric
)
1769 DPLANE_CTX_VALID(ctx
);
1771 ctx
->u
.intf
.metric
= metric
;
1774 /* Is interface addr p2p? */
1775 bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx
*ctx
)
1777 DPLANE_CTX_VALID(ctx
);
1779 return (ctx
->u
.intf
.flags
& DPLANE_INTF_CONNECTED
);
1782 bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx
*ctx
)
1784 DPLANE_CTX_VALID(ctx
);
1786 return (ctx
->u
.intf
.flags
& DPLANE_INTF_SECONDARY
);
1789 bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx
*ctx
)
1791 DPLANE_CTX_VALID(ctx
);
1793 return (ctx
->u
.intf
.flags
& DPLANE_INTF_BROADCAST
);
1796 void dplane_ctx_intf_set_connected(struct zebra_dplane_ctx
*ctx
)
1798 DPLANE_CTX_VALID(ctx
);
1800 ctx
->u
.intf
.flags
|= DPLANE_INTF_CONNECTED
;
1803 void dplane_ctx_intf_set_secondary(struct zebra_dplane_ctx
*ctx
)
1805 DPLANE_CTX_VALID(ctx
);
1807 ctx
->u
.intf
.flags
|= DPLANE_INTF_SECONDARY
;
1810 void dplane_ctx_intf_set_broadcast(struct zebra_dplane_ctx
*ctx
)
1812 DPLANE_CTX_VALID(ctx
);
1814 ctx
->u
.intf
.flags
|= DPLANE_INTF_BROADCAST
;
1817 const struct prefix
*dplane_ctx_get_intf_addr(
1818 const struct zebra_dplane_ctx
*ctx
)
1820 DPLANE_CTX_VALID(ctx
);
1822 return &(ctx
->u
.intf
.prefix
);
1825 void dplane_ctx_set_intf_addr(struct zebra_dplane_ctx
*ctx
,
1826 const struct prefix
*p
)
1828 DPLANE_CTX_VALID(ctx
);
1830 prefix_copy(&(ctx
->u
.intf
.prefix
), p
);
1833 bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx
*ctx
)
1835 DPLANE_CTX_VALID(ctx
);
1837 return (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_DEST
);
1840 const struct prefix
*dplane_ctx_get_intf_dest(
1841 const struct zebra_dplane_ctx
*ctx
)
1843 DPLANE_CTX_VALID(ctx
);
1845 return &(ctx
->u
.intf
.dest_prefix
);
1848 void dplane_ctx_set_intf_dest(struct zebra_dplane_ctx
*ctx
,
1849 const struct prefix
*p
)
1851 DPLANE_CTX_VALID(ctx
);
1853 prefix_copy(&(ctx
->u
.intf
.dest_prefix
), p
);
1856 bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx
*ctx
)
1858 DPLANE_CTX_VALID(ctx
);
1860 return (ctx
->u
.intf
.flags
& DPLANE_INTF_HAS_LABEL
);
1863 const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx
*ctx
)
1865 DPLANE_CTX_VALID(ctx
);
1867 return ctx
->u
.intf
.label
;
1870 void dplane_ctx_set_intf_label(struct zebra_dplane_ctx
*ctx
, const char *label
)
1874 DPLANE_CTX_VALID(ctx
);
1876 if (ctx
->u
.intf
.label
&& ctx
->u
.intf
.label
!= ctx
->u
.intf
.label_buf
)
1877 XFREE(MTYPE_DP_CTX
, ctx
->u
.intf
.label
);
1879 ctx
->u
.intf
.label
= NULL
;
1882 ctx
->u
.intf
.flags
|= DPLANE_INTF_HAS_LABEL
;
1884 /* Use embedded buffer if it's adequate; else allocate. */
1885 len
= strlen(label
);
1887 if (len
< sizeof(ctx
->u
.intf
.label_buf
)) {
1888 strlcpy(ctx
->u
.intf
.label_buf
, label
,
1889 sizeof(ctx
->u
.intf
.label_buf
));
1890 ctx
->u
.intf
.label
= ctx
->u
.intf
.label_buf
;
1892 ctx
->u
.intf
.label
= XSTRDUP(MTYPE_DP_CTX
, label
);
1895 ctx
->u
.intf
.flags
&= ~DPLANE_INTF_HAS_LABEL
;
1899 /* Accessors for MAC information */
1900 vlanid_t
dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx
*ctx
)
1902 DPLANE_CTX_VALID(ctx
);
1903 return ctx
->u
.macinfo
.vid
;
1906 bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx
*ctx
)
1908 DPLANE_CTX_VALID(ctx
);
1909 return ctx
->u
.macinfo
.is_sticky
;
1912 uint32_t dplane_ctx_mac_get_nhg_id(const struct zebra_dplane_ctx
*ctx
)
1914 DPLANE_CTX_VALID(ctx
);
1915 return ctx
->u
.macinfo
.nhg_id
;
1918 uint32_t dplane_ctx_mac_get_update_flags(const struct zebra_dplane_ctx
*ctx
)
1920 DPLANE_CTX_VALID(ctx
);
1921 return ctx
->u
.macinfo
.update_flags
;
1924 const struct ethaddr
*dplane_ctx_mac_get_addr(
1925 const struct zebra_dplane_ctx
*ctx
)
1927 DPLANE_CTX_VALID(ctx
);
1928 return &(ctx
->u
.macinfo
.mac
);
1931 const struct in_addr
*dplane_ctx_mac_get_vtep_ip(
1932 const struct zebra_dplane_ctx
*ctx
)
1934 DPLANE_CTX_VALID(ctx
);
1935 return &(ctx
->u
.macinfo
.vtep_ip
);
1938 ifindex_t
dplane_ctx_mac_get_br_ifindex(const struct zebra_dplane_ctx
*ctx
)
1940 DPLANE_CTX_VALID(ctx
);
1941 return ctx
->u
.macinfo
.br_ifindex
;
1944 /* Accessors for neighbor information */
1945 const struct ipaddr
*dplane_ctx_neigh_get_ipaddr(
1946 const struct zebra_dplane_ctx
*ctx
)
1948 DPLANE_CTX_VALID(ctx
);
1949 return &(ctx
->u
.neigh
.ip_addr
);
1952 const struct ipaddr
*
1953 dplane_ctx_neigh_get_link_ip(const struct zebra_dplane_ctx
*ctx
)
1955 DPLANE_CTX_VALID(ctx
);
1956 return &(ctx
->u
.neigh
.link
.ip_addr
);
1959 const struct ethaddr
*dplane_ctx_neigh_get_mac(
1960 const struct zebra_dplane_ctx
*ctx
)
1962 DPLANE_CTX_VALID(ctx
);
1963 return &(ctx
->u
.neigh
.link
.mac
);
1966 uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx
*ctx
)
1968 DPLANE_CTX_VALID(ctx
);
1969 return ctx
->u
.neigh
.flags
;
1972 uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx
*ctx
)
1974 DPLANE_CTX_VALID(ctx
);
1975 return ctx
->u
.neigh
.state
;
1978 uint32_t dplane_ctx_neigh_get_update_flags(const struct zebra_dplane_ctx
*ctx
)
1980 DPLANE_CTX_VALID(ctx
);
1981 return ctx
->u
.neigh
.update_flags
;
1984 /* Accessor for GRE set */
1986 dplane_ctx_gre_get_link_ifindex(const struct zebra_dplane_ctx
*ctx
)
1988 DPLANE_CTX_VALID(ctx
);
1990 return ctx
->u
.gre
.link_ifindex
;
1994 dplane_ctx_gre_get_mtu(const struct zebra_dplane_ctx
*ctx
)
1996 DPLANE_CTX_VALID(ctx
);
1998 return ctx
->u
.gre
.mtu
;
2001 const struct zebra_l2info_gre
*
2002 dplane_ctx_gre_get_info(const struct zebra_dplane_ctx
*ctx
)
2004 DPLANE_CTX_VALID(ctx
);
2006 return &ctx
->u
.gre
.info
;
2009 /* Accessors for PBR rule information */
2010 int dplane_ctx_rule_get_sock(const struct zebra_dplane_ctx
*ctx
)
2012 DPLANE_CTX_VALID(ctx
);
2014 return ctx
->u
.rule
.sock
;
2017 const char *dplane_ctx_rule_get_ifname(const struct zebra_dplane_ctx
*ctx
)
2019 DPLANE_CTX_VALID(ctx
);
2021 return ctx
->u
.rule
.new.ifname
;
2024 int dplane_ctx_rule_get_unique(const struct zebra_dplane_ctx
*ctx
)
2026 DPLANE_CTX_VALID(ctx
);
2028 return ctx
->u
.rule
.unique
;
2031 int dplane_ctx_rule_get_seq(const struct zebra_dplane_ctx
*ctx
)
2033 DPLANE_CTX_VALID(ctx
);
2035 return ctx
->u
.rule
.seq
;
2038 uint32_t dplane_ctx_rule_get_priority(const struct zebra_dplane_ctx
*ctx
)
2040 DPLANE_CTX_VALID(ctx
);
2042 return ctx
->u
.rule
.new.priority
;
2045 uint32_t dplane_ctx_rule_get_old_priority(const struct zebra_dplane_ctx
*ctx
)
2047 DPLANE_CTX_VALID(ctx
);
2049 return ctx
->u
.rule
.old
.priority
;
2052 uint32_t dplane_ctx_rule_get_table(const struct zebra_dplane_ctx
*ctx
)
2054 DPLANE_CTX_VALID(ctx
);
2056 return ctx
->u
.rule
.new.table
;
2059 uint32_t dplane_ctx_rule_get_old_table(const struct zebra_dplane_ctx
*ctx
)
2061 DPLANE_CTX_VALID(ctx
);
2063 return ctx
->u
.rule
.old
.table
;
2066 uint32_t dplane_ctx_rule_get_filter_bm(const struct zebra_dplane_ctx
*ctx
)
2068 DPLANE_CTX_VALID(ctx
);
2070 return ctx
->u
.rule
.new.filter_bm
;
2073 uint32_t dplane_ctx_rule_get_old_filter_bm(const struct zebra_dplane_ctx
*ctx
)
2075 DPLANE_CTX_VALID(ctx
);
2077 return ctx
->u
.rule
.old
.filter_bm
;
2080 uint32_t dplane_ctx_rule_get_fwmark(const struct zebra_dplane_ctx
*ctx
)
2082 DPLANE_CTX_VALID(ctx
);
2084 return ctx
->u
.rule
.new.fwmark
;
2087 uint32_t dplane_ctx_rule_get_old_fwmark(const struct zebra_dplane_ctx
*ctx
)
2089 DPLANE_CTX_VALID(ctx
);
2091 return ctx
->u
.rule
.old
.fwmark
;
2094 uint8_t dplane_ctx_rule_get_ipproto(const struct zebra_dplane_ctx
*ctx
)
2096 DPLANE_CTX_VALID(ctx
);
2098 return ctx
->u
.rule
.new.ip_proto
;
2101 uint8_t dplane_ctx_rule_get_old_ipproto(const struct zebra_dplane_ctx
*ctx
)
2103 DPLANE_CTX_VALID(ctx
);
2105 return ctx
->u
.rule
.old
.ip_proto
;
2108 uint8_t dplane_ctx_rule_get_dsfield(const struct zebra_dplane_ctx
*ctx
)
2110 DPLANE_CTX_VALID(ctx
);
2112 return ctx
->u
.rule
.new.dsfield
;
2115 uint8_t dplane_ctx_rule_get_old_dsfield(const struct zebra_dplane_ctx
*ctx
)
2117 DPLANE_CTX_VALID(ctx
);
2119 return ctx
->u
.rule
.old
.dsfield
;
2122 const struct prefix
*
2123 dplane_ctx_rule_get_src_ip(const struct zebra_dplane_ctx
*ctx
)
2125 DPLANE_CTX_VALID(ctx
);
2127 return &(ctx
->u
.rule
.new.src_ip
);
2130 const struct prefix
*
2131 dplane_ctx_rule_get_old_src_ip(const struct zebra_dplane_ctx
*ctx
)
2133 DPLANE_CTX_VALID(ctx
);
2135 return &(ctx
->u
.rule
.old
.src_ip
);
2138 const struct prefix
*
2139 dplane_ctx_rule_get_dst_ip(const struct zebra_dplane_ctx
*ctx
)
2141 DPLANE_CTX_VALID(ctx
);
2143 return &(ctx
->u
.rule
.new.dst_ip
);
2146 const struct prefix
*
2147 dplane_ctx_rule_get_old_dst_ip(const struct zebra_dplane_ctx
*ctx
)
2149 DPLANE_CTX_VALID(ctx
);
2151 return &(ctx
->u
.rule
.old
.dst_ip
);
2154 uint32_t dplane_ctx_get_br_port_flags(const struct zebra_dplane_ctx
*ctx
)
2156 DPLANE_CTX_VALID(ctx
);
2158 return ctx
->u
.br_port
.flags
;
2162 dplane_ctx_get_br_port_sph_filter_cnt(const struct zebra_dplane_ctx
*ctx
)
2164 DPLANE_CTX_VALID(ctx
);
2166 return ctx
->u
.br_port
.sph_filter_cnt
;
2169 const struct in_addr
*
2170 dplane_ctx_get_br_port_sph_filters(const struct zebra_dplane_ctx
*ctx
)
2172 DPLANE_CTX_VALID(ctx
);
2174 return ctx
->u
.br_port
.sph_filters
;
2178 dplane_ctx_get_br_port_backup_nhg_id(const struct zebra_dplane_ctx
*ctx
)
2180 DPLANE_CTX_VALID(ctx
);
2182 return ctx
->u
.br_port
.backup_nhg_id
;
2185 /* Accessors for PBR iptable information */
2186 void dplane_ctx_get_pbr_iptable(const struct zebra_dplane_ctx
*ctx
,
2187 struct zebra_pbr_iptable
*table
)
2189 DPLANE_CTX_VALID(ctx
);
2191 memcpy(table
, &ctx
->u
.iptable
, sizeof(struct zebra_pbr_iptable
));
2194 void dplane_ctx_get_pbr_ipset(const struct zebra_dplane_ctx
*ctx
,
2195 struct zebra_pbr_ipset
*ipset
)
2197 DPLANE_CTX_VALID(ctx
);
2201 if (ctx
->zd_op
== DPLANE_OP_IPSET_ENTRY_ADD
||
2202 ctx
->zd_op
== DPLANE_OP_IPSET_ENTRY_DELETE
) {
2203 memset(ipset
, 0, sizeof(struct zebra_pbr_ipset
));
2204 ipset
->type
= ctx
->u
.ipset_entry
.info
.type
;
2205 ipset
->family
= ctx
->u
.ipset_entry
.info
.family
;
2206 memcpy(&ipset
->ipset_name
, &ctx
->u
.ipset_entry
.info
.ipset_name
,
2207 ZEBRA_IPSET_NAME_SIZE
);
2209 memcpy(ipset
, &ctx
->u
.ipset
, sizeof(struct zebra_pbr_ipset
));
2212 void dplane_ctx_get_pbr_ipset_entry(const struct zebra_dplane_ctx
*ctx
,
2213 struct zebra_pbr_ipset_entry
*entry
)
2215 DPLANE_CTX_VALID(ctx
);
2219 memcpy(entry
, &ctx
->u
.ipset_entry
.entry
, sizeof(struct zebra_pbr_ipset_entry
));
2223 * End of dplane context accessors
2226 /* Optional extra info about interfaces in nexthops - a plugin must enable
2229 const struct dplane_intf_extra
*
2230 dplane_ctx_get_intf_extra(const struct zebra_dplane_ctx
*ctx
)
2232 return TAILQ_FIRST(&ctx
->u
.rinfo
.intf_extra_q
);
2235 const struct dplane_intf_extra
*
2236 dplane_ctx_intf_extra_next(const struct zebra_dplane_ctx
*ctx
,
2237 const struct dplane_intf_extra
*ptr
)
2239 return TAILQ_NEXT(ptr
, link
);
2242 vrf_id_t
dplane_intf_extra_get_vrfid(const struct dplane_intf_extra
*ptr
)
2247 uint32_t dplane_intf_extra_get_ifindex(const struct dplane_intf_extra
*ptr
)
2249 return ptr
->ifindex
;
2252 uint32_t dplane_intf_extra_get_flags(const struct dplane_intf_extra
*ptr
)
2257 uint32_t dplane_intf_extra_get_status(const struct dplane_intf_extra
*ptr
)
2263 * End of interface extra info accessors
2266 uint8_t dplane_ctx_neightable_get_family(const struct zebra_dplane_ctx
*ctx
)
2268 DPLANE_CTX_VALID(ctx
);
2270 return ctx
->u
.neightable
.family
;
2274 dplane_ctx_neightable_get_app_probes(const struct zebra_dplane_ctx
*ctx
)
2276 DPLANE_CTX_VALID(ctx
);
2278 return ctx
->u
.neightable
.app_probes
;
2282 dplane_ctx_neightable_get_ucast_probes(const struct zebra_dplane_ctx
*ctx
)
2284 DPLANE_CTX_VALID(ctx
);
2286 return ctx
->u
.neightable
.ucast_probes
;
2290 dplane_ctx_neightable_get_mcast_probes(const struct zebra_dplane_ctx
*ctx
)
2292 DPLANE_CTX_VALID(ctx
);
2294 return ctx
->u
.neightable
.mcast_probes
;
2297 ifindex_t
dplane_ctx_get_netconf_ifindex(const struct zebra_dplane_ctx
*ctx
)
2299 DPLANE_CTX_VALID(ctx
);
2301 return ctx
->u
.netconf
.ifindex
;
2304 ns_id_t
dplane_ctx_get_netconf_ns_id(const struct zebra_dplane_ctx
*ctx
)
2306 DPLANE_CTX_VALID(ctx
);
2308 return ctx
->u
.netconf
.ns_id
;
2311 void dplane_ctx_set_netconf_ifindex(struct zebra_dplane_ctx
*ctx
,
2314 DPLANE_CTX_VALID(ctx
);
2316 ctx
->u
.netconf
.ifindex
= ifindex
;
2319 void dplane_ctx_set_netconf_ns_id(struct zebra_dplane_ctx
*ctx
, ns_id_t ns_id
)
2321 DPLANE_CTX_VALID(ctx
);
2323 ctx
->u
.netconf
.ns_id
= ns_id
;
2326 enum dplane_netconf_status_e
2327 dplane_ctx_get_netconf_mpls(const struct zebra_dplane_ctx
*ctx
)
2329 DPLANE_CTX_VALID(ctx
);
2331 return ctx
->u
.netconf
.mpls_val
;
2334 enum dplane_netconf_status_e
2335 dplane_ctx_get_netconf_mcast(const struct zebra_dplane_ctx
*ctx
)
2337 DPLANE_CTX_VALID(ctx
);
2339 return ctx
->u
.netconf
.mcast_val
;
2342 void dplane_ctx_set_netconf_mpls(struct zebra_dplane_ctx
*ctx
,
2343 enum dplane_netconf_status_e val
)
2345 DPLANE_CTX_VALID(ctx
);
2347 ctx
->u
.netconf
.mpls_val
= val
;
2350 void dplane_ctx_set_netconf_mcast(struct zebra_dplane_ctx
*ctx
,
2351 enum dplane_netconf_status_e val
)
2353 DPLANE_CTX_VALID(ctx
);
2355 ctx
->u
.netconf
.mcast_val
= val
;
2359 * Retrieve the limit on the number of pending, unprocessed updates.
2361 uint32_t dplane_get_in_queue_limit(void)
2363 return atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
2364 memory_order_relaxed
);
2368 * Configure limit on the number of pending, queued updates.
2370 void dplane_set_in_queue_limit(uint32_t limit
, bool set
)
2372 /* Reset to default on 'unset' */
2374 limit
= DPLANE_DEFAULT_MAX_QUEUED
;
2376 atomic_store_explicit(&zdplane_info
.dg_max_queued_updates
, limit
,
2377 memory_order_relaxed
);
2381 * Retrieve the current queue depth of incoming, unprocessed updates
2383 uint32_t dplane_get_in_queue_len(void)
2385 return atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
2386 memory_order_seq_cst
);
2390 * Internal helper that copies information from a zebra ns object; this is
2391 * called in the zebra main pthread context as part of dplane ctx init.
2393 static void ctx_info_from_zns(struct zebra_dplane_info
*ns_info
,
2394 struct zebra_ns
*zns
)
2396 ns_info
->ns_id
= zns
->ns_id
;
2398 #if defined(HAVE_NETLINK)
2399 ns_info
->is_cmd
= true;
2400 ns_info
->sock
= zns
->netlink_dplane_out
.sock
;
2401 ns_info
->seq
= zns
->netlink_dplane_out
.seq
;
2402 #endif /* NETLINK */
2406 * Common dataplane context init with zebra namespace info.
2408 static int dplane_ctx_ns_init(struct zebra_dplane_ctx
*ctx
,
2409 struct zebra_ns
*zns
,
2412 ctx_info_from_zns(&(ctx
->zd_ns_info
), zns
); /* */
2414 ctx
->zd_is_update
= is_update
;
2416 #if defined(HAVE_NETLINK)
2417 /* Increment message counter after copying to context struct - may need
2418 * two messages in some 'update' cases.
2421 zns
->netlink_dplane_out
.seq
+= 2;
2423 zns
->netlink_dplane_out
.seq
++;
2424 #endif /* HAVE_NETLINK */
2430 * Initialize a context block for a route update from zebra data structs.
2432 int dplane_ctx_route_init(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
,
2433 struct route_node
*rn
, struct route_entry
*re
)
2436 const struct route_table
*table
= NULL
;
2437 const struct rib_table_info
*info
;
2438 const struct prefix
*p
, *src_p
;
2439 struct zebra_ns
*zns
;
2440 struct zebra_vrf
*zvrf
;
2441 struct nexthop
*nexthop
;
2442 struct zebra_l3vni
*zl3vni
;
2443 const struct interface
*ifp
;
2444 struct dplane_intf_extra
*if_extra
;
2446 if (!ctx
|| !rn
|| !re
)
2449 TAILQ_INIT(&ctx
->u
.rinfo
.intf_extra_q
);
2452 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2454 ctx
->u
.rinfo
.zd_type
= re
->type
;
2455 ctx
->u
.rinfo
.zd_old_type
= re
->type
;
2457 /* Prefixes: dest, and optional source */
2458 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
2460 prefix_copy(&(ctx
->u
.rinfo
.zd_dest
), p
);
2463 prefix_copy(&(ctx
->u
.rinfo
.zd_src
), src_p
);
2465 memset(&(ctx
->u
.rinfo
.zd_src
), 0, sizeof(ctx
->u
.rinfo
.zd_src
));
2467 ctx
->zd_table_id
= re
->table
;
2469 ctx
->u
.rinfo
.zd_metric
= re
->metric
;
2470 ctx
->u
.rinfo
.zd_old_metric
= re
->metric
;
2471 ctx
->zd_vrf_id
= re
->vrf_id
;
2472 ctx
->u
.rinfo
.zd_mtu
= re
->mtu
;
2473 ctx
->u
.rinfo
.zd_nexthop_mtu
= re
->nexthop_mtu
;
2474 ctx
->u
.rinfo
.zd_instance
= re
->instance
;
2475 ctx
->u
.rinfo
.zd_tag
= re
->tag
;
2476 ctx
->u
.rinfo
.zd_old_tag
= re
->tag
;
2477 ctx
->u
.rinfo
.zd_distance
= re
->distance
;
2479 table
= srcdest_rnode_table(rn
);
2482 ctx
->u
.rinfo
.zd_afi
= info
->afi
;
2483 ctx
->u
.rinfo
.zd_safi
= info
->safi
;
2485 /* Copy nexthops; recursive info is included too */
2486 copy_nexthops(&(ctx
->u
.rinfo
.zd_ng
.nexthop
),
2487 re
->nhe
->nhg
.nexthop
, NULL
);
2488 ctx
->u
.rinfo
.zd_nhg_id
= re
->nhe
->id
;
2490 /* Copy backup nexthop info, if present */
2491 if (re
->nhe
->backup_info
&& re
->nhe
->backup_info
->nhe
) {
2492 copy_nexthops(&(ctx
->u
.rinfo
.backup_ng
.nexthop
),
2493 re
->nhe
->backup_info
->nhe
->nhg
.nexthop
, NULL
);
2497 * Ensure that the dplane nexthops' flags are clear and copy
2498 * encapsulation information.
2500 for (ALL_NEXTHOPS(ctx
->u
.rinfo
.zd_ng
, nexthop
)) {
2501 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2503 /* Optionally capture extra interface info while we're in the
2504 * main zebra pthread - a plugin has to ask for this info.
2506 if (dplane_collect_extra_intf_info
) {
2507 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2513 sizeof(struct dplane_intf_extra
));
2514 if_extra
->vrf_id
= nexthop
->vrf_id
;
2515 if_extra
->ifindex
= nexthop
->ifindex
;
2516 if_extra
->flags
= ifp
->flags
;
2517 if_extra
->status
= ifp
->status
;
2519 TAILQ_INSERT_TAIL(&ctx
->u
.rinfo
.intf_extra_q
,
2524 /* Check for available evpn encapsulations. */
2525 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
2528 zl3vni
= zl3vni_from_vrf(nexthop
->vrf_id
);
2529 if (zl3vni
&& is_l3vni_oper_up(zl3vni
)) {
2530 nexthop
->nh_encap_type
= NET_VXLAN
;
2531 nexthop
->nh_encap
.vni
= zl3vni
->vni
;
2535 /* Don't need some info when capturing a system notification */
2536 if (op
== DPLANE_OP_SYS_ROUTE_ADD
||
2537 op
== DPLANE_OP_SYS_ROUTE_DELETE
) {
2542 /* Extract ns info - can't use pointers to 'core' structs */
2543 zvrf
= vrf_info_lookup(re
->vrf_id
);
2545 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_ROUTE_UPDATE
));
2549 struct nhg_hash_entry
*nhe
= zebra_nhg_resolve(re
->nhe
);
2551 ctx
->u
.rinfo
.nhe
.id
= nhe
->id
;
2552 ctx
->u
.rinfo
.nhe
.old_id
= 0;
2554 * Check if the nhe is installed/queued before doing anything
2557 * If its a delete we only use the prefix anyway, so this only
2558 * matters for INSTALL/UPDATE.
2560 if (zebra_nhg_kernel_nexthops_enabled()
2561 && (((op
== DPLANE_OP_ROUTE_INSTALL
)
2562 || (op
== DPLANE_OP_ROUTE_UPDATE
))
2563 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_INSTALLED
)
2564 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_QUEUED
))) {
2569 re
->nhe_installed_id
= nhe
->id
;
2571 #endif /* HAVE_NETLINK */
2573 /* Trying out the sequence number idea, so we can try to detect
2574 * when a result is stale.
2576 re
->dplane_sequence
= zebra_router_get_next_sequence();
2577 ctx
->zd_seq
= re
->dplane_sequence
;
2586 * dplane_ctx_nexthop_init() - Initialize a context block for a nexthop update
2588 * @ctx: Dataplane context to init
2589 * @op: Operation being performed
2590 * @nhe: Nexthop group hash entry
2592 * Return: Result status
2594 int dplane_ctx_nexthop_init(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
,
2595 struct nhg_hash_entry
*nhe
)
2597 struct zebra_vrf
*zvrf
= NULL
;
2598 struct zebra_ns
*zns
= NULL
;
2605 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2607 /* Copy over nhe info */
2608 ctx
->u
.rinfo
.nhe
.id
= nhe
->id
;
2609 ctx
->u
.rinfo
.nhe
.afi
= nhe
->afi
;
2610 ctx
->u
.rinfo
.nhe
.vrf_id
= nhe
->vrf_id
;
2611 ctx
->u
.rinfo
.nhe
.type
= nhe
->type
;
2613 nexthop_group_copy(&(ctx
->u
.rinfo
.nhe
.ng
), &(nhe
->nhg
));
2615 /* If this is a group, convert it to a grp array of ids */
2616 if (!zebra_nhg_depends_is_empty(nhe
)
2617 && !CHECK_FLAG(nhe
->flags
, NEXTHOP_GROUP_RECURSIVE
))
2618 ctx
->u
.rinfo
.nhe
.nh_grp_count
= zebra_nhg_nhe2grp(
2619 ctx
->u
.rinfo
.nhe
.nh_grp
, nhe
, MULTIPATH_NUM
);
2621 zvrf
= vrf_info_lookup(nhe
->vrf_id
);
2624 * Fallback to default namespace if the vrf got ripped out from under
2627 zns
= zvrf
? zvrf
->zns
: zebra_ns_lookup(NS_DEFAULT
);
2630 * TODO: Might not need to mark this as an update, since
2631 * it probably won't require two messages
2633 dplane_ctx_ns_init(ctx
, zns
, (op
== DPLANE_OP_NH_UPDATE
));
2642 * Capture information for an LSP update in a dplane context.
2644 int dplane_ctx_lsp_init(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
,
2645 struct zebra_lsp
*lsp
)
2648 struct zebra_nhlfe
*nhlfe
, *new_nhlfe
;
2651 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2653 /* Capture namespace info */
2654 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
2655 (op
== DPLANE_OP_LSP_UPDATE
));
2657 memset(&ctx
->u
.lsp
, 0, sizeof(ctx
->u
.lsp
));
2659 nhlfe_list_init(&(ctx
->u
.lsp
.nhlfe_list
));
2660 nhlfe_list_init(&(ctx
->u
.lsp
.backup_nhlfe_list
));
2662 /* This may be called to create/init a dplane context, not necessarily
2663 * to copy an lsp object.
2670 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2671 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
2672 dplane_op2str(op
), lsp
->ile
.in_label
,
2675 ctx
->u
.lsp
.ile
= lsp
->ile
;
2676 ctx
->u
.lsp
.addr_family
= lsp
->addr_family
;
2677 ctx
->u
.lsp
.num_ecmp
= lsp
->num_ecmp
;
2678 ctx
->u
.lsp
.flags
= lsp
->flags
;
2680 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
2681 frr_each(nhlfe_list
, &lsp
->nhlfe_list
, nhlfe
) {
2682 /* Not sure if this is meaningful... */
2683 if (nhlfe
->nexthop
== NULL
)
2686 new_nhlfe
= zebra_mpls_lsp_add_nh(&(ctx
->u
.lsp
), nhlfe
->type
,
2688 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
2693 /* Need to copy flags and backup info too */
2694 new_nhlfe
->flags
= nhlfe
->flags
;
2695 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
2697 if (CHECK_FLAG(new_nhlfe
->nexthop
->flags
,
2698 NEXTHOP_FLAG_HAS_BACKUP
)) {
2699 new_nhlfe
->nexthop
->backup_num
=
2700 nhlfe
->nexthop
->backup_num
;
2701 memcpy(new_nhlfe
->nexthop
->backup_idx
,
2702 nhlfe
->nexthop
->backup_idx
,
2703 new_nhlfe
->nexthop
->backup_num
);
2706 if (nhlfe
== lsp
->best_nhlfe
)
2707 ctx
->u
.lsp
.best_nhlfe
= new_nhlfe
;
2713 /* Capture backup nhlfes/nexthops */
2714 frr_each(nhlfe_list
, &lsp
->backup_nhlfe_list
, nhlfe
) {
2715 /* Not sure if this is meaningful... */
2716 if (nhlfe
->nexthop
== NULL
)
2719 new_nhlfe
= zebra_mpls_lsp_add_backup_nh(&(ctx
->u
.lsp
),
2722 if (new_nhlfe
== NULL
|| new_nhlfe
->nexthop
== NULL
) {
2727 /* Need to copy flags too */
2728 new_nhlfe
->flags
= nhlfe
->flags
;
2729 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
2732 /* On error the ctx will be cleaned-up, so we don't need to
2733 * deal with any allocated nhlfe or nexthop structs here.
2741 * Capture information for an LSP update in a dplane context.
2743 static int dplane_ctx_pw_init(struct zebra_dplane_ctx
*ctx
,
2744 enum dplane_op_e op
,
2745 struct zebra_pw
*pw
)
2750 struct route_table
*table
;
2751 struct route_node
*rn
;
2752 struct route_entry
*re
;
2753 const struct nexthop_group
*nhg
;
2754 struct nexthop
*nh
, *newnh
, *last_nh
;
2756 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2757 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
2758 dplane_op2str(op
), pw
->ifname
, pw
->local_label
,
2762 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2764 /* Capture namespace info: no netlink support as of 12/18,
2765 * but just in case...
2767 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
2769 memset(&ctx
->u
.pw
, 0, sizeof(ctx
->u
.pw
));
2771 /* This name appears to be c-string, so we use string copy. */
2772 strlcpy(ctx
->zd_ifname
, pw
->ifname
, sizeof(ctx
->zd_ifname
));
2774 ctx
->zd_vrf_id
= pw
->vrf_id
;
2775 ctx
->zd_ifindex
= pw
->ifindex
;
2776 ctx
->u
.pw
.type
= pw
->type
;
2777 ctx
->u
.pw
.af
= pw
->af
;
2778 ctx
->u
.pw
.local_label
= pw
->local_label
;
2779 ctx
->u
.pw
.remote_label
= pw
->remote_label
;
2780 ctx
->u
.pw
.flags
= pw
->flags
;
2782 ctx
->u
.pw
.dest
= pw
->nexthop
;
2784 ctx
->u
.pw
.fields
= pw
->data
;
2786 /* Capture nexthop info for the pw destination. We need to look
2787 * up and use zebra datastructs, but we're running in the zebra
2788 * pthread here so that should be ok.
2790 memcpy(&p
.u
, &pw
->nexthop
, sizeof(pw
->nexthop
));
2792 p
.prefixlen
= ((pw
->af
== AF_INET
) ? IPV4_MAX_BITLEN
: IPV6_MAX_BITLEN
);
2794 afi
= (pw
->af
== AF_INET
) ? AFI_IP
: AFI_IP6
;
2795 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, pw
->vrf_id
);
2799 rn
= route_node_match(table
, &p
);
2804 RNODE_FOREACH_RE(rn
, re
) {
2805 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
))
2810 /* We'll capture a 'fib' list of nexthops that meet our
2811 * criteria: installed, and labelled.
2813 nhg
= rib_get_fib_nhg(re
);
2816 if (nhg
&& nhg
->nexthop
) {
2817 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
2818 if (!CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)
2819 || CHECK_FLAG(nh
->flags
,
2820 NEXTHOP_FLAG_RECURSIVE
)
2821 || nh
->nh_label
== NULL
)
2824 newnh
= nexthop_dup(nh
, NULL
);
2827 NEXTHOP_APPEND(last_nh
, newnh
);
2829 ctx
->u
.pw
.fib_nhg
.nexthop
= newnh
;
2834 /* Include any installed backup nexthops also. */
2835 nhg
= rib_get_fib_backup_nhg(re
);
2836 if (nhg
&& nhg
->nexthop
) {
2837 for (ALL_NEXTHOPS_PTR(nhg
, nh
)) {
2838 if (!CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_ACTIVE
)
2839 || CHECK_FLAG(nh
->flags
,
2840 NEXTHOP_FLAG_RECURSIVE
)
2841 || nh
->nh_label
== NULL
)
2844 newnh
= nexthop_dup(nh
, NULL
);
2847 NEXTHOP_APPEND(last_nh
, newnh
);
2849 ctx
->u
.pw
.fib_nhg
.nexthop
= newnh
;
2854 /* Copy primary nexthops; recursive info is included too */
2855 assert(re
->nhe
!= NULL
); /* SA warning */
2856 copy_nexthops(&(ctx
->u
.pw
.primary_nhg
.nexthop
),
2857 re
->nhe
->nhg
.nexthop
, NULL
);
2858 ctx
->u
.pw
.nhg_id
= re
->nhe
->id
;
2860 /* Copy backup nexthop info, if present */
2861 if (re
->nhe
->backup_info
&& re
->nhe
->backup_info
->nhe
) {
2862 copy_nexthops(&(ctx
->u
.pw
.backup_nhg
.nexthop
),
2863 re
->nhe
->backup_info
->nhe
->nhg
.nexthop
,
2867 route_unlock_node(rn
);
2876 * dplane_ctx_rule_init_single() - Initialize a dataplane representation of a
2879 * @dplane_rule: Dataplane internal representation of a rule
2882 static void dplane_ctx_rule_init_single(struct dplane_ctx_rule
*dplane_rule
,
2883 struct zebra_pbr_rule
*rule
)
2885 dplane_rule
->priority
= rule
->rule
.priority
;
2886 dplane_rule
->table
= rule
->rule
.action
.table
;
2888 dplane_rule
->filter_bm
= rule
->rule
.filter
.filter_bm
;
2889 dplane_rule
->fwmark
= rule
->rule
.filter
.fwmark
;
2890 dplane_rule
->dsfield
= rule
->rule
.filter
.dsfield
;
2891 dplane_rule
->ip_proto
= rule
->rule
.filter
.ip_proto
;
2892 prefix_copy(&(dplane_rule
->dst_ip
), &rule
->rule
.filter
.dst_ip
);
2893 prefix_copy(&(dplane_rule
->src_ip
), &rule
->rule
.filter
.src_ip
);
2895 dplane_rule
->action_pcp
= rule
->rule
.action
.pcp
;
2896 dplane_rule
->action_vlan_flags
= rule
->rule
.action
.vlan_flags
;
2897 dplane_rule
->action_vlan_id
= rule
->rule
.action
.vlan_id
;
2898 dplane_rule
->action_queue_id
= rule
->rule
.action
.queue_id
;
2900 strlcpy(dplane_rule
->ifname
, rule
->ifname
, INTERFACE_NAMSIZ
);
2904 * dplane_ctx_rule_init() - Initialize a context block for a PBR rule update.
2906 * @ctx: Dataplane context to init
2907 * @op: Operation being performed
2908 * @new_rule: PBR rule
2910 * Return: Result status
2912 static int dplane_ctx_rule_init(struct zebra_dplane_ctx
*ctx
,
2913 enum dplane_op_e op
,
2914 struct zebra_pbr_rule
*new_rule
,
2915 struct zebra_pbr_rule
*old_rule
)
2917 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2919 "init dplane ctx %s: IF %s Prio %u Fwmark %u Src %pFX Dst %pFX Table %u",
2920 dplane_op2str(op
), new_rule
->ifname
,
2921 new_rule
->rule
.priority
, new_rule
->rule
.filter
.fwmark
,
2922 &new_rule
->rule
.filter
.src_ip
,
2923 &new_rule
->rule
.filter
.dst_ip
,
2924 new_rule
->rule
.action
.table
);
2927 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2929 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
),
2930 op
== DPLANE_OP_RULE_UPDATE
);
2932 ctx
->zd_vrf_id
= new_rule
->vrf_id
;
2933 strlcpy(ctx
->zd_ifname
, new_rule
->ifname
, sizeof(ctx
->zd_ifname
));
2935 ctx
->u
.rule
.sock
= new_rule
->sock
;
2936 ctx
->u
.rule
.unique
= new_rule
->rule
.unique
;
2937 ctx
->u
.rule
.seq
= new_rule
->rule
.seq
;
2939 dplane_ctx_rule_init_single(&ctx
->u
.rule
.new, new_rule
);
2940 if (op
== DPLANE_OP_RULE_UPDATE
)
2941 dplane_ctx_rule_init_single(&ctx
->u
.rule
.old
, old_rule
);
2947 * dplane_ctx_iptable_init() - Initialize a context block for a PBR iptable
2950 * @ctx: Dataplane context to init
2951 * @op: Operation being performed
2952 * @new_rule: PBR iptable
2954 * Return: Result status
2956 static int dplane_ctx_iptable_init(struct zebra_dplane_ctx
*ctx
,
2957 enum dplane_op_e op
,
2958 struct zebra_pbr_iptable
*iptable
)
2961 struct listnode
*node
;
2963 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
2965 "init dplane ctx %s: Unique %u Fwmark %u Family %s Action %s",
2966 dplane_op2str(op
), iptable
->unique
, iptable
->fwmark
,
2967 family2str(iptable
->family
),
2968 iptable
->action
== ZEBRA_IPTABLES_DROP
? "Drop"
2973 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
2975 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
2977 ctx
->zd_vrf_id
= iptable
->vrf_id
;
2978 memcpy(&ctx
->u
.iptable
, iptable
, sizeof(struct zebra_pbr_iptable
));
2979 ctx
->u
.iptable
.interface_name_list
= NULL
;
2980 if (iptable
->nb_interface
> 0) {
2981 ctx
->u
.iptable
.interface_name_list
= list_new();
2982 for (ALL_LIST_ELEMENTS_RO(iptable
->interface_name_list
, node
,
2984 listnode_add(ctx
->u
.iptable
.interface_name_list
,
2985 XSTRDUP(MTYPE_DP_NETFILTER
, ifname
));
2992 * dplane_ctx_ipset_init() - Initialize a context block for a PBR ipset update.
2994 * @ctx: Dataplane context to init
2995 * @op: Operation being performed
2996 * @new_rule: PBR ipset
2998 * Return: Result status
3000 static int dplane_ctx_ipset_init(struct zebra_dplane_ctx
*ctx
,
3001 enum dplane_op_e op
,
3002 struct zebra_pbr_ipset
*ipset
)
3004 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
3005 zlog_debug("init dplane ctx %s: %s Unique %u Family %s Type %s",
3006 dplane_op2str(op
), ipset
->ipset_name
, ipset
->unique
,
3007 family2str(ipset
->family
),
3008 zebra_pbr_ipset_type2str(ipset
->type
));
3012 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3014 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
3016 ctx
->zd_vrf_id
= ipset
->vrf_id
;
3018 memcpy(&ctx
->u
.ipset
, ipset
, sizeof(struct zebra_pbr_ipset
));
3023 * dplane_ctx_ipset_entry_init() - Initialize a context block for a PBR ipset
3026 * @ctx: Dataplane context to init
3027 * @op: Operation being performed
3028 * @new_rule: PBR ipset
3030 * Return: Result status
3033 dplane_ctx_ipset_entry_init(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
,
3034 struct zebra_pbr_ipset_entry
*ipset_entry
)
3036 struct zebra_pbr_ipset
*ipset
;
3038 ipset
= ipset_entry
->backpointer
;
3039 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
3040 zlog_debug("init dplane ctx %s: %s Unique %u filter %u",
3041 dplane_op2str(op
), ipset
->ipset_name
,
3042 ipset_entry
->unique
, ipset_entry
->filter_bm
);
3046 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3048 dplane_ctx_ns_init(ctx
, zebra_ns_lookup(NS_DEFAULT
), false);
3050 ctx
->zd_vrf_id
= ipset
->vrf_id
;
3052 memcpy(&ctx
->u
.ipset_entry
.entry
, ipset_entry
,
3053 sizeof(struct zebra_pbr_ipset_entry
));
3054 ctx
->u
.ipset_entry
.entry
.backpointer
= NULL
;
3055 ctx
->u
.ipset_entry
.info
.type
= ipset
->type
;
3056 ctx
->u
.ipset_entry
.info
.family
= ipset
->family
;
3057 memcpy(&ctx
->u
.ipset_entry
.info
.ipset_name
, &ipset
->ipset_name
,
3058 ZEBRA_IPSET_NAME_SIZE
);
3065 * Enqueue a new update,
3066 * and ensure an event is active for the dataplane pthread.
3068 static int dplane_update_enqueue(struct zebra_dplane_ctx
*ctx
)
3071 uint32_t high
, curr
;
3073 /* Enqueue for processing by the dataplane pthread */
3076 TAILQ_INSERT_TAIL(&zdplane_info
.dg_update_ctx_q
, ctx
,
3081 curr
= atomic_fetch_add_explicit(
3082 &(zdplane_info
.dg_routes_queued
),
3083 1, memory_order_seq_cst
);
3085 curr
++; /* We got the pre-incremented value */
3087 /* Maybe update high-water counter also */
3088 high
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
3089 memory_order_seq_cst
);
3090 while (high
< curr
) {
3091 if (atomic_compare_exchange_weak_explicit(
3092 &zdplane_info
.dg_routes_queued_max
,
3094 memory_order_seq_cst
,
3095 memory_order_seq_cst
))
3099 /* Ensure that an event for the dataplane thread is active */
3100 ret
= dplane_provider_work_ready();
3106 * Utility that prepares a route update and enqueues it for processing
3108 static enum zebra_dplane_result
3109 dplane_route_update_internal(struct route_node
*rn
,
3110 struct route_entry
*re
,
3111 struct route_entry
*old_re
,
3112 enum dplane_op_e op
)
3114 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3116 struct zebra_dplane_ctx
*ctx
= NULL
;
3118 /* Obtain context block */
3119 ctx
= dplane_ctx_alloc();
3121 /* Init context with info from zebra data structs */
3122 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
3124 /* Capture some extra info for update case
3125 * where there's a different 'old' route.
3127 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
3128 old_re
&& (old_re
!= re
)) {
3130 old_re
->dplane_sequence
=
3131 zebra_router_get_next_sequence();
3132 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
3134 ctx
->u
.rinfo
.zd_old_tag
= old_re
->tag
;
3135 ctx
->u
.rinfo
.zd_old_type
= old_re
->type
;
3136 ctx
->u
.rinfo
.zd_old_instance
= old_re
->instance
;
3137 ctx
->u
.rinfo
.zd_old_distance
= old_re
->distance
;
3138 ctx
->u
.rinfo
.zd_old_metric
= old_re
->metric
;
3139 ctx
->u
.rinfo
.nhe
.old_id
= old_re
->nhe
->id
;
3141 #ifndef HAVE_NETLINK
3142 /* For bsd, capture previous re's nexthops too, sigh.
3143 * We'll need these to do per-nexthop deletes.
3145 copy_nexthops(&(ctx
->u
.rinfo
.zd_old_ng
.nexthop
),
3146 old_re
->nhe
->nhg
.nexthop
, NULL
);
3148 if (zebra_nhg_get_backup_nhg(old_re
->nhe
) != NULL
) {
3149 struct nexthop_group
*nhg
;
3150 struct nexthop
**nh
;
3152 nhg
= zebra_nhg_get_backup_nhg(old_re
->nhe
);
3153 nh
= &(ctx
->u
.rinfo
.old_backup_ng
.nexthop
);
3156 copy_nexthops(nh
, nhg
->nexthop
, NULL
);
3158 #endif /* !HAVE_NETLINK */
3162 * If the old and new context type, and nexthop group id
3163 * are the same there is no need to send down a route replace
3164 * as that we know we have sent a nexthop group replace
3165 * or an upper level protocol has sent us the exact
3168 if ((dplane_ctx_get_type(ctx
) == dplane_ctx_get_old_type(ctx
))
3169 && (dplane_ctx_get_nhe_id(ctx
)
3170 == dplane_ctx_get_old_nhe_id(ctx
))
3171 && (dplane_ctx_get_nhe_id(ctx
) >= ZEBRA_NHG_PROTO_LOWER
)) {
3172 struct nexthop
*nexthop
;
3174 if (IS_ZEBRA_DEBUG_DPLANE
)
3176 "%s: Ignoring Route exactly the same",
3179 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
3181 if (CHECK_FLAG(nexthop
->flags
,
3182 NEXTHOP_FLAG_RECURSIVE
))
3185 if (CHECK_FLAG(nexthop
->flags
,
3186 NEXTHOP_FLAG_ACTIVE
))
3187 SET_FLAG(nexthop
->flags
,
3191 dplane_ctx_free(&ctx
);
3192 return ZEBRA_DPLANE_REQUEST_SUCCESS
;
3195 /* Enqueue context for processing */
3196 ret
= dplane_update_enqueue(ctx
);
3199 /* Update counter */
3200 atomic_fetch_add_explicit(&zdplane_info
.dg_routes_in
, 1,
3201 memory_order_relaxed
);
3204 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3206 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
, 1,
3207 memory_order_relaxed
);
3209 dplane_ctx_free(&ctx
);
3216 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
3218 * @nhe: Nexthop group hash entry where the change occured
3219 * @op: The operation to be enqued
3221 * Return: Result of the change
3223 static enum zebra_dplane_result
3224 dplane_nexthop_update_internal(struct nhg_hash_entry
*nhe
, enum dplane_op_e op
)
3226 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3228 struct zebra_dplane_ctx
*ctx
= NULL
;
3230 /* Obtain context block */
3231 ctx
= dplane_ctx_alloc();
3237 ret
= dplane_ctx_nexthop_init(ctx
, op
, nhe
);
3239 ret
= dplane_update_enqueue(ctx
);
3242 /* Update counter */
3243 atomic_fetch_add_explicit(&zdplane_info
.dg_nexthops_in
, 1,
3244 memory_order_relaxed
);
3247 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3249 atomic_fetch_add_explicit(&zdplane_info
.dg_nexthop_errors
, 1,
3250 memory_order_relaxed
);
3252 dplane_ctx_free(&ctx
);
3259 * Enqueue a route 'add' for the dataplane.
3261 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
3262 struct route_entry
*re
)
3264 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3266 if (rn
== NULL
|| re
== NULL
)
3269 ret
= dplane_route_update_internal(rn
, re
, NULL
,
3270 DPLANE_OP_ROUTE_INSTALL
);
3277 * Enqueue a route update for the dataplane.
3279 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
3280 struct route_entry
*re
,
3281 struct route_entry
*old_re
)
3283 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3285 if (rn
== NULL
|| re
== NULL
)
3288 ret
= dplane_route_update_internal(rn
, re
, old_re
,
3289 DPLANE_OP_ROUTE_UPDATE
);
3295 * Enqueue a route removal for the dataplane.
3297 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
3298 struct route_entry
*re
)
3300 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3302 if (rn
== NULL
|| re
== NULL
)
3305 ret
= dplane_route_update_internal(rn
, re
, NULL
,
3306 DPLANE_OP_ROUTE_DELETE
);
3313 * Notify the dplane when system/connected routes change.
3315 enum zebra_dplane_result
dplane_sys_route_add(struct route_node
*rn
,
3316 struct route_entry
*re
)
3318 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3320 /* Ignore this event unless a provider plugin has requested it. */
3321 if (!zdplane_info
.dg_sys_route_notifs
) {
3322 ret
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3326 if (rn
== NULL
|| re
== NULL
)
3329 ret
= dplane_route_update_internal(rn
, re
, NULL
,
3330 DPLANE_OP_SYS_ROUTE_ADD
);
3337 * Notify the dplane when system/connected routes are deleted.
3339 enum zebra_dplane_result
dplane_sys_route_del(struct route_node
*rn
,
3340 struct route_entry
*re
)
3342 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3344 /* Ignore this event unless a provider plugin has requested it. */
3345 if (!zdplane_info
.dg_sys_route_notifs
) {
3346 ret
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3350 if (rn
== NULL
|| re
== NULL
)
3353 ret
= dplane_route_update_internal(rn
, re
, NULL
,
3354 DPLANE_OP_SYS_ROUTE_DELETE
);
3361 * Update from an async notification, to bring other fibs up-to-date.
3363 enum zebra_dplane_result
3364 dplane_route_notif_update(struct route_node
*rn
,
3365 struct route_entry
*re
,
3366 enum dplane_op_e op
,
3367 struct zebra_dplane_ctx
*ctx
)
3369 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3371 struct zebra_dplane_ctx
*new_ctx
= NULL
;
3372 struct nexthop
*nexthop
;
3373 struct nexthop_group
*nhg
;
3375 if (rn
== NULL
|| re
== NULL
)
3378 new_ctx
= dplane_ctx_alloc();
3379 if (new_ctx
== NULL
)
3382 /* Init context with info from zebra data structs */
3383 dplane_ctx_route_init(new_ctx
, op
, rn
, re
);
3385 /* For add/update, need to adjust the nexthops so that we match
3386 * the notification state, which may not be the route-entry/RIB
3389 if (op
== DPLANE_OP_ROUTE_UPDATE
||
3390 op
== DPLANE_OP_ROUTE_INSTALL
) {
3392 nexthops_free(new_ctx
->u
.rinfo
.zd_ng
.nexthop
);
3393 new_ctx
->u
.rinfo
.zd_ng
.nexthop
= NULL
;
3395 nhg
= rib_get_fib_nhg(re
);
3396 if (nhg
&& nhg
->nexthop
)
3397 copy_nexthops(&(new_ctx
->u
.rinfo
.zd_ng
.nexthop
),
3398 nhg
->nexthop
, NULL
);
3400 /* Check for installed backup nexthops also */
3401 nhg
= rib_get_fib_backup_nhg(re
);
3402 if (nhg
&& nhg
->nexthop
) {
3403 copy_nexthops(&(new_ctx
->u
.rinfo
.zd_ng
.nexthop
),
3404 nhg
->nexthop
, NULL
);
3407 for (ALL_NEXTHOPS(new_ctx
->u
.rinfo
.zd_ng
, nexthop
))
3408 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3412 /* Capture info about the source of the notification, in 'ctx' */
3413 dplane_ctx_set_notif_provider(new_ctx
,
3414 dplane_ctx_get_notif_provider(ctx
));
3416 ret
= dplane_update_enqueue(new_ctx
);
3420 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3422 dplane_ctx_free(&new_ctx
);
3428 * Enqueue a nexthop add for the dataplane.
3430 enum zebra_dplane_result
dplane_nexthop_add(struct nhg_hash_entry
*nhe
)
3432 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3435 ret
= dplane_nexthop_update_internal(nhe
, DPLANE_OP_NH_INSTALL
);
3440 * Enqueue a nexthop update for the dataplane.
3442 * Might not need this func since zebra's nexthop objects should be immutable?
3444 enum zebra_dplane_result
dplane_nexthop_update(struct nhg_hash_entry
*nhe
)
3446 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3449 ret
= dplane_nexthop_update_internal(nhe
, DPLANE_OP_NH_UPDATE
);
3454 * Enqueue a nexthop removal for the dataplane.
3456 enum zebra_dplane_result
dplane_nexthop_delete(struct nhg_hash_entry
*nhe
)
3458 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3461 ret
= dplane_nexthop_update_internal(nhe
, DPLANE_OP_NH_DELETE
);
3467 * Enqueue LSP add for the dataplane.
3469 enum zebra_dplane_result
dplane_lsp_add(struct zebra_lsp
*lsp
)
3471 enum zebra_dplane_result ret
=
3472 lsp_update_internal(lsp
, DPLANE_OP_LSP_INSTALL
);
3478 * Enqueue LSP update for the dataplane.
3480 enum zebra_dplane_result
dplane_lsp_update(struct zebra_lsp
*lsp
)
3482 enum zebra_dplane_result ret
=
3483 lsp_update_internal(lsp
, DPLANE_OP_LSP_UPDATE
);
3489 * Enqueue LSP delete for the dataplane.
3491 enum zebra_dplane_result
dplane_lsp_delete(struct zebra_lsp
*lsp
)
3493 enum zebra_dplane_result ret
=
3494 lsp_update_internal(lsp
, DPLANE_OP_LSP_DELETE
);
3499 /* Update or un-install resulting from an async notification */
3500 enum zebra_dplane_result
3501 dplane_lsp_notif_update(struct zebra_lsp
*lsp
, enum dplane_op_e op
,
3502 struct zebra_dplane_ctx
*notif_ctx
)
3504 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3506 struct zebra_dplane_ctx
*ctx
= NULL
;
3507 struct nhlfe_list_head
*head
;
3508 struct zebra_nhlfe
*nhlfe
, *new_nhlfe
;
3510 /* Obtain context block */
3511 ctx
= dplane_ctx_alloc();
3517 /* Copy info from zebra LSP */
3518 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
3522 /* Add any installed backup nhlfes */
3523 head
= &(ctx
->u
.lsp
.backup_nhlfe_list
);
3524 frr_each(nhlfe_list
, head
, nhlfe
) {
3526 if (CHECK_FLAG(nhlfe
->flags
, NHLFE_FLAG_INSTALLED
) &&
3527 CHECK_FLAG(nhlfe
->nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
3528 new_nhlfe
= zebra_mpls_lsp_add_nh(&(ctx
->u
.lsp
),
3532 /* Need to copy flags too */
3533 new_nhlfe
->flags
= nhlfe
->flags
;
3534 new_nhlfe
->nexthop
->flags
= nhlfe
->nexthop
->flags
;
3538 /* Capture info about the source of the notification */
3539 dplane_ctx_set_notif_provider(
3541 dplane_ctx_get_notif_provider(notif_ctx
));
3543 ret
= dplane_update_enqueue(ctx
);
3546 /* Update counter */
3547 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
3548 memory_order_relaxed
);
3551 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3553 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
3554 memory_order_relaxed
);
3556 dplane_ctx_free(&ctx
);
3562 * Enqueue pseudowire install for the dataplane.
3564 enum zebra_dplane_result
dplane_pw_install(struct zebra_pw
*pw
)
3566 return pw_update_internal(pw
, DPLANE_OP_PW_INSTALL
);
3570 * Enqueue pseudowire un-install for the dataplane.
3572 enum zebra_dplane_result
dplane_pw_uninstall(struct zebra_pw
*pw
)
3574 return pw_update_internal(pw
, DPLANE_OP_PW_UNINSTALL
);
3578 * Common internal LSP update utility
3580 static enum zebra_dplane_result
lsp_update_internal(struct zebra_lsp
*lsp
,
3581 enum dplane_op_e op
)
3583 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3585 struct zebra_dplane_ctx
*ctx
= NULL
;
3587 /* Obtain context block */
3588 ctx
= dplane_ctx_alloc();
3590 ret
= dplane_ctx_lsp_init(ctx
, op
, lsp
);
3594 ret
= dplane_update_enqueue(ctx
);
3597 /* Update counter */
3598 atomic_fetch_add_explicit(&zdplane_info
.dg_lsps_in
, 1,
3599 memory_order_relaxed
);
3602 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3604 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
, 1,
3605 memory_order_relaxed
);
3606 dplane_ctx_free(&ctx
);
3613 * Internal, common handler for pseudowire updates.
3615 static enum zebra_dplane_result
pw_update_internal(struct zebra_pw
*pw
,
3616 enum dplane_op_e op
)
3618 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3620 struct zebra_dplane_ctx
*ctx
= NULL
;
3622 ctx
= dplane_ctx_alloc();
3624 ret
= dplane_ctx_pw_init(ctx
, op
, pw
);
3628 ret
= dplane_update_enqueue(ctx
);
3631 /* Update counter */
3632 atomic_fetch_add_explicit(&zdplane_info
.dg_pws_in
, 1,
3633 memory_order_relaxed
);
3636 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3638 atomic_fetch_add_explicit(&zdplane_info
.dg_pw_errors
, 1,
3639 memory_order_relaxed
);
3640 dplane_ctx_free(&ctx
);
3647 * Enqueue access br_port update.
3649 enum zebra_dplane_result
3650 dplane_br_port_update(const struct interface
*ifp
, bool non_df
,
3651 uint32_t sph_filter_cnt
,
3652 const struct in_addr
*sph_filters
, uint32_t backup_nhg_id
)
3654 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3657 struct zebra_dplane_ctx
*ctx
= NULL
;
3658 struct zebra_ns
*zns
;
3659 enum dplane_op_e op
= DPLANE_OP_BR_PORT_UPDATE
;
3662 flags
|= DPLANE_BR_PORT_NON_DF
;
3664 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
|| IS_ZEBRA_DEBUG_EVPN_MH_ES
) {
3666 char vtep_str
[ES_VTEP_LIST_STR_SZ
];
3669 for (i
= 0; i
< sph_filter_cnt
; ++i
) {
3670 snprintfrr(vtep_str
+ strlen(vtep_str
),
3671 sizeof(vtep_str
) - strlen(vtep_str
), "%pI4 ",
3675 "init br_port ctx %s: ifp %s, flags 0x%x backup_nhg 0x%x sph %s",
3676 dplane_op2str(op
), ifp
->name
, flags
, backup_nhg_id
,
3680 ctx
= dplane_ctx_alloc();
3683 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3684 ctx
->zd_vrf_id
= ifp
->vrf
->vrf_id
;
3686 zns
= zebra_ns_lookup(ifp
->vrf
->vrf_id
);
3687 dplane_ctx_ns_init(ctx
, zns
, false);
3689 ctx
->zd_ifindex
= ifp
->ifindex
;
3690 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
3692 /* Init the br-port-specific data area */
3693 memset(&ctx
->u
.br_port
, 0, sizeof(ctx
->u
.br_port
));
3695 ctx
->u
.br_port
.flags
= flags
;
3696 ctx
->u
.br_port
.backup_nhg_id
= backup_nhg_id
;
3697 ctx
->u
.br_port
.sph_filter_cnt
= sph_filter_cnt
;
3698 memcpy(ctx
->u
.br_port
.sph_filters
, sph_filters
,
3699 sizeof(ctx
->u
.br_port
.sph_filters
[0]) * sph_filter_cnt
);
3701 /* Enqueue for processing on the dplane pthread */
3702 ret
= dplane_update_enqueue(ctx
);
3704 /* Increment counter */
3705 atomic_fetch_add_explicit(&zdplane_info
.dg_br_port_in
, 1,
3706 memory_order_relaxed
);
3709 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3712 atomic_fetch_add_explicit(&zdplane_info
.dg_br_port_errors
, 1,
3713 memory_order_relaxed
);
3714 dplane_ctx_free(&ctx
);
3721 * Enqueue interface address add for the dataplane.
3723 enum zebra_dplane_result
dplane_intf_addr_set(const struct interface
*ifp
,
3724 const struct connected
*ifc
)
3726 #if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
3727 /* Extra checks for this OS path. */
3729 /* Don't configure PtP addresses on broadcast ifs or reverse */
3730 if (!(ifp
->flags
& IFF_POINTOPOINT
) != !CONNECTED_PEER(ifc
)) {
3731 if (IS_ZEBRA_DEBUG_KERNEL
|| IS_ZEBRA_DEBUG_DPLANE
)
3732 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
3734 return ZEBRA_DPLANE_REQUEST_FAILURE
;
3738 return intf_addr_update_internal(ifp
, ifc
, DPLANE_OP_ADDR_INSTALL
);
3742 * Enqueue interface address remove/uninstall for the dataplane.
3744 enum zebra_dplane_result
dplane_intf_addr_unset(const struct interface
*ifp
,
3745 const struct connected
*ifc
)
3747 return intf_addr_update_internal(ifp
, ifc
, DPLANE_OP_ADDR_UNINSTALL
);
3750 static enum zebra_dplane_result
intf_addr_update_internal(
3751 const struct interface
*ifp
, const struct connected
*ifc
,
3752 enum dplane_op_e op
)
3754 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3756 struct zebra_dplane_ctx
*ctx
= NULL
;
3757 struct zebra_ns
*zns
;
3759 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3760 zlog_debug("init intf ctx %s: idx %d, addr %u:%pFX",
3761 dplane_op2str(op
), ifp
->ifindex
, ifp
->vrf
->vrf_id
,
3764 ctx
= dplane_ctx_alloc();
3767 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3768 ctx
->zd_vrf_id
= ifp
->vrf
->vrf_id
;
3770 zns
= zebra_ns_lookup(ifp
->vrf
->vrf_id
);
3771 dplane_ctx_ns_init(ctx
, zns
, false);
3773 /* Init the interface-addr-specific area */
3774 memset(&ctx
->u
.intf
, 0, sizeof(ctx
->u
.intf
));
3776 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
3777 ctx
->zd_ifindex
= ifp
->ifindex
;
3778 ctx
->u
.intf
.prefix
= *(ifc
->address
);
3780 if (if_is_broadcast(ifp
))
3781 ctx
->u
.intf
.flags
|= DPLANE_INTF_BROADCAST
;
3783 if (CONNECTED_PEER(ifc
)) {
3784 ctx
->u
.intf
.dest_prefix
= *(ifc
->destination
);
3785 ctx
->u
.intf
.flags
|=
3786 (DPLANE_INTF_CONNECTED
| DPLANE_INTF_HAS_DEST
);
3789 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
3790 ctx
->u
.intf
.flags
|= DPLANE_INTF_SECONDARY
;
3795 ctx
->u
.intf
.flags
|= DPLANE_INTF_HAS_LABEL
;
3797 /* Use embedded buffer if it's adequate; else allocate. */
3798 len
= strlen(ifc
->label
);
3800 if (len
< sizeof(ctx
->u
.intf
.label_buf
)) {
3801 strlcpy(ctx
->u
.intf
.label_buf
, ifc
->label
,
3802 sizeof(ctx
->u
.intf
.label_buf
));
3803 ctx
->u
.intf
.label
= ctx
->u
.intf
.label_buf
;
3805 ctx
->u
.intf
.label
= XSTRDUP(MTYPE_DP_CTX
, ifc
->label
);
3809 ret
= dplane_update_enqueue(ctx
);
3811 /* Increment counter */
3812 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addrs_in
, 1,
3813 memory_order_relaxed
);
3816 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
3819 atomic_fetch_add_explicit(&zdplane_info
.dg_intf_addr_errors
,
3820 1, memory_order_relaxed
);
3821 dplane_ctx_free(&ctx
);
3828 * Enqueue vxlan/evpn mac add (or update).
3830 enum zebra_dplane_result
dplane_rem_mac_add(const struct interface
*ifp
,
3831 const struct interface
*bridge_ifp
,
3833 const struct ethaddr
*mac
,
3834 struct in_addr vtep_ip
,
3839 enum zebra_dplane_result result
;
3840 uint32_t update_flags
= 0;
3842 update_flags
|= DPLANE_MAC_REMOTE
;
3844 update_flags
|= DPLANE_MAC_WAS_STATIC
;
3846 /* Use common helper api */
3847 result
= mac_update_common(DPLANE_OP_MAC_INSTALL
, ifp
, bridge_ifp
,
3848 vid
, mac
, vtep_ip
, sticky
, nhg_id
, update_flags
);
3853 * Enqueue vxlan/evpn mac delete.
3855 enum zebra_dplane_result
dplane_rem_mac_del(const struct interface
*ifp
,
3856 const struct interface
*bridge_ifp
,
3858 const struct ethaddr
*mac
,
3859 struct in_addr vtep_ip
)
3861 enum zebra_dplane_result result
;
3862 uint32_t update_flags
= 0;
3864 update_flags
|= DPLANE_MAC_REMOTE
;
3866 /* Use common helper api */
3867 result
= mac_update_common(DPLANE_OP_MAC_DELETE
, ifp
, bridge_ifp
,
3868 vid
, mac
, vtep_ip
, false, 0, update_flags
);
3873 * API to configure link local with either MAC address or IP information
3875 enum zebra_dplane_result
dplane_neigh_ip_update(enum dplane_op_e op
,
3876 const struct interface
*ifp
,
3877 struct ipaddr
*link_ip
,
3879 uint32_t ndm_state
, int protocol
)
3881 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
3883 uint32_t update_flags
;
3885 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
3886 zlog_debug("%s: init link ctx %s: ifp %s, link_ip %pIA ip %pIA",
3887 __func__
, dplane_op2str(op
), ifp
->name
, link_ip
, ip
);
3889 if (ndm_state
== ZEBRA_NEIGH_STATE_REACHABLE
)
3890 state
= DPLANE_NUD_REACHABLE
;
3891 else if (ndm_state
== ZEBRA_NEIGH_STATE_FAILED
)
3892 state
= DPLANE_NUD_FAILED
;
3894 update_flags
= DPLANE_NEIGH_NO_EXTENSION
;
3896 result
= neigh_update_internal(op
, ifp
, (const void *)link_ip
,
3897 ipaddr_family(link_ip
), ip
, 0, state
,
3898 update_flags
, protocol
);
3904 * Enqueue local mac add (or update).
3906 enum zebra_dplane_result
dplane_local_mac_add(const struct interface
*ifp
,
3907 const struct interface
*bridge_ifp
,
3909 const struct ethaddr
*mac
,
3911 uint32_t set_static
,
3912 uint32_t set_inactive
)
3914 enum zebra_dplane_result result
;
3915 uint32_t update_flags
= 0;
3916 struct in_addr vtep_ip
;
3919 update_flags
|= DPLANE_MAC_SET_STATIC
;
3922 update_flags
|= DPLANE_MAC_SET_INACTIVE
;
3926 /* Use common helper api */
3927 result
= mac_update_common(DPLANE_OP_MAC_INSTALL
, ifp
, bridge_ifp
,
3928 vid
, mac
, vtep_ip
, sticky
, 0,
3934 * Enqueue local mac del
3936 enum zebra_dplane_result
3937 dplane_local_mac_del(const struct interface
*ifp
,
3938 const struct interface
*bridge_ifp
, vlanid_t vid
,
3939 const struct ethaddr
*mac
)
3941 enum zebra_dplane_result result
;
3942 struct in_addr vtep_ip
;
3946 /* Use common helper api */
3947 result
= mac_update_common(DPLANE_OP_MAC_DELETE
, ifp
, bridge_ifp
, vid
,
3948 mac
, vtep_ip
, false, 0, 0);
3952 * Public api to init an empty context - either newly-allocated or
3953 * reset/cleared - for a MAC update.
3955 void dplane_mac_init(struct zebra_dplane_ctx
*ctx
,
3956 const struct interface
*ifp
,
3957 const struct interface
*br_ifp
,
3959 const struct ethaddr
*mac
,
3960 struct in_addr vtep_ip
,
3963 uint32_t update_flags
)
3965 struct zebra_ns
*zns
;
3967 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
3968 ctx
->zd_vrf_id
= ifp
->vrf
->vrf_id
;
3970 zns
= zebra_ns_lookup(ifp
->vrf
->vrf_id
);
3971 dplane_ctx_ns_init(ctx
, zns
, false);
3973 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
3974 ctx
->zd_ifindex
= ifp
->ifindex
;
3976 /* Init the mac-specific data area */
3977 memset(&ctx
->u
.macinfo
, 0, sizeof(ctx
->u
.macinfo
));
3979 ctx
->u
.macinfo
.br_ifindex
= br_ifp
->ifindex
;
3980 ctx
->u
.macinfo
.vtep_ip
= vtep_ip
;
3981 ctx
->u
.macinfo
.mac
= *mac
;
3982 ctx
->u
.macinfo
.vid
= vid
;
3983 ctx
->u
.macinfo
.is_sticky
= sticky
;
3984 ctx
->u
.macinfo
.nhg_id
= nhg_id
;
3985 ctx
->u
.macinfo
.update_flags
= update_flags
;
3989 * Common helper api for MAC address/vxlan updates
3991 static enum zebra_dplane_result
3992 mac_update_common(enum dplane_op_e op
,
3993 const struct interface
*ifp
,
3994 const struct interface
*br_ifp
,
3996 const struct ethaddr
*mac
,
3997 struct in_addr vtep_ip
,
4000 uint32_t update_flags
)
4002 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4004 struct zebra_dplane_ctx
*ctx
= NULL
;
4006 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
4007 zlog_debug("init mac ctx %s: mac %pEA, ifp %s, vtep %pI4",
4008 dplane_op2str(op
), mac
, ifp
->name
, &vtep_ip
);
4010 ctx
= dplane_ctx_alloc();
4013 /* Common init for the ctx */
4014 dplane_mac_init(ctx
, ifp
, br_ifp
, vid
, mac
, vtep_ip
, sticky
,
4015 nhg_id
, update_flags
);
4017 /* Enqueue for processing on the dplane pthread */
4018 ret
= dplane_update_enqueue(ctx
);
4020 /* Increment counter */
4021 atomic_fetch_add_explicit(&zdplane_info
.dg_macs_in
, 1,
4022 memory_order_relaxed
);
4025 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4028 atomic_fetch_add_explicit(&zdplane_info
.dg_mac_errors
, 1,
4029 memory_order_relaxed
);
4030 dplane_ctx_free(&ctx
);
4037 * Enqueue evpn neighbor add for the dataplane.
4039 enum zebra_dplane_result
dplane_rem_neigh_add(const struct interface
*ifp
,
4040 const struct ipaddr
*ip
,
4041 const struct ethaddr
*mac
,
4042 uint32_t flags
, bool was_static
)
4044 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4045 uint32_t update_flags
= 0;
4047 update_flags
|= DPLANE_NEIGH_REMOTE
;
4050 update_flags
|= DPLANE_NEIGH_WAS_STATIC
;
4052 result
= neigh_update_internal(
4053 DPLANE_OP_NEIGH_INSTALL
, ifp
, (const void *)mac
, AF_ETHERNET
,
4054 ip
, flags
, DPLANE_NUD_NOARP
, update_flags
, 0);
4060 * Enqueue local neighbor add for the dataplane.
4062 enum zebra_dplane_result
dplane_local_neigh_add(const struct interface
*ifp
,
4063 const struct ipaddr
*ip
,
4064 const struct ethaddr
*mac
,
4065 bool set_router
, bool set_static
,
4068 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4069 uint32_t update_flags
= 0;
4074 update_flags
|= DPLANE_NEIGH_SET_STATIC
;
4077 update_flags
|= DPLANE_NEIGH_SET_INACTIVE
;
4078 state
= DPLANE_NUD_STALE
;
4080 state
= DPLANE_NUD_REACHABLE
;
4084 ntf
|= DPLANE_NTF_ROUTER
;
4086 result
= neigh_update_internal(DPLANE_OP_NEIGH_INSTALL
, ifp
,
4087 (const void *)mac
, AF_ETHERNET
, ip
, ntf
,
4088 state
, update_flags
, 0);
4094 * Enqueue evpn neighbor delete for the dataplane.
4096 enum zebra_dplane_result
dplane_rem_neigh_delete(const struct interface
*ifp
,
4097 const struct ipaddr
*ip
)
4099 enum zebra_dplane_result result
;
4100 uint32_t update_flags
= 0;
4102 update_flags
|= DPLANE_NEIGH_REMOTE
;
4104 result
= neigh_update_internal(DPLANE_OP_NEIGH_DELETE
, ifp
, NULL
,
4105 AF_ETHERNET
, ip
, 0, 0, update_flags
, 0);
4111 * Enqueue evpn VTEP add for the dataplane.
4113 enum zebra_dplane_result
dplane_vtep_add(const struct interface
*ifp
,
4114 const struct in_addr
*ip
,
4117 enum zebra_dplane_result result
;
4118 struct ethaddr mac
= { {0, 0, 0, 0, 0, 0} };
4121 if (IS_ZEBRA_DEBUG_VXLAN
)
4122 zlog_debug("Install %pI4 into flood list for VNI %u intf %s(%u)",
4123 ip
, vni
, ifp
->name
, ifp
->ifindex
);
4125 SET_IPADDR_V4(&addr
);
4126 addr
.ipaddr_v4
= *ip
;
4128 result
= neigh_update_internal(DPLANE_OP_VTEP_ADD
, ifp
, &mac
,
4129 AF_ETHERNET
, &addr
, 0, 0, 0, 0);
4135 * Enqueue evpn VTEP add for the dataplane.
4137 enum zebra_dplane_result
dplane_vtep_delete(const struct interface
*ifp
,
4138 const struct in_addr
*ip
,
4141 enum zebra_dplane_result result
;
4142 struct ethaddr mac
= { {0, 0, 0, 0, 0, 0} };
4145 if (IS_ZEBRA_DEBUG_VXLAN
)
4147 "Uninstall %pI4 from flood list for VNI %u intf %s(%u)",
4148 ip
, vni
, ifp
->name
, ifp
->ifindex
);
4150 SET_IPADDR_V4(&addr
);
4151 addr
.ipaddr_v4
= *ip
;
4153 result
= neigh_update_internal(DPLANE_OP_VTEP_DELETE
, ifp
,
4154 (const void *)&mac
, AF_ETHERNET
, &addr
,
4160 enum zebra_dplane_result
dplane_neigh_discover(const struct interface
*ifp
,
4161 const struct ipaddr
*ip
)
4163 enum zebra_dplane_result result
;
4165 result
= neigh_update_internal(DPLANE_OP_NEIGH_DISCOVER
, ifp
, NULL
,
4166 AF_ETHERNET
, ip
, DPLANE_NTF_USE
,
4167 DPLANE_NUD_INCOMPLETE
, 0, 0);
4172 enum zebra_dplane_result
dplane_neigh_table_update(const struct interface
*ifp
,
4173 const uint8_t family
,
4174 const uint32_t app_probes
,
4175 const uint32_t ucast_probes
,
4176 const uint32_t mcast_probes
)
4178 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4180 struct zebra_dplane_ctx
*ctx
= NULL
;
4181 struct zebra_ns
*zns
;
4182 enum dplane_op_e op
= DPLANE_OP_NEIGH_TABLE_UPDATE
;
4184 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
4185 zlog_debug("set neigh ctx %s: ifp %s, family %s",
4186 dplane_op2str(op
), ifp
->name
, family2str(family
));
4189 ctx
= dplane_ctx_alloc();
4192 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
4193 ctx
->zd_vrf_id
= ifp
->vrf
->vrf_id
;
4195 zns
= zebra_ns_lookup(ifp
->vrf
->vrf_id
);
4196 dplane_ctx_ns_init(ctx
, zns
, false);
4198 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
4199 ctx
->zd_ifindex
= ifp
->ifindex
;
4201 /* Init the neighbor-specific data area */
4202 memset(&ctx
->u
.neightable
, 0, sizeof(ctx
->u
.neightable
));
4204 ctx
->u
.neightable
.family
= family
;
4205 ctx
->u
.neightable
.app_probes
= app_probes
;
4206 ctx
->u
.neightable
.ucast_probes
= ucast_probes
;
4207 ctx
->u
.neightable
.mcast_probes
= mcast_probes
;
4209 /* Enqueue for processing on the dplane pthread */
4210 ret
= dplane_update_enqueue(ctx
);
4212 /* Increment counter */
4213 atomic_fetch_add_explicit(&zdplane_info
.dg_neightable_in
, 1,
4214 memory_order_relaxed
);
4217 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4220 atomic_fetch_add_explicit(&zdplane_info
.dg_neightable_errors
, 1,
4221 memory_order_relaxed
);
4222 dplane_ctx_free(&ctx
);
4229 * Common helper api for neighbor updates
4231 static enum zebra_dplane_result
4232 neigh_update_internal(enum dplane_op_e op
, const struct interface
*ifp
,
4233 const void *link
, const int link_family
,
4234 const struct ipaddr
*ip
, uint32_t flags
, uint16_t state
,
4235 uint32_t update_flags
, int protocol
)
4237 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4239 struct zebra_dplane_ctx
*ctx
= NULL
;
4240 struct zebra_ns
*zns
;
4241 const struct ethaddr
*mac
= NULL
;
4242 const struct ipaddr
*link_ip
= NULL
;
4244 if (link_family
== AF_ETHERNET
)
4245 mac
= (const struct ethaddr
*)link
;
4247 link_ip
= (const struct ipaddr
*)link
;
4249 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
4250 char buf1
[PREFIX_STRLEN
];
4253 if (link_family
== AF_ETHERNET
)
4254 prefix_mac2str(mac
, buf1
, sizeof(buf1
));
4256 ipaddr2str(link_ip
, buf1
, sizeof(buf1
));
4257 zlog_debug("init neigh ctx %s: ifp %s, %s %s, ip %pIA",
4258 dplane_op2str(op
), ifp
->name
,
4259 link_family
== AF_ETHERNET
? "mac " : "link ",
4263 ctx
= dplane_ctx_alloc();
4266 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
4267 ctx
->zd_vrf_id
= ifp
->vrf
->vrf_id
;
4268 dplane_ctx_set_type(ctx
, protocol
);
4270 zns
= zebra_ns_lookup(ifp
->vrf
->vrf_id
);
4271 dplane_ctx_ns_init(ctx
, zns
, false);
4273 strlcpy(ctx
->zd_ifname
, ifp
->name
, sizeof(ctx
->zd_ifname
));
4274 ctx
->zd_ifindex
= ifp
->ifindex
;
4276 /* Init the neighbor-specific data area */
4277 memset(&ctx
->u
.neigh
, 0, sizeof(ctx
->u
.neigh
));
4279 ctx
->u
.neigh
.ip_addr
= *ip
;
4281 ctx
->u
.neigh
.link
.mac
= *mac
;
4283 ctx
->u
.neigh
.link
.ip_addr
= *link_ip
;
4285 ctx
->u
.neigh
.flags
= flags
;
4286 ctx
->u
.neigh
.state
= state
;
4287 ctx
->u
.neigh
.update_flags
= update_flags
;
4289 /* Enqueue for processing on the dplane pthread */
4290 ret
= dplane_update_enqueue(ctx
);
4292 /* Increment counter */
4293 atomic_fetch_add_explicit(&zdplane_info
.dg_neighs_in
, 1,
4294 memory_order_relaxed
);
4297 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4300 atomic_fetch_add_explicit(&zdplane_info
.dg_neigh_errors
, 1,
4301 memory_order_relaxed
);
4302 dplane_ctx_free(&ctx
);
4309 * Common helper api for PBR rule updates
4311 static enum zebra_dplane_result
4312 rule_update_internal(enum dplane_op_e op
, struct zebra_pbr_rule
*new_rule
,
4313 struct zebra_pbr_rule
*old_rule
)
4315 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4316 struct zebra_dplane_ctx
*ctx
;
4319 ctx
= dplane_ctx_alloc();
4321 ret
= dplane_ctx_rule_init(ctx
, op
, new_rule
, old_rule
);
4325 ret
= dplane_update_enqueue(ctx
);
4328 atomic_fetch_add_explicit(&zdplane_info
.dg_rules_in
, 1,
4329 memory_order_relaxed
);
4332 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4334 atomic_fetch_add_explicit(&zdplane_info
.dg_rule_errors
, 1,
4335 memory_order_relaxed
);
4336 dplane_ctx_free(&ctx
);
4342 enum zebra_dplane_result
dplane_pbr_rule_add(struct zebra_pbr_rule
*rule
)
4344 return rule_update_internal(DPLANE_OP_RULE_ADD
, rule
, NULL
);
4347 enum zebra_dplane_result
dplane_pbr_rule_delete(struct zebra_pbr_rule
*rule
)
4349 return rule_update_internal(DPLANE_OP_RULE_DELETE
, rule
, NULL
);
4352 enum zebra_dplane_result
dplane_pbr_rule_update(struct zebra_pbr_rule
*old_rule
,
4353 struct zebra_pbr_rule
*new_rule
)
4355 return rule_update_internal(DPLANE_OP_RULE_UPDATE
, new_rule
, old_rule
);
4358 * Common helper api for iptable updates
4360 static enum zebra_dplane_result
4361 iptable_update_internal(enum dplane_op_e op
, struct zebra_pbr_iptable
*iptable
)
4363 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4364 struct zebra_dplane_ctx
*ctx
;
4367 ctx
= dplane_ctx_alloc();
4369 ret
= dplane_ctx_iptable_init(ctx
, op
, iptable
);
4373 ret
= dplane_update_enqueue(ctx
);
4376 atomic_fetch_add_explicit(&zdplane_info
.dg_iptable_in
, 1,
4377 memory_order_relaxed
);
4380 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4382 atomic_fetch_add_explicit(&zdplane_info
.dg_iptable_errors
, 1,
4383 memory_order_relaxed
);
4384 dplane_ctx_free(&ctx
);
4390 enum zebra_dplane_result
4391 dplane_pbr_iptable_add(struct zebra_pbr_iptable
*iptable
)
4393 return iptable_update_internal(DPLANE_OP_IPTABLE_ADD
, iptable
);
4396 enum zebra_dplane_result
4397 dplane_pbr_iptable_delete(struct zebra_pbr_iptable
*iptable
)
4399 return iptable_update_internal(DPLANE_OP_IPTABLE_DELETE
, iptable
);
4403 * Common helper api for ipset updates
4405 static enum zebra_dplane_result
4406 ipset_update_internal(enum dplane_op_e op
, struct zebra_pbr_ipset
*ipset
)
4408 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4409 struct zebra_dplane_ctx
*ctx
;
4412 ctx
= dplane_ctx_alloc();
4414 ret
= dplane_ctx_ipset_init(ctx
, op
, ipset
);
4418 ret
= dplane_update_enqueue(ctx
);
4421 atomic_fetch_add_explicit(&zdplane_info
.dg_ipset_in
, 1,
4422 memory_order_relaxed
);
4425 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4427 atomic_fetch_add_explicit(&zdplane_info
.dg_ipset_errors
, 1,
4428 memory_order_relaxed
);
4429 dplane_ctx_free(&ctx
);
4435 enum zebra_dplane_result
dplane_pbr_ipset_add(struct zebra_pbr_ipset
*ipset
)
4437 return ipset_update_internal(DPLANE_OP_IPSET_ADD
, ipset
);
4440 enum zebra_dplane_result
dplane_pbr_ipset_delete(struct zebra_pbr_ipset
*ipset
)
4442 return ipset_update_internal(DPLANE_OP_IPSET_DELETE
, ipset
);
4446 * Common helper api for ipset updates
4448 static enum zebra_dplane_result
4449 ipset_entry_update_internal(enum dplane_op_e op
,
4450 struct zebra_pbr_ipset_entry
*ipset_entry
)
4452 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4453 struct zebra_dplane_ctx
*ctx
;
4456 ctx
= dplane_ctx_alloc();
4458 ret
= dplane_ctx_ipset_entry_init(ctx
, op
, ipset_entry
);
4462 ret
= dplane_update_enqueue(ctx
);
4465 atomic_fetch_add_explicit(&zdplane_info
.dg_ipset_entry_in
, 1,
4466 memory_order_relaxed
);
4469 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4471 atomic_fetch_add_explicit(&zdplane_info
.dg_ipset_entry_errors
,
4472 1, memory_order_relaxed
);
4473 dplane_ctx_free(&ctx
);
4479 enum zebra_dplane_result
4480 dplane_pbr_ipset_entry_add(struct zebra_pbr_ipset_entry
*ipset
)
4482 return ipset_entry_update_internal(DPLANE_OP_IPSET_ENTRY_ADD
, ipset
);
4485 enum zebra_dplane_result
4486 dplane_pbr_ipset_entry_delete(struct zebra_pbr_ipset_entry
*ipset
)
4488 return ipset_entry_update_internal(DPLANE_OP_IPSET_ENTRY_DELETE
, ipset
);
4492 * Common helper api for GRE set
4494 enum zebra_dplane_result
4495 dplane_gre_set(struct interface
*ifp
, struct interface
*ifp_link
,
4496 unsigned int mtu
, const struct zebra_l2info_gre
*gre_info
)
4498 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4499 struct zebra_dplane_ctx
*ctx
;
4500 enum dplane_op_e op
= DPLANE_OP_GRE_SET
;
4502 struct zebra_ns
*zns
;
4504 ctx
= dplane_ctx_alloc();
4509 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
4510 zlog_debug("init dplane ctx %s: if %s link %s%s",
4511 dplane_op2str(op
), ifp
->name
,
4512 ifp_link
? "set" : "unset", ifp_link
?
4513 ifp_link
->name
: "");
4517 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
4518 zns
= zebra_ns_lookup(ifp
->vrf
->vrf_id
);
4521 dplane_ctx_ns_init(ctx
, zns
, false);
4523 dplane_ctx_set_ifname(ctx
, ifp
->name
);
4524 ctx
->zd_vrf_id
= ifp
->vrf
->vrf_id
;
4525 ctx
->zd_ifindex
= ifp
->ifindex
;
4527 ctx
->u
.gre
.link_ifindex
= ifp_link
->ifindex
;
4529 ctx
->u
.gre
.link_ifindex
= 0;
4531 memcpy(&ctx
->u
.gre
.info
, gre_info
, sizeof(ctx
->u
.gre
.info
));
4532 ctx
->u
.gre
.mtu
= mtu
;
4534 ctx
->zd_status
= ZEBRA_DPLANE_REQUEST_SUCCESS
;
4536 /* Enqueue context for processing */
4537 ret
= dplane_update_enqueue(ctx
);
4539 /* Update counter */
4540 atomic_fetch_add_explicit(&zdplane_info
.dg_gre_set_in
, 1,
4541 memory_order_relaxed
);
4544 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
4546 atomic_fetch_add_explicit(
4547 &zdplane_info
.dg_gre_set_errors
, 1,
4548 memory_order_relaxed
);
4550 dplane_ctx_free(&ctx
);
4551 result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
4557 * Handler for 'show dplane'
4559 int dplane_show_helper(struct vty
*vty
, bool detailed
)
4561 uint64_t queued
, queue_max
, limit
, errs
, incoming
, yields
,
4564 /* Using atomics because counters are being changed in different
4567 incoming
= atomic_load_explicit(&zdplane_info
.dg_routes_in
,
4568 memory_order_relaxed
);
4569 limit
= atomic_load_explicit(&zdplane_info
.dg_max_queued_updates
,
4570 memory_order_relaxed
);
4571 queued
= atomic_load_explicit(&zdplane_info
.dg_routes_queued
,
4572 memory_order_relaxed
);
4573 queue_max
= atomic_load_explicit(&zdplane_info
.dg_routes_queued_max
,
4574 memory_order_relaxed
);
4575 errs
= atomic_load_explicit(&zdplane_info
.dg_route_errors
,
4576 memory_order_relaxed
);
4577 yields
= atomic_load_explicit(&zdplane_info
.dg_update_yields
,
4578 memory_order_relaxed
);
4579 other_errs
= atomic_load_explicit(&zdplane_info
.dg_other_errors
,
4580 memory_order_relaxed
);
4582 vty_out(vty
, "Zebra dataplane:\nRoute updates: %"PRIu64
"\n",
4584 vty_out(vty
, "Route update errors: %"PRIu64
"\n", errs
);
4585 vty_out(vty
, "Other errors : %"PRIu64
"\n", other_errs
);
4586 vty_out(vty
, "Route update queue limit: %"PRIu64
"\n", limit
);
4587 vty_out(vty
, "Route update queue depth: %"PRIu64
"\n", queued
);
4588 vty_out(vty
, "Route update queue max: %"PRIu64
"\n", queue_max
);
4589 vty_out(vty
, "Dplane update yields: %"PRIu64
"\n", yields
);
4591 incoming
= atomic_load_explicit(&zdplane_info
.dg_lsps_in
,
4592 memory_order_relaxed
);
4593 errs
= atomic_load_explicit(&zdplane_info
.dg_lsp_errors
,
4594 memory_order_relaxed
);
4595 vty_out(vty
, "LSP updates: %"PRIu64
"\n", incoming
);
4596 vty_out(vty
, "LSP update errors: %"PRIu64
"\n", errs
);
4598 incoming
= atomic_load_explicit(&zdplane_info
.dg_pws_in
,
4599 memory_order_relaxed
);
4600 errs
= atomic_load_explicit(&zdplane_info
.dg_pw_errors
,
4601 memory_order_relaxed
);
4602 vty_out(vty
, "PW updates: %"PRIu64
"\n", incoming
);
4603 vty_out(vty
, "PW update errors: %"PRIu64
"\n", errs
);
4605 incoming
= atomic_load_explicit(&zdplane_info
.dg_intf_addrs_in
,
4606 memory_order_relaxed
);
4607 errs
= atomic_load_explicit(&zdplane_info
.dg_intf_addr_errors
,
4608 memory_order_relaxed
);
4609 vty_out(vty
, "Intf addr updates: %"PRIu64
"\n", incoming
);
4610 vty_out(vty
, "Intf addr errors: %"PRIu64
"\n", errs
);
4612 incoming
= atomic_load_explicit(&zdplane_info
.dg_macs_in
,
4613 memory_order_relaxed
);
4614 errs
= atomic_load_explicit(&zdplane_info
.dg_mac_errors
,
4615 memory_order_relaxed
);
4616 vty_out(vty
, "EVPN MAC updates: %"PRIu64
"\n", incoming
);
4617 vty_out(vty
, "EVPN MAC errors: %"PRIu64
"\n", errs
);
4619 incoming
= atomic_load_explicit(&zdplane_info
.dg_neighs_in
,
4620 memory_order_relaxed
);
4621 errs
= atomic_load_explicit(&zdplane_info
.dg_neigh_errors
,
4622 memory_order_relaxed
);
4623 vty_out(vty
, "EVPN neigh updates: %"PRIu64
"\n", incoming
);
4624 vty_out(vty
, "EVPN neigh errors: %"PRIu64
"\n", errs
);
4626 incoming
= atomic_load_explicit(&zdplane_info
.dg_rules_in
,
4627 memory_order_relaxed
);
4628 errs
= atomic_load_explicit(&zdplane_info
.dg_rule_errors
,
4629 memory_order_relaxed
);
4630 vty_out(vty
, "Rule updates: %" PRIu64
"\n", incoming
);
4631 vty_out(vty
, "Rule errors: %" PRIu64
"\n", errs
);
4633 incoming
= atomic_load_explicit(&zdplane_info
.dg_br_port_in
,
4634 memory_order_relaxed
);
4635 errs
= atomic_load_explicit(&zdplane_info
.dg_br_port_errors
,
4636 memory_order_relaxed
);
4637 vty_out(vty
, "Bridge port updates: %" PRIu64
"\n", incoming
);
4638 vty_out(vty
, "Bridge port errors: %" PRIu64
"\n", errs
);
4640 incoming
= atomic_load_explicit(&zdplane_info
.dg_iptable_in
,
4641 memory_order_relaxed
);
4642 errs
= atomic_load_explicit(&zdplane_info
.dg_iptable_errors
,
4643 memory_order_relaxed
);
4644 vty_out(vty
, "IPtable updates: %" PRIu64
"\n", incoming
);
4645 vty_out(vty
, "IPtable errors: %" PRIu64
"\n", errs
);
4646 incoming
= atomic_load_explicit(&zdplane_info
.dg_ipset_in
,
4647 memory_order_relaxed
);
4648 errs
= atomic_load_explicit(&zdplane_info
.dg_ipset_errors
,
4649 memory_order_relaxed
);
4650 vty_out(vty
, "IPset updates: %" PRIu64
"\n", incoming
);
4651 vty_out(vty
, "IPset errors: %" PRIu64
"\n", errs
);
4652 incoming
= atomic_load_explicit(&zdplane_info
.dg_ipset_entry_in
,
4653 memory_order_relaxed
);
4654 errs
= atomic_load_explicit(&zdplane_info
.dg_ipset_entry_errors
,
4655 memory_order_relaxed
);
4656 vty_out(vty
, "IPset entry updates: %" PRIu64
"\n", incoming
);
4657 vty_out(vty
, "IPset entry errors: %" PRIu64
"\n", errs
);
4659 incoming
= atomic_load_explicit(&zdplane_info
.dg_neightable_in
,
4660 memory_order_relaxed
);
4661 errs
= atomic_load_explicit(&zdplane_info
.dg_neightable_errors
,
4662 memory_order_relaxed
);
4663 vty_out(vty
, "Neighbor Table updates: %"PRIu64
"\n", incoming
);
4664 vty_out(vty
, "Neighbor Table errors: %"PRIu64
"\n", errs
);
4666 incoming
= atomic_load_explicit(&zdplane_info
.dg_gre_set_in
,
4667 memory_order_relaxed
);
4668 errs
= atomic_load_explicit(&zdplane_info
.dg_gre_set_errors
,
4669 memory_order_relaxed
);
4670 vty_out(vty
, "GRE set updates: %"PRIu64
"\n", incoming
);
4671 vty_out(vty
, "GRE set errors: %"PRIu64
"\n", errs
);
4676 * Handler for 'show dplane providers'
4678 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
)
4680 struct zebra_dplane_provider
*prov
;
4681 uint64_t in
, in_q
, in_max
, out
, out_q
, out_max
;
4683 vty_out(vty
, "Zebra dataplane providers:\n");
4686 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
4689 /* Show counters, useful info from each registered provider */
4692 in
= atomic_load_explicit(&prov
->dp_in_counter
,
4693 memory_order_relaxed
);
4694 in_q
= atomic_load_explicit(&prov
->dp_in_queued
,
4695 memory_order_relaxed
);
4696 in_max
= atomic_load_explicit(&prov
->dp_in_max
,
4697 memory_order_relaxed
);
4698 out
= atomic_load_explicit(&prov
->dp_out_counter
,
4699 memory_order_relaxed
);
4700 out_q
= atomic_load_explicit(&prov
->dp_out_queued
,
4701 memory_order_relaxed
);
4702 out_max
= atomic_load_explicit(&prov
->dp_out_max
,
4703 memory_order_relaxed
);
4705 vty_out(vty
, "%s (%u): in: %"PRIu64
", q: %"PRIu64
", q_max: %"PRIu64
", out: %"PRIu64
", q: %"PRIu64
", q_max: %"PRIu64
"\n",
4706 prov
->dp_name
, prov
->dp_id
, in
, in_q
, in_max
,
4707 out
, out_q
, out_max
);
4710 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
4718 * Helper for 'show run' etc.
4720 int dplane_config_write_helper(struct vty
*vty
)
4722 if (zdplane_info
.dg_max_queued_updates
!= DPLANE_DEFAULT_MAX_QUEUED
)
4723 vty_out(vty
, "zebra dplane limit %u\n",
4724 zdplane_info
.dg_max_queued_updates
);
4730 * Provider registration
4732 int dplane_provider_register(const char *name
,
4733 enum dplane_provider_prio prio
,
4735 int (*start_fp
)(struct zebra_dplane_provider
*),
4736 int (*fp
)(struct zebra_dplane_provider
*),
4737 int (*fini_fp
)(struct zebra_dplane_provider
*,
4740 struct zebra_dplane_provider
**prov_p
)
4743 struct zebra_dplane_provider
*p
= NULL
, *last
;
4751 if (prio
<= DPLANE_PRIO_NONE
||
4752 prio
> DPLANE_PRIO_LAST
) {
4757 /* Allocate and init new provider struct */
4758 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider
));
4760 pthread_mutex_init(&(p
->dp_mutex
), NULL
);
4761 TAILQ_INIT(&(p
->dp_ctx_in_q
));
4762 TAILQ_INIT(&(p
->dp_ctx_out_q
));
4764 p
->dp_flags
= flags
;
4765 p
->dp_priority
= prio
;
4767 p
->dp_start
= start_fp
;
4768 p
->dp_fini
= fini_fp
;
4771 /* Lock - the dplane pthread may be running */
4774 p
->dp_id
= ++zdplane_info
.dg_provider_id
;
4777 strlcpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
4779 snprintf(p
->dp_name
, DPLANE_PROVIDER_NAMELEN
,
4780 "provider-%u", p
->dp_id
);
4782 /* Insert into list ordered by priority */
4783 TAILQ_FOREACH(last
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
4784 if (last
->dp_priority
> p
->dp_priority
)
4789 TAILQ_INSERT_BEFORE(last
, p
, dp_prov_link
);
4791 TAILQ_INSERT_TAIL(&zdplane_info
.dg_providers_q
, p
,
4797 if (IS_ZEBRA_DEBUG_DPLANE
)
4798 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
4799 p
->dp_name
, p
->dp_id
, p
->dp_priority
);
4808 /* Accessors for provider attributes */
4809 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
)
4811 return prov
->dp_name
;
4814 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
)
4819 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
)
4821 return prov
->dp_data
;
4824 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
)
4826 return zdplane_info
.dg_updates_per_cycle
;
4829 /* Lock/unlock a provider's mutex - iff the provider was registered with
4830 * the THREADED flag.
4832 void dplane_provider_lock(struct zebra_dplane_provider
*prov
)
4834 if (dplane_provider_is_threaded(prov
))
4835 DPLANE_PROV_LOCK(prov
);
4838 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
)
4840 if (dplane_provider_is_threaded(prov
))
4841 DPLANE_PROV_UNLOCK(prov
);
4845 * Dequeue and maintain associated counter
4847 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
4848 struct zebra_dplane_provider
*prov
)
4850 struct zebra_dplane_ctx
*ctx
= NULL
;
4852 dplane_provider_lock(prov
);
4854 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
4856 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
4858 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, 1,
4859 memory_order_relaxed
);
4862 dplane_provider_unlock(prov
);
4868 * Dequeue work to a list, return count
4870 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
4871 struct dplane_ctx_q
*listp
)
4874 struct zebra_dplane_ctx
*ctx
;
4876 limit
= zdplane_info
.dg_updates_per_cycle
;
4878 dplane_provider_lock(prov
);
4880 for (ret
= 0; ret
< limit
; ret
++) {
4881 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
4883 TAILQ_REMOVE(&(prov
->dp_ctx_in_q
), ctx
, zd_q_entries
);
4885 TAILQ_INSERT_TAIL(listp
, ctx
, zd_q_entries
);
4892 atomic_fetch_sub_explicit(&prov
->dp_in_queued
, ret
,
4893 memory_order_relaxed
);
4895 dplane_provider_unlock(prov
);
4900 uint32_t dplane_provider_out_ctx_queue_len(struct zebra_dplane_provider
*prov
)
4902 return atomic_load_explicit(&(prov
->dp_out_counter
),
4903 memory_order_relaxed
);
4907 * Enqueue and maintain associated counter
4909 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
4910 struct zebra_dplane_ctx
*ctx
)
4912 uint64_t curr
, high
;
4914 dplane_provider_lock(prov
);
4916 TAILQ_INSERT_TAIL(&(prov
->dp_ctx_out_q
), ctx
,
4919 /* Maintain out-queue counters */
4920 atomic_fetch_add_explicit(&(prov
->dp_out_queued
), 1,
4921 memory_order_relaxed
);
4922 curr
= atomic_load_explicit(&prov
->dp_out_queued
,
4923 memory_order_relaxed
);
4924 high
= atomic_load_explicit(&prov
->dp_out_max
,
4925 memory_order_relaxed
);
4927 atomic_store_explicit(&prov
->dp_out_max
, curr
,
4928 memory_order_relaxed
);
4930 dplane_provider_unlock(prov
);
4932 atomic_fetch_add_explicit(&(prov
->dp_out_counter
), 1,
4933 memory_order_relaxed
);
4937 * Accessor for provider object
4939 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
)
4941 return (prov
->dp_flags
& DPLANE_PROV_FLAG_THREADED
);
4946 * Callback when an OS (netlink) incoming event read is ready. This runs
4947 * in the dplane pthread.
4949 static void dplane_incoming_read(struct thread
*event
)
4951 struct dplane_zns_info
*zi
= THREAD_ARG(event
);
4953 kernel_dplane_read(&zi
->info
);
4955 /* Re-start read task */
4956 thread_add_read(zdplane_info
.dg_master
, dplane_incoming_read
, zi
,
4957 zi
->info
.sock
, &zi
->t_read
);
4961 * Callback in the dataplane pthread that requests info from the OS and
4962 * initiates netlink reads.
4964 static void dplane_incoming_request(struct thread
*event
)
4966 struct dplane_zns_info
*zi
= THREAD_ARG(event
);
4968 /* Start read task */
4969 thread_add_read(zdplane_info
.dg_master
, dplane_incoming_read
, zi
,
4970 zi
->info
.sock
, &zi
->t_read
);
4973 netlink_request_netconf(zi
->info
.sock
);
4977 * Initiate requests for existing info from the OS. This is called by the
4978 * main pthread, but we want all activity on the dplane netlink socket to
4979 * take place on the dplane pthread, so we schedule an event to accomplish
4982 static void dplane_kernel_info_request(struct dplane_zns_info
*zi
)
4984 /* If we happen to encounter an enabled zns before the dplane
4985 * pthread is running, we'll initiate this later on.
4987 if (zdplane_info
.dg_master
)
4988 thread_add_event(zdplane_info
.dg_master
,
4989 dplane_incoming_request
, zi
, 0,
4993 #endif /* HAVE_NETLINK */
4996 * Notify dplane when namespaces are enabled and disabled. The dplane
4997 * needs to start and stop reading incoming events from the zns. In the
4998 * common case where vrfs are _not_ namespaces, there will only be one
5001 * This is called in the main pthread.
5003 void zebra_dplane_ns_enable(struct zebra_ns
*zns
, bool enabled
)
5005 struct dplane_zns_info
*zi
;
5007 if (IS_ZEBRA_DEBUG_DPLANE
)
5008 zlog_debug("%s: %s for nsid %u", __func__
,
5009 (enabled
? "ENABLED" : "DISABLED"), zns
->ns_id
);
5011 /* Search for an existing zns info entry */
5012 frr_each (zns_info_list
, &zdplane_info
.dg_zns_list
, zi
) {
5013 if (zi
->info
.ns_id
== zns
->ns_id
)
5018 /* Create a new entry if necessary; start reading. */
5020 zi
= XCALLOC(MTYPE_DP_NS
, sizeof(*zi
));
5022 zi
->info
.ns_id
= zns
->ns_id
;
5024 zns_info_list_add_tail(&zdplane_info
.dg_zns_list
, zi
);
5026 if (IS_ZEBRA_DEBUG_DPLANE
)
5027 zlog_debug("%s: nsid %u, new zi %p", __func__
,
5031 /* Make sure we're up-to-date with the zns object */
5032 #if defined(HAVE_NETLINK)
5033 zi
->info
.is_cmd
= false;
5034 zi
->info
.sock
= zns
->netlink_dplane_in
.sock
;
5036 /* Initiate requests for existing info from the OS, and
5037 * begin reading from the netlink socket.
5039 dplane_kernel_info_request(zi
);
5042 if (IS_ZEBRA_DEBUG_DPLANE
)
5043 zlog_debug("%s: nsid %u, deleting zi %p", __func__
,
5046 /* Stop reading, free memory */
5047 zns_info_list_del(&zdplane_info
.dg_zns_list
, zi
);
5049 /* Stop any outstanding tasks */
5050 if (zdplane_info
.dg_master
) {
5051 thread_cancel_async(zdplane_info
.dg_master
,
5052 &zi
->t_request
, NULL
);
5054 thread_cancel_async(zdplane_info
.dg_master
, &zi
->t_read
,
5058 XFREE(MTYPE_DP_NS
, zi
);
5063 * Provider api to signal that work/events are available
5064 * for the dataplane pthread.
5066 int dplane_provider_work_ready(void)
5068 /* Note that during zebra startup, we may be offered work before
5069 * the dataplane pthread (and thread-master) are ready. We want to
5070 * enqueue the work, but the event-scheduling machinery may not be
5073 if (zdplane_info
.dg_run
) {
5074 thread_add_event(zdplane_info
.dg_master
,
5075 dplane_thread_loop
, NULL
, 0,
5076 &zdplane_info
.dg_t_update
);
5083 * Enqueue a context directly to zebra main.
5085 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx
*ctx
)
5087 struct dplane_ctx_q temp_list
;
5089 /* Zebra's api takes a list, so we need to use a temporary list */
5090 TAILQ_INIT(&temp_list
);
5092 TAILQ_INSERT_TAIL(&temp_list
, ctx
, zd_q_entries
);
5093 (zdplane_info
.dg_results_cb
)(&temp_list
);
5097 * Kernel dataplane provider
5100 static void kernel_dplane_log_detail(struct zebra_dplane_ctx
*ctx
)
5102 char buf
[PREFIX_STRLEN
];
5104 switch (dplane_ctx_get_op(ctx
)) {
5106 case DPLANE_OP_ROUTE_INSTALL
:
5107 case DPLANE_OP_ROUTE_UPDATE
:
5108 case DPLANE_OP_ROUTE_DELETE
:
5109 zlog_debug("%u:%pFX Dplane route update ctx %p op %s",
5110 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_dest(ctx
),
5111 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
5114 case DPLANE_OP_NH_INSTALL
:
5115 case DPLANE_OP_NH_UPDATE
:
5116 case DPLANE_OP_NH_DELETE
:
5117 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
5118 dplane_ctx_get_nhe_id(ctx
), ctx
,
5119 dplane_op2str(dplane_ctx_get_op(ctx
)));
5122 case DPLANE_OP_LSP_INSTALL
:
5123 case DPLANE_OP_LSP_UPDATE
:
5124 case DPLANE_OP_LSP_DELETE
:
5127 case DPLANE_OP_PW_INSTALL
:
5128 case DPLANE_OP_PW_UNINSTALL
:
5129 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
5130 dplane_ctx_get_ifname(ctx
),
5131 dplane_op2str(ctx
->zd_op
), dplane_ctx_get_pw_af(ctx
),
5132 dplane_ctx_get_pw_local_label(ctx
),
5133 dplane_ctx_get_pw_remote_label(ctx
));
5136 case DPLANE_OP_ADDR_INSTALL
:
5137 case DPLANE_OP_ADDR_UNINSTALL
:
5138 zlog_debug("Dplane intf %s, idx %u, addr %pFX",
5139 dplane_op2str(dplane_ctx_get_op(ctx
)),
5140 dplane_ctx_get_ifindex(ctx
),
5141 dplane_ctx_get_intf_addr(ctx
));
5144 case DPLANE_OP_MAC_INSTALL
:
5145 case DPLANE_OP_MAC_DELETE
:
5146 prefix_mac2str(dplane_ctx_mac_get_addr(ctx
), buf
,
5149 zlog_debug("Dplane %s, mac %s, ifindex %u",
5150 dplane_op2str(dplane_ctx_get_op(ctx
)),
5151 buf
, dplane_ctx_get_ifindex(ctx
));
5154 case DPLANE_OP_NEIGH_INSTALL
:
5155 case DPLANE_OP_NEIGH_UPDATE
:
5156 case DPLANE_OP_NEIGH_DELETE
:
5157 case DPLANE_OP_VTEP_ADD
:
5158 case DPLANE_OP_VTEP_DELETE
:
5159 case DPLANE_OP_NEIGH_DISCOVER
:
5160 case DPLANE_OP_NEIGH_IP_INSTALL
:
5161 case DPLANE_OP_NEIGH_IP_DELETE
:
5162 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx
), buf
,
5165 zlog_debug("Dplane %s, ip %s, ifindex %u",
5166 dplane_op2str(dplane_ctx_get_op(ctx
)),
5167 buf
, dplane_ctx_get_ifindex(ctx
));
5170 case DPLANE_OP_RULE_ADD
:
5171 case DPLANE_OP_RULE_DELETE
:
5172 case DPLANE_OP_RULE_UPDATE
:
5173 zlog_debug("Dplane rule update op %s, if %s(%u), ctx %p",
5174 dplane_op2str(dplane_ctx_get_op(ctx
)),
5175 dplane_ctx_get_ifname(ctx
),
5176 dplane_ctx_get_ifindex(ctx
), ctx
);
5179 case DPLANE_OP_SYS_ROUTE_ADD
:
5180 case DPLANE_OP_SYS_ROUTE_DELETE
:
5181 case DPLANE_OP_ROUTE_NOTIFY
:
5182 case DPLANE_OP_LSP_NOTIFY
:
5183 case DPLANE_OP_BR_PORT_UPDATE
:
5185 case DPLANE_OP_NONE
:
5188 case DPLANE_OP_IPTABLE_ADD
:
5189 case DPLANE_OP_IPTABLE_DELETE
: {
5190 struct zebra_pbr_iptable ipt
;
5192 dplane_ctx_get_pbr_iptable(ctx
, &ipt
);
5193 zlog_debug("Dplane iptable update op %s, unique(%u), ctx %p",
5194 dplane_op2str(dplane_ctx_get_op(ctx
)), ipt
.unique
,
5197 case DPLANE_OP_IPSET_ADD
:
5198 case DPLANE_OP_IPSET_DELETE
: {
5199 struct zebra_pbr_ipset ipset
;
5201 dplane_ctx_get_pbr_ipset(ctx
, &ipset
);
5202 zlog_debug("Dplane ipset update op %s, unique(%u), ctx %p",
5203 dplane_op2str(dplane_ctx_get_op(ctx
)), ipset
.unique
,
5206 case DPLANE_OP_IPSET_ENTRY_ADD
:
5207 case DPLANE_OP_IPSET_ENTRY_DELETE
: {
5208 struct zebra_pbr_ipset_entry ipent
;
5210 dplane_ctx_get_pbr_ipset_entry(ctx
, &ipent
);
5212 "Dplane ipset entry update op %s, unique(%u), ctx %p",
5213 dplane_op2str(dplane_ctx_get_op(ctx
)), ipent
.unique
,
5216 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
5217 zlog_debug("Dplane neigh table op %s, ifp %s, family %s",
5218 dplane_op2str(dplane_ctx_get_op(ctx
)),
5219 dplane_ctx_get_ifname(ctx
),
5220 family2str(dplane_ctx_neightable_get_family(ctx
)));
5222 case DPLANE_OP_GRE_SET
:
5223 zlog_debug("Dplane gre set op %s, ifp %s, link %u",
5224 dplane_op2str(dplane_ctx_get_op(ctx
)),
5225 dplane_ctx_get_ifname(ctx
),
5226 ctx
->u
.gre
.link_ifindex
);
5229 case DPLANE_OP_INTF_ADDR_ADD
:
5230 case DPLANE_OP_INTF_ADDR_DEL
:
5231 zlog_debug("Dplane incoming op %s, intf %s, addr %pFX",
5232 dplane_op2str(dplane_ctx_get_op(ctx
)),
5233 dplane_ctx_get_ifname(ctx
),
5234 dplane_ctx_get_intf_addr(ctx
));
5237 case DPLANE_OP_INTF_NETCONFIG
:
5238 zlog_debug("%s: ifindex %d, mpls %d, mcast %d",
5239 dplane_op2str(dplane_ctx_get_op(ctx
)),
5240 dplane_ctx_get_netconf_ifindex(ctx
),
5241 dplane_ctx_get_netconf_mpls(ctx
),
5242 dplane_ctx_get_netconf_mcast(ctx
));
5247 static void kernel_dplane_handle_result(struct zebra_dplane_ctx
*ctx
)
5249 enum zebra_dplane_result res
= dplane_ctx_get_status(ctx
);
5251 switch (dplane_ctx_get_op(ctx
)) {
5253 case DPLANE_OP_ROUTE_INSTALL
:
5254 case DPLANE_OP_ROUTE_UPDATE
:
5255 case DPLANE_OP_ROUTE_DELETE
:
5256 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5257 atomic_fetch_add_explicit(&zdplane_info
.dg_route_errors
,
5258 1, memory_order_relaxed
);
5260 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
)
5261 && (res
== ZEBRA_DPLANE_REQUEST_SUCCESS
)) {
5262 struct nexthop
*nexthop
;
5264 /* Update installed nexthops to signal which have been
5267 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
5269 if (CHECK_FLAG(nexthop
->flags
,
5270 NEXTHOP_FLAG_RECURSIVE
))
5273 if (CHECK_FLAG(nexthop
->flags
,
5274 NEXTHOP_FLAG_ACTIVE
)) {
5275 SET_FLAG(nexthop
->flags
,
5282 case DPLANE_OP_NH_INSTALL
:
5283 case DPLANE_OP_NH_UPDATE
:
5284 case DPLANE_OP_NH_DELETE
:
5285 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5286 atomic_fetch_add_explicit(
5287 &zdplane_info
.dg_nexthop_errors
, 1,
5288 memory_order_relaxed
);
5291 case DPLANE_OP_LSP_INSTALL
:
5292 case DPLANE_OP_LSP_UPDATE
:
5293 case DPLANE_OP_LSP_DELETE
:
5294 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5295 atomic_fetch_add_explicit(&zdplane_info
.dg_lsp_errors
,
5296 1, memory_order_relaxed
);
5299 case DPLANE_OP_PW_INSTALL
:
5300 case DPLANE_OP_PW_UNINSTALL
:
5301 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5302 atomic_fetch_add_explicit(&zdplane_info
.dg_pw_errors
, 1,
5303 memory_order_relaxed
);
5306 case DPLANE_OP_ADDR_INSTALL
:
5307 case DPLANE_OP_ADDR_UNINSTALL
:
5308 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5309 atomic_fetch_add_explicit(
5310 &zdplane_info
.dg_intf_addr_errors
, 1,
5311 memory_order_relaxed
);
5314 case DPLANE_OP_MAC_INSTALL
:
5315 case DPLANE_OP_MAC_DELETE
:
5316 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5317 atomic_fetch_add_explicit(&zdplane_info
.dg_mac_errors
,
5318 1, memory_order_relaxed
);
5321 case DPLANE_OP_NEIGH_INSTALL
:
5322 case DPLANE_OP_NEIGH_UPDATE
:
5323 case DPLANE_OP_NEIGH_DELETE
:
5324 case DPLANE_OP_VTEP_ADD
:
5325 case DPLANE_OP_VTEP_DELETE
:
5326 case DPLANE_OP_NEIGH_DISCOVER
:
5327 case DPLANE_OP_NEIGH_IP_INSTALL
:
5328 case DPLANE_OP_NEIGH_IP_DELETE
:
5329 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5330 atomic_fetch_add_explicit(&zdplane_info
.dg_neigh_errors
,
5331 1, memory_order_relaxed
);
5334 case DPLANE_OP_RULE_ADD
:
5335 case DPLANE_OP_RULE_DELETE
:
5336 case DPLANE_OP_RULE_UPDATE
:
5337 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5338 atomic_fetch_add_explicit(&zdplane_info
.dg_rule_errors
,
5339 1, memory_order_relaxed
);
5342 case DPLANE_OP_IPTABLE_ADD
:
5343 case DPLANE_OP_IPTABLE_DELETE
:
5344 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5345 atomic_fetch_add_explicit(
5346 &zdplane_info
.dg_iptable_errors
, 1,
5347 memory_order_relaxed
);
5350 case DPLANE_OP_IPSET_ADD
:
5351 case DPLANE_OP_IPSET_DELETE
:
5352 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5353 atomic_fetch_add_explicit(&zdplane_info
.dg_ipset_errors
,
5354 1, memory_order_relaxed
);
5357 case DPLANE_OP_IPSET_ENTRY_ADD
:
5358 case DPLANE_OP_IPSET_ENTRY_DELETE
:
5359 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5360 atomic_fetch_add_explicit(
5361 &zdplane_info
.dg_ipset_entry_errors
, 1,
5362 memory_order_relaxed
);
5365 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
5366 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5367 atomic_fetch_add_explicit(
5368 &zdplane_info
.dg_neightable_errors
, 1,
5369 memory_order_relaxed
);
5372 case DPLANE_OP_GRE_SET
:
5373 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5374 atomic_fetch_add_explicit(
5375 &zdplane_info
.dg_gre_set_errors
, 1,
5376 memory_order_relaxed
);
5378 /* Ignore 'notifications' - no-op */
5379 case DPLANE_OP_SYS_ROUTE_ADD
:
5380 case DPLANE_OP_SYS_ROUTE_DELETE
:
5381 case DPLANE_OP_ROUTE_NOTIFY
:
5382 case DPLANE_OP_LSP_NOTIFY
:
5383 case DPLANE_OP_BR_PORT_UPDATE
:
5386 /* TODO -- error counters for incoming events? */
5387 case DPLANE_OP_INTF_ADDR_ADD
:
5388 case DPLANE_OP_INTF_ADDR_DEL
:
5389 case DPLANE_OP_INTF_NETCONFIG
:
5392 case DPLANE_OP_NONE
:
5393 if (res
!= ZEBRA_DPLANE_REQUEST_SUCCESS
)
5394 atomic_fetch_add_explicit(&zdplane_info
.dg_other_errors
,
5395 1, memory_order_relaxed
);
5400 static void kernel_dplane_process_iptable(struct zebra_dplane_provider
*prov
,
5401 struct zebra_dplane_ctx
*ctx
)
5403 zebra_pbr_process_iptable(ctx
);
5404 dplane_provider_enqueue_out_ctx(prov
, ctx
);
5407 static void kernel_dplane_process_ipset(struct zebra_dplane_provider
*prov
,
5408 struct zebra_dplane_ctx
*ctx
)
5410 zebra_pbr_process_ipset(ctx
);
5411 dplane_provider_enqueue_out_ctx(prov
, ctx
);
5415 kernel_dplane_process_ipset_entry(struct zebra_dplane_provider
*prov
,
5416 struct zebra_dplane_ctx
*ctx
)
5418 zebra_pbr_process_ipset_entry(ctx
);
5419 dplane_provider_enqueue_out_ctx(prov
, ctx
);
5423 * Kernel provider callback
5425 static int kernel_dplane_process_func(struct zebra_dplane_provider
*prov
)
5427 struct zebra_dplane_ctx
*ctx
, *tctx
;
5428 struct dplane_ctx_q work_list
;
5431 TAILQ_INIT(&work_list
);
5433 limit
= dplane_provider_get_work_limit(prov
);
5435 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5436 zlog_debug("dplane provider '%s': processing",
5437 dplane_provider_get_name(prov
));
5439 for (counter
= 0; counter
< limit
; counter
++) {
5440 ctx
= dplane_provider_dequeue_in_ctx(prov
);
5443 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5444 kernel_dplane_log_detail(ctx
);
5446 if ((dplane_ctx_get_op(ctx
) == DPLANE_OP_IPTABLE_ADD
5447 || dplane_ctx_get_op(ctx
) == DPLANE_OP_IPTABLE_DELETE
))
5448 kernel_dplane_process_iptable(prov
, ctx
);
5449 else if ((dplane_ctx_get_op(ctx
) == DPLANE_OP_IPSET_ADD
5450 || dplane_ctx_get_op(ctx
) == DPLANE_OP_IPSET_DELETE
))
5451 kernel_dplane_process_ipset(prov
, ctx
);
5452 else if ((dplane_ctx_get_op(ctx
) == DPLANE_OP_IPSET_ENTRY_ADD
5453 || dplane_ctx_get_op(ctx
)
5454 == DPLANE_OP_IPSET_ENTRY_DELETE
))
5455 kernel_dplane_process_ipset_entry(prov
, ctx
);
5457 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
5460 kernel_update_multi(&work_list
);
5462 TAILQ_FOREACH_SAFE (ctx
, &work_list
, zd_q_entries
, tctx
) {
5463 kernel_dplane_handle_result(ctx
);
5465 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
5466 dplane_provider_enqueue_out_ctx(prov
, ctx
);
5469 /* Ensure that we'll run the work loop again if there's still
5472 if (counter
>= limit
) {
5473 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5474 zlog_debug("dplane provider '%s' reached max updates %d",
5475 dplane_provider_get_name(prov
), counter
);
5477 atomic_fetch_add_explicit(&zdplane_info
.dg_update_yields
,
5478 1, memory_order_relaxed
);
5480 dplane_provider_work_ready();
5486 #ifdef DPLANE_TEST_PROVIDER
5489 * Test dataplane provider plugin
5493 * Test provider process callback
5495 static int test_dplane_process_func(struct zebra_dplane_provider
*prov
)
5497 struct zebra_dplane_ctx
*ctx
;
5500 /* Just moving from 'in' queue to 'out' queue */
5502 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5503 zlog_debug("dplane provider '%s': processing",
5504 dplane_provider_get_name(prov
));
5506 limit
= dplane_provider_get_work_limit(prov
);
5508 for (counter
= 0; counter
< limit
; counter
++) {
5509 ctx
= dplane_provider_dequeue_in_ctx(prov
);
5513 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5514 zlog_debug("dplane provider '%s': op %s",
5515 dplane_provider_get_name(prov
),
5516 dplane_op2str(dplane_ctx_get_op(ctx
)));
5518 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
5520 dplane_provider_enqueue_out_ctx(prov
, ctx
);
5523 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5524 zlog_debug("dplane provider '%s': processed %d",
5525 dplane_provider_get_name(prov
), counter
);
5527 /* Ensure that we'll run the work loop again if there's still
5530 if (counter
>= limit
)
5531 dplane_provider_work_ready();
5537 * Test provider shutdown/fini callback
5539 static int test_dplane_shutdown_func(struct zebra_dplane_provider
*prov
,
5542 if (IS_ZEBRA_DEBUG_DPLANE
)
5543 zlog_debug("dplane provider '%s': %sshutdown",
5544 dplane_provider_get_name(prov
),
5545 early
? "early " : "");
5549 #endif /* DPLANE_TEST_PROVIDER */
5552 * Register default kernel provider
5554 static void dplane_provider_init(void)
5558 ret
= dplane_provider_register("Kernel",
5560 DPLANE_PROV_FLAGS_DEFAULT
, NULL
,
5561 kernel_dplane_process_func
,
5566 zlog_err("Unable to register kernel dplane provider: %d",
5569 #ifdef DPLANE_TEST_PROVIDER
5570 /* Optional test provider ... */
5571 ret
= dplane_provider_register("Test",
5572 DPLANE_PRIO_PRE_KERNEL
,
5573 DPLANE_PROV_FLAGS_DEFAULT
, NULL
,
5574 test_dplane_process_func
,
5575 test_dplane_shutdown_func
,
5576 NULL
/* data */, NULL
);
5579 zlog_err("Unable to register test dplane provider: %d",
5581 #endif /* DPLANE_TEST_PROVIDER */
5585 * Allow zebra code to walk the queue of pending contexts, evaluate each one
5586 * using a callback function. If the function returns 'true', the context
5587 * will be dequeued and freed without being processed.
5589 int dplane_clean_ctx_queue(bool (*context_cb
)(struct zebra_dplane_ctx
*ctx
,
5590 void *arg
), void *val
)
5592 struct zebra_dplane_ctx
*ctx
, *temp
;
5593 struct dplane_ctx_q work_list
;
5595 TAILQ_INIT(&work_list
);
5597 if (context_cb
== NULL
)
5600 /* Walk the pending context queue under the dplane lock. */
5603 TAILQ_FOREACH_SAFE(ctx
, &zdplane_info
.dg_update_ctx_q
, zd_q_entries
,
5605 if (context_cb(ctx
, val
)) {
5606 TAILQ_REMOVE(&zdplane_info
.dg_update_ctx_q
, ctx
,
5608 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
5614 /* Now free any contexts selected by the caller, without holding
5617 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, temp
) {
5618 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
5619 dplane_ctx_fini(&ctx
);
5627 /* Indicates zebra shutdown/exit is in progress. Some operations may be
5628 * simplified or skipped during shutdown processing.
5630 bool dplane_is_in_shutdown(void)
5632 return zdplane_info
.dg_is_shutdown
;
5636 * Enable collection of extra info about interfaces in route updates.
5638 void dplane_enable_intf_extra_info(void)
5640 dplane_collect_extra_intf_info
= true;
5644 * Early or pre-shutdown, de-init notification api. This runs pretty
5645 * early during zebra shutdown, as a signal to stop new work and prepare
5646 * for updates generated by shutdown/cleanup activity, as zebra tries to
5647 * remove everything it's responsible for.
5648 * NB: This runs in the main zebra pthread context.
5650 void zebra_dplane_pre_finish(void)
5652 struct zebra_dplane_provider
*prov
;
5654 if (IS_ZEBRA_DEBUG_DPLANE
)
5655 zlog_debug("Zebra dataplane pre-finish called");
5657 zdplane_info
.dg_is_shutdown
= true;
5659 /* Notify provider(s) of pending shutdown. */
5660 TAILQ_FOREACH(prov
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
5661 if (prov
->dp_fini
== NULL
)
5664 prov
->dp_fini(prov
, true /* early */);
5669 * Utility to determine whether work remains enqueued within the dplane;
5670 * used during system shutdown processing.
5672 static bool dplane_work_pending(void)
5675 struct zebra_dplane_ctx
*ctx
;
5676 struct zebra_dplane_provider
*prov
;
5678 /* TODO -- just checking incoming/pending work for now, must check
5683 ctx
= TAILQ_FIRST(&zdplane_info
.dg_update_ctx_q
);
5684 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
5695 dplane_provider_lock(prov
);
5697 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_in_q
));
5699 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
5701 dplane_provider_unlock(prov
);
5707 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
5719 * Shutdown-time intermediate callback, used to determine when all pending
5720 * in-flight updates are done. If there's still work to do, reschedules itself.
5721 * If all work is done, schedules an event to the main zebra thread for
5722 * final zebra shutdown.
5723 * This runs in the dplane pthread context.
5725 static void dplane_check_shutdown_status(struct thread
*event
)
5727 struct dplane_zns_info
*zi
;
5729 if (IS_ZEBRA_DEBUG_DPLANE
)
5730 zlog_debug("Zebra dataplane shutdown status check called");
5732 /* Remove any zns info entries as we stop the dplane pthread. */
5733 frr_each_safe (zns_info_list
, &zdplane_info
.dg_zns_list
, zi
) {
5734 zns_info_list_del(&zdplane_info
.dg_zns_list
, zi
);
5736 if (zdplane_info
.dg_master
) {
5737 thread_cancel(&zi
->t_read
);
5738 thread_cancel(&zi
->t_request
);
5741 XFREE(MTYPE_DP_NS
, zi
);
5744 if (dplane_work_pending()) {
5745 /* Reschedule dplane check on a short timer */
5746 thread_add_timer_msec(zdplane_info
.dg_master
,
5747 dplane_check_shutdown_status
,
5749 &zdplane_info
.dg_t_shutdown_check
);
5751 /* TODO - give up and stop waiting after a short time? */
5754 /* We appear to be done - schedule a final callback event
5755 * for the zebra main pthread.
5757 thread_add_event(zrouter
.master
, zebra_finalize
, NULL
, 0, NULL
);
5762 * Shutdown, de-init api. This runs pretty late during shutdown,
5763 * after zebra has tried to free/remove/uninstall all routes during shutdown.
5764 * At this point, dplane work may still remain to be done, so we can't just
5765 * blindly terminate. If there's still work to do, we'll periodically check
5766 * and when done, we'll enqueue a task to the zebra main thread for final
5767 * termination processing.
5769 * NB: This runs in the main zebra thread context.
5771 void zebra_dplane_finish(void)
5773 if (IS_ZEBRA_DEBUG_DPLANE
)
5774 zlog_debug("Zebra dataplane fini called");
5776 thread_add_event(zdplane_info
.dg_master
,
5777 dplane_check_shutdown_status
, NULL
, 0,
5778 &zdplane_info
.dg_t_shutdown_check
);
5782 * Main dataplane pthread event loop. The thread takes new incoming work
5783 * and offers it to the first provider. It then iterates through the
5784 * providers, taking complete work from each one and offering it
5785 * to the next in order. At each step, a limited number of updates are
5786 * processed during a cycle in order to provide some fairness.
5788 * This loop through the providers is only run once, so that the dataplane
5789 * pthread can look for other pending work - such as i/o work on behalf of
5792 static void dplane_thread_loop(struct thread
*event
)
5794 struct dplane_ctx_q work_list
;
5795 struct dplane_ctx_q error_list
;
5796 struct zebra_dplane_provider
*prov
;
5797 struct zebra_dplane_ctx
*ctx
, *tctx
;
5798 int limit
, counter
, error_counter
;
5799 uint64_t curr
, high
;
5800 bool reschedule
= false;
5802 /* Capture work limit per cycle */
5803 limit
= zdplane_info
.dg_updates_per_cycle
;
5805 /* Init temporary lists used to move contexts among providers */
5806 TAILQ_INIT(&work_list
);
5807 TAILQ_INIT(&error_list
);
5810 /* Check for zebra shutdown */
5811 if (!zdplane_info
.dg_run
)
5814 /* Dequeue some incoming work from zebra (if any) onto the temporary
5819 /* Locate initial registered provider */
5820 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
5822 /* Move new work from incoming list to temp list */
5823 for (counter
= 0; counter
< limit
; counter
++) {
5824 ctx
= TAILQ_FIRST(&zdplane_info
.dg_update_ctx_q
);
5826 TAILQ_REMOVE(&zdplane_info
.dg_update_ctx_q
, ctx
,
5829 ctx
->zd_provider
= prov
->dp_id
;
5831 TAILQ_INSERT_TAIL(&work_list
, ctx
, zd_q_entries
);
5839 atomic_fetch_sub_explicit(&zdplane_info
.dg_routes_queued
, counter
,
5840 memory_order_relaxed
);
5842 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5843 zlog_debug("dplane: incoming new work counter: %d", counter
);
5845 /* Iterate through the registered providers, offering new incoming
5846 * work. If the provider has outgoing work in its queue, take that
5847 * work for the next provider
5851 /* At each iteration, the temporary work list has 'counter'
5854 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5855 zlog_debug("dplane enqueues %d new work to provider '%s'",
5856 counter
, dplane_provider_get_name(prov
));
5858 /* Capture current provider id in each context; check for
5861 TAILQ_FOREACH_SAFE(ctx
, &work_list
, zd_q_entries
, tctx
) {
5862 if (dplane_ctx_get_status(ctx
) ==
5863 ZEBRA_DPLANE_REQUEST_SUCCESS
) {
5864 ctx
->zd_provider
= prov
->dp_id
;
5867 * TODO -- improve error-handling: recirc
5868 * errors backwards so that providers can
5869 * 'undo' their work (if they want to)
5872 /* Move to error list; will be returned
5875 TAILQ_REMOVE(&work_list
, ctx
, zd_q_entries
);
5876 TAILQ_INSERT_TAIL(&error_list
,
5882 /* Enqueue new work to the provider */
5883 dplane_provider_lock(prov
);
5885 if (TAILQ_FIRST(&work_list
))
5886 TAILQ_CONCAT(&(prov
->dp_ctx_in_q
), &work_list
,
5889 atomic_fetch_add_explicit(&prov
->dp_in_counter
, counter
,
5890 memory_order_relaxed
);
5891 atomic_fetch_add_explicit(&prov
->dp_in_queued
, counter
,
5892 memory_order_relaxed
);
5893 curr
= atomic_load_explicit(&prov
->dp_in_queued
,
5894 memory_order_relaxed
);
5895 high
= atomic_load_explicit(&prov
->dp_in_max
,
5896 memory_order_relaxed
);
5898 atomic_store_explicit(&prov
->dp_in_max
, curr
,
5899 memory_order_relaxed
);
5901 dplane_provider_unlock(prov
);
5903 /* Reset the temp list (though the 'concat' may have done this
5904 * already), and the counter
5906 TAILQ_INIT(&work_list
);
5909 /* Call into the provider code. Note that this is
5910 * unconditional: we offer to do work even if we don't enqueue
5913 (*prov
->dp_fp
)(prov
);
5915 /* Check for zebra shutdown */
5916 if (!zdplane_info
.dg_run
)
5919 /* Dequeue completed work from the provider */
5920 dplane_provider_lock(prov
);
5922 while (counter
< limit
) {
5923 ctx
= TAILQ_FIRST(&(prov
->dp_ctx_out_q
));
5925 TAILQ_REMOVE(&(prov
->dp_ctx_out_q
), ctx
,
5928 TAILQ_INSERT_TAIL(&work_list
,
5935 dplane_provider_unlock(prov
);
5937 if (counter
>= limit
)
5940 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5941 zlog_debug("dplane dequeues %d completed work from provider %s",
5942 counter
, dplane_provider_get_name(prov
));
5944 /* Locate next provider */
5946 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
5951 * We hit the work limit while processing at least one provider's
5952 * output queue - ensure we come back and finish it.
5955 dplane_provider_work_ready();
5957 /* After all providers have been serviced, enqueue any completed
5958 * work and any errors back to zebra so it can process the results.
5960 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
5961 zlog_debug("dplane has %d completed, %d errors, for zebra main",
5962 counter
, error_counter
);
5965 * Hand lists through the api to zebra main,
5966 * to reduce the number of lock/unlock cycles
5969 /* Call through to zebra main */
5970 (zdplane_info
.dg_results_cb
)(&error_list
);
5972 TAILQ_INIT(&error_list
);
5974 /* Call through to zebra main */
5975 (zdplane_info
.dg_results_cb
)(&work_list
);
5977 TAILQ_INIT(&work_list
);
5981 * Final phase of shutdown, after all work enqueued to dplane has been
5982 * processed. This is called from the zebra main pthread context.
5984 void zebra_dplane_shutdown(void)
5986 struct zebra_dplane_provider
*dp
;
5988 if (IS_ZEBRA_DEBUG_DPLANE
)
5989 zlog_debug("Zebra dataplane shutdown called");
5991 /* Stop dplane thread, if it's running */
5993 zdplane_info
.dg_run
= false;
5995 if (zdplane_info
.dg_t_update
)
5996 thread_cancel_async(zdplane_info
.dg_t_update
->master
,
5997 &zdplane_info
.dg_t_update
, NULL
);
5999 frr_pthread_stop(zdplane_info
.dg_pthread
, NULL
);
6001 /* Destroy pthread */
6002 frr_pthread_destroy(zdplane_info
.dg_pthread
);
6003 zdplane_info
.dg_pthread
= NULL
;
6004 zdplane_info
.dg_master
= NULL
;
6006 /* Notify provider(s) of final shutdown.
6007 * Note that this call is in the main pthread, so providers must
6008 * be prepared for that.
6010 TAILQ_FOREACH(dp
, &zdplane_info
.dg_providers_q
, dp_prov_link
) {
6011 if (dp
->dp_fini
== NULL
)
6014 dp
->dp_fini(dp
, false);
6017 /* TODO -- Clean-up provider objects */
6019 /* TODO -- Clean queue(s), free memory */
6023 * Initialize the dataplane module during startup, internal/private version
6025 static void zebra_dplane_init_internal(void)
6027 memset(&zdplane_info
, 0, sizeof(zdplane_info
));
6029 pthread_mutex_init(&zdplane_info
.dg_mutex
, NULL
);
6031 TAILQ_INIT(&zdplane_info
.dg_update_ctx_q
);
6032 TAILQ_INIT(&zdplane_info
.dg_providers_q
);
6033 zns_info_list_init(&zdplane_info
.dg_zns_list
);
6035 zdplane_info
.dg_updates_per_cycle
= DPLANE_DEFAULT_NEW_WORK
;
6037 zdplane_info
.dg_max_queued_updates
= DPLANE_DEFAULT_MAX_QUEUED
;
6039 /* Register default kernel 'provider' during init */
6040 dplane_provider_init();
6044 * Start the dataplane pthread. This step needs to be run later than the
6045 * 'init' step, in case zebra has fork-ed.
6047 void zebra_dplane_start(void)
6049 struct dplane_zns_info
*zi
;
6050 struct zebra_dplane_provider
*prov
;
6051 struct frr_pthread_attr pattr
= {
6052 .start
= frr_pthread_attr_default
.start
,
6053 .stop
= frr_pthread_attr_default
.stop
6056 /* Start dataplane pthread */
6058 zdplane_info
.dg_pthread
= frr_pthread_new(&pattr
, "Zebra dplane thread",
6061 zdplane_info
.dg_master
= zdplane_info
.dg_pthread
->master
;
6063 zdplane_info
.dg_run
= true;
6065 /* Enqueue an initial event for the dataplane pthread */
6066 thread_add_event(zdplane_info
.dg_master
, dplane_thread_loop
, NULL
, 0,
6067 &zdplane_info
.dg_t_update
);
6069 /* Enqueue requests and reads if necessary */
6070 frr_each (zns_info_list
, &zdplane_info
.dg_zns_list
, zi
) {
6071 #if defined(HAVE_NETLINK)
6072 thread_add_read(zdplane_info
.dg_master
, dplane_incoming_read
,
6073 zi
, zi
->info
.sock
, &zi
->t_read
);
6074 dplane_kernel_info_request(zi
);
6078 /* Call start callbacks for registered providers */
6081 prov
= TAILQ_FIRST(&zdplane_info
.dg_providers_q
);
6087 (prov
->dp_start
)(prov
);
6089 /* Locate next provider */
6091 prov
= TAILQ_NEXT(prov
, dp_prov_link
);
6095 frr_pthread_run(zdplane_info
.dg_pthread
, NULL
);
6099 * Initialize the dataplane module at startup; called by zebra rib_init()
6101 void zebra_dplane_init(int (*results_fp
)(struct dplane_ctx_q
*))
6103 zebra_dplane_init_internal();
6104 zdplane_info
.dg_results_cb
= results_fp
;