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
27 #include "zebra_memory.h"
31 #include "sockunion.h"
34 #include "workqueue.h"
40 #include "srcdest_table.h"
42 #include "zebra/rib.h"
44 #include "zebra/zebra_ns.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/debug.h"
50 #include "zebra/zebra_rnh.h"
51 #include "zebra/interface.h"
52 #include "zebra/connected.h"
53 #include "zebra/zebra_vxlan.h"
55 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
58 /* Should we allow non Quagga processes to delete our routes */
59 extern int allow_delete
;
61 /* Each route type's string and default distance value. */
65 } route_info
[ZEBRA_ROUTE_MAX
] = {
66 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
67 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
68 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
69 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
70 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
71 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
72 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
73 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
74 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
75 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
76 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
77 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
78 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
79 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
80 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
81 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
82 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
83 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
84 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
85 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
86 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
87 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
88 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
90 /* no entry/default: 150 */
93 /* RPF lookup behaviour */
94 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
97 static void __attribute__((format(printf
, 5, 6)))
98 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
99 int priority
, const char *msgfmt
, ...)
101 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
105 va_start(ap
, msgfmt
);
106 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
110 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
111 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
113 if (info
->safi
== SAFI_MULTICAST
)
114 strcat(buf
, " (MRIB)");
116 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
119 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
122 #define rnode_debug(node, vrf_id, ...) \
123 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
124 #define rnode_info(node, ...) \
125 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
127 uint8_t route_distance(int type
)
131 if ((unsigned)type
>= array_size(route_info
))
134 distance
= route_info
[type
].distance
;
139 int is_zebra_valid_kernel_table(u_int32_t table_id
)
141 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
145 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
146 || (table_id
== RT_TABLE_COMPAT
))
153 int is_zebra_main_routing_table(u_int32_t table_id
)
155 if ((table_id
== RT_TABLE_MAIN
)
156 || (table_id
== zebrad
.rtm_table_default
))
161 int zebra_check_addr(struct prefix
*p
)
163 if (p
->family
== AF_INET
) {
166 addr
= p
->u
.prefix4
.s_addr
;
169 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
170 || IPV4_LINKLOCAL(addr
))
173 if (p
->family
== AF_INET6
) {
174 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
176 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
182 /* Add nexthop to the end of a rib node's nexthop list */
183 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
185 nexthop_add(&re
->nexthop
, nexthop
);
191 * copy_nexthop - copy a nexthop to the rib structure.
193 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
195 assert(!re
->nexthop
);
196 copy_nexthops(&re
->nexthop
, nh
, NULL
);
197 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
201 /* Delete specified nexthop from the list. */
202 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
205 nexthop
->next
->prev
= nexthop
->prev
;
207 nexthop
->prev
->next
= nexthop
->next
;
209 re
->nexthop
= nexthop
->next
;
214 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
;
223 route_entry_nexthop_add(re
, nexthop
);
228 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
229 struct in_addr
*ipv4
,
232 struct nexthop
*nexthop
;
234 nexthop
= nexthop_new();
235 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
236 nexthop
->gate
.ipv4
= *ipv4
;
238 nexthop
->src
.ipv4
= *src
;
240 route_entry_nexthop_add(re
, nexthop
);
245 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
246 struct in_addr
*ipv4
,
250 struct nexthop
*nexthop
;
251 struct interface
*ifp
;
253 nexthop
= nexthop_new();
254 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
255 nexthop
->gate
.ipv4
= *ipv4
;
257 nexthop
->src
.ipv4
= *src
;
258 nexthop
->ifindex
= ifindex
;
259 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
260 /*Pending: need to think if null ifp here is ok during bootup?
261 There was a crash because ifp here was coming to be NULL */
263 if (connected_is_unnumbered(ifp
) ||
264 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_TYPE2_ROUTE
)) {
265 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
268 route_entry_nexthop_add(re
, nexthop
);
273 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
274 struct in6_addr
*ipv6
)
276 struct nexthop
*nexthop
;
278 nexthop
= nexthop_new();
279 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
280 nexthop
->gate
.ipv6
= *ipv6
;
282 route_entry_nexthop_add(re
, nexthop
);
287 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
288 struct in6_addr
*ipv6
,
291 struct nexthop
*nexthop
;
293 nexthop
= nexthop_new();
294 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
295 nexthop
->gate
.ipv6
= *ipv6
;
296 nexthop
->ifindex
= ifindex
;
298 route_entry_nexthop_add(re
, nexthop
);
303 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
304 enum blackhole_type bh_type
)
306 struct nexthop
*nexthop
;
308 nexthop
= nexthop_new();
309 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
310 nexthop
->bh_type
= bh_type
;
312 route_entry_nexthop_add(re
, nexthop
);
317 static void nexthop_set_resolved(afi_t afi
, struct nexthop
*newhop
,
318 struct nexthop
*nexthop
)
320 struct nexthop
*resolved_hop
;
322 resolved_hop
= nexthop_new();
323 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
325 switch (newhop
->type
) {
326 case NEXTHOP_TYPE_IPV4
:
327 case NEXTHOP_TYPE_IPV4_IFINDEX
:
328 /* If the resolving route specifies a gateway, use it */
329 resolved_hop
->type
= newhop
->type
;
330 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
332 if (newhop
->ifindex
) {
333 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
334 resolved_hop
->ifindex
= newhop
->ifindex
;
335 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
336 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
339 case NEXTHOP_TYPE_IPV6
:
340 case NEXTHOP_TYPE_IPV6_IFINDEX
:
341 resolved_hop
->type
= newhop
->type
;
342 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
344 if (newhop
->ifindex
) {
345 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
346 resolved_hop
->ifindex
= newhop
->ifindex
;
349 case NEXTHOP_TYPE_IFINDEX
:
350 /* If the resolving route is an interface route,
351 * it means the gateway we are looking up is connected
352 * to that interface. (The actual network is _not_ onlink).
353 * Therefore, the resolved route should have the original
354 * gateway as nexthop as it is directly connected.
356 * On Linux, we have to set the onlink netlink flag because
357 * otherwise, the kernel won't accept the route.
359 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
361 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
362 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
363 } else if (afi
== AFI_IP6
) {
364 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
365 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
367 resolved_hop
->ifindex
= newhop
->ifindex
;
369 case NEXTHOP_TYPE_BLACKHOLE
:
370 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
371 resolved_hop
->bh_type
= nexthop
->bh_type
;
375 resolved_hop
->rparent
= nexthop
;
376 nexthop_add(&nexthop
->resolved
, resolved_hop
);
379 /* If force flag is not set, do not modify falgs at all for uninstall
380 the route from FIB. */
381 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
382 struct nexthop
*nexthop
, int set
,
383 struct route_node
*top
)
386 struct route_table
*table
;
387 struct route_node
*rn
;
388 struct route_entry
*match
;
390 struct nexthop
*newhop
;
391 struct interface
*ifp
;
393 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
394 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
395 nexthop
->ifindex
= 0;
398 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
399 zebra_deregister_rnh_static_nexthops(re
->vrf_id
,
400 nexthop
->resolved
, top
);
401 nexthops_free(nexthop
->resolved
);
402 nexthop
->resolved
= NULL
;
406 /* Skip nexthops that have been filtered out due to route-map */
407 /* The nexthops are specific to this route and so the same */
408 /* nexthop for a different route may not have this flag set */
409 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
413 * Check to see if we should trust the passed in information
414 * for UNNUMBERED interfaces as that we won't find the GW
415 * address in the routing table.
417 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
418 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
419 if (ifp
&& connected_is_unnumbered(ifp
)) {
420 if (if_is_operative(ifp
))
428 /* Make lookup prefix. */
429 memset(&p
, 0, sizeof(struct prefix
));
433 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
434 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
438 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
439 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
442 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
446 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, re
->vrf_id
);
450 rn
= route_node_match(table
, (struct prefix
*)&p
);
452 route_unlock_node(rn
);
454 /* Lookup should halt if we've matched against ourselves ('top',
455 * if specified) - i.e., we cannot have a nexthop NH1 is
456 * resolved by a route NH1. The exception is if the route is a
459 if (top
&& rn
== top
)
460 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32)) ||
461 ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128)))
464 /* Pick up selected route. */
465 /* However, do not resolve over default route unless explicitly
467 if (is_default_prefix(&rn
->p
)
468 && !nh_resolve_via_default(p
.family
))
471 RNODE_FOREACH_RE (rn
, match
) {
472 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
475 /* if the next hop is imported from another table, skip
477 if (match
->type
== ZEBRA_ROUTE_TABLE
)
479 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
483 /* If there is no selected route or matched route is EGP, go up
488 } while (rn
&& rn
->info
== NULL
);
495 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
496 /* Directly point connected route. */
497 newhop
= match
->nexthop
;
499 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
500 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
501 nexthop
->ifindex
= newhop
->ifindex
;
504 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_INTERNAL
)) {
506 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
507 if (!CHECK_FLAG(newhop
->flags
,
510 if (CHECK_FLAG(newhop
->flags
,
511 NEXTHOP_FLAG_RECURSIVE
))
515 SET_FLAG(nexthop
->flags
,
516 NEXTHOP_FLAG_RECURSIVE
);
518 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
519 nexthop_set_resolved(afi
, newhop
,
525 re
->nexthop_mtu
= match
->mtu
;
527 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
529 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
530 if (!CHECK_FLAG(newhop
->flags
,
535 SET_FLAG(nexthop
->flags
,
536 NEXTHOP_FLAG_RECURSIVE
);
537 nexthop_set_resolved(afi
, newhop
,
543 re
->nexthop_mtu
= match
->mtu
;
552 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
553 union g_addr
*addr
, struct route_node
**rn_out
)
556 struct route_table
*table
;
557 struct route_node
*rn
;
558 struct route_entry
*match
;
559 struct nexthop
*newhop
;
562 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
566 memset(&p
, 0, sizeof(struct prefix
));
569 p
.u
.prefix4
= addr
->ipv4
;
570 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
572 p
.u
.prefix6
= addr
->ipv6
;
573 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
576 rn
= route_node_match(table
, (struct prefix
*)&p
);
579 route_unlock_node(rn
);
581 /* Pick up selected route. */
582 RNODE_FOREACH_RE (rn
, match
) {
583 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
585 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
589 /* If there is no selected route or matched route is EGP, go up
594 } while (rn
&& rn
->info
== NULL
);
598 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
600 for (ALL_NEXTHOPS(match
->nexthop
, newhop
))
601 if (CHECK_FLAG(newhop
->flags
,
618 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
620 struct route_node
**rn_out
)
622 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
623 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
624 union g_addr gaddr
= {.ipv4
= addr
};
626 switch (ipv4_multicast_mode
) {
627 case MCAST_MRIB_ONLY
:
628 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
630 case MCAST_URIB_ONLY
:
631 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
632 case MCAST_NO_CONFIG
:
633 case MCAST_MIX_MRIB_FIRST
:
634 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
637 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
640 case MCAST_MIX_DISTANCE
:
641 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
642 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
644 re
= ure
->distance
< mre
->distance
? ure
: mre
;
650 case MCAST_MIX_PFXLEN
:
651 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
652 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
654 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
663 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
665 if (IS_ZEBRA_DEBUG_RIB
) {
667 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
669 zlog_debug("%s: %s: found %s, using %s", __func__
, buf
,
670 mre
? (ure
? "MRIB+URIB" : "MRIB")
671 : ure
? "URIB" : "nothing",
672 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
677 void multicast_mode_ipv4_set(enum multicast_mode mode
)
679 if (IS_ZEBRA_DEBUG_RIB
)
680 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
682 ipv4_multicast_mode
= mode
;
685 enum multicast_mode
multicast_mode_ipv4_get(void)
687 return ipv4_multicast_mode
;
690 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
692 struct route_table
*table
;
693 struct route_node
*rn
;
694 struct route_entry
*match
;
695 struct nexthop
*nexthop
;
698 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
702 rn
= route_node_lookup(table
, (struct prefix
*)p
);
704 /* No route for this prefix. */
709 route_unlock_node(rn
);
711 RNODE_FOREACH_RE (rn
, match
) {
712 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
714 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
721 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
724 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
725 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
732 * This clone function, unlike its original rib_lookup_ipv4(), checks
733 * if specified IPv4 route record (prefix/mask -> gate) exists in
734 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
738 * 0: exact match found
739 * 1: a match was found with a different gate
740 * 2: connected route found
741 * 3: no matches found
743 int rib_lookup_ipv4_route(struct prefix_ipv4
*p
, union sockunion
*qgate
,
746 struct route_table
*table
;
747 struct route_node
*rn
;
748 struct route_entry
*match
;
749 struct nexthop
*nexthop
;
753 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
755 return ZEBRA_RIB_LOOKUP_ERROR
;
757 /* Scan the RIB table for exactly matching RIB entry. */
758 rn
= route_node_lookup(table
, (struct prefix
*)p
);
760 /* No route for this prefix. */
762 return ZEBRA_RIB_NOTFOUND
;
765 route_unlock_node(rn
);
767 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
769 RNODE_FOREACH_RE (rn
, match
) {
770 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
772 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
776 /* None such found :( */
778 return ZEBRA_RIB_NOTFOUND
;
780 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
781 return ZEBRA_RIB_FOUND_CONNECTED
;
783 /* Ok, we have a cood candidate, let's check it's nexthop list... */
785 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
786 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
788 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip(qgate
))
789 return ZEBRA_RIB_FOUND_EXACT
;
790 if (IS_ZEBRA_DEBUG_RIB
) {
791 char gate_buf
[INET_ADDRSTRLEN
],
792 qgate_buf
[INET_ADDRSTRLEN
];
793 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
.s_addr
,
794 gate_buf
, INET_ADDRSTRLEN
);
795 inet_ntop(AF_INET
, &sockunion2ip(qgate
),
796 qgate_buf
, INET_ADDRSTRLEN
);
797 zlog_debug("%s: qgate == %s, %s == %s",
799 nexthop
->rparent
? "rgate" : "gate",
805 return ZEBRA_RIB_FOUND_NOGATE
;
807 return ZEBRA_RIB_NOTFOUND
;
810 #define RIB_SYSTEM_ROUTE(R) \
811 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
813 /* This function verifies reachability of one given nexthop, which can be
814 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
815 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
816 * nexthop->ifindex will be updated appropriately as well.
817 * An existing route map can turn (otherwise active) nexthop into inactive, but
820 * The return value is the final value of 'ACTIVE' flag.
823 static unsigned nexthop_active_check(struct route_node
*rn
,
824 struct route_entry
*re
,
825 struct nexthop
*nexthop
, int set
)
827 struct interface
*ifp
;
828 route_map_result_t ret
= RMAP_MATCH
;
830 char buf
[SRCDEST2STR_BUFFER
];
831 struct prefix
*p
, *src_p
;
832 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
834 if (rn
->p
.family
== AF_INET
)
836 else if (rn
->p
.family
== AF_INET6
)
840 switch (nexthop
->type
) {
841 case NEXTHOP_TYPE_IFINDEX
:
842 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
843 if (ifp
&& if_is_operative(ifp
))
844 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
846 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
848 case NEXTHOP_TYPE_IPV4
:
849 case NEXTHOP_TYPE_IPV4_IFINDEX
:
851 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
852 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
853 else if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
854 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
856 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
858 case NEXTHOP_TYPE_IPV6
:
860 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
861 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
863 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
865 case NEXTHOP_TYPE_IPV6_IFINDEX
:
866 /* RFC 5549, v4 prefix with v6 NH */
867 if (rn
->p
.family
!= AF_INET
)
869 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
870 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
871 if (ifp
&& if_is_operative(ifp
))
872 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
874 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
876 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
877 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
879 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
882 case NEXTHOP_TYPE_BLACKHOLE
:
883 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
888 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
891 /* XXX: What exactly do those checks do? Do we support
892 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
893 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
894 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
895 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
897 /* The original code didn't determine the family correctly
898 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
899 * from the rib_table_info in those cases.
900 * Possibly it may be better to use only the rib_table_info
904 rib_table_info_t
*info
;
906 info
= srcdest_rnode_table_info(rn
);
910 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
912 /* It'll get set if required inside */
913 ret
= zebra_route_map_check(family
, re
->type
, p
, nexthop
, re
->vrf_id
,
915 if (ret
== RMAP_DENYMATCH
) {
916 if (IS_ZEBRA_DEBUG_RIB
) {
917 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
919 "%u:%s: Filtering out with NH out %s due to route map",
921 ifindex2ifname(nexthop
->ifindex
, re
->vrf_id
));
923 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
925 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
928 /* Iterate over all nexthops of the given RIB entry and refresh their
929 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
930 * nexthop is found to toggle the ACTIVE flag, the whole re structure
931 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
932 * transparently passed to nexthop_active_check().
934 * Return value is the new number of active nexthops.
937 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
940 struct nexthop
*nexthop
;
941 union g_addr prev_src
;
942 unsigned int prev_active
, new_active
, old_num_nh
;
943 ifindex_t prev_index
;
944 old_num_nh
= re
->nexthop_active_num
;
946 re
->nexthop_active_num
= 0;
947 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
949 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
950 /* No protocol daemon provides src and so we're skipping
952 prev_src
= nexthop
->rmap_src
;
953 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
954 prev_index
= nexthop
->ifindex
;
955 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
956 re
->nexthop_active_num
++;
957 /* Don't allow src setting on IPv6 addr for now */
958 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
959 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
960 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
961 && prev_src
.ipv4
.s_addr
962 != nexthop
->rmap_src
.ipv4
.s_addr
)
963 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
964 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
965 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
966 &nexthop
->rmap_src
.ipv6
)))) {
967 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
968 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
972 if (old_num_nh
!= re
->nexthop_active_num
)
973 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
975 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
976 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
979 return re
->nexthop_active_num
;
983 * Is this RIB labeled-unicast? It must be of type BGP and all paths
984 * (nexthops) must have a label.
986 int zebra_rib_labeled_unicast(struct route_entry
*re
)
988 struct nexthop
*nexthop
= NULL
;
990 if (re
->type
!= ZEBRA_ROUTE_BGP
)
993 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
994 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1000 void kernel_route_rib_pass_fail(struct prefix
*p
, struct route_entry
*re
,
1001 enum southbound_results res
)
1003 struct nexthop
*nexthop
;
1004 char buf
[PREFIX_STRLEN
];
1007 case SOUTHBOUND_INSTALL_SUCCESS
:
1008 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1009 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1012 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1013 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1015 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1017 zsend_route_notify_owner(re
->type
, re
->instance
, re
->vrf_id
,
1018 p
, ZAPI_ROUTE_INSTALLED
);
1020 case SOUTHBOUND_INSTALL_FAILURE
:
1021 zsend_route_notify_owner(re
->type
, re
->instance
, re
->vrf_id
,
1022 p
, ZAPI_ROUTE_FAIL_INSTALL
);
1023 zlog_warn("%u:%s: Route install failed", re
->vrf_id
,
1024 prefix2str(p
, buf
, sizeof(buf
)));
1026 case SOUTHBOUND_DELETE_SUCCESS
:
1027 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1028 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1030 case SOUTHBOUND_DELETE_FAILURE
:
1031 zlog_warn("%u:%s: Route Deletion failure", re
->vrf_id
,
1032 prefix2str(p
, buf
, sizeof(buf
)));
1037 /* Update flag indicates whether this is a "replace" or not. Currently, this
1038 * is only used for IPv4.
1040 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1041 struct route_entry
*old
)
1043 struct nexthop
*nexthop
;
1044 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1045 struct prefix
*p
, *src_p
;
1046 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1048 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1050 if (info
->safi
!= SAFI_UNICAST
) {
1051 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1052 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1055 struct nexthop
*prev
;
1057 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1058 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1059 for (ALL_NEXTHOPS(re
->nexthop
, prev
)) {
1060 if (prev
== nexthop
)
1062 if (nexthop_same_firsthop (nexthop
, prev
))
1064 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1072 * If this is a replace to a new RE let the originator of the RE
1073 * know that they've lost
1075 if (old
&& old
!= re
)
1076 zsend_route_notify_owner(old
->type
, old
->instance
,
1078 ZAPI_ROUTE_BETTER_ADMIN_WON
);
1081 * Make sure we update the FPM any time we send new information to
1084 hook_call(rib_update
, rn
, "installing in kernel");
1085 kernel_route_rib(p
, src_p
, old
, re
);
1091 /* Uninstall the route from kernel. */
1092 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1094 struct nexthop
*nexthop
;
1095 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1096 struct prefix
*p
, *src_p
;
1097 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1099 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1101 if (info
->safi
!= SAFI_UNICAST
) {
1102 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1103 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1108 * Make sure we update the FPM any time we send new information to
1111 hook_call(rib_update
, rn
, "uninstalling from kernel");
1112 kernel_route_rib(p
, src_p
, re
, NULL
);
1118 /* Uninstall the route from kernel. */
1119 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1121 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1123 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)) {
1124 if (info
->safi
== SAFI_UNICAST
)
1125 hook_call(rib_update
, rn
, "rib_uninstall");
1127 if (!RIB_SYSTEM_ROUTE(re
))
1128 rib_uninstall_kernel(rn
, re
);
1130 /* If labeled-unicast route, uninstall transit LSP. */
1131 if (zebra_rib_labeled_unicast(re
))
1132 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1134 UNSET_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1137 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1138 struct prefix
*p
, *src_p
;
1139 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1141 redistribute_delete(p
, src_p
, re
);
1142 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1147 * rib_can_delete_dest
1149 * Returns TRUE if the given dest can be deleted from the table.
1151 static int rib_can_delete_dest(rib_dest_t
*dest
)
1158 * Don't delete the dest if we have to update the FPM about this
1161 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1162 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1171 * Garbage collect the rib dest corresponding to the given route node
1174 * Returns TRUE if the dest was deleted, FALSE otherwise.
1176 int rib_gc_dest(struct route_node
*rn
)
1180 dest
= rib_dest_from_rnode(rn
);
1184 if (!rib_can_delete_dest(dest
))
1187 if (IS_ZEBRA_DEBUG_RIB
) {
1188 struct zebra_vrf
*zvrf
;
1190 zvrf
= rib_dest_vrf(dest
);
1191 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1195 XFREE(MTYPE_RIB_DEST
, dest
);
1199 * Release the one reference that we keep on the route node.
1201 route_unlock_node(rn
);
1205 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1206 struct route_entry
*new)
1208 hook_call(rib_update
, rn
, "new route selected");
1210 /* Update real nexthop. This may actually determine if nexthop is active
1212 if (!nexthop_active_update(rn
, new, 1)) {
1213 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1217 SET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1218 if (IS_ZEBRA_DEBUG_RIB
) {
1219 char buf
[SRCDEST2STR_BUFFER
];
1220 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1221 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1222 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1225 /* If labeled-unicast route, install transit LSP. */
1226 if (zebra_rib_labeled_unicast(new))
1227 zebra_mpls_lsp_install(zvrf
, rn
, new);
1229 if (!RIB_SYSTEM_ROUTE(new))
1230 rib_install_kernel(rn
, new, NULL
);
1232 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1235 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1236 struct route_entry
*old
)
1238 hook_call(rib_update
, rn
, "removing existing route");
1240 /* Uninstall from kernel. */
1241 if (IS_ZEBRA_DEBUG_RIB
) {
1242 char buf
[SRCDEST2STR_BUFFER
];
1243 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1244 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1245 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1248 /* If labeled-unicast route, uninstall transit LSP. */
1249 if (zebra_rib_labeled_unicast(old
))
1250 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1252 if (!RIB_SYSTEM_ROUTE(old
))
1253 rib_uninstall_kernel(rn
, old
);
1255 UNSET_FLAG(old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1257 /* Update nexthop for route, reset changed flag. */
1258 nexthop_active_update(rn
, old
, 1);
1259 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1262 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1263 struct route_node
*rn
,
1264 struct route_entry
*old
,
1265 struct route_entry
*new)
1267 struct nexthop
*nexthop
= NULL
;
1272 * We have to install or update if a new route has been selected or
1273 * something has changed.
1275 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1276 hook_call(rib_update
, rn
, "updating existing route");
1278 /* Update the nexthop; we could determine here that nexthop is
1280 if (nexthop_active_update(rn
, new, 1))
1283 /* If nexthop is active, install the selected route, if
1285 * the install succeeds, cleanup flags for prior route, if
1290 if (IS_ZEBRA_DEBUG_RIB
) {
1291 char buf
[SRCDEST2STR_BUFFER
];
1292 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1295 "%u:%s: Updating route rn %p, re %p (type %d) "
1297 zvrf_id(zvrf
), buf
, rn
, new,
1298 new->type
, old
, old
->type
);
1301 "%u:%s: Updating route rn %p, re %p (type %d)",
1302 zvrf_id(zvrf
), buf
, rn
, new,
1306 /* If labeled-unicast route, uninstall transit LSP. */
1307 if (zebra_rib_labeled_unicast(old
))
1308 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1310 /* Non-system route should be installed. */
1311 if (!RIB_SYSTEM_ROUTE(new)) {
1312 /* If labeled-unicast route, install transit
1314 if (zebra_rib_labeled_unicast(new))
1315 zebra_mpls_lsp_install(zvrf
, rn
, new);
1317 rib_install_kernel(rn
, new, old
);
1320 /* If install succeeded or system route, cleanup flags
1321 * for prior route. */
1322 if (installed
&& new != old
) {
1323 if (RIB_SYSTEM_ROUTE(new)) {
1324 if (!RIB_SYSTEM_ROUTE(old
))
1325 rib_uninstall_kernel(rn
, old
);
1327 for (nexthop
= old
->nexthop
; nexthop
;
1328 nexthop
= nexthop
->next
)
1329 UNSET_FLAG(nexthop
->flags
,
1334 /* Update for redistribution. */
1336 SET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1340 * If nexthop for selected route is not active or install
1342 * may need to uninstall and delete for redistribution.
1344 if (!nh_active
|| !installed
) {
1345 if (IS_ZEBRA_DEBUG_RIB
) {
1346 char buf
[SRCDEST2STR_BUFFER
];
1347 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1350 "%u:%s: Deleting route rn %p, re %p (type %d) "
1351 "old %p (type %d) - %s",
1352 zvrf_id(zvrf
), buf
, rn
, new,
1353 new->type
, old
, old
->type
,
1354 nh_active
? "install failed"
1355 : "nexthop inactive");
1358 "%u:%s: Deleting route rn %p, re %p (type %d) - %s",
1359 zvrf_id(zvrf
), buf
, rn
, new,
1361 nh_active
? "install failed"
1362 : "nexthop inactive");
1365 /* If labeled-unicast route, uninstall transit LSP. */
1366 if (zebra_rib_labeled_unicast(old
))
1367 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1369 if (!RIB_SYSTEM_ROUTE(old
))
1370 rib_uninstall_kernel(rn
, old
);
1371 UNSET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1375 * Same route selected; check if in the FIB and if not,
1377 * is housekeeping code to deal with race conditions in kernel
1379 * netlink reporting interface up before IPv4 or IPv6 protocol
1383 if (!RIB_SYSTEM_ROUTE(new)) {
1386 for (ALL_NEXTHOPS(new->nexthop
, nexthop
))
1387 if (CHECK_FLAG(nexthop
->flags
,
1388 NEXTHOP_FLAG_FIB
)) {
1393 rib_install_kernel(rn
, new, NULL
);
1397 /* Update prior route. */
1399 UNSET_FLAG(old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1401 /* Set real nexthop. */
1402 nexthop_active_update(rn
, old
, 1);
1403 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1406 /* Clear changed flag. */
1407 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1410 /* Check if 'alternate' RIB entry is better than 'current'. */
1411 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1412 struct route_entry
*alternate
)
1414 if (current
== NULL
)
1417 /* filter route selection in following order:
1418 * - connected beats other types
1419 * - lower distance beats higher
1420 * - lower metric beats higher for equal distance
1421 * - last, hence oldest, route wins tie break.
1424 /* Connected routes. Pick the last connected
1425 * route of the set of lowest metric connected routes.
1427 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1428 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1429 || alternate
->metric
<= current
->metric
)
1435 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1438 /* higher distance loses */
1439 if (alternate
->distance
< current
->distance
)
1441 if (current
->distance
< alternate
->distance
)
1444 /* metric tie-breaks equal distance */
1445 if (alternate
->metric
<= current
->metric
)
1451 /* Core function for processing routing information base. */
1452 static void rib_process(struct route_node
*rn
)
1454 struct route_entry
*re
;
1455 struct route_entry
*next
;
1456 struct route_entry
*old_selected
= NULL
;
1457 struct route_entry
*new_selected
= NULL
;
1458 struct route_entry
*old_fib
= NULL
;
1459 struct route_entry
*new_fib
= NULL
;
1460 struct route_entry
*best
= NULL
;
1461 char buf
[SRCDEST2STR_BUFFER
];
1463 struct zebra_vrf
*zvrf
= NULL
;
1464 struct prefix
*p
, *src_p
;
1465 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1466 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1470 dest
= rib_dest_from_rnode(rn
);
1472 zvrf
= rib_dest_vrf(dest
);
1473 vrf_id
= zvrf_id(zvrf
);
1476 if (IS_ZEBRA_DEBUG_RIB
)
1477 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1479 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1480 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1482 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1483 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1485 "%u:%s: Examine re %p (type %d) status %x flags %x "
1486 "dist %d metric %d",
1487 vrf_id
, buf
, re
, re
->type
, re
->status
,
1488 re
->flags
, re
->distance
, re
->metric
);
1490 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1492 /* Currently selected re. */
1493 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1494 assert(old_selected
== NULL
);
1497 /* Currently in fib */
1498 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)) {
1499 assert(old_fib
== NULL
);
1503 /* Skip deleted entries from selection */
1504 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1507 /* Skip unreachable nexthop. */
1508 /* This first call to nexthop_active_update is merely to
1510 * there's any change to nexthops associated with this RIB
1512 * rib_process() can be invoked due to an external event such as
1514 * down or due to next-hop-tracking evaluation. In the latter
1516 * a decision has already been made that the NHs have changed.
1518 * need to invoke a potentially expensive call again. Further,
1520 * the change might be in a recursive NH which is not caught in
1521 * the nexthop_active_update() code. Thus, we might miss changes
1525 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1526 && !nexthop_active_update(rn
, re
, 0)) {
1527 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1528 /* XXX: HERE BE DRAGONS!!!!!
1529 * In all honesty, I have not yet figured out
1531 * does or why the ROUTE_ENTRY_CHANGED test
1533 * or why we need to delete a route here, and
1535 * this concerns both selected and fib route, or
1538 /* This entry was denied by the 'ip protocol
1539 * table' route-map, we
1540 * need to delete it */
1541 if (re
!= old_selected
) {
1542 if (IS_ZEBRA_DEBUG_RIB
)
1544 "%s: %s: imported via import-table but denied "
1545 "by the ip protocol table route-map",
1549 SET_FLAG(re
->status
,
1550 ROUTE_ENTRY_REMOVED
);
1556 /* Infinite distance. */
1557 if (re
->distance
== DISTANCE_INFINITY
) {
1558 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1562 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1563 best
= rib_choose_best(new_fib
, re
);
1564 if (new_fib
&& best
!= new_fib
)
1565 UNSET_FLAG(new_fib
->status
,
1566 ROUTE_ENTRY_CHANGED
);
1569 best
= rib_choose_best(new_selected
, re
);
1570 if (new_selected
&& best
!= new_selected
)
1571 UNSET_FLAG(new_selected
->status
,
1572 ROUTE_ENTRY_CHANGED
);
1573 new_selected
= best
;
1576 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1577 } /* RNODE_FOREACH_RE */
1579 /* If no FIB override route, use the selected route also for FIB */
1580 if (new_fib
== NULL
)
1581 new_fib
= new_selected
;
1583 /* After the cycle is finished, the following pointers will be set:
1584 * old_selected --- RE entry currently having SELECTED
1585 * new_selected --- RE entry that is newly SELECTED
1586 * old_fib --- RE entry currently in kernel FIB
1587 * new_fib --- RE entry that is newly to be in kernel FIB
1589 * new_selected will get SELECTED flag, and is going to be redistributed
1590 * the zclients. new_fib (which can be new_selected) will be installed
1594 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1596 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1597 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1598 (void *)old_fib
, (void *)new_fib
);
1601 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1602 * fib == selected */
1603 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1604 ROUTE_ENTRY_CHANGED
);
1606 /* Update fib according to selection results */
1607 if (new_fib
&& old_fib
)
1608 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1610 rib_process_add_fib(zvrf
, rn
, new_fib
);
1612 rib_process_del_fib(zvrf
, rn
, old_fib
);
1614 /* Redistribute SELECTED entry */
1615 if (old_selected
!= new_selected
|| selected_changed
) {
1616 struct nexthop
*nexthop
;
1618 /* Check if we have a FIB route for the destination, otherwise,
1619 * don't redistribute it */
1620 for (ALL_NEXTHOPS(new_fib
? new_fib
->nexthop
: NULL
, nexthop
)) {
1621 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1626 new_selected
= NULL
;
1628 if (new_selected
&& new_selected
!= new_fib
) {
1629 nexthop_active_update(rn
, new_selected
, 1);
1630 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1635 redistribute_delete(p
, src_p
, old_selected
);
1636 if (old_selected
!= new_selected
)
1637 UNSET_FLAG(old_selected
->flags
,
1638 ZEBRA_FLAG_SELECTED
);
1642 /* Install new or replace existing redistributed entry
1644 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1645 redistribute_update(p
, src_p
, new_selected
,
1650 /* Remove all RE entries queued for removal */
1651 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1652 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1653 if (IS_ZEBRA_DEBUG_RIB
) {
1654 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1655 (void *)rn
, (void *)re
);
1662 * Check if the dest can be deleted now.
1667 /* Take a list of route_node structs and return 1, if there was a record
1668 * picked from it and processed by rib_process(). Don't process more,
1669 * than one RN record; operate only in the specified sub-queue.
1671 static unsigned int process_subq(struct list
*subq
, u_char qindex
)
1673 struct listnode
*lnode
= listhead(subq
);
1674 struct route_node
*rnode
;
1676 struct zebra_vrf
*zvrf
= NULL
;
1681 rnode
= listgetdata(lnode
);
1682 dest
= rib_dest_from_rnode(rnode
);
1684 zvrf
= rib_dest_vrf(dest
);
1688 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1689 char buf
[SRCDEST2STR_BUFFER
];
1690 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1691 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1692 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1696 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1697 RIB_ROUTE_QUEUED(qindex
));
1702 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1703 __func__
, rnode
, rnode
->lock
);
1704 zlog_backtrace(LOG_DEBUG
);
1707 route_unlock_node(rnode
);
1708 list_delete_node(subq
, lnode
);
1713 * All meta queues have been processed. Trigger next-hop evaluation.
1715 static void meta_queue_process_complete(struct work_queue
*dummy
)
1718 struct zebra_vrf
*zvrf
;
1720 /* Evaluate nexthops for those VRFs which underwent route processing.
1722 * should limit the evaluation to the necessary VRFs in most common
1725 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1727 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1730 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1731 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
,
1733 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0,
1734 RNH_IMPORT_CHECK_TYPE
, NULL
);
1735 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
,
1737 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0,
1738 RNH_IMPORT_CHECK_TYPE
, NULL
);
1741 /* Schedule LSPs for processing, if needed. */
1742 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1743 if (mpls_should_lsps_be_processed(zvrf
)) {
1744 if (IS_ZEBRA_DEBUG_MPLS
)
1746 "%u: Scheduling all LSPs upon RIB completion",
1748 zebra_mpls_lsp_schedule(zvrf
);
1749 mpls_unmark_lsps_for_processing(zvrf
);
1753 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1754 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1756 * is pointed to the meta queue structure.
1758 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
1760 struct meta_queue
*mq
= data
;
1763 for (i
= 0; i
< MQ_SIZE
; i
++)
1764 if (process_subq(mq
->subq
[i
], i
)) {
1768 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1772 * Map from rib types to queue type (priority) in meta queue
1774 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1775 [ZEBRA_ROUTE_SYSTEM
] = 4,
1776 [ZEBRA_ROUTE_KERNEL
] = 0,
1777 [ZEBRA_ROUTE_CONNECT
] = 0,
1778 [ZEBRA_ROUTE_STATIC
] = 1,
1779 [ZEBRA_ROUTE_RIP
] = 2,
1780 [ZEBRA_ROUTE_RIPNG
] = 2,
1781 [ZEBRA_ROUTE_OSPF
] = 2,
1782 [ZEBRA_ROUTE_OSPF6
] = 2,
1783 [ZEBRA_ROUTE_ISIS
] = 2,
1784 [ZEBRA_ROUTE_BGP
] = 3,
1785 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1786 [ZEBRA_ROUTE_EIGRP
] = 2,
1787 [ZEBRA_ROUTE_NHRP
] = 2,
1788 [ZEBRA_ROUTE_HSLS
] = 4,
1789 [ZEBRA_ROUTE_OLSR
] = 4,
1790 [ZEBRA_ROUTE_TABLE
] = 1,
1791 [ZEBRA_ROUTE_LDP
] = 4,
1792 [ZEBRA_ROUTE_VNC
] = 3,
1793 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1794 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1795 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1796 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1797 [ZEBRA_ROUTE_BABEL
] = 2,
1798 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1801 /* Look into the RN and queue it into one or more priority queues,
1802 * increasing the size for each data push done.
1804 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
1806 struct route_entry
*re
;
1808 RNODE_FOREACH_RE (rn
, re
) {
1809 u_char qindex
= meta_queue_map
[re
->type
];
1810 struct zebra_vrf
*zvrf
;
1812 /* Invariant: at this point we always have rn->info set. */
1813 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
1814 RIB_ROUTE_QUEUED(qindex
))) {
1815 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1818 "rn %p is already queued in sub-queue %u",
1819 (void *)rn
, qindex
);
1823 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
1824 RIB_ROUTE_QUEUED(qindex
));
1825 listnode_add(mq
->subq
[qindex
], rn
);
1826 route_lock_node(rn
);
1829 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1830 rnode_debug(rn
, re
->vrf_id
,
1831 "queued rn %p into sub-queue %u",
1832 (void *)rn
, qindex
);
1834 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
1836 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1840 /* Add route_node to work queue and schedule processing */
1841 void rib_queue_add(struct route_node
*rn
)
1845 /* Pointless to queue a route_node with no RIB entries to add or remove
1847 if (!rnode_to_ribs(rn
)) {
1848 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
1849 __func__
, (void *)rn
, rn
->lock
);
1850 zlog_backtrace(LOG_DEBUG
);
1854 if (zebrad
.ribq
== NULL
) {
1855 zlog_err("%s: work_queue does not exist!", __func__
);
1860 * The RIB queue should normally be either empty or holding the only
1861 * work_queue_item element. In the latter case this element would
1862 * hold a pointer to the meta queue structure, which must be used to
1863 * actually queue the route nodes to process. So create the MQ
1864 * holder, if necessary, then push the work into it in any case.
1865 * This semantics was introduced after 0.99.9 release.
1867 if (work_queue_empty(zebrad
.ribq
))
1868 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
1870 rib_meta_queue_add(zebrad
.mq
, rn
);
1875 /* Create new meta queue.
1876 A destructor function doesn't seem to be necessary here.
1878 static struct meta_queue
*meta_queue_new(void)
1880 struct meta_queue
*new;
1883 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
1886 for (i
= 0; i
< MQ_SIZE
; i
++) {
1887 new->subq
[i
] = list_new();
1888 assert(new->subq
[i
]);
1894 void meta_queue_free(struct meta_queue
*mq
)
1898 for (i
= 0; i
< MQ_SIZE
; i
++)
1899 list_delete_and_null(&mq
->subq
[i
]);
1901 XFREE(MTYPE_WORK_QUEUE
, mq
);
1904 /* initialise zebra rib work queue */
1905 static void rib_queue_init(struct zebra_t
*zebra
)
1910 work_queue_new(zebra
->master
, "route_node processing"))) {
1911 zlog_err("%s: could not initialise work queue!", __func__
);
1915 /* fill in the work queue spec */
1916 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1917 zebra
->ribq
->spec
.errorfunc
= NULL
;
1918 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1919 /* XXX: TODO: These should be runtime configurable via vty */
1920 zebra
->ribq
->spec
.max_retries
= 3;
1921 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
1923 if (!(zebra
->mq
= meta_queue_new())) {
1924 zlog_err("%s: could not initialise meta queue!", __func__
);
1930 /* RIB updates are processed via a queue of pointers to route_nodes.
1932 * The queue length is bounded by the maximal size of the routing table,
1933 * as a route_node will not be requeued, if already queued.
1935 * REs are submitted via rib_addnode or rib_delnode which set minimal
1936 * state, or static_install_route (when an existing RE is updated)
1937 * and then submit route_node to queue for best-path selection later.
1938 * Order of add/delete state changes are preserved for any given RE.
1940 * Deleted REs are reaped during best-path selection.
1943 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
1944 * |-------->| | best RE, if required
1946 * static_install->|->rib_addqueue...... -> rib_process
1948 * |-------->| |-> rib_unlink
1949 * |-> set ROUTE_ENTRY_REMOVE |
1950 * rib_delnode (RE freed)
1952 * The 'info' pointer of a route_node points to a rib_dest_t
1953 * ('dest'). Queueing state for a route_node is kept on the dest. The
1954 * dest is created on-demand by rib_link() and is kept around at least
1955 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1957 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1959 * - route_nodes: refcounted by:
1960 * - dest attached to route_node:
1961 * - managed by: rib_link/rib_gc_dest
1962 * - route_node processing queue
1963 * - managed by: rib_addqueue, rib_process.
1967 /* Add RE to head of the route node. */
1968 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
1970 struct route_entry
*head
;
1973 const char *rmap_name
;
1977 dest
= rib_dest_from_rnode(rn
);
1979 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1980 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
1982 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
1983 route_lock_node(rn
); /* rn route table reference */
1988 head
= dest
->routes
;
1995 afi
= (rn
->p
.family
== AF_INET
)
1997 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
1998 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
1999 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2000 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2005 void rib_addnode(struct route_node
*rn
, struct route_entry
*re
, int process
)
2007 /* RE node has been un-removed before route-node is processed.
2008 * route_node must hence already be on the queue for processing..
2010 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2011 if (IS_ZEBRA_DEBUG_RIB
)
2012 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2013 (void *)rn
, (void *)re
);
2015 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2018 rib_link(rn
, re
, process
);
2024 * Detach a rib structure from a route_node.
2026 * Note that a call to rib_unlink() should be followed by a call to
2027 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2028 * longer required to be deleted.
2030 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2036 if (IS_ZEBRA_DEBUG_RIB
)
2037 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2040 dest
= rib_dest_from_rnode(rn
);
2043 re
->next
->prev
= re
->prev
;
2046 re
->prev
->next
= re
->next
;
2048 dest
->routes
= re
->next
;
2051 /* free RE and nexthops */
2052 zebra_deregister_rnh_static_nexthops(re
->vrf_id
, re
->nexthop
, rn
);
2053 nexthops_free(re
->nexthop
);
2054 XFREE(MTYPE_RE
, re
);
2057 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2061 if (IS_ZEBRA_DEBUG_RIB
)
2062 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2063 (void *)rn
, (void *)re
);
2064 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2066 afi
= (rn
->p
.family
== AF_INET
)
2068 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2069 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2070 zebra_del_import_table_entry(rn
, re
);
2071 /* Just clean up if non main table */
2072 if (IS_ZEBRA_DEBUG_RIB
) {
2073 char buf
[SRCDEST2STR_BUFFER
];
2074 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2076 "%u:%s: Freeing route rn %p, re %p (type %d)",
2077 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2086 /* This function dumps the contents of a given RE entry into
2087 * standard debug log. Calling function name and IP prefix in
2088 * question are passed as 1st and 2nd arguments.
2091 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2092 union prefixconstptr src_pp
,
2093 const struct route_entry
*re
)
2095 const struct prefix
*p
= pp
.p
;
2096 const struct prefix
*src_p
= src_pp
.p
;
2097 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2098 char straddr
[PREFIX_STRLEN
];
2099 char srcaddr
[PREFIX_STRLEN
];
2100 struct nexthop
*nexthop
;
2102 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2103 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2104 is_srcdst
? " from " : "",
2105 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2108 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2109 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2112 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2113 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2114 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2115 re
->nexthop_num
, re
->nexthop_active_num
);
2117 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
2118 inet_ntop(p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2119 zlog_debug("%s: %s %s[%u] with flags %s%s%s", func
,
2120 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2122 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2125 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2128 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2132 zlog_debug("%s: dump complete", func
);
2135 /* This is an exported helper to rtm_read() to dump the strange
2136 * RE entry found by rib_lookup_ipv4_route()
2139 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2141 struct route_table
*table
;
2142 struct route_node
*rn
;
2143 struct route_entry
*re
;
2144 char prefix_buf
[INET_ADDRSTRLEN
];
2147 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2149 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2153 /* Scan the RIB table for exactly matching RE entry. */
2154 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2156 /* No route for this prefix. */
2158 zlog_debug("%s: lookup failed for %s", __func__
,
2159 prefix2str((struct prefix
*)p
, prefix_buf
,
2160 sizeof(prefix_buf
)));
2165 route_unlock_node(rn
);
2168 RNODE_FOREACH_RE (rn
, re
) {
2169 zlog_debug("%s: rn %p, re %p: %s, %s", __func__
, (void *)rn
,
2171 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2174 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2177 route_entry_dump(p
, NULL
, re
);
2181 /* Check if requested address assignment will fail due to another
2182 * route being installed by zebra in FIB already. Take necessary
2183 * actions, if needed: remove such a route from FIB and deSELECT
2184 * corresponding RE entry. Then put affected RN into RIBQ head.
2186 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2188 struct route_table
*table
;
2189 struct route_node
*rn
;
2190 struct route_entry
*re
;
2191 unsigned changed
= 0;
2193 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2194 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2198 /* No matches would be the simplest case. */
2199 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2203 route_unlock_node(rn
);
2205 /* Check all RE entries. In case any changes have to be done, requeue
2206 * the RN into RIBQ head. If the routing message about the new connected
2207 * route (generated by the IP address we are going to assign very soon)
2208 * comes before the RIBQ is processed, the new RE entry will join
2209 * RIBQ record already on head. This is necessary for proper
2211 * of the rest of the RE.
2213 RNODE_FOREACH_RE (rn
, re
) {
2214 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)
2215 && !RIB_SYSTEM_ROUTE(re
)) {
2217 if (IS_ZEBRA_DEBUG_RIB
) {
2218 char buf
[PREFIX_STRLEN
];
2220 "%u:%s: freeing way for connected prefix",
2222 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2223 route_entry_dump(&rn
->p
, NULL
, re
);
2225 rib_uninstall(rn
, re
);
2232 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2233 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2235 struct route_table
*table
;
2236 struct route_node
*rn
;
2237 struct route_entry
*same
;
2238 struct nexthop
*nexthop
;
2244 assert(!src_p
|| afi
== AFI_IP6
);
2247 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2249 XFREE(MTYPE_RE
, re
);
2253 /* Make it sure prefixlen is applied to the prefix. */
2256 apply_mask_ipv6(src_p
);
2258 /* Set default distance by route type. */
2259 if (re
->distance
== 0) {
2260 re
->distance
= route_distance(re
->type
);
2262 /* iBGP distance is 200. */
2263 if (re
->type
== ZEBRA_ROUTE_BGP
2264 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2268 /* Lookup route node.*/
2269 rn
= srcdest_rnode_get(table
, p
, src_p
);
2271 /* If same type of route are installed, treat it as a implicit
2273 RNODE_FOREACH_RE (rn
, same
) {
2274 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2277 if (same
->type
!= re
->type
)
2279 if (same
->instance
!= re
->instance
)
2281 if (same
->type
== ZEBRA_ROUTE_KERNEL
&&
2282 same
->metric
!= re
->metric
)
2285 * We should allow duplicate connected routes because of
2286 * IPv6 link-local routes and unnumbered interfaces on Linux.
2288 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2292 /* If this route is kernel route, set FIB flag to the route. */
2293 if (RIB_SYSTEM_ROUTE(re
))
2294 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2295 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2297 /* Link new re to node.*/
2298 if (IS_ZEBRA_DEBUG_RIB
) {
2301 "Inserting route rn %p, re %p (type %d) existing %p",
2302 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2304 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2305 route_entry_dump(p
, src_p
, re
);
2307 rib_addnode(rn
, re
, 1);
2310 /* Free implicit route.*/
2312 rib_delnode(rn
, same
);
2316 route_unlock_node(rn
);
2320 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2321 u_short instance
, int flags
, struct prefix
*p
,
2322 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2323 u_int32_t table_id
, u_int32_t metric
, bool fromkernel
,
2324 struct ethaddr
*rmac
)
2326 struct route_table
*table
;
2327 struct route_node
*rn
;
2328 struct route_entry
*re
;
2329 struct route_entry
*fib
= NULL
;
2330 struct route_entry
*same
= NULL
;
2331 struct nexthop
*rtnh
;
2332 char buf2
[INET6_ADDRSTRLEN
];
2334 assert(!src_p
|| afi
== AFI_IP6
);
2337 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2344 apply_mask_ipv6(src_p
);
2346 /* Lookup route node. */
2347 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2349 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2351 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2352 if (src_p
&& src_p
->prefixlen
)
2353 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2357 if (IS_ZEBRA_DEBUG_RIB
)
2358 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2360 (src_buf
[0] != '\0') ? " from " : "",
2365 /* Lookup same type route. */
2366 RNODE_FOREACH_RE (rn
, re
) {
2367 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2370 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2373 if (re
->type
!= type
)
2375 if (re
->instance
!= instance
)
2377 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2378 re
->metric
!= metric
)
2380 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->nexthop
)
2381 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2382 if (rtnh
->ifindex
!= nh
->ifindex
)
2387 /* Make sure that the route found has the same gateway. */
2393 for (ALL_NEXTHOPS(re
->nexthop
, rtnh
))
2394 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2402 /* If same type of route can't be found and this message is from
2406 * In the past(HA!) we could get here because
2407 * we were receiving a route delete from the
2408 * kernel and we're not marking the proto
2409 * as coming from it's appropriate originator.
2410 * Now that we are properly noticing the fact
2411 * that the kernel has deleted our route we
2412 * are not going to get called in this path
2413 * I am going to leave this here because
2414 * this might still work this way on non-linux
2415 * platforms as well as some weird state I have
2416 * not properly thought of yet.
2417 * If we can show that this code path is
2418 * dead then we can remove it.
2420 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
2421 && CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2422 if (IS_ZEBRA_DEBUG_RIB
) {
2425 "rn %p, re %p (type %d) was deleted from kernel, adding",
2426 rn
, fib
, fib
->type
);
2430 for (rtnh
= fib
->nexthop
; rtnh
;
2432 UNSET_FLAG(rtnh
->flags
,
2435 UNSET_FLAG(fib
->status
,
2436 ROUTE_ENTRY_SELECTED_FIB
);
2438 /* This means someone else, other than Zebra,
2440 * a Zebra router from the kernel. We will add
2442 rib_install_kernel(rn
, fib
, NULL
);
2445 if (IS_ZEBRA_DEBUG_RIB
) {
2449 "via %s ifindex %d type %d "
2450 "doesn't exist in rib",
2454 INET_ADDRSTRLEN
), /* FIXME
2460 "type %d doesn't exist in rib",
2463 route_unlock_node(rn
);
2470 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) &&
2472 rib_install_kernel(rn
, same
, NULL
);
2473 route_unlock_node(rn
);
2478 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_TYPE2_ROUTE
)) {
2479 struct nexthop
*tmp_nh
;
2481 for (ALL_NEXTHOPS(re
->nexthop
, tmp_nh
)) {
2482 struct ipaddr vtep_ip
;
2484 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2485 vtep_ip
.ipa_type
= IPADDR_V4
;
2486 memcpy(&(vtep_ip
.ipaddr_v4
),
2487 &(tmp_nh
->gate
.ipv4
),
2488 sizeof(struct in_addr
));
2489 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
, rmac
,
2493 rib_delnode(rn
, same
);
2496 route_unlock_node(rn
);
2501 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2502 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2503 const struct nexthop
*nh
, u_int32_t table_id
, u_int32_t metric
,
2504 u_int32_t mtu
, uint8_t distance
)
2506 struct route_entry
*re
;
2507 struct nexthop
*nexthop
;
2509 /* Allocate new route_entry structure. */
2510 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2512 re
->instance
= instance
;
2513 re
->distance
= distance
;
2515 re
->metric
= metric
;
2517 re
->table
= table_id
;
2518 re
->vrf_id
= vrf_id
;
2519 re
->nexthop_num
= 0;
2520 re
->uptime
= time(NULL
);
2523 nexthop
= nexthop_new();
2525 route_entry_nexthop_add(re
, nexthop
);
2527 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2530 /* Schedule routes of a particular table (address-family) based on event. */
2531 static void rib_update_table(struct route_table
*table
,
2532 rib_update_event_t event
)
2534 struct route_node
*rn
;
2535 struct route_entry
*re
, *next
;
2537 /* Walk all routes and queue for processing, if appropriate for
2538 * the trigger event.
2540 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2542 * If we are looking at a route node and the node
2543 * has already been queued we don't
2544 * need to queue it up again
2547 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2548 RIB_ROUTE_ANY_QUEUED
))
2551 case RIB_UPDATE_IF_CHANGE
:
2552 /* Examine all routes that won't get processed by the
2554 * triggered by nexthop evaluation (NHT). This would be
2556 * kernel and certain static routes. Note that NHT will
2558 * triggered upon an interface event as connected routes
2560 * get queued for processing.
2562 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2565 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
&&
2566 re
->type
!= ZEBRA_ROUTE_KERNEL
&&
2567 re
->type
!= ZEBRA_ROUTE_CONNECT
&&
2568 re
->type
!= ZEBRA_ROUTE_STATIC
)
2571 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2576 for (nh
= re
->nexthop
; nh
; nh
= nh
->next
)
2577 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2578 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2581 /* If we only have nexthops to a
2590 case RIB_UPDATE_RMAP_CHANGE
:
2591 case RIB_UPDATE_OTHER
:
2592 /* Right now, examine all routes. Can restrict to a
2594 * some cases (TODO).
2596 if (rnode_to_ribs(rn
))
2606 /* RIB update function. */
2607 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2609 struct route_table
*table
;
2611 /* Process routes of interested address-families. */
2612 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2614 rib_update_table(table
, event
);
2616 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2618 rib_update_table(table
, event
);
2621 /* Remove all routes which comes from non main table. */
2622 static void rib_weed_table(struct route_table
*table
)
2624 struct route_node
*rn
;
2625 struct route_entry
*re
;
2626 struct route_entry
*next
;
2629 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2630 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2631 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2634 if (re
->table
!= zebrad
.rtm_table_default
2635 && re
->table
!= RT_TABLE_MAIN
)
2636 rib_delnode(rn
, re
);
2640 /* Delete all routes from non main table. */
2641 void rib_weed_tables(void)
2644 struct zebra_vrf
*zvrf
;
2646 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2647 if ((zvrf
= vrf
->info
) != NULL
) {
2648 rib_weed_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2649 rib_weed_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2653 /* Delete self installed routes after zebra is relaunched. */
2654 static void rib_sweep_table(struct route_table
*table
)
2656 struct route_node
*rn
;
2657 struct route_entry
*re
;
2658 struct route_entry
*next
;
2659 struct nexthop
*nexthop
;
2664 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2665 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2666 if (IS_ZEBRA_DEBUG_RIB
)
2667 route_entry_dump(&rn
->p
, NULL
, re
);
2669 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2672 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2676 * So we are starting up and have received
2677 * routes from the kernel that we have installed
2678 * from a previous run of zebra but not cleaned
2679 * up ( say a kill -9 )
2680 * But since we haven't actually installed
2681 * them yet( we received them from the kernel )
2682 * we don't think they are active.
2683 * So let's pretend they are active to actually
2685 * In all honesty I'm not sure if we should
2686 * mark them as active when we receive them
2687 * This is startup only so probably ok.
2689 * If we ever decide to move rib_sweep_table
2690 * to a different spot (ie startup )
2691 * this decision needs to be revisited
2693 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
2694 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2696 rib_uninstall_kernel(rn
, re
);
2697 rib_delnode(rn
, re
);
2702 /* Sweep all RIB tables. */
2703 void rib_sweep_route(void)
2706 struct zebra_vrf
*zvrf
;
2708 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2709 if ((zvrf
= vrf
->info
) == NULL
)
2712 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2713 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2717 /* Remove specific by protocol routes from 'table'. */
2718 static unsigned long rib_score_proto_table(u_char proto
, u_short instance
,
2719 struct route_table
*table
)
2721 struct route_node
*rn
;
2722 struct route_entry
*re
;
2723 struct route_entry
*next
;
2724 unsigned long n
= 0;
2727 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2728 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2729 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2731 if (re
->type
== proto
2732 && re
->instance
== instance
) {
2733 rib_delnode(rn
, re
);
2740 /* Remove specific by protocol routes. */
2741 unsigned long rib_score_proto(u_char proto
, u_short instance
)
2744 struct zebra_vrf
*zvrf
;
2745 unsigned long cnt
= 0;
2747 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2748 if ((zvrf
= vrf
->info
) != NULL
)
2749 cnt
+= rib_score_proto_table(
2751 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2752 + rib_score_proto_table(
2754 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2759 /* Close RIB and clean up kernel routes. */
2760 void rib_close_table(struct route_table
*table
)
2762 struct route_node
*rn
;
2763 rib_table_info_t
*info
;
2764 struct route_entry
*re
;
2771 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2772 RNODE_FOREACH_RE (rn
, re
) {
2773 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2776 if (info
->safi
== SAFI_UNICAST
)
2777 hook_call(rib_update
, rn
, NULL
);
2779 if (!RIB_SYSTEM_ROUTE(re
))
2780 rib_uninstall_kernel(rn
, re
);
2784 /* Routing information base initialize. */
2787 rib_queue_init(&zebrad
);
2793 * Get the first vrf id that is greater than the given vrf id if any.
2795 * Returns TRUE if a vrf id was found, FALSE otherwise.
2797 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2801 vrf
= vrf_lookup_by_id(vrf_id
);
2803 vrf
= RB_NEXT(vrf_id_head
, vrf
);
2805 *next_id_p
= vrf
->vrf_id
;
2814 * rib_tables_iter_next
2816 * Returns the next table in the iteration.
2818 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
2820 struct route_table
*table
;
2823 * Array that helps us go over all AFI/SAFI combinations via one
2830 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
2831 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
2832 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
2837 switch (iter
->state
) {
2839 case RIB_TABLES_ITER_S_INIT
:
2840 iter
->vrf_id
= VRF_DEFAULT
;
2841 iter
->afi_safi_ix
= -1;
2845 case RIB_TABLES_ITER_S_ITERATING
:
2846 iter
->afi_safi_ix
++;
2849 while (iter
->afi_safi_ix
2850 < (int)ZEBRA_NUM_OF(afi_safis
)) {
2851 table
= zebra_vrf_table(
2852 afi_safis
[iter
->afi_safi_ix
].afi
,
2853 afi_safis
[iter
->afi_safi_ix
].safi
,
2858 iter
->afi_safi_ix
++;
2862 * Found another table in this vrf.
2868 * Done with all tables in the current vrf, go to the
2872 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
2875 iter
->afi_safi_ix
= 0;
2880 case RIB_TABLES_ITER_S_DONE
:
2885 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2887 iter
->state
= RIB_TABLES_ITER_S_DONE
;