]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_dplane.c
ospfd: replace inet_ntoa
[mirror_frr.git] / zebra / zebra_dplane.c
CommitLineData
ea1c14f6
MS
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
2618a52e
DL
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
18c37974 24#include "lib/libfrr.h"
1d11b21f
MS
25#include "lib/debug.h"
26#include "lib/frratomic.h"
7cdb1a84 27#include "lib/frr_pthread.h"
1d11b21f 28#include "lib/memory.h"
7cdb1a84 29#include "lib/queue.h"
1d11b21f 30#include "lib/zebra.h"
7cdb1a84 31#include "zebra/zebra_memory.h"
3801e764 32#include "zebra/zebra_router.h"
7cdb1a84 33#include "zebra/zebra_dplane.h"
f2a0ba3a 34#include "zebra/zebra_vxlan_private.h"
ee70f629 35#include "zebra/zebra_mpls.h"
7cdb1a84
MS
36#include "zebra/rt.h"
37#include "zebra/debug.h"
f62e5480 38#include "zebra/zebra_pbr.h"
7cdb1a84
MS
39
40/* Memory type for context blocks */
c1344b54
DL
41DEFINE_MTYPE_STATIC(ZEBRA, DP_CTX, "Zebra DPlane Ctx")
42DEFINE_MTYPE_STATIC(ZEBRA, DP_PROV, "Zebra DPlane Provider")
7cdb1a84
MS
43
44#ifndef AOK
45# define AOK 0
46#endif
47
e5a60d82
MS
48/* Enable test dataplane provider */
49/*#define DPLANE_TEST_PROVIDER 1 */
50
91f16812
MS
51/* Default value for max queued incoming updates */
52const uint32_t DPLANE_DEFAULT_MAX_QUEUED = 200;
53
c831033f
MS
54/* Default value for new work per cycle */
55const uint32_t DPLANE_DEFAULT_NEW_WORK = 100;
56
7cdb1a84
MS
57/* Validation check macro for context blocks */
58/* #define DPLANE_DEBUG 1 */
59
60#ifdef DPLANE_DEBUG
61
25779064
MS
62# define DPLANE_CTX_VALID(p) \
63 assert((p) != NULL)
7cdb1a84
MS
64
65#else
66
5709131c 67# define DPLANE_CTX_VALID(p)
7cdb1a84
MS
68
69#endif /* DPLANE_DEBUG */
70
0c8215cb
SW
71/*
72 * Nexthop information captured for nexthop/nexthop group updates
73 */
74struct dplane_nexthop_info {
75 uint32_t id;
df3cef24 76 uint32_t old_id;
0c8215cb
SW
77 afi_t afi;
78 vrf_id_t vrf_id;
38e40db1 79 int type;
0c8215cb
SW
80
81 struct nexthop_group ng;
e22e8001
SW
82 struct nh_grp nh_grp[MULTIPATH_NUM];
83 uint8_t nh_grp_count;
0c8215cb
SW
84};
85
7cdb1a84 86/*
0f461727 87 * Route information captured for route updates.
7cdb1a84 88 */
0f461727 89struct dplane_route_info {
7cdb1a84
MS
90
91 /* Dest and (optional) source prefixes */
92 struct prefix zd_dest;
93 struct prefix zd_src;
94
0f461727
MS
95 afi_t zd_afi;
96 safi_t zd_safi;
7cdb1a84
MS
97
98 int zd_type;
99 int zd_old_type;
100
7cdb1a84
MS
101 route_tag_t zd_tag;
102 route_tag_t zd_old_tag;
103 uint32_t zd_metric;
01ce7cba 104 uint32_t zd_old_metric;
0f461727 105
7cdb1a84
MS
106 uint16_t zd_instance;
107 uint16_t zd_old_instance;
108
109 uint8_t zd_distance;
110 uint8_t zd_old_distance;
111
112 uint32_t zd_mtu;
113 uint32_t zd_nexthop_mtu;
114
0c8215cb
SW
115 /* Nexthop hash entry info */
116 struct dplane_nexthop_info nhe;
f820d025 117
7cdb1a84 118 /* Nexthops */
1d48702e 119 uint32_t zd_nhg_id;
7cdb1a84
MS
120 struct nexthop_group zd_ng;
121
1d48702e
MS
122 /* Backup nexthops (if present) */
123 struct nexthop_group backup_ng;
124
4dfd7a02 125 /* "Previous" nexthops, used only in route updates without netlink */
01ce7cba 126 struct nexthop_group zd_old_ng;
1d48702e 127 struct nexthop_group old_backup_ng;
01ce7cba 128
b8e0423d
MS
129 /* TODO -- use fixed array of nexthops, to avoid mallocs? */
130
0f461727
MS
131};
132
d613b8e1
MS
133/*
134 * Pseudowire info for the dataplane
135 */
136struct dplane_pw_info {
d613b8e1
MS
137 int type;
138 int af;
139 int status;
140 uint32_t flags;
16d69787 141 union g_addr dest;
d613b8e1
MS
142 mpls_label_t local_label;
143 mpls_label_t remote_label;
144
16d69787
MS
145 /* Nexthops */
146 struct nexthop_group nhg;
147
d613b8e1
MS
148 union pw_protocol_fields fields;
149};
150
a4a4802a
MS
151/*
152 * Interface/prefix info for the dataplane
153 */
154struct dplane_intf_info {
155
a4a4802a
MS
156 uint32_t metric;
157 uint32_t flags;
158
159#define DPLANE_INTF_CONNECTED (1 << 0) /* Connected peer, p2p */
160#define DPLANE_INTF_SECONDARY (1 << 1)
64168803 161#define DPLANE_INTF_BROADCAST (1 << 2)
0f3af738 162#define DPLANE_INTF_HAS_DEST DPLANE_INTF_CONNECTED
64168803 163#define DPLANE_INTF_HAS_LABEL (1 << 4)
a4a4802a
MS
164
165 /* Interface address/prefix */
166 struct prefix prefix;
167
168 /* Dest address, for p2p, or broadcast prefix */
169 struct prefix dest_prefix;
170
171 char *label;
172 char label_buf[32];
173};
174
7597ac7b 175/*
0bbd4ff4 176 * EVPN MAC address info for the dataplane.
7597ac7b
MS
177 */
178struct dplane_mac_info {
179 vlanid_t vid;
478566d6 180 ifindex_t br_ifindex;
7597ac7b
MS
181 struct ethaddr mac;
182 struct in_addr vtep_ip;
183 bool is_sticky;
506efd37 184 uint32_t nhg_id;
f188e68e 185 uint32_t update_flags;
7597ac7b
MS
186};
187
931fa60c 188/*
d68e74b4 189 * Neighbor info for the dataplane
931fa60c
MS
190 */
191struct dplane_neigh_info {
192 struct ipaddr ip_addr;
193 struct ethaddr mac;
194 uint32_t flags;
195 uint16_t state;
f188e68e 196 uint32_t update_flags;
931fa60c
MS
197};
198
60d8d43b
JU
199/*
200 * Policy based routing rule info for the dataplane
201 */
202struct dplane_ctx_rule {
203 uint32_t priority;
204
205 /* The route table pointed by this rule */
206 uint32_t table;
207
208 /* Filter criteria */
209 uint32_t filter_bm;
210 uint32_t fwmark;
01f23aff 211 uint8_t dsfield;
60d8d43b
JU
212 struct prefix src_ip;
213 struct prefix dst_ip;
58a1d249 214 char ifname[INTERFACE_NAMSIZ + 1];
60d8d43b
JU
215};
216
217struct dplane_rule_info {
218 /*
219 * Originating zclient sock fd, so we can know who to send
220 * back to.
221 */
222 int sock;
223
224 int unique;
225 int seq;
226
227 struct dplane_ctx_rule new;
228 struct dplane_ctx_rule old;
229};
230
0f461727
MS
231/*
232 * The context block used to exchange info about route updates across
233 * the boundary between the zebra main context (and pthread) and the
234 * dataplane layer (and pthread).
235 */
236struct zebra_dplane_ctx {
237
238 /* Operation code */
239 enum dplane_op_e zd_op;
240
241 /* Status on return */
242 enum zebra_dplane_result zd_status;
243
244 /* Dplane provider id */
245 uint32_t zd_provider;
246
247 /* Flags - used by providers, e.g. */
248 int zd_flags;
249
250 bool zd_is_update;
251
252 uint32_t zd_seq;
253 uint32_t zd_old_seq;
254
0024a559
MS
255 /* Some updates may be generated by notifications: allow the
256 * plugin to notice and ignore results from its own notifications.
257 */
258 uint32_t zd_notif_provider;
259
0f461727
MS
260 /* TODO -- internal/sub-operation status? */
261 enum zebra_dplane_result zd_remote_status;
262 enum zebra_dplane_result zd_kernel_status;
263
264 vrf_id_t zd_vrf_id;
265 uint32_t zd_table_id;
266
7c7ef4a8
MS
267 char zd_ifname[INTERFACE_NAMSIZ];
268 ifindex_t zd_ifindex;
269
a4a4802a 270 /* Support info for different kinds of updates */
0f461727
MS
271 union {
272 struct dplane_route_info rinfo;
273 zebra_lsp_t lsp;
d613b8e1 274 struct dplane_pw_info pw;
a4a4802a 275 struct dplane_intf_info intf;
7597ac7b 276 struct dplane_mac_info macinfo;
931fa60c 277 struct dplane_neigh_info neigh;
60d8d43b 278 struct dplane_rule_info rule;
0f461727
MS
279 } u;
280
281 /* Namespace info, used especially for netlink kernel communication */
282 struct zebra_dplane_info zd_ns_info;
283
7cdb1a84 284 /* Embedded list linkage */
25779064 285 TAILQ_ENTRY(zebra_dplane_ctx) zd_q_entries;
7cdb1a84
MS
286};
287
c831033f
MS
288/* Flag that can be set by a pre-kernel provider as a signal that an update
289 * should bypass the kernel.
290 */
291#define DPLANE_CTX_FLAG_NO_KERNEL 0x01
292
293
7cdb1a84
MS
294/*
295 * Registration block for one dataplane provider.
296 */
25779064 297struct zebra_dplane_provider {
7cdb1a84
MS
298 /* Name */
299 char dp_name[DPLANE_PROVIDER_NAMELEN + 1];
300
301 /* Priority, for ordering among providers */
302 uint8_t dp_priority;
303
304 /* Id value */
305 uint32_t dp_id;
306
c831033f
MS
307 /* Mutex */
308 pthread_mutex_t dp_mutex;
309
310 /* Plugin-provided extra data */
311 void *dp_data;
312
313 /* Flags */
314 int dp_flags;
315
1dd4ea8a
MS
316 int (*dp_start)(struct zebra_dplane_provider *prov);
317
4c206c8f 318 int (*dp_fp)(struct zebra_dplane_provider *prov);
7cdb1a84 319
4c206c8f 320 int (*dp_fini)(struct zebra_dplane_provider *prov, bool early_p);
18c37974 321
0545c373 322 _Atomic uint32_t dp_in_counter;
c9d17fe8 323 _Atomic uint32_t dp_in_queued;
c831033f
MS
324 _Atomic uint32_t dp_in_max;
325 _Atomic uint32_t dp_out_counter;
c9d17fe8 326 _Atomic uint32_t dp_out_queued;
c831033f 327 _Atomic uint32_t dp_out_max;
0545c373 328 _Atomic uint32_t dp_error_counter;
1d11b21f 329
c831033f
MS
330 /* Queue of contexts inbound to the provider */
331 struct dplane_ctx_q dp_ctx_in_q;
332
333 /* Queue of completed contexts outbound from the provider back
334 * towards the dataplane module.
335 */
336 struct dplane_ctx_q dp_ctx_out_q;
7cdb1a84 337
c831033f
MS
338 /* Embedded list linkage for provider objects */
339 TAILQ_ENTRY(zebra_dplane_provider) dp_prov_link;
7cdb1a84
MS
340};
341
342/*
343 * Globals
344 */
25779064 345static struct zebra_dplane_globals {
7cdb1a84
MS
346 /* Mutex to control access to dataplane components */
347 pthread_mutex_t dg_mutex;
348
349 /* Results callback registered by zebra 'core' */
4c206c8f 350 int (*dg_results_cb)(struct dplane_ctx_q *ctxlist);
7cdb1a84 351
4dfd7a02
MS
352 /* Sentinel for beginning of shutdown */
353 volatile bool dg_is_shutdown;
354
355 /* Sentinel for end of shutdown */
1d11b21f
MS
356 volatile bool dg_run;
357
3fe4ccc4
MS
358 /* Update context queue inbound to the dataplane */
359 TAILQ_HEAD(zdg_ctx_q, zebra_dplane_ctx) dg_update_ctx_q;
7cdb1a84
MS
360
361 /* Ordered list of providers */
25779064 362 TAILQ_HEAD(zdg_prov_q, zebra_dplane_provider) dg_providers_q;
7cdb1a84 363
1d11b21f 364 /* Counter used to assign internal ids to providers */
b8e0423d
MS
365 uint32_t dg_provider_id;
366
91f16812
MS
367 /* Limit number of pending, unprocessed updates */
368 _Atomic uint32_t dg_max_queued_updates;
369
cf363e1b
MS
370 /* Control whether system route notifications should be produced. */
371 bool dg_sys_route_notifs;
372
c831033f
MS
373 /* Limit number of new updates dequeued at once, to pace an
374 * incoming burst.
375 */
376 uint32_t dg_updates_per_cycle;
377
0545c373 378 _Atomic uint32_t dg_routes_in;
1d11b21f 379 _Atomic uint32_t dg_routes_queued;
4dfd7a02 380 _Atomic uint32_t dg_routes_queued_max;
0545c373 381 _Atomic uint32_t dg_route_errors;
16c628de
MS
382 _Atomic uint32_t dg_other_errors;
383
f820d025
SW
384 _Atomic uint32_t dg_nexthops_in;
385 _Atomic uint32_t dg_nexthop_errors;
386
16c628de 387 _Atomic uint32_t dg_lsps_in;
16c628de
MS
388 _Atomic uint32_t dg_lsp_errors;
389
97d8d05a
MS
390 _Atomic uint32_t dg_pws_in;
391 _Atomic uint32_t dg_pw_errors;
392
64168803
MS
393 _Atomic uint32_t dg_intf_addrs_in;
394 _Atomic uint32_t dg_intf_addr_errors;
395
7597ac7b
MS
396 _Atomic uint32_t dg_macs_in;
397 _Atomic uint32_t dg_mac_errors;
398
931fa60c
MS
399 _Atomic uint32_t dg_neighs_in;
400 _Atomic uint32_t dg_neigh_errors;
401
60d8d43b
JU
402 _Atomic uint32_t dg_rules_in;
403 _Atomic uint32_t dg_rule_errors;
404
c831033f 405 _Atomic uint32_t dg_update_yields;
1d11b21f 406
d8c16a95
MS
407 /* Dataplane pthread */
408 struct frr_pthread *dg_pthread;
409
7cdb1a84
MS
410 /* Event-delivery context 'master' for the dplane */
411 struct thread_master *dg_master;
412
413 /* Event/'thread' pointer for queued updates */
414 struct thread *dg_t_update;
415
4dfd7a02
MS
416 /* Event pointer for pending shutdown check loop */
417 struct thread *dg_t_shutdown_check;
418
25779064 419} zdplane_info;
7cdb1a84
MS
420
421/*
c831033f 422 * Lock and unlock for interactions with the zebra 'core' pthread
7cdb1a84 423 */
25779064 424#define DPLANE_LOCK() pthread_mutex_lock(&zdplane_info.dg_mutex)
25779064 425#define DPLANE_UNLOCK() pthread_mutex_unlock(&zdplane_info.dg_mutex)
7cdb1a84 426
c831033f
MS
427
428/*
429 * Lock and unlock for individual providers
430 */
431#define DPLANE_PROV_LOCK(p) pthread_mutex_lock(&((p)->dp_mutex))
432#define DPLANE_PROV_UNLOCK(p) pthread_mutex_unlock(&((p)->dp_mutex))
433
7cdb1a84 434/* Prototypes */
c831033f 435static int dplane_thread_loop(struct thread *event);
62b8bb7a
MS
436static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
437 struct zebra_ns *zns);
16c628de
MS
438static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
439 enum dplane_op_e op);
97d8d05a
MS
440static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
441 enum dplane_op_e op);
64168803
MS
442static enum zebra_dplane_result intf_addr_update_internal(
443 const struct interface *ifp, const struct connected *ifc,
444 enum dplane_op_e op);
f73a8467 445static enum zebra_dplane_result mac_update_common(
7597ac7b 446 enum dplane_op_e op, const struct interface *ifp,
478566d6 447 const struct interface *br_ifp,
7597ac7b 448 vlanid_t vid, const struct ethaddr *mac,
f188e68e
AK
449 struct in_addr vtep_ip, bool sticky, uint32_t nhg_id,
450 uint32_t update_flags);
931fa60c
MS
451static enum zebra_dplane_result neigh_update_internal(
452 enum dplane_op_e op,
453 const struct interface *ifp,
454 const struct ethaddr *mac,
455 const struct ipaddr *ip,
f188e68e 456 uint32_t flags, uint16_t state, uint32_t update_flags);
7cdb1a84
MS
457
458/*
459 * Public APIs
460 */
461
ad6aad4d
MS
462/* Obtain thread_master for dataplane thread */
463struct thread_master *dplane_get_thread_master(void)
464{
465 return zdplane_info.dg_master;
466}
467
7cdb1a84 468/*
b8e0423d 469 * Allocate a dataplane update context
7cdb1a84 470 */
593e4eb1 471struct zebra_dplane_ctx *dplane_ctx_alloc(void)
7cdb1a84 472{
25779064 473 struct zebra_dplane_ctx *p;
7cdb1a84 474
b8e0423d
MS
475 /* TODO -- just alloc'ing memory, but would like to maintain
476 * a pool
477 */
25779064 478 p = XCALLOC(MTYPE_DP_CTX, sizeof(struct zebra_dplane_ctx));
7cdb1a84 479
5709131c 480 return p;
7cdb1a84
MS
481}
482
cf363e1b
MS
483/* Enable system route notifications */
484void dplane_enable_sys_route_notifs(void)
485{
486 zdplane_info.dg_sys_route_notifs = true;
487}
488
7cdb1a84 489/*
f73a8467 490 * Clean up dependent/internal allocations inside a context object
7cdb1a84 491 */
f73a8467 492static void dplane_ctx_free_internal(struct zebra_dplane_ctx *ctx)
7cdb1a84 493{
f73a8467
MS
494 /*
495 * Some internal allocations may need to be freed, depending on
16c628de
MS
496 * the type of info captured in the ctx.
497 */
f73a8467 498 switch (ctx->zd_op) {
16c628de
MS
499 case DPLANE_OP_ROUTE_INSTALL:
500 case DPLANE_OP_ROUTE_UPDATE:
501 case DPLANE_OP_ROUTE_DELETE:
cf363e1b
MS
502 case DPLANE_OP_SYS_ROUTE_ADD:
503 case DPLANE_OP_SYS_ROUTE_DELETE:
54818e3b 504 case DPLANE_OP_ROUTE_NOTIFY:
b8e0423d 505
16c628de 506 /* Free allocated nexthops */
f73a8467 507 if (ctx->u.rinfo.zd_ng.nexthop) {
7cdb1a84 508 /* This deals with recursive nexthops too */
f73a8467 509 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
16c628de 510
f73a8467 511 ctx->u.rinfo.zd_ng.nexthop = NULL;
7cdb1a84
MS
512 }
513
1d48702e 514 /* Free backup info also (if present) */
f73a8467 515 if (ctx->u.rinfo.backup_ng.nexthop) {
1d48702e 516 /* This deals with recursive nexthops too */
f73a8467 517 nexthops_free(ctx->u.rinfo.backup_ng.nexthop);
1d48702e 518
f73a8467 519 ctx->u.rinfo.backup_ng.nexthop = NULL;
1d48702e
MS
520 }
521
f73a8467 522 if (ctx->u.rinfo.zd_old_ng.nexthop) {
4dfd7a02 523 /* This deals with recursive nexthops too */
f73a8467 524 nexthops_free(ctx->u.rinfo.zd_old_ng.nexthop);
16c628de 525
f73a8467 526 ctx->u.rinfo.zd_old_ng.nexthop = NULL;
16c628de
MS
527 }
528
f73a8467 529 if (ctx->u.rinfo.old_backup_ng.nexthop) {
1d48702e 530 /* This deals with recursive nexthops too */
f73a8467 531 nexthops_free(ctx->u.rinfo.old_backup_ng.nexthop);
1d48702e 532
f73a8467 533 ctx->u.rinfo.old_backup_ng.nexthop = NULL;
1d48702e
MS
534 }
535
16c628de
MS
536 break;
537
f820d025
SW
538 case DPLANE_OP_NH_INSTALL:
539 case DPLANE_OP_NH_UPDATE:
540 case DPLANE_OP_NH_DELETE: {
f73a8467 541 if (ctx->u.rinfo.nhe.ng.nexthop) {
0c8215cb 542 /* This deals with recursive nexthops too */
f73a8467 543 nexthops_free(ctx->u.rinfo.nhe.ng.nexthop);
0c8215cb 544
f73a8467 545 ctx->u.rinfo.nhe.ng.nexthop = NULL;
0c8215cb 546 }
f820d025
SW
547 break;
548 }
549
16c628de
MS
550 case DPLANE_OP_LSP_INSTALL:
551 case DPLANE_OP_LSP_UPDATE:
552 case DPLANE_OP_LSP_DELETE:
104e3ad9 553 case DPLANE_OP_LSP_NOTIFY:
16c628de 554 {
ee70f629 555 zebra_nhlfe_t *nhlfe;
16c628de 556
cd4bb96f
MS
557 /* Unlink and free allocated NHLFEs */
558 frr_each_safe(nhlfe_list, &ctx->u.lsp.nhlfe_list, nhlfe) {
559 nhlfe_list_del(&ctx->u.lsp.nhlfe_list, nhlfe);
560 zebra_mpls_nhlfe_free(nhlfe);
561 }
562
563 /* Unlink and free allocated backup NHLFEs, if present */
564 frr_each_safe(nhlfe_list,
565 &(ctx->u.lsp.backup_nhlfe_list), nhlfe) {
566 nhlfe_list_del(&ctx->u.lsp.backup_nhlfe_list,
567 nhlfe);
568 zebra_mpls_nhlfe_free(nhlfe);
569 }
01ce7cba 570
cd4bb96f 571 /* Clear pointers in lsp struct, in case we're caching
16c628de
MS
572 * free context structs.
573 */
ee70f629 574 nhlfe_list_init(&ctx->u.lsp.nhlfe_list);
f73a8467 575 ctx->u.lsp.best_nhlfe = NULL;
cd4bb96f 576 nhlfe_list_init(&ctx->u.lsp.backup_nhlfe_list);
16c628de
MS
577
578 break;
579 }
580
97d8d05a
MS
581 case DPLANE_OP_PW_INSTALL:
582 case DPLANE_OP_PW_UNINSTALL:
16d69787 583 /* Free allocated nexthops */
f73a8467 584 if (ctx->u.pw.nhg.nexthop) {
16d69787 585 /* This deals with recursive nexthops too */
f73a8467 586 nexthops_free(ctx->u.pw.nhg.nexthop);
16d69787 587
f73a8467 588 ctx->u.pw.nhg.nexthop = NULL;
16d69787
MS
589 }
590 break;
591
a4a4802a
MS
592 case DPLANE_OP_ADDR_INSTALL:
593 case DPLANE_OP_ADDR_UNINSTALL:
64168803 594 /* Maybe free label string, if allocated */
f73a8467
MS
595 if (ctx->u.intf.label != NULL &&
596 ctx->u.intf.label != ctx->u.intf.label_buf) {
597 free(ctx->u.intf.label);
598 ctx->u.intf.label = NULL;
64168803
MS
599 }
600 break;
601
7597ac7b
MS
602 case DPLANE_OP_MAC_INSTALL:
603 case DPLANE_OP_MAC_DELETE:
f2412b2d 604 case DPLANE_OP_NEIGH_INSTALL:
931fa60c 605 case DPLANE_OP_NEIGH_UPDATE:
f2412b2d 606 case DPLANE_OP_NEIGH_DELETE:
0bbd4ff4
MS
607 case DPLANE_OP_VTEP_ADD:
608 case DPLANE_OP_VTEP_DELETE:
60d8d43b
JU
609 case DPLANE_OP_RULE_ADD:
610 case DPLANE_OP_RULE_DELETE:
611 case DPLANE_OP_RULE_UPDATE:
d68e74b4 612 case DPLANE_OP_NEIGH_DISCOVER:
16c628de
MS
613 case DPLANE_OP_NONE:
614 break;
7cdb1a84 615 }
f73a8467
MS
616}
617
618/*
619 * Free a dataplane results context.
620 */
621static void dplane_ctx_free(struct zebra_dplane_ctx **pctx)
622{
623 if (pctx == NULL)
624 return;
625
626 DPLANE_CTX_VALID(*pctx);
627
628 /* TODO -- just freeing memory, but would like to maintain
629 * a pool
630 */
631
632 /* Some internal allocations may need to be freed, depending on
633 * the type of info captured in the ctx.
634 */
635 dplane_ctx_free_internal(*pctx);
16c628de
MS
636
637 XFREE(MTYPE_DP_CTX, *pctx);
7cdb1a84
MS
638}
639
f73a8467
MS
640/*
641 * Reset an allocated context object for re-use. All internal allocations are
642 * freed and the context is memset.
643 */
644void dplane_ctx_reset(struct zebra_dplane_ctx *ctx)
645{
646 dplane_ctx_free_internal(ctx);
647 memset(ctx, 0, sizeof(*ctx));
648}
649
7cdb1a84
MS
650/*
651 * Return a context block to the dplane module after processing
652 */
25779064 653void dplane_ctx_fini(struct zebra_dplane_ctx **pctx)
7cdb1a84 654{
14b0bc8e 655 /* TODO -- maintain pool; for now, just free */
7cdb1a84
MS
656 dplane_ctx_free(pctx);
657}
658
659/* Enqueue a context block */
25779064
MS
660void dplane_ctx_enqueue_tail(struct dplane_ctx_q *q,
661 const struct zebra_dplane_ctx *ctx)
7cdb1a84 662{
25779064 663 TAILQ_INSERT_TAIL(q, (struct zebra_dplane_ctx *)ctx, zd_q_entries);
7cdb1a84
MS
664}
665
14b0bc8e
MS
666/* Append a list of context blocks to another list */
667void dplane_ctx_list_append(struct dplane_ctx_q *to_list,
668 struct dplane_ctx_q *from_list)
669{
670 if (TAILQ_FIRST(from_list)) {
671 TAILQ_CONCAT(to_list, from_list, zd_q_entries);
672
673 /* And clear 'from' list */
674 TAILQ_INIT(from_list);
675 }
676}
677
7cdb1a84 678/* Dequeue a context block from the head of a list */
68b375e0 679struct zebra_dplane_ctx *dplane_ctx_dequeue(struct dplane_ctx_q *q)
7cdb1a84 680{
25779064 681 struct zebra_dplane_ctx *ctx = TAILQ_FIRST(q);
5709131c
MS
682
683 if (ctx)
7cdb1a84 684 TAILQ_REMOVE(q, ctx, zd_q_entries);
7cdb1a84 685
68b375e0 686 return ctx;
7cdb1a84
MS
687}
688
689/*
690 * Accessors for information from the context object
691 */
25779064
MS
692enum zebra_dplane_result dplane_ctx_get_status(
693 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
694{
695 DPLANE_CTX_VALID(ctx);
696
5709131c 697 return ctx->zd_status;
7cdb1a84
MS
698}
699
c831033f
MS
700void dplane_ctx_set_status(struct zebra_dplane_ctx *ctx,
701 enum zebra_dplane_result status)
702{
703 DPLANE_CTX_VALID(ctx);
704
705 ctx->zd_status = status;
706}
707
708/* Retrieve last/current provider id */
709uint32_t dplane_ctx_get_provider(const struct zebra_dplane_ctx *ctx)
710{
711 DPLANE_CTX_VALID(ctx);
712 return ctx->zd_provider;
713}
714
715/* Providers run before the kernel can control whether a kernel
716 * update should be done.
717 */
718void dplane_ctx_set_skip_kernel(struct zebra_dplane_ctx *ctx)
719{
720 DPLANE_CTX_VALID(ctx);
721
722 SET_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
723}
724
725bool dplane_ctx_is_skip_kernel(const struct zebra_dplane_ctx *ctx)
726{
727 DPLANE_CTX_VALID(ctx);
728
729 return CHECK_FLAG(ctx->zd_flags, DPLANE_CTX_FLAG_NO_KERNEL);
730}
731
593e4eb1
MS
732void dplane_ctx_set_op(struct zebra_dplane_ctx *ctx, enum dplane_op_e op)
733{
734 DPLANE_CTX_VALID(ctx);
735 ctx->zd_op = op;
736}
737
25779064 738enum dplane_op_e dplane_ctx_get_op(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
739{
740 DPLANE_CTX_VALID(ctx);
741
5709131c 742 return ctx->zd_op;
7cdb1a84
MS
743}
744
5709131c 745const char *dplane_op2str(enum dplane_op_e op)
7cdb1a84
MS
746{
747 const char *ret = "UNKNOWN";
748
5709131c 749 switch (op) {
7cdb1a84
MS
750 case DPLANE_OP_NONE:
751 ret = "NONE";
752 break;
753
754 /* Route update */
755 case DPLANE_OP_ROUTE_INSTALL:
756 ret = "ROUTE_INSTALL";
757 break;
758 case DPLANE_OP_ROUTE_UPDATE:
759 ret = "ROUTE_UPDATE";
760 break;
761 case DPLANE_OP_ROUTE_DELETE:
762 ret = "ROUTE_DELETE";
763 break;
54818e3b
MS
764 case DPLANE_OP_ROUTE_NOTIFY:
765 ret = "ROUTE_NOTIFY";
766 break;
7cdb1a84 767
f820d025
SW
768 /* Nexthop update */
769 case DPLANE_OP_NH_INSTALL:
770 ret = "NH_INSTALL";
771 break;
772 case DPLANE_OP_NH_UPDATE:
773 ret = "NH_UPDATE";
774 break;
775 case DPLANE_OP_NH_DELETE:
776 ret = "NH_DELETE";
777 break;
778
16c628de
MS
779 case DPLANE_OP_LSP_INSTALL:
780 ret = "LSP_INSTALL";
781 break;
782 case DPLANE_OP_LSP_UPDATE:
783 ret = "LSP_UPDATE";
784 break;
785 case DPLANE_OP_LSP_DELETE:
786 ret = "LSP_DELETE";
787 break;
104e3ad9
MS
788 case DPLANE_OP_LSP_NOTIFY:
789 ret = "LSP_NOTIFY";
790 break;
16c628de 791
97d8d05a
MS
792 case DPLANE_OP_PW_INSTALL:
793 ret = "PW_INSTALL";
794 break;
795 case DPLANE_OP_PW_UNINSTALL:
796 ret = "PW_UNINSTALL";
797 break;
798
cf363e1b
MS
799 case DPLANE_OP_SYS_ROUTE_ADD:
800 ret = "SYS_ROUTE_ADD";
801 break;
802 case DPLANE_OP_SYS_ROUTE_DELETE:
803 ret = "SYS_ROUTE_DEL";
804 break;
a4a4802a
MS
805
806 case DPLANE_OP_ADDR_INSTALL:
807 ret = "ADDR_INSTALL";
808 break;
809 case DPLANE_OP_ADDR_UNINSTALL:
810 ret = "ADDR_UNINSTALL";
811 break;
812
7597ac7b
MS
813 case DPLANE_OP_MAC_INSTALL:
814 ret = "MAC_INSTALL";
815 break;
816 case DPLANE_OP_MAC_DELETE:
817 ret = "MAC_DELETE";
818 break;
f2412b2d
MS
819
820 case DPLANE_OP_NEIGH_INSTALL:
821 ret = "NEIGH_INSTALL";
822 break;
931fa60c
MS
823 case DPLANE_OP_NEIGH_UPDATE:
824 ret = "NEIGH_UPDATE";
825 break;
f2412b2d
MS
826 case DPLANE_OP_NEIGH_DELETE:
827 ret = "NEIGH_DELETE";
828 break;
0bbd4ff4
MS
829 case DPLANE_OP_VTEP_ADD:
830 ret = "VTEP_ADD";
831 break;
832 case DPLANE_OP_VTEP_DELETE:
833 ret = "VTEP_DELETE";
834 break;
60d8d43b
JU
835
836 case DPLANE_OP_RULE_ADD:
837 ret = "RULE_ADD";
838 break;
839 case DPLANE_OP_RULE_DELETE:
840 ret = "RULE_DELETE";
841 break;
842 case DPLANE_OP_RULE_UPDATE:
843 ret = "RULE_UPDATE";
844 break;
d68e74b4
JU
845
846 case DPLANE_OP_NEIGH_DISCOVER:
847 ret = "NEIGH_DISCOVER";
848 break;
5b94ec50 849 }
7cdb1a84 850
5709131c 851 return ret;
7cdb1a84
MS
852}
853
f183e380
MS
854const char *dplane_res2str(enum zebra_dplane_result res)
855{
856 const char *ret = "<Unknown>";
857
858 switch (res) {
859 case ZEBRA_DPLANE_REQUEST_FAILURE:
860 ret = "FAILURE";
861 break;
862 case ZEBRA_DPLANE_REQUEST_QUEUED:
863 ret = "QUEUED";
864 break;
865 case ZEBRA_DPLANE_REQUEST_SUCCESS:
866 ret = "SUCCESS";
867 break;
5b94ec50 868 }
f183e380
MS
869
870 return ret;
871}
872
593e4eb1
MS
873void dplane_ctx_set_dest(struct zebra_dplane_ctx *ctx,
874 const struct prefix *dest)
875{
876 DPLANE_CTX_VALID(ctx);
877
878 prefix_copy(&(ctx->u.rinfo.zd_dest), dest);
879}
880
25779064 881const struct prefix *dplane_ctx_get_dest(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
882{
883 DPLANE_CTX_VALID(ctx);
884
0f461727 885 return &(ctx->u.rinfo.zd_dest);
7cdb1a84
MS
886}
887
593e4eb1
MS
888void dplane_ctx_set_src(struct zebra_dplane_ctx *ctx, const struct prefix *src)
889{
890 DPLANE_CTX_VALID(ctx);
891
892 if (src)
893 prefix_copy(&(ctx->u.rinfo.zd_src), src);
894 else
895 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(struct prefix));
896}
897
5709131c 898/* Source prefix is a little special - return NULL for "no src prefix" */
25779064 899const struct prefix *dplane_ctx_get_src(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
900{
901 DPLANE_CTX_VALID(ctx);
902
0f461727
MS
903 if (ctx->u.rinfo.zd_src.prefixlen == 0 &&
904 IN6_IS_ADDR_UNSPECIFIED(&(ctx->u.rinfo.zd_src.u.prefix6))) {
5709131c 905 return NULL;
7cdb1a84 906 } else {
0f461727 907 return &(ctx->u.rinfo.zd_src);
7cdb1a84
MS
908 }
909}
910
25779064 911bool dplane_ctx_is_update(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
912{
913 DPLANE_CTX_VALID(ctx);
914
5709131c 915 return ctx->zd_is_update;
7cdb1a84
MS
916}
917
25779064 918uint32_t dplane_ctx_get_seq(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
919{
920 DPLANE_CTX_VALID(ctx);
921
5709131c 922 return ctx->zd_seq;
7cdb1a84
MS
923}
924
25779064 925uint32_t dplane_ctx_get_old_seq(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
926{
927 DPLANE_CTX_VALID(ctx);
928
5709131c 929 return ctx->zd_old_seq;
7cdb1a84
MS
930}
931
593e4eb1
MS
932void dplane_ctx_set_vrf(struct zebra_dplane_ctx *ctx, vrf_id_t vrf)
933{
934 DPLANE_CTX_VALID(ctx);
935
936 ctx->zd_vrf_id = vrf;
937}
938
25779064 939vrf_id_t dplane_ctx_get_vrf(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
940{
941 DPLANE_CTX_VALID(ctx);
942
5709131c 943 return ctx->zd_vrf_id;
7cdb1a84
MS
944}
945
0024a559
MS
946bool dplane_ctx_is_from_notif(const struct zebra_dplane_ctx *ctx)
947{
948 DPLANE_CTX_VALID(ctx);
949
950 return (ctx->zd_notif_provider != 0);
951}
952
953uint32_t dplane_ctx_get_notif_provider(const struct zebra_dplane_ctx *ctx)
954{
955 DPLANE_CTX_VALID(ctx);
956
957 return ctx->zd_notif_provider;
958}
959
9651af61
MS
960void dplane_ctx_set_notif_provider(struct zebra_dplane_ctx *ctx,
961 uint32_t id)
962{
963 DPLANE_CTX_VALID(ctx);
964
965 ctx->zd_notif_provider = id;
966}
fd563cc7 967
7c7ef4a8
MS
968const char *dplane_ctx_get_ifname(const struct zebra_dplane_ctx *ctx)
969{
970 DPLANE_CTX_VALID(ctx);
971
972 return ctx->zd_ifname;
973}
974
fd563cc7
KS
975void dplane_ctx_set_ifname(struct zebra_dplane_ctx *ctx, const char *ifname)
976{
977 DPLANE_CTX_VALID(ctx);
978
979 if (!ifname)
980 return;
981
982 strlcpy(ctx->zd_ifname, ifname, sizeof(ctx->zd_ifname));
983}
984
7c7ef4a8
MS
985ifindex_t dplane_ctx_get_ifindex(const struct zebra_dplane_ctx *ctx)
986{
987 DPLANE_CTX_VALID(ctx);
988
989 return ctx->zd_ifindex;
990}
9651af61 991
593e4eb1
MS
992void dplane_ctx_set_type(struct zebra_dplane_ctx *ctx, int type)
993{
994 DPLANE_CTX_VALID(ctx);
995
996 ctx->u.rinfo.zd_type = type;
997}
998
25779064 999int dplane_ctx_get_type(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1000{
1001 DPLANE_CTX_VALID(ctx);
1002
0f461727 1003 return ctx->u.rinfo.zd_type;
7cdb1a84
MS
1004}
1005
25779064 1006int dplane_ctx_get_old_type(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1007{
1008 DPLANE_CTX_VALID(ctx);
1009
0f461727 1010 return ctx->u.rinfo.zd_old_type;
7cdb1a84
MS
1011}
1012
593e4eb1
MS
1013void dplane_ctx_set_afi(struct zebra_dplane_ctx *ctx, afi_t afi)
1014{
1015 DPLANE_CTX_VALID(ctx);
1016
1017 ctx->u.rinfo.zd_afi = afi;
1018}
1019
25779064 1020afi_t dplane_ctx_get_afi(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1021{
1022 DPLANE_CTX_VALID(ctx);
1023
0f461727 1024 return ctx->u.rinfo.zd_afi;
7cdb1a84
MS
1025}
1026
593e4eb1
MS
1027void dplane_ctx_set_safi(struct zebra_dplane_ctx *ctx, safi_t safi)
1028{
1029 DPLANE_CTX_VALID(ctx);
1030
1031 ctx->u.rinfo.zd_safi = safi;
1032}
1033
25779064 1034safi_t dplane_ctx_get_safi(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1035{
1036 DPLANE_CTX_VALID(ctx);
1037
0f461727 1038 return ctx->u.rinfo.zd_safi;
7cdb1a84
MS
1039}
1040
593e4eb1
MS
1041void dplane_ctx_set_table(struct zebra_dplane_ctx *ctx, uint32_t table)
1042{
1043 DPLANE_CTX_VALID(ctx);
1044
1045 ctx->zd_table_id = table;
1046}
1047
25779064 1048uint32_t dplane_ctx_get_table(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1049{
1050 DPLANE_CTX_VALID(ctx);
1051
5709131c 1052 return ctx->zd_table_id;
7cdb1a84
MS
1053}
1054
25779064 1055route_tag_t dplane_ctx_get_tag(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1056{
1057 DPLANE_CTX_VALID(ctx);
1058
0f461727 1059 return ctx->u.rinfo.zd_tag;
7cdb1a84
MS
1060}
1061
6a91ae98
MS
1062void dplane_ctx_set_tag(struct zebra_dplane_ctx *ctx, route_tag_t tag)
1063{
1064 DPLANE_CTX_VALID(ctx);
1065
1066 ctx->u.rinfo.zd_tag = tag;
1067}
1068
25779064 1069route_tag_t dplane_ctx_get_old_tag(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1070{
1071 DPLANE_CTX_VALID(ctx);
1072
0f461727 1073 return ctx->u.rinfo.zd_old_tag;
7cdb1a84
MS
1074}
1075
25779064 1076uint16_t dplane_ctx_get_instance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1077{
1078 DPLANE_CTX_VALID(ctx);
1079
0f461727 1080 return ctx->u.rinfo.zd_instance;
7cdb1a84
MS
1081}
1082
6a91ae98
MS
1083void dplane_ctx_set_instance(struct zebra_dplane_ctx *ctx, uint16_t instance)
1084{
1085 DPLANE_CTX_VALID(ctx);
1086
1087 ctx->u.rinfo.zd_instance = instance;
1088}
1089
25779064 1090uint16_t dplane_ctx_get_old_instance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1091{
1092 DPLANE_CTX_VALID(ctx);
1093
0f461727 1094 return ctx->u.rinfo.zd_old_instance;
7cdb1a84
MS
1095}
1096
25779064 1097uint32_t dplane_ctx_get_metric(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1098{
1099 DPLANE_CTX_VALID(ctx);
1100
0f461727 1101 return ctx->u.rinfo.zd_metric;
7cdb1a84
MS
1102}
1103
25779064 1104uint32_t dplane_ctx_get_old_metric(const struct zebra_dplane_ctx *ctx)
01ce7cba
MS
1105{
1106 DPLANE_CTX_VALID(ctx);
1107
0f461727 1108 return ctx->u.rinfo.zd_old_metric;
01ce7cba
MS
1109}
1110
25779064 1111uint32_t dplane_ctx_get_mtu(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1112{
1113 DPLANE_CTX_VALID(ctx);
1114
0f461727 1115 return ctx->u.rinfo.zd_mtu;
7cdb1a84
MS
1116}
1117
25779064 1118uint32_t dplane_ctx_get_nh_mtu(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1119{
1120 DPLANE_CTX_VALID(ctx);
1121
0f461727 1122 return ctx->u.rinfo.zd_nexthop_mtu;
7cdb1a84
MS
1123}
1124
25779064 1125uint8_t dplane_ctx_get_distance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1126{
1127 DPLANE_CTX_VALID(ctx);
1128
0f461727 1129 return ctx->u.rinfo.zd_distance;
7cdb1a84
MS
1130}
1131
6a91ae98
MS
1132void dplane_ctx_set_distance(struct zebra_dplane_ctx *ctx, uint8_t distance)
1133{
1134 DPLANE_CTX_VALID(ctx);
1135
1136 ctx->u.rinfo.zd_distance = distance;
1137}
1138
25779064 1139uint8_t dplane_ctx_get_old_distance(const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1140{
1141 DPLANE_CTX_VALID(ctx);
1142
0f461727 1143 return ctx->u.rinfo.zd_old_distance;
7cdb1a84
MS
1144}
1145
e1f3a8eb
MS
1146/*
1147 * Set the nexthops associated with a context: note that processing code
1148 * may well expect that nexthops are in canonical (sorted) order, so we
1149 * will enforce that here.
1150 */
593e4eb1
MS
1151void dplane_ctx_set_nexthops(struct zebra_dplane_ctx *ctx, struct nexthop *nh)
1152{
1153 DPLANE_CTX_VALID(ctx);
1154
1155 if (ctx->u.rinfo.zd_ng.nexthop) {
1156 nexthops_free(ctx->u.rinfo.zd_ng.nexthop);
1157 ctx->u.rinfo.zd_ng.nexthop = NULL;
1158 }
e1f3a8eb 1159 nexthop_group_copy_nh_sorted(&(ctx->u.rinfo.zd_ng), nh);
593e4eb1
MS
1160}
1161
928f94a9
MS
1162/*
1163 * Set the list of backup nexthops; their ordering is preserved (they're not
1164 * re-sorted.)
1165 */
1166void dplane_ctx_set_backup_nhg(struct zebra_dplane_ctx *ctx,
1167 const struct nexthop_group *nhg)
1168{
1169 struct nexthop *nh, *last_nh, *nexthop;
1170
1171 DPLANE_CTX_VALID(ctx);
1172
1173 if (ctx->u.rinfo.backup_ng.nexthop) {
1174 nexthops_free(ctx->u.rinfo.backup_ng.nexthop);
1175 ctx->u.rinfo.backup_ng.nexthop = NULL;
1176 }
1177
1178 last_nh = NULL;
1179
1180 /* Be careful to preserve the order of the backup list */
1181 for (nh = nhg->nexthop; nh; nh = nh->next) {
1182 nexthop = nexthop_dup(nh, NULL);
1183
1184 if (last_nh)
1185 NEXTHOP_APPEND(last_nh, nexthop);
1186 else
1187 ctx->u.rinfo.backup_ng.nexthop = nexthop;
1188
1189 last_nh = nexthop;
1190 }
1191}
1192
1d48702e
MS
1193uint32_t dplane_ctx_get_nhg_id(const struct zebra_dplane_ctx *ctx)
1194{
1195 DPLANE_CTX_VALID(ctx);
1196 return ctx->u.rinfo.zd_nhg_id;
1197}
1198
25779064
MS
1199const struct nexthop_group *dplane_ctx_get_ng(
1200 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1201{
1202 DPLANE_CTX_VALID(ctx);
1203
0f461727 1204 return &(ctx->u.rinfo.zd_ng);
7cdb1a84
MS
1205}
1206
1d48702e
MS
1207const struct nexthop_group *
1208dplane_ctx_get_backup_ng(const struct zebra_dplane_ctx *ctx)
1209{
1210 DPLANE_CTX_VALID(ctx);
1211
1212 return &(ctx->u.rinfo.backup_ng);
1213}
1214
1215const struct nexthop_group *
1216dplane_ctx_get_old_ng(const struct zebra_dplane_ctx *ctx)
01ce7cba
MS
1217{
1218 DPLANE_CTX_VALID(ctx);
1219
0f461727 1220 return &(ctx->u.rinfo.zd_old_ng);
01ce7cba
MS
1221}
1222
1d48702e
MS
1223const struct nexthop_group *
1224dplane_ctx_get_old_backup_ng(const struct zebra_dplane_ctx *ctx)
1225{
1226 DPLANE_CTX_VALID(ctx);
1227
1228 return &(ctx->u.rinfo.old_backup_ng);
1229}
1230
25779064
MS
1231const struct zebra_dplane_info *dplane_ctx_get_ns(
1232 const struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
1233{
1234 DPLANE_CTX_VALID(ctx);
1235
5709131c 1236 return &(ctx->zd_ns_info);
7cdb1a84
MS
1237}
1238
f820d025 1239/* Accessors for nexthop information */
0c8215cb
SW
1240uint32_t dplane_ctx_get_nhe_id(const struct zebra_dplane_ctx *ctx)
1241{
1242 DPLANE_CTX_VALID(ctx);
1243 return ctx->u.rinfo.nhe.id;
1244}
1245
df3cef24
DS
1246uint32_t dplane_ctx_get_old_nhe_id(const struct zebra_dplane_ctx *ctx)
1247{
1248 DPLANE_CTX_VALID(ctx);
1249 return ctx->u.rinfo.nhe.old_id;
1250}
1251
0c8215cb
SW
1252afi_t dplane_ctx_get_nhe_afi(const struct zebra_dplane_ctx *ctx)
1253{
1254 DPLANE_CTX_VALID(ctx);
1255 return ctx->u.rinfo.nhe.afi;
1256}
1257
1258vrf_id_t dplane_ctx_get_nhe_vrf_id(const struct zebra_dplane_ctx *ctx)
f820d025
SW
1259{
1260 DPLANE_CTX_VALID(ctx);
0c8215cb
SW
1261 return ctx->u.rinfo.nhe.vrf_id;
1262}
1263
38e40db1 1264int dplane_ctx_get_nhe_type(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1265{
1266 DPLANE_CTX_VALID(ctx);
38e40db1 1267 return ctx->u.rinfo.nhe.type;
0c8215cb
SW
1268}
1269
1270const struct nexthop_group *
1271dplane_ctx_get_nhe_ng(const struct zebra_dplane_ctx *ctx)
1272{
1273 DPLANE_CTX_VALID(ctx);
1274 return &(ctx->u.rinfo.nhe.ng);
1275}
1276
e22e8001
SW
1277const struct nh_grp *
1278dplane_ctx_get_nhe_nh_grp(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1279{
1280 DPLANE_CTX_VALID(ctx);
e22e8001 1281 return ctx->u.rinfo.nhe.nh_grp;
0c8215cb
SW
1282}
1283
e22e8001 1284uint8_t dplane_ctx_get_nhe_nh_grp_count(const struct zebra_dplane_ctx *ctx)
0c8215cb
SW
1285{
1286 DPLANE_CTX_VALID(ctx);
e22e8001 1287 return ctx->u.rinfo.nhe.nh_grp_count;
f820d025
SW
1288}
1289
0f461727
MS
1290/* Accessors for LSP information */
1291
1292mpls_label_t dplane_ctx_get_in_label(const struct zebra_dplane_ctx *ctx)
1293{
1294 DPLANE_CTX_VALID(ctx);
1295
1296 return ctx->u.lsp.ile.in_label;
1297}
1298
3ab54059
MS
1299void dplane_ctx_set_in_label(struct zebra_dplane_ctx *ctx, mpls_label_t label)
1300{
1301 DPLANE_CTX_VALID(ctx);
1302
1303 ctx->u.lsp.ile.in_label = label;
1304}
1305
0f461727
MS
1306uint8_t dplane_ctx_get_addr_family(const struct zebra_dplane_ctx *ctx)
1307{
1308 DPLANE_CTX_VALID(ctx);
1309
1310 return ctx->u.lsp.addr_family;
1311}
1312
3ab54059
MS
1313void dplane_ctx_set_addr_family(struct zebra_dplane_ctx *ctx,
1314 uint8_t family)
1315{
1316 DPLANE_CTX_VALID(ctx);
1317
1318 ctx->u.lsp.addr_family = family;
1319}
1320
0f461727
MS
1321uint32_t dplane_ctx_get_lsp_flags(const struct zebra_dplane_ctx *ctx)
1322{
1323 DPLANE_CTX_VALID(ctx);
1324
1325 return ctx->u.lsp.flags;
1326}
1327
3ab54059
MS
1328void dplane_ctx_set_lsp_flags(struct zebra_dplane_ctx *ctx,
1329 uint32_t flags)
1330{
1331 DPLANE_CTX_VALID(ctx);
1332
1333 ctx->u.lsp.flags = flags;
1334}
1335
ee70f629
MS
1336const struct nhlfe_list_head *dplane_ctx_get_nhlfe_list(
1337 const struct zebra_dplane_ctx *ctx)
0f461727
MS
1338{
1339 DPLANE_CTX_VALID(ctx);
ee70f629 1340 return &(ctx->u.lsp.nhlfe_list);
0f461727
MS
1341}
1342
cd4bb96f
MS
1343const struct nhlfe_list_head *dplane_ctx_get_backup_nhlfe_list(
1344 const struct zebra_dplane_ctx *ctx)
1345{
1346 DPLANE_CTX_VALID(ctx);
1347 return &(ctx->u.lsp.backup_nhlfe_list);
1348}
1349
3ab54059
MS
1350zebra_nhlfe_t *dplane_ctx_add_nhlfe(struct zebra_dplane_ctx *ctx,
1351 enum lsp_types_t lsp_type,
1352 enum nexthop_types_t nh_type,
963a9803 1353 const union g_addr *gate,
3ab54059 1354 ifindex_t ifindex,
5065db0a 1355 uint8_t num_labels,
ee70f629 1356 mpls_label_t *out_labels)
3ab54059
MS
1357{
1358 zebra_nhlfe_t *nhlfe;
1359
1360 DPLANE_CTX_VALID(ctx);
1361
1362 nhlfe = zebra_mpls_lsp_add_nhlfe(&(ctx->u.lsp),
1363 lsp_type, nh_type, gate,
5065db0a 1364 ifindex, num_labels, out_labels);
3ab54059
MS
1365
1366 return nhlfe;
1367}
1368
cd4bb96f
MS
1369zebra_nhlfe_t *dplane_ctx_add_backup_nhlfe(struct zebra_dplane_ctx *ctx,
1370 enum lsp_types_t lsp_type,
1371 enum nexthop_types_t nh_type,
963a9803 1372 const union g_addr *gate,
cd4bb96f
MS
1373 ifindex_t ifindex,
1374 uint8_t num_labels,
1375 mpls_label_t *out_labels)
1376{
1377 zebra_nhlfe_t *nhlfe;
1378
1379 DPLANE_CTX_VALID(ctx);
1380
1381 nhlfe = zebra_mpls_lsp_add_backup_nhlfe(&(ctx->u.lsp),
1382 lsp_type, nh_type, gate,
1383 ifindex, num_labels,
1384 out_labels);
1385
1386 return nhlfe;
1387}
1388
81793ac1
MS
1389const zebra_nhlfe_t *
1390dplane_ctx_get_best_nhlfe(const struct zebra_dplane_ctx *ctx)
0f461727
MS
1391{
1392 DPLANE_CTX_VALID(ctx);
1393
1394 return ctx->u.lsp.best_nhlfe;
1395}
1396
3ab54059
MS
1397const zebra_nhlfe_t *
1398dplane_ctx_set_best_nhlfe(struct zebra_dplane_ctx *ctx,
1399 zebra_nhlfe_t *nhlfe)
1400{
1401 DPLANE_CTX_VALID(ctx);
1402
1403 ctx->u.lsp.best_nhlfe = nhlfe;
1404 return ctx->u.lsp.best_nhlfe;
1405}
1406
0f461727
MS
1407uint32_t dplane_ctx_get_lsp_num_ecmp(const struct zebra_dplane_ctx *ctx)
1408{
1409 DPLANE_CTX_VALID(ctx);
1410
1411 return ctx->u.lsp.num_ecmp;
1412}
1413
d613b8e1
MS
1414mpls_label_t dplane_ctx_get_pw_local_label(const struct zebra_dplane_ctx *ctx)
1415{
1416 DPLANE_CTX_VALID(ctx);
1417
1418 return ctx->u.pw.local_label;
1419}
1420
1421mpls_label_t dplane_ctx_get_pw_remote_label(const struct zebra_dplane_ctx *ctx)
1422{
1423 DPLANE_CTX_VALID(ctx);
1424
1425 return ctx->u.pw.remote_label;
1426}
1427
1428int dplane_ctx_get_pw_type(const struct zebra_dplane_ctx *ctx)
1429{
1430 DPLANE_CTX_VALID(ctx);
1431
1432 return ctx->u.pw.type;
1433}
1434
1435int dplane_ctx_get_pw_af(const struct zebra_dplane_ctx *ctx)
1436{
1437 DPLANE_CTX_VALID(ctx);
1438
1439 return ctx->u.pw.af;
1440}
1441
1442uint32_t dplane_ctx_get_pw_flags(const struct zebra_dplane_ctx *ctx)
1443{
1444 DPLANE_CTX_VALID(ctx);
1445
1446 return ctx->u.pw.flags;
1447}
1448
1449int dplane_ctx_get_pw_status(const struct zebra_dplane_ctx *ctx)
1450{
1451 DPLANE_CTX_VALID(ctx);
1452
1453 return ctx->u.pw.status;
1454}
1455
fd563cc7
KS
1456void dplane_ctx_set_pw_status(struct zebra_dplane_ctx *ctx, int status)
1457{
1458 DPLANE_CTX_VALID(ctx);
1459
1460 ctx->u.pw.status = status;
1461}
1462
16d69787 1463const union g_addr *dplane_ctx_get_pw_dest(
d613b8e1
MS
1464 const struct zebra_dplane_ctx *ctx)
1465{
1466 DPLANE_CTX_VALID(ctx);
1467
16d69787 1468 return &(ctx->u.pw.dest);
d613b8e1
MS
1469}
1470
1471const union pw_protocol_fields *dplane_ctx_get_pw_proto(
1472 const struct zebra_dplane_ctx *ctx)
1473{
1474 DPLANE_CTX_VALID(ctx);
1475
1476 return &(ctx->u.pw.fields);
1477}
1478
09cd307c
MS
1479const struct nexthop_group *
1480dplane_ctx_get_pw_nhg(const struct zebra_dplane_ctx *ctx)
1481{
1482 DPLANE_CTX_VALID(ctx);
1483
1484 return &(ctx->u.pw.nhg);
1485}
1486
a4a4802a 1487/* Accessors for interface information */
a4a4802a
MS
1488uint32_t dplane_ctx_get_intf_metric(const struct zebra_dplane_ctx *ctx)
1489{
1490 DPLANE_CTX_VALID(ctx);
1491
1492 return ctx->u.intf.metric;
1493}
1494
1495/* Is interface addr p2p? */
1496bool dplane_ctx_intf_is_connected(const struct zebra_dplane_ctx *ctx)
1497{
1498 DPLANE_CTX_VALID(ctx);
1499
1500 return (ctx->u.intf.flags & DPLANE_INTF_CONNECTED);
1501}
1502
1503bool dplane_ctx_intf_is_secondary(const struct zebra_dplane_ctx *ctx)
1504{
1505 DPLANE_CTX_VALID(ctx);
1506
1507 return (ctx->u.intf.flags & DPLANE_INTF_SECONDARY);
1508}
1509
64168803
MS
1510bool dplane_ctx_intf_is_broadcast(const struct zebra_dplane_ctx *ctx)
1511{
1512 DPLANE_CTX_VALID(ctx);
1513
1514 return (ctx->u.intf.flags & DPLANE_INTF_BROADCAST);
1515}
1516
a4a4802a
MS
1517const struct prefix *dplane_ctx_get_intf_addr(
1518 const struct zebra_dplane_ctx *ctx)
1519{
1520 DPLANE_CTX_VALID(ctx);
1521
1522 return &(ctx->u.intf.prefix);
1523}
1524
1525bool dplane_ctx_intf_has_dest(const struct zebra_dplane_ctx *ctx)
1526{
1527 DPLANE_CTX_VALID(ctx);
1528
1529 return (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST);
1530}
1531
1532const struct prefix *dplane_ctx_get_intf_dest(
1533 const struct zebra_dplane_ctx *ctx)
1534{
1535 DPLANE_CTX_VALID(ctx);
1536
1537 if (ctx->u.intf.flags & DPLANE_INTF_HAS_DEST)
1538 return &(ctx->u.intf.dest_prefix);
1539 else
1540 return NULL;
1541}
1542
1543bool dplane_ctx_intf_has_label(const struct zebra_dplane_ctx *ctx)
1544{
1545 DPLANE_CTX_VALID(ctx);
1546
1547 return (ctx->u.intf.flags & DPLANE_INTF_HAS_LABEL);
1548}
1549
1550const char *dplane_ctx_get_intf_label(const struct zebra_dplane_ctx *ctx)
1551{
1552 DPLANE_CTX_VALID(ctx);
1553
1554 return ctx->u.intf.label;
1555}
1556
7597ac7b
MS
1557/* Accessors for MAC information */
1558vlanid_t dplane_ctx_mac_get_vlan(const struct zebra_dplane_ctx *ctx)
1559{
1560 DPLANE_CTX_VALID(ctx);
1561 return ctx->u.macinfo.vid;
1562}
1563
1564bool dplane_ctx_mac_is_sticky(const struct zebra_dplane_ctx *ctx)
1565{
1566 DPLANE_CTX_VALID(ctx);
1567 return ctx->u.macinfo.is_sticky;
1568}
1569
506efd37
AK
1570uint32_t dplane_ctx_mac_get_nhg_id(const struct zebra_dplane_ctx *ctx)
1571{
1572 DPLANE_CTX_VALID(ctx);
1573 return ctx->u.macinfo.nhg_id;
1574}
1575
f188e68e
AK
1576uint32_t dplane_ctx_mac_get_update_flags(const struct zebra_dplane_ctx *ctx)
1577{
1578 DPLANE_CTX_VALID(ctx);
1579 return ctx->u.macinfo.update_flags;
1580}
1581
7597ac7b
MS
1582const struct ethaddr *dplane_ctx_mac_get_addr(
1583 const struct zebra_dplane_ctx *ctx)
1584{
1585 DPLANE_CTX_VALID(ctx);
1586 return &(ctx->u.macinfo.mac);
1587}
1588
1589const struct in_addr *dplane_ctx_mac_get_vtep_ip(
1590 const struct zebra_dplane_ctx *ctx)
1591{
1592 DPLANE_CTX_VALID(ctx);
1593 return &(ctx->u.macinfo.vtep_ip);
1594}
1595
478566d6
MS
1596ifindex_t dplane_ctx_mac_get_br_ifindex(const struct zebra_dplane_ctx *ctx)
1597{
1598 DPLANE_CTX_VALID(ctx);
1599 return ctx->u.macinfo.br_ifindex;
1600}
1601
931fa60c
MS
1602/* Accessors for neighbor information */
1603const struct ipaddr *dplane_ctx_neigh_get_ipaddr(
1604 const struct zebra_dplane_ctx *ctx)
1605{
1606 DPLANE_CTX_VALID(ctx);
1607 return &(ctx->u.neigh.ip_addr);
1608}
1609
1610const struct ethaddr *dplane_ctx_neigh_get_mac(
1611 const struct zebra_dplane_ctx *ctx)
1612{
1613 DPLANE_CTX_VALID(ctx);
1614 return &(ctx->u.neigh.mac);
1615}
1616
1617uint32_t dplane_ctx_neigh_get_flags(const struct zebra_dplane_ctx *ctx)
1618{
1619 DPLANE_CTX_VALID(ctx);
1620 return ctx->u.neigh.flags;
1621}
1622
1623uint16_t dplane_ctx_neigh_get_state(const struct zebra_dplane_ctx *ctx)
1624{
1625 DPLANE_CTX_VALID(ctx);
1626 return ctx->u.neigh.state;
1627}
1628
f188e68e
AK
1629uint32_t dplane_ctx_neigh_get_update_flags(const struct zebra_dplane_ctx *ctx)
1630{
1631 DPLANE_CTX_VALID(ctx);
1632 return ctx->u.neigh.update_flags;
1633}
1634
60d8d43b
JU
1635/* Accessors for PBR rule information */
1636int dplane_ctx_rule_get_sock(const struct zebra_dplane_ctx *ctx)
1637{
1638 DPLANE_CTX_VALID(ctx);
1639
1640 return ctx->u.rule.sock;
1641}
1642
58a1d249
DS
1643const char *dplane_ctx_rule_get_ifname(const struct zebra_dplane_ctx *ctx)
1644{
1645 DPLANE_CTX_VALID(ctx);
1646
1647 return ctx->u.rule.new.ifname;
1648}
1649
60d8d43b
JU
1650int dplane_ctx_rule_get_unique(const struct zebra_dplane_ctx *ctx)
1651{
1652 DPLANE_CTX_VALID(ctx);
1653
1654 return ctx->u.rule.unique;
1655}
1656
1657int dplane_ctx_rule_get_seq(const struct zebra_dplane_ctx *ctx)
1658{
1659 DPLANE_CTX_VALID(ctx);
1660
1661 return ctx->u.rule.seq;
1662}
1663
1664uint32_t dplane_ctx_rule_get_priority(const struct zebra_dplane_ctx *ctx)
1665{
1666 DPLANE_CTX_VALID(ctx);
1667
1668 return ctx->u.rule.new.priority;
1669}
1670
1671uint32_t dplane_ctx_rule_get_old_priority(const struct zebra_dplane_ctx *ctx)
1672{
1673 DPLANE_CTX_VALID(ctx);
1674
1675 return ctx->u.rule.old.priority;
1676}
1677
1678uint32_t dplane_ctx_rule_get_table(const struct zebra_dplane_ctx *ctx)
1679{
1680 DPLANE_CTX_VALID(ctx);
1681
1682 return ctx->u.rule.new.table;
1683}
1684
1685uint32_t dplane_ctx_rule_get_old_table(const struct zebra_dplane_ctx *ctx)
1686{
1687 DPLANE_CTX_VALID(ctx);
1688
1689 return ctx->u.rule.old.table;
1690}
1691
1692uint32_t dplane_ctx_rule_get_filter_bm(const struct zebra_dplane_ctx *ctx)
1693{
1694 DPLANE_CTX_VALID(ctx);
1695
1696 return ctx->u.rule.new.filter_bm;
1697}
1698
1699uint32_t dplane_ctx_rule_get_old_filter_bm(const struct zebra_dplane_ctx *ctx)
1700{
1701 DPLANE_CTX_VALID(ctx);
1702
1703 return ctx->u.rule.old.filter_bm;
1704}
1705
1706uint32_t dplane_ctx_rule_get_fwmark(const struct zebra_dplane_ctx *ctx)
1707{
1708 DPLANE_CTX_VALID(ctx);
1709
1710 return ctx->u.rule.new.fwmark;
1711}
1712
1713uint32_t dplane_ctx_rule_get_old_fwmark(const struct zebra_dplane_ctx *ctx)
1714{
1715 DPLANE_CTX_VALID(ctx);
1716
1717 return ctx->u.rule.old.fwmark;
1718}
1719
01f23aff
WC
1720uint8_t dplane_ctx_rule_get_dsfield(const struct zebra_dplane_ctx *ctx)
1721{
1722 DPLANE_CTX_VALID(ctx);
1723
1724 return ctx->u.rule.new.dsfield;
1725}
1726
1727uint8_t dplane_ctx_rule_get_old_dsfield(const struct zebra_dplane_ctx *ctx)
1728{
1729 DPLANE_CTX_VALID(ctx);
1730
1731 return ctx->u.rule.old.dsfield;
1732}
1733
60d8d43b
JU
1734const struct prefix *
1735dplane_ctx_rule_get_src_ip(const struct zebra_dplane_ctx *ctx)
1736{
1737 DPLANE_CTX_VALID(ctx);
1738
1739 return &(ctx->u.rule.new.src_ip);
1740}
1741
1742const struct prefix *
1743dplane_ctx_rule_get_old_src_ip(const struct zebra_dplane_ctx *ctx)
1744{
1745 DPLANE_CTX_VALID(ctx);
1746
1747 return &(ctx->u.rule.old.src_ip);
1748}
1749
1750const struct prefix *
1751dplane_ctx_rule_get_dst_ip(const struct zebra_dplane_ctx *ctx)
1752{
1753 DPLANE_CTX_VALID(ctx);
1754
1755 return &(ctx->u.rule.new.dst_ip);
1756}
1757
1758const struct prefix *
1759dplane_ctx_rule_get_old_dst_ip(const struct zebra_dplane_ctx *ctx)
1760{
1761 DPLANE_CTX_VALID(ctx);
1762
1763 return &(ctx->u.rule.old.dst_ip);
1764}
1765
7cdb1a84
MS
1766/*
1767 * End of dplane context accessors
1768 */
1769
c831033f 1770
91f16812
MS
1771/*
1772 * Retrieve the limit on the number of pending, unprocessed updates.
1773 */
1774uint32_t dplane_get_in_queue_limit(void)
1775{
1776 return atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
1777 memory_order_relaxed);
1778}
1779
1780/*
1781 * Configure limit on the number of pending, queued updates.
1782 */
1783void dplane_set_in_queue_limit(uint32_t limit, bool set)
1784{
1785 /* Reset to default on 'unset' */
1786 if (!set)
1787 limit = DPLANE_DEFAULT_MAX_QUEUED;
1788
1789 atomic_store_explicit(&zdplane_info.dg_max_queued_updates, limit,
1790 memory_order_relaxed);
1791}
1792
1793/*
1794 * Retrieve the current queue depth of incoming, unprocessed updates
1795 */
1796uint32_t dplane_get_in_queue_len(void)
1797{
1798 return atomic_load_explicit(&zdplane_info.dg_routes_queued,
1799 memory_order_seq_cst);
1800}
1801
16c628de
MS
1802/*
1803 * Common dataplane context init with zebra namespace info.
1804 */
1805static int dplane_ctx_ns_init(struct zebra_dplane_ctx *ctx,
1806 struct zebra_ns *zns,
1807 bool is_update)
1808{
1809 dplane_info_from_zns(&(ctx->zd_ns_info), zns);
1810
1811#if defined(HAVE_NETLINK)
1812 /* Increment message counter after copying to context struct - may need
1813 * two messages in some 'update' cases.
1814 */
1815 if (is_update)
1816 zns->netlink_dplane.seq += 2;
1817 else
1818 zns->netlink_dplane.seq++;
1819#endif /* HAVE_NETLINK */
1820
1821 return AOK;
1822}
1823
7cdb1a84
MS
1824/*
1825 * Initialize a context block for a route update from zebra data structs.
1826 */
018e77bc
RZ
1827int dplane_ctx_route_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
1828 struct route_node *rn, struct route_entry *re)
7cdb1a84
MS
1829{
1830 int ret = EINVAL;
1831 const struct route_table *table = NULL;
630d5962 1832 const struct rib_table_info *info;
7cdb1a84
MS
1833 const struct prefix *p, *src_p;
1834 struct zebra_ns *zns;
1835 struct zebra_vrf *zvrf;
f183e380 1836 struct nexthop *nexthop;
f2a0ba3a 1837 zebra_l3vni_t *zl3vni;
7cdb1a84 1838
5709131c 1839 if (!ctx || !rn || !re)
7cdb1a84 1840 goto done;
7cdb1a84
MS
1841
1842 ctx->zd_op = op;
14b0bc8e 1843 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
7cdb1a84 1844
0f461727
MS
1845 ctx->u.rinfo.zd_type = re->type;
1846 ctx->u.rinfo.zd_old_type = re->type;
7cdb1a84
MS
1847
1848 /* Prefixes: dest, and optional source */
1849 srcdest_rnode_prefixes(rn, &p, &src_p);
1850
0f461727 1851 prefix_copy(&(ctx->u.rinfo.zd_dest), p);
7cdb1a84 1852
5709131c 1853 if (src_p)
0f461727 1854 prefix_copy(&(ctx->u.rinfo.zd_src), src_p);
5709131c 1855 else
0f461727 1856 memset(&(ctx->u.rinfo.zd_src), 0, sizeof(ctx->u.rinfo.zd_src));
7cdb1a84
MS
1857
1858 ctx->zd_table_id = re->table;
1859
0f461727
MS
1860 ctx->u.rinfo.zd_metric = re->metric;
1861 ctx->u.rinfo.zd_old_metric = re->metric;
7cdb1a84 1862 ctx->zd_vrf_id = re->vrf_id;
0f461727
MS
1863 ctx->u.rinfo.zd_mtu = re->mtu;
1864 ctx->u.rinfo.zd_nexthop_mtu = re->nexthop_mtu;
1865 ctx->u.rinfo.zd_instance = re->instance;
1866 ctx->u.rinfo.zd_tag = re->tag;
1867 ctx->u.rinfo.zd_old_tag = re->tag;
1868 ctx->u.rinfo.zd_distance = re->distance;
7cdb1a84
MS
1869
1870 table = srcdest_rnode_table(rn);
1871 info = table->info;
1872
0f461727
MS
1873 ctx->u.rinfo.zd_afi = info->afi;
1874 ctx->u.rinfo.zd_safi = info->safi;
7cdb1a84 1875
7cdb1a84 1876 /* Copy nexthops; recursive info is included too */
0eb97b86 1877 copy_nexthops(&(ctx->u.rinfo.zd_ng.nexthop),
c415d895 1878 re->nhe->nhg.nexthop, NULL);
1d48702e
MS
1879 ctx->u.rinfo.zd_nhg_id = re->nhe->id;
1880
1881 /* Copy backup nexthop info, if present */
1882 if (re->nhe->backup_info && re->nhe->backup_info->nhe) {
1883 copy_nexthops(&(ctx->u.rinfo.backup_ng.nexthop),
1884 re->nhe->backup_info->nhe->nhg.nexthop, NULL);
1885 }
7cdb1a84 1886
f2a0ba3a
RZ
1887 /*
1888 * Ensure that the dplane nexthops' flags are clear and copy
1889 * encapsulation information.
1890 */
1891 for (ALL_NEXTHOPS(ctx->u.rinfo.zd_ng, nexthop)) {
f183e380 1892 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
f183e380 1893
f2a0ba3a
RZ
1894 /* Check for available encapsulations. */
1895 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE))
1896 continue;
1897
1898 zl3vni = zl3vni_from_vrf(nexthop->vrf_id);
1899 if (zl3vni && is_l3vni_oper_up(zl3vni)) {
1900 nexthop->nh_encap_type = NET_VXLAN;
1901 nexthop->nh_encap.vni = zl3vni->vni;
1902 }
1903 }
1904
cf363e1b
MS
1905 /* Don't need some info when capturing a system notification */
1906 if (op == DPLANE_OP_SYS_ROUTE_ADD ||
1907 op == DPLANE_OP_SYS_ROUTE_DELETE) {
1908 ret = AOK;
1909 goto done;
1910 }
1911
1912 /* Extract ns info - can't use pointers to 'core' structs */
1913 zvrf = vrf_info_lookup(re->vrf_id);
1914 zns = zvrf->zns;
1915 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_ROUTE_UPDATE));
1916
6df59152 1917#ifdef HAVE_NETLINK
f924db49 1918 {
1d48702e 1919 struct nhg_hash_entry *nhe = zebra_nhg_resolve(re->nhe);
6df59152 1920
ae9bfa06 1921 ctx->u.rinfo.nhe.id = nhe->id;
df3cef24 1922 ctx->u.rinfo.nhe.old_id = 0;
6df59152 1923 /*
ae9bfa06
SW
1924 * Check if the nhe is installed/queued before doing anything
1925 * with this route.
08c51a38
SW
1926 *
1927 * If its a delete we only use the prefix anyway, so this only
1928 * matters for INSTALL/UPDATE.
6df59152 1929 */
1a39fdc2
DS
1930 if (zebra_nhg_kernel_nexthops_enabled()
1931 && (((op == DPLANE_OP_ROUTE_INSTALL)
1932 || (op == DPLANE_OP_ROUTE_UPDATE))
1933 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED)
1934 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_QUEUED))) {
6df59152
SW
1935 ret = ENOENT;
1936 goto done;
1937 }
98cda54a 1938 }
6df59152 1939#endif /* HAVE_NETLINK */
de3f5488 1940
7cdb1a84
MS
1941 /* Trying out the sequence number idea, so we can try to detect
1942 * when a result is stale.
1943 */
1485bbe7 1944 re->dplane_sequence = zebra_router_get_next_sequence();
7cdb1a84
MS
1945 ctx->zd_seq = re->dplane_sequence;
1946
1947 ret = AOK;
1948
1949done:
1950 return ret;
1951}
1952
f820d025
SW
1953/**
1954 * dplane_ctx_nexthop_init() - Initialize a context block for a nexthop update
1955 *
1956 * @ctx: Dataplane context to init
1957 * @op: Operation being performed
1958 * @nhe: Nexthop group hash entry
1959 *
1960 * Return: Result status
1961 */
a2072e71 1962int dplane_ctx_nexthop_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
981ca597 1963 struct nhg_hash_entry *nhe)
f820d025 1964{
a7df21c4 1965 struct zebra_vrf *zvrf = NULL;
8e401b25 1966 struct zebra_ns *zns = NULL;
f820d025
SW
1967 int ret = EINVAL;
1968
1969 if (!ctx || !nhe)
1970 goto done;
1971
1972 ctx->zd_op = op;
1973 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1974
1975 /* Copy over nhe info */
0c8215cb
SW
1976 ctx->u.rinfo.nhe.id = nhe->id;
1977 ctx->u.rinfo.nhe.afi = nhe->afi;
1978 ctx->u.rinfo.nhe.vrf_id = nhe->vrf_id;
38e40db1 1979 ctx->u.rinfo.nhe.type = nhe->type;
0c8215cb 1980
c415d895 1981 nexthop_group_copy(&(ctx->u.rinfo.nhe.ng), &(nhe->nhg));
0c8215cb 1982
c415d895 1983 /* If this is a group, convert it to a grp array of ids */
98cda54a
SW
1984 if (!zebra_nhg_depends_is_empty(nhe)
1985 && !CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_RECURSIVE))
8dbc800f
SW
1986 ctx->u.rinfo.nhe.nh_grp_count = zebra_nhg_nhe2grp(
1987 ctx->u.rinfo.nhe.nh_grp, nhe, MULTIPATH_NUM);
f820d025 1988
a7df21c4
SW
1989 zvrf = vrf_info_lookup(nhe->vrf_id);
1990
1991 /*
1992 * Fallback to default namespace if the vrf got ripped out from under
1993 * us.
1994 */
1995 zns = zvrf ? zvrf->zns : zebra_ns_lookup(NS_DEFAULT);
1909e63a 1996
2d3c57e6
SW
1997 /*
1998 * TODO: Might not need to mark this as an update, since
1999 * it probably won't require two messages
2000 */
1909e63a 2001 dplane_ctx_ns_init(ctx, zns, (op == DPLANE_OP_NH_UPDATE));
f6feb48b 2002 ctx->zd_is_update = (op == DPLANE_OP_NH_UPDATE);
1909e63a 2003
f820d025
SW
2004 ret = AOK;
2005
2006done:
2007 return ret;
2008}
2009
16c628de
MS
2010/*
2011 * Capture information for an LSP update in a dplane context.
2012 */
65f264cf
MS
2013int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
2014 zebra_lsp_t *lsp)
16c628de
MS
2015{
2016 int ret = AOK;
2017 zebra_nhlfe_t *nhlfe, *new_nhlfe;
2018
16c628de
MS
2019 ctx->zd_op = op;
2020 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2021
2022 /* Capture namespace info */
2023 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2024 (op == DPLANE_OP_LSP_UPDATE));
f6feb48b 2025 ctx->zd_is_update = (op == DPLANE_OP_LSP_UPDATE);
16c628de
MS
2026
2027 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
2028
ee70f629 2029 nhlfe_list_init(&(ctx->u.lsp.nhlfe_list));
cd4bb96f 2030 nhlfe_list_init(&(ctx->u.lsp.backup_nhlfe_list));
65f264cf
MS
2031
2032 /* This may be called to create/init a dplane context, not necessarily
2033 * to copy an lsp object.
2034 */
2035 if (lsp == NULL) {
2036 ret = AOK;
2037 goto done;
2038 }
2039
2040 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2041 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
2042 dplane_op2str(op), lsp->ile.in_label,
2043 lsp->num_ecmp);
2044
16c628de
MS
2045 ctx->u.lsp.ile = lsp->ile;
2046 ctx->u.lsp.addr_family = lsp->addr_family;
2047 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
2048 ctx->u.lsp.flags = lsp->flags;
2049
2050 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
ee70f629 2051 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
16c628de
MS
2052 /* Not sure if this is meaningful... */
2053 if (nhlfe->nexthop == NULL)
2054 continue;
2055
cd4bb96f
MS
2056 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp), nhlfe->type,
2057 nhlfe->nexthop);
16c628de
MS
2058 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2059 ret = ENOMEM;
2060 break;
2061 }
2062
3c0e1622 2063 /* Need to copy flags and backup info too */
16c628de
MS
2064 new_nhlfe->flags = nhlfe->flags;
2065 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2066
3c0e1622
MS
2067 if (CHECK_FLAG(new_nhlfe->nexthop->flags,
2068 NEXTHOP_FLAG_HAS_BACKUP)) {
2069 new_nhlfe->nexthop->backup_num =
2070 nhlfe->nexthop->backup_num;
2071 memcpy(new_nhlfe->nexthop->backup_idx,
2072 nhlfe->nexthop->backup_idx,
2073 new_nhlfe->nexthop->backup_num);
2074 }
2075
16c628de
MS
2076 if (nhlfe == lsp->best_nhlfe)
2077 ctx->u.lsp.best_nhlfe = new_nhlfe;
2078 }
2079
cd4bb96f
MS
2080 if (ret != AOK)
2081 goto done;
2082
2083 /* Capture backup nhlfes/nexthops */
2084 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
2085 /* Not sure if this is meaningful... */
2086 if (nhlfe->nexthop == NULL)
2087 continue;
2088
2089 new_nhlfe = zebra_mpls_lsp_add_backup_nh(&(ctx->u.lsp),
2090 nhlfe->type,
2091 nhlfe->nexthop);
2092 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2093 ret = ENOMEM;
2094 break;
2095 }
2096
2097 /* Need to copy flags too */
2098 new_nhlfe->flags = nhlfe->flags;
2099 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2100 }
2101
16c628de
MS
2102 /* On error the ctx will be cleaned-up, so we don't need to
2103 * deal with any allocated nhlfe or nexthop structs here.
2104 */
cd4bb96f 2105done:
16c628de
MS
2106
2107 return ret;
2108}
2109
97d8d05a
MS
2110/*
2111 * Capture information for an LSP update in a dplane context.
2112 */
2113static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
2114 enum dplane_op_e op,
2115 struct zebra_pw *pw)
2116{
09cd307c
MS
2117 struct prefix p;
2118 afi_t afi;
2119 struct route_table *table;
2120 struct route_node *rn;
2121 struct route_entry *re;
0024ea8e 2122 const struct nexthop_group *nhg;
09cd307c 2123
97d8d05a
MS
2124 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2125 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
2126 dplane_op2str(op), pw->ifname, pw->local_label,
2127 pw->remote_label);
2128
2129 ctx->zd_op = op;
2130 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2131
2132 /* Capture namespace info: no netlink support as of 12/18,
2133 * but just in case...
2134 */
2135 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2136
2137 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
2138
2139 /* This name appears to be c-string, so we use string copy. */
7c7ef4a8 2140 strlcpy(ctx->zd_ifname, pw->ifname, sizeof(ctx->zd_ifname));
16d69787 2141
9bd9717b 2142 ctx->zd_vrf_id = pw->vrf_id;
7c7ef4a8 2143 ctx->zd_ifindex = pw->ifindex;
97d8d05a
MS
2144 ctx->u.pw.type = pw->type;
2145 ctx->u.pw.af = pw->af;
2146 ctx->u.pw.local_label = pw->local_label;
2147 ctx->u.pw.remote_label = pw->remote_label;
2148 ctx->u.pw.flags = pw->flags;
2149
16d69787 2150 ctx->u.pw.dest = pw->nexthop;
97d8d05a
MS
2151
2152 ctx->u.pw.fields = pw->data;
2153
09cd307c
MS
2154 /* Capture nexthop info for the pw destination. We need to look
2155 * up and use zebra datastructs, but we're running in the zebra
2156 * pthread here so that should be ok.
2157 */
2158 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
2159 p.family = pw->af;
2160 p.prefixlen = ((pw->af == AF_INET) ?
2161 IPV4_MAX_PREFIXLEN : IPV6_MAX_PREFIXLEN);
2162
2163 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
2164 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
2165 if (table) {
2166 rn = route_node_match(table, &p);
2167 if (rn) {
2168 RNODE_FOREACH_RE(rn, re) {
2169 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
2170 break;
2171 }
2172
0024ea8e
MS
2173 if (re) {
2174 nhg = rib_get_fib_nhg(re);
3c0e1622
MS
2175 if (nhg && nhg->nexthop)
2176 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
2177 nhg->nexthop, NULL);
8b117ff0
MS
2178
2179 /* Include any installed backup nexthops */
2180 nhg = rib_get_fib_backup_nhg(re);
2181 if (nhg && nhg->nexthop)
2182 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
2183 nhg->nexthop, NULL);
0024ea8e 2184 }
09cd307c
MS
2185 route_unlock_node(rn);
2186 }
2187 }
2188
9f2d0354 2189 return AOK;
97d8d05a
MS
2190}
2191
f62e5480
JU
2192/**
2193 * dplane_ctx_rule_init_single() - Initialize a dataplane representation of a
2194 * PBR rule.
2195 *
2196 * @dplane_rule: Dataplane internal representation of a rule
2197 * @rule: PBR rule
2198 */
2199static void dplane_ctx_rule_init_single(struct dplane_ctx_rule *dplane_rule,
2200 struct zebra_pbr_rule *rule)
2201{
2202 dplane_rule->priority = rule->rule.priority;
2203 dplane_rule->table = rule->rule.action.table;
2204
2205 dplane_rule->filter_bm = rule->rule.filter.filter_bm;
2206 dplane_rule->fwmark = rule->rule.filter.fwmark;
01f23aff 2207 dplane_rule->dsfield = rule->rule.filter.dsfield;
f62e5480
JU
2208 prefix_copy(&(dplane_rule->dst_ip), &rule->rule.filter.dst_ip);
2209 prefix_copy(&(dplane_rule->src_ip), &rule->rule.filter.src_ip);
58a1d249 2210 strlcpy(dplane_rule->ifname, rule->ifname, INTERFACE_NAMSIZ);
f62e5480
JU
2211}
2212
2213/**
2214 * dplane_ctx_rule_init() - Initialize a context block for a PBR rule update.
2215 *
2216 * @ctx: Dataplane context to init
2217 * @op: Operation being performed
2218 * @new_rule: PBR rule
2219 *
2220 * Return: Result status
2221 */
2222static int dplane_ctx_rule_init(struct zebra_dplane_ctx *ctx,
2223 enum dplane_op_e op,
2224 struct zebra_pbr_rule *new_rule,
2225 struct zebra_pbr_rule *old_rule)
2226{
2dbe669b 2227 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
f62e5480 2228 zlog_debug(
2dbe669b 2229 "init dplane ctx %s: IF %s Prio %u Fwmark %u Src %pFX Dst %pFX Table %u",
f62e5480 2230 dplane_op2str(op), new_rule->ifname,
58a1d249 2231 new_rule->rule.priority, new_rule->rule.filter.fwmark,
2dbe669b
DA
2232 &new_rule->rule.filter.src_ip,
2233 &new_rule->rule.filter.dst_ip,
f62e5480 2234 new_rule->rule.action.table);
f62e5480
JU
2235
2236 ctx->zd_op = op;
2237 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2238
2239 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2240 op == DPLANE_OP_RULE_UPDATE);
f6feb48b 2241 ctx->zd_is_update = (op == DPLANE_OP_RULE_UPDATE);
f62e5480
JU
2242
2243 ctx->zd_vrf_id = new_rule->vrf_id;
2244 memcpy(ctx->zd_ifname, new_rule->ifname, sizeof(new_rule->ifname));
f62e5480
JU
2245
2246 ctx->u.rule.sock = new_rule->sock;
2247 ctx->u.rule.unique = new_rule->rule.unique;
2248 ctx->u.rule.seq = new_rule->rule.seq;
2249
2250 dplane_ctx_rule_init_single(&ctx->u.rule.new, new_rule);
2251 if (op == DPLANE_OP_RULE_UPDATE)
2252 dplane_ctx_rule_init_single(&ctx->u.rule.old, old_rule);
2253
2254 return AOK;
2255}
2256
7cdb1a84 2257/*
3fe4ccc4 2258 * Enqueue a new update,
16c628de 2259 * and ensure an event is active for the dataplane pthread.
7cdb1a84 2260 */
3fe4ccc4 2261static int dplane_update_enqueue(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
2262{
2263 int ret = EINVAL;
91f16812 2264 uint32_t high, curr;
7cdb1a84 2265
16c628de 2266 /* Enqueue for processing by the dataplane pthread */
7cdb1a84
MS
2267 DPLANE_LOCK();
2268 {
3fe4ccc4 2269 TAILQ_INSERT_TAIL(&zdplane_info.dg_update_ctx_q, ctx,
25779064 2270 zd_q_entries);
7cdb1a84
MS
2271 }
2272 DPLANE_UNLOCK();
2273
e3d9208a 2274 curr = atomic_fetch_add_explicit(
e07e9549 2275 &(zdplane_info.dg_routes_queued),
e07e9549 2276 1, memory_order_seq_cst);
91f16812 2277
e3d9208a
MS
2278 curr++; /* We got the pre-incremented value */
2279
91f16812
MS
2280 /* Maybe update high-water counter also */
2281 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
2282 memory_order_seq_cst);
2283 while (high < curr) {
2284 if (atomic_compare_exchange_weak_explicit(
2285 &zdplane_info.dg_routes_queued_max,
2286 &high, curr,
2287 memory_order_seq_cst,
2288 memory_order_seq_cst))
2289 break;
2290 }
2291
7cdb1a84 2292 /* Ensure that an event for the dataplane thread is active */
c831033f 2293 ret = dplane_provider_work_ready();
7cdb1a84 2294
5709131c 2295 return ret;
7cdb1a84
MS
2296}
2297
7cdb1a84
MS
2298/*
2299 * Utility that prepares a route update and enqueues it for processing
2300 */
655d681a
MS
2301static enum zebra_dplane_result
2302dplane_route_update_internal(struct route_node *rn,
2303 struct route_entry *re,
2304 struct route_entry *old_re,
5709131c 2305 enum dplane_op_e op)
7cdb1a84 2306{
655d681a 2307 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2308 int ret = EINVAL;
25779064 2309 struct zebra_dplane_ctx *ctx = NULL;
7cdb1a84
MS
2310
2311 /* Obtain context block */
2312 ctx = dplane_ctx_alloc();
7cdb1a84
MS
2313
2314 /* Init context with info from zebra data structs */
2315 ret = dplane_ctx_route_init(ctx, op, rn, re);
2316 if (ret == AOK) {
2317 /* Capture some extra info for update case
2318 * where there's a different 'old' route.
2319 */
b8e0423d
MS
2320 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
2321 old_re && (old_re != re)) {
7cdb1a84
MS
2322 ctx->zd_is_update = true;
2323
1485bbe7
DS
2324 old_re->dplane_sequence =
2325 zebra_router_get_next_sequence();
7cdb1a84
MS
2326 ctx->zd_old_seq = old_re->dplane_sequence;
2327
0f461727
MS
2328 ctx->u.rinfo.zd_old_tag = old_re->tag;
2329 ctx->u.rinfo.zd_old_type = old_re->type;
2330 ctx->u.rinfo.zd_old_instance = old_re->instance;
2331 ctx->u.rinfo.zd_old_distance = old_re->distance;
2332 ctx->u.rinfo.zd_old_metric = old_re->metric;
df3cef24 2333 ctx->u.rinfo.nhe.old_id = old_re->nhe->id;
01ce7cba
MS
2334
2335#ifndef HAVE_NETLINK
f183e380
MS
2336 /* For bsd, capture previous re's nexthops too, sigh.
2337 * We'll need these to do per-nexthop deletes.
2338 */
0f461727 2339 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
c415d895 2340 old_re->nhe->nhg.nexthop, NULL);
1d48702e
MS
2341
2342 if (zebra_nhg_get_backup_nhg(old_re->nhe) != NULL) {
2343 struct nexthop_group *nhg;
2344 struct nexthop **nh;
2345
2346 nhg = zebra_nhg_get_backup_nhg(old_re->nhe);
2347 nh = &(ctx->u.rinfo.old_backup_ng.nexthop);
2348
2349 if (nhg->nexthop)
2350 copy_nexthops(nh, nhg->nexthop, NULL);
2351 }
01ce7cba 2352#endif /* !HAVE_NETLINK */
7cdb1a84
MS
2353 }
2354
df3cef24
DS
2355 /*
2356 * If the old and new context type, and nexthop group id
2357 * are the same there is no need to send down a route replace
2358 * as that we know we have sent a nexthop group replace
2359 * or an upper level protocol has sent us the exact
2360 * same route again.
2361 */
2362 if ((dplane_ctx_get_type(ctx) == dplane_ctx_get_old_type(ctx))
2363 && (dplane_ctx_get_nhe_id(ctx)
3d3a9dc8
SW
2364 == dplane_ctx_get_old_nhe_id(ctx))
2365 && (dplane_ctx_get_nhe_id(ctx) >= ZEBRA_NHG_PROTO_LOWER)) {
27805e74
DS
2366 struct nexthop *nexthop;
2367
df3cef24
DS
2368 if (IS_ZEBRA_DEBUG_DPLANE)
2369 zlog_debug(
2370 "%s: Ignoring Route exactly the same",
2371 __func__);
2372
27805e74
DS
2373 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
2374 nexthop)) {
2375 if (CHECK_FLAG(nexthop->flags,
2376 NEXTHOP_FLAG_RECURSIVE))
2377 continue;
2378
2379 if (CHECK_FLAG(nexthop->flags,
2380 NEXTHOP_FLAG_ACTIVE))
2381 SET_FLAG(nexthop->flags,
2382 NEXTHOP_FLAG_FIB);
2383 }
2384
841f77ff 2385 dplane_ctx_free(&ctx);
df3cef24
DS
2386 return ZEBRA_DPLANE_REQUEST_SUCCESS;
2387 }
2388
7cdb1a84 2389 /* Enqueue context for processing */
3fe4ccc4 2390 ret = dplane_update_enqueue(ctx);
7cdb1a84
MS
2391 }
2392
91f16812 2393 /* Update counter */
25779064 2394 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
1d11b21f
MS
2395 memory_order_relaxed);
2396
91f16812 2397 if (ret == AOK)
655d681a 2398 result = ZEBRA_DPLANE_REQUEST_QUEUED;
16c628de 2399 else {
b96f64f7
DS
2400 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors, 1,
2401 memory_order_relaxed);
16c628de
MS
2402 if (ctx)
2403 dplane_ctx_free(&ctx);
1d11b21f 2404 }
7cdb1a84 2405
5709131c 2406 return result;
7cdb1a84
MS
2407}
2408
f820d025
SW
2409/**
2410 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
2411 *
2412 * @nhe: Nexthop group hash entry where the change occured
2413 * @op: The operation to be enqued
2414 *
2415 * Return: Result of the change
2416 */
2417static enum zebra_dplane_result
2418dplane_nexthop_update_internal(struct nhg_hash_entry *nhe, enum dplane_op_e op)
2419{
2420 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2421 int ret = EINVAL;
2422 struct zebra_dplane_ctx *ctx = NULL;
2423
2424 /* Obtain context block */
2425 ctx = dplane_ctx_alloc();
2426 if (!ctx) {
2427 ret = ENOMEM;
2428 goto done;
2429 }
2430
2431 ret = dplane_ctx_nexthop_init(ctx, op, nhe);
2d3c57e6 2432 if (ret == AOK)
f820d025 2433 ret = dplane_update_enqueue(ctx);
2d3c57e6 2434
f820d025
SW
2435done:
2436 /* Update counter */
2437 atomic_fetch_add_explicit(&zdplane_info.dg_nexthops_in, 1,
2438 memory_order_relaxed);
2439
2440 if (ret == AOK)
2441 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2442 else {
81505946
SW
2443 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
2444 memory_order_relaxed);
f820d025
SW
2445 if (ctx)
2446 dplane_ctx_free(&ctx);
2447 }
2448
2449 return result;
2450}
2451
7cdb1a84
MS
2452/*
2453 * Enqueue a route 'add' for the dataplane.
2454 */
655d681a
MS
2455enum zebra_dplane_result dplane_route_add(struct route_node *rn,
2456 struct route_entry *re)
7cdb1a84 2457{
655d681a 2458 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2459
5709131c 2460 if (rn == NULL || re == NULL)
7cdb1a84 2461 goto done;
7cdb1a84
MS
2462
2463 ret = dplane_route_update_internal(rn, re, NULL,
2464 DPLANE_OP_ROUTE_INSTALL);
2465
2466done:
5709131c 2467 return ret;
7cdb1a84
MS
2468}
2469
2470/*
2471 * Enqueue a route update for the dataplane.
2472 */
655d681a
MS
2473enum zebra_dplane_result dplane_route_update(struct route_node *rn,
2474 struct route_entry *re,
2475 struct route_entry *old_re)
7cdb1a84 2476{
655d681a 2477 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2478
5709131c 2479 if (rn == NULL || re == NULL)
7cdb1a84 2480 goto done;
7cdb1a84
MS
2481
2482 ret = dplane_route_update_internal(rn, re, old_re,
2483 DPLANE_OP_ROUTE_UPDATE);
7cdb1a84 2484done:
5709131c 2485 return ret;
7cdb1a84
MS
2486}
2487
2488/*
2489 * Enqueue a route removal for the dataplane.
2490 */
655d681a
MS
2491enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
2492 struct route_entry *re)
7cdb1a84 2493{
655d681a 2494 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2495
5709131c 2496 if (rn == NULL || re == NULL)
7cdb1a84 2497 goto done;
7cdb1a84
MS
2498
2499 ret = dplane_route_update_internal(rn, re, NULL,
2500 DPLANE_OP_ROUTE_DELETE);
2501
2502done:
5709131c 2503 return ret;
7cdb1a84
MS
2504}
2505
cf363e1b
MS
2506/*
2507 * Notify the dplane when system/connected routes change.
2508 */
2509enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
2510 struct route_entry *re)
2511{
2512 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2513
2514 /* Ignore this event unless a provider plugin has requested it. */
2515 if (!zdplane_info.dg_sys_route_notifs) {
2516 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2517 goto done;
2518 }
2519
2520 if (rn == NULL || re == NULL)
2521 goto done;
2522
2523 ret = dplane_route_update_internal(rn, re, NULL,
2524 DPLANE_OP_SYS_ROUTE_ADD);
2525
2526done:
2527 return ret;
2528}
2529
2530/*
2531 * Notify the dplane when system/connected routes are deleted.
2532 */
2533enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
2534 struct route_entry *re)
2535{
2536 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2537
2538 /* Ignore this event unless a provider plugin has requested it. */
2539 if (!zdplane_info.dg_sys_route_notifs) {
2540 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2541 goto done;
2542 }
2543
2544 if (rn == NULL || re == NULL)
2545 goto done;
2546
2547 ret = dplane_route_update_internal(rn, re, NULL,
2548 DPLANE_OP_SYS_ROUTE_DELETE);
2549
2550done:
2551 return ret;
2552}
2553
188a00e0
MS
2554/*
2555 * Update from an async notification, to bring other fibs up-to-date.
2556 */
2557enum zebra_dplane_result
2558dplane_route_notif_update(struct route_node *rn,
2559 struct route_entry *re,
2560 enum dplane_op_e op,
2561 struct zebra_dplane_ctx *ctx)
2562{
54d321aa
MS
2563 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2564 int ret = EINVAL;
188a00e0
MS
2565 struct zebra_dplane_ctx *new_ctx = NULL;
2566 struct nexthop *nexthop;
3c0e1622 2567 struct nexthop_group *nhg;
188a00e0
MS
2568
2569 if (rn == NULL || re == NULL)
2570 goto done;
2571
2572 new_ctx = dplane_ctx_alloc();
2573 if (new_ctx == NULL)
2574 goto done;
2575
2576 /* Init context with info from zebra data structs */
2577 dplane_ctx_route_init(new_ctx, op, rn, re);
2578
2579 /* For add/update, need to adjust the nexthops so that we match
2580 * the notification state, which may not be the route-entry/RIB
2581 * state.
2582 */
2583 if (op == DPLANE_OP_ROUTE_UPDATE ||
2584 op == DPLANE_OP_ROUTE_INSTALL) {
2585
2586 nexthops_free(new_ctx->u.rinfo.zd_ng.nexthop);
2587 new_ctx->u.rinfo.zd_ng.nexthop = NULL;
2588
3c0e1622
MS
2589 nhg = rib_get_fib_nhg(re);
2590 if (nhg && nhg->nexthop)
2591 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
2592 nhg->nexthop, NULL);
188a00e0 2593
3c0e1622
MS
2594 /* Check for installed backup nexthops also */
2595 nhg = rib_get_fib_backup_nhg(re);
2596 if (nhg && nhg->nexthop) {
00a9b150 2597 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
3c0e1622 2598 nhg->nexthop, NULL);
00a9b150 2599 }
188a00e0
MS
2600
2601 for (ALL_NEXTHOPS(new_ctx->u.rinfo.zd_ng, nexthop))
2602 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2603
2604 }
2605
2606 /* Capture info about the source of the notification, in 'ctx' */
2607 dplane_ctx_set_notif_provider(new_ctx,
2608 dplane_ctx_get_notif_provider(ctx));
2609
54d321aa 2610 ret = dplane_update_enqueue(new_ctx);
188a00e0
MS
2611
2612done:
54d321aa
MS
2613 if (ret == AOK)
2614 result = ZEBRA_DPLANE_REQUEST_QUEUED;
dd3b6cb5
MS
2615 else if (new_ctx)
2616 dplane_ctx_free(&new_ctx);
54d321aa
MS
2617
2618 return result;
188a00e0
MS
2619}
2620
f820d025
SW
2621/*
2622 * Enqueue a nexthop add for the dataplane.
2623 */
2624enum zebra_dplane_result dplane_nexthop_add(struct nhg_hash_entry *nhe)
2625{
2626 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2627
2628 if (nhe)
2629 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_INSTALL);
2630 return ret;
2631}
2632
2633/*
2634 * Enqueue a nexthop update for the dataplane.
81505946
SW
2635 *
2636 * Might not need this func since zebra's nexthop objects should be immutable?
f820d025
SW
2637 */
2638enum zebra_dplane_result dplane_nexthop_update(struct nhg_hash_entry *nhe)
2639{
2640 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2641
2642 if (nhe)
2643 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_UPDATE);
2644 return ret;
2645}
2646
2647/*
2648 * Enqueue a nexthop removal for the dataplane.
2649 */
2650enum zebra_dplane_result dplane_nexthop_delete(struct nhg_hash_entry *nhe)
2651{
2652 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2653
2654 if (nhe)
2655 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_DELETE);
2656
2657 return ret;
2658}
2659
16c628de
MS
2660/*
2661 * Enqueue LSP add for the dataplane.
2662 */
2663enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp)
2664{
2665 enum zebra_dplane_result ret =
2666 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
2667
2668 return ret;
2669}
2670
2671/*
2672 * Enqueue LSP update for the dataplane.
2673 */
2674enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp)
2675{
2676 enum zebra_dplane_result ret =
2677 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
2678
2679 return ret;
2680}
2681
2682/*
2683 * Enqueue LSP delete for the dataplane.
2684 */
2685enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp)
2686{
2687 enum zebra_dplane_result ret =
2688 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
2689
2690 return ret;
2691}
2692
188a00e0
MS
2693/* Update or un-install resulting from an async notification */
2694enum zebra_dplane_result
2695dplane_lsp_notif_update(zebra_lsp_t *lsp,
2696 enum dplane_op_e op,
2697 struct zebra_dplane_ctx *notif_ctx)
2698{
2699 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2700 int ret = EINVAL;
2701 struct zebra_dplane_ctx *ctx = NULL;
00a9b150
MS
2702 struct nhlfe_list_head *head;
2703 zebra_nhlfe_t *nhlfe, *new_nhlfe;
188a00e0
MS
2704
2705 /* Obtain context block */
2706 ctx = dplane_ctx_alloc();
2707 if (ctx == NULL) {
2708 ret = ENOMEM;
2709 goto done;
2710 }
2711
00a9b150 2712 /* Copy info from zebra LSP */
188a00e0
MS
2713 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2714 if (ret != AOK)
2715 goto done;
2716
00a9b150
MS
2717 /* Add any installed backup nhlfes */
2718 head = &(ctx->u.lsp.backup_nhlfe_list);
2719 frr_each(nhlfe_list, head, nhlfe) {
2720
2721 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) &&
2722 CHECK_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_FIB)) {
2723 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp),
2724 nhlfe->type,
2725 nhlfe->nexthop);
2726
2727 /* Need to copy flags too */
2728 new_nhlfe->flags = nhlfe->flags;
2729 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2730 }
2731 }
2732
188a00e0
MS
2733 /* Capture info about the source of the notification */
2734 dplane_ctx_set_notif_provider(
2735 ctx,
2736 dplane_ctx_get_notif_provider(notif_ctx));
2737
3fe4ccc4 2738 ret = dplane_update_enqueue(ctx);
188a00e0
MS
2739
2740done:
2741 /* Update counter */
2742 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2743 memory_order_relaxed);
2744
2745 if (ret == AOK)
2746 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2747 else {
2748 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2749 memory_order_relaxed);
2750 if (ctx)
2751 dplane_ctx_free(&ctx);
2752 }
2753 return result;
2754}
2755
97d8d05a
MS
2756/*
2757 * Enqueue pseudowire install for the dataplane.
2758 */
2759enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
2760{
2761 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
2762}
2763
2764/*
2765 * Enqueue pseudowire un-install for the dataplane.
2766 */
2767enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
2768{
2769 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
2770}
2771
16c628de
MS
2772/*
2773 * Common internal LSP update utility
2774 */
2775static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
2776 enum dplane_op_e op)
2777{
2778 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2779 int ret = EINVAL;
2780 struct zebra_dplane_ctx *ctx = NULL;
2781
2782 /* Obtain context block */
2783 ctx = dplane_ctx_alloc();
16c628de
MS
2784
2785 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2786 if (ret != AOK)
2787 goto done;
2788
3fe4ccc4 2789 ret = dplane_update_enqueue(ctx);
16c628de
MS
2790
2791done:
2792 /* Update counter */
2793 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2794 memory_order_relaxed);
2795
2796 if (ret == AOK)
2797 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2798 else {
2799 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2800 memory_order_relaxed);
d8e479a3 2801 dplane_ctx_free(&ctx);
16c628de
MS
2802 }
2803
2804 return result;
2805}
2806
97d8d05a
MS
2807/*
2808 * Internal, common handler for pseudowire updates.
2809 */
2810static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
2811 enum dplane_op_e op)
2812{
2813 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2814 int ret;
2815 struct zebra_dplane_ctx *ctx = NULL;
2816
2817 ctx = dplane_ctx_alloc();
97d8d05a
MS
2818
2819 ret = dplane_ctx_pw_init(ctx, op, pw);
2820 if (ret != AOK)
2821 goto done;
2822
3fe4ccc4 2823 ret = dplane_update_enqueue(ctx);
97d8d05a
MS
2824
2825done:
2826 /* Update counter */
2827 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
2828 memory_order_relaxed);
2829
2830 if (ret == AOK)
2831 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2832 else {
2833 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
2834 memory_order_relaxed);
d8e479a3 2835 dplane_ctx_free(&ctx);
97d8d05a
MS
2836 }
2837
2838 return result;
2839}
2840
a4a4802a
MS
2841/*
2842 * Enqueue interface address add for the dataplane.
2843 */
2844enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
2845 const struct connected *ifc)
2846{
64168803
MS
2847#if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2848 /* Extra checks for this OS path. */
2849
2850 /* Don't configure PtP addresses on broadcast ifs or reverse */
2851 if (!(ifp->flags & IFF_POINTOPOINT) != !CONNECTED_PEER(ifc)) {
2852 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_DPLANE)
2853 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2854
2855 return ZEBRA_DPLANE_REQUEST_FAILURE;
2856 }
2857
2858 /* Ensure that no existing installed v4 route conflicts with
2859 * the new interface prefix. This check must be done in the
2860 * zebra pthread context, and any route delete (if needed)
2861 * is enqueued before the interface address programming attempt.
2862 */
2863 if (ifc->address->family == AF_INET) {
2864 struct prefix_ipv4 *p;
2865
2866 p = (struct prefix_ipv4 *)ifc->address;
a36898e7 2867 rib_lookup_and_pushup(p, ifp->vrf_id);
64168803
MS
2868 }
2869#endif
2870
2871 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_INSTALL);
a4a4802a
MS
2872}
2873
2874/*
2875 * Enqueue interface address remove/uninstall for the dataplane.
2876 */
2877enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
2878 const struct connected *ifc)
2879{
64168803
MS
2880 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_UNINSTALL);
2881}
2882
2883static enum zebra_dplane_result intf_addr_update_internal(
2884 const struct interface *ifp, const struct connected *ifc,
2885 enum dplane_op_e op)
2886{
2887 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2888 int ret = EINVAL;
2889 struct zebra_dplane_ctx *ctx = NULL;
2890 struct zebra_ns *zns;
2891
2dbe669b
DA
2892 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2893 zlog_debug("init intf ctx %s: idx %d, addr %u:%pFX",
a36898e7 2894 dplane_op2str(op), ifp->ifindex, ifp->vrf_id,
2dbe669b 2895 ifc->address);
64168803
MS
2896
2897 ctx = dplane_ctx_alloc();
64168803
MS
2898
2899 ctx->zd_op = op;
2900 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
a36898e7 2901 ctx->zd_vrf_id = ifp->vrf_id;
64168803 2902
a36898e7 2903 zns = zebra_ns_lookup(ifp->vrf_id);
64168803
MS
2904 dplane_ctx_ns_init(ctx, zns, false);
2905
2906 /* Init the interface-addr-specific area */
2907 memset(&ctx->u.intf, 0, sizeof(ctx->u.intf));
2908
7c7ef4a8
MS
2909 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2910 ctx->zd_ifindex = ifp->ifindex;
64168803
MS
2911 ctx->u.intf.prefix = *(ifc->address);
2912
2913 if (if_is_broadcast(ifp))
2914 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
2915
2916 if (CONNECTED_PEER(ifc)) {
2917 ctx->u.intf.dest_prefix = *(ifc->destination);
2918 ctx->u.intf.flags |=
2919 (DPLANE_INTF_CONNECTED | DPLANE_INTF_HAS_DEST);
64168803
MS
2920 }
2921
2922 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
2923 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
2924
2925 if (ifc->label) {
2926 size_t len;
2927
2928 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
2929
2930 /* Use embedded buffer if it's adequate; else allocate. */
2931 len = strlen(ifc->label);
2932
2933 if (len < sizeof(ctx->u.intf.label_buf)) {
b7b7bf31 2934 strlcpy(ctx->u.intf.label_buf, ifc->label,
64168803
MS
2935 sizeof(ctx->u.intf.label_buf));
2936 ctx->u.intf.label = ctx->u.intf.label_buf;
2937 } else {
2938 ctx->u.intf.label = strdup(ifc->label);
2939 }
2940 }
2941
3fe4ccc4 2942 ret = dplane_update_enqueue(ctx);
64168803 2943
64168803
MS
2944 /* Increment counter */
2945 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addrs_in, 1,
2946 memory_order_relaxed);
2947
2948 if (ret == AOK)
2949 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2950 else {
2951 /* Error counter */
2952 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
2953 1, memory_order_relaxed);
d8e479a3 2954 dplane_ctx_free(&ctx);
64168803
MS
2955 }
2956
2957 return result;
a4a4802a
MS
2958}
2959
7597ac7b
MS
2960/*
2961 * Enqueue vxlan/evpn mac add (or update).
2962 */
f188e68e 2963enum zebra_dplane_result dplane_rem_mac_add(const struct interface *ifp,
478566d6 2964 const struct interface *bridge_ifp,
7597ac7b
MS
2965 vlanid_t vid,
2966 const struct ethaddr *mac,
2967 struct in_addr vtep_ip,
506efd37 2968 bool sticky,
f188e68e
AK
2969 uint32_t nhg_id,
2970 bool was_static)
7597ac7b
MS
2971{
2972 enum zebra_dplane_result result;
f188e68e
AK
2973 uint32_t update_flags = 0;
2974
2975 update_flags |= DPLANE_MAC_REMOTE;
2976 if (was_static)
2977 update_flags |= DPLANE_MAC_WAS_STATIC;
7597ac7b
MS
2978
2979 /* Use common helper api */
f73a8467 2980 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
f188e68e 2981 vid, mac, vtep_ip, sticky, nhg_id, update_flags);
7597ac7b
MS
2982 return result;
2983}
2984
2985/*
2986 * Enqueue vxlan/evpn mac delete.
2987 */
f188e68e 2988enum zebra_dplane_result dplane_rem_mac_del(const struct interface *ifp,
478566d6 2989 const struct interface *bridge_ifp,
7597ac7b
MS
2990 vlanid_t vid,
2991 const struct ethaddr *mac,
2992 struct in_addr vtep_ip)
2993{
2994 enum zebra_dplane_result result;
f188e68e
AK
2995 uint32_t update_flags = 0;
2996
2997 update_flags |= DPLANE_MAC_REMOTE;
7597ac7b
MS
2998
2999 /* Use common helper api */
f73a8467 3000 result = mac_update_common(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp,
f188e68e
AK
3001 vid, mac, vtep_ip, false, 0, update_flags);
3002 return result;
3003}
3004
3005/*
3006 * Enqueue local mac add (or update).
3007 */
3008enum zebra_dplane_result dplane_local_mac_add(const struct interface *ifp,
3009 const struct interface *bridge_ifp,
3010 vlanid_t vid,
3011 const struct ethaddr *mac,
3012 bool sticky,
3013 uint32_t set_static,
3014 uint32_t set_inactive)
3015{
3016 enum zebra_dplane_result result;
3017 uint32_t update_flags = 0;
3018 struct in_addr vtep_ip;
3019
3020 if (set_static)
3021 update_flags |= DPLANE_MAC_SET_STATIC;
3022
3023 if (set_inactive)
3024 update_flags |= DPLANE_MAC_SET_INACTIVE;
3025
3026 vtep_ip.s_addr = 0;
3027
3028 /* Use common helper api */
3029 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
3030 vid, mac, vtep_ip, sticky, 0,
3031 update_flags);
7597ac7b
MS
3032 return result;
3033}
3034
3035/*
f73a8467
MS
3036 * Public api to init an empty context - either newly-allocated or
3037 * reset/cleared - for a MAC update.
7597ac7b 3038 */
f73a8467
MS
3039void dplane_mac_init(struct zebra_dplane_ctx *ctx,
3040 const struct interface *ifp,
3041 const struct interface *br_ifp,
3042 vlanid_t vid,
3043 const struct ethaddr *mac,
3044 struct in_addr vtep_ip,
506efd37 3045 bool sticky,
f188e68e
AK
3046 uint32_t nhg_id,
3047 uint32_t update_flags)
7597ac7b 3048{
7597ac7b
MS
3049 struct zebra_ns *zns;
3050
7597ac7b
MS
3051 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3052 ctx->zd_vrf_id = ifp->vrf_id;
3053
3054 zns = zebra_ns_lookup(ifp->vrf_id);
3055 dplane_ctx_ns_init(ctx, zns, false);
3056
3057 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3058 ctx->zd_ifindex = ifp->ifindex;
3059
3060 /* Init the mac-specific data area */
3061 memset(&ctx->u.macinfo, 0, sizeof(ctx->u.macinfo));
3062
478566d6 3063 ctx->u.macinfo.br_ifindex = br_ifp->ifindex;
7597ac7b
MS
3064 ctx->u.macinfo.vtep_ip = vtep_ip;
3065 ctx->u.macinfo.mac = *mac;
3066 ctx->u.macinfo.vid = vid;
3067 ctx->u.macinfo.is_sticky = sticky;
506efd37 3068 ctx->u.macinfo.nhg_id = nhg_id;
f188e68e 3069 ctx->u.macinfo.update_flags = update_flags;
f73a8467
MS
3070}
3071
3072/*
3073 * Common helper api for MAC address/vxlan updates
3074 */
3075static enum zebra_dplane_result
3076mac_update_common(enum dplane_op_e op,
3077 const struct interface *ifp,
3078 const struct interface *br_ifp,
3079 vlanid_t vid,
3080 const struct ethaddr *mac,
3081 struct in_addr vtep_ip,
506efd37 3082 bool sticky,
f188e68e
AK
3083 uint32_t nhg_id,
3084 uint32_t update_flags)
f73a8467
MS
3085{
3086 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3087 int ret;
3088 struct zebra_dplane_ctx *ctx = NULL;
3089
3090 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3091 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
3092
3093 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
3094 dplane_op2str(op),
3095 prefix_mac2str(mac, buf1, sizeof(buf1)),
3096 ifp->name,
3097 inet_ntop(AF_INET, &vtep_ip, buf2, sizeof(buf2)));
3098 }
3099
3100 ctx = dplane_ctx_alloc();
3101 ctx->zd_op = op;
3102
3103 /* Common init for the ctx */
f188e68e
AK
3104 dplane_mac_init(ctx, ifp, br_ifp, vid, mac, vtep_ip, sticky,
3105 nhg_id, update_flags);
7597ac7b
MS
3106
3107 /* Enqueue for processing on the dplane pthread */
3108 ret = dplane_update_enqueue(ctx);
3109
3110 /* Increment counter */
3111 atomic_fetch_add_explicit(&zdplane_info.dg_macs_in, 1,
3112 memory_order_relaxed);
3113
3114 if (ret == AOK)
3115 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3116 else {
3117 /* Error counter */
3118 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors, 1,
3119 memory_order_relaxed);
3120 dplane_ctx_free(&ctx);
3121 }
3122
3123 return result;
3124}
3125
931fa60c
MS
3126/*
3127 * Enqueue evpn neighbor add for the dataplane.
3128 */
f188e68e 3129enum zebra_dplane_result dplane_rem_neigh_add(const struct interface *ifp,
931fa60c
MS
3130 const struct ipaddr *ip,
3131 const struct ethaddr *mac,
f188e68e 3132 uint32_t flags, bool was_static)
931fa60c
MS
3133{
3134 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
f188e68e
AK
3135 uint32_t update_flags = 0;
3136
3137 update_flags |= DPLANE_NEIGH_REMOTE;
3138
3139 if (was_static)
3140 update_flags |= DPLANE_NEIGH_WAS_STATIC;
931fa60c
MS
3141
3142 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
f188e68e
AK
3143 ifp, mac, ip, flags, DPLANE_NUD_NOARP,
3144 update_flags);
3145
3146 return result;
3147}
3148
3149/*
3150 * Enqueue local neighbor add for the dataplane.
3151 */
3152enum zebra_dplane_result dplane_local_neigh_add(const struct interface *ifp,
931fa60c
MS
3153 const struct ipaddr *ip,
3154 const struct ethaddr *mac,
f188e68e
AK
3155 bool set_router, bool set_static,
3156 bool set_inactive)
931fa60c
MS
3157{
3158 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
f188e68e
AK
3159 uint32_t update_flags = 0;
3160 uint32_t ntf = 0;
3161 uint16_t state;
3162
3163 if (set_static)
3164 update_flags |= DPLANE_NEIGH_SET_STATIC;
3165
3166 if (set_inactive) {
3167 update_flags |= DPLANE_NEIGH_SET_INACTIVE;
3168 state = DPLANE_NUD_STALE;
3169 } else {
3170 state = DPLANE_NUD_REACHABLE;
3171 }
3172
3173 if (set_router)
3174 ntf |= DPLANE_NTF_ROUTER;
931fa60c
MS
3175
3176 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
f188e68e
AK
3177 ifp, mac, ip, ntf,
3178 state, update_flags);
931fa60c
MS
3179
3180 return result;
3181}
3182
931fa60c
MS
3183/*
3184 * Enqueue evpn neighbor delete for the dataplane.
3185 */
f188e68e 3186enum zebra_dplane_result dplane_rem_neigh_delete(const struct interface *ifp,
931fa60c
MS
3187 const struct ipaddr *ip)
3188{
0bbd4ff4 3189 enum zebra_dplane_result result;
f188e68e
AK
3190 uint32_t update_flags = 0;
3191
3192 update_flags |= DPLANE_NEIGH_REMOTE;
931fa60c
MS
3193
3194 result = neigh_update_internal(DPLANE_OP_NEIGH_DELETE,
f188e68e 3195 ifp, NULL, ip, 0, 0, update_flags);
931fa60c
MS
3196
3197 return result;
3198}
3199
0bbd4ff4
MS
3200/*
3201 * Enqueue evpn VTEP add for the dataplane.
3202 */
3203enum zebra_dplane_result dplane_vtep_add(const struct interface *ifp,
3204 const struct in_addr *ip,
3205 vni_t vni)
3206{
3207 enum zebra_dplane_result result;
3208 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3209 struct ipaddr addr;
3210
3211 if (IS_ZEBRA_DEBUG_VXLAN)
3212 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
3213 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3214
3215 SET_IPADDR_V4(&addr);
3216 addr.ipaddr_v4 = *ip;
3217
3218 result = neigh_update_internal(DPLANE_OP_VTEP_ADD,
f188e68e 3219 ifp, &mac, &addr, 0, 0, 0);
0bbd4ff4
MS
3220
3221 return result;
3222}
3223
3224/*
3225 * Enqueue evpn VTEP add for the dataplane.
3226 */
3227enum zebra_dplane_result dplane_vtep_delete(const struct interface *ifp,
3228 const struct in_addr *ip,
3229 vni_t vni)
3230{
3231 enum zebra_dplane_result result;
3232 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3233 struct ipaddr addr;
3234
3235 if (IS_ZEBRA_DEBUG_VXLAN)
3236 zlog_debug(
3237 "Uninstall %s from flood list for VNI %u intf %s(%u)",
3238 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3239
3240 SET_IPADDR_V4(&addr);
3241 addr.ipaddr_v4 = *ip;
3242
3243 result = neigh_update_internal(DPLANE_OP_VTEP_DELETE,
f188e68e 3244 ifp, &mac, &addr, 0, 0, 0);
0bbd4ff4
MS
3245
3246 return result;
3247}
3248
d68e74b4
JU
3249enum zebra_dplane_result dplane_neigh_discover(const struct interface *ifp,
3250 const struct ipaddr *ip)
3251{
3252 enum zebra_dplane_result result;
3253
3254 result = neigh_update_internal(DPLANE_OP_NEIGH_DISCOVER, ifp, NULL, ip,
3255 DPLANE_NTF_USE, DPLANE_NUD_INCOMPLETE, 0);
3256
3257 return result;
3258}
3259
931fa60c 3260/*
d68e74b4 3261 * Common helper api for neighbor updates
931fa60c
MS
3262 */
3263static enum zebra_dplane_result
3264neigh_update_internal(enum dplane_op_e op,
3265 const struct interface *ifp,
3266 const struct ethaddr *mac,
3267 const struct ipaddr *ip,
f188e68e
AK
3268 uint32_t flags, uint16_t state,
3269 uint32_t update_flags)
931fa60c
MS
3270{
3271 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3272 int ret;
3273 struct zebra_dplane_ctx *ctx = NULL;
3274 struct zebra_ns *zns;
3275
3276 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3277 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
3278
3279 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
d68e74b4 3280 dplane_op2str(op), ifp->name,
931fa60c 3281 prefix_mac2str(mac, buf1, sizeof(buf1)),
931fa60c
MS
3282 ipaddr2str(ip, buf2, sizeof(buf2)));
3283 }
3284
3285 ctx = dplane_ctx_alloc();
3286
3287 ctx->zd_op = op;
3288 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3289 ctx->zd_vrf_id = ifp->vrf_id;
3290
3291 zns = zebra_ns_lookup(ifp->vrf_id);
3292 dplane_ctx_ns_init(ctx, zns, false);
3293
3294 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3295 ctx->zd_ifindex = ifp->ifindex;
3296
3297 /* Init the neighbor-specific data area */
3298 memset(&ctx->u.neigh, 0, sizeof(ctx->u.neigh));
3299
3300 ctx->u.neigh.ip_addr = *ip;
3301 if (mac)
3302 ctx->u.neigh.mac = *mac;
3303 ctx->u.neigh.flags = flags;
3304 ctx->u.neigh.state = state;
f188e68e 3305 ctx->u.neigh.update_flags = update_flags;
931fa60c
MS
3306
3307 /* Enqueue for processing on the dplane pthread */
3308 ret = dplane_update_enqueue(ctx);
3309
3310 /* Increment counter */
3311 atomic_fetch_add_explicit(&zdplane_info.dg_neighs_in, 1,
3312 memory_order_relaxed);
3313
3314 if (ret == AOK)
3315 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3316 else {
3317 /* Error counter */
3318 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors, 1,
3319 memory_order_relaxed);
3320 dplane_ctx_free(&ctx);
3321 }
3322
3323 return result;
3324}
3325
f62e5480
JU
3326/*
3327 * Common helper api for PBR rule updates
3328 */
3329static enum zebra_dplane_result
3330rule_update_internal(enum dplane_op_e op, struct zebra_pbr_rule *new_rule,
3331 struct zebra_pbr_rule *old_rule)
3332{
3333 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3334 struct zebra_dplane_ctx *ctx;
3335 int ret;
3336
3337 ctx = dplane_ctx_alloc();
3338
3339 ret = dplane_ctx_rule_init(ctx, op, new_rule, old_rule);
3340 if (ret != AOK)
3341 goto done;
3342
3343 ret = dplane_update_enqueue(ctx);
3344
3345done:
3346 atomic_fetch_add_explicit(&zdplane_info.dg_rules_in, 1,
3347 memory_order_relaxed);
3348
3349 if (ret == AOK)
3350 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3351 else {
3352 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors, 1,
3353 memory_order_relaxed);
3354 dplane_ctx_free(&ctx);
3355 }
3356
3357 return result;
3358}
3359
3360enum zebra_dplane_result dplane_pbr_rule_add(struct zebra_pbr_rule *rule)
3361{
3362 return rule_update_internal(DPLANE_OP_RULE_ADD, rule, NULL);
3363}
3364
3365enum zebra_dplane_result dplane_pbr_rule_delete(struct zebra_pbr_rule *rule)
3366{
3367 return rule_update_internal(DPLANE_OP_RULE_DELETE, rule, NULL);
3368}
3369
3370enum zebra_dplane_result dplane_pbr_rule_update(struct zebra_pbr_rule *old_rule,
3371 struct zebra_pbr_rule *new_rule)
3372{
3373 return rule_update_internal(DPLANE_OP_RULE_UPDATE, new_rule, old_rule);
3374}
3375
1d11b21f
MS
3376/*
3377 * Handler for 'show dplane'
3378 */
3379int dplane_show_helper(struct vty *vty, bool detailed)
3380{
16c628de
MS
3381 uint64_t queued, queue_max, limit, errs, incoming, yields,
3382 other_errs;
1d11b21f 3383
4dfd7a02 3384 /* Using atomics because counters are being changed in different
c831033f 3385 * pthread contexts.
4dfd7a02 3386 */
25779064 3387 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
1d11b21f 3388 memory_order_relaxed);
91f16812
MS
3389 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
3390 memory_order_relaxed);
25779064 3391 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
1d11b21f 3392 memory_order_relaxed);
25779064 3393 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
4dfd7a02 3394 memory_order_relaxed);
25779064 3395 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
1d11b21f 3396 memory_order_relaxed);
c831033f
MS
3397 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
3398 memory_order_relaxed);
16c628de
MS
3399 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
3400 memory_order_relaxed);
1d11b21f 3401
c831033f
MS
3402 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
3403 incoming);
1d11b21f 3404 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
16c628de 3405 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
91f16812 3406 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
1d11b21f 3407 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
4dfd7a02 3408 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
4280d91c
MS
3409 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
3410
3411 incoming = atomic_load_explicit(&zdplane_info.dg_lsps_in,
3412 memory_order_relaxed);
3413 errs = atomic_load_explicit(&zdplane_info.dg_lsp_errors,
3414 memory_order_relaxed);
3415 vty_out(vty, "LSP updates: %"PRIu64"\n", incoming);
3416 vty_out(vty, "LSP update errors: %"PRIu64"\n", errs);
3417
3418 incoming = atomic_load_explicit(&zdplane_info.dg_pws_in,
3419 memory_order_relaxed);
3420 errs = atomic_load_explicit(&zdplane_info.dg_pw_errors,
3421 memory_order_relaxed);
3422 vty_out(vty, "PW updates: %"PRIu64"\n", incoming);
3423 vty_out(vty, "PW update errors: %"PRIu64"\n", errs);
3424
3425 incoming = atomic_load_explicit(&zdplane_info.dg_intf_addrs_in,
3426 memory_order_relaxed);
3427 errs = atomic_load_explicit(&zdplane_info.dg_intf_addr_errors,
3428 memory_order_relaxed);
3429 vty_out(vty, "Intf addr updates: %"PRIu64"\n", incoming);
3430 vty_out(vty, "Intf addr errors: %"PRIu64"\n", errs);
3431
3432 incoming = atomic_load_explicit(&zdplane_info.dg_macs_in,
3433 memory_order_relaxed);
3434 errs = atomic_load_explicit(&zdplane_info.dg_mac_errors,
3435 memory_order_relaxed);
3436 vty_out(vty, "EVPN MAC updates: %"PRIu64"\n", incoming);
3437 vty_out(vty, "EVPN MAC errors: %"PRIu64"\n", errs);
1d11b21f 3438
931fa60c
MS
3439 incoming = atomic_load_explicit(&zdplane_info.dg_neighs_in,
3440 memory_order_relaxed);
3441 errs = atomic_load_explicit(&zdplane_info.dg_neigh_errors,
3442 memory_order_relaxed);
3443 vty_out(vty, "EVPN neigh updates: %"PRIu64"\n", incoming);
3444 vty_out(vty, "EVPN neigh errors: %"PRIu64"\n", errs);
3445
60d8d43b
JU
3446 incoming = atomic_load_explicit(&zdplane_info.dg_rules_in,
3447 memory_order_relaxed);
3448 errs = atomic_load_explicit(&zdplane_info.dg_rule_errors,
3449 memory_order_relaxed);
3450 vty_out(vty, "Rule updates: %" PRIu64 "\n", incoming);
3451 vty_out(vty, "Rule errors: %" PRIu64 "\n", errs);
3452
1d11b21f
MS
3453 return CMD_SUCCESS;
3454}
3455
3456/*
3457 * Handler for 'show dplane providers'
3458 */
3459int dplane_show_provs_helper(struct vty *vty, bool detailed)
3460{
c831033f
MS
3461 struct zebra_dplane_provider *prov;
3462 uint64_t in, in_max, out, out_max;
3463
3464 vty_out(vty, "Zebra dataplane providers:\n");
3465
3466 DPLANE_LOCK();
3467 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
3468 DPLANE_UNLOCK();
3469
3470 /* Show counters, useful info from each registered provider */
3471 while (prov) {
3472
3473 in = atomic_load_explicit(&prov->dp_in_counter,
3474 memory_order_relaxed);
3475 in_max = atomic_load_explicit(&prov->dp_in_max,
3476 memory_order_relaxed);
3477 out = atomic_load_explicit(&prov->dp_out_counter,
3478 memory_order_relaxed);
3479 out_max = atomic_load_explicit(&prov->dp_out_max,
3480 memory_order_relaxed);
3481
d68e74b4
JU
3482 vty_out(vty,
3483 "%s (%u): in: %" PRIu64 ", q_max: %" PRIu64
3484 ", out: %" PRIu64 ", q_max: %" PRIu64 "\n",
c831033f
MS
3485 prov->dp_name, prov->dp_id, in, in_max, out, out_max);
3486
3487 DPLANE_LOCK();
3488 prov = TAILQ_NEXT(prov, dp_prov_link);
3489 DPLANE_UNLOCK();
3490 }
1d11b21f
MS
3491
3492 return CMD_SUCCESS;
3493}
3494
f26730e1
MS
3495/*
3496 * Helper for 'show run' etc.
3497 */
3498int dplane_config_write_helper(struct vty *vty)
3499{
3500 if (zdplane_info.dg_max_queued_updates != DPLANE_DEFAULT_MAX_QUEUED)
3501 vty_out(vty, "zebra dplane limit %u\n",
3502 zdplane_info.dg_max_queued_updates);
3503
3504 return 0;
3505}
3506
b8e0423d
MS
3507/*
3508 * Provider registration
3509 */
3510int dplane_provider_register(const char *name,
c831033f
MS
3511 enum dplane_provider_prio prio,
3512 int flags,
1dd4ea8a 3513 int (*start_fp)(struct zebra_dplane_provider *),
4c206c8f
MS
3514 int (*fp)(struct zebra_dplane_provider *),
3515 int (*fini_fp)(struct zebra_dplane_provider *,
3516 bool early),
1ff8a248
MS
3517 void *data,
3518 struct zebra_dplane_provider **prov_p)
b8e0423d
MS
3519{
3520 int ret = 0;
6fb51ccb 3521 struct zebra_dplane_provider *p = NULL, *last;
b8e0423d
MS
3522
3523 /* Validate */
3524 if (fp == NULL) {
3525 ret = EINVAL;
3526 goto done;
3527 }
3528
3529 if (prio <= DPLANE_PRIO_NONE ||
1bcea841 3530 prio > DPLANE_PRIO_LAST) {
b8e0423d
MS
3531 ret = EINVAL;
3532 goto done;
3533 }
3534
3535 /* Allocate and init new provider struct */
25779064 3536 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
b8e0423d 3537
c831033f
MS
3538 pthread_mutex_init(&(p->dp_mutex), NULL);
3539 TAILQ_INIT(&(p->dp_ctx_in_q));
3540 TAILQ_INIT(&(p->dp_ctx_out_q));
b8e0423d 3541
932dbb4d 3542 p->dp_flags = flags;
b8e0423d
MS
3543 p->dp_priority = prio;
3544 p->dp_fp = fp;
1dd4ea8a 3545 p->dp_start = start_fp;
18c37974 3546 p->dp_fini = fini_fp;
c831033f 3547 p->dp_data = data;
18c37974 3548
c831033f 3549 /* Lock - the dplane pthread may be running */
18c37974 3550 DPLANE_LOCK();
b8e0423d 3551
25779064 3552 p->dp_id = ++zdplane_info.dg_provider_id;
b8e0423d 3553
c831033f
MS
3554 if (name)
3555 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
3556 else
3557 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
3558 "provider-%u", p->dp_id);
3559
b8e0423d 3560 /* Insert into list ordered by priority */
c831033f 3561 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
5709131c 3562 if (last->dp_priority > p->dp_priority)
b8e0423d 3563 break;
b8e0423d
MS
3564 }
3565
5709131c 3566 if (last)
c831033f 3567 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
5709131c 3568 else
25779064 3569 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
c831033f 3570 dp_prov_link);
b8e0423d 3571
18c37974
MS
3572 /* And unlock */
3573 DPLANE_UNLOCK();
3574
c831033f
MS
3575 if (IS_ZEBRA_DEBUG_DPLANE)
3576 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
3577 p->dp_name, p->dp_id, p->dp_priority);
3578
b8e0423d 3579done:
1ff8a248
MS
3580 if (prov_p)
3581 *prov_p = p;
3582
5709131c 3583 return ret;
b8e0423d
MS
3584}
3585
c831033f
MS
3586/* Accessors for provider attributes */
3587const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
3588{
3589 return prov->dp_name;
3590}
3591
3592uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
3593{
3594 return prov->dp_id;
3595}
3596
3597void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
3598{
3599 return prov->dp_data;
3600}
3601
3602int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
3603{
3604 return zdplane_info.dg_updates_per_cycle;
3605}
3606
ad6aad4d
MS
3607/* Lock/unlock a provider's mutex - iff the provider was registered with
3608 * the THREADED flag.
3609 */
3610void dplane_provider_lock(struct zebra_dplane_provider *prov)
3611{
3612 if (dplane_provider_is_threaded(prov))
3613 DPLANE_PROV_LOCK(prov);
3614}
3615
3616void dplane_provider_unlock(struct zebra_dplane_provider *prov)
3617{
3618 if (dplane_provider_is_threaded(prov))
3619 DPLANE_PROV_UNLOCK(prov);
3620}
3621
c831033f
MS
3622/*
3623 * Dequeue and maintain associated counter
3624 */
3625struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
3626 struct zebra_dplane_provider *prov)
3627{
3628 struct zebra_dplane_ctx *ctx = NULL;
3629
ad6aad4d 3630 dplane_provider_lock(prov);
c831033f
MS
3631
3632 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3633 if (ctx) {
3634 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
c9d17fe8
MS
3635
3636 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
3637 memory_order_relaxed);
c831033f
MS
3638 }
3639
ad6aad4d 3640 dplane_provider_unlock(prov);
c831033f
MS
3641
3642 return ctx;
3643}
3644
3645/*
3646 * Dequeue work to a list, return count
3647 */
3648int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
3649 struct dplane_ctx_q *listp)
3650{
3651 int limit, ret;
3652 struct zebra_dplane_ctx *ctx;
3653
3654 limit = zdplane_info.dg_updates_per_cycle;
3655
ad6aad4d 3656 dplane_provider_lock(prov);
c831033f
MS
3657
3658 for (ret = 0; ret < limit; ret++) {
3659 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3660 if (ctx) {
3661 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
3662
3663 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
3664 } else {
3665 break;
3666 }
3667 }
3668
c9d17fe8
MS
3669 if (ret > 0)
3670 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
3671 memory_order_relaxed);
3672
ad6aad4d 3673 dplane_provider_unlock(prov);
c831033f
MS
3674
3675 return ret;
3676}
3677
3678/*
3679 * Enqueue and maintain associated counter
3680 */
3681void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
3682 struct zebra_dplane_ctx *ctx)
3683{
ad6aad4d 3684 dplane_provider_lock(prov);
c831033f
MS
3685
3686 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
3687 zd_q_entries);
3688
ad6aad4d 3689 dplane_provider_unlock(prov);
c831033f
MS
3690
3691 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
3692 memory_order_relaxed);
3693}
3694
62b8bb7a
MS
3695/*
3696 * Accessor for provider object
3697 */
c831033f
MS
3698bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
3699{
3700 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
3701}
3702
62b8bb7a
MS
3703/*
3704 * Internal helper that copies information from a zebra ns object; this is
3705 * called in the zebra main pthread context as part of dplane ctx init.
3706 */
3707static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
3708 struct zebra_ns *zns)
3709{
3710 ns_info->ns_id = zns->ns_id;
3711
3712#if defined(HAVE_NETLINK)
3713 ns_info->is_cmd = true;
3714 ns_info->nls = zns->netlink_dplane;
3715#endif /* NETLINK */
3716}
3717
c831033f
MS
3718/*
3719 * Provider api to signal that work/events are available
3720 * for the dataplane pthread.
3721 */
3722int dplane_provider_work_ready(void)
3723{
e5a60d82
MS
3724 /* Note that during zebra startup, we may be offered work before
3725 * the dataplane pthread (and thread-master) are ready. We want to
3726 * enqueue the work, but the event-scheduling machinery may not be
3727 * available.
3728 */
3729 if (zdplane_info.dg_run) {
3730 thread_add_event(zdplane_info.dg_master,
3731 dplane_thread_loop, NULL, 0,
3732 &zdplane_info.dg_t_update);
3733 }
c831033f
MS
3734
3735 return AOK;
3736}
3737
593e4eb1
MS
3738/*
3739 * Enqueue a context directly to zebra main.
3740 */
3741void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx)
3742{
3743 struct dplane_ctx_q temp_list;
3744
3745 /* Zebra's api takes a list, so we need to use a temporary list */
3746 TAILQ_INIT(&temp_list);
3747
3748 TAILQ_INSERT_TAIL(&temp_list, ctx, zd_q_entries);
3749 (zdplane_info.dg_results_cb)(&temp_list);
3750}
3751
7cdb1a84 3752/*
c831033f 3753 * Kernel dataplane provider
7cdb1a84 3754 */
c831033f 3755
fef24b03 3756static void kernel_dplane_log_detail(struct zebra_dplane_ctx *ctx)
c10a646d 3757{
fef24b03 3758 char buf[PREFIX_STRLEN];
c10a646d 3759
fef24b03 3760 switch (dplane_ctx_get_op(ctx)) {
16c628de 3761
fef24b03
JU
3762 case DPLANE_OP_ROUTE_INSTALL:
3763 case DPLANE_OP_ROUTE_UPDATE:
3764 case DPLANE_OP_ROUTE_DELETE:
2dbe669b
DA
3765 zlog_debug("%u:%pFX Dplane route update ctx %p op %s",
3766 dplane_ctx_get_vrf(ctx), dplane_ctx_get_dest(ctx),
3767 ctx, dplane_op2str(dplane_ctx_get_op(ctx)));
fef24b03 3768 break;
64168803 3769
fef24b03
JU
3770 case DPLANE_OP_NH_INSTALL:
3771 case DPLANE_OP_NH_UPDATE:
3772 case DPLANE_OP_NH_DELETE:
f820d025 3773 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
0c8215cb 3774 dplane_ctx_get_nhe_id(ctx), ctx,
f820d025 3775 dplane_op2str(dplane_ctx_get_op(ctx)));
fef24b03 3776 break;
f820d025 3777
fef24b03
JU
3778 case DPLANE_OP_LSP_INSTALL:
3779 case DPLANE_OP_LSP_UPDATE:
3780 case DPLANE_OP_LSP_DELETE:
3781 break;
f820d025 3782
fef24b03
JU
3783 case DPLANE_OP_PW_INSTALL:
3784 case DPLANE_OP_PW_UNINSTALL:
3785 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
3786 dplane_ctx_get_ifname(ctx),
3787 dplane_op2str(ctx->zd_op), dplane_ctx_get_pw_af(ctx),
3788 dplane_ctx_get_pw_local_label(ctx),
3789 dplane_ctx_get_pw_remote_label(ctx));
3790 break;
3791
3792 case DPLANE_OP_ADDR_INSTALL:
3793 case DPLANE_OP_ADDR_UNINSTALL:
2dbe669b 3794 zlog_debug("Dplane intf %s, idx %u, addr %pFX",
fef24b03 3795 dplane_op2str(dplane_ctx_get_op(ctx)),
2dbe669b
DA
3796 dplane_ctx_get_ifindex(ctx),
3797 dplane_ctx_get_intf_addr(ctx));
fef24b03 3798 break;
036d93c0 3799
fef24b03
JU
3800 case DPLANE_OP_MAC_INSTALL:
3801 case DPLANE_OP_MAC_DELETE:
036d93c0
MS
3802 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf,
3803 sizeof(buf));
3804
3805 zlog_debug("Dplane %s, mac %s, ifindex %u",
3806 dplane_op2str(dplane_ctx_get_op(ctx)),
3807 buf, dplane_ctx_get_ifindex(ctx));
fef24b03 3808 break;
931fa60c 3809
fef24b03
JU
3810 case DPLANE_OP_NEIGH_INSTALL:
3811 case DPLANE_OP_NEIGH_UPDATE:
3812 case DPLANE_OP_NEIGH_DELETE:
3813 case DPLANE_OP_VTEP_ADD:
3814 case DPLANE_OP_VTEP_DELETE:
d68e74b4 3815 case DPLANE_OP_NEIGH_DISCOVER:
931fa60c
MS
3816 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx), buf,
3817 sizeof(buf));
3818
3819 zlog_debug("Dplane %s, ip %s, ifindex %u",
3820 dplane_op2str(dplane_ctx_get_op(ctx)),
3821 buf, dplane_ctx_get_ifindex(ctx));
fef24b03 3822 break;
931fa60c 3823
fef24b03
JU
3824 case DPLANE_OP_RULE_ADD:
3825 case DPLANE_OP_RULE_DELETE:
3826 case DPLANE_OP_RULE_UPDATE:
f62e5480
JU
3827 zlog_debug("Dplane rule update op %s, if %s(%u), ctx %p",
3828 dplane_op2str(dplane_ctx_get_op(ctx)),
3829 dplane_ctx_get_ifname(ctx),
3830 dplane_ctx_get_ifindex(ctx), ctx);
fef24b03 3831 break;
f62e5480 3832
fef24b03
JU
3833 case DPLANE_OP_SYS_ROUTE_ADD:
3834 case DPLANE_OP_SYS_ROUTE_DELETE:
3835 case DPLANE_OP_ROUTE_NOTIFY:
3836 case DPLANE_OP_LSP_NOTIFY:
3837
3838 case DPLANE_OP_NONE:
3839 break;
3840 }
2f74a82a 3841}
f62e5480 3842
fef24b03 3843static void kernel_dplane_handle_result(struct zebra_dplane_ctx *ctx)
2f74a82a 3844{
fef24b03
JU
3845 enum zebra_dplane_result res = dplane_ctx_get_status(ctx);
3846
2f74a82a
JU
3847 switch (dplane_ctx_get_op(ctx)) {
3848
3849 case DPLANE_OP_ROUTE_INSTALL:
3850 case DPLANE_OP_ROUTE_UPDATE:
3851 case DPLANE_OP_ROUTE_DELETE:
3852 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3853 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
3854 1, memory_order_relaxed);
fef24b03
JU
3855
3856 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE)
3857 && (res == ZEBRA_DPLANE_REQUEST_SUCCESS)) {
3858 struct nexthop *nexthop;
3859
3860 /* Update installed nexthops to signal which have been
3861 * installed.
3862 */
3863 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
3864 nexthop)) {
3865 if (CHECK_FLAG(nexthop->flags,
3866 NEXTHOP_FLAG_RECURSIVE))
3867 continue;
3868
3869 if (CHECK_FLAG(nexthop->flags,
3870 NEXTHOP_FLAG_ACTIVE)) {
3871 SET_FLAG(nexthop->flags,
3872 NEXTHOP_FLAG_FIB);
3873 }
3874 }
3875 }
2f74a82a
JU
3876 break;
3877
3878 case DPLANE_OP_NH_INSTALL:
3879 case DPLANE_OP_NH_UPDATE:
3880 case DPLANE_OP_NH_DELETE:
3881 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3882 atomic_fetch_add_explicit(
3883 &zdplane_info.dg_nexthop_errors, 1,
3884 memory_order_relaxed);
3885 break;
3886
3887 case DPLANE_OP_LSP_INSTALL:
3888 case DPLANE_OP_LSP_UPDATE:
3889 case DPLANE_OP_LSP_DELETE:
3890 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3891 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors,
3892 1, memory_order_relaxed);
3893 break;
3894
3895 case DPLANE_OP_PW_INSTALL:
3896 case DPLANE_OP_PW_UNINSTALL:
3897 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3898 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
3899 memory_order_relaxed);
3900 break;
f62e5480 3901
2f74a82a
JU
3902 case DPLANE_OP_ADDR_INSTALL:
3903 case DPLANE_OP_ADDR_UNINSTALL:
3904 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3905 atomic_fetch_add_explicit(
3906 &zdplane_info.dg_intf_addr_errors, 1,
3907 memory_order_relaxed);
3908 break;
3909
3910 case DPLANE_OP_MAC_INSTALL:
3911 case DPLANE_OP_MAC_DELETE:
3912 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3913 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors,
3914 1, memory_order_relaxed);
3915 break;
3916
3917 case DPLANE_OP_NEIGH_INSTALL:
3918 case DPLANE_OP_NEIGH_UPDATE:
3919 case DPLANE_OP_NEIGH_DELETE:
3920 case DPLANE_OP_VTEP_ADD:
3921 case DPLANE_OP_VTEP_DELETE:
d68e74b4 3922 case DPLANE_OP_NEIGH_DISCOVER:
2f74a82a
JU
3923 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3924 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors,
3925 1, memory_order_relaxed);
3926 break;
3927
3928 case DPLANE_OP_RULE_ADD:
3929 case DPLANE_OP_RULE_DELETE:
3930 case DPLANE_OP_RULE_UPDATE:
3931 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3932 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors,
3933 1, memory_order_relaxed);
3934 break;
3935
3936 /* Ignore 'notifications' - no-op */
3937 case DPLANE_OP_SYS_ROUTE_ADD:
3938 case DPLANE_OP_SYS_ROUTE_DELETE:
3939 case DPLANE_OP_ROUTE_NOTIFY:
3940 case DPLANE_OP_LSP_NOTIFY:
fef24b03
JU
3941 break;
3942
2f74a82a 3943 case DPLANE_OP_NONE:
fef24b03
JU
3944 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3945 atomic_fetch_add_explicit(&zdplane_info.dg_other_errors,
3946 1, memory_order_relaxed);
2f74a82a
JU
3947 break;
3948 }
f62e5480
JU
3949}
3950
fef24b03
JU
3951/*
3952 * Kernel provider callback
3953 */
3954static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
3955{
3956 struct zebra_dplane_ctx *ctx, *tctx;
3957 struct dplane_ctx_q work_list;
3958 int counter, limit;
3959
3960 TAILQ_INIT(&work_list);
3961
3962 limit = dplane_provider_get_work_limit(prov);
3963
3964 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3965 zlog_debug("dplane provider '%s': processing",
3966 dplane_provider_get_name(prov));
3967
3968 for (counter = 0; counter < limit; counter++) {
3969 ctx = dplane_provider_dequeue_in_ctx(prov);
3970 if (ctx == NULL)
3971 break;
3972
3973 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3974 kernel_dplane_log_detail(ctx);
2f74a82a
JU
3975
3976 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
3977 }
c831033f 3978
18f60fe9 3979 kernel_update_multi(&work_list);
fef24b03 3980
2f74a82a 3981 TAILQ_FOREACH_SAFE (ctx, &work_list, zd_q_entries, tctx) {
fef24b03
JU
3982 kernel_dplane_handle_result(ctx);
3983
2f74a82a 3984 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
c831033f
MS
3985 dplane_provider_enqueue_out_ctx(prov, ctx);
3986 }
3987
3988 /* Ensure that we'll run the work loop again if there's still
3989 * more work to do.
3990 */
3991 if (counter >= limit) {
3992 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3993 zlog_debug("dplane provider '%s' reached max updates %d",
3994 dplane_provider_get_name(prov), counter);
3995
3996 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
3997 1, memory_order_relaxed);
3998
3999 dplane_provider_work_ready();
4000 }
4001
4002 return 0;
4003}
4004
1e20238a 4005#ifdef DPLANE_TEST_PROVIDER
e5a60d82 4006
c831033f
MS
4007/*
4008 * Test dataplane provider plugin
4009 */
4010
4011/*
4012 * Test provider process callback
4013 */
4014static int test_dplane_process_func(struct zebra_dplane_provider *prov)
4015{
4016 struct zebra_dplane_ctx *ctx;
4017 int counter, limit;
4018
4019 /* Just moving from 'in' queue to 'out' queue */
4020
4021 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4022 zlog_debug("dplane provider '%s': processing",
4023 dplane_provider_get_name(prov));
4024
4025 limit = dplane_provider_get_work_limit(prov);
4026
4027 for (counter = 0; counter < limit; counter++) {
c831033f
MS
4028 ctx = dplane_provider_dequeue_in_ctx(prov);
4029 if (ctx == NULL)
4030 break;
4031
cf363e1b
MS
4032 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4033 zlog_debug("dplane provider '%s': op %s",
4034 dplane_provider_get_name(prov),
4035 dplane_op2str(dplane_ctx_get_op(ctx)));
4036
c831033f
MS
4037 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
4038
4039 dplane_provider_enqueue_out_ctx(prov, ctx);
4040 }
4041
c9d17fe8
MS
4042 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4043 zlog_debug("dplane provider '%s': processed %d",
4044 dplane_provider_get_name(prov), counter);
4045
c831033f
MS
4046 /* Ensure that we'll run the work loop again if there's still
4047 * more work to do.
4048 */
4049 if (counter >= limit)
4050 dplane_provider_work_ready();
4051
4052 return 0;
4053}
4054
4055/*
4056 * Test provider shutdown/fini callback
4057 */
4058static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
4059 bool early)
4060{
4061 if (IS_ZEBRA_DEBUG_DPLANE)
4062 zlog_debug("dplane provider '%s': %sshutdown",
4063 dplane_provider_get_name(prov),
4064 early ? "early " : "");
4065
4066 return 0;
4067}
e5a60d82 4068#endif /* DPLANE_TEST_PROVIDER */
c831033f
MS
4069
4070/*
4071 * Register default kernel provider
4072 */
4073static void dplane_provider_init(void)
4074{
4075 int ret;
4076
4077 ret = dplane_provider_register("Kernel",
4078 DPLANE_PRIO_KERNEL,
1dd4ea8a 4079 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
4080 kernel_dplane_process_func,
4081 NULL,
1ff8a248 4082 NULL, NULL);
c831033f
MS
4083
4084 if (ret != AOK)
4085 zlog_err("Unable to register kernel dplane provider: %d",
4086 ret);
4087
1e20238a 4088#ifdef DPLANE_TEST_PROVIDER
e5a60d82 4089 /* Optional test provider ... */
c831033f
MS
4090 ret = dplane_provider_register("Test",
4091 DPLANE_PRIO_PRE_KERNEL,
1dd4ea8a 4092 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
4093 test_dplane_process_func,
4094 test_dplane_shutdown_func,
1ff8a248 4095 NULL /* data */, NULL);
c831033f
MS
4096
4097 if (ret != AOK)
4098 zlog_err("Unable to register test dplane provider: %d",
4099 ret);
e5a60d82 4100#endif /* DPLANE_TEST_PROVIDER */
7cdb1a84
MS
4101}
4102
4dfd7a02
MS
4103/* Indicates zebra shutdown/exit is in progress. Some operations may be
4104 * simplified or skipped during shutdown processing.
4105 */
4106bool dplane_is_in_shutdown(void)
4107{
25779064 4108 return zdplane_info.dg_is_shutdown;
4dfd7a02
MS
4109}
4110
4111/*
4112 * Early or pre-shutdown, de-init notification api. This runs pretty
4113 * early during zebra shutdown, as a signal to stop new work and prepare
4114 * for updates generated by shutdown/cleanup activity, as zebra tries to
4115 * remove everything it's responsible for.
c9d17fe8 4116 * NB: This runs in the main zebra pthread context.
4dfd7a02
MS
4117 */
4118void zebra_dplane_pre_finish(void)
4119{
3c0e1622 4120 struct zebra_dplane_provider *prov;
6ba8db21 4121
4dfd7a02 4122 if (IS_ZEBRA_DEBUG_DPLANE)
3c0e1622 4123 zlog_debug("Zebra dataplane pre-finish called");
4dfd7a02 4124
25779064 4125 zdplane_info.dg_is_shutdown = true;
4dfd7a02 4126
6ba8db21 4127 /* Notify provider(s) of pending shutdown. */
3c0e1622
MS
4128 TAILQ_FOREACH(prov, &zdplane_info.dg_providers_q, dp_prov_link) {
4129 if (prov->dp_fini == NULL)
6ba8db21
RZ
4130 continue;
4131
3c0e1622 4132 prov->dp_fini(prov, true /* early */);
6ba8db21 4133 }
4dfd7a02
MS
4134}
4135
4136/*
4137 * Utility to determine whether work remains enqueued within the dplane;
4138 * used during system shutdown processing.
4139 */
4140static bool dplane_work_pending(void)
4141{
c9d17fe8 4142 bool ret = false;
25779064 4143 struct zebra_dplane_ctx *ctx;
c9d17fe8 4144 struct zebra_dplane_provider *prov;
4dfd7a02 4145
c831033f
MS
4146 /* TODO -- just checking incoming/pending work for now, must check
4147 * providers
4148 */
4dfd7a02
MS
4149 DPLANE_LOCK();
4150 {
3fe4ccc4 4151 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c9d17fe8 4152 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4dfd7a02
MS
4153 }
4154 DPLANE_UNLOCK();
4155
c9d17fe8
MS
4156 if (ctx != NULL) {
4157 ret = true;
4158 goto done;
4159 }
4160
4161 while (prov) {
4162
ad6aad4d 4163 dplane_provider_lock(prov);
c9d17fe8
MS
4164
4165 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
4166 if (ctx == NULL)
4167 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4168
ad6aad4d 4169 dplane_provider_unlock(prov);
c9d17fe8
MS
4170
4171 if (ctx != NULL)
4172 break;
4173
4174 DPLANE_LOCK();
4175 prov = TAILQ_NEXT(prov, dp_prov_link);
4176 DPLANE_UNLOCK();
4177 }
4178
4179 if (ctx != NULL)
4180 ret = true;
4181
4182done:
4183 return ret;
4dfd7a02
MS
4184}
4185
4186/*
4187 * Shutdown-time intermediate callback, used to determine when all pending
4188 * in-flight updates are done. If there's still work to do, reschedules itself.
4189 * If all work is done, schedules an event to the main zebra thread for
4190 * final zebra shutdown.
4191 * This runs in the dplane pthread context.
4192 */
4193static int dplane_check_shutdown_status(struct thread *event)
4194{
4195 if (IS_ZEBRA_DEBUG_DPLANE)
4196 zlog_debug("Zebra dataplane shutdown status check called");
4197
4198 if (dplane_work_pending()) {
4199 /* Reschedule dplane check on a short timer */
25779064 4200 thread_add_timer_msec(zdplane_info.dg_master,
4dfd7a02
MS
4201 dplane_check_shutdown_status,
4202 NULL, 100,
25779064 4203 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
4204
4205 /* TODO - give up and stop waiting after a short time? */
4206
4207 } else {
4208 /* We appear to be done - schedule a final callback event
4209 * for the zebra main pthread.
4210 */
3801e764 4211 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
4dfd7a02
MS
4212 }
4213
4214 return 0;
4215}
4216
18c37974 4217/*
1d11b21f 4218 * Shutdown, de-init api. This runs pretty late during shutdown,
4dfd7a02
MS
4219 * after zebra has tried to free/remove/uninstall all routes during shutdown.
4220 * At this point, dplane work may still remain to be done, so we can't just
4221 * blindly terminate. If there's still work to do, we'll periodically check
4222 * and when done, we'll enqueue a task to the zebra main thread for final
4223 * termination processing.
4224 *
1d11b21f 4225 * NB: This runs in the main zebra thread context.
18c37974 4226 */
1d11b21f 4227void zebra_dplane_finish(void)
18c37974 4228{
4dfd7a02
MS
4229 if (IS_ZEBRA_DEBUG_DPLANE)
4230 zlog_debug("Zebra dataplane fini called");
4231
25779064 4232 thread_add_event(zdplane_info.dg_master,
4dfd7a02 4233 dplane_check_shutdown_status, NULL, 0,
25779064 4234 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
4235}
4236
c831033f
MS
4237/*
4238 * Main dataplane pthread event loop. The thread takes new incoming work
4239 * and offers it to the first provider. It then iterates through the
4240 * providers, taking complete work from each one and offering it
4241 * to the next in order. At each step, a limited number of updates are
4242 * processed during a cycle in order to provide some fairness.
14b0bc8e
MS
4243 *
4244 * This loop through the providers is only run once, so that the dataplane
4245 * pthread can look for other pending work - such as i/o work on behalf of
4246 * providers.
c831033f
MS
4247 */
4248static int dplane_thread_loop(struct thread *event)
4249{
4250 struct dplane_ctx_q work_list;
4251 struct dplane_ctx_q error_list;
4252 struct zebra_dplane_provider *prov;
4253 struct zebra_dplane_ctx *ctx, *tctx;
4254 int limit, counter, error_counter;
c9d17fe8 4255 uint64_t curr, high;
c831033f
MS
4256
4257 /* Capture work limit per cycle */
4258 limit = zdplane_info.dg_updates_per_cycle;
4259
14b0bc8e 4260 /* Init temporary lists used to move contexts among providers */
c831033f 4261 TAILQ_INIT(&work_list);
14b0bc8e
MS
4262 TAILQ_INIT(&error_list);
4263 error_counter = 0;
c831033f
MS
4264
4265 /* Check for zebra shutdown */
4266 if (!zdplane_info.dg_run)
4267 goto done;
4268
4269 /* Dequeue some incoming work from zebra (if any) onto the temporary
4270 * working list.
4271 */
4272 DPLANE_LOCK();
4273
4274 /* Locate initial registered provider */
4275 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4276
14b0bc8e 4277 /* Move new work from incoming list to temp list */
c831033f 4278 for (counter = 0; counter < limit; counter++) {
3fe4ccc4 4279 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c831033f 4280 if (ctx) {
3fe4ccc4 4281 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
c831033f
MS
4282 zd_q_entries);
4283
c831033f
MS
4284 ctx->zd_provider = prov->dp_id;
4285
4286 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
4287 } else {
4288 break;
4289 }
4290 }
4291
4292 DPLANE_UNLOCK();
4293
14b0bc8e
MS
4294 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
4295 memory_order_relaxed);
4296
c831033f
MS
4297 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4298 zlog_debug("dplane: incoming new work counter: %d", counter);
4299
4300 /* Iterate through the registered providers, offering new incoming
4301 * work. If the provider has outgoing work in its queue, take that
4302 * work for the next provider
4303 */
4304 while (prov) {
4305
14b0bc8e
MS
4306 /* At each iteration, the temporary work list has 'counter'
4307 * items.
4308 */
c831033f
MS
4309 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4310 zlog_debug("dplane enqueues %d new work to provider '%s'",
4311 counter, dplane_provider_get_name(prov));
4312
4313 /* Capture current provider id in each context; check for
4314 * error status.
4315 */
4316 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
4317 if (dplane_ctx_get_status(ctx) ==
4318 ZEBRA_DPLANE_REQUEST_SUCCESS) {
4319 ctx->zd_provider = prov->dp_id;
4320 } else {
4321 /*
4322 * TODO -- improve error-handling: recirc
4323 * errors backwards so that providers can
4324 * 'undo' their work (if they want to)
4325 */
4326
4327 /* Move to error list; will be returned
4328 * zebra main.
4329 */
4330 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
4331 TAILQ_INSERT_TAIL(&error_list,
4332 ctx, zd_q_entries);
4333 error_counter++;
4334 }
4335 }
4336
4337 /* Enqueue new work to the provider */
ad6aad4d 4338 dplane_provider_lock(prov);
c831033f
MS
4339
4340 if (TAILQ_FIRST(&work_list))
4341 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
4342 zd_q_entries);
4343
c9d17fe8
MS
4344 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
4345 memory_order_relaxed);
4346 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
4347 memory_order_relaxed);
4348 curr = atomic_load_explicit(&prov->dp_in_queued,
4349 memory_order_relaxed);
4350 high = atomic_load_explicit(&prov->dp_in_max,
4351 memory_order_relaxed);
4352 if (curr > high)
4353 atomic_store_explicit(&prov->dp_in_max, curr,
c831033f
MS
4354 memory_order_relaxed);
4355
ad6aad4d 4356 dplane_provider_unlock(prov);
c831033f 4357
14b0bc8e
MS
4358 /* Reset the temp list (though the 'concat' may have done this
4359 * already), and the counter
4360 */
c831033f
MS
4361 TAILQ_INIT(&work_list);
4362 counter = 0;
4363
14b0bc8e
MS
4364 /* Call into the provider code. Note that this is
4365 * unconditional: we offer to do work even if we don't enqueue
4366 * any _new_ work.
4367 */
c831033f
MS
4368 (*prov->dp_fp)(prov);
4369
4370 /* Check for zebra shutdown */
4371 if (!zdplane_info.dg_run)
4372 break;
4373
4374 /* Dequeue completed work from the provider */
ad6aad4d 4375 dplane_provider_lock(prov);
c831033f
MS
4376
4377 while (counter < limit) {
4378 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4379 if (ctx) {
4380 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
4381 zd_q_entries);
4382
4383 TAILQ_INSERT_TAIL(&work_list,
4384 ctx, zd_q_entries);
4385 counter++;
4386 } else
4387 break;
4388 }
4389
ad6aad4d 4390 dplane_provider_unlock(prov);
c831033f
MS
4391
4392 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4393 zlog_debug("dplane dequeues %d completed work from provider %s",
4394 counter, dplane_provider_get_name(prov));
4395
4396 /* Locate next provider */
4397 DPLANE_LOCK();
4398 prov = TAILQ_NEXT(prov, dp_prov_link);
4399 DPLANE_UNLOCK();
c831033f
MS
4400 }
4401
4402 /* After all providers have been serviced, enqueue any completed
14b0bc8e 4403 * work and any errors back to zebra so it can process the results.
c831033f 4404 */
c831033f 4405 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
14b0bc8e
MS
4406 zlog_debug("dplane has %d completed, %d errors, for zebra main",
4407 counter, error_counter);
c831033f
MS
4408
4409 /*
4c206c8f 4410 * Hand lists through the api to zebra main,
c831033f
MS
4411 * to reduce the number of lock/unlock cycles
4412 */
14b0bc8e 4413
4c206c8f
MS
4414 /* Call through to zebra main */
4415 (zdplane_info.dg_results_cb)(&error_list);
14b0bc8e 4416
4c206c8f 4417 TAILQ_INIT(&error_list);
14b0bc8e 4418
4c206c8f
MS
4419 /* Call through to zebra main */
4420 (zdplane_info.dg_results_cb)(&work_list);
c831033f 4421
4c206c8f 4422 TAILQ_INIT(&work_list);
c831033f
MS
4423
4424done:
4425 return 0;
4426}
4427
4dfd7a02
MS
4428/*
4429 * Final phase of shutdown, after all work enqueued to dplane has been
4430 * processed. This is called from the zebra main pthread context.
4431 */
4432void zebra_dplane_shutdown(void)
4433{
6ba8db21
RZ
4434 struct zebra_dplane_provider *dp;
4435
4dfd7a02
MS
4436 if (IS_ZEBRA_DEBUG_DPLANE)
4437 zlog_debug("Zebra dataplane shutdown called");
1d11b21f
MS
4438
4439 /* Stop dplane thread, if it's running */
4440
25779064 4441 zdplane_info.dg_run = false;
1d11b21f 4442
9344d3fc
SW
4443 if (zdplane_info.dg_t_update)
4444 thread_cancel_async(zdplane_info.dg_t_update->master,
4445 &zdplane_info.dg_t_update, NULL);
1d11b21f 4446
d8c16a95
MS
4447 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
4448
4449 /* Destroy pthread */
4450 frr_pthread_destroy(zdplane_info.dg_pthread);
4451 zdplane_info.dg_pthread = NULL;
4452 zdplane_info.dg_master = NULL;
4dfd7a02 4453
3c0e1622
MS
4454 /* Notify provider(s) of final shutdown.
4455 * Note that this call is in the main pthread, so providers must
4456 * be prepared for that.
4457 */
6ba8db21
RZ
4458 TAILQ_FOREACH(dp, &zdplane_info.dg_providers_q, dp_prov_link) {
4459 if (dp->dp_fini == NULL)
4460 continue;
4461
4462 dp->dp_fini(dp, false);
4463 }
c831033f
MS
4464
4465 /* TODO -- Clean-up provider objects */
4466
4467 /* TODO -- Clean queue(s), free memory */
4468}
4469
4470/*
4471 * Initialize the dataplane module during startup, internal/private version
4472 */
2561d12e 4473static void zebra_dplane_init_internal(void)
c831033f
MS
4474{
4475 memset(&zdplane_info, 0, sizeof(zdplane_info));
4476
4477 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
1d11b21f 4478
3fe4ccc4 4479 TAILQ_INIT(&zdplane_info.dg_update_ctx_q);
c831033f 4480 TAILQ_INIT(&zdplane_info.dg_providers_q);
1d11b21f 4481
c831033f
MS
4482 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
4483
4484 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
4485
4486 /* Register default kernel 'provider' during init */
4487 dplane_provider_init();
e5a60d82 4488}
c831033f 4489
e5a60d82
MS
4490/*
4491 * Start the dataplane pthread. This step needs to be run later than the
4492 * 'init' step, in case zebra has fork-ed.
4493 */
4494void zebra_dplane_start(void)
4495{
1dd4ea8a 4496 struct zebra_dplane_provider *prov;
c831033f
MS
4497 struct frr_pthread_attr pattr = {
4498 .start = frr_pthread_attr_default.start,
4499 .stop = frr_pthread_attr_default.stop
4500 };
4501
1dd4ea8a
MS
4502 /* Start dataplane pthread */
4503
c831033f 4504 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
0eca319c 4505 "zebra_dplane");
c831033f
MS
4506
4507 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
4508
e5a60d82
MS
4509 zdplane_info.dg_run = true;
4510
c831033f
MS
4511 /* Enqueue an initial event for the dataplane pthread */
4512 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
4513 &zdplane_info.dg_t_update);
4514
1dd4ea8a
MS
4515 /* Call start callbacks for registered providers */
4516
4517 DPLANE_LOCK();
4518 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4519 DPLANE_UNLOCK();
4520
4521 while (prov) {
4522
4523 if (prov->dp_start)
4524 (prov->dp_start)(prov);
4525
4526 /* Locate next provider */
4527 DPLANE_LOCK();
4528 prov = TAILQ_NEXT(prov, dp_prov_link);
4529 DPLANE_UNLOCK();
4530 }
4531
c831033f 4532 frr_pthread_run(zdplane_info.dg_pthread, NULL);
18c37974
MS
4533}
4534
7cdb1a84 4535/*
b8e0423d 4536 * Initialize the dataplane module at startup; called by zebra rib_init()
7cdb1a84 4537 */
4c206c8f 4538void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
7cdb1a84 4539{
2561d12e 4540 zebra_dplane_init_internal();
4c206c8f 4541 zdplane_info.dg_results_cb = results_fp;
7cdb1a84 4542}