]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
zebra: add dplane context accessors
[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;
8dc7a759 74 uint8_t 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},
303b93cd 104 [ZEBRA_ROUTE_VRRP] = {ZEBRA_ROUTE_VRRP, 255, 4}
d6abd8b0 105 /* Any new route type added to zebra, should be mirrored here */
c710b277 106
d62a17ae 107 /* no entry/default: 150 */
718e3744 108};
6b0655a2 109
4623d897
DL
110/* RPF lookup behaviour */
111static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
112
6c4f4e6e 113
d62a17ae 114static void __attribute__((format(printf, 5, 6)))
115_rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn,
116 int priority, const char *msgfmt, ...)
2263a412 117{
d62a17ae 118 char buf[SRCDEST2STR_BUFFER + sizeof(" (MRIB)")];
119 char msgbuf[512];
120 va_list ap;
2263a412 121
d62a17ae 122 va_start(ap, msgfmt);
123 vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
124 va_end(ap);
2263a412 125
d62a17ae 126 if (rn) {
127 rib_table_info_t *info = srcdest_rnode_table_info(rn);
128 srcdest_rnode2str(rn, buf, sizeof(buf));
cb653491 129
d62a17ae 130 if (info->safi == SAFI_MULTICAST)
131 strcat(buf, " (MRIB)");
132 } else {
133 snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
134 }
2263a412 135
d62a17ae 136 zlog(priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
2263a412
DL
137}
138
d62a17ae 139#define rnode_debug(node, vrf_id, ...) \
2263a412 140 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
d62a17ae 141#define rnode_info(node, ...) \
2263a412
DL
142 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
143
fd289fc8 144uint8_t route_distance(int type)
40c7bdb0 145{
fd289fc8 146 uint8_t distance;
40c7bdb0 147
d62a17ae 148 if ((unsigned)type >= array_size(route_info))
149 distance = 150;
150 else
151 distance = route_info[type].distance;
40c7bdb0 152
d62a17ae 153 return distance;
40c7bdb0 154}
155
d7c0a89a 156int is_zebra_valid_kernel_table(uint32_t table_id)
7a4bb9c5 157{
8f500a1c 158#ifdef linux
d62a17ae 159 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
160 || (table_id == RT_TABLE_COMPAT))
161 return 0;
8f500a1c
RW
162#endif
163
d62a17ae 164 return 1;
7a4bb9c5
DS
165}
166
d7c0a89a 167int is_zebra_main_routing_table(uint32_t table_id)
7a4bb9c5 168{
c447ad08 169 if (table_id == RT_TABLE_MAIN)
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
dc95824a
DO
801/* This function verifies reachability of one given nexthop, which can be
802 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
99eabcec
DS
803 * in nexthop->flags field. The nexthop->ifindex will be updated
804 * appropriately as well. An existing route map can turn
805 * (otherwise active) nexthop into inactive, but not vice versa.
dc95824a
DO
806 *
807 * The return value is the final value of 'ACTIVE' flag.
808 */
d62a17ae 809static unsigned nexthop_active_check(struct route_node *rn,
810 struct route_entry *re,
99eabcec 811 struct nexthop *nexthop)
d62a17ae 812{
813 struct interface *ifp;
814 route_map_result_t ret = RMAP_MATCH;
815 int family;
816 char buf[SRCDEST2STR_BUFFER];
86391e56 817 const struct prefix *p, *src_p;
ac6eebce 818 struct zebra_vrf *zvrf;
819
d62a17ae 820 srcdest_rnode_prefixes(rn, &p, &src_p);
821
822 if (rn->p.family == AF_INET)
823 family = AFI_IP;
824 else if (rn->p.family == AF_INET6)
825 family = AFI_IP6;
826 else
827 family = 0;
828 switch (nexthop->type) {
829 case NEXTHOP_TYPE_IFINDEX:
4a7371e9 830 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
d62a17ae 831 if (ifp && if_is_operative(ifp))
832 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
833 else
834 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
835 break;
836 case NEXTHOP_TYPE_IPV4:
837 case NEXTHOP_TYPE_IPV4_IFINDEX:
838 family = AFI_IP;
99eabcec 839 if (nexthop_active(AFI_IP, re, nexthop, rn))
d62a17ae 840 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
841 else
842 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
843 break;
844 case NEXTHOP_TYPE_IPV6:
845 family = AFI_IP6;
99eabcec 846 if (nexthop_active(AFI_IP6, re, nexthop, rn))
d62a17ae 847 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
848 else
849 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
850 break;
851 case NEXTHOP_TYPE_IPV6_IFINDEX:
852 /* RFC 5549, v4 prefix with v6 NH */
853 if (rn->p.family != AF_INET)
854 family = AFI_IP6;
855 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
007dbee6 856 ifp = if_lookup_by_index(nexthop->ifindex,
4a7371e9 857 nexthop->vrf_id);
d62a17ae 858 if (ifp && if_is_operative(ifp))
859 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
860 else
861 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
862 } else {
99eabcec 863 if (nexthop_active(AFI_IP6, re, nexthop, rn))
d62a17ae 864 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
865 else
866 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
867 }
868 break;
869 case NEXTHOP_TYPE_BLACKHOLE:
870 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
871 break;
872 default:
873 break;
874 }
34815ea3
DS
875 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
876 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
877 zlog_debug("\t%s: Unable to find a active nexthop",
878 __PRETTY_FUNCTION__);
d62a17ae 879 return 0;
34815ea3 880 }
d62a17ae 881
882 /* XXX: What exactly do those checks do? Do we support
ac6eebce 883 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
884 */
d62a17ae 885 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
886 || (family == AFI_IP6 && p->family != AF_INET6))
887 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
888
889 /* The original code didn't determine the family correctly
890 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
891 * from the rib_table_info in those cases.
892 * Possibly it may be better to use only the rib_table_info
893 * in every case.
894 */
895 if (!family) {
896 rib_table_info_t *info;
897
898 info = srcdest_rnode_table_info(rn);
899 family = info->afi;
718e3744 900 }
c52ef59f 901
d62a17ae 902 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
903
ac6eebce 904 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
905 if (!zvrf) {
906 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
907 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__);
908 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
909 }
910
d62a17ae 911 /* It'll get set if required inside */
ac6eebce 912 ret = zebra_route_map_check(family, re->type, re->instance, p,
913 nexthop, zvrf, re->tag);
d62a17ae 914 if (ret == RMAP_DENYMATCH) {
915 if (IS_ZEBRA_DEBUG_RIB) {
916 srcdest_rnode2str(rn, buf, sizeof(buf));
917 zlog_debug(
918 "%u:%s: Filtering out with NH out %s due to route map",
919 re->vrf_id, buf,
99b9d960 920 ifindex2ifname(nexthop->ifindex,
4a7371e9 921 nexthop->vrf_id));
d62a17ae 922 }
923 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
518f0eb1 924 }
d62a17ae 925 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
718e3744 926}
927
99eabcec
DS
928/*
929 * Iterate over all nexthops of the given RIB entry and refresh their
f0f77c9a
DS
930 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
931 * nexthop is found to toggle the ACTIVE flag, the whole re structure
99eabcec 932 * is flagged with ROUTE_ENTRY_CHANGED.
03e232a4
DO
933 *
934 * Return value is the new number of active nexthops.
935 */
99eabcec 936static int nexthop_active_update(struct route_node *rn, struct route_entry *re)
d62a17ae 937{
938 struct nexthop *nexthop;
939 union g_addr prev_src;
80ad0418 940 unsigned int prev_active, new_active;
d62a17ae 941 ifindex_t prev_index;
ac6eebce 942
d62a17ae 943 re->nexthop_active_num = 0;
944 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
945
7ee30f28 946 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
d62a17ae 947 /* No protocol daemon provides src and so we're skipping
948 * tracking it */
949 prev_src = nexthop->rmap_src;
950 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
951 prev_index = nexthop->ifindex;
220f0f42
DS
952 /*
953 * We need to respect the multipath_num here
954 * as that what we should be able to install from
955 * a multipath perpsective should not be a data plane
956 * decision point.
957 */
99eabcec 958 new_active = nexthop_active_check(rn, re, nexthop);
b3f2b590
DS
959 if (new_active
960 && re->nexthop_active_num >= zrouter.multipath_num) {
220f0f42
DS
961 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
962 new_active = 0;
963 }
964 if (new_active)
d62a17ae 965 re->nexthop_active_num++;
966 /* Don't allow src setting on IPv6 addr for now */
967 if (prev_active != new_active || prev_index != nexthop->ifindex
968 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
969 && nexthop->type < NEXTHOP_TYPE_IPV6)
970 && prev_src.ipv4.s_addr
971 != nexthop->rmap_src.ipv4.s_addr)
972 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
973 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
974 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
6883bf8d
DS
975 &nexthop->rmap_src.ipv6)))
976 || CHECK_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED)) {
d62a17ae 977 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
978 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
979 }
980 }
981
d62a17ae 982 return re->nexthop_active_num;
718e3744 983}
6baeb988 984
a64448ba
DS
985/*
986 * Is this RIB labeled-unicast? It must be of type BGP and all paths
987 * (nexthops) must have a label.
988 */
d62a17ae 989int zebra_rib_labeled_unicast(struct route_entry *re)
a64448ba 990{
d62a17ae 991 struct nexthop *nexthop = NULL;
a64448ba 992
d62a17ae 993 if (re->type != ZEBRA_ROUTE_BGP)
994 return 0;
a64448ba 995
7ee30f28 996 for (ALL_NEXTHOPS(re->ng, nexthop))
d62a17ae 997 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
998 return 0;
6b0655a2 999
d62a17ae 1000 return 1;
a64448ba 1001}
718e3744 1002
6ae24471
DS
1003/* Update flag indicates whether this is a "replace" or not. Currently, this
1004 * is only used for IPv4.
1005 */
0c555cc6
DS
1006void rib_install_kernel(struct route_node *rn, struct route_entry *re,
1007 struct route_entry *old)
718e3744 1008{
d62a17ae 1009 struct nexthop *nexthop;
1010 rib_table_info_t *info = srcdest_rnode_table_info(rn);
d62a17ae 1011 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
97f5b441
MS
1012 const struct prefix *p, *src_p;
1013 enum zebra_dplane_result ret;
1014
1015 rib_dest_t *dest = rib_dest_from_rnode(rn);
718e3744 1016
d62a17ae 1017 srcdest_rnode_prefixes(rn, &p, &src_p);
416ec78d 1018
d62a17ae 1019 if (info->safi != SAFI_UNICAST) {
7ee30f28 1020 for (ALL_NEXTHOPS(re->ng, nexthop))
d62a17ae 1021 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 1022 return;
25b9cb0c
DL
1023 } else {
1024 struct nexthop *prev;
1025
7ee30f28 1026 for (ALL_NEXTHOPS(re->ng, nexthop)) {
25b9cb0c 1027 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_DUPLICATE);
7ee30f28 1028 for (ALL_NEXTHOPS(re->ng, prev)) {
25b9cb0c
DL
1029 if (prev == nexthop)
1030 break;
996c9314
LB
1031 if (nexthop_same_firsthop(nexthop, prev)) {
1032 SET_FLAG(nexthop->flags,
1033 NEXTHOP_FLAG_DUPLICATE);
25b9cb0c
DL
1034 break;
1035 }
1036 }
1037 }
d62a17ae 1038 }
718e3744 1039
2063a814
DS
1040 /*
1041 * If this is a replace to a new RE let the originator of the RE
1042 * know that they've lost
1043 */
9a9f8926 1044 if (old && (old != re) && (old->type != re->type))
28610f7e 1045 zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON);
25b9cb0c 1046
97f5b441
MS
1047 /* Update fib selection */
1048 dest->selected_fib = re;
1049
d62a17ae 1050 /*
1051 * Make sure we update the FPM any time we send new information to
1052 * the kernel.
1053 */
1054 hook_call(rib_update, rn, "installing in kernel");
97f5b441
MS
1055
1056 /* Send add or update */
8263d1d0 1057 if (old)
97f5b441 1058 ret = dplane_route_update(rn, re, old);
5709131c 1059 else
97f5b441 1060 ret = dplane_route_add(rn, re);
97f5b441
MS
1061
1062 switch (ret) {
ea1c14f6 1063 case ZEBRA_DPLANE_REQUEST_QUEUED:
60f98b23
DS
1064 SET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
1065 if (old)
1066 SET_FLAG(old->status, ROUTE_ENTRY_QUEUED);
97f5b441
MS
1067 if (zvrf)
1068 zvrf->installs_queued++;
1e885672 1069 break;
ea1c14f6 1070 case ZEBRA_DPLANE_REQUEST_FAILURE:
97f5b441
MS
1071 {
1072 char str[SRCDEST2STR_BUFFER];
1073
1074 srcdest_rnode2str(rn, str, sizeof(str));
1075 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
1076 "%u:%s: Failed to enqueue dataplane install",
1077 re->vrf_id, str);
1e885672 1078 break;
97f5b441 1079 }
ea1c14f6 1080 case ZEBRA_DPLANE_REQUEST_SUCCESS:
97f5b441
MS
1081 if (zvrf)
1082 zvrf->installs++;
1e885672
DS
1083 break;
1084 }
d62a17ae 1085
0c555cc6 1086 return;
718e3744 1087}
1088
1089/* Uninstall the route from kernel. */
0c555cc6 1090void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
718e3744 1091{
d62a17ae 1092 struct nexthop *nexthop;
1093 rib_table_info_t *info = srcdest_rnode_table_info(rn);
d62a17ae 1094 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
05737783 1095
d62a17ae 1096 if (info->safi != SAFI_UNICAST) {
677c1dd5 1097 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
7ee30f28 1098 for (ALL_NEXTHOPS(re->ng, nexthop))
d6792f9d 1099 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 1100 return;
d62a17ae 1101 }
416ec78d 1102
d62a17ae 1103 /*
1104 * Make sure we update the FPM any time we send new information to
97f5b441 1105 * the dataplane.
d62a17ae 1106 */
1107 hook_call(rib_update, rn, "uninstalling from kernel");
97f5b441
MS
1108
1109 switch (dplane_route_delete(rn, re)) {
ea1c14f6 1110 case ZEBRA_DPLANE_REQUEST_QUEUED:
97f5b441
MS
1111 if (zvrf)
1112 zvrf->removals_queued++;
1e885672 1113 break;
ea1c14f6 1114 case ZEBRA_DPLANE_REQUEST_FAILURE:
97f5b441
MS
1115 {
1116 char str[SRCDEST2STR_BUFFER];
1117
1118 srcdest_rnode2str(rn, str, sizeof(str));
1119 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
1120 "%u:%s: Failed to enqueue dataplane uninstall",
1121 re->vrf_id, str);
1e885672 1122 break;
97f5b441 1123 }
ea1c14f6 1124 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1e885672
DS
1125 if (zvrf)
1126 zvrf->removals++;
1127 break;
1128 }
718e3744 1129
0c555cc6 1130 return;
718e3744 1131}
1132
1133/* Uninstall the route from kernel. */
d62a17ae 1134static void rib_uninstall(struct route_node *rn, struct route_entry *re)
718e3744 1135{
d62a17ae 1136 rib_table_info_t *info = srcdest_rnode_table_info(rn);
5f7a4718 1137 rib_dest_t *dest = rib_dest_from_rnode(rn);
97f5b441 1138 struct nexthop *nexthop;
416ec78d 1139
5f7a4718 1140 if (dest && dest->selected_fib == re) {
d62a17ae 1141 if (info->safi == SAFI_UNICAST)
1142 hook_call(rib_update, rn, "rib_uninstall");
5adc2528 1143
d62a17ae 1144 /* If labeled-unicast route, uninstall transit LSP. */
1145 if (zebra_rib_labeled_unicast(re))
1146 zebra_mpls_lsp_uninstall(info->zvrf, rn, re);
7cdb1a84 1147
3cdba47a 1148 rib_uninstall_kernel(rn, re);
97f5b441
MS
1149
1150 dest->selected_fib = NULL;
1151
1152 for (ALL_NEXTHOPS(re->ng, nexthop))
1153 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
d62a17ae 1154 }
446bb95e 1155
d62a17ae 1156 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
86391e56
MS
1157 const struct prefix *p, *src_p;
1158
d62a17ae 1159 srcdest_rnode_prefixes(rn, &p, &src_p);
05737783 1160
d62a17ae 1161 redistribute_delete(p, src_p, re);
1162 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
1163 }
718e3744 1164}
1165
9fd92e3c
AS
1166/*
1167 * rib_can_delete_dest
1168 *
1169 * Returns TRUE if the given dest can be deleted from the table.
1170 */
d62a17ae 1171static int rib_can_delete_dest(rib_dest_t *dest)
9fd92e3c 1172{
045207e2 1173 if (re_list_first(&dest->routes)) {
d62a17ae 1174 return 0;
1175 }
9fd92e3c 1176
c86ba6c2
DS
1177 /*
1178 * Unresolved rnh's are stored on the default route's list
1179 *
1180 * dest->rnode can also be the source prefix node in an
1181 * ipv6 sourcedest table. Fortunately the prefix of a
1182 * source prefix node can never be the default prefix.
1183 */
1184 if (is_default_prefix(&dest->rnode->p))
1185 return 0;
1186
d62a17ae 1187 /*
1188 * Don't delete the dest if we have to update the FPM about this
1189 * prefix.
1190 */
1191 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
1192 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
1193 return 0;
5adc2528 1194
d62a17ae 1195 return 1;
9fd92e3c
AS
1196}
1197
699dae23
DS
1198void zebra_rib_evaluate_rn_nexthops(struct route_node *rn, uint32_t seq)
1199{
1200 rib_dest_t *dest = rib_dest_from_rnode(rn);
699dae23
DS
1201 struct rnh *rnh;
1202
1203 /*
50872b08 1204 * We are storing the rnh's associated withb
699dae23
DS
1205 * the tracked nexthop as a list of the rn's.
1206 * Unresolved rnh's are placed at the top
1207 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
1208 * As such for each rn we need to walk up the tree
1209 * and see if any rnh's need to see if they
1210 * would match a more specific route
1211 */
1212 while (rn) {
50872b08
DS
1213 if (IS_ZEBRA_DEBUG_NHT_DETAILED) {
1214 char buf[PREFIX_STRLEN];
1215
1216 zlog_debug("%s: %s Being examined for Nexthop Tracking",
1217 __PRETTY_FUNCTION__,
1218 srcdest_rnode2str(rn, buf, sizeof(buf)));
1219 }
699dae23
DS
1220 if (!dest) {
1221 rn = rn->parent;
1222 if (rn)
1223 dest = rib_dest_from_rnode(rn);
1224 continue;
1225 }
1226 /*
1227 * If we have any rnh's stored in the nht list
1228 * then we know that this route node was used for
1229 * nht resolution and as such we need to call the
1230 * nexthop tracking evaluation code
1231 */
81fddbe7 1232 frr_each (rnh_list, &dest->nht, rnh) {
699dae23
DS
1233 struct zebra_vrf *zvrf =
1234 zebra_vrf_lookup_by_id(rnh->vrf_id);
1235 struct prefix *p = &rnh->node->p;
1236
50872b08 1237 if (IS_ZEBRA_DEBUG_NHT_DETAILED) {
699dae23
DS
1238 char buf1[PREFIX_STRLEN];
1239 char buf2[PREFIX_STRLEN];
1240
1241 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
1242 zvrf->vrf->vrf_id,
50872b08 1243 srcdest_rnode2str(rn, buf1,
699dae23
DS
1244 sizeof(buf1)),
1245 prefix2str(p, buf2, sizeof(buf2)),
1246 seq, rnh->seqno);
1247 }
1248
1249 /*
1250 * If we have evaluated this node on this pass
1251 * already, due to following the tree up
1252 * then we know that we can move onto the next
1253 * rnh to process.
1254 *
1255 * Additionally we call zebra_evaluate_rnh
1256 * when we gc the dest. In this case we know
1257 * that there must be no other re's where
1258 * we were originally as such we know that
1259 * that sequence number is ok to respect.
1260 */
50872b08
DS
1261 if (rnh->seqno == seq) {
1262 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
1263 zlog_debug(
1264 "\tNode processed and moved already");
699dae23 1265 continue;
50872b08 1266 }
699dae23
DS
1267
1268 rnh->seqno = seq;
1269 zebra_evaluate_rnh(zvrf, family2afi(p->family), 0,
1270 rnh->type, p);
1271 }
1272
1273 rn = rn->parent;
1274 if (rn)
1275 dest = rib_dest_from_rnode(rn);
1276 }
1277}
1278
9fd92e3c
AS
1279/*
1280 * rib_gc_dest
1281 *
1282 * Garbage collect the rib dest corresponding to the given route node
1283 * if appropriate.
1284 *
1285 * Returns TRUE if the dest was deleted, FALSE otherwise.
1286 */
d62a17ae 1287int rib_gc_dest(struct route_node *rn)
9fd92e3c 1288{
d62a17ae 1289 rib_dest_t *dest;
9fd92e3c 1290
d62a17ae 1291 dest = rib_dest_from_rnode(rn);
1292 if (!dest)
1293 return 0;
9fd92e3c 1294
d62a17ae 1295 if (!rib_can_delete_dest(dest))
1296 return 0;
9fd92e3c 1297
c9abf558
DS
1298 if (IS_ZEBRA_DEBUG_RIB) {
1299 struct zebra_vrf *zvrf;
1300
1301 zvrf = rib_dest_vrf(dest);
d62a17ae 1302 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
c9abf558 1303 }
9fd92e3c 1304
699dae23
DS
1305 zebra_rib_evaluate_rn_nexthops(rn, zebra_router_get_next_sequence());
1306
d62a17ae 1307 dest->rnode = NULL;
aa57abfb 1308 rnh_list_fini(&dest->nht);
d62a17ae 1309 XFREE(MTYPE_RIB_DEST, dest);
1310 rn->info = NULL;
9fd92e3c 1311
d62a17ae 1312 /*
1313 * Release the one reference that we keep on the route node.
1314 */
1315 route_unlock_node(rn);
1316 return 1;
9fd92e3c
AS
1317}
1318
d62a17ae 1319static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1320 struct route_entry *new)
3e5c6e00 1321{
d62a17ae 1322 hook_call(rib_update, rn, "new route selected");
3e5c6e00 1323
d62a17ae 1324 /* Update real nexthop. This may actually determine if nexthop is active
1325 * or not. */
6883bf8d 1326 if (!nexthop_group_active_nexthop_num(&new->ng)) {
d62a17ae 1327 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1328 return;
1329 }
3e5c6e00 1330
d62a17ae 1331 if (IS_ZEBRA_DEBUG_RIB) {
1332 char buf[SRCDEST2STR_BUFFER];
1333 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
1334 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1335 zvrf_id(zvrf), buf, rn, new,
1336 zebra_route_string(new->type));
d62a17ae 1337 }
3e5c6e00 1338
d62a17ae 1339 /* If labeled-unicast route, install transit LSP. */
1340 if (zebra_rib_labeled_unicast(new))
1341 zebra_mpls_lsp_install(zvrf, rn, new);
a64448ba 1342
3cdba47a 1343 rib_install_kernel(rn, new, NULL);
3e5c6e00 1344
d62a17ae 1345 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1346}
1347
d62a17ae 1348static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1349 struct route_entry *old)
3e5c6e00 1350{
d62a17ae 1351 hook_call(rib_update, rn, "removing existing route");
3e5c6e00 1352
d62a17ae 1353 /* Uninstall from kernel. */
1354 if (IS_ZEBRA_DEBUG_RIB) {
1355 char buf[SRCDEST2STR_BUFFER];
1356 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
1357 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1358 zvrf_id(zvrf), buf, rn, old,
1359 zebra_route_string(old->type));
d62a17ae 1360 }
3e5c6e00 1361
d62a17ae 1362 /* If labeled-unicast route, uninstall transit LSP. */
1363 if (zebra_rib_labeled_unicast(old))
1364 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1365
3cdba47a 1366 rib_uninstall_kernel(rn, old);
d62a17ae 1367
1368 /* Update nexthop for route, reset changed flag. */
ef57f35f
DL
1369 /* Note: this code also handles the Linux case when an interface goes
1370 * down, causing the kernel to delete routes without sending DELROUTE
1371 * notifications
1372 */
6883bf8d 1373 if (RIB_KERNEL_ROUTE(old))
212df1de
PG
1374 SET_FLAG(old->status, ROUTE_ENTRY_REMOVED);
1375 else
1376 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
d62a17ae 1377}
1378
1379static void rib_process_update_fib(struct zebra_vrf *zvrf,
1380 struct route_node *rn,
1381 struct route_entry *old,
1382 struct route_entry *new)
1383{
d62a17ae 1384 int nh_active = 0;
d62a17ae 1385
1386 /*
1387 * We have to install or update if a new route has been selected or
1388 * something has changed.
1389 */
1390 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1391 hook_call(rib_update, rn, "updating existing route");
1392
1393 /* Update the nexthop; we could determine here that nexthop is
1394 * inactive. */
6883bf8d 1395 if (nexthop_group_active_nexthop_num(&new->ng))
d62a17ae 1396 nh_active = 1;
1397
1398 /* If nexthop is active, install the selected route, if
1399 * appropriate. If
1400 * the install succeeds, cleanup flags for prior route, if
1401 * different from
1402 * newly selected.
1403 */
1404 if (nh_active) {
1405 if (IS_ZEBRA_DEBUG_RIB) {
1406 char buf[SRCDEST2STR_BUFFER];
1407 srcdest_rnode2str(rn, buf, sizeof(buf));
1408 if (new != old)
1409 zlog_debug(
2da33d6b 1410 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
d62a17ae 1411 zvrf_id(zvrf), buf, rn, new,
2da33d6b
DS
1412 zebra_route_string(new->type),
1413 old,
1414 zebra_route_string(old->type));
d62a17ae 1415 else
1416 zlog_debug(
2da33d6b 1417 "%u:%s: Updating route rn %p, re %p (%s)",
d62a17ae 1418 zvrf_id(zvrf), buf, rn, new,
2da33d6b 1419 zebra_route_string(new->type));
d62a17ae 1420 }
1421
1422 /* If labeled-unicast route, uninstall transit LSP. */
1423 if (zebra_rib_labeled_unicast(old))
1424 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1425
3cdba47a
DS
1426 /*
1427 * Non-system route should be installed.
1428 * If labeled-unicast route, install transit
1429 * LSP.
1430 */
1431 if (zebra_rib_labeled_unicast(new))
1432 zebra_mpls_lsp_install(zvrf, rn, new);
d62a17ae 1433
3cdba47a 1434 rib_install_kernel(rn, new, old);
d62a17ae 1435 }
a64448ba 1436
d62a17ae 1437 /*
1438 * If nexthop for selected route is not active or install
1439 * failed, we
1440 * may need to uninstall and delete for redistribution.
1441 */
ed216282 1442 if (!nh_active) {
d62a17ae 1443 if (IS_ZEBRA_DEBUG_RIB) {
1444 char buf[SRCDEST2STR_BUFFER];
1445 srcdest_rnode2str(rn, buf, sizeof(buf));
1446 if (new != old)
1447 zlog_debug(
2da33d6b 1448 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
d62a17ae 1449 zvrf_id(zvrf), buf, rn, new,
2da33d6b
DS
1450 zebra_route_string(new->type),
1451 old,
1452 zebra_route_string(old->type));
d62a17ae 1453 else
1454 zlog_debug(
2da33d6b 1455 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
d62a17ae 1456 zvrf_id(zvrf), buf, rn, new,
2da33d6b 1457 zebra_route_string(new->type));
d62a17ae 1458 }
1459
1460 /* If labeled-unicast route, uninstall transit LSP. */
1461 if (zebra_rib_labeled_unicast(old))
1462 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1463
3cdba47a 1464 rib_uninstall_kernel(rn, old);
d62a17ae 1465 }
1466 } else {
1467 /*
1468 * Same route selected; check if in the FIB and if not,
3cdba47a
DS
1469 * re-install. This is housekeeping code to deal with
1470 * race conditions in kernel with linux netlink reporting
1471 * interface up before IPv4 or IPv6 protocol is ready
d62a17ae 1472 * to add routes.
1473 */
3cdba47a
DS
1474 if (!CHECK_FLAG(new->status, ROUTE_ENTRY_INSTALLED) ||
1475 RIB_SYSTEM_ROUTE(new))
677c1dd5 1476 rib_install_kernel(rn, new, NULL);
d62a17ae 1477 }
1478
1479 /* Update prior route. */
6883bf8d 1480 if (new != old)
d62a17ae 1481 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1482
d62a17ae 1483 /* Clear changed flag. */
1484 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1485}
1486
d62a17ae 1487/* Check if 'alternate' RIB entry is better than 'current'. */
1488static struct route_entry *rib_choose_best(struct route_entry *current,
1489 struct route_entry *alternate)
1490{
1491 if (current == NULL)
1492 return alternate;
1493
1494 /* filter route selection in following order:
1495 * - connected beats other types
fec4ca19 1496 * - if both connected, loopback or vrf wins
d62a17ae 1497 * - lower distance beats higher
1498 * - lower metric beats higher for equal distance
1499 * - last, hence oldest, route wins tie break.
1500 */
1501
fec4ca19
DS
1502 /* Connected routes. Check to see if either are a vrf
1503 * or loopback interface. If not, pick the last connected
d62a17ae 1504 * route of the set of lowest metric connected routes.
1505 */
1506 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
fec4ca19
DS
1507 if (current->type != ZEBRA_ROUTE_CONNECT)
1508 return alternate;
1509
1510 /* both are connected. are either loop or vrf? */
1511 struct nexthop *nexthop = NULL;
1512
1513 for (ALL_NEXTHOPS(alternate->ng, nexthop)) {
1514 if (if_is_loopback_or_vrf(if_lookup_by_index(
1515 nexthop->ifindex, alternate->vrf_id)))
1516 return alternate;
1517 }
1518
1519 for (ALL_NEXTHOPS(current->ng, nexthop)) {
1520 if (if_is_loopback_or_vrf(if_lookup_by_index(
1521 nexthop->ifindex, current->vrf_id)))
1522 return current;
1523 }
1524
1525 /* Neither are loop or vrf so pick best metric */
1526 if (alternate->metric <= current->metric)
d62a17ae 1527 return alternate;
1528
1529 return current;
1530 }
3e5c6e00 1531
d62a17ae 1532 if (current->type == ZEBRA_ROUTE_CONNECT)
1533 return current;
3e5c6e00 1534
d62a17ae 1535 /* higher distance loses */
1536 if (alternate->distance < current->distance)
1537 return alternate;
1538 if (current->distance < alternate->distance)
1539 return current;
3e5c6e00 1540
d62a17ae 1541 /* metric tie-breaks equal distance */
1542 if (alternate->metric <= current->metric)
1543 return alternate;
3e5c6e00 1544
d62a17ae 1545 return current;
3e5c6e00 1546}
1547
d62a17ae 1548/* Core function for processing routing information base. */
1549static void rib_process(struct route_node *rn)
1550{
1551 struct route_entry *re;
1552 struct route_entry *next;
1553 struct route_entry *old_selected = NULL;
1554 struct route_entry *new_selected = NULL;
1555 struct route_entry *old_fib = NULL;
1556 struct route_entry *new_fib = NULL;
1557 struct route_entry *best = NULL;
1558 char buf[SRCDEST2STR_BUFFER];
1559 rib_dest_t *dest;
1560 struct zebra_vrf *zvrf = NULL;
86391e56
MS
1561 const struct prefix *p, *src_p;
1562
d62a17ae 1563 srcdest_rnode_prefixes(rn, &p, &src_p);
1564 vrf_id_t vrf_id = VRF_UNKNOWN;
1565
1566 assert(rn);
1567
1568 dest = rib_dest_from_rnode(rn);
1569 if (dest) {
1570 zvrf = rib_dest_vrf(dest);
1571 vrf_id = zvrf_id(zvrf);
1572 }
bab85d4f 1573
d62a17ae 1574 if (IS_ZEBRA_DEBUG_RIB)
1575 srcdest_rnode2str(rn, buf, sizeof(buf));
bab85d4f 1576
d62a17ae 1577 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1578 zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn);
bab85d4f 1579
607425e5
DS
1580 /*
1581 * we can have rn's that have a NULL info pointer
1582 * (dest). As such let's not let the deref happen
1583 * additionally we know RNODE_FOREACH_RE_SAFE
1584 * will not iterate so we are ok.
1585 */
1586 if (dest)
1587 old_fib = dest->selected_fib;
5f7a4718 1588
a2addae8 1589 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1590 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1591 zlog_debug(
2da33d6b
DS
1592 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1593 vrf_id, buf, re, zebra_route_string(re->type),
1594 re->status, re->flags, re->distance,
1595 re->metric);
d62a17ae 1596
1597 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1598
1599 /* Currently selected re. */
1600 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1601 assert(old_selected == NULL);
1602 old_selected = re;
1603 }
bab85d4f 1604
d62a17ae 1605 /* Skip deleted entries from selection */
1606 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1607 continue;
1608
1609 /* Skip unreachable nexthop. */
1610 /* This first call to nexthop_active_update is merely to
6883bf8d
DS
1611 * determine if there's any change to nexthops associated
1612 * with this RIB entry. Now, rib_process() can be invoked due
1613 * to an external event such as link down or due to
1614 * next-hop-tracking evaluation. In the latter case,
d62a17ae 1615 * a decision has already been made that the NHs have changed.
6883bf8d
DS
1616 * So, no need to invoke a potentially expensive call again.
1617 * Further, since the change might be in a recursive NH which
1618 * is not caught in the nexthop_active_update() code. Thus, we
1619 * might miss changes to recursive NHs.
d62a17ae 1620 */
6883bf8d 1621 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
99eabcec 1622 && !nexthop_active_update(rn, re)) {
d62a17ae 1623 if (re->type == ZEBRA_ROUTE_TABLE) {
1624 /* XXX: HERE BE DRAGONS!!!!!
1625 * In all honesty, I have not yet figured out
6883bf8d
DS
1626 * what this part does or why the
1627 * ROUTE_ENTRY_CHANGED test above is correct
d62a17ae 1628 * or why we need to delete a route here, and
6883bf8d
DS
1629 * also not whether this concerns both selected
1630 * and fib route, or only selected
1631 * or only fib
1632 *
1633 * This entry was denied by the 'ip protocol
1634 * table' route-map, we need to delete it */
d62a17ae 1635 if (re != old_selected) {
1636 if (IS_ZEBRA_DEBUG_RIB)
1637 zlog_debug(
32391aff 1638 "%s: %u:%s: imported via import-table but denied "
d62a17ae 1639 "by the ip protocol table route-map",
32391aff 1640 __func__, vrf_id, buf);
d62a17ae 1641 rib_unlink(rn, re);
1642 } else
1643 SET_FLAG(re->status,
1644 ROUTE_ENTRY_REMOVED);
1645 }
1646
1647 continue;
1648 }
bab85d4f 1649
d62a17ae 1650 /* Infinite distance. */
1651 if (re->distance == DISTANCE_INFINITY) {
1652 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1653 continue;
1654 }
bab85d4f 1655
d62a17ae 1656 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1657 best = rib_choose_best(new_fib, re);
1658 if (new_fib && best != new_fib)
1659 UNSET_FLAG(new_fib->status,
1660 ROUTE_ENTRY_CHANGED);
1661 new_fib = best;
1662 } else {
1663 best = rib_choose_best(new_selected, re);
1664 if (new_selected && best != new_selected)
1665 UNSET_FLAG(new_selected->status,
1666 ROUTE_ENTRY_CHANGED);
1667 new_selected = best;
1668 }
1669 if (best != re)
1670 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1671 } /* RNODE_FOREACH_RE */
1672
1673 /* If no FIB override route, use the selected route also for FIB */
1674 if (new_fib == NULL)
1675 new_fib = new_selected;
1676
1677 /* After the cycle is finished, the following pointers will be set:
1678 * old_selected --- RE entry currently having SELECTED
1679 * new_selected --- RE entry that is newly SELECTED
1680 * old_fib --- RE entry currently in kernel FIB
1681 * new_fib --- RE entry that is newly to be in kernel FIB
1682 *
1683 * new_selected will get SELECTED flag, and is going to be redistributed
1684 * the zclients. new_fib (which can be new_selected) will be installed
1685 * in kernel.
1686 */
1687
1688 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1689 zlog_debug(
1690 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1691 vrf_id, buf, (void *)old_selected, (void *)new_selected,
1692 (void *)old_fib, (void *)new_fib);
1693 }
446bb95e 1694
d62a17ae 1695 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1696 * fib == selected */
9d303b37
DL
1697 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1698 ROUTE_ENTRY_CHANGED);
d62a17ae 1699
1700 /* Update fib according to selection results */
1701 if (new_fib && old_fib)
1702 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1703 else if (new_fib)
1704 rib_process_add_fib(zvrf, rn, new_fib);
1705 else if (old_fib)
1706 rib_process_del_fib(zvrf, rn, old_fib);
1707
8cb41cd6 1708 /* Update SELECTED entry */
d62a17ae 1709 if (old_selected != new_selected || selected_changed) {
93bdadae 1710
6883bf8d 1711 if (new_selected && new_selected != new_fib)
d62a17ae 1712 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
41ec9222 1713
3cdba47a 1714 if (new_selected)
5af4b346
MS
1715 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1716
d62a17ae 1717 if (old_selected) {
1718 if (!new_selected)
1719 redistribute_delete(p, src_p, old_selected);
1720 if (old_selected != new_selected)
1721 UNSET_FLAG(old_selected->flags,
1722 ZEBRA_FLAG_SELECTED);
f857321e 1723 }
d62a17ae 1724 }
3e5c6e00 1725
d62a17ae 1726 /* Remove all RE entries queued for removal */
a2addae8 1727 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1728 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1729 if (IS_ZEBRA_DEBUG_RIB) {
1730 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1731 (void *)rn, (void *)re);
1732 }
1733 rib_unlink(rn, re);
1734 }
1735 }
4d38fdb4 1736
d62a17ae 1737 /*
1738 * Check if the dest can be deleted now.
1739 */
1740 rib_gc_dest(rn);
e96f9203
DO
1741}
1742
a1494c25
DS
1743static void zebra_rib_evaluate_mpls(struct route_node *rn)
1744{
1745 rib_dest_t *dest = rib_dest_from_rnode(rn);
1746 struct zebra_vrf *zvrf = vrf_info_lookup(VRF_DEFAULT);
1747
1748 if (!dest)
1749 return;
1750
1751 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_LSPS)) {
1752 if (IS_ZEBRA_DEBUG_MPLS)
1753 zlog_debug(
1754 "%u: Scheduling all LSPs upon RIB completion",
1755 zvrf_id(zvrf));
1756 zebra_mpls_lsp_schedule(zvrf);
1757 mpls_unmark_lsps_for_processing(rn);
1758 }
1759}
1760
e5ac2adf
MS
1761/*
1762 * Utility to match route with dplane context data
1763 */
1764static bool rib_route_match_ctx(const struct route_entry *re,
25779064
MS
1765 const struct zebra_dplane_ctx *ctx,
1766 bool is_update)
e5ac2adf
MS
1767{
1768 bool result = false;
1769
1770 if (is_update) {
1771 /*
1772 * In 'update' case, we test info about the 'previous' or
1773 * 'old' route
1774 */
1775 if ((re->type == dplane_ctx_get_old_type(ctx)) &&
1776 (re->instance == dplane_ctx_get_old_instance(ctx))) {
1777 result = true;
1778
1779 /* TODO -- we're using this extra test, but it's not
1780 * exactly clear why.
1781 */
1782 if (re->type == ZEBRA_ROUTE_STATIC &&
1783 (re->distance != dplane_ctx_get_old_distance(ctx) ||
1784 re->tag != dplane_ctx_get_old_tag(ctx))) {
1785 result = false;
1786 }
1787 }
1788
1789 } else {
1790 /*
1791 * Ordinary, single-route case using primary context info
1792 */
1793 if ((dplane_ctx_get_op(ctx) != DPLANE_OP_ROUTE_DELETE) &&
1794 CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1795 /* Skip route that's been deleted */
1796 goto done;
1797 }
1798
1799 if ((re->type == dplane_ctx_get_type(ctx)) &&
1800 (re->instance == dplane_ctx_get_instance(ctx))) {
1801 result = true;
1802
1803 /* TODO -- we're using this extra test, but it's not
1804 * exactly clear why.
1805 */
1806 if (re->type == ZEBRA_ROUTE_STATIC &&
1807 (re->distance != dplane_ctx_get_distance(ctx) ||
1808 re->tag != dplane_ctx_get_tag(ctx))) {
1809 result = false;
1810 }
1811 }
1812 }
1813
1814done:
1815
1816 return (result);
1817}
1818
3cdba47a
DS
1819static void zebra_rib_fixup_system(struct route_node *rn)
1820{
1821 struct route_entry *re;
1822
1823 RNODE_FOREACH_RE(rn, re) {
1824 struct nexthop *nhop;
1825
1826 if (!RIB_SYSTEM_ROUTE(re))
1827 continue;
1828
1829 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1830 continue;
1831
1832 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
1833
1834 for (ALL_NEXTHOPS(re->ng, nhop)) {
1835 if (CHECK_FLAG(nhop->flags, NEXTHOP_FLAG_RECURSIVE))
1836 continue;
1837
1838 SET_FLAG(nhop->flags, NEXTHOP_FLAG_FIB);
1839 }
1840 }
1841}
1842
78bf56b0
MS
1843/*
1844 * Update a route from a dplane context. This consolidates common code
1845 * that can be used in processing of results from FIB updates, and in
1846 * async notification processing.
1847 */
1848static int rib_update_re_from_ctx(struct route_entry *re,
1849 struct route_node *rn,
1850 const struct zebra_dplane_ctx *ctx)
1851{
1852 int result = 0;
1853 struct nexthop *nexthop, *ctx_nexthop;
1854 const struct prefix *dest_pfx, *src_pfx;
1855
1856 srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx);
1857
1858 /* Update zebra nexthop FIB flag for each
1859 * nexthop that was installed.
1860 */
5695d9ac 1861 for (ALL_NEXTHOPS(re->ng, nexthop)) {
78bf56b0 1862
5695d9ac 1863 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
78bf56b0
MS
1864 continue;
1865
5695d9ac
MS
1866 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx),
1867 ctx_nexthop)) {
1868
78bf56b0
MS
1869 if (nexthop_same(ctx_nexthop, nexthop))
1870 break;
1871 }
1872
5695d9ac
MS
1873 /* If the FIB doesn't know about the nexthop,
1874 * it's not installed
1875 */
1876 if (ctx_nexthop == NULL) {
1877 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
78bf56b0 1878 continue;
5695d9ac 1879 }
78bf56b0 1880
5695d9ac
MS
1881 if (CHECK_FLAG(ctx_nexthop->flags, NEXTHOP_FLAG_FIB))
1882 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
78bf56b0 1883 else
5695d9ac 1884 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
78bf56b0
MS
1885 }
1886
1887 /* Redistribute */
1888 redistribute_update(dest_pfx, src_pfx, re, NULL);
1889
1890 return result;
1891}
1892
e5ac2adf 1893/*
fad4d69c
MS
1894 * Helper to locate a zebra route-node from a dplane context. This is used
1895 * when processing dplane results, e.g. Note well: the route-node is returned
1896 * with a ref held - route_unlock_node() must be called eventually.
e5ac2adf 1897 */
fad4d69c
MS
1898static struct route_node *
1899rib_find_rn_from_ctx(const struct zebra_dplane_ctx *ctx)
e5ac2adf
MS
1900{
1901 struct route_table *table = NULL;
1902 struct route_node *rn = NULL;
e5ac2adf
MS
1903 const struct prefix *dest_pfx, *src_pfx;
1904
1905 /* Locate rn and re(s) from ctx */
1906
1907 table = zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx),
1908 dplane_ctx_get_safi(ctx),
1909 dplane_ctx_get_vrf(ctx),
1910 dplane_ctx_get_table(ctx));
1911 if (table == NULL) {
1912 if (IS_ZEBRA_DEBUG_DPLANE) {
fad4d69c 1913 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
e5ac2adf
MS
1914 dplane_ctx_get_afi(ctx),
1915 dplane_ctx_get_safi(ctx),
1916 dplane_ctx_get_vrf(ctx));
1917 }
1918 goto done;
1919 }
1920
fad4d69c
MS
1921 dest_pfx = dplane_ctx_get_dest(ctx);
1922 src_pfx = dplane_ctx_get_src(ctx);
1923
1924 rn = srcdest_rnode_get(table, dest_pfx,
1925 src_pfx ? (struct prefix_ipv6 *)src_pfx : NULL);
1926
1927done:
1928 return rn;
1929}
1930
1931
97f5b441 1932
fad4d69c
MS
1933/*
1934 * Route-update results processing after async dataplane update.
1935 */
1936static void rib_process_result(struct zebra_dplane_ctx *ctx)
1937{
1938 struct zebra_vrf *zvrf = NULL;
1939 struct route_node *rn = NULL;
1940 struct route_entry *re = NULL, *old_re = NULL, *rib;
1941 bool is_update = false;
1942 char dest_str[PREFIX_STRLEN] = "";
1943 enum dplane_op_e op;
1944 enum zebra_dplane_result status;
1945 const struct prefix *dest_pfx, *src_pfx;
1946 uint32_t seq;
1947
1948 zvrf = vrf_info_lookup(dplane_ctx_get_vrf(ctx));
e5ac2adf
MS
1949 dest_pfx = dplane_ctx_get_dest(ctx);
1950
1951 /* Note well: only capturing the prefix string if debug is enabled here;
1952 * unconditional log messages will have to generate the string.
1953 */
5709131c 1954 if (IS_ZEBRA_DEBUG_DPLANE)
e5ac2adf 1955 prefix2str(dest_pfx, dest_str, sizeof(dest_str));
e5ac2adf 1956
fad4d69c
MS
1957 /* Locate rn and re(s) from ctx */
1958 rn = rib_find_rn_from_ctx(ctx);
e5ac2adf
MS
1959 if (rn == NULL) {
1960 if (IS_ZEBRA_DEBUG_DPLANE) {
fe2c53d4 1961 zlog_debug("Failed to process dplane results: no route for %u:%s",
e5ac2adf
MS
1962 dplane_ctx_get_vrf(ctx), dest_str);
1963 }
1964 goto done;
1965 }
1966
1967 srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx);
1968
1969 op = dplane_ctx_get_op(ctx);
1970 status = dplane_ctx_get_status(ctx);
1971
c831033f 1972 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
f183e380 1973 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
e5ac2adf 1974 dplane_ctx_get_vrf(ctx), dest_str, ctx,
f183e380 1975 dplane_op2str(op), dplane_res2str(status));
e5ac2adf 1976
e5ac2adf
MS
1977 /*
1978 * Update is a bit of a special case, where we may have both old and new
1979 * routes to post-process.
1980 */
1981 is_update = dplane_ctx_is_update(ctx);
1982
1983 /*
1984 * Take a pass through the routes, look for matches with the context
1985 * info.
1986 */
1987 RNODE_FOREACH_RE(rn, rib) {
1988
1989 if (re == NULL) {
5709131c 1990 if (rib_route_match_ctx(rib, ctx, false))
e5ac2adf 1991 re = rib;
e5ac2adf
MS
1992 }
1993
1994 /* Check for old route match */
1995 if (is_update && (old_re == NULL)) {
5709131c 1996 if (rib_route_match_ctx(rib, ctx, true /*is_update*/))
e5ac2adf 1997 old_re = rib;
e5ac2adf
MS
1998 }
1999
2000 /* Have we found the routes we need to work on? */
5709131c 2001 if (re && ((!is_update || old_re)))
e5ac2adf 2002 break;
e5ac2adf
MS
2003 }
2004
699dae23
DS
2005 seq = dplane_ctx_get_seq(ctx);
2006
e5ac2adf
MS
2007 /*
2008 * Check sequence number(s) to detect stale results before continuing
2009 */
60f98b23 2010 if (re) {
699dae23 2011 if (re->dplane_sequence != seq) {
60f98b23
DS
2012 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2013 zlog_debug("%u:%s Stale dplane result for re %p",
2014 dplane_ctx_get_vrf(ctx),
2015 dest_str, re);
2016 } else
2017 UNSET_FLAG(re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
2018 }
2019
60f98b23
DS
2020 if (old_re) {
2021 if (old_re->dplane_sequence != dplane_ctx_get_old_seq(ctx)) {
2022 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2023 zlog_debug("%u:%s Stale dplane result for old_re %p",
2024 dplane_ctx_get_vrf(ctx),
2025 dest_str, old_re);
2026 } else
9ef0c6ba 2027 UNSET_FLAG(old_re->status, ROUTE_ENTRY_QUEUED);
e5ac2adf
MS
2028 }
2029
12e7fe3a 2030 switch (op) {
12e7fe3a
DS
2031 case DPLANE_OP_ROUTE_INSTALL:
2032 case DPLANE_OP_ROUTE_UPDATE:
2033 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2034 if (re) {
2035 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2036 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2037 }
b9f0e5ee
DS
2038 /*
2039 * On an update operation from the same route type
2040 * context retrieval currently has no way to know
2041 * which was the old and which was the new.
2042 * So don't unset our flags that we just set.
2043 * We know redistribution is ok because the
2044 * old_re in this case is used for nothing
2045 * more than knowing whom to contact if necessary.
2046 */
2047 if (old_re && old_re != re) {
677c1dd5
DS
2048 UNSET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2049 UNSET_FLAG(old_re->status,
2050 ROUTE_ENTRY_INSTALLED);
2051 }
12e7fe3a 2052
78bf56b0
MS
2053 /* Update zebra route based on the results in
2054 * the context struct. This also triggers
2055 * redistribution for the route.
2056 */
2057 if (re)
2058 rib_update_re_from_ctx(re, rn, ctx);
f183e380 2059
3cdba47a
DS
2060 /*
2061 * System routes are weird in that they
2062 * allow multiple to be installed that match
2063 * to the same prefix, so after we get the
2064 * result we need to clean them up so that
2065 * we can actually use them.
2066 */
2067 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2068 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2069 zebra_rib_fixup_system(rn);
2070
f4c6e2a8 2071 if (zvrf)
12e7fe3a 2072 zvrf->installs++;
f183e380 2073
12e7fe3a 2074 /* Notify route owner */
677c1dd5 2075 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_INSTALLED);
e5ac2adf 2076
12e7fe3a 2077 } else {
7a230a9d 2078 if (re) {
677c1dd5 2079 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
7a230a9d
DS
2080 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2081 } if (old_re)
677c1dd5
DS
2082 SET_FLAG(old_re->status, ROUTE_ENTRY_FAILED);
2083 if (re)
2084 zsend_route_notify_owner(re, dest_pfx,
2085 ZAPI_ROUTE_FAIL_INSTALL);
97f5b441 2086
12e7fe3a
DS
2087 zlog_warn("%u:%s: Route install failed",
2088 dplane_ctx_get_vrf(ctx),
2089 prefix2str(dest_pfx,
2090 dest_str, sizeof(dest_str)));
2091 }
2092 break;
2093 case DPLANE_OP_ROUTE_DELETE:
677c1dd5
DS
2094 if (re)
2095 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2096 /*
2097 * In the delete case, the zebra core datastructs were
2098 * updated (or removed) at the time the delete was issued,
2099 * so we're just notifying the route owner.
e5ac2adf 2100 */
12e7fe3a 2101 if (status == ZEBRA_DPLANE_REQUEST_SUCCESS) {
677c1dd5
DS
2102 if (re) {
2103 UNSET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
2104 UNSET_FLAG(re->status, ROUTE_ENTRY_FAILED);
2105 }
12e7fe3a 2106 zsend_route_notify_owner_ctx(ctx, ZAPI_ROUTE_REMOVED);
e5ac2adf 2107
12e7fe3a
DS
2108 if (zvrf)
2109 zvrf->removals++;
2110 } else {
677c1dd5
DS
2111 if (re)
2112 SET_FLAG(re->status, ROUTE_ENTRY_FAILED);
12e7fe3a
DS
2113 zsend_route_notify_owner_ctx(ctx,
2114 ZAPI_ROUTE_REMOVE_FAIL);
e5ac2adf 2115
12e7fe3a
DS
2116 zlog_warn("%u:%s: Route Deletion failure",
2117 dplane_ctx_get_vrf(ctx),
2118 prefix2str(dest_pfx,
2119 dest_str, sizeof(dest_str)));
2120 }
3cdba47a
DS
2121
2122 /*
2123 * System routes are weird in that they
2124 * allow multiple to be installed that match
2125 * to the same prefix, so after we get the
2126 * result we need to clean them up so that
2127 * we can actually use them.
2128 */
2129 if ((re && RIB_SYSTEM_ROUTE(re)) ||
2130 (old_re && RIB_SYSTEM_ROUTE(old_re)))
2131 zebra_rib_fixup_system(rn);
12e7fe3a 2132 break;
d37f4d6c
MS
2133 default:
2134 break;
e5ac2adf 2135 }
699dae23
DS
2136
2137 zebra_rib_evaluate_rn_nexthops(rn, seq);
a1494c25 2138 zebra_rib_evaluate_mpls(rn);
e5ac2adf
MS
2139done:
2140
5f27bcba
DS
2141 if (rn)
2142 route_unlock_node(rn);
2143
e5ac2adf
MS
2144 /* Return context to dataplane module */
2145 dplane_ctx_fini(&ctx);
2146}
2147
54818e3b
MS
2148/*
2149 * Handle notification from async dataplane: the dataplane has detected
2150 * some change to a route, and notifies zebra so that the control plane
2151 * can reflect that change.
2152 */
2153static void rib_process_dplane_notify(struct zebra_dplane_ctx *ctx)
2154{
2155 struct route_node *rn = NULL;
2156 struct route_entry *re = NULL, *rib;
2157 char dest_str[PREFIX_STRLEN] = "";
2158 const struct prefix *dest_pfx, *src_pfx;
2159 bool changed_p = false;
2160
2161 dest_pfx = dplane_ctx_get_dest(ctx);
2162
2163 /* Note well: only capturing the prefix string if debug is enabled here;
2164 * unconditional log messages will have to generate the string.
2165 */
2166 if (IS_ZEBRA_DEBUG_DPLANE)
2167 prefix2str(dest_pfx, dest_str, sizeof(dest_str));
2168
2169 /* Locate rn and re(s) from ctx */
2170 rn = rib_find_rn_from_ctx(ctx);
2171 if (rn == NULL) {
2172 if (IS_ZEBRA_DEBUG_DPLANE) {
2173 zlog_debug("Failed to process dplane notification: no route for %u:%s",
2174 dplane_ctx_get_vrf(ctx), dest_str);
2175 }
2176 goto done;
2177 }
2178
2179 route_unlock_node(rn);
2180
2181 srcdest_rnode_prefixes(rn, &dest_pfx, &src_pfx);
2182
2183 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2184 zlog_debug("%u:%s Processing dplane notif ctx %p",
2185 dplane_ctx_get_vrf(ctx), dest_str, ctx);
2186
2187 /*
2188 * Take a pass through the routes, look for matches with the context
2189 * info.
2190 */
2191 RNODE_FOREACH_RE(rn, rib) {
2192
2193 if (re == NULL) {
2194 if (rib_route_match_ctx(rib, ctx, false))
2195 re = rib;
2196 }
2197
2198 /* Have we found the route we need to work on? */
2199 if (re)
2200 break;
2201 }
2202
2203 /* No match? Nothing we can do */
2204 if (re == NULL) {
2205 if (IS_ZEBRA_DEBUG_DPLANE)
2206 zlog_debug("Unable to process dplane notification: no entry for %u:%s, type %d",
2207 dplane_ctx_get_vrf(ctx), dest_str,
2208 dplane_ctx_get_type(ctx));
2209
2210 goto done;
2211 }
2212
2213 /* Update zebra's nexthop FIB flags based on the context struct's
2214 * nexthops.
2215 */
2216 rib_update_re_from_ctx(re, rn, ctx);
2217
2218 /* TODO -- we'd like to know about this possibility? */
2219 if (!changed_p) {
2220 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL)
2221 zlog_debug("%u:%s No change from dplane notification",
2222 dplane_ctx_get_vrf(ctx), dest_str);
2223 }
2224
2225done:
2226 /* Return context to dataplane module */
2227 dplane_ctx_fini(&ctx);
2228}
2229
5110a0c6 2230/* Take a list of route_node structs and return 1, if there was a record
d62a17ae 2231 * picked from it and processed by rib_process(). Don't process more,
5110a0c6 2232 * than one RN record; operate only in the specified sub-queue.
e96f9203 2233 */
d7c0a89a 2234static unsigned int process_subq(struct list *subq, uint8_t qindex)
e96f9203 2235{
d62a17ae 2236 struct listnode *lnode = listhead(subq);
2237 struct route_node *rnode;
2238 rib_dest_t *dest;
2239 struct zebra_vrf *zvrf = NULL;
5110a0c6 2240
d62a17ae 2241 if (!lnode)
2242 return 0;
5110a0c6 2243
d62a17ae 2244 rnode = listgetdata(lnode);
2245 dest = rib_dest_from_rnode(rnode);
2246 if (dest)
2247 zvrf = rib_dest_vrf(dest);
41ec9222 2248
d62a17ae 2249 rib_process(rnode);
5110a0c6 2250
d62a17ae 2251 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
2252 char buf[SRCDEST2STR_BUFFER];
2253 srcdest_rnode2str(rnode, buf, sizeof(buf));
2254 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2255 zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
2256 }
41ec9222 2257
d62a17ae 2258 if (rnode->info)
2259 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
2260 RIB_ROUTE_QUEUED(qindex));
9fd92e3c 2261
67b9467f 2262#if 0
5110a0c6
SH
2263 else
2264 {
2265 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2266 __func__, rnode, rnode->lock);
2267 zlog_backtrace(LOG_DEBUG);
2268 }
67b9467f 2269#endif
d62a17ae 2270 route_unlock_node(rnode);
2271 list_delete_node(subq, lnode);
2272 return 1;
e96f9203
DO
2273}
2274
a1494c25 2275
fb018d25 2276/*
f183e380 2277 * Perform next-hop tracking processing after RIB updates.
fb018d25 2278 */
f183e380 2279static void do_nht_processing(void)
fb018d25 2280{
fb018d25
DS
2281}
2282
e96f9203 2283/* Dispatch the meta queue by picking, processing and unlocking the next RN from
d62a17ae 2284 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2285 * data
e96f9203
DO
2286 * is pointed to the meta queue structure.
2287 */
d62a17ae 2288static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
e96f9203 2289{
d62a17ae 2290 struct meta_queue *mq = data;
2291 unsigned i;
91f16812
MS
2292 uint32_t queue_len, queue_limit;
2293
2294 /* Ensure there's room for more dataplane updates */
2295 queue_limit = dplane_get_in_queue_limit();
2296 queue_len = dplane_get_in_queue_len();
2297 if (queue_len > queue_limit) {
2298 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
8b962e77
MS
2299 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2300 queue_len, queue_limit);
91f16812
MS
2301
2302 /* Ensure that the meta-queue is actually enqueued */
489a9614 2303 if (work_queue_empty(zrouter.ribq))
ea45a4e7 2304 work_queue_add(zrouter.ribq, zrouter.mq);
91f16812
MS
2305
2306 return WQ_QUEUE_BLOCKED;
2307 }
5110a0c6 2308
d62a17ae 2309 for (i = 0; i < MQ_SIZE; i++)
2310 if (process_subq(mq->subq[i], i)) {
2311 mq->size--;
2312 break;
2313 }
2314 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
e96f9203
DO
2315}
2316
f52ed677
DS
2317
2318/*
2319 * Look into the RN and queue it into the highest priority queue
2320 * at this point in time for processing.
2321 *
2322 * We will enqueue a route node only once per invocation.
2323 *
2324 * There are two possibilities here that should be kept in mind.
2325 * If the original invocation has not been pulled off for processing
2326 * yet, A subsuquent invocation can have a route entry with a better
2327 * meta queue index value and we can have a situation where
2328 * we might have the same node enqueued 2 times. Not necessarily
2329 * an optimal situation but it should be ok.
2330 *
2331 * The other possibility is that the original invocation has not
2332 * been pulled off for processing yet, A subsusquent invocation
2333 * doesn't have a route_entry with a better meta-queue and the
2334 * original metaqueue index value will win and we'll end up with
2335 * the route node enqueued once.
e96f9203 2336 */
d62a17ae 2337static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
e96f9203 2338{
f52ed677
DS
2339 struct route_entry *re = NULL, *curr_re = NULL;
2340 uint8_t qindex = MQ_SIZE, curr_qindex = MQ_SIZE;
5110a0c6 2341
f52ed677
DS
2342 RNODE_FOREACH_RE (rn, curr_re) {
2343 curr_qindex = route_info[curr_re->type].meta_q_map;
d62a17ae 2344
f52ed677
DS
2345 if (curr_qindex <= qindex) {
2346 re = curr_re;
2347 qindex = curr_qindex;
d62a17ae 2348 }
f52ed677 2349 }
5110a0c6 2350
f52ed677
DS
2351 if (!re)
2352 return;
5110a0c6 2353
f52ed677
DS
2354 /* Invariant: at this point we always have rn->info set. */
2355 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2356 RIB_ROUTE_QUEUED(qindex))) {
d62a17ae 2357 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2358 rnode_debug(rn, re->vrf_id,
f52ed677 2359 "rn %p is already queued in sub-queue %u",
d62a17ae 2360 (void *)rn, qindex);
f52ed677 2361 return;
d62a17ae 2362 }
f52ed677
DS
2363
2364 SET_FLAG(rib_dest_from_rnode(rn)->flags, RIB_ROUTE_QUEUED(qindex));
2365 listnode_add(mq->subq[qindex], rn);
2366 route_lock_node(rn);
2367 mq->size++;
2368
2369 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2370 rnode_debug(rn, re->vrf_id, "queued rn %p into sub-queue %u",
2371 (void *)rn, qindex);
4d38fdb4 2372}
2373
6d691129 2374/* Add route_node to work queue and schedule processing */
d62a17ae 2375void rib_queue_add(struct route_node *rn)
4d38fdb4 2376{
d62a17ae 2377 assert(rn);
fc328ac9 2378
d62a17ae 2379 /* Pointless to queue a route_node with no RIB entries to add or remove
2380 */
2381 if (!rnode_to_ribs(rn)) {
2382 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2383 __func__, (void *)rn, rn->lock);
2384 zlog_backtrace(LOG_DEBUG);
2385 return;
2386 }
4d38fdb4 2387
489a9614 2388 if (zrouter.ribq == NULL) {
e914ccbe 2389 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2390 "%s: work_queue does not exist!", __func__);
d62a17ae 2391 return;
2392 }
2393
2394 /*
2395 * The RIB queue should normally be either empty or holding the only
2396 * work_queue_item element. In the latter case this element would
2397 * hold a pointer to the meta queue structure, which must be used to
2398 * actually queue the route nodes to process. So create the MQ
2399 * holder, if necessary, then push the work into it in any case.
2400 * This semantics was introduced after 0.99.9 release.
2401 */
489a9614 2402 if (work_queue_empty(zrouter.ribq))
ea45a4e7 2403 work_queue_add(zrouter.ribq, zrouter.mq);
e96f9203 2404
ea45a4e7 2405 rib_meta_queue_add(zrouter.mq, rn);
fc328ac9 2406
d62a17ae 2407 return;
4d38fdb4 2408}
2409
5110a0c6
SH
2410/* Create new meta queue.
2411 A destructor function doesn't seem to be necessary here.
2412 */
d62a17ae 2413static struct meta_queue *meta_queue_new(void)
e96f9203 2414{
d62a17ae 2415 struct meta_queue *new;
2416 unsigned i;
5110a0c6 2417
d62a17ae 2418 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
e96f9203 2419
d62a17ae 2420 for (i = 0; i < MQ_SIZE; i++) {
2421 new->subq[i] = list_new();
2422 assert(new->subq[i]);
2423 }
5110a0c6 2424
d62a17ae 2425 return new;
e96f9203
DO
2426}
2427
d62a17ae 2428void meta_queue_free(struct meta_queue *mq)
5a8dfcd8 2429{
d62a17ae 2430 unsigned i;
5a8dfcd8 2431
d62a17ae 2432 for (i = 0; i < MQ_SIZE; i++)
6a154c88 2433 list_delete(&mq->subq[i]);
5a8dfcd8 2434
d62a17ae 2435 XFREE(MTYPE_WORK_QUEUE, mq);
5a8dfcd8
RW
2436}
2437
4d38fdb4 2438/* initialise zebra rib work queue */
2561d12e 2439static void rib_queue_init(void)
4d38fdb4 2440{
489a9614
DS
2441 if (!(zrouter.ribq = work_queue_new(zrouter.master,
2442 "route_node processing"))) {
e914ccbe 2443 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2444 "%s: could not initialise work queue!", __func__);
d62a17ae 2445 return;
2446 }
4d38fdb4 2447
d62a17ae 2448 /* fill in the work queue spec */
489a9614
DS
2449 zrouter.ribq->spec.workfunc = &meta_queue_process;
2450 zrouter.ribq->spec.errorfunc = NULL;
46a4e345 2451 zrouter.ribq->spec.completion_func = NULL;
d62a17ae 2452 /* XXX: TODO: These should be runtime configurable via vty */
489a9614
DS
2453 zrouter.ribq->spec.max_retries = 3;
2454 zrouter.ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
2455 zrouter.ribq->spec.retry = ZEBRA_RIB_PROCESS_RETRY_TIME;
d62a17ae 2456
ea45a4e7 2457 if (!(zrouter.mq = meta_queue_new())) {
e914ccbe 2458 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2459 "%s: could not initialise meta queue!", __func__);
d62a17ae 2460 return;
2461 }
2462 return;
718e3744 2463}
2464
434434f7
DS
2465rib_dest_t *zebra_rib_create_dest(struct route_node *rn)
2466{
2467 rib_dest_t *dest;
2468
2469 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
aa57abfb 2470 rnh_list_init(&dest->nht);
434434f7
DS
2471 route_lock_node(rn); /* rn route table reference */
2472 rn->info = dest;
2473 dest->rnode = rn;
2474
2475 return dest;
2476}
2477
6d691129
PJ
2478/* RIB updates are processed via a queue of pointers to route_nodes.
2479 *
2480 * The queue length is bounded by the maximal size of the routing table,
2481 * as a route_node will not be requeued, if already queued.
2482 *
f0f77c9a
DS
2483 * REs are submitted via rib_addnode or rib_delnode which set minimal
2484 * state, or static_install_route (when an existing RE is updated)
3c0755dc 2485 * and then submit route_node to queue for best-path selection later.
f0f77c9a 2486 * Order of add/delete state changes are preserved for any given RE.
6d691129 2487 *
f0f77c9a 2488 * Deleted REs are reaped during best-path selection.
6d691129
PJ
2489 *
2490 * rib_addnode
f0f77c9a
DS
2491 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2492 * |-------->| | best RE, if required
3c0755dc
PJ
2493 * | |
2494 * static_install->|->rib_addqueue...... -> rib_process
2495 * | |
2496 * |-------->| |-> rib_unlink
f0f77c9a
DS
2497 * |-> set ROUTE_ENTRY_REMOVE |
2498 * rib_delnode (RE freed)
6d691129 2499 *
9fd92e3c
AS
2500 * The 'info' pointer of a route_node points to a rib_dest_t
2501 * ('dest'). Queueing state for a route_node is kept on the dest. The
2502 * dest is created on-demand by rib_link() and is kept around at least
2503 * as long as there are ribs hanging off it (@see rib_gc_dest()).
d62a17ae 2504 *
6d691129
PJ
2505 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2506 *
2507 * - route_nodes: refcounted by:
9fd92e3c
AS
2508 * - dest attached to route_node:
2509 * - managed by: rib_link/rib_gc_dest
6d691129
PJ
2510 * - route_node processing queue
2511 * - managed by: rib_addqueue, rib_process.
2512 *
2513 */
d62a17ae 2514
f0f77c9a 2515/* Add RE to head of the route node. */
d62a17ae 2516static void rib_link(struct route_node *rn, struct route_entry *re, int process)
2517{
d62a17ae 2518 rib_dest_t *dest;
2519 afi_t afi;
2520 const char *rmap_name;
9fd92e3c 2521
d62a17ae 2522 assert(re && rn);
9fd92e3c 2523
d62a17ae 2524 dest = rib_dest_from_rnode(rn);
2525 if (!dest) {
2526 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2527 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
7a4bb9c5 2528
434434f7 2529 dest = zebra_rib_create_dest(rn);
d62a17ae 2530 }
2263a412 2531
045207e2 2532 re_list_add_head(&dest->routes, re);
d62a17ae 2533
2534 afi = (rn->p.family == AF_INET)
2535 ? AFI_IP
2536 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2537 if (is_zebra_import_table_enabled(afi, re->table)) {
2538 rmap_name = zebra_get_import_table_route_map(afi, re->table);
2539 zebra_add_import_table_entry(rn, re, rmap_name);
2540 } else if (process)
2541 rib_queue_add(rn);
2542}
2543
7e24fdf3
DS
2544static void rib_addnode(struct route_node *rn,
2545 struct route_entry *re, int process)
d62a17ae 2546{
2547 /* RE node has been un-removed before route-node is processed.
2548 * route_node must hence already be on the queue for processing..
2549 */
2550 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2551 if (IS_ZEBRA_DEBUG_RIB)
2552 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
2553 (void *)rn, (void *)re);
2554
2555 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2556 return;
2557 }
2558 rib_link(rn, re, process);
6d691129
PJ
2559}
2560
9fd92e3c
AS
2561/*
2562 * rib_unlink
2563 *
2564 * Detach a rib structure from a route_node.
2565 *
2566 * Note that a call to rib_unlink() should be followed by a call to
2567 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2568 * longer required to be deleted.
2569 */
d62a17ae 2570void rib_unlink(struct route_node *rn, struct route_entry *re)
6d691129 2571{
d62a17ae 2572 rib_dest_t *dest;
9fd92e3c 2573
d62a17ae 2574 assert(rn && re);
6d691129 2575
d62a17ae 2576 if (IS_ZEBRA_DEBUG_RIB)
2577 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
2578 (void *)re);
6d691129 2579
d62a17ae 2580 dest = rib_dest_from_rnode(rn);
6d691129 2581
045207e2 2582 re_list_del(&dest->routes, re);
7a4bb9c5 2583
2eb07de3
DS
2584 if (dest->selected_fib == re)
2585 dest->selected_fib = NULL;
2586
7ee30f28 2587 nexthops_free(re->ng.nexthop);
d62a17ae 2588 XFREE(MTYPE_RE, re);
2589}
2590
2591void rib_delnode(struct route_node *rn, struct route_entry *re)
2592{
2593 afi_t afi;
2594
2595 if (IS_ZEBRA_DEBUG_RIB)
2596 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
2597 (void *)rn, (void *)re);
2598 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2599
2600 afi = (rn->p.family == AF_INET)
2601 ? AFI_IP
2602 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2603 if (is_zebra_import_table_enabled(afi, re->table)) {
2604 zebra_del_import_table_entry(rn, re);
2605 /* Just clean up if non main table */
2606 if (IS_ZEBRA_DEBUG_RIB) {
2607 char buf[SRCDEST2STR_BUFFER];
2608 srcdest_rnode2str(rn, buf, sizeof(buf));
2da33d6b
DS
2609 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2610 re->vrf_id, buf, rn, re,
2611 zebra_route_string(re->type));
d62a17ae 2612 }
7a4bb9c5 2613
d62a17ae 2614 rib_unlink(rn, re);
2615 } else {
2616 rib_queue_add(rn);
2617 }
718e3744 2618}
2619
f0f77c9a 2620/* This function dumps the contents of a given RE entry into
dc95824a
DO
2621 * standard debug log. Calling function name and IP prefix in
2622 * question are passed as 1st and 2nd arguments.
2623 */
2624
d62a17ae 2625void _route_entry_dump(const char *func, union prefixconstptr pp,
2626 union prefixconstptr src_pp,
2627 const struct route_entry *re)
2628{
d62a17ae 2629 const struct prefix *src_p = src_pp.p;
2630 bool is_srcdst = src_p && src_p->prefixlen;
2631 char straddr[PREFIX_STRLEN];
2632 char srcaddr[PREFIX_STRLEN];
2633 struct nexthop *nexthop;
2634
2635 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
2636 (const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
2637 is_srcdst ? " from " : "",
2638 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
2639 : "",
2640 re->vrf_id);
cc54cfee
RW
2641 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2642 func, (unsigned long)re->uptime, re->type, re->instance,
2643 re->table);
d62a17ae 2644 zlog_debug(
2645 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2646 func, re->metric, re->mtu, re->distance, re->flags, re->status);
2647 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func,
2648 re->nexthop_num, re->nexthop_active_num);
2649
7ee30f28 2650 for (ALL_NEXTHOPS(re->ng, nexthop)) {
2d68a0f2
DS
2651 struct interface *ifp;
2652 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
2653
2654 switch (nexthop->type) {
2655 case NEXTHOP_TYPE_BLACKHOLE:
2656 sprintf(straddr, "Blackhole");
2657 break;
2658 case NEXTHOP_TYPE_IFINDEX:
2659 ifp = if_lookup_by_index(nexthop->ifindex,
2660 nexthop->vrf_id);
2661 sprintf(straddr, "%s", ifp ? ifp->name : "Unknown");
2662 break;
2663 case NEXTHOP_TYPE_IPV4:
2664 /* fallthrough */
2665 case NEXTHOP_TYPE_IPV4_IFINDEX:
2666 inet_ntop(AF_INET, &nexthop->gate, straddr,
2667 INET6_ADDRSTRLEN);
2668 break;
2669 case NEXTHOP_TYPE_IPV6:
2670 case NEXTHOP_TYPE_IPV6_IFINDEX:
2671 inet_ntop(AF_INET6, &nexthop->gate, straddr,
2672 INET6_ADDRSTRLEN);
2673 break;
2674 }
df38b099
DS
2675 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2676 func, (nexthop->rparent ? " NH" : "NH"), straddr,
2d68a0f2
DS
2677 nexthop->ifindex, vrf ? vrf->name : "Unknown",
2678 nexthop->vrf_id,
d62a17ae 2679 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
2680 ? "ACTIVE "
2681 : ""),
677c1dd5 2682 (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
d62a17ae 2683 ? "FIB "
2684 : ""),
2685 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
df38b099
DS
2686 ? "RECURSIVE "
2687 : ""),
2688 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)
2689 ? "ONLINK "
2690 : ""),
2691 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_MATCHED)
2692 ? "MATCHED "
2693 : ""),
2694 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_DUPLICATE)
2695 ? "DUPLICATE "
d62a17ae 2696 : ""));
2697 }
2698 zlog_debug("%s: dump complete", func);
dc95824a
DO
2699}
2700
2701/* This is an exported helper to rtm_read() to dump the strange
f0f77c9a 2702 * RE entry found by rib_lookup_ipv4_route()
dc95824a
DO
2703 */
2704
d62a17ae 2705void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2706{
2707 struct route_table *table;
2708 struct route_node *rn;
2709 struct route_entry *re;
2710 char prefix_buf[INET_ADDRSTRLEN];
2711
2712 /* Lookup table. */
2713 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2714 if (!table) {
e914ccbe 2715 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2716 "%s:%u zebra_vrf_table() returned NULL", __func__,
2717 vrf_id);
d62a17ae 2718 return;
2719 }
2720
2721 /* Scan the RIB table for exactly matching RE entry. */
2722 rn = route_node_lookup(table, (struct prefix *)p);
2723
2724 /* No route for this prefix. */
2725 if (!rn) {
32391aff 2726 zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
d62a17ae 2727 prefix2str((struct prefix *)p, prefix_buf,
2728 sizeof(prefix_buf)));
2729 return;
2730 }
2731
2732 /* Unlock node. */
2733 route_unlock_node(rn);
2734
2735 /* let's go */
a2addae8 2736 RNODE_FOREACH_RE (rn, re) {
32391aff
DS
2737 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2738 __func__, vrf_id,
2739 (void *)rn, (void *)re,
d62a17ae 2740 (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
2741 ? "removed"
2742 : "NOT removed"),
2743 (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
2744 ? "selected"
2745 : "NOT selected"));
2746 route_entry_dump(p, NULL, re);
2747 }
dc95824a
DO
2748}
2749
20e5ff0a
DO
2750/* Check if requested address assignment will fail due to another
2751 * route being installed by zebra in FIB already. Take necessary
2752 * actions, if needed: remove such a route from FIB and deSELECT
f0f77c9a 2753 * corresponding RE entry. Then put affected RN into RIBQ head.
20e5ff0a 2754 */
d62a17ae 2755void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2756{
2757 struct route_table *table;
2758 struct route_node *rn;
5f7a4718 2759 rib_dest_t *dest;
d62a17ae 2760
2761 if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
e914ccbe 2762 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2763 "%s:%u zebra_vrf_table() returned NULL", __func__,
2764 vrf_id);
d62a17ae 2765 return;
2766 }
2767
2768 /* No matches would be the simplest case. */
2769 if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
2770 return;
2771
2772 /* Unlock node. */
2773 route_unlock_node(rn);
2774
5f7a4718 2775 dest = rib_dest_from_rnode(rn);
d62a17ae 2776 /* Check all RE entries. In case any changes have to be done, requeue
2777 * the RN into RIBQ head. If the routing message about the new connected
2778 * route (generated by the IP address we are going to assign very soon)
2779 * comes before the RIBQ is processed, the new RE entry will join
2780 * RIBQ record already on head. This is necessary for proper
2781 * revalidation
2782 * of the rest of the RE.
2783 */
3cdba47a 2784 if (dest->selected_fib) {
5f7a4718
DS
2785 if (IS_ZEBRA_DEBUG_RIB) {
2786 char buf[PREFIX_STRLEN];
2787
2788 zlog_debug("%u:%s: freeing way for connected prefix",
2789 dest->selected_fib->vrf_id,
2790 prefix2str(&rn->p, buf, sizeof(buf)));
2791 route_entry_dump(&rn->p, NULL, dest->selected_fib);
d62a17ae 2792 }
5f7a4718 2793 rib_uninstall(rn, dest->selected_fib);
d62a17ae 2794 rib_queue_add(rn);
ade4a886 2795 }
20e5ff0a
DO
2796}
2797
d62a17ae 2798int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
2799 struct prefix_ipv6 *src_p, struct route_entry *re)
718e3744 2800{
d62a17ae 2801 struct route_table *table;
2802 struct route_node *rn;
40ecd8e4 2803 struct route_entry *same = NULL;
d62a17ae 2804 int ret = 0;
b4c034b0 2805
d62a17ae 2806 if (!re)
2807 return 0;
b4c034b0 2808
1f610a1f 2809 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
05737783 2810
d62a17ae 2811 /* Lookup table. */
7865c65d
RW
2812 table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
2813 if (!table) {
2814 XFREE(MTYPE_RE, re);
d62a17ae 2815 return 0;
7865c65d 2816 }
cddf391b 2817
d62a17ae 2818 /* Make it sure prefixlen is applied to the prefix. */
2819 apply_mask(p);
2820 if (src_p)
2821 apply_mask_ipv6(src_p);
718e3744 2822
d62a17ae 2823 /* Set default distance by route type. */
2824 if (re->distance == 0) {
0492eea0 2825 re->distance = route_distance(re->type);
718e3744 2826
d62a17ae 2827 /* iBGP distance is 200. */
2828 if (re->type == ZEBRA_ROUTE_BGP
2829 && CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
2830 re->distance = 200;
2831 }
718e3744 2832
d62a17ae 2833 /* Lookup route node.*/
2834 rn = srcdest_rnode_get(table, p, src_p);
718e3744 2835
40ecd8e4
DS
2836 /*
2837 * If same type of route are installed, treat it as a implicit
2838 * withdraw.
2839 * If the user has specified the No route replace semantics
2840 * for the install don't do a route replace.
2841 */
a2addae8 2842 RNODE_FOREACH_RE (rn, same) {
d62a17ae 2843 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
2844 continue;
41ec9222 2845
eb327fa5
RW
2846 if (same->type != re->type)
2847 continue;
2848 if (same->instance != re->instance)
2849 continue;
996c9314
LB
2850 if (same->type == ZEBRA_ROUTE_KERNEL
2851 && same->metric != re->metric)
eb327fa5 2852 continue;
40ecd8e4
DS
2853
2854 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2855 same->distance != re->distance)
2856 continue;
2857
844b3a87 2858 /*
40ecd8e4
DS
2859 * We should allow duplicate connected routes
2860 * because of IPv6 link-local routes and unnumbered
2861 * interfaces on Linux.
844b3a87
RW
2862 */
2863 if (same->type != ZEBRA_ROUTE_CONNECT)
d62a17ae 2864 break;
2865 }
718e3744 2866
cf363e1b
MS
2867 /* If this route is kernel/connected route, notify the dataplane. */
2868 if (RIB_SYSTEM_ROUTE(re)) {
2869 /* Notify dataplane */
2870 dplane_sys_route_add(rn, re);
2871 }
2872
d62a17ae 2873 /* Link new re to node.*/
2874 if (IS_ZEBRA_DEBUG_RIB) {
2da33d6b
DS
2875 rnode_debug(rn, re->vrf_id,
2876 "Inserting route rn %p, re %p (%s) existing %p",
2877 rn, re, zebra_route_string(re->type), same);
718e3744 2878
d62a17ae 2879 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2880 route_entry_dump(p, src_p, re);
718e3744 2881 }
6883bf8d
DS
2882
2883 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
d62a17ae 2884 rib_addnode(rn, re, 1);
2885 ret = 1;
6b0655a2 2886
d62a17ae 2887 /* Free implicit route.*/
2888 if (same) {
2889 rib_delnode(rn, same);
2890 ret = -1;
2891 }
718e3744 2892
d62a17ae 2893 route_unlock_node(rn);
2894 return ret;
2895}
2896
2897void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
d7c0a89a 2898 unsigned short instance, int flags, struct prefix *p,
fd36be7e 2899 struct prefix_ipv6 *src_p, const struct nexthop *nh,
40ecd8e4
DS
2900 uint32_t table_id, uint32_t metric, uint8_t distance,
2901 bool fromkernel)
d62a17ae 2902{
2903 struct route_table *table;
2904 struct route_node *rn;
2905 struct route_entry *re;
2906 struct route_entry *fib = NULL;
2907 struct route_entry *same = NULL;
fd36be7e 2908 struct nexthop *rtnh;
d62a17ae 2909 char buf2[INET6_ADDRSTRLEN];
5f7a4718 2910 rib_dest_t *dest;
d62a17ae 2911
1f610a1f 2912 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
d62a17ae 2913
2914 /* Lookup table. */
2915 table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
2916 if (!table)
2917 return;
2918
2919 /* Apply mask. */
2920 apply_mask(p);
2921 if (src_p)
2922 apply_mask_ipv6(src_p);
2923
2924 /* Lookup route node. */
2925 rn = srcdest_rnode_lookup(table, p, src_p);
2926 if (!rn) {
2927 char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
2928
2929 prefix2str(p, dst_buf, sizeof(dst_buf));
2930 if (src_p && src_p->prefixlen)
2931 prefix2str(src_p, src_buf, sizeof(src_buf));
2932 else
2933 src_buf[0] = '\0';
2934
2935 if (IS_ZEBRA_DEBUG_RIB)
2936 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id,
2937 dst_buf,
2938 (src_buf[0] != '\0') ? " from " : "",
2939 src_buf);
2940 return;
2941 }
718e3744 2942
5f7a4718
DS
2943 dest = rib_dest_from_rnode(rn);
2944 fib = dest->selected_fib;
2945
d62a17ae 2946 /* Lookup same type route. */
a2addae8 2947 RNODE_FOREACH_RE (rn, re) {
d62a17ae 2948 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2949 continue;
2950
d62a17ae 2951 if (re->type != type)
2952 continue;
2953 if (re->instance != instance)
2954 continue;
40ecd8e4
DS
2955 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2956 distance != re->distance)
2957 continue;
2958
996c9314 2959 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
f19435a8 2960 continue;
7ee30f28 2961 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
fd36be7e
DL
2962 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2963 if (rtnh->ifindex != nh->ifindex)
d62a17ae 2964 continue;
d62a17ae 2965 same = re;
2966 break;
2967 }
2968 /* Make sure that the route found has the same gateway. */
2969 else {
fd36be7e 2970 if (nh == NULL) {
d62a17ae 2971 same = re;
2972 break;
2973 }
7ee30f28 2974 for (ALL_NEXTHOPS(re->ng, rtnh))
fd36be7e 2975 if (nexthop_same_no_recurse(rtnh, nh)) {
d62a17ae 2976 same = re;
2977 break;
2978 }
2979 if (same)
2980 break;
2981 }
2982 }
2983 /* If same type of route can't be found and this message is from
2984 kernel. */
2985 if (!same) {
5dfeba19
DS
2986 /*
2987 * In the past(HA!) we could get here because
2988 * we were receiving a route delete from the
2989 * kernel and we're not marking the proto
2990 * as coming from it's appropriate originator.
2991 * Now that we are properly noticing the fact
2992 * that the kernel has deleted our route we
2993 * are not going to get called in this path
2994 * I am going to leave this here because
2995 * this might still work this way on non-linux
2996 * platforms as well as some weird state I have
2997 * not properly thought of yet.
2998 * If we can show that this code path is
2999 * dead then we can remove it.
3000 */
b8faa875 3001 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
d62a17ae 3002 if (IS_ZEBRA_DEBUG_RIB) {
2da33d6b
DS
3003 rnode_debug(rn, vrf_id,
3004 "rn %p, re %p (%s) was deleted from kernel, adding",
3005 rn, fib,
3006 zebra_route_string(fib->type));
d62a17ae 3007 }
3008 if (allow_delete) {
677c1dd5 3009 UNSET_FLAG(fib->status, ROUTE_ENTRY_INSTALLED);
d62a17ae 3010 /* Unset flags. */
7ee30f28 3011 for (rtnh = fib->ng.nexthop; rtnh;
fd36be7e
DL
3012 rtnh = rtnh->next)
3013 UNSET_FLAG(rtnh->flags,
d62a17ae 3014 NEXTHOP_FLAG_FIB);
3015
ed216282
DS
3016 /*
3017 * This is a non FRR route
3018 * as such we should mark
3019 * it as deleted
3020 */
5f7a4718 3021 dest->selected_fib = NULL;
d62a17ae 3022 } else {
3023 /* This means someone else, other than Zebra,
3024 * has deleted
3025 * a Zebra router from the kernel. We will add
3026 * it back */
3027 rib_install_kernel(rn, fib, NULL);
3028 }
3029 } else {
3030 if (IS_ZEBRA_DEBUG_RIB) {
fd36be7e 3031 if (nh)
d62a17ae 3032 rnode_debug(
3033 rn, vrf_id,
3034 "via %s ifindex %d type %d "
3035 "doesn't exist in rib",
36228974 3036 inet_ntop(afi2family(afi),
3037 &nh->gate, buf2,
3038 sizeof(buf2)),
3039 nh->ifindex, type);
d62a17ae 3040 else
3041 rnode_debug(
3042 rn, vrf_id,
fd36be7e
DL
3043 "type %d doesn't exist in rib",
3044 type);
d62a17ae 3045 }
3046 route_unlock_node(rn);
3047 return;
3048 }
3049 }
718e3744 3050
5dfeba19 3051 if (same) {
996c9314
LB
3052 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
3053 && !allow_delete) {
5dfeba19
DS
3054 rib_install_kernel(rn, same, NULL);
3055 route_unlock_node(rn);
3056
3057 return;
3058 }
6134fd82 3059
2b83602b 3060 /* Special handling for IPv4 or IPv6 routes sourced from
3061 * EVPN - the nexthop (and associated MAC) need to be
3062 * uninstalled if no more refs.
3063 */
90264d64 3064 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
6134fd82 3065 struct nexthop *tmp_nh;
3066
7ee30f28 3067 for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
6134fd82 3068 struct ipaddr vtep_ip;
3069
3070 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1ec31309 3071 if (afi == AFI_IP) {
3072 vtep_ip.ipa_type = IPADDR_V4;
3073 memcpy(&(vtep_ip.ipaddr_v4),
3074 &(tmp_nh->gate.ipv4),
3075 sizeof(struct in_addr));
3076 } else {
3077 vtep_ip.ipa_type = IPADDR_V6;
3078 memcpy(&(vtep_ip.ipaddr_v6),
3079 &(tmp_nh->gate.ipv6),
3080 sizeof(struct in6_addr));
3081 }
a317a9b9 3082 zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
6134fd82 3083 &vtep_ip, p);
3084 }
3085 }
cf363e1b
MS
3086
3087 /* Notify dplane if system route changes */
3088 if (RIB_SYSTEM_ROUTE(re))
3089 dplane_sys_route_del(rn, same);
3090
d62a17ae 3091 rib_delnode(rn, same);
5dfeba19 3092 }
05737783 3093
d62a17ae 3094 route_unlock_node(rn);
3095 return;
3096}
718e3744 3097
718e3744 3098
d7c0a89a
QY
3099int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
3100 unsigned short instance, int flags, struct prefix *p,
3101 struct prefix_ipv6 *src_p, const struct nexthop *nh,
3102 uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
3103 route_tag_t tag)
d62a17ae 3104{
3105 struct route_entry *re;
66af6845 3106 struct nexthop *nexthop;
718e3744 3107
66af6845 3108 /* Allocate new route_entry structure. */
d62a17ae 3109 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
d62a17ae 3110 re->type = type;
3111 re->instance = instance;
3112 re->distance = distance;
3113 re->flags = flags;
3114 re->metric = metric;
3115 re->mtu = mtu;
3116 re->table = table_id;
3117 re->vrf_id = vrf_id;
3118 re->nexthop_num = 0;
98572489 3119 re->uptime = monotime(NULL);
4e40b6d6 3120 re->tag = tag;
d62a17ae 3121
66af6845
RW
3122 /* Add nexthop. */
3123 nexthop = nexthop_new();
3124 *nexthop = *nh;
3125 route_entry_nexthop_add(re, nexthop);
718e3744 3126
66af6845 3127 return rib_add_multipath(afi, safi, p, src_p, re);
718e3744 3128}
3129
1c848137 3130/* Schedule routes of a particular table (address-family) based on event. */
d5b8c216 3131void rib_update_table(struct route_table *table, rib_update_event_t event)
d62a17ae 3132{
3133 struct route_node *rn;
3134 struct route_entry *re, *next;
3135
3136 /* Walk all routes and queue for processing, if appropriate for
3137 * the trigger event.
3138 */
3139 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
1ca60f2c
DS
3140 /*
3141 * If we are looking at a route node and the node
3142 * has already been queued we don't
3143 * need to queue it up again
3144 */
996c9314
LB
3145 if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
3146 RIB_ROUTE_ANY_QUEUED))
1ca60f2c 3147 continue;
d62a17ae 3148 switch (event) {
3149 case RIB_UPDATE_IF_CHANGE:
3150 /* Examine all routes that won't get processed by the
3151 * protocol or
3152 * triggered by nexthop evaluation (NHT). This would be
3153 * system,
3154 * kernel and certain static routes. Note that NHT will
3155 * get
3156 * triggered upon an interface event as connected routes
3157 * always
3158 * get queued for processing.
3159 */
a2addae8 3160 RNODE_FOREACH_RE_SAFE (rn, re, next) {
0a16efff
DS
3161 struct nexthop *nh;
3162
996c9314
LB
3163 if (re->type != ZEBRA_ROUTE_SYSTEM
3164 && re->type != ZEBRA_ROUTE_KERNEL
3165 && re->type != ZEBRA_ROUTE_CONNECT
3166 && re->type != ZEBRA_ROUTE_STATIC)
0a16efff
DS
3167 continue;
3168
3169 if (re->type != ZEBRA_ROUTE_STATIC) {
ade4a886
DS
3170 SET_FLAG(re->status,
3171 ROUTE_ENTRY_CHANGED);
0a16efff
DS
3172 rib_queue_add(rn);
3173 continue;
3174 }
3175
7ee30f28 3176 for (nh = re->ng.nexthop; nh; nh = nh->next)
0a16efff
DS
3177 if (!(nh->type == NEXTHOP_TYPE_IPV4
3178 || nh->type == NEXTHOP_TYPE_IPV6))
3179 break;
3180
3181 /* If we only have nexthops to a
3182 * gateway, NHT will
3183 * take care.
3184 */
ade4a886
DS
3185 if (nh) {
3186 SET_FLAG(re->status,
3187 ROUTE_ENTRY_CHANGED);
d62a17ae 3188 rib_queue_add(rn);
ade4a886 3189 }
d62a17ae 3190 }
3191 break;
3192
3193 case RIB_UPDATE_RMAP_CHANGE:
3194 case RIB_UPDATE_OTHER:
3195 /* Right now, examine all routes. Can restrict to a
3196 * protocol in
3197 * some cases (TODO).
3198 */
ade4a886
DS
3199 if (rnode_to_ribs(rn)) {
3200 RNODE_FOREACH_RE_SAFE (rn, re, next)
3201 SET_FLAG(re->status,
3202 ROUTE_ENTRY_CHANGED);
d62a17ae 3203 rib_queue_add(rn);
ade4a886 3204 }
d62a17ae 3205 break;
3206
3207 default:
3208 break;
3209 }
3210 }
b84c7253 3211}
3212
718e3744 3213/* RIB update function. */
d62a17ae 3214void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
718e3744 3215{
d62a17ae 3216 struct route_table *table;
1c848137 3217
d62a17ae 3218 /* Process routes of interested address-families. */
3219 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
d5b8c216 3220 if (table) {
3221 if (IS_ZEBRA_DEBUG_EVENT)
3222 zlog_debug("%s : AFI_IP event %d", __func__, event);
d62a17ae 3223 rib_update_table(table, event);
d5b8c216 3224 }
718e3744 3225
d62a17ae 3226 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
d5b8c216 3227 if (table) {
3228 if (IS_ZEBRA_DEBUG_EVENT)
3229 zlog_debug("%s : AFI_IP6 event %d", __func__, event);
d62a17ae 3230 rib_update_table(table, event);
d5b8c216 3231 }
718e3744 3232}
3233
718e3744 3234/* Delete self installed routes after zebra is relaunched. */
95a29032 3235void rib_sweep_table(struct route_table *table)
d62a17ae 3236{
3237 struct route_node *rn;
3238 struct route_entry *re;
3239 struct route_entry *next;
915902cb 3240 struct nexthop *nexthop;
d62a17ae 3241
915902cb
DS
3242 if (!table)
3243 return;
d62a17ae 3244
915902cb 3245 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
a2addae8 3246 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d4644d41 3247
915902cb
DS
3248 if (IS_ZEBRA_DEBUG_RIB)
3249 route_entry_dump(&rn->p, NULL, re);
3250
3251 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3252 continue;
3253
3254 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
3255 continue;
3256
d4644d41
DS
3257 /*
3258 * If routes are older than startup_time then
3259 * we know we read them in from the kernel.
3260 * As such we can safely remove them.
3261 */
3262 if (zrouter.startup_time < re->uptime)
3263 continue;
3264
915902cb
DS
3265 /*
3266 * So we are starting up and have received
3267 * routes from the kernel that we have installed
3268 * from a previous run of zebra but not cleaned
3269 * up ( say a kill -9 )
3270 * But since we haven't actually installed
3271 * them yet( we received them from the kernel )
3272 * we don't think they are active.
3273 * So let's pretend they are active to actually
3274 * remove them.
3275 * In all honesty I'm not sure if we should
3276 * mark them as active when we receive them
3277 * This is startup only so probably ok.
3278 *
3279 * If we ever decide to move rib_sweep_table
3280 * to a different spot (ie startup )
3281 * this decision needs to be revisited
3282 */
677c1dd5 3283 SET_FLAG(re->status, ROUTE_ENTRY_INSTALLED);
7ee30f28 3284 for (ALL_NEXTHOPS(re->ng, nexthop))
915902cb
DS
3285 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
3286
0c555cc6
DS
3287 rib_uninstall_kernel(rn, re);
3288 rib_delnode(rn, re);
915902cb
DS
3289 }
3290 }
718e3744 3291}
3292
3293/* Sweep all RIB tables. */
d4644d41 3294int rib_sweep_route(struct thread *t)
718e3744 3295{
d62a17ae 3296 struct vrf *vrf;
3297 struct zebra_vrf *zvrf;
78104b9b 3298
a2addae8 3299 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
915902cb
DS
3300 if ((zvrf = vrf->info) == NULL)
3301 continue;
3302
d62a17ae 3303 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
3304 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
3305 }
95a29032 3306
89272910 3307 zebra_router_sweep_route();
d4644d41
DS
3308
3309 return 0;
718e3744 3310}
2ea1ab1c
VT
3311
3312/* Remove specific by protocol routes from 'table'. */
d7c0a89a 3313unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
47a08aa9 3314 struct route_table *table)
d62a17ae 3315{
3316 struct route_node *rn;
3317 struct route_entry *re;
3318 struct route_entry *next;
3319 unsigned long n = 0;
3320
3321 if (table)
3322 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 3323 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 3324 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
3325 continue;
3326 if (re->type == proto
3327 && re->instance == instance) {
3328 rib_delnode(rn, re);
3329 n++;
3330 }
3331 }
3332 return n;
2ea1ab1c
VT
3333}
3334
3335/* Remove specific by protocol routes. */
d7c0a89a 3336unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2ea1ab1c 3337{
d62a17ae 3338 struct vrf *vrf;
3339 struct zebra_vrf *zvrf;
d8612e65 3340 struct other_route_table *ort;
d62a17ae 3341 unsigned long cnt = 0;
78104b9b 3342
d8612e65
DS
3343 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
3344 zvrf = vrf->info;
3345 if (!zvrf)
3346 continue;
78104b9b 3347
d8612e65
DS
3348 cnt += rib_score_proto_table(proto, instance,
3349 zvrf->table[AFI_IP][SAFI_UNICAST])
3350 + rib_score_proto_table(
3351 proto, instance,
3352 zvrf->table[AFI_IP6][SAFI_UNICAST]);
3353
81fddbe7 3354 frr_each(otable, &zvrf->other_tables, ort) cnt +=
d8612e65
DS
3355 rib_score_proto_table(proto, instance, ort->table);
3356 }
47a08aa9 3357
d62a17ae 3358 return cnt;
2ea1ab1c
VT
3359}
3360
718e3744 3361/* Close RIB and clean up kernel routes. */
d62a17ae 3362void rib_close_table(struct route_table *table)
718e3744 3363{
d62a17ae 3364 struct route_node *rn;
1e9f448f 3365 rib_table_info_t *info;
5f7a4718 3366 rib_dest_t *dest;
718e3744 3367
1e9f448f
DS
3368 if (!table)
3369 return;
9fd92e3c 3370
6ca30e9e 3371 info = route_table_get_info(table);
5adc2528 3372
5f7a4718
DS
3373 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
3374 dest = rib_dest_from_rnode(rn);
1e9f448f 3375
5f7a4718 3376 if (dest && dest->selected_fib) {
1e9f448f
DS
3377 if (info->safi == SAFI_UNICAST)
3378 hook_call(rib_update, rn, NULL);
3379
3cdba47a
DS
3380 rib_uninstall_kernel(rn, dest->selected_fib);
3381 dest->selected_fib = NULL;
1e9f448f 3382 }
5f7a4718 3383 }
718e3744 3384}
3385
9bd9717b
MS
3386/*
3387 * Handler for async dataplane results after a pseudowire installation
3388 */
3389static int handle_pw_result(struct zebra_dplane_ctx *ctx)
3390{
9bd9717b
MS
3391 struct zebra_pw *pw;
3392 struct zebra_vrf *vrf;
3393
3394 /* The pseudowire code assumes success - we act on an error
3395 * result for installation attempts here.
3396 */
3397 if (dplane_ctx_get_op(ctx) != DPLANE_OP_PW_INSTALL)
3398 goto done;
3399
3400 if (dplane_ctx_get_status(ctx) != ZEBRA_DPLANE_REQUEST_SUCCESS) {
3401 vrf = zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx));
3402 pw = zebra_pw_find(vrf, dplane_ctx_get_pw_ifname(ctx));
3403 if (pw)
3404 zebra_pw_install_failure(pw);
3405 }
3406
3407done:
3408
9f2d0354 3409 return 0;
9bd9717b
MS
3410}
3411
3412
7cdb1a84 3413/*
d37f4d6c
MS
3414 * Handle results from the dataplane system. Dequeue update context
3415 * structs, dispatch to appropriate internal handlers.
7cdb1a84
MS
3416 */
3417static int rib_process_dplane_results(struct thread *thread)
3418{
25779064 3419 struct zebra_dplane_ctx *ctx;
4c206c8f
MS
3420 struct dplane_ctx_q ctxlist;
3421
3422 /* Dequeue a list of completed updates with one lock/unlock cycle */
7cdb1a84
MS
3423
3424 do {
4c206c8f
MS
3425 TAILQ_INIT(&ctxlist);
3426
7cdb1a84
MS
3427 /* Take lock controlling queue of results */
3428 pthread_mutex_lock(&dplane_mutex);
3429 {
d37f4d6c 3430 /* Dequeue list of context structs */
4c206c8f 3431 dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
7cdb1a84
MS
3432 }
3433 pthread_mutex_unlock(&dplane_mutex);
3434
4c206c8f
MS
3435 /* Dequeue context block */
3436 ctx = dplane_ctx_dequeue(&ctxlist);
3437
3438 /* If we've emptied the results queue, we're done */
3439 if (ctx == NULL)
7cdb1a84 3440 break;
7cdb1a84 3441
4c206c8f 3442 while (ctx) {
d37f4d6c
MS
3443 switch (dplane_ctx_get_op(ctx)) {
3444 case DPLANE_OP_ROUTE_INSTALL:
3445 case DPLANE_OP_ROUTE_UPDATE:
3446 case DPLANE_OP_ROUTE_DELETE:
3447 rib_process_result(ctx);
3448 break;
3449
54818e3b
MS
3450 case DPLANE_OP_ROUTE_NOTIFY:
3451 rib_process_dplane_notify(ctx);
3452 break;
3453
d37f4d6c
MS
3454 case DPLANE_OP_LSP_INSTALL:
3455 case DPLANE_OP_LSP_UPDATE:
3456 case DPLANE_OP_LSP_DELETE:
3457 zebra_mpls_lsp_dplane_result(ctx);
3458 break;
3459
9bd9717b
MS
3460 case DPLANE_OP_PW_INSTALL:
3461 case DPLANE_OP_PW_UNINSTALL:
3462 handle_pw_result(ctx);
3463 break;
3464
cf363e1b
MS
3465 case DPLANE_OP_SYS_ROUTE_ADD:
3466 case DPLANE_OP_SYS_ROUTE_DELETE:
3467 /* No further processing in zebra for these. */
3468 dplane_ctx_fini(&ctx);
3469 break;
3470
d37f4d6c
MS
3471 default:
3472 /* Don't expect this: just return the struct? */
3473 dplane_ctx_fini(&ctx);
3474 break;
3475 } /* Dispatch by op code */
4c206c8f
MS
3476
3477 ctx = dplane_ctx_dequeue(&ctxlist);
3478 }
3479
5709131c 3480 } while (1);
7cdb1a84 3481
f183e380
MS
3482 /* Check for nexthop tracking processing after finishing with results */
3483 do_nht_processing();
3484
5709131c 3485 return 0;
7cdb1a84
MS
3486}
3487
3488/*
3489 * Results are returned from the dataplane subsystem, in the context of
1bcea841 3490 * the dataplane pthread. We enqueue the results here for processing by
7cdb1a84
MS
3491 * the main thread later.
3492 */
4c206c8f 3493static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
7cdb1a84
MS
3494{
3495 /* Take lock controlling queue of results */
3496 pthread_mutex_lock(&dplane_mutex);
3497 {
4c206c8f
MS
3498 /* Enqueue context blocks */
3499 dplane_ctx_list_append(&rib_dplane_q, ctxlist);
7cdb1a84
MS
3500 }
3501 pthread_mutex_unlock(&dplane_mutex);
3502
4c206c8f 3503 /* Ensure event is signalled to zebra main pthread */
3801e764 3504 thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
7cdb1a84
MS
3505 &t_dplane);
3506
5709131c 3507 return 0;
7cdb1a84
MS
3508}
3509
eaa2716d
SW
3510/*
3511 * Ensure there are no empty slots in the route_info array.
3512 * Every route type in zebra should be present there.
3513 */
3514static void check_route_info(void)
3515{
3516 int len = array_size(route_info);
3517
3518 /*
3519 * ZEBRA_ROUTE_SYSTEM is special cased since
3520 * its key is 0 anyway.
3521 *
3522 * ZEBRA_ROUTE_ALL is also ignored.
3523 */
3524 for (int i = 0; i < len; i++) {
3525 if (i == ZEBRA_ROUTE_SYSTEM || i == ZEBRA_ROUTE_ALL)
3526 continue;
3527 assert(route_info[i].key);
8dc7a759 3528 assert(route_info[i].meta_q_map < MQ_SIZE);
eaa2716d
SW
3529 }
3530}
3531
718e3744 3532/* Routing information base initialize. */
d62a17ae 3533void rib_init(void)
718e3744 3534{
eaa2716d
SW
3535 check_route_info();
3536
2561d12e 3537 rib_queue_init();
7cdb1a84
MS
3538
3539 /* Init dataplane, and register for results */
3540 pthread_mutex_init(&dplane_mutex, NULL);
3541 TAILQ_INIT(&rib_dplane_q);
4c206c8f 3542 zebra_dplane_init(rib_dplane_results);
718e3744 3543}
0915bb0c
AS
3544
3545/*
3546 * vrf_id_get_next
3547 *
3548 * Get the first vrf id that is greater than the given vrf id if any.
3549 *
3550 * Returns TRUE if a vrf id was found, FALSE otherwise.
3551 */
d62a17ae 3552static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 3553{
d62a17ae 3554 struct vrf *vrf;
b72ede27 3555
d62a17ae 3556 vrf = vrf_lookup_by_id(vrf_id);
3557 if (vrf) {
3558 vrf = RB_NEXT(vrf_id_head, vrf);
3559 if (vrf) {
3560 *next_id_p = vrf->vrf_id;
3561 return 1;
3562 }
3563 }
0915bb0c 3564
d62a17ae 3565 return 0;
0915bb0c
AS
3566}
3567
3568/*
3569 * rib_tables_iter_next
3570 *
3571 * Returns the next table in the iteration.
3572 */
d62a17ae 3573struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
3574{
3575 struct route_table *table;
3576
3577 /*
3578 * Array that helps us go over all AFI/SAFI combinations via one
3579 * index.
3580 */
3581 static struct {
3582 afi_t afi;
3583 safi_t safi;
3584 } afi_safis[] = {
3585 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
3586 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
3587 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
3588 };
3589
3590 table = NULL;
3591
3592 switch (iter->state) {
3593
3594 case RIB_TABLES_ITER_S_INIT:
3595 iter->vrf_id = VRF_DEFAULT;
3596 iter->afi_safi_ix = -1;
3597
3598 /* Fall through */
3599
3600 case RIB_TABLES_ITER_S_ITERATING:
3601 iter->afi_safi_ix++;
3602 while (1) {
3603
3604 while (iter->afi_safi_ix
7e3a1ec7 3605 < (int)array_size(afi_safis)) {
d62a17ae 3606 table = zebra_vrf_table(
3607 afi_safis[iter->afi_safi_ix].afi,
3608 afi_safis[iter->afi_safi_ix].safi,
3609 iter->vrf_id);
3610 if (table)
3611 break;
3612
3613 iter->afi_safi_ix++;
3614 }
3615
3616 /*
3617 * Found another table in this vrf.
3618 */
3619 if (table)
3620 break;
3621
3622 /*
3623 * Done with all tables in the current vrf, go to the
3624 * next
3625 * one.
3626 */
3627 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
3628 break;
3629
3630 iter->afi_safi_ix = 0;
3631 }
0915bb0c 3632
0915bb0c
AS
3633 break;
3634
d62a17ae 3635 case RIB_TABLES_ITER_S_DONE:
3636 return NULL;
0915bb0c
AS
3637 }
3638
d62a17ae 3639 if (table)
3640 iter->state = RIB_TABLES_ITER_S_ITERATING;
3641 else
3642 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c 3643
d62a17ae 3644 return table;
0915bb0c 3645}