]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / zebra / zebra_rib.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/* Routing Information Base.
3 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
718e3744 4 */
5
6#include <zebra.h>
7
718e3744 8#include "command.h"
43e52561
QY
9#include "if.h"
10#include "linklist.h"
718e3744 11#include "log.h"
43e52561
QY
12#include "memory.h"
13#include "mpls.h"
14#include "nexthop.h"
15#include "prefix.h"
7514fb77
PJ
16#include "prefix.h"
17#include "routemap.h"
43e52561 18#include "sockunion.h"
05737783 19#include "srcdest_table.h"
43e52561 20#include "table.h"
24a58196 21#include "frrevent.h"
43e52561
QY
22#include "vrf.h"
23#include "workqueue.h"
50d89650 24#include "nexthop_group_private.h"
00dffa8c 25#include "frr_pthread.h"
61e6de9d 26#include "printfrr.h"
1247efcc 27#include "frrscript.h"
718e3744 28
89272910 29#include "zebra/zebra_router.h"
43e52561
QY
30#include "zebra/connected.h"
31#include "zebra/debug.h"
32#include "zebra/interface.h"
33#include "zebra/redistribute.h"
718e3744 34#include "zebra/rib.h"
35#include "zebra/rt.h"
43e52561
QY
36#include "zebra/zapi_msg.h"
37#include "zebra/zebra_errors.h"
7c551956 38#include "zebra/zebra_ns.h"
fb018d25 39#include "zebra/zebra_rnh.h"
43e52561
QY
40#include "zebra/zebra_routemap.h"
41#include "zebra/zebra_vrf.h"
6134fd82 42#include "zebra/zebra_vxlan.h"
7cdb1a84
MS
43#include "zebra/zapi_msg.h"
44#include "zebra/zebra_dplane.h"
32367e7a 45#include "zebra/zebra_evpn_mh.h"
1247efcc 46#include "zebra/zebra_script.h"
7cdb1a84 47
224ccf29
DL
48DEFINE_MGROUP(ZEBRA, "zebra");
49
50DEFINE_MTYPE(ZEBRA, RE, "Route Entry");
51DEFINE_MTYPE_STATIC(ZEBRA, RIB_DEST, "RIB destination");
2a99ab95 52DEFINE_MTYPE_STATIC(ZEBRA, RIB_UPDATE_CTX, "Rib update context object");
32367e7a 53DEFINE_MTYPE_STATIC(ZEBRA, WQ_WRAPPER, "WQ wrapper");
2a99ab95 54
7cdb1a84
MS
55/*
56 * Event, list, and mutex for delivery of dataplane results
57 */
58static pthread_mutex_t dplane_mutex;
e6685141 59static struct event *t_dplane;
ac96497c 60static struct dplane_ctx_list_head rib_dplane_q;
718e3744 61
d62a17ae 62DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
8451921b 63 (rn, reason));
0d0f516c 64DEFINE_HOOK(rib_shutdown, (struct route_node * rn), (rn));
65
4f8ea50c 66
81322b96
DS
67/*
68 * Meta Q's specific names
69 *
70 * If you add something here ensure that you
71 * change MQ_SIZE as well over in rib.h
72 */
014e732d
DS
73enum meta_queue_indexes {
74 META_QUEUE_NHG,
75 META_QUEUE_EVPN,
d7ac4c4d 76 META_QUEUE_EARLY_ROUTE,
53216dff 77 META_QUEUE_EARLY_LABEL,
014e732d
DS
78 META_QUEUE_CONNECTED,
79 META_QUEUE_KERNEL,
80 META_QUEUE_STATIC,
81 META_QUEUE_NOTBGP,
82 META_QUEUE_BGP,
83 META_QUEUE_OTHER,
81322b96 84 META_QUEUE_GR_RUN,
014e732d
DS
85};
86
718e3744 87/* Each route type's string and default distance value. */
d62a17ae 88static const struct {
89 int key;
8dc7a759 90 uint8_t distance;
014e732d 91 enum meta_queue_indexes meta_q_map;
d62a17ae 92} route_info[ZEBRA_ROUTE_MAX] = {
3d55a4ef
DS
93 [ZEBRA_ROUTE_NHG] =
94 {ZEBRA_ROUTE_NHG,
95 ZEBRA_MAX_DISTANCE_DEFAULT /* Unneeded for nhg's */,
96 META_QUEUE_NHG},
97 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM,
98 ZEBRA_KERNEL_DISTANCE_DEFAULT,
99 META_QUEUE_KERNEL},
100 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL,
101 ZEBRA_KERNEL_DISTANCE_DEFAULT,
102 META_QUEUE_KERNEL},
103 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT,
104 ZEBRA_CONNECT_DISTANCE_DEFAULT,
105 META_QUEUE_CONNECTED},
106 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC,
107 ZEBRA_STATIC_DISTANCE_DEFAULT,
108 META_QUEUE_STATIC},
109 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, ZEBRA_RIP_DISTANCE_DEFAULT,
110 META_QUEUE_NOTBGP},
111 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, ZEBRA_RIP_DISTANCE_DEFAULT,
112 META_QUEUE_NOTBGP},
113 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, ZEBRA_OSPF_DISTANCE_DEFAULT,
114 META_QUEUE_NOTBGP},
115 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, ZEBRA_OSPF6_DISTANCE_DEFAULT,
116 META_QUEUE_NOTBGP},
117 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, ZEBRA_ISIS_DISTANCE_DEFAULT,
118 META_QUEUE_NOTBGP},
119 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP,
120 ZEBRA_EBGP_DISTANCE_DEFAULT /* IBGP is 200. */,
014e732d 121 META_QUEUE_BGP},
3d55a4ef
DS
122 [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, ZEBRA_MAX_DISTANCE_DEFAULT,
123 META_QUEUE_OTHER},
124 [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, ZEBRA_EIGRP_DISTANCE_DEFAULT,
125 META_QUEUE_NOTBGP},
126 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, ZEBRA_NHRP_DISTANCE_DEFAULT,
127 META_QUEUE_NOTBGP},
128 [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, ZEBRA_MAX_DISTANCE_DEFAULT,
129 META_QUEUE_OTHER},
130 [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, ZEBRA_MAX_DISTANCE_DEFAULT,
131 META_QUEUE_OTHER},
8b6a9cca 132 [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, ZEBRA_TABLE_DISTANCE_DEFAULT, META_QUEUE_STATIC},
3d55a4ef
DS
133 [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, ZEBRA_LDP_DISTANCE_DEFAULT,
134 META_QUEUE_OTHER},
135 [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, ZEBRA_EBGP_DISTANCE_DEFAULT,
136 META_QUEUE_BGP},
137 [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT,
138 ZEBRA_EBGP_DISTANCE_DEFAULT,
139 META_QUEUE_BGP},
140 [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH,
141 ZEBRA_EBGP_DISTANCE_DEFAULT,
014e732d 142 META_QUEUE_BGP},
3d55a4ef
DS
143 [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT,
144 ZEBRA_EBGP_DISTANCE_DEFAULT,
145 META_QUEUE_BGP},
146 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT,
147 ZEBRA_EBGP_DISTANCE_DEFAULT,
014e732d 148 META_QUEUE_BGP},
3d55a4ef
DS
149 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, ZEBRA_BABEL_DISTANCE_DEFAULT,
150 META_QUEUE_NOTBGP},
151 [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, ZEBRA_SHARP_DISTANCE_DEFAULT,
152 META_QUEUE_OTHER},
153 [ZEBRA_ROUTE_PBR] = {ZEBRA_ROUTE_PBR, ZEBRA_PBR_DISTANCE_DEFAULT,
154 META_QUEUE_OTHER},
155 [ZEBRA_ROUTE_BFD] = {ZEBRA_ROUTE_BFD, ZEBRA_MAX_DISTANCE_DEFAULT,
156 META_QUEUE_OTHER},
157 [ZEBRA_ROUTE_OPENFABRIC] = {ZEBRA_ROUTE_OPENFABRIC,
158 ZEBRA_OPENFABRIC_DISTANCE_DEFAULT,
014e732d 159 META_QUEUE_NOTBGP},
3d55a4ef
DS
160 [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, ZEBRA_MAX_DISTANCE_DEFAULT,
161 META_QUEUE_OTHER},
162 [ZEBRA_ROUTE_SRTE] = {ZEBRA_ROUTE_SRTE, ZEBRA_MAX_DISTANCE_DEFAULT,
163 META_QUEUE_OTHER},
164 [ZEBRA_ROUTE_ALL] = {ZEBRA_ROUTE_ALL, ZEBRA_MAX_DISTANCE_DEFAULT,
165 META_QUEUE_OTHER},
d6abd8b0 166 /* Any new route type added to zebra, should be mirrored here */
c710b277 167
d62a17ae 168 /* no entry/default: 150 */
718e3744 169};
6b0655a2 170
04bec7b2
MS
171/* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
172 * from the OS, and an 'nhe' is a nhe update.
173 */
174struct wq_nhg_wrapper {
175 int type;
176 union {
177 struct nhg_ctx *ctx;
178 struct nhg_hash_entry *nhe;
179 } u;
180};
181
182#define WQ_NHG_WRAPPER_TYPE_CTX 0x01
183#define WQ_NHG_WRAPPER_TYPE_NHG 0x02
184
32367e7a
MS
185/* Wrapper structs for evpn/vxlan workqueue items. */
186struct wq_evpn_wrapper {
187 int type;
188 bool add_p;
189 vrf_id_t vrf_id;
190 bool esr_rxed;
191 uint8_t df_alg;
192 uint16_t df_pref;
7f7e49d1
MS
193 uint32_t flags;
194 uint32_t seq;
32367e7a 195 esi_t esi;
7f7e49d1 196 vni_t vni;
32367e7a 197 struct ipaddr ip;
7f7e49d1 198 struct ethaddr macaddr;
32367e7a 199 struct prefix prefix;
7f7e49d1 200 struct in_addr vtep_ip;
32367e7a
MS
201};
202
7f7e49d1
MS
203#define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
204#define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
205#define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
7e5b0b2b 206#define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
32367e7a 207
53216dff
DS
208enum wq_label_types {
209 WQ_LABEL_FTN_UNINSTALL,
210 WQ_LABEL_LABELS_PROCESS,
211};
212
213struct wq_label_wrapper {
214 enum wq_label_types type;
215 vrf_id_t vrf_id;
216
217 struct prefix p;
218 enum lsp_types_t ltype;
219 uint8_t route_type;
220 uint8_t route_instance;
221
222 bool add_p;
223 struct zapi_labels zl;
224
225 int afi;
226};
227
d7ac4c4d
DS
228static void rib_addnode(struct route_node *rn, struct route_entry *re,
229 int process);
230
9d75e309
DL
231/* %pRN is already a printer for route_nodes that just prints the prefix */
232#ifdef _FRR_ATTRIBUTE_PRINTFRR
233#pragma FRR printfrr_ext "%pZN" (struct route_node *)
234#endif
235
014e732d 236static const char *subqueue2str(enum meta_queue_indexes index)
7a96ccc7
DS
237{
238 switch (index) {
4bbbd1f6 239 case META_QUEUE_NHG:
7a96ccc7 240 return "NHG Objects";
4bbbd1f6 241 case META_QUEUE_EVPN:
7a96ccc7 242 return "EVPN/VxLan Objects";
d7ac4c4d
DS
243 case META_QUEUE_EARLY_ROUTE:
244 return "Early Route Processing";
53216dff
DS
245 case META_QUEUE_EARLY_LABEL:
246 return "Early Label Handling";
014e732d 247 case META_QUEUE_CONNECTED:
7a96ccc7 248 return "Connected Routes";
014e732d 249 case META_QUEUE_KERNEL:
7a96ccc7 250 return "Kernel Routes";
014e732d 251 case META_QUEUE_STATIC:
7a96ccc7 252 return "Static Routes";
014e732d 253 case META_QUEUE_NOTBGP:
7a96ccc7 254 return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
014e732d 255 case META_QUEUE_BGP:
7a96ccc7 256 return "BGP Routes";
014e732d 257 case META_QUEUE_OTHER:
7a96ccc7 258 return "Other Routes";
81322b96
DS
259 case META_QUEUE_GR_RUN:
260 return "Graceful Restart";
7a96ccc7
DS
261 }
262
263 return "Unknown";
264}
265
9d75e309
DL
266printfrr_ext_autoreg_p("ZN", printfrr_zebra_node);
267static ssize_t printfrr_zebra_node(struct fbuf *buf, struct printfrr_eargs *ea,
268 const void *ptr)
2263a412 269{
9d75e309
DL
270 struct route_node *rn = (struct route_node *)ptr;
271 ssize_t rv = 0;
272
273 /* just the table number? */
274 if (ea->fmt[0] == 't') {
275 rib_dest_t *dest;
c2c02b76
DS
276 struct route_entry *re = NULL;
277
9d75e309 278 ea->fmt++;
cb653491 279
9d75e309
DL
280 if (!rn)
281 return bputch(buf, '!');
c2c02b76
DS
282
283 dest = rib_dest_from_rnode(rn);
284 if (dest)
285 re = re_list_first(&dest->routes);
286 if (re)
9d75e309
DL
287 rv += bprintfrr(buf, "%u", re->table);
288 else
289 rv += bputch(buf, '?');
290
d62a17ae 291 } else {
9d75e309
DL
292 char cbuf[PREFIX_STRLEN * 2 + 6];
293 struct rib_table_info *info;
294
295 if (!rn)
296 return bputs(buf, "{(route_node *) NULL}");
2263a412 297
9d75e309
DL
298 srcdest_rnode2str(rn, cbuf, sizeof(cbuf));
299 rv += bputs(buf, cbuf);
300
301 info = srcdest_rnode_table_info(rn);
302 if (info->safi == SAFI_MULTICAST)
303 rv += bputs(buf, " (MRIB)");
304 }
305 return rv;
2263a412
DL
306}
307
9d75e309
DL
308#define rnode_debug(node, vrf_id, msg, ...) \
309 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
310 ##__VA_ARGS__)
311
312#define rnode_info(node, vrf_id, msg, ...) \
313 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
314 ##__VA_ARGS__)
315
61e6de9d
DS
316static char *_dump_re_status(const struct route_entry *re, char *buf,
317 size_t len)
318{
319 if (re->status == 0) {
320 snprintfrr(buf, len, "None ");
321 return buf;
322 }
323
324 snprintfrr(
8d4665aa 325 buf, len, "%s%s%s%s%s%s%s%s",
61e6de9d
DS
326 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED) ? "Removed " : "",
327 CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED) ? "Changed " : "",
328 CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)
329 ? "Label Changed "
330 : "",
331 CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED) ? "Queued " : "",
8d4665aa
DS
332 CHECK_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING)
333 ? "Replacing"
334 : "",
61e6de9d
DS
335 CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED) ? "Installed "
336 : "",
337 CHECK_FLAG(re->status, ROUTE_ENTRY_FAILED) ? "Failed " : "",
338 CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG) ? "Fib NHG "
339 : "");
340 return buf;
341}
342
fd289fc8 343uint8_t route_distance(int type)
40c7bdb0 344{
fd289fc8 345 uint8_t distance;
40c7bdb0 346
d62a17ae 347 if ((unsigned)type >= array_size(route_info))
348 distance = 150;
349 else
350 distance = route_info[type].distance;
40c7bdb0 351
d62a17ae 352 return distance;
40c7bdb0 353}
354
d7c0a89a 355int is_zebra_valid_kernel_table(uint32_t table_id)
7a4bb9c5 356{
8f500a1c 357#ifdef linux
d62a17ae 358 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
359 || (table_id == RT_TABLE_COMPAT))
360 return 0;
8f500a1c
RW
361#endif
362
d62a17ae 363 return 1;
7a4bb9c5
DS
364}
365
d7c0a89a 366int is_zebra_main_routing_table(uint32_t table_id)
7a4bb9c5 367{
c447ad08 368 if (table_id == RT_TABLE_MAIN)
d62a17ae 369 return 1;
370 return 0;
7a4bb9c5
DS
371}
372
86391e56 373int zebra_check_addr(const struct prefix *p)
0aabccc0 374{
d62a17ae 375 if (p->family == AF_INET) {
d7c0a89a 376 uint32_t addr;
0aabccc0 377
d62a17ae 378 addr = p->u.prefix4.s_addr;
379 addr = ntohl(addr);
0aabccc0 380
d62a17ae 381 if (IPV4_NET127(addr) || IN_CLASSD(addr)
382 || IPV4_LINKLOCAL(addr))
383 return 0;
384 }
385 if (p->family == AF_INET6) {
386 if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
387 return 0;
388 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
389 return 0;
390 }
391 return 1;
0aabccc0
DD
392}
393
5463ce26
MS
394static void route_entry_attach_ref(struct route_entry *re,
395 struct nhg_hash_entry *new)
718e3744 396{
0eb97b86 397 re->nhe = new;
5463ce26 398 re->nhe_id = new->id;
002930f7 399 re->nhe_installed_id = 0;
718e3744 400
5463ce26 401 zebra_nhg_increment_ref(new);
718e3744 402}
403
002930f7 404/* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
cd7108ba
EDP
405int route_entry_update_nhe(struct route_entry *re,
406 struct nhg_hash_entry *new_nhghe)
718e3744 407{
5463ce26 408 int ret = 0;
002930f7 409 struct nhg_hash_entry *old_nhg = NULL;
718e3744 410
cd7108ba 411 if (new_nhghe == NULL) {
002930f7
MS
412 old_nhg = re->nhe;
413
414 re->nhe_id = 0;
415 re->nhe_installed_id = 0;
c415d895 416 re->nhe = NULL;
5463ce26
MS
417 goto done;
418 }
718e3744 419
1f655680 420 if ((re->nhe_id != 0) && re->nhe && (re->nhe != new_nhghe)) {
002930f7
MS
421 /* Capture previous nhg, if any */
422 old_nhg = re->nhe;
718e3744 423
cd7108ba 424 route_entry_attach_ref(re, new_nhghe);
84a89a8d 425 } else if (!re->nhe)
5463ce26 426 /* This is the first time it's being attached */
cd7108ba 427 route_entry_attach_ref(re, new_nhghe);
595db7f1 428
5463ce26 429done:
002930f7
MS
430 /* Detach / deref previous nhg */
431 if (old_nhg)
432 zebra_nhg_decrement_ref(old_nhg);
433
5463ce26 434 return ret;
595db7f1 435}
436
2e8db20d
EDP
437void rib_handle_nhg_replace(struct nhg_hash_entry *old_entry,
438 struct nhg_hash_entry *new_entry)
1f655680
SW
439{
440 struct zebra_router_table *zrt;
441 struct route_node *rn;
442 struct route_entry *re, *next;
443
444 if (IS_ZEBRA_DEBUG_RIB_DETAILED || IS_ZEBRA_DEBUG_NHG_DETAIL)
445 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
2e8db20d 446 __func__, new_entry->id, new_entry, old_entry);
1f655680
SW
447
448 /* We have to do them ALL */
449 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
450 for (rn = route_top(zrt->table); rn;
451 rn = srcdest_route_next(rn)) {
452 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2e8db20d
EDP
453 if (re->nhe && re->nhe == old_entry)
454 route_entry_update_nhe(re, new_entry);
1f655680
SW
455 }
456 }
457 }
458}
459
d62a17ae 460struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
bc77c3bb
MS
461 const union g_addr *addr,
462 struct route_node **rn_out)
d62a17ae 463{
464 struct prefix p;
465 struct route_table *table;
466 struct route_node *rn;
5f7a4718 467 struct route_entry *match = NULL;
d62a17ae 468
469 /* Lookup table. */
470 table = zebra_vrf_table(afi, safi, vrf_id);
471 if (!table)
472 return 0;
473
6006b807 474 memset(&p, 0, sizeof(p));
d62a17ae 475 p.family = afi;
476 if (afi == AFI_IP) {
477 p.u.prefix4 = addr->ipv4;
936fbaef 478 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 479 } else {
480 p.u.prefix6 = addr->ipv6;
f4d81e55 481 p.prefixlen = IPV6_MAX_BITLEN;
d62a17ae 482 }
718e3744 483
c4efd0f4 484 rn = route_node_match(table, &p);
718e3744 485
d62a17ae 486 while (rn) {
5f7a4718
DS
487 rib_dest_t *dest;
488
d62a17ae 489 route_unlock_node(rn);
718e3744 490
5f7a4718 491 dest = rib_dest_from_rnode(rn);
996c9314
LB
492 if (dest && dest->selected_fib
493 && !CHECK_FLAG(dest->selected_fib->status,
494 ROUTE_ENTRY_REMOVED))
5f7a4718 495 match = dest->selected_fib;
718e3744 496
d62a17ae 497 /* If there is no selected route or matched route is EGP, go up
498 tree. */
499 if (!match) {
500 do {
501 rn = rn->parent;
502 } while (rn && rn->info == NULL);
503 if (rn)
504 route_lock_node(rn);
505 } else {
506 if (match->type != ZEBRA_ROUTE_CONNECT) {
677c1dd5
DS
507 if (!CHECK_FLAG(match->status,
508 ROUTE_ENTRY_INSTALLED))
d62a17ae 509 return NULL;
510 }
511
512 if (rn_out)
513 *rn_out = rn;
514 return match;
515 }
16814f96 516 }
d62a17ae 517 return NULL;
518}
519
312e29b0
DS
520struct route_entry *rib_match_multicast(afi_t afi, vrf_id_t vrf_id,
521 union g_addr *gaddr,
522 struct route_node **rn_out)
d62a17ae 523{
524 struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
525 struct route_node *m_rn = NULL, *u_rn = NULL;
d62a17ae 526
526052fb 527 switch (zrouter.ipv4_multicast_mode) {
d62a17ae 528 case MCAST_MRIB_ONLY:
312e29b0 529 return rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, rn_out);
d62a17ae 530 case MCAST_URIB_ONLY:
312e29b0 531 return rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, rn_out);
d62a17ae 532 case MCAST_NO_CONFIG:
533 case MCAST_MIX_MRIB_FIRST:
312e29b0 534 re = mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn);
d62a17ae 535 if (!mre)
312e29b0
DS
536 re = ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr,
537 &u_rn);
d62a17ae 538 break;
539 case MCAST_MIX_DISTANCE:
312e29b0
DS
540 mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn);
541 ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, &u_rn);
d62a17ae 542 if (mre && ure)
543 re = ure->distance < mre->distance ? ure : mre;
544 else if (mre)
545 re = mre;
546 else if (ure)
547 re = ure;
548 break;
549 case MCAST_MIX_PFXLEN:
312e29b0
DS
550 mre = rib_match(afi, SAFI_MULTICAST, vrf_id, gaddr, &m_rn);
551 ure = rib_match(afi, SAFI_UNICAST, vrf_id, gaddr, &u_rn);
d62a17ae 552 if (mre && ure)
553 re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
554 else if (mre)
555 re = mre;
556 else if (ure)
557 re = ure;
558 break;
718e3744 559 }
33550aa8 560
d62a17ae 561 if (rn_out)
562 *rn_out = (re == mre) ? m_rn : u_rn;
718e3744 563
d62a17ae 564 if (IS_ZEBRA_DEBUG_RIB) {
565 char buf[BUFSIZ];
312e29b0
DS
566 inet_ntop(afi == AFI_IP ? AF_INET : AF_INET6, gaddr, buf,
567 BUFSIZ);
4623d897 568
312e29b0
DS
569 zlog_debug("%s: %s: %pRN vrf: %s(%u) found %s, using %s",
570 __func__, buf, (re == mre) ? m_rn : u_rn,
571 vrf_id_to_name(vrf_id), vrf_id,
d62a17ae 572 mre ? (ure ? "MRIB+URIB" : "MRIB")
573 : ure ? "URIB" : "nothing",
574 re == ure ? "URIB" : re == mre ? "MRIB" : "none");
575 }
576 return re;
4623d897 577}
425fd200 578
d62a17ae 579struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
718e3744 580{
d62a17ae 581 struct route_table *table;
582 struct route_node *rn;
5f7a4718 583 struct route_entry *match = NULL;
5f7a4718 584 rib_dest_t *dest;
718e3744 585
d62a17ae 586 /* Lookup table. */
587 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
588 if (!table)
589 return 0;
718e3744 590
d62a17ae 591 rn = route_node_lookup(table, (struct prefix *)p);
718e3744 592
d62a17ae 593 /* No route for this prefix. */
594 if (!rn)
595 return NULL;
718e3744 596
d62a17ae 597 /* Unlock node. */
598 route_unlock_node(rn);
5f7a4718 599 dest = rib_dest_from_rnode(rn);
718e3744 600
996c9314
LB
601 if (dest && dest->selected_fib
602 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
5f7a4718 603 match = dest->selected_fib;
718e3744 604
d62a17ae 605 if (!match)
606 return NULL;
718e3744 607
d62a17ae 608 if (match->type == ZEBRA_ROUTE_CONNECT)
609 return match;
f9e1b38e 610
677c1dd5
DS
611 if (CHECK_FLAG(match->status, ROUTE_ENTRY_INSTALLED))
612 return match;
718e3744 613
d62a17ae 614 return NULL;
718e3744 615}
616
a64448ba
DS
617/*
618 * Is this RIB labeled-unicast? It must be of type BGP and all paths
619 * (nexthops) must have a label.
620 */
d62a17ae 621int zebra_rib_labeled_unicast(struct route_entry *re)
a64448ba 622{
d62a17ae 623 struct nexthop *nexthop = NULL;
a64448ba 624
d62a17ae 625 if (re->type != ZEBRA_ROUTE_BGP)
626 return 0;
a64448ba 627
c415d895 628 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
d62a17ae 629 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
630 return 0;
6b0655a2 631
d62a17ae 632 return 1;
a64448ba 633}
718e3744 634
6ae24471
DS
635/* Update flag indicates whether this is a "replace" or not. Currently, this
636 * is only used for IPv4.
637 */
0c555cc6
DS
638void rib_install_kernel(struct route_node *rn, struct route_entry *re,
639 struct route_entry *old)
718e3744 640{
d62a17ae 641 struct nexthop *nexthop;
630d5962 642 struct rib_table_info *info = srcdest_rnode_table_info(rn);
9a7d1e74 643 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
97f5b441
MS
644 const struct prefix *p, *src_p;
645 enum zebra_dplane_result ret;
646
647 rib_dest_t *dest = rib_dest_from_rnode(rn);
718e3744 648
d62a17ae 649 srcdest_rnode_prefixes(rn, &p, &src_p);
416ec78d 650
d62a17ae 651 if (info->safi != SAFI_UNICAST) {
c415d895 652 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
d62a17ae 653 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 654 return;
d62a17ae 655 }
718e3744 656
9ef49038 657
8dfbc657
SW
658 /*
659 * Install the resolved nexthop object first.
660 */
c415d895 661 zebra_nhg_install_kernel(re->nhe);
8dfbc657 662
2063a814
DS
663 /*
664 * If this is a replace to a new RE let the originator of the RE
665 * know that they've lost
666 */
9a9f8926 667 if (old && (old != re) && (old->type != re->type))
a7704e1b 668 zsend_route_notify_owner(rn, old, ZAPI_ROUTE_BETTER_ADMIN_WON,
77b38a4a 669 info->afi, info->safi);
25b9cb0c 670
97f5b441
MS
671 /* Update fib selection */
672 dest->selected_fib = re;
673
d62a17ae 674 /*
675 * Make sure we update the FPM any time we send new information to
676 * the kernel.
677 */
678 hook_call(rib_update, rn, "installing in kernel");
97f5b441
MS
679
680 /* Send add or update */
8263d1d0 681 if (old)
97f5b441 682 ret = dplane_route_update(rn, re, old);
5709131c 683 else
97f5b441 684 ret = dplane_route_add(rn, re);
97f5b441
MS
685
686 switch (ret) {
ea1c14f6 687 case ZEBRA_DPLANE_REQUEST_QUEUED:
60f98b23 688 SET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
efe6c026
MS
689
690 if (old) {
60f98b23 691 SET_FLAG(old->status, ROUTE_ENTRY_QUEUED);
8d4665aa 692 SET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
efe6c026
MS
693
694 /* Free old FIB nexthop group */
7483dcbe 695 UNSET_FLAG(old->status, ROUTE_ENTRY_USE_FIB_NHG);
efe6c026
MS
696 if (old->fib_ng.nexthop) {
697 nexthops_free(old->fib_ng.nexthop);
698 old->fib_ng.nexthop = NULL;
699 }
efe6c026
MS
700 }
701
97f5b441
MS
702 if (zvrf)
703 zvrf->installs_queued++;
1e885672 704 break;
ea1c14f6 705 case ZEBRA_DPLANE_REQUEST_FAILURE:
97f5b441 706 {
97f5b441 707 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
e658173a
DS
708 "%u:%u:%pRN: Failed to enqueue dataplane install",
709 re->vrf_id, re->table, rn);
1e885672 710 break;
97f5b441 711 }
ea1c14f6 712 case ZEBRA_DPLANE_REQUEST_SUCCESS:
97f5b441
MS
713 if (zvrf)
714 zvrf->installs++;
1e885672
DS
715 break;
716 }
d62a17ae 717
0c555cc6 718 return;
718e3744 719}
720
721/* Uninstall the route from kernel. */
0c555cc6 722void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
718e3744 723{
d62a17ae 724 struct nexthop *nexthop;
630d5962 725 struct rib_table_info *info = srcdest_rnode_table_info(rn);
9a7d1e74 726 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
05737783 727
d62a17ae 728 if (info->safi != SAFI_UNICAST) {
677c1dd5 729 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
c415d895 730 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
d6792f9d 731 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 732 return;
d62a17ae 733 }
416ec78d 734
d62a17ae 735 /*
736 * Make sure we update the FPM any time we send new information to
97f5b441 737 * the dataplane.
d62a17ae 738 */
739 hook_call(rib_update, rn, "uninstalling from kernel");
97f5b441
MS
740
741 switch (dplane_route_delete(rn, re)) {
ea1c14f6 742 case ZEBRA_DPLANE_REQUEST_QUEUED:
97f5b441
MS
743 if (zvrf)
744 zvrf->removals_queued++;
1e885672 745 break;
ea1c14f6 746 case ZEBRA_DPLANE_REQUEST_FAILURE:
97f5b441 747 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
e658173a
DS
748 "%u:%pRN: Failed to enqueue dataplane uninstall",
749 re->vrf_id, rn);
1e885672 750 break;
ea1c14f6 751 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1e885672
DS
752 if (zvrf)
753 zvrf->removals++;
754 break;
755 }
718e3744 756
0c555cc6 757 return;
718e3744 758}
759
9fd92e3c
AS
760/*
761 * rib_can_delete_dest
762 *
2951a7a4 763 * Returns true if the given dest can be deleted from the table.
9fd92e3c 764 */
d62a17ae 765static int rib_can_delete_dest(rib_dest_t *dest)
9fd92e3c 766{
045207e2 767 if (re_list_first(&dest->routes)) {
d62a17ae 768 return 0;
769 }
9fd92e3c 770
c86ba6c2
DS
771 /*
772 * Unresolved rnh's are stored on the default route's list
773 *
774 * dest->rnode can also be the source prefix node in an
775 * ipv6 sourcedest table. Fortunately the prefix of a
776 * source prefix node can never be the default prefix.
777 */
778 if (is_default_prefix(&dest->rnode->p))
779 return 0;
780
d62a17ae 781 /*
782 * Don't delete the dest if we have to update the FPM about this
783 * prefix.
784 */
785 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
786 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
787 return 0;
5adc2528 788
d62a17ae 789 return 1;
9fd92e3c
AS
790}
791
f99f1ff5
SP
792void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq,
793 bool rt_delete)
699dae23
DS
794{
795 rib_dest_t *dest = rib_dest_from_rnode(rn);
699dae23
DS
796 struct rnh *rnh;
797
798 /*
50872b08 799 * We are storing the rnh's associated withb
699dae23
DS
800 * the tracked nexthop as a list of the rn's.
801 * Unresolved rnh's are placed at the top
802 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
803 * As such for each rn we need to walk up the tree
804 * and see if any rnh's need to see if they
805 * would match a more specific route
806 */
807 while (rn) {
e658173a 808 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
15569c58 809 zlog_debug(
e658173a
DS
810 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
811 __func__, rn,
15569c58 812 dest ? rnh_list_count(&dest->nht) : 0);
e658173a 813
f99f1ff5
SP
814 if (rt_delete && (!dest || !rnh_list_count(&dest->nht))) {
815 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
816 zlog_debug("%pRN has no tracking NHTs. Bailing",
817 rn);
818 break;
819 }
699dae23
DS
820 if (!dest) {
821 rn = rn->parent;
822 if (rn)
823 dest = rib_dest_from_rnode(rn);
824 continue;
825 }
826 /*
827 * If we have any rnh's stored in the nht list
828 * then we know that this route node was used for
829 * nht resolution and as such we need to call the
830 * nexthop tracking evaluation code
831 */
739c9c90 832 frr_each_safe(rnh_list, &dest->nht, rnh) {
699dae23
DS
833 struct zebra_vrf *zvrf =
834 zebra_vrf_lookup_by_id(rnh->vrf_id);
835 struct prefix *p = &rnh->node->p;
836
e658173a 837 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
bd47f3a3 838 zlog_debug(
5ef3568f
DS
839 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
840 zvrf_name(zvrf), zvrf_id(zvrf), rn,
841 rnh->node, seq, rnh->seqno);
699dae23
DS
842
843 /*
844 * If we have evaluated this node on this pass
845 * already, due to following the tree up
846 * then we know that we can move onto the next
847 * rnh to process.
848 *
849 * Additionally we call zebra_evaluate_rnh
850 * when we gc the dest. In this case we know
851 * that there must be no other re's where
852 * we were originally as such we know that
853 * that sequence number is ok to respect.
854 */
50872b08
DS
855 if (rnh->seqno == seq) {
856 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
857 zlog_debug(
1d5453d6 858 " Node processed and moved already");
699dae23 859 continue;
50872b08 860 }
699dae23
DS
861
862 rnh->seqno = seq;
d597533a
DS
863 zebra_evaluate_rnh(zvrf, family2afi(p->family), 0, p,
864 rnh->safi);
699dae23
DS
865 }
866
867 rn = rn->parent;
868 if (rn)
869 dest = rib_dest_from_rnode(rn);
870 }
871}
872
9fd92e3c
AS
873/*
874 * rib_gc_dest
875 *
876 * Garbage collect the rib dest corresponding to the given route node
877 * if appropriate.
878 *
2951a7a4 879 * Returns true if the dest was deleted, false otherwise.
9fd92e3c 880 */
d62a17ae 881int rib_gc_dest(struct route_node *rn)
9fd92e3c 882{
d62a17ae 883 rib_dest_t *dest;
9fd92e3c 884
d62a17ae 885 dest = rib_dest_from_rnode(rn);
886 if (!dest)
887 return 0;
9fd92e3c 888
d62a17ae 889 if (!rib_can_delete_dest(dest))
890 return 0;
9fd92e3c 891
c9abf558
DS
892 if (IS_ZEBRA_DEBUG_RIB) {
893 struct zebra_vrf *zvrf;
894
895 zvrf = rib_dest_vrf(dest);
d62a17ae 896 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
c9abf558 897 }
9fd92e3c 898
f99f1ff5
SP
899 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
900 true);
699dae23 901
d62a17ae 902 dest->rnode = NULL;
aa57abfb 903 rnh_list_fini(&dest->nht);
d62a17ae 904 XFREE(MTYPE_RIB_DEST, dest);
905 rn->info = NULL;
9fd92e3c 906
d62a17ae 907 /*
908 * Release the one reference that we keep on the route node.
909 */
910 route_unlock_node(rn);
911 return 1;
9fd92e3c
AS
912}
913
224ccf29
DL
914void zebra_rtable_node_cleanup(struct route_table *table,
915 struct route_node *node)
916{
917 struct route_entry *re, *next;
918
919 RNODE_FOREACH_RE_SAFE (node, re, next) {
920 rib_unlink(node, re);
921 }
922
923 if (node->info) {
924 rib_dest_t *dest = node->info;
925
0d0f516c 926 /* Remove from update queue of FPM module */
927 hook_call(rib_shutdown, node);
928
224ccf29
DL
929 rnh_list_fini(&dest->nht);
930 XFREE(MTYPE_RIB_DEST, node->info);
931 }
932}
933
d62a17ae 934static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
935 struct route_entry *new)
3e5c6e00 936{
d62a17ae 937 hook_call(rib_update, rn, "new route selected");
3e5c6e00 938
d62a17ae 939 /* Update real nexthop. This may actually determine if nexthop is active
940 * or not. */
c415d895 941 if (!nexthop_group_active_nexthop_num(&(new->nhe->nhg))) {
d62a17ae 942 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
943 return;
944 }
3e5c6e00 945
e658173a
DS
946 if (IS_ZEBRA_DEBUG_RIB)
947 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
948 zvrf_name(zvrf), zvrf_id(zvrf), new->table, rn, rn,
c2c02b76 949 new, zebra_route_string(new->type));
3e5c6e00 950
d62a17ae 951 /* If labeled-unicast route, install transit LSP. */
952 if (zebra_rib_labeled_unicast(new))
953 zebra_mpls_lsp_install(zvrf, rn, new);
a64448ba 954
3cdba47a 955 rib_install_kernel(rn, new, NULL);
3e5c6e00 956
d62a17ae 957 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 958}
959
d62a17ae 960static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
961 struct route_entry *old)
3e5c6e00 962{
d62a17ae 963 hook_call(rib_update, rn, "removing existing route");
3e5c6e00 964
d62a17ae 965 /* Uninstall from kernel. */
e658173a
DS
966 if (IS_ZEBRA_DEBUG_RIB)
967 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
968 zvrf_name(zvrf), zvrf_id(zvrf), old->table, rn, rn,
c2c02b76 969 old, zebra_route_string(old->type));
3e5c6e00 970
d62a17ae 971 /* If labeled-unicast route, uninstall transit LSP. */
972 if (zebra_rib_labeled_unicast(old))
973 zebra_mpls_lsp_uninstall(zvrf, rn, old);
974
3cdba47a 975 rib_uninstall_kernel(rn, old);
d62a17ae 976
977 /* Update nexthop for route, reset changed flag. */
ef57f35f
DL
978 /* Note: this code also handles the Linux case when an interface goes
979 * down, causing the kernel to delete routes without sending DELROUTE
980 * notifications
981 */
6883bf8d 982 if (RIB_KERNEL_ROUTE(old))
212df1de
PG
983 SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
984 else
985 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
d62a17ae 986}
987
988static void rib_process_update_fib(struct zebra_vrf *zvrf,
989 struct route_node *rn,
990 struct route_entry *old,
991 struct route_entry *new)
992{
d62a17ae 993 int nh_active = 0;
d62a17ae 994
995 /*
996 * We have to install or update if a new route has been selected or
997 * something has changed.
998 */
999 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1000 hook_call(rib_update, rn, "updating existing route");
1001
1002 /* Update the nexthop; we could determine here that nexthop is
1003 * inactive. */
c415d895 1004 if (nexthop_group_active_nexthop_num(&(new->nhe->nhg)))
d62a17ae 1005 nh_active = 1;
1006
1007 /* If nexthop is active, install the selected route, if
1008 * appropriate. If
1009 * the install succeeds, cleanup flags for prior route, if
1010 * different from
1011 * newly selected.
1012 */
1013 if (nh_active) {
1014 if (IS_ZEBRA_DEBUG_RIB) {
d62a17ae 1015 if (new != old)
1016 zlog_debug(
e658173a 1017 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
bd47f3a3 1018 zvrf_name(zvrf), zvrf_id(zvrf),
e658173a 1019 new->table, rn, rn, new,
2da33d6b
DS
1020 zebra_route_string(new->type),
1021 old,
1022 zebra_route_string(old->type));
d62a17ae 1023 else
1024 zlog_debug(
e658173a 1025 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
bd47f3a3 1026 zvrf_name(zvrf), zvrf_id(zvrf),
e658173a 1027 new->table, rn, rn, new,
2da33d6b 1028 zebra_route_string(new->type));
d62a17ae 1029 }
1030
1031 /* If labeled-unicast route, uninstall transit LSP. */
1032 if (zebra_rib_labeled_unicast(old))
1033 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1034
3cdba47a
DS
1035 /*
1036 * Non-system route should be installed.
1037 * If labeled-unicast route, install transit
1038 * LSP.
1039 */
1040 if (zebra_rib_labeled_unicast(new))
1041 zebra_mpls_lsp_install(zvrf, rn, new);
d62a17ae 1042
3cdba47a 1043 rib_install_kernel(rn, new, old);
d62a17ae 1044 }
a64448ba 1045
d62a17ae 1046 /*
1047 * If nexthop for selected route is not active or install
1048 * failed, we
1049 * may need to uninstall and delete for redistribution.
1050 */
ed216282 1051 if (!nh_active) {
d62a17ae 1052 if (IS_ZEBRA_DEBUG_RIB) {
d62a17ae 1053 if (new != old)
1054 zlog_debug(
e658173a 1055 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
bd47f3a3 1056 zvrf_name(zvrf), zvrf_id(zvrf),
e658173a 1057 new->table, rn, rn, new,
2da33d6b
DS
1058 zebra_route_string(new->type),
1059 old,
1060 zebra_route_string(old->type));
d62a17ae 1061 else
1062 zlog_debug(
e658173a 1063 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
bd47f3a3 1064 zvrf_name(zvrf), zvrf_id(zvrf),
e658173a 1065 new->table, rn, rn, new,
2da33d6b 1066 zebra_route_string(new->type));
d62a17ae 1067 }
1068
5c18e662
DS
1069 /*
1070 * When we have gotten to this point
1071 * the new route entry has no nexthops
1072 * that are usable and as such we need
1073 * to remove the old route, but only
1074 * if we were the one who installed
1075 * the old route
1076 */
1077 if (!RIB_SYSTEM_ROUTE(old)) {
1078 /* If labeled-unicast route, uninstall transit
1079 * LSP. */
1080 if (zebra_rib_labeled_unicast(old))
1081 zebra_mpls_lsp_uninstall(zvrf, rn, old);
d62a17ae 1082
5c18e662
DS
1083 rib_uninstall_kernel(rn, old);
1084 }
d62a17ae 1085 }
1086 } else {
1087 /*
1088 * Same route selected; check if in the FIB and if not,
3cdba47a
DS
1089 * re-install. This is housekeeping code to deal with
1090 * race conditions in kernel with linux netlink reporting
1091 * interface up before IPv4 or IPv6 protocol is ready
d62a17ae 1092 * to add routes.
1093 */
3cdba47a
DS
1094 if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) ||
1095 RIB_SYSTEM_ROUTE(new))
677c1dd5 1096 rib_install_kernel(rn, new, NULL);
d62a17ae 1097 }
1098
1099 /* Update prior route. */
6883bf8d 1100 if (new != old)
d62a17ae 1101 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1102
d62a17ae 1103 /* Clear changed flag. */
1104 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1105}
1106
d62a17ae 1107/* Check if 'alternate' RIB entry is better than 'current'. */
1108static struct route_entry *rib_choose_best(struct route_entry *current,
1109 struct route_entry *alternate)
1110{
1111 if (current == NULL)
1112 return alternate;
1113
1114 /* filter route selection in following order:
1115 * - connected beats other types
fec4ca19 1116 * - if both connected, loopback or vrf wins
d62a17ae 1117 * - lower distance beats higher
1118 * - lower metric beats higher for equal distance
1119 * - last, hence oldest, route wins tie break.
1120 */
1121
fec4ca19
DS
1122 /* Connected routes. Check to see if either are a vrf
1123 * or loopback interface. If not, pick the last connected
d62a17ae 1124 * route of the set of lowest metric connected routes.
1125 */
1126 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
fec4ca19
DS
1127 if (current->type != ZEBRA_ROUTE_CONNECT)
1128 return alternate;
1129
1130 /* both are connected. are either loop or vrf? */
1131 struct nexthop *nexthop = NULL;
1132
c415d895 1133 for (ALL_NEXTHOPS(alternate->nhe->nhg, nexthop)) {
873fde8c 1134 struct interface *ifp = if_lookup_by_index(
1135 nexthop->ifindex, alternate->vrf_id);
1136
608c8870 1137 if (ifp && if_is_loopback(ifp))
fec4ca19
DS
1138 return alternate;
1139 }
1140
c415d895 1141 for (ALL_NEXTHOPS(current->nhe->nhg, nexthop)) {
873fde8c 1142 struct interface *ifp = if_lookup_by_index(
1143 nexthop->ifindex, current->vrf_id);
1144
608c8870 1145 if (ifp && if_is_loopback(ifp))
fec4ca19
DS
1146 return current;
1147 }
1148
1149 /* Neither are loop or vrf so pick best metric */
1150 if (alternate->metric <= current->metric)
d62a17ae 1151 return alternate;
1152
1153 return current;
1154 }
3e5c6e00 1155
d62a17ae 1156 if (current->type == ZEBRA_ROUTE_CONNECT)
1157 return current;
3e5c6e00 1158
d62a17ae 1159 /* higher distance loses */
1160 if (alternate->distance < current->distance)
1161 return alternate;
1162 if (current->distance < alternate->distance)
1163 return current;
3e5c6e00 1164
d62a17ae 1165 /* metric tie-breaks equal distance */
1166 if (alternate->metric <= current->metric)
1167 return alternate;
3e5c6e00 1168
d62a17ae 1169 return current;
3e5c6e00 1170}
1171
d62a17ae 1172/* Core function for processing routing information base. */
1173static void rib_process(struct route_node *rn)
1174{
1175 struct route_entry *re;
1176 struct route_entry *next;
1177 struct route_entry *old_selected = NULL;
1178 struct route_entry *new_selected = NULL;
1179 struct route_entry *old_fib = NULL;
1180 struct route_entry *new_fib = NULL;
1181 struct route_entry *best = NULL;
d62a17ae 1182 rib_dest_t *dest;
1183 struct zebra_vrf *zvrf = NULL;
bd47f3a3 1184 struct vrf *vrf;
86391e56 1185
d62a17ae 1186 vrf_id_t vrf_id = VRF_UNKNOWN;
1187
1188 assert(rn);
1189
1190 dest = rib_dest_from_rnode(rn);
f0afc61d
DS
1191 /*
1192 * We have an enqueued node with nothing to process here
1193 * let's just finish up and return;
1194 */
1195 if (!dest)
1196 return;
bab85d4f 1197
f0afc61d
DS
1198 zvrf = rib_dest_vrf(dest);
1199 vrf_id = zvrf_id(zvrf);
bd47f3a3 1200
bd47f3a3 1201 vrf = vrf_lookup_by_id(vrf_id);
bab85d4f 1202
607425e5
DS
1203 /*
1204 * we can have rn's that have a NULL info pointer
1205 * (dest). As such let's not let the deref happen
1206 * additionally we know RNODE_FOREACH_RE_SAFE
1207 * will not iterate so we are ok.
1208 */
f0afc61d
DS
1209 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1210 struct route_entry *re = re_list_first(&dest->routes);
f5158712 1211
e658173a
DS
1212 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1213 VRF_LOGNAME(vrf), vrf_id, re->table, rn,
f0afc61d 1214 rn);
f5158712 1215 }
5f7a4718 1216
f0afc61d
DS
1217 old_fib = dest->selected_fib;
1218
a2addae8 1219 RNODE_FOREACH_RE_SAFE (rn, re, next) {
61e6de9d
DS
1220 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1221 char flags_buf[128];
1222 char status_buf[128];
1223
d62a17ae 1224 zlog_debug(
e658173a
DS
1225 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1226 VRF_LOGNAME(vrf), vrf_id, re->table, rn, re,
61e6de9d
DS
1227 zebra_route_string(re->type),
1228 _dump_re_status(re, status_buf,
1229 sizeof(status_buf)),
1230 zclient_dump_route_flags(re->flags, flags_buf,
1231 sizeof(flags_buf)),
1232 re->distance, re->metric);
1233 }
d62a17ae 1234
d62a17ae 1235 /* Currently selected re. */
1236 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1237 assert(old_selected == NULL);
1238 old_selected = re;
1239 }
bab85d4f 1240
d62a17ae 1241 /* Skip deleted entries from selection */
1242 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1243 continue;
1244
9d221fac
DS
1245 /*
1246 * If the route entry has changed, verify/resolve
1247 * the nexthops associated with the entry.
1248 *
1249 * In any event if we have nexthops that are not active
1250 * then we cannot use this particular route entry so
1251 * skip it.
d62a17ae 1252 */
9d221fac
DS
1253 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1254 if (!nexthop_active_update(rn, re)) {
7e010c4b
DS
1255 const struct prefix *p;
1256 struct rib_table_info *info;
1257
9d221fac
DS
1258 if (re->type == ZEBRA_ROUTE_TABLE) {
1259 /* XXX: HERE BE DRAGONS!!!!!
1260 * In all honesty, I have not yet
1261 * figured out what this part does or
1262 * why the ROUTE_ENTRY_CHANGED test
1263 * above is correct or why we need to
1264 * delete a route here, and also not
1265 * whether this concerns both selected
1266 * and fib route, or only selected
1267 * or only fib
1268 *
1269 * This entry was denied by the 'ip
1270 * protocol
1271 * table' route-map, we need to delete
1272 * it */
1273 if (re != old_selected) {
1274 if (IS_ZEBRA_DEBUG_RIB)
1275 zlog_debug(
e658173a 1276 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
9d221fac
DS
1277 __func__,
1278 VRF_LOGNAME(
1279 vrf),
e658173a 1280 vrf_id, rn);
9d221fac 1281 rib_unlink(rn, re);
e96817f8 1282 continue;
9d221fac
DS
1283 } else
1284 SET_FLAG(re->status,
1285 ROUTE_ENTRY_REMOVED);
1286 }
d62a17ae 1287
7e010c4b
DS
1288 info = srcdest_rnode_table_info(rn);
1289 srcdest_rnode_prefixes(rn, &p, NULL);
a7704e1b
DS
1290 zsend_route_notify_owner(
1291 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
1292 info->afi, info->safi);
9d221fac
DS
1293 continue;
1294 }
1295 } else {
1296 /*
1297 * If the re has not changed and the nhg we have is
1298 * not usable, then we cannot use this route entry
1299 * for consideration, as that the route will just
1300 * not install if it is selected.
1301 */
1302 if (!nexthop_group_active_nexthop_num(&re->nhe->nhg))
1303 continue;
d62a17ae 1304 }
bab85d4f 1305
d62a17ae 1306 /* Infinite distance. */
3332f4f0
DS
1307 if (re->distance == DISTANCE_INFINITY &&
1308 re->type != ZEBRA_ROUTE_KERNEL) {
d62a17ae 1309 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1310 continue;
1311 }
bab85d4f 1312
d62a17ae 1313 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1314 best = rib_choose_best(new_fib, re);
1315 if (new_fib && best != new_fib)
1316 UNSET_FLAG(new_fib->status,
1317 ROUTE_ENTRY_CHANGED);
1318 new_fib = best;
1319 } else {
1320 best = rib_choose_best(new_selected, re);
1321 if (new_selected && best != new_selected)
1322 UNSET_FLAG(new_selected->status,
1323 ROUTE_ENTRY_CHANGED);
1324 new_selected = best;
1325 }
1326 if (best != re)
1327 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1328 } /* RNODE_FOREACH_RE */
1329
1330 /* If no FIB override route, use the selected route also for FIB */
1331 if (new_fib == NULL)
1332 new_fib = new_selected;
1333
1334 /* After the cycle is finished, the following pointers will be set:
1335 * old_selected --- RE entry currently having SELECTED
1336 * new_selected --- RE entry that is newly SELECTED
1337 * old_fib --- RE entry currently in kernel FIB
1338 * new_fib --- RE entry that is newly to be in kernel FIB
1339 *
1340 * new_selected will get SELECTED flag, and is going to be redistributed
1341 * the zclients. new_fib (which can be new_selected) will be installed
1342 * in kernel.
1343 */
1344
1345 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
c2c02b76
DS
1346 struct route_entry *entry;
1347
1348 entry = old_selected
1349 ? old_selected
1350 : new_selected
1351 ? new_selected
1352 : old_fib ? old_fib
1353 : new_fib ? new_fib : NULL;
1354
d62a17ae 1355 zlog_debug(
e658173a
DS
1356 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1357 VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn,
c2c02b76
DS
1358 (void *)old_selected, (void *)new_selected,
1359 (void *)old_fib, (void *)new_fib);
d62a17ae 1360 }
446bb95e 1361
d62a17ae 1362 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1363 * fib == selected */
9d303b37
DL
1364 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1365 ROUTE_ENTRY_CHANGED);
d62a17ae 1366
8cb41cd6 1367 /* Update SELECTED entry */
d62a17ae 1368 if (old_selected != new_selected || selected_changed) {
93bdadae 1369
6883bf8d 1370 if (new_selected && new_selected != new_fib)
d62a17ae 1371 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
41ec9222 1372
3cdba47a 1373 if (new_selected)
5af4b346
MS
1374 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1375
d62a17ae 1376 if (old_selected) {
40f321c0
MS
1377 /*
1378 * If we're removing the old entry, we should tell
1379 * redist subscribers about that *if* they aren't
1380 * going to see a redist for the new entry.
1381 */
1382 if (!new_selected || CHECK_FLAG(old_selected->status,
1383 ROUTE_ENTRY_REMOVED))
b3a9fca1 1384 redistribute_delete(rn, old_selected,
40f321c0
MS
1385 new_selected);
1386
d62a17ae 1387 if (old_selected != new_selected)
1388 UNSET_FLAG(old_selected->flags,
1389 ZEBRA_FLAG_SELECTED);
f857321e 1390 }
d62a17ae 1391 }
3e5c6e00 1392
f68ce976
DL
1393 /* Update fib according to selection results */
1394 if (new_fib && old_fib)
1395 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1396 else if (new_fib)
1397 rib_process_add_fib(zvrf, rn, new_fib);
1398 else if (old_fib)
1399 rib_process_del_fib(zvrf, rn, old_fib);
1400
d62a17ae 1401 /* Remove all RE entries queued for removal */
a2addae8 1402 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1403 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1404 if (IS_ZEBRA_DEBUG_RIB) {
1405 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1406 (void *)rn, (void *)re);
1407 }
1408 rib_unlink(rn, re);
1409 }
1410 }
4d38fdb4 1411
d62a17ae 1412 /*
1413 * Check if the dest can be deleted now.
1414 */
1415 rib_gc_dest(rn);
e96f9203
DO
1416}
1417
a1494c25
DS
1418static void zebra_rib_evaluate_mpls(struct route_node *rn)
1419{
1420 rib_dest_t *dest = rib_dest_from_rnode(rn);
9a7d1e74 1421 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(VRF_DEFAULT);
a1494c25
DS
1422
1423 if (!dest)
1424 return;
1425
1426 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1427 if (IS_ZEBRA_DEBUG_MPLS)
1428 zlog_debug(
bd47f3a3
JU
1429 "%s(%u): Scheduling all LSPs upon RIB completion",
1430 zvrf_name(zvrf), zvrf_id(zvrf));
a1494c25
DS
1431 zebra_mpls_lsp_schedule(zvrf);
1432 mpls_unmark_lsps_for_processing(rn);
1433 }
1434}
1435
e5ac2adf
MS
1436/*
1437 * Utility to match route with dplane context data
1438 */
1439static bool rib_route_match_ctx(const struct route_entry *re,
25779064
MS
1440 const struct zebra_dplane_ctx *ctx,
1441 bool is_update)
e5ac2adf
MS
1442{
1443 bool result = false;
1444
1445 if (is_update) {
1446 /*
1447 * In 'update' case, we test info about the 'previous' or
1448 * 'old' route
1449 */
1450 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1451 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1452 result = true;
1453
9db35a5e
MS
1454 /* We use an extra test for statics, and another for
1455 * kernel routes.
e5ac2adf
MS
1456 */
1457 if (re->type == ZEBRA_ROUTE_STATIC &&
1458 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1459 re->tag != dplane_ctx_get_old_tag(ctx))) {
1460 result = false;
9db35a5e
MS
1461 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1462 re->metric !=
1463 dplane_ctx_get_old_metric(ctx)) {
1464 result = false;
e5ac2adf
MS
1465 }
1466 }
1467
1468 } else {
1469 /*
1470 * Ordinary, single-route case using primary context info
1471 */
1472 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1473 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1474 /* Skip route that's been deleted */
1475 goto done;
1476 }
1477
1478 if ((re->type == dplane_ctx_get_type(ctx)) &&
1479 (re->instance == dplane_ctx_get_instance(ctx))) {
1480 result = true;
1481
9db35a5e
MS
1482 /* We use an extra test for statics, and another for
1483 * kernel routes.
e5ac2adf
MS
1484 */
1485 if (re->type == ZEBRA_ROUTE_STATIC &&
1486 (re->distance != dplane_ctx_get_distance(ctx) ||
1487 re->tag != dplane_ctx_get_tag(ctx))) {
1488 result = false;
9db35a5e
MS
1489 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1490 re->metric != dplane_ctx_get_metric(ctx)) {
1491 result = false;
1492 } else if (re->type == ZEBRA_ROUTE_CONNECT) {
1493 result = nexthop_group_equal_no_recurse(
1494 &re->nhe->nhg, dplane_ctx_get_ng(ctx));
e5ac2adf
MS
1495 }
1496 }
1497 }
1498
1499done:
871a16cd 1500 return result;
e5ac2adf
MS
1501}
1502
3cdba47a
DS
1503static void zebra_rib_fixup_system(struct route_node *rn)
1504{
1505 struct route_entry *re;
1506
1507 RNODE_FOREACH_RE(rn, re) {
1508 struct nexthop *nhop;
1509
1510 if (!RIB_SYSTEM_ROUTE(re))
1511 continue;
1512
1513 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1514 continue;
1515
1516 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
6d0ee6a0 1517 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
8d4665aa 1518 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
3cdba47a 1519
c415d895 1520 for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) {
3cdba47a
DS
1521 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1522 continue;
1523
1524 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1525 }
1526 }
1527}
1528
9db35a5e
MS
1529/* Route comparison logic, with various special cases. */
1530static bool rib_compare_routes(const struct route_entry *re1,
1531 const struct route_entry *re2)
1532{
9db35a5e
MS
1533 if (re1->type != re2->type)
1534 return false;
1535
1536 if (re1->instance != re2->instance)
1537 return false;
1538
1539 if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric)
1540 return false;
1541
1542 if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
1543 re1->distance != re2->distance)
1544 return false;
1545
87009d7d
MS
1546 /* We support multiple connected routes: this supports multiple
1547 * v6 link-locals, and we also support multiple addresses in the same
1548 * subnet on a single interface.
1549 */
9db35a5e
MS
1550 if (re1->type != ZEBRA_ROUTE_CONNECT)
1551 return true;
1552
87009d7d 1553 return false;
9db35a5e
MS
1554}
1555
9959f1da
MS
1556/*
1557 * Compare nexthop lists from a route and a dplane context; test whether
1558 * the list installed in the FIB matches the route's list.
1559 * Set 'changed_p' to 'true' if there were changes to the route's
1560 * installed nexthops.
1561 *
1562 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1563 * list.
1564 */
1565static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg,
1566 const struct nexthop_group *ctx_nhg,
1567 bool *changed_p)
1568{
1569 bool matched_p = true;
1570 struct nexthop *nexthop, *ctx_nexthop;
1571
1572 /* Get the first `installed` one to check against.
1573 * If the dataplane doesn't set these to be what was actually installed,
1574 * it will just be whatever was in re->nhe->nhg?
1575 */
1576 ctx_nexthop = ctx_nhg->nexthop;
1577
1578 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
1579 || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1580 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1581
1582 for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) {
1583
1584 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1585 continue;
1586
1587 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1588 continue;
1589
1590 /* Check for a FIB nexthop corresponding to the RIB nexthop */
7483dcbe 1591 if (!nexthop_same(ctx_nexthop, nexthop)) {
9959f1da
MS
1592 /* If the FIB doesn't know about the nexthop,
1593 * it's not installed
1594 */
1595 if (IS_ZEBRA_DEBUG_RIB_DETAILED ||
1596 IS_ZEBRA_DEBUG_NHG_DETAIL) {
1597 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1598 __func__, nexthop,
1599 (CHECK_FLAG(nexthop->flags,
1600 NEXTHOP_FLAG_FIB) ?
1601 "(FIB)":""));
1602 }
1603 matched_p = false;
1604
1605 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1606 *changed_p = true;
1607
1608 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1609
1610 /* Keep checking nexthops */
1611 continue;
1612 }
1613
1614 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
1615 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1616 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1617 zlog_debug("%s: rib nh %pNHv -> installed",
1618 __func__, nexthop);
1619
1620 *changed_p = true;
1621 }
1622
1623 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1624 } else {
1625 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1626 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1627 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1628 __func__, nexthop);
1629
1630 *changed_p = true;
1631 }
1632
1633 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1634 }
1635
1636 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1637 }
1638
1639 return matched_p;
1640}
1641
e5ac2adf 1642/*
78bf56b0
MS
1643 * Update a route from a dplane context. This consolidates common code
1644 * that can be used in processing of results from FIB updates, and in
1645 * async notification processing.
efe6c026 1646 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
e5ac2adf 1647 */
efe6c026
MS
1648static bool rib_update_re_from_ctx(struct route_entry *re,
1649 struct route_node *rn,
1650 struct zebra_dplane_ctx *ctx)
78bf56b0 1651{
9959f1da 1652 struct nexthop *nexthop;
941e261c
MS
1653 bool matched;
1654 const struct nexthop_group *ctxnhg;
9959f1da 1655 struct nexthop_group *re_nhg;
efe6c026
MS
1656 bool is_selected = false; /* Is 're' currently the selected re? */
1657 bool changed_p = false; /* Change to nexthops? */
1658 rib_dest_t *dest;
bd47f3a3
JU
1659 struct vrf *vrf;
1660
1661 vrf = vrf_lookup_by_id(re->vrf_id);
78bf56b0 1662
efe6c026
MS
1663 dest = rib_dest_from_rnode(rn);
1664 if (dest)
1665 is_selected = (re == dest->selected_fib);
1666
1667 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2919eea8
DS
1668 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1669 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
9db35a5e 1670 (is_selected ? "" : "NOT "), re);
efe6c026
MS
1671
1672 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
941e261c 1673 * If the installed set differs from the set requested by the rib/owner,
efe6c026
MS
1674 * we use the fib-specific nexthop-group to record the actual FIB
1675 * status.
78bf56b0 1676 */
a126f120
MS
1677 matched = false;
1678 ctxnhg = dplane_ctx_get_ng(ctx);
941e261c 1679
9959f1da 1680 /* Check route's fib group and incoming notif group for equivalence.
986a6617
SW
1681 *
1682 * Let's assume the nexthops are ordered here to save time.
941e261c 1683 */
9959f1da
MS
1684 /* TODO -- this isn't testing or comparing the FIB flags; we should
1685 * do a more explicit loop, checking the incoming notification's flags.
1686 */
a126f120
MS
1687 if (re->fib_ng.nexthop && ctxnhg->nexthop &&
1688 nexthop_group_equal(&re->fib_ng, ctxnhg))
986a6617 1689 matched = true;
941e261c
MS
1690
1691 /* If the new FIB set matches the existing FIB set, we're done. */
1692 if (matched) {
1693 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1694 zlog_debug(
2919eea8
DS
1695 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1696 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
9959f1da 1697 goto check_backups;
941e261c 1698
7483dcbe 1699 } else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) {
941e261c
MS
1700 /*
1701 * Free stale fib list and move on to check the rib nhg.
1702 */
1703 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1704 zlog_debug(
2919eea8
DS
1705 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1706 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
941e261c
MS
1707 nexthops_free(re->fib_ng.nexthop);
1708 re->fib_ng.nexthop = NULL;
efe6c026 1709
7483dcbe
MS
1710 UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1711
efe6c026
MS
1712 /* Note that the installed nexthops have changed */
1713 changed_p = true;
941e261c
MS
1714 } else {
1715 if (IS_ZEBRA_DEBUG_RIB)
2919eea8
DS
1716 zlog_debug(
1717 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1718 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
941e261c
MS
1719 }
1720
1721 /*
1722 * Compare with the rib nexthop group. The comparison here is different:
1723 * the RIB group may be a superset of the list installed in the FIB. We
1724 * walk the RIB group, looking for the 'installable' candidate
1725 * nexthops, and then check those against the set
1726 * that is actually installed.
986a6617
SW
1727 *
1728 * Assume nexthops are ordered here as well.
941e261c 1729 */
986a6617 1730
9959f1da 1731 /* If nothing is installed, we can skip some of the checking/comparison
1f6a5aca
MS
1732 * of nexthops.
1733 */
9959f1da 1734 if (ctxnhg->nexthop == NULL) {
1f6a5aca
MS
1735 changed_p = true;
1736 goto no_nexthops;
1737 }
1738
9959f1da 1739 matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p);
78bf56b0 1740
941e261c
MS
1741 /* If all nexthops were processed, we're done */
1742 if (matched) {
1743 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1744 zlog_debug(
2919eea8
DS
1745 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1746 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1747 (changed_p ? "true" : "false"));
9959f1da 1748 goto check_backups;
941e261c 1749 }
78bf56b0 1750
1f6a5aca
MS
1751no_nexthops:
1752
941e261c
MS
1753 /* FIB nexthop set differs from the RIB set:
1754 * create a fib-specific nexthop-group
1755 */
1756 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1757 zlog_debug(
2919eea8
DS
1758 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1759 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
7483dcbe
MS
1760 (changed_p ? "true" : "false"),
1761 ctxnhg->nexthop != NULL ? "" : " (empty)");
941e261c 1762
7483dcbe 1763 /* Set the flag about the dedicated fib list */
8ce0e517
DS
1764 if (zrouter.asic_notification_nexthop_control) {
1765 SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1766 if (ctxnhg->nexthop)
1767 copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop,
1768 NULL);
1769 }
941e261c 1770
9959f1da
MS
1771check_backups:
1772
1773 /*
1774 * Check the status of the route's backup nexthops, if any.
1775 * The logic for backups is somewhat different: if any backup is
1776 * installed, a new fib nhg will be attached to the route.
1777 */
1778 re_nhg = zebra_nhg_get_backup_nhg(re->nhe);
1779 if (re_nhg == NULL)
1780 goto done; /* No backup nexthops */
1781
1782 /* First check the route's 'fib' list of backups, if it's present
1783 * from some previous event.
1784 */
1785 re_nhg = &re->fib_backup_ng;
1786 ctxnhg = dplane_ctx_get_backup_ng(ctx);
1787
1788 matched = false;
1789 if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg))
1790 matched = true;
1791
1792 /* If the new FIB set matches an existing FIB set, we're done. */
1793 if (matched) {
1794 if (IS_ZEBRA_DEBUG_RIB)
1795 zlog_debug(
2919eea8
DS
1796 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1797 VRF_LOGNAME(vrf), re->vrf_id, rn);
9959f1da
MS
1798 goto done;
1799
1800 } else if (re->fib_backup_ng.nexthop) {
1801 /*
1802 * Free stale fib backup list and move on to check
1803 * the route's backups.
1804 */
1805 if (IS_ZEBRA_DEBUG_RIB)
1806 zlog_debug(
2919eea8
DS
1807 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1808 VRF_LOGNAME(vrf), re->vrf_id, rn);
9959f1da
MS
1809 nexthops_free(re->fib_backup_ng.nexthop);
1810 re->fib_backup_ng.nexthop = NULL;
1811
1812 /* Note that the installed nexthops have changed */
1813 changed_p = true;
1814 } else {
1815 if (IS_ZEBRA_DEBUG_RIB)
2919eea8
DS
1816 zlog_debug(
1817 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1818 VRF_LOGNAME(vrf), re->vrf_id, rn);
9959f1da
MS
1819 }
1820
1821 /*
7483dcbe 1822 * If a FIB backup nexthop set exists, attach a copy
9959f1da
MS
1823 * to the route if any backup is installed
1824 */
1825 if (ctxnhg && ctxnhg->nexthop) {
1826
1827 for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) {
1828 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1829 break;
1830 }
1831
1832 /* If no installed backups, we're done */
1833 if (nexthop == NULL)
1834 goto done;
1835
1836 if (IS_ZEBRA_DEBUG_RIB)
2919eea8
DS
1837 zlog_debug(
1838 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1839 VRF_LOGNAME(vrf), re->vrf_id, rn,
1840 (changed_p ? "true" : "false"));
9959f1da
MS
1841
1842 copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop,
1843 NULL);
1844 }
1845
941e261c 1846done:
9959f1da 1847
efe6c026 1848 return changed_p;
78bf56b0
MS
1849}
1850
e5ac2adf 1851/*
fad4d69c
MS
1852 * Helper to locate a zebra route-node from a dplane context. This is used
1853 * when processing dplane results, e.g. Note well: the route-node is returned
1854 * with a ref held - route_unlock_node() must be called eventually.
e5ac2adf 1855 */
a7704e1b 1856struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
e5ac2adf
MS
1857{
1858 struct route_table *table = NULL;
1859 struct route_node *rn = NULL;
e5ac2adf
MS
1860 const struct prefix *dest_pfx, *src_pfx;
1861
1862 /* Locate rn and re(s) from ctx */
1863
c7c0b007
SW
1864 table = zebra_vrf_lookup_table_with_table_id(
1865 dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx),
1866 dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx));
e5ac2adf
MS
1867 if (table == NULL) {
1868 if (IS_ZEBRA_DEBUG_DPLANE) {
bd47f3a3
JU
1869 zlog_debug(
1870 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1871 dplane_ctx_get_afi(ctx),
1872 dplane_ctx_get_safi(ctx),
1873 vrf_id_to_name(dplane_ctx_get_vrf(ctx)),
1874 dplane_ctx_get_vrf(ctx));
e5ac2adf
MS
1875 }
1876 goto done;
1877 }
1878
fad4d69c
MS
1879 dest_pfx = dplane_ctx_get_dest(ctx);
1880 src_pfx = dplane_ctx_get_src(ctx);
1881
1882 rn = srcdest_rnode_get(table, dest_pfx,
1883 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1884
1885done:
1886 return rn;
1887}
1888
1889
97f5b441 1890
fad4d69c
MS
1891/*
1892 * Route-update results processing after async dataplane update.
1893 */
1894static void rib_process_result(struct zebra_dplane_ctx *ctx)
1895{
1896 struct zebra_vrf *zvrf = NULL;
bd47f3a3 1897 struct vrf *vrf;
fad4d69c
MS
1898 struct route_node *rn = NULL;
1899 struct route_entry *re = NULL, *old_re = NULL, *rib;
1900 bool is_update = false;
fad4d69c
MS
1901 enum dplane_op_e op;
1902 enum zebra_dplane_result status;
fad4d69c 1903 uint32_t seq;
9db35a5e 1904 rib_dest_t *dest;
efe6c026 1905 bool fib_changed = false;
77b38a4a 1906 struct rib_table_info *info;
f99f1ff5 1907 bool rt_delete = false;
97f5b441 1908
9a7d1e74 1909 zvrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
bd47f3a3 1910 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
e5ac2adf 1911
fad4d69c
MS
1912 /* Locate rn and re(s) from ctx */
1913 rn = rib_find_rn_from_ctx(ctx);
e5ac2adf
MS
1914 if (rn == NULL) {
1915 if (IS_ZEBRA_DEBUG_DPLANE) {
bd47f3a3 1916 zlog_debug(
69a2d597
DS
1917 "Failed to process dplane results: no route for %s(%u):%pRN",
1918 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn);
e5ac2adf
MS
1919 }
1920 goto done;
1921 }
1922
9db35a5e 1923 dest = rib_dest_from_rnode(rn);
77b38a4a 1924 info = srcdest_rnode_table_info(rn);
e5ac2adf
MS
1925
1926 op = dplane_ctx_get_op(ctx);
1927 status = dplane_ctx_get_status(ctx);
1928
c831033f 1929 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 1930 zlog_debug(
69a2d597 1931 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
c2c02b76 1932 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
69a2d597
DS
1933 dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op),
1934 dplane_res2str(status));
e5ac2adf 1935
e5ac2adf
MS
1936 /*
1937 * Update is a bit of a special case, where we may have both old and new
1938 * routes to post-process.
1939 */
1940 is_update = dplane_ctx_is_update(ctx);
1941
1942 /*
1943 * Take a pass through the routes, look for matches with the context
1944 * info.
1945 */
1946 RNODE_FOREACH_RE(rn, rib) {
1947
1948 if (re == NULL) {
5709131c 1949 if (rib_route_match_ctx(rib, ctx, false))
e5ac2adf 1950 re = rib;
e5ac2adf
MS
1951 }
1952
1953 /* Check for old route match */
1954 if (is_update && (old_re == NULL)) {
5709131c 1955 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
e5ac2adf 1956 old_re = rib;
e5ac2adf
MS
1957 }
1958
1959 /* Have we found the routes we need to work on? */
5709131c 1960 if (re && ((!is_update || old_re)))
e5ac2adf 1961 break;
e5ac2adf
MS
1962 }
1963
699dae23
DS
1964 seq = dplane_ctx_get_seq(ctx);
1965
e5ac2adf
MS
1966 /*
1967 * Check sequence number(s) to detect stale results before continuing
1968 */
60f98b23 1969 if (re) {
699dae23 1970 if (re->dplane_sequence != seq) {
60f98b23 1971 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 1972 zlog_debug(
69a2d597 1973 "%s(%u):%pRN Stale dplane result for re %p",
bd47f3a3 1974 VRF_LOGNAME(vrf),
69a2d597 1975 dplane_ctx_get_vrf(ctx), rn, re);
e4876266
DS
1976 } else {
1977 if (!zrouter.asic_offloaded ||
1978 (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) ||
8d4665aa
DS
1979 CHECK_FLAG(re->flags,
1980 ZEBRA_FLAG_OFFLOAD_FAILED))) {
1981 UNSET_FLAG(re->status,
1982 ROUTE_ENTRY_ROUTE_REPLACING);
e4876266 1983 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
8d4665aa 1984 }
e4876266 1985 }
e5ac2adf
MS
1986 }
1987
60f98b23
DS
1988 if (old_re) {
1989 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
1990 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 1991 zlog_debug(
69a2d597 1992 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
bd47f3a3 1993 VRF_LOGNAME(vrf),
c2c02b76 1994 dplane_ctx_get_vrf(ctx), old_re->table,
69a2d597 1995 rn, old_re);
60f98b23 1996 } else
9ef0c6ba 1997 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
1998 }
1999
12e7fe3a 2000 switch (op) {
12e7fe3a
DS
2001 case DPLANE_OP_ROUTE_INSTALL:
2002 case DPLANE_OP_ROUTE_UPDATE:
2003 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2004 if (re) {
2005 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2006 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2007 }
b9f0e5ee
DS
2008 /*
2009 * On an update operation from the same route type
2010 * context retrieval currently has no way to know
2011 * which was the old and which was the new.
2012 * So don't unset our flags that we just set.
2013 * We know redistribution is ok because the
2014 * old_re in this case is used for nothing
2015 * more than knowing whom to contact if necessary.
2016 */
2017 if (old_re && old_re != re) {
677c1dd5
DS
2018 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2019 UNSET_FLAG(old_re->status,
2020 ROUTE_ENTRY_INSTALLED);
2021 }
f183e380 2022
78bf56b0 2023 /* Update zebra route based on the results in
941e261c 2024 * the context struct.
78bf56b0 2025 */
941e261c 2026 if (re) {
efe6c026
MS
2027 fib_changed =
2028 rib_update_re_from_ctx(re, rn, ctx);
2029
2030 if (!fib_changed) {
2031 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 2032 zlog_debug(
69a2d597 2033 "%s(%u:%u):%pRN no fib change for re",
bd47f3a3
JU
2034 VRF_LOGNAME(vrf),
2035 dplane_ctx_get_vrf(ctx),
c2c02b76
DS
2036 dplane_ctx_get_table(
2037 ctx),
69a2d597 2038 rn);
12e7fe3a
DS
2039 }
2040
9db35a5e
MS
2041 /* Redistribute if this is the selected re */
2042 if (dest && re == dest->selected_fib)
ee78ed68 2043 redistribute_update(rn, re, old_re);
f183e380
MS
2044 }
2045
3cdba47a
DS
2046 /*
2047 * System routes are weird in that they
2048 * allow multiple to be installed that match
2049 * to the same prefix, so after we get the
2050 * result we need to clean them up so that
2051 * we can actually use them.
2052 */
2053 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2054 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2055 zebra_rib_fixup_system(rn);
2056
f4c6e2a8 2057 if (zvrf)
12e7fe3a 2058 zvrf->installs++;
f183e380 2059
12e7fe3a 2060 /* Notify route owner */
e4876266
DS
2061 if (zebra_router_notify_on_ack())
2062 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2063 else {
2064 if (re) {
2065 if (CHECK_FLAG(re->flags,
2066 ZEBRA_FLAG_OFFLOADED))
2067 zsend_route_notify_owner_ctx(
2068 ctx,
2069 ZAPI_ROUTE_INSTALLED);
2070 if (CHECK_FLAG(
2071 re->flags,
2072 ZEBRA_FLAG_OFFLOAD_FAILED))
2073 zsend_route_notify_owner_ctx(
2074 ctx,
2075 ZAPI_ROUTE_FAIL_INSTALL);
2076 }
2077 }
12e7fe3a 2078 } else {
7a230a9d 2079 if (re) {
677c1dd5 2080 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
7a230a9d
DS
2081 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2082 } if (old_re)
677c1dd5
DS
2083 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2084 if (re)
a7704e1b
DS
2085 zsend_route_notify_owner(
2086 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
2087 info->afi, info->safi);
97f5b441 2088
69a2d597 2089 zlog_warn("%s(%u:%u):%pRN: Route install failed",
bd47f3a3 2090 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
69a2d597 2091 dplane_ctx_get_table(ctx), rn);
12e7fe3a
DS
2092 }
2093 break;
2094 case DPLANE_OP_ROUTE_DELETE:
f99f1ff5 2095 rt_delete = true;
677c1dd5
DS
2096 if (re)
2097 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2098 /*
2099 * In the delete case, the zebra core datastructs were
2100 * updated (or removed) at the time the delete was issued,
2101 * so we're just notifying the route owner.
e5ac2adf 2102 */
12e7fe3a 2103 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2104 if (re) {
2105 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2106 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2107 }
12e7fe3a 2108 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
e5ac2adf 2109
12e7fe3a
DS
2110 if (zvrf)
2111 zvrf->removals++;
2112 } else {
677c1dd5
DS
2113 if (re)
2114 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2115 zsend_route_notify_owner_ctx(ctx,
2116 ZAPI_ROUTE_REMOVE_FAIL);
e5ac2adf 2117
69a2d597 2118 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
bd47f3a3 2119 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
69a2d597 2120 dplane_ctx_get_table(ctx), rn);
12e7fe3a 2121 }
3cdba47a
DS
2122
2123 /*
2124 * System routes are weird in that they
2125 * allow multiple to be installed that match
2126 * to the same prefix, so after we get the
2127 * result we need to clean them up so that
2128 * we can actually use them.
2129 */
2130 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2131 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2132 zebra_rib_fixup_system(rn);
12e7fe3a 2133 break;
a98701f0
DS
2134
2135 case DPLANE_OP_NONE:
2136 case DPLANE_OP_ROUTE_NOTIFY:
2137 case DPLANE_OP_NH_INSTALL:
2138 case DPLANE_OP_NH_UPDATE:
2139 case DPLANE_OP_NH_DELETE:
2140 case DPLANE_OP_LSP_INSTALL:
2141 case DPLANE_OP_LSP_UPDATE:
2142 case DPLANE_OP_LSP_DELETE:
2143 case DPLANE_OP_LSP_NOTIFY:
2144 case DPLANE_OP_PW_INSTALL:
2145 case DPLANE_OP_PW_UNINSTALL:
2146 case DPLANE_OP_SYS_ROUTE_ADD:
2147 case DPLANE_OP_SYS_ROUTE_DELETE:
2148 case DPLANE_OP_ADDR_INSTALL:
2149 case DPLANE_OP_ADDR_UNINSTALL:
2150 case DPLANE_OP_MAC_INSTALL:
2151 case DPLANE_OP_MAC_DELETE:
2152 case DPLANE_OP_NEIGH_INSTALL:
2153 case DPLANE_OP_NEIGH_UPDATE:
2154 case DPLANE_OP_NEIGH_DELETE:
2155 case DPLANE_OP_VTEP_ADD:
2156 case DPLANE_OP_VTEP_DELETE:
2157 case DPLANE_OP_RULE_ADD:
2158 case DPLANE_OP_RULE_DELETE:
2159 case DPLANE_OP_RULE_UPDATE:
2160 case DPLANE_OP_NEIGH_DISCOVER:
2161 case DPLANE_OP_BR_PORT_UPDATE:
2162 case DPLANE_OP_IPTABLE_ADD:
2163 case DPLANE_OP_IPTABLE_DELETE:
2164 case DPLANE_OP_IPSET_ADD:
2165 case DPLANE_OP_IPSET_DELETE:
2166 case DPLANE_OP_IPSET_ENTRY_ADD:
2167 case DPLANE_OP_IPSET_ENTRY_DELETE:
2168 case DPLANE_OP_NEIGH_IP_INSTALL:
2169 case DPLANE_OP_NEIGH_IP_DELETE:
2170 case DPLANE_OP_NEIGH_TABLE_UPDATE:
2171 case DPLANE_OP_GRE_SET:
2172 case DPLANE_OP_INTF_ADDR_ADD:
2173 case DPLANE_OP_INTF_ADDR_DEL:
2174 case DPLANE_OP_INTF_NETCONFIG:
2175 case DPLANE_OP_INTF_INSTALL:
2176 case DPLANE_OP_INTF_UPDATE:
2177 case DPLANE_OP_INTF_DELETE:
2178 case DPLANE_OP_TC_QDISC_INSTALL:
2179 case DPLANE_OP_TC_QDISC_UNINSTALL:
2180 case DPLANE_OP_TC_CLASS_ADD:
2181 case DPLANE_OP_TC_CLASS_DELETE:
2182 case DPLANE_OP_TC_CLASS_UPDATE:
2183 case DPLANE_OP_TC_FILTER_ADD:
2184 case DPLANE_OP_TC_FILTER_DELETE:
2185 case DPLANE_OP_TC_FILTER_UPDATE:
d37f4d6c 2186 break;
e5ac2adf 2187 }
699dae23 2188
f99f1ff5 2189 zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete);
a1494c25 2190 zebra_rib_evaluate_mpls(rn);
e5ac2adf
MS
2191done:
2192
5f27bcba
DS
2193 if (rn)
2194 route_unlock_node(rn);
e5ac2adf
MS
2195}
2196
9959f1da
MS
2197/*
2198 * Count installed/FIB nexthops
2199 */
2200static int rib_count_installed_nh(struct route_entry *re)
2201{
2202 int count = 0;
2203 struct nexthop *nexthop;
2204 struct nexthop_group *nhg;
2205
2206 nhg = rib_get_fib_nhg(re);
2207
2208 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2209 /* The meaningful flag depends on where the installed
2210 * nexthops reside.
2211 */
43a9f66c 2212 if (nhg == &(re->fib_ng)) {
9959f1da
MS
2213 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2214 count++;
2215 } else {
2216 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2217 count++;
2218 }
2219 }
2220
43a9f66c
MS
2221 nhg = rib_get_fib_backup_nhg(re);
2222 if (nhg) {
2223 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2224 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2225 count++;
2226 }
9959f1da
MS
2227 }
2228
2229 return count;
2230}
2231
54818e3b
MS
2232/*
2233 * Handle notification from async dataplane: the dataplane has detected
2234 * some change to a route, and notifies zebra so that the control plane
2235 * can reflect that change.
2236 */
2237static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2238{
2239 struct route_node *rn = NULL;
efe6c026 2240 struct route_entry *re = NULL;
bd47f3a3 2241 struct vrf *vrf;
efe6c026 2242 struct nexthop *nexthop;
941e261c 2243 rib_dest_t *dest;
efe6c026 2244 bool fib_changed = false;
941e261c 2245 bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
efe6c026 2246 int start_count, end_count;
898b4a6d 2247
bd47f3a3 2248 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
54818e3b 2249
54818e3b
MS
2250 /* Locate rn and re(s) from ctx */
2251 rn = rib_find_rn_from_ctx(ctx);
2252 if (rn == NULL) {
941e261c 2253 if (debug_p) {
bd47f3a3 2254 zlog_debug(
898b4a6d 2255 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
bd47f3a3 2256 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2257 dplane_ctx_get_table(ctx), rn);
54818e3b
MS
2258 }
2259 goto done;
2260 }
2261
941e261c 2262 dest = rib_dest_from_rnode(rn);
54818e3b 2263
efe6c026 2264 if (debug_p)
898b4a6d 2265 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
c2c02b76 2266 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2267 dplane_ctx_get_table(ctx), rn, ctx);
54818e3b
MS
2268
2269 /*
2270 * Take a pass through the routes, look for matches with the context
2271 * info.
2272 */
efe6c026
MS
2273 RNODE_FOREACH_RE(rn, re) {
2274 if (rib_route_match_ctx(re, ctx, false /*!update*/))
54818e3b
MS
2275 break;
2276 }
2277
2278 /* No match? Nothing we can do */
2279 if (re == NULL) {
941e261c 2280 if (debug_p)
bd47f3a3 2281 zlog_debug(
898b4a6d 2282 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
bd47f3a3 2283 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2284 dplane_ctx_get_table(ctx), rn,
bd47f3a3 2285 zebra_route_string(dplane_ctx_get_type(ctx)));
54818e3b
MS
2286
2287 goto done;
2288 }
2289
272e8903 2290 /* Ensure we clear the QUEUED flag */
a0e11736
DS
2291 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
2292 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
272e8903 2293
4c0b5436
MS
2294 /* Is this a notification that ... matters? We mostly care about
2295 * the route that is currently selected for installation; we may also
2296 * get an un-install notification, and handle that too.
941e261c
MS
2297 */
2298 if (re != dest->selected_fib) {
4c0b5436
MS
2299 /*
2300 * If we need to, clean up after a delete that was part of
2301 * an update operation.
941e261c 2302 */
4c0b5436
MS
2303 end_count = 0;
2304 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2305 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2306 end_count++;
2307 }
2308
2309 /* If no nexthops or none installed, ensure that this re
2310 * gets its 'installed' flag cleared.
2311 */
2312 if (end_count == 0) {
2313 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2314 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2315 if (debug_p)
bd47f3a3 2316 zlog_debug(
898b4a6d 2317 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
bd47f3a3 2318 VRF_LOGNAME(vrf),
c2c02b76 2319 dplane_ctx_get_vrf(ctx),
898b4a6d 2320 dplane_ctx_get_table(ctx), rn,
bd47f3a3
JU
2321 zebra_route_string(
2322 dplane_ctx_get_type(ctx)));
4c0b5436
MS
2323 } else {
2324 /* At least report on the event. */
2325 if (debug_p)
bd47f3a3 2326 zlog_debug(
898b4a6d 2327 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
bd47f3a3 2328 VRF_LOGNAME(vrf),
c2c02b76 2329 dplane_ctx_get_vrf(ctx),
898b4a6d 2330 dplane_ctx_get_table(ctx), rn,
bd47f3a3
JU
2331 zebra_route_string(
2332 dplane_ctx_get_type(ctx)));
4c0b5436 2333 }
941e261c 2334 goto done;
a0e11736
DS
2335 } else {
2336 uint32_t flags = dplane_ctx_get_flags(ctx);
2337
2338 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOADED)) {
2339 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2340 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2341 }
2342 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOAD_FAILED)) {
2343 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2344 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2345 }
2346 if (CHECK_FLAG(flags, ZEBRA_FLAG_TRAPPED))
2347 SET_FLAG(re->flags, ZEBRA_FLAG_TRAPPED);
941e261c
MS
2348 }
2349
efe6c026
MS
2350 /* We'll want to determine whether the installation status of the
2351 * route has changed: we'll check the status before processing,
2352 * and then again if there's been a change.
2353 */
2354 start_count = 0;
1c30d64b 2355
9959f1da
MS
2356 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2357 start_count = rib_count_installed_nh(re);
efe6c026 2358
54818e3b
MS
2359 /* Update zebra's nexthop FIB flags based on the context struct's
2360 * nexthops.
2361 */
efe6c026 2362 fib_changed = rib_update_re_from_ctx(re, rn, ctx);
54818e3b 2363
efe6c026
MS
2364 if (!fib_changed) {
2365 if (debug_p)
bd47f3a3 2366 zlog_debug(
898b4a6d 2367 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
bd47f3a3 2368 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2369 dplane_ctx_get_table(ctx), rn);
54818e3b
MS
2370 }
2371
188a00e0
MS
2372 /*
2373 * Perform follow-up work if the actual status of the prefix
efe6c026 2374 * changed.
941e261c 2375 */
9959f1da 2376 end_count = rib_count_installed_nh(re);
941e261c 2377
188a00e0
MS
2378 /* Various fib transitions: changed nexthops; from installed to
2379 * not-installed; or not-installed to installed.
efe6c026 2380 */
06525c4f
DS
2381 if (zrouter.asic_notification_nexthop_control) {
2382 if (start_count > 0 && end_count > 0) {
2383 if (debug_p)
2384 zlog_debug(
2385 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2386 VRF_LOGNAME(vrf),
2387 dplane_ctx_get_vrf(ctx),
2388 dplane_ctx_get_table(ctx), rn);
188a00e0 2389
06525c4f
DS
2390 /* Changed nexthops - update kernel/others */
2391 dplane_route_notif_update(rn, re,
2392 DPLANE_OP_ROUTE_UPDATE, ctx);
188a00e0 2393
06525c4f
DS
2394 } else if (start_count == 0 && end_count > 0) {
2395 if (debug_p)
2396 zlog_debug(
2397 "%s(%u:%u):%pRN installed transition from dplane notification",
2398 VRF_LOGNAME(vrf),
2399 dplane_ctx_get_vrf(ctx),
2400 dplane_ctx_get_table(ctx), rn);
efe6c026 2401
06525c4f
DS
2402 /* We expect this to be the selected route, so we want
2403 * to tell others about this transition.
2404 */
2405 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
efe6c026 2406
06525c4f
DS
2407 /* Changed nexthops - update kernel/others */
2408 dplane_route_notif_update(rn, re,
2409 DPLANE_OP_ROUTE_UPDATE, ctx);
188a00e0 2410
06525c4f
DS
2411 /* Redistribute, lsp, and nht update */
2412 redistribute_update(rn, re, NULL);
efe6c026 2413
06525c4f
DS
2414 } else if (start_count > 0 && end_count == 0) {
2415 if (debug_p)
2416 zlog_debug(
2417 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2418 VRF_LOGNAME(vrf),
2419 dplane_ctx_get_vrf(ctx),
2420 dplane_ctx_get_table(ctx), rn);
efe6c026 2421
06525c4f
DS
2422 /* Transition from _something_ installed to _nothing_
2423 * installed.
2424 */
2425 /* We expect this to be the selected route, so we want
2426 * to tell others about this transistion.
2427 */
2428 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
efe6c026 2429
06525c4f
DS
2430 /* Changed nexthops - update kernel/others */
2431 dplane_route_notif_update(rn, re,
2432 DPLANE_OP_ROUTE_DELETE, ctx);
188a00e0 2433
06525c4f
DS
2434 /* Redistribute, lsp, and nht update */
2435 redistribute_delete(rn, re, NULL);
2436 }
2437 }
2438
2439 if (!zebra_router_notify_on_ack()) {
2440 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED))
2441 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2442 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED))
2443 zsend_route_notify_owner_ctx(ctx,
2444 ZAPI_ROUTE_FAIL_INSTALL);
9287b4c5 2445 }
efe6c026 2446
9287b4c5 2447 /* Make any changes visible for lsp and nexthop-tracking processing */
f99f1ff5
SP
2448 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
2449 false);
efe6c026 2450
9287b4c5 2451 zebra_rib_evaluate_mpls(rn);
941e261c 2452
54818e3b 2453done:
941e261c
MS
2454 if (rn)
2455 route_unlock_node(rn);
54818e3b
MS
2456}
2457
32367e7a
MS
2458/*
2459 * Process a node from the EVPN/VXLAN subqueue.
2460 */
2461static void process_subq_evpn(struct listnode *lnode)
2462{
2463 struct wq_evpn_wrapper *w;
2464
2465 /* In general, the list node points to a wrapper object
2466 * holding the info necessary to make some update.
2467 */
2468 w = listgetdata(lnode);
2469 if (!w)
2470 return;
2471
2472 if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) {
2473 if (w->add_p)
7f7e49d1 2474 zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr,
32367e7a
MS
2475 &w->ip, &w->prefix);
2476 else
2477 zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip,
2478 &w->prefix);
2479 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) {
2480 if (w->add_p)
2481 zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4,
2482 w->esr_rxed, w->df_alg,
2483 w->df_pref);
2484 else
2485 zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4);
7f7e49d1
MS
2486 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) {
2487 uint16_t ipa_len = 0;
2488
2489 if (w->ip.ipa_type == IPADDR_V4)
2490 ipa_len = IPV4_MAX_BYTELEN;
2491 else if (w->ip.ipa_type == IPADDR_V6)
2492 ipa_len = IPV6_MAX_BYTELEN;
2493
2494 if (w->add_p)
2495 zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len,
2496 &w->ip, w->flags, w->seq,
2497 w->vtep_ip, &w->esi);
2498 else
2499 zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len,
2500 &w->ip, w->vtep_ip);
7e5b0b2b
MS
2501 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) {
2502 if (w->add_p)
2503 zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni,
2504 w->vtep_ip, w->flags);
2505 else
2506 zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni,
2507 w->vtep_ip);
32367e7a
MS
2508 }
2509
7e5b0b2b 2510
32367e7a
MS
2511 XFREE(MTYPE_WQ_WRAPPER, w);
2512}
2513
04bec7b2
MS
2514/*
2515 * Process the nexthop-group workqueue subqueue
2516 */
e22e8001 2517static void process_subq_nhg(struct listnode *lnode)
e96f9203 2518{
04bec7b2
MS
2519 struct nhg_ctx *ctx;
2520 struct nhg_hash_entry *nhe, *newnhe;
2521 struct wq_nhg_wrapper *w;
4bbbd1f6 2522 uint8_t qindex = META_QUEUE_NHG;
5110a0c6 2523
04bec7b2 2524 w = listgetdata(lnode);
e22e8001 2525
04bec7b2 2526 if (!w)
e22e8001
SW
2527 return;
2528
04bec7b2
MS
2529 /* Two types of object - an update from the local kernel, or
2530 * an nhg update from a daemon.
2531 */
2532 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) {
2533 ctx = w->u.ctx;
2534
2535 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2536 zlog_debug(
7a96ccc7
DS
2537 "NHG Context id=%u dequeued from sub-queue %s",
2538 ctx->id, subqueue2str(qindex));
04bec7b2
MS
2539
2540
2541 /* Process nexthop group updates coming 'up' from the OS */
2542 nhg_ctx_process(ctx);
e22e8001 2543
04bec7b2
MS
2544 } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) {
2545 nhe = w->u.nhe;
2546
2547 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
7a96ccc7
DS
2548 zlog_debug("NHG %u dequeued from sub-queue %s", nhe->id,
2549 subqueue2str(qindex));
04bec7b2
MS
2550
2551 /* Process incoming nhg update, probably from a proto daemon */
2552 newnhe = zebra_nhg_proto_add(nhe->id, nhe->type,
2553 nhe->zapi_instance,
32367e7a 2554 nhe->zapi_session, &nhe->nhg, 0);
04bec7b2
MS
2555
2556 /* Report error to daemon via ZAPI */
2557 if (newnhe == NULL)
2558 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2559 nhe->zapi_session, nhe->id,
2560 ZAPI_NHG_FAIL_INSTALL);
2561
2562 /* Free temp nhe - we own that memory. */
2563 zebra_nhg_free(nhe);
2564 }
e22e8001 2565
32367e7a 2566 XFREE(MTYPE_WQ_WRAPPER, w);
e22e8001
SW
2567}
2568
53216dff
DS
2569static void process_subq_early_label(struct listnode *lnode)
2570{
2571 struct wq_label_wrapper *w = listgetdata(lnode);
2572 struct zebra_vrf *zvrf;
2573
2574 if (!w)
2575 return;
2576
9a7d1e74 2577 zvrf = zebra_vrf_lookup_by_id(w->vrf_id);
53216dff
DS
2578 if (!zvrf) {
2579 XFREE(MTYPE_WQ_WRAPPER, w);
2580 return;
2581 }
2582
2583 switch (w->type) {
2584 case WQ_LABEL_FTN_UNINSTALL:
2585 zebra_mpls_ftn_uninstall(zvrf, w->ltype, &w->p, w->route_type,
2586 w->route_instance);
2587 break;
2588 case WQ_LABEL_LABELS_PROCESS:
2589 zebra_mpls_zapi_labels_process(w->add_p, zvrf, &w->zl);
2590 break;
2591 }
2592
2593 XFREE(MTYPE_WQ_WRAPPER, w);
2594}
2595
e22e8001
SW
2596static void process_subq_route(struct listnode *lnode, uint8_t qindex)
2597{
2598 struct route_node *rnode = NULL;
2599 rib_dest_t *dest = NULL;
2600 struct zebra_vrf *zvrf = NULL;
5110a0c6 2601
d62a17ae 2602 rnode = listgetdata(lnode);
2603 dest = rib_dest_from_rnode(rnode);
c2c02b76
DS
2604 assert(dest);
2605
2606 zvrf = rib_dest_vrf(dest);
41ec9222 2607
d62a17ae 2608 rib_process(rnode);
5110a0c6 2609
d62a17ae 2610 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
7e26b9d4 2611 struct route_entry *re = NULL;
efe6c026 2612
7e26b9d4
DS
2613 /*
2614 * rib_process may have freed the dest
2615 * as part of the garbage collection. Let's
2616 * prevent stupidity from happening.
2617 */
2618 dest = rib_dest_from_rnode(rnode);
2619 if (dest)
2620 re = re_list_first(&dest->routes);
2621
7a96ccc7 2622 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
04bec7b2 2623 zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0,
7a96ccc7 2624 rnode, rnode, subqueue2str(qindex));
d62a17ae 2625 }
41ec9222 2626
d62a17ae 2627 if (rnode->info)
2628 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2629 RIB_ROUTE_QUEUED(qindex));
9fd92e3c 2630
d62a17ae 2631 route_unlock_node(rnode);
e22e8001
SW
2632}
2633
d7ac4c4d 2634static void rib_re_nhg_free(struct route_entry *re)
e22e8001 2635{
d7ac4c4d
DS
2636 if (re->nhe && re->nhe_id) {
2637 assert(re->nhe->id == re->nhe_id);
2638 route_entry_update_nhe(re, NULL);
2639 } else if (re->nhe && re->nhe->nhg.nexthop)
2640 nexthops_free(re->nhe->nhg.nexthop);
e22e8001 2641
d7ac4c4d
DS
2642 nexthops_free(re->fib_ng.nexthop);
2643}
e22e8001 2644
d7ac4c4d
DS
2645struct zebra_early_route {
2646 afi_t afi;
2647 safi_t safi;
2648 struct prefix p;
2649 struct prefix_ipv6 src_p;
2650 bool src_p_provided;
2651 struct route_entry *re;
2652 struct nhg_hash_entry *re_nhe;
2653 bool startup;
2654 bool deletion;
2655 bool fromkernel;
2656};
e22e8001 2657
d7ac4c4d
DS
2658static void early_route_memory_free(struct zebra_early_route *ere)
2659{
2660 if (ere->re_nhe)
2661 zebra_nhg_free(ere->re_nhe);
e22e8001 2662
d7ac4c4d
DS
2663 XFREE(MTYPE_RE, ere->re);
2664 XFREE(MTYPE_WQ_WRAPPER, ere);
e96f9203
DO
2665}
2666
d7ac4c4d 2667static void process_subq_early_route_add(struct zebra_early_route *ere)
e96f9203 2668{
d7ac4c4d
DS
2669 struct route_entry *re = ere->re;
2670 struct route_table *table;
2671 struct nhg_hash_entry *nhe = NULL;
2672 struct route_node *rn;
2673 struct route_entry *same = NULL, *first_same = NULL;
2674 int same_count = 0;
2675 rib_dest_t *dest;
91f16812 2676
d7ac4c4d
DS
2677 /* Lookup table. */
2678 table = zebra_vrf_get_table_with_table_id(ere->afi, ere->safi,
2679 re->vrf_id, re->table);
2680 if (!table) {
2681 early_route_memory_free(ere);
2682 return;
2683 }
91f16812 2684
d7ac4c4d
DS
2685 if (re->nhe_id > 0) {
2686 nhe = zebra_nhg_lookup_id(re->nhe_id);
91f16812 2687
d7ac4c4d
DS
2688 if (!nhe) {
2689 /*
2690 * We've received from the kernel a nexthop id
2691 * that we don't have saved yet. More than likely
2692 * it has not been processed and is on the
2693 * queue to be processed. Let's stop what we
2694 * are doing and cause the meta q to be processed
2695 * storing this for later.
2696 *
2697 * This is being done this way because zebra
2698 * runs with the assumption t
2699 */
2700 flog_err(
2701 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2702 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2703 re->nhe_id, &ere->p);
5110a0c6 2704
d7ac4c4d
DS
2705 early_route_memory_free(ere);
2706 return;
d62a17ae 2707 }
d7ac4c4d
DS
2708 } else {
2709 /* Lookup nhe from route information */
2710 nhe = zebra_nhg_rib_find_nhe(ere->re_nhe, ere->afi);
2711 if (!nhe) {
2712 char buf2[PREFIX_STRLEN] = "";
e96f9203 2713
d7ac4c4d
DS
2714 flog_err(
2715 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2716 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2717 &ere->p, ere->src_p_provided ? " from " : "",
2718 ere->src_p_provided
2719 ? prefix2str(&ere->src_p, buf2,
2720 sizeof(buf2))
2721 : "");
f52ed677 2722
d7ac4c4d
DS
2723 early_route_memory_free(ere);
2724 return;
2725 }
2726 }
5110a0c6 2727
d7ac4c4d
DS
2728 /*
2729 * Attach the re to the nhe's nexthop group.
2730 *
2731 * TODO: This will need to change when we start getting IDs from upper
2732 * level protocols, as the refcnt might be wrong, since it checks
2733 * if old_id != new_id.
2734 */
2735 route_entry_update_nhe(re, nhe);
e22e8001 2736
d7ac4c4d
DS
2737 /* Make it sure prefixlen is applied to the prefix. */
2738 apply_mask(&ere->p);
2739 if (ere->src_p_provided)
2740 apply_mask_ipv6(&ere->src_p);
d62a17ae 2741
d7ac4c4d
DS
2742 /* Lookup route node.*/
2743 rn = srcdest_rnode_get(table, &ere->p,
2744 ere->src_p_provided ? &ere->src_p : NULL);
2745
2746 /*
2747 * If same type of route are installed, treat it as a implicit
2748 * withdraw. If the user has specified the No route replace semantics
2749 * for the install don't do a route replace.
2750 */
2751 RNODE_FOREACH_RE (rn, same) {
2752 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED)) {
2753 same_count++;
2754 continue;
d62a17ae 2755 }
5110a0c6 2756
d7ac4c4d
DS
2757 /* Compare various route_entry properties */
2758 if (rib_compare_routes(re, same)) {
2759 same_count++;
5110a0c6 2760
d7ac4c4d
DS
2761 if (first_same == NULL)
2762 first_same = same;
2763 }
d62a17ae 2764 }
f52ed677 2765
d7ac4c4d 2766 same = first_same;
e22e8001 2767
d7ac4c4d
DS
2768 if (!ere->startup && (re->flags & ZEBRA_FLAG_SELFROUTE) &&
2769 zrouter.asic_offloaded) {
2770 if (!same) {
2771 if (IS_ZEBRA_DEBUG_RIB)
2772 zlog_debug(
2773 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2774 rn);
2775 /*
2776 * We are not on startup, this is a self route
2777 * and we have asic offload. Which means
2778 * we are getting a callback for a entry
2779 * that was already deleted to the kernel
2780 * but an earlier response was just handed
2781 * back. Drop it on the floor
2782 */
2783 early_route_memory_free(ere);
2784 return;
2785 }
2786 }
4d38fdb4 2787
1cd8bd05
DS
2788 /* Set default distance by route type. */
2789 if (re->distance == 0) {
2790 if (same && !zebra_router_notify_on_ack())
2791 re->distance = same->distance;
2792 else
2793 re->distance = route_distance(re->type);
2794 }
2795
68ff69fa
DS
2796 if (re->metric == ROUTE_INSTALLATION_METRIC &&
2797 CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE)) {
2798 if (same && !zebra_router_notify_on_ack())
2799 re->metric = same->metric;
2800 else
2801 re->metric = 0;
2802 }
2803
d7ac4c4d
DS
2804 /* If this route is kernel/connected route, notify the dataplane. */
2805 if (RIB_SYSTEM_ROUTE(re)) {
2806 /* Notify dataplane */
2807 dplane_sys_route_add(rn, re);
2808 }
53216dff 2809
d7ac4c4d
DS
2810 /* Link new re to node.*/
2811 if (IS_ZEBRA_DEBUG_RIB) {
2812 rnode_debug(
2813 rn, re->vrf_id,
2814 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2815 rn, re, zebra_route_string(re->type), same, same_count);
fc328ac9 2816
d7ac4c4d
DS
2817 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2818 route_entry_dump(
2819 &ere->p,
2820 ere->src_p_provided ? &ere->src_p : NULL, re);
2821 }
e22e8001 2822
d7ac4c4d
DS
2823 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2824 rib_addnode(rn, re, 1);
4d38fdb4 2825
d7ac4c4d
DS
2826 /* Free implicit route.*/
2827 if (same)
2828 rib_delnode(rn, same);
04bec7b2 2829
d7ac4c4d
DS
2830 /* See if we can remove some RE entries that are queued for
2831 * removal, but won't be considered in rib processing.
2832 */
2833 dest = rib_dest_from_rnode(rn);
2834 RNODE_FOREACH_RE_SAFE (rn, re, same) {
2835 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2836 /* If the route was used earlier, must retain it. */
2837 if (dest && re == dest->selected_fib)
2838 continue;
04bec7b2 2839
d7ac4c4d
DS
2840 if (IS_ZEBRA_DEBUG_RIB)
2841 rnode_debug(rn, re->vrf_id,
2842 "rn %p, removing unneeded re %p",
2843 rn, re);
e22e8001 2844
d7ac4c4d
DS
2845 rib_unlink(rn, re);
2846 }
2847 }
e22e8001 2848
d7ac4c4d
DS
2849 route_unlock_node(rn);
2850 if (ere->re_nhe)
2851 zebra_nhg_free(ere->re_nhe);
2852 XFREE(MTYPE_WQ_WRAPPER, ere);
e22e8001
SW
2853}
2854
d7ac4c4d 2855static void process_subq_early_route_delete(struct zebra_early_route *ere)
04bec7b2 2856{
d7ac4c4d
DS
2857 struct route_table *table;
2858 struct route_node *rn;
2859 struct route_entry *re;
2860 struct route_entry *fib = NULL;
2861 struct route_entry *same = NULL;
2862 struct nexthop *rtnh;
2863 char buf2[INET6_ADDRSTRLEN];
2864 rib_dest_t *dest;
04bec7b2 2865
d7ac4c4d
DS
2866 if (ere->src_p_provided)
2867 assert(!ere->src_p.prefixlen || ere->afi == AFI_IP6);
2868
2869 /* Lookup table. */
2870 table = zebra_vrf_lookup_table_with_table_id(
2871 ere->afi, ere->safi, ere->re->vrf_id, ere->re->table);
2872 if (!table) {
2873 early_route_memory_free(ere);
2874 return;
2875 }
2876
2877 /* Apply mask. */
2878 apply_mask(&ere->p);
2879 if (ere->src_p_provided)
2880 apply_mask_ipv6(&ere->src_p);
2881
2882 /* Lookup route node. */
2883 rn = srcdest_rnode_lookup(table, &ere->p,
2884 ere->src_p_provided ? &ere->src_p : NULL);
2885 if (!rn) {
2886 if (IS_ZEBRA_DEBUG_RIB) {
2887 char src_buf[PREFIX_STRLEN];
2888 struct vrf *vrf = vrf_lookup_by_id(ere->re->vrf_id);
2889
2890 if (ere->src_p_provided && ere->src_p.prefixlen)
2891 prefix2str(&ere->src_p, src_buf,
2892 sizeof(src_buf));
2893 else
2894 src_buf[0] = '\0';
2895
2896 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2897 vrf->name, ere->re->table, rn,
2898 (src_buf[0] != '\0') ? " from " : "",
2899 src_buf);
2900 }
2901 early_route_memory_free(ere);
2902 return;
2903 }
2904
2905 dest = rib_dest_from_rnode(rn);
2906 fib = dest->selected_fib;
2907
2908 struct nexthop *nh = NULL;
2909
2910 if (ere->re->nhe)
2911 nh = ere->re->nhe->nhg.nexthop;
2912
2913 /* Lookup same type route. */
2914 RNODE_FOREACH_RE (rn, re) {
2915 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2916 continue;
2917
2918 if (re->type != ere->re->type)
2919 continue;
2920 if (re->instance != ere->re->instance)
2921 continue;
2922 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2923 ere->re->distance != re->distance)
2924 continue;
2925
2926 if (re->type == ZEBRA_ROUTE_KERNEL &&
2927 re->metric != ere->re->metric)
2928 continue;
2929 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = nh) &&
2930 rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2931 if (rtnh->ifindex != nh->ifindex)
2932 continue;
2933 same = re;
2934 break;
2935 }
2936
2937 /* Make sure that the route found has the same gateway. */
2938 if (ere->re->nhe_id && re->nhe_id == ere->re->nhe_id) {
2939 same = re;
2940 break;
2941 }
2942
2943 if (nh == NULL) {
2944 same = re;
2945 break;
2946 }
2947 for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) {
2948 /*
2949 * No guarantee all kernel send nh with labels
2950 * on delete.
2951 */
2952 if (nexthop_same_no_labels(rtnh, nh)) {
2953 same = re;
2954 break;
2955 }
2956 }
2957
2958 if (same)
2959 break;
2960 }
2961 /*
2962 * If same type of route can't be found and this message is from
2963 * kernel.
2964 */
2965 if (!same) {
2966 /*
2967 * In the past(HA!) we could get here because
2968 * we were receiving a route delete from the
2969 * kernel and we're not marking the proto
2970 * as coming from it's appropriate originator.
2971 * Now that we are properly noticing the fact
2972 * that the kernel has deleted our route we
2973 * are not going to get called in this path
2974 * I am going to leave this here because
2975 * this might still work this way on non-linux
2976 * platforms as well as some weird state I have
2977 * not properly thought of yet.
2978 * If we can show that this code path is
2979 * dead then we can remove it.
2980 */
2981 if (fib && CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE)) {
2982 if (IS_ZEBRA_DEBUG_RIB) {
2983 rnode_debug(
2984 rn, ere->re->vrf_id,
2985 "rn %p, re %p (%s) was deleted from kernel, adding",
2986 rn, fib, zebra_route_string(fib->type));
2987 }
2988 if (zrouter.allow_delete ||
2989 CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) {
2990 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
2991 /* Unset flags. */
2992 for (rtnh = fib->nhe->nhg.nexthop; rtnh;
2993 rtnh = rtnh->next)
2994 UNSET_FLAG(rtnh->flags,
2995 NEXTHOP_FLAG_FIB);
2996
2997 /*
2998 * This is a non FRR route
2999 * as such we should mark
3000 * it as deleted
3001 */
3002 dest->selected_fib = NULL;
3003 } else {
3004 /*
3005 * This means someone else, other than Zebra,
3006 * has deleted a Zebra router from the kernel.
3007 * We will add it back
3008 */
3009 rib_install_kernel(rn, fib, NULL);
3010 }
3011 } else {
3012 if (IS_ZEBRA_DEBUG_RIB) {
3013 if (nh)
3014 rnode_debug(
3015 rn, ere->re->vrf_id,
3016 "via %s ifindex %d type %d doesn't exist in rib",
3017 inet_ntop(afi2family(ere->afi),
3018 &nh->gate, buf2,
3019 sizeof(buf2)),
3020 nh->ifindex, ere->re->type);
3021 else
3022 rnode_debug(
3023 rn, ere->re->vrf_id,
3024 "type %d doesn't exist in rib",
3025 ere->re->type);
3026 }
3027 route_unlock_node(rn);
3028 early_route_memory_free(ere);
3029 return;
3030 }
3031 }
3032
3033 if (same) {
3034 struct nexthop *tmp_nh;
3035
3036 if (ere->fromkernel &&
3037 CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE) &&
3038 !zrouter.allow_delete) {
3039 rib_install_kernel(rn, same, NULL);
3040 route_unlock_node(rn);
3041
3042 early_route_memory_free(ere);
3043 return;
3044 }
3045
3046 /* Special handling for IPv4 or IPv6 routes sourced from
3047 * EVPN - the nexthop (and associated MAC) need to be
3048 * uninstalled if no more refs.
3049 */
3050 for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) {
3051 struct ipaddr vtep_ip;
3052
3053 if (CHECK_FLAG(tmp_nh->flags, NEXTHOP_FLAG_EVPN)) {
3054 memset(&vtep_ip, 0, sizeof(struct ipaddr));
3055 if (ere->afi == AFI_IP) {
3056 vtep_ip.ipa_type = IPADDR_V4;
3057 memcpy(&(vtep_ip.ipaddr_v4),
3058 &(tmp_nh->gate.ipv4),
3059 sizeof(struct in_addr));
3060 } else {
3061 vtep_ip.ipa_type = IPADDR_V6;
3062 memcpy(&(vtep_ip.ipaddr_v6),
3063 &(tmp_nh->gate.ipv6),
3064 sizeof(struct in6_addr));
3065 }
3066 zebra_rib_queue_evpn_route_del(
3067 re->vrf_id, &vtep_ip, &ere->p);
3068 }
3069 }
3070
3071 /* Notify dplane if system route changes */
3072 if (RIB_SYSTEM_ROUTE(re))
3073 dplane_sys_route_del(rn, same);
3074
3075 rib_delnode(rn, same);
3076 }
3077
3078 route_unlock_node(rn);
3079
3080 early_route_memory_free(ere);
3081}
3082
3083/*
3084 * When FRR receives a route we need to match the route up to
3085 * nexthop groups. That we also may have just received
3086 * place the data on this queue so that this work of finding
3087 * the nexthop group entries for the route entry is always
3088 * done after the nexthop group has had a chance to be processed
3089 */
3090static void process_subq_early_route(struct listnode *lnode)
3091{
3092 struct zebra_early_route *ere = listgetdata(lnode);
3093
3094 if (ere->deletion)
3095 process_subq_early_route_delete(ere);
3096 else
3097 process_subq_early_route_add(ere);
3098}
3099
81322b96
DS
3100struct meta_q_gr_run {
3101 afi_t afi;
3102 vrf_id_t vrf_id;
3103 uint8_t proto;
3104 uint8_t instance;
3105};
3106
3107static void process_subq_gr_run(struct listnode *lnode)
3108{
3109 struct meta_q_gr_run *gr_run = listgetdata(lnode);
3110
3111 zebra_gr_process_client(gr_run->afi, gr_run->vrf_id, gr_run->proto,
3112 gr_run->instance);
3113
3114 XFREE(MTYPE_WQ_WRAPPER, gr_run);
3115}
3116
d7ac4c4d
DS
3117/*
3118 * Examine the specified subqueue; process one entry and return 1 if
3119 * there is a node, return 0 otherwise.
3120 */
3121static unsigned int process_subq(struct list *subq,
3122 enum meta_queue_indexes qindex)
3123{
3124 struct listnode *lnode = listhead(subq);
3125
3126 if (!lnode)
3127 return 0;
3128
3129 switch (qindex) {
3130 case META_QUEUE_EVPN:
3131 process_subq_evpn(lnode);
3132 break;
3133 case META_QUEUE_NHG:
3134 process_subq_nhg(lnode);
3135 break;
3136 case META_QUEUE_EARLY_ROUTE:
3137 process_subq_early_route(lnode);
3138 break;
3139 case META_QUEUE_EARLY_LABEL:
3140 process_subq_early_label(lnode);
3141 break;
3142 case META_QUEUE_CONNECTED:
3143 case META_QUEUE_KERNEL:
3144 case META_QUEUE_STATIC:
3145 case META_QUEUE_NOTBGP:
3146 case META_QUEUE_BGP:
3147 case META_QUEUE_OTHER:
3148 process_subq_route(lnode, qindex);
3149 break;
81322b96
DS
3150 case META_QUEUE_GR_RUN:
3151 process_subq_gr_run(lnode);
3152 break;
d7ac4c4d
DS
3153 }
3154
3155 list_delete_node(subq, lnode);
3156
3157 return 1;
3158}
3159
3160/* Dispatch the meta queue by picking and processing the next node from
3161 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3162 * data is pointed to the meta queue structure.
3163 */
3164static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
3165{
3166 struct meta_queue *mq = data;
3167 unsigned i;
3168 uint32_t queue_len, queue_limit;
3169
3170 /* Ensure there's room for more dataplane updates */
3171 queue_limit = dplane_get_in_queue_limit();
3172 queue_len = dplane_get_in_queue_len();
3173 if (queue_len > queue_limit) {
3174 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3175 zlog_debug(
3176 "rib queue: dplane queue len %u, limit %u, retrying",
3177 queue_len, queue_limit);
3178
3179 /* Ensure that the meta-queue is actually enqueued */
3180 if (work_queue_empty(zrouter.ribq))
3181 work_queue_add(zrouter.ribq, zrouter.mq);
3182
3183 return WQ_QUEUE_BLOCKED;
3184 }
3185
3186 for (i = 0; i < MQ_SIZE; i++)
3187 if (process_subq(mq->subq[i], i)) {
3188 mq->size--;
3189 break;
3190 }
3191 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
3192}
3193
3194
3195/*
3196 * Look into the RN and queue it into the highest priority queue
3197 * at this point in time for processing.
3198 *
3199 * We will enqueue a route node only once per invocation.
3200 *
3201 * There are two possibilities here that should be kept in mind.
3202 * If the original invocation has not been pulled off for processing
3203 * yet, A subsuquent invocation can have a route entry with a better
3204 * meta queue index value and we can have a situation where
3205 * we might have the same node enqueued 2 times. Not necessarily
3206 * an optimal situation but it should be ok.
3207 *
3208 * The other possibility is that the original invocation has not
3209 * been pulled off for processing yet, A subsusquent invocation
3210 * doesn't have a route_entry with a better meta-queue and the
3211 * original metaqueue index value will win and we'll end up with
3212 * the route node enqueued once.
3213 */
3214static int rib_meta_queue_add(struct meta_queue *mq, void *data)
3215{
3216 struct route_node *rn = NULL;
3217 struct route_entry *re = NULL, *curr_re = NULL;
3218 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
3219
3220 rn = (struct route_node *)data;
3221
3222 RNODE_FOREACH_RE (rn, curr_re) {
3223 curr_qindex = route_info[curr_re->type].meta_q_map;
3224
3225 if (curr_qindex <= qindex) {
3226 re = curr_re;
3227 qindex = curr_qindex;
3228 }
3229 }
3230
3231 if (!re)
3232 return -1;
3233
3234 /* Invariant: at this point we always have rn->info set. */
3235 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3236 RIB_ROUTE_QUEUED(qindex))) {
3237 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3238 rnode_debug(rn, re->vrf_id,
3239 "rn %p is already queued in sub-queue %s",
3240 (void *)rn, subqueue2str(qindex));
3241 return -1;
3242 }
3243
3244 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
3245 listnode_add(mq->subq[qindex], rn);
3246 route_lock_node(rn);
3247 mq->size++;
3248
3249 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3250 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %s",
3251 (void *)rn, subqueue2str(qindex));
3252
3253 return 0;
3254}
3255
3256static int early_label_meta_queue_add(struct meta_queue *mq, void *data)
3257{
3258 listnode_add(mq->subq[META_QUEUE_EARLY_LABEL], data);
3259 mq->size++;
3260 return 0;
3261}
3262
3263static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data)
3264{
3265 struct nhg_ctx *ctx = NULL;
3266 uint8_t qindex = META_QUEUE_NHG;
3267 struct wq_nhg_wrapper *w;
3268
3269 ctx = (struct nhg_ctx *)data;
3270
3271 if (!ctx)
3272 return -1;
3273
3274 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
3275
3276 w->type = WQ_NHG_WRAPPER_TYPE_CTX;
3277 w->u.ctx = ctx;
3278
3279 listnode_add(mq->subq[qindex], w);
3280 mq->size++;
3281
3282 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3283 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3284 ctx->id, subqueue2str(qindex));
3285
3286 return 0;
3287}
3288
3289static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data)
3290{
3291 struct nhg_hash_entry *nhe = NULL;
3292 uint8_t qindex = META_QUEUE_NHG;
3293 struct wq_nhg_wrapper *w;
3294
3295 nhe = (struct nhg_hash_entry *)data;
04bec7b2
MS
3296
3297 if (!nhe)
3298 return -1;
3299
32367e7a 3300 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
04bec7b2
MS
3301
3302 w->type = WQ_NHG_WRAPPER_TYPE_NHG;
3303 w->u.nhe = nhe;
3304
3305 listnode_add(mq->subq[qindex], w);
3306 mq->size++;
3307
3308 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
7a96ccc7
DS
3309 zlog_debug("NHG id=%u queued into sub-queue %s", nhe->id,
3310 subqueue2str(qindex));
04bec7b2
MS
3311
3312 return 0;
3313}
3314
32367e7a
MS
3315static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data)
3316{
3317 listnode_add(mq->subq[META_QUEUE_EVPN], data);
3318 mq->size++;
3319
3320 return 0;
3321}
3322
e22e8001
SW
3323static int mq_add_handler(void *data,
3324 int (*mq_add_func)(struct meta_queue *mq, void *data))
3325{
489a9614 3326 if (zrouter.ribq == NULL) {
e914ccbe 3327 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 3328 "%s: work_queue does not exist!", __func__);
e22e8001 3329 return -1;
d62a17ae 3330 }
3331
3332 /*
3333 * The RIB queue should normally be either empty or holding the only
3334 * work_queue_item element. In the latter case this element would
3335 * hold a pointer to the meta queue structure, which must be used to
3336 * actually queue the route nodes to process. So create the MQ
3337 * holder, if necessary, then push the work into it in any case.
3338 * This semantics was introduced after 0.99.9 release.
3339 */
489a9614 3340 if (work_queue_empty(zrouter.ribq))
ea45a4e7 3341 work_queue_add(zrouter.ribq, zrouter.mq);
e96f9203 3342
e22e8001
SW
3343 return mq_add_func(zrouter.mq, data);
3344}
fc328ac9 3345
53216dff
DS
3346void mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
3347 struct prefix *prefix, uint8_t route_type,
3348 uint8_t route_instance)
3349{
3350 struct wq_label_wrapper *w;
3351
3352 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3353
3354 w->type = WQ_LABEL_FTN_UNINSTALL;
3355 w->vrf_id = zvrf->vrf->vrf_id;
3356 w->p = *prefix;
3357 w->ltype = type;
3358 w->route_type = route_type;
3359 w->route_instance = route_instance;
3360
3361 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3362 zlog_debug("Early Label Handling for %pFX", prefix);
3363
3364 mq_add_handler(w, early_label_meta_queue_add);
3365}
3366
3367void mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
3368 const struct zapi_labels *zl)
3369{
3370 struct wq_label_wrapper *w;
3371
3372 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3373 w->type = WQ_LABEL_LABELS_PROCESS;
3374 w->vrf_id = zvrf->vrf->vrf_id;
3375 w->add_p = add_p;
3376 w->zl = *zl;
3377
3378 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3379 zlog_debug("Early Label Handling: Labels Process");
3380
3381 mq_add_handler(w, early_label_meta_queue_add);
3382}
3383
e22e8001
SW
3384/* Add route_node to work queue and schedule processing */
3385int rib_queue_add(struct route_node *rn)
3386{
3387 assert(rn);
3388
3389 /* Pointless to queue a route_node with no RIB entries to add or remove
3390 */
3391 if (!rnode_to_ribs(rn)) {
c10e14e9
DS
3392 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3393 __func__, (void *)rn, route_node_get_lock_count(rn));
e22e8001
SW
3394 zlog_backtrace(LOG_DEBUG);
3395 return -1;
3396 }
3397
04bec7b2 3398 return mq_add_handler(rn, rib_meta_queue_add);
e22e8001
SW
3399}
3400
04bec7b2
MS
3401/*
3402 * Enqueue incoming nhg info from OS for processing
3403 */
3404int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx)
e22e8001
SW
3405{
3406 assert(ctx);
3407
04bec7b2
MS
3408 return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add);
3409}
3410
3411/*
3412 * Enqueue incoming nhg from proto daemon for processing
3413 */
3414int rib_queue_nhe_add(struct nhg_hash_entry *nhe)
3415{
3416 if (nhe == NULL)
3417 return -1;
3418
3419 return mq_add_handler(nhe, rib_meta_queue_nhg_add);
3420}
3421
32367e7a
MS
3422/*
3423 * Enqueue evpn route for processing
3424 */
3425int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
3426 const struct ipaddr *vtep_ip,
3427 const struct prefix *host_prefix)
3428{
3429 struct wq_evpn_wrapper *w;
3430
3431 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3432
3433 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3434 w->add_p = true;
3435 w->vrf_id = vrf_id;
7f7e49d1 3436 w->macaddr = *rmac;
32367e7a
MS
3437 w->ip = *vtep_ip;
3438 w->prefix = *host_prefix;
3439
3440 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3441 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3442 vrf_id, vtep_ip, host_prefix);
3443
3444 return mq_add_handler(w, rib_meta_queue_evpn_add);
3445}
3446
3447int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
3448 const struct ipaddr *vtep_ip,
3449 const struct prefix *host_prefix)
3450{
3451 struct wq_evpn_wrapper *w;
3452
3453 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3454
3455 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3456 w->add_p = false;
3457 w->vrf_id = vrf_id;
3458 w->ip = *vtep_ip;
3459 w->prefix = *host_prefix;
3460
3461 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3462 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3463 vrf_id, vtep_ip, host_prefix);
3464
3465 return mq_add_handler(w, rib_meta_queue_evpn_add);
3466}
3467
3468/* Enqueue EVPN remote ES for processing */
3469int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
3470 const struct in_addr *vtep_ip,
3471 bool esr_rxed, uint8_t df_alg,
3472 uint16_t df_pref)
3473{
3474 struct wq_evpn_wrapper *w;
3475 char buf[ESI_STR_LEN];
3476
3477 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3478
3479 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3480 w->add_p = true;
3481 w->esi = *esi;
3482 w->ip.ipa_type = IPADDR_V4;
3483 w->ip.ipaddr_v4 = *vtep_ip;
3484 w->esr_rxed = esr_rxed;
3485 w->df_alg = df_alg;
3486 w->df_pref = df_pref;
3487
3488 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3489 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3490 esi_to_str(esi, buf, sizeof(buf)));
3491
3492 return mq_add_handler(w, rib_meta_queue_evpn_add);
3493}
3494
3495int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
3496 const struct in_addr *vtep_ip)
3497{
3498 struct wq_evpn_wrapper *w;
3499 char buf[ESI_STR_LEN];
3500
3501 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3502
3503 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3504 w->add_p = false;
3505 w->esi = *esi;
3506 w->ip.ipa_type = IPADDR_V4;
3507 w->ip.ipaddr_v4 = *vtep_ip;
3508
3509 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3510 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3511 esi_to_str(esi, buf, sizeof(buf));
3512 else
3513 strlcpy(buf, "-", sizeof(buf));
3514
3515 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3516 buf);
3517 }
3518
3519 return mq_add_handler(w, rib_meta_queue_evpn_add);
3520}
3521
7f7e49d1
MS
3522/*
3523 * Enqueue EVPN remote macip update for processing
3524 */
3525int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
3526 const struct ipaddr *ipaddr,
3527 uint8_t flags, uint32_t seq,
3528 struct in_addr vtep_ip, const esi_t *esi)
3529{
3530 struct wq_evpn_wrapper *w;
3531 char buf[ESI_STR_LEN];
3532
3533 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3534
3535 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3536 w->add_p = true;
3537 w->vni = vni;
3538 w->macaddr = *macaddr;
3539 w->ip = *ipaddr;
3540 w->flags = flags;
3541 w->seq = seq;
3542 w->vtep_ip = vtep_ip;
3543 w->esi = *esi;
3544
3545 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3546 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3547 esi_to_str(esi, buf, sizeof(buf));
3548 else
3549 strlcpy(buf, "-", sizeof(buf));
3550
3551 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__,
3552 macaddr, &vtep_ip, buf);
3553 }
3554
3555 return mq_add_handler(w, rib_meta_queue_evpn_add);
3556}
3557
3558int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
3559 const struct ipaddr *ip,
3560 struct in_addr vtep_ip)
3561{
3562 struct wq_evpn_wrapper *w;
3563
3564 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3565
3566 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3567 w->add_p = false;
3568 w->vni = vni;
3569 w->macaddr = *macaddr;
3570 w->ip = *ip;
3571 w->vtep_ip = vtep_ip;
3572
3573 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3574 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__,
3575 macaddr, &vtep_ip);
3576
3577 return mq_add_handler(w, rib_meta_queue_evpn_add);
3578}
3579
7e5b0b2b
MS
3580/*
3581 * Enqueue remote VTEP address for processing
3582 */
3583int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
3584 struct in_addr vtep_ip, int flood_control)
3585{
3586 struct wq_evpn_wrapper *w;
3587
3588 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3589
3590 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3591 w->add_p = true;
3592 w->vrf_id = vrf_id;
3593 w->vni = vni;
3594 w->vtep_ip = vtep_ip;
3595 w->flags = flood_control;
3596
3597 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3598 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3599 &vtep_ip);
3600
3601 return mq_add_handler(w, rib_meta_queue_evpn_add);
3602}
3603
3604int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
3605 struct in_addr vtep_ip)
3606{
3607 struct wq_evpn_wrapper *w;
3608
3609 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3610
3611 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3612 w->add_p = false;
3613 w->vrf_id = vrf_id;
3614 w->vni = vni;
3615 w->vtep_ip = vtep_ip;
3616
3617 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3618 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3619 &vtep_ip);
3620
3621 return mq_add_handler(w, rib_meta_queue_evpn_add);
3622}
3623
a310ebc1
DS
3624/* Create new meta queue.
3625 A destructor function doesn't seem to be necessary here.
3626 */
3627static struct meta_queue *meta_queue_new(void)
3628{
3629 struct meta_queue *new;
3630 unsigned i;
3631
3632 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
3633
3634 for (i = 0; i < MQ_SIZE; i++) {
3635 new->subq[i] = list_new();
3636 assert(new->subq[i]);
3637 }
3638
3639 return new;
3640}
3641
32367e7a 3642/* Clean up the EVPN meta-queue list */
a310ebc1
DS
3643static void evpn_meta_queue_free(struct meta_queue *mq, struct list *l,
3644 struct zebra_vrf *zvrf)
32367e7a 3645{
a310ebc1 3646 struct listnode *node, *nnode;
32367e7a
MS
3647 struct wq_evpn_wrapper *w;
3648
3649 /* Free the node wrapper object, and the struct it wraps */
a310ebc1
DS
3650 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3651 if (zvrf) {
3652 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3653
3654 if (w->vrf_id != vrf_id)
3655 continue;
3656 }
3657
32367e7a
MS
3658 node->data = NULL;
3659
3660 XFREE(MTYPE_WQ_WRAPPER, w);
3661
3662 list_delete_node(l, node);
a310ebc1 3663 mq->size--;
32367e7a
MS
3664 }
3665}
3666
04bec7b2 3667/* Clean up the nhg meta-queue list */
a310ebc1
DS
3668static void nhg_meta_queue_free(struct meta_queue *mq, struct list *l,
3669 struct zebra_vrf *zvrf)
04bec7b2
MS
3670{
3671 struct wq_nhg_wrapper *w;
a310ebc1 3672 struct listnode *node, *nnode;
04bec7b2
MS
3673
3674 /* Free the node wrapper object, and the struct it wraps */
a310ebc1
DS
3675 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3676 if (zvrf) {
3677 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
04bec7b2 3678
a310ebc1
DS
3679 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX &&
3680 w->u.ctx->vrf_id != vrf_id)
3681 continue;
3682 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG &&
3683 w->u.nhe->vrf_id != vrf_id)
3684 continue;
3685 }
04bec7b2
MS
3686 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX)
3687 nhg_ctx_free(&w->u.ctx);
3688 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG)
3689 zebra_nhg_free(w->u.nhe);
3690
a310ebc1 3691 node->data = NULL;
32367e7a 3692 XFREE(MTYPE_WQ_WRAPPER, w);
04bec7b2
MS
3693
3694 list_delete_node(l, node);
a310ebc1 3695 mq->size--;
04bec7b2 3696 }
4d38fdb4 3697}
3698
53216dff
DS
3699static void early_label_meta_queue_free(struct meta_queue *mq, struct list *l,
3700 struct zebra_vrf *zvrf)
3701{
3702 struct wq_label_wrapper *w;
3703 struct listnode *node, *nnode;
3704
3705 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3706 if (zvrf && zvrf->vrf->vrf_id != w->vrf_id)
3707 continue;
3708
3709 switch (w->type) {
3710 case WQ_LABEL_FTN_UNINSTALL:
3711 case WQ_LABEL_LABELS_PROCESS:
3712 break;
3713 }
3714
3715 node->data = NULL;
3716 XFREE(MTYPE_WQ_WRAPPER, w);
3717 list_delete_node(l, node);
3718 mq->size--;
3719 }
3720}
3721
a310ebc1
DS
3722static void rib_meta_queue_free(struct meta_queue *mq, struct list *l,
3723 struct zebra_vrf *zvrf)
e96f9203 3724{
a310ebc1
DS
3725 struct route_node *rnode;
3726 struct listnode *node, *nnode;
5110a0c6 3727
a310ebc1
DS
3728 for (ALL_LIST_ELEMENTS(l, node, nnode, rnode)) {
3729 rib_dest_t *dest = rib_dest_from_rnode(rnode);
e96f9203 3730
a310ebc1
DS
3731 if (dest && rib_dest_vrf(dest) != zvrf)
3732 continue;
5110a0c6 3733
a310ebc1
DS
3734 route_unlock_node(rnode);
3735 node->data = NULL;
3736 list_delete_node(l, node);
3737 mq->size--;
3738 }
e96f9203
DO
3739}
3740
d7ac4c4d
DS
3741static void early_route_meta_queue_free(struct meta_queue *mq, struct list *l,
3742 struct zebra_vrf *zvrf)
3743{
739bc9fd 3744 struct zebra_early_route *ere;
d7ac4c4d
DS
3745 struct listnode *node, *nnode;
3746
739bc9fd
DS
3747 for (ALL_LIST_ELEMENTS(l, node, nnode, ere)) {
3748 if (zvrf && ere->re->vrf_id != zvrf->vrf->vrf_id)
d7ac4c4d
DS
3749 continue;
3750
739bc9fd 3751 early_route_memory_free(ere);
d7ac4c4d
DS
3752 node->data = NULL;
3753 list_delete_node(l, node);
3754 mq->size--;
3755 }
3756}
a310ebc1 3757
81322b96
DS
3758static void rib_meta_queue_gr_run_free(struct meta_queue *mq, struct list *l,
3759 struct zebra_vrf *zvrf)
3760{
3761 struct meta_q_gr_run *gr_run;
3762 struct listnode *node, *nnode;
3763
3764 for (ALL_LIST_ELEMENTS(l, node, nnode, gr_run)) {
3765 if (zvrf && zvrf->vrf->vrf_id != gr_run->vrf_id)
3766 continue;
3767
3768 XFREE(MTYPE_WQ_WRAPPER, gr_run);
1b192d88
DS
3769 node->data = NULL;
3770 list_delete_node(l, node);
3771 mq->size--;
81322b96
DS
3772 }
3773}
3774
a310ebc1 3775void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf)
5a8dfcd8 3776{
014e732d 3777 enum meta_queue_indexes i;
5a8dfcd8 3778
04bec7b2
MS
3779 for (i = 0; i < MQ_SIZE; i++) {
3780 /* Some subqueues may need cleanup - nhgs for example */
014e732d
DS
3781 switch (i) {
3782 case META_QUEUE_NHG:
a310ebc1 3783 nhg_meta_queue_free(mq, mq->subq[i], zvrf);
014e732d
DS
3784 break;
3785 case META_QUEUE_EVPN:
a310ebc1 3786 evpn_meta_queue_free(mq, mq->subq[i], zvrf);
014e732d 3787 break;
d7ac4c4d
DS
3788 case META_QUEUE_EARLY_ROUTE:
3789 early_route_meta_queue_free(mq, mq->subq[i], zvrf);
3790 break;
53216dff
DS
3791 case META_QUEUE_EARLY_LABEL:
3792 early_label_meta_queue_free(mq, mq->subq[i], zvrf);
3793 break;
014e732d
DS
3794 case META_QUEUE_CONNECTED:
3795 case META_QUEUE_KERNEL:
3796 case META_QUEUE_STATIC:
3797 case META_QUEUE_NOTBGP:
3798 case META_QUEUE_BGP:
3799 case META_QUEUE_OTHER:
a310ebc1 3800 rib_meta_queue_free(mq, mq->subq[i], zvrf);
014e732d 3801 break;
81322b96
DS
3802 case META_QUEUE_GR_RUN:
3803 rib_meta_queue_gr_run_free(mq, mq->subq[i], zvrf);
3804 break;
014e732d 3805 }
a310ebc1
DS
3806 if (!zvrf)
3807 list_delete(&mq->subq[i]);
04bec7b2 3808 }
5a8dfcd8 3809
a310ebc1
DS
3810 if (!zvrf)
3811 XFREE(MTYPE_WORK_QUEUE, mq);
0ef6eacc
IR
3812}
3813
4d38fdb4 3814/* initialise zebra rib work queue */
2561d12e 3815static void rib_queue_init(void)
4d38fdb4 3816{
489a9614
DS
3817 if (!(zrouter.ribq = work_queue_new(zrouter.master,
3818 "route_node processing"))) {
e914ccbe 3819 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 3820 "%s: could not initialise work queue!", __func__);
d62a17ae 3821 return;
3822 }
4d38fdb4 3823
d62a17ae 3824 /* fill in the work queue spec */
489a9614 3825 zrouter.ribq->spec.workfunc = &meta_queue_process;
46a4e345 3826 zrouter.ribq->spec.completion_func = NULL;
d62a17ae 3827 /* XXX: TODO: These should be runtime configurable via vty */
489a9614
DS
3828 zrouter.ribq->spec.max_retries = 3;
3829 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
3830 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
d62a17ae 3831
ea45a4e7 3832 if (!(zrouter.mq = meta_queue_new())) {
e914ccbe 3833 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 3834 "%s: could not initialise meta queue!", __func__);
d62a17ae 3835 return;
3836 }
3837 return;
718e3744 3838}
3839
434434f7
DS
3840rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
3841{
3842 rib_dest_t *dest;
3843
3844 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
aa57abfb 3845 rnh_list_init(&dest->nht);
daaeaa21 3846 re_list_init(&dest->routes);
434434f7
DS
3847 route_lock_node(rn); /* rn route table reference */
3848 rn->info = dest;
3849 dest->rnode = rn;
3850
3851 return dest;
3852}
3853
6d691129
PJ
3854/* RIB updates are processed via a queue of pointers to route_nodes.
3855 *
3856 * The queue length is bounded by the maximal size of the routing table,
3857 * as a route_node will not be requeued, if already queued.
3858 *
f0f77c9a
DS
3859 * REs are submitted via rib_addnode or rib_delnode which set minimal
3860 * state, or static_install_route (when an existing RE is updated)
3c0755dc 3861 * and then submit route_node to queue for best-path selection later.
f0f77c9a 3862 * Order of add/delete state changes are preserved for any given RE.
6d691129 3863 *
f0f77c9a 3864 * Deleted REs are reaped during best-path selection.
6d691129
PJ
3865 *
3866 * rib_addnode
f0f77c9a
DS
3867 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3868 * |-------->| | best RE, if required
3c0755dc
PJ
3869 * | |
3870 * static_install->|->rib_addqueue...... -> rib_process
3871 * | |
3872 * |-------->| |-> rib_unlink
f0f77c9a
DS
3873 * |-> set ROUTE_ENTRY_REMOVE |
3874 * rib_delnode (RE freed)
6d691129 3875 *
9fd92e3c
AS
3876 * The 'info' pointer of a route_node points to a rib_dest_t
3877 * ('dest'). Queueing state for a route_node is kept on the dest. The
3878 * dest is created on-demand by rib_link() and is kept around at least
3879 * as long as there are ribs hanging off it (@see rib_gc_dest()).
d62a17ae 3880 *
32367e7a 3881 * Refcounting (aka "locking" throughout the Zebra and FRR code):
6d691129
PJ
3882 *
3883 * - route_nodes: refcounted by:
9fd92e3c
AS
3884 * - dest attached to route_node:
3885 * - managed by: rib_link/rib_gc_dest
6d691129
PJ
3886 * - route_node processing queue
3887 * - managed by: rib_addqueue, rib_process.
3888 *
3889 */
d62a17ae 3890
f0f77c9a 3891/* Add RE to head of the route node. */
d62a17ae 3892static void rib_link(struct route_node *rn, struct route_entry *re, int process)
3893{
d62a17ae 3894 rib_dest_t *dest;
3895 afi_t afi;
3896 const char *rmap_name;
9fd92e3c 3897
d62a17ae 3898 assert(re && rn);
9fd92e3c 3899
d62a17ae 3900 dest = rib_dest_from_rnode(rn);
3901 if (!dest) {
3902 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3903 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
7a4bb9c5 3904
434434f7 3905 dest = zebra_rib_create_dest(rn);
d62a17ae 3906 }
2263a412 3907
045207e2 3908 re_list_add_head(&dest->routes, re);
d62a17ae 3909
3910 afi = (rn->p.family == AF_INET)
3911 ? AFI_IP
3912 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
fe257ae7
DS
3913 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3914 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3915
d62a17ae 3916 rmap_name = zebra_get_import_table_route_map(afi, re->table);
fe257ae7 3917 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
207207c0
S
3918 }
3919
3920 if (process)
d62a17ae 3921 rib_queue_add(rn);
3922}
3923
7e24fdf3
DS
3924static void rib_addnode(struct route_node *rn,
3925 struct route_entry *re, int process)
d62a17ae 3926{
3927 /* RE node has been un-removed before route-node is processed.
3928 * route_node must hence already be on the queue for processing..
3929 */
3930 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
3931 if (IS_ZEBRA_DEBUG_RIB)
3932 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
3933 (void *)rn, (void *)re);
3934
3935 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3936 return;
3937 }
3938 rib_link(rn, re, process);
6d691129
PJ
3939}
3940
9fd92e3c
AS
3941/*
3942 * rib_unlink
3943 *
3944 * Detach a rib structure from a route_node.
3945 *
3946 * Note that a call to rib_unlink() should be followed by a call to
3947 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3948 * longer required to be deleted.
3949 */
d62a17ae 3950void rib_unlink(struct route_node *rn, struct route_entry *re)
6d691129 3951{
d62a17ae 3952 rib_dest_t *dest;
9fd92e3c 3953
d62a17ae 3954 assert(rn && re);
6d691129 3955
d62a17ae 3956 if (IS_ZEBRA_DEBUG_RIB)
3957 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
3958 (void *)re);
6d691129 3959
d62a17ae 3960 dest = rib_dest_from_rnode(rn);
6d691129 3961
045207e2 3962 re_list_del(&dest->routes, re);
7a4bb9c5 3963
2eb07de3
DS
3964 if (dest->selected_fib == re)
3965 dest->selected_fib = NULL;
3966
ce649b9d 3967 rib_re_nhg_free(re);
ee5e8a48 3968
b86c1f4f 3969 zapi_re_opaque_free(re->opaque);
a29a6001 3970
d62a17ae 3971 XFREE(MTYPE_RE, re);
3972}
3973
3974void rib_delnode(struct route_node *rn, struct route_entry *re)
3975{
3976 afi_t afi;
3977
3978 if (IS_ZEBRA_DEBUG_RIB)
3979 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
3980 (void *)rn, (void *)re);
3981 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3982
3983 afi = (rn->p.family == AF_INET)
3984 ? AFI_IP
3985 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
fe257ae7
DS
3986 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3987 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3988
3989 zebra_del_import_table_entry(zvrf, rn, re);
d62a17ae 3990 /* Just clean up if non main table */
e658173a
DS
3991 if (IS_ZEBRA_DEBUG_RIB)
3992 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3993 vrf_id_to_name(re->vrf_id), re->vrf_id, rn,
bd47f3a3 3994 rn, re, zebra_route_string(re->type));
d62a17ae 3995 }
207207c0
S
3996
3997 rib_queue_add(rn);
718e3744 3998}
3999
6d81b590
MS
4000/*
4001 * Helper that debugs a single nexthop within a route-entry
4002 */
4003static void _route_entry_dump_nh(const struct route_entry *re,
4004 const char *straddr,
4005 const struct nexthop *nexthop)
4006{
4007 char nhname[PREFIX_STRLEN];
4008 char backup_str[50];
4009 char wgt_str[50];
474aebd9 4010 char temp_str[10];
cbe5bafb 4011 char label_str[MPLS_LABEL_STRLEN];
474aebd9 4012 int i;
6d81b590
MS
4013 struct interface *ifp;
4014 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
4015
4016 switch (nexthop->type) {
4017 case NEXTHOP_TYPE_BLACKHOLE:
772270f3 4018 snprintf(nhname, sizeof(nhname), "Blackhole");
6d81b590
MS
4019 break;
4020 case NEXTHOP_TYPE_IFINDEX:
4021 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
772270f3
QY
4022 snprintf(nhname, sizeof(nhname), "%s",
4023 ifp ? ifp->name : "Unknown");
6d81b590
MS
4024 break;
4025 case NEXTHOP_TYPE_IPV4:
4026 /* fallthrough */
4027 case NEXTHOP_TYPE_IPV4_IFINDEX:
4028 inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
4029 break;
4030 case NEXTHOP_TYPE_IPV6:
4031 case NEXTHOP_TYPE_IPV6_IFINDEX:
4032 inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
4033 break;
4034 }
4035
cbe5bafb
MS
4036 /* Label stack */
4037 label_str[0] = '\0';
4038 if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) {
4039 mpls_label2str(nexthop->nh_label->num_labels,
4040 nexthop->nh_label->label, label_str,
4645cb6b
SW
4041 sizeof(label_str), nexthop->nh_label_type,
4042 0 /*pretty*/);
cbe5bafb
MS
4043 strlcat(label_str, ", ", sizeof(label_str));
4044 }
4045
6d81b590
MS
4046 backup_str[0] = '\0';
4047 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
474aebd9
MS
4048 snprintf(backup_str, sizeof(backup_str), "backup ");
4049 for (i = 0; i < nexthop->backup_num; i++) {
4050 snprintf(temp_str, sizeof(temp_str), "%d, ",
4051 nexthop->backup_idx[i]);
4052 strlcat(backup_str, temp_str, sizeof(backup_str));
4053 }
6d81b590
MS
4054 }
4055
4056 wgt_str[0] = '\0';
4057 if (nexthop->weight)
4058 snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight);
4059
5609e70f 4060 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
6d81b590 4061 straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
cbe5bafb 4062 nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown",
6d81b590
MS
4063 nexthop->vrf_id,
4064 wgt_str, backup_str,
4065 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
4066 ? "ACTIVE "
4067 : ""),
4068 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
4069 ? "FIB "
4070 : ""),
4071 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
4072 ? "RECURSIVE "
4073 : ""),
4074 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
4075 ? "ONLINK "
4076 : ""),
4077 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
4078 ? "DUPLICATE "
aab4eca1
DS
4079 : ""),
4080 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED)
4081 ? "FILTERED " : ""),
4082 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)
4083 ? "BACKUP " : ""),
4084 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE)
5609e70f
XL
4085 ? "SRTE " : ""),
4086 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN)
4087 ? "EVPN " : ""));
6d81b590
MS
4088
4089}
4090
f0f77c9a 4091/* This function dumps the contents of a given RE entry into
dc95824a
DO
4092 * standard debug log. Calling function name and IP prefix in
4093 * question are passed as 1st and 2nd arguments.
4094 */
d62a17ae 4095void _route_entry_dump(const char *func, union prefixconstptr pp,
4096 union prefixconstptr src_pp,
4097 const struct route_entry *re)
4098{
d62a17ae 4099 const struct prefix *src_p = src_pp.p;
4100 bool is_srcdst = src_p && src_p->prefixlen;
4101 char straddr[PREFIX_STRLEN];
4102 char srcaddr[PREFIX_STRLEN];
61e6de9d
DS
4103 char flags_buf[128];
4104 char status_buf[128];
d62a17ae 4105 struct nexthop *nexthop;
c479e756 4106 struct vrf *vrf = vrf_lookup_by_id(re->vrf_id);
6d81b590 4107 struct nexthop_group *nhg;
d62a17ae 4108
5d27875b
DL
4109 prefix2str(pp, straddr, sizeof(straddr));
4110
c479e756 4111 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func,
5d27875b 4112 (const void *)re, straddr,
d62a17ae 4113 is_srcdst ? " from " : "",
4114 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
4115 : "",
c479e756 4116 VRF_LOGNAME(vrf), re->vrf_id);
cc54cfee 4117 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
53c16fbe 4118 straddr, (unsigned long)re->uptime, re->type, re->instance,
cc54cfee 4119 re->table);
61e6de9d
DS
4120 zlog_debug(
4121 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4122 straddr, re->metric, re->mtu, re->distance,
4123 zclient_dump_route_flags(re->flags, flags_buf,
4124 sizeof(flags_buf)),
4125 _dump_re_status(re, status_buf, sizeof(status_buf)));
53c16fbe 4126 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
c415d895
MS
4127 nexthop_group_nexthop_num(&(re->nhe->nhg)),
4128 nexthop_group_active_nexthop_num(&(re->nhe->nhg)));
d62a17ae 4129
6d81b590
MS
4130 /* Dump nexthops */
4131 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4132 _route_entry_dump_nh(re, straddr, nexthop);
2d68a0f2 4133
d7ac4c4d
DS
4134 if (zebra_nhg_get_backup_nhg(re->nhe)) {
4135 zlog_debug("%s: backup nexthops:", straddr);
c6eee91f 4136
d7ac4c4d
DS
4137 nhg = zebra_nhg_get_backup_nhg(re->nhe);
4138 for (ALL_NEXTHOPS_PTR(nhg, nexthop))
4139 _route_entry_dump_nh(re, straddr, nexthop);
c6eee91f
DS
4140 }
4141
d7ac4c4d
DS
4142 zlog_debug("%s: dump complete", straddr);
4143}
cf363e1b 4144
81322b96
DS
4145static int rib_meta_queue_gr_run_add(struct meta_queue *mq, void *data)
4146{
4147 listnode_add(mq->subq[META_QUEUE_GR_RUN], data);
4148 mq->size++;
4149
4150 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
4151 zlog_debug("Graceful Run adding");
4152
4153 return 0;
4154}
4155
d7ac4c4d
DS
4156static int rib_meta_queue_early_route_add(struct meta_queue *mq, void *data)
4157{
4158 struct zebra_early_route *ere = data;
718e3744 4159
d7ac4c4d
DS
4160 listnode_add(mq->subq[META_QUEUE_EARLY_ROUTE], data);
4161 mq->size++;
6883bf8d 4162
d7ac4c4d
DS
4163 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
4164 zlog_debug(
4165 "Route %pFX(%u) queued for processing into sub-queue %s",
4166 &ere->p, ere->re->vrf_id,
4167 subqueue2str(META_QUEUE_EARLY_ROUTE));
6b0655a2 4168
d7ac4c4d
DS
4169 return 0;
4170}
718e3744 4171
81322b96
DS
4172int rib_add_gr_run(afi_t afi, vrf_id_t vrf_id, uint8_t proto, uint8_t instance)
4173{
4174 struct meta_q_gr_run *gr_run;
4175
4176 gr_run = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*gr_run));
4177
4178 gr_run->afi = afi;
4179 gr_run->proto = proto;
4180 gr_run->vrf_id = vrf_id;
4181 gr_run->instance = instance;
4182
4183 return mq_add_handler(gr_run, rib_meta_queue_gr_run_add);
4184}
4185
b0385873
DS
4186struct route_entry *zebra_rib_route_entry_new(vrf_id_t vrf_id, int type,
4187 uint8_t instance, uint32_t flags,
4188 uint32_t nhe_id,
4189 uint32_t table_id,
4190 uint32_t metric, uint32_t mtu,
4191 uint8_t distance, route_tag_t tag)
4192{
4193 struct route_entry *re;
4194
4195 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
4196 re->type = type;
4197 re->instance = instance;
4198 re->distance = distance;
4199 re->flags = flags;
4200 re->metric = metric;
4201 re->mtu = mtu;
4202 re->table = table_id;
4203 re->vrf_id = vrf_id;
4204 re->uptime = monotime(NULL);
4205 re->tag = tag;
4206 re->nhe_id = nhe_id;
4207
4208 return re;
4209}
d7ac4c4d
DS
4210/*
4211 * Internal route-add implementation; there are a couple of different public
4212 * signatures. Callers in this path are responsible for the memory they
4213 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4214 * must free those objects. If this returns < 0, an error has occurred and the
4215 * route_entry 're' has not been captured; the caller should free that also.
4216 *
4217 * -1 -> error
4218 * 0 -> Add
4219 * 1 -> update
4220 */
4221int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
4222 struct prefix_ipv6 *src_p, struct route_entry *re,
4223 struct nhg_hash_entry *re_nhe, bool startup)
4224{
4225 struct zebra_early_route *ere;
0ca6f3b1 4226
d7ac4c4d
DS
4227 if (!re)
4228 return -1;
0ca6f3b1 4229
d7ac4c4d 4230 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
0ca6f3b1 4231
d7ac4c4d
DS
4232 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4233 ere->afi = afi;
4234 ere->safi = safi;
4235 ere->p = *p;
4236 if (src_p)
4237 ere->src_p = *src_p;
4238 ere->src_p_provided = !!src_p;
4239 ere->re = re;
4240 ere->re_nhe = re_nhe;
4241 ere->startup = startup;
4242
4243 return mq_add_handler(ere, rib_meta_queue_early_route_add);
d62a17ae 4244}
4245
1d48702e
MS
4246/*
4247 * Add a single route.
4248 */
4249int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
4250 struct prefix_ipv6 *src_p, struct route_entry *re,
c6eee91f 4251 struct nexthop_group *ng, bool startup)
1d48702e
MS
4252{
4253 int ret;
d7ac4c4d 4254 struct nhg_hash_entry nhe, *n;
1d48702e
MS
4255
4256 if (!re)
4257 return -1;
4258
4259 /* We either need nexthop(s) or an existing nexthop id */
4260 if (ng == NULL && re->nhe_id == 0)
4261 return -1;
4262
4263 /*
4264 * Use a temporary nhe to convey info to the common/main api.
4265 */
377e29f7 4266 zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL));
1d48702e
MS
4267 if (ng)
4268 nhe.nhg.nexthop = ng->nexthop;
4269 else if (re->nhe_id > 0)
4270 nhe.id = re->nhe_id;
4271
d7ac4c4d
DS
4272 n = zebra_nhe_copy(&nhe, 0);
4273 ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, n, startup);
1d48702e
MS
4274
4275 /* In error cases, free the route also */
4276 if (ret < 0)
4277 XFREE(MTYPE_RE, re);
4278
4279 return ret;
4280}
4281
d62a17ae 4282void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
acde7f6b 4283 unsigned short instance, uint32_t flags, struct prefix *p,
fd36be7e 4284 struct prefix_ipv6 *src_p, const struct nexthop *nh,
bc541126 4285 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
3ceae22b 4286 uint8_t distance, bool fromkernel)
d62a17ae 4287{
d7ac4c4d
DS
4288 struct zebra_early_route *ere;
4289 struct route_entry *re = NULL;
4290 struct nhg_hash_entry *nhe = NULL;
cf363e1b 4291
b0385873
DS
4292 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4293 table_id, metric, 0, distance, 0);
cf363e1b 4294
d7ac4c4d
DS
4295 if (nh) {
4296 nhe = zebra_nhg_alloc();
4297 nhe->nhg.nexthop = nexthop_dup(nh, NULL);
5dfeba19 4298 }
05737783 4299
d7ac4c4d
DS
4300 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4301 ere->afi = afi;
4302 ere->safi = safi;
4303 ere->p = *p;
4304 if (src_p)
4305 ere->src_p = *src_p;
4306 ere->src_p_provided = !!src_p;
4307 ere->re = re;
4308 ere->re_nhe = nhe;
4309 ere->startup = false;
4310 ere->deletion = true;
4311 ere->fromkernel = fromkernel;
4312
4313 mq_add_handler(ere, rib_meta_queue_early_route_add);
d62a17ae 4314}
718e3744 4315
718e3744 4316
d7c0a89a 4317int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
acde7f6b 4318 unsigned short instance, uint32_t flags, struct prefix *p,
d7c0a89a 4319 struct prefix_ipv6 *src_p, const struct nexthop *nh,
8032b717 4320 uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu,
c6eee91f 4321 uint8_t distance, route_tag_t tag, bool startup)
d62a17ae 4322{
bbb322f2 4323 struct route_entry *re = NULL;
d7ac4c4d
DS
4324 struct nexthop nexthop = {};
4325 struct nexthop_group ng = {};
718e3744 4326
66af6845 4327 /* Allocate new route_entry structure. */
b0385873
DS
4328 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4329 table_id, metric, mtu, distance, tag);
bbb322f2 4330
0eb97b86
MS
4331 /* If the owner of the route supplies a shared nexthop-group id,
4332 * we'll use that. Otherwise, pass the nexthop along directly.
4333 */
e22e8001 4334 if (!nhe_id) {
e22e8001 4335 /* Add nexthop. */
d7ac4c4d
DS
4336 nexthop = *nh;
4337 nexthop_group_add_sorted(&ng, &nexthop);
e22e8001 4338 }
718e3744 4339
d7ac4c4d 4340 return rib_add_multipath(afi, safi, p, src_p, re, &ng, startup);
718e3744 4341}
4342
91e6f25b 4343static const char *rib_update_event2str(enum rib_update_event event)
2a99ab95
SW
4344{
4345 const char *ret = "UNKNOWN";
4346
4347 switch (event) {
4348 case RIB_UPDATE_KERNEL:
4349 ret = "RIB_UPDATE_KERNEL";
4350 break;
4351 case RIB_UPDATE_RMAP_CHANGE:
4352 ret = "RIB_UPDATE_RMAP_CHANGE";
4353 break;
4354 case RIB_UPDATE_OTHER:
4355 ret = "RIB_UPDATE_OTHER";
4356 break;
4357 case RIB_UPDATE_MAX:
4358 break;
4359 }
4360
4361 return ret;
4362}
4363
4364
4365/* Schedule route nodes to be processed if they match the type */
4366static void rib_update_route_node(struct route_node *rn, int type)
4367{
4368 struct route_entry *re, *next;
4369 bool re_changed = false;
4370
4371 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4372 if (type == ZEBRA_ROUTE_ALL || type == re->type) {
4373 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
4374 re_changed = true;
4375 }
4376 }
4377
4378 if (re_changed)
4379 rib_queue_add(rn);
4380}
4381
1c848137 4382/* Schedule routes of a particular table (address-family) based on event. */
54aeba35
DS
4383void rib_update_table(struct route_table *table, enum rib_update_event event,
4384 int rtype)
d62a17ae 4385{
4386 struct route_node *rn;
2a99ab95
SW
4387
4388 if (IS_ZEBRA_DEBUG_EVENT) {
4389 struct zebra_vrf *zvrf;
4390 struct vrf *vrf;
4391
630d5962
DS
4392 zvrf = table->info
4393 ? ((struct rib_table_info *)table->info)->zvrf
4394 : NULL;
2a99ab95
SW
4395 vrf = zvrf ? zvrf->vrf : NULL;
4396
54aeba35 4397 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__,
2a99ab95 4398 table->info ? afi2str(
630d5962 4399 ((struct rib_table_info *)table->info)->afi)
2a99ab95 4400 : "Unknown",
bd47f3a3 4401 VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0,
54aeba35 4402 rib_update_event2str(event), zebra_route_string(rtype));
2a99ab95 4403 }
d62a17ae 4404
4405 /* Walk all routes and queue for processing, if appropriate for
4406 * the trigger event.
4407 */
4408 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
1ca60f2c
DS
4409 /*
4410 * If we are looking at a route node and the node
4411 * has already been queued we don't
4412 * need to queue it up again
4413 */
2a99ab95
SW
4414 if (rn->info
4415 && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
4416 RIB_ROUTE_ANY_QUEUED))
1ca60f2c 4417 continue;
2a99ab95 4418
d62a17ae 4419 switch (event) {
2a99ab95
SW
4420 case RIB_UPDATE_KERNEL:
4421 rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL);
4422 break;
d62a17ae 4423 case RIB_UPDATE_RMAP_CHANGE:
4424 case RIB_UPDATE_OTHER:
54aeba35 4425 rib_update_route_node(rn, rtype);
d62a17ae 4426 break;
a98701f0 4427 case RIB_UPDATE_MAX:
d62a17ae 4428 break;
4429 }
4430 }
b84c7253 4431}
4432
54aeba35 4433static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype)
2a99ab95
SW
4434{
4435 struct zebra_router_table *zrt;
4436
4437 if (IS_ZEBRA_DEBUG_EVENT)
4438 zlog_debug("%s: Handling VRF (ALL) event %s", __func__,
4439 rib_update_event2str(event));
4440
4441 /* Just iterate over all the route tables, rather than vrf lookups */
4442 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables)
54aeba35 4443 rib_update_table(zrt->table, event, rtype);
2a99ab95
SW
4444}
4445
4446struct rib_update_ctx {
91e6f25b 4447 enum rib_update_event event;
2a99ab95
SW
4448 vrf_id_t vrf_id;
4449};
4450
4451static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id,
91e6f25b 4452 enum rib_update_event event)
2a99ab95
SW
4453{
4454 struct rib_update_ctx *ctx;
4455
4456 ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx));
4457
4458 ctx->event = event;
4459 ctx->vrf_id = vrf_id;
4460
4461 return ctx;
4462}
4463
4464static void rib_update_ctx_fini(struct rib_update_ctx **ctx)
4465{
4466 XFREE(MTYPE_RIB_UPDATE_CTX, *ctx);
2a99ab95
SW
4467}
4468
e6685141 4469static void rib_update_handler(struct event *thread)
2a99ab95
SW
4470{
4471 struct rib_update_ctx *ctx;
4472
e16d030c 4473 ctx = EVENT_ARG(thread);
2a99ab95 4474
d2a17423 4475 rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL);
2a99ab95
SW
4476
4477 rib_update_ctx_fini(&ctx);
2a99ab95
SW
4478}
4479
4480/*
4481 * Thread list to ensure we don't schedule a ton of events
4482 * if interfaces are flapping for instance.
4483 */
e6685141 4484static struct event *t_rib_update_threads[RIB_UPDATE_MAX];
2a99ab95 4485
3cd0accb
DS
4486void rib_update_finish(void)
4487{
4488 int i;
4489
4490 for (i = RIB_UPDATE_KERNEL; i < RIB_UPDATE_MAX; i++) {
4491 if (event_is_scheduled(t_rib_update_threads[i])) {
4492 struct rib_update_ctx *ctx;
4493
4494 ctx = EVENT_ARG(t_rib_update_threads[i]);
4495
4496 rib_update_ctx_fini(&ctx);
4497 EVENT_OFF(t_rib_update_threads[i]);
4498 }
4499 }
4500}
4501
2a99ab95 4502/* Schedule a RIB update event for all vrfs */
91e6f25b 4503void rib_update(enum rib_update_event event)
2a99ab95
SW
4504{
4505 struct rib_update_ctx *ctx;
4506
5f6eaa9b 4507 if (event_is_scheduled(t_rib_update_threads[event]))
e13f12a7 4508 return;
2a99ab95 4509
3cd0accb
DS
4510 if (zebra_router_in_shutdown())
4511 return;
4512
e13f12a7 4513 ctx = rib_update_ctx_init(0, event);
2a99ab95 4514
907a2395
DS
4515 event_add_event(zrouter.master, rib_update_handler, ctx, 0,
4516 &t_rib_update_threads[event]);
e13f12a7
DS
4517
4518 if (IS_ZEBRA_DEBUG_EVENT)
05267678 4519 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__,
2a99ab95 4520 rib_update_event2str(event));
718e3744 4521}
4522
718e3744 4523/* Delete self installed routes after zebra is relaunched. */
95a29032 4524void rib_sweep_table(struct route_table *table)
d62a17ae 4525{
4526 struct route_node *rn;
4527 struct route_entry *re;
4528 struct route_entry *next;
915902cb 4529 struct nexthop *nexthop;
d62a17ae 4530
915902cb
DS
4531 if (!table)
4532 return;
d62a17ae 4533
6d81b590
MS
4534 if (IS_ZEBRA_DEBUG_RIB)
4535 zlog_debug("%s: starting", __func__);
4536
915902cb 4537 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
a2addae8 4538 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d4644d41 4539
915902cb
DS
4540 if (IS_ZEBRA_DEBUG_RIB)
4541 route_entry_dump(&rn->p, NULL, re);
4542
4543 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4544 continue;
4545
4546 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
4547 continue;
4548
d4644d41
DS
4549 /*
4550 * If routes are older than startup_time then
4551 * we know we read them in from the kernel.
4552 * As such we can safely remove them.
4553 */
4554 if (zrouter.startup_time < re->uptime)
4555 continue;
4556
915902cb
DS
4557 /*
4558 * So we are starting up and have received
4559 * routes from the kernel that we have installed
4560 * from a previous run of zebra but not cleaned
4561 * up ( say a kill -9 )
4562 * But since we haven't actually installed
4563 * them yet( we received them from the kernel )
4564 * we don't think they are active.
4565 * So let's pretend they are active to actually
4566 * remove them.
4567 * In all honesty I'm not sure if we should
4568 * mark them as active when we receive them
4569 * This is startup only so probably ok.
4570 *
4571 * If we ever decide to move rib_sweep_table
4572 * to a different spot (ie startup )
4573 * this decision needs to be revisited
4574 */
677c1dd5 4575 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
c415d895 4576 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
915902cb
DS
4577 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
4578
0c555cc6
DS
4579 rib_uninstall_kernel(rn, re);
4580 rib_delnode(rn, re);
915902cb
DS
4581 }
4582 }
6d81b590
MS
4583
4584 if (IS_ZEBRA_DEBUG_RIB)
4585 zlog_debug("%s: ends", __func__);
718e3744 4586}
4587
4588/* Sweep all RIB tables. */
e6685141 4589void rib_sweep_route(struct event *t)
718e3744 4590{
d62a17ae 4591 struct vrf *vrf;
4592 struct zebra_vrf *zvrf;
78104b9b 4593
a2addae8 4594 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
915902cb
DS
4595 if ((zvrf = vrf->info) == NULL)
4596 continue;
4597
d62a17ae 4598 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
4599 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
4600 }
95a29032 4601
89272910 4602 zebra_router_sweep_route();
38e40db1 4603 zebra_router_sweep_nhgs();
718e3744 4604}
2ea1ab1c
VT
4605
4606/* Remove specific by protocol routes from 'table'. */
d7c0a89a 4607unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
47a08aa9 4608 struct route_table *table)
d62a17ae 4609{
4610 struct route_node *rn;
4611 struct route_entry *re;
4612 struct route_entry *next;
4613 unsigned long n = 0;
4614
4615 if (table)
4616 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 4617 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 4618 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4619 continue;
4620 if (re->type == proto
4621 && re->instance == instance) {
4622 rib_delnode(rn, re);
4623 n++;
4624 }
4625 }
4626 return n;
2ea1ab1c
VT
4627}
4628
4629/* Remove specific by protocol routes. */
d7c0a89a 4630unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2ea1ab1c 4631{
d62a17ae 4632 struct vrf *vrf;
4633 struct zebra_vrf *zvrf;
d8612e65 4634 struct other_route_table *ort;
d62a17ae 4635 unsigned long cnt = 0;
78104b9b 4636
d8612e65
DS
4637 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4638 zvrf = vrf->info;
4639 if (!zvrf)
4640 continue;
78104b9b 4641
d8612e65
DS
4642 cnt += rib_score_proto_table(proto, instance,
4643 zvrf->table[AFI_IP][SAFI_UNICAST])
4644 + rib_score_proto_table(
4645 proto, instance,
4646 zvrf->table[AFI_IP6][SAFI_UNICAST]);
4647
81fddbe7 4648 frr_each(otable, &zvrf->other_tables, ort) cnt +=
d8612e65
DS
4649 rib_score_proto_table(proto, instance, ort->table);
4650 }
47a08aa9 4651
d62a17ae 4652 return cnt;
2ea1ab1c
VT
4653}
4654
718e3744 4655/* Close RIB and clean up kernel routes. */
d62a17ae 4656void rib_close_table(struct route_table *table)
718e3744 4657{
d62a17ae 4658 struct route_node *rn;
5f7a4718 4659 rib_dest_t *dest;
718e3744 4660
1e9f448f
DS
4661 if (!table)
4662 return;
9fd92e3c 4663
5f7a4718
DS
4664 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4665 dest = rib_dest_from_rnode(rn);
1e9f448f 4666
5f7a4718 4667 if (dest && dest->selected_fib) {
3cdba47a
DS
4668 rib_uninstall_kernel(rn, dest->selected_fib);
4669 dest->selected_fib = NULL;
1e9f448f 4670 }
5f7a4718 4671 }
718e3744 4672}
4673
9bd9717b
MS
4674/*
4675 * Handler for async dataplane results after a pseudowire installation
4676 */
f00b37e7 4677static void handle_pw_result(struct zebra_dplane_ctx *ctx)
9bd9717b 4678{
9bd9717b
MS
4679 struct zebra_pw *pw;
4680 struct zebra_vrf *vrf;
4681
4682 /* The pseudowire code assumes success - we act on an error
4683 * result for installation attempts here.
4684 */
4685 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
f00b37e7 4686 return;
9bd9717b
MS
4687
4688 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
4689 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
7c7ef4a8 4690 pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx));
9bd9717b 4691 if (pw)
fd563cc7
KS
4692 zebra_pw_install_failure(pw,
4693 dplane_ctx_get_pw_status(ctx));
9bd9717b 4694 }
9bd9717b
MS
4695}
4696
7cdb1a84 4697/*
d37f4d6c
MS
4698 * Handle results from the dataplane system. Dequeue update context
4699 * structs, dispatch to appropriate internal handlers.
7cdb1a84 4700 */
e6685141 4701static void rib_process_dplane_results(struct event *thread)
7cdb1a84 4702{
25779064 4703 struct zebra_dplane_ctx *ctx;
ac96497c 4704 struct dplane_ctx_list_head ctxlist;
2fc69f03 4705 bool shut_p = false;
4c206c8f 4706
d8be1399
DS
4707#ifdef HAVE_SCRIPTING
4708 char *script_name =
4709 frrscript_names_get_script_name(ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
4710
4711 int ret = 1;
4712 struct frrscript *fs = NULL;
4713
4714 if (script_name) {
4715 fs = frrscript_new(script_name);
4716 if (fs)
4717 ret = frrscript_load(fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4718 NULL);
4719 }
4720#endif /* HAVE_SCRIPTING */
4721
4c206c8f 4722 /* Dequeue a list of completed updates with one lock/unlock cycle */
7cdb1a84
MS
4723
4724 do {
ac96497c 4725 dplane_ctx_q_init(&ctxlist);
4c206c8f 4726
7cdb1a84 4727 /* Take lock controlling queue of results */
cb1991af 4728 frr_with_mutex (&dplane_mutex) {
d37f4d6c 4729 /* Dequeue list of context structs */
4c206c8f 4730 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
7cdb1a84 4731 }
7cdb1a84 4732
4c206c8f
MS
4733 /* Dequeue context block */
4734 ctx = dplane_ctx_dequeue(&ctxlist);
4735
4736 /* If we've emptied the results queue, we're done */
4737 if (ctx == NULL)
7cdb1a84 4738 break;
7cdb1a84 4739
2fc69f03
MS
4740 /* If zebra is shutting down, avoid processing results,
4741 * just drain the results queue.
4742 */
4743 shut_p = atomic_load_explicit(&zrouter.in_shutdown,
4744 memory_order_relaxed);
4745 if (shut_p) {
4746 while (ctx) {
4747 dplane_ctx_fini(&ctx);
4748
4749 ctx = dplane_ctx_dequeue(&ctxlist);
4750 }
4751
4752 continue;
4753 }
4754
4c206c8f 4755 while (ctx) {
1247efcc
DL
4756#ifdef HAVE_SCRIPTING
4757 if (ret == 0)
4758 frrscript_call(fs,
4759 ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4760 ("ctx", ctx));
4761#endif /* HAVE_SCRIPTING */
4762
d37f4d6c
MS
4763 switch (dplane_ctx_get_op(ctx)) {
4764 case DPLANE_OP_ROUTE_INSTALL:
4765 case DPLANE_OP_ROUTE_UPDATE:
4766 case DPLANE_OP_ROUTE_DELETE:
188a00e0
MS
4767 /* Bit of special case for route updates
4768 * that were generated by async notifications:
4769 * we don't want to continue processing these
4770 * in the rib.
4771 */
4772 if (dplane_ctx_get_notif_provider(ctx) == 0)
4773 rib_process_result(ctx);
f00b37e7 4774 break;
d37f4d6c 4775
54818e3b
MS
4776 case DPLANE_OP_ROUTE_NOTIFY:
4777 rib_process_dplane_notify(ctx);
d37f4d6c
MS
4778 break;
4779
9865e1c3
SW
4780 case DPLANE_OP_NH_INSTALL:
4781 case DPLANE_OP_NH_UPDATE:
4782 case DPLANE_OP_NH_DELETE:
4783 zebra_nhg_dplane_result(ctx);
4784 break;
4785
d37f4d6c
MS
4786 case DPLANE_OP_LSP_INSTALL:
4787 case DPLANE_OP_LSP_UPDATE:
4788 case DPLANE_OP_LSP_DELETE:
188a00e0
MS
4789 /* Bit of special case for LSP updates
4790 * that were generated by async notifications:
4791 * we don't want to continue processing these.
4792 */
4793 if (dplane_ctx_get_notif_provider(ctx) == 0)
4794 zebra_mpls_lsp_dplane_result(ctx);
f00b37e7 4795 break;
d37f4d6c 4796
104e3ad9
MS
4797 case DPLANE_OP_LSP_NOTIFY:
4798 zebra_mpls_process_dplane_notify(ctx);
d37f4d6c
MS
4799 break;
4800
9bd9717b
MS
4801 case DPLANE_OP_PW_INSTALL:
4802 case DPLANE_OP_PW_UNINSTALL:
4803 handle_pw_result(ctx);
4804 break;
4805
cf363e1b
MS
4806 case DPLANE_OP_SYS_ROUTE_ADD:
4807 case DPLANE_OP_SYS_ROUTE_DELETE:
cf363e1b
MS
4808 break;
4809
036d93c0
MS
4810 case DPLANE_OP_MAC_INSTALL:
4811 case DPLANE_OP_MAC_DELETE:
4812 zebra_vxlan_handle_result(ctx);
4813 break;
4814
f62e5480
JU
4815 case DPLANE_OP_RULE_ADD:
4816 case DPLANE_OP_RULE_DELETE:
4817 case DPLANE_OP_RULE_UPDATE:
5162e000
PG
4818 case DPLANE_OP_IPTABLE_ADD:
4819 case DPLANE_OP_IPTABLE_DELETE:
ef524230
PG
4820 case DPLANE_OP_IPSET_ADD:
4821 case DPLANE_OP_IPSET_DELETE:
4822 case DPLANE_OP_IPSET_ENTRY_ADD:
4823 case DPLANE_OP_IPSET_ENTRY_DELETE:
f62e5480
JU
4824 zebra_pbr_dplane_result(ctx);
4825 break;
4826
d166308b
MS
4827 case DPLANE_OP_INTF_ADDR_ADD:
4828 case DPLANE_OP_INTF_ADDR_DEL:
5d414138
SW
4829 case DPLANE_OP_INTF_INSTALL:
4830 case DPLANE_OP_INTF_UPDATE:
4831 case DPLANE_OP_INTF_DELETE:
cd787a8a 4832 case DPLANE_OP_INTF_NETCONFIG:
5d414138 4833 zebra_if_dplane_result(ctx);
cd787a8a
MS
4834 break;
4835
c317d3f2
SY
4836 case DPLANE_OP_TC_QDISC_INSTALL:
4837 case DPLANE_OP_TC_QDISC_UNINSTALL:
4838 case DPLANE_OP_TC_CLASS_ADD:
4839 case DPLANE_OP_TC_CLASS_DELETE:
4840 case DPLANE_OP_TC_CLASS_UPDATE:
4841 case DPLANE_OP_TC_FILTER_ADD:
4842 case DPLANE_OP_TC_FILTER_DELETE:
4843 case DPLANE_OP_TC_FILTER_UPDATE:
449a30ed
SY
4844 break;
4845
931fa60c
MS
4846 /* Some op codes not handled here */
4847 case DPLANE_OP_ADDR_INSTALL:
4848 case DPLANE_OP_ADDR_UNINSTALL:
4849 case DPLANE_OP_NEIGH_INSTALL:
4850 case DPLANE_OP_NEIGH_UPDATE:
4851 case DPLANE_OP_NEIGH_DELETE:
0a27a2fe
PG
4852 case DPLANE_OP_NEIGH_IP_INSTALL:
4853 case DPLANE_OP_NEIGH_IP_DELETE:
0bbd4ff4
MS
4854 case DPLANE_OP_VTEP_ADD:
4855 case DPLANE_OP_VTEP_DELETE:
d68e74b4 4856 case DPLANE_OP_NEIGH_DISCOVER:
c60522f7 4857 case DPLANE_OP_BR_PORT_UPDATE:
e18747a9 4858 case DPLANE_OP_NEIGH_TABLE_UPDATE:
62b4b7e4 4859 case DPLANE_OP_GRE_SET:
931fa60c 4860 case DPLANE_OP_NONE:
d37f4d6c 4861 break;
931fa60c 4862
d37f4d6c 4863 } /* Dispatch by op code */
4c206c8f 4864
f00b37e7 4865 dplane_ctx_fini(&ctx);
4c206c8f
MS
4866 ctx = dplane_ctx_dequeue(&ctxlist);
4867 }
4868
5709131c 4869 } while (1);
d8be1399
DS
4870
4871#ifdef HAVE_SCRIPTING
4872 if (fs)
4873 frrscript_delete(fs);
4874#endif
7cdb1a84
MS
4875}
4876
4877/*
4878 * Results are returned from the dataplane subsystem, in the context of
1bcea841 4879 * the dataplane pthread. We enqueue the results here for processing by
7cdb1a84
MS
4880 * the main thread later.
4881 */
ac96497c 4882static int rib_dplane_results(struct dplane_ctx_list_head *ctxlist)
7cdb1a84
MS
4883{
4884 /* Take lock controlling queue of results */
cb1991af 4885 frr_with_mutex (&dplane_mutex) {
4c206c8f
MS
4886 /* Enqueue context blocks */
4887 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
7cdb1a84 4888 }
7cdb1a84 4889
4c206c8f 4890 /* Ensure event is signalled to zebra main pthread */
907a2395
DS
4891 event_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
4892 &t_dplane);
7cdb1a84 4893
5709131c 4894 return 0;
7cdb1a84
MS
4895}
4896
eaa2716d
SW
4897/*
4898 * Ensure there are no empty slots in the route_info array.
4899 * Every route type in zebra should be present there.
4900 */
4901static void check_route_info(void)
4902{
4903 int len = array_size(route_info);
4904
4905 /*
4906 * ZEBRA_ROUTE_SYSTEM is special cased since
4907 * its key is 0 anyway.
4908 *
4909 * ZEBRA_ROUTE_ALL is also ignored.
4910 */
4911 for (int i = 0; i < len; i++) {
16d91fce
DS
4912 assert(route_info[i].key >= ZEBRA_ROUTE_SYSTEM &&
4913 route_info[i].key < ZEBRA_ROUTE_MAX);
8dc7a759 4914 assert(route_info[i].meta_q_map < MQ_SIZE);
eaa2716d
SW
4915 }
4916}
4917
718e3744 4918/* Routing information base initialize. */
d62a17ae 4919void rib_init(void)
718e3744 4920{
eaa2716d
SW
4921 check_route_info();
4922
2561d12e 4923 rib_queue_init();
7cdb1a84
MS
4924
4925 /* Init dataplane, and register for results */
4926 pthread_mutex_init(&dplane_mutex, NULL);
ac96497c 4927 dplane_ctx_q_init(&rib_dplane_q);
4c206c8f 4928 zebra_dplane_init(rib_dplane_results);
718e3744 4929}
0915bb0c
AS
4930
4931/*
4932 * vrf_id_get_next
4933 *
4934 * Get the first vrf id that is greater than the given vrf id if any.
4935 *
2951a7a4 4936 * Returns true if a vrf id was found, false otherwise.
0915bb0c 4937 */
d62a17ae 4938static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 4939{
d62a17ae 4940 struct vrf *vrf;
b72ede27 4941
d62a17ae 4942 vrf = vrf_lookup_by_id(vrf_id);
4943 if (vrf) {
4944 vrf = RB_NEXT(vrf_id_head, vrf);
4945 if (vrf) {
4946 *next_id_p = vrf->vrf_id;
4947 return 1;
4948 }
4949 }
0915bb0c 4950
d62a17ae 4951 return 0;
0915bb0c
AS
4952}
4953
4954/*
4955 * rib_tables_iter_next
4956 *
4957 * Returns the next table in the iteration.
4958 */
d62a17ae 4959struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
4960{
4961 struct route_table *table;
4962
4963 /*
4964 * Array that helps us go over all AFI/SAFI combinations via one
4965 * index.
4966 */
2b64873d 4967 static const struct {
d62a17ae 4968 afi_t afi;
4969 safi_t safi;
4970 } afi_safis[] = {
4971 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
4972 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
4973 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
4974 };
4975
4976 table = NULL;
4977
4978 switch (iter->state) {
4979
4980 case RIB_TABLES_ITER_S_INIT:
4981 iter->vrf_id = VRF_DEFAULT;
4982 iter->afi_safi_ix = -1;
4983
4984 /* Fall through */
4985
4986 case RIB_TABLES_ITER_S_ITERATING:
4987 iter->afi_safi_ix++;
4988 while (1) {
4989
4990 while (iter->afi_safi_ix
7e3a1ec7 4991 < (int)array_size(afi_safis)) {
d62a17ae 4992 table = zebra_vrf_table(
4993 afi_safis[iter->afi_safi_ix].afi,
4994 afi_safis[iter->afi_safi_ix].safi,
4995 iter->vrf_id);
4996 if (table)
4997 break;
4998
4999 iter->afi_safi_ix++;
5000 }
5001
5002 /*
5003 * Found another table in this vrf.
5004 */
5005 if (table)
5006 break;
5007
5008 /*
5009 * Done with all tables in the current vrf, go to the
5010 * next
5011 * one.
5012 */
5013 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
5014 break;
5015
5016 iter->afi_safi_ix = 0;
5017 }
0915bb0c 5018
0915bb0c
AS
5019 break;
5020
d62a17ae 5021 case RIB_TABLES_ITER_S_DONE:
5022 return NULL;
0915bb0c
AS
5023 }
5024
d62a17ae 5025 if (table)
5026 iter->state = RIB_TABLES_ITER_S_ITERATING;
5027 else
5028 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c 5029
d62a17ae 5030 return table;
0915bb0c 5031}