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