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