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"
29 #include "zebra/zebra_ns.h"
30 #include "zebra/rib.h"
31 #include "zebra/zserv.h"
32 #include "zebra/zebra_mpls.h"
38 /* Key netlink info from zebra ns */
39 struct zebra_dplane_info
{
42 #if defined(HAVE_NETLINK)
48 /* Utility to fill in zns info from main zns struct */
50 zebra_dplane_info_from_zns(struct zebra_dplane_info
*zns_info
,
51 const struct zebra_ns
*zns
, bool is_cmd
)
53 zns_info
->ns_id
= zns
->ns_id
;
55 #if defined(HAVE_NETLINK)
56 zns_info
->is_cmd
= is_cmd
;
58 zns_info
->nls
= zns
->netlink_cmd
;
60 zns_info
->nls
= zns
->netlink
;
66 * Result codes used when returning status back to the main zebra context.
72 * Flags being SET/UNSET do not belong in the South Bound
73 * Interface. This Setting belongs at the calling level
74 * because we can and will have multiple different interfaces
75 * and we will have potentially multiple different
76 * modules/filters to call. As such Setting/Unsetting
77 * success failure should be handled by the caller.
79 enum zebra_dplane_status
{
80 ZEBRA_DPLANE_STATUS_NONE
= 0,
81 ZEBRA_DPLANE_INSTALL_SUCCESS
,
82 ZEBRA_DPLANE_INSTALL_FAILURE
,
83 ZEBRA_DPLANE_DELETE_SUCCESS
,
84 ZEBRA_DPLANE_DELETE_FAILURE
,
88 enum zebra_dplane_result
{
89 ZEBRA_DPLANE_REQUEST_QUEUED
,
90 ZEBRA_DPLANE_REQUEST_SUCCESS
,
91 ZEBRA_DPLANE_REQUEST_FAILURE
,
95 * API between the zebra dataplane system and the main zebra processing
100 * Enqueue a route install or update for the dataplane.
106 DPLANE_OP_ROUTE_INSTALL
,
107 DPLANE_OP_ROUTE_UPDATE
,
108 DPLANE_OP_ROUTE_DELETE
,
109 DPLANE_OP_ROUTE_NOTIFY
,
112 DPLANE_OP_LSP_INSTALL
,
113 DPLANE_OP_LSP_UPDATE
,
114 DPLANE_OP_LSP_DELETE
,
115 DPLANE_OP_LSP_NOTIFY
,
117 /* Pseudowire update */
118 DPLANE_OP_PW_INSTALL
,
119 DPLANE_OP_PW_UNINSTALL
,
121 /* System route notification */
122 DPLANE_OP_SYS_ROUTE_ADD
,
123 DPLANE_OP_SYS_ROUTE_DELETE
,
125 /* Interface address update */
126 DPLANE_OP_ADDR_INSTALL
,
127 DPLANE_OP_ADDR_UNINSTALL
,
129 /* MAC address update */
130 DPLANE_OP_MAC_INSTALL
,
131 DPLANE_OP_MAC_DELETE
,
133 /* EVPN neighbor updates */
134 DPLANE_OP_NEIGH_INSTALL
,
135 DPLANE_OP_NEIGH_DELETE
,
138 /* Enable system route notifications */
139 void dplane_enable_sys_route_notifs(void);
142 * The dataplane context struct is used to exchange info between the main zebra
143 * context and the dataplane module(s). If these are two independent pthreads,
144 * they cannot share existing global data structures safely.
147 /* Define a tailq list type for context blocks. The list is exposed/public,
148 * but the internal linkage in the context struct is private, so there
149 * are accessor apis that support enqueue and dequeue.
151 TAILQ_HEAD(dplane_ctx_q
, zebra_dplane_ctx
);
153 /* Allocate a context object */
154 struct zebra_dplane_ctx
*dplane_ctx_alloc(void);
156 /* Return a dataplane results context block after use; the caller's pointer will
159 void dplane_ctx_fini(struct zebra_dplane_ctx
**pctx
);
161 /* Enqueue a context block to caller's tailq. This exists so that the
162 * context struct can remain opaque.
164 void dplane_ctx_enqueue_tail(struct dplane_ctx_q
*q
,
165 const struct zebra_dplane_ctx
*ctx
);
167 /* Append a list of context blocks to another list - again, just keeping
168 * the context struct opaque.
170 void dplane_ctx_list_append(struct dplane_ctx_q
*to_list
,
171 struct dplane_ctx_q
*from_list
);
173 /* Dequeue a context block from the head of caller's tailq */
174 struct zebra_dplane_ctx
*dplane_ctx_dequeue(struct dplane_ctx_q
*q
);
177 * Accessors for information from the context object
179 enum zebra_dplane_result
dplane_ctx_get_status(
180 const struct zebra_dplane_ctx
*ctx
);
181 void dplane_ctx_set_status(struct zebra_dplane_ctx
*ctx
,
182 enum zebra_dplane_result status
);
183 const char *dplane_res2str(enum zebra_dplane_result res
);
185 enum dplane_op_e
dplane_ctx_get_op(const struct zebra_dplane_ctx
*ctx
);
186 void dplane_ctx_set_op(struct zebra_dplane_ctx
*ctx
, enum dplane_op_e op
);
187 const char *dplane_op2str(enum dplane_op_e op
);
189 const struct prefix
*dplane_ctx_get_dest(const struct zebra_dplane_ctx
*ctx
);
190 void dplane_ctx_set_dest(struct zebra_dplane_ctx
*ctx
,
191 const struct prefix
*dest
);
192 const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx
*ctx
);
193 ifindex_t
dplane_ctx_get_ifindex(const struct zebra_dplane_ctx
*ctx
);
195 /* Retrieve last/current provider id */
196 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx
*ctx
);
198 /* Providers running before the kernel can control whether a kernel
199 * update should be done.
201 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx
*ctx
);
202 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx
*ctx
);
204 /* Source prefix is a little special - use convention to return NULL
205 * to mean "no src prefix"
207 const struct prefix
*dplane_ctx_get_src(const struct zebra_dplane_ctx
*ctx
);
208 void dplane_ctx_set_src(struct zebra_dplane_ctx
*ctx
, const struct prefix
*src
);
210 bool dplane_ctx_is_update(const struct zebra_dplane_ctx
*ctx
);
211 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx
*ctx
);
212 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx
*ctx
);
213 void dplane_ctx_set_vrf(struct zebra_dplane_ctx
*ctx
, vrf_id_t vrf
);
214 vrf_id_t
dplane_ctx_get_vrf(const struct zebra_dplane_ctx
*ctx
);
216 bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx
*ctx
);
217 void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx
*ctx
,
219 uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx
*ctx
);
221 /* Accessors for route update information */
222 void dplane_ctx_set_type(struct zebra_dplane_ctx
*ctx
, int type
);
223 int dplane_ctx_get_type(const struct zebra_dplane_ctx
*ctx
);
224 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx
*ctx
);
225 void dplane_ctx_set_afi(struct zebra_dplane_ctx
*ctx
, afi_t afi
);
226 afi_t
dplane_ctx_get_afi(const struct zebra_dplane_ctx
*ctx
);
227 void dplane_ctx_set_safi(struct zebra_dplane_ctx
*ctx
, safi_t safi
);
228 safi_t
dplane_ctx_get_safi(const struct zebra_dplane_ctx
*ctx
);
229 void dplane_ctx_set_table(struct zebra_dplane_ctx
*ctx
, uint32_t table
);
230 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx
*ctx
);
231 route_tag_t
dplane_ctx_get_tag(const struct zebra_dplane_ctx
*ctx
);
232 void dplane_ctx_set_tag(struct zebra_dplane_ctx
*ctx
, route_tag_t tag
);
233 route_tag_t
dplane_ctx_get_old_tag(const struct zebra_dplane_ctx
*ctx
);
234 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx
*ctx
);
235 void dplane_ctx_set_instance(struct zebra_dplane_ctx
*ctx
, uint16_t instance
);
236 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx
*ctx
);
237 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx
*ctx
);
238 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx
*ctx
);
239 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx
*ctx
);
240 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx
*ctx
);
241 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx
*ctx
);
242 void dplane_ctx_set_distance(struct zebra_dplane_ctx
*ctx
, uint8_t distance
);
243 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx
*ctx
);
245 void dplane_ctx_set_nexthops(struct zebra_dplane_ctx
*ctx
, struct nexthop
*nh
);
246 const struct nexthop_group
*dplane_ctx_get_ng(
247 const struct zebra_dplane_ctx
*ctx
);
248 const struct nexthop_group
*dplane_ctx_get_old_ng(
249 const struct zebra_dplane_ctx
*ctx
);
251 /* Accessors for LSP information */
252 mpls_label_t
dplane_ctx_get_in_label(const struct zebra_dplane_ctx
*ctx
);
253 void dplane_ctx_set_in_label(struct zebra_dplane_ctx
*ctx
,
255 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx
*ctx
);
256 void dplane_ctx_set_addr_family(struct zebra_dplane_ctx
*ctx
,
258 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx
*ctx
);
259 void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx
*ctx
,
261 const zebra_nhlfe_t
*dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx
*ctx
);
262 zebra_nhlfe_t
*dplane_ctx_add_nhlfe(struct zebra_dplane_ctx
*ctx
,
263 enum lsp_types_t lsp_type
,
264 enum nexthop_types_t nh_type
,
267 mpls_label_t out_label
);
269 const zebra_nhlfe_t
*dplane_ctx_get_best_nhlfe(
270 const struct zebra_dplane_ctx
*ctx
);
271 const zebra_nhlfe_t
*dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx
*ctx
,
272 zebra_nhlfe_t
*nhlfe
);
273 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx
*ctx
);
275 /* Accessors for pseudowire information */
276 mpls_label_t
dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx
*ctx
);
277 mpls_label_t
dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx
*ctx
);
278 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx
*ctx
);
279 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx
*ctx
);
280 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx
*ctx
);
281 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx
*ctx
);
282 const union g_addr
*dplane_ctx_get_pw_dest(
283 const struct zebra_dplane_ctx
*ctx
);
284 const union pw_protocol_fields
*dplane_ctx_get_pw_proto(
285 const struct zebra_dplane_ctx
*ctx
);
286 const struct nexthop_group
*dplane_ctx_get_pw_nhg(
287 const struct zebra_dplane_ctx
*ctx
);
289 /* Accessors for interface information */
290 uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx
*ctx
);
291 /* Is interface addr p2p? */
292 bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx
*ctx
);
293 bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx
*ctx
);
294 bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx
*ctx
);
295 const struct prefix
*dplane_ctx_get_intf_addr(
296 const struct zebra_dplane_ctx
*ctx
);
297 bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx
*ctx
);
298 const struct prefix
*dplane_ctx_get_intf_dest(
299 const struct zebra_dplane_ctx
*ctx
);
300 bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx
*ctx
);
301 const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx
*ctx
);
303 /* Accessors for MAC information */
304 vlanid_t
dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx
*ctx
);
305 bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx
*ctx
);
306 const struct ethaddr
*dplane_ctx_mac_get_addr(
307 const struct zebra_dplane_ctx
*ctx
);
308 const struct in_addr
*dplane_ctx_mac_get_vtep_ip(
309 const struct zebra_dplane_ctx
*ctx
);
311 /* Namespace info - esp. for netlink communication */
312 const struct zebra_dplane_info
*dplane_ctx_get_ns(
313 const struct zebra_dplane_ctx
*ctx
);
315 /* Indicates zebra shutdown/exit is in progress. Some operations may be
316 * simplified or skipped during shutdown processing.
318 bool dplane_is_in_shutdown(void);
321 * Enqueue route change operations for the dataplane.
323 enum zebra_dplane_result
dplane_route_add(struct route_node
*rn
,
324 struct route_entry
*re
);
326 enum zebra_dplane_result
dplane_route_update(struct route_node
*rn
,
327 struct route_entry
*re
,
328 struct route_entry
*old_re
);
330 enum zebra_dplane_result
dplane_route_delete(struct route_node
*rn
,
331 struct route_entry
*re
);
333 /* Notify the dplane when system/connected routes change */
334 enum zebra_dplane_result
dplane_sys_route_add(struct route_node
*rn
,
335 struct route_entry
*re
);
336 enum zebra_dplane_result
dplane_sys_route_del(struct route_node
*rn
,
337 struct route_entry
*re
);
339 /* Update from an async notification, to bring other fibs up-to-date */
340 enum zebra_dplane_result
dplane_route_notif_update(
341 struct route_node
*rn
,
342 struct route_entry
*re
,
344 struct zebra_dplane_ctx
*ctx
);
347 * Enqueue LSP change operations for the dataplane.
349 enum zebra_dplane_result
dplane_lsp_add(zebra_lsp_t
*lsp
);
350 enum zebra_dplane_result
dplane_lsp_update(zebra_lsp_t
*lsp
);
351 enum zebra_dplane_result
dplane_lsp_delete(zebra_lsp_t
*lsp
);
353 /* Update or un-install resulting from an async notification */
354 enum zebra_dplane_result
dplane_lsp_notif_update(zebra_lsp_t
*lsp
,
356 struct zebra_dplane_ctx
*ctx
);
359 * Enqueue pseudowire operations for the dataplane.
361 enum zebra_dplane_result
dplane_pw_install(struct zebra_pw
*pw
);
362 enum zebra_dplane_result
dplane_pw_uninstall(struct zebra_pw
*pw
);
365 * Enqueue interface address changes for the dataplane.
367 enum zebra_dplane_result
dplane_intf_addr_set(const struct interface
*ifp
,
368 const struct connected
*ifc
);
369 enum zebra_dplane_result
dplane_intf_addr_unset(const struct interface
*ifp
,
370 const struct connected
*ifc
);
373 * Enqueue evpn mac operations for the dataplane.
375 enum zebra_dplane_result
dplane_mac_add(const struct interface
*ifp
,
377 const struct ethaddr
*mac
,
378 struct in_addr vtep_ip
,
381 enum zebra_dplane_result
dplane_mac_del(const struct interface
*ifp
,
383 const struct ethaddr
*mac
,
384 struct in_addr vtep_ip
);
387 * Enqueue evpn neighbor updates for the dataplane.
389 enum zebra_dplane_result
dplane_neigh_add(const struct interface
*ifp
,
390 const struct ipaddr
*ip
,
391 const struct ethaddr
*mac
,
393 enum zebra_dplane_result
dplane_neigh_del(const struct interface
*ifp
,
394 const struct ipaddr
*ip
);
396 /* Retrieve the limit on the number of pending, unprocessed updates. */
397 uint32_t dplane_get_in_queue_limit(void);
399 /* Configure limit on the number of pending, queued updates. If 'unset', reset
402 void dplane_set_in_queue_limit(uint32_t limit
, bool set
);
404 /* Retrieve the current queue depth of incoming, unprocessed updates */
405 uint32_t dplane_get_in_queue_len(void);
410 int dplane_show_helper(struct vty
*vty
, bool detailed
);
411 int dplane_show_provs_helper(struct vty
*vty
, bool detailed
);
414 * Dataplane providers: modules that process or consume dataplane events.
417 struct zebra_dplane_provider
;
419 /* Support string name for a dataplane provider */
420 #define DPLANE_PROVIDER_NAMELEN 64
422 /* Priority or ordering values for providers. The idea is that there may be
423 * some pre-processing, followed by an external or remote dataplane,
424 * followed by the kernel, followed by some post-processing step (such as
425 * the fpm output stream.)
427 enum dplane_provider_prio
{
428 DPLANE_PRIO_NONE
= 0,
429 DPLANE_PRIO_PREPROCESS
,
430 DPLANE_PRIO_PRE_KERNEL
,
432 DPLANE_PRIO_POSTPROCESS
,
436 /* Flags values used during provider registration. */
437 #define DPLANE_PROV_FLAGS_DEFAULT 0x0
439 /* Provider will be spawning its own worker thread */
440 #define DPLANE_PROV_FLAG_THREADED 0x1
443 /* Provider registration: ordering or priority value, callbacks, and optional
444 * opaque data value. If 'prov_p', return the newly-allocated provider object
448 /* Providers offer an entry-point for incoming work, called in the context of
449 * the dataplane pthread. The dataplane pthread enqueues any new work to the
450 * provider's 'inbound' queue, then calls the callback. The dataplane
451 * then checks the provider's outbound queue for completed work.
455 * Providers can offer a 'start' callback; if present, the dataplane will
456 * call it when it is starting - when its pthread and event-scheduling
457 * thread_master are available.
460 /* Providers can offer an entry-point for shutdown and cleanup. This is called
461 * with 'early' during shutdown, to indicate that the dataplane subsystem
462 * is allowing work to move through the providers and finish.
463 * When called without 'early', the provider should release
464 * all resources (if it has any allocated).
466 int dplane_provider_register(const char *name
,
467 enum dplane_provider_prio prio
,
469 int (*start_fp
)(struct zebra_dplane_provider
*),
470 int (*fp
)(struct zebra_dplane_provider
*),
471 int (*fini_fp
)(struct zebra_dplane_provider
*,
474 struct zebra_dplane_provider
**prov_p
);
476 /* Accessors for provider attributes */
477 const char *dplane_provider_get_name(const struct zebra_dplane_provider
*prov
);
478 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider
*prov
);
479 void *dplane_provider_get_data(const struct zebra_dplane_provider
*prov
);
480 bool dplane_provider_is_threaded(const struct zebra_dplane_provider
*prov
);
482 /* Lock/unlock a provider's mutex - iff the provider was registered with
485 void dplane_provider_lock(struct zebra_dplane_provider
*prov
);
486 void dplane_provider_unlock(struct zebra_dplane_provider
*prov
);
488 /* Obtain thread_master for dataplane thread */
489 struct thread_master
*dplane_get_thread_master(void);
491 /* Providers should (generally) limit number of updates per work cycle */
492 int dplane_provider_get_work_limit(const struct zebra_dplane_provider
*prov
);
494 /* Provider api to signal that work/events are available
495 * for the dataplane pthread.
497 int dplane_provider_work_ready(void);
499 /* Dequeue, maintain associated counter and locking */
500 struct zebra_dplane_ctx
*dplane_provider_dequeue_in_ctx(
501 struct zebra_dplane_provider
*prov
);
503 /* Dequeue work to a list, maintain counter and locking, return count */
504 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider
*prov
,
505 struct dplane_ctx_q
*listp
);
507 /* Enqueue completed work, maintain associated counter and locking */
508 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider
*prov
,
509 struct zebra_dplane_ctx
*ctx
);
511 /* Enqueue a context directly to zebra main. */
512 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx
*ctx
);
515 * Initialize the dataplane modules at zebra startup. This is currently called
516 * by the rib module. Zebra registers a results callback with the dataplane.
517 * The callback is called in the dataplane pthread context,
518 * so the expectation is that the contexts are queued for the zebra
521 void zebra_dplane_init(int (*) (struct dplane_ctx_q
*));
524 * Start the dataplane pthread. This step needs to be run later than the
525 * 'init' step, in case zebra has fork-ed.
527 void zebra_dplane_start(void);
529 /* Finalize/cleanup apis, one called early as shutdown is starting,
530 * one called late at the end of zebra shutdown, and then one called
531 * from the zebra main pthread to stop the dplane pthread and
532 * free all resources.
534 * Zebra expects to try to clean up all vrfs and all routes during
535 * shutdown, so the dplane must be available until very late.
537 void zebra_dplane_pre_finish(void);
538 void zebra_dplane_finish(void);
539 void zebra_dplane_shutdown(void);
545 #endif /* _ZEBRA_DPLANE_H */