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/zebra_router.h"
42 #include "zebra/connected.h"
43 #include "zebra/debug.h"
44 #include "zebra/interface.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/rib.h"
48 #include "zebra/zapi_msg.h"
49 #include "zebra/zebra_errors.h"
50 #include "zebra/zebra_memory.h"
51 #include "zebra/zebra_ns.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/zebra_routemap.h"
54 #include "zebra/zebra_vrf.h"
55 #include "zebra/zebra_vxlan.h"
56 #include "zebra/zapi_msg.h"
57 #include "zebra/zebra_dplane.h"
60 * Event, list, and mutex for delivery of dataplane results
62 static pthread_mutex_t dplane_mutex
;
63 static struct thread
*t_dplane
;
64 static struct dplane_ctx_q rib_dplane_q
;
66 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
69 /* Should we allow non Quagga processes to delete our routes */
70 extern int allow_delete
;
72 /* Each route type's string and default distance value. */
76 } route_info
[ZEBRA_ROUTE_MAX
] = {
77 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
78 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
79 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
80 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
81 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
82 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
83 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
84 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
85 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
86 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
87 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
88 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
89 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
90 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
91 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
92 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
93 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
94 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
95 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
96 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
97 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
98 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
99 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
100 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150},
102 /* no entry/default: 150 */
105 /* RPF lookup behaviour */
106 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
109 static void __attribute__((format(printf
, 5, 6)))
110 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
111 int priority
, const char *msgfmt
, ...)
113 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
117 va_start(ap
, msgfmt
);
118 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
122 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
123 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
125 if (info
->safi
== SAFI_MULTICAST
)
126 strcat(buf
, " (MRIB)");
128 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
131 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
134 #define rnode_debug(node, vrf_id, ...) \
135 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
136 #define rnode_info(node, ...) \
137 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
139 uint8_t route_distance(int type
)
143 if ((unsigned)type
>= array_size(route_info
))
146 distance
= route_info
[type
].distance
;
151 int is_zebra_valid_kernel_table(uint32_t table_id
)
154 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
155 || (table_id
== RT_TABLE_COMPAT
))
162 int is_zebra_main_routing_table(uint32_t table_id
)
164 if ((table_id
== RT_TABLE_MAIN
)
165 || (table_id
== zebrad
.rtm_table_default
))
170 int zebra_check_addr(const struct prefix
*p
)
172 if (p
->family
== AF_INET
) {
175 addr
= p
->u
.prefix4
.s_addr
;
178 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
179 || IPV4_LINKLOCAL(addr
))
182 if (p
->family
== AF_INET6
) {
183 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
185 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
191 /* Add nexthop to the end of a rib node's nexthop list */
192 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
194 nexthop_add(&re
->ng
.nexthop
, nexthop
);
200 * copy_nexthop - copy a nexthop to the rib structure.
202 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
204 assert(!re
->ng
.nexthop
);
205 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
206 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
210 /* Delete specified nexthop from the list. */
211 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
214 nexthop
->next
->prev
= nexthop
->prev
;
216 nexthop
->prev
->next
= nexthop
->next
;
218 re
->ng
.nexthop
= nexthop
->next
;
223 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
227 struct nexthop
*nexthop
;
229 nexthop
= nexthop_new();
230 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
231 nexthop
->ifindex
= ifindex
;
232 nexthop
->vrf_id
= nh_vrf_id
;
234 route_entry_nexthop_add(re
, nexthop
);
239 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
240 struct in_addr
*ipv4
,
244 struct nexthop
*nexthop
;
246 nexthop
= nexthop_new();
247 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
248 nexthop
->vrf_id
= nh_vrf_id
;
249 nexthop
->gate
.ipv4
= *ipv4
;
251 nexthop
->src
.ipv4
= *src
;
253 route_entry_nexthop_add(re
, nexthop
);
258 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
259 struct in_addr
*ipv4
,
264 struct nexthop
*nexthop
;
265 struct interface
*ifp
;
267 nexthop
= nexthop_new();
268 nexthop
->vrf_id
= nh_vrf_id
;
269 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
270 nexthop
->gate
.ipv4
= *ipv4
;
272 nexthop
->src
.ipv4
= *src
;
273 nexthop
->ifindex
= ifindex
;
274 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
275 /*Pending: need to think if null ifp here is ok during bootup?
276 There was a crash because ifp here was coming to be NULL */
278 if (connected_is_unnumbered(ifp
)
279 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)
280 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
281 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
284 route_entry_nexthop_add(re
, nexthop
);
289 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
290 struct in6_addr
*ipv6
,
293 struct nexthop
*nexthop
;
295 nexthop
= nexthop_new();
296 nexthop
->vrf_id
= nh_vrf_id
;
297 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
298 nexthop
->gate
.ipv6
= *ipv6
;
300 route_entry_nexthop_add(re
, nexthop
);
305 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
306 struct in6_addr
*ipv6
,
310 struct nexthop
*nexthop
;
312 nexthop
= nexthop_new();
313 nexthop
->vrf_id
= nh_vrf_id
;
314 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
315 nexthop
->gate
.ipv6
= *ipv6
;
316 nexthop
->ifindex
= ifindex
;
317 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)
318 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
319 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
322 route_entry_nexthop_add(re
, nexthop
);
327 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
328 enum blackhole_type bh_type
)
330 struct nexthop
*nexthop
;
332 nexthop
= nexthop_new();
333 nexthop
->vrf_id
= VRF_DEFAULT
;
334 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
335 nexthop
->bh_type
= bh_type
;
337 route_entry_nexthop_add(re
, nexthop
);
342 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
343 struct nexthop
*nexthop
)
345 struct nexthop
*resolved_hop
;
347 resolved_hop
= nexthop_new();
348 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
350 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
351 switch (newhop
->type
) {
352 case NEXTHOP_TYPE_IPV4
:
353 case NEXTHOP_TYPE_IPV4_IFINDEX
:
354 /* If the resolving route specifies a gateway, use it */
355 resolved_hop
->type
= newhop
->type
;
356 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
358 if (newhop
->ifindex
) {
359 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
360 resolved_hop
->ifindex
= newhop
->ifindex
;
363 case NEXTHOP_TYPE_IPV6
:
364 case NEXTHOP_TYPE_IPV6_IFINDEX
:
365 resolved_hop
->type
= newhop
->type
;
366 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
368 if (newhop
->ifindex
) {
369 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
370 resolved_hop
->ifindex
= newhop
->ifindex
;
373 case NEXTHOP_TYPE_IFINDEX
:
374 /* If the resolving route is an interface route,
375 * it means the gateway we are looking up is connected
376 * to that interface. (The actual network is _not_ onlink).
377 * Therefore, the resolved route should have the original
378 * gateway as nexthop as it is directly connected.
380 * On Linux, we have to set the onlink netlink flag because
381 * otherwise, the kernel won't accept the route.
383 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
385 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
386 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
387 } else if (afi
== AFI_IP6
) {
388 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
389 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
391 resolved_hop
->ifindex
= newhop
->ifindex
;
393 case NEXTHOP_TYPE_BLACKHOLE
:
394 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
395 resolved_hop
->bh_type
= nexthop
->bh_type
;
399 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
400 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
402 /* Copy labels of the resolved route */
403 if (newhop
->nh_label
)
404 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
405 newhop
->nh_label
->num_labels
,
406 &newhop
->nh_label
->label
[0]);
408 resolved_hop
->rparent
= nexthop
;
409 nexthop_add(&nexthop
->resolved
, resolved_hop
);
412 /* If force flag is not set, do not modify falgs at all for uninstall
413 the route from FIB. */
414 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
415 struct nexthop
*nexthop
, int set
,
416 struct route_node
*top
)
419 struct route_table
*table
;
420 struct route_node
*rn
;
421 struct route_entry
*match
= NULL
;
423 struct nexthop
*newhop
;
424 struct interface
*ifp
;
427 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
428 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
429 nexthop
->ifindex
= 0;
432 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
433 nexthops_free(nexthop
->resolved
);
434 nexthop
->resolved
= NULL
;
438 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
439 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
442 /* Skip nexthops that have been filtered out due to route-map */
443 /* The nexthops are specific to this route and so the same */
444 /* nexthop for a different route may not have this flag set */
445 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
)) {
446 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
447 zlog_debug("\t%s: Nexthop Filtered",
448 __PRETTY_FUNCTION__
);
453 * Check to see if we should trust the passed in information
454 * for UNNUMBERED interfaces as that we won't find the GW
455 * address in the routing table.
457 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
458 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
459 if ((ifp
&& connected_is_unnumbered(ifp
))
460 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
461 if (if_is_operative(ifp
))
464 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
466 "\t%s: Onlink and interface %s is not operative",
467 __PRETTY_FUNCTION__
, ifp
->name
);
471 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
473 "\t%s: Interface %s is not unnumbered",
475 ifp
? ifp
->name
: "Unknown");
480 /* Make lookup prefix. */
481 memset(&p
, 0, sizeof(struct prefix
));
485 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
486 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
490 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
491 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
494 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
498 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
500 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
501 zlog_debug("\t%s: Table not found",
502 __PRETTY_FUNCTION__
);
506 rn
= route_node_match(table
, (struct prefix
*)&p
);
508 route_unlock_node(rn
);
510 /* Lookup should halt if we've matched against ourselves ('top',
511 * if specified) - i.e., we cannot have a nexthop NH1 is
512 * resolved by a route NH1. The exception is if the route is a
515 if (top
&& rn
== top
)
516 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
517 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
518 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
520 "\t%s: Matched against ourself and prefix length is not max bit length",
521 __PRETTY_FUNCTION__
);
525 /* Pick up selected route. */
526 /* However, do not resolve over default route unless explicitly
528 if (is_default_prefix(&rn
->p
)
529 && !rnh_resolve_via_default(p
.family
)) {
530 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
532 "\t:%s: Resolved against default route",
533 __PRETTY_FUNCTION__
);
537 dest
= rib_dest_from_rnode(rn
);
538 if (dest
&& dest
->selected_fib
539 && !CHECK_FLAG(dest
->selected_fib
->status
,
541 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
542 match
= dest
->selected_fib
;
544 /* If there is no selected route or matched route is EGP, go up
549 } while (rn
&& rn
->info
== NULL
);
556 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
557 /* Directly point connected route. */
558 newhop
= match
->ng
.nexthop
;
560 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
561 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
562 nexthop
->ifindex
= newhop
->ifindex
;
565 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
567 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
568 if (!CHECK_FLAG(newhop
->flags
,
571 if (CHECK_FLAG(newhop
->flags
,
572 NEXTHOP_FLAG_RECURSIVE
))
576 SET_FLAG(nexthop
->flags
,
577 NEXTHOP_FLAG_RECURSIVE
);
579 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
580 nexthop_set_resolved(afi
, newhop
,
586 re
->nexthop_mtu
= match
->mtu
;
587 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
588 zlog_debug("\t%s: Recursion failed to find",
589 __PRETTY_FUNCTION__
);
591 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
593 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
594 if (!CHECK_FLAG(newhop
->flags
,
599 SET_FLAG(nexthop
->flags
,
600 NEXTHOP_FLAG_RECURSIVE
);
601 nexthop_set_resolved(afi
, newhop
,
607 re
->nexthop_mtu
= match
->mtu
;
609 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
611 "\t%s: Static route unable to resolve",
612 __PRETTY_FUNCTION__
);
615 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
616 zlog_debug("\t%s: Route Type %s has not turned on recursion",
618 zebra_route_string(re
->type
));
619 if (re
->type
== ZEBRA_ROUTE_BGP
&&
620 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
621 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
626 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
627 zlog_debug("\t%s: Nexthop did not lookup in table",
628 __PRETTY_FUNCTION__
);
632 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
633 union g_addr
*addr
, struct route_node
**rn_out
)
636 struct route_table
*table
;
637 struct route_node
*rn
;
638 struct route_entry
*match
= NULL
;
639 struct nexthop
*newhop
;
642 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
646 memset(&p
, 0, sizeof(struct prefix
));
649 p
.u
.prefix4
= addr
->ipv4
;
650 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
652 p
.u
.prefix6
= addr
->ipv6
;
653 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
656 rn
= route_node_match(table
, (struct prefix
*)&p
);
661 route_unlock_node(rn
);
663 dest
= rib_dest_from_rnode(rn
);
664 if (dest
&& dest
->selected_fib
665 && !CHECK_FLAG(dest
->selected_fib
->status
,
666 ROUTE_ENTRY_REMOVED
))
667 match
= dest
->selected_fib
;
669 /* If there is no selected route or matched route is EGP, go up
674 } while (rn
&& rn
->info
== NULL
);
678 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
680 for (ALL_NEXTHOPS(match
->ng
, newhop
))
681 if (CHECK_FLAG(newhop
->flags
,
698 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
700 struct route_node
**rn_out
)
702 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
703 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
704 union g_addr gaddr
= {.ipv4
= addr
};
706 switch (ipv4_multicast_mode
) {
707 case MCAST_MRIB_ONLY
:
708 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
710 case MCAST_URIB_ONLY
:
711 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
712 case MCAST_NO_CONFIG
:
713 case MCAST_MIX_MRIB_FIRST
:
714 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
717 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
720 case MCAST_MIX_DISTANCE
:
721 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
722 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
724 re
= ure
->distance
< mre
->distance
? ure
: mre
;
730 case MCAST_MIX_PFXLEN
:
731 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
732 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
734 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
743 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
745 if (IS_ZEBRA_DEBUG_RIB
) {
747 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
749 zlog_debug("%s: %s: vrf: %u found %s, using %s",
750 __func__
, buf
, vrf_id
,
751 mre
? (ure
? "MRIB+URIB" : "MRIB")
752 : ure
? "URIB" : "nothing",
753 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
758 void multicast_mode_ipv4_set(enum multicast_mode mode
)
760 if (IS_ZEBRA_DEBUG_RIB
)
761 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
763 ipv4_multicast_mode
= mode
;
766 enum multicast_mode
multicast_mode_ipv4_get(void)
768 return ipv4_multicast_mode
;
771 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
773 struct route_table
*table
;
774 struct route_node
*rn
;
775 struct route_entry
*match
= NULL
;
776 struct nexthop
*nexthop
;
780 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
784 rn
= route_node_lookup(table
, (struct prefix
*)p
);
786 /* No route for this prefix. */
791 route_unlock_node(rn
);
792 dest
= rib_dest_from_rnode(rn
);
794 if (dest
&& dest
->selected_fib
795 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
796 match
= dest
->selected_fib
;
801 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
804 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
805 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
811 #define RIB_SYSTEM_ROUTE(R) \
812 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
814 #define RIB_KERNEL_ROUTE(R) \
815 ((R)->type == ZEBRA_ROUTE_KERNEL)
817 /* This function verifies reachability of one given nexthop, which can be
818 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
819 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
820 * nexthop->ifindex will be updated appropriately as well.
821 * An existing route map can turn (otherwise active) nexthop into inactive, but
824 * The return value is the final value of 'ACTIVE' flag.
827 static unsigned nexthop_active_check(struct route_node
*rn
,
828 struct route_entry
*re
,
829 struct nexthop
*nexthop
, int set
)
831 struct interface
*ifp
;
832 route_map_result_t ret
= RMAP_MATCH
;
834 char buf
[SRCDEST2STR_BUFFER
];
835 const struct prefix
*p
, *src_p
;
836 struct zebra_vrf
*zvrf
;
838 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
840 if (rn
->p
.family
== AF_INET
)
842 else if (rn
->p
.family
== AF_INET6
)
846 switch (nexthop
->type
) {
847 case NEXTHOP_TYPE_IFINDEX
:
848 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
849 if (ifp
&& if_is_operative(ifp
))
850 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
852 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
854 case NEXTHOP_TYPE_IPV4
:
855 case NEXTHOP_TYPE_IPV4_IFINDEX
:
857 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
858 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
860 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
862 case NEXTHOP_TYPE_IPV6
:
864 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
865 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
867 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
869 case NEXTHOP_TYPE_IPV6_IFINDEX
:
870 /* RFC 5549, v4 prefix with v6 NH */
871 if (rn
->p
.family
!= AF_INET
)
873 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
874 ifp
= if_lookup_by_index(nexthop
->ifindex
,
876 if (ifp
&& if_is_operative(ifp
))
877 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
879 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
881 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
882 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
884 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
887 case NEXTHOP_TYPE_BLACKHOLE
:
888 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
893 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
894 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
895 zlog_debug("\t%s: Unable to find a active nexthop",
896 __PRETTY_FUNCTION__
);
900 /* XXX: What exactly do those checks do? Do we support
901 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
903 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
904 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
905 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
907 /* The original code didn't determine the family correctly
908 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
909 * from the rib_table_info in those cases.
910 * Possibly it may be better to use only the rib_table_info
914 rib_table_info_t
*info
;
916 info
= srcdest_rnode_table_info(rn
);
920 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
922 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
924 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
925 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
926 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
929 /* It'll get set if required inside */
930 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
931 nexthop
, zvrf
, re
->tag
);
932 if (ret
== RMAP_DENYMATCH
) {
933 if (IS_ZEBRA_DEBUG_RIB
) {
934 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
936 "%u:%s: Filtering out with NH out %s due to route map",
938 ifindex2ifname(nexthop
->ifindex
,
941 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
943 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
946 /* Iterate over all nexthops of the given RIB entry and refresh their
947 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
948 * nexthop is found to toggle the ACTIVE flag, the whole re structure
949 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
950 * transparently passed to nexthop_active_check().
952 * Return value is the new number of active nexthops.
955 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
958 struct nexthop
*nexthop
;
959 union g_addr prev_src
;
960 unsigned int prev_active
, new_active
, old_num_nh
;
961 ifindex_t prev_index
;
963 old_num_nh
= re
->nexthop_active_num
;
965 re
->nexthop_active_num
= 0;
966 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
968 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
969 /* No protocol daemon provides src and so we're skipping
971 prev_src
= nexthop
->rmap_src
;
972 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
973 prev_index
= nexthop
->ifindex
;
974 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
975 re
->nexthop_active_num
++;
976 /* Don't allow src setting on IPv6 addr for now */
977 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
978 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
979 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
980 && prev_src
.ipv4
.s_addr
981 != nexthop
->rmap_src
.ipv4
.s_addr
)
982 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
983 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
984 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
985 &nexthop
->rmap_src
.ipv6
)))) {
986 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
987 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
991 if (old_num_nh
!= re
->nexthop_active_num
)
992 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
994 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
995 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
998 return re
->nexthop_active_num
;
1002 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1003 * (nexthops) must have a label.
1005 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1007 struct nexthop
*nexthop
= NULL
;
1009 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1012 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1013 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1019 /* Update flag indicates whether this is a "replace" or not. Currently, this
1020 * is only used for IPv4.
1022 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1023 struct route_entry
*old
)
1025 struct nexthop
*nexthop
;
1026 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1027 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1028 const struct prefix
*p
, *src_p
;
1029 enum zebra_dplane_result ret
;
1031 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1033 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1035 if (info
->safi
!= SAFI_UNICAST
) {
1036 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1037 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1040 struct nexthop
*prev
;
1042 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1043 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1044 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1045 if (prev
== nexthop
)
1047 if (nexthop_same_firsthop(nexthop
, prev
)) {
1048 SET_FLAG(nexthop
->flags
,
1049 NEXTHOP_FLAG_DUPLICATE
);
1057 * If this is a replace to a new RE let the originator of the RE
1058 * know that they've lost
1060 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1061 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1063 /* Update fib selection */
1064 dest
->selected_fib
= re
;
1067 * Make sure we update the FPM any time we send new information to
1070 hook_call(rib_update
, rn
, "installing in kernel");
1072 /* Send add or update */
1073 if (old
&& (old
!= re
))
1074 ret
= dplane_route_update(rn
, re
, old
);
1076 ret
= dplane_route_add(rn
, re
);
1079 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1081 zvrf
->installs_queued
++;
1083 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1085 char str
[SRCDEST2STR_BUFFER
];
1087 srcdest_rnode2str(rn
, str
, sizeof(str
));
1088 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1089 "%u:%s: Failed to enqueue dataplane install",
1093 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1102 /* Uninstall the route from kernel. */
1103 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1105 struct nexthop
*nexthop
;
1106 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1107 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1109 if (info
->safi
!= SAFI_UNICAST
) {
1110 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1111 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1116 * Make sure we update the FPM any time we send new information to
1119 hook_call(rib_update
, rn
, "uninstalling from kernel");
1121 switch (dplane_route_delete(rn
, re
)) {
1122 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1124 zvrf
->removals_queued
++;
1126 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1128 char str
[SRCDEST2STR_BUFFER
];
1130 srcdest_rnode2str(rn
, str
, sizeof(str
));
1131 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1132 "%u:%s: Failed to enqueue dataplane uninstall",
1136 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1145 /* Uninstall the route from kernel. */
1146 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1148 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1149 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1150 struct nexthop
*nexthop
;
1152 if (dest
&& dest
->selected_fib
== re
) {
1153 if (info
->safi
== SAFI_UNICAST
)
1154 hook_call(rib_update
, rn
, "rib_uninstall");
1156 /* If labeled-unicast route, uninstall transit LSP. */
1157 if (zebra_rib_labeled_unicast(re
))
1158 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1160 if (!RIB_SYSTEM_ROUTE(re
))
1161 rib_uninstall_kernel(rn
, re
);
1163 dest
->selected_fib
= NULL
;
1165 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1166 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1169 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1170 const struct prefix
*p
, *src_p
;
1172 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1174 redistribute_delete(p
, src_p
, re
);
1175 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1180 * rib_can_delete_dest
1182 * Returns TRUE if the given dest can be deleted from the table.
1184 static int rib_can_delete_dest(rib_dest_t
*dest
)
1191 * Don't delete the dest if we have to update the FPM about this
1194 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1195 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1204 * Garbage collect the rib dest corresponding to the given route node
1207 * Returns TRUE if the dest was deleted, FALSE otherwise.
1209 int rib_gc_dest(struct route_node
*rn
)
1213 dest
= rib_dest_from_rnode(rn
);
1217 if (!rib_can_delete_dest(dest
))
1220 if (IS_ZEBRA_DEBUG_RIB
) {
1221 struct zebra_vrf
*zvrf
;
1223 zvrf
= rib_dest_vrf(dest
);
1224 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1228 XFREE(MTYPE_RIB_DEST
, dest
);
1232 * Release the one reference that we keep on the route node.
1234 route_unlock_node(rn
);
1238 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1239 struct route_entry
*new)
1241 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1243 hook_call(rib_update
, rn
, "new route selected");
1245 /* Update real nexthop. This may actually determine if nexthop is active
1247 if (!nexthop_active_update(rn
, new, 1)) {
1248 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1252 if (IS_ZEBRA_DEBUG_RIB
) {
1253 char buf
[SRCDEST2STR_BUFFER
];
1254 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1255 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1256 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1259 /* If labeled-unicast route, install transit LSP. */
1260 if (zebra_rib_labeled_unicast(new))
1261 zebra_mpls_lsp_install(zvrf
, rn
, new);
1263 if (!RIB_SYSTEM_ROUTE(new))
1264 rib_install_kernel(rn
, new, NULL
);
1266 dest
->selected_fib
= new;
1268 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1271 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1272 struct route_entry
*old
)
1274 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1275 hook_call(rib_update
, rn
, "removing existing route");
1277 /* Uninstall from kernel. */
1278 if (IS_ZEBRA_DEBUG_RIB
) {
1279 char buf
[SRCDEST2STR_BUFFER
];
1280 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1281 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1282 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1285 /* If labeled-unicast route, uninstall transit LSP. */
1286 if (zebra_rib_labeled_unicast(old
))
1287 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1289 if (!RIB_SYSTEM_ROUTE(old
))
1290 rib_uninstall_kernel(rn
, old
);
1293 * We are setting this to NULL here
1294 * because that is what we traditionally
1295 * have been doing. I am not positive
1296 * that this is the right thing to do
1297 * but let's leave the code alone
1298 * for the RIB_SYSTEM_ROUTE case
1300 dest
->selected_fib
= NULL
;
1303 /* Update nexthop for route, reset changed flag. */
1304 /* Note: this code also handles the Linux case when an interface goes
1305 * down, causing the kernel to delete routes without sending DELROUTE
1308 if (!nexthop_active_update(rn
, old
, 1) &&
1309 (RIB_KERNEL_ROUTE(old
)))
1310 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1312 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1315 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1316 struct route_node
*rn
,
1317 struct route_entry
*old
,
1318 struct route_entry
*new)
1320 struct nexthop
*nexthop
= NULL
;
1322 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1325 * We have to install or update if a new route has been selected or
1326 * something has changed.
1328 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1329 hook_call(rib_update
, rn
, "updating existing route");
1331 /* Update the nexthop; we could determine here that nexthop is
1333 if (nexthop_active_update(rn
, new, 1))
1336 /* If nexthop is active, install the selected route, if
1338 * the install succeeds, cleanup flags for prior route, if
1343 if (IS_ZEBRA_DEBUG_RIB
) {
1344 char buf
[SRCDEST2STR_BUFFER
];
1345 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1348 "%u:%s: Updating route rn %p, re %p (type %d) "
1350 zvrf_id(zvrf
), buf
, rn
, new,
1351 new->type
, old
, old
->type
);
1354 "%u:%s: Updating route rn %p, re %p (type %d)",
1355 zvrf_id(zvrf
), buf
, rn
, new,
1359 /* If labeled-unicast route, uninstall transit LSP. */
1360 if (zebra_rib_labeled_unicast(old
))
1361 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1363 /* Non-system route should be installed. */
1364 if (!RIB_SYSTEM_ROUTE(new)) {
1365 /* If labeled-unicast route, install transit
1367 if (zebra_rib_labeled_unicast(new))
1368 zebra_mpls_lsp_install(zvrf
, rn
, new);
1370 rib_install_kernel(rn
, new, old
);
1373 * We do not need to install the
1374 * selected route because it
1375 * is already isntalled by
1376 * the system( ie not us )
1377 * so just mark it as winning
1378 * we do need to ensure that
1379 * if we uninstall a route
1380 * from ourselves we don't
1381 * over write this pointer
1383 dest
->selected_fib
= NULL
;
1385 /* If install succeeded or system route, cleanup flags
1386 * for prior route. */
1388 if (RIB_SYSTEM_ROUTE(new)) {
1389 if (!RIB_SYSTEM_ROUTE(old
))
1390 rib_uninstall_kernel(rn
, old
);
1392 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1393 nexthop
= nexthop
->next
)
1394 UNSET_FLAG(nexthop
->flags
,
1401 * If nexthop for selected route is not active or install
1403 * may need to uninstall and delete for redistribution.
1406 if (IS_ZEBRA_DEBUG_RIB
) {
1407 char buf
[SRCDEST2STR_BUFFER
];
1408 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1411 "%u:%s: Deleting route rn %p, re %p (type %d) "
1412 "old %p (type %d) - nexthop inactive",
1413 zvrf_id(zvrf
), buf
, rn
, new,
1414 new->type
, old
, old
->type
);
1417 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1418 zvrf_id(zvrf
), buf
, rn
, new,
1422 /* If labeled-unicast route, uninstall transit LSP. */
1423 if (zebra_rib_labeled_unicast(old
))
1424 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1426 if (!RIB_SYSTEM_ROUTE(old
))
1427 rib_uninstall_kernel(rn
, old
);
1429 dest
->selected_fib
= NULL
;
1433 * Same route selected; check if in the FIB and if not,
1435 * is housekeeping code to deal with race conditions in kernel
1437 * netlink reporting interface up before IPv4 or IPv6 protocol
1441 if (!RIB_SYSTEM_ROUTE(new)) {
1442 bool in_fib
= false;
1444 for (ALL_NEXTHOPS(new->ng
, nexthop
))
1445 if (CHECK_FLAG(nexthop
->flags
,
1446 NEXTHOP_FLAG_FIB
)) {
1451 rib_install_kernel(rn
, new, NULL
);
1455 /* Update prior route. */
1457 /* Set real nexthop. */
1458 nexthop_active_update(rn
, old
, 1);
1459 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1462 /* Clear changed flag. */
1463 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1466 /* Check if 'alternate' RIB entry is better than 'current'. */
1467 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1468 struct route_entry
*alternate
)
1470 if (current
== NULL
)
1473 /* filter route selection in following order:
1474 * - connected beats other types
1475 * - if both connected, loopback or vrf wins
1476 * - lower distance beats higher
1477 * - lower metric beats higher for equal distance
1478 * - last, hence oldest, route wins tie break.
1481 /* Connected routes. Check to see if either are a vrf
1482 * or loopback interface. If not, pick the last connected
1483 * route of the set of lowest metric connected routes.
1485 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1486 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1489 /* both are connected. are either loop or vrf? */
1490 struct nexthop
*nexthop
= NULL
;
1492 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1493 if (if_is_loopback_or_vrf(if_lookup_by_index(
1494 nexthop
->ifindex
, alternate
->vrf_id
)))
1498 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1499 if (if_is_loopback_or_vrf(if_lookup_by_index(
1500 nexthop
->ifindex
, current
->vrf_id
)))
1504 /* Neither are loop or vrf so pick best metric */
1505 if (alternate
->metric
<= current
->metric
)
1511 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1514 /* higher distance loses */
1515 if (alternate
->distance
< current
->distance
)
1517 if (current
->distance
< alternate
->distance
)
1520 /* metric tie-breaks equal distance */
1521 if (alternate
->metric
<= current
->metric
)
1527 /* Core function for processing routing information base. */
1528 static void rib_process(struct route_node
*rn
)
1530 struct route_entry
*re
;
1531 struct route_entry
*next
;
1532 struct route_entry
*old_selected
= NULL
;
1533 struct route_entry
*new_selected
= NULL
;
1534 struct route_entry
*old_fib
= NULL
;
1535 struct route_entry
*new_fib
= NULL
;
1536 struct route_entry
*best
= NULL
;
1537 char buf
[SRCDEST2STR_BUFFER
];
1539 struct zebra_vrf
*zvrf
= NULL
;
1540 const struct prefix
*p
, *src_p
;
1542 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1543 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1547 dest
= rib_dest_from_rnode(rn
);
1549 zvrf
= rib_dest_vrf(dest
);
1550 vrf_id
= zvrf_id(zvrf
);
1553 if (IS_ZEBRA_DEBUG_RIB
)
1554 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1556 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1557 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1560 * we can have rn's that have a NULL info pointer
1561 * (dest). As such let's not let the deref happen
1562 * additionally we know RNODE_FOREACH_RE_SAFE
1563 * will not iterate so we are ok.
1566 old_fib
= dest
->selected_fib
;
1568 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1569 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1571 "%u:%s: Examine re %p (type %d) status %x flags %x "
1572 "dist %d metric %d",
1573 vrf_id
, buf
, re
, re
->type
, re
->status
,
1574 re
->flags
, re
->distance
, re
->metric
);
1576 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1578 /* Currently selected re. */
1579 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1580 assert(old_selected
== NULL
);
1584 /* Skip deleted entries from selection */
1585 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1588 /* Skip unreachable nexthop. */
1589 /* This first call to nexthop_active_update is merely to
1591 * there's any change to nexthops associated with this RIB
1593 * rib_process() can be invoked due to an external event such as
1595 * down or due to next-hop-tracking evaluation. In the latter
1597 * a decision has already been made that the NHs have changed.
1599 * need to invoke a potentially expensive call again. Further,
1601 * the change might be in a recursive NH which is not caught in
1602 * the nexthop_active_update() code. Thus, we might miss changes
1606 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1607 && !nexthop_active_update(rn
, re
, 0)) {
1608 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1609 /* XXX: HERE BE DRAGONS!!!!!
1610 * In all honesty, I have not yet figured out
1612 * does or why the ROUTE_ENTRY_CHANGED test
1614 * or why we need to delete a route here, and
1616 * this concerns both selected and fib route, or
1619 /* This entry was denied by the 'ip protocol
1620 * table' route-map, we
1621 * need to delete it */
1622 if (re
!= old_selected
) {
1623 if (IS_ZEBRA_DEBUG_RIB
)
1625 "%s: %u:%s: imported via import-table but denied "
1626 "by the ip protocol table route-map",
1627 __func__
, vrf_id
, buf
);
1630 SET_FLAG(re
->status
,
1631 ROUTE_ENTRY_REMOVED
);
1637 /* Infinite distance. */
1638 if (re
->distance
== DISTANCE_INFINITY
) {
1639 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1643 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1644 best
= rib_choose_best(new_fib
, re
);
1645 if (new_fib
&& best
!= new_fib
)
1646 UNSET_FLAG(new_fib
->status
,
1647 ROUTE_ENTRY_CHANGED
);
1650 best
= rib_choose_best(new_selected
, re
);
1651 if (new_selected
&& best
!= new_selected
)
1652 UNSET_FLAG(new_selected
->status
,
1653 ROUTE_ENTRY_CHANGED
);
1654 new_selected
= best
;
1657 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1658 } /* RNODE_FOREACH_RE */
1660 /* If no FIB override route, use the selected route also for FIB */
1661 if (new_fib
== NULL
)
1662 new_fib
= new_selected
;
1664 /* After the cycle is finished, the following pointers will be set:
1665 * old_selected --- RE entry currently having SELECTED
1666 * new_selected --- RE entry that is newly SELECTED
1667 * old_fib --- RE entry currently in kernel FIB
1668 * new_fib --- RE entry that is newly to be in kernel FIB
1670 * new_selected will get SELECTED flag, and is going to be redistributed
1671 * the zclients. new_fib (which can be new_selected) will be installed
1675 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1677 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1678 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1679 (void *)old_fib
, (void *)new_fib
);
1682 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1683 * fib == selected */
1684 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1685 ROUTE_ENTRY_CHANGED
);
1687 /* Update fib according to selection results */
1688 if (new_fib
&& old_fib
)
1689 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1691 rib_process_add_fib(zvrf
, rn
, new_fib
);
1693 rib_process_del_fib(zvrf
, rn
, old_fib
);
1695 /* Update SELECTED entry */
1696 if (old_selected
!= new_selected
|| selected_changed
) {
1698 if (new_selected
&& new_selected
!= new_fib
) {
1699 nexthop_active_update(rn
, new_selected
, 1);
1700 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1704 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1706 /* Special case: new route is system route, so
1707 * dataplane update will not be done - ensure we
1708 * redistribute the route.
1710 if (RIB_SYSTEM_ROUTE(new_selected
))
1711 redistribute_update(p
, src_p
, new_selected
,
1717 redistribute_delete(p
, src_p
, old_selected
);
1718 if (old_selected
!= new_selected
)
1719 UNSET_FLAG(old_selected
->flags
,
1720 ZEBRA_FLAG_SELECTED
);
1724 /* Remove all RE entries queued for removal */
1725 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1726 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1727 if (IS_ZEBRA_DEBUG_RIB
) {
1728 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1729 (void *)rn
, (void *)re
);
1736 * Check if the dest can be deleted now.
1742 * Utility to match route with dplane context data
1744 static bool rib_route_match_ctx(const struct route_entry
*re
,
1745 const struct zebra_dplane_ctx
*ctx
,
1748 bool result
= false;
1752 * In 'update' case, we test info about the 'previous' or
1755 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1756 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1759 /* TODO -- we're using this extra test, but it's not
1760 * exactly clear why.
1762 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1763 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1764 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1771 * Ordinary, single-route case using primary context info
1773 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1774 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1775 /* Skip route that's been deleted */
1779 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1780 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1783 /* TODO -- we're using this extra test, but it's not
1784 * exactly clear why.
1786 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1787 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1788 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1800 * Route-update results processing after async dataplane update.
1802 static void rib_process_after(struct zebra_dplane_ctx
*ctx
)
1804 struct route_table
*table
= NULL
;
1805 struct zebra_vrf
*zvrf
= NULL
;
1806 struct route_node
*rn
= NULL
;
1807 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1808 bool is_update
= false;
1809 struct nexthop
*nexthop
, *ctx_nexthop
;
1810 char dest_str
[PREFIX_STRLEN
] = "";
1811 enum dplane_op_e op
;
1812 enum zebra_dplane_result status
;
1813 const struct prefix
*dest_pfx
, *src_pfx
;
1815 /* Locate rn and re(s) from ctx */
1817 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1818 dplane_ctx_get_safi(ctx
),
1819 dplane_ctx_get_vrf(ctx
),
1820 dplane_ctx_get_table(ctx
));
1821 if (table
== NULL
) {
1822 if (IS_ZEBRA_DEBUG_DPLANE
) {
1823 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1824 dplane_ctx_get_afi(ctx
),
1825 dplane_ctx_get_safi(ctx
),
1826 dplane_ctx_get_vrf(ctx
));
1831 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1833 dest_pfx
= dplane_ctx_get_dest(ctx
);
1835 /* Note well: only capturing the prefix string if debug is enabled here;
1836 * unconditional log messages will have to generate the string.
1838 if (IS_ZEBRA_DEBUG_DPLANE
)
1839 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1841 src_pfx
= dplane_ctx_get_src(ctx
);
1842 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1843 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1845 if (IS_ZEBRA_DEBUG_DPLANE
) {
1846 zlog_debug("Failed to process dplane results: no route for %u:%s",
1847 dplane_ctx_get_vrf(ctx
), dest_str
);
1852 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1854 op
= dplane_ctx_get_op(ctx
);
1855 status
= dplane_ctx_get_status(ctx
);
1857 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1858 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1859 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1860 dplane_op2str(op
), dplane_res2str(status
));
1862 if (op
== DPLANE_OP_ROUTE_DELETE
) {
1864 * In the delete case, the zebra core datastructs were
1865 * updated (or removed) at the time the delete was issued,
1866 * so we're just notifying the route owner.
1868 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1869 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1874 zsend_route_notify_owner_ctx(ctx
,
1875 ZAPI_ROUTE_FAIL_INSTALL
);
1877 zlog_warn("%u:%s: Route Deletion failure",
1878 dplane_ctx_get_vrf(ctx
),
1879 prefix2str(dest_pfx
,
1880 dest_str
, sizeof(dest_str
)));
1883 /* Nothing more to do in delete case */
1888 * Update is a bit of a special case, where we may have both old and new
1889 * routes to post-process.
1891 is_update
= dplane_ctx_is_update(ctx
);
1894 * Take a pass through the routes, look for matches with the context
1897 RNODE_FOREACH_RE(rn
, rib
) {
1900 if (rib_route_match_ctx(rib
, ctx
, false))
1904 /* Check for old route match */
1905 if (is_update
&& (old_re
== NULL
)) {
1906 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1910 /* Have we found the routes we need to work on? */
1911 if (re
&& ((!is_update
|| old_re
)))
1916 * Check sequence number(s) to detect stale results before continuing
1918 if (re
&& (re
->dplane_sequence
!= dplane_ctx_get_seq(ctx
))) {
1919 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1920 zlog_debug("%u:%s Stale dplane result for re %p",
1921 dplane_ctx_get_vrf(ctx
), dest_str
, re
);
1927 (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
))) {
1928 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1929 zlog_debug("%u:%s Stale dplane result for old_re %p",
1930 dplane_ctx_get_vrf(ctx
), dest_str
, old_re
);
1936 * Here's sort of a tough one: the route update result is stale.
1937 * Is it better to use the context block info to generate
1938 * redist and owner notification, or is it better to wait
1939 * for the up-to-date result to arrive?
1942 /* TODO -- for now, only expose up-to-date results */
1946 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1947 /* Update zebra nexthop FIB flag for each
1948 * nexthop that was installed.
1950 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1952 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1953 if (nexthop_same(ctx_nexthop
, nexthop
))
1957 if (nexthop
== NULL
)
1960 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1963 if (CHECK_FLAG(ctx_nexthop
->flags
,
1965 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1967 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1972 /* Set flag for nexthop tracking processing */
1973 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1977 /* TODO -- still calling the redist api using the route_entries,
1978 * and there's a corner-case here: if there's no client
1979 * for the 'new' route, a redist deleting the 'old' route
1980 * will be sent. But if the 'old' context info was stale,
1981 * 'old_re' will be NULL here and that delete will not be sent.
1983 redistribute_update(dest_pfx
, src_pfx
, re
, old_re
);
1985 /* Notify route owner */
1986 zsend_route_notify_owner(re
,
1987 dest_pfx
, ZAPI_ROUTE_INSTALLED
);
1990 zsend_route_notify_owner(re
, dest_pfx
,
1991 ZAPI_ROUTE_FAIL_INSTALL
);
1993 zlog_warn("%u:%s: Route install failed",
1994 dplane_ctx_get_vrf(ctx
),
1995 prefix2str(dest_pfx
,
1996 dest_str
, sizeof(dest_str
)));
2001 /* Return context to dataplane module */
2002 dplane_ctx_fini(&ctx
);
2005 /* Take a list of route_node structs and return 1, if there was a record
2006 * picked from it and processed by rib_process(). Don't process more,
2007 * than one RN record; operate only in the specified sub-queue.
2009 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2011 struct listnode
*lnode
= listhead(subq
);
2012 struct route_node
*rnode
;
2014 struct zebra_vrf
*zvrf
= NULL
;
2019 rnode
= listgetdata(lnode
);
2020 dest
= rib_dest_from_rnode(rnode
);
2022 zvrf
= rib_dest_vrf(dest
);
2026 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2027 char buf
[SRCDEST2STR_BUFFER
];
2028 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2029 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2030 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2034 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2035 RIB_ROUTE_QUEUED(qindex
));
2040 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2041 __func__
, rnode
, rnode
->lock
);
2042 zlog_backtrace(LOG_DEBUG
);
2045 route_unlock_node(rnode
);
2046 list_delete_node(subq
, lnode
);
2051 * Perform next-hop tracking processing after RIB updates.
2053 static void do_nht_processing(void)
2056 struct zebra_vrf
*zvrf
;
2058 /* Evaluate nexthops for those VRFs which underwent route processing.
2060 * should limit the evaluation to the necessary VRFs in most common
2063 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2065 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2068 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHT
)
2069 zlog_debug("NHT processing check for zvrf %s",
2072 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2073 zebra_evaluate_rnh(zvrf
, AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2074 zebra_evaluate_rnh(zvrf
, AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
,
2076 zebra_evaluate_rnh(zvrf
, AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2077 zebra_evaluate_rnh(zvrf
, AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
,
2081 /* Schedule LSPs for processing, if needed. */
2082 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2083 if (mpls_should_lsps_be_processed(zvrf
)) {
2084 if (IS_ZEBRA_DEBUG_MPLS
)
2086 "%u: Scheduling all LSPs upon RIB completion",
2088 zebra_mpls_lsp_schedule(zvrf
);
2089 mpls_unmark_lsps_for_processing(zvrf
);
2094 * All meta queues have been processed. Trigger next-hop evaluation.
2096 static void meta_queue_process_complete(struct work_queue
*dummy
)
2098 do_nht_processing();
2101 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2102 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2104 * is pointed to the meta queue structure.
2106 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2108 struct meta_queue
*mq
= data
;
2110 uint32_t queue_len
, queue_limit
;
2112 /* Ensure there's room for more dataplane updates */
2113 queue_limit
= dplane_get_in_queue_limit();
2114 queue_len
= dplane_get_in_queue_len();
2115 if (queue_len
> queue_limit
) {
2116 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2117 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2118 queue_len
, queue_limit
);
2120 /* Ensure that the meta-queue is actually enqueued */
2121 if (work_queue_empty(zebrad
.ribq
))
2122 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2124 return WQ_QUEUE_BLOCKED
;
2127 for (i
= 0; i
< MQ_SIZE
; i
++)
2128 if (process_subq(mq
->subq
[i
], i
)) {
2132 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2136 * Map from rib types to queue type (priority) in meta queue
2138 static const uint8_t meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
2139 [ZEBRA_ROUTE_SYSTEM
] = 4,
2140 [ZEBRA_ROUTE_KERNEL
] = 0,
2141 [ZEBRA_ROUTE_CONNECT
] = 0,
2142 [ZEBRA_ROUTE_STATIC
] = 1,
2143 [ZEBRA_ROUTE_RIP
] = 2,
2144 [ZEBRA_ROUTE_RIPNG
] = 2,
2145 [ZEBRA_ROUTE_OSPF
] = 2,
2146 [ZEBRA_ROUTE_OSPF6
] = 2,
2147 [ZEBRA_ROUTE_ISIS
] = 2,
2148 [ZEBRA_ROUTE_BGP
] = 3,
2149 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
2150 [ZEBRA_ROUTE_EIGRP
] = 2,
2151 [ZEBRA_ROUTE_NHRP
] = 2,
2152 [ZEBRA_ROUTE_HSLS
] = 4,
2153 [ZEBRA_ROUTE_OLSR
] = 4,
2154 [ZEBRA_ROUTE_TABLE
] = 1,
2155 [ZEBRA_ROUTE_LDP
] = 4,
2156 [ZEBRA_ROUTE_VNC
] = 3,
2157 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
2158 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
2159 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
2160 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
2161 [ZEBRA_ROUTE_BABEL
] = 2,
2162 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
2165 /* Look into the RN and queue it into one or more priority queues,
2166 * increasing the size for each data push done.
2168 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2170 struct route_entry
*re
;
2172 RNODE_FOREACH_RE (rn
, re
) {
2173 uint8_t qindex
= meta_queue_map
[re
->type
];
2174 struct zebra_vrf
*zvrf
;
2176 /* Invariant: at this point we always have rn->info set. */
2177 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2178 RIB_ROUTE_QUEUED(qindex
))) {
2179 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2182 "rn %p is already queued in sub-queue %u",
2183 (void *)rn
, qindex
);
2187 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
2188 RIB_ROUTE_QUEUED(qindex
));
2189 listnode_add(mq
->subq
[qindex
], rn
);
2190 route_lock_node(rn
);
2193 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2194 rnode_debug(rn
, re
->vrf_id
,
2195 "queued rn %p into sub-queue %u",
2196 (void *)rn
, qindex
);
2198 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2200 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2204 /* Add route_node to work queue and schedule processing */
2205 void rib_queue_add(struct route_node
*rn
)
2209 /* Pointless to queue a route_node with no RIB entries to add or remove
2211 if (!rnode_to_ribs(rn
)) {
2212 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2213 __func__
, (void *)rn
, rn
->lock
);
2214 zlog_backtrace(LOG_DEBUG
);
2218 if (zebrad
.ribq
== NULL
) {
2219 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2220 "%s: work_queue does not exist!", __func__
);
2225 * The RIB queue should normally be either empty or holding the only
2226 * work_queue_item element. In the latter case this element would
2227 * hold a pointer to the meta queue structure, which must be used to
2228 * actually queue the route nodes to process. So create the MQ
2229 * holder, if necessary, then push the work into it in any case.
2230 * This semantics was introduced after 0.99.9 release.
2232 if (work_queue_empty(zebrad
.ribq
))
2233 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2235 rib_meta_queue_add(zebrad
.mq
, rn
);
2240 /* Create new meta queue.
2241 A destructor function doesn't seem to be necessary here.
2243 static struct meta_queue
*meta_queue_new(void)
2245 struct meta_queue
*new;
2248 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2250 for (i
= 0; i
< MQ_SIZE
; i
++) {
2251 new->subq
[i
] = list_new();
2252 assert(new->subq
[i
]);
2258 void meta_queue_free(struct meta_queue
*mq
)
2262 for (i
= 0; i
< MQ_SIZE
; i
++)
2263 list_delete(&mq
->subq
[i
]);
2265 XFREE(MTYPE_WORK_QUEUE
, mq
);
2268 /* initialise zebra rib work queue */
2269 static void rib_queue_init(struct zebra_t
*zebra
)
2274 work_queue_new(zebra
->master
, "route_node processing"))) {
2275 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2276 "%s: could not initialise work queue!", __func__
);
2280 /* fill in the work queue spec */
2281 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2282 zebra
->ribq
->spec
.errorfunc
= NULL
;
2283 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2284 /* XXX: TODO: These should be runtime configurable via vty */
2285 zebra
->ribq
->spec
.max_retries
= 3;
2286 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2288 if (!(zebra
->mq
= meta_queue_new())) {
2289 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2290 "%s: could not initialise meta queue!", __func__
);
2296 /* RIB updates are processed via a queue of pointers to route_nodes.
2298 * The queue length is bounded by the maximal size of the routing table,
2299 * as a route_node will not be requeued, if already queued.
2301 * REs are submitted via rib_addnode or rib_delnode which set minimal
2302 * state, or static_install_route (when an existing RE is updated)
2303 * and then submit route_node to queue for best-path selection later.
2304 * Order of add/delete state changes are preserved for any given RE.
2306 * Deleted REs are reaped during best-path selection.
2309 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2310 * |-------->| | best RE, if required
2312 * static_install->|->rib_addqueue...... -> rib_process
2314 * |-------->| |-> rib_unlink
2315 * |-> set ROUTE_ENTRY_REMOVE |
2316 * rib_delnode (RE freed)
2318 * The 'info' pointer of a route_node points to a rib_dest_t
2319 * ('dest'). Queueing state for a route_node is kept on the dest. The
2320 * dest is created on-demand by rib_link() and is kept around at least
2321 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2323 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2325 * - route_nodes: refcounted by:
2326 * - dest attached to route_node:
2327 * - managed by: rib_link/rib_gc_dest
2328 * - route_node processing queue
2329 * - managed by: rib_addqueue, rib_process.
2333 /* Add RE to head of the route node. */
2334 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2336 struct route_entry
*head
;
2339 const char *rmap_name
;
2343 dest
= rib_dest_from_rnode(rn
);
2345 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2346 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2348 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2349 route_lock_node(rn
); /* rn route table reference */
2354 head
= dest
->routes
;
2361 afi
= (rn
->p
.family
== AF_INET
)
2363 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2364 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2365 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2366 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2371 static void rib_addnode(struct route_node
*rn
,
2372 struct route_entry
*re
, int process
)
2374 /* RE node has been un-removed before route-node is processed.
2375 * route_node must hence already be on the queue for processing..
2377 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2378 if (IS_ZEBRA_DEBUG_RIB
)
2379 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2380 (void *)rn
, (void *)re
);
2382 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2385 rib_link(rn
, re
, process
);
2391 * Detach a rib structure from a route_node.
2393 * Note that a call to rib_unlink() should be followed by a call to
2394 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2395 * longer required to be deleted.
2397 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2403 if (IS_ZEBRA_DEBUG_RIB
)
2404 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2407 dest
= rib_dest_from_rnode(rn
);
2410 re
->next
->prev
= re
->prev
;
2413 re
->prev
->next
= re
->next
;
2415 dest
->routes
= re
->next
;
2418 if (dest
->selected_fib
== re
)
2419 dest
->selected_fib
= NULL
;
2421 nexthops_free(re
->ng
.nexthop
);
2422 XFREE(MTYPE_RE
, re
);
2425 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2429 if (IS_ZEBRA_DEBUG_RIB
)
2430 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2431 (void *)rn
, (void *)re
);
2432 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2434 afi
= (rn
->p
.family
== AF_INET
)
2436 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2437 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2438 zebra_del_import_table_entry(rn
, re
);
2439 /* Just clean up if non main table */
2440 if (IS_ZEBRA_DEBUG_RIB
) {
2441 char buf
[SRCDEST2STR_BUFFER
];
2442 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2444 "%u:%s: Freeing route rn %p, re %p (type %d)",
2445 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2454 /* This function dumps the contents of a given RE entry into
2455 * standard debug log. Calling function name and IP prefix in
2456 * question are passed as 1st and 2nd arguments.
2459 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2460 union prefixconstptr src_pp
,
2461 const struct route_entry
*re
)
2463 const struct prefix
*src_p
= src_pp
.p
;
2464 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2465 char straddr
[PREFIX_STRLEN
];
2466 char srcaddr
[PREFIX_STRLEN
];
2467 struct nexthop
*nexthop
;
2469 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2470 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2471 is_srcdst
? " from " : "",
2472 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2475 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2476 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2479 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2480 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2481 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2482 re
->nexthop_num
, re
->nexthop_active_num
);
2484 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2485 struct interface
*ifp
;
2486 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2488 switch (nexthop
->type
) {
2489 case NEXTHOP_TYPE_BLACKHOLE
:
2490 sprintf(straddr
, "Blackhole");
2492 case NEXTHOP_TYPE_IFINDEX
:
2493 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2495 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2497 case NEXTHOP_TYPE_IPV4
:
2499 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2500 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2503 case NEXTHOP_TYPE_IPV6
:
2504 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2505 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2509 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2510 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2511 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2513 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2516 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2519 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2523 zlog_debug("%s: dump complete", func
);
2526 /* This is an exported helper to rtm_read() to dump the strange
2527 * RE entry found by rib_lookup_ipv4_route()
2530 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2532 struct route_table
*table
;
2533 struct route_node
*rn
;
2534 struct route_entry
*re
;
2535 char prefix_buf
[INET_ADDRSTRLEN
];
2538 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2540 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2541 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2546 /* Scan the RIB table for exactly matching RE entry. */
2547 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2549 /* No route for this prefix. */
2551 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2552 prefix2str((struct prefix
*)p
, prefix_buf
,
2553 sizeof(prefix_buf
)));
2558 route_unlock_node(rn
);
2561 RNODE_FOREACH_RE (rn
, re
) {
2562 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2564 (void *)rn
, (void *)re
,
2565 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2568 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2571 route_entry_dump(p
, NULL
, re
);
2575 /* Check if requested address assignment will fail due to another
2576 * route being installed by zebra in FIB already. Take necessary
2577 * actions, if needed: remove such a route from FIB and deSELECT
2578 * corresponding RE entry. Then put affected RN into RIBQ head.
2580 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2582 struct route_table
*table
;
2583 struct route_node
*rn
;
2584 unsigned changed
= 0;
2587 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2588 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2589 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2594 /* No matches would be the simplest case. */
2595 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2599 route_unlock_node(rn
);
2601 dest
= rib_dest_from_rnode(rn
);
2602 /* Check all RE entries. In case any changes have to be done, requeue
2603 * the RN into RIBQ head. If the routing message about the new connected
2604 * route (generated by the IP address we are going to assign very soon)
2605 * comes before the RIBQ is processed, the new RE entry will join
2606 * RIBQ record already on head. This is necessary for proper
2608 * of the rest of the RE.
2610 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2612 if (IS_ZEBRA_DEBUG_RIB
) {
2613 char buf
[PREFIX_STRLEN
];
2615 zlog_debug("%u:%s: freeing way for connected prefix",
2616 dest
->selected_fib
->vrf_id
,
2617 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2618 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2620 rib_uninstall(rn
, dest
->selected_fib
);
2626 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2627 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2629 struct route_table
*table
;
2630 struct route_node
*rn
;
2631 struct route_entry
*same
= NULL
;
2632 struct nexthop
*nexthop
;
2638 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2641 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2643 XFREE(MTYPE_RE
, re
);
2647 /* Make it sure prefixlen is applied to the prefix. */
2650 apply_mask_ipv6(src_p
);
2652 /* Set default distance by route type. */
2653 if (re
->distance
== 0) {
2654 re
->distance
= route_distance(re
->type
);
2656 /* iBGP distance is 200. */
2657 if (re
->type
== ZEBRA_ROUTE_BGP
2658 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2662 /* Lookup route node.*/
2663 rn
= srcdest_rnode_get(table
, p
, src_p
);
2666 * If same type of route are installed, treat it as a implicit
2668 * If the user has specified the No route replace semantics
2669 * for the install don't do a route replace.
2671 RNODE_FOREACH_RE (rn
, same
) {
2672 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2675 if (same
->type
!= re
->type
)
2677 if (same
->instance
!= re
->instance
)
2679 if (same
->type
== ZEBRA_ROUTE_KERNEL
2680 && same
->metric
!= re
->metric
)
2683 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2684 same
->distance
!= re
->distance
)
2688 * We should allow duplicate connected routes
2689 * because of IPv6 link-local routes and unnumbered
2690 * interfaces on Linux.
2692 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2696 /* If this route is kernel route, set FIB flag to the route. */
2697 if (RIB_SYSTEM_ROUTE(re
))
2698 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2699 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2701 /* Link new re to node.*/
2702 if (IS_ZEBRA_DEBUG_RIB
) {
2705 "Inserting route rn %p, re %p (type %d) existing %p",
2706 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2708 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2709 route_entry_dump(p
, src_p
, re
);
2711 rib_addnode(rn
, re
, 1);
2714 /* Free implicit route.*/
2716 rib_delnode(rn
, same
);
2720 route_unlock_node(rn
);
2724 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2725 unsigned short instance
, int flags
, struct prefix
*p
,
2726 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2727 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2730 struct route_table
*table
;
2731 struct route_node
*rn
;
2732 struct route_entry
*re
;
2733 struct route_entry
*fib
= NULL
;
2734 struct route_entry
*same
= NULL
;
2735 struct nexthop
*rtnh
;
2736 char buf2
[INET6_ADDRSTRLEN
];
2739 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2742 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2749 apply_mask_ipv6(src_p
);
2751 /* Lookup route node. */
2752 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2754 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2756 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2757 if (src_p
&& src_p
->prefixlen
)
2758 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2762 if (IS_ZEBRA_DEBUG_RIB
)
2763 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2765 (src_buf
[0] != '\0') ? " from " : "",
2770 dest
= rib_dest_from_rnode(rn
);
2771 fib
= dest
->selected_fib
;
2773 /* Lookup same type route. */
2774 RNODE_FOREACH_RE (rn
, re
) {
2775 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2778 if (re
->type
!= type
)
2780 if (re
->instance
!= instance
)
2782 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2783 distance
!= re
->distance
)
2786 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2788 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2789 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2790 if (rtnh
->ifindex
!= nh
->ifindex
)
2795 /* Make sure that the route found has the same gateway. */
2801 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2802 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2810 /* If same type of route can't be found and this message is from
2814 * In the past(HA!) we could get here because
2815 * we were receiving a route delete from the
2816 * kernel and we're not marking the proto
2817 * as coming from it's appropriate originator.
2818 * Now that we are properly noticing the fact
2819 * that the kernel has deleted our route we
2820 * are not going to get called in this path
2821 * I am going to leave this here because
2822 * this might still work this way on non-linux
2823 * platforms as well as some weird state I have
2824 * not properly thought of yet.
2825 * If we can show that this code path is
2826 * dead then we can remove it.
2828 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2829 if (IS_ZEBRA_DEBUG_RIB
) {
2832 "rn %p, re %p (type %d) was deleted from kernel, adding",
2833 rn
, fib
, fib
->type
);
2837 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2839 UNSET_FLAG(rtnh
->flags
,
2843 * This is a non FRR route
2844 * as such we should mark
2847 dest
->selected_fib
= NULL
;
2849 /* This means someone else, other than Zebra,
2851 * a Zebra router from the kernel. We will add
2853 rib_install_kernel(rn
, fib
, NULL
);
2856 if (IS_ZEBRA_DEBUG_RIB
) {
2860 "via %s ifindex %d type %d "
2861 "doesn't exist in rib",
2862 inet_ntop(afi2family(afi
),
2869 "type %d doesn't exist in rib",
2872 route_unlock_node(rn
);
2878 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2880 rib_install_kernel(rn
, same
, NULL
);
2881 route_unlock_node(rn
);
2886 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2887 struct nexthop
*tmp_nh
;
2889 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2890 struct ipaddr vtep_ip
;
2892 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2893 if (afi
== AFI_IP
) {
2894 vtep_ip
.ipa_type
= IPADDR_V4
;
2895 memcpy(&(vtep_ip
.ipaddr_v4
),
2896 &(tmp_nh
->gate
.ipv4
),
2897 sizeof(struct in_addr
));
2899 vtep_ip
.ipa_type
= IPADDR_V6
;
2900 memcpy(&(vtep_ip
.ipaddr_v6
),
2901 &(tmp_nh
->gate
.ipv6
),
2902 sizeof(struct in6_addr
));
2904 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2908 rib_delnode(rn
, same
);
2911 route_unlock_node(rn
);
2916 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2917 unsigned short instance
, int flags
, struct prefix
*p
,
2918 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2919 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2922 struct route_entry
*re
;
2923 struct nexthop
*nexthop
;
2925 /* Allocate new route_entry structure. */
2926 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2928 re
->instance
= instance
;
2929 re
->distance
= distance
;
2931 re
->metric
= metric
;
2933 re
->table
= table_id
;
2934 re
->vrf_id
= vrf_id
;
2935 re
->nexthop_num
= 0;
2936 re
->uptime
= time(NULL
);
2940 nexthop
= nexthop_new();
2942 route_entry_nexthop_add(re
, nexthop
);
2944 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2947 /* Schedule routes of a particular table (address-family) based on event. */
2948 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2950 struct route_node
*rn
;
2951 struct route_entry
*re
, *next
;
2953 /* Walk all routes and queue for processing, if appropriate for
2954 * the trigger event.
2956 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2958 * If we are looking at a route node and the node
2959 * has already been queued we don't
2960 * need to queue it up again
2962 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2963 RIB_ROUTE_ANY_QUEUED
))
2966 case RIB_UPDATE_IF_CHANGE
:
2967 /* Examine all routes that won't get processed by the
2969 * triggered by nexthop evaluation (NHT). This would be
2971 * kernel and certain static routes. Note that NHT will
2973 * triggered upon an interface event as connected routes
2975 * get queued for processing.
2977 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2980 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
2981 && re
->type
!= ZEBRA_ROUTE_KERNEL
2982 && re
->type
!= ZEBRA_ROUTE_CONNECT
2983 && re
->type
!= ZEBRA_ROUTE_STATIC
)
2986 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2991 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
2992 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2993 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2996 /* If we only have nexthops to a
3005 case RIB_UPDATE_RMAP_CHANGE
:
3006 case RIB_UPDATE_OTHER
:
3007 /* Right now, examine all routes. Can restrict to a
3009 * some cases (TODO).
3011 if (rnode_to_ribs(rn
))
3021 /* RIB update function. */
3022 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3024 struct route_table
*table
;
3026 /* Process routes of interested address-families. */
3027 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3029 if (IS_ZEBRA_DEBUG_EVENT
)
3030 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3031 rib_update_table(table
, event
);
3034 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3036 if (IS_ZEBRA_DEBUG_EVENT
)
3037 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3038 rib_update_table(table
, event
);
3042 /* Delete self installed routes after zebra is relaunched. */
3043 void rib_sweep_table(struct route_table
*table
)
3045 struct route_node
*rn
;
3046 struct route_entry
*re
;
3047 struct route_entry
*next
;
3048 struct nexthop
*nexthop
;
3053 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3054 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3055 if (IS_ZEBRA_DEBUG_RIB
)
3056 route_entry_dump(&rn
->p
, NULL
, re
);
3058 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3061 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3065 * So we are starting up and have received
3066 * routes from the kernel that we have installed
3067 * from a previous run of zebra but not cleaned
3068 * up ( say a kill -9 )
3069 * But since we haven't actually installed
3070 * them yet( we received them from the kernel )
3071 * we don't think they are active.
3072 * So let's pretend they are active to actually
3074 * In all honesty I'm not sure if we should
3075 * mark them as active when we receive them
3076 * This is startup only so probably ok.
3078 * If we ever decide to move rib_sweep_table
3079 * to a different spot (ie startup )
3080 * this decision needs to be revisited
3082 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3083 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3085 rib_uninstall_kernel(rn
, re
);
3086 rib_delnode(rn
, re
);
3091 /* Sweep all RIB tables. */
3092 void rib_sweep_route(void)
3095 struct zebra_vrf
*zvrf
;
3097 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3098 if ((zvrf
= vrf
->info
) == NULL
)
3101 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3102 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3105 zebra_router_sweep_route();
3108 /* Remove specific by protocol routes from 'table'. */
3109 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3110 struct route_table
*table
)
3112 struct route_node
*rn
;
3113 struct route_entry
*re
;
3114 struct route_entry
*next
;
3115 unsigned long n
= 0;
3118 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3119 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3120 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3122 if (re
->type
== proto
3123 && re
->instance
== instance
) {
3124 rib_delnode(rn
, re
);
3131 /* Remove specific by protocol routes. */
3132 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3135 struct zebra_vrf
*zvrf
;
3136 unsigned long cnt
= 0;
3138 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3139 if ((zvrf
= vrf
->info
) != NULL
)
3140 cnt
+= rib_score_proto_table(
3142 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3143 + rib_score_proto_table(
3145 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3147 cnt
+= zebra_router_score_proto(proto
, instance
);
3152 /* Close RIB and clean up kernel routes. */
3153 void rib_close_table(struct route_table
*table
)
3155 struct route_node
*rn
;
3156 rib_table_info_t
*info
;
3162 info
= route_table_get_info(table
);
3164 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3165 dest
= rib_dest_from_rnode(rn
);
3167 if (dest
&& dest
->selected_fib
) {
3168 if (info
->safi
== SAFI_UNICAST
)
3169 hook_call(rib_update
, rn
, NULL
);
3171 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
3172 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3173 dest
->selected_fib
= NULL
;
3182 static int rib_process_dplane_results(struct thread
*thread
)
3184 struct zebra_dplane_ctx
*ctx
;
3187 /* Take lock controlling queue of results */
3188 pthread_mutex_lock(&dplane_mutex
);
3190 /* Dequeue context block */
3191 ctx
= dplane_ctx_dequeue(&rib_dplane_q
);
3193 pthread_mutex_unlock(&dplane_mutex
);
3196 rib_process_after(ctx
);
3202 /* Check for nexthop tracking processing after finishing with results */
3203 do_nht_processing();
3209 * Results are returned from the dataplane subsystem, in the context of
3210 * the dataplane pthread. We enqueue the results here for processing by
3211 * the main thread later.
3213 static int rib_dplane_results(struct zebra_dplane_ctx
*ctx
)
3215 /* Take lock controlling queue of results */
3216 pthread_mutex_lock(&dplane_mutex
);
3218 /* Enqueue context block */
3219 dplane_ctx_enqueue_tail(&rib_dplane_q
, ctx
);
3221 pthread_mutex_unlock(&dplane_mutex
);
3223 /* Ensure event is signalled to zebra main thread */
3224 thread_add_event(zebrad
.master
, rib_process_dplane_results
, NULL
, 0,
3230 /* Routing information base initialize. */
3233 rib_queue_init(&zebrad
);
3235 /* Init dataplane, and register for results */
3236 pthread_mutex_init(&dplane_mutex
, NULL
);
3237 TAILQ_INIT(&rib_dplane_q
);
3238 zebra_dplane_init();
3239 dplane_results_register(rib_dplane_results
);
3245 * Get the first vrf id that is greater than the given vrf id if any.
3247 * Returns TRUE if a vrf id was found, FALSE otherwise.
3249 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3253 vrf
= vrf_lookup_by_id(vrf_id
);
3255 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3257 *next_id_p
= vrf
->vrf_id
;
3266 * rib_tables_iter_next
3268 * Returns the next table in the iteration.
3270 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3272 struct route_table
*table
;
3275 * Array that helps us go over all AFI/SAFI combinations via one
3282 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3283 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3284 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3289 switch (iter
->state
) {
3291 case RIB_TABLES_ITER_S_INIT
:
3292 iter
->vrf_id
= VRF_DEFAULT
;
3293 iter
->afi_safi_ix
= -1;
3297 case RIB_TABLES_ITER_S_ITERATING
:
3298 iter
->afi_safi_ix
++;
3301 while (iter
->afi_safi_ix
3302 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3303 table
= zebra_vrf_table(
3304 afi_safis
[iter
->afi_safi_ix
].afi
,
3305 afi_safis
[iter
->afi_safi_ix
].safi
,
3310 iter
->afi_safi_ix
++;
3314 * Found another table in this vrf.
3320 * Done with all tables in the current vrf, go to the
3324 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3327 iter
->afi_safi_ix
= 0;
3332 case RIB_TABLES_ITER_S_DONE
:
3337 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3339 iter
->state
= RIB_TABLES_ITER_S_DONE
;