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