2 * Zebra dataplane layer api interfaces.
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 #ifndef _ZEBRA_DPLANE_H
21 #define _ZEBRA_DPLANE_H 1
23 #include "lib/zebra.h"
24 #include "lib/prefix.h"
25 #include "lib/nexthop.h"
26 #include "lib/nexthop_group.h"
27 #include "lib/openbsd-queue.h"
28 #include "zebra/zebra_ns.h"
29 #include "zebra/rib.h"
30 #include "zebra/zserv.h"
32 /* Key netlink info from zebra ns */
33 struct zebra_dplane_info
{
36 #if defined(HAVE_NETLINK)
42 /* Utility to fill in zns info from main zns struct */
44 zebra_dplane_info_from_zns(struct zebra_dplane_info
*zns_info
,
45 const struct zebra_ns
*zns
, bool is_cmd
)
47 zns_info
->ns_id
= zns
->ns_id
;
49 #if defined(HAVE_NETLINK)
50 zns_info
->is_cmd
= is_cmd
;
52 zns_info
->nls
= zns
->netlink_cmd
;
54 zns_info
->nls
= zns
->netlink
;
60 * Result codes used when returning status back to the main zebra context.
66 * Flags being SET/UNSET do not belong in the South Bound
67 * Interface. This Setting belongs at the calling level
68 * because we can and will have multiple different interfaces
69 * and we will have potentially multiple different
70 * modules/filters to call. As such Setting/Unsetting
71 * success failure should be handled by the caller.
73 enum zebra_dplane_status
{
74 ZEBRA_DPLANE_STATUS_NONE
= 0,
75 ZEBRA_DPLANE_INSTALL_SUCCESS
,
76 ZEBRA_DPLANE_INSTALL_FAILURE
,
77 ZEBRA_DPLANE_DELETE_SUCCESS
,
78 ZEBRA_DPLANE_DELETE_FAILURE
,
82 enum zebra_dplane_result
{
83 ZEBRA_DPLANE_REQUEST_QUEUED
,
84 ZEBRA_DPLANE_REQUEST_SUCCESS
,
85 ZEBRA_DPLANE_REQUEST_FAILURE
,
89 * API between the zebra dataplane system and the main zebra processing
94 * Enqueue a route install or update for the dataplane.
100 DPLANE_OP_ROUTE_INSTALL
,
101 DPLANE_OP_ROUTE_UPDATE
,
102 DPLANE_OP_ROUTE_DELETE
,
107 * The dataplane context struct is used to exchange info between the main zebra
108 * context and the dataplane module(s). If these are two independent pthreads,
109 * they cannot share existing global data structures safely.
112 /* Define a tailq list type for context blocks. The list is exposed/public,
113 * but the internal linkage in the context struct is private, so there
114 * are accessor apis that support enqueue and dequeue.
116 TAILQ_HEAD(dplane_ctx_q
, zebra_dplane_ctx
);
118 /* Return a dataplane results context block after use; the caller's pointer will
121 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
);
123 /* Enqueue a context block to caller's tailq. This exists so that the
124 * context struct can remain opaque.
126 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
127 const struct zebra_dplane_ctx
*ctx
);
129 /* Append a list of context blocks to another list - again, just keeping
130 * the context struct opaque.
132 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
133 struct dplane_ctx_q
*from_list
);
135 /* Dequeue a context block from the head of caller's tailq */
136 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
);
139 * Accessors for information from the context object
141 enum zebra_dplane_result
dplane_ctx_get_status(
142 const struct zebra_dplane_ctx
*ctx
);
143 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
144 enum zebra_dplane_result status
);
145 const char *dplane_res2str(enum zebra_dplane_result res
);
147 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
);
148 const char *dplane_op2str(enum dplane_op_e op
);
150 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
);
152 /* Retrieve last/current provider id */
153 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
);
155 /* Providers running before the kernel can control whether a kernel
156 * update should be done.
158 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
);
159 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
);
161 /* Source prefix is a little special - use convention to return NULL
162 * to mean "no src prefix"
164 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
);
166 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
);
167 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
);
168 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
);
169 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
);
170 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
);
171 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
);
172 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
);
173 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
);
174 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
);
175 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
);
176 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
);
177 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
);
178 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
);
179 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
);
180 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
);
181 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
);
182 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
);
183 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
);
184 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
);
186 const struct nexthop_group
*dplane_ctx_get_ng(
187 const struct zebra_dplane_ctx
*ctx
);
188 const struct nexthop_group
*dplane_ctx_get_old_ng(
189 const struct zebra_dplane_ctx
*ctx
);
191 const struct zebra_dplane_info
*dplane_ctx_get_ns(
192 const struct zebra_dplane_ctx
*ctx
);
194 /* Indicates zebra shutdown/exit is in progress. Some operations may be
195 * simplified or skipped during shutdown processing.
197 bool dplane_is_in_shutdown(void);
200 * Enqueue route change operations for the dataplane.
202 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
203 struct route_entry
*re
);
205 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
206 struct route_entry
*re
,
207 struct route_entry
*old_re
);
209 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
210 struct route_entry
*re
);
212 /* Retrieve the limit on the number of pending, unprocessed updates. */
213 uint32_t dplane_get_in_queue_limit(void);
215 /* Configure limit on the number of pending, queued updates. If 'unset', reset
218 void dplane_set_in_queue_limit(uint32_t limit
, bool set
);
220 /* Retrieve the current queue depth of incoming, unprocessed updates */
221 uint32_t dplane_get_in_queue_len(void);
226 int dplane_show_helper(struct vty
*vty
, bool detailed
);
227 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
);
231 * Dataplane providers: modules that process or consume dataplane events.
234 struct zebra_dplane_provider
;
236 /* Support string name for a dataplane provider */
237 #define DPLANE_PROVIDER_NAMELEN 64
239 /* Priority or ordering values for providers. The idea is that there may be
240 * some pre-processing, followed by an external or remote dataplane,
241 * followed by the kernel, followed by some post-processing step (such as
242 * the fpm output stream.)
244 enum dplane_provider_prio
{
245 DPLANE_PRIO_NONE
= 0,
246 DPLANE_PRIO_PREPROCESS
,
247 DPLANE_PRIO_PRE_KERNEL
,
249 DPLANE_PRIO_POSTPROCESS
,
253 /* Provider's entry-point for incoming work, called in the context of the
254 * dataplane pthread. The dataplane pthread enqueues any new work to the
255 * provider's 'inbound' queue, then calls the callback. The dataplane
256 * then checks the provider's outbound queue.
258 typedef int (*dplane_provider_process_fp
)(struct zebra_dplane_provider
*prov
);
260 /* Provider's entry-point for shutdown and cleanup. Called with 'early'
261 * during shutdown, to indicate that the dataplane subsystem is allowing
262 * work to move through the providers and finish. When called without 'early',
263 * the provider should release all resources (if it has any allocated).
265 typedef int (*dplane_provider_fini_fp
)(struct zebra_dplane_provider
*prov
,
268 /* Flags values used during provider registration. */
269 #define DPLANE_PROV_FLAGS_DEFAULT 0x0
271 /* Provider will be spawning its own worker thread */
272 #define DPLANE_PROV_FLAG_THREADED 0x1
275 /* Provider registration: ordering or priority value, callbacks, and optional
278 int dplane_provider_register(const char *name
,
279 enum dplane_provider_prio prio
,
281 dplane_provider_process_fp fp
,
282 dplane_provider_fini_fp fini_fp
,
285 /* Accessors for provider attributes */
286 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
);
287 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
);
288 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
);
289 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
);
291 /* Lock/unlock a provider's mutex - iff the provider was registered with
294 void dplane_provider_lock(struct zebra_dplane_provider
*prov
);
295 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
);
297 /* Obtain thread_master for dataplane thread */
298 struct thread_master
*dplane_get_thread_master(void);
300 /* Providers should (generally) limit number of updates per work cycle */
301 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
);
303 /* Provider api to signal that work/events are available
304 * for the dataplane pthread.
306 int dplane_provider_work_ready(void);
308 /* Dequeue, maintain associated counter and locking */
309 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
310 struct zebra_dplane_provider
*prov
);
312 /* Dequeue work to a list, maintain counter and locking, return count */
313 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
314 struct dplane_ctx_q
*listp
);
316 /* Enqueue, maintain associated counter and locking */
317 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
318 struct zebra_dplane_ctx
*ctx
);
321 * Zebra registers a results callback with the dataplane. The callback is
322 * called in the dataplane pthread context, so the expectation is that the
323 * context is queued for the zebra main pthread or that processing
326 typedef int (*dplane_results_fp
)(struct zebra_dplane_ctx
*ctx
);
328 int dplane_results_register(dplane_results_fp fp
);
331 * Initialize the dataplane modules at zebra startup. This is currently called
334 void zebra_dplane_init(void);
337 * Start the dataplane pthread. This step needs to be run later than the
338 * 'init' step, in case zebra has fork-ed.
340 void zebra_dplane_start(void);
342 /* Finalize/cleanup apis, one called early as shutdown is starting,
343 * one called late at the end of zebra shutdown, and then one called
344 * from the zebra main pthread to stop the dplane pthread and
345 * free all resources.
347 * Zebra expects to try to clean up all vrfs and all routes during
348 * shutdown, so the dplane must be available until very late.
350 void zebra_dplane_pre_finish(void);
351 void zebra_dplane_finish(void);
352 void zebra_dplane_shutdown(void);
354 #endif /* _ZEBRA_DPLANE_H */