]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_dplane.h
zebra: add setters for distance, instance
[mirror_frr.git] / zebra / zebra_dplane.h
1 /*
2 * Zebra dataplane layer api interfaces.
3 * Copyright (c) 2018 Volta Networks, Inc.
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #ifndef _ZEBRA_DPLANE_H
21 #define _ZEBRA_DPLANE_H 1
22
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"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /* Key netlink info from zebra ns */
38 struct zebra_dplane_info {
39 ns_id_t ns_id;
40
41 #if defined(HAVE_NETLINK)
42 struct nlsock nls;
43 bool is_cmd;
44 #endif
45 };
46
47 /* Utility to fill in zns info from main zns struct */
48 static inline void
49 zebra_dplane_info_from_zns(struct zebra_dplane_info *zns_info,
50 const struct zebra_ns *zns, bool is_cmd)
51 {
52 zns_info->ns_id = zns->ns_id;
53
54 #if defined(HAVE_NETLINK)
55 zns_info->is_cmd = is_cmd;
56 if (is_cmd) {
57 zns_info->nls = zns->netlink_cmd;
58 } else {
59 zns_info->nls = zns->netlink;
60 }
61 #endif /* NETLINK */
62 }
63
64 /*
65 * Result codes used when returning status back to the main zebra context.
66 */
67
68 /*
69 * Philosophy Note:
70 *
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.
77 */
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,
84
85 };
86
87 enum zebra_dplane_result {
88 ZEBRA_DPLANE_REQUEST_QUEUED,
89 ZEBRA_DPLANE_REQUEST_SUCCESS,
90 ZEBRA_DPLANE_REQUEST_FAILURE,
91 };
92
93 /*
94 * API between the zebra dataplane system and the main zebra processing
95 * context.
96 */
97
98 /*
99 * Enqueue a route install or update for the dataplane.
100 */
101 enum dplane_op_e {
102 DPLANE_OP_NONE = 0,
103
104 /* Route update */
105 DPLANE_OP_ROUTE_INSTALL,
106 DPLANE_OP_ROUTE_UPDATE,
107 DPLANE_OP_ROUTE_DELETE,
108 DPLANE_OP_ROUTE_NOTIFY,
109
110 /* LSP update */
111 DPLANE_OP_LSP_INSTALL,
112 DPLANE_OP_LSP_UPDATE,
113 DPLANE_OP_LSP_DELETE,
114 DPLANE_OP_LSP_NOTIFY,
115
116 /* Pseudowire update */
117 DPLANE_OP_PW_INSTALL,
118 DPLANE_OP_PW_UNINSTALL,
119
120 /* System route notification */
121 DPLANE_OP_SYS_ROUTE_ADD,
122 DPLANE_OP_SYS_ROUTE_DELETE,
123
124 /* Interface address update */
125 DPLANE_OP_ADDR_INSTALL,
126 DPLANE_OP_ADDR_UNINSTALL,
127 };
128
129 /* Enable system route notifications */
130 void dplane_enable_sys_route_notifs(void);
131
132 /*
133 * The dataplane context struct is used to exchange info between the main zebra
134 * context and the dataplane module(s). If these are two independent pthreads,
135 * they cannot share existing global data structures safely.
136 */
137
138 /* Define a tailq list type for context blocks. The list is exposed/public,
139 * but the internal linkage in the context struct is private, so there
140 * are accessor apis that support enqueue and dequeue.
141 */
142 TAILQ_HEAD(dplane_ctx_q, zebra_dplane_ctx);
143
144 /* Allocate a context object */
145 struct zebra_dplane_ctx *dplane_ctx_alloc(void);
146
147 /* Return a dataplane results context block after use; the caller's pointer will
148 * be cleared.
149 */
150 void dplane_ctx_fini(struct zebra_dplane_ctx **pctx);
151
152 /* Enqueue a context block to caller's tailq. This exists so that the
153 * context struct can remain opaque.
154 */
155 void dplane_ctx_enqueue_tail(struct dplane_ctx_q *q,
156 const struct zebra_dplane_ctx *ctx);
157
158 /* Append a list of context blocks to another list - again, just keeping
159 * the context struct opaque.
160 */
161 void dplane_ctx_list_append(struct dplane_ctx_q *to_list,
162 struct dplane_ctx_q *from_list);
163
164 /* Dequeue a context block from the head of caller's tailq */
165 struct zebra_dplane_ctx *dplane_ctx_dequeue(struct dplane_ctx_q *q);
166
167 /*
168 * Accessors for information from the context object
169 */
170 enum zebra_dplane_result dplane_ctx_get_status(
171 const struct zebra_dplane_ctx *ctx);
172 void dplane_ctx_set_status(struct zebra_dplane_ctx *ctx,
173 enum zebra_dplane_result status);
174 const char *dplane_res2str(enum zebra_dplane_result res);
175
176 enum dplane_op_e dplane_ctx_get_op(const struct zebra_dplane_ctx *ctx);
177 void dplane_ctx_set_op(struct zebra_dplane_ctx *ctx, enum dplane_op_e op);
178 const char *dplane_op2str(enum dplane_op_e op);
179
180 const struct prefix *dplane_ctx_get_dest(const struct zebra_dplane_ctx *ctx);
181 void dplane_ctx_set_dest(struct zebra_dplane_ctx *ctx,
182 const struct prefix *dest);
183
184 /* Retrieve last/current provider id */
185 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx *ctx);
186
187 /* Providers running before the kernel can control whether a kernel
188 * update should be done.
189 */
190 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx *ctx);
191 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx *ctx);
192
193 /* Source prefix is a little special - use convention to return NULL
194 * to mean "no src prefix"
195 */
196 const struct prefix *dplane_ctx_get_src(const struct zebra_dplane_ctx *ctx);
197 void dplane_ctx_set_src(struct zebra_dplane_ctx *ctx, const struct prefix *src);
198
199 bool dplane_ctx_is_update(const struct zebra_dplane_ctx *ctx);
200 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx *ctx);
201 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx *ctx);
202 void dplane_ctx_set_vrf(struct zebra_dplane_ctx *ctx, vrf_id_t vrf);
203 vrf_id_t dplane_ctx_get_vrf(const struct zebra_dplane_ctx *ctx);
204
205 bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx *ctx);
206 void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx *ctx,
207 uint32_t id);
208 uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx *ctx);
209
210 /* Accessors for route update information */
211 void dplane_ctx_set_type(struct zebra_dplane_ctx *ctx, int type);
212 int dplane_ctx_get_type(const struct zebra_dplane_ctx *ctx);
213 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx *ctx);
214 void dplane_ctx_set_afi(struct zebra_dplane_ctx *ctx, afi_t afi);
215 afi_t dplane_ctx_get_afi(const struct zebra_dplane_ctx *ctx);
216 void dplane_ctx_set_safi(struct zebra_dplane_ctx *ctx, safi_t safi);
217 safi_t dplane_ctx_get_safi(const struct zebra_dplane_ctx *ctx);
218 void dplane_ctx_set_table(struct zebra_dplane_ctx *ctx, uint32_t table);
219 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx *ctx);
220 route_tag_t dplane_ctx_get_tag(const struct zebra_dplane_ctx *ctx);
221 void dplane_ctx_set_tag(struct zebra_dplane_ctx *ctx, route_tag_t tag);
222 route_tag_t dplane_ctx_get_old_tag(const struct zebra_dplane_ctx *ctx);
223 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx *ctx);
224 void dplane_ctx_set_instance(struct zebra_dplane_ctx *ctx, uint16_t instance);
225 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx *ctx);
226 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx *ctx);
227 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx *ctx);
228 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx *ctx);
229 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx *ctx);
230 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx *ctx);
231 void dplane_ctx_set_distance(struct zebra_dplane_ctx *ctx, uint8_t distance);
232 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx *ctx);
233
234 void dplane_ctx_set_nexthops(struct zebra_dplane_ctx *ctx, struct nexthop *nh);
235 const struct nexthop_group *dplane_ctx_get_ng(
236 const struct zebra_dplane_ctx *ctx);
237 const struct nexthop_group *dplane_ctx_get_old_ng(
238 const struct zebra_dplane_ctx *ctx);
239
240 /* Accessors for LSP information */
241 mpls_label_t dplane_ctx_get_in_label(const struct zebra_dplane_ctx *ctx);
242 void dplane_ctx_set_in_label(struct zebra_dplane_ctx *ctx,
243 mpls_label_t label);
244 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx *ctx);
245 void dplane_ctx_set_addr_family(struct zebra_dplane_ctx *ctx,
246 uint8_t family);
247 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx *ctx);
248 void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx *ctx,
249 uint32_t flags);
250 const zebra_nhlfe_t *dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx *ctx);
251 zebra_nhlfe_t *dplane_ctx_add_nhlfe(struct zebra_dplane_ctx *ctx,
252 enum lsp_types_t lsp_type,
253 enum nexthop_types_t nh_type,
254 union g_addr *gate,
255 ifindex_t ifindex,
256 mpls_label_t out_label);
257
258 const zebra_nhlfe_t *dplane_ctx_get_best_nhlfe(
259 const struct zebra_dplane_ctx *ctx);
260 const zebra_nhlfe_t *dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx *ctx,
261 zebra_nhlfe_t *nhlfe);
262 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx *ctx);
263
264 /* Accessors for pseudowire information */
265 const char *dplane_ctx_get_pw_ifname(const struct zebra_dplane_ctx *ctx);
266 mpls_label_t dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx *ctx);
267 mpls_label_t dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx *ctx);
268 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx *ctx);
269 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx *ctx);
270 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx *ctx);
271 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx *ctx);
272 const union g_addr *dplane_ctx_get_pw_dest(
273 const struct zebra_dplane_ctx *ctx);
274 const union pw_protocol_fields *dplane_ctx_get_pw_proto(
275 const struct zebra_dplane_ctx *ctx);
276 const struct nexthop_group *dplane_ctx_get_pw_nhg(
277 const struct zebra_dplane_ctx *ctx);
278
279 /* Accessors for interface information */
280 const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx *ctx);
281 ifindex_t dplane_ctx_get_ifindex(const struct zebra_dplane_ctx *ctx);
282 uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx *ctx);
283 /* Is interface addr p2p? */
284 bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx *ctx);
285 bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx *ctx);
286 bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx *ctx);
287 const struct prefix *dplane_ctx_get_intf_addr(
288 const struct zebra_dplane_ctx *ctx);
289 bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx *ctx);
290 const struct prefix *dplane_ctx_get_intf_dest(
291 const struct zebra_dplane_ctx *ctx);
292 bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx *ctx);
293 const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx *ctx);
294
295 /* Namespace info - esp. for netlink communication */
296 const struct zebra_dplane_info *dplane_ctx_get_ns(
297 const struct zebra_dplane_ctx *ctx);
298
299 /* Indicates zebra shutdown/exit is in progress. Some operations may be
300 * simplified or skipped during shutdown processing.
301 */
302 bool dplane_is_in_shutdown(void);
303
304 /*
305 * Enqueue route change operations for the dataplane.
306 */
307 enum zebra_dplane_result dplane_route_add(struct route_node *rn,
308 struct route_entry *re);
309
310 enum zebra_dplane_result dplane_route_update(struct route_node *rn,
311 struct route_entry *re,
312 struct route_entry *old_re);
313
314 enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
315 struct route_entry *re);
316
317 /* Notify the dplane when system/connected routes change */
318 enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
319 struct route_entry *re);
320 enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
321 struct route_entry *re);
322
323 /* Update from an async notification, to bring other fibs up-to-date */
324 enum zebra_dplane_result dplane_route_notif_update(
325 struct route_node *rn,
326 struct route_entry *re,
327 enum dplane_op_e op,
328 struct zebra_dplane_ctx *ctx);
329
330 /*
331 * Enqueue LSP change operations for the dataplane.
332 */
333 enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp);
334 enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp);
335 enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp);
336
337 /* Update or un-install resulting from an async notification */
338 enum zebra_dplane_result dplane_lsp_notif_update(zebra_lsp_t *lsp,
339 enum dplane_op_e op,
340 struct zebra_dplane_ctx *ctx);
341
342 /*
343 * Enqueue pseudowire operations for the dataplane.
344 */
345 enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw);
346 enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw);
347
348 /*
349 * Enqueue interface address changes for the dataplane.
350 */
351 enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
352 const struct connected *ifc);
353 enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
354 const struct connected *ifc);
355
356
357 /* Retrieve the limit on the number of pending, unprocessed updates. */
358 uint32_t dplane_get_in_queue_limit(void);
359
360 /* Configure limit on the number of pending, queued updates. If 'unset', reset
361 * to default value.
362 */
363 void dplane_set_in_queue_limit(uint32_t limit, bool set);
364
365 /* Retrieve the current queue depth of incoming, unprocessed updates */
366 uint32_t dplane_get_in_queue_len(void);
367
368 /*
369 * Vty/cli apis
370 */
371 int dplane_show_helper(struct vty *vty, bool detailed);
372 int dplane_show_provs_helper(struct vty *vty, bool detailed);
373
374 /*
375 * Dataplane providers: modules that process or consume dataplane events.
376 */
377
378 struct zebra_dplane_provider;
379
380 /* Support string name for a dataplane provider */
381 #define DPLANE_PROVIDER_NAMELEN 64
382
383 /* Priority or ordering values for providers. The idea is that there may be
384 * some pre-processing, followed by an external or remote dataplane,
385 * followed by the kernel, followed by some post-processing step (such as
386 * the fpm output stream.)
387 */
388 enum dplane_provider_prio {
389 DPLANE_PRIO_NONE = 0,
390 DPLANE_PRIO_PREPROCESS,
391 DPLANE_PRIO_PRE_KERNEL,
392 DPLANE_PRIO_KERNEL,
393 DPLANE_PRIO_POSTPROCESS,
394 DPLANE_PRIO_LAST
395 };
396
397 /* Flags values used during provider registration. */
398 #define DPLANE_PROV_FLAGS_DEFAULT 0x0
399
400 /* Provider will be spawning its own worker thread */
401 #define DPLANE_PROV_FLAG_THREADED 0x1
402
403
404 /* Provider registration: ordering or priority value, callbacks, and optional
405 * opaque data value. If 'prov_p', return the newly-allocated provider object
406 * on success.
407 */
408
409 /* Providers offer an entry-point for incoming work, called in the context of
410 * the dataplane pthread. The dataplane pthread enqueues any new work to the
411 * provider's 'inbound' queue, then calls the callback. The dataplane
412 * then checks the provider's outbound queue for completed work.
413 */
414
415 /*
416 * Providers can offer a 'start' callback; if present, the dataplane will
417 * call it when it is starting - when its pthread and event-scheduling
418 * thread_master are available.
419 */
420
421 /* Providers can offer an entry-point for shutdown and cleanup. This is called
422 * with 'early' during shutdown, to indicate that the dataplane subsystem
423 * is allowing work to move through the providers and finish.
424 * When called without 'early', the provider should release
425 * all resources (if it has any allocated).
426 */
427 int dplane_provider_register(const char *name,
428 enum dplane_provider_prio prio,
429 int flags,
430 int (*start_fp)(struct zebra_dplane_provider *),
431 int (*fp)(struct zebra_dplane_provider *),
432 int (*fini_fp)(struct zebra_dplane_provider *,
433 bool early),
434 void *data,
435 struct zebra_dplane_provider **prov_p);
436
437 /* Accessors for provider attributes */
438 const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov);
439 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov);
440 void *dplane_provider_get_data(const struct zebra_dplane_provider *prov);
441 bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov);
442
443 /* Lock/unlock a provider's mutex - iff the provider was registered with
444 * the THREADED flag.
445 */
446 void dplane_provider_lock(struct zebra_dplane_provider *prov);
447 void dplane_provider_unlock(struct zebra_dplane_provider *prov);
448
449 /* Obtain thread_master for dataplane thread */
450 struct thread_master *dplane_get_thread_master(void);
451
452 /* Providers should (generally) limit number of updates per work cycle */
453 int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov);
454
455 /* Provider api to signal that work/events are available
456 * for the dataplane pthread.
457 */
458 int dplane_provider_work_ready(void);
459
460 /* Dequeue, maintain associated counter and locking */
461 struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
462 struct zebra_dplane_provider *prov);
463
464 /* Dequeue work to a list, maintain counter and locking, return count */
465 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
466 struct dplane_ctx_q *listp);
467
468 /* Enqueue completed work, maintain associated counter and locking */
469 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
470 struct zebra_dplane_ctx *ctx);
471
472 /* Enqueue a context directly to zebra main. */
473 void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx);
474
475 /*
476 * Initialize the dataplane modules at zebra startup. This is currently called
477 * by the rib module. Zebra registers a results callback with the dataplane.
478 * The callback is called in the dataplane pthread context,
479 * so the expectation is that the contexts are queued for the zebra
480 * main pthread.
481 */
482 void zebra_dplane_init(int (*) (struct dplane_ctx_q *));
483
484 /*
485 * Start the dataplane pthread. This step needs to be run later than the
486 * 'init' step, in case zebra has fork-ed.
487 */
488 void zebra_dplane_start(void);
489
490 /* Finalize/cleanup apis, one called early as shutdown is starting,
491 * one called late at the end of zebra shutdown, and then one called
492 * from the zebra main pthread to stop the dplane pthread and
493 * free all resources.
494 *
495 * Zebra expects to try to clean up all vrfs and all routes during
496 * shutdown, so the dplane must be available until very late.
497 */
498 void zebra_dplane_pre_finish(void);
499 void zebra_dplane_finish(void);
500 void zebra_dplane_shutdown(void);
501
502 #ifdef __cplusplus
503 }
504 #endif
505
506 #endif /* _ZEBRA_DPLANE_H */