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