]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_dplane.c
zebra: use const in dplane pw nhlfe accessors
[mirror_frr.git] / zebra / zebra_dplane.c
1 /*
2 * Zebra dataplane layer.
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 #include "lib/libfrr.h"
21 #include "lib/debug.h"
22 #include "lib/frratomic.h"
23 #include "lib/frr_pthread.h"
24 #include "lib/memory.h"
25 #include "lib/queue.h"
26 #include "lib/zebra.h"
27 #include "zebra/zebra_router.h"
28 #include "zebra/zebra_memory.h"
29 #include "zebra/zebra_router.h"
30 #include "zebra/zebra_dplane.h"
31 #include "zebra/rt.h"
32 #include "zebra/debug.h"
33
34 /* Memory type for context blocks */
35 DEFINE_MTYPE(ZEBRA, DP_CTX, "Zebra DPlane Ctx")
36 DEFINE_MTYPE(ZEBRA, DP_PROV, "Zebra DPlane Provider")
37
38 #ifndef AOK
39 # define AOK 0
40 #endif
41
42 /* Enable test dataplane provider */
43 /*#define DPLANE_TEST_PROVIDER 1 */
44
45 /* Default value for max queued incoming updates */
46 const uint32_t DPLANE_DEFAULT_MAX_QUEUED = 200;
47
48 /* Default value for new work per cycle */
49 const uint32_t DPLANE_DEFAULT_NEW_WORK = 100;
50
51 /* Validation check macro for context blocks */
52 /* #define DPLANE_DEBUG 1 */
53
54 #ifdef DPLANE_DEBUG
55
56 # define DPLANE_CTX_VALID(p) \
57 assert((p) != NULL)
58
59 #else
60
61 # define DPLANE_CTX_VALID(p)
62
63 #endif /* DPLANE_DEBUG */
64
65 /*
66 * Route information captured for route updates.
67 */
68 struct dplane_route_info {
69
70 /* Dest and (optional) source prefixes */
71 struct prefix zd_dest;
72 struct prefix zd_src;
73
74 afi_t zd_afi;
75 safi_t zd_safi;
76
77 int zd_type;
78 int zd_old_type;
79
80 route_tag_t zd_tag;
81 route_tag_t zd_old_tag;
82 uint32_t zd_metric;
83 uint32_t zd_old_metric;
84
85 uint16_t zd_instance;
86 uint16_t zd_old_instance;
87
88 uint8_t zd_distance;
89 uint8_t zd_old_distance;
90
91 uint32_t zd_mtu;
92 uint32_t zd_nexthop_mtu;
93
94 /* Nexthops */
95 struct nexthop_group zd_ng;
96
97 /* "Previous" nexthops, used only in route updates without netlink */
98 struct nexthop_group zd_old_ng;
99
100 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
101
102 };
103
104 /*
105 * Pseudowire info for the dataplane
106 */
107 struct dplane_pw_info {
108 char ifname[IF_NAMESIZE];
109 ifindex_t ifindex;
110 int type;
111 int af;
112 int status;
113 uint32_t flags;
114 union g_addr dest;
115 mpls_label_t local_label;
116 mpls_label_t remote_label;
117
118 /* Nexthops */
119 struct nexthop_group nhg;
120
121 union pw_protocol_fields fields;
122 };
123
124 /*
125 * The context block used to exchange info about route updates across
126 * the boundary between the zebra main context (and pthread) and the
127 * dataplane layer (and pthread).
128 */
129 struct zebra_dplane_ctx {
130
131 /* Operation code */
132 enum dplane_op_e zd_op;
133
134 /* Status on return */
135 enum zebra_dplane_result zd_status;
136
137 /* Dplane provider id */
138 uint32_t zd_provider;
139
140 /* Flags - used by providers, e.g. */
141 int zd_flags;
142
143 bool zd_is_update;
144
145 uint32_t zd_seq;
146 uint32_t zd_old_seq;
147
148 /* TODO -- internal/sub-operation status? */
149 enum zebra_dplane_result zd_remote_status;
150 enum zebra_dplane_result zd_kernel_status;
151
152 vrf_id_t zd_vrf_id;
153 uint32_t zd_table_id;
154
155 /* Support info for either route or LSP update */
156 union {
157 struct dplane_route_info rinfo;
158 zebra_lsp_t lsp;
159 struct dplane_pw_info pw;
160 } u;
161
162 /* Namespace info, used especially for netlink kernel communication */
163 struct zebra_dplane_info zd_ns_info;
164
165 /* Embedded list linkage */
166 TAILQ_ENTRY(zebra_dplane_ctx) zd_q_entries;
167 };
168
169 /* Flag that can be set by a pre-kernel provider as a signal that an update
170 * should bypass the kernel.
171 */
172 #define DPLANE_CTX_FLAG_NO_KERNEL 0x01
173
174
175 /*
176 * Registration block for one dataplane provider.
177 */
178 struct zebra_dplane_provider {
179 /* Name */
180 char dp_name[DPLANE_PROVIDER_NAMELEN + 1];
181
182 /* Priority, for ordering among providers */
183 uint8_t dp_priority;
184
185 /* Id value */
186 uint32_t dp_id;
187
188 /* Mutex */
189 pthread_mutex_t dp_mutex;
190
191 /* Plugin-provided extra data */
192 void *dp_data;
193
194 /* Flags */
195 int dp_flags;
196
197 int (*dp_fp)(struct zebra_dplane_provider *prov);
198
199 int (*dp_fini)(struct zebra_dplane_provider *prov, bool early_p);
200
201 _Atomic uint32_t dp_in_counter;
202 _Atomic uint32_t dp_in_queued;
203 _Atomic uint32_t dp_in_max;
204 _Atomic uint32_t dp_out_counter;
205 _Atomic uint32_t dp_out_queued;
206 _Atomic uint32_t dp_out_max;
207 _Atomic uint32_t dp_error_counter;
208
209 /* Queue of contexts inbound to the provider */
210 struct dplane_ctx_q dp_ctx_in_q;
211
212 /* Queue of completed contexts outbound from the provider back
213 * towards the dataplane module.
214 */
215 struct dplane_ctx_q dp_ctx_out_q;
216
217 /* Embedded list linkage for provider objects */
218 TAILQ_ENTRY(zebra_dplane_provider) dp_prov_link;
219 };
220
221 /*
222 * Globals
223 */
224 static struct zebra_dplane_globals {
225 /* Mutex to control access to dataplane components */
226 pthread_mutex_t dg_mutex;
227
228 /* Results callback registered by zebra 'core' */
229 int (*dg_results_cb)(struct dplane_ctx_q *ctxlist);
230
231 /* Sentinel for beginning of shutdown */
232 volatile bool dg_is_shutdown;
233
234 /* Sentinel for end of shutdown */
235 volatile bool dg_run;
236
237 /* Route-update context queue inbound to the dataplane */
238 TAILQ_HEAD(zdg_ctx_q, zebra_dplane_ctx) dg_route_ctx_q;
239
240 /* Ordered list of providers */
241 TAILQ_HEAD(zdg_prov_q, zebra_dplane_provider) dg_providers_q;
242
243 /* Counter used to assign internal ids to providers */
244 uint32_t dg_provider_id;
245
246 /* Limit number of pending, unprocessed updates */
247 _Atomic uint32_t dg_max_queued_updates;
248
249 /* Limit number of new updates dequeued at once, to pace an
250 * incoming burst.
251 */
252 uint32_t dg_updates_per_cycle;
253
254 _Atomic uint32_t dg_routes_in;
255 _Atomic uint32_t dg_routes_queued;
256 _Atomic uint32_t dg_routes_queued_max;
257 _Atomic uint32_t dg_route_errors;
258 _Atomic uint32_t dg_other_errors;
259
260 _Atomic uint32_t dg_lsps_in;
261 _Atomic uint32_t dg_lsp_errors;
262
263 _Atomic uint32_t dg_pws_in;
264 _Atomic uint32_t dg_pw_errors;
265
266 _Atomic uint32_t dg_update_yields;
267
268 /* Dataplane pthread */
269 struct frr_pthread *dg_pthread;
270
271 /* Event-delivery context 'master' for the dplane */
272 struct thread_master *dg_master;
273
274 /* Event/'thread' pointer for queued updates */
275 struct thread *dg_t_update;
276
277 /* Event pointer for pending shutdown check loop */
278 struct thread *dg_t_shutdown_check;
279
280 } zdplane_info;
281
282 /*
283 * Lock and unlock for interactions with the zebra 'core' pthread
284 */
285 #define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
286 #define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
287
288
289 /*
290 * Lock and unlock for individual providers
291 */
292 #define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
293 #define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
294
295 /* Prototypes */
296 static int dplane_thread_loop(struct thread *event);
297 static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
298 struct zebra_ns *zns);
299 static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
300 enum dplane_op_e op);
301 static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
302 enum dplane_op_e op);
303
304 /*
305 * Public APIs
306 */
307
308 /* Obtain thread_master for dataplane thread */
309 struct thread_master *dplane_get_thread_master(void)
310 {
311 return zdplane_info.dg_master;
312 }
313
314 /*
315 * Allocate a dataplane update context
316 */
317 static struct zebra_dplane_ctx *dplane_ctx_alloc(void)
318 {
319 struct zebra_dplane_ctx *p;
320
321 /* TODO -- just alloc'ing memory, but would like to maintain
322 * a pool
323 */
324 p = XCALLOC(MTYPE_DP_CTX, sizeof(struct zebra_dplane_ctx));
325
326 return p;
327 }
328
329 /*
330 * Free a dataplane results context.
331 */
332 static void dplane_ctx_free(struct zebra_dplane_ctx **pctx)
333 {
334 if (pctx == NULL)
335 return;
336
337 DPLANE_CTX_VALID(*pctx);
338
339 /* TODO -- just freeing memory, but would like to maintain
340 * a pool
341 */
342
343 /* Some internal allocations may need to be freed, depending on
344 * the type of info captured in the ctx.
345 */
346 switch ((*pctx)->zd_op) {
347 case DPLANE_OP_ROUTE_INSTALL:
348 case DPLANE_OP_ROUTE_UPDATE:
349 case DPLANE_OP_ROUTE_DELETE:
350
351 /* Free allocated nexthops */
352 if ((*pctx)->u.rinfo.zd_ng.nexthop) {
353 /* This deals with recursive nexthops too */
354 nexthops_free((*pctx)->u.rinfo.zd_ng.nexthop);
355
356 (*pctx)->u.rinfo.zd_ng.nexthop = NULL;
357 }
358
359 if ((*pctx)->u.rinfo.zd_old_ng.nexthop) {
360 /* This deals with recursive nexthops too */
361 nexthops_free((*pctx)->u.rinfo.zd_old_ng.nexthop);
362
363 (*pctx)->u.rinfo.zd_old_ng.nexthop = NULL;
364 }
365
366 break;
367
368 case DPLANE_OP_LSP_INSTALL:
369 case DPLANE_OP_LSP_UPDATE:
370 case DPLANE_OP_LSP_DELETE:
371 {
372 zebra_nhlfe_t *nhlfe, *next;
373
374 /* Free allocated NHLFEs */
375 for (nhlfe = (*pctx)->u.lsp.nhlfe_list; nhlfe; nhlfe = next) {
376 next = nhlfe->next;
377
378 zebra_mpls_nhlfe_del(nhlfe);
379 }
380
381 /* Clear pointers in lsp struct, in case we're cacheing
382 * free context structs.
383 */
384 (*pctx)->u.lsp.nhlfe_list = NULL;
385 (*pctx)->u.lsp.best_nhlfe = NULL;
386
387 break;
388 }
389
390 case DPLANE_OP_PW_INSTALL:
391 case DPLANE_OP_PW_UNINSTALL:
392 /* Free allocated nexthops */
393 if ((*pctx)->u.pw.nhg.nexthop) {
394 /* This deals with recursive nexthops too */
395 nexthops_free((*pctx)->u.pw.nhg.nexthop);
396
397 (*pctx)->u.pw.nhg.nexthop = NULL;
398 }
399 break;
400
401 case DPLANE_OP_NONE:
402 break;
403 }
404
405 XFREE(MTYPE_DP_CTX, *pctx);
406 *pctx = NULL;
407 }
408
409 /*
410 * Return a context block to the dplane module after processing
411 */
412 void dplane_ctx_fini(struct zebra_dplane_ctx **pctx)
413 {
414 /* TODO -- maintain pool; for now, just free */
415 dplane_ctx_free(pctx);
416 }
417
418 /* Enqueue a context block */
419 void dplane_ctx_enqueue_tail(struct dplane_ctx_q *q,
420 const struct zebra_dplane_ctx *ctx)
421 {
422 TAILQ_INSERT_TAIL(q, (struct zebra_dplane_ctx *)ctx, zd_q_entries);
423 }
424
425 /* Append a list of context blocks to another list */
426 void dplane_ctx_list_append(struct dplane_ctx_q *to_list,
427 struct dplane_ctx_q *from_list)
428 {
429 if (TAILQ_FIRST(from_list)) {
430 TAILQ_CONCAT(to_list, from_list, zd_q_entries);
431
432 /* And clear 'from' list */
433 TAILQ_INIT(from_list);
434 }
435 }
436
437 /* Dequeue a context block from the head of a list */
438 struct zebra_dplane_ctx *dplane_ctx_dequeue(struct dplane_ctx_q *q)
439 {
440 struct zebra_dplane_ctx *ctx = TAILQ_FIRST(q);
441
442 if (ctx)
443 TAILQ_REMOVE(q, ctx, zd_q_entries);
444
445 return ctx;
446 }
447
448 /*
449 * Accessors for information from the context object
450 */
451 enum zebra_dplane_result dplane_ctx_get_status(
452 const struct zebra_dplane_ctx *ctx)
453 {
454 DPLANE_CTX_VALID(ctx);
455
456 return ctx->zd_status;
457 }
458
459 void dplane_ctx_set_status(struct zebra_dplane_ctx *ctx,
460 enum zebra_dplane_result status)
461 {
462 DPLANE_CTX_VALID(ctx);
463
464 ctx->zd_status = status;
465 }
466
467 /* Retrieve last/current provider id */
468 uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx *ctx)
469 {
470 DPLANE_CTX_VALID(ctx);
471 return ctx->zd_provider;
472 }
473
474 /* Providers run before the kernel can control whether a kernel
475 * update should be done.
476 */
477 void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx *ctx)
478 {
479 DPLANE_CTX_VALID(ctx);
480
481 SET_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
482 }
483
484 bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx *ctx)
485 {
486 DPLANE_CTX_VALID(ctx);
487
488 return CHECK_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
489 }
490
491 enum dplane_op_e dplane_ctx_get_op(const struct zebra_dplane_ctx *ctx)
492 {
493 DPLANE_CTX_VALID(ctx);
494
495 return ctx->zd_op;
496 }
497
498 const char *dplane_op2str(enum dplane_op_e op)
499 {
500 const char *ret = "UNKNOWN";
501
502 switch (op) {
503 case DPLANE_OP_NONE:
504 ret = "NONE";
505 break;
506
507 /* Route update */
508 case DPLANE_OP_ROUTE_INSTALL:
509 ret = "ROUTE_INSTALL";
510 break;
511 case DPLANE_OP_ROUTE_UPDATE:
512 ret = "ROUTE_UPDATE";
513 break;
514 case DPLANE_OP_ROUTE_DELETE:
515 ret = "ROUTE_DELETE";
516 break;
517
518 case DPLANE_OP_LSP_INSTALL:
519 ret = "LSP_INSTALL";
520 break;
521 case DPLANE_OP_LSP_UPDATE:
522 ret = "LSP_UPDATE";
523 break;
524 case DPLANE_OP_LSP_DELETE:
525 ret = "LSP_DELETE";
526 break;
527
528 case DPLANE_OP_PW_INSTALL:
529 ret = "PW_INSTALL";
530 break;
531 case DPLANE_OP_PW_UNINSTALL:
532 ret = "PW_UNINSTALL";
533 break;
534
535 }
536
537 return ret;
538 }
539
540 const char *dplane_res2str(enum zebra_dplane_result res)
541 {
542 const char *ret = "<Unknown>";
543
544 switch (res) {
545 case ZEBRA_DPLANE_REQUEST_FAILURE:
546 ret = "FAILURE";
547 break;
548 case ZEBRA_DPLANE_REQUEST_QUEUED:
549 ret = "QUEUED";
550 break;
551 case ZEBRA_DPLANE_REQUEST_SUCCESS:
552 ret = "SUCCESS";
553 break;
554 }
555
556 return ret;
557 }
558
559 const struct prefix *dplane_ctx_get_dest(const struct zebra_dplane_ctx *ctx)
560 {
561 DPLANE_CTX_VALID(ctx);
562
563 return &(ctx->u.rinfo.zd_dest);
564 }
565
566 /* Source prefix is a little special - return NULL for "no src prefix" */
567 const struct prefix *dplane_ctx_get_src(const struct zebra_dplane_ctx *ctx)
568 {
569 DPLANE_CTX_VALID(ctx);
570
571 if (ctx->u.rinfo.zd_src.prefixlen == 0 &&
572 IN6_IS_ADDR_UNSPECIFIED(&(ctx->u.rinfo.zd_src.u.prefix6))) {
573 return NULL;
574 } else {
575 return &(ctx->u.rinfo.zd_src);
576 }
577 }
578
579 bool dplane_ctx_is_update(const struct zebra_dplane_ctx *ctx)
580 {
581 DPLANE_CTX_VALID(ctx);
582
583 return ctx->zd_is_update;
584 }
585
586 uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx *ctx)
587 {
588 DPLANE_CTX_VALID(ctx);
589
590 return ctx->zd_seq;
591 }
592
593 uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx *ctx)
594 {
595 DPLANE_CTX_VALID(ctx);
596
597 return ctx->zd_old_seq;
598 }
599
600 vrf_id_t dplane_ctx_get_vrf(const struct zebra_dplane_ctx *ctx)
601 {
602 DPLANE_CTX_VALID(ctx);
603
604 return ctx->zd_vrf_id;
605 }
606
607 int dplane_ctx_get_type(const struct zebra_dplane_ctx *ctx)
608 {
609 DPLANE_CTX_VALID(ctx);
610
611 return ctx->u.rinfo.zd_type;
612 }
613
614 int dplane_ctx_get_old_type(const struct zebra_dplane_ctx *ctx)
615 {
616 DPLANE_CTX_VALID(ctx);
617
618 return ctx->u.rinfo.zd_old_type;
619 }
620
621 afi_t dplane_ctx_get_afi(const struct zebra_dplane_ctx *ctx)
622 {
623 DPLANE_CTX_VALID(ctx);
624
625 return ctx->u.rinfo.zd_afi;
626 }
627
628 safi_t dplane_ctx_get_safi(const struct zebra_dplane_ctx *ctx)
629 {
630 DPLANE_CTX_VALID(ctx);
631
632 return ctx->u.rinfo.zd_safi;
633 }
634
635 uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx *ctx)
636 {
637 DPLANE_CTX_VALID(ctx);
638
639 return ctx->zd_table_id;
640 }
641
642 route_tag_t dplane_ctx_get_tag(const struct zebra_dplane_ctx *ctx)
643 {
644 DPLANE_CTX_VALID(ctx);
645
646 return ctx->u.rinfo.zd_tag;
647 }
648
649 route_tag_t dplane_ctx_get_old_tag(const struct zebra_dplane_ctx *ctx)
650 {
651 DPLANE_CTX_VALID(ctx);
652
653 return ctx->u.rinfo.zd_old_tag;
654 }
655
656 uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx *ctx)
657 {
658 DPLANE_CTX_VALID(ctx);
659
660 return ctx->u.rinfo.zd_instance;
661 }
662
663 uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx *ctx)
664 {
665 DPLANE_CTX_VALID(ctx);
666
667 return ctx->u.rinfo.zd_old_instance;
668 }
669
670 uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx *ctx)
671 {
672 DPLANE_CTX_VALID(ctx);
673
674 return ctx->u.rinfo.zd_metric;
675 }
676
677 uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx *ctx)
678 {
679 DPLANE_CTX_VALID(ctx);
680
681 return ctx->u.rinfo.zd_old_metric;
682 }
683
684 uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx *ctx)
685 {
686 DPLANE_CTX_VALID(ctx);
687
688 return ctx->u.rinfo.zd_mtu;
689 }
690
691 uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx *ctx)
692 {
693 DPLANE_CTX_VALID(ctx);
694
695 return ctx->u.rinfo.zd_nexthop_mtu;
696 }
697
698 uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx *ctx)
699 {
700 DPLANE_CTX_VALID(ctx);
701
702 return ctx->u.rinfo.zd_distance;
703 }
704
705 uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx *ctx)
706 {
707 DPLANE_CTX_VALID(ctx);
708
709 return ctx->u.rinfo.zd_old_distance;
710 }
711
712 const struct nexthop_group *dplane_ctx_get_ng(
713 const struct zebra_dplane_ctx *ctx)
714 {
715 DPLANE_CTX_VALID(ctx);
716
717 return &(ctx->u.rinfo.zd_ng);
718 }
719
720 const struct nexthop_group *dplane_ctx_get_old_ng(
721 const struct zebra_dplane_ctx *ctx)
722 {
723 DPLANE_CTX_VALID(ctx);
724
725 return &(ctx->u.rinfo.zd_old_ng);
726 }
727
728 const struct zebra_dplane_info *dplane_ctx_get_ns(
729 const struct zebra_dplane_ctx *ctx)
730 {
731 DPLANE_CTX_VALID(ctx);
732
733 return &(ctx->zd_ns_info);
734 }
735
736 /* Accessors for LSP information */
737
738 mpls_label_t dplane_ctx_get_in_label(const struct zebra_dplane_ctx *ctx)
739 {
740 DPLANE_CTX_VALID(ctx);
741
742 return ctx->u.lsp.ile.in_label;
743 }
744
745 uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx *ctx)
746 {
747 DPLANE_CTX_VALID(ctx);
748
749 return ctx->u.lsp.addr_family;
750 }
751
752 uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx *ctx)
753 {
754 DPLANE_CTX_VALID(ctx);
755
756 return ctx->u.lsp.flags;
757 }
758
759 const zebra_nhlfe_t *dplane_ctx_get_nhlfe(const struct zebra_dplane_ctx *ctx)
760 {
761 DPLANE_CTX_VALID(ctx);
762
763 return ctx->u.lsp.nhlfe_list;
764 }
765
766 const zebra_nhlfe_t *
767 dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx *ctx)
768 {
769 DPLANE_CTX_VALID(ctx);
770
771 return ctx->u.lsp.best_nhlfe;
772 }
773
774 uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx *ctx)
775 {
776 DPLANE_CTX_VALID(ctx);
777
778 return ctx->u.lsp.num_ecmp;
779 }
780
781 const char *dplane_ctx_get_pw_ifname(const struct zebra_dplane_ctx *ctx)
782 {
783 DPLANE_CTX_VALID(ctx);
784
785 return ctx->u.pw.ifname;
786 }
787
788 mpls_label_t dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx *ctx)
789 {
790 DPLANE_CTX_VALID(ctx);
791
792 return ctx->u.pw.local_label;
793 }
794
795 mpls_label_t dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx *ctx)
796 {
797 DPLANE_CTX_VALID(ctx);
798
799 return ctx->u.pw.remote_label;
800 }
801
802 int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx *ctx)
803 {
804 DPLANE_CTX_VALID(ctx);
805
806 return ctx->u.pw.type;
807 }
808
809 int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx *ctx)
810 {
811 DPLANE_CTX_VALID(ctx);
812
813 return ctx->u.pw.af;
814 }
815
816 uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx *ctx)
817 {
818 DPLANE_CTX_VALID(ctx);
819
820 return ctx->u.pw.flags;
821 }
822
823 int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx *ctx)
824 {
825 DPLANE_CTX_VALID(ctx);
826
827 return ctx->u.pw.status;
828 }
829
830 const union g_addr *dplane_ctx_get_pw_dest(
831 const struct zebra_dplane_ctx *ctx)
832 {
833 DPLANE_CTX_VALID(ctx);
834
835 return &(ctx->u.pw.dest);
836 }
837
838 const union pw_protocol_fields *dplane_ctx_get_pw_proto(
839 const struct zebra_dplane_ctx *ctx)
840 {
841 DPLANE_CTX_VALID(ctx);
842
843 return &(ctx->u.pw.fields);
844 }
845
846 const struct nexthop_group *
847 dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx *ctx)
848 {
849 DPLANE_CTX_VALID(ctx);
850
851 return &(ctx->u.pw.nhg);
852 }
853
854 /*
855 * End of dplane context accessors
856 */
857
858
859 /*
860 * Retrieve the limit on the number of pending, unprocessed updates.
861 */
862 uint32_t dplane_get_in_queue_limit(void)
863 {
864 return atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
865 memory_order_relaxed);
866 }
867
868 /*
869 * Configure limit on the number of pending, queued updates.
870 */
871 void dplane_set_in_queue_limit(uint32_t limit, bool set)
872 {
873 /* Reset to default on 'unset' */
874 if (!set)
875 limit = DPLANE_DEFAULT_MAX_QUEUED;
876
877 atomic_store_explicit(&zdplane_info.dg_max_queued_updates, limit,
878 memory_order_relaxed);
879 }
880
881 /*
882 * Retrieve the current queue depth of incoming, unprocessed updates
883 */
884 uint32_t dplane_get_in_queue_len(void)
885 {
886 return atomic_load_explicit(&zdplane_info.dg_routes_queued,
887 memory_order_seq_cst);
888 }
889
890 /*
891 * Common dataplane context init with zebra namespace info.
892 */
893 static int dplane_ctx_ns_init(struct zebra_dplane_ctx *ctx,
894 struct zebra_ns *zns,
895 bool is_update)
896 {
897 dplane_info_from_zns(&(ctx->zd_ns_info), zns);
898
899 #if defined(HAVE_NETLINK)
900 /* Increment message counter after copying to context struct - may need
901 * two messages in some 'update' cases.
902 */
903 if (is_update)
904 zns->netlink_dplane.seq += 2;
905 else
906 zns->netlink_dplane.seq++;
907 #endif /* HAVE_NETLINK */
908
909 return AOK;
910 }
911
912 /*
913 * Initialize a context block for a route update from zebra data structs.
914 */
915 static int dplane_ctx_route_init(struct zebra_dplane_ctx *ctx,
916 enum dplane_op_e op,
917 struct route_node *rn,
918 struct route_entry *re)
919 {
920 int ret = EINVAL;
921 const struct route_table *table = NULL;
922 const rib_table_info_t *info;
923 const struct prefix *p, *src_p;
924 struct zebra_ns *zns;
925 struct zebra_vrf *zvrf;
926 struct nexthop *nexthop;
927
928 if (!ctx || !rn || !re)
929 goto done;
930
931 ctx->zd_op = op;
932 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
933
934 ctx->u.rinfo.zd_type = re->type;
935 ctx->u.rinfo.zd_old_type = re->type;
936
937 /* Prefixes: dest, and optional source */
938 srcdest_rnode_prefixes(rn, &p, &src_p);
939
940 prefix_copy(&(ctx->u.rinfo.zd_dest), p);
941
942 if (src_p)
943 prefix_copy(&(ctx->u.rinfo.zd_src), src_p);
944 else
945 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(ctx->u.rinfo.zd_src));
946
947 ctx->zd_table_id = re->table;
948
949 ctx->u.rinfo.zd_metric = re->metric;
950 ctx->u.rinfo.zd_old_metric = re->metric;
951 ctx->zd_vrf_id = re->vrf_id;
952 ctx->u.rinfo.zd_mtu = re->mtu;
953 ctx->u.rinfo.zd_nexthop_mtu = re->nexthop_mtu;
954 ctx->u.rinfo.zd_instance = re->instance;
955 ctx->u.rinfo.zd_tag = re->tag;
956 ctx->u.rinfo.zd_old_tag = re->tag;
957 ctx->u.rinfo.zd_distance = re->distance;
958
959 table = srcdest_rnode_table(rn);
960 info = table->info;
961
962 ctx->u.rinfo.zd_afi = info->afi;
963 ctx->u.rinfo.zd_safi = info->safi;
964
965 /* Extract ns info - can't use pointers to 'core' structs */
966 zvrf = vrf_info_lookup(re->vrf_id);
967 zns = zvrf->zns;
968
969 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_ROUTE_UPDATE));
970
971 /* Copy nexthops; recursive info is included too */
972 copy_nexthops(&(ctx->u.rinfo.zd_ng.nexthop), re->ng.nexthop, NULL);
973
974 /* TODO -- maybe use array of nexthops to avoid allocs? */
975
976 /* Ensure that the dplane's nexthops flags are clear. */
977 for (ALL_NEXTHOPS(ctx->u.rinfo.zd_ng, nexthop))
978 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
979
980 /* Trying out the sequence number idea, so we can try to detect
981 * when a result is stale.
982 */
983 re->dplane_sequence = zebra_router_get_next_sequence();
984 ctx->zd_seq = re->dplane_sequence;
985
986 ret = AOK;
987
988 done:
989 return ret;
990 }
991
992 /*
993 * Capture information for an LSP update in a dplane context.
994 */
995 static int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx,
996 enum dplane_op_e op,
997 zebra_lsp_t *lsp)
998 {
999 int ret = AOK;
1000 zebra_nhlfe_t *nhlfe, *new_nhlfe;
1001
1002 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1003 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1004 dplane_op2str(op), lsp->ile.in_label,
1005 lsp->num_ecmp);
1006
1007 ctx->zd_op = op;
1008 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1009
1010 /* Capture namespace info */
1011 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
1012 (op == DPLANE_OP_LSP_UPDATE));
1013
1014 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
1015
1016 ctx->u.lsp.ile = lsp->ile;
1017 ctx->u.lsp.addr_family = lsp->addr_family;
1018 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
1019 ctx->u.lsp.flags = lsp->flags;
1020
1021 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
1022 for (nhlfe = lsp->nhlfe_list; nhlfe; nhlfe = nhlfe->next) {
1023 /* Not sure if this is meaningful... */
1024 if (nhlfe->nexthop == NULL)
1025 continue;
1026
1027 new_nhlfe =
1028 zebra_mpls_lsp_add_nhlfe(
1029 &(ctx->u.lsp),
1030 nhlfe->type,
1031 nhlfe->nexthop->type,
1032 &(nhlfe->nexthop->gate),
1033 nhlfe->nexthop->ifindex,
1034 nhlfe->nexthop->nh_label->label[0]);
1035
1036 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
1037 ret = ENOMEM;
1038 break;
1039 }
1040
1041 /* Need to copy flags too */
1042 new_nhlfe->flags = nhlfe->flags;
1043 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
1044
1045 if (nhlfe == lsp->best_nhlfe)
1046 ctx->u.lsp.best_nhlfe = new_nhlfe;
1047 }
1048
1049 /* On error the ctx will be cleaned-up, so we don't need to
1050 * deal with any allocated nhlfe or nexthop structs here.
1051 */
1052
1053 return ret;
1054 }
1055
1056 /*
1057 * Capture information for an LSP update in a dplane context.
1058 */
1059 static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
1060 enum dplane_op_e op,
1061 struct zebra_pw *pw)
1062 {
1063 struct prefix p;
1064 afi_t afi;
1065 struct route_table *table;
1066 struct route_node *rn;
1067 struct route_entry *re;
1068
1069 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1070 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
1071 dplane_op2str(op), pw->ifname, pw->local_label,
1072 pw->remote_label);
1073
1074 ctx->zd_op = op;
1075 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1076
1077 /* Capture namespace info: no netlink support as of 12/18,
1078 * but just in case...
1079 */
1080 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
1081
1082 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
1083
1084 /* This name appears to be c-string, so we use string copy. */
1085 strlcpy(ctx->u.pw.ifname, pw->ifname, sizeof(ctx->u.pw.ifname));
1086
1087 ctx->zd_vrf_id = pw->vrf_id;
1088 ctx->u.pw.ifindex = pw->ifindex;
1089 ctx->u.pw.type = pw->type;
1090 ctx->u.pw.af = pw->af;
1091 ctx->u.pw.local_label = pw->local_label;
1092 ctx->u.pw.remote_label = pw->remote_label;
1093 ctx->u.pw.flags = pw->flags;
1094
1095 ctx->u.pw.dest = pw->nexthop;
1096
1097 ctx->u.pw.fields = pw->data;
1098
1099 /* Capture nexthop info for the pw destination. We need to look
1100 * up and use zebra datastructs, but we're running in the zebra
1101 * pthread here so that should be ok.
1102 */
1103 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
1104 p.family = pw->af;
1105 p.prefixlen = ((pw->af == AF_INET) ?
1106 IPV4_MAX_PREFIXLEN : IPV6_MAX_PREFIXLEN);
1107
1108 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
1109 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
1110 if (table) {
1111 rn = route_node_match(table, &p);
1112 if (rn) {
1113 RNODE_FOREACH_RE(rn, re) {
1114 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
1115 break;
1116 }
1117
1118 if (re)
1119 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
1120 re->ng.nexthop, NULL);
1121
1122 route_unlock_node(rn);
1123 }
1124 }
1125
1126 return AOK;
1127 }
1128
1129 /*
1130 * Enqueue a new route update,
1131 * and ensure an event is active for the dataplane pthread.
1132 */
1133 static int dplane_route_enqueue(struct zebra_dplane_ctx *ctx)
1134 {
1135 int ret = EINVAL;
1136 uint32_t high, curr;
1137
1138 /* Enqueue for processing by the dataplane pthread */
1139 DPLANE_LOCK();
1140 {
1141 TAILQ_INSERT_TAIL(&zdplane_info.dg_route_ctx_q, ctx,
1142 zd_q_entries);
1143 }
1144 DPLANE_UNLOCK();
1145
1146 curr = atomic_add_fetch_explicit(
1147 #ifdef __clang__
1148 /* TODO -- issue with the clang atomic/intrinsics currently;
1149 * casting away the 'Atomic'-ness of the variable works.
1150 */
1151 (uint32_t *)&(zdplane_info.dg_routes_queued),
1152 #else
1153 &(zdplane_info.dg_routes_queued),
1154 #endif
1155 1, memory_order_seq_cst);
1156
1157 /* Maybe update high-water counter also */
1158 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
1159 memory_order_seq_cst);
1160 while (high < curr) {
1161 if (atomic_compare_exchange_weak_explicit(
1162 &zdplane_info.dg_routes_queued_max,
1163 &high, curr,
1164 memory_order_seq_cst,
1165 memory_order_seq_cst))
1166 break;
1167 }
1168
1169 /* Ensure that an event for the dataplane thread is active */
1170 ret = dplane_provider_work_ready();
1171
1172 return ret;
1173 }
1174
1175 /*
1176 * Utility that prepares a route update and enqueues it for processing
1177 */
1178 static enum zebra_dplane_result
1179 dplane_route_update_internal(struct route_node *rn,
1180 struct route_entry *re,
1181 struct route_entry *old_re,
1182 enum dplane_op_e op)
1183 {
1184 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
1185 int ret = EINVAL;
1186 struct zebra_dplane_ctx *ctx = NULL;
1187
1188 /* Obtain context block */
1189 ctx = dplane_ctx_alloc();
1190 if (ctx == NULL) {
1191 ret = ENOMEM;
1192 goto done;
1193 }
1194
1195 /* Init context with info from zebra data structs */
1196 ret = dplane_ctx_route_init(ctx, op, rn, re);
1197 if (ret == AOK) {
1198 /* Capture some extra info for update case
1199 * where there's a different 'old' route.
1200 */
1201 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
1202 old_re && (old_re != re)) {
1203 ctx->zd_is_update = true;
1204
1205 old_re->dplane_sequence =
1206 zebra_router_get_next_sequence();
1207 ctx->zd_old_seq = old_re->dplane_sequence;
1208
1209 ctx->u.rinfo.zd_old_tag = old_re->tag;
1210 ctx->u.rinfo.zd_old_type = old_re->type;
1211 ctx->u.rinfo.zd_old_instance = old_re->instance;
1212 ctx->u.rinfo.zd_old_distance = old_re->distance;
1213 ctx->u.rinfo.zd_old_metric = old_re->metric;
1214
1215 #ifndef HAVE_NETLINK
1216 /* For bsd, capture previous re's nexthops too, sigh.
1217 * We'll need these to do per-nexthop deletes.
1218 */
1219 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
1220 old_re->ng.nexthop, NULL);
1221 #endif /* !HAVE_NETLINK */
1222 }
1223
1224 /* Enqueue context for processing */
1225 ret = dplane_route_enqueue(ctx);
1226 }
1227
1228 done:
1229 /* Update counter */
1230 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
1231 memory_order_relaxed);
1232
1233 if (ret == AOK)
1234 result = ZEBRA_DPLANE_REQUEST_QUEUED;
1235 else {
1236 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors, 1,
1237 memory_order_relaxed);
1238 if (ctx)
1239 dplane_ctx_free(&ctx);
1240 }
1241
1242 return result;
1243 }
1244
1245 /*
1246 * Enqueue a route 'add' for the dataplane.
1247 */
1248 enum zebra_dplane_result dplane_route_add(struct route_node *rn,
1249 struct route_entry *re)
1250 {
1251 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
1252
1253 if (rn == NULL || re == NULL)
1254 goto done;
1255
1256 ret = dplane_route_update_internal(rn, re, NULL,
1257 DPLANE_OP_ROUTE_INSTALL);
1258
1259 done:
1260 return ret;
1261 }
1262
1263 /*
1264 * Enqueue a route update for the dataplane.
1265 */
1266 enum zebra_dplane_result dplane_route_update(struct route_node *rn,
1267 struct route_entry *re,
1268 struct route_entry *old_re)
1269 {
1270 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
1271
1272 if (rn == NULL || re == NULL)
1273 goto done;
1274
1275 ret = dplane_route_update_internal(rn, re, old_re,
1276 DPLANE_OP_ROUTE_UPDATE);
1277 done:
1278 return ret;
1279 }
1280
1281 /*
1282 * Enqueue a route removal for the dataplane.
1283 */
1284 enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
1285 struct route_entry *re)
1286 {
1287 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
1288
1289 if (rn == NULL || re == NULL)
1290 goto done;
1291
1292 ret = dplane_route_update_internal(rn, re, NULL,
1293 DPLANE_OP_ROUTE_DELETE);
1294
1295 done:
1296 return ret;
1297 }
1298
1299 /*
1300 * Enqueue LSP add for the dataplane.
1301 */
1302 enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp)
1303 {
1304 enum zebra_dplane_result ret =
1305 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
1306
1307 return ret;
1308 }
1309
1310 /*
1311 * Enqueue LSP update for the dataplane.
1312 */
1313 enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp)
1314 {
1315 enum zebra_dplane_result ret =
1316 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
1317
1318 return ret;
1319 }
1320
1321 /*
1322 * Enqueue LSP delete for the dataplane.
1323 */
1324 enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp)
1325 {
1326 enum zebra_dplane_result ret =
1327 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
1328
1329 return ret;
1330 }
1331
1332 /*
1333 * Enqueue pseudowire install for the dataplane.
1334 */
1335 enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
1336 {
1337 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
1338 }
1339
1340 /*
1341 * Enqueue pseudowire un-install for the dataplane.
1342 */
1343 enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
1344 {
1345 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
1346 }
1347
1348 /*
1349 * Common internal LSP update utility
1350 */
1351 static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
1352 enum dplane_op_e op)
1353 {
1354 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
1355 int ret = EINVAL;
1356 struct zebra_dplane_ctx *ctx = NULL;
1357
1358 /* Obtain context block */
1359 ctx = dplane_ctx_alloc();
1360 if (ctx == NULL) {
1361 ret = ENOMEM;
1362 goto done;
1363 }
1364
1365 ret = dplane_ctx_lsp_init(ctx, op, lsp);
1366 if (ret != AOK)
1367 goto done;
1368
1369 ret = dplane_route_enqueue(ctx);
1370
1371 done:
1372 /* Update counter */
1373 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
1374 memory_order_relaxed);
1375
1376 if (ret == AOK)
1377 result = ZEBRA_DPLANE_REQUEST_QUEUED;
1378 else {
1379 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
1380 memory_order_relaxed);
1381 if (ctx)
1382 dplane_ctx_free(&ctx);
1383 }
1384
1385 return result;
1386 }
1387
1388 /*
1389 * Internal, common handler for pseudowire updates.
1390 */
1391 static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
1392 enum dplane_op_e op)
1393 {
1394 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
1395 int ret;
1396 struct zebra_dplane_ctx *ctx = NULL;
1397
1398 ctx = dplane_ctx_alloc();
1399 if (ctx == NULL) {
1400 ret = ENOMEM;
1401 goto done;
1402 }
1403
1404 ret = dplane_ctx_pw_init(ctx, op, pw);
1405 if (ret != AOK)
1406 goto done;
1407
1408 ret = dplane_route_enqueue(ctx);
1409
1410 done:
1411 /* Update counter */
1412 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
1413 memory_order_relaxed);
1414
1415 if (ret == AOK)
1416 result = ZEBRA_DPLANE_REQUEST_QUEUED;
1417 else {
1418 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
1419 memory_order_relaxed);
1420 if (ctx)
1421 dplane_ctx_free(&ctx);
1422 }
1423
1424 return result;
1425 }
1426
1427 /*
1428 * Handler for 'show dplane'
1429 */
1430 int dplane_show_helper(struct vty *vty, bool detailed)
1431 {
1432 uint64_t queued, queue_max, limit, errs, incoming, yields,
1433 other_errs;
1434
1435 /* Using atomics because counters are being changed in different
1436 * pthread contexts.
1437 */
1438 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
1439 memory_order_relaxed);
1440 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
1441 memory_order_relaxed);
1442 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
1443 memory_order_relaxed);
1444 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
1445 memory_order_relaxed);
1446 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
1447 memory_order_relaxed);
1448 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
1449 memory_order_relaxed);
1450 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
1451 memory_order_relaxed);
1452
1453 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
1454 incoming);
1455 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
1456 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
1457 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
1458 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
1459 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
1460 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
1461
1462 return CMD_SUCCESS;
1463 }
1464
1465 /*
1466 * Handler for 'show dplane providers'
1467 */
1468 int dplane_show_provs_helper(struct vty *vty, bool detailed)
1469 {
1470 struct zebra_dplane_provider *prov;
1471 uint64_t in, in_max, out, out_max;
1472
1473 vty_out(vty, "Zebra dataplane providers:\n");
1474
1475 DPLANE_LOCK();
1476 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
1477 DPLANE_UNLOCK();
1478
1479 /* Show counters, useful info from each registered provider */
1480 while (prov) {
1481
1482 in = atomic_load_explicit(&prov->dp_in_counter,
1483 memory_order_relaxed);
1484 in_max = atomic_load_explicit(&prov->dp_in_max,
1485 memory_order_relaxed);
1486 out = atomic_load_explicit(&prov->dp_out_counter,
1487 memory_order_relaxed);
1488 out_max = atomic_load_explicit(&prov->dp_out_max,
1489 memory_order_relaxed);
1490
1491 vty_out(vty, "%s (%u): in: %"PRIu64", q_max: %"PRIu64", "
1492 "out: %"PRIu64", q_max: %"PRIu64"\n",
1493 prov->dp_name, prov->dp_id, in, in_max, out, out_max);
1494
1495 DPLANE_LOCK();
1496 prov = TAILQ_NEXT(prov, dp_prov_link);
1497 DPLANE_UNLOCK();
1498 }
1499
1500 return CMD_SUCCESS;
1501 }
1502
1503 /*
1504 * Provider registration
1505 */
1506 int dplane_provider_register(const char *name,
1507 enum dplane_provider_prio prio,
1508 int flags,
1509 int (*fp)(struct zebra_dplane_provider *),
1510 int (*fini_fp)(struct zebra_dplane_provider *,
1511 bool early),
1512 void *data,
1513 struct zebra_dplane_provider **prov_p)
1514 {
1515 int ret = 0;
1516 struct zebra_dplane_provider *p = NULL, *last;
1517
1518 /* Validate */
1519 if (fp == NULL) {
1520 ret = EINVAL;
1521 goto done;
1522 }
1523
1524 if (prio <= DPLANE_PRIO_NONE ||
1525 prio > DPLANE_PRIO_LAST) {
1526 ret = EINVAL;
1527 goto done;
1528 }
1529
1530 /* Allocate and init new provider struct */
1531 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
1532
1533 pthread_mutex_init(&(p->dp_mutex), NULL);
1534 TAILQ_INIT(&(p->dp_ctx_in_q));
1535 TAILQ_INIT(&(p->dp_ctx_out_q));
1536
1537 p->dp_priority = prio;
1538 p->dp_fp = fp;
1539 p->dp_fini = fini_fp;
1540 p->dp_data = data;
1541
1542 /* Lock - the dplane pthread may be running */
1543 DPLANE_LOCK();
1544
1545 p->dp_id = ++zdplane_info.dg_provider_id;
1546
1547 if (name)
1548 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
1549 else
1550 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
1551 "provider-%u", p->dp_id);
1552
1553 /* Insert into list ordered by priority */
1554 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
1555 if (last->dp_priority > p->dp_priority)
1556 break;
1557 }
1558
1559 if (last)
1560 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
1561 else
1562 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
1563 dp_prov_link);
1564
1565 /* And unlock */
1566 DPLANE_UNLOCK();
1567
1568 if (IS_ZEBRA_DEBUG_DPLANE)
1569 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
1570 p->dp_name, p->dp_id, p->dp_priority);
1571
1572 done:
1573 if (prov_p)
1574 *prov_p = p;
1575
1576 return ret;
1577 }
1578
1579 /* Accessors for provider attributes */
1580 const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
1581 {
1582 return prov->dp_name;
1583 }
1584
1585 uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
1586 {
1587 return prov->dp_id;
1588 }
1589
1590 void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
1591 {
1592 return prov->dp_data;
1593 }
1594
1595 int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
1596 {
1597 return zdplane_info.dg_updates_per_cycle;
1598 }
1599
1600 /* Lock/unlock a provider's mutex - iff the provider was registered with
1601 * the THREADED flag.
1602 */
1603 void dplane_provider_lock(struct zebra_dplane_provider *prov)
1604 {
1605 if (dplane_provider_is_threaded(prov))
1606 DPLANE_PROV_LOCK(prov);
1607 }
1608
1609 void dplane_provider_unlock(struct zebra_dplane_provider *prov)
1610 {
1611 if (dplane_provider_is_threaded(prov))
1612 DPLANE_PROV_UNLOCK(prov);
1613 }
1614
1615 /*
1616 * Dequeue and maintain associated counter
1617 */
1618 struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
1619 struct zebra_dplane_provider *prov)
1620 {
1621 struct zebra_dplane_ctx *ctx = NULL;
1622
1623 dplane_provider_lock(prov);
1624
1625 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
1626 if (ctx) {
1627 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
1628
1629 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
1630 memory_order_relaxed);
1631 }
1632
1633 dplane_provider_unlock(prov);
1634
1635 return ctx;
1636 }
1637
1638 /*
1639 * Dequeue work to a list, return count
1640 */
1641 int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
1642 struct dplane_ctx_q *listp)
1643 {
1644 int limit, ret;
1645 struct zebra_dplane_ctx *ctx;
1646
1647 limit = zdplane_info.dg_updates_per_cycle;
1648
1649 dplane_provider_lock(prov);
1650
1651 for (ret = 0; ret < limit; ret++) {
1652 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
1653 if (ctx) {
1654 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
1655
1656 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
1657 } else {
1658 break;
1659 }
1660 }
1661
1662 if (ret > 0)
1663 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
1664 memory_order_relaxed);
1665
1666 dplane_provider_unlock(prov);
1667
1668 return ret;
1669 }
1670
1671 /*
1672 * Enqueue and maintain associated counter
1673 */
1674 void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
1675 struct zebra_dplane_ctx *ctx)
1676 {
1677 dplane_provider_lock(prov);
1678
1679 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
1680 zd_q_entries);
1681
1682 dplane_provider_unlock(prov);
1683
1684 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
1685 memory_order_relaxed);
1686 }
1687
1688 /*
1689 * Accessor for provider object
1690 */
1691 bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
1692 {
1693 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
1694 }
1695
1696 /*
1697 * Internal helper that copies information from a zebra ns object; this is
1698 * called in the zebra main pthread context as part of dplane ctx init.
1699 */
1700 static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
1701 struct zebra_ns *zns)
1702 {
1703 ns_info->ns_id = zns->ns_id;
1704
1705 #if defined(HAVE_NETLINK)
1706 ns_info->is_cmd = true;
1707 ns_info->nls = zns->netlink_dplane;
1708 #endif /* NETLINK */
1709 }
1710
1711 /*
1712 * Provider api to signal that work/events are available
1713 * for the dataplane pthread.
1714 */
1715 int dplane_provider_work_ready(void)
1716 {
1717 /* Note that during zebra startup, we may be offered work before
1718 * the dataplane pthread (and thread-master) are ready. We want to
1719 * enqueue the work, but the event-scheduling machinery may not be
1720 * available.
1721 */
1722 if (zdplane_info.dg_run) {
1723 thread_add_event(zdplane_info.dg_master,
1724 dplane_thread_loop, NULL, 0,
1725 &zdplane_info.dg_t_update);
1726 }
1727
1728 return AOK;
1729 }
1730
1731 /*
1732 * Kernel dataplane provider
1733 */
1734
1735 /*
1736 * Handler for kernel LSP updates
1737 */
1738 static enum zebra_dplane_result
1739 kernel_dplane_lsp_update(struct zebra_dplane_ctx *ctx)
1740 {
1741 enum zebra_dplane_result res;
1742
1743 /* Call into the synchronous kernel-facing code here */
1744 res = kernel_lsp_update(ctx);
1745
1746 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
1747 atomic_fetch_add_explicit(
1748 &zdplane_info.dg_lsp_errors, 1,
1749 memory_order_relaxed);
1750
1751 return res;
1752 }
1753
1754 /*
1755 * Handler for kernel pseudowire updates
1756 */
1757 static enum zebra_dplane_result
1758 kernel_dplane_pw_update(struct zebra_dplane_ctx *ctx)
1759 {
1760 enum zebra_dplane_result res;
1761
1762 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1763 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
1764 dplane_ctx_get_pw_ifname(ctx),
1765 dplane_op2str(ctx->zd_op),
1766 dplane_ctx_get_pw_af(ctx),
1767 dplane_ctx_get_pw_local_label(ctx),
1768 dplane_ctx_get_pw_remote_label(ctx));
1769
1770 res = kernel_pw_update(ctx);
1771
1772 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
1773 atomic_fetch_add_explicit(
1774 &zdplane_info.dg_pw_errors, 1,
1775 memory_order_relaxed);
1776
1777 return res;
1778 }
1779
1780 /*
1781 * Handler for kernel route updates
1782 */
1783 static enum zebra_dplane_result
1784 kernel_dplane_route_update(struct zebra_dplane_ctx *ctx)
1785 {
1786 enum zebra_dplane_result res;
1787
1788 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
1789 char dest_str[PREFIX_STRLEN];
1790
1791 prefix2str(dplane_ctx_get_dest(ctx),
1792 dest_str, sizeof(dest_str));
1793
1794 zlog_debug("%u:%s Dplane route update ctx %p op %s",
1795 dplane_ctx_get_vrf(ctx), dest_str,
1796 ctx, dplane_op2str(dplane_ctx_get_op(ctx)));
1797 }
1798
1799 /* Call into the synchronous kernel-facing code here */
1800 res = kernel_route_update(ctx);
1801
1802 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
1803 atomic_fetch_add_explicit(
1804 &zdplane_info.dg_route_errors, 1,
1805 memory_order_relaxed);
1806
1807 return res;
1808 }
1809
1810 /*
1811 * Kernel provider callback
1812 */
1813 static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
1814 {
1815 enum zebra_dplane_result res;
1816 struct zebra_dplane_ctx *ctx;
1817 int counter, limit;
1818
1819 limit = dplane_provider_get_work_limit(prov);
1820
1821 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1822 zlog_debug("dplane provider '%s': processing",
1823 dplane_provider_get_name(prov));
1824
1825 for (counter = 0; counter < limit; counter++) {
1826
1827 ctx = dplane_provider_dequeue_in_ctx(prov);
1828 if (ctx == NULL)
1829 break;
1830
1831 /* Dispatch to appropriate kernel-facing apis */
1832 switch (dplane_ctx_get_op(ctx)) {
1833
1834 case DPLANE_OP_ROUTE_INSTALL:
1835 case DPLANE_OP_ROUTE_UPDATE:
1836 case DPLANE_OP_ROUTE_DELETE:
1837 res = kernel_dplane_route_update(ctx);
1838 break;
1839
1840 case DPLANE_OP_LSP_INSTALL:
1841 case DPLANE_OP_LSP_UPDATE:
1842 case DPLANE_OP_LSP_DELETE:
1843 res = kernel_dplane_lsp_update(ctx);
1844 break;
1845
1846 case DPLANE_OP_PW_INSTALL:
1847 case DPLANE_OP_PW_UNINSTALL:
1848 res = kernel_dplane_pw_update(ctx);
1849 break;
1850
1851 default:
1852 atomic_fetch_add_explicit(
1853 &zdplane_info.dg_other_errors, 1,
1854 memory_order_relaxed);
1855
1856 res = ZEBRA_DPLANE_REQUEST_FAILURE;
1857 break;
1858 }
1859
1860 dplane_ctx_set_status(ctx, res);
1861
1862 dplane_provider_enqueue_out_ctx(prov, ctx);
1863 }
1864
1865 /* Ensure that we'll run the work loop again if there's still
1866 * more work to do.
1867 */
1868 if (counter >= limit) {
1869 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1870 zlog_debug("dplane provider '%s' reached max updates %d",
1871 dplane_provider_get_name(prov), counter);
1872
1873 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
1874 1, memory_order_relaxed);
1875
1876 dplane_provider_work_ready();
1877 }
1878
1879 return 0;
1880 }
1881
1882 #if DPLANE_TEST_PROVIDER
1883
1884 /*
1885 * Test dataplane provider plugin
1886 */
1887
1888 /*
1889 * Test provider process callback
1890 */
1891 static int test_dplane_process_func(struct zebra_dplane_provider *prov)
1892 {
1893 struct zebra_dplane_ctx *ctx;
1894 int counter, limit;
1895
1896 /* Just moving from 'in' queue to 'out' queue */
1897
1898 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1899 zlog_debug("dplane provider '%s': processing",
1900 dplane_provider_get_name(prov));
1901
1902 limit = dplane_provider_get_work_limit(prov);
1903
1904 for (counter = 0; counter < limit; counter++) {
1905
1906 ctx = dplane_provider_dequeue_in_ctx(prov);
1907 if (ctx == NULL)
1908 break;
1909
1910 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
1911
1912 dplane_provider_enqueue_out_ctx(prov, ctx);
1913 }
1914
1915 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1916 zlog_debug("dplane provider '%s': processed %d",
1917 dplane_provider_get_name(prov), counter);
1918
1919 /* Ensure that we'll run the work loop again if there's still
1920 * more work to do.
1921 */
1922 if (counter >= limit)
1923 dplane_provider_work_ready();
1924
1925 return 0;
1926 }
1927
1928 /*
1929 * Test provider shutdown/fini callback
1930 */
1931 static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
1932 bool early)
1933 {
1934 if (IS_ZEBRA_DEBUG_DPLANE)
1935 zlog_debug("dplane provider '%s': %sshutdown",
1936 dplane_provider_get_name(prov),
1937 early ? "early " : "");
1938
1939 return 0;
1940 }
1941 #endif /* DPLANE_TEST_PROVIDER */
1942
1943 /*
1944 * Register default kernel provider
1945 */
1946 static void dplane_provider_init(void)
1947 {
1948 int ret;
1949
1950 ret = dplane_provider_register("Kernel",
1951 DPLANE_PRIO_KERNEL,
1952 DPLANE_PROV_FLAGS_DEFAULT,
1953 kernel_dplane_process_func,
1954 NULL,
1955 NULL, NULL);
1956
1957 if (ret != AOK)
1958 zlog_err("Unable to register kernel dplane provider: %d",
1959 ret);
1960
1961 #if DPLANE_TEST_PROVIDER
1962 /* Optional test provider ... */
1963 ret = dplane_provider_register("Test",
1964 DPLANE_PRIO_PRE_KERNEL,
1965 DPLANE_PROV_FLAGS_DEFAULT,
1966 test_dplane_process_func,
1967 test_dplane_shutdown_func,
1968 NULL /* data */, NULL);
1969
1970 if (ret != AOK)
1971 zlog_err("Unable to register test dplane provider: %d",
1972 ret);
1973 #endif /* DPLANE_TEST_PROVIDER */
1974 }
1975
1976 /* Indicates zebra shutdown/exit is in progress. Some operations may be
1977 * simplified or skipped during shutdown processing.
1978 */
1979 bool dplane_is_in_shutdown(void)
1980 {
1981 return zdplane_info.dg_is_shutdown;
1982 }
1983
1984 /*
1985 * Early or pre-shutdown, de-init notification api. This runs pretty
1986 * early during zebra shutdown, as a signal to stop new work and prepare
1987 * for updates generated by shutdown/cleanup activity, as zebra tries to
1988 * remove everything it's responsible for.
1989 * NB: This runs in the main zebra pthread context.
1990 */
1991 void zebra_dplane_pre_finish(void)
1992 {
1993 if (IS_ZEBRA_DEBUG_DPLANE)
1994 zlog_debug("Zebra dataplane pre-fini called");
1995
1996 zdplane_info.dg_is_shutdown = true;
1997
1998 /* TODO -- Notify provider(s) of pending shutdown */
1999 }
2000
2001 /*
2002 * Utility to determine whether work remains enqueued within the dplane;
2003 * used during system shutdown processing.
2004 */
2005 static bool dplane_work_pending(void)
2006 {
2007 bool ret = false;
2008 struct zebra_dplane_ctx *ctx;
2009 struct zebra_dplane_provider *prov;
2010
2011 /* TODO -- just checking incoming/pending work for now, must check
2012 * providers
2013 */
2014 DPLANE_LOCK();
2015 {
2016 ctx = TAILQ_FIRST(&zdplane_info.dg_route_ctx_q);
2017 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
2018 }
2019 DPLANE_UNLOCK();
2020
2021 if (ctx != NULL) {
2022 ret = true;
2023 goto done;
2024 }
2025
2026 while (prov) {
2027
2028 dplane_provider_lock(prov);
2029
2030 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
2031 if (ctx == NULL)
2032 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
2033
2034 dplane_provider_unlock(prov);
2035
2036 if (ctx != NULL)
2037 break;
2038
2039 DPLANE_LOCK();
2040 prov = TAILQ_NEXT(prov, dp_prov_link);
2041 DPLANE_UNLOCK();
2042 }
2043
2044 if (ctx != NULL)
2045 ret = true;
2046
2047 done:
2048 return ret;
2049 }
2050
2051 /*
2052 * Shutdown-time intermediate callback, used to determine when all pending
2053 * in-flight updates are done. If there's still work to do, reschedules itself.
2054 * If all work is done, schedules an event to the main zebra thread for
2055 * final zebra shutdown.
2056 * This runs in the dplane pthread context.
2057 */
2058 static int dplane_check_shutdown_status(struct thread *event)
2059 {
2060 if (IS_ZEBRA_DEBUG_DPLANE)
2061 zlog_debug("Zebra dataplane shutdown status check called");
2062
2063 if (dplane_work_pending()) {
2064 /* Reschedule dplane check on a short timer */
2065 thread_add_timer_msec(zdplane_info.dg_master,
2066 dplane_check_shutdown_status,
2067 NULL, 100,
2068 &zdplane_info.dg_t_shutdown_check);
2069
2070 /* TODO - give up and stop waiting after a short time? */
2071
2072 } else {
2073 /* We appear to be done - schedule a final callback event
2074 * for the zebra main pthread.
2075 */
2076 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
2077 }
2078
2079 return 0;
2080 }
2081
2082 /*
2083 * Shutdown, de-init api. This runs pretty late during shutdown,
2084 * after zebra has tried to free/remove/uninstall all routes during shutdown.
2085 * At this point, dplane work may still remain to be done, so we can't just
2086 * blindly terminate. If there's still work to do, we'll periodically check
2087 * and when done, we'll enqueue a task to the zebra main thread for final
2088 * termination processing.
2089 *
2090 * NB: This runs in the main zebra thread context.
2091 */
2092 void zebra_dplane_finish(void)
2093 {
2094 if (IS_ZEBRA_DEBUG_DPLANE)
2095 zlog_debug("Zebra dataplane fini called");
2096
2097 thread_add_event(zdplane_info.dg_master,
2098 dplane_check_shutdown_status, NULL, 0,
2099 &zdplane_info.dg_t_shutdown_check);
2100 }
2101
2102 /*
2103 * Main dataplane pthread event loop. The thread takes new incoming work
2104 * and offers it to the first provider. It then iterates through the
2105 * providers, taking complete work from each one and offering it
2106 * to the next in order. At each step, a limited number of updates are
2107 * processed during a cycle in order to provide some fairness.
2108 *
2109 * This loop through the providers is only run once, so that the dataplane
2110 * pthread can look for other pending work - such as i/o work on behalf of
2111 * providers.
2112 */
2113 static int dplane_thread_loop(struct thread *event)
2114 {
2115 struct dplane_ctx_q work_list;
2116 struct dplane_ctx_q error_list;
2117 struct zebra_dplane_provider *prov;
2118 struct zebra_dplane_ctx *ctx, *tctx;
2119 int limit, counter, error_counter;
2120 uint64_t curr, high;
2121
2122 /* Capture work limit per cycle */
2123 limit = zdplane_info.dg_updates_per_cycle;
2124
2125 /* Init temporary lists used to move contexts among providers */
2126 TAILQ_INIT(&work_list);
2127 TAILQ_INIT(&error_list);
2128 error_counter = 0;
2129
2130 /* Check for zebra shutdown */
2131 if (!zdplane_info.dg_run)
2132 goto done;
2133
2134 /* Dequeue some incoming work from zebra (if any) onto the temporary
2135 * working list.
2136 */
2137 DPLANE_LOCK();
2138
2139 /* Locate initial registered provider */
2140 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
2141
2142 /* Move new work from incoming list to temp list */
2143 for (counter = 0; counter < limit; counter++) {
2144 ctx = TAILQ_FIRST(&zdplane_info.dg_route_ctx_q);
2145 if (ctx) {
2146 TAILQ_REMOVE(&zdplane_info.dg_route_ctx_q, ctx,
2147 zd_q_entries);
2148
2149 ctx->zd_provider = prov->dp_id;
2150
2151 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
2152 } else {
2153 break;
2154 }
2155 }
2156
2157 DPLANE_UNLOCK();
2158
2159 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
2160 memory_order_relaxed);
2161
2162 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2163 zlog_debug("dplane: incoming new work counter: %d", counter);
2164
2165 /* Iterate through the registered providers, offering new incoming
2166 * work. If the provider has outgoing work in its queue, take that
2167 * work for the next provider
2168 */
2169 while (prov) {
2170
2171 /* At each iteration, the temporary work list has 'counter'
2172 * items.
2173 */
2174 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2175 zlog_debug("dplane enqueues %d new work to provider '%s'",
2176 counter, dplane_provider_get_name(prov));
2177
2178 /* Capture current provider id in each context; check for
2179 * error status.
2180 */
2181 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
2182 if (dplane_ctx_get_status(ctx) ==
2183 ZEBRA_DPLANE_REQUEST_SUCCESS) {
2184 ctx->zd_provider = prov->dp_id;
2185 } else {
2186 /*
2187 * TODO -- improve error-handling: recirc
2188 * errors backwards so that providers can
2189 * 'undo' their work (if they want to)
2190 */
2191
2192 /* Move to error list; will be returned
2193 * zebra main.
2194 */
2195 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
2196 TAILQ_INSERT_TAIL(&error_list,
2197 ctx, zd_q_entries);
2198 error_counter++;
2199 }
2200 }
2201
2202 /* Enqueue new work to the provider */
2203 dplane_provider_lock(prov);
2204
2205 if (TAILQ_FIRST(&work_list))
2206 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
2207 zd_q_entries);
2208
2209 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
2210 memory_order_relaxed);
2211 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
2212 memory_order_relaxed);
2213 curr = atomic_load_explicit(&prov->dp_in_queued,
2214 memory_order_relaxed);
2215 high = atomic_load_explicit(&prov->dp_in_max,
2216 memory_order_relaxed);
2217 if (curr > high)
2218 atomic_store_explicit(&prov->dp_in_max, curr,
2219 memory_order_relaxed);
2220
2221 dplane_provider_unlock(prov);
2222
2223 /* Reset the temp list (though the 'concat' may have done this
2224 * already), and the counter
2225 */
2226 TAILQ_INIT(&work_list);
2227 counter = 0;
2228
2229 /* Call into the provider code. Note that this is
2230 * unconditional: we offer to do work even if we don't enqueue
2231 * any _new_ work.
2232 */
2233 (*prov->dp_fp)(prov);
2234
2235 /* Check for zebra shutdown */
2236 if (!zdplane_info.dg_run)
2237 break;
2238
2239 /* Dequeue completed work from the provider */
2240 dplane_provider_lock(prov);
2241
2242 while (counter < limit) {
2243 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
2244 if (ctx) {
2245 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
2246 zd_q_entries);
2247
2248 TAILQ_INSERT_TAIL(&work_list,
2249 ctx, zd_q_entries);
2250 counter++;
2251 } else
2252 break;
2253 }
2254
2255 dplane_provider_unlock(prov);
2256
2257 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2258 zlog_debug("dplane dequeues %d completed work from provider %s",
2259 counter, dplane_provider_get_name(prov));
2260
2261 /* Locate next provider */
2262 DPLANE_LOCK();
2263 prov = TAILQ_NEXT(prov, dp_prov_link);
2264 DPLANE_UNLOCK();
2265 }
2266
2267 /* After all providers have been serviced, enqueue any completed
2268 * work and any errors back to zebra so it can process the results.
2269 */
2270 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2271 zlog_debug("dplane has %d completed, %d errors, for zebra main",
2272 counter, error_counter);
2273
2274 /*
2275 * Hand lists through the api to zebra main,
2276 * to reduce the number of lock/unlock cycles
2277 */
2278
2279 /* Call through to zebra main */
2280 (zdplane_info.dg_results_cb)(&error_list);
2281
2282 TAILQ_INIT(&error_list);
2283
2284
2285 /* Call through to zebra main */
2286 (zdplane_info.dg_results_cb)(&work_list);
2287
2288 TAILQ_INIT(&work_list);
2289
2290 done:
2291 return 0;
2292 }
2293
2294 /*
2295 * Final phase of shutdown, after all work enqueued to dplane has been
2296 * processed. This is called from the zebra main pthread context.
2297 */
2298 void zebra_dplane_shutdown(void)
2299 {
2300 if (IS_ZEBRA_DEBUG_DPLANE)
2301 zlog_debug("Zebra dataplane shutdown called");
2302
2303 /* Stop dplane thread, if it's running */
2304
2305 zdplane_info.dg_run = false;
2306
2307 THREAD_OFF(zdplane_info.dg_t_update);
2308
2309 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
2310
2311 /* Destroy pthread */
2312 frr_pthread_destroy(zdplane_info.dg_pthread);
2313 zdplane_info.dg_pthread = NULL;
2314 zdplane_info.dg_master = NULL;
2315
2316 /* TODO -- Notify provider(s) of final shutdown */
2317
2318 /* TODO -- Clean-up provider objects */
2319
2320 /* TODO -- Clean queue(s), free memory */
2321 }
2322
2323 /*
2324 * Initialize the dataplane module during startup, internal/private version
2325 */
2326 static void zebra_dplane_init_internal(void)
2327 {
2328 memset(&zdplane_info, 0, sizeof(zdplane_info));
2329
2330 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
2331
2332 TAILQ_INIT(&zdplane_info.dg_route_ctx_q);
2333 TAILQ_INIT(&zdplane_info.dg_providers_q);
2334
2335 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
2336
2337 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
2338
2339 /* Register default kernel 'provider' during init */
2340 dplane_provider_init();
2341 }
2342
2343 /*
2344 * Start the dataplane pthread. This step needs to be run later than the
2345 * 'init' step, in case zebra has fork-ed.
2346 */
2347 void zebra_dplane_start(void)
2348 {
2349 /* Start dataplane pthread */
2350
2351 struct frr_pthread_attr pattr = {
2352 .start = frr_pthread_attr_default.start,
2353 .stop = frr_pthread_attr_default.stop
2354 };
2355
2356 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
2357 "Zebra dplane");
2358
2359 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
2360
2361 zdplane_info.dg_run = true;
2362
2363 /* Enqueue an initial event for the dataplane pthread */
2364 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
2365 &zdplane_info.dg_t_update);
2366
2367 frr_pthread_run(zdplane_info.dg_pthread, NULL);
2368 }
2369
2370 /*
2371 * Initialize the dataplane module at startup; called by zebra rib_init()
2372 */
2373 void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
2374 {
2375 zebra_dplane_init_internal();
2376 zdplane_info.dg_results_cb = results_fp;
2377 }