]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_dplane.c
zebra: add a route_entry flag for FIB-specific nexthops
[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 */
65f264cf
MS
1955int dplane_ctx_lsp_init(struct zebra_dplane_ctx *ctx, enum dplane_op_e op,
1956 zebra_lsp_t *lsp)
16c628de
MS
1957{
1958 int ret = AOK;
1959 zebra_nhlfe_t *nhlfe, *new_nhlfe;
1960
16c628de
MS
1961 ctx->zd_op = op;
1962 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
1963
1964 /* Capture namespace info */
1965 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
1966 (op == DPLANE_OP_LSP_UPDATE));
1967
1968 memset(&ctx->u.lsp, 0, sizeof(ctx->u.lsp));
1969
ee70f629 1970 nhlfe_list_init(&(ctx->u.lsp.nhlfe_list));
cd4bb96f 1971 nhlfe_list_init(&(ctx->u.lsp.backup_nhlfe_list));
65f264cf
MS
1972
1973 /* This may be called to create/init a dplane context, not necessarily
1974 * to copy an lsp object.
1975 */
1976 if (lsp == NULL) {
1977 ret = AOK;
1978 goto done;
1979 }
1980
1981 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1982 zlog_debug("init dplane ctx %s: in-label %u ecmp# %d",
1983 dplane_op2str(op), lsp->ile.in_label,
1984 lsp->num_ecmp);
1985
16c628de
MS
1986 ctx->u.lsp.ile = lsp->ile;
1987 ctx->u.lsp.addr_family = lsp->addr_family;
1988 ctx->u.lsp.num_ecmp = lsp->num_ecmp;
1989 ctx->u.lsp.flags = lsp->flags;
1990
1991 /* Copy source LSP's nhlfes, and capture 'best' nhlfe */
ee70f629 1992 frr_each(nhlfe_list, &lsp->nhlfe_list, nhlfe) {
16c628de
MS
1993 /* Not sure if this is meaningful... */
1994 if (nhlfe->nexthop == NULL)
1995 continue;
1996
cd4bb96f
MS
1997 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp), nhlfe->type,
1998 nhlfe->nexthop);
16c628de
MS
1999 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2000 ret = ENOMEM;
2001 break;
2002 }
2003
2004 /* Need to copy flags too */
2005 new_nhlfe->flags = nhlfe->flags;
2006 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2007
2008 if (nhlfe == lsp->best_nhlfe)
2009 ctx->u.lsp.best_nhlfe = new_nhlfe;
2010 }
2011
cd4bb96f
MS
2012 if (ret != AOK)
2013 goto done;
2014
2015 /* Capture backup nhlfes/nexthops */
2016 frr_each(nhlfe_list, &lsp->backup_nhlfe_list, nhlfe) {
2017 /* Not sure if this is meaningful... */
2018 if (nhlfe->nexthop == NULL)
2019 continue;
2020
2021 new_nhlfe = zebra_mpls_lsp_add_backup_nh(&(ctx->u.lsp),
2022 nhlfe->type,
2023 nhlfe->nexthop);
2024 if (new_nhlfe == NULL || new_nhlfe->nexthop == NULL) {
2025 ret = ENOMEM;
2026 break;
2027 }
2028
2029 /* Need to copy flags too */
2030 new_nhlfe->flags = nhlfe->flags;
2031 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2032 }
2033
16c628de
MS
2034 /* On error the ctx will be cleaned-up, so we don't need to
2035 * deal with any allocated nhlfe or nexthop structs here.
2036 */
cd4bb96f 2037done:
16c628de
MS
2038
2039 return ret;
2040}
2041
97d8d05a
MS
2042/*
2043 * Capture information for an LSP update in a dplane context.
2044 */
2045static int dplane_ctx_pw_init(struct zebra_dplane_ctx *ctx,
2046 enum dplane_op_e op,
2047 struct zebra_pw *pw)
2048{
09cd307c
MS
2049 struct prefix p;
2050 afi_t afi;
2051 struct route_table *table;
2052 struct route_node *rn;
2053 struct route_entry *re;
0024ea8e 2054 const struct nexthop_group *nhg;
09cd307c 2055
97d8d05a
MS
2056 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2057 zlog_debug("init dplane ctx %s: pw '%s', loc %u, rem %u",
2058 dplane_op2str(op), pw->ifname, pw->local_label,
2059 pw->remote_label);
2060
2061 ctx->zd_op = op;
2062 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2063
2064 /* Capture namespace info: no netlink support as of 12/18,
2065 * but just in case...
2066 */
2067 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT), false);
2068
2069 memset(&ctx->u.pw, 0, sizeof(ctx->u.pw));
2070
2071 /* This name appears to be c-string, so we use string copy. */
7c7ef4a8 2072 strlcpy(ctx->zd_ifname, pw->ifname, sizeof(ctx->zd_ifname));
16d69787 2073
9bd9717b 2074 ctx->zd_vrf_id = pw->vrf_id;
7c7ef4a8 2075 ctx->zd_ifindex = pw->ifindex;
97d8d05a
MS
2076 ctx->u.pw.type = pw->type;
2077 ctx->u.pw.af = pw->af;
2078 ctx->u.pw.local_label = pw->local_label;
2079 ctx->u.pw.remote_label = pw->remote_label;
2080 ctx->u.pw.flags = pw->flags;
2081
16d69787 2082 ctx->u.pw.dest = pw->nexthop;
97d8d05a
MS
2083
2084 ctx->u.pw.fields = pw->data;
2085
09cd307c
MS
2086 /* Capture nexthop info for the pw destination. We need to look
2087 * up and use zebra datastructs, but we're running in the zebra
2088 * pthread here so that should be ok.
2089 */
2090 memcpy(&p.u, &pw->nexthop, sizeof(pw->nexthop));
2091 p.family = pw->af;
2092 p.prefixlen = ((pw->af == AF_INET) ?
2093 IPV4_MAX_PREFIXLEN : IPV6_MAX_PREFIXLEN);
2094
2095 afi = (pw->af == AF_INET) ? AFI_IP : AFI_IP6;
2096 table = zebra_vrf_table(afi, SAFI_UNICAST, pw->vrf_id);
2097 if (table) {
2098 rn = route_node_match(table, &p);
2099 if (rn) {
2100 RNODE_FOREACH_RE(rn, re) {
2101 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED))
2102 break;
2103 }
2104
0024ea8e
MS
2105 if (re) {
2106 nhg = rib_get_fib_nhg(re);
09cd307c 2107 copy_nexthops(&(ctx->u.pw.nhg.nexthop),
0024ea8e
MS
2108 nhg->nexthop, NULL);
2109 }
09cd307c
MS
2110 route_unlock_node(rn);
2111 }
2112 }
2113
9f2d0354 2114 return AOK;
97d8d05a
MS
2115}
2116
f62e5480
JU
2117/**
2118 * dplane_ctx_rule_init_single() - Initialize a dataplane representation of a
2119 * PBR rule.
2120 *
2121 * @dplane_rule: Dataplane internal representation of a rule
2122 * @rule: PBR rule
2123 */
2124static void dplane_ctx_rule_init_single(struct dplane_ctx_rule *dplane_rule,
2125 struct zebra_pbr_rule *rule)
2126{
2127 dplane_rule->priority = rule->rule.priority;
2128 dplane_rule->table = rule->rule.action.table;
2129
2130 dplane_rule->filter_bm = rule->rule.filter.filter_bm;
2131 dplane_rule->fwmark = rule->rule.filter.fwmark;
2132 prefix_copy(&(dplane_rule->dst_ip), &rule->rule.filter.dst_ip);
2133 prefix_copy(&(dplane_rule->src_ip), &rule->rule.filter.src_ip);
2134}
2135
2136/**
2137 * dplane_ctx_rule_init() - Initialize a context block for a PBR rule update.
2138 *
2139 * @ctx: Dataplane context to init
2140 * @op: Operation being performed
2141 * @new_rule: PBR rule
2142 *
2143 * Return: Result status
2144 */
2145static int dplane_ctx_rule_init(struct zebra_dplane_ctx *ctx,
2146 enum dplane_op_e op,
2147 struct zebra_pbr_rule *new_rule,
2148 struct zebra_pbr_rule *old_rule)
2149{
2150 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2151 char buf1[PREFIX_STRLEN];
2152 char buf2[PREFIX_STRLEN];
2153
2154 zlog_debug(
2155 "init dplane ctx %s: IF %s(%u) Prio %u Fwmark %u Src %s Dst %s Table %u",
2156 dplane_op2str(op), new_rule->ifname,
2157 new_rule->rule.ifindex, new_rule->rule.priority,
2158 new_rule->rule.filter.fwmark,
2159 prefix2str(&new_rule->rule.filter.src_ip, buf1,
2160 sizeof(buf1)),
2161 prefix2str(&new_rule->rule.filter.dst_ip, buf2,
2162 sizeof(buf2)),
2163 new_rule->rule.action.table);
2164 }
2165
2166 ctx->zd_op = op;
2167 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2168
2169 dplane_ctx_ns_init(ctx, zebra_ns_lookup(NS_DEFAULT),
2170 op == DPLANE_OP_RULE_UPDATE);
2171
2172 ctx->zd_vrf_id = new_rule->vrf_id;
2173 memcpy(ctx->zd_ifname, new_rule->ifname, sizeof(new_rule->ifname));
2174 ctx->zd_ifindex = new_rule->rule.ifindex;
2175
2176 ctx->u.rule.sock = new_rule->sock;
2177 ctx->u.rule.unique = new_rule->rule.unique;
2178 ctx->u.rule.seq = new_rule->rule.seq;
2179
2180 dplane_ctx_rule_init_single(&ctx->u.rule.new, new_rule);
2181 if (op == DPLANE_OP_RULE_UPDATE)
2182 dplane_ctx_rule_init_single(&ctx->u.rule.old, old_rule);
2183
2184 return AOK;
2185}
2186
7cdb1a84 2187/*
3fe4ccc4 2188 * Enqueue a new update,
16c628de 2189 * and ensure an event is active for the dataplane pthread.
7cdb1a84 2190 */
3fe4ccc4 2191static int dplane_update_enqueue(struct zebra_dplane_ctx *ctx)
7cdb1a84
MS
2192{
2193 int ret = EINVAL;
91f16812 2194 uint32_t high, curr;
7cdb1a84 2195
16c628de 2196 /* Enqueue for processing by the dataplane pthread */
7cdb1a84
MS
2197 DPLANE_LOCK();
2198 {
3fe4ccc4 2199 TAILQ_INSERT_TAIL(&zdplane_info.dg_update_ctx_q, ctx,
25779064 2200 zd_q_entries);
7cdb1a84
MS
2201 }
2202 DPLANE_UNLOCK();
2203
e3d9208a 2204 curr = atomic_fetch_add_explicit(
e07e9549 2205 &(zdplane_info.dg_routes_queued),
e07e9549 2206 1, memory_order_seq_cst);
91f16812 2207
e3d9208a
MS
2208 curr++; /* We got the pre-incremented value */
2209
91f16812
MS
2210 /* Maybe update high-water counter also */
2211 high = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
2212 memory_order_seq_cst);
2213 while (high < curr) {
2214 if (atomic_compare_exchange_weak_explicit(
2215 &zdplane_info.dg_routes_queued_max,
2216 &high, curr,
2217 memory_order_seq_cst,
2218 memory_order_seq_cst))
2219 break;
2220 }
2221
7cdb1a84 2222 /* Ensure that an event for the dataplane thread is active */
c831033f 2223 ret = dplane_provider_work_ready();
7cdb1a84 2224
5709131c 2225 return ret;
7cdb1a84
MS
2226}
2227
7cdb1a84
MS
2228/*
2229 * Utility that prepares a route update and enqueues it for processing
2230 */
655d681a
MS
2231static enum zebra_dplane_result
2232dplane_route_update_internal(struct route_node *rn,
2233 struct route_entry *re,
2234 struct route_entry *old_re,
5709131c 2235 enum dplane_op_e op)
7cdb1a84 2236{
655d681a 2237 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2238 int ret = EINVAL;
25779064 2239 struct zebra_dplane_ctx *ctx = NULL;
7cdb1a84
MS
2240
2241 /* Obtain context block */
2242 ctx = dplane_ctx_alloc();
7cdb1a84
MS
2243
2244 /* Init context with info from zebra data structs */
2245 ret = dplane_ctx_route_init(ctx, op, rn, re);
2246 if (ret == AOK) {
2247 /* Capture some extra info for update case
2248 * where there's a different 'old' route.
2249 */
b8e0423d
MS
2250 if ((op == DPLANE_OP_ROUTE_UPDATE) &&
2251 old_re && (old_re != re)) {
7cdb1a84
MS
2252 ctx->zd_is_update = true;
2253
1485bbe7
DS
2254 old_re->dplane_sequence =
2255 zebra_router_get_next_sequence();
7cdb1a84
MS
2256 ctx->zd_old_seq = old_re->dplane_sequence;
2257
0f461727
MS
2258 ctx->u.rinfo.zd_old_tag = old_re->tag;
2259 ctx->u.rinfo.zd_old_type = old_re->type;
2260 ctx->u.rinfo.zd_old_instance = old_re->instance;
2261 ctx->u.rinfo.zd_old_distance = old_re->distance;
2262 ctx->u.rinfo.zd_old_metric = old_re->metric;
01ce7cba
MS
2263
2264#ifndef HAVE_NETLINK
f183e380
MS
2265 /* For bsd, capture previous re's nexthops too, sigh.
2266 * We'll need these to do per-nexthop deletes.
2267 */
0f461727 2268 copy_nexthops(&(ctx->u.rinfo.zd_old_ng.nexthop),
c415d895 2269 old_re->nhe->nhg.nexthop, NULL);
1d48702e
MS
2270
2271 if (zebra_nhg_get_backup_nhg(old_re->nhe) != NULL) {
2272 struct nexthop_group *nhg;
2273 struct nexthop **nh;
2274
2275 nhg = zebra_nhg_get_backup_nhg(old_re->nhe);
2276 nh = &(ctx->u.rinfo.old_backup_ng.nexthop);
2277
2278 if (nhg->nexthop)
2279 copy_nexthops(nh, nhg->nexthop, NULL);
2280 }
01ce7cba 2281#endif /* !HAVE_NETLINK */
7cdb1a84
MS
2282 }
2283
2284 /* Enqueue context for processing */
3fe4ccc4 2285 ret = dplane_update_enqueue(ctx);
7cdb1a84
MS
2286 }
2287
91f16812 2288 /* Update counter */
25779064 2289 atomic_fetch_add_explicit(&zdplane_info.dg_routes_in, 1,
1d11b21f
MS
2290 memory_order_relaxed);
2291
91f16812 2292 if (ret == AOK)
655d681a 2293 result = ZEBRA_DPLANE_REQUEST_QUEUED;
16c628de 2294 else {
6df59152
SW
2295 if (ret == ENOENT)
2296 result = ZEBRA_DPLANE_REQUEST_SUCCESS;
2297 else
2298 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
2299 1, memory_order_relaxed);
16c628de
MS
2300 if (ctx)
2301 dplane_ctx_free(&ctx);
1d11b21f 2302 }
7cdb1a84 2303
5709131c 2304 return result;
7cdb1a84
MS
2305}
2306
f820d025
SW
2307/**
2308 * dplane_nexthop_update_internal() - Helper for enqueuing nexthop changes
2309 *
2310 * @nhe: Nexthop group hash entry where the change occured
2311 * @op: The operation to be enqued
2312 *
2313 * Return: Result of the change
2314 */
2315static enum zebra_dplane_result
2316dplane_nexthop_update_internal(struct nhg_hash_entry *nhe, enum dplane_op_e op)
2317{
2318 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2319 int ret = EINVAL;
2320 struct zebra_dplane_ctx *ctx = NULL;
2321
2322 /* Obtain context block */
2323 ctx = dplane_ctx_alloc();
2324 if (!ctx) {
2325 ret = ENOMEM;
2326 goto done;
2327 }
2328
2329 ret = dplane_ctx_nexthop_init(ctx, op, nhe);
2d3c57e6 2330 if (ret == AOK)
f820d025 2331 ret = dplane_update_enqueue(ctx);
2d3c57e6 2332
f820d025
SW
2333done:
2334 /* Update counter */
2335 atomic_fetch_add_explicit(&zdplane_info.dg_nexthops_in, 1,
2336 memory_order_relaxed);
2337
2338 if (ret == AOK)
2339 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2340 else {
81505946
SW
2341 atomic_fetch_add_explicit(&zdplane_info.dg_nexthop_errors, 1,
2342 memory_order_relaxed);
f820d025
SW
2343 if (ctx)
2344 dplane_ctx_free(&ctx);
2345 }
2346
2347 return result;
2348}
2349
7cdb1a84
MS
2350/*
2351 * Enqueue a route 'add' for the dataplane.
2352 */
655d681a
MS
2353enum zebra_dplane_result dplane_route_add(struct route_node *rn,
2354 struct route_entry *re)
7cdb1a84 2355{
655d681a 2356 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2357
5709131c 2358 if (rn == NULL || re == NULL)
7cdb1a84 2359 goto done;
7cdb1a84
MS
2360
2361 ret = dplane_route_update_internal(rn, re, NULL,
2362 DPLANE_OP_ROUTE_INSTALL);
2363
2364done:
5709131c 2365 return ret;
7cdb1a84
MS
2366}
2367
2368/*
2369 * Enqueue a route update for the dataplane.
2370 */
655d681a
MS
2371enum zebra_dplane_result dplane_route_update(struct route_node *rn,
2372 struct route_entry *re,
2373 struct route_entry *old_re)
7cdb1a84 2374{
655d681a 2375 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2376
5709131c 2377 if (rn == NULL || re == NULL)
7cdb1a84 2378 goto done;
7cdb1a84
MS
2379
2380 ret = dplane_route_update_internal(rn, re, old_re,
2381 DPLANE_OP_ROUTE_UPDATE);
7cdb1a84 2382done:
5709131c 2383 return ret;
7cdb1a84
MS
2384}
2385
2386/*
2387 * Enqueue a route removal for the dataplane.
2388 */
655d681a
MS
2389enum zebra_dplane_result dplane_route_delete(struct route_node *rn,
2390 struct route_entry *re)
7cdb1a84 2391{
655d681a 2392 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
7cdb1a84 2393
5709131c 2394 if (rn == NULL || re == NULL)
7cdb1a84 2395 goto done;
7cdb1a84
MS
2396
2397 ret = dplane_route_update_internal(rn, re, NULL,
2398 DPLANE_OP_ROUTE_DELETE);
2399
2400done:
5709131c 2401 return ret;
7cdb1a84
MS
2402}
2403
cf363e1b
MS
2404/*
2405 * Notify the dplane when system/connected routes change.
2406 */
2407enum zebra_dplane_result dplane_sys_route_add(struct route_node *rn,
2408 struct route_entry *re)
2409{
2410 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2411
2412 /* Ignore this event unless a provider plugin has requested it. */
2413 if (!zdplane_info.dg_sys_route_notifs) {
2414 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2415 goto done;
2416 }
2417
2418 if (rn == NULL || re == NULL)
2419 goto done;
2420
2421 ret = dplane_route_update_internal(rn, re, NULL,
2422 DPLANE_OP_SYS_ROUTE_ADD);
2423
2424done:
2425 return ret;
2426}
2427
2428/*
2429 * Notify the dplane when system/connected routes are deleted.
2430 */
2431enum zebra_dplane_result dplane_sys_route_del(struct route_node *rn,
2432 struct route_entry *re)
2433{
2434 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2435
2436 /* Ignore this event unless a provider plugin has requested it. */
2437 if (!zdplane_info.dg_sys_route_notifs) {
2438 ret = ZEBRA_DPLANE_REQUEST_SUCCESS;
2439 goto done;
2440 }
2441
2442 if (rn == NULL || re == NULL)
2443 goto done;
2444
2445 ret = dplane_route_update_internal(rn, re, NULL,
2446 DPLANE_OP_SYS_ROUTE_DELETE);
2447
2448done:
2449 return ret;
2450}
2451
188a00e0
MS
2452/*
2453 * Update from an async notification, to bring other fibs up-to-date.
2454 */
2455enum zebra_dplane_result
2456dplane_route_notif_update(struct route_node *rn,
2457 struct route_entry *re,
2458 enum dplane_op_e op,
2459 struct zebra_dplane_ctx *ctx)
2460{
2461 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2462 struct zebra_dplane_ctx *new_ctx = NULL;
2463 struct nexthop *nexthop;
2464
2465 if (rn == NULL || re == NULL)
2466 goto done;
2467
2468 new_ctx = dplane_ctx_alloc();
2469 if (new_ctx == NULL)
2470 goto done;
2471
2472 /* Init context with info from zebra data structs */
2473 dplane_ctx_route_init(new_ctx, op, rn, re);
2474
2475 /* For add/update, need to adjust the nexthops so that we match
2476 * the notification state, which may not be the route-entry/RIB
2477 * state.
2478 */
2479 if (op == DPLANE_OP_ROUTE_UPDATE ||
2480 op == DPLANE_OP_ROUTE_INSTALL) {
2481
2482 nexthops_free(new_ctx->u.rinfo.zd_ng.nexthop);
2483 new_ctx->u.rinfo.zd_ng.nexthop = NULL;
2484
2485 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
4db01e79 2486 (rib_get_fib_nhg(re))->nexthop, NULL);
188a00e0 2487
00a9b150
MS
2488 /* Check for backup nexthops also */
2489 if (re->fib_backup_ng.nexthop) {
2490 copy_nexthops(&(new_ctx->u.rinfo.zd_ng.nexthop),
2491 re->fib_backup_ng.nexthop, NULL);
2492 }
2493
188a00e0
MS
2494 for (ALL_NEXTHOPS(new_ctx->u.rinfo.zd_ng, nexthop))
2495 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2496
2497 }
2498
2499 /* Capture info about the source of the notification, in 'ctx' */
2500 dplane_ctx_set_notif_provider(new_ctx,
2501 dplane_ctx_get_notif_provider(ctx));
2502
3fe4ccc4 2503 dplane_update_enqueue(new_ctx);
188a00e0
MS
2504
2505 ret = ZEBRA_DPLANE_REQUEST_QUEUED;
2506
2507done:
2508 return ret;
2509}
2510
f820d025
SW
2511/*
2512 * Enqueue a nexthop add for the dataplane.
2513 */
2514enum zebra_dplane_result dplane_nexthop_add(struct nhg_hash_entry *nhe)
2515{
2516 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2517
2518 if (nhe)
2519 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_INSTALL);
2520 return ret;
2521}
2522
2523/*
2524 * Enqueue a nexthop update for the dataplane.
81505946
SW
2525 *
2526 * Might not need this func since zebra's nexthop objects should be immutable?
f820d025
SW
2527 */
2528enum zebra_dplane_result dplane_nexthop_update(struct nhg_hash_entry *nhe)
2529{
2530 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2531
2532 if (nhe)
2533 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_UPDATE);
2534 return ret;
2535}
2536
2537/*
2538 * Enqueue a nexthop removal for the dataplane.
2539 */
2540enum zebra_dplane_result dplane_nexthop_delete(struct nhg_hash_entry *nhe)
2541{
2542 enum zebra_dplane_result ret = ZEBRA_DPLANE_REQUEST_FAILURE;
2543
2544 if (nhe)
2545 ret = dplane_nexthop_update_internal(nhe, DPLANE_OP_NH_DELETE);
2546
2547 return ret;
2548}
2549
16c628de
MS
2550/*
2551 * Enqueue LSP add for the dataplane.
2552 */
2553enum zebra_dplane_result dplane_lsp_add(zebra_lsp_t *lsp)
2554{
2555 enum zebra_dplane_result ret =
2556 lsp_update_internal(lsp, DPLANE_OP_LSP_INSTALL);
2557
2558 return ret;
2559}
2560
2561/*
2562 * Enqueue LSP update for the dataplane.
2563 */
2564enum zebra_dplane_result dplane_lsp_update(zebra_lsp_t *lsp)
2565{
2566 enum zebra_dplane_result ret =
2567 lsp_update_internal(lsp, DPLANE_OP_LSP_UPDATE);
2568
2569 return ret;
2570}
2571
2572/*
2573 * Enqueue LSP delete for the dataplane.
2574 */
2575enum zebra_dplane_result dplane_lsp_delete(zebra_lsp_t *lsp)
2576{
2577 enum zebra_dplane_result ret =
2578 lsp_update_internal(lsp, DPLANE_OP_LSP_DELETE);
2579
2580 return ret;
2581}
2582
188a00e0
MS
2583/* Update or un-install resulting from an async notification */
2584enum zebra_dplane_result
2585dplane_lsp_notif_update(zebra_lsp_t *lsp,
2586 enum dplane_op_e op,
2587 struct zebra_dplane_ctx *notif_ctx)
2588{
2589 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2590 int ret = EINVAL;
2591 struct zebra_dplane_ctx *ctx = NULL;
00a9b150
MS
2592 struct nhlfe_list_head *head;
2593 zebra_nhlfe_t *nhlfe, *new_nhlfe;
188a00e0
MS
2594
2595 /* Obtain context block */
2596 ctx = dplane_ctx_alloc();
2597 if (ctx == NULL) {
2598 ret = ENOMEM;
2599 goto done;
2600 }
2601
00a9b150 2602 /* Copy info from zebra LSP */
188a00e0
MS
2603 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2604 if (ret != AOK)
2605 goto done;
2606
00a9b150
MS
2607 /* Add any installed backup nhlfes */
2608 head = &(ctx->u.lsp.backup_nhlfe_list);
2609 frr_each(nhlfe_list, head, nhlfe) {
2610
2611 if (CHECK_FLAG(nhlfe->flags, NHLFE_FLAG_INSTALLED) &&
2612 CHECK_FLAG(nhlfe->nexthop->flags, NEXTHOP_FLAG_FIB)) {
2613 new_nhlfe = zebra_mpls_lsp_add_nh(&(ctx->u.lsp),
2614 nhlfe->type,
2615 nhlfe->nexthop);
2616
2617 /* Need to copy flags too */
2618 new_nhlfe->flags = nhlfe->flags;
2619 new_nhlfe->nexthop->flags = nhlfe->nexthop->flags;
2620 }
2621 }
2622
188a00e0
MS
2623 /* Capture info about the source of the notification */
2624 dplane_ctx_set_notif_provider(
2625 ctx,
2626 dplane_ctx_get_notif_provider(notif_ctx));
2627
3fe4ccc4 2628 ret = dplane_update_enqueue(ctx);
188a00e0
MS
2629
2630done:
2631 /* Update counter */
2632 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2633 memory_order_relaxed);
2634
2635 if (ret == AOK)
2636 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2637 else {
2638 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2639 memory_order_relaxed);
2640 if (ctx)
2641 dplane_ctx_free(&ctx);
2642 }
2643 return result;
2644}
2645
97d8d05a
MS
2646/*
2647 * Enqueue pseudowire install for the dataplane.
2648 */
2649enum zebra_dplane_result dplane_pw_install(struct zebra_pw *pw)
2650{
2651 return pw_update_internal(pw, DPLANE_OP_PW_INSTALL);
2652}
2653
2654/*
2655 * Enqueue pseudowire un-install for the dataplane.
2656 */
2657enum zebra_dplane_result dplane_pw_uninstall(struct zebra_pw *pw)
2658{
2659 return pw_update_internal(pw, DPLANE_OP_PW_UNINSTALL);
2660}
2661
16c628de
MS
2662/*
2663 * Common internal LSP update utility
2664 */
2665static enum zebra_dplane_result lsp_update_internal(zebra_lsp_t *lsp,
2666 enum dplane_op_e op)
2667{
2668 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2669 int ret = EINVAL;
2670 struct zebra_dplane_ctx *ctx = NULL;
2671
2672 /* Obtain context block */
2673 ctx = dplane_ctx_alloc();
16c628de
MS
2674
2675 ret = dplane_ctx_lsp_init(ctx, op, lsp);
2676 if (ret != AOK)
2677 goto done;
2678
3fe4ccc4 2679 ret = dplane_update_enqueue(ctx);
16c628de
MS
2680
2681done:
2682 /* Update counter */
2683 atomic_fetch_add_explicit(&zdplane_info.dg_lsps_in, 1,
2684 memory_order_relaxed);
2685
2686 if (ret == AOK)
2687 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2688 else {
2689 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors, 1,
2690 memory_order_relaxed);
d8e479a3 2691 dplane_ctx_free(&ctx);
16c628de
MS
2692 }
2693
2694 return result;
2695}
2696
97d8d05a
MS
2697/*
2698 * Internal, common handler for pseudowire updates.
2699 */
2700static enum zebra_dplane_result pw_update_internal(struct zebra_pw *pw,
2701 enum dplane_op_e op)
2702{
2703 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2704 int ret;
2705 struct zebra_dplane_ctx *ctx = NULL;
2706
2707 ctx = dplane_ctx_alloc();
97d8d05a
MS
2708
2709 ret = dplane_ctx_pw_init(ctx, op, pw);
2710 if (ret != AOK)
2711 goto done;
2712
3fe4ccc4 2713 ret = dplane_update_enqueue(ctx);
97d8d05a
MS
2714
2715done:
2716 /* Update counter */
2717 atomic_fetch_add_explicit(&zdplane_info.dg_pws_in, 1,
2718 memory_order_relaxed);
2719
2720 if (ret == AOK)
2721 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2722 else {
2723 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
2724 memory_order_relaxed);
d8e479a3 2725 dplane_ctx_free(&ctx);
97d8d05a
MS
2726 }
2727
2728 return result;
2729}
2730
a4a4802a
MS
2731/*
2732 * Enqueue interface address add for the dataplane.
2733 */
2734enum zebra_dplane_result dplane_intf_addr_set(const struct interface *ifp,
2735 const struct connected *ifc)
2736{
64168803
MS
2737#if !defined(HAVE_NETLINK) && defined(HAVE_STRUCT_IFALIASREQ)
2738 /* Extra checks for this OS path. */
2739
2740 /* Don't configure PtP addresses on broadcast ifs or reverse */
2741 if (!(ifp->flags & IFF_POINTOPOINT) != !CONNECTED_PEER(ifc)) {
2742 if (IS_ZEBRA_DEBUG_KERNEL || IS_ZEBRA_DEBUG_DPLANE)
2743 zlog_debug("Failed to set intf addr: mismatch p2p and connected");
2744
2745 return ZEBRA_DPLANE_REQUEST_FAILURE;
2746 }
2747
2748 /* Ensure that no existing installed v4 route conflicts with
2749 * the new interface prefix. This check must be done in the
2750 * zebra pthread context, and any route delete (if needed)
2751 * is enqueued before the interface address programming attempt.
2752 */
2753 if (ifc->address->family == AF_INET) {
2754 struct prefix_ipv4 *p;
2755
2756 p = (struct prefix_ipv4 *)ifc->address;
a36898e7 2757 rib_lookup_and_pushup(p, ifp->vrf_id);
64168803
MS
2758 }
2759#endif
2760
2761 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_INSTALL);
a4a4802a
MS
2762}
2763
2764/*
2765 * Enqueue interface address remove/uninstall for the dataplane.
2766 */
2767enum zebra_dplane_result dplane_intf_addr_unset(const struct interface *ifp,
2768 const struct connected *ifc)
2769{
64168803
MS
2770 return intf_addr_update_internal(ifp, ifc, DPLANE_OP_ADDR_UNINSTALL);
2771}
2772
2773static enum zebra_dplane_result intf_addr_update_internal(
2774 const struct interface *ifp, const struct connected *ifc,
2775 enum dplane_op_e op)
2776{
2777 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2778 int ret = EINVAL;
2779 struct zebra_dplane_ctx *ctx = NULL;
2780 struct zebra_ns *zns;
2781
2782 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2783 char addr_str[PREFIX_STRLEN];
2784
2785 prefix2str(ifc->address, addr_str, sizeof(addr_str));
2786
2787 zlog_debug("init intf ctx %s: idx %d, addr %u:%s",
a36898e7 2788 dplane_op2str(op), ifp->ifindex, ifp->vrf_id,
64168803
MS
2789 addr_str);
2790 }
2791
2792 ctx = dplane_ctx_alloc();
64168803
MS
2793
2794 ctx->zd_op = op;
2795 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
a36898e7 2796 ctx->zd_vrf_id = ifp->vrf_id;
64168803 2797
a36898e7 2798 zns = zebra_ns_lookup(ifp->vrf_id);
64168803
MS
2799 dplane_ctx_ns_init(ctx, zns, false);
2800
2801 /* Init the interface-addr-specific area */
2802 memset(&ctx->u.intf, 0, sizeof(ctx->u.intf));
2803
7c7ef4a8
MS
2804 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2805 ctx->zd_ifindex = ifp->ifindex;
64168803
MS
2806 ctx->u.intf.prefix = *(ifc->address);
2807
2808 if (if_is_broadcast(ifp))
2809 ctx->u.intf.flags |= DPLANE_INTF_BROADCAST;
2810
2811 if (CONNECTED_PEER(ifc)) {
2812 ctx->u.intf.dest_prefix = *(ifc->destination);
2813 ctx->u.intf.flags |=
2814 (DPLANE_INTF_CONNECTED | DPLANE_INTF_HAS_DEST);
64168803
MS
2815 }
2816
2817 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
2818 ctx->u.intf.flags |= DPLANE_INTF_SECONDARY;
2819
2820 if (ifc->label) {
2821 size_t len;
2822
2823 ctx->u.intf.flags |= DPLANE_INTF_HAS_LABEL;
2824
2825 /* Use embedded buffer if it's adequate; else allocate. */
2826 len = strlen(ifc->label);
2827
2828 if (len < sizeof(ctx->u.intf.label_buf)) {
b7b7bf31 2829 strlcpy(ctx->u.intf.label_buf, ifc->label,
64168803
MS
2830 sizeof(ctx->u.intf.label_buf));
2831 ctx->u.intf.label = ctx->u.intf.label_buf;
2832 } else {
2833 ctx->u.intf.label = strdup(ifc->label);
2834 }
2835 }
2836
3fe4ccc4 2837 ret = dplane_update_enqueue(ctx);
64168803 2838
64168803
MS
2839 /* Increment counter */
2840 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addrs_in, 1,
2841 memory_order_relaxed);
2842
2843 if (ret == AOK)
2844 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2845 else {
2846 /* Error counter */
2847 atomic_fetch_add_explicit(&zdplane_info.dg_intf_addr_errors,
2848 1, memory_order_relaxed);
d8e479a3 2849 dplane_ctx_free(&ctx);
64168803
MS
2850 }
2851
2852 return result;
a4a4802a
MS
2853}
2854
7597ac7b
MS
2855/*
2856 * Enqueue vxlan/evpn mac add (or update).
2857 */
2858enum zebra_dplane_result dplane_mac_add(const struct interface *ifp,
478566d6 2859 const struct interface *bridge_ifp,
7597ac7b
MS
2860 vlanid_t vid,
2861 const struct ethaddr *mac,
2862 struct in_addr vtep_ip,
2863 bool sticky)
2864{
2865 enum zebra_dplane_result result;
2866
2867 /* Use common helper api */
f73a8467
MS
2868 result = mac_update_common(DPLANE_OP_MAC_INSTALL, ifp, bridge_ifp,
2869 vid, mac, vtep_ip, sticky);
7597ac7b
MS
2870 return result;
2871}
2872
2873/*
2874 * Enqueue vxlan/evpn mac delete.
2875 */
2876enum zebra_dplane_result dplane_mac_del(const struct interface *ifp,
478566d6 2877 const struct interface *bridge_ifp,
7597ac7b
MS
2878 vlanid_t vid,
2879 const struct ethaddr *mac,
2880 struct in_addr vtep_ip)
2881{
2882 enum zebra_dplane_result result;
2883
2884 /* Use common helper api */
f73a8467
MS
2885 result = mac_update_common(DPLANE_OP_MAC_DELETE, ifp, bridge_ifp,
2886 vid, mac, vtep_ip, false);
7597ac7b
MS
2887 return result;
2888}
2889
2890/*
f73a8467
MS
2891 * Public api to init an empty context - either newly-allocated or
2892 * reset/cleared - for a MAC update.
7597ac7b 2893 */
f73a8467
MS
2894void dplane_mac_init(struct zebra_dplane_ctx *ctx,
2895 const struct interface *ifp,
2896 const struct interface *br_ifp,
2897 vlanid_t vid,
2898 const struct ethaddr *mac,
2899 struct in_addr vtep_ip,
2900 bool sticky)
7597ac7b 2901{
7597ac7b
MS
2902 struct zebra_ns *zns;
2903
7597ac7b
MS
2904 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
2905 ctx->zd_vrf_id = ifp->vrf_id;
2906
2907 zns = zebra_ns_lookup(ifp->vrf_id);
2908 dplane_ctx_ns_init(ctx, zns, false);
2909
2910 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
2911 ctx->zd_ifindex = ifp->ifindex;
2912
2913 /* Init the mac-specific data area */
2914 memset(&ctx->u.macinfo, 0, sizeof(ctx->u.macinfo));
2915
478566d6 2916 ctx->u.macinfo.br_ifindex = br_ifp->ifindex;
7597ac7b
MS
2917 ctx->u.macinfo.vtep_ip = vtep_ip;
2918 ctx->u.macinfo.mac = *mac;
2919 ctx->u.macinfo.vid = vid;
2920 ctx->u.macinfo.is_sticky = sticky;
f73a8467
MS
2921}
2922
2923/*
2924 * Common helper api for MAC address/vxlan updates
2925 */
2926static enum zebra_dplane_result
2927mac_update_common(enum dplane_op_e op,
2928 const struct interface *ifp,
2929 const struct interface *br_ifp,
2930 vlanid_t vid,
2931 const struct ethaddr *mac,
2932 struct in_addr vtep_ip,
2933 bool sticky)
2934{
2935 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2936 int ret;
2937 struct zebra_dplane_ctx *ctx = NULL;
2938
2939 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
2940 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
2941
2942 zlog_debug("init mac ctx %s: mac %s, ifp %s, vtep %s",
2943 dplane_op2str(op),
2944 prefix_mac2str(mac, buf1, sizeof(buf1)),
2945 ifp->name,
2946 inet_ntop(AF_INET, &vtep_ip, buf2, sizeof(buf2)));
2947 }
2948
2949 ctx = dplane_ctx_alloc();
2950 ctx->zd_op = op;
2951
2952 /* Common init for the ctx */
2953 dplane_mac_init(ctx, ifp, br_ifp, vid, mac, vtep_ip, sticky);
7597ac7b
MS
2954
2955 /* Enqueue for processing on the dplane pthread */
2956 ret = dplane_update_enqueue(ctx);
2957
2958 /* Increment counter */
2959 atomic_fetch_add_explicit(&zdplane_info.dg_macs_in, 1,
2960 memory_order_relaxed);
2961
2962 if (ret == AOK)
2963 result = ZEBRA_DPLANE_REQUEST_QUEUED;
2964 else {
2965 /* Error counter */
2966 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors, 1,
2967 memory_order_relaxed);
2968 dplane_ctx_free(&ctx);
2969 }
2970
2971 return result;
2972}
2973
931fa60c
MS
2974/*
2975 * Enqueue evpn neighbor add for the dataplane.
2976 */
2977enum zebra_dplane_result dplane_neigh_add(const struct interface *ifp,
2978 const struct ipaddr *ip,
2979 const struct ethaddr *mac,
2980 uint32_t flags)
2981{
2982 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
2983
2984 result = neigh_update_internal(DPLANE_OP_NEIGH_INSTALL,
4dd9d11c 2985 ifp, mac, ip, flags, DPLANE_NUD_NOARP);
931fa60c
MS
2986
2987 return result;
2988}
2989
2990/*
2991 * Enqueue evpn neighbor update for the dataplane.
2992 */
2993enum zebra_dplane_result dplane_neigh_update(const struct interface *ifp,
2994 const struct ipaddr *ip,
2995 const struct ethaddr *mac)
2996{
0bbd4ff4 2997 enum zebra_dplane_result result;
931fa60c
MS
2998
2999 result = neigh_update_internal(DPLANE_OP_NEIGH_UPDATE,
3000 ifp, mac, ip, 0, DPLANE_NUD_PROBE);
3001
3002 return result;
3003}
3004
3005/*
3006 * Enqueue evpn neighbor delete for the dataplane.
3007 */
3008enum zebra_dplane_result dplane_neigh_delete(const struct interface *ifp,
3009 const struct ipaddr *ip)
3010{
0bbd4ff4 3011 enum zebra_dplane_result result;
931fa60c
MS
3012
3013 result = neigh_update_internal(DPLANE_OP_NEIGH_DELETE,
3014 ifp, NULL, ip, 0, 0);
3015
3016 return result;
3017}
3018
0bbd4ff4
MS
3019/*
3020 * Enqueue evpn VTEP add for the dataplane.
3021 */
3022enum zebra_dplane_result dplane_vtep_add(const struct interface *ifp,
3023 const struct in_addr *ip,
3024 vni_t vni)
3025{
3026 enum zebra_dplane_result result;
3027 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3028 struct ipaddr addr;
3029
3030 if (IS_ZEBRA_DEBUG_VXLAN)
3031 zlog_debug("Install %s into flood list for VNI %u intf %s(%u)",
3032 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3033
3034 SET_IPADDR_V4(&addr);
3035 addr.ipaddr_v4 = *ip;
3036
3037 result = neigh_update_internal(DPLANE_OP_VTEP_ADD,
3038 ifp, &mac, &addr, 0, 0);
3039
3040 return result;
3041}
3042
3043/*
3044 * Enqueue evpn VTEP add for the dataplane.
3045 */
3046enum zebra_dplane_result dplane_vtep_delete(const struct interface *ifp,
3047 const struct in_addr *ip,
3048 vni_t vni)
3049{
3050 enum zebra_dplane_result result;
3051 struct ethaddr mac = { {0, 0, 0, 0, 0, 0} };
3052 struct ipaddr addr;
3053
3054 if (IS_ZEBRA_DEBUG_VXLAN)
3055 zlog_debug(
3056 "Uninstall %s from flood list for VNI %u intf %s(%u)",
3057 inet_ntoa(*ip), vni, ifp->name, ifp->ifindex);
3058
3059 SET_IPADDR_V4(&addr);
3060 addr.ipaddr_v4 = *ip;
3061
3062 result = neigh_update_internal(DPLANE_OP_VTEP_DELETE,
3063 ifp, &mac, &addr, 0, 0);
3064
3065 return result;
3066}
3067
931fa60c
MS
3068/*
3069 * Common helper api for evpn neighbor updates
3070 */
3071static enum zebra_dplane_result
3072neigh_update_internal(enum dplane_op_e op,
3073 const struct interface *ifp,
3074 const struct ethaddr *mac,
3075 const struct ipaddr *ip,
3076 uint32_t flags, uint16_t state)
3077{
3078 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3079 int ret;
3080 struct zebra_dplane_ctx *ctx = NULL;
3081 struct zebra_ns *zns;
3082
3083 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3084 char buf1[ETHER_ADDR_STRLEN], buf2[PREFIX_STRLEN];
3085
3086 zlog_debug("init neigh ctx %s: ifp %s, mac %s, ip %s",
3087 dplane_op2str(op),
3088 prefix_mac2str(mac, buf1, sizeof(buf1)),
3089 ifp->name,
3090 ipaddr2str(ip, buf2, sizeof(buf2)));
3091 }
3092
3093 ctx = dplane_ctx_alloc();
3094
3095 ctx->zd_op = op;
3096 ctx->zd_status = ZEBRA_DPLANE_REQUEST_SUCCESS;
3097 ctx->zd_vrf_id = ifp->vrf_id;
3098
3099 zns = zebra_ns_lookup(ifp->vrf_id);
3100 dplane_ctx_ns_init(ctx, zns, false);
3101
3102 strlcpy(ctx->zd_ifname, ifp->name, sizeof(ctx->zd_ifname));
3103 ctx->zd_ifindex = ifp->ifindex;
3104
3105 /* Init the neighbor-specific data area */
3106 memset(&ctx->u.neigh, 0, sizeof(ctx->u.neigh));
3107
3108 ctx->u.neigh.ip_addr = *ip;
3109 if (mac)
3110 ctx->u.neigh.mac = *mac;
3111 ctx->u.neigh.flags = flags;
3112 ctx->u.neigh.state = state;
3113
3114 /* Enqueue for processing on the dplane pthread */
3115 ret = dplane_update_enqueue(ctx);
3116
3117 /* Increment counter */
3118 atomic_fetch_add_explicit(&zdplane_info.dg_neighs_in, 1,
3119 memory_order_relaxed);
3120
3121 if (ret == AOK)
3122 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3123 else {
3124 /* Error counter */
3125 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors, 1,
3126 memory_order_relaxed);
3127 dplane_ctx_free(&ctx);
3128 }
3129
3130 return result;
3131}
3132
f62e5480
JU
3133/*
3134 * Common helper api for PBR rule updates
3135 */
3136static enum zebra_dplane_result
3137rule_update_internal(enum dplane_op_e op, struct zebra_pbr_rule *new_rule,
3138 struct zebra_pbr_rule *old_rule)
3139{
3140 enum zebra_dplane_result result = ZEBRA_DPLANE_REQUEST_FAILURE;
3141 struct zebra_dplane_ctx *ctx;
3142 int ret;
3143
3144 ctx = dplane_ctx_alloc();
3145
3146 ret = dplane_ctx_rule_init(ctx, op, new_rule, old_rule);
3147 if (ret != AOK)
3148 goto done;
3149
3150 ret = dplane_update_enqueue(ctx);
3151
3152done:
3153 atomic_fetch_add_explicit(&zdplane_info.dg_rules_in, 1,
3154 memory_order_relaxed);
3155
3156 if (ret == AOK)
3157 result = ZEBRA_DPLANE_REQUEST_QUEUED;
3158 else {
3159 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors, 1,
3160 memory_order_relaxed);
3161 dplane_ctx_free(&ctx);
3162 }
3163
3164 return result;
3165}
3166
3167enum zebra_dplane_result dplane_pbr_rule_add(struct zebra_pbr_rule *rule)
3168{
3169 return rule_update_internal(DPLANE_OP_RULE_ADD, rule, NULL);
3170}
3171
3172enum zebra_dplane_result dplane_pbr_rule_delete(struct zebra_pbr_rule *rule)
3173{
3174 return rule_update_internal(DPLANE_OP_RULE_DELETE, rule, NULL);
3175}
3176
3177enum zebra_dplane_result dplane_pbr_rule_update(struct zebra_pbr_rule *old_rule,
3178 struct zebra_pbr_rule *new_rule)
3179{
3180 return rule_update_internal(DPLANE_OP_RULE_UPDATE, new_rule, old_rule);
3181}
3182
1d11b21f
MS
3183/*
3184 * Handler for 'show dplane'
3185 */
3186int dplane_show_helper(struct vty *vty, bool detailed)
3187{
16c628de
MS
3188 uint64_t queued, queue_max, limit, errs, incoming, yields,
3189 other_errs;
1d11b21f 3190
4dfd7a02 3191 /* Using atomics because counters are being changed in different
c831033f 3192 * pthread contexts.
4dfd7a02 3193 */
25779064 3194 incoming = atomic_load_explicit(&zdplane_info.dg_routes_in,
1d11b21f 3195 memory_order_relaxed);
91f16812
MS
3196 limit = atomic_load_explicit(&zdplane_info.dg_max_queued_updates,
3197 memory_order_relaxed);
25779064 3198 queued = atomic_load_explicit(&zdplane_info.dg_routes_queued,
1d11b21f 3199 memory_order_relaxed);
25779064 3200 queue_max = atomic_load_explicit(&zdplane_info.dg_routes_queued_max,
4dfd7a02 3201 memory_order_relaxed);
25779064 3202 errs = atomic_load_explicit(&zdplane_info.dg_route_errors,
1d11b21f 3203 memory_order_relaxed);
c831033f
MS
3204 yields = atomic_load_explicit(&zdplane_info.dg_update_yields,
3205 memory_order_relaxed);
16c628de
MS
3206 other_errs = atomic_load_explicit(&zdplane_info.dg_other_errors,
3207 memory_order_relaxed);
1d11b21f 3208
c831033f
MS
3209 vty_out(vty, "Zebra dataplane:\nRoute updates: %"PRIu64"\n",
3210 incoming);
1d11b21f 3211 vty_out(vty, "Route update errors: %"PRIu64"\n", errs);
16c628de 3212 vty_out(vty, "Other errors : %"PRIu64"\n", other_errs);
91f16812 3213 vty_out(vty, "Route update queue limit: %"PRIu64"\n", limit);
1d11b21f 3214 vty_out(vty, "Route update queue depth: %"PRIu64"\n", queued);
4dfd7a02 3215 vty_out(vty, "Route update queue max: %"PRIu64"\n", queue_max);
4280d91c
MS
3216 vty_out(vty, "Dplane update yields: %"PRIu64"\n", yields);
3217
3218 incoming = atomic_load_explicit(&zdplane_info.dg_lsps_in,
3219 memory_order_relaxed);
3220 errs = atomic_load_explicit(&zdplane_info.dg_lsp_errors,
3221 memory_order_relaxed);
3222 vty_out(vty, "LSP updates: %"PRIu64"\n", incoming);
3223 vty_out(vty, "LSP update errors: %"PRIu64"\n", errs);
3224
3225 incoming = atomic_load_explicit(&zdplane_info.dg_pws_in,
3226 memory_order_relaxed);
3227 errs = atomic_load_explicit(&zdplane_info.dg_pw_errors,
3228 memory_order_relaxed);
3229 vty_out(vty, "PW updates: %"PRIu64"\n", incoming);
3230 vty_out(vty, "PW update errors: %"PRIu64"\n", errs);
3231
3232 incoming = atomic_load_explicit(&zdplane_info.dg_intf_addrs_in,
3233 memory_order_relaxed);
3234 errs = atomic_load_explicit(&zdplane_info.dg_intf_addr_errors,
3235 memory_order_relaxed);
3236 vty_out(vty, "Intf addr updates: %"PRIu64"\n", incoming);
3237 vty_out(vty, "Intf addr errors: %"PRIu64"\n", errs);
3238
3239 incoming = atomic_load_explicit(&zdplane_info.dg_macs_in,
3240 memory_order_relaxed);
3241 errs = atomic_load_explicit(&zdplane_info.dg_mac_errors,
3242 memory_order_relaxed);
3243 vty_out(vty, "EVPN MAC updates: %"PRIu64"\n", incoming);
3244 vty_out(vty, "EVPN MAC errors: %"PRIu64"\n", errs);
1d11b21f 3245
931fa60c
MS
3246 incoming = atomic_load_explicit(&zdplane_info.dg_neighs_in,
3247 memory_order_relaxed);
3248 errs = atomic_load_explicit(&zdplane_info.dg_neigh_errors,
3249 memory_order_relaxed);
3250 vty_out(vty, "EVPN neigh updates: %"PRIu64"\n", incoming);
3251 vty_out(vty, "EVPN neigh errors: %"PRIu64"\n", errs);
3252
60d8d43b
JU
3253 incoming = atomic_load_explicit(&zdplane_info.dg_rules_in,
3254 memory_order_relaxed);
3255 errs = atomic_load_explicit(&zdplane_info.dg_rule_errors,
3256 memory_order_relaxed);
3257 vty_out(vty, "Rule updates: %" PRIu64 "\n", incoming);
3258 vty_out(vty, "Rule errors: %" PRIu64 "\n", errs);
3259
1d11b21f
MS
3260 return CMD_SUCCESS;
3261}
3262
3263/*
3264 * Handler for 'show dplane providers'
3265 */
3266int dplane_show_provs_helper(struct vty *vty, bool detailed)
3267{
c831033f
MS
3268 struct zebra_dplane_provider *prov;
3269 uint64_t in, in_max, out, out_max;
3270
3271 vty_out(vty, "Zebra dataplane providers:\n");
3272
3273 DPLANE_LOCK();
3274 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
3275 DPLANE_UNLOCK();
3276
3277 /* Show counters, useful info from each registered provider */
3278 while (prov) {
3279
3280 in = atomic_load_explicit(&prov->dp_in_counter,
3281 memory_order_relaxed);
3282 in_max = atomic_load_explicit(&prov->dp_in_max,
3283 memory_order_relaxed);
3284 out = atomic_load_explicit(&prov->dp_out_counter,
3285 memory_order_relaxed);
3286 out_max = atomic_load_explicit(&prov->dp_out_max,
3287 memory_order_relaxed);
3288
c9d17fe8
MS
3289 vty_out(vty, "%s (%u): in: %"PRIu64", q_max: %"PRIu64", "
3290 "out: %"PRIu64", q_max: %"PRIu64"\n",
c831033f
MS
3291 prov->dp_name, prov->dp_id, in, in_max, out, out_max);
3292
3293 DPLANE_LOCK();
3294 prov = TAILQ_NEXT(prov, dp_prov_link);
3295 DPLANE_UNLOCK();
3296 }
1d11b21f
MS
3297
3298 return CMD_SUCCESS;
3299}
3300
f26730e1
MS
3301/*
3302 * Helper for 'show run' etc.
3303 */
3304int dplane_config_write_helper(struct vty *vty)
3305{
3306 if (zdplane_info.dg_max_queued_updates != DPLANE_DEFAULT_MAX_QUEUED)
3307 vty_out(vty, "zebra dplane limit %u\n",
3308 zdplane_info.dg_max_queued_updates);
3309
3310 return 0;
3311}
3312
b8e0423d
MS
3313/*
3314 * Provider registration
3315 */
3316int dplane_provider_register(const char *name,
c831033f
MS
3317 enum dplane_provider_prio prio,
3318 int flags,
1dd4ea8a 3319 int (*start_fp)(struct zebra_dplane_provider *),
4c206c8f
MS
3320 int (*fp)(struct zebra_dplane_provider *),
3321 int (*fini_fp)(struct zebra_dplane_provider *,
3322 bool early),
1ff8a248
MS
3323 void *data,
3324 struct zebra_dplane_provider **prov_p)
b8e0423d
MS
3325{
3326 int ret = 0;
6fb51ccb 3327 struct zebra_dplane_provider *p = NULL, *last;
b8e0423d
MS
3328
3329 /* Validate */
3330 if (fp == NULL) {
3331 ret = EINVAL;
3332 goto done;
3333 }
3334
3335 if (prio <= DPLANE_PRIO_NONE ||
1bcea841 3336 prio > DPLANE_PRIO_LAST) {
b8e0423d
MS
3337 ret = EINVAL;
3338 goto done;
3339 }
3340
3341 /* Allocate and init new provider struct */
25779064 3342 p = XCALLOC(MTYPE_DP_PROV, sizeof(struct zebra_dplane_provider));
b8e0423d 3343
c831033f
MS
3344 pthread_mutex_init(&(p->dp_mutex), NULL);
3345 TAILQ_INIT(&(p->dp_ctx_in_q));
3346 TAILQ_INIT(&(p->dp_ctx_out_q));
b8e0423d 3347
932dbb4d 3348 p->dp_flags = flags;
b8e0423d
MS
3349 p->dp_priority = prio;
3350 p->dp_fp = fp;
1dd4ea8a 3351 p->dp_start = start_fp;
18c37974 3352 p->dp_fini = fini_fp;
c831033f 3353 p->dp_data = data;
18c37974 3354
c831033f 3355 /* Lock - the dplane pthread may be running */
18c37974 3356 DPLANE_LOCK();
b8e0423d 3357
25779064 3358 p->dp_id = ++zdplane_info.dg_provider_id;
b8e0423d 3359
c831033f
MS
3360 if (name)
3361 strlcpy(p->dp_name, name, DPLANE_PROVIDER_NAMELEN);
3362 else
3363 snprintf(p->dp_name, DPLANE_PROVIDER_NAMELEN,
3364 "provider-%u", p->dp_id);
3365
b8e0423d 3366 /* Insert into list ordered by priority */
c831033f 3367 TAILQ_FOREACH(last, &zdplane_info.dg_providers_q, dp_prov_link) {
5709131c 3368 if (last->dp_priority > p->dp_priority)
b8e0423d 3369 break;
b8e0423d
MS
3370 }
3371
5709131c 3372 if (last)
c831033f 3373 TAILQ_INSERT_BEFORE(last, p, dp_prov_link);
5709131c 3374 else
25779064 3375 TAILQ_INSERT_TAIL(&zdplane_info.dg_providers_q, p,
c831033f 3376 dp_prov_link);
b8e0423d 3377
18c37974
MS
3378 /* And unlock */
3379 DPLANE_UNLOCK();
3380
c831033f
MS
3381 if (IS_ZEBRA_DEBUG_DPLANE)
3382 zlog_debug("dplane: registered new provider '%s' (%u), prio %d",
3383 p->dp_name, p->dp_id, p->dp_priority);
3384
b8e0423d 3385done:
1ff8a248
MS
3386 if (prov_p)
3387 *prov_p = p;
3388
5709131c 3389 return ret;
b8e0423d
MS
3390}
3391
c831033f
MS
3392/* Accessors for provider attributes */
3393const char *dplane_provider_get_name(const struct zebra_dplane_provider *prov)
3394{
3395 return prov->dp_name;
3396}
3397
3398uint32_t dplane_provider_get_id(const struct zebra_dplane_provider *prov)
3399{
3400 return prov->dp_id;
3401}
3402
3403void *dplane_provider_get_data(const struct zebra_dplane_provider *prov)
3404{
3405 return prov->dp_data;
3406}
3407
3408int dplane_provider_get_work_limit(const struct zebra_dplane_provider *prov)
3409{
3410 return zdplane_info.dg_updates_per_cycle;
3411}
3412
ad6aad4d
MS
3413/* Lock/unlock a provider's mutex - iff the provider was registered with
3414 * the THREADED flag.
3415 */
3416void dplane_provider_lock(struct zebra_dplane_provider *prov)
3417{
3418 if (dplane_provider_is_threaded(prov))
3419 DPLANE_PROV_LOCK(prov);
3420}
3421
3422void dplane_provider_unlock(struct zebra_dplane_provider *prov)
3423{
3424 if (dplane_provider_is_threaded(prov))
3425 DPLANE_PROV_UNLOCK(prov);
3426}
3427
c831033f
MS
3428/*
3429 * Dequeue and maintain associated counter
3430 */
3431struct zebra_dplane_ctx *dplane_provider_dequeue_in_ctx(
3432 struct zebra_dplane_provider *prov)
3433{
3434 struct zebra_dplane_ctx *ctx = NULL;
3435
ad6aad4d 3436 dplane_provider_lock(prov);
c831033f
MS
3437
3438 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3439 if (ctx) {
3440 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
c9d17fe8
MS
3441
3442 atomic_fetch_sub_explicit(&prov->dp_in_queued, 1,
3443 memory_order_relaxed);
c831033f
MS
3444 }
3445
ad6aad4d 3446 dplane_provider_unlock(prov);
c831033f
MS
3447
3448 return ctx;
3449}
3450
3451/*
3452 * Dequeue work to a list, return count
3453 */
3454int dplane_provider_dequeue_in_list(struct zebra_dplane_provider *prov,
3455 struct dplane_ctx_q *listp)
3456{
3457 int limit, ret;
3458 struct zebra_dplane_ctx *ctx;
3459
3460 limit = zdplane_info.dg_updates_per_cycle;
3461
ad6aad4d 3462 dplane_provider_lock(prov);
c831033f
MS
3463
3464 for (ret = 0; ret < limit; ret++) {
3465 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
3466 if (ctx) {
3467 TAILQ_REMOVE(&(prov->dp_ctx_in_q), ctx, zd_q_entries);
3468
3469 TAILQ_INSERT_TAIL(listp, ctx, zd_q_entries);
3470 } else {
3471 break;
3472 }
3473 }
3474
c9d17fe8
MS
3475 if (ret > 0)
3476 atomic_fetch_sub_explicit(&prov->dp_in_queued, ret,
3477 memory_order_relaxed);
3478
ad6aad4d 3479 dplane_provider_unlock(prov);
c831033f
MS
3480
3481 return ret;
3482}
3483
3484/*
3485 * Enqueue and maintain associated counter
3486 */
3487void dplane_provider_enqueue_out_ctx(struct zebra_dplane_provider *prov,
3488 struct zebra_dplane_ctx *ctx)
3489{
ad6aad4d 3490 dplane_provider_lock(prov);
c831033f
MS
3491
3492 TAILQ_INSERT_TAIL(&(prov->dp_ctx_out_q), ctx,
3493 zd_q_entries);
3494
ad6aad4d 3495 dplane_provider_unlock(prov);
c831033f
MS
3496
3497 atomic_fetch_add_explicit(&(prov->dp_out_counter), 1,
3498 memory_order_relaxed);
3499}
3500
62b8bb7a
MS
3501/*
3502 * Accessor for provider object
3503 */
c831033f
MS
3504bool dplane_provider_is_threaded(const struct zebra_dplane_provider *prov)
3505{
3506 return (prov->dp_flags & DPLANE_PROV_FLAG_THREADED);
3507}
3508
62b8bb7a
MS
3509/*
3510 * Internal helper that copies information from a zebra ns object; this is
3511 * called in the zebra main pthread context as part of dplane ctx init.
3512 */
3513static void dplane_info_from_zns(struct zebra_dplane_info *ns_info,
3514 struct zebra_ns *zns)
3515{
3516 ns_info->ns_id = zns->ns_id;
3517
3518#if defined(HAVE_NETLINK)
3519 ns_info->is_cmd = true;
3520 ns_info->nls = zns->netlink_dplane;
3521#endif /* NETLINK */
3522}
3523
c831033f
MS
3524/*
3525 * Provider api to signal that work/events are available
3526 * for the dataplane pthread.
3527 */
3528int dplane_provider_work_ready(void)
3529{
e5a60d82
MS
3530 /* Note that during zebra startup, we may be offered work before
3531 * the dataplane pthread (and thread-master) are ready. We want to
3532 * enqueue the work, but the event-scheduling machinery may not be
3533 * available.
3534 */
3535 if (zdplane_info.dg_run) {
3536 thread_add_event(zdplane_info.dg_master,
3537 dplane_thread_loop, NULL, 0,
3538 &zdplane_info.dg_t_update);
3539 }
c831033f
MS
3540
3541 return AOK;
3542}
3543
593e4eb1
MS
3544/*
3545 * Enqueue a context directly to zebra main.
3546 */
3547void dplane_provider_enqueue_to_zebra(struct zebra_dplane_ctx *ctx)
3548{
3549 struct dplane_ctx_q temp_list;
3550
3551 /* Zebra's api takes a list, so we need to use a temporary list */
3552 TAILQ_INIT(&temp_list);
3553
3554 TAILQ_INSERT_TAIL(&temp_list, ctx, zd_q_entries);
3555 (zdplane_info.dg_results_cb)(&temp_list);
3556}
3557
7cdb1a84 3558/*
c831033f 3559 * Kernel dataplane provider
7cdb1a84 3560 */
c831033f 3561
16c628de
MS
3562/*
3563 * Handler for kernel LSP updates
3564 */
3565static enum zebra_dplane_result
3566kernel_dplane_lsp_update(struct zebra_dplane_ctx *ctx)
3567{
2f74a82a 3568 return kernel_lsp_update(ctx);
16c628de
MS
3569}
3570
c10a646d
MS
3571/*
3572 * Handler for kernel pseudowire updates
3573 */
3574static enum zebra_dplane_result
3575kernel_dplane_pw_update(struct zebra_dplane_ctx *ctx)
3576{
c10a646d
MS
3577 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3578 zlog_debug("Dplane pw %s: op %s af %d loc: %u rem: %u",
7c7ef4a8 3579 dplane_ctx_get_ifname(ctx),
c10a646d
MS
3580 dplane_op2str(ctx->zd_op),
3581 dplane_ctx_get_pw_af(ctx),
3582 dplane_ctx_get_pw_local_label(ctx),
3583 dplane_ctx_get_pw_remote_label(ctx));
3584
2f74a82a 3585 return kernel_pw_update(ctx);
c10a646d
MS
3586}
3587
16c628de
MS
3588/*
3589 * Handler for kernel route updates
3590 */
3591static enum zebra_dplane_result
3592kernel_dplane_route_update(struct zebra_dplane_ctx *ctx)
3593{
16c628de
MS
3594 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3595 char dest_str[PREFIX_STRLEN];
3596
3597 prefix2str(dplane_ctx_get_dest(ctx),
3598 dest_str, sizeof(dest_str));
3599
3600 zlog_debug("%u:%s Dplane route update ctx %p op %s",
3601 dplane_ctx_get_vrf(ctx), dest_str,
3602 ctx, dplane_op2str(dplane_ctx_get_op(ctx)));
3603 }
3604
2f74a82a 3605 return kernel_route_update(ctx);
16c628de
MS
3606}
3607
64168803
MS
3608/*
3609 * Handler for kernel-facing interface address updates
3610 */
3611static enum zebra_dplane_result
3612kernel_dplane_address_update(struct zebra_dplane_ctx *ctx)
3613{
64168803
MS
3614 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3615 char dest_str[PREFIX_STRLEN];
3616
3617 prefix2str(dplane_ctx_get_intf_addr(ctx), dest_str,
3618 sizeof(dest_str));
3619
3620 zlog_debug("Dplane intf %s, idx %u, addr %s",
3621 dplane_op2str(dplane_ctx_get_op(ctx)),
3622 dplane_ctx_get_ifindex(ctx), dest_str);
3623 }
3624
2f74a82a 3625 return kernel_address_update_ctx(ctx);
64168803
MS
3626}
3627
f820d025
SW
3628/**
3629 * kernel_dplane_nexthop_update() - Handler for kernel nexthop updates
3630 *
3631 * @ctx: Dataplane context
3632 *
3633 * Return: Dataplane result flag
3634 */
3635static enum zebra_dplane_result
3636kernel_dplane_nexthop_update(struct zebra_dplane_ctx *ctx)
3637{
f820d025
SW
3638 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3639 zlog_debug("ID (%u) Dplane nexthop update ctx %p op %s",
0c8215cb 3640 dplane_ctx_get_nhe_id(ctx), ctx,
f820d025
SW
3641 dplane_op2str(dplane_ctx_get_op(ctx)));
3642 }
3643
2f74a82a 3644 return kernel_nexthop_update(ctx);
f820d025
SW
3645}
3646
036d93c0 3647/*
931fa60c 3648 * Handler for kernel-facing EVPN MAC address updates
036d93c0
MS
3649 */
3650static enum zebra_dplane_result
3651kernel_dplane_mac_update(struct zebra_dplane_ctx *ctx)
3652{
036d93c0
MS
3653 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3654 char buf[ETHER_ADDR_STRLEN];
3655
3656 prefix_mac2str(dplane_ctx_mac_get_addr(ctx), buf,
3657 sizeof(buf));
3658
3659 zlog_debug("Dplane %s, mac %s, ifindex %u",
3660 dplane_op2str(dplane_ctx_get_op(ctx)),
3661 buf, dplane_ctx_get_ifindex(ctx));
3662 }
3663
2f74a82a 3664 return kernel_mac_update_ctx(ctx);
036d93c0
MS
3665}
3666
931fa60c
MS
3667/*
3668 * Handler for kernel-facing EVPN neighbor updates
3669 */
3670static enum zebra_dplane_result
3671kernel_dplane_neigh_update(struct zebra_dplane_ctx *ctx)
3672{
931fa60c
MS
3673 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL) {
3674 char buf[PREFIX_STRLEN];
3675
3676 ipaddr2str(dplane_ctx_neigh_get_ipaddr(ctx), buf,
3677 sizeof(buf));
3678
3679 zlog_debug("Dplane %s, ip %s, ifindex %u",
3680 dplane_op2str(dplane_ctx_get_op(ctx)),
3681 buf, dplane_ctx_get_ifindex(ctx));
3682 }
3683
2f74a82a 3684 return kernel_neigh_update_ctx(ctx);
931fa60c
MS
3685}
3686
f62e5480
JU
3687/*
3688 * Handler for kernel PBR rule updates
3689 */
3690static enum zebra_dplane_result
3691kernel_dplane_rule_update(struct zebra_dplane_ctx *ctx)
3692{
f62e5480
JU
3693 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3694 zlog_debug("Dplane rule update op %s, if %s(%u), ctx %p",
3695 dplane_op2str(dplane_ctx_get_op(ctx)),
3696 dplane_ctx_get_ifname(ctx),
3697 dplane_ctx_get_ifindex(ctx), ctx);
3698
2f74a82a
JU
3699 return kernel_pbr_rule_update(ctx);
3700}
f62e5480 3701
2f74a82a
JU
3702static void kernel_dplane_handle_result(struct zebra_dplane_ctx *ctx,
3703 enum zebra_dplane_result res)
3704{
3705 switch (dplane_ctx_get_op(ctx)) {
3706
3707 case DPLANE_OP_ROUTE_INSTALL:
3708 case DPLANE_OP_ROUTE_UPDATE:
3709 case DPLANE_OP_ROUTE_DELETE:
3710 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3711 atomic_fetch_add_explicit(&zdplane_info.dg_route_errors,
3712 1, memory_order_relaxed);
3713 break;
3714
3715 case DPLANE_OP_NH_INSTALL:
3716 case DPLANE_OP_NH_UPDATE:
3717 case DPLANE_OP_NH_DELETE:
3718 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3719 atomic_fetch_add_explicit(
3720 &zdplane_info.dg_nexthop_errors, 1,
3721 memory_order_relaxed);
3722 break;
3723
3724 case DPLANE_OP_LSP_INSTALL:
3725 case DPLANE_OP_LSP_UPDATE:
3726 case DPLANE_OP_LSP_DELETE:
3727 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3728 atomic_fetch_add_explicit(&zdplane_info.dg_lsp_errors,
3729 1, memory_order_relaxed);
3730 break;
3731
3732 case DPLANE_OP_PW_INSTALL:
3733 case DPLANE_OP_PW_UNINSTALL:
3734 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3735 atomic_fetch_add_explicit(&zdplane_info.dg_pw_errors, 1,
3736 memory_order_relaxed);
3737 break;
f62e5480 3738
2f74a82a
JU
3739 case DPLANE_OP_ADDR_INSTALL:
3740 case DPLANE_OP_ADDR_UNINSTALL:
3741 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3742 atomic_fetch_add_explicit(
3743 &zdplane_info.dg_intf_addr_errors, 1,
3744 memory_order_relaxed);
3745 break;
3746
3747 case DPLANE_OP_MAC_INSTALL:
3748 case DPLANE_OP_MAC_DELETE:
3749 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3750 atomic_fetch_add_explicit(&zdplane_info.dg_mac_errors,
3751 1, memory_order_relaxed);
3752 break;
3753
3754 case DPLANE_OP_NEIGH_INSTALL:
3755 case DPLANE_OP_NEIGH_UPDATE:
3756 case DPLANE_OP_NEIGH_DELETE:
3757 case DPLANE_OP_VTEP_ADD:
3758 case DPLANE_OP_VTEP_DELETE:
3759 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3760 atomic_fetch_add_explicit(&zdplane_info.dg_neigh_errors,
3761 1, memory_order_relaxed);
3762 break;
3763
3764 case DPLANE_OP_RULE_ADD:
3765 case DPLANE_OP_RULE_DELETE:
3766 case DPLANE_OP_RULE_UPDATE:
3767 if (res != ZEBRA_DPLANE_REQUEST_SUCCESS)
3768 atomic_fetch_add_explicit(&zdplane_info.dg_rule_errors,
3769 1, memory_order_relaxed);
3770 break;
3771
3772 /* Ignore 'notifications' - no-op */
3773 case DPLANE_OP_SYS_ROUTE_ADD:
3774 case DPLANE_OP_SYS_ROUTE_DELETE:
3775 case DPLANE_OP_ROUTE_NOTIFY:
3776 case DPLANE_OP_LSP_NOTIFY:
3777 case DPLANE_OP_NONE:
3778 break;
3779 }
3780
3781 dplane_ctx_set_status(ctx, res);
f62e5480
JU
3782}
3783
c831033f
MS
3784/*
3785 * Kernel provider callback
3786 */
3787static int kernel_dplane_process_func(struct zebra_dplane_provider *prov)
7cdb1a84 3788{
c831033f 3789 enum zebra_dplane_result res;
2f74a82a
JU
3790 struct zebra_dplane_ctx *ctx, *tctx;
3791 struct dplane_ctx_q work_list;
c831033f 3792 int counter, limit;
7cdb1a84 3793
2f74a82a
JU
3794 TAILQ_INIT(&work_list);
3795
c831033f 3796 limit = dplane_provider_get_work_limit(prov);
7cdb1a84 3797
c831033f
MS
3798 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3799 zlog_debug("dplane provider '%s': processing",
3800 dplane_provider_get_name(prov));
7cdb1a84 3801
c831033f 3802 for (counter = 0; counter < limit; counter++) {
91f16812 3803
c831033f
MS
3804 ctx = dplane_provider_dequeue_in_ctx(prov);
3805 if (ctx == NULL)
3806 break;
d8c16a95 3807
9677961e
MS
3808 /* A previous provider plugin may have asked to skip the
3809 * kernel update.
3810 */
3811 if (dplane_ctx_is_skip_kernel(ctx)) {
3812 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3813 goto skip_one;
3814 }
3815
16c628de
MS
3816 /* Dispatch to appropriate kernel-facing apis */
3817 switch (dplane_ctx_get_op(ctx)) {
1d11b21f 3818
16c628de
MS
3819 case DPLANE_OP_ROUTE_INSTALL:
3820 case DPLANE_OP_ROUTE_UPDATE:
3821 case DPLANE_OP_ROUTE_DELETE:
3822 res = kernel_dplane_route_update(ctx);
3823 break;
d8c16a95 3824
f820d025
SW
3825 case DPLANE_OP_NH_INSTALL:
3826 case DPLANE_OP_NH_UPDATE:
3827 case DPLANE_OP_NH_DELETE:
3828 res = kernel_dplane_nexthop_update(ctx);
3829 break;
3830
16c628de
MS
3831 case DPLANE_OP_LSP_INSTALL:
3832 case DPLANE_OP_LSP_UPDATE:
3833 case DPLANE_OP_LSP_DELETE:
3834 res = kernel_dplane_lsp_update(ctx);
3835 break;
d8c16a95 3836
c10a646d
MS
3837 case DPLANE_OP_PW_INSTALL:
3838 case DPLANE_OP_PW_UNINSTALL:
3839 res = kernel_dplane_pw_update(ctx);
3840 break;
3841
64168803
MS
3842 case DPLANE_OP_ADDR_INSTALL:
3843 case DPLANE_OP_ADDR_UNINSTALL:
3844 res = kernel_dplane_address_update(ctx);
3845 break;
3846
036d93c0
MS
3847 case DPLANE_OP_MAC_INSTALL:
3848 case DPLANE_OP_MAC_DELETE:
3849 res = kernel_dplane_mac_update(ctx);
3850 break;
3851
931fa60c
MS
3852 case DPLANE_OP_NEIGH_INSTALL:
3853 case DPLANE_OP_NEIGH_UPDATE:
3854 case DPLANE_OP_NEIGH_DELETE:
0bbd4ff4
MS
3855 case DPLANE_OP_VTEP_ADD:
3856 case DPLANE_OP_VTEP_DELETE:
931fa60c
MS
3857 res = kernel_dplane_neigh_update(ctx);
3858 break;
3859
f62e5480
JU
3860 case DPLANE_OP_RULE_ADD:
3861 case DPLANE_OP_RULE_DELETE:
3862 case DPLANE_OP_RULE_UPDATE:
3863 res = kernel_dplane_rule_update(ctx);
3864 break;
3865
104e3ad9 3866 /* Ignore 'notifications' - no-op */
cf363e1b
MS
3867 case DPLANE_OP_SYS_ROUTE_ADD:
3868 case DPLANE_OP_SYS_ROUTE_DELETE:
54818e3b 3869 case DPLANE_OP_ROUTE_NOTIFY:
104e3ad9 3870 case DPLANE_OP_LSP_NOTIFY:
cf363e1b
MS
3871 res = ZEBRA_DPLANE_REQUEST_SUCCESS;
3872 break;
3873
16c628de 3874 default:
c831033f 3875 atomic_fetch_add_explicit(
16c628de 3876 &zdplane_info.dg_other_errors, 1,
c831033f 3877 memory_order_relaxed);
d8c16a95 3878
16c628de
MS
3879 res = ZEBRA_DPLANE_REQUEST_FAILURE;
3880 break;
3881 }
3882
2f74a82a
JU
3883 skip_one:
3884 /* If the request isn't pending, we can handle the result right
3885 * away.
3886 */
3887 if (res != ZEBRA_DPLANE_REQUEST_PENDING)
3888 kernel_dplane_handle_result(ctx, res);
3889
3890 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
3891 }
c831033f 3892
2f74a82a
JU
3893 TAILQ_FOREACH_SAFE (ctx, &work_list, zd_q_entries, tctx) {
3894 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
c831033f
MS
3895 dplane_provider_enqueue_out_ctx(prov, ctx);
3896 }
3897
3898 /* Ensure that we'll run the work loop again if there's still
3899 * more work to do.
3900 */
3901 if (counter >= limit) {
3902 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3903 zlog_debug("dplane provider '%s' reached max updates %d",
3904 dplane_provider_get_name(prov), counter);
3905
3906 atomic_fetch_add_explicit(&zdplane_info.dg_update_yields,
3907 1, memory_order_relaxed);
3908
3909 dplane_provider_work_ready();
3910 }
3911
3912 return 0;
3913}
3914
1e20238a 3915#ifdef DPLANE_TEST_PROVIDER
e5a60d82 3916
c831033f
MS
3917/*
3918 * Test dataplane provider plugin
3919 */
3920
3921/*
3922 * Test provider process callback
3923 */
3924static int test_dplane_process_func(struct zebra_dplane_provider *prov)
3925{
3926 struct zebra_dplane_ctx *ctx;
3927 int counter, limit;
3928
3929 /* Just moving from 'in' queue to 'out' queue */
3930
3931 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3932 zlog_debug("dplane provider '%s': processing",
3933 dplane_provider_get_name(prov));
3934
3935 limit = dplane_provider_get_work_limit(prov);
3936
3937 for (counter = 0; counter < limit; counter++) {
3938
3939 ctx = dplane_provider_dequeue_in_ctx(prov);
3940 if (ctx == NULL)
3941 break;
3942
cf363e1b
MS
3943 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3944 zlog_debug("dplane provider '%s': op %s",
3945 dplane_provider_get_name(prov),
3946 dplane_op2str(dplane_ctx_get_op(ctx)));
3947
c831033f
MS
3948 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
3949
3950 dplane_provider_enqueue_out_ctx(prov, ctx);
3951 }
3952
c9d17fe8
MS
3953 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
3954 zlog_debug("dplane provider '%s': processed %d",
3955 dplane_provider_get_name(prov), counter);
3956
c831033f
MS
3957 /* Ensure that we'll run the work loop again if there's still
3958 * more work to do.
3959 */
3960 if (counter >= limit)
3961 dplane_provider_work_ready();
3962
3963 return 0;
3964}
3965
3966/*
3967 * Test provider shutdown/fini callback
3968 */
3969static int test_dplane_shutdown_func(struct zebra_dplane_provider *prov,
3970 bool early)
3971{
3972 if (IS_ZEBRA_DEBUG_DPLANE)
3973 zlog_debug("dplane provider '%s': %sshutdown",
3974 dplane_provider_get_name(prov),
3975 early ? "early " : "");
3976
3977 return 0;
3978}
e5a60d82 3979#endif /* DPLANE_TEST_PROVIDER */
c831033f
MS
3980
3981/*
3982 * Register default kernel provider
3983 */
3984static void dplane_provider_init(void)
3985{
3986 int ret;
3987
3988 ret = dplane_provider_register("Kernel",
3989 DPLANE_PRIO_KERNEL,
1dd4ea8a 3990 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
3991 kernel_dplane_process_func,
3992 NULL,
1ff8a248 3993 NULL, NULL);
c831033f
MS
3994
3995 if (ret != AOK)
3996 zlog_err("Unable to register kernel dplane provider: %d",
3997 ret);
3998
1e20238a 3999#ifdef DPLANE_TEST_PROVIDER
e5a60d82 4000 /* Optional test provider ... */
c831033f
MS
4001 ret = dplane_provider_register("Test",
4002 DPLANE_PRIO_PRE_KERNEL,
1dd4ea8a 4003 DPLANE_PROV_FLAGS_DEFAULT, NULL,
c831033f
MS
4004 test_dplane_process_func,
4005 test_dplane_shutdown_func,
1ff8a248 4006 NULL /* data */, NULL);
c831033f
MS
4007
4008 if (ret != AOK)
4009 zlog_err("Unable to register test dplane provider: %d",
4010 ret);
e5a60d82 4011#endif /* DPLANE_TEST_PROVIDER */
7cdb1a84
MS
4012}
4013
4dfd7a02
MS
4014/* Indicates zebra shutdown/exit is in progress. Some operations may be
4015 * simplified or skipped during shutdown processing.
4016 */
4017bool dplane_is_in_shutdown(void)
4018{
25779064 4019 return zdplane_info.dg_is_shutdown;
4dfd7a02
MS
4020}
4021
4022/*
4023 * Early or pre-shutdown, de-init notification api. This runs pretty
4024 * early during zebra shutdown, as a signal to stop new work and prepare
4025 * for updates generated by shutdown/cleanup activity, as zebra tries to
4026 * remove everything it's responsible for.
c9d17fe8 4027 * NB: This runs in the main zebra pthread context.
4dfd7a02
MS
4028 */
4029void zebra_dplane_pre_finish(void)
4030{
6ba8db21
RZ
4031 struct zebra_dplane_provider *dp;
4032
4dfd7a02
MS
4033 if (IS_ZEBRA_DEBUG_DPLANE)
4034 zlog_debug("Zebra dataplane pre-fini called");
4035
25779064 4036 zdplane_info.dg_is_shutdown = true;
4dfd7a02 4037
6ba8db21
RZ
4038 /* Notify provider(s) of pending shutdown. */
4039 TAILQ_FOREACH(dp, &zdplane_info.dg_providers_q, dp_prov_link) {
4040 if (dp->dp_fini == NULL)
4041 continue;
4042
4043 dp->dp_fini(dp, true);
4044 }
4dfd7a02
MS
4045}
4046
4047/*
4048 * Utility to determine whether work remains enqueued within the dplane;
4049 * used during system shutdown processing.
4050 */
4051static bool dplane_work_pending(void)
4052{
c9d17fe8 4053 bool ret = false;
25779064 4054 struct zebra_dplane_ctx *ctx;
c9d17fe8 4055 struct zebra_dplane_provider *prov;
4dfd7a02 4056
c831033f
MS
4057 /* TODO -- just checking incoming/pending work for now, must check
4058 * providers
4059 */
4dfd7a02
MS
4060 DPLANE_LOCK();
4061 {
3fe4ccc4 4062 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c9d17fe8 4063 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4dfd7a02
MS
4064 }
4065 DPLANE_UNLOCK();
4066
c9d17fe8
MS
4067 if (ctx != NULL) {
4068 ret = true;
4069 goto done;
4070 }
4071
4072 while (prov) {
4073
ad6aad4d 4074 dplane_provider_lock(prov);
c9d17fe8
MS
4075
4076 ctx = TAILQ_FIRST(&(prov->dp_ctx_in_q));
4077 if (ctx == NULL)
4078 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4079
ad6aad4d 4080 dplane_provider_unlock(prov);
c9d17fe8
MS
4081
4082 if (ctx != NULL)
4083 break;
4084
4085 DPLANE_LOCK();
4086 prov = TAILQ_NEXT(prov, dp_prov_link);
4087 DPLANE_UNLOCK();
4088 }
4089
4090 if (ctx != NULL)
4091 ret = true;
4092
4093done:
4094 return ret;
4dfd7a02
MS
4095}
4096
4097/*
4098 * Shutdown-time intermediate callback, used to determine when all pending
4099 * in-flight updates are done. If there's still work to do, reschedules itself.
4100 * If all work is done, schedules an event to the main zebra thread for
4101 * final zebra shutdown.
4102 * This runs in the dplane pthread context.
4103 */
4104static int dplane_check_shutdown_status(struct thread *event)
4105{
4106 if (IS_ZEBRA_DEBUG_DPLANE)
4107 zlog_debug("Zebra dataplane shutdown status check called");
4108
4109 if (dplane_work_pending()) {
4110 /* Reschedule dplane check on a short timer */
25779064 4111 thread_add_timer_msec(zdplane_info.dg_master,
4dfd7a02
MS
4112 dplane_check_shutdown_status,
4113 NULL, 100,
25779064 4114 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
4115
4116 /* TODO - give up and stop waiting after a short time? */
4117
4118 } else {
4119 /* We appear to be done - schedule a final callback event
4120 * for the zebra main pthread.
4121 */
3801e764 4122 thread_add_event(zrouter.master, zebra_finalize, NULL, 0, NULL);
4dfd7a02
MS
4123 }
4124
4125 return 0;
4126}
4127
18c37974 4128/*
1d11b21f 4129 * Shutdown, de-init api. This runs pretty late during shutdown,
4dfd7a02
MS
4130 * after zebra has tried to free/remove/uninstall all routes during shutdown.
4131 * At this point, dplane work may still remain to be done, so we can't just
4132 * blindly terminate. If there's still work to do, we'll periodically check
4133 * and when done, we'll enqueue a task to the zebra main thread for final
4134 * termination processing.
4135 *
1d11b21f 4136 * NB: This runs in the main zebra thread context.
18c37974 4137 */
1d11b21f 4138void zebra_dplane_finish(void)
18c37974 4139{
4dfd7a02
MS
4140 if (IS_ZEBRA_DEBUG_DPLANE)
4141 zlog_debug("Zebra dataplane fini called");
4142
25779064 4143 thread_add_event(zdplane_info.dg_master,
4dfd7a02 4144 dplane_check_shutdown_status, NULL, 0,
25779064 4145 &zdplane_info.dg_t_shutdown_check);
4dfd7a02
MS
4146}
4147
c831033f
MS
4148/*
4149 * Main dataplane pthread event loop. The thread takes new incoming work
4150 * and offers it to the first provider. It then iterates through the
4151 * providers, taking complete work from each one and offering it
4152 * to the next in order. At each step, a limited number of updates are
4153 * processed during a cycle in order to provide some fairness.
14b0bc8e
MS
4154 *
4155 * This loop through the providers is only run once, so that the dataplane
4156 * pthread can look for other pending work - such as i/o work on behalf of
4157 * providers.
c831033f
MS
4158 */
4159static int dplane_thread_loop(struct thread *event)
4160{
4161 struct dplane_ctx_q work_list;
4162 struct dplane_ctx_q error_list;
4163 struct zebra_dplane_provider *prov;
4164 struct zebra_dplane_ctx *ctx, *tctx;
4165 int limit, counter, error_counter;
c9d17fe8 4166 uint64_t curr, high;
c831033f
MS
4167
4168 /* Capture work limit per cycle */
4169 limit = zdplane_info.dg_updates_per_cycle;
4170
14b0bc8e 4171 /* Init temporary lists used to move contexts among providers */
c831033f 4172 TAILQ_INIT(&work_list);
14b0bc8e
MS
4173 TAILQ_INIT(&error_list);
4174 error_counter = 0;
c831033f
MS
4175
4176 /* Check for zebra shutdown */
4177 if (!zdplane_info.dg_run)
4178 goto done;
4179
4180 /* Dequeue some incoming work from zebra (if any) onto the temporary
4181 * working list.
4182 */
4183 DPLANE_LOCK();
4184
4185 /* Locate initial registered provider */
4186 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4187
14b0bc8e 4188 /* Move new work from incoming list to temp list */
c831033f 4189 for (counter = 0; counter < limit; counter++) {
3fe4ccc4 4190 ctx = TAILQ_FIRST(&zdplane_info.dg_update_ctx_q);
c831033f 4191 if (ctx) {
3fe4ccc4 4192 TAILQ_REMOVE(&zdplane_info.dg_update_ctx_q, ctx,
c831033f
MS
4193 zd_q_entries);
4194
c831033f
MS
4195 ctx->zd_provider = prov->dp_id;
4196
4197 TAILQ_INSERT_TAIL(&work_list, ctx, zd_q_entries);
4198 } else {
4199 break;
4200 }
4201 }
4202
4203 DPLANE_UNLOCK();
4204
14b0bc8e
MS
4205 atomic_fetch_sub_explicit(&zdplane_info.dg_routes_queued, counter,
4206 memory_order_relaxed);
4207
c831033f
MS
4208 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4209 zlog_debug("dplane: incoming new work counter: %d", counter);
4210
4211 /* Iterate through the registered providers, offering new incoming
4212 * work. If the provider has outgoing work in its queue, take that
4213 * work for the next provider
4214 */
4215 while (prov) {
4216
14b0bc8e
MS
4217 /* At each iteration, the temporary work list has 'counter'
4218 * items.
4219 */
c831033f
MS
4220 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4221 zlog_debug("dplane enqueues %d new work to provider '%s'",
4222 counter, dplane_provider_get_name(prov));
4223
4224 /* Capture current provider id in each context; check for
4225 * error status.
4226 */
4227 TAILQ_FOREACH_SAFE(ctx, &work_list, zd_q_entries, tctx) {
4228 if (dplane_ctx_get_status(ctx) ==
4229 ZEBRA_DPLANE_REQUEST_SUCCESS) {
4230 ctx->zd_provider = prov->dp_id;
4231 } else {
4232 /*
4233 * TODO -- improve error-handling: recirc
4234 * errors backwards so that providers can
4235 * 'undo' their work (if they want to)
4236 */
4237
4238 /* Move to error list; will be returned
4239 * zebra main.
4240 */
4241 TAILQ_REMOVE(&work_list, ctx, zd_q_entries);
4242 TAILQ_INSERT_TAIL(&error_list,
4243 ctx, zd_q_entries);
4244 error_counter++;
4245 }
4246 }
4247
4248 /* Enqueue new work to the provider */
ad6aad4d 4249 dplane_provider_lock(prov);
c831033f
MS
4250
4251 if (TAILQ_FIRST(&work_list))
4252 TAILQ_CONCAT(&(prov->dp_ctx_in_q), &work_list,
4253 zd_q_entries);
4254
c9d17fe8
MS
4255 atomic_fetch_add_explicit(&prov->dp_in_counter, counter,
4256 memory_order_relaxed);
4257 atomic_fetch_add_explicit(&prov->dp_in_queued, counter,
4258 memory_order_relaxed);
4259 curr = atomic_load_explicit(&prov->dp_in_queued,
4260 memory_order_relaxed);
4261 high = atomic_load_explicit(&prov->dp_in_max,
4262 memory_order_relaxed);
4263 if (curr > high)
4264 atomic_store_explicit(&prov->dp_in_max, curr,
c831033f
MS
4265 memory_order_relaxed);
4266
ad6aad4d 4267 dplane_provider_unlock(prov);
c831033f 4268
14b0bc8e
MS
4269 /* Reset the temp list (though the 'concat' may have done this
4270 * already), and the counter
4271 */
c831033f
MS
4272 TAILQ_INIT(&work_list);
4273 counter = 0;
4274
14b0bc8e
MS
4275 /* Call into the provider code. Note that this is
4276 * unconditional: we offer to do work even if we don't enqueue
4277 * any _new_ work.
4278 */
c831033f
MS
4279 (*prov->dp_fp)(prov);
4280
4281 /* Check for zebra shutdown */
4282 if (!zdplane_info.dg_run)
4283 break;
4284
4285 /* Dequeue completed work from the provider */
ad6aad4d 4286 dplane_provider_lock(prov);
c831033f
MS
4287
4288 while (counter < limit) {
4289 ctx = TAILQ_FIRST(&(prov->dp_ctx_out_q));
4290 if (ctx) {
4291 TAILQ_REMOVE(&(prov->dp_ctx_out_q), ctx,
4292 zd_q_entries);
4293
4294 TAILQ_INSERT_TAIL(&work_list,
4295 ctx, zd_q_entries);
4296 counter++;
4297 } else
4298 break;
4299 }
4300
ad6aad4d 4301 dplane_provider_unlock(prov);
c831033f
MS
4302
4303 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
4304 zlog_debug("dplane dequeues %d completed work from provider %s",
4305 counter, dplane_provider_get_name(prov));
4306
4307 /* Locate next provider */
4308 DPLANE_LOCK();
4309 prov = TAILQ_NEXT(prov, dp_prov_link);
4310 DPLANE_UNLOCK();
c831033f
MS
4311 }
4312
4313 /* After all providers have been serviced, enqueue any completed
14b0bc8e 4314 * work and any errors back to zebra so it can process the results.
c831033f 4315 */
c831033f 4316 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
14b0bc8e
MS
4317 zlog_debug("dplane has %d completed, %d errors, for zebra main",
4318 counter, error_counter);
c831033f
MS
4319
4320 /*
4c206c8f 4321 * Hand lists through the api to zebra main,
c831033f
MS
4322 * to reduce the number of lock/unlock cycles
4323 */
14b0bc8e 4324
4c206c8f
MS
4325 /* Call through to zebra main */
4326 (zdplane_info.dg_results_cb)(&error_list);
14b0bc8e 4327
4c206c8f 4328 TAILQ_INIT(&error_list);
14b0bc8e 4329
4c206c8f
MS
4330 /* Call through to zebra main */
4331 (zdplane_info.dg_results_cb)(&work_list);
c831033f 4332
4c206c8f 4333 TAILQ_INIT(&work_list);
c831033f
MS
4334
4335done:
4336 return 0;
4337}
4338
4dfd7a02
MS
4339/*
4340 * Final phase of shutdown, after all work enqueued to dplane has been
4341 * processed. This is called from the zebra main pthread context.
4342 */
4343void zebra_dplane_shutdown(void)
4344{
6ba8db21
RZ
4345 struct zebra_dplane_provider *dp;
4346
4dfd7a02
MS
4347 if (IS_ZEBRA_DEBUG_DPLANE)
4348 zlog_debug("Zebra dataplane shutdown called");
1d11b21f
MS
4349
4350 /* Stop dplane thread, if it's running */
4351
25779064 4352 zdplane_info.dg_run = false;
1d11b21f 4353
9344d3fc
SW
4354 if (zdplane_info.dg_t_update)
4355 thread_cancel_async(zdplane_info.dg_t_update->master,
4356 &zdplane_info.dg_t_update, NULL);
1d11b21f 4357
d8c16a95
MS
4358 frr_pthread_stop(zdplane_info.dg_pthread, NULL);
4359
4360 /* Destroy pthread */
4361 frr_pthread_destroy(zdplane_info.dg_pthread);
4362 zdplane_info.dg_pthread = NULL;
4363 zdplane_info.dg_master = NULL;
4dfd7a02 4364
6ba8db21
RZ
4365 /* Notify provider(s) of final shutdown. */
4366 TAILQ_FOREACH(dp, &zdplane_info.dg_providers_q, dp_prov_link) {
4367 if (dp->dp_fini == NULL)
4368 continue;
4369
4370 dp->dp_fini(dp, false);
4371 }
c831033f
MS
4372
4373 /* TODO -- Clean-up provider objects */
4374
4375 /* TODO -- Clean queue(s), free memory */
4376}
4377
4378/*
4379 * Initialize the dataplane module during startup, internal/private version
4380 */
2561d12e 4381static void zebra_dplane_init_internal(void)
c831033f
MS
4382{
4383 memset(&zdplane_info, 0, sizeof(zdplane_info));
4384
4385 pthread_mutex_init(&zdplane_info.dg_mutex, NULL);
1d11b21f 4386
3fe4ccc4 4387 TAILQ_INIT(&zdplane_info.dg_update_ctx_q);
c831033f 4388 TAILQ_INIT(&zdplane_info.dg_providers_q);
1d11b21f 4389
c831033f
MS
4390 zdplane_info.dg_updates_per_cycle = DPLANE_DEFAULT_NEW_WORK;
4391
4392 zdplane_info.dg_max_queued_updates = DPLANE_DEFAULT_MAX_QUEUED;
4393
4394 /* Register default kernel 'provider' during init */
4395 dplane_provider_init();
e5a60d82 4396}
c831033f 4397
e5a60d82
MS
4398/*
4399 * Start the dataplane pthread. This step needs to be run later than the
4400 * 'init' step, in case zebra has fork-ed.
4401 */
4402void zebra_dplane_start(void)
4403{
1dd4ea8a 4404 struct zebra_dplane_provider *prov;
c831033f
MS
4405 struct frr_pthread_attr pattr = {
4406 .start = frr_pthread_attr_default.start,
4407 .stop = frr_pthread_attr_default.stop
4408 };
4409
1dd4ea8a
MS
4410 /* Start dataplane pthread */
4411
c831033f 4412 zdplane_info.dg_pthread = frr_pthread_new(&pattr, "Zebra dplane thread",
0eca319c 4413 "zebra_dplane");
c831033f
MS
4414
4415 zdplane_info.dg_master = zdplane_info.dg_pthread->master;
4416
e5a60d82
MS
4417 zdplane_info.dg_run = true;
4418
c831033f
MS
4419 /* Enqueue an initial event for the dataplane pthread */
4420 thread_add_event(zdplane_info.dg_master, dplane_thread_loop, NULL, 0,
4421 &zdplane_info.dg_t_update);
4422
1dd4ea8a
MS
4423 /* Call start callbacks for registered providers */
4424
4425 DPLANE_LOCK();
4426 prov = TAILQ_FIRST(&zdplane_info.dg_providers_q);
4427 DPLANE_UNLOCK();
4428
4429 while (prov) {
4430
4431 if (prov->dp_start)
4432 (prov->dp_start)(prov);
4433
4434 /* Locate next provider */
4435 DPLANE_LOCK();
4436 prov = TAILQ_NEXT(prov, dp_prov_link);
4437 DPLANE_UNLOCK();
4438 }
4439
c831033f 4440 frr_pthread_run(zdplane_info.dg_pthread, NULL);
18c37974
MS
4441}
4442
7cdb1a84 4443/*
b8e0423d 4444 * Initialize the dataplane module at startup; called by zebra rib_init()
7cdb1a84 4445 */
4c206c8f 4446void zebra_dplane_init(int (*results_fp)(struct dplane_ctx_q *))
7cdb1a84 4447{
2561d12e 4448 zebra_dplane_init_internal();
4c206c8f 4449 zdplane_info.dg_results_cb = results_fp;
7cdb1a84 4450}