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