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