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