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