]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
lib, tests, zebra: Remove unused workqueue error function
[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;
25779064 75static struct dplane_ctx_q 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
DS
1308 rib_unlink(rn, re);
1309 } else
1310 SET_FLAG(re->status,
1311 ROUTE_ENTRY_REMOVED);
1312 }
d62a17ae 1313
7e010c4b
DS
1314 info = srcdest_rnode_table_info(rn);
1315 srcdest_rnode_prefixes(rn, &p, NULL);
a7704e1b
DS
1316 zsend_route_notify_owner(
1317 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
1318 info->afi, info->safi);
9d221fac
DS
1319 continue;
1320 }
1321 } else {
1322 /*
1323 * If the re has not changed and the nhg we have is
1324 * not usable, then we cannot use this route entry
1325 * for consideration, as that the route will just
1326 * not install if it is selected.
1327 */
1328 if (!nexthop_group_active_nexthop_num(&re->nhe->nhg))
1329 continue;
d62a17ae 1330 }
bab85d4f 1331
d62a17ae 1332 /* Infinite distance. */
3332f4f0
DS
1333 if (re->distance == DISTANCE_INFINITY &&
1334 re->type != ZEBRA_ROUTE_KERNEL) {
d62a17ae 1335 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1336 continue;
1337 }
bab85d4f 1338
d62a17ae 1339 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1340 best = rib_choose_best(new_fib, re);
1341 if (new_fib && best != new_fib)
1342 UNSET_FLAG(new_fib->status,
1343 ROUTE_ENTRY_CHANGED);
1344 new_fib = best;
1345 } else {
1346 best = rib_choose_best(new_selected, re);
1347 if (new_selected && best != new_selected)
1348 UNSET_FLAG(new_selected->status,
1349 ROUTE_ENTRY_CHANGED);
1350 new_selected = best;
1351 }
1352 if (best != re)
1353 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1354 } /* RNODE_FOREACH_RE */
1355
1356 /* If no FIB override route, use the selected route also for FIB */
1357 if (new_fib == NULL)
1358 new_fib = new_selected;
1359
1360 /* After the cycle is finished, the following pointers will be set:
1361 * old_selected --- RE entry currently having SELECTED
1362 * new_selected --- RE entry that is newly SELECTED
1363 * old_fib --- RE entry currently in kernel FIB
1364 * new_fib --- RE entry that is newly to be in kernel FIB
1365 *
1366 * new_selected will get SELECTED flag, and is going to be redistributed
1367 * the zclients. new_fib (which can be new_selected) will be installed
1368 * in kernel.
1369 */
1370
1371 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
c2c02b76
DS
1372 struct route_entry *entry;
1373
1374 entry = old_selected
1375 ? old_selected
1376 : new_selected
1377 ? new_selected
1378 : old_fib ? old_fib
1379 : new_fib ? new_fib : NULL;
1380
d62a17ae 1381 zlog_debug(
e658173a
DS
1382 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1383 VRF_LOGNAME(vrf), vrf_id, entry ? entry->table : 0, rn,
c2c02b76
DS
1384 (void *)old_selected, (void *)new_selected,
1385 (void *)old_fib, (void *)new_fib);
d62a17ae 1386 }
446bb95e 1387
d62a17ae 1388 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1389 * fib == selected */
9d303b37
DL
1390 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1391 ROUTE_ENTRY_CHANGED);
d62a17ae 1392
8cb41cd6 1393 /* Update SELECTED entry */
d62a17ae 1394 if (old_selected != new_selected || selected_changed) {
93bdadae 1395
6883bf8d 1396 if (new_selected && new_selected != new_fib)
d62a17ae 1397 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
41ec9222 1398
3cdba47a 1399 if (new_selected)
5af4b346
MS
1400 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1401
d62a17ae 1402 if (old_selected) {
40f321c0
MS
1403 /*
1404 * If we're removing the old entry, we should tell
1405 * redist subscribers about that *if* they aren't
1406 * going to see a redist for the new entry.
1407 */
1408 if (!new_selected || CHECK_FLAG(old_selected->status,
1409 ROUTE_ENTRY_REMOVED))
b3a9fca1 1410 redistribute_delete(rn, old_selected,
40f321c0
MS
1411 new_selected);
1412
d62a17ae 1413 if (old_selected != new_selected)
1414 UNSET_FLAG(old_selected->flags,
1415 ZEBRA_FLAG_SELECTED);
f857321e 1416 }
d62a17ae 1417 }
3e5c6e00 1418
f68ce976
DL
1419 /* Update fib according to selection results */
1420 if (new_fib && old_fib)
1421 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1422 else if (new_fib)
1423 rib_process_add_fib(zvrf, rn, new_fib);
1424 else if (old_fib)
1425 rib_process_del_fib(zvrf, rn, old_fib);
1426
d62a17ae 1427 /* Remove all RE entries queued for removal */
a2addae8 1428 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1429 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1430 if (IS_ZEBRA_DEBUG_RIB) {
1431 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1432 (void *)rn, (void *)re);
1433 }
1434 rib_unlink(rn, re);
1435 }
1436 }
4d38fdb4 1437
d62a17ae 1438 /*
1439 * Check if the dest can be deleted now.
1440 */
1441 rib_gc_dest(rn);
e96f9203
DO
1442}
1443
a1494c25
DS
1444static void zebra_rib_evaluate_mpls(struct route_node *rn)
1445{
1446 rib_dest_t *dest = rib_dest_from_rnode(rn);
1447 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
1448
1449 if (!dest)
1450 return;
1451
1452 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1453 if (IS_ZEBRA_DEBUG_MPLS)
1454 zlog_debug(
bd47f3a3
JU
1455 "%s(%u): Scheduling all LSPs upon RIB completion",
1456 zvrf_name(zvrf), zvrf_id(zvrf));
a1494c25
DS
1457 zebra_mpls_lsp_schedule(zvrf);
1458 mpls_unmark_lsps_for_processing(rn);
1459 }
1460}
1461
e5ac2adf
MS
1462/*
1463 * Utility to match route with dplane context data
1464 */
1465static bool rib_route_match_ctx(const struct route_entry *re,
25779064
MS
1466 const struct zebra_dplane_ctx *ctx,
1467 bool is_update)
e5ac2adf
MS
1468{
1469 bool result = false;
1470
1471 if (is_update) {
1472 /*
1473 * In 'update' case, we test info about the 'previous' or
1474 * 'old' route
1475 */
1476 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1477 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1478 result = true;
1479
9db35a5e
MS
1480 /* We use an extra test for statics, and another for
1481 * kernel routes.
e5ac2adf
MS
1482 */
1483 if (re->type == ZEBRA_ROUTE_STATIC &&
1484 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1485 re->tag != dplane_ctx_get_old_tag(ctx))) {
1486 result = false;
9db35a5e
MS
1487 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1488 re->metric !=
1489 dplane_ctx_get_old_metric(ctx)) {
1490 result = false;
e5ac2adf
MS
1491 }
1492 }
1493
1494 } else {
1495 /*
1496 * Ordinary, single-route case using primary context info
1497 */
1498 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1499 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1500 /* Skip route that's been deleted */
1501 goto done;
1502 }
1503
1504 if ((re->type == dplane_ctx_get_type(ctx)) &&
1505 (re->instance == dplane_ctx_get_instance(ctx))) {
1506 result = true;
1507
9db35a5e
MS
1508 /* We use an extra test for statics, and another for
1509 * kernel routes.
e5ac2adf
MS
1510 */
1511 if (re->type == ZEBRA_ROUTE_STATIC &&
1512 (re->distance != dplane_ctx_get_distance(ctx) ||
1513 re->tag != dplane_ctx_get_tag(ctx))) {
1514 result = false;
9db35a5e
MS
1515 } else if (re->type == ZEBRA_ROUTE_KERNEL &&
1516 re->metric != dplane_ctx_get_metric(ctx)) {
1517 result = false;
1518 } else if (re->type == ZEBRA_ROUTE_CONNECT) {
1519 result = nexthop_group_equal_no_recurse(
1520 &re->nhe->nhg, dplane_ctx_get_ng(ctx));
e5ac2adf
MS
1521 }
1522 }
1523 }
1524
1525done:
871a16cd 1526 return result;
e5ac2adf
MS
1527}
1528
3cdba47a
DS
1529static void zebra_rib_fixup_system(struct route_node *rn)
1530{
1531 struct route_entry *re;
1532
1533 RNODE_FOREACH_RE(rn, re) {
1534 struct nexthop *nhop;
1535
1536 if (!RIB_SYSTEM_ROUTE(re))
1537 continue;
1538
1539 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1540 continue;
1541
1542 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
6d0ee6a0 1543 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
8d4665aa 1544 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
3cdba47a 1545
c415d895 1546 for (ALL_NEXTHOPS(re->nhe->nhg, nhop)) {
3cdba47a
DS
1547 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1548 continue;
1549
1550 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1551 }
1552 }
1553}
1554
9db35a5e
MS
1555/* Route comparison logic, with various special cases. */
1556static bool rib_compare_routes(const struct route_entry *re1,
1557 const struct route_entry *re2)
1558{
9db35a5e
MS
1559 if (re1->type != re2->type)
1560 return false;
1561
1562 if (re1->instance != re2->instance)
1563 return false;
1564
1565 if (re1->type == ZEBRA_ROUTE_KERNEL && re1->metric != re2->metric)
1566 return false;
1567
1568 if (CHECK_FLAG(re1->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
1569 re1->distance != re2->distance)
1570 return false;
1571
87009d7d
MS
1572 /* We support multiple connected routes: this supports multiple
1573 * v6 link-locals, and we also support multiple addresses in the same
1574 * subnet on a single interface.
1575 */
9db35a5e
MS
1576 if (re1->type != ZEBRA_ROUTE_CONNECT)
1577 return true;
1578
87009d7d 1579 return false;
9db35a5e
MS
1580}
1581
9959f1da
MS
1582/*
1583 * Compare nexthop lists from a route and a dplane context; test whether
1584 * the list installed in the FIB matches the route's list.
1585 * Set 'changed_p' to 'true' if there were changes to the route's
1586 * installed nexthops.
1587 *
1588 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1589 * list.
1590 */
1591static bool rib_update_nhg_from_ctx(struct nexthop_group *re_nhg,
1592 const struct nexthop_group *ctx_nhg,
1593 bool *changed_p)
1594{
1595 bool matched_p = true;
1596 struct nexthop *nexthop, *ctx_nexthop;
1597
1598 /* Get the first `installed` one to check against.
1599 * If the dataplane doesn't set these to be what was actually installed,
1600 * it will just be whatever was in re->nhe->nhg?
1601 */
1602 ctx_nexthop = ctx_nhg->nexthop;
1603
1604 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
1605 || !CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1606 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1607
1608 for (ALL_NEXTHOPS_PTR(re_nhg, nexthop)) {
1609
1610 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1611 continue;
1612
1613 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1614 continue;
1615
1616 /* Check for a FIB nexthop corresponding to the RIB nexthop */
7483dcbe 1617 if (!nexthop_same(ctx_nexthop, nexthop)) {
9959f1da
MS
1618 /* If the FIB doesn't know about the nexthop,
1619 * it's not installed
1620 */
1621 if (IS_ZEBRA_DEBUG_RIB_DETAILED ||
1622 IS_ZEBRA_DEBUG_NHG_DETAIL) {
1623 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1624 __func__, nexthop,
1625 (CHECK_FLAG(nexthop->flags,
1626 NEXTHOP_FLAG_FIB) ?
1627 "(FIB)":""));
1628 }
1629 matched_p = false;
1630
1631 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1632 *changed_p = true;
1633
1634 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1635
1636 /* Keep checking nexthops */
1637 continue;
1638 }
1639
1640 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
1641 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1642 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1643 zlog_debug("%s: rib nh %pNHv -> installed",
1644 __func__, nexthop);
1645
1646 *changed_p = true;
1647 }
1648
1649 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1650 } else {
1651 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
1652 if (IS_ZEBRA_DEBUG_NHG_DETAIL)
1653 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1654 __func__, nexthop);
1655
1656 *changed_p = true;
1657 }
1658
1659 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1660 }
1661
1662 ctx_nexthop = nexthop_next_active_resolved(ctx_nexthop);
1663 }
1664
1665 return matched_p;
1666}
1667
e5ac2adf 1668/*
78bf56b0
MS
1669 * Update a route from a dplane context. This consolidates common code
1670 * that can be used in processing of results from FIB updates, and in
1671 * async notification processing.
efe6c026 1672 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
e5ac2adf 1673 */
efe6c026
MS
1674static bool rib_update_re_from_ctx(struct route_entry *re,
1675 struct route_node *rn,
1676 struct zebra_dplane_ctx *ctx)
78bf56b0 1677{
9959f1da 1678 struct nexthop *nexthop;
941e261c
MS
1679 bool matched;
1680 const struct nexthop_group *ctxnhg;
9959f1da 1681 struct nexthop_group *re_nhg;
efe6c026
MS
1682 bool is_selected = false; /* Is 're' currently the selected re? */
1683 bool changed_p = false; /* Change to nexthops? */
1684 rib_dest_t *dest;
bd47f3a3
JU
1685 struct vrf *vrf;
1686
1687 vrf = vrf_lookup_by_id(re->vrf_id);
78bf56b0 1688
efe6c026
MS
1689 dest = rib_dest_from_rnode(rn);
1690 if (dest)
1691 is_selected = (re == dest->selected_fib);
1692
1693 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2919eea8
DS
1694 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1695 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
9db35a5e 1696 (is_selected ? "" : "NOT "), re);
efe6c026
MS
1697
1698 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
941e261c 1699 * If the installed set differs from the set requested by the rib/owner,
efe6c026
MS
1700 * we use the fib-specific nexthop-group to record the actual FIB
1701 * status.
78bf56b0 1702 */
a126f120
MS
1703 matched = false;
1704 ctxnhg = dplane_ctx_get_ng(ctx);
941e261c 1705
9959f1da 1706 /* Check route's fib group and incoming notif group for equivalence.
986a6617
SW
1707 *
1708 * Let's assume the nexthops are ordered here to save time.
941e261c 1709 */
9959f1da
MS
1710 /* TODO -- this isn't testing or comparing the FIB flags; we should
1711 * do a more explicit loop, checking the incoming notification's flags.
1712 */
a126f120
MS
1713 if (re->fib_ng.nexthop && ctxnhg->nexthop &&
1714 nexthop_group_equal(&re->fib_ng, ctxnhg))
986a6617 1715 matched = true;
941e261c
MS
1716
1717 /* If the new FIB set matches the existing FIB set, we're done. */
1718 if (matched) {
1719 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1720 zlog_debug(
2919eea8
DS
1721 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1722 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
9959f1da 1723 goto check_backups;
941e261c 1724
7483dcbe 1725 } else if (CHECK_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG)) {
941e261c
MS
1726 /*
1727 * Free stale fib list and move on to check the rib nhg.
1728 */
1729 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1730 zlog_debug(
2919eea8
DS
1731 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1732 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
941e261c
MS
1733 nexthops_free(re->fib_ng.nexthop);
1734 re->fib_ng.nexthop = NULL;
efe6c026 1735
7483dcbe
MS
1736 UNSET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
1737
efe6c026
MS
1738 /* Note that the installed nexthops have changed */
1739 changed_p = true;
941e261c
MS
1740 } else {
1741 if (IS_ZEBRA_DEBUG_RIB)
2919eea8
DS
1742 zlog_debug(
1743 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1744 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn);
941e261c
MS
1745 }
1746
1747 /*
1748 * Compare with the rib nexthop group. The comparison here is different:
1749 * the RIB group may be a superset of the list installed in the FIB. We
1750 * walk the RIB group, looking for the 'installable' candidate
1751 * nexthops, and then check those against the set
1752 * that is actually installed.
986a6617
SW
1753 *
1754 * Assume nexthops are ordered here as well.
941e261c 1755 */
986a6617 1756
9959f1da 1757 /* If nothing is installed, we can skip some of the checking/comparison
1f6a5aca
MS
1758 * of nexthops.
1759 */
9959f1da 1760 if (ctxnhg->nexthop == NULL) {
1f6a5aca
MS
1761 changed_p = true;
1762 goto no_nexthops;
1763 }
1764
9959f1da 1765 matched = rib_update_nhg_from_ctx(&(re->nhe->nhg), ctxnhg, &changed_p);
78bf56b0 1766
941e261c
MS
1767 /* If all nexthops were processed, we're done */
1768 if (matched) {
1769 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1770 zlog_debug(
2919eea8
DS
1771 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1772 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
1773 (changed_p ? "true" : "false"));
9959f1da 1774 goto check_backups;
941e261c 1775 }
78bf56b0 1776
1f6a5aca
MS
1777no_nexthops:
1778
941e261c
MS
1779 /* FIB nexthop set differs from the RIB set:
1780 * create a fib-specific nexthop-group
1781 */
1782 if (IS_ZEBRA_DEBUG_RIB)
bd47f3a3 1783 zlog_debug(
2919eea8
DS
1784 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1785 VRF_LOGNAME(vrf), re->vrf_id, re->table, rn,
7483dcbe
MS
1786 (changed_p ? "true" : "false"),
1787 ctxnhg->nexthop != NULL ? "" : " (empty)");
941e261c 1788
7483dcbe
MS
1789 /* Set the flag about the dedicated fib list */
1790 SET_FLAG(re->status, ROUTE_ENTRY_USE_FIB_NHG);
188a00e0
MS
1791 if (ctxnhg->nexthop)
1792 copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, NULL);
941e261c 1793
9959f1da
MS
1794check_backups:
1795
1796 /*
1797 * Check the status of the route's backup nexthops, if any.
1798 * The logic for backups is somewhat different: if any backup is
1799 * installed, a new fib nhg will be attached to the route.
1800 */
1801 re_nhg = zebra_nhg_get_backup_nhg(re->nhe);
1802 if (re_nhg == NULL)
1803 goto done; /* No backup nexthops */
1804
1805 /* First check the route's 'fib' list of backups, if it's present
1806 * from some previous event.
1807 */
1808 re_nhg = &re->fib_backup_ng;
1809 ctxnhg = dplane_ctx_get_backup_ng(ctx);
1810
1811 matched = false;
1812 if (re_nhg->nexthop && ctxnhg && nexthop_group_equal(re_nhg, ctxnhg))
1813 matched = true;
1814
1815 /* If the new FIB set matches an existing FIB set, we're done. */
1816 if (matched) {
1817 if (IS_ZEBRA_DEBUG_RIB)
1818 zlog_debug(
2919eea8
DS
1819 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1820 VRF_LOGNAME(vrf), re->vrf_id, rn);
9959f1da
MS
1821 goto done;
1822
1823 } else if (re->fib_backup_ng.nexthop) {
1824 /*
1825 * Free stale fib backup list and move on to check
1826 * the route's backups.
1827 */
1828 if (IS_ZEBRA_DEBUG_RIB)
1829 zlog_debug(
2919eea8
DS
1830 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1831 VRF_LOGNAME(vrf), re->vrf_id, rn);
9959f1da
MS
1832 nexthops_free(re->fib_backup_ng.nexthop);
1833 re->fib_backup_ng.nexthop = NULL;
1834
1835 /* Note that the installed nexthops have changed */
1836 changed_p = true;
1837 } else {
1838 if (IS_ZEBRA_DEBUG_RIB)
2919eea8
DS
1839 zlog_debug(
1840 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1841 VRF_LOGNAME(vrf), re->vrf_id, rn);
9959f1da
MS
1842 }
1843
1844 /*
7483dcbe 1845 * If a FIB backup nexthop set exists, attach a copy
9959f1da
MS
1846 * to the route if any backup is installed
1847 */
1848 if (ctxnhg && ctxnhg->nexthop) {
1849
1850 for (ALL_NEXTHOPS_PTR(ctxnhg, nexthop)) {
1851 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1852 break;
1853 }
1854
1855 /* If no installed backups, we're done */
1856 if (nexthop == NULL)
1857 goto done;
1858
1859 if (IS_ZEBRA_DEBUG_RIB)
2919eea8
DS
1860 zlog_debug(
1861 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1862 VRF_LOGNAME(vrf), re->vrf_id, rn,
1863 (changed_p ? "true" : "false"));
9959f1da
MS
1864
1865 copy_nexthops(&(re->fib_backup_ng.nexthop), ctxnhg->nexthop,
1866 NULL);
1867 }
1868
941e261c 1869done:
9959f1da 1870
efe6c026 1871 return changed_p;
78bf56b0
MS
1872}
1873
e5ac2adf 1874/*
fad4d69c
MS
1875 * Helper to locate a zebra route-node from a dplane context. This is used
1876 * when processing dplane results, e.g. Note well: the route-node is returned
1877 * with a ref held - route_unlock_node() must be called eventually.
e5ac2adf 1878 */
a7704e1b 1879struct route_node *rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
e5ac2adf
MS
1880{
1881 struct route_table *table = NULL;
1882 struct route_node *rn = NULL;
e5ac2adf
MS
1883 const struct prefix *dest_pfx, *src_pfx;
1884
1885 /* Locate rn and re(s) from ctx */
1886
c7c0b007
SW
1887 table = zebra_vrf_lookup_table_with_table_id(
1888 dplane_ctx_get_afi(ctx), dplane_ctx_get_safi(ctx),
1889 dplane_ctx_get_vrf(ctx), dplane_ctx_get_table(ctx));
e5ac2adf
MS
1890 if (table == NULL) {
1891 if (IS_ZEBRA_DEBUG_DPLANE) {
bd47f3a3
JU
1892 zlog_debug(
1893 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1894 dplane_ctx_get_afi(ctx),
1895 dplane_ctx_get_safi(ctx),
1896 vrf_id_to_name(dplane_ctx_get_vrf(ctx)),
1897 dplane_ctx_get_vrf(ctx));
e5ac2adf
MS
1898 }
1899 goto done;
1900 }
1901
fad4d69c
MS
1902 dest_pfx = dplane_ctx_get_dest(ctx);
1903 src_pfx = dplane_ctx_get_src(ctx);
1904
1905 rn = srcdest_rnode_get(table, dest_pfx,
1906 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1907
1908done:
1909 return rn;
1910}
1911
1912
97f5b441 1913
fad4d69c
MS
1914/*
1915 * Route-update results processing after async dataplane update.
1916 */
1917static void rib_process_result(struct zebra_dplane_ctx *ctx)
1918{
1919 struct zebra_vrf *zvrf = NULL;
bd47f3a3 1920 struct vrf *vrf;
fad4d69c
MS
1921 struct route_node *rn = NULL;
1922 struct route_entry *re = NULL, *old_re = NULL, *rib;
1923 bool is_update = false;
fad4d69c
MS
1924 enum dplane_op_e op;
1925 enum zebra_dplane_result status;
fad4d69c 1926 uint32_t seq;
9db35a5e 1927 rib_dest_t *dest;
efe6c026 1928 bool fib_changed = false;
77b38a4a 1929 struct rib_table_info *info;
f99f1ff5 1930 bool rt_delete = false;
97f5b441 1931
fad4d69c 1932 zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
bd47f3a3 1933 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
e5ac2adf 1934
fad4d69c
MS
1935 /* Locate rn and re(s) from ctx */
1936 rn = rib_find_rn_from_ctx(ctx);
e5ac2adf
MS
1937 if (rn == NULL) {
1938 if (IS_ZEBRA_DEBUG_DPLANE) {
bd47f3a3 1939 zlog_debug(
69a2d597
DS
1940 "Failed to process dplane results: no route for %s(%u):%pRN",
1941 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx), rn);
e5ac2adf
MS
1942 }
1943 goto done;
1944 }
1945
9db35a5e 1946 dest = rib_dest_from_rnode(rn);
77b38a4a 1947 info = srcdest_rnode_table_info(rn);
e5ac2adf
MS
1948
1949 op = dplane_ctx_get_op(ctx);
1950 status = dplane_ctx_get_status(ctx);
1951
c831033f 1952 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 1953 zlog_debug(
69a2d597 1954 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
c2c02b76 1955 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
69a2d597
DS
1956 dplane_ctx_get_table(ctx), rn, ctx, dplane_op2str(op),
1957 dplane_res2str(status));
e5ac2adf 1958
e5ac2adf
MS
1959 /*
1960 * Update is a bit of a special case, where we may have both old and new
1961 * routes to post-process.
1962 */
1963 is_update = dplane_ctx_is_update(ctx);
1964
1965 /*
1966 * Take a pass through the routes, look for matches with the context
1967 * info.
1968 */
1969 RNODE_FOREACH_RE(rn, rib) {
1970
1971 if (re == NULL) {
5709131c 1972 if (rib_route_match_ctx(rib, ctx, false))
e5ac2adf 1973 re = rib;
e5ac2adf
MS
1974 }
1975
1976 /* Check for old route match */
1977 if (is_update && (old_re == NULL)) {
5709131c 1978 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
e5ac2adf 1979 old_re = rib;
e5ac2adf
MS
1980 }
1981
1982 /* Have we found the routes we need to work on? */
5709131c 1983 if (re && ((!is_update || old_re)))
e5ac2adf 1984 break;
e5ac2adf
MS
1985 }
1986
699dae23
DS
1987 seq = dplane_ctx_get_seq(ctx);
1988
e5ac2adf
MS
1989 /*
1990 * Check sequence number(s) to detect stale results before continuing
1991 */
60f98b23 1992 if (re) {
699dae23 1993 if (re->dplane_sequence != seq) {
60f98b23 1994 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 1995 zlog_debug(
69a2d597 1996 "%s(%u):%pRN Stale dplane result for re %p",
bd47f3a3 1997 VRF_LOGNAME(vrf),
69a2d597 1998 dplane_ctx_get_vrf(ctx), rn, re);
e4876266
DS
1999 } else {
2000 if (!zrouter.asic_offloaded ||
2001 (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED) ||
8d4665aa
DS
2002 CHECK_FLAG(re->flags,
2003 ZEBRA_FLAG_OFFLOAD_FAILED))) {
2004 UNSET_FLAG(re->status,
2005 ROUTE_ENTRY_ROUTE_REPLACING);
e4876266 2006 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
8d4665aa 2007 }
e4876266 2008 }
e5ac2adf
MS
2009 }
2010
60f98b23
DS
2011 if (old_re) {
2012 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
2013 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 2014 zlog_debug(
69a2d597 2015 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
bd47f3a3 2016 VRF_LOGNAME(vrf),
c2c02b76 2017 dplane_ctx_get_vrf(ctx), old_re->table,
69a2d597 2018 rn, old_re);
60f98b23 2019 } else
9ef0c6ba 2020 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
2021 }
2022
12e7fe3a 2023 switch (op) {
12e7fe3a
DS
2024 case DPLANE_OP_ROUTE_INSTALL:
2025 case DPLANE_OP_ROUTE_UPDATE:
2026 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2027 if (re) {
2028 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2029 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2030 }
b9f0e5ee
DS
2031 /*
2032 * On an update operation from the same route type
2033 * context retrieval currently has no way to know
2034 * which was the old and which was the new.
2035 * So don't unset our flags that we just set.
2036 * We know redistribution is ok because the
2037 * old_re in this case is used for nothing
2038 * more than knowing whom to contact if necessary.
2039 */
2040 if (old_re && old_re != re) {
677c1dd5
DS
2041 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2042 UNSET_FLAG(old_re->status,
2043 ROUTE_ENTRY_INSTALLED);
2044 }
f183e380 2045
78bf56b0 2046 /* Update zebra route based on the results in
941e261c 2047 * the context struct.
78bf56b0 2048 */
941e261c 2049 if (re) {
efe6c026
MS
2050 fib_changed =
2051 rib_update_re_from_ctx(re, rn, ctx);
2052
2053 if (!fib_changed) {
2054 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
bd47f3a3 2055 zlog_debug(
69a2d597 2056 "%s(%u:%u):%pRN no fib change for re",
bd47f3a3
JU
2057 VRF_LOGNAME(vrf),
2058 dplane_ctx_get_vrf(ctx),
c2c02b76
DS
2059 dplane_ctx_get_table(
2060 ctx),
69a2d597 2061 rn);
12e7fe3a
DS
2062 }
2063
9db35a5e
MS
2064 /* Redistribute if this is the selected re */
2065 if (dest && re == dest->selected_fib)
ee78ed68 2066 redistribute_update(rn, re, old_re);
f183e380
MS
2067 }
2068
3cdba47a
DS
2069 /*
2070 * System routes are weird in that they
2071 * allow multiple to be installed that match
2072 * to the same prefix, so after we get the
2073 * result we need to clean them up so that
2074 * we can actually use them.
2075 */
2076 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2077 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2078 zebra_rib_fixup_system(rn);
2079
f4c6e2a8 2080 if (zvrf)
12e7fe3a 2081 zvrf->installs++;
f183e380 2082
12e7fe3a 2083 /* Notify route owner */
e4876266
DS
2084 if (zebra_router_notify_on_ack())
2085 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2086 else {
2087 if (re) {
2088 if (CHECK_FLAG(re->flags,
2089 ZEBRA_FLAG_OFFLOADED))
2090 zsend_route_notify_owner_ctx(
2091 ctx,
2092 ZAPI_ROUTE_INSTALLED);
2093 if (CHECK_FLAG(
2094 re->flags,
2095 ZEBRA_FLAG_OFFLOAD_FAILED))
2096 zsend_route_notify_owner_ctx(
2097 ctx,
2098 ZAPI_ROUTE_FAIL_INSTALL);
2099 }
2100 }
12e7fe3a 2101 } else {
7a230a9d 2102 if (re) {
677c1dd5 2103 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
7a230a9d
DS
2104 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2105 } if (old_re)
677c1dd5
DS
2106 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2107 if (re)
a7704e1b
DS
2108 zsend_route_notify_owner(
2109 rn, re, ZAPI_ROUTE_FAIL_INSTALL,
2110 info->afi, info->safi);
97f5b441 2111
69a2d597 2112 zlog_warn("%s(%u:%u):%pRN: Route install failed",
bd47f3a3 2113 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
69a2d597 2114 dplane_ctx_get_table(ctx), rn);
12e7fe3a
DS
2115 }
2116 break;
2117 case DPLANE_OP_ROUTE_DELETE:
f99f1ff5 2118 rt_delete = true;
677c1dd5
DS
2119 if (re)
2120 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2121 /*
2122 * In the delete case, the zebra core datastructs were
2123 * updated (or removed) at the time the delete was issued,
2124 * so we're just notifying the route owner.
e5ac2adf 2125 */
12e7fe3a 2126 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2127 if (re) {
2128 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2129 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2130 }
12e7fe3a 2131 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
e5ac2adf 2132
12e7fe3a
DS
2133 if (zvrf)
2134 zvrf->removals++;
2135 } else {
677c1dd5
DS
2136 if (re)
2137 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2138 zsend_route_notify_owner_ctx(ctx,
2139 ZAPI_ROUTE_REMOVE_FAIL);
e5ac2adf 2140
69a2d597 2141 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
bd47f3a3 2142 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
69a2d597 2143 dplane_ctx_get_table(ctx), rn);
12e7fe3a 2144 }
3cdba47a
DS
2145
2146 /*
2147 * System routes are weird in that they
2148 * allow multiple to be installed that match
2149 * to the same prefix, so after we get the
2150 * result we need to clean them up so that
2151 * we can actually use them.
2152 */
2153 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2154 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2155 zebra_rib_fixup_system(rn);
12e7fe3a 2156 break;
d37f4d6c
MS
2157 default:
2158 break;
e5ac2adf 2159 }
699dae23 2160
f99f1ff5 2161 zebra_rib_evaluate_rn_nexthops(rn, seq, rt_delete);
a1494c25 2162 zebra_rib_evaluate_mpls(rn);
e5ac2adf
MS
2163done:
2164
5f27bcba
DS
2165 if (rn)
2166 route_unlock_node(rn);
e5ac2adf
MS
2167}
2168
9959f1da
MS
2169/*
2170 * Count installed/FIB nexthops
2171 */
2172static int rib_count_installed_nh(struct route_entry *re)
2173{
2174 int count = 0;
2175 struct nexthop *nexthop;
2176 struct nexthop_group *nhg;
2177
2178 nhg = rib_get_fib_nhg(re);
2179
2180 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2181 /* The meaningful flag depends on where the installed
2182 * nexthops reside.
2183 */
43a9f66c 2184 if (nhg == &(re->fib_ng)) {
9959f1da
MS
2185 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2186 count++;
2187 } else {
2188 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
2189 count++;
2190 }
2191 }
2192
43a9f66c
MS
2193 nhg = rib_get_fib_backup_nhg(re);
2194 if (nhg) {
2195 for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
2196 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2197 count++;
2198 }
9959f1da
MS
2199 }
2200
2201 return count;
2202}
2203
54818e3b
MS
2204/*
2205 * Handle notification from async dataplane: the dataplane has detected
2206 * some change to a route, and notifies zebra so that the control plane
2207 * can reflect that change.
2208 */
2209static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2210{
2211 struct route_node *rn = NULL;
efe6c026 2212 struct route_entry *re = NULL;
bd47f3a3 2213 struct vrf *vrf;
efe6c026 2214 struct nexthop *nexthop;
941e261c 2215 rib_dest_t *dest;
efe6c026 2216 bool fib_changed = false;
941e261c 2217 bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
efe6c026 2218 int start_count, end_count;
898b4a6d 2219
bd47f3a3 2220 vrf = vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
54818e3b 2221
54818e3b
MS
2222 /* Locate rn and re(s) from ctx */
2223 rn = rib_find_rn_from_ctx(ctx);
2224 if (rn == NULL) {
941e261c 2225 if (debug_p) {
bd47f3a3 2226 zlog_debug(
898b4a6d 2227 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
bd47f3a3 2228 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2229 dplane_ctx_get_table(ctx), rn);
54818e3b
MS
2230 }
2231 goto done;
2232 }
2233
941e261c 2234 dest = rib_dest_from_rnode(rn);
54818e3b 2235
efe6c026 2236 if (debug_p)
898b4a6d 2237 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
c2c02b76 2238 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2239 dplane_ctx_get_table(ctx), rn, ctx);
54818e3b
MS
2240
2241 /*
2242 * Take a pass through the routes, look for matches with the context
2243 * info.
2244 */
efe6c026
MS
2245 RNODE_FOREACH_RE(rn, re) {
2246 if (rib_route_match_ctx(re, ctx, false /*!update*/))
54818e3b
MS
2247 break;
2248 }
2249
2250 /* No match? Nothing we can do */
2251 if (re == NULL) {
941e261c 2252 if (debug_p)
bd47f3a3 2253 zlog_debug(
898b4a6d 2254 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
bd47f3a3 2255 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2256 dplane_ctx_get_table(ctx), rn,
bd47f3a3 2257 zebra_route_string(dplane_ctx_get_type(ctx)));
54818e3b
MS
2258
2259 goto done;
2260 }
2261
272e8903 2262 /* Ensure we clear the QUEUED flag */
a0e11736
DS
2263 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
2264 UNSET_FLAG(re->status, ROUTE_ENTRY_ROUTE_REPLACING);
272e8903 2265
4c0b5436
MS
2266 /* Is this a notification that ... matters? We mostly care about
2267 * the route that is currently selected for installation; we may also
2268 * get an un-install notification, and handle that too.
941e261c
MS
2269 */
2270 if (re != dest->selected_fib) {
4c0b5436
MS
2271 /*
2272 * If we need to, clean up after a delete that was part of
2273 * an update operation.
941e261c 2274 */
4c0b5436
MS
2275 end_count = 0;
2276 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), nexthop)) {
2277 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
2278 end_count++;
2279 }
2280
2281 /* If no nexthops or none installed, ensure that this re
2282 * gets its 'installed' flag cleared.
2283 */
2284 if (end_count == 0) {
2285 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2286 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2287 if (debug_p)
bd47f3a3 2288 zlog_debug(
898b4a6d 2289 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
bd47f3a3 2290 VRF_LOGNAME(vrf),
c2c02b76 2291 dplane_ctx_get_vrf(ctx),
898b4a6d 2292 dplane_ctx_get_table(ctx), rn,
bd47f3a3
JU
2293 zebra_route_string(
2294 dplane_ctx_get_type(ctx)));
4c0b5436
MS
2295 } else {
2296 /* At least report on the event. */
2297 if (debug_p)
bd47f3a3 2298 zlog_debug(
898b4a6d 2299 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
bd47f3a3 2300 VRF_LOGNAME(vrf),
c2c02b76 2301 dplane_ctx_get_vrf(ctx),
898b4a6d 2302 dplane_ctx_get_table(ctx), rn,
bd47f3a3
JU
2303 zebra_route_string(
2304 dplane_ctx_get_type(ctx)));
4c0b5436 2305 }
941e261c 2306 goto done;
a0e11736
DS
2307 } else {
2308 uint32_t flags = dplane_ctx_get_flags(ctx);
2309
2310 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOADED)) {
2311 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2312 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2313 }
2314 if (CHECK_FLAG(flags, ZEBRA_FLAG_OFFLOAD_FAILED)) {
2315 UNSET_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED);
2316 SET_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED);
2317 }
2318 if (CHECK_FLAG(flags, ZEBRA_FLAG_TRAPPED))
2319 SET_FLAG(re->flags, ZEBRA_FLAG_TRAPPED);
941e261c
MS
2320 }
2321
efe6c026
MS
2322 /* We'll want to determine whether the installation status of the
2323 * route has changed: we'll check the status before processing,
2324 * and then again if there's been a change.
2325 */
2326 start_count = 0;
1c30d64b 2327
9959f1da
MS
2328 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
2329 start_count = rib_count_installed_nh(re);
efe6c026 2330
54818e3b
MS
2331 /* Update zebra's nexthop FIB flags based on the context struct's
2332 * nexthops.
2333 */
efe6c026 2334 fib_changed = rib_update_re_from_ctx(re, rn, ctx);
54818e3b 2335
efe6c026
MS
2336 if (!fib_changed) {
2337 if (debug_p)
bd47f3a3 2338 zlog_debug(
898b4a6d 2339 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
bd47f3a3 2340 VRF_LOGNAME(vrf), dplane_ctx_get_vrf(ctx),
898b4a6d 2341 dplane_ctx_get_table(ctx), rn);
54818e3b
MS
2342 }
2343
188a00e0
MS
2344 /*
2345 * Perform follow-up work if the actual status of the prefix
efe6c026 2346 * changed.
941e261c 2347 */
9959f1da 2348 end_count = rib_count_installed_nh(re);
941e261c 2349
188a00e0
MS
2350 /* Various fib transitions: changed nexthops; from installed to
2351 * not-installed; or not-installed to installed.
efe6c026 2352 */
06525c4f
DS
2353 if (zrouter.asic_notification_nexthop_control) {
2354 if (start_count > 0 && end_count > 0) {
2355 if (debug_p)
2356 zlog_debug(
2357 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2358 VRF_LOGNAME(vrf),
2359 dplane_ctx_get_vrf(ctx),
2360 dplane_ctx_get_table(ctx), rn);
188a00e0 2361
06525c4f
DS
2362 /* Changed nexthops - update kernel/others */
2363 dplane_route_notif_update(rn, re,
2364 DPLANE_OP_ROUTE_UPDATE, ctx);
188a00e0 2365
06525c4f
DS
2366 } else if (start_count == 0 && end_count > 0) {
2367 if (debug_p)
2368 zlog_debug(
2369 "%s(%u:%u):%pRN installed transition from dplane notification",
2370 VRF_LOGNAME(vrf),
2371 dplane_ctx_get_vrf(ctx),
2372 dplane_ctx_get_table(ctx), rn);
efe6c026 2373
06525c4f
DS
2374 /* We expect this to be the selected route, so we want
2375 * to tell others about this transition.
2376 */
2377 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
efe6c026 2378
06525c4f
DS
2379 /* Changed nexthops - update kernel/others */
2380 dplane_route_notif_update(rn, re,
2381 DPLANE_OP_ROUTE_UPDATE, ctx);
188a00e0 2382
06525c4f
DS
2383 /* Redistribute, lsp, and nht update */
2384 redistribute_update(rn, re, NULL);
efe6c026 2385
06525c4f
DS
2386 } else if (start_count > 0 && end_count == 0) {
2387 if (debug_p)
2388 zlog_debug(
2389 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2390 VRF_LOGNAME(vrf),
2391 dplane_ctx_get_vrf(ctx),
2392 dplane_ctx_get_table(ctx), rn);
efe6c026 2393
06525c4f
DS
2394 /* Transition from _something_ installed to _nothing_
2395 * installed.
2396 */
2397 /* We expect this to be the selected route, so we want
2398 * to tell others about this transistion.
2399 */
2400 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
efe6c026 2401
06525c4f
DS
2402 /* Changed nexthops - update kernel/others */
2403 dplane_route_notif_update(rn, re,
2404 DPLANE_OP_ROUTE_DELETE, ctx);
188a00e0 2405
06525c4f
DS
2406 /* Redistribute, lsp, and nht update */
2407 redistribute_delete(rn, re, NULL);
2408 }
2409 }
2410
2411 if (!zebra_router_notify_on_ack()) {
2412 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOADED))
2413 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
2414 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_OFFLOAD_FAILED))
2415 zsend_route_notify_owner_ctx(ctx,
2416 ZAPI_ROUTE_FAIL_INSTALL);
9287b4c5 2417 }
efe6c026 2418
9287b4c5 2419 /* Make any changes visible for lsp and nexthop-tracking processing */
f99f1ff5
SP
2420 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence(),
2421 false);
efe6c026 2422
9287b4c5 2423 zebra_rib_evaluate_mpls(rn);
941e261c 2424
54818e3b 2425done:
941e261c
MS
2426 if (rn)
2427 route_unlock_node(rn);
54818e3b
MS
2428}
2429
32367e7a
MS
2430/*
2431 * Process a node from the EVPN/VXLAN subqueue.
2432 */
2433static void process_subq_evpn(struct listnode *lnode)
2434{
2435 struct wq_evpn_wrapper *w;
2436
2437 /* In general, the list node points to a wrapper object
2438 * holding the info necessary to make some update.
2439 */
2440 w = listgetdata(lnode);
2441 if (!w)
2442 return;
2443
2444 if (w->type == WQ_EVPN_WRAPPER_TYPE_VRFROUTE) {
2445 if (w->add_p)
7f7e49d1 2446 zebra_vxlan_evpn_vrf_route_add(w->vrf_id, &w->macaddr,
32367e7a
MS
2447 &w->ip, &w->prefix);
2448 else
2449 zebra_vxlan_evpn_vrf_route_del(w->vrf_id, &w->ip,
2450 &w->prefix);
2451 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_ES) {
2452 if (w->add_p)
2453 zebra_evpn_remote_es_add(&w->esi, w->ip.ipaddr_v4,
2454 w->esr_rxed, w->df_alg,
2455 w->df_pref);
2456 else
2457 zebra_evpn_remote_es_del(&w->esi, w->ip.ipaddr_v4);
7f7e49d1
MS
2458 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_MACIP) {
2459 uint16_t ipa_len = 0;
2460
2461 if (w->ip.ipa_type == IPADDR_V4)
2462 ipa_len = IPV4_MAX_BYTELEN;
2463 else if (w->ip.ipa_type == IPADDR_V6)
2464 ipa_len = IPV6_MAX_BYTELEN;
2465
2466 if (w->add_p)
2467 zebra_evpn_rem_macip_add(w->vni, &w->macaddr, ipa_len,
2468 &w->ip, w->flags, w->seq,
2469 w->vtep_ip, &w->esi);
2470 else
2471 zebra_evpn_rem_macip_del(w->vni, &w->macaddr, ipa_len,
2472 &w->ip, w->vtep_ip);
7e5b0b2b
MS
2473 } else if (w->type == WQ_EVPN_WRAPPER_TYPE_REM_VTEP) {
2474 if (w->add_p)
2475 zebra_vxlan_remote_vtep_add(w->vrf_id, w->vni,
2476 w->vtep_ip, w->flags);
2477 else
2478 zebra_vxlan_remote_vtep_del(w->vrf_id, w->vni,
2479 w->vtep_ip);
32367e7a
MS
2480 }
2481
7e5b0b2b 2482
32367e7a
MS
2483 XFREE(MTYPE_WQ_WRAPPER, w);
2484}
2485
04bec7b2
MS
2486/*
2487 * Process the nexthop-group workqueue subqueue
2488 */
e22e8001 2489static void process_subq_nhg(struct listnode *lnode)
e96f9203 2490{
04bec7b2
MS
2491 struct nhg_ctx *ctx;
2492 struct nhg_hash_entry *nhe, *newnhe;
2493 struct wq_nhg_wrapper *w;
4bbbd1f6 2494 uint8_t qindex = META_QUEUE_NHG;
5110a0c6 2495
04bec7b2 2496 w = listgetdata(lnode);
e22e8001 2497
04bec7b2 2498 if (!w)
e22e8001
SW
2499 return;
2500
04bec7b2
MS
2501 /* Two types of object - an update from the local kernel, or
2502 * an nhg update from a daemon.
2503 */
2504 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX) {
2505 ctx = w->u.ctx;
2506
2507 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2508 zlog_debug(
7a96ccc7
DS
2509 "NHG Context id=%u dequeued from sub-queue %s",
2510 ctx->id, subqueue2str(qindex));
04bec7b2
MS
2511
2512
2513 /* Process nexthop group updates coming 'up' from the OS */
2514 nhg_ctx_process(ctx);
e22e8001 2515
04bec7b2
MS
2516 } else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG) {
2517 nhe = w->u.nhe;
2518
2519 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
7a96ccc7
DS
2520 zlog_debug("NHG %u dequeued from sub-queue %s", nhe->id,
2521 subqueue2str(qindex));
04bec7b2
MS
2522
2523 /* Process incoming nhg update, probably from a proto daemon */
2524 newnhe = zebra_nhg_proto_add(nhe->id, nhe->type,
2525 nhe->zapi_instance,
32367e7a 2526 nhe->zapi_session, &nhe->nhg, 0);
04bec7b2
MS
2527
2528 /* Report error to daemon via ZAPI */
2529 if (newnhe == NULL)
2530 zsend_nhg_notify(nhe->type, nhe->zapi_instance,
2531 nhe->zapi_session, nhe->id,
2532 ZAPI_NHG_FAIL_INSTALL);
2533
2534 /* Free temp nhe - we own that memory. */
2535 zebra_nhg_free(nhe);
2536 }
e22e8001 2537
32367e7a 2538 XFREE(MTYPE_WQ_WRAPPER, w);
e22e8001
SW
2539}
2540
53216dff
DS
2541static void process_subq_early_label(struct listnode *lnode)
2542{
2543 struct wq_label_wrapper *w = listgetdata(lnode);
2544 struct zebra_vrf *zvrf;
2545
2546 if (!w)
2547 return;
2548
2549 zvrf = vrf_info_lookup(w->vrf_id);
2550 if (!zvrf) {
2551 XFREE(MTYPE_WQ_WRAPPER, w);
2552 return;
2553 }
2554
2555 switch (w->type) {
2556 case WQ_LABEL_FTN_UNINSTALL:
2557 zebra_mpls_ftn_uninstall(zvrf, w->ltype, &w->p, w->route_type,
2558 w->route_instance);
2559 break;
2560 case WQ_LABEL_LABELS_PROCESS:
2561 zebra_mpls_zapi_labels_process(w->add_p, zvrf, &w->zl);
2562 break;
2563 }
2564
2565 XFREE(MTYPE_WQ_WRAPPER, w);
2566}
2567
e22e8001
SW
2568static void process_subq_route(struct listnode *lnode, uint8_t qindex)
2569{
2570 struct route_node *rnode = NULL;
2571 rib_dest_t *dest = NULL;
2572 struct zebra_vrf *zvrf = NULL;
5110a0c6 2573
d62a17ae 2574 rnode = listgetdata(lnode);
2575 dest = rib_dest_from_rnode(rnode);
c2c02b76
DS
2576 assert(dest);
2577
2578 zvrf = rib_dest_vrf(dest);
41ec9222 2579
d62a17ae 2580 rib_process(rnode);
5110a0c6 2581
d62a17ae 2582 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
7e26b9d4 2583 struct route_entry *re = NULL;
efe6c026 2584
7e26b9d4
DS
2585 /*
2586 * rib_process may have freed the dest
2587 * as part of the garbage collection. Let's
2588 * prevent stupidity from happening.
2589 */
2590 dest = rib_dest_from_rnode(rnode);
2591 if (dest)
2592 re = re_list_first(&dest->routes);
2593
7a96ccc7 2594 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
04bec7b2 2595 zvrf_name(zvrf), zvrf_id(zvrf), re ? re->table : 0,
7a96ccc7 2596 rnode, rnode, subqueue2str(qindex));
d62a17ae 2597 }
41ec9222 2598
d62a17ae 2599 if (rnode->info)
2600 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2601 RIB_ROUTE_QUEUED(qindex));
9fd92e3c 2602
d62a17ae 2603 route_unlock_node(rnode);
e22e8001
SW
2604}
2605
d7ac4c4d 2606static void rib_re_nhg_free(struct route_entry *re)
e22e8001 2607{
d7ac4c4d
DS
2608 if (re->nhe && re->nhe_id) {
2609 assert(re->nhe->id == re->nhe_id);
2610 route_entry_update_nhe(re, NULL);
2611 } else if (re->nhe && re->nhe->nhg.nexthop)
2612 nexthops_free(re->nhe->nhg.nexthop);
e22e8001 2613
d7ac4c4d
DS
2614 nexthops_free(re->fib_ng.nexthop);
2615}
e22e8001 2616
d7ac4c4d
DS
2617struct zebra_early_route {
2618 afi_t afi;
2619 safi_t safi;
2620 struct prefix p;
2621 struct prefix_ipv6 src_p;
2622 bool src_p_provided;
2623 struct route_entry *re;
2624 struct nhg_hash_entry *re_nhe;
2625 bool startup;
2626 bool deletion;
2627 bool fromkernel;
2628};
e22e8001 2629
d7ac4c4d
DS
2630static void early_route_memory_free(struct zebra_early_route *ere)
2631{
2632 if (ere->re_nhe)
2633 zebra_nhg_free(ere->re_nhe);
e22e8001 2634
d7ac4c4d
DS
2635 XFREE(MTYPE_RE, ere->re);
2636 XFREE(MTYPE_WQ_WRAPPER, ere);
e96f9203
DO
2637}
2638
d7ac4c4d 2639static void process_subq_early_route_add(struct zebra_early_route *ere)
e96f9203 2640{
d7ac4c4d
DS
2641 struct route_entry *re = ere->re;
2642 struct route_table *table;
2643 struct nhg_hash_entry *nhe = NULL;
2644 struct route_node *rn;
2645 struct route_entry *same = NULL, *first_same = NULL;
2646 int same_count = 0;
2647 rib_dest_t *dest;
91f16812 2648
d7ac4c4d
DS
2649 /* Lookup table. */
2650 table = zebra_vrf_get_table_with_table_id(ere->afi, ere->safi,
2651 re->vrf_id, re->table);
2652 if (!table) {
2653 early_route_memory_free(ere);
2654 return;
2655 }
91f16812 2656
d7ac4c4d
DS
2657 if (re->nhe_id > 0) {
2658 nhe = zebra_nhg_lookup_id(re->nhe_id);
91f16812 2659
d7ac4c4d
DS
2660 if (!nhe) {
2661 /*
2662 * We've received from the kernel a nexthop id
2663 * that we don't have saved yet. More than likely
2664 * it has not been processed and is on the
2665 * queue to be processed. Let's stop what we
2666 * are doing and cause the meta q to be processed
2667 * storing this for later.
2668 *
2669 * This is being done this way because zebra
2670 * runs with the assumption t
2671 */
2672 flog_err(
2673 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2674 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2675 re->nhe_id, &ere->p);
5110a0c6 2676
d7ac4c4d
DS
2677 early_route_memory_free(ere);
2678 return;
d62a17ae 2679 }
d7ac4c4d
DS
2680 } else {
2681 /* Lookup nhe from route information */
2682 nhe = zebra_nhg_rib_find_nhe(ere->re_nhe, ere->afi);
2683 if (!nhe) {
2684 char buf2[PREFIX_STRLEN] = "";
e96f9203 2685
d7ac4c4d
DS
2686 flog_err(
2687 EC_ZEBRA_TABLE_LOOKUP_FAILED,
2688 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2689 &ere->p, ere->src_p_provided ? " from " : "",
2690 ere->src_p_provided
2691 ? prefix2str(&ere->src_p, buf2,
2692 sizeof(buf2))
2693 : "");
f52ed677 2694
d7ac4c4d
DS
2695 early_route_memory_free(ere);
2696 return;
2697 }
2698 }
5110a0c6 2699
d7ac4c4d
DS
2700 /*
2701 * Attach the re to the nhe's nexthop group.
2702 *
2703 * TODO: This will need to change when we start getting IDs from upper
2704 * level protocols, as the refcnt might be wrong, since it checks
2705 * if old_id != new_id.
2706 */
2707 route_entry_update_nhe(re, nhe);
e22e8001 2708
d7ac4c4d
DS
2709 /* Make it sure prefixlen is applied to the prefix. */
2710 apply_mask(&ere->p);
2711 if (ere->src_p_provided)
2712 apply_mask_ipv6(&ere->src_p);
d62a17ae 2713
d7ac4c4d
DS
2714 /* Set default distance by route type. */
2715 if (re->distance == 0)
2716 re->distance = route_distance(re->type);
2717
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
d7ac4c4d
DS
2764 /* If this route is kernel/connected route, notify the dataplane. */
2765 if (RIB_SYSTEM_ROUTE(re)) {
2766 /* Notify dataplane */
2767 dplane_sys_route_add(rn, re);
2768 }
53216dff 2769
d7ac4c4d
DS
2770 /* Link new re to node.*/
2771 if (IS_ZEBRA_DEBUG_RIB) {
2772 rnode_debug(
2773 rn, re->vrf_id,
2774 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2775 rn, re, zebra_route_string(re->type), same, same_count);
fc328ac9 2776
d7ac4c4d
DS
2777 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2778 route_entry_dump(
2779 &ere->p,
2780 ere->src_p_provided ? &ere->src_p : NULL, re);
2781 }
e22e8001 2782
d7ac4c4d
DS
2783 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
2784 rib_addnode(rn, re, 1);
4d38fdb4 2785
d7ac4c4d
DS
2786 /* Free implicit route.*/
2787 if (same)
2788 rib_delnode(rn, same);
04bec7b2 2789
d7ac4c4d
DS
2790 /* See if we can remove some RE entries that are queued for
2791 * removal, but won't be considered in rib processing.
2792 */
2793 dest = rib_dest_from_rnode(rn);
2794 RNODE_FOREACH_RE_SAFE (rn, re, same) {
2795 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2796 /* If the route was used earlier, must retain it. */
2797 if (dest && re == dest->selected_fib)
2798 continue;
04bec7b2 2799
d7ac4c4d
DS
2800 if (IS_ZEBRA_DEBUG_RIB)
2801 rnode_debug(rn, re->vrf_id,
2802 "rn %p, removing unneeded re %p",
2803 rn, re);
e22e8001 2804
d7ac4c4d
DS
2805 rib_unlink(rn, re);
2806 }
2807 }
e22e8001 2808
d7ac4c4d
DS
2809 route_unlock_node(rn);
2810 if (ere->re_nhe)
2811 zebra_nhg_free(ere->re_nhe);
2812 XFREE(MTYPE_WQ_WRAPPER, ere);
e22e8001
SW
2813}
2814
d7ac4c4d 2815static void process_subq_early_route_delete(struct zebra_early_route *ere)
04bec7b2 2816{
d7ac4c4d
DS
2817 struct route_table *table;
2818 struct route_node *rn;
2819 struct route_entry *re;
2820 struct route_entry *fib = NULL;
2821 struct route_entry *same = NULL;
2822 struct nexthop *rtnh;
2823 char buf2[INET6_ADDRSTRLEN];
2824 rib_dest_t *dest;
04bec7b2 2825
d7ac4c4d
DS
2826 if (ere->src_p_provided)
2827 assert(!ere->src_p.prefixlen || ere->afi == AFI_IP6);
2828
2829 /* Lookup table. */
2830 table = zebra_vrf_lookup_table_with_table_id(
2831 ere->afi, ere->safi, ere->re->vrf_id, ere->re->table);
2832 if (!table) {
2833 early_route_memory_free(ere);
2834 return;
2835 }
2836
2837 /* Apply mask. */
2838 apply_mask(&ere->p);
2839 if (ere->src_p_provided)
2840 apply_mask_ipv6(&ere->src_p);
2841
2842 /* Lookup route node. */
2843 rn = srcdest_rnode_lookup(table, &ere->p,
2844 ere->src_p_provided ? &ere->src_p : NULL);
2845 if (!rn) {
2846 if (IS_ZEBRA_DEBUG_RIB) {
2847 char src_buf[PREFIX_STRLEN];
2848 struct vrf *vrf = vrf_lookup_by_id(ere->re->vrf_id);
2849
2850 if (ere->src_p_provided && ere->src_p.prefixlen)
2851 prefix2str(&ere->src_p, src_buf,
2852 sizeof(src_buf));
2853 else
2854 src_buf[0] = '\0';
2855
2856 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2857 vrf->name, ere->re->table, rn,
2858 (src_buf[0] != '\0') ? " from " : "",
2859 src_buf);
2860 }
2861 early_route_memory_free(ere);
2862 return;
2863 }
2864
2865 dest = rib_dest_from_rnode(rn);
2866 fib = dest->selected_fib;
2867
2868 struct nexthop *nh = NULL;
2869
2870 if (ere->re->nhe)
2871 nh = ere->re->nhe->nhg.nexthop;
2872
2873 /* Lookup same type route. */
2874 RNODE_FOREACH_RE (rn, re) {
2875 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2876 continue;
2877
2878 if (re->type != ere->re->type)
2879 continue;
2880 if (re->instance != ere->re->instance)
2881 continue;
2882 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2883 ere->re->distance != re->distance)
2884 continue;
2885
2886 if (re->type == ZEBRA_ROUTE_KERNEL &&
2887 re->metric != ere->re->metric)
2888 continue;
2889 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = nh) &&
2890 rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2891 if (rtnh->ifindex != nh->ifindex)
2892 continue;
2893 same = re;
2894 break;
2895 }
2896
2897 /* Make sure that the route found has the same gateway. */
2898 if (ere->re->nhe_id && re->nhe_id == ere->re->nhe_id) {
2899 same = re;
2900 break;
2901 }
2902
2903 if (nh == NULL) {
2904 same = re;
2905 break;
2906 }
2907 for (ALL_NEXTHOPS(re->nhe->nhg, rtnh)) {
2908 /*
2909 * No guarantee all kernel send nh with labels
2910 * on delete.
2911 */
2912 if (nexthop_same_no_labels(rtnh, nh)) {
2913 same = re;
2914 break;
2915 }
2916 }
2917
2918 if (same)
2919 break;
2920 }
2921 /*
2922 * If same type of route can't be found and this message is from
2923 * kernel.
2924 */
2925 if (!same) {
2926 /*
2927 * In the past(HA!) we could get here because
2928 * we were receiving a route delete from the
2929 * kernel and we're not marking the proto
2930 * as coming from it's appropriate originator.
2931 * Now that we are properly noticing the fact
2932 * that the kernel has deleted our route we
2933 * are not going to get called in this path
2934 * I am going to leave this here because
2935 * this might still work this way on non-linux
2936 * platforms as well as some weird state I have
2937 * not properly thought of yet.
2938 * If we can show that this code path is
2939 * dead then we can remove it.
2940 */
2941 if (fib && CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE)) {
2942 if (IS_ZEBRA_DEBUG_RIB) {
2943 rnode_debug(
2944 rn, ere->re->vrf_id,
2945 "rn %p, re %p (%s) was deleted from kernel, adding",
2946 rn, fib, zebra_route_string(fib->type));
2947 }
2948 if (zrouter.allow_delete ||
2949 CHECK_FLAG(dest->flags, RIB_ROUTE_ANY_QUEUED)) {
2950 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
2951 /* Unset flags. */
2952 for (rtnh = fib->nhe->nhg.nexthop; rtnh;
2953 rtnh = rtnh->next)
2954 UNSET_FLAG(rtnh->flags,
2955 NEXTHOP_FLAG_FIB);
2956
2957 /*
2958 * This is a non FRR route
2959 * as such we should mark
2960 * it as deleted
2961 */
2962 dest->selected_fib = NULL;
2963 } else {
2964 /*
2965 * This means someone else, other than Zebra,
2966 * has deleted a Zebra router from the kernel.
2967 * We will add it back
2968 */
2969 rib_install_kernel(rn, fib, NULL);
2970 }
2971 } else {
2972 if (IS_ZEBRA_DEBUG_RIB) {
2973 if (nh)
2974 rnode_debug(
2975 rn, ere->re->vrf_id,
2976 "via %s ifindex %d type %d doesn't exist in rib",
2977 inet_ntop(afi2family(ere->afi),
2978 &nh->gate, buf2,
2979 sizeof(buf2)),
2980 nh->ifindex, ere->re->type);
2981 else
2982 rnode_debug(
2983 rn, ere->re->vrf_id,
2984 "type %d doesn't exist in rib",
2985 ere->re->type);
2986 }
2987 route_unlock_node(rn);
2988 early_route_memory_free(ere);
2989 return;
2990 }
2991 }
2992
2993 if (same) {
2994 struct nexthop *tmp_nh;
2995
2996 if (ere->fromkernel &&
2997 CHECK_FLAG(ere->re->flags, ZEBRA_FLAG_SELFROUTE) &&
2998 !zrouter.allow_delete) {
2999 rib_install_kernel(rn, same, NULL);
3000 route_unlock_node(rn);
3001
3002 early_route_memory_free(ere);
3003 return;
3004 }
3005
3006 /* Special handling for IPv4 or IPv6 routes sourced from
3007 * EVPN - the nexthop (and associated MAC) need to be
3008 * uninstalled if no more refs.
3009 */
3010 for (ALL_NEXTHOPS(re->nhe->nhg, tmp_nh)) {
3011 struct ipaddr vtep_ip;
3012
3013 if (CHECK_FLAG(tmp_nh->flags, NEXTHOP_FLAG_EVPN)) {
3014 memset(&vtep_ip, 0, sizeof(struct ipaddr));
3015 if (ere->afi == AFI_IP) {
3016 vtep_ip.ipa_type = IPADDR_V4;
3017 memcpy(&(vtep_ip.ipaddr_v4),
3018 &(tmp_nh->gate.ipv4),
3019 sizeof(struct in_addr));
3020 } else {
3021 vtep_ip.ipa_type = IPADDR_V6;
3022 memcpy(&(vtep_ip.ipaddr_v6),
3023 &(tmp_nh->gate.ipv6),
3024 sizeof(struct in6_addr));
3025 }
3026 zebra_rib_queue_evpn_route_del(
3027 re->vrf_id, &vtep_ip, &ere->p);
3028 }
3029 }
3030
3031 /* Notify dplane if system route changes */
3032 if (RIB_SYSTEM_ROUTE(re))
3033 dplane_sys_route_del(rn, same);
3034
3035 rib_delnode(rn, same);
3036 }
3037
3038 route_unlock_node(rn);
3039
3040 early_route_memory_free(ere);
3041}
3042
3043/*
3044 * When FRR receives a route we need to match the route up to
3045 * nexthop groups. That we also may have just received
3046 * place the data on this queue so that this work of finding
3047 * the nexthop group entries for the route entry is always
3048 * done after the nexthop group has had a chance to be processed
3049 */
3050static void process_subq_early_route(struct listnode *lnode)
3051{
3052 struct zebra_early_route *ere = listgetdata(lnode);
3053
3054 if (ere->deletion)
3055 process_subq_early_route_delete(ere);
3056 else
3057 process_subq_early_route_add(ere);
3058}
3059
3060/*
3061 * Examine the specified subqueue; process one entry and return 1 if
3062 * there is a node, return 0 otherwise.
3063 */
3064static unsigned int process_subq(struct list *subq,
3065 enum meta_queue_indexes qindex)
3066{
3067 struct listnode *lnode = listhead(subq);
3068
3069 if (!lnode)
3070 return 0;
3071
3072 switch (qindex) {
3073 case META_QUEUE_EVPN:
3074 process_subq_evpn(lnode);
3075 break;
3076 case META_QUEUE_NHG:
3077 process_subq_nhg(lnode);
3078 break;
3079 case META_QUEUE_EARLY_ROUTE:
3080 process_subq_early_route(lnode);
3081 break;
3082 case META_QUEUE_EARLY_LABEL:
3083 process_subq_early_label(lnode);
3084 break;
3085 case META_QUEUE_CONNECTED:
3086 case META_QUEUE_KERNEL:
3087 case META_QUEUE_STATIC:
3088 case META_QUEUE_NOTBGP:
3089 case META_QUEUE_BGP:
3090 case META_QUEUE_OTHER:
3091 process_subq_route(lnode, qindex);
3092 break;
3093 }
3094
3095 list_delete_node(subq, lnode);
3096
3097 return 1;
3098}
3099
3100/* Dispatch the meta queue by picking and processing the next node from
3101 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3102 * data is pointed to the meta queue structure.
3103 */
3104static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
3105{
3106 struct meta_queue *mq = data;
3107 unsigned i;
3108 uint32_t queue_len, queue_limit;
3109
3110 /* Ensure there's room for more dataplane updates */
3111 queue_limit = dplane_get_in_queue_limit();
3112 queue_len = dplane_get_in_queue_len();
3113 if (queue_len > queue_limit) {
3114 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3115 zlog_debug(
3116 "rib queue: dplane queue len %u, limit %u, retrying",
3117 queue_len, queue_limit);
3118
3119 /* Ensure that the meta-queue is actually enqueued */
3120 if (work_queue_empty(zrouter.ribq))
3121 work_queue_add(zrouter.ribq, zrouter.mq);
3122
3123 return WQ_QUEUE_BLOCKED;
3124 }
3125
3126 for (i = 0; i < MQ_SIZE; i++)
3127 if (process_subq(mq->subq[i], i)) {
3128 mq->size--;
3129 break;
3130 }
3131 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
3132}
3133
3134
3135/*
3136 * Look into the RN and queue it into the highest priority queue
3137 * at this point in time for processing.
3138 *
3139 * We will enqueue a route node only once per invocation.
3140 *
3141 * There are two possibilities here that should be kept in mind.
3142 * If the original invocation has not been pulled off for processing
3143 * yet, A subsuquent invocation can have a route entry with a better
3144 * meta queue index value and we can have a situation where
3145 * we might have the same node enqueued 2 times. Not necessarily
3146 * an optimal situation but it should be ok.
3147 *
3148 * The other possibility is that the original invocation has not
3149 * been pulled off for processing yet, A subsusquent invocation
3150 * doesn't have a route_entry with a better meta-queue and the
3151 * original metaqueue index value will win and we'll end up with
3152 * the route node enqueued once.
3153 */
3154static int rib_meta_queue_add(struct meta_queue *mq, void *data)
3155{
3156 struct route_node *rn = NULL;
3157 struct route_entry *re = NULL, *curr_re = NULL;
3158 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
3159
3160 rn = (struct route_node *)data;
3161
3162 RNODE_FOREACH_RE (rn, curr_re) {
3163 curr_qindex = route_info[curr_re->type].meta_q_map;
3164
3165 if (curr_qindex <= qindex) {
3166 re = curr_re;
3167 qindex = curr_qindex;
3168 }
3169 }
3170
3171 if (!re)
3172 return -1;
3173
3174 /* Invariant: at this point we always have rn->info set. */
3175 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3176 RIB_ROUTE_QUEUED(qindex))) {
3177 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3178 rnode_debug(rn, re->vrf_id,
3179 "rn %p is already queued in sub-queue %s",
3180 (void *)rn, subqueue2str(qindex));
3181 return -1;
3182 }
3183
3184 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
3185 listnode_add(mq->subq[qindex], rn);
3186 route_lock_node(rn);
3187 mq->size++;
3188
3189 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3190 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %s",
3191 (void *)rn, subqueue2str(qindex));
3192
3193 return 0;
3194}
3195
3196static int early_label_meta_queue_add(struct meta_queue *mq, void *data)
3197{
3198 listnode_add(mq->subq[META_QUEUE_EARLY_LABEL], data);
3199 mq->size++;
3200 return 0;
3201}
3202
3203static int rib_meta_queue_nhg_ctx_add(struct meta_queue *mq, void *data)
3204{
3205 struct nhg_ctx *ctx = NULL;
3206 uint8_t qindex = META_QUEUE_NHG;
3207 struct wq_nhg_wrapper *w;
3208
3209 ctx = (struct nhg_ctx *)data;
3210
3211 if (!ctx)
3212 return -1;
3213
3214 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
3215
3216 w->type = WQ_NHG_WRAPPER_TYPE_CTX;
3217 w->u.ctx = ctx;
3218
3219 listnode_add(mq->subq[qindex], w);
3220 mq->size++;
3221
3222 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3223 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3224 ctx->id, subqueue2str(qindex));
3225
3226 return 0;
3227}
3228
3229static int rib_meta_queue_nhg_add(struct meta_queue *mq, void *data)
3230{
3231 struct nhg_hash_entry *nhe = NULL;
3232 uint8_t qindex = META_QUEUE_NHG;
3233 struct wq_nhg_wrapper *w;
3234
3235 nhe = (struct nhg_hash_entry *)data;
04bec7b2
MS
3236
3237 if (!nhe)
3238 return -1;
3239
32367e7a 3240 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_nhg_wrapper));
04bec7b2
MS
3241
3242 w->type = WQ_NHG_WRAPPER_TYPE_NHG;
3243 w->u.nhe = nhe;
3244
3245 listnode_add(mq->subq[qindex], w);
3246 mq->size++;
3247
3248 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
7a96ccc7
DS
3249 zlog_debug("NHG id=%u queued into sub-queue %s", nhe->id,
3250 subqueue2str(qindex));
04bec7b2
MS
3251
3252 return 0;
3253}
3254
32367e7a
MS
3255static int rib_meta_queue_evpn_add(struct meta_queue *mq, void *data)
3256{
3257 listnode_add(mq->subq[META_QUEUE_EVPN], data);
3258 mq->size++;
3259
3260 return 0;
3261}
3262
e22e8001
SW
3263static int mq_add_handler(void *data,
3264 int (*mq_add_func)(struct meta_queue *mq, void *data))
3265{
489a9614 3266 if (zrouter.ribq == NULL) {
e914ccbe 3267 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 3268 "%s: work_queue does not exist!", __func__);
e22e8001 3269 return -1;
d62a17ae 3270 }
3271
3272 /*
3273 * The RIB queue should normally be either empty or holding the only
3274 * work_queue_item element. In the latter case this element would
3275 * hold a pointer to the meta queue structure, which must be used to
3276 * actually queue the route nodes to process. So create the MQ
3277 * holder, if necessary, then push the work into it in any case.
3278 * This semantics was introduced after 0.99.9 release.
3279 */
489a9614 3280 if (work_queue_empty(zrouter.ribq))
ea45a4e7 3281 work_queue_add(zrouter.ribq, zrouter.mq);
e96f9203 3282
e22e8001
SW
3283 return mq_add_func(zrouter.mq, data);
3284}
fc328ac9 3285
53216dff
DS
3286void mpls_ftn_uninstall(struct zebra_vrf *zvrf, enum lsp_types_t type,
3287 struct prefix *prefix, uint8_t route_type,
3288 uint8_t route_instance)
3289{
3290 struct wq_label_wrapper *w;
3291
3292 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3293
3294 w->type = WQ_LABEL_FTN_UNINSTALL;
3295 w->vrf_id = zvrf->vrf->vrf_id;
3296 w->p = *prefix;
3297 w->ltype = type;
3298 w->route_type = route_type;
3299 w->route_instance = route_instance;
3300
3301 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3302 zlog_debug("Early Label Handling for %pFX", prefix);
3303
3304 mq_add_handler(w, early_label_meta_queue_add);
3305}
3306
3307void mpls_zapi_labels_process(bool add_p, struct zebra_vrf *zvrf,
3308 const struct zapi_labels *zl)
3309{
3310 struct wq_label_wrapper *w;
3311
3312 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_label_wrapper));
3313 w->type = WQ_LABEL_LABELS_PROCESS;
3314 w->vrf_id = zvrf->vrf->vrf_id;
3315 w->add_p = add_p;
3316 w->zl = *zl;
3317
3318 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3319 zlog_debug("Early Label Handling: Labels Process");
3320
3321 mq_add_handler(w, early_label_meta_queue_add);
3322}
3323
e22e8001
SW
3324/* Add route_node to work queue and schedule processing */
3325int rib_queue_add(struct route_node *rn)
3326{
3327 assert(rn);
3328
3329 /* Pointless to queue a route_node with no RIB entries to add or remove
3330 */
3331 if (!rnode_to_ribs(rn)) {
c10e14e9
DS
3332 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3333 __func__, (void *)rn, route_node_get_lock_count(rn));
e22e8001
SW
3334 zlog_backtrace(LOG_DEBUG);
3335 return -1;
3336 }
3337
04bec7b2 3338 return mq_add_handler(rn, rib_meta_queue_add);
e22e8001
SW
3339}
3340
04bec7b2
MS
3341/*
3342 * Enqueue incoming nhg info from OS for processing
3343 */
3344int rib_queue_nhg_ctx_add(struct nhg_ctx *ctx)
e22e8001
SW
3345{
3346 assert(ctx);
3347
04bec7b2
MS
3348 return mq_add_handler(ctx, rib_meta_queue_nhg_ctx_add);
3349}
3350
3351/*
3352 * Enqueue incoming nhg from proto daemon for processing
3353 */
3354int rib_queue_nhe_add(struct nhg_hash_entry *nhe)
3355{
3356 if (nhe == NULL)
3357 return -1;
3358
3359 return mq_add_handler(nhe, rib_meta_queue_nhg_add);
3360}
3361
32367e7a
MS
3362/*
3363 * Enqueue evpn route for processing
3364 */
3365int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id, const struct ethaddr *rmac,
3366 const struct ipaddr *vtep_ip,
3367 const struct prefix *host_prefix)
3368{
3369 struct wq_evpn_wrapper *w;
3370
3371 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3372
3373 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3374 w->add_p = true;
3375 w->vrf_id = vrf_id;
7f7e49d1 3376 w->macaddr = *rmac;
32367e7a
MS
3377 w->ip = *vtep_ip;
3378 w->prefix = *host_prefix;
3379
3380 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3381 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3382 vrf_id, vtep_ip, host_prefix);
3383
3384 return mq_add_handler(w, rib_meta_queue_evpn_add);
3385}
3386
3387int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id,
3388 const struct ipaddr *vtep_ip,
3389 const struct prefix *host_prefix)
3390{
3391 struct wq_evpn_wrapper *w;
3392
3393 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3394
3395 w->type = WQ_EVPN_WRAPPER_TYPE_VRFROUTE;
3396 w->add_p = false;
3397 w->vrf_id = vrf_id;
3398 w->ip = *vtep_ip;
3399 w->prefix = *host_prefix;
3400
3401 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3402 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__,
3403 vrf_id, vtep_ip, host_prefix);
3404
3405 return mq_add_handler(w, rib_meta_queue_evpn_add);
3406}
3407
3408/* Enqueue EVPN remote ES for processing */
3409int zebra_rib_queue_evpn_rem_es_add(const esi_t *esi,
3410 const struct in_addr *vtep_ip,
3411 bool esr_rxed, uint8_t df_alg,
3412 uint16_t df_pref)
3413{
3414 struct wq_evpn_wrapper *w;
3415 char buf[ESI_STR_LEN];
3416
3417 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3418
3419 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3420 w->add_p = true;
3421 w->esi = *esi;
3422 w->ip.ipa_type = IPADDR_V4;
3423 w->ip.ipaddr_v4 = *vtep_ip;
3424 w->esr_rxed = esr_rxed;
3425 w->df_alg = df_alg;
3426 w->df_pref = df_pref;
3427
3428 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3429 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3430 esi_to_str(esi, buf, sizeof(buf)));
3431
3432 return mq_add_handler(w, rib_meta_queue_evpn_add);
3433}
3434
3435int zebra_rib_queue_evpn_rem_es_del(const esi_t *esi,
3436 const struct in_addr *vtep_ip)
3437{
3438 struct wq_evpn_wrapper *w;
3439 char buf[ESI_STR_LEN];
3440
3441 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3442
3443 w->type = WQ_EVPN_WRAPPER_TYPE_REM_ES;
3444 w->add_p = false;
3445 w->esi = *esi;
3446 w->ip.ipa_type = IPADDR_V4;
3447 w->ip.ipaddr_v4 = *vtep_ip;
3448
3449 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3450 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3451 esi_to_str(esi, buf, sizeof(buf));
3452 else
3453 strlcpy(buf, "-", sizeof(buf));
3454
3455 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__, vtep_ip,
3456 buf);
3457 }
3458
3459 return mq_add_handler(w, rib_meta_queue_evpn_add);
3460}
3461
7f7e49d1
MS
3462/*
3463 * Enqueue EVPN remote macip update for processing
3464 */
3465int zebra_rib_queue_evpn_rem_macip_add(vni_t vni, const struct ethaddr *macaddr,
3466 const struct ipaddr *ipaddr,
3467 uint8_t flags, uint32_t seq,
3468 struct in_addr vtep_ip, const esi_t *esi)
3469{
3470 struct wq_evpn_wrapper *w;
3471 char buf[ESI_STR_LEN];
3472
3473 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3474
3475 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3476 w->add_p = true;
3477 w->vni = vni;
3478 w->macaddr = *macaddr;
3479 w->ip = *ipaddr;
3480 w->flags = flags;
3481 w->seq = seq;
3482 w->vtep_ip = vtep_ip;
3483 w->esi = *esi;
3484
3485 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
3486 if (memcmp(esi, zero_esi, sizeof(esi_t)) != 0)
3487 esi_to_str(esi, buf, sizeof(buf));
3488 else
3489 strlcpy(buf, "-", sizeof(buf));
3490
3491 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__,
3492 macaddr, &vtep_ip, buf);
3493 }
3494
3495 return mq_add_handler(w, rib_meta_queue_evpn_add);
3496}
3497
3498int zebra_rib_queue_evpn_rem_macip_del(vni_t vni, const struct ethaddr *macaddr,
3499 const struct ipaddr *ip,
3500 struct in_addr vtep_ip)
3501{
3502 struct wq_evpn_wrapper *w;
3503
3504 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3505
3506 w->type = WQ_EVPN_WRAPPER_TYPE_REM_MACIP;
3507 w->add_p = false;
3508 w->vni = vni;
3509 w->macaddr = *macaddr;
3510 w->ip = *ip;
3511 w->vtep_ip = vtep_ip;
3512
3513 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3514 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__,
3515 macaddr, &vtep_ip);
3516
3517 return mq_add_handler(w, rib_meta_queue_evpn_add);
3518}
3519
7e5b0b2b
MS
3520/*
3521 * Enqueue remote VTEP address for processing
3522 */
3523int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id, vni_t vni,
3524 struct in_addr vtep_ip, int flood_control)
3525{
3526 struct wq_evpn_wrapper *w;
3527
3528 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3529
3530 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3531 w->add_p = true;
3532 w->vrf_id = vrf_id;
3533 w->vni = vni;
3534 w->vtep_ip = vtep_ip;
3535 w->flags = flood_control;
3536
3537 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3538 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3539 &vtep_ip);
3540
3541 return mq_add_handler(w, rib_meta_queue_evpn_add);
3542}
3543
3544int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id, vni_t vni,
3545 struct in_addr vtep_ip)
3546{
3547 struct wq_evpn_wrapper *w;
3548
3549 w = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(struct wq_evpn_wrapper));
3550
3551 w->type = WQ_EVPN_WRAPPER_TYPE_REM_VTEP;
3552 w->add_p = false;
3553 w->vrf_id = vrf_id;
3554 w->vni = vni;
3555 w->vtep_ip = vtep_ip;
3556
3557 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3558 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__, vrf_id,
3559 &vtep_ip);
3560
3561 return mq_add_handler(w, rib_meta_queue_evpn_add);
3562}
3563
a310ebc1
DS
3564/* Create new meta queue.
3565 A destructor function doesn't seem to be necessary here.
3566 */
3567static struct meta_queue *meta_queue_new(void)
3568{
3569 struct meta_queue *new;
3570 unsigned i;
3571
3572 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
3573
3574 for (i = 0; i < MQ_SIZE; i++) {
3575 new->subq[i] = list_new();
3576 assert(new->subq[i]);
3577 }
3578
3579 return new;
3580}
3581
32367e7a 3582/* Clean up the EVPN meta-queue list */
a310ebc1
DS
3583static void evpn_meta_queue_free(struct meta_queue *mq, struct list *l,
3584 struct zebra_vrf *zvrf)
32367e7a 3585{
a310ebc1 3586 struct listnode *node, *nnode;
32367e7a
MS
3587 struct wq_evpn_wrapper *w;
3588
3589 /* Free the node wrapper object, and the struct it wraps */
a310ebc1
DS
3590 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3591 if (zvrf) {
3592 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
3593
3594 if (w->vrf_id != vrf_id)
3595 continue;
3596 }
3597
32367e7a
MS
3598 node->data = NULL;
3599
3600 XFREE(MTYPE_WQ_WRAPPER, w);
3601
3602 list_delete_node(l, node);
a310ebc1 3603 mq->size--;
32367e7a
MS
3604 }
3605}
3606
04bec7b2 3607/* Clean up the nhg meta-queue list */
a310ebc1
DS
3608static void nhg_meta_queue_free(struct meta_queue *mq, struct list *l,
3609 struct zebra_vrf *zvrf)
04bec7b2
MS
3610{
3611 struct wq_nhg_wrapper *w;
a310ebc1 3612 struct listnode *node, *nnode;
04bec7b2
MS
3613
3614 /* Free the node wrapper object, and the struct it wraps */
a310ebc1
DS
3615 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3616 if (zvrf) {
3617 vrf_id_t vrf_id = zvrf->vrf->vrf_id;
04bec7b2 3618
a310ebc1
DS
3619 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX &&
3620 w->u.ctx->vrf_id != vrf_id)
3621 continue;
3622 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG &&
3623 w->u.nhe->vrf_id != vrf_id)
3624 continue;
3625 }
04bec7b2
MS
3626 if (w->type == WQ_NHG_WRAPPER_TYPE_CTX)
3627 nhg_ctx_free(&w->u.ctx);
3628 else if (w->type == WQ_NHG_WRAPPER_TYPE_NHG)
3629 zebra_nhg_free(w->u.nhe);
3630
a310ebc1 3631 node->data = NULL;
32367e7a 3632 XFREE(MTYPE_WQ_WRAPPER, w);
04bec7b2
MS
3633
3634 list_delete_node(l, node);
a310ebc1 3635 mq->size--;
04bec7b2 3636 }
4d38fdb4 3637}
3638
53216dff
DS
3639static void early_label_meta_queue_free(struct meta_queue *mq, struct list *l,
3640 struct zebra_vrf *zvrf)
3641{
3642 struct wq_label_wrapper *w;
3643 struct listnode *node, *nnode;
3644
3645 for (ALL_LIST_ELEMENTS(l, node, nnode, w)) {
3646 if (zvrf && zvrf->vrf->vrf_id != w->vrf_id)
3647 continue;
3648
3649 switch (w->type) {
3650 case WQ_LABEL_FTN_UNINSTALL:
3651 case WQ_LABEL_LABELS_PROCESS:
3652 break;
3653 }
3654
3655 node->data = NULL;
3656 XFREE(MTYPE_WQ_WRAPPER, w);
3657 list_delete_node(l, node);
3658 mq->size--;
3659 }
3660}
3661
a310ebc1
DS
3662static void rib_meta_queue_free(struct meta_queue *mq, struct list *l,
3663 struct zebra_vrf *zvrf)
e96f9203 3664{
a310ebc1
DS
3665 struct route_node *rnode;
3666 struct listnode *node, *nnode;
5110a0c6 3667
a310ebc1
DS
3668 for (ALL_LIST_ELEMENTS(l, node, nnode, rnode)) {
3669 rib_dest_t *dest = rib_dest_from_rnode(rnode);
e96f9203 3670
a310ebc1
DS
3671 if (dest && rib_dest_vrf(dest) != zvrf)
3672 continue;
5110a0c6 3673
a310ebc1
DS
3674 route_unlock_node(rnode);
3675 node->data = NULL;
3676 list_delete_node(l, node);
3677 mq->size--;
3678 }
e96f9203
DO
3679}
3680
d7ac4c4d
DS
3681static void early_route_meta_queue_free(struct meta_queue *mq, struct list *l,
3682 struct zebra_vrf *zvrf)
3683{
3684 struct zebra_early_route *zer;
3685 struct listnode *node, *nnode;
3686
3687 for (ALL_LIST_ELEMENTS(l, node, nnode, zer)) {
3688 if (zvrf && zer->re->vrf_id != zvrf->vrf->vrf_id)
3689 continue;
3690
3691 XFREE(MTYPE_RE, zer);
3692 node->data = NULL;
3693 list_delete_node(l, node);
3694 mq->size--;
3695 }
3696}
a310ebc1
DS
3697
3698void meta_queue_free(struct meta_queue *mq, struct zebra_vrf *zvrf)
5a8dfcd8 3699{
014e732d 3700 enum meta_queue_indexes i;
5a8dfcd8 3701
04bec7b2
MS
3702 for (i = 0; i < MQ_SIZE; i++) {
3703 /* Some subqueues may need cleanup - nhgs for example */
014e732d
DS
3704 switch (i) {
3705 case META_QUEUE_NHG:
a310ebc1 3706 nhg_meta_queue_free(mq, mq->subq[i], zvrf);
014e732d
DS
3707 break;
3708 case META_QUEUE_EVPN:
a310ebc1 3709 evpn_meta_queue_free(mq, mq->subq[i], zvrf);
014e732d 3710 break;
d7ac4c4d
DS
3711 case META_QUEUE_EARLY_ROUTE:
3712 early_route_meta_queue_free(mq, mq->subq[i], zvrf);
3713 break;
53216dff
DS
3714 case META_QUEUE_EARLY_LABEL:
3715 early_label_meta_queue_free(mq, mq->subq[i], zvrf);
3716 break;
014e732d
DS
3717 case META_QUEUE_CONNECTED:
3718 case META_QUEUE_KERNEL:
3719 case META_QUEUE_STATIC:
3720 case META_QUEUE_NOTBGP:
3721 case META_QUEUE_BGP:
3722 case META_QUEUE_OTHER:
a310ebc1 3723 rib_meta_queue_free(mq, mq->subq[i], zvrf);
014e732d
DS
3724 break;
3725 }
a310ebc1
DS
3726 if (!zvrf)
3727 list_delete(&mq->subq[i]);
04bec7b2 3728 }
5a8dfcd8 3729
a310ebc1
DS
3730 if (!zvrf)
3731 XFREE(MTYPE_WORK_QUEUE, mq);
0ef6eacc
IR
3732}
3733
4d38fdb4 3734/* initialise zebra rib work queue */
2561d12e 3735static void rib_queue_init(void)
4d38fdb4 3736{
489a9614
DS
3737 if (!(zrouter.ribq = work_queue_new(zrouter.master,
3738 "route_node processing"))) {
e914ccbe 3739 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 3740 "%s: could not initialise work queue!", __func__);
d62a17ae 3741 return;
3742 }
4d38fdb4 3743
d62a17ae 3744 /* fill in the work queue spec */
489a9614 3745 zrouter.ribq->spec.workfunc = &meta_queue_process;
46a4e345 3746 zrouter.ribq->spec.completion_func = NULL;
d62a17ae 3747 /* XXX: TODO: These should be runtime configurable via vty */
489a9614
DS
3748 zrouter.ribq->spec.max_retries = 3;
3749 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
3750 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
d62a17ae 3751
ea45a4e7 3752 if (!(zrouter.mq = meta_queue_new())) {
e914ccbe 3753 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 3754 "%s: could not initialise meta queue!", __func__);
d62a17ae 3755 return;
3756 }
3757 return;
718e3744 3758}
3759
434434f7
DS
3760rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
3761{
3762 rib_dest_t *dest;
3763
3764 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
aa57abfb 3765 rnh_list_init(&dest->nht);
daaeaa21 3766 re_list_init(&dest->routes);
434434f7
DS
3767 route_lock_node(rn); /* rn route table reference */
3768 rn->info = dest;
3769 dest->rnode = rn;
3770
3771 return dest;
3772}
3773
6d691129
PJ
3774/* RIB updates are processed via a queue of pointers to route_nodes.
3775 *
3776 * The queue length is bounded by the maximal size of the routing table,
3777 * as a route_node will not be requeued, if already queued.
3778 *
f0f77c9a
DS
3779 * REs are submitted via rib_addnode or rib_delnode which set minimal
3780 * state, or static_install_route (when an existing RE is updated)
3c0755dc 3781 * and then submit route_node to queue for best-path selection later.
f0f77c9a 3782 * Order of add/delete state changes are preserved for any given RE.
6d691129 3783 *
f0f77c9a 3784 * Deleted REs are reaped during best-path selection.
6d691129
PJ
3785 *
3786 * rib_addnode
f0f77c9a
DS
3787 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3788 * |-------->| | best RE, if required
3c0755dc
PJ
3789 * | |
3790 * static_install->|->rib_addqueue...... -> rib_process
3791 * | |
3792 * |-------->| |-> rib_unlink
f0f77c9a
DS
3793 * |-> set ROUTE_ENTRY_REMOVE |
3794 * rib_delnode (RE freed)
6d691129 3795 *
9fd92e3c
AS
3796 * The 'info' pointer of a route_node points to a rib_dest_t
3797 * ('dest'). Queueing state for a route_node is kept on the dest. The
3798 * dest is created on-demand by rib_link() and is kept around at least
3799 * as long as there are ribs hanging off it (@see rib_gc_dest()).
d62a17ae 3800 *
32367e7a 3801 * Refcounting (aka "locking" throughout the Zebra and FRR code):
6d691129
PJ
3802 *
3803 * - route_nodes: refcounted by:
9fd92e3c
AS
3804 * - dest attached to route_node:
3805 * - managed by: rib_link/rib_gc_dest
6d691129
PJ
3806 * - route_node processing queue
3807 * - managed by: rib_addqueue, rib_process.
3808 *
3809 */
d62a17ae 3810
f0f77c9a 3811/* Add RE to head of the route node. */
d62a17ae 3812static void rib_link(struct route_node *rn, struct route_entry *re, int process)
3813{
d62a17ae 3814 rib_dest_t *dest;
3815 afi_t afi;
3816 const char *rmap_name;
9fd92e3c 3817
d62a17ae 3818 assert(re && rn);
9fd92e3c 3819
d62a17ae 3820 dest = rib_dest_from_rnode(rn);
3821 if (!dest) {
3822 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
3823 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
7a4bb9c5 3824
434434f7 3825 dest = zebra_rib_create_dest(rn);
d62a17ae 3826 }
2263a412 3827
045207e2 3828 re_list_add_head(&dest->routes, re);
d62a17ae 3829
3830 afi = (rn->p.family == AF_INET)
3831 ? AFI_IP
3832 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
fe257ae7
DS
3833 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3834 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3835
d62a17ae 3836 rmap_name = zebra_get_import_table_route_map(afi, re->table);
fe257ae7 3837 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
d62a17ae 3838 } else if (process)
3839 rib_queue_add(rn);
3840}
3841
7e24fdf3
DS
3842static void rib_addnode(struct route_node *rn,
3843 struct route_entry *re, int process)
d62a17ae 3844{
3845 /* RE node has been un-removed before route-node is processed.
3846 * route_node must hence already be on the queue for processing..
3847 */
3848 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
3849 if (IS_ZEBRA_DEBUG_RIB)
3850 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
3851 (void *)rn, (void *)re);
3852
3853 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3854 return;
3855 }
3856 rib_link(rn, re, process);
6d691129
PJ
3857}
3858
9fd92e3c
AS
3859/*
3860 * rib_unlink
3861 *
3862 * Detach a rib structure from a route_node.
3863 *
3864 * Note that a call to rib_unlink() should be followed by a call to
3865 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3866 * longer required to be deleted.
3867 */
d62a17ae 3868void rib_unlink(struct route_node *rn, struct route_entry *re)
6d691129 3869{
d62a17ae 3870 rib_dest_t *dest;
9fd92e3c 3871
d62a17ae 3872 assert(rn && re);
6d691129 3873
d62a17ae 3874 if (IS_ZEBRA_DEBUG_RIB)
3875 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
3876 (void *)re);
6d691129 3877
d62a17ae 3878 dest = rib_dest_from_rnode(rn);
6d691129 3879
045207e2 3880 re_list_del(&dest->routes, re);
7a4bb9c5 3881
2eb07de3
DS
3882 if (dest->selected_fib == re)
3883 dest->selected_fib = NULL;
3884
ce649b9d 3885 rib_re_nhg_free(re);
ee5e8a48 3886
b86c1f4f 3887 zapi_re_opaque_free(re->opaque);
a29a6001 3888
d62a17ae 3889 XFREE(MTYPE_RE, re);
3890}
3891
3892void rib_delnode(struct route_node *rn, struct route_entry *re)
3893{
3894 afi_t afi;
3895
3896 if (IS_ZEBRA_DEBUG_RIB)
3897 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
3898 (void *)rn, (void *)re);
3899 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
3900
3901 afi = (rn->p.family == AF_INET)
3902 ? AFI_IP
3903 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
fe257ae7
DS
3904 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
3905 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
3906
3907 zebra_del_import_table_entry(zvrf, rn, re);
d62a17ae 3908 /* Just clean up if non main table */
e658173a
DS
3909 if (IS_ZEBRA_DEBUG_RIB)
3910 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3911 vrf_id_to_name(re->vrf_id), re->vrf_id, rn,
bd47f3a3 3912 rn, re, zebra_route_string(re->type));
7a4bb9c5 3913
d62a17ae 3914 rib_unlink(rn, re);
3915 } else {
3916 rib_queue_add(rn);
3917 }
718e3744 3918}
3919
6d81b590
MS
3920/*
3921 * Helper that debugs a single nexthop within a route-entry
3922 */
3923static void _route_entry_dump_nh(const struct route_entry *re,
3924 const char *straddr,
3925 const struct nexthop *nexthop)
3926{
3927 char nhname[PREFIX_STRLEN];
3928 char backup_str[50];
3929 char wgt_str[50];
474aebd9 3930 char temp_str[10];
cbe5bafb 3931 char label_str[MPLS_LABEL_STRLEN];
474aebd9 3932 int i;
6d81b590
MS
3933 struct interface *ifp;
3934 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
3935
3936 switch (nexthop->type) {
3937 case NEXTHOP_TYPE_BLACKHOLE:
772270f3 3938 snprintf(nhname, sizeof(nhname), "Blackhole");
6d81b590
MS
3939 break;
3940 case NEXTHOP_TYPE_IFINDEX:
3941 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
772270f3
QY
3942 snprintf(nhname, sizeof(nhname), "%s",
3943 ifp ? ifp->name : "Unknown");
6d81b590
MS
3944 break;
3945 case NEXTHOP_TYPE_IPV4:
3946 /* fallthrough */
3947 case NEXTHOP_TYPE_IPV4_IFINDEX:
3948 inet_ntop(AF_INET, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
3949 break;
3950 case NEXTHOP_TYPE_IPV6:
3951 case NEXTHOP_TYPE_IPV6_IFINDEX:
3952 inet_ntop(AF_INET6, &nexthop->gate, nhname, INET6_ADDRSTRLEN);
3953 break;
3954 }
3955
cbe5bafb
MS
3956 /* Label stack */
3957 label_str[0] = '\0';
3958 if (nexthop->nh_label && nexthop->nh_label->num_labels > 0) {
3959 mpls_label2str(nexthop->nh_label->num_labels,
3960 nexthop->nh_label->label, label_str,
3961 sizeof(label_str), 0 /*pretty*/);
3962 strlcat(label_str, ", ", sizeof(label_str));
3963 }
3964
6d81b590
MS
3965 backup_str[0] = '\0';
3966 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)) {
474aebd9
MS
3967 snprintf(backup_str, sizeof(backup_str), "backup ");
3968 for (i = 0; i < nexthop->backup_num; i++) {
3969 snprintf(temp_str, sizeof(temp_str), "%d, ",
3970 nexthop->backup_idx[i]);
3971 strlcat(backup_str, temp_str, sizeof(backup_str));
3972 }
6d81b590
MS
3973 }
3974
3975 wgt_str[0] = '\0';
3976 if (nexthop->weight)
3977 snprintf(wgt_str, sizeof(wgt_str), "wgt %d,", nexthop->weight);
3978
5609e70f 3979 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
6d81b590 3980 straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
cbe5bafb 3981 nexthop->ifindex, label_str, vrf ? vrf->name : "Unknown",
6d81b590
MS
3982 nexthop->vrf_id,
3983 wgt_str, backup_str,
3984 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
3985 ? "ACTIVE "
3986 : ""),
3987 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
3988 ? "FIB "
3989 : ""),
3990 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
3991 ? "RECURSIVE "
3992 : ""),
3993 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
3994 ? "ONLINK "
3995 : ""),
3996 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
3997 ? "DUPLICATE "
aab4eca1
DS
3998 : ""),
3999 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED)
4000 ? "FILTERED " : ""),
4001 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_HAS_BACKUP)
4002 ? "BACKUP " : ""),
4003 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_SRTE)
5609e70f
XL
4004 ? "SRTE " : ""),
4005 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN)
4006 ? "EVPN " : ""));
6d81b590
MS
4007
4008}
4009
f0f77c9a 4010/* This function dumps the contents of a given RE entry into
dc95824a
DO
4011 * standard debug log. Calling function name and IP prefix in
4012 * question are passed as 1st and 2nd arguments.
4013 */
d62a17ae 4014void _route_entry_dump(const char *func, union prefixconstptr pp,
4015 union prefixconstptr src_pp,
4016 const struct route_entry *re)
4017{
d62a17ae 4018 const struct prefix *src_p = src_pp.p;
4019 bool is_srcdst = src_p && src_p->prefixlen;
4020 char straddr[PREFIX_STRLEN];
4021 char srcaddr[PREFIX_STRLEN];
61e6de9d
DS
4022 char flags_buf[128];
4023 char status_buf[128];
d62a17ae 4024 struct nexthop *nexthop;
c479e756 4025 struct vrf *vrf = vrf_lookup_by_id(re->vrf_id);
6d81b590 4026 struct nexthop_group *nhg;
d62a17ae 4027
5d27875b
DL
4028 prefix2str(pp, straddr, sizeof(straddr));
4029
c479e756 4030 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func,
5d27875b 4031 (const void *)re, straddr,
d62a17ae 4032 is_srcdst ? " from " : "",
4033 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
4034 : "",
c479e756 4035 VRF_LOGNAME(vrf), re->vrf_id);
cc54cfee 4036 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
53c16fbe 4037 straddr, (unsigned long)re->uptime, re->type, re->instance,
cc54cfee 4038 re->table);
61e6de9d
DS
4039 zlog_debug(
4040 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4041 straddr, re->metric, re->mtu, re->distance,
4042 zclient_dump_route_flags(re->flags, flags_buf,
4043 sizeof(flags_buf)),
4044 _dump_re_status(re, status_buf, sizeof(status_buf)));
53c16fbe 4045 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
c415d895
MS
4046 nexthop_group_nexthop_num(&(re->nhe->nhg)),
4047 nexthop_group_active_nexthop_num(&(re->nhe->nhg)));
d62a17ae 4048
6d81b590
MS
4049 /* Dump nexthops */
4050 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
4051 _route_entry_dump_nh(re, straddr, nexthop);
2d68a0f2 4052
d7ac4c4d
DS
4053 if (zebra_nhg_get_backup_nhg(re->nhe)) {
4054 zlog_debug("%s: backup nexthops:", straddr);
c6eee91f 4055
d7ac4c4d
DS
4056 nhg = zebra_nhg_get_backup_nhg(re->nhe);
4057 for (ALL_NEXTHOPS_PTR(nhg, nexthop))
4058 _route_entry_dump_nh(re, straddr, nexthop);
c6eee91f
DS
4059 }
4060
d7ac4c4d
DS
4061 zlog_debug("%s: dump complete", straddr);
4062}
cf363e1b 4063
d7ac4c4d
DS
4064static int rib_meta_queue_early_route_add(struct meta_queue *mq, void *data)
4065{
4066 struct zebra_early_route *ere = data;
718e3744 4067
d7ac4c4d
DS
4068 listnode_add(mq->subq[META_QUEUE_EARLY_ROUTE], data);
4069 mq->size++;
6883bf8d 4070
d7ac4c4d
DS
4071 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
4072 zlog_debug(
4073 "Route %pFX(%u) queued for processing into sub-queue %s",
4074 &ere->p, ere->re->vrf_id,
4075 subqueue2str(META_QUEUE_EARLY_ROUTE));
6b0655a2 4076
d7ac4c4d
DS
4077 return 0;
4078}
718e3744 4079
b0385873
DS
4080struct route_entry *zebra_rib_route_entry_new(vrf_id_t vrf_id, int type,
4081 uint8_t instance, uint32_t flags,
4082 uint32_t nhe_id,
4083 uint32_t table_id,
4084 uint32_t metric, uint32_t mtu,
4085 uint8_t distance, route_tag_t tag)
4086{
4087 struct route_entry *re;
4088
4089 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
4090 re->type = type;
4091 re->instance = instance;
4092 re->distance = distance;
4093 re->flags = flags;
4094 re->metric = metric;
4095 re->mtu = mtu;
4096 re->table = table_id;
4097 re->vrf_id = vrf_id;
4098 re->uptime = monotime(NULL);
4099 re->tag = tag;
4100 re->nhe_id = nhe_id;
4101
4102 return re;
4103}
d7ac4c4d
DS
4104/*
4105 * Internal route-add implementation; there are a couple of different public
4106 * signatures. Callers in this path are responsible for the memory they
4107 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4108 * must free those objects. If this returns < 0, an error has occurred and the
4109 * route_entry 're' has not been captured; the caller should free that also.
4110 *
4111 * -1 -> error
4112 * 0 -> Add
4113 * 1 -> update
4114 */
4115int rib_add_multipath_nhe(afi_t afi, safi_t safi, struct prefix *p,
4116 struct prefix_ipv6 *src_p, struct route_entry *re,
4117 struct nhg_hash_entry *re_nhe, bool startup)
4118{
4119 struct zebra_early_route *ere;
0ca6f3b1 4120
d7ac4c4d
DS
4121 if (!re)
4122 return -1;
0ca6f3b1 4123
d7ac4c4d 4124 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
0ca6f3b1 4125
d7ac4c4d
DS
4126 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4127 ere->afi = afi;
4128 ere->safi = safi;
4129 ere->p = *p;
4130 if (src_p)
4131 ere->src_p = *src_p;
4132 ere->src_p_provided = !!src_p;
4133 ere->re = re;
4134 ere->re_nhe = re_nhe;
4135 ere->startup = startup;
4136
4137 return mq_add_handler(ere, rib_meta_queue_early_route_add);
d62a17ae 4138}
4139
1d48702e
MS
4140/*
4141 * Add a single route.
4142 */
4143int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
4144 struct prefix_ipv6 *src_p, struct route_entry *re,
c6eee91f 4145 struct nexthop_group *ng, bool startup)
1d48702e
MS
4146{
4147 int ret;
d7ac4c4d 4148 struct nhg_hash_entry nhe, *n;
1d48702e
MS
4149
4150 if (!re)
4151 return -1;
4152
4153 /* We either need nexthop(s) or an existing nexthop id */
4154 if (ng == NULL && re->nhe_id == 0)
4155 return -1;
4156
4157 /*
4158 * Use a temporary nhe to convey info to the common/main api.
4159 */
377e29f7 4160 zebra_nhe_init(&nhe, afi, (ng ? ng->nexthop : NULL));
1d48702e
MS
4161 if (ng)
4162 nhe.nhg.nexthop = ng->nexthop;
4163 else if (re->nhe_id > 0)
4164 nhe.id = re->nhe_id;
4165
d7ac4c4d
DS
4166 n = zebra_nhe_copy(&nhe, 0);
4167 ret = rib_add_multipath_nhe(afi, safi, p, src_p, re, n, startup);
1d48702e
MS
4168
4169 /* In error cases, free the route also */
4170 if (ret < 0)
4171 XFREE(MTYPE_RE, re);
4172
4173 return ret;
4174}
4175
d62a17ae 4176void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
acde7f6b 4177 unsigned short instance, uint32_t flags, struct prefix *p,
fd36be7e 4178 struct prefix_ipv6 *src_p, const struct nexthop *nh,
bc541126 4179 uint32_t nhe_id, uint32_t table_id, uint32_t metric,
3ceae22b 4180 uint8_t distance, bool fromkernel)
d62a17ae 4181{
d7ac4c4d
DS
4182 struct zebra_early_route *ere;
4183 struct route_entry *re = NULL;
4184 struct nhg_hash_entry *nhe = NULL;
cf363e1b 4185
b0385873
DS
4186 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4187 table_id, metric, 0, distance, 0);
cf363e1b 4188
d7ac4c4d
DS
4189 if (nh) {
4190 nhe = zebra_nhg_alloc();
4191 nhe->nhg.nexthop = nexthop_dup(nh, NULL);
5dfeba19 4192 }
05737783 4193
d7ac4c4d
DS
4194 ere = XCALLOC(MTYPE_WQ_WRAPPER, sizeof(*ere));
4195 ere->afi = afi;
4196 ere->safi = safi;
4197 ere->p = *p;
4198 if (src_p)
4199 ere->src_p = *src_p;
4200 ere->src_p_provided = !!src_p;
4201 ere->re = re;
4202 ere->re_nhe = nhe;
4203 ere->startup = false;
4204 ere->deletion = true;
4205 ere->fromkernel = fromkernel;
4206
4207 mq_add_handler(ere, rib_meta_queue_early_route_add);
d62a17ae 4208}
718e3744 4209
718e3744 4210
d7c0a89a 4211int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
acde7f6b 4212 unsigned short instance, uint32_t flags, struct prefix *p,
d7c0a89a 4213 struct prefix_ipv6 *src_p, const struct nexthop *nh,
8032b717 4214 uint32_t nhe_id, uint32_t table_id, uint32_t metric, uint32_t mtu,
c6eee91f 4215 uint8_t distance, route_tag_t tag, bool startup)
d62a17ae 4216{
bbb322f2 4217 struct route_entry *re = NULL;
d7ac4c4d
DS
4218 struct nexthop nexthop = {};
4219 struct nexthop_group ng = {};
718e3744 4220
66af6845 4221 /* Allocate new route_entry structure. */
b0385873
DS
4222 re = zebra_rib_route_entry_new(vrf_id, type, instance, flags, nhe_id,
4223 table_id, metric, mtu, distance, tag);
bbb322f2 4224
0eb97b86
MS
4225 /* If the owner of the route supplies a shared nexthop-group id,
4226 * we'll use that. Otherwise, pass the nexthop along directly.
4227 */
e22e8001 4228 if (!nhe_id) {
e22e8001 4229 /* Add nexthop. */
d7ac4c4d
DS
4230 nexthop = *nh;
4231 nexthop_group_add_sorted(&ng, &nexthop);
e22e8001 4232 }
718e3744 4233
d7ac4c4d 4234 return rib_add_multipath(afi, safi, p, src_p, re, &ng, startup);
718e3744 4235}
4236
91e6f25b 4237static const char *rib_update_event2str(enum rib_update_event event)
2a99ab95
SW
4238{
4239 const char *ret = "UNKNOWN";
4240
4241 switch (event) {
4242 case RIB_UPDATE_KERNEL:
4243 ret = "RIB_UPDATE_KERNEL";
4244 break;
4245 case RIB_UPDATE_RMAP_CHANGE:
4246 ret = "RIB_UPDATE_RMAP_CHANGE";
4247 break;
4248 case RIB_UPDATE_OTHER:
4249 ret = "RIB_UPDATE_OTHER";
4250 break;
4251 case RIB_UPDATE_MAX:
4252 break;
4253 }
4254
4255 return ret;
4256}
4257
4258
4259/* Schedule route nodes to be processed if they match the type */
4260static void rib_update_route_node(struct route_node *rn, int type)
4261{
4262 struct route_entry *re, *next;
4263 bool re_changed = false;
4264
4265 RNODE_FOREACH_RE_SAFE (rn, re, next) {
4266 if (type == ZEBRA_ROUTE_ALL || type == re->type) {
4267 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
4268 re_changed = true;
4269 }
4270 }
4271
4272 if (re_changed)
4273 rib_queue_add(rn);
4274}
4275
1c848137 4276/* Schedule routes of a particular table (address-family) based on event. */
54aeba35
DS
4277void rib_update_table(struct route_table *table, enum rib_update_event event,
4278 int rtype)
d62a17ae 4279{
4280 struct route_node *rn;
2a99ab95
SW
4281
4282 if (IS_ZEBRA_DEBUG_EVENT) {
4283 struct zebra_vrf *zvrf;
4284 struct vrf *vrf;
4285
630d5962
DS
4286 zvrf = table->info
4287 ? ((struct rib_table_info *)table->info)->zvrf
4288 : NULL;
2a99ab95
SW
4289 vrf = zvrf ? zvrf->vrf : NULL;
4290
54aeba35 4291 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__,
2a99ab95 4292 table->info ? afi2str(
630d5962 4293 ((struct rib_table_info *)table->info)->afi)
2a99ab95 4294 : "Unknown",
bd47f3a3 4295 VRF_LOGNAME(vrf), zvrf ? zvrf->table_id : 0,
54aeba35 4296 rib_update_event2str(event), zebra_route_string(rtype));
2a99ab95 4297 }
d62a17ae 4298
4299 /* Walk all routes and queue for processing, if appropriate for
4300 * the trigger event.
4301 */
4302 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
1ca60f2c
DS
4303 /*
4304 * If we are looking at a route node and the node
4305 * has already been queued we don't
4306 * need to queue it up again
4307 */
2a99ab95
SW
4308 if (rn->info
4309 && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
4310 RIB_ROUTE_ANY_QUEUED))
1ca60f2c 4311 continue;
2a99ab95 4312
d62a17ae 4313 switch (event) {
2a99ab95
SW
4314 case RIB_UPDATE_KERNEL:
4315 rib_update_route_node(rn, ZEBRA_ROUTE_KERNEL);
4316 break;
d62a17ae 4317 case RIB_UPDATE_RMAP_CHANGE:
4318 case RIB_UPDATE_OTHER:
54aeba35 4319 rib_update_route_node(rn, rtype);
d62a17ae 4320 break;
d62a17ae 4321 default:
4322 break;
4323 }
4324 }
b84c7253 4325}
4326
54aeba35
DS
4327static void rib_update_handle_vrf(vrf_id_t vrf_id, enum rib_update_event event,
4328 int rtype)
718e3744 4329{
d62a17ae 4330 struct route_table *table;
1c848137 4331
2a99ab95
SW
4332 if (IS_ZEBRA_DEBUG_EVENT)
4333 zlog_debug("%s: Handling VRF %s event %s", __func__,
4334 vrf_id_to_name(vrf_id), rib_update_event2str(event));
4335
d62a17ae 4336 /* Process routes of interested address-families. */
4337 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2a99ab95 4338 if (table)
54aeba35 4339 rib_update_table(table, event, rtype);
718e3744 4340
d62a17ae 4341 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
2a99ab95 4342 if (table)
54aeba35 4343 rib_update_table(table, event, rtype);
2a99ab95
SW
4344}
4345
54aeba35 4346static void rib_update_handle_vrf_all(enum rib_update_event event, int rtype)
2a99ab95
SW
4347{
4348 struct zebra_router_table *zrt;
4349
4350 if (IS_ZEBRA_DEBUG_EVENT)
4351 zlog_debug("%s: Handling VRF (ALL) event %s", __func__,
4352 rib_update_event2str(event));
4353
4354 /* Just iterate over all the route tables, rather than vrf lookups */
4355 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables)
54aeba35 4356 rib_update_table(zrt->table, event, rtype);
2a99ab95
SW
4357}
4358
4359struct rib_update_ctx {
91e6f25b 4360 enum rib_update_event event;
2a99ab95
SW
4361 bool vrf_all;
4362 vrf_id_t vrf_id;
4363};
4364
4365static struct rib_update_ctx *rib_update_ctx_init(vrf_id_t vrf_id,
91e6f25b 4366 enum rib_update_event event)
2a99ab95
SW
4367{
4368 struct rib_update_ctx *ctx;
4369
4370 ctx = XCALLOC(MTYPE_RIB_UPDATE_CTX, sizeof(struct rib_update_ctx));
4371
4372 ctx->event = event;
4373 ctx->vrf_id = vrf_id;
4374
4375 return ctx;
4376}
4377
4378static void rib_update_ctx_fini(struct rib_update_ctx **ctx)
4379{
4380 XFREE(MTYPE_RIB_UPDATE_CTX, *ctx);
2a99ab95
SW
4381}
4382
cc9f21da 4383static void rib_update_handler(struct thread *thread)
2a99ab95
SW
4384{
4385 struct rib_update_ctx *ctx;
4386
4387 ctx = THREAD_ARG(thread);
4388
4389 if (ctx->vrf_all)
54aeba35 4390 rib_update_handle_vrf_all(ctx->event, ZEBRA_ROUTE_ALL);
2a99ab95 4391 else
54aeba35 4392 rib_update_handle_vrf(ctx->vrf_id, ctx->event, ZEBRA_ROUTE_ALL);
2a99ab95
SW
4393
4394 rib_update_ctx_fini(&ctx);
2a99ab95
SW
4395}
4396
4397/*
4398 * Thread list to ensure we don't schedule a ton of events
4399 * if interfaces are flapping for instance.
4400 */
4401static struct thread *t_rib_update_threads[RIB_UPDATE_MAX];
4402
2a99ab95 4403/* Schedule a RIB update event for all vrfs */
91e6f25b 4404void rib_update(enum rib_update_event event)
2a99ab95
SW
4405{
4406 struct rib_update_ctx *ctx;
4407
e13f12a7
DS
4408 if (thread_is_scheduled(t_rib_update_threads[event]))
4409 return;
2a99ab95 4410
e13f12a7 4411 ctx = rib_update_ctx_init(0, event);
2a99ab95
SW
4412 ctx->vrf_all = true;
4413
e13f12a7
DS
4414 thread_add_event(zrouter.master, rib_update_handler, ctx, 0,
4415 &t_rib_update_threads[event]);
4416
4417 if (IS_ZEBRA_DEBUG_EVENT)
05267678 4418 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__,
2a99ab95 4419 rib_update_event2str(event));
718e3744 4420}
4421
718e3744 4422/* Delete self installed routes after zebra is relaunched. */
95a29032 4423void rib_sweep_table(struct route_table *table)
d62a17ae 4424{
4425 struct route_node *rn;
4426 struct route_entry *re;
4427 struct route_entry *next;
915902cb 4428 struct nexthop *nexthop;
d62a17ae 4429
915902cb
DS
4430 if (!table)
4431 return;
d62a17ae 4432
6d81b590
MS
4433 if (IS_ZEBRA_DEBUG_RIB)
4434 zlog_debug("%s: starting", __func__);
4435
915902cb 4436 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
a2addae8 4437 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d4644d41 4438
915902cb
DS
4439 if (IS_ZEBRA_DEBUG_RIB)
4440 route_entry_dump(&rn->p, NULL, re);
4441
4442 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4443 continue;
4444
4445 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
4446 continue;
4447
d4644d41
DS
4448 /*
4449 * If routes are older than startup_time then
4450 * we know we read them in from the kernel.
4451 * As such we can safely remove them.
4452 */
4453 if (zrouter.startup_time < re->uptime)
4454 continue;
4455
915902cb
DS
4456 /*
4457 * So we are starting up and have received
4458 * routes from the kernel that we have installed
4459 * from a previous run of zebra but not cleaned
4460 * up ( say a kill -9 )
4461 * But since we haven't actually installed
4462 * them yet( we received them from the kernel )
4463 * we don't think they are active.
4464 * So let's pretend they are active to actually
4465 * remove them.
4466 * In all honesty I'm not sure if we should
4467 * mark them as active when we receive them
4468 * This is startup only so probably ok.
4469 *
4470 * If we ever decide to move rib_sweep_table
4471 * to a different spot (ie startup )
4472 * this decision needs to be revisited
4473 */
677c1dd5 4474 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
c415d895 4475 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop))
915902cb
DS
4476 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
4477
0c555cc6
DS
4478 rib_uninstall_kernel(rn, re);
4479 rib_delnode(rn, re);
915902cb
DS
4480 }
4481 }
6d81b590
MS
4482
4483 if (IS_ZEBRA_DEBUG_RIB)
4484 zlog_debug("%s: ends", __func__);
718e3744 4485}
4486
4487/* Sweep all RIB tables. */
cc9f21da 4488void rib_sweep_route(struct thread *t)
718e3744 4489{
d62a17ae 4490 struct vrf *vrf;
4491 struct zebra_vrf *zvrf;
78104b9b 4492
a2addae8 4493 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
915902cb
DS
4494 if ((zvrf = vrf->info) == NULL)
4495 continue;
4496
d62a17ae 4497 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
4498 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
4499 }
95a29032 4500
89272910 4501 zebra_router_sweep_route();
38e40db1 4502 zebra_router_sweep_nhgs();
718e3744 4503}
2ea1ab1c
VT
4504
4505/* Remove specific by protocol routes from 'table'. */
d7c0a89a 4506unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
47a08aa9 4507 struct route_table *table)
d62a17ae 4508{
4509 struct route_node *rn;
4510 struct route_entry *re;
4511 struct route_entry *next;
4512 unsigned long n = 0;
4513
4514 if (table)
4515 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 4516 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 4517 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
4518 continue;
4519 if (re->type == proto
4520 && re->instance == instance) {
4521 rib_delnode(rn, re);
4522 n++;
4523 }
4524 }
4525 return n;
2ea1ab1c
VT
4526}
4527
4528/* Remove specific by protocol routes. */
d7c0a89a 4529unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2ea1ab1c 4530{
d62a17ae 4531 struct vrf *vrf;
4532 struct zebra_vrf *zvrf;
d8612e65 4533 struct other_route_table *ort;
d62a17ae 4534 unsigned long cnt = 0;
78104b9b 4535
d8612e65
DS
4536 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4537 zvrf = vrf->info;
4538 if (!zvrf)
4539 continue;
78104b9b 4540
d8612e65
DS
4541 cnt += rib_score_proto_table(proto, instance,
4542 zvrf->table[AFI_IP][SAFI_UNICAST])
4543 + rib_score_proto_table(
4544 proto, instance,
4545 zvrf->table[AFI_IP6][SAFI_UNICAST]);
4546
81fddbe7 4547 frr_each(otable, &zvrf->other_tables, ort) cnt +=
d8612e65
DS
4548 rib_score_proto_table(proto, instance, ort->table);
4549 }
47a08aa9 4550
d62a17ae 4551 return cnt;
2ea1ab1c
VT
4552}
4553
718e3744 4554/* Close RIB and clean up kernel routes. */
d62a17ae 4555void rib_close_table(struct route_table *table)
718e3744 4556{
d62a17ae 4557 struct route_node *rn;
5f7a4718 4558 rib_dest_t *dest;
718e3744 4559
1e9f448f
DS
4560 if (!table)
4561 return;
9fd92e3c 4562
5f7a4718
DS
4563 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
4564 dest = rib_dest_from_rnode(rn);
1e9f448f 4565
5f7a4718 4566 if (dest && dest->selected_fib) {
3cdba47a
DS
4567 rib_uninstall_kernel(rn, dest->selected_fib);
4568 dest->selected_fib = NULL;
1e9f448f 4569 }
5f7a4718 4570 }
718e3744 4571}
4572
9bd9717b
MS
4573/*
4574 * Handler for async dataplane results after a pseudowire installation
4575 */
f00b37e7 4576static void handle_pw_result(struct zebra_dplane_ctx *ctx)
9bd9717b 4577{
9bd9717b
MS
4578 struct zebra_pw *pw;
4579 struct zebra_vrf *vrf;
4580
4581 /* The pseudowire code assumes success - we act on an error
4582 * result for installation attempts here.
4583 */
4584 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
f00b37e7 4585 return;
9bd9717b
MS
4586
4587 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
4588 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
7c7ef4a8 4589 pw = zebra_pw_find(vrf, dplane_ctx_get_ifname(ctx));
9bd9717b 4590 if (pw)
fd563cc7
KS
4591 zebra_pw_install_failure(pw,
4592 dplane_ctx_get_pw_status(ctx));
9bd9717b 4593 }
9bd9717b
MS
4594}
4595
7cdb1a84 4596/*
d37f4d6c
MS
4597 * Handle results from the dataplane system. Dequeue update context
4598 * structs, dispatch to appropriate internal handlers.
7cdb1a84 4599 */
cc9f21da 4600static void rib_process_dplane_results(struct thread *thread)
7cdb1a84 4601{
25779064 4602 struct zebra_dplane_ctx *ctx;
4c206c8f 4603 struct dplane_ctx_q ctxlist;
2fc69f03 4604 bool shut_p = false;
4c206c8f
MS
4605
4606 /* Dequeue a list of completed updates with one lock/unlock cycle */
7cdb1a84
MS
4607
4608 do {
4c206c8f
MS
4609 TAILQ_INIT(&ctxlist);
4610
7cdb1a84 4611 /* Take lock controlling queue of results */
cb1991af 4612 frr_with_mutex (&dplane_mutex) {
d37f4d6c 4613 /* Dequeue list of context structs */
4c206c8f 4614 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
7cdb1a84 4615 }
7cdb1a84 4616
4c206c8f
MS
4617 /* Dequeue context block */
4618 ctx = dplane_ctx_dequeue(&ctxlist);
4619
4620 /* If we've emptied the results queue, we're done */
4621 if (ctx == NULL)
7cdb1a84 4622 break;
7cdb1a84 4623
2fc69f03
MS
4624 /* If zebra is shutting down, avoid processing results,
4625 * just drain the results queue.
4626 */
4627 shut_p = atomic_load_explicit(&zrouter.in_shutdown,
4628 memory_order_relaxed);
4629 if (shut_p) {
4630 while (ctx) {
4631 dplane_ctx_fini(&ctx);
4632
4633 ctx = dplane_ctx_dequeue(&ctxlist);
4634 }
4635
4636 continue;
4637 }
4638
1247efcc
DL
4639#ifdef HAVE_SCRIPTING
4640 char *script_name = frrscript_names_get_script_name(
4641 ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
4642
4643 int ret = 1;
4644 struct frrscript *fs;
4645
4646 if (script_name) {
4647 fs = frrscript_new(script_name);
4648 if (fs)
4649 ret = frrscript_load(
4650 fs, ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4651 NULL);
4652 }
4653#endif /* HAVE_SCRIPTING */
4654
4c206c8f 4655 while (ctx) {
1247efcc
DL
4656
4657#ifdef HAVE_SCRIPTING
4658 if (ret == 0)
4659 frrscript_call(fs,
4660 ZEBRA_ON_RIB_PROCESS_HOOK_CALL,
4661 ("ctx", ctx));
4662#endif /* HAVE_SCRIPTING */
4663
d37f4d6c
MS
4664 switch (dplane_ctx_get_op(ctx)) {
4665 case DPLANE_OP_ROUTE_INSTALL:
4666 case DPLANE_OP_ROUTE_UPDATE:
4667 case DPLANE_OP_ROUTE_DELETE:
188a00e0
MS
4668 /* Bit of special case for route updates
4669 * that were generated by async notifications:
4670 * we don't want to continue processing these
4671 * in the rib.
4672 */
4673 if (dplane_ctx_get_notif_provider(ctx) == 0)
4674 rib_process_result(ctx);
f00b37e7 4675 break;
d37f4d6c 4676
54818e3b
MS
4677 case DPLANE_OP_ROUTE_NOTIFY:
4678 rib_process_dplane_notify(ctx);
d37f4d6c
MS
4679 break;
4680
9865e1c3
SW
4681 case DPLANE_OP_NH_INSTALL:
4682 case DPLANE_OP_NH_UPDATE:
4683 case DPLANE_OP_NH_DELETE:
4684 zebra_nhg_dplane_result(ctx);
4685 break;
4686
d37f4d6c
MS
4687 case DPLANE_OP_LSP_INSTALL:
4688 case DPLANE_OP_LSP_UPDATE:
4689 case DPLANE_OP_LSP_DELETE:
188a00e0
MS
4690 /* Bit of special case for LSP updates
4691 * that were generated by async notifications:
4692 * we don't want to continue processing these.
4693 */
4694 if (dplane_ctx_get_notif_provider(ctx) == 0)
4695 zebra_mpls_lsp_dplane_result(ctx);
f00b37e7 4696 break;
d37f4d6c 4697
104e3ad9
MS
4698 case DPLANE_OP_LSP_NOTIFY:
4699 zebra_mpls_process_dplane_notify(ctx);
d37f4d6c
MS
4700 break;
4701
9bd9717b
MS
4702 case DPLANE_OP_PW_INSTALL:
4703 case DPLANE_OP_PW_UNINSTALL:
4704 handle_pw_result(ctx);
4705 break;
4706
cf363e1b
MS
4707 case DPLANE_OP_SYS_ROUTE_ADD:
4708 case DPLANE_OP_SYS_ROUTE_DELETE:
cf363e1b
MS
4709 break;
4710
036d93c0
MS
4711 case DPLANE_OP_MAC_INSTALL:
4712 case DPLANE_OP_MAC_DELETE:
4713 zebra_vxlan_handle_result(ctx);
4714 break;
4715
f62e5480
JU
4716 case DPLANE_OP_RULE_ADD:
4717 case DPLANE_OP_RULE_DELETE:
4718 case DPLANE_OP_RULE_UPDATE:
5162e000
PG
4719 case DPLANE_OP_IPTABLE_ADD:
4720 case DPLANE_OP_IPTABLE_DELETE:
ef524230
PG
4721 case DPLANE_OP_IPSET_ADD:
4722 case DPLANE_OP_IPSET_DELETE:
4723 case DPLANE_OP_IPSET_ENTRY_ADD:
4724 case DPLANE_OP_IPSET_ENTRY_DELETE:
f62e5480
JU
4725 zebra_pbr_dplane_result(ctx);
4726 break;
4727
d166308b
MS
4728 case DPLANE_OP_INTF_ADDR_ADD:
4729 case DPLANE_OP_INTF_ADDR_DEL:
5d414138
SW
4730 case DPLANE_OP_INTF_INSTALL:
4731 case DPLANE_OP_INTF_UPDATE:
4732 case DPLANE_OP_INTF_DELETE:
cd787a8a 4733 case DPLANE_OP_INTF_NETCONFIG:
5d414138 4734 zebra_if_dplane_result(ctx);
cd787a8a
MS
4735 break;
4736
c317d3f2
SY
4737 case DPLANE_OP_TC_QDISC_INSTALL:
4738 case DPLANE_OP_TC_QDISC_UNINSTALL:
4739 case DPLANE_OP_TC_CLASS_ADD:
4740 case DPLANE_OP_TC_CLASS_DELETE:
4741 case DPLANE_OP_TC_CLASS_UPDATE:
4742 case DPLANE_OP_TC_FILTER_ADD:
4743 case DPLANE_OP_TC_FILTER_DELETE:
4744 case DPLANE_OP_TC_FILTER_UPDATE:
449a30ed
SY
4745 break;
4746
931fa60c
MS
4747 /* Some op codes not handled here */
4748 case DPLANE_OP_ADDR_INSTALL:
4749 case DPLANE_OP_ADDR_UNINSTALL:
4750 case DPLANE_OP_NEIGH_INSTALL:
4751 case DPLANE_OP_NEIGH_UPDATE:
4752 case DPLANE_OP_NEIGH_DELETE:
0a27a2fe
PG
4753 case DPLANE_OP_NEIGH_IP_INSTALL:
4754 case DPLANE_OP_NEIGH_IP_DELETE:
0bbd4ff4
MS
4755 case DPLANE_OP_VTEP_ADD:
4756 case DPLANE_OP_VTEP_DELETE:
d68e74b4 4757 case DPLANE_OP_NEIGH_DISCOVER:
c60522f7 4758 case DPLANE_OP_BR_PORT_UPDATE:
e18747a9 4759 case DPLANE_OP_NEIGH_TABLE_UPDATE:
62b4b7e4 4760 case DPLANE_OP_GRE_SET:
931fa60c 4761 case DPLANE_OP_NONE:
d37f4d6c 4762 break;
931fa60c 4763
d37f4d6c 4764 } /* Dispatch by op code */
4c206c8f 4765
f00b37e7 4766 dplane_ctx_fini(&ctx);
4c206c8f
MS
4767 ctx = dplane_ctx_dequeue(&ctxlist);
4768 }
4769
5709131c 4770 } while (1);
7cdb1a84
MS
4771}
4772
4773/*
4774 * Results are returned from the dataplane subsystem, in the context of
1bcea841 4775 * the dataplane pthread. We enqueue the results here for processing by
7cdb1a84
MS
4776 * the main thread later.
4777 */
4c206c8f 4778static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
7cdb1a84
MS
4779{
4780 /* Take lock controlling queue of results */
cb1991af 4781 frr_with_mutex (&dplane_mutex) {
4c206c8f
MS
4782 /* Enqueue context blocks */
4783 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
7cdb1a84 4784 }
7cdb1a84 4785
4c206c8f 4786 /* Ensure event is signalled to zebra main pthread */
3801e764 4787 thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
7cdb1a84
MS
4788 &t_dplane);
4789
5709131c 4790 return 0;
7cdb1a84
MS
4791}
4792
eaa2716d
SW
4793/*
4794 * Ensure there are no empty slots in the route_info array.
4795 * Every route type in zebra should be present there.
4796 */
4797static void check_route_info(void)
4798{
4799 int len = array_size(route_info);
4800
4801 /*
4802 * ZEBRA_ROUTE_SYSTEM is special cased since
4803 * its key is 0 anyway.
4804 *
4805 * ZEBRA_ROUTE_ALL is also ignored.
4806 */
4807 for (int i = 0; i < len; i++) {
16d91fce
DS
4808 assert(route_info[i].key >= ZEBRA_ROUTE_SYSTEM &&
4809 route_info[i].key < ZEBRA_ROUTE_MAX);
8dc7a759 4810 assert(route_info[i].meta_q_map < MQ_SIZE);
eaa2716d
SW
4811 }
4812}
4813
718e3744 4814/* Routing information base initialize. */
d62a17ae 4815void rib_init(void)
718e3744 4816{
eaa2716d
SW
4817 check_route_info();
4818
2561d12e 4819 rib_queue_init();
7cdb1a84
MS
4820
4821 /* Init dataplane, and register for results */
4822 pthread_mutex_init(&dplane_mutex, NULL);
4823 TAILQ_INIT(&rib_dplane_q);
4c206c8f 4824 zebra_dplane_init(rib_dplane_results);
718e3744 4825}
0915bb0c
AS
4826
4827/*
4828 * vrf_id_get_next
4829 *
4830 * Get the first vrf id that is greater than the given vrf id if any.
4831 *
2951a7a4 4832 * Returns true if a vrf id was found, false otherwise.
0915bb0c 4833 */
d62a17ae 4834static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 4835{
d62a17ae 4836 struct vrf *vrf;
b72ede27 4837
d62a17ae 4838 vrf = vrf_lookup_by_id(vrf_id);
4839 if (vrf) {
4840 vrf = RB_NEXT(vrf_id_head, vrf);
4841 if (vrf) {
4842 *next_id_p = vrf->vrf_id;
4843 return 1;
4844 }
4845 }
0915bb0c 4846
d62a17ae 4847 return 0;
0915bb0c
AS
4848}
4849
4850/*
4851 * rib_tables_iter_next
4852 *
4853 * Returns the next table in the iteration.
4854 */
d62a17ae 4855struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
4856{
4857 struct route_table *table;
4858
4859 /*
4860 * Array that helps us go over all AFI/SAFI combinations via one
4861 * index.
4862 */
2b64873d 4863 static const struct {
d62a17ae 4864 afi_t afi;
4865 safi_t safi;
4866 } afi_safis[] = {
4867 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
4868 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
4869 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
4870 };
4871
4872 table = NULL;
4873
4874 switch (iter->state) {
4875
4876 case RIB_TABLES_ITER_S_INIT:
4877 iter->vrf_id = VRF_DEFAULT;
4878 iter->afi_safi_ix = -1;
4879
4880 /* Fall through */
4881
4882 case RIB_TABLES_ITER_S_ITERATING:
4883 iter->afi_safi_ix++;
4884 while (1) {
4885
4886 while (iter->afi_safi_ix
7e3a1ec7 4887 < (int)array_size(afi_safis)) {
d62a17ae 4888 table = zebra_vrf_table(
4889 afi_safis[iter->afi_safi_ix].afi,
4890 afi_safis[iter->afi_safi_ix].safi,
4891 iter->vrf_id);
4892 if (table)
4893 break;
4894
4895 iter->afi_safi_ix++;
4896 }
4897
4898 /*
4899 * Found another table in this vrf.
4900 */
4901 if (table)
4902 break;
4903
4904 /*
4905 * Done with all tables in the current vrf, go to the
4906 * next
4907 * one.
4908 */
4909 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
4910 break;
4911
4912 iter->afi_safi_ix = 0;
4913 }
0915bb0c 4914
0915bb0c
AS
4915 break;
4916
d62a17ae 4917 case RIB_TABLES_ITER_S_DONE:
4918 return NULL;
0915bb0c
AS
4919 }
4920
d62a17ae 4921 if (table)
4922 iter->state = RIB_TABLES_ITER_S_ITERATING;
4923 else
4924 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c 4925
d62a17ae 4926 return table;
0915bb0c 4927}