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/queue.h"
28 #include "zebra/zebra_ns.h"
29 #include "zebra/rib.h"
30 #include "zebra/zserv.h"
31 #include "zebra/zebra_mpls.h"
37 /* Key netlink info from zebra ns */
38 struct zebra_dplane_info
{
41 #if defined(HAVE_NETLINK)
47 /* Utility to fill in zns info from main zns struct */
49 zebra_dplane_info_from_zns(struct zebra_dplane_info
*zns_info
,
50 const struct zebra_ns
*zns
, bool is_cmd
)
52 zns_info
->ns_id
= zns
->ns_id
;
54 #if defined(HAVE_NETLINK)
55 zns_info
->is_cmd
= is_cmd
;
57 zns_info
->nls
= zns
->netlink_cmd
;
59 zns_info
->nls
= zns
->netlink
;
65 * Result codes used when returning status back to the main zebra context.
71 * Flags being SET/UNSET do not belong in the South Bound
72 * Interface. This Setting belongs at the calling level
73 * because we can and will have multiple different interfaces
74 * and we will have potentially multiple different
75 * modules/filters to call. As such Setting/Unsetting
76 * success failure should be handled by the caller.
78 enum zebra_dplane_status
{
79 ZEBRA_DPLANE_STATUS_NONE
= 0,
80 ZEBRA_DPLANE_INSTALL_SUCCESS
,
81 ZEBRA_DPLANE_INSTALL_FAILURE
,
82 ZEBRA_DPLANE_DELETE_SUCCESS
,
83 ZEBRA_DPLANE_DELETE_FAILURE
,
87 enum zebra_dplane_result
{
88 ZEBRA_DPLANE_REQUEST_QUEUED
,
89 ZEBRA_DPLANE_REQUEST_SUCCESS
,
90 ZEBRA_DPLANE_REQUEST_FAILURE
,
94 * API between the zebra dataplane system and the main zebra processing
99 * Enqueue a route install or update for the dataplane.
105 DPLANE_OP_ROUTE_INSTALL
,
106 DPLANE_OP_ROUTE_UPDATE
,
107 DPLANE_OP_ROUTE_DELETE
,
110 DPLANE_OP_LSP_INSTALL
,
111 DPLANE_OP_LSP_UPDATE
,
112 DPLANE_OP_LSP_DELETE
,
114 /* Pseudowire update */
115 DPLANE_OP_PW_INSTALL
,
116 DPLANE_OP_PW_UNINSTALL
,
118 /* System route notification */
119 DPLANE_OP_SYS_ROUTE_ADD
,
120 DPLANE_OP_SYS_ROUTE_DELETE
,
123 /* Enable system route notifications */
124 void dplane_enable_sys_route_notifs(void);
127 * The dataplane context struct is used to exchange info between the main zebra
128 * context and the dataplane module(s). If these are two independent pthreads,
129 * they cannot share existing global data structures safely.
132 /* Define a tailq list type for context blocks. The list is exposed/public,
133 * but the internal linkage in the context struct is private, so there
134 * are accessor apis that support enqueue and dequeue.
136 TAILQ_HEAD(dplane_ctx_q
, zebra_dplane_ctx
);
138 /* Return a dataplane results context block after use; the caller's pointer will
141 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
);
143 /* Enqueue a context block to caller's tailq. This exists so that the
144 * context struct can remain opaque.
146 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
147 const struct zebra_dplane_ctx
*ctx
);
149 /* Append a list of context blocks to another list - again, just keeping
150 * the context struct opaque.
152 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
153 struct dplane_ctx_q
*from_list
);
155 /* Dequeue a context block from the head of caller's tailq */
156 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
);
159 * Accessors for information from the context object
161 enum zebra_dplane_result
dplane_ctx_get_status(
162 const struct zebra_dplane_ctx
*ctx
);
163 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
164 enum zebra_dplane_result status
);
165 const char *dplane_res2str(enum zebra_dplane_result res
);
167 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
);
168 const char *dplane_op2str(enum dplane_op_e op
);
170 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
);
172 /* Retrieve last/current provider id */
173 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
);
175 /* Providers running before the kernel can control whether a kernel
176 * update should be done.
178 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
);
179 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
);
181 /* Source prefix is a little special - use convention to return NULL
182 * to mean "no src prefix"
184 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
);
186 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
);
187 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
);
188 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
);
189 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
);
191 /* Accessors for route update information */
192 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
);
193 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
);
194 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
);
195 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
);
196 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
);
197 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
);
198 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
);
199 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
);
200 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
);
201 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
);
202 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
);
203 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
);
204 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
);
205 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
);
206 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
);
208 const struct nexthop_group
*dplane_ctx_get_ng(
209 const struct zebra_dplane_ctx
*ctx
);
210 const struct nexthop_group
*dplane_ctx_get_old_ng(
211 const struct zebra_dplane_ctx
*ctx
);
213 /* Accessors for LSP information */
214 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
);
215 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
);
216 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
);
217 const zebra_nhlfe_t
*dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx
*ctx
);
218 const zebra_nhlfe_t
*dplane_ctx_get_best_nhlfe(
219 const struct zebra_dplane_ctx
*ctx
);
220 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
);
222 /* Accessors for pseudowire information */
223 const char *dplane_ctx_get_pw_ifname(const struct zebra_dplane_ctx
*ctx
);
224 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
);
225 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
);
226 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
);
227 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
);
228 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
);
229 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
);
230 const union g_addr
*dplane_ctx_get_pw_dest(
231 const struct zebra_dplane_ctx
*ctx
);
232 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
233 const struct zebra_dplane_ctx
*ctx
);
234 const struct nexthop_group
*dplane_ctx_get_pw_nhg(
235 const struct zebra_dplane_ctx
*ctx
);
237 /* Namespace info - esp. for netlink communication */
238 const struct zebra_dplane_info
*dplane_ctx_get_ns(
239 const struct zebra_dplane_ctx
*ctx
);
241 /* Indicates zebra shutdown/exit is in progress. Some operations may be
242 * simplified or skipped during shutdown processing.
244 bool dplane_is_in_shutdown(void);
247 * Enqueue route change operations for the dataplane.
249 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
250 struct route_entry
*re
);
252 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
253 struct route_entry
*re
,
254 struct route_entry
*old_re
);
256 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
257 struct route_entry
*re
);
259 /* Notify the dplane when system/connected routes change */
260 enum zebra_dplane_result
dplane_sys_route_add(struct route_node
*rn
,
261 struct route_entry
*re
);
262 enum zebra_dplane_result
dplane_sys_route_del(struct route_node
*rn
,
263 struct route_entry
*re
);
266 * Enqueue LSP change operations for the dataplane.
268 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
);
269 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
);
270 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
);
273 * Enqueue pseudowire operations for the dataplane.
275 enum zebra_dplane_result
dplane_pw_install(struct zebra_pw
*pw
);
276 enum zebra_dplane_result
dplane_pw_uninstall(struct zebra_pw
*pw
);
278 /* Retrieve the limit on the number of pending, unprocessed updates. */
279 uint32_t dplane_get_in_queue_limit(void);
281 /* Configure limit on the number of pending, queued updates. If 'unset', reset
284 void dplane_set_in_queue_limit(uint32_t limit
, bool set
);
286 /* Retrieve the current queue depth of incoming, unprocessed updates */
287 uint32_t dplane_get_in_queue_len(void);
292 int dplane_show_helper(struct vty
*vty
, bool detailed
);
293 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
);
297 * Dataplane providers: modules that process or consume dataplane events.
300 struct zebra_dplane_provider
;
302 /* Support string name for a dataplane provider */
303 #define DPLANE_PROVIDER_NAMELEN 64
305 /* Priority or ordering values for providers. The idea is that there may be
306 * some pre-processing, followed by an external or remote dataplane,
307 * followed by the kernel, followed by some post-processing step (such as
308 * the fpm output stream.)
310 enum dplane_provider_prio
{
311 DPLANE_PRIO_NONE
= 0,
312 DPLANE_PRIO_PREPROCESS
,
313 DPLANE_PRIO_PRE_KERNEL
,
315 DPLANE_PRIO_POSTPROCESS
,
319 /* Flags values used during provider registration. */
320 #define DPLANE_PROV_FLAGS_DEFAULT 0x0
322 /* Provider will be spawning its own worker thread */
323 #define DPLANE_PROV_FLAG_THREADED 0x1
326 /* Provider registration: ordering or priority value, callbacks, and optional
327 * opaque data value. If 'prov_p', return the newly-allocated provider object
331 /* Providers offer an entry-point for incoming work, called in the context of
332 * the dataplane pthread. The dataplane pthread enqueues any new work to the
333 * provider's 'inbound' queue, then calls the callback. The dataplane
334 * then checks the provider's outbound queue for completed work.
337 /* Providers offer an entry-point for shutdown and cleanup. This is called
338 * with 'early' during shutdown, to indicate that the dataplane subsystem
339 * is allowing work to move through the providers and finish.
340 * When called without 'early', the provider should release
341 * all resources (if it has any allocated).
343 int dplane_provider_register(const char *name
,
344 enum dplane_provider_prio prio
,
346 int (*fp
)(struct zebra_dplane_provider
*),
347 int (*fini_fp
)(struct zebra_dplane_provider
*,
350 struct zebra_dplane_provider
**prov_p
);
352 /* Accessors for provider attributes */
353 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
);
354 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
);
355 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
);
356 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
);
358 /* Lock/unlock a provider's mutex - iff the provider was registered with
361 void dplane_provider_lock(struct zebra_dplane_provider
*prov
);
362 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
);
364 /* Obtain thread_master for dataplane thread */
365 struct thread_master
*dplane_get_thread_master(void);
367 /* Providers should (generally) limit number of updates per work cycle */
368 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
);
370 /* Provider api to signal that work/events are available
371 * for the dataplane pthread.
373 int dplane_provider_work_ready(void);
375 /* Dequeue, maintain associated counter and locking */
376 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
377 struct zebra_dplane_provider
*prov
);
379 /* Dequeue work to a list, maintain counter and locking, return count */
380 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
381 struct dplane_ctx_q
*listp
);
383 /* Enqueue, maintain associated counter and locking */
384 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
385 struct zebra_dplane_ctx
*ctx
);
388 * Initialize the dataplane modules at zebra startup. This is currently called
389 * by the rib module. Zebra registers a results callback with the dataplane.
390 * The callback is called in the dataplane pthread context,
391 * so the expectation is that the contexts are queued for the zebra
394 void zebra_dplane_init(int (*) (struct dplane_ctx_q
*));
397 * Start the dataplane pthread. This step needs to be run later than the
398 * 'init' step, in case zebra has fork-ed.
400 void zebra_dplane_start(void);
402 /* Finalize/cleanup apis, one called early as shutdown is starting,
403 * one called late at the end of zebra shutdown, and then one called
404 * from the zebra main pthread to stop the dplane pthread and
405 * free all resources.
407 * Zebra expects to try to clean up all vrfs and all routes during
408 * shutdown, so the dplane must be available until very late.
410 void zebra_dplane_pre_finish(void);
411 void zebra_dplane_finish(void);
412 void zebra_dplane_shutdown(void);
418 #endif /* _ZEBRA_DPLANE_H */