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