]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
Merge pull request #4730 from idryzhov/fix
[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
1131 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1132
1133 /* Currently selected re. */
1134 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1135 assert(old_selected == NULL);
1136 old_selected = re;
1137 }
bab85d4f 1138
d62a17ae 1139 /* Skip deleted entries from selection */
1140 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1141 continue;
1142
1143 /* Skip unreachable nexthop. */
1144 /* This first call to nexthop_active_update is merely to
6883bf8d
DS
1145 * determine if there's any change to nexthops associated
1146 * with this RIB entry. Now, rib_process() can be invoked due
1147 * to an external event such as link down or due to
1148 * next-hop-tracking evaluation. In the latter case,
d62a17ae 1149 * a decision has already been made that the NHs have changed.
6883bf8d
DS
1150 * So, no need to invoke a potentially expensive call again.
1151 * Further, since the change might be in a recursive NH which
1152 * is not caught in the nexthop_active_update() code. Thus, we
1153 * might miss changes to recursive NHs.
d62a17ae 1154 */
6883bf8d 1155 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
99eabcec 1156 && !nexthop_active_update(rn, re)) {
d62a17ae 1157 if (re->type == ZEBRA_ROUTE_TABLE) {
1158 /* XXX: HERE BE DRAGONS!!!!!
1159 * In all honesty, I have not yet figured out
6883bf8d
DS
1160 * what this part does or why the
1161 * ROUTE_ENTRY_CHANGED test above is correct
d62a17ae 1162 * or why we need to delete a route here, and
6883bf8d
DS
1163 * also not whether this concerns both selected
1164 * and fib route, or only selected
1165 * or only fib
1166 *
1167 * This entry was denied by the 'ip protocol
1168 * table' route-map, we need to delete it */
d62a17ae 1169 if (re != old_selected) {
1170 if (IS_ZEBRA_DEBUG_RIB)
1171 zlog_debug(
32391aff 1172 "%s: %u:%s: imported via import-table but denied "
d62a17ae 1173 "by the ip protocol table route-map",
32391aff 1174 __func__, vrf_id, buf);
d62a17ae 1175 rib_unlink(rn, re);
1176 } else
1177 SET_FLAG(re->status,
1178 ROUTE_ENTRY_REMOVED);
1179 }
1180
1181 continue;
1182 }
bab85d4f 1183
d62a17ae 1184 /* Infinite distance. */
1185 if (re->distance == DISTANCE_INFINITY) {
1186 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1187 continue;
1188 }
bab85d4f 1189
d62a17ae 1190 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1191 best = rib_choose_best(new_fib, re);
1192 if (new_fib && best != new_fib)
1193 UNSET_FLAG(new_fib->status,
1194 ROUTE_ENTRY_CHANGED);
1195 new_fib = best;
1196 } else {
1197 best = rib_choose_best(new_selected, re);
1198 if (new_selected && best != new_selected)
1199 UNSET_FLAG(new_selected->status,
1200 ROUTE_ENTRY_CHANGED);
1201 new_selected = best;
1202 }
1203 if (best != re)
1204 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1205 } /* RNODE_FOREACH_RE */
1206
1207 /* If no FIB override route, use the selected route also for FIB */
1208 if (new_fib == NULL)
1209 new_fib = new_selected;
1210
1211 /* After the cycle is finished, the following pointers will be set:
1212 * old_selected --- RE entry currently having SELECTED
1213 * new_selected --- RE entry that is newly SELECTED
1214 * old_fib --- RE entry currently in kernel FIB
1215 * new_fib --- RE entry that is newly to be in kernel FIB
1216 *
1217 * new_selected will get SELECTED flag, and is going to be redistributed
1218 * the zclients. new_fib (which can be new_selected) will be installed
1219 * in kernel.
1220 */
1221
1222 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1223 zlog_debug(
1224 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1225 vrf_id, buf, (void *)old_selected, (void *)new_selected,
1226 (void *)old_fib, (void *)new_fib);
1227 }
446bb95e 1228
d62a17ae 1229 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1230 * fib == selected */
9d303b37
DL
1231 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1232 ROUTE_ENTRY_CHANGED);
d62a17ae 1233
1234 /* Update fib according to selection results */
1235 if (new_fib && old_fib)
1236 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1237 else if (new_fib)
1238 rib_process_add_fib(zvrf, rn, new_fib);
1239 else if (old_fib)
1240 rib_process_del_fib(zvrf, rn, old_fib);
1241
8cb41cd6 1242 /* Update SELECTED entry */
d62a17ae 1243 if (old_selected != new_selected || selected_changed) {
93bdadae 1244
6883bf8d 1245 if (new_selected && new_selected != new_fib)
d62a17ae 1246 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
41ec9222 1247
3cdba47a 1248 if (new_selected)
5af4b346
MS
1249 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1250
d62a17ae 1251 if (old_selected) {
1252 if (!new_selected)
1253 redistribute_delete(p, src_p, old_selected);
1254 if (old_selected != new_selected)
1255 UNSET_FLAG(old_selected->flags,
1256 ZEBRA_FLAG_SELECTED);
f857321e 1257 }
d62a17ae 1258 }
3e5c6e00 1259
d62a17ae 1260 /* Remove all RE entries queued for removal */
a2addae8 1261 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1262 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1263 if (IS_ZEBRA_DEBUG_RIB) {
1264 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1265 (void *)rn, (void *)re);
1266 }
1267 rib_unlink(rn, re);
1268 }
1269 }
4d38fdb4 1270
d62a17ae 1271 /*
1272 * Check if the dest can be deleted now.
1273 */
1274 rib_gc_dest(rn);
e96f9203
DO
1275}
1276
a1494c25
DS
1277static void zebra_rib_evaluate_mpls(struct route_node *rn)
1278{
1279 rib_dest_t *dest = rib_dest_from_rnode(rn);
1280 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
1281
1282 if (!dest)
1283 return;
1284
1285 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1286 if (IS_ZEBRA_DEBUG_MPLS)
1287 zlog_debug(
1288 "%u: Scheduling all LSPs upon RIB completion",
1289 zvrf_id(zvrf));
1290 zebra_mpls_lsp_schedule(zvrf);
1291 mpls_unmark_lsps_for_processing(rn);
1292 }
1293}
1294
e5ac2adf
MS
1295/*
1296 * Utility to match route with dplane context data
1297 */
1298static bool rib_route_match_ctx(const struct route_entry *re,
25779064
MS
1299 const struct zebra_dplane_ctx *ctx,
1300 bool is_update)
e5ac2adf
MS
1301{
1302 bool result = false;
1303
1304 if (is_update) {
1305 /*
1306 * In 'update' case, we test info about the 'previous' or
1307 * 'old' route
1308 */
1309 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1310 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1311 result = true;
1312
1313 /* TODO -- we're using this extra test, but it's not
1314 * exactly clear why.
1315 */
1316 if (re->type == ZEBRA_ROUTE_STATIC &&
1317 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1318 re->tag != dplane_ctx_get_old_tag(ctx))) {
1319 result = false;
1320 }
1321 }
1322
1323 } else {
1324 /*
1325 * Ordinary, single-route case using primary context info
1326 */
1327 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1328 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1329 /* Skip route that's been deleted */
1330 goto done;
1331 }
1332
1333 if ((re->type == dplane_ctx_get_type(ctx)) &&
1334 (re->instance == dplane_ctx_get_instance(ctx))) {
1335 result = true;
1336
1337 /* TODO -- we're using this extra test, but it's not
1338 * exactly clear why.
1339 */
1340 if (re->type == ZEBRA_ROUTE_STATIC &&
1341 (re->distance != dplane_ctx_get_distance(ctx) ||
1342 re->tag != dplane_ctx_get_tag(ctx))) {
1343 result = false;
1344 }
1345 }
1346 }
1347
1348done:
1349
1350 return (result);
1351}
1352
3cdba47a
DS
1353static void zebra_rib_fixup_system(struct route_node *rn)
1354{
1355 struct route_entry *re;
1356
1357 RNODE_FOREACH_RE(rn, re) {
1358 struct nexthop *nhop;
1359
1360 if (!RIB_SYSTEM_ROUTE(re))
1361 continue;
1362
1363 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1364 continue;
1365
1366 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1367
1368 for (ALL_NEXTHOPS(re->ng, nhop)) {
1369 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1370 continue;
1371
1372 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1373 }
1374 }
1375}
1376
e5ac2adf 1377/*
78bf56b0
MS
1378 * Update a route from a dplane context. This consolidates common code
1379 * that can be used in processing of results from FIB updates, and in
1380 * async notification processing.
efe6c026 1381 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
e5ac2adf 1382 */
efe6c026
MS
1383static bool rib_update_re_from_ctx(struct route_entry *re,
1384 struct route_node *rn,
1385 struct zebra_dplane_ctx *ctx)
78bf56b0 1386{
941e261c 1387 char dest_str[PREFIX_STRLEN] = "";
efe6c026 1388 char nh_str[NEXTHOP_STRLEN];
78bf56b0 1389 struct nexthop *nexthop, *ctx_nexthop;
941e261c
MS
1390 bool matched;
1391 const struct nexthop_group *ctxnhg;
efe6c026
MS
1392 bool is_selected = false; /* Is 're' currently the selected re? */
1393 bool changed_p = false; /* Change to nexthops? */
1394 rib_dest_t *dest;
78bf56b0 1395
941e261c
MS
1396 /* Note well: only capturing the prefix string if debug is enabled here;
1397 * unconditional log messages will have to generate the string.
1398 */
1399 if (IS_ZEBRA_DEBUG_RIB)
1400 prefix2str(&(rn->p), dest_str, sizeof(dest_str));
78bf56b0 1401
efe6c026
MS
1402 dest = rib_dest_from_rnode(rn);
1403 if (dest)
1404 is_selected = (re == dest->selected_fib);
1405
1406 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1407 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1408 re->vrf_id, dest_str, (is_selected ? "" : "NOT "));
1409
1410 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
941e261c 1411 * If the installed set differs from the set requested by the rib/owner,
efe6c026
MS
1412 * we use the fib-specific nexthop-group to record the actual FIB
1413 * status.
78bf56b0 1414 */
941e261c
MS
1415
1416 /*
1417 * First check the fib nexthop-group, if it's present. The comparison
efe6c026 1418 * here is quite strict: we require that the fib sets match exactly.
941e261c
MS
1419 */
1420 matched = false;
1421 do {
1422 if (re->fib_ng.nexthop == NULL)
1423 break;
1424
1425 matched = true;
1426
1427 /* First check the route's fib nexthops */
1428 for (ALL_NEXTHOPS(re->fib_ng, nexthop)) {
1429
1430 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1431 continue;
1432
efe6c026 1433 ctx_nexthop = NULL;
941e261c
MS
1434 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
1435 ctx_nexthop)) {
1436 if (nexthop_same(ctx_nexthop, nexthop))
1437 break;
1438 }
1439
1440 if (ctx_nexthop == NULL) {
1441 /* Nexthop not in the new installed set */
efe6c026
MS
1442 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1443 nexthop2str(nexthop, nh_str,
1444 sizeof(nh_str));
1445 zlog_debug("update_from_ctx: no match for fib nh %s",
1446 nh_str);
1447 }
1448
941e261c
MS
1449 matched = false;
1450 break;
1451 }
1452 }
1453
1454 if (!matched)
1455 break;
1456
1457 /* Check the new installed set */
efe6c026 1458 ctx_nexthop = NULL;
941e261c
MS
1459 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), ctx_nexthop)) {
1460
1461 if (CHECK_FLAG(ctx_nexthop->flags,
1462 NEXTHOP_FLAG_RECURSIVE))
1463 continue;
1464
1465 /* Compare with the current group's nexthops */
efe6c026
MS
1466 nexthop = NULL;
1467 for (ALL_NEXTHOPS(re->fib_ng, nexthop)) {
941e261c
MS
1468 if (nexthop_same(nexthop, ctx_nexthop))
1469 break;
1470 }
1471
1472 if (nexthop == NULL) {
1473 /* Nexthop not in the old installed set */
efe6c026
MS
1474 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1475 nexthop2str(ctx_nexthop, nh_str,
1476 sizeof(nh_str));
1477 zlog_debug("update_from_ctx: no fib match for notif nh %s",
1478 nh_str);
1479 }
941e261c
MS
1480 matched = false;
1481 break;
1482 }
1483 }
1484
1485 } while (0);
1486
1487 /* If the new FIB set matches the existing FIB set, we're done. */
1488 if (matched) {
1489 if (IS_ZEBRA_DEBUG_RIB)
1490 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1491 re->vrf_id, dest_str);
1492 goto done;
1493
1494 } else if (re->fib_ng.nexthop) {
1495 /*
1496 * Free stale fib list and move on to check the rib nhg.
1497 */
1498 if (IS_ZEBRA_DEBUG_RIB)
1499 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1500 re->vrf_id, dest_str);
1501 nexthops_free(re->fib_ng.nexthop);
1502 re->fib_ng.nexthop = NULL;
efe6c026
MS
1503
1504 /* Note that the installed nexthops have changed */
1505 changed_p = true;
941e261c
MS
1506 } else {
1507 if (IS_ZEBRA_DEBUG_RIB)
1508 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1509 re->vrf_id, dest_str);
1510 }
1511
1512 /*
1513 * Compare with the rib nexthop group. The comparison here is different:
1514 * the RIB group may be a superset of the list installed in the FIB. We
1515 * walk the RIB group, looking for the 'installable' candidate
1516 * nexthops, and then check those against the set
1517 * that is actually installed.
1518 */
1519 matched = true;
5695d9ac 1520 for (ALL_NEXTHOPS(re->ng, nexthop)) {
78bf56b0 1521
5695d9ac 1522 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
78bf56b0
MS
1523 continue;
1524
941e261c
MS
1525 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1526 continue;
5695d9ac 1527
941e261c 1528 /* Check for a FIB nexthop corresponding to the RIB nexthop */
efe6c026 1529 ctx_nexthop = NULL;
941e261c 1530 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx), ctx_nexthop)) {
78bf56b0
MS
1531 if (nexthop_same(ctx_nexthop, nexthop))
1532 break;
1533 }
1534
5695d9ac
MS
1535 /* If the FIB doesn't know about the nexthop,
1536 * it's not installed
1537 */
1538 if (ctx_nexthop == NULL) {
efe6c026
MS
1539 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1540 nexthop2str(nexthop, nh_str, sizeof(nh_str));
1541 zlog_debug("update_from_ctx: no notif match for rib nh %s",
1542 nh_str);
1543 }
941e261c 1544 matched = false;
efe6c026
MS
1545
1546 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1547 changed_p = true;
1548
1549 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
941e261c 1550 break;
5695d9ac 1551 }
78bf56b0 1552
efe6c026
MS
1553 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB)) {
1554 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1555 changed_p = true;
1556
5695d9ac 1557 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
efe6c026
MS
1558 } else {
1559 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1560 changed_p = true;
1561
5695d9ac 1562 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
efe6c026 1563 }
78bf56b0
MS
1564 }
1565
941e261c
MS
1566 /* If all nexthops were processed, we're done */
1567 if (matched) {
1568 if (IS_ZEBRA_DEBUG_RIB)
efe6c026
MS
1569 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1570 re->vrf_id, dest_str,
1571 (changed_p ? "true" : "false"));
941e261c
MS
1572 goto done;
1573 }
78bf56b0 1574
941e261c
MS
1575 /* FIB nexthop set differs from the RIB set:
1576 * create a fib-specific nexthop-group
1577 */
1578 if (IS_ZEBRA_DEBUG_RIB)
efe6c026
MS
1579 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1580 re->vrf_id, dest_str,
1581 (changed_p ? "true" : "false"));
941e261c
MS
1582
1583 ctxnhg = dplane_ctx_get_ng(ctx);
188a00e0
MS
1584
1585 if (ctxnhg->nexthop)
1586 copy_nexthops(&(re->fib_ng.nexthop), ctxnhg->nexthop, NULL);
1587 else {
1588 /* Bit of a special case when the fib has _no_ installed
1589 * nexthops.
1590 */
1591 nexthop = nexthop_new();
1592 nexthop->type = NEXTHOP_TYPE_IPV4;
50d89650 1593 _nexthop_add(&(re->fib_ng.nexthop), nexthop);
188a00e0 1594 }
941e261c
MS
1595
1596done:
efe6c026 1597 return changed_p;
78bf56b0
MS
1598}
1599
e5ac2adf 1600/*
fad4d69c
MS
1601 * Helper to locate a zebra route-node from a dplane context. This is used
1602 * when processing dplane results, e.g. Note well: the route-node is returned
1603 * with a ref held - route_unlock_node() must be called eventually.
e5ac2adf 1604 */
fad4d69c
MS
1605static struct route_node *
1606rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
e5ac2adf
MS
1607{
1608 struct route_table *table = NULL;
1609 struct route_node *rn = NULL;
e5ac2adf
MS
1610 const struct prefix *dest_pfx, *src_pfx;
1611
1612 /* Locate rn and re(s) from ctx */
1613
1614 table = zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx),
1615 dplane_ctx_get_safi(ctx),
1616 dplane_ctx_get_vrf(ctx),
1617 dplane_ctx_get_table(ctx));
1618 if (table == NULL) {
1619 if (IS_ZEBRA_DEBUG_DPLANE) {
fad4d69c 1620 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
e5ac2adf
MS
1621 dplane_ctx_get_afi(ctx),
1622 dplane_ctx_get_safi(ctx),
1623 dplane_ctx_get_vrf(ctx));
1624 }
1625 goto done;
1626 }
1627
fad4d69c
MS
1628 dest_pfx = dplane_ctx_get_dest(ctx);
1629 src_pfx = dplane_ctx_get_src(ctx);
1630
1631 rn = srcdest_rnode_get(table, dest_pfx,
1632 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1633
1634done:
1635 return rn;
1636}
1637
1638
97f5b441 1639
fad4d69c
MS
1640/*
1641 * Route-update results processing after async dataplane update.
1642 */
1643static void rib_process_result(struct zebra_dplane_ctx *ctx)
1644{
1645 struct zebra_vrf *zvrf = NULL;
1646 struct route_node *rn = NULL;
1647 struct route_entry *re = NULL, *old_re = NULL, *rib;
1648 bool is_update = false;
1649 char dest_str[PREFIX_STRLEN] = "";
1650 enum dplane_op_e op;
1651 enum zebra_dplane_result status;
1652 const struct prefix *dest_pfx, *src_pfx;
1653 uint32_t seq;
efe6c026 1654 bool fib_changed = false;
97f5b441 1655
fad4d69c 1656 zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
e5ac2adf
MS
1657 dest_pfx = dplane_ctx_get_dest(ctx);
1658
1659 /* Note well: only capturing the prefix string if debug is enabled here;
1660 * unconditional log messages will have to generate the string.
1661 */
5709131c 1662 if (IS_ZEBRA_DEBUG_DPLANE)
e5ac2adf 1663 prefix2str(dest_pfx, dest_str, sizeof(dest_str));
e5ac2adf 1664
fad4d69c
MS
1665 /* Locate rn and re(s) from ctx */
1666 rn = rib_find_rn_from_ctx(ctx);
e5ac2adf
MS
1667 if (rn == NULL) {
1668 if (IS_ZEBRA_DEBUG_DPLANE) {
fe2c53d4 1669 zlog_debug("Failed to process dplane results: no route for %u:%s",
e5ac2adf
MS
1670 dplane_ctx_get_vrf(ctx), dest_str);
1671 }
1672 goto done;
1673 }
1674
1675 srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx);
1676
1677 op = dplane_ctx_get_op(ctx);
1678 status = dplane_ctx_get_status(ctx);
1679
c831033f 1680 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
f183e380 1681 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
e5ac2adf 1682 dplane_ctx_get_vrf(ctx), dest_str, ctx,
f183e380 1683 dplane_op2str(op), dplane_res2str(status));
e5ac2adf 1684
e5ac2adf
MS
1685 /*
1686 * Update is a bit of a special case, where we may have both old and new
1687 * routes to post-process.
1688 */
1689 is_update = dplane_ctx_is_update(ctx);
1690
1691 /*
1692 * Take a pass through the routes, look for matches with the context
1693 * info.
1694 */
1695 RNODE_FOREACH_RE(rn, rib) {
1696
1697 if (re == NULL) {
5709131c 1698 if (rib_route_match_ctx(rib, ctx, false))
e5ac2adf 1699 re = rib;
e5ac2adf
MS
1700 }
1701
1702 /* Check for old route match */
1703 if (is_update && (old_re == NULL)) {
5709131c 1704 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
e5ac2adf 1705 old_re = rib;
e5ac2adf
MS
1706 }
1707
1708 /* Have we found the routes we need to work on? */
5709131c 1709 if (re && ((!is_update || old_re)))
e5ac2adf 1710 break;
e5ac2adf
MS
1711 }
1712
699dae23
DS
1713 seq = dplane_ctx_get_seq(ctx);
1714
e5ac2adf
MS
1715 /*
1716 * Check sequence number(s) to detect stale results before continuing
1717 */
60f98b23 1718 if (re) {
699dae23 1719 if (re->dplane_sequence != seq) {
60f98b23
DS
1720 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1721 zlog_debug("%u:%s Stale dplane result for re %p",
1722 dplane_ctx_get_vrf(ctx),
1723 dest_str, re);
1724 } else
1725 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
1726 }
1727
60f98b23
DS
1728 if (old_re) {
1729 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
1730 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1731 zlog_debug("%u:%s Stale dplane result for old_re %p",
1732 dplane_ctx_get_vrf(ctx),
1733 dest_str, old_re);
1734 } else
9ef0c6ba 1735 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
1736 }
1737
12e7fe3a 1738 switch (op) {
12e7fe3a
DS
1739 case DPLANE_OP_ROUTE_INSTALL:
1740 case DPLANE_OP_ROUTE_UPDATE:
1741 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
1742 if (re) {
1743 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
1744 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1745 }
b9f0e5ee
DS
1746 /*
1747 * On an update operation from the same route type
1748 * context retrieval currently has no way to know
1749 * which was the old and which was the new.
1750 * So don't unset our flags that we just set.
1751 * We know redistribution is ok because the
1752 * old_re in this case is used for nothing
1753 * more than knowing whom to contact if necessary.
1754 */
1755 if (old_re && old_re != re) {
677c1dd5
DS
1756 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
1757 UNSET_FLAG(old_re->status,
1758 ROUTE_ENTRY_INSTALLED);
1759 }
f183e380 1760
78bf56b0 1761 /* Update zebra route based on the results in
941e261c 1762 * the context struct.
78bf56b0 1763 */
941e261c 1764 if (re) {
efe6c026
MS
1765 fib_changed =
1766 rib_update_re_from_ctx(re, rn, ctx);
1767
1768 if (!fib_changed) {
1769 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
1770 zlog_debug("%u:%s no fib change for re",
1771 dplane_ctx_get_vrf(
1772 ctx),
1773 dest_str);
12e7fe3a
DS
1774 }
1775
941e261c
MS
1776 /* Redistribute */
1777 redistribute_update(dest_pfx, src_pfx,
1778 re, NULL);
f183e380
MS
1779 }
1780
3cdba47a
DS
1781 /*
1782 * System routes are weird in that they
1783 * allow multiple to be installed that match
1784 * to the same prefix, so after we get the
1785 * result we need to clean them up so that
1786 * we can actually use them.
1787 */
1788 if ((re && RIB_SYSTEM_ROUTE(re)) ||
1789 (old_re && RIB_SYSTEM_ROUTE(old_re)))
1790 zebra_rib_fixup_system(rn);
1791
f4c6e2a8 1792 if (zvrf)
12e7fe3a 1793 zvrf->installs++;
f183e380 1794
12e7fe3a 1795 /* Notify route owner */
677c1dd5 1796 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
e5ac2adf 1797
12e7fe3a 1798 } else {
7a230a9d 1799 if (re) {
677c1dd5 1800 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
7a230a9d
DS
1801 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1802 } if (old_re)
677c1dd5
DS
1803 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
1804 if (re)
1805 zsend_route_notify_owner(re, dest_pfx,
1806 ZAPI_ROUTE_FAIL_INSTALL);
97f5b441 1807
12e7fe3a
DS
1808 zlog_warn("%u:%s: Route install failed",
1809 dplane_ctx_get_vrf(ctx),
1810 prefix2str(dest_pfx,
1811 dest_str, sizeof(dest_str)));
1812 }
1813 break;
1814 case DPLANE_OP_ROUTE_DELETE:
677c1dd5
DS
1815 if (re)
1816 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
1817 /*
1818 * In the delete case, the zebra core datastructs were
1819 * updated (or removed) at the time the delete was issued,
1820 * so we're just notifying the route owner.
e5ac2adf 1821 */
12e7fe3a 1822 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
1823 if (re) {
1824 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1825 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
1826 }
12e7fe3a 1827 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
e5ac2adf 1828
12e7fe3a
DS
1829 if (zvrf)
1830 zvrf->removals++;
1831 } else {
677c1dd5
DS
1832 if (re)
1833 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
1834 zsend_route_notify_owner_ctx(ctx,
1835 ZAPI_ROUTE_REMOVE_FAIL);
e5ac2adf 1836
12e7fe3a
DS
1837 zlog_warn("%u:%s: Route Deletion failure",
1838 dplane_ctx_get_vrf(ctx),
1839 prefix2str(dest_pfx,
1840 dest_str, sizeof(dest_str)));
1841 }
3cdba47a
DS
1842
1843 /*
1844 * System routes are weird in that they
1845 * allow multiple to be installed that match
1846 * to the same prefix, so after we get the
1847 * result we need to clean them up so that
1848 * we can actually use them.
1849 */
1850 if ((re && RIB_SYSTEM_ROUTE(re)) ||
1851 (old_re && RIB_SYSTEM_ROUTE(old_re)))
1852 zebra_rib_fixup_system(rn);
12e7fe3a 1853 break;
d37f4d6c
MS
1854 default:
1855 break;
e5ac2adf 1856 }
699dae23
DS
1857
1858 zebra_rib_evaluate_rn_nexthops(rn, seq);
a1494c25 1859 zebra_rib_evaluate_mpls(rn);
e5ac2adf
MS
1860done:
1861
5f27bcba
DS
1862 if (rn)
1863 route_unlock_node(rn);
1864
e5ac2adf
MS
1865 /* Return context to dataplane module */
1866 dplane_ctx_fini(&ctx);
1867}
1868
54818e3b
MS
1869/*
1870 * Handle notification from async dataplane: the dataplane has detected
1871 * some change to a route, and notifies zebra so that the control plane
1872 * can reflect that change.
1873 */
1874static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
1875{
1876 struct route_node *rn = NULL;
efe6c026
MS
1877 struct route_entry *re = NULL;
1878 struct nexthop *nexthop;
54818e3b
MS
1879 char dest_str[PREFIX_STRLEN] = "";
1880 const struct prefix *dest_pfx, *src_pfx;
941e261c 1881 rib_dest_t *dest;
efe6c026 1882 bool fib_changed = false;
941e261c 1883 bool debug_p = IS_ZEBRA_DEBUG_DPLANE | IS_ZEBRA_DEBUG_RIB;
efe6c026 1884 int start_count, end_count;
54818e3b
MS
1885 dest_pfx = dplane_ctx_get_dest(ctx);
1886
1887 /* Note well: only capturing the prefix string if debug is enabled here;
1888 * unconditional log messages will have to generate the string.
1889 */
941e261c 1890 if (debug_p)
54818e3b
MS
1891 prefix2str(dest_pfx, dest_str, sizeof(dest_str));
1892
1893 /* Locate rn and re(s) from ctx */
1894 rn = rib_find_rn_from_ctx(ctx);
1895 if (rn == NULL) {
941e261c
MS
1896 if (debug_p) {
1897 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
54818e3b
MS
1898 dplane_ctx_get_vrf(ctx), dest_str);
1899 }
1900 goto done;
1901 }
1902
941e261c 1903 dest = rib_dest_from_rnode(rn);
54818e3b
MS
1904 srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx);
1905
efe6c026 1906 if (debug_p)
54818e3b
MS
1907 zlog_debug("%u:%s Processing dplane notif ctx %p",
1908 dplane_ctx_get_vrf(ctx), dest_str, ctx);
1909
1910 /*
1911 * Take a pass through the routes, look for matches with the context
1912 * info.
1913 */
efe6c026
MS
1914 RNODE_FOREACH_RE(rn, re) {
1915 if (rib_route_match_ctx(re, ctx, false /*!update*/))
54818e3b
MS
1916 break;
1917 }
1918
1919 /* No match? Nothing we can do */
1920 if (re == NULL) {
941e261c
MS
1921 if (debug_p)
1922 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
54818e3b 1923 dplane_ctx_get_vrf(ctx), dest_str,
941e261c
MS
1924 zebra_route_string(
1925 dplane_ctx_get_type(ctx)));
54818e3b
MS
1926
1927 goto done;
1928 }
1929
941e261c
MS
1930 /* Is this a notification that ... matters? We only really care about
1931 * the route that is currently selected for installation.
1932 */
1933 if (re != dest->selected_fib) {
1934 /* TODO -- don't skip processing entirely? We might like to
1935 * at least report on the event.
1936 */
1937 if (debug_p)
efe6c026 1938 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
941e261c
MS
1939 dplane_ctx_get_vrf(ctx), dest_str,
1940 zebra_route_string(
1941 dplane_ctx_get_type(ctx)));
1942 goto done;
1943 }
1944
efe6c026
MS
1945 /* We'll want to determine whether the installation status of the
1946 * route has changed: we'll check the status before processing,
1947 * and then again if there's been a change.
1948 */
1949 start_count = 0;
1950 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re), nexthop)) {
1951 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1952 start_count++;
1953 }
1954
54818e3b
MS
1955 /* Update zebra's nexthop FIB flags based on the context struct's
1956 * nexthops.
1957 */
efe6c026 1958 fib_changed = rib_update_re_from_ctx(re, rn, ctx);
54818e3b 1959
efe6c026
MS
1960 if (!fib_changed) {
1961 if (debug_p)
54818e3b
MS
1962 zlog_debug("%u:%s No change from dplane notification",
1963 dplane_ctx_get_vrf(ctx), dest_str);
efe6c026
MS
1964
1965 goto done;
54818e3b
MS
1966 }
1967
188a00e0
MS
1968 /*
1969 * Perform follow-up work if the actual status of the prefix
efe6c026 1970 * changed.
941e261c
MS
1971 */
1972
efe6c026
MS
1973 end_count = 0;
1974 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re), nexthop)) {
1975 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
1976 end_count++;
1977 }
941e261c 1978
188a00e0
MS
1979 /* Various fib transitions: changed nexthops; from installed to
1980 * not-installed; or not-installed to installed.
efe6c026 1981 */
188a00e0
MS
1982 if (start_count > 0 && end_count > 0) {
1983
1984 /* Changed nexthops - update kernel/others */
1985 dplane_route_notif_update(rn, re,
1986 DPLANE_OP_ROUTE_UPDATE, ctx);
1987
1988 } else if (start_count == 0 && end_count > 0) {
efe6c026
MS
1989 if (debug_p)
1990 zlog_debug("%u:%s installed transition from dplane notification",
1991 dplane_ctx_get_vrf(ctx), dest_str);
1992
1993 /* We expect this to be the selected route, so we want
1994 * to tell others about this transistion.
1995 */
1996 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1997
188a00e0
MS
1998 /* Changed nexthops - update kernel/others */
1999 dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_INSTALL, ctx);
2000
efe6c026
MS
2001 /* Redistribute, lsp, and nht update */
2002 redistribute_update(dest_pfx, src_pfx, re, NULL);
2003
2004 zebra_rib_evaluate_rn_nexthops(
2005 rn, zebra_router_get_next_sequence());
2006
2007 zebra_rib_evaluate_mpls(rn);
2008
2009 } else if (start_count > 0 && end_count == 0) {
2010 if (debug_p)
2011 zlog_debug("%u:%s un-installed transition from dplane notification",
2012 dplane_ctx_get_vrf(ctx), dest_str);
2013
2014 /* Transition from _something_ installed to _nothing_
2015 * installed.
2016 */
2017 /* We expect this to be the selected route, so we want
2018 * to tell others about this transistion.
2019 */
2020 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2021
188a00e0
MS
2022 /* Changed nexthops - update kernel/others */
2023 dplane_route_notif_update(rn, re, DPLANE_OP_ROUTE_DELETE, ctx);
2024
efe6c026
MS
2025 /* Redistribute, lsp, and nht update */
2026 redistribute_delete(dest_pfx, src_pfx, re);
2027
2028 zebra_rib_evaluate_rn_nexthops(
2029 rn, zebra_router_get_next_sequence());
2030
2031 zebra_rib_evaluate_mpls(rn);
2032 }
941e261c 2033
54818e3b 2034done:
941e261c
MS
2035 if (rn)
2036 route_unlock_node(rn);
2037
54818e3b
MS
2038 /* Return context to dataplane module */
2039 dplane_ctx_fini(&ctx);
2040}
2041
5110a0c6 2042/* Take a list of route_node structs and return 1, if there was a record
d62a17ae 2043 * picked from it and processed by rib_process(). Don't process more,
5110a0c6 2044 * than one RN record; operate only in the specified sub-queue.
e96f9203 2045 */
d7c0a89a 2046static unsigned int process_subq(struct list *subq, uint8_t qindex)
e96f9203 2047{
d62a17ae 2048 struct listnode *lnode = listhead(subq);
2049 struct route_node *rnode;
2050 rib_dest_t *dest;
2051 struct zebra_vrf *zvrf = NULL;
5110a0c6 2052
d62a17ae 2053 if (!lnode)
2054 return 0;
5110a0c6 2055
d62a17ae 2056 rnode = listgetdata(lnode);
2057 dest = rib_dest_from_rnode(rnode);
2058 if (dest)
2059 zvrf = rib_dest_vrf(dest);
41ec9222 2060
d62a17ae 2061 rib_process(rnode);
5110a0c6 2062
d62a17ae 2063 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2064 char buf[SRCDEST2STR_BUFFER];
efe6c026 2065
d62a17ae 2066 srcdest_rnode2str(rnode, buf, sizeof(buf));
2067 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2068 zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
2069 }
41ec9222 2070
d62a17ae 2071 if (rnode->info)
2072 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2073 RIB_ROUTE_QUEUED(qindex));
9fd92e3c 2074
67b9467f 2075#if 0
5110a0c6
SH
2076 else
2077 {
2078 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2079 __func__, rnode, rnode->lock);
2080 zlog_backtrace(LOG_DEBUG);
2081 }
67b9467f 2082#endif
d62a17ae 2083 route_unlock_node(rnode);
2084 list_delete_node(subq, lnode);
2085 return 1;
e96f9203
DO
2086}
2087
a1494c25 2088
fb018d25 2089/*
f183e380 2090 * Perform next-hop tracking processing after RIB updates.
fb018d25 2091 */
f183e380 2092static void do_nht_processing(void)
fb018d25 2093{
fb018d25
DS
2094}
2095
e96f9203 2096/* Dispatch the meta queue by picking, processing and unlocking the next RN from
d62a17ae 2097 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2098 * data
e96f9203
DO
2099 * is pointed to the meta queue structure.
2100 */
d62a17ae 2101static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
e96f9203 2102{
d62a17ae 2103 struct meta_queue *mq = data;
2104 unsigned i;
91f16812
MS
2105 uint32_t queue_len, queue_limit;
2106
2107 /* Ensure there's room for more dataplane updates */
2108 queue_limit = dplane_get_in_queue_limit();
2109 queue_len = dplane_get_in_queue_len();
2110 if (queue_len > queue_limit) {
2111 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
8b962e77
MS
2112 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2113 queue_len, queue_limit);
91f16812
MS
2114
2115 /* Ensure that the meta-queue is actually enqueued */
489a9614 2116 if (work_queue_empty(zrouter.ribq))
ea45a4e7 2117 work_queue_add(zrouter.ribq, zrouter.mq);
91f16812
MS
2118
2119 return WQ_QUEUE_BLOCKED;
2120 }
5110a0c6 2121
d62a17ae 2122 for (i = 0; i < MQ_SIZE; i++)
2123 if (process_subq(mq->subq[i], i)) {
2124 mq->size--;
2125 break;
2126 }
2127 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
e96f9203
DO
2128}
2129
f52ed677
DS
2130
2131/*
2132 * Look into the RN and queue it into the highest priority queue
2133 * at this point in time for processing.
2134 *
2135 * We will enqueue a route node only once per invocation.
2136 *
2137 * There are two possibilities here that should be kept in mind.
2138 * If the original invocation has not been pulled off for processing
2139 * yet, A subsuquent invocation can have a route entry with a better
2140 * meta queue index value and we can have a situation where
2141 * we might have the same node enqueued 2 times. Not necessarily
2142 * an optimal situation but it should be ok.
2143 *
2144 * The other possibility is that the original invocation has not
2145 * been pulled off for processing yet, A subsusquent invocation
2146 * doesn't have a route_entry with a better meta-queue and the
2147 * original metaqueue index value will win and we'll end up with
2148 * the route node enqueued once.
e96f9203 2149 */
d62a17ae 2150static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
e96f9203 2151{
f52ed677
DS
2152 struct route_entry *re = NULL, *curr_re = NULL;
2153 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
5110a0c6 2154
f52ed677
DS
2155 RNODE_FOREACH_RE (rn, curr_re) {
2156 curr_qindex = route_info[curr_re->type].meta_q_map;
d62a17ae 2157
f52ed677
DS
2158 if (curr_qindex <= qindex) {
2159 re = curr_re;
2160 qindex = curr_qindex;
d62a17ae 2161 }
f52ed677 2162 }
5110a0c6 2163
f52ed677
DS
2164 if (!re)
2165 return;
5110a0c6 2166
f52ed677
DS
2167 /* Invariant: at this point we always have rn->info set. */
2168 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2169 RIB_ROUTE_QUEUED(qindex))) {
d62a17ae 2170 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2171 rnode_debug(rn, re->vrf_id,
f52ed677 2172 "rn %p is already queued in sub-queue %u",
d62a17ae 2173 (void *)rn, qindex);
f52ed677 2174 return;
d62a17ae 2175 }
f52ed677
DS
2176
2177 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
2178 listnode_add(mq->subq[qindex], rn);
2179 route_lock_node(rn);
2180 mq->size++;
2181
2182 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2183 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %u",
2184 (void *)rn, qindex);
4d38fdb4 2185}
2186
6d691129 2187/* Add route_node to work queue and schedule processing */
d62a17ae 2188void rib_queue_add(struct route_node *rn)
4d38fdb4 2189{
d62a17ae 2190 assert(rn);
fc328ac9 2191
d62a17ae 2192 /* Pointless to queue a route_node with no RIB entries to add or remove
2193 */
2194 if (!rnode_to_ribs(rn)) {
2195 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2196 __func__, (void *)rn, rn->lock);
2197 zlog_backtrace(LOG_DEBUG);
2198 return;
2199 }
4d38fdb4 2200
489a9614 2201 if (zrouter.ribq == NULL) {
e914ccbe 2202 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2203 "%s: work_queue does not exist!", __func__);
d62a17ae 2204 return;
2205 }
2206
2207 /*
2208 * The RIB queue should normally be either empty or holding the only
2209 * work_queue_item element. In the latter case this element would
2210 * hold a pointer to the meta queue structure, which must be used to
2211 * actually queue the route nodes to process. So create the MQ
2212 * holder, if necessary, then push the work into it in any case.
2213 * This semantics was introduced after 0.99.9 release.
2214 */
489a9614 2215 if (work_queue_empty(zrouter.ribq))
ea45a4e7 2216 work_queue_add(zrouter.ribq, zrouter.mq);
e96f9203 2217
ea45a4e7 2218 rib_meta_queue_add(zrouter.mq, rn);
fc328ac9 2219
d62a17ae 2220 return;
4d38fdb4 2221}
2222
5110a0c6
SH
2223/* Create new meta queue.
2224 A destructor function doesn't seem to be necessary here.
2225 */
d62a17ae 2226static struct meta_queue *meta_queue_new(void)
e96f9203 2227{
d62a17ae 2228 struct meta_queue *new;
2229 unsigned i;
5110a0c6 2230
d62a17ae 2231 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
e96f9203 2232
d62a17ae 2233 for (i = 0; i < MQ_SIZE; i++) {
2234 new->subq[i] = list_new();
2235 assert(new->subq[i]);
2236 }
5110a0c6 2237
d62a17ae 2238 return new;
e96f9203
DO
2239}
2240
d62a17ae 2241void meta_queue_free(struct meta_queue *mq)
5a8dfcd8 2242{
d62a17ae 2243 unsigned i;
5a8dfcd8 2244
d62a17ae 2245 for (i = 0; i < MQ_SIZE; i++)
6a154c88 2246 list_delete(&mq->subq[i]);
5a8dfcd8 2247
d62a17ae 2248 XFREE(MTYPE_WORK_QUEUE, mq);
5a8dfcd8
RW
2249}
2250
4d38fdb4 2251/* initialise zebra rib work queue */
2561d12e 2252static void rib_queue_init(void)
4d38fdb4 2253{
489a9614
DS
2254 if (!(zrouter.ribq = work_queue_new(zrouter.master,
2255 "route_node processing"))) {
e914ccbe 2256 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2257 "%s: could not initialise work queue!", __func__);
d62a17ae 2258 return;
2259 }
4d38fdb4 2260
d62a17ae 2261 /* fill in the work queue spec */
489a9614
DS
2262 zrouter.ribq->spec.workfunc = &meta_queue_process;
2263 zrouter.ribq->spec.errorfunc = NULL;
46a4e345 2264 zrouter.ribq->spec.completion_func = NULL;
d62a17ae 2265 /* XXX: TODO: These should be runtime configurable via vty */
489a9614
DS
2266 zrouter.ribq->spec.max_retries = 3;
2267 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
2268 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
d62a17ae 2269
ea45a4e7 2270 if (!(zrouter.mq = meta_queue_new())) {
e914ccbe 2271 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2272 "%s: could not initialise meta queue!", __func__);
d62a17ae 2273 return;
2274 }
2275 return;
718e3744 2276}
2277
434434f7
DS
2278rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
2279{
2280 rib_dest_t *dest;
2281
2282 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
aa57abfb 2283 rnh_list_init(&dest->nht);
434434f7
DS
2284 route_lock_node(rn); /* rn route table reference */
2285 rn->info = dest;
2286 dest->rnode = rn;
2287
2288 return dest;
2289}
2290
6d691129
PJ
2291/* RIB updates are processed via a queue of pointers to route_nodes.
2292 *
2293 * The queue length is bounded by the maximal size of the routing table,
2294 * as a route_node will not be requeued, if already queued.
2295 *
f0f77c9a
DS
2296 * REs are submitted via rib_addnode or rib_delnode which set minimal
2297 * state, or static_install_route (when an existing RE is updated)
3c0755dc 2298 * and then submit route_node to queue for best-path selection later.
f0f77c9a 2299 * Order of add/delete state changes are preserved for any given RE.
6d691129 2300 *
f0f77c9a 2301 * Deleted REs are reaped during best-path selection.
6d691129
PJ
2302 *
2303 * rib_addnode
f0f77c9a
DS
2304 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2305 * |-------->| | best RE, if required
3c0755dc
PJ
2306 * | |
2307 * static_install->|->rib_addqueue...... -> rib_process
2308 * | |
2309 * |-------->| |-> rib_unlink
f0f77c9a
DS
2310 * |-> set ROUTE_ENTRY_REMOVE |
2311 * rib_delnode (RE freed)
6d691129 2312 *
9fd92e3c
AS
2313 * The 'info' pointer of a route_node points to a rib_dest_t
2314 * ('dest'). Queueing state for a route_node is kept on the dest. The
2315 * dest is created on-demand by rib_link() and is kept around at least
2316 * as long as there are ribs hanging off it (@see rib_gc_dest()).
d62a17ae 2317 *
6d691129
PJ
2318 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2319 *
2320 * - route_nodes: refcounted by:
9fd92e3c
AS
2321 * - dest attached to route_node:
2322 * - managed by: rib_link/rib_gc_dest
6d691129
PJ
2323 * - route_node processing queue
2324 * - managed by: rib_addqueue, rib_process.
2325 *
2326 */
d62a17ae 2327
f0f77c9a 2328/* Add RE to head of the route node. */
d62a17ae 2329static void rib_link(struct route_node *rn, struct route_entry *re, int process)
2330{
d62a17ae 2331 rib_dest_t *dest;
2332 afi_t afi;
2333 const char *rmap_name;
9fd92e3c 2334
d62a17ae 2335 assert(re && rn);
9fd92e3c 2336
d62a17ae 2337 dest = rib_dest_from_rnode(rn);
2338 if (!dest) {
2339 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2340 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
7a4bb9c5 2341
434434f7 2342 dest = zebra_rib_create_dest(rn);
d62a17ae 2343 }
2263a412 2344
045207e2 2345 re_list_add_head(&dest->routes, re);
d62a17ae 2346
2347 afi = (rn->p.family == AF_INET)
2348 ? AFI_IP
2349 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
fe257ae7
DS
2350 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
2351 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
2352
d62a17ae 2353 rmap_name = zebra_get_import_table_route_map(afi, re->table);
fe257ae7 2354 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
d62a17ae 2355 } else if (process)
2356 rib_queue_add(rn);
2357}
2358
7e24fdf3
DS
2359static void rib_addnode(struct route_node *rn,
2360 struct route_entry *re, int process)
d62a17ae 2361{
2362 /* RE node has been un-removed before route-node is processed.
2363 * route_node must hence already be on the queue for processing..
2364 */
2365 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2366 if (IS_ZEBRA_DEBUG_RIB)
2367 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
2368 (void *)rn, (void *)re);
2369
2370 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2371 return;
2372 }
2373 rib_link(rn, re, process);
6d691129
PJ
2374}
2375
9fd92e3c
AS
2376/*
2377 * rib_unlink
2378 *
2379 * Detach a rib structure from a route_node.
2380 *
2381 * Note that a call to rib_unlink() should be followed by a call to
2382 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2383 * longer required to be deleted.
2384 */
d62a17ae 2385void rib_unlink(struct route_node *rn, struct route_entry *re)
6d691129 2386{
d62a17ae 2387 rib_dest_t *dest;
9fd92e3c 2388
d62a17ae 2389 assert(rn && re);
6d691129 2390
d62a17ae 2391 if (IS_ZEBRA_DEBUG_RIB)
2392 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
2393 (void *)re);
6d691129 2394
d62a17ae 2395 dest = rib_dest_from_rnode(rn);
6d691129 2396
045207e2 2397 re_list_del(&dest->routes, re);
7a4bb9c5 2398
2eb07de3
DS
2399 if (dest->selected_fib == re)
2400 dest->selected_fib = NULL;
2401
7ee30f28 2402 nexthops_free(re->ng.nexthop);
ee5e8a48
MS
2403 nexthops_free(re->fib_ng.nexthop);
2404
d62a17ae 2405 XFREE(MTYPE_RE, re);
2406}
2407
2408void rib_delnode(struct route_node *rn, struct route_entry *re)
2409{
2410 afi_t afi;
2411
2412 if (IS_ZEBRA_DEBUG_RIB)
2413 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
2414 (void *)rn, (void *)re);
2415 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2416
2417 afi = (rn->p.family == AF_INET)
2418 ? AFI_IP
2419 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
fe257ae7
DS
2420 if (is_zebra_import_table_enabled(afi, re->vrf_id, re->table)) {
2421 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
2422
2423 zebra_del_import_table_entry(zvrf, rn, re);
d62a17ae 2424 /* Just clean up if non main table */
2425 if (IS_ZEBRA_DEBUG_RIB) {
2426 char buf[SRCDEST2STR_BUFFER];
2427 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
2428 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2429 re->vrf_id, buf, rn, re,
2430 zebra_route_string(re->type));
d62a17ae 2431 }
7a4bb9c5 2432
d62a17ae 2433 rib_unlink(rn, re);
2434 } else {
2435 rib_queue_add(rn);
2436 }
718e3744 2437}
2438
f0f77c9a 2439/* This function dumps the contents of a given RE entry into
dc95824a
DO
2440 * standard debug log. Calling function name and IP prefix in
2441 * question are passed as 1st and 2nd arguments.
2442 */
2443
d62a17ae 2444void _route_entry_dump(const char *func, union prefixconstptr pp,
2445 union prefixconstptr src_pp,
2446 const struct route_entry *re)
2447{
d62a17ae 2448 const struct prefix *src_p = src_pp.p;
2449 bool is_srcdst = src_p && src_p->prefixlen;
2450 char straddr[PREFIX_STRLEN];
2451 char srcaddr[PREFIX_STRLEN];
53c16fbe 2452 char nhname[PREFIX_STRLEN];
d62a17ae 2453 struct nexthop *nexthop;
2454
2455 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
2456 (const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
2457 is_srcdst ? " from " : "",
2458 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
2459 : "",
2460 re->vrf_id);
cc54cfee 2461 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
53c16fbe 2462 straddr, (unsigned long)re->uptime, re->type, re->instance,
cc54cfee 2463 re->table);
d62a17ae 2464 zlog_debug(
2465 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
53c16fbe
DS
2466 straddr, re->metric, re->mtu, re->distance, re->flags, re->status);
2467 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr,
d62a17ae 2468 re->nexthop_num, re->nexthop_active_num);
2469
7ee30f28 2470 for (ALL_NEXTHOPS(re->ng, nexthop)) {
2d68a0f2
DS
2471 struct interface *ifp;
2472 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
2473
2474 switch (nexthop->type) {
2475 case NEXTHOP_TYPE_BLACKHOLE:
53c16fbe 2476 sprintf(nhname, "Blackhole");
2d68a0f2
DS
2477 break;
2478 case NEXTHOP_TYPE_IFINDEX:
2479 ifp = if_lookup_by_index(nexthop->ifindex,
2480 nexthop->vrf_id);
53c16fbe 2481 sprintf(nhname, "%s", ifp ? ifp->name : "Unknown");
2d68a0f2
DS
2482 break;
2483 case NEXTHOP_TYPE_IPV4:
2484 /* fallthrough */
2485 case NEXTHOP_TYPE_IPV4_IFINDEX:
53c16fbe 2486 inet_ntop(AF_INET, &nexthop->gate, nhname,
2d68a0f2
DS
2487 INET6_ADDRSTRLEN);
2488 break;
2489 case NEXTHOP_TYPE_IPV6:
2490 case NEXTHOP_TYPE_IPV6_IFINDEX:
53c16fbe 2491 inet_ntop(AF_INET6, &nexthop->gate, nhname,
2d68a0f2
DS
2492 INET6_ADDRSTRLEN);
2493 break;
2494 }
df38b099 2495 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
53c16fbe 2496 straddr, (nexthop->rparent ? " NH" : "NH"), nhname,
2d68a0f2
DS
2497 nexthop->ifindex, vrf ? vrf->name : "Unknown",
2498 nexthop->vrf_id,
d62a17ae 2499 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
2500 ? "ACTIVE "
2501 : ""),
677c1dd5 2502 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
d62a17ae 2503 ? "FIB "
2504 : ""),
2505 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
df38b099
DS
2506 ? "RECURSIVE "
2507 : ""),
2508 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
2509 ? "ONLINK "
2510 : ""),
2511 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_MATCHED)
2512 ? "MATCHED "
2513 : ""),
2514 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
2515 ? "DUPLICATE "
d62a17ae 2516 : ""));
2517 }
53c16fbe 2518 zlog_debug("%s: dump complete", straddr);
dc95824a
DO
2519}
2520
2521/* This is an exported helper to rtm_read() to dump the strange
f0f77c9a 2522 * RE entry found by rib_lookup_ipv4_route()
dc95824a
DO
2523 */
2524
d62a17ae 2525void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2526{
2527 struct route_table *table;
2528 struct route_node *rn;
2529 struct route_entry *re;
2530 char prefix_buf[INET_ADDRSTRLEN];
2531
2532 /* Lookup table. */
2533 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2534 if (!table) {
e914ccbe 2535 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2536 "%s:%u zebra_vrf_table() returned NULL", __func__,
2537 vrf_id);
d62a17ae 2538 return;
2539 }
2540
2541 /* Scan the RIB table for exactly matching RE entry. */
2542 rn = route_node_lookup(table, (struct prefix *)p);
2543
2544 /* No route for this prefix. */
2545 if (!rn) {
32391aff 2546 zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
d62a17ae 2547 prefix2str((struct prefix *)p, prefix_buf,
2548 sizeof(prefix_buf)));
2549 return;
2550 }
2551
2552 /* Unlock node. */
2553 route_unlock_node(rn);
2554
2555 /* let's go */
a2addae8 2556 RNODE_FOREACH_RE (rn, re) {
32391aff
DS
2557 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2558 __func__, vrf_id,
2559 (void *)rn, (void *)re,
d62a17ae 2560 (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
2561 ? "removed"
2562 : "NOT removed"),
2563 (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
2564 ? "selected"
2565 : "NOT selected"));
2566 route_entry_dump(p, NULL, re);
2567 }
dc95824a
DO
2568}
2569
20e5ff0a
DO
2570/* Check if requested address assignment will fail due to another
2571 * route being installed by zebra in FIB already. Take necessary
2572 * actions, if needed: remove such a route from FIB and deSELECT
f0f77c9a 2573 * corresponding RE entry. Then put affected RN into RIBQ head.
20e5ff0a 2574 */
d62a17ae 2575void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2576{
2577 struct route_table *table;
2578 struct route_node *rn;
5f7a4718 2579 rib_dest_t *dest;
d62a17ae 2580
2581 if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
e914ccbe 2582 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2583 "%s:%u zebra_vrf_table() returned NULL", __func__,
2584 vrf_id);
d62a17ae 2585 return;
2586 }
2587
2588 /* No matches would be the simplest case. */
2589 if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
2590 return;
2591
2592 /* Unlock node. */
2593 route_unlock_node(rn);
2594
5f7a4718 2595 dest = rib_dest_from_rnode(rn);
d62a17ae 2596 /* Check all RE entries. In case any changes have to be done, requeue
2597 * the RN into RIBQ head. If the routing message about the new connected
2598 * route (generated by the IP address we are going to assign very soon)
2599 * comes before the RIBQ is processed, the new RE entry will join
2600 * RIBQ record already on head. This is necessary for proper
2601 * revalidation
2602 * of the rest of the RE.
2603 */
3cdba47a 2604 if (dest->selected_fib) {
5f7a4718
DS
2605 if (IS_ZEBRA_DEBUG_RIB) {
2606 char buf[PREFIX_STRLEN];
2607
2608 zlog_debug("%u:%s: freeing way for connected prefix",
2609 dest->selected_fib->vrf_id,
2610 prefix2str(&rn->p, buf, sizeof(buf)));
2611 route_entry_dump(&rn->p, NULL, dest->selected_fib);
d62a17ae 2612 }
5f7a4718 2613 rib_uninstall(rn, dest->selected_fib);
d62a17ae 2614 rib_queue_add(rn);
ade4a886 2615 }
20e5ff0a
DO
2616}
2617
d62a17ae 2618int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
2619 struct prefix_ipv6 *src_p, struct route_entry *re)
718e3744 2620{
d62a17ae 2621 struct route_table *table;
2622 struct route_node *rn;
40ecd8e4 2623 struct route_entry *same = NULL;
d62a17ae 2624 int ret = 0;
b4c034b0 2625
d62a17ae 2626 if (!re)
2627 return 0;
b4c034b0 2628
1f610a1f 2629 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
05737783 2630
d62a17ae 2631 /* Lookup table. */
7865c65d
RW
2632 table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
2633 if (!table) {
2634 XFREE(MTYPE_RE, re);
d62a17ae 2635 return 0;
7865c65d 2636 }
cddf391b 2637
d62a17ae 2638 /* Make it sure prefixlen is applied to the prefix. */
2639 apply_mask(p);
2640 if (src_p)
2641 apply_mask_ipv6(src_p);
718e3744 2642
d62a17ae 2643 /* Set default distance by route type. */
eea2ef07 2644 if (re->distance == 0)
0492eea0 2645 re->distance = route_distance(re->type);
718e3744 2646
d62a17ae 2647 /* Lookup route node.*/
2648 rn = srcdest_rnode_get(table, p, src_p);
718e3744 2649
40ecd8e4
DS
2650 /*
2651 * If same type of route are installed, treat it as a implicit
2652 * withdraw.
2653 * If the user has specified the No route replace semantics
2654 * for the install don't do a route replace.
2655 */
a2addae8 2656 RNODE_FOREACH_RE (rn, same) {
d62a17ae 2657 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
2658 continue;
41ec9222 2659
eb327fa5
RW
2660 if (same->type != re->type)
2661 continue;
2662 if (same->instance != re->instance)
2663 continue;
996c9314
LB
2664 if (same->type == ZEBRA_ROUTE_KERNEL
2665 && same->metric != re->metric)
eb327fa5 2666 continue;
40ecd8e4
DS
2667
2668 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2669 same->distance != re->distance)
2670 continue;
2671
844b3a87 2672 /*
40ecd8e4
DS
2673 * We should allow duplicate connected routes
2674 * because of IPv6 link-local routes and unnumbered
2675 * interfaces on Linux.
844b3a87
RW
2676 */
2677 if (same->type != ZEBRA_ROUTE_CONNECT)
d62a17ae 2678 break;
2679 }
718e3744 2680
cf363e1b
MS
2681 /* If this route is kernel/connected route, notify the dataplane. */
2682 if (RIB_SYSTEM_ROUTE(re)) {
2683 /* Notify dataplane */
2684 dplane_sys_route_add(rn, re);
2685 }
2686
d62a17ae 2687 /* Link new re to node.*/
2688 if (IS_ZEBRA_DEBUG_RIB) {
2da33d6b
DS
2689 rnode_debug(rn, re->vrf_id,
2690 "Inserting route rn %p, re %p (%s) existing %p",
2691 rn, re, zebra_route_string(re->type), same);
718e3744 2692
d62a17ae 2693 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2694 route_entry_dump(p, src_p, re);
718e3744 2695 }
6883bf8d
DS
2696
2697 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
d62a17ae 2698 rib_addnode(rn, re, 1);
2699 ret = 1;
6b0655a2 2700
d62a17ae 2701 /* Free implicit route.*/
2702 if (same) {
2703 rib_delnode(rn, same);
2704 ret = -1;
2705 }
718e3744 2706
d62a17ae 2707 route_unlock_node(rn);
2708 return ret;
2709}
2710
2711void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
d7c0a89a 2712 unsigned short instance, int flags, struct prefix *p,
fd36be7e 2713 struct prefix_ipv6 *src_p, const struct nexthop *nh,
40ecd8e4
DS
2714 uint32_t table_id, uint32_t metric, uint8_t distance,
2715 bool fromkernel)
d62a17ae 2716{
2717 struct route_table *table;
2718 struct route_node *rn;
2719 struct route_entry *re;
2720 struct route_entry *fib = NULL;
2721 struct route_entry *same = NULL;
fd36be7e 2722 struct nexthop *rtnh;
d62a17ae 2723 char buf2[INET6_ADDRSTRLEN];
5f7a4718 2724 rib_dest_t *dest;
d62a17ae 2725
1f610a1f 2726 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
d62a17ae 2727
2728 /* Lookup table. */
2729 table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
2730 if (!table)
2731 return;
2732
2733 /* Apply mask. */
2734 apply_mask(p);
2735 if (src_p)
2736 apply_mask_ipv6(src_p);
2737
2738 /* Lookup route node. */
2739 rn = srcdest_rnode_lookup(table, p, src_p);
2740 if (!rn) {
2741 char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
2742
2743 prefix2str(p, dst_buf, sizeof(dst_buf));
2744 if (src_p && src_p->prefixlen)
2745 prefix2str(src_p, src_buf, sizeof(src_buf));
2746 else
2747 src_buf[0] = '\0';
2748
82d7d0e2
DS
2749 if (IS_ZEBRA_DEBUG_RIB) {
2750 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
2751
2752 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
2753 vrf->name, table_id, dst_buf,
d62a17ae 2754 (src_buf[0] != '\0') ? " from " : "",
2755 src_buf);
82d7d0e2 2756 }
d62a17ae 2757 return;
2758 }
718e3744 2759
5f7a4718
DS
2760 dest = rib_dest_from_rnode(rn);
2761 fib = dest->selected_fib;
2762
d62a17ae 2763 /* Lookup same type route. */
a2addae8 2764 RNODE_FOREACH_RE (rn, re) {
d62a17ae 2765 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2766 continue;
2767
d62a17ae 2768 if (re->type != type)
2769 continue;
2770 if (re->instance != instance)
2771 continue;
40ecd8e4
DS
2772 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2773 distance != re->distance)
2774 continue;
2775
996c9314 2776 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
f19435a8 2777 continue;
7ee30f28 2778 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
fd36be7e
DL
2779 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2780 if (rtnh->ifindex != nh->ifindex)
d62a17ae 2781 continue;
d62a17ae 2782 same = re;
2783 break;
2784 }
2785 /* Make sure that the route found has the same gateway. */
2786 else {
fd36be7e 2787 if (nh == NULL) {
d62a17ae 2788 same = re;
2789 break;
2790 }
7ee30f28 2791 for (ALL_NEXTHOPS(re->ng, rtnh))
a5a2d802
SW
2792 /*
2793 * No guarantee all kernel send nh with labels
2794 * on delete.
2795 */
2796 if (nexthop_same_no_labels(rtnh, nh)) {
d62a17ae 2797 same = re;
2798 break;
2799 }
2800 if (same)
2801 break;
2802 }
2803 }
2804 /* If same type of route can't be found and this message is from
2805 kernel. */
2806 if (!same) {
5dfeba19
DS
2807 /*
2808 * In the past(HA!) we could get here because
2809 * we were receiving a route delete from the
2810 * kernel and we're not marking the proto
2811 * as coming from it's appropriate originator.
2812 * Now that we are properly noticing the fact
2813 * that the kernel has deleted our route we
2814 * are not going to get called in this path
2815 * I am going to leave this here because
2816 * this might still work this way on non-linux
2817 * platforms as well as some weird state I have
2818 * not properly thought of yet.
2819 * If we can show that this code path is
2820 * dead then we can remove it.
2821 */
b8faa875 2822 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
d62a17ae 2823 if (IS_ZEBRA_DEBUG_RIB) {
2da33d6b
DS
2824 rnode_debug(rn, vrf_id,
2825 "rn %p, re %p (%s) was deleted from kernel, adding",
2826 rn, fib,
2827 zebra_route_string(fib->type));
d62a17ae 2828 }
2829 if (allow_delete) {
677c1dd5 2830 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
d62a17ae 2831 /* Unset flags. */
7ee30f28 2832 for (rtnh = fib->ng.nexthop; rtnh;
fd36be7e
DL
2833 rtnh = rtnh->next)
2834 UNSET_FLAG(rtnh->flags,
d62a17ae 2835 NEXTHOP_FLAG_FIB);
2836
ed216282
DS
2837 /*
2838 * This is a non FRR route
2839 * as such we should mark
2840 * it as deleted
2841 */
5f7a4718 2842 dest->selected_fib = NULL;
d62a17ae 2843 } else {
2844 /* This means someone else, other than Zebra,
2845 * has deleted
2846 * a Zebra router from the kernel. We will add
2847 * it back */
2848 rib_install_kernel(rn, fib, NULL);
2849 }
2850 } else {
2851 if (IS_ZEBRA_DEBUG_RIB) {
fd36be7e 2852 if (nh)
d62a17ae 2853 rnode_debug(
2854 rn, vrf_id,
2855 "via %s ifindex %d type %d "
2856 "doesn't exist in rib",
36228974 2857 inet_ntop(afi2family(afi),
2858 &nh->gate, buf2,
2859 sizeof(buf2)),
2860 nh->ifindex, type);
d62a17ae 2861 else
2862 rnode_debug(
2863 rn, vrf_id,
fd36be7e
DL
2864 "type %d doesn't exist in rib",
2865 type);
d62a17ae 2866 }
2867 route_unlock_node(rn);
2868 return;
2869 }
2870 }
718e3744 2871
5dfeba19 2872 if (same) {
996c9314
LB
2873 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
2874 && !allow_delete) {
5dfeba19
DS
2875 rib_install_kernel(rn, same, NULL);
2876 route_unlock_node(rn);
2877
2878 return;
2879 }
6134fd82 2880
2b83602b 2881 /* Special handling for IPv4 or IPv6 routes sourced from
2882 * EVPN - the nexthop (and associated MAC) need to be
2883 * uninstalled if no more refs.
2884 */
90264d64 2885 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
6134fd82 2886 struct nexthop *tmp_nh;
2887
7ee30f28 2888 for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
6134fd82 2889 struct ipaddr vtep_ip;
2890
2891 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1ec31309 2892 if (afi == AFI_IP) {
2893 vtep_ip.ipa_type = IPADDR_V4;
2894 memcpy(&(vtep_ip.ipaddr_v4),
2895 &(tmp_nh->gate.ipv4),
2896 sizeof(struct in_addr));
2897 } else {
2898 vtep_ip.ipa_type = IPADDR_V6;
2899 memcpy(&(vtep_ip.ipaddr_v6),
2900 &(tmp_nh->gate.ipv6),
2901 sizeof(struct in6_addr));
2902 }
a317a9b9 2903 zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
6134fd82 2904 &vtep_ip, p);
2905 }
2906 }
cf363e1b
MS
2907
2908 /* Notify dplane if system route changes */
2909 if (RIB_SYSTEM_ROUTE(re))
2910 dplane_sys_route_del(rn, same);
2911
d62a17ae 2912 rib_delnode(rn, same);
5dfeba19 2913 }
05737783 2914
d62a17ae 2915 route_unlock_node(rn);
2916 return;
2917}
718e3744 2918
718e3744 2919
d7c0a89a
QY
2920int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2921 unsigned short instance, int flags, struct prefix *p,
2922 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2923 uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
2924 route_tag_t tag)
d62a17ae 2925{
2926 struct route_entry *re;
66af6845 2927 struct nexthop *nexthop;
718e3744 2928
66af6845 2929 /* Allocate new route_entry structure. */
d62a17ae 2930 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
d62a17ae 2931 re->type = type;
2932 re->instance = instance;
2933 re->distance = distance;
2934 re->flags = flags;
2935 re->metric = metric;
2936 re->mtu = mtu;
2937 re->table = table_id;
2938 re->vrf_id = vrf_id;
2939 re->nexthop_num = 0;
98572489 2940 re->uptime = monotime(NULL);
4e40b6d6 2941 re->tag = tag;
d62a17ae 2942
66af6845
RW
2943 /* Add nexthop. */
2944 nexthop = nexthop_new();
2945 *nexthop = *nh;
2946 route_entry_nexthop_add(re, nexthop);
718e3744 2947
66af6845 2948 return rib_add_multipath(afi, safi, p, src_p, re);
718e3744 2949}
2950
1c848137 2951/* Schedule routes of a particular table (address-family) based on event. */
d5b8c216 2952void rib_update_table(struct route_table *table, rib_update_event_t event)
d62a17ae 2953{
2954 struct route_node *rn;
2955 struct route_entry *re, *next;
2956
2957 /* Walk all routes and queue for processing, if appropriate for
2958 * the trigger event.
2959 */
2960 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
1ca60f2c
DS
2961 /*
2962 * If we are looking at a route node and the node
2963 * has already been queued we don't
2964 * need to queue it up again
2965 */
996c9314
LB
2966 if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2967 RIB_ROUTE_ANY_QUEUED))
1ca60f2c 2968 continue;
d62a17ae 2969 switch (event) {
2970 case RIB_UPDATE_IF_CHANGE:
2971 /* Examine all routes that won't get processed by the
2972 * protocol or
2973 * triggered by nexthop evaluation (NHT). This would be
2974 * system,
2975 * kernel and certain static routes. Note that NHT will
2976 * get
2977 * triggered upon an interface event as connected routes
2978 * always
2979 * get queued for processing.
2980 */
a2addae8 2981 RNODE_FOREACH_RE_SAFE (rn, re, next) {
0a16efff
DS
2982 struct nexthop *nh;
2983
996c9314
LB
2984 if (re->type != ZEBRA_ROUTE_SYSTEM
2985 && re->type != ZEBRA_ROUTE_KERNEL
2986 && re->type != ZEBRA_ROUTE_CONNECT
2987 && re->type != ZEBRA_ROUTE_STATIC)
0a16efff
DS
2988 continue;
2989
2990 if (re->type != ZEBRA_ROUTE_STATIC) {
ade4a886
DS
2991 SET_FLAG(re->status,
2992 ROUTE_ENTRY_CHANGED);
0a16efff
DS
2993 rib_queue_add(rn);
2994 continue;
2995 }
2996
7ee30f28 2997 for (nh = re->ng.nexthop; nh; nh = nh->next)
0a16efff
DS
2998 if (!(nh->type == NEXTHOP_TYPE_IPV4
2999 || nh->type == NEXTHOP_TYPE_IPV6))
3000 break;
3001
3002 /* If we only have nexthops to a
3003 * gateway, NHT will
3004 * take care.
3005 */
ade4a886
DS
3006 if (nh) {
3007 SET_FLAG(re->status,
3008 ROUTE_ENTRY_CHANGED);
d62a17ae 3009 rib_queue_add(rn);
ade4a886 3010 }
d62a17ae 3011 }
3012 break;
3013
3014 case RIB_UPDATE_RMAP_CHANGE:
3015 case RIB_UPDATE_OTHER:
3016 /* Right now, examine all routes. Can restrict to a
3017 * protocol in
3018 * some cases (TODO).
3019 */
ade4a886
DS
3020 if (rnode_to_ribs(rn)) {
3021 RNODE_FOREACH_RE_SAFE (rn, re, next)
3022 SET_FLAG(re->status,
3023 ROUTE_ENTRY_CHANGED);
d62a17ae 3024 rib_queue_add(rn);
ade4a886 3025 }
d62a17ae 3026 break;
3027
3028 default:
3029 break;
3030 }
3031 }
b84c7253 3032}
3033
718e3744 3034/* RIB update function. */
d62a17ae 3035void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
718e3744 3036{
d62a17ae 3037 struct route_table *table;
1c848137 3038
d62a17ae 3039 /* Process routes of interested address-families. */
3040 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
d5b8c216 3041 if (table) {
3042 if (IS_ZEBRA_DEBUG_EVENT)
3043 zlog_debug("%s : AFI_IP event %d", __func__, event);
d62a17ae 3044 rib_update_table(table, event);
d5b8c216 3045 }
718e3744 3046
d62a17ae 3047 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
d5b8c216 3048 if (table) {
3049 if (IS_ZEBRA_DEBUG_EVENT)
3050 zlog_debug("%s : AFI_IP6 event %d", __func__, event);
d62a17ae 3051 rib_update_table(table, event);
d5b8c216 3052 }
718e3744 3053}
3054
718e3744 3055/* Delete self installed routes after zebra is relaunched. */
95a29032 3056void rib_sweep_table(struct route_table *table)
d62a17ae 3057{
3058 struct route_node *rn;
3059 struct route_entry *re;
3060 struct route_entry *next;
915902cb 3061 struct nexthop *nexthop;
d62a17ae 3062
915902cb
DS
3063 if (!table)
3064 return;
d62a17ae 3065
915902cb 3066 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
a2addae8 3067 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d4644d41 3068
915902cb
DS
3069 if (IS_ZEBRA_DEBUG_RIB)
3070 route_entry_dump(&rn->p, NULL, re);
3071
3072 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3073 continue;
3074
3075 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
3076 continue;
3077
d4644d41
DS
3078 /*
3079 * If routes are older than startup_time then
3080 * we know we read them in from the kernel.
3081 * As such we can safely remove them.
3082 */
3083 if (zrouter.startup_time < re->uptime)
3084 continue;
3085
915902cb
DS
3086 /*
3087 * So we are starting up and have received
3088 * routes from the kernel that we have installed
3089 * from a previous run of zebra but not cleaned
3090 * up ( say a kill -9 )
3091 * But since we haven't actually installed
3092 * them yet( we received them from the kernel )
3093 * we don't think they are active.
3094 * So let's pretend they are active to actually
3095 * remove them.
3096 * In all honesty I'm not sure if we should
3097 * mark them as active when we receive them
3098 * This is startup only so probably ok.
3099 *
3100 * If we ever decide to move rib_sweep_table
3101 * to a different spot (ie startup )
3102 * this decision needs to be revisited
3103 */
677c1dd5 3104 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
7ee30f28 3105 for (ALL_NEXTHOPS(re->ng, nexthop))
915902cb
DS
3106 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
3107
0c555cc6
DS
3108 rib_uninstall_kernel(rn, re);
3109 rib_delnode(rn, re);
915902cb
DS
3110 }
3111 }
718e3744 3112}
3113
3114/* Sweep all RIB tables. */
d4644d41 3115int rib_sweep_route(struct thread *t)
718e3744 3116{
d62a17ae 3117 struct vrf *vrf;
3118 struct zebra_vrf *zvrf;
78104b9b 3119
a2addae8 3120 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
915902cb
DS
3121 if ((zvrf = vrf->info) == NULL)
3122 continue;
3123
d62a17ae 3124 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
3125 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
3126 }
95a29032 3127
89272910 3128 zebra_router_sweep_route();
d4644d41
DS
3129
3130 return 0;
718e3744 3131}
2ea1ab1c
VT
3132
3133/* Remove specific by protocol routes from 'table'. */
d7c0a89a 3134unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
47a08aa9 3135 struct route_table *table)
d62a17ae 3136{
3137 struct route_node *rn;
3138 struct route_entry *re;
3139 struct route_entry *next;
3140 unsigned long n = 0;
3141
3142 if (table)
3143 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 3144 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 3145 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3146 continue;
3147 if (re->type == proto
3148 && re->instance == instance) {
3149 rib_delnode(rn, re);
3150 n++;
3151 }
3152 }
3153 return n;
2ea1ab1c
VT
3154}
3155
3156/* Remove specific by protocol routes. */
d7c0a89a 3157unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2ea1ab1c 3158{
d62a17ae 3159 struct vrf *vrf;
3160 struct zebra_vrf *zvrf;
d8612e65 3161 struct other_route_table *ort;
d62a17ae 3162 unsigned long cnt = 0;
78104b9b 3163
d8612e65
DS
3164 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
3165 zvrf = vrf->info;
3166 if (!zvrf)
3167 continue;
78104b9b 3168
d8612e65
DS
3169 cnt += rib_score_proto_table(proto, instance,
3170 zvrf->table[AFI_IP][SAFI_UNICAST])
3171 + rib_score_proto_table(
3172 proto, instance,
3173 zvrf->table[AFI_IP6][SAFI_UNICAST]);
3174
81fddbe7 3175 frr_each(otable, &zvrf->other_tables, ort) cnt +=
d8612e65
DS
3176 rib_score_proto_table(proto, instance, ort->table);
3177 }
47a08aa9 3178
d62a17ae 3179 return cnt;
2ea1ab1c
VT
3180}
3181
718e3744 3182/* Close RIB and clean up kernel routes. */
d62a17ae 3183void rib_close_table(struct route_table *table)
718e3744 3184{
d62a17ae 3185 struct route_node *rn;
1e9f448f 3186 rib_table_info_t *info;
5f7a4718 3187 rib_dest_t *dest;
718e3744 3188
1e9f448f
DS
3189 if (!table)
3190 return;
9fd92e3c 3191
6ca30e9e 3192 info = route_table_get_info(table);
5adc2528 3193
5f7a4718
DS
3194 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
3195 dest = rib_dest_from_rnode(rn);
1e9f448f 3196
5f7a4718 3197 if (dest && dest->selected_fib) {
1e9f448f
DS
3198 if (info->safi == SAFI_UNICAST)
3199 hook_call(rib_update, rn, NULL);
3200
3cdba47a
DS
3201 rib_uninstall_kernel(rn, dest->selected_fib);
3202 dest->selected_fib = NULL;
1e9f448f 3203 }
5f7a4718 3204 }
718e3744 3205}
3206
9bd9717b
MS
3207/*
3208 * Handler for async dataplane results after a pseudowire installation
3209 */
3210static int handle_pw_result(struct zebra_dplane_ctx *ctx)
3211{
9bd9717b
MS
3212 struct zebra_pw *pw;
3213 struct zebra_vrf *vrf;
3214
3215 /* The pseudowire code assumes success - we act on an error
3216 * result for installation attempts here.
3217 */
3218 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
3219 goto done;
3220
3221 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
3222 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
3223 pw = zebra_pw_find(vrf, dplane_ctx_get_pw_ifname(ctx));
3224 if (pw)
3225 zebra_pw_install_failure(pw);
3226 }
3227
3228done:
3229
9f2d0354 3230 return 0;
9bd9717b
MS
3231}
3232
3233
7cdb1a84 3234/*
d37f4d6c
MS
3235 * Handle results from the dataplane system. Dequeue update context
3236 * structs, dispatch to appropriate internal handlers.
7cdb1a84
MS
3237 */
3238static int rib_process_dplane_results(struct thread *thread)
3239{
25779064 3240 struct zebra_dplane_ctx *ctx;
4c206c8f
MS
3241 struct dplane_ctx_q ctxlist;
3242
3243 /* Dequeue a list of completed updates with one lock/unlock cycle */
7cdb1a84
MS
3244
3245 do {
4c206c8f
MS
3246 TAILQ_INIT(&ctxlist);
3247
7cdb1a84
MS
3248 /* Take lock controlling queue of results */
3249 pthread_mutex_lock(&dplane_mutex);
3250 {
d37f4d6c 3251 /* Dequeue list of context structs */
4c206c8f 3252 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
7cdb1a84
MS
3253 }
3254 pthread_mutex_unlock(&dplane_mutex);
3255
4c206c8f
MS
3256 /* Dequeue context block */
3257 ctx = dplane_ctx_dequeue(&ctxlist);
3258
3259 /* If we've emptied the results queue, we're done */
3260 if (ctx == NULL)
7cdb1a84 3261 break;
7cdb1a84 3262
4c206c8f 3263 while (ctx) {
d37f4d6c
MS
3264 switch (dplane_ctx_get_op(ctx)) {
3265 case DPLANE_OP_ROUTE_INSTALL:
3266 case DPLANE_OP_ROUTE_UPDATE:
3267 case DPLANE_OP_ROUTE_DELETE:
188a00e0
MS
3268 {
3269 /* Bit of special case for route updates
3270 * that were generated by async notifications:
3271 * we don't want to continue processing these
3272 * in the rib.
3273 */
3274 if (dplane_ctx_get_notif_provider(ctx) == 0)
3275 rib_process_result(ctx);
3276 else
3277 dplane_ctx_fini(&ctx);
3278 }
3279 break;
d37f4d6c 3280
54818e3b
MS
3281 case DPLANE_OP_ROUTE_NOTIFY:
3282 rib_process_dplane_notify(ctx);
d37f4d6c
MS
3283 break;
3284
3285 case DPLANE_OP_LSP_INSTALL:
3286 case DPLANE_OP_LSP_UPDATE:
3287 case DPLANE_OP_LSP_DELETE:
188a00e0
MS
3288 {
3289 /* Bit of special case for LSP updates
3290 * that were generated by async notifications:
3291 * we don't want to continue processing these.
3292 */
3293 if (dplane_ctx_get_notif_provider(ctx) == 0)
3294 zebra_mpls_lsp_dplane_result(ctx);
3295 else
3296 dplane_ctx_fini(&ctx);
3297 }
3298 break;
d37f4d6c 3299
104e3ad9
MS
3300 case DPLANE_OP_LSP_NOTIFY:
3301 zebra_mpls_process_dplane_notify(ctx);
d37f4d6c
MS
3302 break;
3303
9bd9717b
MS
3304 case DPLANE_OP_PW_INSTALL:
3305 case DPLANE_OP_PW_UNINSTALL:
3306 handle_pw_result(ctx);
3307 break;
3308
cf363e1b
MS
3309 case DPLANE_OP_SYS_ROUTE_ADD:
3310 case DPLANE_OP_SYS_ROUTE_DELETE:
3311 /* No further processing in zebra for these. */
3312 dplane_ctx_fini(&ctx);
3313 break;
3314
d37f4d6c
MS
3315 default:
3316 /* Don't expect this: just return the struct? */
3317 dplane_ctx_fini(&ctx);
3318 break;
3319 } /* Dispatch by op code */
4c206c8f
MS
3320
3321 ctx = dplane_ctx_dequeue(&ctxlist);
3322 }
3323
5709131c 3324 } while (1);
7cdb1a84 3325
f183e380
MS
3326 /* Check for nexthop tracking processing after finishing with results */
3327 do_nht_processing();
3328
5709131c 3329 return 0;
7cdb1a84
MS
3330}
3331
3332/*
3333 * Results are returned from the dataplane subsystem, in the context of
1bcea841 3334 * the dataplane pthread. We enqueue the results here for processing by
7cdb1a84
MS
3335 * the main thread later.
3336 */
4c206c8f 3337static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
7cdb1a84
MS
3338{
3339 /* Take lock controlling queue of results */
3340 pthread_mutex_lock(&dplane_mutex);
3341 {
4c206c8f
MS
3342 /* Enqueue context blocks */
3343 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
7cdb1a84
MS
3344 }
3345 pthread_mutex_unlock(&dplane_mutex);
3346
4c206c8f 3347 /* Ensure event is signalled to zebra main pthread */
3801e764 3348 thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
7cdb1a84
MS
3349 &t_dplane);
3350
5709131c 3351 return 0;
7cdb1a84
MS
3352}
3353
eaa2716d
SW
3354/*
3355 * Ensure there are no empty slots in the route_info array.
3356 * Every route type in zebra should be present there.
3357 */
3358static void check_route_info(void)
3359{
3360 int len = array_size(route_info);
3361
3362 /*
3363 * ZEBRA_ROUTE_SYSTEM is special cased since
3364 * its key is 0 anyway.
3365 *
3366 * ZEBRA_ROUTE_ALL is also ignored.
3367 */
3368 for (int i = 0; i < len; i++) {
3369 if (i == ZEBRA_ROUTE_SYSTEM || i == ZEBRA_ROUTE_ALL)
3370 continue;
3371 assert(route_info[i].key);
8dc7a759 3372 assert(route_info[i].meta_q_map < MQ_SIZE);
eaa2716d
SW
3373 }
3374}
3375
718e3744 3376/* Routing information base initialize. */
d62a17ae 3377void rib_init(void)
718e3744 3378{
eaa2716d
SW
3379 check_route_info();
3380
2561d12e 3381 rib_queue_init();
7cdb1a84
MS
3382
3383 /* Init dataplane, and register for results */
3384 pthread_mutex_init(&dplane_mutex, NULL);
3385 TAILQ_INIT(&rib_dplane_q);
4c206c8f 3386 zebra_dplane_init(rib_dplane_results);
718e3744 3387}
0915bb0c
AS
3388
3389/*
3390 * vrf_id_get_next
3391 *
3392 * Get the first vrf id that is greater than the given vrf id if any.
3393 *
2951a7a4 3394 * Returns true if a vrf id was found, false otherwise.
0915bb0c 3395 */
d62a17ae 3396static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 3397{
d62a17ae 3398 struct vrf *vrf;
b72ede27 3399
d62a17ae 3400 vrf = vrf_lookup_by_id(vrf_id);
3401 if (vrf) {
3402 vrf = RB_NEXT(vrf_id_head, vrf);
3403 if (vrf) {
3404 *next_id_p = vrf->vrf_id;
3405 return 1;
3406 }
3407 }
0915bb0c 3408
d62a17ae 3409 return 0;
0915bb0c
AS
3410}
3411
3412/*
3413 * rib_tables_iter_next
3414 *
3415 * Returns the next table in the iteration.
3416 */
d62a17ae 3417struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
3418{
3419 struct route_table *table;
3420
3421 /*
3422 * Array that helps us go over all AFI/SAFI combinations via one
3423 * index.
3424 */
3425 static struct {
3426 afi_t afi;
3427 safi_t safi;
3428 } afi_safis[] = {
3429 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
3430 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
3431 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
3432 };
3433
3434 table = NULL;
3435
3436 switch (iter->state) {
3437
3438 case RIB_TABLES_ITER_S_INIT:
3439 iter->vrf_id = VRF_DEFAULT;
3440 iter->afi_safi_ix = -1;
3441
3442 /* Fall through */
3443
3444 case RIB_TABLES_ITER_S_ITERATING:
3445 iter->afi_safi_ix++;
3446 while (1) {
3447
3448 while (iter->afi_safi_ix
7e3a1ec7 3449 < (int)array_size(afi_safis)) {
d62a17ae 3450 table = zebra_vrf_table(
3451 afi_safis[iter->afi_safi_ix].afi,
3452 afi_safis[iter->afi_safi_ix].safi,
3453 iter->vrf_id);
3454 if (table)
3455 break;
3456
3457 iter->afi_safi_ix++;
3458 }
3459
3460 /*
3461 * Found another table in this vrf.
3462 */
3463 if (table)
3464 break;
3465
3466 /*
3467 * Done with all tables in the current vrf, go to the
3468 * next
3469 * one.
3470 */
3471 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
3472 break;
3473
3474 iter->afi_safi_ix = 0;
3475 }
0915bb0c 3476
0915bb0c
AS
3477 break;
3478
d62a17ae 3479 case RIB_TABLES_ITER_S_DONE:
3480 return NULL;
0915bb0c
AS
3481 }
3482
d62a17ae 3483 if (table)
3484 iter->state = RIB_TABLES_ITER_S_ITERATING;
3485 else
3486 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c 3487
d62a17ae 3488 return table;
0915bb0c 3489}