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