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