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