2 * Zebra dataplane layer.
3 * Copyright (c) 2018 Volta Networks, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "lib/zebra.h"
21 #include "lib/libfrr.h"
22 #include "lib/memory.h"
23 #include "lib/frr_pthread.h"
24 #include "lib/queue.h"
25 #include "zebra/zebra_memory.h"
26 #include "zebra/zserv.h"
27 #include "zebra/zebra_dplane.h"
29 #include "zebra/debug.h"
31 /* Memory type for context blocks */
32 DEFINE_MTYPE(ZEBRA
, DP_CTX
, "Zebra DPlane Ctx")
33 DEFINE_MTYPE(ZEBRA
, DP_PROV
, "Zebra DPlane Provider")
39 /* Validation value for context blocks */
40 const uint32_t DPLANE_CTX_MAGIC
= 0xb97a557f;
42 /* Validation check macro for context blocks */
43 /* #define DPLANE_DEBUG 1 */
47 # define DPLANE_CTX_VALID(p) \
48 assert((p) && ((p)->zd_magic == DPLANE_CTX_MAGIC))
52 # define DPLANE_CTX_VALID(p)
54 #endif /* DPLANE_DEBUG */
57 * The context block used to exchange info about route updates across
58 * the boundary between the zebra main context (and pthread) and the
59 * dataplane layer (and pthread).
61 struct zebra_dplane_ctx_s
{
64 enum dplane_op_e zd_op
;
66 /* Status on return */
67 enum zebra_dplane_result zd_status
;
69 /* TODO -- internal/sub-operation status? */
70 enum zebra_dplane_status zd_remote_status
;
71 enum zebra_dplane_status zd_kernel_status
;
73 /* Dest and (optional) source prefixes */
74 struct prefix zd_dest
;
91 route_tag_t zd_old_tag
;
94 uint16_t zd_old_instance
;
97 uint8_t zd_old_distance
;
100 uint32_t zd_nexthop_mtu
;
103 struct zebra_dplane_info zd_ns_info
;
106 struct nexthop_group zd_ng
;
108 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
110 /* Embedded list linkage */
111 TAILQ_ENTRY(zebra_dplane_ctx_s
) zd_q_entries
;
113 /* Magic validation value */
118 * Registration block for one dataplane provider.
120 struct zebra_dplane_provider_s
{
122 char dp_name
[DPLANE_PROVIDER_NAMELEN
+ 1];
124 /* Priority, for ordering among providers */
130 dplane_provider_process_fp dp_fp
;
132 dplane_provider_fini_fp dp_fini
;
134 /* Embedded list linkage */
135 TAILQ_ENTRY(zebra_dplane_provider_s
) dp_q_providers
;
142 static struct zebra_dplane_globals_s
{
143 /* Mutex to control access to dataplane components */
144 pthread_mutex_t dg_mutex
;
146 /* Results callback registered by zebra 'core' */
147 dplane_results_fp dg_results_cb
;
149 /* Route-update context queue inbound to the dataplane */
150 TAILQ_HEAD(zdg_ctx_q
, zebra_dplane_ctx_s
) dg_route_ctx_q
;
152 /* Ordered list of providers */
153 TAILQ_HEAD(zdg_prov_q
, zebra_dplane_provider_s
) dg_providers_q
;
155 /* Counter used to assign ids to providers */
156 uint32_t dg_provider_id
;
158 /* Event-delivery context 'master' for the dplane */
159 struct thread_master
*dg_master
;
161 /* Event/'thread' pointer for queued updates */
162 struct thread
*dg_t_update
;
167 * Lock and unlock for interactions with the zebra 'core'
169 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_g.dg_mutex)
171 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_g.dg_mutex)
174 static int dplane_route_process(struct thread
*event
);
181 * Allocate a dataplane update context
183 static dplane_ctx_h
dplane_ctx_alloc(void)
185 struct zebra_dplane_ctx_s
*p
;
187 /* TODO -- just alloc'ing memory, but would like to maintain
190 p
= XCALLOC(MTYPE_DP_CTX
, sizeof(struct zebra_dplane_ctx_s
));
192 p
->zd_magic
= DPLANE_CTX_MAGIC
;
198 * Free a dataplane results context.
200 static void dplane_ctx_free(dplane_ctx_h
*pctx
)
203 DPLANE_CTX_VALID(*pctx
);
205 /* TODO -- just freeing memory, but would like to maintain
209 /* Free embedded nexthops */
210 if ((*pctx
)->zd_ng
.nexthop
) {
211 /* This deals with recursive nexthops too */
212 nexthops_free((*pctx
)->zd_ng
.nexthop
);
215 /* Clear validation value */
216 (*pctx
)->zd_magic
= 0;
218 XFREE(MTYPE_DP_CTX
, *pctx
);
224 * Return a context block to the dplane module after processing
226 void dplane_ctx_fini(dplane_ctx_h
*pctx
)
228 /* TODO -- enqueue for next provider; for now, just free */
229 dplane_ctx_free(pctx
);
232 /* Enqueue a context block */
233 void dplane_ctx_enqueue_tail(struct dplane_ctx_q_s
*q
, dplane_ctx_h ctx
)
235 TAILQ_INSERT_TAIL(q
, ctx
, zd_q_entries
);
238 /* Dequeue a context block from the head of a list */
239 void dplane_ctx_dequeue(struct dplane_ctx_q_s
*q
, dplane_ctx_h
*ctxp
)
241 dplane_ctx_h ctx
= TAILQ_FIRST(q
);
244 TAILQ_REMOVE(q
, ctx
, zd_q_entries
);
250 * Accessors for information from the context object
252 enum zebra_dplane_result
dplane_ctx_get_status(const dplane_ctx_h ctx
)
254 DPLANE_CTX_VALID(ctx
);
256 return ctx
->zd_status
;
259 enum dplane_op_e
dplane_ctx_get_op(const dplane_ctx_h ctx
)
261 DPLANE_CTX_VALID(ctx
);
266 const char *dplane_op2str(enum dplane_op_e op
)
268 const char *ret
= "UNKNOWN";
276 case DPLANE_OP_ROUTE_INSTALL
:
277 ret
= "ROUTE_INSTALL";
279 case DPLANE_OP_ROUTE_UPDATE
:
280 ret
= "ROUTE_UPDATE";
282 case DPLANE_OP_ROUTE_DELETE
:
283 ret
= "ROUTE_DELETE";
291 const struct prefix
*dplane_ctx_get_dest(const dplane_ctx_h ctx
)
293 DPLANE_CTX_VALID(ctx
);
295 return &(ctx
->zd_dest
);
298 /* Source prefix is a little special - return NULL for "no src prefix" */
299 const struct prefix
*dplane_ctx_get_src(const dplane_ctx_h ctx
)
301 DPLANE_CTX_VALID(ctx
);
303 if (ctx
->zd_src
.prefixlen
== 0 &&
304 IN6_IS_ADDR_UNSPECIFIED(&(ctx
->zd_src
.u
.prefix6
))) {
307 return &(ctx
->zd_src
);
311 bool dplane_ctx_is_update(const dplane_ctx_h ctx
)
313 DPLANE_CTX_VALID(ctx
);
315 return ctx
->zd_is_update
;
318 uint32_t dplane_ctx_get_seq(const dplane_ctx_h ctx
)
320 DPLANE_CTX_VALID(ctx
);
325 uint32_t dplane_ctx_get_old_seq(const dplane_ctx_h ctx
)
327 DPLANE_CTX_VALID(ctx
);
329 return ctx
->zd_old_seq
;
332 vrf_id_t
dplane_ctx_get_vrf(const dplane_ctx_h ctx
)
334 DPLANE_CTX_VALID(ctx
);
336 return ctx
->zd_vrf_id
;
339 int dplane_ctx_get_type(const dplane_ctx_h ctx
)
341 DPLANE_CTX_VALID(ctx
);
346 int dplane_ctx_get_old_type(const dplane_ctx_h ctx
)
348 DPLANE_CTX_VALID(ctx
);
350 return ctx
->zd_old_type
;
353 afi_t
dplane_ctx_get_afi(const dplane_ctx_h ctx
)
355 DPLANE_CTX_VALID(ctx
);
360 safi_t
dplane_ctx_get_safi(const dplane_ctx_h ctx
)
362 DPLANE_CTX_VALID(ctx
);
367 uint32_t dplane_ctx_get_table(const dplane_ctx_h ctx
)
369 DPLANE_CTX_VALID(ctx
);
371 return ctx
->zd_table_id
;
374 route_tag_t
dplane_ctx_get_tag(const dplane_ctx_h ctx
)
376 DPLANE_CTX_VALID(ctx
);
381 route_tag_t
dplane_ctx_get_old_tag(const dplane_ctx_h ctx
)
383 DPLANE_CTX_VALID(ctx
);
385 return ctx
->zd_old_tag
;
388 uint16_t dplane_ctx_get_instance(const dplane_ctx_h ctx
)
390 DPLANE_CTX_VALID(ctx
);
392 return ctx
->zd_instance
;
395 uint16_t dplane_ctx_get_old_instance(const dplane_ctx_h ctx
)
397 DPLANE_CTX_VALID(ctx
);
399 return ctx
->zd_instance
;
402 uint32_t dplane_ctx_get_metric(const dplane_ctx_h ctx
)
404 DPLANE_CTX_VALID(ctx
);
406 return ctx
->zd_metric
;
409 uint32_t dplane_ctx_get_mtu(const dplane_ctx_h ctx
)
411 DPLANE_CTX_VALID(ctx
);
416 uint32_t dplane_ctx_get_nh_mtu(const dplane_ctx_h ctx
)
418 DPLANE_CTX_VALID(ctx
);
420 return ctx
->zd_nexthop_mtu
;
423 uint8_t dplane_ctx_get_distance(const dplane_ctx_h ctx
)
425 DPLANE_CTX_VALID(ctx
);
427 return ctx
->zd_distance
;
430 uint8_t dplane_ctx_get_old_distance(const dplane_ctx_h ctx
)
432 DPLANE_CTX_VALID(ctx
);
434 return ctx
->zd_old_distance
;
437 const struct nexthop_group
*dplane_ctx_get_ng(const dplane_ctx_h ctx
)
439 DPLANE_CTX_VALID(ctx
);
441 return &(ctx
->zd_ng
);
444 const struct zebra_dplane_info
*dplane_ctx_get_ns(const dplane_ctx_h ctx
)
446 DPLANE_CTX_VALID(ctx
);
448 return &(ctx
->zd_ns_info
);
452 * End of dplane context accessors
456 * Initialize a context block for a route update from zebra data structs.
458 static int dplane_ctx_route_init(dplane_ctx_h ctx
,
460 struct route_node
*rn
,
461 struct route_entry
*re
)
464 const struct route_table
*table
= NULL
;
465 const rib_table_info_t
*info
;
466 const struct prefix
*p
, *src_p
;
467 struct zebra_ns
*zns
;
468 struct zebra_vrf
*zvrf
;
470 if (!ctx
|| !rn
|| !re
)
475 ctx
->zd_type
= re
->type
;
476 ctx
->zd_old_type
= re
->type
;
478 /* Prefixes: dest, and optional source */
479 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
481 prefix_copy(&(ctx
->zd_dest
), p
);
484 prefix_copy(&(ctx
->zd_src
), src_p
);
486 memset(&(ctx
->zd_src
), 0, sizeof(ctx
->zd_src
));
488 ctx
->zd_table_id
= re
->table
;
490 ctx
->zd_metric
= re
->metric
;
491 ctx
->zd_vrf_id
= re
->vrf_id
;
492 ctx
->zd_mtu
= re
->mtu
;
493 ctx
->zd_nexthop_mtu
= re
->nexthop_mtu
;
494 ctx
->zd_instance
= re
->instance
;
495 ctx
->zd_tag
= re
->tag
;
496 ctx
->zd_old_tag
= re
->tag
;
497 ctx
->zd_distance
= re
->distance
;
499 table
= srcdest_rnode_table(rn
);
502 ctx
->zd_afi
= info
->afi
;
503 ctx
->zd_safi
= info
->safi
;
505 /* Extract ns info - can't use pointers to 'core' structs */
506 zvrf
= vrf_info_lookup(re
->vrf_id
);
509 zebra_dplane_info_from_zns(&(ctx
->zd_ns_info
), zns
, true /*is_cmd*/);
511 #if defined(HAVE_NETLINK)
512 /* Increment message counter after copying to context struct - may need
513 * two messages in some 'update' cases.
515 if (op
== DPLANE_OP_ROUTE_UPDATE
)
516 zns
->netlink_cmd
.seq
+= 2;
518 zns
->netlink_cmd
.seq
++;
521 /* Copy nexthops; recursive info is included too */
522 copy_nexthops(&(ctx
->zd_ng
.nexthop
), re
->ng
.nexthop
, NULL
);
524 /* TODO -- maybe use array of nexthops to avoid allocs? */
526 /* Trying out the sequence number idea, so we can try to detect
527 * when a result is stale.
529 re
->dplane_sequence
++;
530 ctx
->zd_seq
= re
->dplane_sequence
;
539 * Enqueue a new route update,
540 * and ensure an event is active for the dataplane thread.
542 static int dplane_route_enqueue(dplane_ctx_h ctx
)
546 /* Enqueue for processing by the dataplane thread */
549 TAILQ_INSERT_TAIL(&zdplane_g
.dg_route_ctx_q
, ctx
, zd_q_entries
);
553 /* Ensure that an event for the dataplane thread is active */
554 thread_add_event(zdplane_g
.dg_master
, dplane_route_process
, NULL
, 0,
555 &zdplane_g
.dg_t_update
);
563 * Attempt to dequeue a route-update block
565 static dplane_ctx_h
dplane_route_dequeue(void)
567 dplane_ctx_h ctx
= NULL
;
571 ctx
= TAILQ_FIRST(&zdplane_g
.dg_route_ctx_q
);
573 TAILQ_REMOVE(&zdplane_g
.dg_route_ctx_q
,
583 * Utility that prepares a route update and enqueues it for processing
585 static enum zebra_dplane_result
586 dplane_route_update_internal(struct route_node
*rn
,
587 struct route_entry
*re
,
588 struct route_entry
*old_re
,
591 enum zebra_dplane_result result
= ZEBRA_DPLANE_REQUEST_FAILURE
;
593 dplane_ctx_h ctx
= NULL
;
595 /* Obtain context block */
596 ctx
= dplane_ctx_alloc();
602 /* Init context with info from zebra data structs */
603 ret
= dplane_ctx_route_init(ctx
, op
, rn
, re
);
605 /* Capture some extra info for update case
606 * where there's a different 'old' route.
608 if ((op
== DPLANE_OP_ROUTE_UPDATE
) &&
609 old_re
&& (old_re
!= re
)) {
610 ctx
->zd_is_update
= true;
612 old_re
->dplane_sequence
++;
613 ctx
->zd_old_seq
= old_re
->dplane_sequence
;
615 ctx
->zd_old_tag
= old_re
->tag
;
616 ctx
->zd_old_type
= old_re
->type
;
617 ctx
->zd_old_instance
= old_re
->instance
;
618 ctx
->zd_old_distance
= old_re
->distance
;
621 /* Enqueue context for processing */
622 ret
= dplane_route_enqueue(ctx
);
627 result
= ZEBRA_DPLANE_REQUEST_QUEUED
;
629 dplane_ctx_free(&ctx
);
635 * Enqueue a route 'add' for the dataplane.
637 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
638 struct route_entry
*re
)
640 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
642 if (rn
== NULL
|| re
== NULL
)
645 ret
= dplane_route_update_internal(rn
, re
, NULL
,
646 DPLANE_OP_ROUTE_INSTALL
);
653 * Enqueue a route update for the dataplane.
655 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
656 struct route_entry
*re
,
657 struct route_entry
*old_re
)
659 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
661 if (rn
== NULL
|| re
== NULL
)
664 ret
= dplane_route_update_internal(rn
, re
, old_re
,
665 DPLANE_OP_ROUTE_UPDATE
);
671 * Enqueue a route removal for the dataplane.
673 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
674 struct route_entry
*re
)
676 enum zebra_dplane_result ret
= ZEBRA_DPLANE_REQUEST_FAILURE
;
678 if (rn
== NULL
|| re
== NULL
)
681 ret
= dplane_route_update_internal(rn
, re
, NULL
,
682 DPLANE_OP_ROUTE_DELETE
);
689 * Event handler function for routing updates
691 static int dplane_route_process(struct thread
*event
)
693 enum zebra_dplane_result res
;
697 /* TODO -- limit number of updates per cycle? */
698 ctx
= dplane_route_dequeue();
702 /* TODO -- support series of providers */
704 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
705 char dest_str
[PREFIX_STRLEN
];
707 prefix2str(dplane_ctx_get_dest(ctx
),
708 dest_str
, sizeof(dest_str
));
710 zlog_debug("%u:%s Dplane update ctx %p op %s",
711 dplane_ctx_get_vrf(ctx
), dest_str
,
712 ctx
, dplane_op2str(dplane_ctx_get_op(ctx
)));
715 /* Initially, just doing kernel-facing update here */
716 res
= kernel_route_update(ctx
);
718 ctx
->zd_status
= res
;
720 /* Enqueue result to zebra main context */
721 (*zdplane_g
.dg_results_cb
)(ctx
);
730 * Provider registration
732 int dplane_provider_register(const char *name
,
733 enum dplane_provider_prio_e prio
,
734 dplane_provider_process_fp fp
,
735 dplane_provider_fini_fp fini_fp
)
738 struct zebra_dplane_provider_s
*p
, *last
;
746 if (prio
<= DPLANE_PRIO_NONE
||
747 prio
> DPLANE_PRIO_LAST
) {
752 /* Allocate and init new provider struct */
753 p
= XCALLOC(MTYPE_DP_PROV
, sizeof(struct zebra_dplane_provider_s
));
759 strncpy(p
->dp_name
, name
, DPLANE_PROVIDER_NAMELEN
);
760 p
->dp_name
[DPLANE_PROVIDER_NAMELEN
] = '\0'; /* Belt-and-suspenders */
762 p
->dp_priority
= prio
;
764 p
->dp_fini
= fini_fp
;
766 /* Lock the lock - the dplane pthread may be running */
769 p
->dp_id
= ++zdplane_g
.dg_provider_id
;
771 /* Insert into list ordered by priority */
772 TAILQ_FOREACH(last
, &zdplane_g
.dg_providers_q
, dp_q_providers
) {
773 if (last
->dp_priority
> p
->dp_priority
)
778 TAILQ_INSERT_BEFORE(last
, p
, dp_q_providers
);
780 TAILQ_INSERT_TAIL(&zdplane_g
.dg_providers_q
, p
, dp_q_providers
);
790 * Zebra registers a results callback with the dataplane system
792 int dplane_results_register(dplane_results_fp fp
)
794 zdplane_g
.dg_results_cb
= fp
;
799 * Initialize the dataplane module during startup, internal/private version
801 static void zebra_dplane_init_internal(struct zebra_t
*zebra
)
803 memset(&zdplane_g
, 0, sizeof(zdplane_g
));
805 pthread_mutex_init(&zdplane_g
.dg_mutex
, NULL
);
807 TAILQ_INIT(&zdplane_g
.dg_route_ctx_q
);
808 TAILQ_INIT(&zdplane_g
.dg_providers_q
);
810 /* TODO -- register default kernel 'provider' during init */
812 /* TODO -- start dataplane pthread. We're using the zebra
813 * core/main thread temporarily
815 zdplane_g
.dg_master
= zebra
->master
;
819 * Shutdown, de-init hook callback. This runs pretty early during shutdown.
821 static int zebra_dplane_fini(void)
823 /* TODO -- stop thread, clean queues */
829 * Initialize the dataplane module at startup; called by zebra rib_init()
831 void zebra_dplane_init(void)
833 zebra_dplane_init_internal(&zebrad
);
835 /* Register for shutdown/de-init */
836 hook_register(frr_early_fini
, zebra_dplane_fini
);