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