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(match
->status
,
575 ROUTE_ENTRY_INSTALLED
))
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(match
->status
,
601 ROUTE_ENTRY_INSTALLED
))
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
;
647 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
651 memset(&p
, 0, sizeof(struct prefix
));
654 p
.u
.prefix4
= addr
->ipv4
;
655 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
657 p
.u
.prefix6
= addr
->ipv6
;
658 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
661 rn
= route_node_match(table
, (struct prefix
*)&p
);
666 route_unlock_node(rn
);
668 dest
= rib_dest_from_rnode(rn
);
669 if (dest
&& dest
->selected_fib
670 && !CHECK_FLAG(dest
->selected_fib
->status
,
671 ROUTE_ENTRY_REMOVED
))
672 match
= dest
->selected_fib
;
674 /* If there is no selected route or matched route is EGP, go up
679 } while (rn
&& rn
->info
== NULL
);
683 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
684 if (!CHECK_FLAG(match
->status
,
685 ROUTE_ENTRY_INSTALLED
))
697 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
699 struct route_node
**rn_out
)
701 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
702 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
703 union g_addr gaddr
= {.ipv4
= addr
};
705 switch (ipv4_multicast_mode
) {
706 case MCAST_MRIB_ONLY
:
707 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
709 case MCAST_URIB_ONLY
:
710 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
711 case MCAST_NO_CONFIG
:
712 case MCAST_MIX_MRIB_FIRST
:
713 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
716 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
719 case MCAST_MIX_DISTANCE
:
720 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
721 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
723 re
= ure
->distance
< mre
->distance
? ure
: mre
;
729 case MCAST_MIX_PFXLEN
:
730 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
731 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
733 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
742 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
744 if (IS_ZEBRA_DEBUG_RIB
) {
746 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
748 zlog_debug("%s: %s: vrf: %u found %s, using %s",
749 __func__
, buf
, vrf_id
,
750 mre
? (ure
? "MRIB+URIB" : "MRIB")
751 : ure
? "URIB" : "nothing",
752 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
757 void multicast_mode_ipv4_set(enum multicast_mode mode
)
759 if (IS_ZEBRA_DEBUG_RIB
)
760 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
762 ipv4_multicast_mode
= mode
;
765 enum multicast_mode
multicast_mode_ipv4_get(void)
767 return ipv4_multicast_mode
;
770 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
772 struct route_table
*table
;
773 struct route_node
*rn
;
774 struct route_entry
*match
= NULL
;
778 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
782 rn
= route_node_lookup(table
, (struct prefix
*)p
);
784 /* No route for this prefix. */
789 route_unlock_node(rn
);
790 dest
= rib_dest_from_rnode(rn
);
792 if (dest
&& dest
->selected_fib
793 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
794 match
= dest
->selected_fib
;
799 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
802 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
808 #define RIB_SYSTEM_ROUTE(R) \
809 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
811 #define RIB_KERNEL_ROUTE(R) \
812 ((R)->type == ZEBRA_ROUTE_KERNEL)
814 /* This function verifies reachability of one given nexthop, which can be
815 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
816 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
817 * nexthop->ifindex will be updated appropriately as well.
818 * An existing route map can turn (otherwise active) nexthop into inactive, but
821 * The return value is the final value of 'ACTIVE' flag.
824 static unsigned nexthop_active_check(struct route_node
*rn
,
825 struct route_entry
*re
,
826 struct nexthop
*nexthop
, bool set
)
828 struct interface
*ifp
;
829 route_map_result_t ret
= RMAP_MATCH
;
831 char buf
[SRCDEST2STR_BUFFER
];
832 const struct prefix
*p
, *src_p
;
833 struct zebra_vrf
*zvrf
;
835 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
837 if (rn
->p
.family
== AF_INET
)
839 else if (rn
->p
.family
== AF_INET6
)
843 switch (nexthop
->type
) {
844 case NEXTHOP_TYPE_IFINDEX
:
845 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
846 if (ifp
&& if_is_operative(ifp
))
847 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
849 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
851 case NEXTHOP_TYPE_IPV4
:
852 case NEXTHOP_TYPE_IPV4_IFINDEX
:
854 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
855 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
857 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
859 case NEXTHOP_TYPE_IPV6
:
861 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
862 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
864 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
866 case NEXTHOP_TYPE_IPV6_IFINDEX
:
867 /* RFC 5549, v4 prefix with v6 NH */
868 if (rn
->p
.family
!= AF_INET
)
870 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
871 ifp
= if_lookup_by_index(nexthop
->ifindex
,
873 if (ifp
&& if_is_operative(ifp
))
874 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
876 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
878 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
879 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
881 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
884 case NEXTHOP_TYPE_BLACKHOLE
:
885 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
890 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
891 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
892 zlog_debug("\t%s: Unable to find a active nexthop",
893 __PRETTY_FUNCTION__
);
897 /* XXX: What exactly do those checks do? Do we support
898 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
900 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
901 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
902 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
904 /* The original code didn't determine the family correctly
905 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
906 * from the rib_table_info in those cases.
907 * Possibly it may be better to use only the rib_table_info
911 rib_table_info_t
*info
;
913 info
= srcdest_rnode_table_info(rn
);
917 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
919 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
921 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
922 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
923 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
926 /* It'll get set if required inside */
927 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
928 nexthop
, zvrf
, re
->tag
);
929 if (ret
== RMAP_DENYMATCH
) {
930 if (IS_ZEBRA_DEBUG_RIB
) {
931 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
933 "%u:%s: Filtering out with NH out %s due to route map",
935 ifindex2ifname(nexthop
->ifindex
,
938 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
940 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
943 /* Iterate over all nexthops of the given RIB entry and refresh their
944 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
945 * nexthop is found to toggle the ACTIVE flag, the whole re structure
946 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
947 * transparently passed to nexthop_active_check().
949 * Return value is the new number of active nexthops.
952 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
955 struct nexthop
*nexthop
;
956 union g_addr prev_src
;
957 unsigned int prev_active
, new_active
, old_num_nh
;
958 ifindex_t prev_index
;
960 old_num_nh
= re
->nexthop_active_num
;
962 re
->nexthop_active_num
= 0;
963 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
965 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
966 /* No protocol daemon provides src and so we're skipping
968 prev_src
= nexthop
->rmap_src
;
969 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
970 prev_index
= nexthop
->ifindex
;
972 * We need to respect the multipath_num here
973 * as that what we should be able to install from
974 * a multipath perpsective should not be a data plane
977 new_active
= nexthop_active_check(rn
, re
, nexthop
, set
);
978 if (new_active
&& re
->nexthop_active_num
>= multipath_num
) {
979 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
983 re
->nexthop_active_num
++;
984 /* Don't allow src setting on IPv6 addr for now */
985 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
986 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
987 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
988 && prev_src
.ipv4
.s_addr
989 != nexthop
->rmap_src
.ipv4
.s_addr
)
990 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
991 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
992 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
993 &nexthop
->rmap_src
.ipv6
)))) {
994 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
995 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
999 if (old_num_nh
!= re
->nexthop_active_num
)
1000 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1002 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1003 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1006 return re
->nexthop_active_num
;
1010 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1011 * (nexthops) must have a label.
1013 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1015 struct nexthop
*nexthop
= NULL
;
1017 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1020 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1021 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1027 /* Update flag indicates whether this is a "replace" or not. Currently, this
1028 * is only used for IPv4.
1030 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1031 struct route_entry
*old
)
1033 struct nexthop
*nexthop
;
1034 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1035 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1036 const struct prefix
*p
, *src_p
;
1037 enum zebra_dplane_result ret
;
1039 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1041 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1043 if (info
->safi
!= SAFI_UNICAST
) {
1044 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1045 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1048 struct nexthop
*prev
;
1050 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1051 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1052 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1053 if (prev
== nexthop
)
1055 if (nexthop_same_firsthop(nexthop
, prev
)) {
1056 SET_FLAG(nexthop
->flags
,
1057 NEXTHOP_FLAG_DUPLICATE
);
1065 * If this is a replace to a new RE let the originator of the RE
1066 * know that they've lost
1068 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1069 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1071 /* Update fib selection */
1072 dest
->selected_fib
= re
;
1075 * Make sure we update the FPM any time we send new information to
1078 hook_call(rib_update
, rn
, "installing in kernel");
1080 /* Send add or update */
1081 if (old
&& (old
!= re
))
1082 ret
= dplane_route_update(rn
, re
, old
);
1084 ret
= dplane_route_add(rn
, re
);
1087 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1088 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1090 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
1092 zvrf
->installs_queued
++;
1094 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1096 char str
[SRCDEST2STR_BUFFER
];
1098 srcdest_rnode2str(rn
, str
, sizeof(str
));
1099 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1100 "%u:%s: Failed to enqueue dataplane install",
1104 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1113 /* Uninstall the route from kernel. */
1114 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1116 struct nexthop
*nexthop
;
1117 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1118 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1120 if (info
->safi
!= SAFI_UNICAST
) {
1121 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1122 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1123 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1128 * Make sure we update the FPM any time we send new information to
1131 hook_call(rib_update
, rn
, "uninstalling from kernel");
1133 switch (dplane_route_delete(rn
, re
)) {
1134 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1136 zvrf
->removals_queued
++;
1138 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1140 char str
[SRCDEST2STR_BUFFER
];
1142 srcdest_rnode2str(rn
, str
, sizeof(str
));
1143 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1144 "%u:%s: Failed to enqueue dataplane uninstall",
1148 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1157 /* Uninstall the route from kernel. */
1158 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1160 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1161 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1162 struct nexthop
*nexthop
;
1164 if (dest
&& dest
->selected_fib
== re
) {
1165 if (info
->safi
== SAFI_UNICAST
)
1166 hook_call(rib_update
, rn
, "rib_uninstall");
1168 /* If labeled-unicast route, uninstall transit LSP. */
1169 if (zebra_rib_labeled_unicast(re
))
1170 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1172 if (!RIB_SYSTEM_ROUTE(re
))
1173 rib_uninstall_kernel(rn
, re
);
1175 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1177 dest
->selected_fib
= NULL
;
1179 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1180 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1183 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1184 const struct prefix
*p
, *src_p
;
1186 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1188 redistribute_delete(p
, src_p
, re
);
1189 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1194 * rib_can_delete_dest
1196 * Returns TRUE if the given dest can be deleted from the table.
1198 static int rib_can_delete_dest(rib_dest_t
*dest
)
1205 * Don't delete the dest if we have to update the FPM about this
1208 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1209 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1218 * Garbage collect the rib dest corresponding to the given route node
1221 * Returns TRUE if the dest was deleted, FALSE otherwise.
1223 int rib_gc_dest(struct route_node
*rn
)
1227 dest
= rib_dest_from_rnode(rn
);
1231 if (!rib_can_delete_dest(dest
))
1234 if (IS_ZEBRA_DEBUG_RIB
) {
1235 struct zebra_vrf
*zvrf
;
1237 zvrf
= rib_dest_vrf(dest
);
1238 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1242 XFREE(MTYPE_RIB_DEST
, dest
);
1246 * Release the one reference that we keep on the route node.
1248 route_unlock_node(rn
);
1252 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1253 struct route_entry
*new)
1255 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1257 hook_call(rib_update
, rn
, "new route selected");
1259 /* Update real nexthop. This may actually determine if nexthop is active
1261 if (!nexthop_active_update(rn
, new, true)) {
1262 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1266 if (IS_ZEBRA_DEBUG_RIB
) {
1267 char buf
[SRCDEST2STR_BUFFER
];
1268 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1269 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1270 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1273 /* If labeled-unicast route, install transit LSP. */
1274 if (zebra_rib_labeled_unicast(new))
1275 zebra_mpls_lsp_install(zvrf
, rn
, new);
1277 if (!RIB_SYSTEM_ROUTE(new))
1278 rib_install_kernel(rn
, new, NULL
);
1280 dest
->selected_fib
= new;
1282 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1285 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1286 struct route_entry
*old
)
1288 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1289 hook_call(rib_update
, rn
, "removing existing route");
1291 /* Uninstall from kernel. */
1292 if (IS_ZEBRA_DEBUG_RIB
) {
1293 char buf
[SRCDEST2STR_BUFFER
];
1294 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1295 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1296 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1299 /* If labeled-unicast route, uninstall transit LSP. */
1300 if (zebra_rib_labeled_unicast(old
))
1301 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1303 if (!RIB_SYSTEM_ROUTE(old
))
1304 rib_uninstall_kernel(rn
, old
);
1306 UNSET_FLAG(old
->status
, ROUTE_ENTRY_INSTALLED
);
1308 * We are setting this to NULL here
1309 * because that is what we traditionally
1310 * have been doing. I am not positive
1311 * that this is the right thing to do
1312 * but let's leave the code alone
1313 * for the RIB_SYSTEM_ROUTE case
1315 dest
->selected_fib
= NULL
;
1318 /* Update nexthop for route, reset changed flag. */
1319 /* Note: this code also handles the Linux case when an interface goes
1320 * down, causing the kernel to delete routes without sending DELROUTE
1323 if (!nexthop_active_update(rn
, old
, true) &&
1324 (RIB_KERNEL_ROUTE(old
)))
1325 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1327 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1330 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1331 struct route_node
*rn
,
1332 struct route_entry
*old
,
1333 struct route_entry
*new)
1335 struct nexthop
*nexthop
= NULL
;
1337 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1340 * We have to install or update if a new route has been selected or
1341 * something has changed.
1343 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1344 hook_call(rib_update
, rn
, "updating existing route");
1346 /* Update the nexthop; we could determine here that nexthop is
1348 if (nexthop_active_update(rn
, new, true))
1351 /* If nexthop is active, install the selected route, if
1353 * the install succeeds, cleanup flags for prior route, if
1358 if (IS_ZEBRA_DEBUG_RIB
) {
1359 char buf
[SRCDEST2STR_BUFFER
];
1360 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1363 "%u:%s: Updating route rn %p, re %p (type %d) "
1365 zvrf_id(zvrf
), buf
, rn
, new,
1366 new->type
, old
, old
->type
);
1369 "%u:%s: Updating route rn %p, re %p (type %d)",
1370 zvrf_id(zvrf
), buf
, rn
, new,
1374 /* If labeled-unicast route, uninstall transit LSP. */
1375 if (zebra_rib_labeled_unicast(old
))
1376 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1378 /* Non-system route should be installed. */
1379 if (!RIB_SYSTEM_ROUTE(new)) {
1380 /* If labeled-unicast route, install transit
1382 if (zebra_rib_labeled_unicast(new))
1383 zebra_mpls_lsp_install(zvrf
, rn
, new);
1385 rib_install_kernel(rn
, new, old
);
1387 UNSET_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
);
1389 * We do not need to install the
1390 * selected route because it
1391 * is already isntalled by
1392 * the system( ie not us )
1393 * so just mark it as winning
1394 * we do need to ensure that
1395 * if we uninstall a route
1396 * from ourselves we don't
1397 * over write this pointer
1399 dest
->selected_fib
= NULL
;
1401 /* If install succeeded or system route, cleanup flags
1402 * for prior route. */
1404 if (RIB_SYSTEM_ROUTE(new)) {
1405 if (!RIB_SYSTEM_ROUTE(old
))
1406 rib_uninstall_kernel(rn
, old
);
1410 ROUTE_ENTRY_INSTALLED
);
1412 UNSET_FLAG(old
->status
,
1413 ROUTE_ENTRY_INSTALLED
);
1414 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1415 nexthop
= nexthop
->next
)
1416 UNSET_FLAG(nexthop
->flags
,
1423 * If nexthop for selected route is not active or install
1425 * may need to uninstall and delete for redistribution.
1428 if (IS_ZEBRA_DEBUG_RIB
) {
1429 char buf
[SRCDEST2STR_BUFFER
];
1430 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1433 "%u:%s: Deleting route rn %p, re %p (type %d) "
1434 "old %p (type %d) - nexthop inactive",
1435 zvrf_id(zvrf
), buf
, rn
, new,
1436 new->type
, old
, old
->type
);
1439 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1440 zvrf_id(zvrf
), buf
, rn
, new,
1444 /* If labeled-unicast route, uninstall transit LSP. */
1445 if (zebra_rib_labeled_unicast(old
))
1446 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1448 if (!RIB_SYSTEM_ROUTE(old
))
1449 rib_uninstall_kernel(rn
, old
);
1451 UNSET_FLAG(old
->status
, ROUTE_ENTRY_INSTALLED
);
1452 dest
->selected_fib
= NULL
;
1457 * Same route selected; check if in the FIB and if not,
1459 * is housekeeping code to deal with race conditions in kernel
1461 * netlink reporting interface up before IPv4 or IPv6 protocol
1465 if (!RIB_SYSTEM_ROUTE(new)
1466 && !CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
))
1467 rib_install_kernel(rn
, new, NULL
);
1470 /* Update prior route. */
1472 /* Set real nexthop. */
1473 nexthop_active_update(rn
, old
, true);
1474 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1477 /* Clear changed flag. */
1478 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1481 /* Check if 'alternate' RIB entry is better than 'current'. */
1482 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1483 struct route_entry
*alternate
)
1485 if (current
== NULL
)
1488 /* filter route selection in following order:
1489 * - connected beats other types
1490 * - if both connected, loopback or vrf wins
1491 * - lower distance beats higher
1492 * - lower metric beats higher for equal distance
1493 * - last, hence oldest, route wins tie break.
1496 /* Connected routes. Check to see if either are a vrf
1497 * or loopback interface. If not, pick the last connected
1498 * route of the set of lowest metric connected routes.
1500 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1501 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1504 /* both are connected. are either loop or vrf? */
1505 struct nexthop
*nexthop
= NULL
;
1507 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1508 if (if_is_loopback_or_vrf(if_lookup_by_index(
1509 nexthop
->ifindex
, alternate
->vrf_id
)))
1513 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1514 if (if_is_loopback_or_vrf(if_lookup_by_index(
1515 nexthop
->ifindex
, current
->vrf_id
)))
1519 /* Neither are loop or vrf so pick best metric */
1520 if (alternate
->metric
<= current
->metric
)
1526 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1529 /* higher distance loses */
1530 if (alternate
->distance
< current
->distance
)
1532 if (current
->distance
< alternate
->distance
)
1535 /* metric tie-breaks equal distance */
1536 if (alternate
->metric
<= current
->metric
)
1542 /* Core function for processing routing information base. */
1543 static void rib_process(struct route_node
*rn
)
1545 struct route_entry
*re
;
1546 struct route_entry
*next
;
1547 struct route_entry
*old_selected
= NULL
;
1548 struct route_entry
*new_selected
= NULL
;
1549 struct route_entry
*old_fib
= NULL
;
1550 struct route_entry
*new_fib
= NULL
;
1551 struct route_entry
*best
= NULL
;
1552 char buf
[SRCDEST2STR_BUFFER
];
1554 struct zebra_vrf
*zvrf
= NULL
;
1555 const struct prefix
*p
, *src_p
;
1557 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1558 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1562 dest
= rib_dest_from_rnode(rn
);
1564 zvrf
= rib_dest_vrf(dest
);
1565 vrf_id
= zvrf_id(zvrf
);
1568 if (IS_ZEBRA_DEBUG_RIB
)
1569 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1571 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1572 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1575 * we can have rn's that have a NULL info pointer
1576 * (dest). As such let's not let the deref happen
1577 * additionally we know RNODE_FOREACH_RE_SAFE
1578 * will not iterate so we are ok.
1581 old_fib
= dest
->selected_fib
;
1583 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1584 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1586 "%u:%s: Examine re %p (type %d) status %x flags %x "
1587 "dist %d metric %d",
1588 vrf_id
, buf
, re
, re
->type
, re
->status
,
1589 re
->flags
, re
->distance
, re
->metric
);
1591 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1593 /* Currently selected re. */
1594 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1595 assert(old_selected
== NULL
);
1599 /* Skip deleted entries from selection */
1600 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1603 /* Skip unreachable nexthop. */
1604 /* This first call to nexthop_active_update is merely to
1606 * there's any change to nexthops associated with this RIB
1608 * rib_process() can be invoked due to an external event such as
1610 * down or due to next-hop-tracking evaluation. In the latter
1612 * a decision has already been made that the NHs have changed.
1614 * need to invoke a potentially expensive call again. Further,
1616 * the change might be in a recursive NH which is not caught in
1617 * the nexthop_active_update() code. Thus, we might miss changes
1621 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1622 && !nexthop_active_update(rn
, re
, false)) {
1623 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1624 /* XXX: HERE BE DRAGONS!!!!!
1625 * In all honesty, I have not yet figured out
1627 * does or why the ROUTE_ENTRY_CHANGED test
1629 * or why we need to delete a route here, and
1631 * this concerns both selected and fib route, or
1634 /* This entry was denied by the 'ip protocol
1635 * table' route-map, we
1636 * need to delete it */
1637 if (re
!= old_selected
) {
1638 if (IS_ZEBRA_DEBUG_RIB
)
1640 "%s: %u:%s: imported via import-table but denied "
1641 "by the ip protocol table route-map",
1642 __func__
, vrf_id
, buf
);
1645 SET_FLAG(re
->status
,
1646 ROUTE_ENTRY_REMOVED
);
1652 /* Infinite distance. */
1653 if (re
->distance
== DISTANCE_INFINITY
) {
1654 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1658 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1659 best
= rib_choose_best(new_fib
, re
);
1660 if (new_fib
&& best
!= new_fib
)
1661 UNSET_FLAG(new_fib
->status
,
1662 ROUTE_ENTRY_CHANGED
);
1665 best
= rib_choose_best(new_selected
, re
);
1666 if (new_selected
&& best
!= new_selected
)
1667 UNSET_FLAG(new_selected
->status
,
1668 ROUTE_ENTRY_CHANGED
);
1669 new_selected
= best
;
1672 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1673 } /* RNODE_FOREACH_RE */
1675 /* If no FIB override route, use the selected route also for FIB */
1676 if (new_fib
== NULL
)
1677 new_fib
= new_selected
;
1679 /* After the cycle is finished, the following pointers will be set:
1680 * old_selected --- RE entry currently having SELECTED
1681 * new_selected --- RE entry that is newly SELECTED
1682 * old_fib --- RE entry currently in kernel FIB
1683 * new_fib --- RE entry that is newly to be in kernel FIB
1685 * new_selected will get SELECTED flag, and is going to be redistributed
1686 * the zclients. new_fib (which can be new_selected) will be installed
1690 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1692 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1693 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1694 (void *)old_fib
, (void *)new_fib
);
1697 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1698 * fib == selected */
1699 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1700 ROUTE_ENTRY_CHANGED
);
1702 /* Update fib according to selection results */
1703 if (new_fib
&& old_fib
)
1704 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1706 rib_process_add_fib(zvrf
, rn
, new_fib
);
1708 rib_process_del_fib(zvrf
, rn
, old_fib
);
1710 /* Update SELECTED entry */
1711 if (old_selected
!= new_selected
|| selected_changed
) {
1713 if (new_selected
&& new_selected
!= new_fib
) {
1714 nexthop_active_update(rn
, new_selected
, true);
1715 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1719 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1721 /* Special case: new route is system route, so
1722 * dataplane update will not be done - ensure we
1723 * redistribute the route.
1725 if (RIB_SYSTEM_ROUTE(new_selected
))
1726 redistribute_update(p
, src_p
, new_selected
,
1732 redistribute_delete(p
, src_p
, old_selected
);
1733 if (old_selected
!= new_selected
)
1734 UNSET_FLAG(old_selected
->flags
,
1735 ZEBRA_FLAG_SELECTED
);
1739 /* Remove all RE entries queued for removal */
1740 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1741 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1742 if (IS_ZEBRA_DEBUG_RIB
) {
1743 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1744 (void *)rn
, (void *)re
);
1751 * Check if the dest can be deleted now.
1757 * Utility to match route with dplane context data
1759 static bool rib_route_match_ctx(const struct route_entry
*re
,
1760 const struct zebra_dplane_ctx
*ctx
,
1763 bool result
= false;
1767 * In 'update' case, we test info about the 'previous' or
1770 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1771 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1774 /* TODO -- we're using this extra test, but it's not
1775 * exactly clear why.
1777 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1778 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1779 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1786 * Ordinary, single-route case using primary context info
1788 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1789 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1790 /* Skip route that's been deleted */
1794 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1795 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1798 /* TODO -- we're using this extra test, but it's not
1799 * exactly clear why.
1801 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1802 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1803 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1815 * Route-update results processing after async dataplane update.
1817 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1819 struct route_table
*table
= NULL
;
1820 struct zebra_vrf
*zvrf
= NULL
;
1821 struct route_node
*rn
= NULL
;
1822 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1823 bool is_update
= false;
1824 struct nexthop
*nexthop
, *ctx_nexthop
;
1825 char dest_str
[PREFIX_STRLEN
] = "";
1826 enum dplane_op_e op
;
1827 enum zebra_dplane_result status
;
1828 const struct prefix
*dest_pfx
, *src_pfx
;
1830 /* Locate rn and re(s) from ctx */
1832 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1833 dplane_ctx_get_safi(ctx
),
1834 dplane_ctx_get_vrf(ctx
),
1835 dplane_ctx_get_table(ctx
));
1836 if (table
== NULL
) {
1837 if (IS_ZEBRA_DEBUG_DPLANE
) {
1838 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1839 dplane_ctx_get_afi(ctx
),
1840 dplane_ctx_get_safi(ctx
),
1841 dplane_ctx_get_vrf(ctx
));
1846 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1848 dest_pfx
= dplane_ctx_get_dest(ctx
);
1850 /* Note well: only capturing the prefix string if debug is enabled here;
1851 * unconditional log messages will have to generate the string.
1853 if (IS_ZEBRA_DEBUG_DPLANE
)
1854 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1856 src_pfx
= dplane_ctx_get_src(ctx
);
1857 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1858 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1860 if (IS_ZEBRA_DEBUG_DPLANE
) {
1861 zlog_debug("Failed to process dplane results: no route for %u:%s",
1862 dplane_ctx_get_vrf(ctx
), dest_str
);
1867 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1869 op
= dplane_ctx_get_op(ctx
);
1870 status
= dplane_ctx_get_status(ctx
);
1872 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1873 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1874 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1875 dplane_op2str(op
), dplane_res2str(status
));
1878 * Update is a bit of a special case, where we may have both old and new
1879 * routes to post-process.
1881 is_update
= dplane_ctx_is_update(ctx
);
1884 * Take a pass through the routes, look for matches with the context
1887 RNODE_FOREACH_RE(rn
, rib
) {
1890 if (rib_route_match_ctx(rib
, ctx
, false))
1894 /* Check for old route match */
1895 if (is_update
&& (old_re
== NULL
)) {
1896 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1900 /* Have we found the routes we need to work on? */
1901 if (re
&& ((!is_update
|| old_re
)))
1906 * Check sequence number(s) to detect stale results before continuing
1909 if (re
->dplane_sequence
!= dplane_ctx_get_seq(ctx
)) {
1910 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1911 zlog_debug("%u:%s Stale dplane result for re %p",
1912 dplane_ctx_get_vrf(ctx
),
1915 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1919 if (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
),
1925 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1929 case DPLANE_OP_ROUTE_INSTALL
:
1930 case DPLANE_OP_ROUTE_UPDATE
:
1931 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1933 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1934 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1937 * On an update operation from the same route type
1938 * context retrieval currently has no way to know
1939 * which was the old and which was the new.
1940 * So don't unset our flags that we just set.
1941 * We know redistribution is ok because the
1942 * old_re in this case is used for nothing
1943 * more than knowing whom to contact if necessary.
1945 if (old_re
&& old_re
!= re
) {
1946 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1947 UNSET_FLAG(old_re
->status
,
1948 ROUTE_ENTRY_INSTALLED
);
1950 /* Update zebra nexthop FIB flag for each
1951 * nexthop that was installed.
1953 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1959 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1960 if (nexthop_same(ctx_nexthop
, nexthop
))
1964 if (nexthop
== NULL
)
1967 if (CHECK_FLAG(nexthop
->flags
,
1968 NEXTHOP_FLAG_RECURSIVE
))
1971 if (CHECK_FLAG(ctx_nexthop
->flags
,
1973 SET_FLAG(nexthop
->flags
,
1976 UNSET_FLAG(nexthop
->flags
,
1982 /* Set flag for nexthop tracking processing */
1983 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1988 * TODO -- still calling the redist api using the
1989 * route_entries, and there's a corner-case here:
1990 * if there's no client for the 'new' route, a redist
1991 * deleting the 'old' route will be sent. But if the
1992 * 'old' context info was stale, 'old_re' will be
1993 * NULL here and that delete will not be sent.
1996 redistribute_update(dest_pfx
, src_pfx
,
1999 /* Notify route owner */
2000 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2004 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2006 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2008 zsend_route_notify_owner(re
, dest_pfx
,
2009 ZAPI_ROUTE_FAIL_INSTALL
);
2011 zlog_warn("%u:%s: Route install failed",
2012 dplane_ctx_get_vrf(ctx
),
2013 prefix2str(dest_pfx
,
2014 dest_str
, sizeof(dest_str
)));
2017 case DPLANE_OP_ROUTE_DELETE
:
2019 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2021 * In the delete case, the zebra core datastructs were
2022 * updated (or removed) at the time the delete was issued,
2023 * so we're just notifying the route owner.
2025 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2027 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2028 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2030 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2036 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2037 zsend_route_notify_owner_ctx(ctx
,
2038 ZAPI_ROUTE_REMOVE_FAIL
);
2040 zlog_warn("%u:%s: Route Deletion failure",
2041 dplane_ctx_get_vrf(ctx
),
2042 prefix2str(dest_pfx
,
2043 dest_str
, sizeof(dest_str
)));
2051 /* Return context to dataplane module */
2052 dplane_ctx_fini(&ctx
);
2055 /* Take a list of route_node structs and return 1, if there was a record
2056 * picked from it and processed by rib_process(). Don't process more,
2057 * than one RN record; operate only in the specified sub-queue.
2059 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2061 struct listnode
*lnode
= listhead(subq
);
2062 struct route_node
*rnode
;
2064 struct zebra_vrf
*zvrf
= NULL
;
2069 rnode
= listgetdata(lnode
);
2070 dest
= rib_dest_from_rnode(rnode
);
2072 zvrf
= rib_dest_vrf(dest
);
2076 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2077 char buf
[SRCDEST2STR_BUFFER
];
2078 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2079 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2080 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2084 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2085 RIB_ROUTE_QUEUED(qindex
));
2090 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2091 __func__
, rnode
, rnode
->lock
);
2092 zlog_backtrace(LOG_DEBUG
);
2095 route_unlock_node(rnode
);
2096 list_delete_node(subq
, lnode
);
2101 * Perform next-hop tracking processing after RIB updates.
2103 static void do_nht_processing(void)
2106 struct zebra_vrf
*zvrf
;
2108 /* Evaluate nexthops for those VRFs which underwent route processing.
2110 * should limit the evaluation to the necessary VRFs in most common
2113 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2115 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2118 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHT
)
2119 zlog_debug("NHT processing check for zvrf %s",
2122 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2123 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2124 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_IMPORT_CHECK_TYPE
,
2126 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2127 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_IMPORT_CHECK_TYPE
,
2131 /* Schedule LSPs for processing, if needed. */
2132 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2133 if (mpls_should_lsps_be_processed(zvrf
)) {
2134 if (IS_ZEBRA_DEBUG_MPLS
)
2136 "%u: Scheduling all LSPs upon RIB completion",
2138 zebra_mpls_lsp_schedule(zvrf
);
2139 mpls_unmark_lsps_for_processing(zvrf
);
2144 * All meta queues have been processed. Trigger next-hop evaluation.
2146 static void meta_queue_process_complete(struct work_queue
*dummy
)
2148 do_nht_processing();
2151 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2152 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2154 * is pointed to the meta queue structure.
2156 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2158 struct meta_queue
*mq
= data
;
2160 uint32_t queue_len
, queue_limit
;
2162 /* Ensure there's room for more dataplane updates */
2163 queue_limit
= dplane_get_in_queue_limit();
2164 queue_len
= dplane_get_in_queue_len();
2165 if (queue_len
> queue_limit
) {
2166 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2167 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2168 queue_len
, queue_limit
);
2170 /* Ensure that the meta-queue is actually enqueued */
2171 if (work_queue_empty(zebrad
.ribq
))
2172 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2174 return WQ_QUEUE_BLOCKED
;
2177 for (i
= 0; i
< MQ_SIZE
; i
++)
2178 if (process_subq(mq
->subq
[i
], i
)) {
2182 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2187 * Look into the RN and queue it into the highest priority queue
2188 * at this point in time for processing.
2190 * We will enqueue a route node only once per invocation.
2192 * There are two possibilities here that should be kept in mind.
2193 * If the original invocation has not been pulled off for processing
2194 * yet, A subsuquent invocation can have a route entry with a better
2195 * meta queue index value and we can have a situation where
2196 * we might have the same node enqueued 2 times. Not necessarily
2197 * an optimal situation but it should be ok.
2199 * The other possibility is that the original invocation has not
2200 * been pulled off for processing yet, A subsusquent invocation
2201 * doesn't have a route_entry with a better meta-queue and the
2202 * original metaqueue index value will win and we'll end up with
2203 * the route node enqueued once.
2205 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2207 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2208 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2209 struct zebra_vrf
*zvrf
;
2211 RNODE_FOREACH_RE (rn
, curr_re
) {
2212 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2214 if (curr_qindex
<= qindex
) {
2216 qindex
= curr_qindex
;
2223 /* Invariant: at this point we always have rn->info set. */
2224 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2225 RIB_ROUTE_QUEUED(qindex
))) {
2226 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2227 rnode_debug(rn
, re
->vrf_id
,
2228 "rn %p is already queued in sub-queue %u",
2229 (void *)rn
, qindex
);
2233 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2234 listnode_add(mq
->subq
[qindex
], rn
);
2235 route_lock_node(rn
);
2238 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2239 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2240 (void *)rn
, qindex
);
2242 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2244 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2247 /* Add route_node to work queue and schedule processing */
2248 void rib_queue_add(struct route_node
*rn
)
2252 /* Pointless to queue a route_node with no RIB entries to add or remove
2254 if (!rnode_to_ribs(rn
)) {
2255 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2256 __func__
, (void *)rn
, rn
->lock
);
2257 zlog_backtrace(LOG_DEBUG
);
2261 if (zebrad
.ribq
== NULL
) {
2262 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2263 "%s: work_queue does not exist!", __func__
);
2268 * The RIB queue should normally be either empty or holding the only
2269 * work_queue_item element. In the latter case this element would
2270 * hold a pointer to the meta queue structure, which must be used to
2271 * actually queue the route nodes to process. So create the MQ
2272 * holder, if necessary, then push the work into it in any case.
2273 * This semantics was introduced after 0.99.9 release.
2275 if (work_queue_empty(zebrad
.ribq
))
2276 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2278 rib_meta_queue_add(zebrad
.mq
, rn
);
2283 /* Create new meta queue.
2284 A destructor function doesn't seem to be necessary here.
2286 static struct meta_queue
*meta_queue_new(void)
2288 struct meta_queue
*new;
2291 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2293 for (i
= 0; i
< MQ_SIZE
; i
++) {
2294 new->subq
[i
] = list_new();
2295 assert(new->subq
[i
]);
2301 void meta_queue_free(struct meta_queue
*mq
)
2305 for (i
= 0; i
< MQ_SIZE
; i
++)
2306 list_delete(&mq
->subq
[i
]);
2308 XFREE(MTYPE_WORK_QUEUE
, mq
);
2311 /* initialise zebra rib work queue */
2312 static void rib_queue_init(struct zebra_t
*zebra
)
2317 work_queue_new(zebra
->master
, "route_node processing"))) {
2318 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2319 "%s: could not initialise work queue!", __func__
);
2323 /* fill in the work queue spec */
2324 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2325 zebra
->ribq
->spec
.errorfunc
= NULL
;
2326 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2327 /* XXX: TODO: These should be runtime configurable via vty */
2328 zebra
->ribq
->spec
.max_retries
= 3;
2329 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2330 zebra
->ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2332 if (!(zebra
->mq
= meta_queue_new())) {
2333 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2334 "%s: could not initialise meta queue!", __func__
);
2340 /* RIB updates are processed via a queue of pointers to route_nodes.
2342 * The queue length is bounded by the maximal size of the routing table,
2343 * as a route_node will not be requeued, if already queued.
2345 * REs are submitted via rib_addnode or rib_delnode which set minimal
2346 * state, or static_install_route (when an existing RE is updated)
2347 * and then submit route_node to queue for best-path selection later.
2348 * Order of add/delete state changes are preserved for any given RE.
2350 * Deleted REs are reaped during best-path selection.
2353 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2354 * |-------->| | best RE, if required
2356 * static_install->|->rib_addqueue...... -> rib_process
2358 * |-------->| |-> rib_unlink
2359 * |-> set ROUTE_ENTRY_REMOVE |
2360 * rib_delnode (RE freed)
2362 * The 'info' pointer of a route_node points to a rib_dest_t
2363 * ('dest'). Queueing state for a route_node is kept on the dest. The
2364 * dest is created on-demand by rib_link() and is kept around at least
2365 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2367 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2369 * - route_nodes: refcounted by:
2370 * - dest attached to route_node:
2371 * - managed by: rib_link/rib_gc_dest
2372 * - route_node processing queue
2373 * - managed by: rib_addqueue, rib_process.
2377 /* Add RE to head of the route node. */
2378 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2380 struct route_entry
*head
;
2383 const char *rmap_name
;
2387 dest
= rib_dest_from_rnode(rn
);
2389 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2390 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2392 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2393 route_lock_node(rn
); /* rn route table reference */
2398 head
= dest
->routes
;
2405 afi
= (rn
->p
.family
== AF_INET
)
2407 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2408 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2409 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2410 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2415 static void rib_addnode(struct route_node
*rn
,
2416 struct route_entry
*re
, int process
)
2418 /* RE node has been un-removed before route-node is processed.
2419 * route_node must hence already be on the queue for processing..
2421 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2422 if (IS_ZEBRA_DEBUG_RIB
)
2423 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2424 (void *)rn
, (void *)re
);
2426 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2429 rib_link(rn
, re
, process
);
2435 * Detach a rib structure from a route_node.
2437 * Note that a call to rib_unlink() should be followed by a call to
2438 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2439 * longer required to be deleted.
2441 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2447 if (IS_ZEBRA_DEBUG_RIB
)
2448 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2451 dest
= rib_dest_from_rnode(rn
);
2454 re
->next
->prev
= re
->prev
;
2457 re
->prev
->next
= re
->next
;
2459 dest
->routes
= re
->next
;
2462 if (dest
->selected_fib
== re
)
2463 dest
->selected_fib
= NULL
;
2465 nexthops_free(re
->ng
.nexthop
);
2466 XFREE(MTYPE_RE
, re
);
2469 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2473 if (IS_ZEBRA_DEBUG_RIB
)
2474 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2475 (void *)rn
, (void *)re
);
2476 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2478 afi
= (rn
->p
.family
== AF_INET
)
2480 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2481 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2482 zebra_del_import_table_entry(rn
, re
);
2483 /* Just clean up if non main table */
2484 if (IS_ZEBRA_DEBUG_RIB
) {
2485 char buf
[SRCDEST2STR_BUFFER
];
2486 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2488 "%u:%s: Freeing route rn %p, re %p (type %d)",
2489 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2498 /* This function dumps the contents of a given RE entry into
2499 * standard debug log. Calling function name and IP prefix in
2500 * question are passed as 1st and 2nd arguments.
2503 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2504 union prefixconstptr src_pp
,
2505 const struct route_entry
*re
)
2507 const struct prefix
*src_p
= src_pp
.p
;
2508 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2509 char straddr
[PREFIX_STRLEN
];
2510 char srcaddr
[PREFIX_STRLEN
];
2511 struct nexthop
*nexthop
;
2513 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2514 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2515 is_srcdst
? " from " : "",
2516 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2519 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2520 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2523 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2524 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2525 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2526 re
->nexthop_num
, re
->nexthop_active_num
);
2528 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2529 struct interface
*ifp
;
2530 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2532 switch (nexthop
->type
) {
2533 case NEXTHOP_TYPE_BLACKHOLE
:
2534 sprintf(straddr
, "Blackhole");
2536 case NEXTHOP_TYPE_IFINDEX
:
2537 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2539 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2541 case NEXTHOP_TYPE_IPV4
:
2543 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2544 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2547 case NEXTHOP_TYPE_IPV6
:
2548 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2549 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2553 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2554 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2555 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2557 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2560 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2563 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2567 zlog_debug("%s: dump complete", func
);
2570 /* This is an exported helper to rtm_read() to dump the strange
2571 * RE entry found by rib_lookup_ipv4_route()
2574 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2576 struct route_table
*table
;
2577 struct route_node
*rn
;
2578 struct route_entry
*re
;
2579 char prefix_buf
[INET_ADDRSTRLEN
];
2582 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2584 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2585 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2590 /* Scan the RIB table for exactly matching RE entry. */
2591 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2593 /* No route for this prefix. */
2595 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2596 prefix2str((struct prefix
*)p
, prefix_buf
,
2597 sizeof(prefix_buf
)));
2602 route_unlock_node(rn
);
2605 RNODE_FOREACH_RE (rn
, re
) {
2606 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2608 (void *)rn
, (void *)re
,
2609 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2612 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2615 route_entry_dump(p
, NULL
, re
);
2619 /* Check if requested address assignment will fail due to another
2620 * route being installed by zebra in FIB already. Take necessary
2621 * actions, if needed: remove such a route from FIB and deSELECT
2622 * corresponding RE entry. Then put affected RN into RIBQ head.
2624 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2626 struct route_table
*table
;
2627 struct route_node
*rn
;
2628 unsigned changed
= 0;
2631 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2632 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2633 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2638 /* No matches would be the simplest case. */
2639 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2643 route_unlock_node(rn
);
2645 dest
= rib_dest_from_rnode(rn
);
2646 /* Check all RE entries. In case any changes have to be done, requeue
2647 * the RN into RIBQ head. If the routing message about the new connected
2648 * route (generated by the IP address we are going to assign very soon)
2649 * comes before the RIBQ is processed, the new RE entry will join
2650 * RIBQ record already on head. This is necessary for proper
2652 * of the rest of the RE.
2654 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2656 if (IS_ZEBRA_DEBUG_RIB
) {
2657 char buf
[PREFIX_STRLEN
];
2659 zlog_debug("%u:%s: freeing way for connected prefix",
2660 dest
->selected_fib
->vrf_id
,
2661 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2662 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2664 rib_uninstall(rn
, dest
->selected_fib
);
2670 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2671 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2673 struct route_table
*table
;
2674 struct route_node
*rn
;
2675 struct route_entry
*same
= NULL
;
2676 struct nexthop
*nexthop
;
2682 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2685 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2687 XFREE(MTYPE_RE
, re
);
2691 /* Make it sure prefixlen is applied to the prefix. */
2694 apply_mask_ipv6(src_p
);
2696 /* Set default distance by route type. */
2697 if (re
->distance
== 0) {
2698 re
->distance
= route_distance(re
->type
);
2700 /* iBGP distance is 200. */
2701 if (re
->type
== ZEBRA_ROUTE_BGP
2702 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2706 /* Lookup route node.*/
2707 rn
= srcdest_rnode_get(table
, p
, src_p
);
2710 * If same type of route are installed, treat it as a implicit
2712 * If the user has specified the No route replace semantics
2713 * for the install don't do a route replace.
2715 RNODE_FOREACH_RE (rn
, same
) {
2716 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2719 if (same
->type
!= re
->type
)
2721 if (same
->instance
!= re
->instance
)
2723 if (same
->type
== ZEBRA_ROUTE_KERNEL
2724 && same
->metric
!= re
->metric
)
2727 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2728 same
->distance
!= re
->distance
)
2732 * We should allow duplicate connected routes
2733 * because of IPv6 link-local routes and unnumbered
2734 * interfaces on Linux.
2736 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2740 /* If this route is kernel route, set FIB flag to the route. */
2741 if (RIB_SYSTEM_ROUTE(re
)) {
2742 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2743 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2744 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2747 /* Link new re to node.*/
2748 if (IS_ZEBRA_DEBUG_RIB
) {
2751 "Inserting route rn %p, re %p (type %d) existing %p",
2752 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2754 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2755 route_entry_dump(p
, src_p
, re
);
2757 rib_addnode(rn
, re
, 1);
2760 /* Free implicit route.*/
2762 rib_delnode(rn
, same
);
2766 route_unlock_node(rn
);
2770 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2771 unsigned short instance
, int flags
, struct prefix
*p
,
2772 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2773 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2776 struct route_table
*table
;
2777 struct route_node
*rn
;
2778 struct route_entry
*re
;
2779 struct route_entry
*fib
= NULL
;
2780 struct route_entry
*same
= NULL
;
2781 struct nexthop
*rtnh
;
2782 char buf2
[INET6_ADDRSTRLEN
];
2785 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2788 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2795 apply_mask_ipv6(src_p
);
2797 /* Lookup route node. */
2798 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2800 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2802 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2803 if (src_p
&& src_p
->prefixlen
)
2804 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2808 if (IS_ZEBRA_DEBUG_RIB
)
2809 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2811 (src_buf
[0] != '\0') ? " from " : "",
2816 dest
= rib_dest_from_rnode(rn
);
2817 fib
= dest
->selected_fib
;
2819 /* Lookup same type route. */
2820 RNODE_FOREACH_RE (rn
, re
) {
2821 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2824 if (re
->type
!= type
)
2826 if (re
->instance
!= instance
)
2828 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2829 distance
!= re
->distance
)
2832 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2834 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2835 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2836 if (rtnh
->ifindex
!= nh
->ifindex
)
2841 /* Make sure that the route found has the same gateway. */
2847 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2848 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2856 /* If same type of route can't be found and this message is from
2860 * In the past(HA!) we could get here because
2861 * we were receiving a route delete from the
2862 * kernel and we're not marking the proto
2863 * as coming from it's appropriate originator.
2864 * Now that we are properly noticing the fact
2865 * that the kernel has deleted our route we
2866 * are not going to get called in this path
2867 * I am going to leave this here because
2868 * this might still work this way on non-linux
2869 * platforms as well as some weird state I have
2870 * not properly thought of yet.
2871 * If we can show that this code path is
2872 * dead then we can remove it.
2874 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2875 if (IS_ZEBRA_DEBUG_RIB
) {
2878 "rn %p, re %p (type %d) was deleted from kernel, adding",
2879 rn
, fib
, fib
->type
);
2882 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2884 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2886 UNSET_FLAG(rtnh
->flags
,
2890 * This is a non FRR route
2891 * as such we should mark
2894 dest
->selected_fib
= NULL
;
2896 /* This means someone else, other than Zebra,
2898 * a Zebra router from the kernel. We will add
2900 rib_install_kernel(rn
, fib
, NULL
);
2903 if (IS_ZEBRA_DEBUG_RIB
) {
2907 "via %s ifindex %d type %d "
2908 "doesn't exist in rib",
2909 inet_ntop(afi2family(afi
),
2916 "type %d doesn't exist in rib",
2919 route_unlock_node(rn
);
2925 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2927 rib_install_kernel(rn
, same
, NULL
);
2928 route_unlock_node(rn
);
2933 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2934 struct nexthop
*tmp_nh
;
2936 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2937 struct ipaddr vtep_ip
;
2939 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2940 if (afi
== AFI_IP
) {
2941 vtep_ip
.ipa_type
= IPADDR_V4
;
2942 memcpy(&(vtep_ip
.ipaddr_v4
),
2943 &(tmp_nh
->gate
.ipv4
),
2944 sizeof(struct in_addr
));
2946 vtep_ip
.ipa_type
= IPADDR_V6
;
2947 memcpy(&(vtep_ip
.ipaddr_v6
),
2948 &(tmp_nh
->gate
.ipv6
),
2949 sizeof(struct in6_addr
));
2951 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2955 rib_delnode(rn
, same
);
2958 route_unlock_node(rn
);
2963 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2964 unsigned short instance
, int flags
, struct prefix
*p
,
2965 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2966 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2969 struct route_entry
*re
;
2970 struct nexthop
*nexthop
;
2972 /* Allocate new route_entry structure. */
2973 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2975 re
->instance
= instance
;
2976 re
->distance
= distance
;
2978 re
->metric
= metric
;
2980 re
->table
= table_id
;
2981 re
->vrf_id
= vrf_id
;
2982 re
->nexthop_num
= 0;
2983 re
->uptime
= time(NULL
);
2987 nexthop
= nexthop_new();
2989 route_entry_nexthop_add(re
, nexthop
);
2991 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2994 /* Schedule routes of a particular table (address-family) based on event. */
2995 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2997 struct route_node
*rn
;
2998 struct route_entry
*re
, *next
;
3000 /* Walk all routes and queue for processing, if appropriate for
3001 * the trigger event.
3003 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3005 * If we are looking at a route node and the node
3006 * has already been queued we don't
3007 * need to queue it up again
3009 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3010 RIB_ROUTE_ANY_QUEUED
))
3013 case RIB_UPDATE_IF_CHANGE
:
3014 /* Examine all routes that won't get processed by the
3016 * triggered by nexthop evaluation (NHT). This would be
3018 * kernel and certain static routes. Note that NHT will
3020 * triggered upon an interface event as connected routes
3022 * get queued for processing.
3024 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3027 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3028 && re
->type
!= ZEBRA_ROUTE_KERNEL
3029 && re
->type
!= ZEBRA_ROUTE_CONNECT
3030 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3033 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3038 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3039 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3040 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3043 /* If we only have nexthops to a
3052 case RIB_UPDATE_RMAP_CHANGE
:
3053 case RIB_UPDATE_OTHER
:
3054 /* Right now, examine all routes. Can restrict to a
3056 * some cases (TODO).
3058 if (rnode_to_ribs(rn
))
3068 /* RIB update function. */
3069 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3071 struct route_table
*table
;
3073 /* Process routes of interested address-families. */
3074 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3076 if (IS_ZEBRA_DEBUG_EVENT
)
3077 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3078 rib_update_table(table
, event
);
3081 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3083 if (IS_ZEBRA_DEBUG_EVENT
)
3084 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3085 rib_update_table(table
, event
);
3089 /* Delete self installed routes after zebra is relaunched. */
3090 void rib_sweep_table(struct route_table
*table
)
3092 struct route_node
*rn
;
3093 struct route_entry
*re
;
3094 struct route_entry
*next
;
3095 struct nexthop
*nexthop
;
3100 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3101 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3102 if (IS_ZEBRA_DEBUG_RIB
)
3103 route_entry_dump(&rn
->p
, NULL
, re
);
3105 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3108 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3112 * So we are starting up and have received
3113 * routes from the kernel that we have installed
3114 * from a previous run of zebra but not cleaned
3115 * up ( say a kill -9 )
3116 * But since we haven't actually installed
3117 * them yet( we received them from the kernel )
3118 * we don't think they are active.
3119 * So let's pretend they are active to actually
3121 * In all honesty I'm not sure if we should
3122 * mark them as active when we receive them
3123 * This is startup only so probably ok.
3125 * If we ever decide to move rib_sweep_table
3126 * to a different spot (ie startup )
3127 * this decision needs to be revisited
3129 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3130 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3131 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3133 rib_uninstall_kernel(rn
, re
);
3134 rib_delnode(rn
, re
);
3139 /* Sweep all RIB tables. */
3140 void rib_sweep_route(void)
3143 struct zebra_vrf
*zvrf
;
3145 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3146 if ((zvrf
= vrf
->info
) == NULL
)
3149 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3150 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3153 zebra_router_sweep_route();
3156 /* Remove specific by protocol routes from 'table'. */
3157 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3158 struct route_table
*table
)
3160 struct route_node
*rn
;
3161 struct route_entry
*re
;
3162 struct route_entry
*next
;
3163 unsigned long n
= 0;
3166 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3167 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3168 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3170 if (re
->type
== proto
3171 && re
->instance
== instance
) {
3172 rib_delnode(rn
, re
);
3179 /* Remove specific by protocol routes. */
3180 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3183 struct zebra_vrf
*zvrf
;
3184 unsigned long cnt
= 0;
3186 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3187 if ((zvrf
= vrf
->info
) != NULL
)
3188 cnt
+= rib_score_proto_table(
3190 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3191 + rib_score_proto_table(
3193 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3195 cnt
+= zebra_router_score_proto(proto
, instance
);
3200 /* Close RIB and clean up kernel routes. */
3201 void rib_close_table(struct route_table
*table
)
3203 struct route_node
*rn
;
3204 rib_table_info_t
*info
;
3210 info
= route_table_get_info(table
);
3212 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3213 dest
= rib_dest_from_rnode(rn
);
3215 if (dest
&& dest
->selected_fib
) {
3216 if (info
->safi
== SAFI_UNICAST
)
3217 hook_call(rib_update
, rn
, NULL
);
3219 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
3220 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3221 dest
->selected_fib
= NULL
;
3228 * Handle results from the dataplane system. Dequeue update context
3229 * structs, dispatch to appropriate internal handlers.
3231 static int rib_process_dplane_results(struct thread
*thread
)
3233 struct zebra_dplane_ctx
*ctx
;
3234 struct dplane_ctx_q ctxlist
;
3236 /* Dequeue a list of completed updates with one lock/unlock cycle */
3238 /* TODO -- dequeue a list with one lock/unlock cycle? */
3241 TAILQ_INIT(&ctxlist
);
3243 /* Take lock controlling queue of results */
3244 pthread_mutex_lock(&dplane_mutex
);
3246 /* Dequeue list of context structs */
3247 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3249 pthread_mutex_unlock(&dplane_mutex
);
3251 /* Dequeue context block */
3252 ctx
= dplane_ctx_dequeue(&ctxlist
);
3254 /* If we've emptied the results queue, we're done */
3259 switch (dplane_ctx_get_op(ctx
)) {
3260 case DPLANE_OP_ROUTE_INSTALL
:
3261 case DPLANE_OP_ROUTE_UPDATE
:
3262 case DPLANE_OP_ROUTE_DELETE
:
3263 rib_process_result(ctx
);
3266 case DPLANE_OP_LSP_INSTALL
:
3267 case DPLANE_OP_LSP_UPDATE
:
3268 case DPLANE_OP_LSP_DELETE
:
3269 zebra_mpls_lsp_dplane_result(ctx
);
3273 /* Don't expect this: just return the struct? */
3274 dplane_ctx_fini(&ctx
);
3276 } /* Dispatch by op code */
3278 ctx
= dplane_ctx_dequeue(&ctxlist
);
3283 /* Check for nexthop tracking processing after finishing with results */
3284 do_nht_processing();
3290 * Results are returned from the dataplane subsystem, in the context of
3291 * the dataplane pthread. We enqueue the results here for processing by
3292 * the main thread later.
3294 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3296 /* Take lock controlling queue of results */
3297 pthread_mutex_lock(&dplane_mutex
);
3299 /* Enqueue context blocks */
3300 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3302 pthread_mutex_unlock(&dplane_mutex
);
3304 /* Ensure event is signalled to zebra main pthread */
3305 thread_add_event(zebrad
.master
, rib_process_dplane_results
, NULL
, 0,
3311 /* Routing information base initialize. */
3314 rib_queue_init(&zebrad
);
3316 /* Init dataplane, and register for results */
3317 pthread_mutex_init(&dplane_mutex
, NULL
);
3318 TAILQ_INIT(&rib_dplane_q
);
3319 zebra_dplane_init(rib_dplane_results
);
3325 * Get the first vrf id that is greater than the given vrf id if any.
3327 * Returns TRUE if a vrf id was found, FALSE otherwise.
3329 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3333 vrf
= vrf_lookup_by_id(vrf_id
);
3335 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3337 *next_id_p
= vrf
->vrf_id
;
3346 * rib_tables_iter_next
3348 * Returns the next table in the iteration.
3350 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3352 struct route_table
*table
;
3355 * Array that helps us go over all AFI/SAFI combinations via one
3362 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3363 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3364 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3369 switch (iter
->state
) {
3371 case RIB_TABLES_ITER_S_INIT
:
3372 iter
->vrf_id
= VRF_DEFAULT
;
3373 iter
->afi_safi_ix
= -1;
3377 case RIB_TABLES_ITER_S_ITERATING
:
3378 iter
->afi_safi_ix
++;
3381 while (iter
->afi_safi_ix
3382 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3383 table
= zebra_vrf_table(
3384 afi_safis
[iter
->afi_safi_ix
].afi
,
3385 afi_safis
[iter
->afi_safi_ix
].safi
,
3390 iter
->afi_safi_ix
++;
3394 * Found another table in this vrf.
3400 * Done with all tables in the current vrf, go to the
3404 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3407 iter
->afi_safi_ix
= 0;
3412 case RIB_TABLES_ITER_S_DONE
:
3417 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3419 iter
->state
= RIB_TABLES_ITER_S_DONE
;