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. */
77 } route_info
[ZEBRA_ROUTE_MAX
] = {
78 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
79 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
80 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
81 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
82 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
83 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
84 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
85 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
86 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
87 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
88 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
89 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
90 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
91 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
92 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
93 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
94 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
95 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
97 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
99 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
100 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
101 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
103 /* no entry/default: 150 */
106 /* RPF lookup behaviour */
107 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
110 static void __attribute__((format(printf
, 5, 6)))
111 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
112 int priority
, const char *msgfmt
, ...)
114 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
118 va_start(ap
, msgfmt
);
119 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
123 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
124 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
126 if (info
->safi
== SAFI_MULTICAST
)
127 strcat(buf
, " (MRIB)");
129 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
132 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
135 #define rnode_debug(node, vrf_id, ...) \
136 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
137 #define rnode_info(node, ...) \
138 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
140 uint8_t route_distance(int type
)
144 if ((unsigned)type
>= array_size(route_info
))
147 distance
= route_info
[type
].distance
;
152 int is_zebra_valid_kernel_table(uint32_t table_id
)
155 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
156 || (table_id
== RT_TABLE_COMPAT
))
163 int is_zebra_main_routing_table(uint32_t table_id
)
165 if ((table_id
== RT_TABLE_MAIN
)
166 || (table_id
== zebrad
.rtm_table_default
))
171 int zebra_check_addr(const struct prefix
*p
)
173 if (p
->family
== AF_INET
) {
176 addr
= p
->u
.prefix4
.s_addr
;
179 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
180 || IPV4_LINKLOCAL(addr
))
183 if (p
->family
== AF_INET6
) {
184 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
186 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
192 /* Add nexthop to the end of a rib node's nexthop list */
193 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
195 nexthop_add(&re
->ng
.nexthop
, nexthop
);
201 * copy_nexthop - copy a nexthop to the rib structure.
203 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
205 assert(!re
->ng
.nexthop
);
206 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
207 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
211 /* Delete specified nexthop from the list. */
212 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
215 nexthop
->next
->prev
= nexthop
->prev
;
217 nexthop
->prev
->next
= nexthop
->next
;
219 re
->ng
.nexthop
= nexthop
->next
;
224 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
228 struct nexthop
*nexthop
;
230 nexthop
= nexthop_new();
231 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
232 nexthop
->ifindex
= ifindex
;
233 nexthop
->vrf_id
= nh_vrf_id
;
235 route_entry_nexthop_add(re
, nexthop
);
240 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
241 struct in_addr
*ipv4
,
245 struct nexthop
*nexthop
;
247 nexthop
= nexthop_new();
248 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
249 nexthop
->vrf_id
= nh_vrf_id
;
250 nexthop
->gate
.ipv4
= *ipv4
;
252 nexthop
->src
.ipv4
= *src
;
254 route_entry_nexthop_add(re
, nexthop
);
259 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
260 struct in_addr
*ipv4
,
265 struct nexthop
*nexthop
;
266 struct interface
*ifp
;
268 nexthop
= nexthop_new();
269 nexthop
->vrf_id
= nh_vrf_id
;
270 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
271 nexthop
->gate
.ipv4
= *ipv4
;
273 nexthop
->src
.ipv4
= *src
;
274 nexthop
->ifindex
= ifindex
;
275 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
276 /*Pending: need to think if null ifp here is ok during bootup?
277 There was a crash because ifp here was coming to be NULL */
279 if (connected_is_unnumbered(ifp
)
280 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
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 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
320 route_entry_nexthop_add(re
, nexthop
);
325 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
326 enum blackhole_type bh_type
)
328 struct nexthop
*nexthop
;
330 nexthop
= nexthop_new();
331 nexthop
->vrf_id
= VRF_DEFAULT
;
332 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
333 nexthop
->bh_type
= bh_type
;
335 route_entry_nexthop_add(re
, nexthop
);
340 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
341 struct nexthop
*nexthop
)
343 struct nexthop
*resolved_hop
;
345 resolved_hop
= nexthop_new();
346 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
348 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
349 switch (newhop
->type
) {
350 case NEXTHOP_TYPE_IPV4
:
351 case NEXTHOP_TYPE_IPV4_IFINDEX
:
352 /* If the resolving route specifies a gateway, use it */
353 resolved_hop
->type
= newhop
->type
;
354 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
356 if (newhop
->ifindex
) {
357 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
358 resolved_hop
->ifindex
= newhop
->ifindex
;
361 case NEXTHOP_TYPE_IPV6
:
362 case NEXTHOP_TYPE_IPV6_IFINDEX
:
363 resolved_hop
->type
= newhop
->type
;
364 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
366 if (newhop
->ifindex
) {
367 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
368 resolved_hop
->ifindex
= newhop
->ifindex
;
371 case NEXTHOP_TYPE_IFINDEX
:
372 /* If the resolving route is an interface route,
373 * it means the gateway we are looking up is connected
374 * to that interface. (The actual network is _not_ onlink).
375 * Therefore, the resolved route should have the original
376 * gateway as nexthop as it is directly connected.
378 * On Linux, we have to set the onlink netlink flag because
379 * otherwise, the kernel won't accept the route.
381 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
383 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
384 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
385 } else if (afi
== AFI_IP6
) {
386 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
387 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
389 resolved_hop
->ifindex
= newhop
->ifindex
;
391 case NEXTHOP_TYPE_BLACKHOLE
:
392 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
393 resolved_hop
->bh_type
= nexthop
->bh_type
;
397 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
398 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
400 /* Copy labels of the resolved route */
401 if (newhop
->nh_label
)
402 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
403 newhop
->nh_label
->num_labels
,
404 &newhop
->nh_label
->label
[0]);
406 resolved_hop
->rparent
= nexthop
;
407 nexthop_add(&nexthop
->resolved
, resolved_hop
);
410 /* If force flag is not set, do not modify falgs at all for uninstall
411 the route from FIB. */
412 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
413 struct nexthop
*nexthop
, bool set
,
414 struct route_node
*top
)
417 struct route_table
*table
;
418 struct route_node
*rn
;
419 struct route_entry
*match
= NULL
;
421 struct nexthop
*newhop
;
422 struct interface
*ifp
;
425 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
426 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
427 nexthop
->ifindex
= 0;
430 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
431 nexthops_free(nexthop
->resolved
);
432 nexthop
->resolved
= NULL
;
436 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
437 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
440 /* Skip nexthops that have been filtered out due to route-map */
441 /* The nexthops are specific to this route and so the same */
442 /* nexthop for a different route may not have this flag set */
443 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
)) {
444 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
445 zlog_debug("\t%s: Nexthop Filtered",
446 __PRETTY_FUNCTION__
);
451 * Check to see if we should trust the passed in information
452 * for UNNUMBERED interfaces as that we won't find the GW
453 * address in the routing table.
455 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
456 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
458 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
460 "\t%s: Onlink and interface: %u[%u] does not exist",
461 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
465 if (connected_is_unnumbered(ifp
)) {
466 if (if_is_operative(ifp
))
469 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
471 "\t%s: Onlink and interface %s is not operative",
472 __PRETTY_FUNCTION__
, ifp
->name
);
476 if (!if_is_operative(ifp
)) {
477 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
479 "\t%s: Interface %s is not unnumbered",
481 ifp
? ifp
->name
: "Unknown");
486 /* Make lookup prefix. */
487 memset(&p
, 0, sizeof(struct prefix
));
491 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
492 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
496 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
497 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
500 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
504 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
506 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
507 zlog_debug("\t%s: Table not found",
508 __PRETTY_FUNCTION__
);
512 rn
= route_node_match(table
, (struct prefix
*)&p
);
514 route_unlock_node(rn
);
516 /* Lookup should halt if we've matched against ourselves ('top',
517 * if specified) - i.e., we cannot have a nexthop NH1 is
518 * resolved by a route NH1. The exception is if the route is a
521 if (top
&& rn
== top
)
522 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
523 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
524 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
526 "\t%s: Matched against ourself and prefix length is not max bit length",
527 __PRETTY_FUNCTION__
);
531 /* Pick up selected route. */
532 /* However, do not resolve over default route unless explicitly
534 if (is_default_prefix(&rn
->p
)
535 && !rnh_resolve_via_default(p
.family
)) {
536 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
538 "\t:%s: Resolved against default route",
539 __PRETTY_FUNCTION__
);
543 dest
= rib_dest_from_rnode(rn
);
544 if (dest
&& dest
->selected_fib
545 && !CHECK_FLAG(dest
->selected_fib
->status
,
547 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
548 match
= dest
->selected_fib
;
550 /* If there is no selected route or matched route is EGP, go up
555 } while (rn
&& rn
->info
== NULL
);
562 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
563 /* Directly point connected route. */
564 newhop
= match
->ng
.nexthop
;
566 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
567 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
568 nexthop
->ifindex
= newhop
->ifindex
;
571 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
573 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
574 if (!CHECK_FLAG(newhop
->flags
,
577 if (CHECK_FLAG(newhop
->flags
,
578 NEXTHOP_FLAG_RECURSIVE
))
582 SET_FLAG(nexthop
->flags
,
583 NEXTHOP_FLAG_RECURSIVE
);
585 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
586 nexthop_set_resolved(afi
, newhop
,
592 re
->nexthop_mtu
= match
->mtu
;
593 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
594 zlog_debug("\t%s: Recursion failed to find",
595 __PRETTY_FUNCTION__
);
597 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
599 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
600 if (!CHECK_FLAG(newhop
->flags
,
605 SET_FLAG(nexthop
->flags
,
606 NEXTHOP_FLAG_RECURSIVE
);
607 nexthop_set_resolved(afi
, newhop
,
613 re
->nexthop_mtu
= match
->mtu
;
615 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
617 "\t%s: Static route unable to resolve",
618 __PRETTY_FUNCTION__
);
621 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
622 zlog_debug("\t%s: Route Type %s has not turned on recursion",
624 zebra_route_string(re
->type
));
625 if (re
->type
== ZEBRA_ROUTE_BGP
&&
626 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
627 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
632 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
633 zlog_debug("\t%s: Nexthop did not lookup in table",
634 __PRETTY_FUNCTION__
);
638 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
639 union g_addr
*addr
, struct route_node
**rn_out
)
642 struct route_table
*table
;
643 struct route_node
*rn
;
644 struct route_entry
*match
= NULL
;
645 struct nexthop
*newhop
;
648 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
652 memset(&p
, 0, sizeof(struct prefix
));
655 p
.u
.prefix4
= addr
->ipv4
;
656 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
658 p
.u
.prefix6
= addr
->ipv6
;
659 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
662 rn
= route_node_match(table
, (struct prefix
*)&p
);
667 route_unlock_node(rn
);
669 dest
= rib_dest_from_rnode(rn
);
670 if (dest
&& dest
->selected_fib
671 && !CHECK_FLAG(dest
->selected_fib
->status
,
672 ROUTE_ENTRY_REMOVED
))
673 match
= dest
->selected_fib
;
675 /* If there is no selected route or matched route is EGP, go up
680 } while (rn
&& rn
->info
== NULL
);
684 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
686 for (ALL_NEXTHOPS(match
->ng
, newhop
))
687 if (CHECK_FLAG(newhop
->flags
,
704 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
706 struct route_node
**rn_out
)
708 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
709 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
710 union g_addr gaddr
= {.ipv4
= addr
};
712 switch (ipv4_multicast_mode
) {
713 case MCAST_MRIB_ONLY
:
714 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
716 case MCAST_URIB_ONLY
:
717 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
718 case MCAST_NO_CONFIG
:
719 case MCAST_MIX_MRIB_FIRST
:
720 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
723 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
726 case MCAST_MIX_DISTANCE
:
727 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
728 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
730 re
= ure
->distance
< mre
->distance
? ure
: mre
;
736 case MCAST_MIX_PFXLEN
:
737 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
738 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
740 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
749 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
751 if (IS_ZEBRA_DEBUG_RIB
) {
753 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
755 zlog_debug("%s: %s: vrf: %u found %s, using %s",
756 __func__
, buf
, vrf_id
,
757 mre
? (ure
? "MRIB+URIB" : "MRIB")
758 : ure
? "URIB" : "nothing",
759 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
764 void multicast_mode_ipv4_set(enum multicast_mode mode
)
766 if (IS_ZEBRA_DEBUG_RIB
)
767 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
769 ipv4_multicast_mode
= mode
;
772 enum multicast_mode
multicast_mode_ipv4_get(void)
774 return ipv4_multicast_mode
;
777 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
779 struct route_table
*table
;
780 struct route_node
*rn
;
781 struct route_entry
*match
= NULL
;
782 struct nexthop
*nexthop
;
786 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
790 rn
= route_node_lookup(table
, (struct prefix
*)p
);
792 /* No route for this prefix. */
797 route_unlock_node(rn
);
798 dest
= rib_dest_from_rnode(rn
);
800 if (dest
&& dest
->selected_fib
801 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
802 match
= dest
->selected_fib
;
807 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
810 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
811 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
817 #define RIB_SYSTEM_ROUTE(R) \
818 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
820 #define RIB_KERNEL_ROUTE(R) \
821 ((R)->type == ZEBRA_ROUTE_KERNEL)
823 /* This function verifies reachability of one given nexthop, which can be
824 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
825 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
826 * nexthop->ifindex will be updated appropriately as well.
827 * An existing route map can turn (otherwise active) nexthop into inactive, but
830 * The return value is the final value of 'ACTIVE' flag.
833 static unsigned nexthop_active_check(struct route_node
*rn
,
834 struct route_entry
*re
,
835 struct nexthop
*nexthop
, bool set
)
837 struct interface
*ifp
;
838 route_map_result_t ret
= RMAP_MATCH
;
840 char buf
[SRCDEST2STR_BUFFER
];
841 const struct prefix
*p
, *src_p
;
842 struct zebra_vrf
*zvrf
;
844 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
846 if (rn
->p
.family
== AF_INET
)
848 else if (rn
->p
.family
== AF_INET6
)
852 switch (nexthop
->type
) {
853 case NEXTHOP_TYPE_IFINDEX
:
854 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
855 if (ifp
&& if_is_operative(ifp
))
856 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
858 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
860 case NEXTHOP_TYPE_IPV4
:
861 case NEXTHOP_TYPE_IPV4_IFINDEX
:
863 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
864 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
866 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
868 case NEXTHOP_TYPE_IPV6
:
870 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
871 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
873 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
875 case NEXTHOP_TYPE_IPV6_IFINDEX
:
876 /* RFC 5549, v4 prefix with v6 NH */
877 if (rn
->p
.family
!= AF_INET
)
879 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
880 ifp
= if_lookup_by_index(nexthop
->ifindex
,
882 if (ifp
&& if_is_operative(ifp
))
883 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
885 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
887 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
888 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
890 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
893 case NEXTHOP_TYPE_BLACKHOLE
:
894 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
899 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
900 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
901 zlog_debug("\t%s: Unable to find a active nexthop",
902 __PRETTY_FUNCTION__
);
906 /* XXX: What exactly do those checks do? Do we support
907 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
909 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
910 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
911 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
913 /* The original code didn't determine the family correctly
914 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
915 * from the rib_table_info in those cases.
916 * Possibly it may be better to use only the rib_table_info
920 rib_table_info_t
*info
;
922 info
= srcdest_rnode_table_info(rn
);
926 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
928 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
930 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
931 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
932 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
935 /* It'll get set if required inside */
936 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
937 nexthop
, zvrf
, re
->tag
);
938 if (ret
== RMAP_DENYMATCH
) {
939 if (IS_ZEBRA_DEBUG_RIB
) {
940 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
942 "%u:%s: Filtering out with NH out %s due to route map",
944 ifindex2ifname(nexthop
->ifindex
,
947 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
949 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
952 /* Iterate over all nexthops of the given RIB entry and refresh their
953 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
954 * nexthop is found to toggle the ACTIVE flag, the whole re structure
955 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
956 * transparently passed to nexthop_active_check().
958 * Return value is the new number of active nexthops.
961 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
964 struct nexthop
*nexthop
;
965 union g_addr prev_src
;
966 unsigned int prev_active
, new_active
, old_num_nh
;
967 ifindex_t prev_index
;
969 old_num_nh
= re
->nexthop_active_num
;
971 re
->nexthop_active_num
= 0;
972 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
974 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
975 /* No protocol daemon provides src and so we're skipping
977 prev_src
= nexthop
->rmap_src
;
978 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
979 prev_index
= nexthop
->ifindex
;
981 * We need to respect the multipath_num here
982 * as that what we should be able to install from
983 * a multipath perpsective should not be a data plane
986 new_active
= nexthop_active_check(rn
, re
, nexthop
, set
);
987 if (new_active
&& re
->nexthop_active_num
>= multipath_num
) {
988 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
992 re
->nexthop_active_num
++;
993 /* Don't allow src setting on IPv6 addr for now */
994 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
995 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
996 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
997 && prev_src
.ipv4
.s_addr
998 != nexthop
->rmap_src
.ipv4
.s_addr
)
999 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
1000 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
1001 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
1002 &nexthop
->rmap_src
.ipv6
)))) {
1003 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1004 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1008 if (old_num_nh
!= re
->nexthop_active_num
)
1009 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1011 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1012 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1015 return re
->nexthop_active_num
;
1019 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1020 * (nexthops) must have a label.
1022 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1024 struct nexthop
*nexthop
= NULL
;
1026 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1029 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1030 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1036 /* Update flag indicates whether this is a "replace" or not. Currently, this
1037 * is only used for IPv4.
1039 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1040 struct route_entry
*old
)
1042 struct nexthop
*nexthop
;
1043 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1044 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1045 const struct prefix
*p
, *src_p
;
1046 enum zebra_dplane_result ret
;
1048 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1050 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1052 if (info
->safi
!= SAFI_UNICAST
) {
1053 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1054 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1057 struct nexthop
*prev
;
1059 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1060 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1061 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1062 if (prev
== nexthop
)
1064 if (nexthop_same_firsthop(nexthop
, prev
)) {
1065 SET_FLAG(nexthop
->flags
,
1066 NEXTHOP_FLAG_DUPLICATE
);
1074 * If this is a replace to a new RE let the originator of the RE
1075 * know that they've lost
1077 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1078 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1080 /* Update fib selection */
1081 dest
->selected_fib
= re
;
1084 * Make sure we update the FPM any time we send new information to
1087 hook_call(rib_update
, rn
, "installing in kernel");
1089 /* Send add or update */
1090 if (old
&& (old
!= re
))
1091 ret
= dplane_route_update(rn
, re
, old
);
1093 ret
= dplane_route_add(rn
, re
);
1096 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1098 zvrf
->installs_queued
++;
1100 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1102 char str
[SRCDEST2STR_BUFFER
];
1104 srcdest_rnode2str(rn
, str
, sizeof(str
));
1105 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1106 "%u:%s: Failed to enqueue dataplane install",
1110 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1119 /* Uninstall the route from kernel. */
1120 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1122 struct nexthop
*nexthop
;
1123 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1124 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1126 if (info
->safi
!= SAFI_UNICAST
) {
1127 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1128 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1133 * Make sure we update the FPM any time we send new information to
1136 hook_call(rib_update
, rn
, "uninstalling from kernel");
1138 switch (dplane_route_delete(rn
, re
)) {
1139 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1141 zvrf
->removals_queued
++;
1143 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1145 char str
[SRCDEST2STR_BUFFER
];
1147 srcdest_rnode2str(rn
, str
, sizeof(str
));
1148 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1149 "%u:%s: Failed to enqueue dataplane uninstall",
1153 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1162 /* Uninstall the route from kernel. */
1163 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1165 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1166 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1167 struct nexthop
*nexthop
;
1169 if (dest
&& dest
->selected_fib
== re
) {
1170 if (info
->safi
== SAFI_UNICAST
)
1171 hook_call(rib_update
, rn
, "rib_uninstall");
1173 /* If labeled-unicast route, uninstall transit LSP. */
1174 if (zebra_rib_labeled_unicast(re
))
1175 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1177 if (!RIB_SYSTEM_ROUTE(re
))
1178 rib_uninstall_kernel(rn
, re
);
1180 dest
->selected_fib
= NULL
;
1182 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1183 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1186 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1187 const struct prefix
*p
, *src_p
;
1189 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1191 redistribute_delete(p
, src_p
, re
);
1192 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1197 * rib_can_delete_dest
1199 * Returns TRUE if the given dest can be deleted from the table.
1201 static int rib_can_delete_dest(rib_dest_t
*dest
)
1208 * Don't delete the dest if we have to update the FPM about this
1211 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1212 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1221 * Garbage collect the rib dest corresponding to the given route node
1224 * Returns TRUE if the dest was deleted, FALSE otherwise.
1226 int rib_gc_dest(struct route_node
*rn
)
1230 dest
= rib_dest_from_rnode(rn
);
1234 if (!rib_can_delete_dest(dest
))
1237 if (IS_ZEBRA_DEBUG_RIB
) {
1238 struct zebra_vrf
*zvrf
;
1240 zvrf
= rib_dest_vrf(dest
);
1241 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1245 XFREE(MTYPE_RIB_DEST
, dest
);
1249 * Release the one reference that we keep on the route node.
1251 route_unlock_node(rn
);
1255 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1256 struct route_entry
*new)
1258 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1260 hook_call(rib_update
, rn
, "new route selected");
1262 /* Update real nexthop. This may actually determine if nexthop is active
1264 if (!nexthop_active_update(rn
, new, true)) {
1265 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1269 if (IS_ZEBRA_DEBUG_RIB
) {
1270 char buf
[SRCDEST2STR_BUFFER
];
1271 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1272 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1273 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1276 /* If labeled-unicast route, install transit LSP. */
1277 if (zebra_rib_labeled_unicast(new))
1278 zebra_mpls_lsp_install(zvrf
, rn
, new);
1280 if (!RIB_SYSTEM_ROUTE(new))
1281 rib_install_kernel(rn
, new, NULL
);
1283 dest
->selected_fib
= new;
1285 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1288 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1289 struct route_entry
*old
)
1291 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1292 hook_call(rib_update
, rn
, "removing existing route");
1294 /* Uninstall from kernel. */
1295 if (IS_ZEBRA_DEBUG_RIB
) {
1296 char buf
[SRCDEST2STR_BUFFER
];
1297 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1298 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1299 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1302 /* If labeled-unicast route, uninstall transit LSP. */
1303 if (zebra_rib_labeled_unicast(old
))
1304 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1306 if (!RIB_SYSTEM_ROUTE(old
))
1307 rib_uninstall_kernel(rn
, old
);
1310 * We are setting this to NULL here
1311 * because that is what we traditionally
1312 * have been doing. I am not positive
1313 * that this is the right thing to do
1314 * but let's leave the code alone
1315 * for the RIB_SYSTEM_ROUTE case
1317 dest
->selected_fib
= NULL
;
1320 /* Update nexthop for route, reset changed flag. */
1321 /* Note: this code also handles the Linux case when an interface goes
1322 * down, causing the kernel to delete routes without sending DELROUTE
1325 if (!nexthop_active_update(rn
, old
, true) &&
1326 (RIB_KERNEL_ROUTE(old
)))
1327 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1329 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1332 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1333 struct route_node
*rn
,
1334 struct route_entry
*old
,
1335 struct route_entry
*new)
1337 struct nexthop
*nexthop
= NULL
;
1339 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1342 * We have to install or update if a new route has been selected or
1343 * something has changed.
1345 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1346 hook_call(rib_update
, rn
, "updating existing route");
1348 /* Update the nexthop; we could determine here that nexthop is
1350 if (nexthop_active_update(rn
, new, true))
1353 /* If nexthop is active, install the selected route, if
1355 * the install succeeds, cleanup flags for prior route, if
1360 if (IS_ZEBRA_DEBUG_RIB
) {
1361 char buf
[SRCDEST2STR_BUFFER
];
1362 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1365 "%u:%s: Updating route rn %p, re %p (type %d) "
1367 zvrf_id(zvrf
), buf
, rn
, new,
1368 new->type
, old
, old
->type
);
1371 "%u:%s: Updating route rn %p, re %p (type %d)",
1372 zvrf_id(zvrf
), buf
, rn
, new,
1376 /* If labeled-unicast route, uninstall transit LSP. */
1377 if (zebra_rib_labeled_unicast(old
))
1378 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1380 /* Non-system route should be installed. */
1381 if (!RIB_SYSTEM_ROUTE(new)) {
1382 /* If labeled-unicast route, install transit
1384 if (zebra_rib_labeled_unicast(new))
1385 zebra_mpls_lsp_install(zvrf
, rn
, new);
1387 rib_install_kernel(rn
, new, old
);
1390 * We do not need to install the
1391 * selected route because it
1392 * is already isntalled by
1393 * the system( ie not us )
1394 * so just mark it as winning
1395 * we do need to ensure that
1396 * if we uninstall a route
1397 * from ourselves we don't
1398 * over write this pointer
1400 dest
->selected_fib
= NULL
;
1402 /* If install succeeded or system route, cleanup flags
1403 * for prior route. */
1405 if (RIB_SYSTEM_ROUTE(new)) {
1406 if (!RIB_SYSTEM_ROUTE(old
))
1407 rib_uninstall_kernel(rn
, old
);
1409 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1410 nexthop
= nexthop
->next
)
1411 UNSET_FLAG(nexthop
->flags
,
1418 * If nexthop for selected route is not active or install
1420 * may need to uninstall and delete for redistribution.
1423 if (IS_ZEBRA_DEBUG_RIB
) {
1424 char buf
[SRCDEST2STR_BUFFER
];
1425 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1428 "%u:%s: Deleting route rn %p, re %p (type %d) "
1429 "old %p (type %d) - nexthop inactive",
1430 zvrf_id(zvrf
), buf
, rn
, new,
1431 new->type
, old
, old
->type
);
1434 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1435 zvrf_id(zvrf
), buf
, rn
, new,
1439 /* If labeled-unicast route, uninstall transit LSP. */
1440 if (zebra_rib_labeled_unicast(old
))
1441 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1443 if (!RIB_SYSTEM_ROUTE(old
))
1444 rib_uninstall_kernel(rn
, old
);
1446 dest
->selected_fib
= NULL
;
1450 * Same route selected; check if in the FIB and if not,
1452 * is housekeeping code to deal with race conditions in kernel
1454 * netlink reporting interface up before IPv4 or IPv6 protocol
1458 if (!RIB_SYSTEM_ROUTE(new)) {
1459 bool in_fib
= false;
1461 for (ALL_NEXTHOPS(new->ng
, nexthop
))
1462 if (CHECK_FLAG(nexthop
->flags
,
1463 NEXTHOP_FLAG_FIB
)) {
1468 rib_install_kernel(rn
, new, NULL
);
1472 /* Update prior route. */
1474 /* Set real nexthop. */
1475 nexthop_active_update(rn
, old
, true);
1476 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1479 /* Clear changed flag. */
1480 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1483 /* Check if 'alternate' RIB entry is better than 'current'. */
1484 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1485 struct route_entry
*alternate
)
1487 if (current
== NULL
)
1490 /* filter route selection in following order:
1491 * - connected beats other types
1492 * - if both connected, loopback or vrf wins
1493 * - lower distance beats higher
1494 * - lower metric beats higher for equal distance
1495 * - last, hence oldest, route wins tie break.
1498 /* Connected routes. Check to see if either are a vrf
1499 * or loopback interface. If not, pick the last connected
1500 * route of the set of lowest metric connected routes.
1502 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1503 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1506 /* both are connected. are either loop or vrf? */
1507 struct nexthop
*nexthop
= NULL
;
1509 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1510 if (if_is_loopback_or_vrf(if_lookup_by_index(
1511 nexthop
->ifindex
, alternate
->vrf_id
)))
1515 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1516 if (if_is_loopback_or_vrf(if_lookup_by_index(
1517 nexthop
->ifindex
, current
->vrf_id
)))
1521 /* Neither are loop or vrf so pick best metric */
1522 if (alternate
->metric
<= current
->metric
)
1528 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1531 /* higher distance loses */
1532 if (alternate
->distance
< current
->distance
)
1534 if (current
->distance
< alternate
->distance
)
1537 /* metric tie-breaks equal distance */
1538 if (alternate
->metric
<= current
->metric
)
1544 /* Core function for processing routing information base. */
1545 static void rib_process(struct route_node
*rn
)
1547 struct route_entry
*re
;
1548 struct route_entry
*next
;
1549 struct route_entry
*old_selected
= NULL
;
1550 struct route_entry
*new_selected
= NULL
;
1551 struct route_entry
*old_fib
= NULL
;
1552 struct route_entry
*new_fib
= NULL
;
1553 struct route_entry
*best
= NULL
;
1554 char buf
[SRCDEST2STR_BUFFER
];
1556 struct zebra_vrf
*zvrf
= NULL
;
1557 const struct prefix
*p
, *src_p
;
1559 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1560 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1564 dest
= rib_dest_from_rnode(rn
);
1566 zvrf
= rib_dest_vrf(dest
);
1567 vrf_id
= zvrf_id(zvrf
);
1570 if (IS_ZEBRA_DEBUG_RIB
)
1571 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1573 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1574 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1577 * we can have rn's that have a NULL info pointer
1578 * (dest). As such let's not let the deref happen
1579 * additionally we know RNODE_FOREACH_RE_SAFE
1580 * will not iterate so we are ok.
1583 old_fib
= dest
->selected_fib
;
1585 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1586 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1588 "%u:%s: Examine re %p (type %d) status %x flags %x "
1589 "dist %d metric %d",
1590 vrf_id
, buf
, re
, re
->type
, re
->status
,
1591 re
->flags
, re
->distance
, re
->metric
);
1593 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1595 /* Currently selected re. */
1596 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1597 assert(old_selected
== NULL
);
1601 /* Skip deleted entries from selection */
1602 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1605 /* Skip unreachable nexthop. */
1606 /* This first call to nexthop_active_update is merely to
1608 * there's any change to nexthops associated with this RIB
1610 * rib_process() can be invoked due to an external event such as
1612 * down or due to next-hop-tracking evaluation. In the latter
1614 * a decision has already been made that the NHs have changed.
1616 * need to invoke a potentially expensive call again. Further,
1618 * the change might be in a recursive NH which is not caught in
1619 * the nexthop_active_update() code. Thus, we might miss changes
1623 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1624 && !nexthop_active_update(rn
, re
, false)) {
1625 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1626 /* XXX: HERE BE DRAGONS!!!!!
1627 * In all honesty, I have not yet figured out
1629 * does or why the ROUTE_ENTRY_CHANGED test
1631 * or why we need to delete a route here, and
1633 * this concerns both selected and fib route, or
1636 /* This entry was denied by the 'ip protocol
1637 * table' route-map, we
1638 * need to delete it */
1639 if (re
!= old_selected
) {
1640 if (IS_ZEBRA_DEBUG_RIB
)
1642 "%s: %u:%s: imported via import-table but denied "
1643 "by the ip protocol table route-map",
1644 __func__
, vrf_id
, buf
);
1647 SET_FLAG(re
->status
,
1648 ROUTE_ENTRY_REMOVED
);
1654 /* Infinite distance. */
1655 if (re
->distance
== DISTANCE_INFINITY
) {
1656 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1660 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1661 best
= rib_choose_best(new_fib
, re
);
1662 if (new_fib
&& best
!= new_fib
)
1663 UNSET_FLAG(new_fib
->status
,
1664 ROUTE_ENTRY_CHANGED
);
1667 best
= rib_choose_best(new_selected
, re
);
1668 if (new_selected
&& best
!= new_selected
)
1669 UNSET_FLAG(new_selected
->status
,
1670 ROUTE_ENTRY_CHANGED
);
1671 new_selected
= best
;
1674 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1675 } /* RNODE_FOREACH_RE */
1677 /* If no FIB override route, use the selected route also for FIB */
1678 if (new_fib
== NULL
)
1679 new_fib
= new_selected
;
1681 /* After the cycle is finished, the following pointers will be set:
1682 * old_selected --- RE entry currently having SELECTED
1683 * new_selected --- RE entry that is newly SELECTED
1684 * old_fib --- RE entry currently in kernel FIB
1685 * new_fib --- RE entry that is newly to be in kernel FIB
1687 * new_selected will get SELECTED flag, and is going to be redistributed
1688 * the zclients. new_fib (which can be new_selected) will be installed
1692 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1694 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1695 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1696 (void *)old_fib
, (void *)new_fib
);
1699 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1700 * fib == selected */
1701 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1702 ROUTE_ENTRY_CHANGED
);
1704 /* Update fib according to selection results */
1705 if (new_fib
&& old_fib
)
1706 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1708 rib_process_add_fib(zvrf
, rn
, new_fib
);
1710 rib_process_del_fib(zvrf
, rn
, old_fib
);
1712 /* Update SELECTED entry */
1713 if (old_selected
!= new_selected
|| selected_changed
) {
1715 if (new_selected
&& new_selected
!= new_fib
) {
1716 nexthop_active_update(rn
, new_selected
, true);
1717 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1721 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1723 /* Special case: new route is system route, so
1724 * dataplane update will not be done - ensure we
1725 * redistribute the route.
1727 if (RIB_SYSTEM_ROUTE(new_selected
))
1728 redistribute_update(p
, src_p
, new_selected
,
1734 redistribute_delete(p
, src_p
, old_selected
);
1735 if (old_selected
!= new_selected
)
1736 UNSET_FLAG(old_selected
->flags
,
1737 ZEBRA_FLAG_SELECTED
);
1741 /* Remove all RE entries queued for removal */
1742 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1743 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1744 if (IS_ZEBRA_DEBUG_RIB
) {
1745 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1746 (void *)rn
, (void *)re
);
1753 * Check if the dest can be deleted now.
1759 * Utility to match route with dplane context data
1761 static bool rib_route_match_ctx(const struct route_entry
*re
,
1762 const struct zebra_dplane_ctx
*ctx
,
1765 bool result
= false;
1769 * In 'update' case, we test info about the 'previous' or
1772 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1773 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1776 /* TODO -- we're using this extra test, but it's not
1777 * exactly clear why.
1779 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1780 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1781 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1788 * Ordinary, single-route case using primary context info
1790 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1791 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1792 /* Skip route that's been deleted */
1796 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1797 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1800 /* TODO -- we're using this extra test, but it's not
1801 * exactly clear why.
1803 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1804 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1805 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1817 * Route-update results processing after async dataplane update.
1819 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1821 struct route_table
*table
= NULL
;
1822 struct zebra_vrf
*zvrf
= NULL
;
1823 struct route_node
*rn
= NULL
;
1824 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1825 bool is_update
= false;
1826 struct nexthop
*nexthop
, *ctx_nexthop
;
1827 char dest_str
[PREFIX_STRLEN
] = "";
1828 enum dplane_op_e op
;
1829 enum zebra_dplane_result status
;
1830 const struct prefix
*dest_pfx
, *src_pfx
;
1832 /* Locate rn and re(s) from ctx */
1834 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1835 dplane_ctx_get_safi(ctx
),
1836 dplane_ctx_get_vrf(ctx
),
1837 dplane_ctx_get_table(ctx
));
1838 if (table
== NULL
) {
1839 if (IS_ZEBRA_DEBUG_DPLANE
) {
1840 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1841 dplane_ctx_get_afi(ctx
),
1842 dplane_ctx_get_safi(ctx
),
1843 dplane_ctx_get_vrf(ctx
));
1848 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1850 dest_pfx
= dplane_ctx_get_dest(ctx
);
1852 /* Note well: only capturing the prefix string if debug is enabled here;
1853 * unconditional log messages will have to generate the string.
1855 if (IS_ZEBRA_DEBUG_DPLANE
)
1856 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1858 src_pfx
= dplane_ctx_get_src(ctx
);
1859 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1860 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1862 if (IS_ZEBRA_DEBUG_DPLANE
) {
1863 zlog_debug("Failed to process dplane results: no route for %u:%s",
1864 dplane_ctx_get_vrf(ctx
), dest_str
);
1869 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1871 op
= dplane_ctx_get_op(ctx
);
1872 status
= dplane_ctx_get_status(ctx
);
1874 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1875 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1876 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1877 dplane_op2str(op
), dplane_res2str(status
));
1880 * Update is a bit of a special case, where we may have both old and new
1881 * routes to post-process.
1883 is_update
= dplane_ctx_is_update(ctx
);
1886 * Take a pass through the routes, look for matches with the context
1889 RNODE_FOREACH_RE(rn
, rib
) {
1892 if (rib_route_match_ctx(rib
, ctx
, false))
1896 /* Check for old route match */
1897 if (is_update
&& (old_re
== NULL
)) {
1898 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1902 /* Have we found the routes we need to work on? */
1903 if (re
&& ((!is_update
|| old_re
)))
1908 * Check sequence number(s) to detect stale results before continuing
1910 if (re
&& (re
->dplane_sequence
!= dplane_ctx_get_seq(ctx
))) {
1911 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1912 zlog_debug("%u:%s Stale dplane result for re %p",
1913 dplane_ctx_get_vrf(ctx
), dest_str
, re
);
1919 (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
))) {
1920 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1921 zlog_debug("%u:%s Stale dplane result for old_re %p",
1922 dplane_ctx_get_vrf(ctx
), dest_str
, old_re
);
1928 * Here's sort of a tough one: the route update result is stale.
1929 * Is it better to use the context block info to generate
1930 * redist and owner notification, or is it better to wait
1931 * for the up-to-date result to arrive?
1934 /* TODO -- for now, only expose up-to-date results */
1939 case DPLANE_OP_ROUTE_INSTALL
:
1940 case DPLANE_OP_ROUTE_UPDATE
:
1941 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1942 /* Update zebra nexthop FIB flag for each
1943 * nexthop that was installed.
1945 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1948 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1949 if (nexthop_same(ctx_nexthop
, nexthop
))
1953 if (nexthop
== NULL
)
1956 if (CHECK_FLAG(nexthop
->flags
,
1957 NEXTHOP_FLAG_RECURSIVE
))
1960 if (CHECK_FLAG(ctx_nexthop
->flags
,
1962 SET_FLAG(nexthop
->flags
,
1965 UNSET_FLAG(nexthop
->flags
,
1971 /* Set flag for nexthop tracking processing */
1972 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1977 * TODO -- still calling the redist api using the
1978 * route_entries, and there's a corner-case here:
1979 * if there's no client for the 'new' route, a redist
1980 * deleting the 'old' route will be sent. But if the
1981 * 'old' context info was stale, 'old_re' will be
1982 * NULL here and that delete will not be sent.
1984 redistribute_update(dest_pfx
, src_pfx
, re
, old_re
);
1986 /* Notify route owner */
1987 zsend_route_notify_owner(re
, dest_pfx
,
1988 ZAPI_ROUTE_INSTALLED
);
1991 zsend_route_notify_owner(re
, dest_pfx
,
1992 ZAPI_ROUTE_FAIL_INSTALL
);
1994 zlog_warn("%u:%s: Route install failed",
1995 dplane_ctx_get_vrf(ctx
),
1996 prefix2str(dest_pfx
,
1997 dest_str
, sizeof(dest_str
)));
2000 case DPLANE_OP_ROUTE_DELETE
:
2002 * In the delete case, the zebra core datastructs were
2003 * updated (or removed) at the time the delete was issued,
2004 * so we're just notifying the route owner.
2006 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2007 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2012 zsend_route_notify_owner_ctx(ctx
,
2013 ZAPI_ROUTE_REMOVE_FAIL
);
2015 zlog_warn("%u:%s: Route Deletion failure",
2016 dplane_ctx_get_vrf(ctx
),
2017 prefix2str(dest_pfx
,
2018 dest_str
, sizeof(dest_str
)));
2026 /* Return context to dataplane module */
2027 dplane_ctx_fini(&ctx
);
2030 /* Take a list of route_node structs and return 1, if there was a record
2031 * picked from it and processed by rib_process(). Don't process more,
2032 * than one RN record; operate only in the specified sub-queue.
2034 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2036 struct listnode
*lnode
= listhead(subq
);
2037 struct route_node
*rnode
;
2039 struct zebra_vrf
*zvrf
= NULL
;
2044 rnode
= listgetdata(lnode
);
2045 dest
= rib_dest_from_rnode(rnode
);
2047 zvrf
= rib_dest_vrf(dest
);
2051 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2052 char buf
[SRCDEST2STR_BUFFER
];
2053 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2054 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2055 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2059 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2060 RIB_ROUTE_QUEUED(qindex
));
2065 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2066 __func__
, rnode
, rnode
->lock
);
2067 zlog_backtrace(LOG_DEBUG
);
2070 route_unlock_node(rnode
);
2071 list_delete_node(subq
, lnode
);
2076 * Perform next-hop tracking processing after RIB updates.
2078 static void do_nht_processing(void)
2081 struct zebra_vrf
*zvrf
;
2083 /* Evaluate nexthops for those VRFs which underwent route processing.
2085 * should limit the evaluation to the necessary VRFs in most common
2088 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2090 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2093 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHT
)
2094 zlog_debug("NHT processing check for zvrf %s",
2097 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2098 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2099 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_IMPORT_CHECK_TYPE
,
2101 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2102 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_IMPORT_CHECK_TYPE
,
2106 /* Schedule LSPs for processing, if needed. */
2107 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2108 if (mpls_should_lsps_be_processed(zvrf
)) {
2109 if (IS_ZEBRA_DEBUG_MPLS
)
2111 "%u: Scheduling all LSPs upon RIB completion",
2113 zebra_mpls_lsp_schedule(zvrf
);
2114 mpls_unmark_lsps_for_processing(zvrf
);
2119 * All meta queues have been processed. Trigger next-hop evaluation.
2121 static void meta_queue_process_complete(struct work_queue
*dummy
)
2123 do_nht_processing();
2126 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2127 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2129 * is pointed to the meta queue structure.
2131 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2133 struct meta_queue
*mq
= data
;
2135 uint32_t queue_len
, queue_limit
;
2137 /* Ensure there's room for more dataplane updates */
2138 queue_limit
= dplane_get_in_queue_limit();
2139 queue_len
= dplane_get_in_queue_len();
2140 if (queue_len
> queue_limit
) {
2141 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2142 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2143 queue_len
, queue_limit
);
2145 /* Ensure that the meta-queue is actually enqueued */
2146 if (work_queue_empty(zebrad
.ribq
))
2147 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2149 return WQ_QUEUE_BLOCKED
;
2152 for (i
= 0; i
< MQ_SIZE
; i
++)
2153 if (process_subq(mq
->subq
[i
], i
)) {
2157 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2162 * Look into the RN and queue it into the highest priority queue
2163 * at this point in time for processing.
2165 * We will enqueue a route node only once per invocation.
2167 * There are two possibilities here that should be kept in mind.
2168 * If the original invocation has not been pulled off for processing
2169 * yet, A subsuquent invocation can have a route entry with a better
2170 * meta queue index value and we can have a situation where
2171 * we might have the same node enqueued 2 times. Not necessarily
2172 * an optimal situation but it should be ok.
2174 * The other possibility is that the original invocation has not
2175 * been pulled off for processing yet, A subsusquent invocation
2176 * doesn't have a route_entry with a better meta-queue and the
2177 * original metaqueue index value will win and we'll end up with
2178 * the route node enqueued once.
2180 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2182 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2183 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2184 struct zebra_vrf
*zvrf
;
2186 RNODE_FOREACH_RE (rn
, curr_re
) {
2187 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2189 if (curr_qindex
<= qindex
) {
2191 qindex
= curr_qindex
;
2198 /* Invariant: at this point we always have rn->info set. */
2199 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2200 RIB_ROUTE_QUEUED(qindex
))) {
2201 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2202 rnode_debug(rn
, re
->vrf_id
,
2203 "rn %p is already queued in sub-queue %u",
2204 (void *)rn
, qindex
);
2208 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2209 listnode_add(mq
->subq
[qindex
], rn
);
2210 route_lock_node(rn
);
2213 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2214 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2215 (void *)rn
, qindex
);
2217 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2219 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2222 /* Add route_node to work queue and schedule processing */
2223 void rib_queue_add(struct route_node
*rn
)
2227 /* Pointless to queue a route_node with no RIB entries to add or remove
2229 if (!rnode_to_ribs(rn
)) {
2230 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2231 __func__
, (void *)rn
, rn
->lock
);
2232 zlog_backtrace(LOG_DEBUG
);
2236 if (zebrad
.ribq
== NULL
) {
2237 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2238 "%s: work_queue does not exist!", __func__
);
2243 * The RIB queue should normally be either empty or holding the only
2244 * work_queue_item element. In the latter case this element would
2245 * hold a pointer to the meta queue structure, which must be used to
2246 * actually queue the route nodes to process. So create the MQ
2247 * holder, if necessary, then push the work into it in any case.
2248 * This semantics was introduced after 0.99.9 release.
2250 if (work_queue_empty(zebrad
.ribq
))
2251 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2253 rib_meta_queue_add(zebrad
.mq
, rn
);
2258 /* Create new meta queue.
2259 A destructor function doesn't seem to be necessary here.
2261 static struct meta_queue
*meta_queue_new(void)
2263 struct meta_queue
*new;
2266 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2268 for (i
= 0; i
< MQ_SIZE
; i
++) {
2269 new->subq
[i
] = list_new();
2270 assert(new->subq
[i
]);
2276 void meta_queue_free(struct meta_queue
*mq
)
2280 for (i
= 0; i
< MQ_SIZE
; i
++)
2281 list_delete(&mq
->subq
[i
]);
2283 XFREE(MTYPE_WORK_QUEUE
, mq
);
2286 /* initialise zebra rib work queue */
2287 static void rib_queue_init(struct zebra_t
*zebra
)
2292 work_queue_new(zebra
->master
, "route_node processing"))) {
2293 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2294 "%s: could not initialise work queue!", __func__
);
2298 /* fill in the work queue spec */
2299 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2300 zebra
->ribq
->spec
.errorfunc
= NULL
;
2301 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2302 /* XXX: TODO: These should be runtime configurable via vty */
2303 zebra
->ribq
->spec
.max_retries
= 3;
2304 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2305 zebra
->ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2307 if (!(zebra
->mq
= meta_queue_new())) {
2308 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2309 "%s: could not initialise meta queue!", __func__
);
2315 /* RIB updates are processed via a queue of pointers to route_nodes.
2317 * The queue length is bounded by the maximal size of the routing table,
2318 * as a route_node will not be requeued, if already queued.
2320 * REs are submitted via rib_addnode or rib_delnode which set minimal
2321 * state, or static_install_route (when an existing RE is updated)
2322 * and then submit route_node to queue for best-path selection later.
2323 * Order of add/delete state changes are preserved for any given RE.
2325 * Deleted REs are reaped during best-path selection.
2328 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2329 * |-------->| | best RE, if required
2331 * static_install->|->rib_addqueue...... -> rib_process
2333 * |-------->| |-> rib_unlink
2334 * |-> set ROUTE_ENTRY_REMOVE |
2335 * rib_delnode (RE freed)
2337 * The 'info' pointer of a route_node points to a rib_dest_t
2338 * ('dest'). Queueing state for a route_node is kept on the dest. The
2339 * dest is created on-demand by rib_link() and is kept around at least
2340 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2342 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2344 * - route_nodes: refcounted by:
2345 * - dest attached to route_node:
2346 * - managed by: rib_link/rib_gc_dest
2347 * - route_node processing queue
2348 * - managed by: rib_addqueue, rib_process.
2352 /* Add RE to head of the route node. */
2353 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2355 struct route_entry
*head
;
2358 const char *rmap_name
;
2362 dest
= rib_dest_from_rnode(rn
);
2364 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2365 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2367 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2368 route_lock_node(rn
); /* rn route table reference */
2373 head
= dest
->routes
;
2380 afi
= (rn
->p
.family
== AF_INET
)
2382 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2383 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2384 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2385 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2390 static void rib_addnode(struct route_node
*rn
,
2391 struct route_entry
*re
, int process
)
2393 /* RE node has been un-removed before route-node is processed.
2394 * route_node must hence already be on the queue for processing..
2396 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2397 if (IS_ZEBRA_DEBUG_RIB
)
2398 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2399 (void *)rn
, (void *)re
);
2401 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2404 rib_link(rn
, re
, process
);
2410 * Detach a rib structure from a route_node.
2412 * Note that a call to rib_unlink() should be followed by a call to
2413 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2414 * longer required to be deleted.
2416 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2422 if (IS_ZEBRA_DEBUG_RIB
)
2423 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2426 dest
= rib_dest_from_rnode(rn
);
2429 re
->next
->prev
= re
->prev
;
2432 re
->prev
->next
= re
->next
;
2434 dest
->routes
= re
->next
;
2437 if (dest
->selected_fib
== re
)
2438 dest
->selected_fib
= NULL
;
2440 nexthops_free(re
->ng
.nexthop
);
2441 XFREE(MTYPE_RE
, re
);
2444 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2448 if (IS_ZEBRA_DEBUG_RIB
)
2449 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2450 (void *)rn
, (void *)re
);
2451 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2453 afi
= (rn
->p
.family
== AF_INET
)
2455 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2456 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2457 zebra_del_import_table_entry(rn
, re
);
2458 /* Just clean up if non main table */
2459 if (IS_ZEBRA_DEBUG_RIB
) {
2460 char buf
[SRCDEST2STR_BUFFER
];
2461 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2463 "%u:%s: Freeing route rn %p, re %p (type %d)",
2464 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2473 /* This function dumps the contents of a given RE entry into
2474 * standard debug log. Calling function name and IP prefix in
2475 * question are passed as 1st and 2nd arguments.
2478 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2479 union prefixconstptr src_pp
,
2480 const struct route_entry
*re
)
2482 const struct prefix
*src_p
= src_pp
.p
;
2483 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2484 char straddr
[PREFIX_STRLEN
];
2485 char srcaddr
[PREFIX_STRLEN
];
2486 struct nexthop
*nexthop
;
2488 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2489 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2490 is_srcdst
? " from " : "",
2491 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2494 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2495 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2498 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2499 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2500 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2501 re
->nexthop_num
, re
->nexthop_active_num
);
2503 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2504 struct interface
*ifp
;
2505 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2507 switch (nexthop
->type
) {
2508 case NEXTHOP_TYPE_BLACKHOLE
:
2509 sprintf(straddr
, "Blackhole");
2511 case NEXTHOP_TYPE_IFINDEX
:
2512 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2514 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2516 case NEXTHOP_TYPE_IPV4
:
2518 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2519 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2522 case NEXTHOP_TYPE_IPV6
:
2523 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2524 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2528 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2529 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2530 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2532 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2535 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2538 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2542 zlog_debug("%s: dump complete", func
);
2545 /* This is an exported helper to rtm_read() to dump the strange
2546 * RE entry found by rib_lookup_ipv4_route()
2549 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2551 struct route_table
*table
;
2552 struct route_node
*rn
;
2553 struct route_entry
*re
;
2554 char prefix_buf
[INET_ADDRSTRLEN
];
2557 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2559 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2560 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2565 /* Scan the RIB table for exactly matching RE entry. */
2566 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2568 /* No route for this prefix. */
2570 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2571 prefix2str((struct prefix
*)p
, prefix_buf
,
2572 sizeof(prefix_buf
)));
2577 route_unlock_node(rn
);
2580 RNODE_FOREACH_RE (rn
, re
) {
2581 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2583 (void *)rn
, (void *)re
,
2584 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2587 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2590 route_entry_dump(p
, NULL
, re
);
2594 /* Check if requested address assignment will fail due to another
2595 * route being installed by zebra in FIB already. Take necessary
2596 * actions, if needed: remove such a route from FIB and deSELECT
2597 * corresponding RE entry. Then put affected RN into RIBQ head.
2599 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2601 struct route_table
*table
;
2602 struct route_node
*rn
;
2603 unsigned changed
= 0;
2606 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2607 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2608 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2613 /* No matches would be the simplest case. */
2614 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2618 route_unlock_node(rn
);
2620 dest
= rib_dest_from_rnode(rn
);
2621 /* Check all RE entries. In case any changes have to be done, requeue
2622 * the RN into RIBQ head. If the routing message about the new connected
2623 * route (generated by the IP address we are going to assign very soon)
2624 * comes before the RIBQ is processed, the new RE entry will join
2625 * RIBQ record already on head. This is necessary for proper
2627 * of the rest of the RE.
2629 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2631 if (IS_ZEBRA_DEBUG_RIB
) {
2632 char buf
[PREFIX_STRLEN
];
2634 zlog_debug("%u:%s: freeing way for connected prefix",
2635 dest
->selected_fib
->vrf_id
,
2636 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2637 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2639 rib_uninstall(rn
, dest
->selected_fib
);
2645 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2646 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2648 struct route_table
*table
;
2649 struct route_node
*rn
;
2650 struct route_entry
*same
= NULL
;
2651 struct nexthop
*nexthop
;
2657 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2660 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2662 XFREE(MTYPE_RE
, re
);
2666 /* Make it sure prefixlen is applied to the prefix. */
2669 apply_mask_ipv6(src_p
);
2671 /* Set default distance by route type. */
2672 if (re
->distance
== 0) {
2673 re
->distance
= route_distance(re
->type
);
2675 /* iBGP distance is 200. */
2676 if (re
->type
== ZEBRA_ROUTE_BGP
2677 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2681 /* Lookup route node.*/
2682 rn
= srcdest_rnode_get(table
, p
, src_p
);
2685 * If same type of route are installed, treat it as a implicit
2687 * If the user has specified the No route replace semantics
2688 * for the install don't do a route replace.
2690 RNODE_FOREACH_RE (rn
, same
) {
2691 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2694 if (same
->type
!= re
->type
)
2696 if (same
->instance
!= re
->instance
)
2698 if (same
->type
== ZEBRA_ROUTE_KERNEL
2699 && same
->metric
!= re
->metric
)
2702 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2703 same
->distance
!= re
->distance
)
2707 * We should allow duplicate connected routes
2708 * because of IPv6 link-local routes and unnumbered
2709 * interfaces on Linux.
2711 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2715 /* If this route is kernel route, set FIB flag to the route. */
2716 if (RIB_SYSTEM_ROUTE(re
))
2717 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2718 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2720 /* Link new re to node.*/
2721 if (IS_ZEBRA_DEBUG_RIB
) {
2724 "Inserting route rn %p, re %p (type %d) existing %p",
2725 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2727 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2728 route_entry_dump(p
, src_p
, re
);
2730 rib_addnode(rn
, re
, 1);
2733 /* Free implicit route.*/
2735 rib_delnode(rn
, same
);
2739 route_unlock_node(rn
);
2743 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2744 unsigned short instance
, int flags
, struct prefix
*p
,
2745 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2746 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2749 struct route_table
*table
;
2750 struct route_node
*rn
;
2751 struct route_entry
*re
;
2752 struct route_entry
*fib
= NULL
;
2753 struct route_entry
*same
= NULL
;
2754 struct nexthop
*rtnh
;
2755 char buf2
[INET6_ADDRSTRLEN
];
2758 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2761 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2768 apply_mask_ipv6(src_p
);
2770 /* Lookup route node. */
2771 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2773 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2775 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2776 if (src_p
&& src_p
->prefixlen
)
2777 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2781 if (IS_ZEBRA_DEBUG_RIB
)
2782 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2784 (src_buf
[0] != '\0') ? " from " : "",
2789 dest
= rib_dest_from_rnode(rn
);
2790 fib
= dest
->selected_fib
;
2792 /* Lookup same type route. */
2793 RNODE_FOREACH_RE (rn
, re
) {
2794 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2797 if (re
->type
!= type
)
2799 if (re
->instance
!= instance
)
2801 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2802 distance
!= re
->distance
)
2805 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2807 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2808 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2809 if (rtnh
->ifindex
!= nh
->ifindex
)
2814 /* Make sure that the route found has the same gateway. */
2820 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2821 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2829 /* If same type of route can't be found and this message is from
2833 * In the past(HA!) we could get here because
2834 * we were receiving a route delete from the
2835 * kernel and we're not marking the proto
2836 * as coming from it's appropriate originator.
2837 * Now that we are properly noticing the fact
2838 * that the kernel has deleted our route we
2839 * are not going to get called in this path
2840 * I am going to leave this here because
2841 * this might still work this way on non-linux
2842 * platforms as well as some weird state I have
2843 * not properly thought of yet.
2844 * If we can show that this code path is
2845 * dead then we can remove it.
2847 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2848 if (IS_ZEBRA_DEBUG_RIB
) {
2851 "rn %p, re %p (type %d) was deleted from kernel, adding",
2852 rn
, fib
, fib
->type
);
2856 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2858 UNSET_FLAG(rtnh
->flags
,
2862 * This is a non FRR route
2863 * as such we should mark
2866 dest
->selected_fib
= NULL
;
2868 /* This means someone else, other than Zebra,
2870 * a Zebra router from the kernel. We will add
2872 rib_install_kernel(rn
, fib
, NULL
);
2875 if (IS_ZEBRA_DEBUG_RIB
) {
2879 "via %s ifindex %d type %d "
2880 "doesn't exist in rib",
2881 inet_ntop(afi2family(afi
),
2888 "type %d doesn't exist in rib",
2891 route_unlock_node(rn
);
2897 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2899 rib_install_kernel(rn
, same
, NULL
);
2900 route_unlock_node(rn
);
2905 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2906 struct nexthop
*tmp_nh
;
2908 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2909 struct ipaddr vtep_ip
;
2911 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2912 if (afi
== AFI_IP
) {
2913 vtep_ip
.ipa_type
= IPADDR_V4
;
2914 memcpy(&(vtep_ip
.ipaddr_v4
),
2915 &(tmp_nh
->gate
.ipv4
),
2916 sizeof(struct in_addr
));
2918 vtep_ip
.ipa_type
= IPADDR_V6
;
2919 memcpy(&(vtep_ip
.ipaddr_v6
),
2920 &(tmp_nh
->gate
.ipv6
),
2921 sizeof(struct in6_addr
));
2923 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2927 rib_delnode(rn
, same
);
2930 route_unlock_node(rn
);
2935 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2936 unsigned short instance
, int flags
, struct prefix
*p
,
2937 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2938 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2941 struct route_entry
*re
;
2942 struct nexthop
*nexthop
;
2944 /* Allocate new route_entry structure. */
2945 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2947 re
->instance
= instance
;
2948 re
->distance
= distance
;
2950 re
->metric
= metric
;
2952 re
->table
= table_id
;
2953 re
->vrf_id
= vrf_id
;
2954 re
->nexthop_num
= 0;
2955 re
->uptime
= time(NULL
);
2959 nexthop
= nexthop_new();
2961 route_entry_nexthop_add(re
, nexthop
);
2963 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2966 /* Schedule routes of a particular table (address-family) based on event. */
2967 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2969 struct route_node
*rn
;
2970 struct route_entry
*re
, *next
;
2972 /* Walk all routes and queue for processing, if appropriate for
2973 * the trigger event.
2975 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2977 * If we are looking at a route node and the node
2978 * has already been queued we don't
2979 * need to queue it up again
2981 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2982 RIB_ROUTE_ANY_QUEUED
))
2985 case RIB_UPDATE_IF_CHANGE
:
2986 /* Examine all routes that won't get processed by the
2988 * triggered by nexthop evaluation (NHT). This would be
2990 * kernel and certain static routes. Note that NHT will
2992 * triggered upon an interface event as connected routes
2994 * get queued for processing.
2996 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2999 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3000 && re
->type
!= ZEBRA_ROUTE_KERNEL
3001 && re
->type
!= ZEBRA_ROUTE_CONNECT
3002 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3005 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3010 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3011 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3012 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3015 /* If we only have nexthops to a
3024 case RIB_UPDATE_RMAP_CHANGE
:
3025 case RIB_UPDATE_OTHER
:
3026 /* Right now, examine all routes. Can restrict to a
3028 * some cases (TODO).
3030 if (rnode_to_ribs(rn
))
3040 /* RIB update function. */
3041 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3043 struct route_table
*table
;
3045 /* Process routes of interested address-families. */
3046 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3048 if (IS_ZEBRA_DEBUG_EVENT
)
3049 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3050 rib_update_table(table
, event
);
3053 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3055 if (IS_ZEBRA_DEBUG_EVENT
)
3056 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3057 rib_update_table(table
, event
);
3061 /* Delete self installed routes after zebra is relaunched. */
3062 void rib_sweep_table(struct route_table
*table
)
3064 struct route_node
*rn
;
3065 struct route_entry
*re
;
3066 struct route_entry
*next
;
3067 struct nexthop
*nexthop
;
3072 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3073 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3074 if (IS_ZEBRA_DEBUG_RIB
)
3075 route_entry_dump(&rn
->p
, NULL
, re
);
3077 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3080 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3084 * So we are starting up and have received
3085 * routes from the kernel that we have installed
3086 * from a previous run of zebra but not cleaned
3087 * up ( say a kill -9 )
3088 * But since we haven't actually installed
3089 * them yet( we received them from the kernel )
3090 * we don't think they are active.
3091 * So let's pretend they are active to actually
3093 * In all honesty I'm not sure if we should
3094 * mark them as active when we receive them
3095 * This is startup only so probably ok.
3097 * If we ever decide to move rib_sweep_table
3098 * to a different spot (ie startup )
3099 * this decision needs to be revisited
3101 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3102 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3104 rib_uninstall_kernel(rn
, re
);
3105 rib_delnode(rn
, re
);
3110 /* Sweep all RIB tables. */
3111 void rib_sweep_route(void)
3114 struct zebra_vrf
*zvrf
;
3116 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3117 if ((zvrf
= vrf
->info
) == NULL
)
3120 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3121 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3124 zebra_router_sweep_route();
3127 /* Remove specific by protocol routes from 'table'. */
3128 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3129 struct route_table
*table
)
3131 struct route_node
*rn
;
3132 struct route_entry
*re
;
3133 struct route_entry
*next
;
3134 unsigned long n
= 0;
3137 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3138 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3139 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3141 if (re
->type
== proto
3142 && re
->instance
== instance
) {
3143 rib_delnode(rn
, re
);
3150 /* Remove specific by protocol routes. */
3151 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3154 struct zebra_vrf
*zvrf
;
3155 unsigned long cnt
= 0;
3157 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3158 if ((zvrf
= vrf
->info
) != NULL
)
3159 cnt
+= rib_score_proto_table(
3161 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3162 + rib_score_proto_table(
3164 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3166 cnt
+= zebra_router_score_proto(proto
, instance
);
3171 /* Close RIB and clean up kernel routes. */
3172 void rib_close_table(struct route_table
*table
)
3174 struct route_node
*rn
;
3175 rib_table_info_t
*info
;
3181 info
= route_table_get_info(table
);
3183 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3184 dest
= rib_dest_from_rnode(rn
);
3186 if (dest
&& dest
->selected_fib
) {
3187 if (info
->safi
== SAFI_UNICAST
)
3188 hook_call(rib_update
, rn
, NULL
);
3190 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
3191 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3192 dest
->selected_fib
= NULL
;
3199 * Handle results from the dataplane system. Dequeue update context
3200 * structs, dispatch to appropriate internal handlers.
3202 static int rib_process_dplane_results(struct thread
*thread
)
3204 struct zebra_dplane_ctx
*ctx
;
3205 struct dplane_ctx_q ctxlist
;
3207 /* Dequeue a list of completed updates with one lock/unlock cycle */
3209 /* TODO -- dequeue a list with one lock/unlock cycle? */
3212 TAILQ_INIT(&ctxlist
);
3214 /* Take lock controlling queue of results */
3215 pthread_mutex_lock(&dplane_mutex
);
3217 /* Dequeue list of context structs */
3218 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3220 pthread_mutex_unlock(&dplane_mutex
);
3222 /* Dequeue context block */
3223 ctx
= dplane_ctx_dequeue(&ctxlist
);
3225 /* If we've emptied the results queue, we're done */
3230 switch (dplane_ctx_get_op(ctx
)) {
3231 case DPLANE_OP_ROUTE_INSTALL
:
3232 case DPLANE_OP_ROUTE_UPDATE
:
3233 case DPLANE_OP_ROUTE_DELETE
:
3234 rib_process_result(ctx
);
3237 case DPLANE_OP_LSP_INSTALL
:
3238 case DPLANE_OP_LSP_UPDATE
:
3239 case DPLANE_OP_LSP_DELETE
:
3240 zebra_mpls_lsp_dplane_result(ctx
);
3244 /* Don't expect this: just return the struct? */
3245 dplane_ctx_fini(&ctx
);
3247 } /* Dispatch by op code */
3249 ctx
= dplane_ctx_dequeue(&ctxlist
);
3254 /* Check for nexthop tracking processing after finishing with results */
3255 do_nht_processing();
3261 * Results are returned from the dataplane subsystem, in the context of
3262 * the dataplane pthread. We enqueue the results here for processing by
3263 * the main thread later.
3265 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3267 /* Take lock controlling queue of results */
3268 pthread_mutex_lock(&dplane_mutex
);
3270 /* Enqueue context blocks */
3271 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3273 pthread_mutex_unlock(&dplane_mutex
);
3275 /* Ensure event is signalled to zebra main pthread */
3276 thread_add_event(zebrad
.master
, rib_process_dplane_results
, NULL
, 0,
3282 /* Routing information base initialize. */
3285 rib_queue_init(&zebrad
);
3287 /* Init dataplane, and register for results */
3288 pthread_mutex_init(&dplane_mutex
, NULL
);
3289 TAILQ_INIT(&rib_dplane_q
);
3290 zebra_dplane_init(rib_dplane_results
);
3296 * Get the first vrf id that is greater than the given vrf id if any.
3298 * Returns TRUE if a vrf id was found, FALSE otherwise.
3300 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3304 vrf
= vrf_lookup_by_id(vrf_id
);
3306 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3308 *next_id_p
= vrf
->vrf_id
;
3317 * rib_tables_iter_next
3319 * Returns the next table in the iteration.
3321 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3323 struct route_table
*table
;
3326 * Array that helps us go over all AFI/SAFI combinations via one
3333 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3334 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3335 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3340 switch (iter
->state
) {
3342 case RIB_TABLES_ITER_S_INIT
:
3343 iter
->vrf_id
= VRF_DEFAULT
;
3344 iter
->afi_safi_ix
= -1;
3348 case RIB_TABLES_ITER_S_ITERATING
:
3349 iter
->afi_safi_ix
++;
3352 while (iter
->afi_safi_ix
3353 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3354 table
= zebra_vrf_table(
3355 afi_safis
[iter
->afi_safi_ix
].afi
,
3356 afi_safis
[iter
->afi_safi_ix
].safi
,
3361 iter
->afi_safi_ix
++;
3365 * Found another table in this vrf.
3371 * Done with all tables in the current vrf, go to the
3375 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3378 iter
->afi_safi_ix
= 0;
3383 case RIB_TABLES_ITER_S_DONE
:
3388 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3390 iter
->state
= RIB_TABLES_ITER_S_DONE
;