1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
34 #include "sockunion.h"
35 #include "srcdest_table.h"
39 #include "workqueue.h"
41 #include "zebra/connected.h"
42 #include "zebra/debug.h"
43 #include "zebra/interface.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/rib.h"
47 #include "zebra/zapi_msg.h"
48 #include "zebra/zebra_errors.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/zebra_routemap.h"
53 #include "zebra/zebra_vrf.h"
54 #include "zebra/zebra_vxlan.h"
56 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
59 /* Should we allow non Quagga processes to delete our routes */
60 extern int allow_delete
;
62 /* Each route type's string and default distance value. */
66 } route_info
[ZEBRA_ROUTE_MAX
] = {
67 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
68 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
69 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
70 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
71 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
72 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
73 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
74 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
75 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
76 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
77 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
78 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
79 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
80 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
81 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
82 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
83 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
84 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
85 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
86 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
87 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
88 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
89 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
90 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150},
92 /* no entry/default: 150 */
95 /* RPF lookup behaviour */
96 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
99 static void __attribute__((format(printf
, 5, 6)))
100 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
101 int priority
, const char *msgfmt
, ...)
103 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
107 va_start(ap
, msgfmt
);
108 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
112 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
113 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
115 if (info
->safi
== SAFI_MULTICAST
)
116 strcat(buf
, " (MRIB)");
118 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
121 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
124 #define rnode_debug(node, vrf_id, ...) \
125 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
126 #define rnode_info(node, ...) \
127 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
129 uint8_t route_distance(int type
)
133 if ((unsigned)type
>= array_size(route_info
))
136 distance
= route_info
[type
].distance
;
141 int is_zebra_valid_kernel_table(uint32_t table_id
)
144 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
145 || (table_id
== RT_TABLE_COMPAT
))
152 int is_zebra_main_routing_table(uint32_t table_id
)
154 if ((table_id
== RT_TABLE_MAIN
)
155 || (table_id
== zebrad
.rtm_table_default
))
160 int zebra_check_addr(const struct prefix
*p
)
162 if (p
->family
== AF_INET
) {
165 addr
= p
->u
.prefix4
.s_addr
;
168 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
169 || IPV4_LINKLOCAL(addr
))
172 if (p
->family
== AF_INET6
) {
173 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
175 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
181 /* Add nexthop to the end of a rib node's nexthop list */
182 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
184 nexthop_add(&re
->ng
.nexthop
, nexthop
);
190 * copy_nexthop - copy a nexthop to the rib structure.
192 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
194 assert(!re
->ng
.nexthop
);
195 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
196 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
200 /* Delete specified nexthop from the list. */
201 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
204 nexthop
->next
->prev
= nexthop
->prev
;
206 nexthop
->prev
->next
= nexthop
->next
;
208 re
->ng
.nexthop
= nexthop
->next
;
213 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
217 struct nexthop
*nexthop
;
219 nexthop
= nexthop_new();
220 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
221 nexthop
->ifindex
= ifindex
;
222 nexthop
->vrf_id
= nh_vrf_id
;
224 route_entry_nexthop_add(re
, nexthop
);
229 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
230 struct in_addr
*ipv4
,
234 struct nexthop
*nexthop
;
236 nexthop
= nexthop_new();
237 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
238 nexthop
->vrf_id
= nh_vrf_id
;
239 nexthop
->gate
.ipv4
= *ipv4
;
241 nexthop
->src
.ipv4
= *src
;
243 route_entry_nexthop_add(re
, nexthop
);
248 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
249 struct in_addr
*ipv4
,
254 struct nexthop
*nexthop
;
255 struct interface
*ifp
;
257 nexthop
= nexthop_new();
258 nexthop
->vrf_id
= nh_vrf_id
;
259 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
260 nexthop
->gate
.ipv4
= *ipv4
;
262 nexthop
->src
.ipv4
= *src
;
263 nexthop
->ifindex
= ifindex
;
264 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
265 /*Pending: need to think if null ifp here is ok during bootup?
266 There was a crash because ifp here was coming to be NULL */
268 if (connected_is_unnumbered(ifp
)
269 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)
270 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
271 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
274 route_entry_nexthop_add(re
, nexthop
);
279 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
280 struct in6_addr
*ipv6
,
283 struct nexthop
*nexthop
;
285 nexthop
= nexthop_new();
286 nexthop
->vrf_id
= nh_vrf_id
;
287 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
288 nexthop
->gate
.ipv6
= *ipv6
;
290 route_entry_nexthop_add(re
, nexthop
);
295 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
296 struct in6_addr
*ipv6
,
300 struct nexthop
*nexthop
;
302 nexthop
= nexthop_new();
303 nexthop
->vrf_id
= nh_vrf_id
;
304 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
305 nexthop
->gate
.ipv6
= *ipv6
;
306 nexthop
->ifindex
= ifindex
;
307 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)
308 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
309 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
312 route_entry_nexthop_add(re
, nexthop
);
317 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
318 enum blackhole_type bh_type
)
320 struct nexthop
*nexthop
;
322 nexthop
= nexthop_new();
323 nexthop
->vrf_id
= VRF_DEFAULT
;
324 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
325 nexthop
->bh_type
= bh_type
;
327 route_entry_nexthop_add(re
, nexthop
);
332 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
333 struct nexthop
*nexthop
)
335 struct nexthop
*resolved_hop
;
337 resolved_hop
= nexthop_new();
338 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
340 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
341 switch (newhop
->type
) {
342 case NEXTHOP_TYPE_IPV4
:
343 case NEXTHOP_TYPE_IPV4_IFINDEX
:
344 /* If the resolving route specifies a gateway, use it */
345 resolved_hop
->type
= newhop
->type
;
346 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
348 if (newhop
->ifindex
) {
349 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
350 resolved_hop
->ifindex
= newhop
->ifindex
;
351 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
352 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
355 case NEXTHOP_TYPE_IPV6
:
356 case NEXTHOP_TYPE_IPV6_IFINDEX
:
357 resolved_hop
->type
= newhop
->type
;
358 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
360 if (newhop
->ifindex
) {
361 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
362 resolved_hop
->ifindex
= newhop
->ifindex
;
365 case NEXTHOP_TYPE_IFINDEX
:
366 /* If the resolving route is an interface route,
367 * it means the gateway we are looking up is connected
368 * to that interface. (The actual network is _not_ onlink).
369 * Therefore, the resolved route should have the original
370 * gateway as nexthop as it is directly connected.
372 * On Linux, we have to set the onlink netlink flag because
373 * otherwise, the kernel won't accept the route.
375 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
377 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
378 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
379 } else if (afi
== AFI_IP6
) {
380 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
381 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
383 resolved_hop
->ifindex
= newhop
->ifindex
;
385 case NEXTHOP_TYPE_BLACKHOLE
:
386 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
387 resolved_hop
->bh_type
= nexthop
->bh_type
;
391 /* Copy labels of the resolved route */
392 if (newhop
->nh_label
)
393 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
394 newhop
->nh_label
->num_labels
,
395 &newhop
->nh_label
->label
[0]);
397 resolved_hop
->rparent
= nexthop
;
398 nexthop_add(&nexthop
->resolved
, resolved_hop
);
401 /* If force flag is not set, do not modify falgs at all for uninstall
402 the route from FIB. */
403 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
404 struct nexthop
*nexthop
, int set
,
405 struct route_node
*top
)
408 struct route_table
*table
;
409 struct route_node
*rn
;
410 struct route_entry
*match
= NULL
;
412 struct nexthop
*newhop
;
413 struct interface
*ifp
;
416 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
417 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
418 nexthop
->ifindex
= 0;
421 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
422 nexthops_free(nexthop
->resolved
);
423 nexthop
->resolved
= NULL
;
427 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
428 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
431 /* Skip nexthops that have been filtered out due to route-map */
432 /* The nexthops are specific to this route and so the same */
433 /* nexthop for a different route may not have this flag set */
434 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
)) {
435 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
436 zlog_debug("\t%s: Nexthop Filtered",
437 __PRETTY_FUNCTION__
);
442 * Check to see if we should trust the passed in information
443 * for UNNUMBERED interfaces as that we won't find the GW
444 * address in the routing table.
446 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
447 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
448 if ((ifp
&& connected_is_unnumbered(ifp
))
449 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
450 if (if_is_operative(ifp
))
453 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
455 "\t%s: Onlink and interface %s is not operative",
456 __PRETTY_FUNCTION__
, ifp
->name
);
460 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
462 "\t%s: Interface %s is not unnumbered",
464 ifp
? ifp
->name
: "Unknown");
469 /* Make lookup prefix. */
470 memset(&p
, 0, sizeof(struct prefix
));
474 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
475 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
479 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
480 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
483 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
487 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
489 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
490 zlog_debug("\t%s: Table not found",
491 __PRETTY_FUNCTION__
);
495 rn
= route_node_match(table
, (struct prefix
*)&p
);
497 route_unlock_node(rn
);
499 /* Lookup should halt if we've matched against ourselves ('top',
500 * if specified) - i.e., we cannot have a nexthop NH1 is
501 * resolved by a route NH1. The exception is if the route is a
504 if (top
&& rn
== top
)
505 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
506 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
507 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
509 "\t%s: Matched against ourself and prefix length is not max bit length",
510 __PRETTY_FUNCTION__
);
514 /* Pick up selected route. */
515 /* However, do not resolve over default route unless explicitly
517 if (is_default_prefix(&rn
->p
)
518 && !rnh_resolve_via_default(p
.family
)) {
519 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
521 "\t:%s: Resolved against default route",
522 __PRETTY_FUNCTION__
);
526 dest
= rib_dest_from_rnode(rn
);
527 if (dest
&& dest
->selected_fib
528 && !CHECK_FLAG(dest
->selected_fib
->status
,
530 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
531 match
= dest
->selected_fib
;
533 /* If there is no selected route or matched route is EGP, go up
538 } while (rn
&& rn
->info
== NULL
);
545 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
546 /* Directly point connected route. */
547 newhop
= match
->ng
.nexthop
;
549 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
550 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
551 nexthop
->ifindex
= newhop
->ifindex
;
554 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
556 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
557 if (!CHECK_FLAG(newhop
->flags
,
560 if (CHECK_FLAG(newhop
->flags
,
561 NEXTHOP_FLAG_RECURSIVE
))
565 SET_FLAG(nexthop
->flags
,
566 NEXTHOP_FLAG_RECURSIVE
);
568 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
569 nexthop_set_resolved(afi
, newhop
,
575 re
->nexthop_mtu
= match
->mtu
;
576 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
577 zlog_debug("\t%s: Recursion failed to find",
578 __PRETTY_FUNCTION__
);
580 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
582 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
583 if (!CHECK_FLAG(newhop
->flags
,
588 SET_FLAG(nexthop
->flags
,
589 NEXTHOP_FLAG_RECURSIVE
);
590 nexthop_set_resolved(afi
, newhop
,
596 re
->nexthop_mtu
= match
->mtu
;
598 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
600 "\t%s: Static route unable to resolve",
601 __PRETTY_FUNCTION__
);
610 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
611 union g_addr
*addr
, struct route_node
**rn_out
)
614 struct route_table
*table
;
615 struct route_node
*rn
;
616 struct route_entry
*match
= NULL
;
617 struct nexthop
*newhop
;
620 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
624 memset(&p
, 0, sizeof(struct prefix
));
627 p
.u
.prefix4
= addr
->ipv4
;
628 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
630 p
.u
.prefix6
= addr
->ipv6
;
631 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
634 rn
= route_node_match(table
, (struct prefix
*)&p
);
639 route_unlock_node(rn
);
641 dest
= rib_dest_from_rnode(rn
);
642 if (dest
&& dest
->selected_fib
643 && !CHECK_FLAG(dest
->selected_fib
->status
,
644 ROUTE_ENTRY_REMOVED
))
645 match
= dest
->selected_fib
;
647 /* If there is no selected route or matched route is EGP, go up
652 } while (rn
&& rn
->info
== NULL
);
656 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
658 for (ALL_NEXTHOPS(match
->ng
, newhop
))
659 if (CHECK_FLAG(newhop
->flags
,
676 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
678 struct route_node
**rn_out
)
680 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
681 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
682 union g_addr gaddr
= {.ipv4
= addr
};
684 switch (ipv4_multicast_mode
) {
685 case MCAST_MRIB_ONLY
:
686 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
688 case MCAST_URIB_ONLY
:
689 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
690 case MCAST_NO_CONFIG
:
691 case MCAST_MIX_MRIB_FIRST
:
692 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
695 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
698 case MCAST_MIX_DISTANCE
:
699 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
700 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
702 re
= ure
->distance
< mre
->distance
? ure
: mre
;
708 case MCAST_MIX_PFXLEN
:
709 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
710 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
712 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
721 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
723 if (IS_ZEBRA_DEBUG_RIB
) {
725 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
727 zlog_debug("%s: %s: vrf: %u found %s, using %s",
728 __func__
, buf
, vrf_id
,
729 mre
? (ure
? "MRIB+URIB" : "MRIB")
730 : ure
? "URIB" : "nothing",
731 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
736 void multicast_mode_ipv4_set(enum multicast_mode mode
)
738 if (IS_ZEBRA_DEBUG_RIB
)
739 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
741 ipv4_multicast_mode
= mode
;
744 enum multicast_mode
multicast_mode_ipv4_get(void)
746 return ipv4_multicast_mode
;
749 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
751 struct route_table
*table
;
752 struct route_node
*rn
;
753 struct route_entry
*match
= NULL
;
754 struct nexthop
*nexthop
;
758 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
762 rn
= route_node_lookup(table
, (struct prefix
*)p
);
764 /* No route for this prefix. */
769 route_unlock_node(rn
);
770 dest
= rib_dest_from_rnode(rn
);
772 if (dest
&& dest
->selected_fib
773 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
774 match
= dest
->selected_fib
;
779 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
782 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
783 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
790 * This clone function, unlike its original rib_lookup_ipv4(), checks
791 * if specified IPv4 route record (prefix/mask -> gate) exists in
792 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
796 * 0: exact match found
797 * 1: a match was found with a different gate
798 * 2: connected route found
799 * 3: no matches found
801 int rib_lookup_ipv4_route(struct prefix_ipv4
*p
, union sockunion
*qgate
,
804 struct route_table
*table
;
805 struct route_node
*rn
;
806 struct route_entry
*match
= NULL
;
807 struct nexthop
*nexthop
;
812 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
814 return ZEBRA_RIB_LOOKUP_ERROR
;
816 /* Scan the RIB table for exactly matching RIB entry. */
817 rn
= route_node_lookup(table
, (struct prefix
*)p
);
819 /* No route for this prefix. */
821 return ZEBRA_RIB_NOTFOUND
;
824 route_unlock_node(rn
);
825 dest
= rib_dest_from_rnode(rn
);
827 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
829 if (dest
&& dest
->selected_fib
830 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
831 match
= dest
->selected_fib
;
833 /* None such found :( */
835 return ZEBRA_RIB_NOTFOUND
;
837 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
838 return ZEBRA_RIB_FOUND_CONNECTED
;
840 /* Ok, we have a cood candidate, let's check it's nexthop list... */
842 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
843 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
845 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip(qgate
))
846 return ZEBRA_RIB_FOUND_EXACT
;
847 if (IS_ZEBRA_DEBUG_RIB
) {
848 char gate_buf
[INET_ADDRSTRLEN
],
849 qgate_buf
[INET_ADDRSTRLEN
];
850 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
.s_addr
,
851 gate_buf
, INET_ADDRSTRLEN
);
852 inet_ntop(AF_INET
, &sockunion2ip(qgate
),
853 qgate_buf
, INET_ADDRSTRLEN
);
854 zlog_debug("%s: qgate == %s, %s == %s",
856 nexthop
->rparent
? "rgate" : "gate",
862 return ZEBRA_RIB_FOUND_NOGATE
;
864 return ZEBRA_RIB_NOTFOUND
;
867 #define RIB_SYSTEM_ROUTE(R) \
868 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
870 /* This function verifies reachability of one given nexthop, which can be
871 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
872 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
873 * nexthop->ifindex will be updated appropriately as well.
874 * An existing route map can turn (otherwise active) nexthop into inactive, but
877 * The return value is the final value of 'ACTIVE' flag.
880 static unsigned nexthop_active_check(struct route_node
*rn
,
881 struct route_entry
*re
,
882 struct nexthop
*nexthop
, int set
)
884 struct interface
*ifp
;
885 route_map_result_t ret
= RMAP_MATCH
;
887 char buf
[SRCDEST2STR_BUFFER
];
888 const struct prefix
*p
, *src_p
;
889 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
891 if (rn
->p
.family
== AF_INET
)
893 else if (rn
->p
.family
== AF_INET6
)
897 switch (nexthop
->type
) {
898 case NEXTHOP_TYPE_IFINDEX
:
899 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
900 if (ifp
&& if_is_operative(ifp
))
901 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
903 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
905 case NEXTHOP_TYPE_IPV4
:
906 case NEXTHOP_TYPE_IPV4_IFINDEX
:
908 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
909 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
911 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
913 case NEXTHOP_TYPE_IPV6
:
915 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
916 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
918 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
920 case NEXTHOP_TYPE_IPV6_IFINDEX
:
921 /* RFC 5549, v4 prefix with v6 NH */
922 if (rn
->p
.family
!= AF_INET
)
924 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
925 ifp
= if_lookup_by_index(nexthop
->ifindex
,
927 if (ifp
&& if_is_operative(ifp
))
928 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
930 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
932 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
933 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
935 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
938 case NEXTHOP_TYPE_BLACKHOLE
:
939 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
944 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
945 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
946 zlog_debug("\t%s: Unable to find a active nexthop",
947 __PRETTY_FUNCTION__
);
951 /* XXX: What exactly do those checks do? Do we support
952 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
953 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
954 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
955 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
957 /* The original code didn't determine the family correctly
958 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
959 * from the rib_table_info in those cases.
960 * Possibly it may be better to use only the rib_table_info
964 rib_table_info_t
*info
;
966 info
= srcdest_rnode_table_info(rn
);
970 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
972 /* It'll get set if required inside */
973 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
, nexthop
,
974 nexthop
->vrf_id
, re
->tag
);
975 if (ret
== RMAP_DENYMATCH
) {
976 if (IS_ZEBRA_DEBUG_RIB
) {
977 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
979 "%u:%s: Filtering out with NH out %s due to route map",
981 ifindex2ifname(nexthop
->ifindex
,
984 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
986 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
989 /* Iterate over all nexthops of the given RIB entry and refresh their
990 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
991 * nexthop is found to toggle the ACTIVE flag, the whole re structure
992 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
993 * transparently passed to nexthop_active_check().
995 * Return value is the new number of active nexthops.
998 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
1001 struct nexthop
*nexthop
;
1002 union g_addr prev_src
;
1003 unsigned int prev_active
, new_active
, old_num_nh
;
1004 ifindex_t prev_index
;
1005 old_num_nh
= re
->nexthop_active_num
;
1007 re
->nexthop_active_num
= 0;
1008 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1010 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
1011 /* No protocol daemon provides src and so we're skipping
1013 prev_src
= nexthop
->rmap_src
;
1014 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1015 prev_index
= nexthop
->ifindex
;
1016 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
1017 re
->nexthop_active_num
++;
1018 /* Don't allow src setting on IPv6 addr for now */
1019 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
1020 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
1021 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
1022 && prev_src
.ipv4
.s_addr
1023 != nexthop
->rmap_src
.ipv4
.s_addr
)
1024 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
1025 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
1026 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
1027 &nexthop
->rmap_src
.ipv6
)))) {
1028 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1029 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1033 if (old_num_nh
!= re
->nexthop_active_num
)
1034 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1036 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1037 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1040 return re
->nexthop_active_num
;
1044 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1045 * (nexthops) must have a label.
1047 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1049 struct nexthop
*nexthop
= NULL
;
1051 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1054 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1055 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1061 void kernel_route_rib_pass_fail(struct route_node
*rn
, const struct prefix
*p
,
1062 struct route_entry
*re
,
1063 enum zebra_dplane_status res
)
1065 struct nexthop
*nexthop
;
1066 char buf
[PREFIX_STRLEN
];
1069 dest
= rib_dest_from_rnode(rn
);
1072 case ZEBRA_DPLANE_INSTALL_SUCCESS
:
1073 dest
->selected_fib
= re
;
1074 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1075 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1078 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1079 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1081 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1083 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_INSTALLED
);
1085 case ZEBRA_DPLANE_INSTALL_FAILURE
:
1087 * I am not sure this is the right thing to do here
1088 * but the code always set selected_fib before
1089 * this assignment was moved here.
1091 dest
->selected_fib
= re
;
1093 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_FAIL_INSTALL
);
1094 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1095 "%u:%s: Route install failed", re
->vrf_id
,
1096 prefix2str(p
, buf
, sizeof(buf
)));
1098 case ZEBRA_DPLANE_DELETE_SUCCESS
:
1100 * The case where selected_fib is not re is
1101 * when we have received a system route
1102 * that is overriding our installed route
1103 * as such we should leave the selected_fib
1106 if (dest
->selected_fib
== re
)
1107 dest
->selected_fib
= NULL
;
1108 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1109 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1111 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_REMOVED
);
1113 case ZEBRA_DPLANE_DELETE_FAILURE
:
1115 * Should we set this to NULL if the
1118 dest
->selected_fib
= NULL
;
1119 flog_err(EC_ZEBRA_DP_DELETE_FAIL
,
1120 "%u:%s: Route Deletion failure", re
->vrf_id
,
1121 prefix2str(p
, buf
, sizeof(buf
)));
1123 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_REMOVE_FAIL
);
1125 case ZEBRA_DPLANE_STATUS_NONE
:
1130 /* Update flag indicates whether this is a "replace" or not. Currently, this
1131 * is only used for IPv4.
1133 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1134 struct route_entry
*old
)
1136 struct nexthop
*nexthop
;
1137 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1138 const struct prefix
*p
, *src_p
;
1139 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1141 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1143 if (info
->safi
!= SAFI_UNICAST
) {
1144 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1145 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1148 struct nexthop
*prev
;
1150 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1151 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1152 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1153 if (prev
== nexthop
)
1155 if (nexthop_same_firsthop(nexthop
, prev
)) {
1156 SET_FLAG(nexthop
->flags
,
1157 NEXTHOP_FLAG_DUPLICATE
);
1165 * If this is a replace to a new RE let the originator of the RE
1166 * know that they've lost
1168 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1169 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1172 * Make sure we update the FPM any time we send new information to
1175 hook_call(rib_update
, rn
, "installing in kernel");
1176 switch (kernel_route_rib(rn
, p
, src_p
, old
, re
)) {
1177 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1179 EC_ZEBRA_DP_INVALID_RC
,
1180 "No current known DataPlane interfaces can return this, please fix");
1182 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1184 EC_ZEBRA_DP_INSTALL_FAIL
,
1185 "No current known Rib Install Failure cases, please fix");
1187 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1195 /* Uninstall the route from kernel. */
1196 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1198 struct nexthop
*nexthop
;
1199 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1200 const struct prefix
*p
, *src_p
;
1201 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1203 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1205 if (info
->safi
!= SAFI_UNICAST
) {
1206 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1207 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1212 * Make sure we update the FPM any time we send new information to
1215 hook_call(rib_update
, rn
, "uninstalling from kernel");
1216 switch (kernel_route_rib(rn
, p
, src_p
, re
, NULL
)) {
1217 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1219 EC_ZEBRA_DP_INVALID_RC
,
1220 "No current known DataPlane interfaces can return this, please fix");
1222 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1224 EC_ZEBRA_DP_INSTALL_FAIL
,
1225 "No current known RIB Install Failure cases, please fix");
1227 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1236 /* Uninstall the route from kernel. */
1237 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1239 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1240 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1242 if (dest
&& dest
->selected_fib
== re
) {
1243 if (info
->safi
== SAFI_UNICAST
)
1244 hook_call(rib_update
, rn
, "rib_uninstall");
1246 if (!RIB_SYSTEM_ROUTE(re
))
1247 rib_uninstall_kernel(rn
, re
);
1249 /* If labeled-unicast route, uninstall transit LSP. */
1250 if (zebra_rib_labeled_unicast(re
))
1251 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1254 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1255 const struct prefix
*p
, *src_p
;
1257 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1259 redistribute_delete(p
, src_p
, re
);
1260 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1265 * rib_can_delete_dest
1267 * Returns TRUE if the given dest can be deleted from the table.
1269 static int rib_can_delete_dest(rib_dest_t
*dest
)
1276 * Don't delete the dest if we have to update the FPM about this
1279 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1280 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1289 * Garbage collect the rib dest corresponding to the given route node
1292 * Returns TRUE if the dest was deleted, FALSE otherwise.
1294 int rib_gc_dest(struct route_node
*rn
)
1298 dest
= rib_dest_from_rnode(rn
);
1302 if (!rib_can_delete_dest(dest
))
1305 if (IS_ZEBRA_DEBUG_RIB
) {
1306 struct zebra_vrf
*zvrf
;
1308 zvrf
= rib_dest_vrf(dest
);
1309 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1313 XFREE(MTYPE_RIB_DEST
, dest
);
1317 * Release the one reference that we keep on the route node.
1319 route_unlock_node(rn
);
1323 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1324 struct route_entry
*new)
1326 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1328 hook_call(rib_update
, rn
, "new route selected");
1330 /* Update real nexthop. This may actually determine if nexthop is active
1332 if (!nexthop_active_update(rn
, new, 1)) {
1333 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1337 if (IS_ZEBRA_DEBUG_RIB
) {
1338 char buf
[SRCDEST2STR_BUFFER
];
1339 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1340 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1341 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1344 /* If labeled-unicast route, install transit LSP. */
1345 if (zebra_rib_labeled_unicast(new))
1346 zebra_mpls_lsp_install(zvrf
, rn
, new);
1348 if (!RIB_SYSTEM_ROUTE(new))
1349 rib_install_kernel(rn
, new, NULL
);
1351 dest
->selected_fib
= new;
1353 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1356 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1357 struct route_entry
*old
)
1359 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1360 hook_call(rib_update
, rn
, "removing existing route");
1362 /* Uninstall from kernel. */
1363 if (IS_ZEBRA_DEBUG_RIB
) {
1364 char buf
[SRCDEST2STR_BUFFER
];
1365 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1366 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1367 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1370 /* If labeled-unicast route, uninstall transit LSP. */
1371 if (zebra_rib_labeled_unicast(old
))
1372 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1374 if (!RIB_SYSTEM_ROUTE(old
))
1375 rib_uninstall_kernel(rn
, old
);
1378 * We are setting this to NULL here
1379 * because that is what we traditionally
1380 * have been doing. I am not positive
1381 * that this is the right thing to do
1382 * but let's leave the code alone
1383 * for the RIB_SYSTEM_ROUTE case
1385 dest
->selected_fib
= NULL
;
1388 /* Update nexthop for route, reset changed flag. */
1389 nexthop_active_update(rn
, old
, 1);
1390 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1393 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1394 struct route_node
*rn
,
1395 struct route_entry
*old
,
1396 struct route_entry
*new)
1398 struct nexthop
*nexthop
= NULL
;
1400 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1403 * We have to install or update if a new route has been selected or
1404 * something has changed.
1406 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1407 hook_call(rib_update
, rn
, "updating existing route");
1409 /* Update the nexthop; we could determine here that nexthop is
1411 if (nexthop_active_update(rn
, new, 1))
1414 /* If nexthop is active, install the selected route, if
1416 * the install succeeds, cleanup flags for prior route, if
1421 if (IS_ZEBRA_DEBUG_RIB
) {
1422 char buf
[SRCDEST2STR_BUFFER
];
1423 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1426 "%u:%s: Updating route rn %p, re %p (type %d) "
1428 zvrf_id(zvrf
), buf
, rn
, new,
1429 new->type
, old
, old
->type
);
1432 "%u:%s: Updating route rn %p, re %p (type %d)",
1433 zvrf_id(zvrf
), buf
, rn
, new,
1437 /* If labeled-unicast route, uninstall transit LSP. */
1438 if (zebra_rib_labeled_unicast(old
))
1439 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1441 /* Non-system route should be installed. */
1442 if (!RIB_SYSTEM_ROUTE(new)) {
1443 /* If labeled-unicast route, install transit
1445 if (zebra_rib_labeled_unicast(new))
1446 zebra_mpls_lsp_install(zvrf
, rn
, new);
1448 rib_install_kernel(rn
, new, old
);
1451 * We do not need to install the
1452 * selected route because it
1453 * is already isntalled by
1454 * the system( ie not us )
1455 * so just mark it as winning
1456 * we do need to ensure that
1457 * if we uninstall a route
1458 * from ourselves we don't
1459 * over write this pointer
1461 dest
->selected_fib
= NULL
;
1463 /* If install succeeded or system route, cleanup flags
1464 * for prior route. */
1466 if (RIB_SYSTEM_ROUTE(new)) {
1467 if (!RIB_SYSTEM_ROUTE(old
))
1468 rib_uninstall_kernel(rn
, old
);
1470 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1471 nexthop
= nexthop
->next
)
1472 UNSET_FLAG(nexthop
->flags
,
1479 * If nexthop for selected route is not active or install
1481 * may need to uninstall and delete for redistribution.
1484 if (IS_ZEBRA_DEBUG_RIB
) {
1485 char buf
[SRCDEST2STR_BUFFER
];
1486 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1489 "%u:%s: Deleting route rn %p, re %p (type %d) "
1490 "old %p (type %d) - nexthop inactive",
1491 zvrf_id(zvrf
), buf
, rn
, new,
1492 new->type
, old
, old
->type
);
1495 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1496 zvrf_id(zvrf
), buf
, rn
, new,
1500 /* If labeled-unicast route, uninstall transit LSP. */
1501 if (zebra_rib_labeled_unicast(old
))
1502 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1504 if (!RIB_SYSTEM_ROUTE(old
))
1505 rib_uninstall_kernel(rn
, old
);
1507 dest
->selected_fib
= NULL
;
1511 * Same route selected; check if in the FIB and if not,
1513 * is housekeeping code to deal with race conditions in kernel
1515 * netlink reporting interface up before IPv4 or IPv6 protocol
1519 if (!RIB_SYSTEM_ROUTE(new)) {
1520 bool in_fib
= false;
1522 for (ALL_NEXTHOPS(new->ng
, nexthop
))
1523 if (CHECK_FLAG(nexthop
->flags
,
1524 NEXTHOP_FLAG_FIB
)) {
1529 rib_install_kernel(rn
, new, NULL
);
1533 /* Update prior route. */
1535 /* Set real nexthop. */
1536 nexthop_active_update(rn
, old
, 1);
1537 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1540 /* Clear changed flag. */
1541 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1544 /* Check if 'alternate' RIB entry is better than 'current'. */
1545 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1546 struct route_entry
*alternate
)
1548 if (current
== NULL
)
1551 /* filter route selection in following order:
1552 * - connected beats other types
1553 * - if both connected, loopback or vrf wins
1554 * - lower distance beats higher
1555 * - lower metric beats higher for equal distance
1556 * - last, hence oldest, route wins tie break.
1559 /* Connected routes. Check to see if either are a vrf
1560 * or loopback interface. If not, pick the last connected
1561 * route of the set of lowest metric connected routes.
1563 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1564 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1567 /* both are connected. are either loop or vrf? */
1568 struct nexthop
*nexthop
= NULL
;
1570 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1571 if (if_is_loopback_or_vrf(if_lookup_by_index(
1572 nexthop
->ifindex
, alternate
->vrf_id
)))
1576 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1577 if (if_is_loopback_or_vrf(if_lookup_by_index(
1578 nexthop
->ifindex
, current
->vrf_id
)))
1582 /* Neither are loop or vrf so pick best metric */
1583 if (alternate
->metric
<= current
->metric
)
1589 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1592 /* higher distance loses */
1593 if (alternate
->distance
< current
->distance
)
1595 if (current
->distance
< alternate
->distance
)
1598 /* metric tie-breaks equal distance */
1599 if (alternate
->metric
<= current
->metric
)
1605 /* Core function for processing routing information base. */
1606 static void rib_process(struct route_node
*rn
)
1608 struct route_entry
*re
;
1609 struct route_entry
*next
;
1610 struct route_entry
*old_selected
= NULL
;
1611 struct route_entry
*new_selected
= NULL
;
1612 struct route_entry
*old_fib
= NULL
;
1613 struct route_entry
*new_fib
= NULL
;
1614 struct route_entry
*best
= NULL
;
1615 char buf
[SRCDEST2STR_BUFFER
];
1617 struct zebra_vrf
*zvrf
= NULL
;
1618 const struct prefix
*p
, *src_p
;
1620 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1621 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1625 dest
= rib_dest_from_rnode(rn
);
1627 zvrf
= rib_dest_vrf(dest
);
1628 vrf_id
= zvrf_id(zvrf
);
1631 if (IS_ZEBRA_DEBUG_RIB
)
1632 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1634 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1635 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1638 * we can have rn's that have a NULL info pointer
1639 * (dest). As such let's not let the deref happen
1640 * additionally we know RNODE_FOREACH_RE_SAFE
1641 * will not iterate so we are ok.
1644 old_fib
= dest
->selected_fib
;
1646 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1647 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1649 "%u:%s: Examine re %p (type %d) status %x flags %x "
1650 "dist %d metric %d",
1651 vrf_id
, buf
, re
, re
->type
, re
->status
,
1652 re
->flags
, re
->distance
, re
->metric
);
1654 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1656 /* Currently selected re. */
1657 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1658 assert(old_selected
== NULL
);
1662 /* Skip deleted entries from selection */
1663 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1666 /* Skip unreachable nexthop. */
1667 /* This first call to nexthop_active_update is merely to
1669 * there's any change to nexthops associated with this RIB
1671 * rib_process() can be invoked due to an external event such as
1673 * down or due to next-hop-tracking evaluation. In the latter
1675 * a decision has already been made that the NHs have changed.
1677 * need to invoke a potentially expensive call again. Further,
1679 * the change might be in a recursive NH which is not caught in
1680 * the nexthop_active_update() code. Thus, we might miss changes
1684 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1685 && !nexthop_active_update(rn
, re
, 0)) {
1686 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1687 /* XXX: HERE BE DRAGONS!!!!!
1688 * In all honesty, I have not yet figured out
1690 * does or why the ROUTE_ENTRY_CHANGED test
1692 * or why we need to delete a route here, and
1694 * this concerns both selected and fib route, or
1697 /* This entry was denied by the 'ip protocol
1698 * table' route-map, we
1699 * need to delete it */
1700 if (re
!= old_selected
) {
1701 if (IS_ZEBRA_DEBUG_RIB
)
1703 "%s: %u:%s: imported via import-table but denied "
1704 "by the ip protocol table route-map",
1705 __func__
, vrf_id
, buf
);
1708 SET_FLAG(re
->status
,
1709 ROUTE_ENTRY_REMOVED
);
1715 /* Infinite distance. */
1716 if (re
->distance
== DISTANCE_INFINITY
) {
1717 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1721 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1722 best
= rib_choose_best(new_fib
, re
);
1723 if (new_fib
&& best
!= new_fib
)
1724 UNSET_FLAG(new_fib
->status
,
1725 ROUTE_ENTRY_CHANGED
);
1728 best
= rib_choose_best(new_selected
, re
);
1729 if (new_selected
&& best
!= new_selected
)
1730 UNSET_FLAG(new_selected
->status
,
1731 ROUTE_ENTRY_CHANGED
);
1732 new_selected
= best
;
1735 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1736 } /* RNODE_FOREACH_RE */
1738 /* If no FIB override route, use the selected route also for FIB */
1739 if (new_fib
== NULL
)
1740 new_fib
= new_selected
;
1742 /* After the cycle is finished, the following pointers will be set:
1743 * old_selected --- RE entry currently having SELECTED
1744 * new_selected --- RE entry that is newly SELECTED
1745 * old_fib --- RE entry currently in kernel FIB
1746 * new_fib --- RE entry that is newly to be in kernel FIB
1748 * new_selected will get SELECTED flag, and is going to be redistributed
1749 * the zclients. new_fib (which can be new_selected) will be installed
1753 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1755 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1756 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1757 (void *)old_fib
, (void *)new_fib
);
1760 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1761 * fib == selected */
1762 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1763 ROUTE_ENTRY_CHANGED
);
1765 /* Update fib according to selection results */
1766 if (new_fib
&& old_fib
)
1767 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1769 rib_process_add_fib(zvrf
, rn
, new_fib
);
1771 rib_process_del_fib(zvrf
, rn
, old_fib
);
1773 /* Redistribute SELECTED entry */
1774 if (old_selected
!= new_selected
|| selected_changed
) {
1775 struct nexthop
*nexthop
= NULL
;
1777 /* Check if we have a FIB route for the destination, otherwise,
1778 * don't redistribute it */
1780 for (ALL_NEXTHOPS(new_fib
->ng
, nexthop
)) {
1781 if (CHECK_FLAG(nexthop
->flags
,
1782 NEXTHOP_FLAG_FIB
)) {
1788 new_selected
= NULL
;
1790 if (new_selected
&& new_selected
!= new_fib
) {
1791 nexthop_active_update(rn
, new_selected
, 1);
1792 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1797 redistribute_delete(p
, src_p
, old_selected
);
1798 if (old_selected
!= new_selected
)
1799 UNSET_FLAG(old_selected
->flags
,
1800 ZEBRA_FLAG_SELECTED
);
1804 /* Install new or replace existing redistributed entry
1806 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1807 redistribute_update(p
, src_p
, new_selected
,
1812 /* Remove all RE entries queued for removal */
1813 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1814 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1815 if (IS_ZEBRA_DEBUG_RIB
) {
1816 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1817 (void *)rn
, (void *)re
);
1824 * Check if the dest can be deleted now.
1829 /* Take a list of route_node structs and return 1, if there was a record
1830 * picked from it and processed by rib_process(). Don't process more,
1831 * than one RN record; operate only in the specified sub-queue.
1833 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
1835 struct listnode
*lnode
= listhead(subq
);
1836 struct route_node
*rnode
;
1838 struct zebra_vrf
*zvrf
= NULL
;
1843 rnode
= listgetdata(lnode
);
1844 dest
= rib_dest_from_rnode(rnode
);
1846 zvrf
= rib_dest_vrf(dest
);
1850 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1851 char buf
[SRCDEST2STR_BUFFER
];
1852 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1853 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1854 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1858 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1859 RIB_ROUTE_QUEUED(qindex
));
1864 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1865 __func__
, rnode
, rnode
->lock
);
1866 zlog_backtrace(LOG_DEBUG
);
1869 route_unlock_node(rnode
);
1870 list_delete_node(subq
, lnode
);
1875 * All meta queues have been processed. Trigger next-hop evaluation.
1877 static void meta_queue_process_complete(struct work_queue
*dummy
)
1880 struct zebra_vrf
*zvrf
;
1882 /* Evaluate nexthops for those VRFs which underwent route processing.
1884 * should limit the evaluation to the necessary VRFs in most common
1887 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1889 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1892 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1893 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
,
1895 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0,
1896 RNH_IMPORT_CHECK_TYPE
, NULL
);
1897 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
,
1899 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0,
1900 RNH_IMPORT_CHECK_TYPE
, NULL
);
1903 /* Schedule LSPs for processing, if needed. */
1904 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1905 if (mpls_should_lsps_be_processed(zvrf
)) {
1906 if (IS_ZEBRA_DEBUG_MPLS
)
1908 "%u: Scheduling all LSPs upon RIB completion",
1910 zebra_mpls_lsp_schedule(zvrf
);
1911 mpls_unmark_lsps_for_processing(zvrf
);
1915 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1916 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1918 * is pointed to the meta queue structure.
1920 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
1922 struct meta_queue
*mq
= data
;
1925 for (i
= 0; i
< MQ_SIZE
; i
++)
1926 if (process_subq(mq
->subq
[i
], i
)) {
1930 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1934 * Map from rib types to queue type (priority) in meta queue
1936 static const uint8_t meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1937 [ZEBRA_ROUTE_SYSTEM
] = 4,
1938 [ZEBRA_ROUTE_KERNEL
] = 0,
1939 [ZEBRA_ROUTE_CONNECT
] = 0,
1940 [ZEBRA_ROUTE_STATIC
] = 1,
1941 [ZEBRA_ROUTE_RIP
] = 2,
1942 [ZEBRA_ROUTE_RIPNG
] = 2,
1943 [ZEBRA_ROUTE_OSPF
] = 2,
1944 [ZEBRA_ROUTE_OSPF6
] = 2,
1945 [ZEBRA_ROUTE_ISIS
] = 2,
1946 [ZEBRA_ROUTE_BGP
] = 3,
1947 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1948 [ZEBRA_ROUTE_EIGRP
] = 2,
1949 [ZEBRA_ROUTE_NHRP
] = 2,
1950 [ZEBRA_ROUTE_HSLS
] = 4,
1951 [ZEBRA_ROUTE_OLSR
] = 4,
1952 [ZEBRA_ROUTE_TABLE
] = 1,
1953 [ZEBRA_ROUTE_LDP
] = 4,
1954 [ZEBRA_ROUTE_VNC
] = 3,
1955 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1956 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1957 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1958 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1959 [ZEBRA_ROUTE_BABEL
] = 2,
1960 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1963 /* Look into the RN and queue it into one or more priority queues,
1964 * increasing the size for each data push done.
1966 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
1968 struct route_entry
*re
;
1970 RNODE_FOREACH_RE (rn
, re
) {
1971 uint8_t qindex
= meta_queue_map
[re
->type
];
1972 struct zebra_vrf
*zvrf
;
1974 /* Invariant: at this point we always have rn->info set. */
1975 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
1976 RIB_ROUTE_QUEUED(qindex
))) {
1977 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1980 "rn %p is already queued in sub-queue %u",
1981 (void *)rn
, qindex
);
1985 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
1986 RIB_ROUTE_QUEUED(qindex
));
1987 listnode_add(mq
->subq
[qindex
], rn
);
1988 route_lock_node(rn
);
1991 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1992 rnode_debug(rn
, re
->vrf_id
,
1993 "queued rn %p into sub-queue %u",
1994 (void *)rn
, qindex
);
1996 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
1998 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2002 /* Add route_node to work queue and schedule processing */
2003 void rib_queue_add(struct route_node
*rn
)
2007 /* Pointless to queue a route_node with no RIB entries to add or remove
2009 if (!rnode_to_ribs(rn
)) {
2010 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2011 __func__
, (void *)rn
, rn
->lock
);
2012 zlog_backtrace(LOG_DEBUG
);
2016 if (zebrad
.ribq
== NULL
) {
2017 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2018 "%s: work_queue does not exist!", __func__
);
2023 * The RIB queue should normally be either empty or holding the only
2024 * work_queue_item element. In the latter case this element would
2025 * hold a pointer to the meta queue structure, which must be used to
2026 * actually queue the route nodes to process. So create the MQ
2027 * holder, if necessary, then push the work into it in any case.
2028 * This semantics was introduced after 0.99.9 release.
2030 if (work_queue_empty(zebrad
.ribq
))
2031 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2033 rib_meta_queue_add(zebrad
.mq
, rn
);
2038 /* Create new meta queue.
2039 A destructor function doesn't seem to be necessary here.
2041 static struct meta_queue
*meta_queue_new(void)
2043 struct meta_queue
*new;
2046 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2048 for (i
= 0; i
< MQ_SIZE
; i
++) {
2049 new->subq
[i
] = list_new();
2050 assert(new->subq
[i
]);
2056 void meta_queue_free(struct meta_queue
*mq
)
2060 for (i
= 0; i
< MQ_SIZE
; i
++)
2061 list_delete(&mq
->subq
[i
]);
2063 XFREE(MTYPE_WORK_QUEUE
, mq
);
2066 /* initialise zebra rib work queue */
2067 static void rib_queue_init(struct zebra_t
*zebra
)
2072 work_queue_new(zebra
->master
, "route_node processing"))) {
2073 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2074 "%s: could not initialise work queue!", __func__
);
2078 /* fill in the work queue spec */
2079 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2080 zebra
->ribq
->spec
.errorfunc
= NULL
;
2081 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2082 /* XXX: TODO: These should be runtime configurable via vty */
2083 zebra
->ribq
->spec
.max_retries
= 3;
2084 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2086 if (!(zebra
->mq
= meta_queue_new())) {
2087 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2088 "%s: could not initialise meta queue!", __func__
);
2094 /* RIB updates are processed via a queue of pointers to route_nodes.
2096 * The queue length is bounded by the maximal size of the routing table,
2097 * as a route_node will not be requeued, if already queued.
2099 * REs are submitted via rib_addnode or rib_delnode which set minimal
2100 * state, or static_install_route (when an existing RE is updated)
2101 * and then submit route_node to queue for best-path selection later.
2102 * Order of add/delete state changes are preserved for any given RE.
2104 * Deleted REs are reaped during best-path selection.
2107 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2108 * |-------->| | best RE, if required
2110 * static_install->|->rib_addqueue...... -> rib_process
2112 * |-------->| |-> rib_unlink
2113 * |-> set ROUTE_ENTRY_REMOVE |
2114 * rib_delnode (RE freed)
2116 * The 'info' pointer of a route_node points to a rib_dest_t
2117 * ('dest'). Queueing state for a route_node is kept on the dest. The
2118 * dest is created on-demand by rib_link() and is kept around at least
2119 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2121 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2123 * - route_nodes: refcounted by:
2124 * - dest attached to route_node:
2125 * - managed by: rib_link/rib_gc_dest
2126 * - route_node processing queue
2127 * - managed by: rib_addqueue, rib_process.
2131 /* Add RE to head of the route node. */
2132 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2134 struct route_entry
*head
;
2137 const char *rmap_name
;
2141 dest
= rib_dest_from_rnode(rn
);
2143 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2144 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2146 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2147 route_lock_node(rn
); /* rn route table reference */
2152 head
= dest
->routes
;
2159 afi
= (rn
->p
.family
== AF_INET
)
2161 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2162 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2163 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2164 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2169 static void rib_addnode(struct route_node
*rn
,
2170 struct route_entry
*re
, int process
)
2172 /* RE node has been un-removed before route-node is processed.
2173 * route_node must hence already be on the queue for processing..
2175 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2176 if (IS_ZEBRA_DEBUG_RIB
)
2177 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2178 (void *)rn
, (void *)re
);
2180 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2183 rib_link(rn
, re
, process
);
2189 * Detach a rib structure from a route_node.
2191 * Note that a call to rib_unlink() should be followed by a call to
2192 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2193 * longer required to be deleted.
2195 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2201 if (IS_ZEBRA_DEBUG_RIB
)
2202 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2205 dest
= rib_dest_from_rnode(rn
);
2208 re
->next
->prev
= re
->prev
;
2211 re
->prev
->next
= re
->next
;
2213 dest
->routes
= re
->next
;
2216 if (dest
->selected_fib
== re
)
2217 dest
->selected_fib
= NULL
;
2219 nexthops_free(re
->ng
.nexthop
);
2220 XFREE(MTYPE_RE
, re
);
2223 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2227 if (IS_ZEBRA_DEBUG_RIB
)
2228 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2229 (void *)rn
, (void *)re
);
2230 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2232 afi
= (rn
->p
.family
== AF_INET
)
2234 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2235 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2236 zebra_del_import_table_entry(rn
, re
);
2237 /* Just clean up if non main table */
2238 if (IS_ZEBRA_DEBUG_RIB
) {
2239 char buf
[SRCDEST2STR_BUFFER
];
2240 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2242 "%u:%s: Freeing route rn %p, re %p (type %d)",
2243 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2252 /* This function dumps the contents of a given RE entry into
2253 * standard debug log. Calling function name and IP prefix in
2254 * question are passed as 1st and 2nd arguments.
2257 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2258 union prefixconstptr src_pp
,
2259 const struct route_entry
*re
)
2261 const struct prefix
*src_p
= src_pp
.p
;
2262 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2263 char straddr
[PREFIX_STRLEN
];
2264 char srcaddr
[PREFIX_STRLEN
];
2265 struct nexthop
*nexthop
;
2267 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2268 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2269 is_srcdst
? " from " : "",
2270 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2273 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2274 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2277 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2278 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2279 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2280 re
->nexthop_num
, re
->nexthop_active_num
);
2282 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2283 struct interface
*ifp
;
2284 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2286 switch (nexthop
->type
) {
2287 case NEXTHOP_TYPE_BLACKHOLE
:
2288 sprintf(straddr
, "Blackhole");
2290 case NEXTHOP_TYPE_IFINDEX
:
2291 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2293 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2295 case NEXTHOP_TYPE_IPV4
:
2297 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2298 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2301 case NEXTHOP_TYPE_IPV6
:
2302 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2303 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2307 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2308 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2309 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2311 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2314 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2317 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2321 zlog_debug("%s: dump complete", func
);
2324 /* This is an exported helper to rtm_read() to dump the strange
2325 * RE entry found by rib_lookup_ipv4_route()
2328 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2330 struct route_table
*table
;
2331 struct route_node
*rn
;
2332 struct route_entry
*re
;
2333 char prefix_buf
[INET_ADDRSTRLEN
];
2336 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2338 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2339 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2344 /* Scan the RIB table for exactly matching RE entry. */
2345 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2347 /* No route for this prefix. */
2349 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2350 prefix2str((struct prefix
*)p
, prefix_buf
,
2351 sizeof(prefix_buf
)));
2356 route_unlock_node(rn
);
2359 RNODE_FOREACH_RE (rn
, re
) {
2360 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2362 (void *)rn
, (void *)re
,
2363 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2366 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2369 route_entry_dump(p
, NULL
, re
);
2373 /* Check if requested address assignment will fail due to another
2374 * route being installed by zebra in FIB already. Take necessary
2375 * actions, if needed: remove such a route from FIB and deSELECT
2376 * corresponding RE entry. Then put affected RN into RIBQ head.
2378 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2380 struct route_table
*table
;
2381 struct route_node
*rn
;
2382 unsigned changed
= 0;
2385 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2386 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2387 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2392 /* No matches would be the simplest case. */
2393 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2397 route_unlock_node(rn
);
2399 dest
= rib_dest_from_rnode(rn
);
2400 /* Check all RE entries. In case any changes have to be done, requeue
2401 * the RN into RIBQ head. If the routing message about the new connected
2402 * route (generated by the IP address we are going to assign very soon)
2403 * comes before the RIBQ is processed, the new RE entry will join
2404 * RIBQ record already on head. This is necessary for proper
2406 * of the rest of the RE.
2408 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2410 if (IS_ZEBRA_DEBUG_RIB
) {
2411 char buf
[PREFIX_STRLEN
];
2413 zlog_debug("%u:%s: freeing way for connected prefix",
2414 dest
->selected_fib
->vrf_id
,
2415 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2416 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2418 rib_uninstall(rn
, dest
->selected_fib
);
2424 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2425 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2427 struct route_table
*table
;
2428 struct route_node
*rn
;
2429 struct route_entry
*same
= NULL
;
2430 struct nexthop
*nexthop
;
2436 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2439 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2441 XFREE(MTYPE_RE
, re
);
2445 /* Make it sure prefixlen is applied to the prefix. */
2448 apply_mask_ipv6(src_p
);
2450 /* Set default distance by route type. */
2451 if (re
->distance
== 0) {
2452 re
->distance
= route_distance(re
->type
);
2454 /* iBGP distance is 200. */
2455 if (re
->type
== ZEBRA_ROUTE_BGP
2456 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2460 /* Lookup route node.*/
2461 rn
= srcdest_rnode_get(table
, p
, src_p
);
2464 * If same type of route are installed, treat it as a implicit
2466 * If the user has specified the No route replace semantics
2467 * for the install don't do a route replace.
2469 RNODE_FOREACH_RE (rn
, same
) {
2470 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2473 if (same
->type
!= re
->type
)
2475 if (same
->instance
!= re
->instance
)
2477 if (same
->type
== ZEBRA_ROUTE_KERNEL
2478 && same
->metric
!= re
->metric
)
2481 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2482 same
->distance
!= re
->distance
)
2486 * We should allow duplicate connected routes
2487 * because of IPv6 link-local routes and unnumbered
2488 * interfaces on Linux.
2490 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2494 /* If this route is kernel route, set FIB flag to the route. */
2495 if (RIB_SYSTEM_ROUTE(re
))
2496 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2497 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2499 /* Link new re to node.*/
2500 if (IS_ZEBRA_DEBUG_RIB
) {
2503 "Inserting route rn %p, re %p (type %d) existing %p",
2504 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2506 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2507 route_entry_dump(p
, src_p
, re
);
2509 rib_addnode(rn
, re
, 1);
2512 /* Free implicit route.*/
2514 rib_delnode(rn
, same
);
2518 route_unlock_node(rn
);
2522 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2523 unsigned short instance
, int flags
, struct prefix
*p
,
2524 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2525 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2528 struct route_table
*table
;
2529 struct route_node
*rn
;
2530 struct route_entry
*re
;
2531 struct route_entry
*fib
= NULL
;
2532 struct route_entry
*same
= NULL
;
2533 struct nexthop
*rtnh
;
2534 char buf2
[INET6_ADDRSTRLEN
];
2537 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2540 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2547 apply_mask_ipv6(src_p
);
2549 /* Lookup route node. */
2550 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2552 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2554 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2555 if (src_p
&& src_p
->prefixlen
)
2556 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2560 if (IS_ZEBRA_DEBUG_RIB
)
2561 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2563 (src_buf
[0] != '\0') ? " from " : "",
2568 dest
= rib_dest_from_rnode(rn
);
2569 fib
= dest
->selected_fib
;
2571 /* Lookup same type route. */
2572 RNODE_FOREACH_RE (rn
, re
) {
2573 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2576 if (re
->type
!= type
)
2578 if (re
->instance
!= instance
)
2580 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2581 distance
!= re
->distance
)
2584 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2586 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2587 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2588 if (rtnh
->ifindex
!= nh
->ifindex
)
2593 /* Make sure that the route found has the same gateway. */
2599 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2600 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2608 /* If same type of route can't be found and this message is from
2612 * In the past(HA!) we could get here because
2613 * we were receiving a route delete from the
2614 * kernel and we're not marking the proto
2615 * as coming from it's appropriate originator.
2616 * Now that we are properly noticing the fact
2617 * that the kernel has deleted our route we
2618 * are not going to get called in this path
2619 * I am going to leave this here because
2620 * this might still work this way on non-linux
2621 * platforms as well as some weird state I have
2622 * not properly thought of yet.
2623 * If we can show that this code path is
2624 * dead then we can remove it.
2626 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2627 if (IS_ZEBRA_DEBUG_RIB
) {
2630 "rn %p, re %p (type %d) was deleted from kernel, adding",
2631 rn
, fib
, fib
->type
);
2635 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2637 UNSET_FLAG(rtnh
->flags
,
2641 * This is a non FRR route
2642 * as such we should mark
2645 dest
->selected_fib
= NULL
;
2647 /* This means someone else, other than Zebra,
2649 * a Zebra router from the kernel. We will add
2651 rib_install_kernel(rn
, fib
, NULL
);
2654 if (IS_ZEBRA_DEBUG_RIB
) {
2658 "via %s ifindex %d type %d "
2659 "doesn't exist in rib",
2660 inet_ntop(afi2family(afi
),
2667 "type %d doesn't exist in rib",
2670 route_unlock_node(rn
);
2676 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2678 rib_install_kernel(rn
, same
, NULL
);
2679 route_unlock_node(rn
);
2684 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2685 struct nexthop
*tmp_nh
;
2687 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2688 struct ipaddr vtep_ip
;
2690 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2691 if (afi
== AFI_IP
) {
2692 vtep_ip
.ipa_type
= IPADDR_V4
;
2693 memcpy(&(vtep_ip
.ipaddr_v4
),
2694 &(tmp_nh
->gate
.ipv4
),
2695 sizeof(struct in_addr
));
2697 vtep_ip
.ipa_type
= IPADDR_V6
;
2698 memcpy(&(vtep_ip
.ipaddr_v6
),
2699 &(tmp_nh
->gate
.ipv6
),
2700 sizeof(struct in6_addr
));
2702 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2706 rib_delnode(rn
, same
);
2709 route_unlock_node(rn
);
2714 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2715 unsigned short instance
, int flags
, struct prefix
*p
,
2716 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2717 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2720 struct route_entry
*re
;
2721 struct nexthop
*nexthop
;
2723 /* Allocate new route_entry structure. */
2724 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2726 re
->instance
= instance
;
2727 re
->distance
= distance
;
2729 re
->metric
= metric
;
2731 re
->table
= table_id
;
2732 re
->vrf_id
= vrf_id
;
2733 re
->nexthop_num
= 0;
2734 re
->uptime
= time(NULL
);
2738 nexthop
= nexthop_new();
2740 route_entry_nexthop_add(re
, nexthop
);
2742 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2745 /* Schedule routes of a particular table (address-family) based on event. */
2746 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2748 struct route_node
*rn
;
2749 struct route_entry
*re
, *next
;
2751 /* Walk all routes and queue for processing, if appropriate for
2752 * the trigger event.
2754 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2756 * If we are looking at a route node and the node
2757 * has already been queued we don't
2758 * need to queue it up again
2760 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2761 RIB_ROUTE_ANY_QUEUED
))
2764 case RIB_UPDATE_IF_CHANGE
:
2765 /* Examine all routes that won't get processed by the
2767 * triggered by nexthop evaluation (NHT). This would be
2769 * kernel and certain static routes. Note that NHT will
2771 * triggered upon an interface event as connected routes
2773 * get queued for processing.
2775 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2778 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
2779 && re
->type
!= ZEBRA_ROUTE_KERNEL
2780 && re
->type
!= ZEBRA_ROUTE_CONNECT
2781 && re
->type
!= ZEBRA_ROUTE_STATIC
)
2784 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2789 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
2790 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2791 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2794 /* If we only have nexthops to a
2803 case RIB_UPDATE_RMAP_CHANGE
:
2804 case RIB_UPDATE_OTHER
:
2805 /* Right now, examine all routes. Can restrict to a
2807 * some cases (TODO).
2809 if (rnode_to_ribs(rn
))
2819 /* RIB update function. */
2820 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2822 struct route_table
*table
;
2824 /* Process routes of interested address-families. */
2825 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2827 if (IS_ZEBRA_DEBUG_EVENT
)
2828 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
2829 rib_update_table(table
, event
);
2832 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2834 if (IS_ZEBRA_DEBUG_EVENT
)
2835 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
2836 rib_update_table(table
, event
);
2840 /* Delete self installed routes after zebra is relaunched. */
2841 void rib_sweep_table(struct route_table
*table
)
2843 struct route_node
*rn
;
2844 struct route_entry
*re
;
2845 struct route_entry
*next
;
2846 struct nexthop
*nexthop
;
2851 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2852 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2853 if (IS_ZEBRA_DEBUG_RIB
)
2854 route_entry_dump(&rn
->p
, NULL
, re
);
2856 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2859 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2863 * So we are starting up and have received
2864 * routes from the kernel that we have installed
2865 * from a previous run of zebra but not cleaned
2866 * up ( say a kill -9 )
2867 * But since we haven't actually installed
2868 * them yet( we received them from the kernel )
2869 * we don't think they are active.
2870 * So let's pretend they are active to actually
2872 * In all honesty I'm not sure if we should
2873 * mark them as active when we receive them
2874 * This is startup only so probably ok.
2876 * If we ever decide to move rib_sweep_table
2877 * to a different spot (ie startup )
2878 * this decision needs to be revisited
2880 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
2881 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2883 rib_uninstall_kernel(rn
, re
);
2884 rib_delnode(rn
, re
);
2889 /* Sweep all RIB tables. */
2890 void rib_sweep_route(void)
2893 struct zebra_vrf
*zvrf
;
2895 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2896 if ((zvrf
= vrf
->info
) == NULL
)
2899 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2900 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2903 zebra_ns_sweep_route();
2906 /* Remove specific by protocol routes from 'table'. */
2907 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
2908 struct route_table
*table
)
2910 struct route_node
*rn
;
2911 struct route_entry
*re
;
2912 struct route_entry
*next
;
2913 unsigned long n
= 0;
2916 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2917 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2918 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2920 if (re
->type
== proto
2921 && re
->instance
== instance
) {
2922 rib_delnode(rn
, re
);
2929 /* Remove specific by protocol routes. */
2930 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
2933 struct zebra_vrf
*zvrf
;
2934 unsigned long cnt
= 0;
2936 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2937 if ((zvrf
= vrf
->info
) != NULL
)
2938 cnt
+= rib_score_proto_table(
2940 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2941 + rib_score_proto_table(
2943 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2945 cnt
+= zebra_ns_score_proto(proto
, instance
);
2950 /* Close RIB and clean up kernel routes. */
2951 void rib_close_table(struct route_table
*table
)
2953 struct route_node
*rn
;
2954 rib_table_info_t
*info
;
2960 info
= route_table_get_info(table
);
2962 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2963 dest
= rib_dest_from_rnode(rn
);
2965 if (dest
&& dest
->selected_fib
) {
2966 if (info
->safi
== SAFI_UNICAST
)
2967 hook_call(rib_update
, rn
, NULL
);
2969 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
))
2970 rib_uninstall_kernel(rn
, dest
->selected_fib
);
2975 /* Routing information base initialize. */
2978 rib_queue_init(&zebrad
);
2984 * Get the first vrf id that is greater than the given vrf id if any.
2986 * Returns TRUE if a vrf id was found, FALSE otherwise.
2988 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2992 vrf
= vrf_lookup_by_id(vrf_id
);
2994 vrf
= RB_NEXT(vrf_id_head
, vrf
);
2996 *next_id_p
= vrf
->vrf_id
;
3005 * rib_tables_iter_next
3007 * Returns the next table in the iteration.
3009 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3011 struct route_table
*table
;
3014 * Array that helps us go over all AFI/SAFI combinations via one
3021 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3022 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3023 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3028 switch (iter
->state
) {
3030 case RIB_TABLES_ITER_S_INIT
:
3031 iter
->vrf_id
= VRF_DEFAULT
;
3032 iter
->afi_safi_ix
= -1;
3036 case RIB_TABLES_ITER_S_ITERATING
:
3037 iter
->afi_safi_ix
++;
3040 while (iter
->afi_safi_ix
3041 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3042 table
= zebra_vrf_table(
3043 afi_safis
[iter
->afi_safi_ix
].afi
,
3044 afi_safis
[iter
->afi_safi_ix
].safi
,
3049 iter
->afi_safi_ix
++;
3053 * Found another table in this vrf.
3059 * Done with all tables in the current vrf, go to the
3063 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3066 iter
->afi_safi_ix
= 0;
3071 case RIB_TABLES_ITER_S_DONE
:
3076 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3078 iter
->state
= RIB_TABLES_ITER_S_DONE
;