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
== zrouter
.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
))
441 * If the kernel has sent us a route, then
442 * by golly gee whiz it's a good route.
444 if (re
->type
== ZEBRA_ROUTE_KERNEL
||
445 re
->type
== ZEBRA_ROUTE_SYSTEM
)
448 /* Skip nexthops that have been filtered out due to route-map */
449 /* The nexthops are specific to this route and so the same */
450 /* nexthop for a different route may not have this flag set */
451 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
)) {
452 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
453 zlog_debug("\t%s: Nexthop Filtered",
454 __PRETTY_FUNCTION__
);
459 * Check to see if we should trust the passed in information
460 * for UNNUMBERED interfaces as that we won't find the GW
461 * address in the routing table.
463 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
464 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
466 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
468 "\t%s: Onlink and interface: %u[%u] does not exist",
469 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
473 if (connected_is_unnumbered(ifp
)) {
474 if (if_is_operative(ifp
))
477 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
479 "\t%s: Onlink and interface %s is not operative",
480 __PRETTY_FUNCTION__
, ifp
->name
);
484 if (!if_is_operative(ifp
)) {
485 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
487 "\t%s: Interface %s is not unnumbered",
489 ifp
? ifp
->name
: "Unknown");
494 /* Make lookup prefix. */
495 memset(&p
, 0, sizeof(struct prefix
));
499 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
500 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
504 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
505 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
508 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
512 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
514 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
515 zlog_debug("\t%s: Table not found",
516 __PRETTY_FUNCTION__
);
520 rn
= route_node_match(table
, (struct prefix
*)&p
);
522 route_unlock_node(rn
);
524 /* Lookup should halt if we've matched against ourselves ('top',
525 * if specified) - i.e., we cannot have a nexthop NH1 is
526 * resolved by a route NH1. The exception is if the route is a
529 if (top
&& rn
== top
)
530 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
531 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
532 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
534 "\t%s: Matched against ourself and prefix length is not max bit length",
535 __PRETTY_FUNCTION__
);
539 /* Pick up selected route. */
540 /* However, do not resolve over default route unless explicitly
542 if (is_default_prefix(&rn
->p
)
543 && !rnh_resolve_via_default(p
.family
)) {
544 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
546 "\t:%s: Resolved against default route",
547 __PRETTY_FUNCTION__
);
551 dest
= rib_dest_from_rnode(rn
);
552 if (dest
&& dest
->selected_fib
553 && !CHECK_FLAG(dest
->selected_fib
->status
,
555 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
556 match
= dest
->selected_fib
;
558 /* If there is no selected route or matched route is EGP, go up
563 } while (rn
&& rn
->info
== NULL
);
570 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
571 /* Directly point connected route. */
572 newhop
= match
->ng
.nexthop
;
574 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
575 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
576 nexthop
->ifindex
= newhop
->ifindex
;
579 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
581 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
582 if (!CHECK_FLAG(match
->status
,
583 ROUTE_ENTRY_INSTALLED
))
585 if (CHECK_FLAG(newhop
->flags
,
586 NEXTHOP_FLAG_RECURSIVE
))
590 SET_FLAG(nexthop
->flags
,
591 NEXTHOP_FLAG_RECURSIVE
);
593 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
594 nexthop_set_resolved(afi
, newhop
,
600 re
->nexthop_mtu
= match
->mtu
;
601 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
602 zlog_debug("\t%s: Recursion failed to find",
603 __PRETTY_FUNCTION__
);
605 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
607 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
608 if (!CHECK_FLAG(match
->status
,
609 ROUTE_ENTRY_INSTALLED
))
611 if (CHECK_FLAG(newhop
->flags
,
612 NEXTHOP_FLAG_RECURSIVE
))
616 SET_FLAG(nexthop
->flags
,
617 NEXTHOP_FLAG_RECURSIVE
);
618 nexthop_set_resolved(afi
, newhop
,
624 re
->nexthop_mtu
= match
->mtu
;
626 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
628 "\t%s: Static route unable to resolve",
629 __PRETTY_FUNCTION__
);
632 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
633 zlog_debug("\t%s: Route Type %s has not turned on recursion",
635 zebra_route_string(re
->type
));
636 if (re
->type
== ZEBRA_ROUTE_BGP
&&
637 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
638 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
643 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
644 zlog_debug("\t%s: Nexthop did not lookup in table",
645 __PRETTY_FUNCTION__
);
649 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
650 union g_addr
*addr
, struct route_node
**rn_out
)
653 struct route_table
*table
;
654 struct route_node
*rn
;
655 struct route_entry
*match
= NULL
;
658 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
662 memset(&p
, 0, sizeof(struct prefix
));
665 p
.u
.prefix4
= addr
->ipv4
;
666 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
668 p
.u
.prefix6
= addr
->ipv6
;
669 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
672 rn
= route_node_match(table
, (struct prefix
*)&p
);
677 route_unlock_node(rn
);
679 dest
= rib_dest_from_rnode(rn
);
680 if (dest
&& dest
->selected_fib
681 && !CHECK_FLAG(dest
->selected_fib
->status
,
682 ROUTE_ENTRY_REMOVED
))
683 match
= dest
->selected_fib
;
685 /* If there is no selected route or matched route is EGP, go up
690 } while (rn
&& rn
->info
== NULL
);
694 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
695 if (!CHECK_FLAG(match
->status
,
696 ROUTE_ENTRY_INSTALLED
))
708 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
710 struct route_node
**rn_out
)
712 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
713 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
714 union g_addr gaddr
= {.ipv4
= addr
};
716 switch (ipv4_multicast_mode
) {
717 case MCAST_MRIB_ONLY
:
718 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
720 case MCAST_URIB_ONLY
:
721 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
722 case MCAST_NO_CONFIG
:
723 case MCAST_MIX_MRIB_FIRST
:
724 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
727 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
730 case MCAST_MIX_DISTANCE
:
731 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
732 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
734 re
= ure
->distance
< mre
->distance
? ure
: mre
;
740 case MCAST_MIX_PFXLEN
:
741 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
742 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
744 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
753 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
755 if (IS_ZEBRA_DEBUG_RIB
) {
757 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
759 zlog_debug("%s: %s: vrf: %u found %s, using %s",
760 __func__
, buf
, vrf_id
,
761 mre
? (ure
? "MRIB+URIB" : "MRIB")
762 : ure
? "URIB" : "nothing",
763 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
768 void multicast_mode_ipv4_set(enum multicast_mode mode
)
770 if (IS_ZEBRA_DEBUG_RIB
)
771 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
773 ipv4_multicast_mode
= mode
;
776 enum multicast_mode
multicast_mode_ipv4_get(void)
778 return ipv4_multicast_mode
;
781 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
783 struct route_table
*table
;
784 struct route_node
*rn
;
785 struct route_entry
*match
= NULL
;
789 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
793 rn
= route_node_lookup(table
, (struct prefix
*)p
);
795 /* No route for this prefix. */
800 route_unlock_node(rn
);
801 dest
= rib_dest_from_rnode(rn
);
803 if (dest
&& dest
->selected_fib
804 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
805 match
= dest
->selected_fib
;
810 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
813 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
819 #define RIB_SYSTEM_ROUTE(R) \
820 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
822 #define RIB_KERNEL_ROUTE(R) \
823 ((R)->type == ZEBRA_ROUTE_KERNEL)
825 /* This function verifies reachability of one given nexthop, which can be
826 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
827 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
828 * nexthop->ifindex will be updated appropriately as well.
829 * An existing route map can turn (otherwise active) nexthop into inactive, but
832 * The return value is the final value of 'ACTIVE' flag.
835 static unsigned nexthop_active_check(struct route_node
*rn
,
836 struct route_entry
*re
,
837 struct nexthop
*nexthop
, bool set
)
839 struct interface
*ifp
;
840 route_map_result_t ret
= RMAP_MATCH
;
842 char buf
[SRCDEST2STR_BUFFER
];
843 const struct prefix
*p
, *src_p
;
844 struct zebra_vrf
*zvrf
;
846 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
848 if (rn
->p
.family
== AF_INET
)
850 else if (rn
->p
.family
== AF_INET6
)
854 switch (nexthop
->type
) {
855 case NEXTHOP_TYPE_IFINDEX
:
856 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
857 if (ifp
&& if_is_operative(ifp
))
858 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
860 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
862 case NEXTHOP_TYPE_IPV4
:
863 case NEXTHOP_TYPE_IPV4_IFINDEX
:
865 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
866 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
868 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 case NEXTHOP_TYPE_IPV6
:
872 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
873 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
875 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
877 case NEXTHOP_TYPE_IPV6_IFINDEX
:
878 /* RFC 5549, v4 prefix with v6 NH */
879 if (rn
->p
.family
!= AF_INET
)
881 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
882 ifp
= if_lookup_by_index(nexthop
->ifindex
,
884 if (ifp
&& if_is_operative(ifp
))
885 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
887 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
889 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
890 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
892 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
895 case NEXTHOP_TYPE_BLACKHOLE
:
896 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
901 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
902 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
903 zlog_debug("\t%s: Unable to find a active nexthop",
904 __PRETTY_FUNCTION__
);
908 /* XXX: What exactly do those checks do? Do we support
909 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
911 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
912 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
913 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
915 /* The original code didn't determine the family correctly
916 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
917 * from the rib_table_info in those cases.
918 * Possibly it may be better to use only the rib_table_info
922 rib_table_info_t
*info
;
924 info
= srcdest_rnode_table_info(rn
);
928 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
930 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
932 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
933 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
934 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
937 /* It'll get set if required inside */
938 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
939 nexthop
, zvrf
, re
->tag
);
940 if (ret
== RMAP_DENYMATCH
) {
941 if (IS_ZEBRA_DEBUG_RIB
) {
942 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
944 "%u:%s: Filtering out with NH out %s due to route map",
946 ifindex2ifname(nexthop
->ifindex
,
949 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
951 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
954 /* Iterate over all nexthops of the given RIB entry and refresh their
955 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
956 * nexthop is found to toggle the ACTIVE flag, the whole re structure
957 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
958 * transparently passed to nexthop_active_check().
960 * Return value is the new number of active nexthops.
963 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
966 struct nexthop
*nexthop
;
967 union g_addr prev_src
;
968 unsigned int prev_active
, new_active
, old_num_nh
;
969 ifindex_t prev_index
;
971 old_num_nh
= re
->nexthop_active_num
;
973 re
->nexthop_active_num
= 0;
974 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
976 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
977 /* No protocol daemon provides src and so we're skipping
979 prev_src
= nexthop
->rmap_src
;
980 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
981 prev_index
= nexthop
->ifindex
;
983 * We need to respect the multipath_num here
984 * as that what we should be able to install from
985 * a multipath perpsective should not be a data plane
988 new_active
= nexthop_active_check(rn
, re
, nexthop
, set
);
989 if (new_active
&& re
->nexthop_active_num
>= multipath_num
) {
990 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
994 re
->nexthop_active_num
++;
995 /* Don't allow src setting on IPv6 addr for now */
996 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
997 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
998 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
999 && prev_src
.ipv4
.s_addr
1000 != nexthop
->rmap_src
.ipv4
.s_addr
)
1001 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
1002 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
1003 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
1004 &nexthop
->rmap_src
.ipv6
)))) {
1005 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1006 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1010 if (old_num_nh
!= re
->nexthop_active_num
)
1011 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1013 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1014 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1017 return re
->nexthop_active_num
;
1021 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1022 * (nexthops) must have a label.
1024 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1026 struct nexthop
*nexthop
= NULL
;
1028 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1031 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1032 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1038 /* Update flag indicates whether this is a "replace" or not. Currently, this
1039 * is only used for IPv4.
1041 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1042 struct route_entry
*old
)
1044 struct nexthop
*nexthop
;
1045 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1046 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1047 const struct prefix
*p
, *src_p
;
1048 enum zebra_dplane_result ret
;
1050 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1052 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1054 if (info
->safi
!= SAFI_UNICAST
) {
1055 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1056 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1059 struct nexthop
*prev
;
1061 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1062 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1063 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1064 if (prev
== nexthop
)
1066 if (nexthop_same_firsthop(nexthop
, prev
)) {
1067 SET_FLAG(nexthop
->flags
,
1068 NEXTHOP_FLAG_DUPLICATE
);
1076 * If this is a replace to a new RE let the originator of the RE
1077 * know that they've lost
1079 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1080 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1082 /* Update fib selection */
1083 dest
->selected_fib
= re
;
1086 * Make sure we update the FPM any time we send new information to
1089 hook_call(rib_update
, rn
, "installing in kernel");
1091 /* Send add or update */
1093 ret
= dplane_route_update(rn
, re
, old
);
1095 ret
= dplane_route_add(rn
, re
);
1098 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1099 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1101 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
1103 zvrf
->installs_queued
++;
1105 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1107 char str
[SRCDEST2STR_BUFFER
];
1109 srcdest_rnode2str(rn
, str
, sizeof(str
));
1110 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1111 "%u:%s: Failed to enqueue dataplane install",
1115 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1124 /* Uninstall the route from kernel. */
1125 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1127 struct nexthop
*nexthop
;
1128 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1129 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1131 if (info
->safi
!= SAFI_UNICAST
) {
1132 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1133 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1134 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1139 * Make sure we update the FPM any time we send new information to
1142 hook_call(rib_update
, rn
, "uninstalling from kernel");
1144 switch (dplane_route_delete(rn
, re
)) {
1145 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1147 zvrf
->removals_queued
++;
1149 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1151 char str
[SRCDEST2STR_BUFFER
];
1153 srcdest_rnode2str(rn
, str
, sizeof(str
));
1154 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1155 "%u:%s: Failed to enqueue dataplane uninstall",
1159 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1168 /* Uninstall the route from kernel. */
1169 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1171 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1172 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1173 struct nexthop
*nexthop
;
1175 if (dest
&& dest
->selected_fib
== re
) {
1176 if (info
->safi
== SAFI_UNICAST
)
1177 hook_call(rib_update
, rn
, "rib_uninstall");
1179 /* If labeled-unicast route, uninstall transit LSP. */
1180 if (zebra_rib_labeled_unicast(re
))
1181 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1183 if (!RIB_SYSTEM_ROUTE(re
))
1184 rib_uninstall_kernel(rn
, re
);
1186 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1188 dest
->selected_fib
= NULL
;
1190 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1191 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1194 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1195 const struct prefix
*p
, *src_p
;
1197 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1199 redistribute_delete(p
, src_p
, re
);
1200 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1205 * rib_can_delete_dest
1207 * Returns TRUE if the given dest can be deleted from the table.
1209 static int rib_can_delete_dest(rib_dest_t
*dest
)
1216 * Don't delete the dest if we have to update the FPM about this
1219 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1220 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1229 * Garbage collect the rib dest corresponding to the given route node
1232 * Returns TRUE if the dest was deleted, FALSE otherwise.
1234 int rib_gc_dest(struct route_node
*rn
)
1238 dest
= rib_dest_from_rnode(rn
);
1242 if (!rib_can_delete_dest(dest
))
1245 if (IS_ZEBRA_DEBUG_RIB
) {
1246 struct zebra_vrf
*zvrf
;
1248 zvrf
= rib_dest_vrf(dest
);
1249 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1253 XFREE(MTYPE_RIB_DEST
, dest
);
1257 * Release the one reference that we keep on the route node.
1259 route_unlock_node(rn
);
1263 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1264 struct route_entry
*new)
1266 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1268 hook_call(rib_update
, rn
, "new route selected");
1270 /* Update real nexthop. This may actually determine if nexthop is active
1272 if (!nexthop_active_update(rn
, new, true)) {
1273 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1277 if (IS_ZEBRA_DEBUG_RIB
) {
1278 char buf
[SRCDEST2STR_BUFFER
];
1279 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1280 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1281 zvrf_id(zvrf
), buf
, rn
, new,
1282 zebra_route_string(new->type
));
1285 /* If labeled-unicast route, install transit LSP. */
1286 if (zebra_rib_labeled_unicast(new))
1287 zebra_mpls_lsp_install(zvrf
, rn
, new);
1289 if (!RIB_SYSTEM_ROUTE(new))
1290 rib_install_kernel(rn
, new, NULL
);
1292 dest
->selected_fib
= new;
1294 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1297 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1298 struct route_entry
*old
)
1300 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1301 hook_call(rib_update
, rn
, "removing existing route");
1303 /* Uninstall from kernel. */
1304 if (IS_ZEBRA_DEBUG_RIB
) {
1305 char buf
[SRCDEST2STR_BUFFER
];
1306 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1307 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1308 zvrf_id(zvrf
), buf
, rn
, old
,
1309 zebra_route_string(old
->type
));
1312 /* If labeled-unicast route, uninstall transit LSP. */
1313 if (zebra_rib_labeled_unicast(old
))
1314 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1316 if (!RIB_SYSTEM_ROUTE(old
))
1317 rib_uninstall_kernel(rn
, old
);
1319 UNSET_FLAG(old
->status
, ROUTE_ENTRY_INSTALLED
);
1321 * We are setting this to NULL here
1322 * because that is what we traditionally
1323 * have been doing. I am not positive
1324 * that this is the right thing to do
1325 * but let's leave the code alone
1326 * for the RIB_SYSTEM_ROUTE case
1328 dest
->selected_fib
= NULL
;
1331 /* Update nexthop for route, reset changed flag. */
1332 /* Note: this code also handles the Linux case when an interface goes
1333 * down, causing the kernel to delete routes without sending DELROUTE
1336 if (!nexthop_active_update(rn
, old
, true) &&
1337 (RIB_KERNEL_ROUTE(old
)))
1338 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1340 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1343 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1344 struct route_node
*rn
,
1345 struct route_entry
*old
,
1346 struct route_entry
*new)
1348 struct nexthop
*nexthop
= NULL
;
1350 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1353 * We have to install or update if a new route has been selected or
1354 * something has changed.
1356 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1357 hook_call(rib_update
, rn
, "updating existing route");
1359 /* Update the nexthop; we could determine here that nexthop is
1361 if (nexthop_active_update(rn
, new, true))
1364 /* If nexthop is active, install the selected route, if
1366 * the install succeeds, cleanup flags for prior route, if
1371 if (IS_ZEBRA_DEBUG_RIB
) {
1372 char buf
[SRCDEST2STR_BUFFER
];
1373 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1376 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
1377 zvrf_id(zvrf
), buf
, rn
, new,
1378 zebra_route_string(new->type
),
1380 zebra_route_string(old
->type
));
1383 "%u:%s: Updating route rn %p, re %p (%s)",
1384 zvrf_id(zvrf
), buf
, rn
, new,
1385 zebra_route_string(new->type
));
1388 /* If labeled-unicast route, uninstall transit LSP. */
1389 if (zebra_rib_labeled_unicast(old
))
1390 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1392 /* Non-system route should be installed. */
1393 if (!RIB_SYSTEM_ROUTE(new)) {
1394 /* If labeled-unicast route, install transit
1396 if (zebra_rib_labeled_unicast(new))
1397 zebra_mpls_lsp_install(zvrf
, rn
, new);
1399 rib_install_kernel(rn
, new, old
);
1401 UNSET_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
);
1403 * We do not need to install the
1404 * selected route because it
1405 * is already isntalled by
1406 * the system( ie not us )
1407 * so just mark it as winning
1408 * we do need to ensure that
1409 * if we uninstall a route
1410 * from ourselves we don't
1411 * over write this pointer
1413 dest
->selected_fib
= NULL
;
1415 /* If install succeeded or system route, cleanup flags
1416 * for prior route. */
1418 if (RIB_SYSTEM_ROUTE(new)) {
1419 if (!RIB_SYSTEM_ROUTE(old
))
1420 rib_uninstall_kernel(rn
, old
);
1424 ROUTE_ENTRY_INSTALLED
);
1426 UNSET_FLAG(old
->status
,
1427 ROUTE_ENTRY_INSTALLED
);
1428 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1429 nexthop
= nexthop
->next
)
1430 UNSET_FLAG(nexthop
->flags
,
1437 * If nexthop for selected route is not active or install
1439 * may need to uninstall and delete for redistribution.
1442 if (IS_ZEBRA_DEBUG_RIB
) {
1443 char buf
[SRCDEST2STR_BUFFER
];
1444 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1447 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1448 zvrf_id(zvrf
), buf
, rn
, new,
1449 zebra_route_string(new->type
),
1451 zebra_route_string(old
->type
));
1454 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
1455 zvrf_id(zvrf
), buf
, rn
, new,
1456 zebra_route_string(new->type
));
1459 /* If labeled-unicast route, uninstall transit LSP. */
1460 if (zebra_rib_labeled_unicast(old
))
1461 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1463 if (!RIB_SYSTEM_ROUTE(old
))
1464 rib_uninstall_kernel(rn
, old
);
1466 UNSET_FLAG(old
->status
, ROUTE_ENTRY_INSTALLED
);
1467 dest
->selected_fib
= NULL
;
1472 * Same route selected; check if in the FIB and if not,
1474 * is housekeeping code to deal with race conditions in kernel
1476 * netlink reporting interface up before IPv4 or IPv6 protocol
1480 if (!RIB_SYSTEM_ROUTE(new)
1481 && !CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
))
1482 rib_install_kernel(rn
, new, NULL
);
1485 /* Update prior route. */
1487 /* Set real nexthop. */
1488 nexthop_active_update(rn
, old
, true);
1489 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1492 /* Clear changed flag. */
1493 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1496 /* Check if 'alternate' RIB entry is better than 'current'. */
1497 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1498 struct route_entry
*alternate
)
1500 if (current
== NULL
)
1503 /* filter route selection in following order:
1504 * - connected beats other types
1505 * - if both connected, loopback or vrf wins
1506 * - lower distance beats higher
1507 * - lower metric beats higher for equal distance
1508 * - last, hence oldest, route wins tie break.
1511 /* Connected routes. Check to see if either are a vrf
1512 * or loopback interface. If not, pick the last connected
1513 * route of the set of lowest metric connected routes.
1515 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1516 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1519 /* both are connected. are either loop or vrf? */
1520 struct nexthop
*nexthop
= NULL
;
1522 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1523 if (if_is_loopback_or_vrf(if_lookup_by_index(
1524 nexthop
->ifindex
, alternate
->vrf_id
)))
1528 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1529 if (if_is_loopback_or_vrf(if_lookup_by_index(
1530 nexthop
->ifindex
, current
->vrf_id
)))
1534 /* Neither are loop or vrf so pick best metric */
1535 if (alternate
->metric
<= current
->metric
)
1541 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1544 /* higher distance loses */
1545 if (alternate
->distance
< current
->distance
)
1547 if (current
->distance
< alternate
->distance
)
1550 /* metric tie-breaks equal distance */
1551 if (alternate
->metric
<= current
->metric
)
1557 /* Core function for processing routing information base. */
1558 static void rib_process(struct route_node
*rn
)
1560 struct route_entry
*re
;
1561 struct route_entry
*next
;
1562 struct route_entry
*old_selected
= NULL
;
1563 struct route_entry
*new_selected
= NULL
;
1564 struct route_entry
*old_fib
= NULL
;
1565 struct route_entry
*new_fib
= NULL
;
1566 struct route_entry
*best
= NULL
;
1567 char buf
[SRCDEST2STR_BUFFER
];
1569 struct zebra_vrf
*zvrf
= NULL
;
1570 const struct prefix
*p
, *src_p
;
1572 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1573 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1577 dest
= rib_dest_from_rnode(rn
);
1579 zvrf
= rib_dest_vrf(dest
);
1580 vrf_id
= zvrf_id(zvrf
);
1583 if (IS_ZEBRA_DEBUG_RIB
)
1584 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1586 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1587 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1590 * we can have rn's that have a NULL info pointer
1591 * (dest). As such let's not let the deref happen
1592 * additionally we know RNODE_FOREACH_RE_SAFE
1593 * will not iterate so we are ok.
1596 old_fib
= dest
->selected_fib
;
1598 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1599 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1601 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1602 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1603 re
->status
, re
->flags
, re
->distance
,
1606 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1608 /* Currently selected re. */
1609 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1610 assert(old_selected
== NULL
);
1614 /* Skip deleted entries from selection */
1615 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1618 /* Skip unreachable nexthop. */
1619 /* This first call to nexthop_active_update is merely to
1621 * there's any change to nexthops associated with this RIB
1623 * rib_process() can be invoked due to an external event such as
1625 * down or due to next-hop-tracking evaluation. In the latter
1627 * a decision has already been made that the NHs have changed.
1629 * need to invoke a potentially expensive call again. Further,
1631 * the change might be in a recursive NH which is not caught in
1632 * the nexthop_active_update() code. Thus, we might miss changes
1636 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1637 && !nexthop_active_update(rn
, re
, false)) {
1638 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1639 /* XXX: HERE BE DRAGONS!!!!!
1640 * In all honesty, I have not yet figured out
1642 * does or why the ROUTE_ENTRY_CHANGED test
1644 * or why we need to delete a route here, and
1646 * this concerns both selected and fib route, or
1649 /* This entry was denied by the 'ip protocol
1650 * table' route-map, we
1651 * need to delete it */
1652 if (re
!= old_selected
) {
1653 if (IS_ZEBRA_DEBUG_RIB
)
1655 "%s: %u:%s: imported via import-table but denied "
1656 "by the ip protocol table route-map",
1657 __func__
, vrf_id
, buf
);
1660 SET_FLAG(re
->status
,
1661 ROUTE_ENTRY_REMOVED
);
1667 /* Infinite distance. */
1668 if (re
->distance
== DISTANCE_INFINITY
) {
1669 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1673 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1674 best
= rib_choose_best(new_fib
, re
);
1675 if (new_fib
&& best
!= new_fib
)
1676 UNSET_FLAG(new_fib
->status
,
1677 ROUTE_ENTRY_CHANGED
);
1680 best
= rib_choose_best(new_selected
, re
);
1681 if (new_selected
&& best
!= new_selected
)
1682 UNSET_FLAG(new_selected
->status
,
1683 ROUTE_ENTRY_CHANGED
);
1684 new_selected
= best
;
1687 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1688 } /* RNODE_FOREACH_RE */
1690 /* If no FIB override route, use the selected route also for FIB */
1691 if (new_fib
== NULL
)
1692 new_fib
= new_selected
;
1694 /* After the cycle is finished, the following pointers will be set:
1695 * old_selected --- RE entry currently having SELECTED
1696 * new_selected --- RE entry that is newly SELECTED
1697 * old_fib --- RE entry currently in kernel FIB
1698 * new_fib --- RE entry that is newly to be in kernel FIB
1700 * new_selected will get SELECTED flag, and is going to be redistributed
1701 * the zclients. new_fib (which can be new_selected) will be installed
1705 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1707 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1708 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1709 (void *)old_fib
, (void *)new_fib
);
1712 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1713 * fib == selected */
1714 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1715 ROUTE_ENTRY_CHANGED
);
1717 /* Update fib according to selection results */
1718 if (new_fib
&& old_fib
)
1719 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1721 rib_process_add_fib(zvrf
, rn
, new_fib
);
1723 rib_process_del_fib(zvrf
, rn
, old_fib
);
1725 /* Update SELECTED entry */
1726 if (old_selected
!= new_selected
|| selected_changed
) {
1728 if (new_selected
&& new_selected
!= new_fib
) {
1729 nexthop_active_update(rn
, new_selected
, true);
1730 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1734 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1736 /* Special case: new route is system route, so
1737 * dataplane update will not be done - ensure we
1738 * redistribute the route.
1740 if (RIB_SYSTEM_ROUTE(new_selected
))
1741 redistribute_update(p
, src_p
, new_selected
,
1747 redistribute_delete(p
, src_p
, old_selected
);
1748 if (old_selected
!= new_selected
)
1749 UNSET_FLAG(old_selected
->flags
,
1750 ZEBRA_FLAG_SELECTED
);
1754 /* Remove all RE entries queued for removal */
1755 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1756 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1757 if (IS_ZEBRA_DEBUG_RIB
) {
1758 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1759 (void *)rn
, (void *)re
);
1766 * Check if the dest can be deleted now.
1772 * Utility to match route with dplane context data
1774 static bool rib_route_match_ctx(const struct route_entry
*re
,
1775 const struct zebra_dplane_ctx
*ctx
,
1778 bool result
= false;
1782 * In 'update' case, we test info about the 'previous' or
1785 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1786 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1789 /* TODO -- we're using this extra test, but it's not
1790 * exactly clear why.
1792 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1793 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1794 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1801 * Ordinary, single-route case using primary context info
1803 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1804 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1805 /* Skip route that's been deleted */
1809 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1810 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1813 /* TODO -- we're using this extra test, but it's not
1814 * exactly clear why.
1816 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1817 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1818 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1830 * Route-update results processing after async dataplane update.
1832 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1834 struct route_table
*table
= NULL
;
1835 struct zebra_vrf
*zvrf
= NULL
;
1836 struct route_node
*rn
= NULL
;
1837 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1838 bool is_update
= false;
1839 struct nexthop
*nexthop
, *ctx_nexthop
;
1840 char dest_str
[PREFIX_STRLEN
] = "";
1841 enum dplane_op_e op
;
1842 enum zebra_dplane_result status
;
1843 const struct prefix
*dest_pfx
, *src_pfx
;
1845 /* Locate rn and re(s) from ctx */
1847 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1848 dplane_ctx_get_safi(ctx
),
1849 dplane_ctx_get_vrf(ctx
),
1850 dplane_ctx_get_table(ctx
));
1851 if (table
== NULL
) {
1852 if (IS_ZEBRA_DEBUG_DPLANE
) {
1853 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1854 dplane_ctx_get_afi(ctx
),
1855 dplane_ctx_get_safi(ctx
),
1856 dplane_ctx_get_vrf(ctx
));
1861 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1863 dest_pfx
= dplane_ctx_get_dest(ctx
);
1865 /* Note well: only capturing the prefix string if debug is enabled here;
1866 * unconditional log messages will have to generate the string.
1868 if (IS_ZEBRA_DEBUG_DPLANE
)
1869 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1871 src_pfx
= dplane_ctx_get_src(ctx
);
1872 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1873 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1875 if (IS_ZEBRA_DEBUG_DPLANE
) {
1876 zlog_debug("Failed to process dplane results: no route for %u:%s",
1877 dplane_ctx_get_vrf(ctx
), dest_str
);
1882 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1884 op
= dplane_ctx_get_op(ctx
);
1885 status
= dplane_ctx_get_status(ctx
);
1887 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1888 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1889 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1890 dplane_op2str(op
), dplane_res2str(status
));
1893 * Update is a bit of a special case, where we may have both old and new
1894 * routes to post-process.
1896 is_update
= dplane_ctx_is_update(ctx
);
1899 * Take a pass through the routes, look for matches with the context
1902 RNODE_FOREACH_RE(rn
, rib
) {
1905 if (rib_route_match_ctx(rib
, ctx
, false))
1909 /* Check for old route match */
1910 if (is_update
&& (old_re
== NULL
)) {
1911 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1915 /* Have we found the routes we need to work on? */
1916 if (re
&& ((!is_update
|| old_re
)))
1921 * Check sequence number(s) to detect stale results before continuing
1924 if (re
->dplane_sequence
!= dplane_ctx_get_seq(ctx
)) {
1925 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1926 zlog_debug("%u:%s Stale dplane result for re %p",
1927 dplane_ctx_get_vrf(ctx
),
1930 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1934 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1935 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1936 zlog_debug("%u:%s Stale dplane result for old_re %p",
1937 dplane_ctx_get_vrf(ctx
),
1940 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1944 case DPLANE_OP_ROUTE_INSTALL
:
1945 case DPLANE_OP_ROUTE_UPDATE
:
1946 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1948 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1949 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1952 * On an update operation from the same route type
1953 * context retrieval currently has no way to know
1954 * which was the old and which was the new.
1955 * So don't unset our flags that we just set.
1956 * We know redistribution is ok because the
1957 * old_re in this case is used for nothing
1958 * more than knowing whom to contact if necessary.
1960 if (old_re
&& old_re
!= re
) {
1961 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1962 UNSET_FLAG(old_re
->status
,
1963 ROUTE_ENTRY_INSTALLED
);
1965 /* Update zebra nexthop FIB flag for each
1966 * nexthop that was installed.
1968 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1974 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1975 if (nexthop_same(ctx_nexthop
, nexthop
))
1979 if (nexthop
== NULL
)
1982 if (CHECK_FLAG(nexthop
->flags
,
1983 NEXTHOP_FLAG_RECURSIVE
))
1986 if (CHECK_FLAG(ctx_nexthop
->flags
,
1988 SET_FLAG(nexthop
->flags
,
1991 UNSET_FLAG(nexthop
->flags
,
1997 /* Set flag for nexthop tracking processing */
1998 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2003 * TODO -- still calling the redist api using the
2004 * route_entries, and there's a corner-case here:
2005 * if there's no client for the 'new' route, a redist
2006 * deleting the 'old' route will be sent. But if the
2007 * 'old' context info was stale, 'old_re' will be
2008 * NULL here and that delete will not be sent.
2011 redistribute_update(dest_pfx
, src_pfx
,
2014 /* Notify route owner */
2015 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2019 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2021 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2023 zsend_route_notify_owner(re
, dest_pfx
,
2024 ZAPI_ROUTE_FAIL_INSTALL
);
2026 zlog_warn("%u:%s: Route install failed",
2027 dplane_ctx_get_vrf(ctx
),
2028 prefix2str(dest_pfx
,
2029 dest_str
, sizeof(dest_str
)));
2032 case DPLANE_OP_ROUTE_DELETE
:
2034 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2036 * In the delete case, the zebra core datastructs were
2037 * updated (or removed) at the time the delete was issued,
2038 * so we're just notifying the route owner.
2040 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2042 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2043 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2045 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2051 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2052 zsend_route_notify_owner_ctx(ctx
,
2053 ZAPI_ROUTE_REMOVE_FAIL
);
2055 zlog_warn("%u:%s: Route Deletion failure",
2056 dplane_ctx_get_vrf(ctx
),
2057 prefix2str(dest_pfx
,
2058 dest_str
, sizeof(dest_str
)));
2067 route_unlock_node(rn
);
2069 /* Return context to dataplane module */
2070 dplane_ctx_fini(&ctx
);
2073 /* Take a list of route_node structs and return 1, if there was a record
2074 * picked from it and processed by rib_process(). Don't process more,
2075 * than one RN record; operate only in the specified sub-queue.
2077 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2079 struct listnode
*lnode
= listhead(subq
);
2080 struct route_node
*rnode
;
2082 struct zebra_vrf
*zvrf
= NULL
;
2087 rnode
= listgetdata(lnode
);
2088 dest
= rib_dest_from_rnode(rnode
);
2090 zvrf
= rib_dest_vrf(dest
);
2094 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2095 char buf
[SRCDEST2STR_BUFFER
];
2096 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2097 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2098 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2102 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2103 RIB_ROUTE_QUEUED(qindex
));
2108 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2109 __func__
, rnode
, rnode
->lock
);
2110 zlog_backtrace(LOG_DEBUG
);
2113 route_unlock_node(rnode
);
2114 list_delete_node(subq
, lnode
);
2119 * Perform next-hop tracking processing after RIB updates.
2121 static void do_nht_processing(void)
2124 struct zebra_vrf
*zvrf
;
2126 /* Evaluate nexthops for those VRFs which underwent route processing.
2128 * should limit the evaluation to the necessary VRFs in most common
2131 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2133 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2136 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHT
)
2137 zlog_debug("NHT processing check for zvrf %s",
2140 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2141 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2142 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_IMPORT_CHECK_TYPE
,
2144 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2145 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_IMPORT_CHECK_TYPE
,
2149 /* Schedule LSPs for processing, if needed. */
2150 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2151 if (mpls_should_lsps_be_processed(zvrf
)) {
2152 if (IS_ZEBRA_DEBUG_MPLS
)
2154 "%u: Scheduling all LSPs upon RIB completion",
2156 zebra_mpls_lsp_schedule(zvrf
);
2157 mpls_unmark_lsps_for_processing(zvrf
);
2161 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2162 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2164 * is pointed to the meta queue structure.
2166 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2168 struct meta_queue
*mq
= data
;
2170 uint32_t queue_len
, queue_limit
;
2172 /* Ensure there's room for more dataplane updates */
2173 queue_limit
= dplane_get_in_queue_limit();
2174 queue_len
= dplane_get_in_queue_len();
2175 if (queue_len
> queue_limit
) {
2176 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2177 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2178 queue_len
, queue_limit
);
2180 /* Ensure that the meta-queue is actually enqueued */
2181 if (work_queue_empty(zrouter
.ribq
))
2182 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2184 return WQ_QUEUE_BLOCKED
;
2187 for (i
= 0; i
< MQ_SIZE
; i
++)
2188 if (process_subq(mq
->subq
[i
], i
)) {
2192 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2197 * Look into the RN and queue it into the highest priority queue
2198 * at this point in time for processing.
2200 * We will enqueue a route node only once per invocation.
2202 * There are two possibilities here that should be kept in mind.
2203 * If the original invocation has not been pulled off for processing
2204 * yet, A subsuquent invocation can have a route entry with a better
2205 * meta queue index value and we can have a situation where
2206 * we might have the same node enqueued 2 times. Not necessarily
2207 * an optimal situation but it should be ok.
2209 * The other possibility is that the original invocation has not
2210 * been pulled off for processing yet, A subsusquent invocation
2211 * doesn't have a route_entry with a better meta-queue and the
2212 * original metaqueue index value will win and we'll end up with
2213 * the route node enqueued once.
2215 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2217 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2218 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2219 struct zebra_vrf
*zvrf
;
2221 RNODE_FOREACH_RE (rn
, curr_re
) {
2222 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2224 if (curr_qindex
<= qindex
) {
2226 qindex
= curr_qindex
;
2233 /* Invariant: at this point we always have rn->info set. */
2234 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2235 RIB_ROUTE_QUEUED(qindex
))) {
2236 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2237 rnode_debug(rn
, re
->vrf_id
,
2238 "rn %p is already queued in sub-queue %u",
2239 (void *)rn
, qindex
);
2243 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2244 listnode_add(mq
->subq
[qindex
], rn
);
2245 route_lock_node(rn
);
2248 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2249 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2250 (void *)rn
, qindex
);
2252 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2254 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2257 /* Add route_node to work queue and schedule processing */
2258 void rib_queue_add(struct route_node
*rn
)
2262 /* Pointless to queue a route_node with no RIB entries to add or remove
2264 if (!rnode_to_ribs(rn
)) {
2265 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2266 __func__
, (void *)rn
, rn
->lock
);
2267 zlog_backtrace(LOG_DEBUG
);
2271 if (zrouter
.ribq
== NULL
) {
2272 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2273 "%s: work_queue does not exist!", __func__
);
2278 * The RIB queue should normally be either empty or holding the only
2279 * work_queue_item element. In the latter case this element would
2280 * hold a pointer to the meta queue structure, which must be used to
2281 * actually queue the route nodes to process. So create the MQ
2282 * holder, if necessary, then push the work into it in any case.
2283 * This semantics was introduced after 0.99.9 release.
2285 if (work_queue_empty(zrouter
.ribq
))
2286 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2288 rib_meta_queue_add(zrouter
.mq
, rn
);
2293 /* Create new meta queue.
2294 A destructor function doesn't seem to be necessary here.
2296 static struct meta_queue
*meta_queue_new(void)
2298 struct meta_queue
*new;
2301 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2303 for (i
= 0; i
< MQ_SIZE
; i
++) {
2304 new->subq
[i
] = list_new();
2305 assert(new->subq
[i
]);
2311 void meta_queue_free(struct meta_queue
*mq
)
2315 for (i
= 0; i
< MQ_SIZE
; i
++)
2316 list_delete(&mq
->subq
[i
]);
2318 XFREE(MTYPE_WORK_QUEUE
, mq
);
2321 /* initialise zebra rib work queue */
2322 static void rib_queue_init(void)
2324 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2325 "route_node processing"))) {
2326 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2327 "%s: could not initialise work queue!", __func__
);
2331 /* fill in the work queue spec */
2332 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2333 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2334 zrouter
.ribq
->spec
.completion_func
= NULL
;
2335 /* XXX: TODO: These should be runtime configurable via vty */
2336 zrouter
.ribq
->spec
.max_retries
= 3;
2337 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2338 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2340 if (!(zrouter
.mq
= meta_queue_new())) {
2341 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2342 "%s: could not initialise meta queue!", __func__
);
2348 /* RIB updates are processed via a queue of pointers to route_nodes.
2350 * The queue length is bounded by the maximal size of the routing table,
2351 * as a route_node will not be requeued, if already queued.
2353 * REs are submitted via rib_addnode or rib_delnode which set minimal
2354 * state, or static_install_route (when an existing RE is updated)
2355 * and then submit route_node to queue for best-path selection later.
2356 * Order of add/delete state changes are preserved for any given RE.
2358 * Deleted REs are reaped during best-path selection.
2361 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2362 * |-------->| | best RE, if required
2364 * static_install->|->rib_addqueue...... -> rib_process
2366 * |-------->| |-> rib_unlink
2367 * |-> set ROUTE_ENTRY_REMOVE |
2368 * rib_delnode (RE freed)
2370 * The 'info' pointer of a route_node points to a rib_dest_t
2371 * ('dest'). Queueing state for a route_node is kept on the dest. The
2372 * dest is created on-demand by rib_link() and is kept around at least
2373 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2375 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2377 * - route_nodes: refcounted by:
2378 * - dest attached to route_node:
2379 * - managed by: rib_link/rib_gc_dest
2380 * - route_node processing queue
2381 * - managed by: rib_addqueue, rib_process.
2385 /* Add RE to head of the route node. */
2386 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2388 struct route_entry
*head
;
2391 const char *rmap_name
;
2395 dest
= rib_dest_from_rnode(rn
);
2397 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2398 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2400 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2401 route_lock_node(rn
); /* rn route table reference */
2406 head
= dest
->routes
;
2413 afi
= (rn
->p
.family
== AF_INET
)
2415 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2416 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2417 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2418 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2423 static void rib_addnode(struct route_node
*rn
,
2424 struct route_entry
*re
, int process
)
2426 /* RE node has been un-removed before route-node is processed.
2427 * route_node must hence already be on the queue for processing..
2429 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2430 if (IS_ZEBRA_DEBUG_RIB
)
2431 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2432 (void *)rn
, (void *)re
);
2434 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2437 rib_link(rn
, re
, process
);
2443 * Detach a rib structure from a route_node.
2445 * Note that a call to rib_unlink() should be followed by a call to
2446 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2447 * longer required to be deleted.
2449 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2455 if (IS_ZEBRA_DEBUG_RIB
)
2456 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2459 dest
= rib_dest_from_rnode(rn
);
2462 re
->next
->prev
= re
->prev
;
2465 re
->prev
->next
= re
->next
;
2467 dest
->routes
= re
->next
;
2470 if (dest
->selected_fib
== re
)
2471 dest
->selected_fib
= NULL
;
2473 nexthops_free(re
->ng
.nexthop
);
2474 XFREE(MTYPE_RE
, re
);
2477 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2481 if (IS_ZEBRA_DEBUG_RIB
)
2482 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2483 (void *)rn
, (void *)re
);
2484 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2486 afi
= (rn
->p
.family
== AF_INET
)
2488 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2489 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2490 zebra_del_import_table_entry(rn
, re
);
2491 /* Just clean up if non main table */
2492 if (IS_ZEBRA_DEBUG_RIB
) {
2493 char buf
[SRCDEST2STR_BUFFER
];
2494 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2495 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2496 re
->vrf_id
, buf
, rn
, re
,
2497 zebra_route_string(re
->type
));
2506 /* This function dumps the contents of a given RE entry into
2507 * standard debug log. Calling function name and IP prefix in
2508 * question are passed as 1st and 2nd arguments.
2511 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2512 union prefixconstptr src_pp
,
2513 const struct route_entry
*re
)
2515 const struct prefix
*src_p
= src_pp
.p
;
2516 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2517 char straddr
[PREFIX_STRLEN
];
2518 char srcaddr
[PREFIX_STRLEN
];
2519 struct nexthop
*nexthop
;
2521 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2522 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2523 is_srcdst
? " from " : "",
2524 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2527 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2528 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2531 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2532 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2533 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2534 re
->nexthop_num
, re
->nexthop_active_num
);
2536 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2537 struct interface
*ifp
;
2538 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2540 switch (nexthop
->type
) {
2541 case NEXTHOP_TYPE_BLACKHOLE
:
2542 sprintf(straddr
, "Blackhole");
2544 case NEXTHOP_TYPE_IFINDEX
:
2545 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2547 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2549 case NEXTHOP_TYPE_IPV4
:
2551 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2552 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2555 case NEXTHOP_TYPE_IPV6
:
2556 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2557 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2561 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2562 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2563 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2565 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2568 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2571 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2575 zlog_debug("%s: dump complete", func
);
2578 /* This is an exported helper to rtm_read() to dump the strange
2579 * RE entry found by rib_lookup_ipv4_route()
2582 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2584 struct route_table
*table
;
2585 struct route_node
*rn
;
2586 struct route_entry
*re
;
2587 char prefix_buf
[INET_ADDRSTRLEN
];
2590 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2592 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2593 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2598 /* Scan the RIB table for exactly matching RE entry. */
2599 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2601 /* No route for this prefix. */
2603 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2604 prefix2str((struct prefix
*)p
, prefix_buf
,
2605 sizeof(prefix_buf
)));
2610 route_unlock_node(rn
);
2613 RNODE_FOREACH_RE (rn
, re
) {
2614 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2616 (void *)rn
, (void *)re
,
2617 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2620 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2623 route_entry_dump(p
, NULL
, re
);
2627 /* Check if requested address assignment will fail due to another
2628 * route being installed by zebra in FIB already. Take necessary
2629 * actions, if needed: remove such a route from FIB and deSELECT
2630 * corresponding RE entry. Then put affected RN into RIBQ head.
2632 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2634 struct route_table
*table
;
2635 struct route_node
*rn
;
2636 unsigned changed
= 0;
2639 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2640 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2641 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2646 /* No matches would be the simplest case. */
2647 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2651 route_unlock_node(rn
);
2653 dest
= rib_dest_from_rnode(rn
);
2654 /* Check all RE entries. In case any changes have to be done, requeue
2655 * the RN into RIBQ head. If the routing message about the new connected
2656 * route (generated by the IP address we are going to assign very soon)
2657 * comes before the RIBQ is processed, the new RE entry will join
2658 * RIBQ record already on head. This is necessary for proper
2660 * of the rest of the RE.
2662 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2664 if (IS_ZEBRA_DEBUG_RIB
) {
2665 char buf
[PREFIX_STRLEN
];
2667 zlog_debug("%u:%s: freeing way for connected prefix",
2668 dest
->selected_fib
->vrf_id
,
2669 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2670 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2672 rib_uninstall(rn
, dest
->selected_fib
);
2678 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2679 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2681 struct route_table
*table
;
2682 struct route_node
*rn
;
2683 struct route_entry
*same
= NULL
;
2684 struct nexthop
*nexthop
;
2690 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2693 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2695 XFREE(MTYPE_RE
, re
);
2699 /* Make it sure prefixlen is applied to the prefix. */
2702 apply_mask_ipv6(src_p
);
2704 /* Set default distance by route type. */
2705 if (re
->distance
== 0) {
2706 re
->distance
= route_distance(re
->type
);
2708 /* iBGP distance is 200. */
2709 if (re
->type
== ZEBRA_ROUTE_BGP
2710 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2714 /* Lookup route node.*/
2715 rn
= srcdest_rnode_get(table
, p
, src_p
);
2718 * If same type of route are installed, treat it as a implicit
2720 * If the user has specified the No route replace semantics
2721 * for the install don't do a route replace.
2723 RNODE_FOREACH_RE (rn
, same
) {
2724 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2727 if (same
->type
!= re
->type
)
2729 if (same
->instance
!= re
->instance
)
2731 if (same
->type
== ZEBRA_ROUTE_KERNEL
2732 && same
->metric
!= re
->metric
)
2735 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2736 same
->distance
!= re
->distance
)
2740 * We should allow duplicate connected routes
2741 * because of IPv6 link-local routes and unnumbered
2742 * interfaces on Linux.
2744 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2748 /* If this route is kernel route, set FIB flag to the route. */
2749 if (RIB_SYSTEM_ROUTE(re
)) {
2750 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2751 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2752 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2755 /* Link new re to node.*/
2756 if (IS_ZEBRA_DEBUG_RIB
) {
2757 rnode_debug(rn
, re
->vrf_id
,
2758 "Inserting route rn %p, re %p (%s) existing %p",
2759 rn
, re
, zebra_route_string(re
->type
), same
);
2761 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2762 route_entry_dump(p
, src_p
, re
);
2764 rib_addnode(rn
, re
, 1);
2767 /* Free implicit route.*/
2769 rib_delnode(rn
, same
);
2773 route_unlock_node(rn
);
2777 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2778 unsigned short instance
, int flags
, struct prefix
*p
,
2779 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2780 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2783 struct route_table
*table
;
2784 struct route_node
*rn
;
2785 struct route_entry
*re
;
2786 struct route_entry
*fib
= NULL
;
2787 struct route_entry
*same
= NULL
;
2788 struct nexthop
*rtnh
;
2789 char buf2
[INET6_ADDRSTRLEN
];
2792 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2795 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2802 apply_mask_ipv6(src_p
);
2804 /* Lookup route node. */
2805 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2807 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2809 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2810 if (src_p
&& src_p
->prefixlen
)
2811 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2815 if (IS_ZEBRA_DEBUG_RIB
)
2816 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2818 (src_buf
[0] != '\0') ? " from " : "",
2823 dest
= rib_dest_from_rnode(rn
);
2824 fib
= dest
->selected_fib
;
2826 /* Lookup same type route. */
2827 RNODE_FOREACH_RE (rn
, re
) {
2828 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2831 if (re
->type
!= type
)
2833 if (re
->instance
!= instance
)
2835 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2836 distance
!= re
->distance
)
2839 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2841 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2842 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2843 if (rtnh
->ifindex
!= nh
->ifindex
)
2848 /* Make sure that the route found has the same gateway. */
2854 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2855 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2863 /* If same type of route can't be found and this message is from
2867 * In the past(HA!) we could get here because
2868 * we were receiving a route delete from the
2869 * kernel and we're not marking the proto
2870 * as coming from it's appropriate originator.
2871 * Now that we are properly noticing the fact
2872 * that the kernel has deleted our route we
2873 * are not going to get called in this path
2874 * I am going to leave this here because
2875 * this might still work this way on non-linux
2876 * platforms as well as some weird state I have
2877 * not properly thought of yet.
2878 * If we can show that this code path is
2879 * dead then we can remove it.
2881 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2882 if (IS_ZEBRA_DEBUG_RIB
) {
2883 rnode_debug(rn
, vrf_id
,
2884 "rn %p, re %p (%s) was deleted from kernel, adding",
2886 zebra_route_string(fib
->type
));
2889 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2891 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2893 UNSET_FLAG(rtnh
->flags
,
2897 * This is a non FRR route
2898 * as such we should mark
2901 dest
->selected_fib
= NULL
;
2903 /* This means someone else, other than Zebra,
2905 * a Zebra router from the kernel. We will add
2907 rib_install_kernel(rn
, fib
, NULL
);
2910 if (IS_ZEBRA_DEBUG_RIB
) {
2914 "via %s ifindex %d type %d "
2915 "doesn't exist in rib",
2916 inet_ntop(afi2family(afi
),
2923 "type %d doesn't exist in rib",
2926 route_unlock_node(rn
);
2932 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2934 rib_install_kernel(rn
, same
, NULL
);
2935 route_unlock_node(rn
);
2940 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2941 struct nexthop
*tmp_nh
;
2943 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2944 struct ipaddr vtep_ip
;
2946 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2947 if (afi
== AFI_IP
) {
2948 vtep_ip
.ipa_type
= IPADDR_V4
;
2949 memcpy(&(vtep_ip
.ipaddr_v4
),
2950 &(tmp_nh
->gate
.ipv4
),
2951 sizeof(struct in_addr
));
2953 vtep_ip
.ipa_type
= IPADDR_V6
;
2954 memcpy(&(vtep_ip
.ipaddr_v6
),
2955 &(tmp_nh
->gate
.ipv6
),
2956 sizeof(struct in6_addr
));
2958 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2962 rib_delnode(rn
, same
);
2965 route_unlock_node(rn
);
2970 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2971 unsigned short instance
, int flags
, struct prefix
*p
,
2972 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2973 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2976 struct route_entry
*re
;
2977 struct nexthop
*nexthop
;
2979 /* Allocate new route_entry structure. */
2980 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2982 re
->instance
= instance
;
2983 re
->distance
= distance
;
2985 re
->metric
= metric
;
2987 re
->table
= table_id
;
2988 re
->vrf_id
= vrf_id
;
2989 re
->nexthop_num
= 0;
2990 re
->uptime
= time(NULL
);
2994 nexthop
= nexthop_new();
2996 route_entry_nexthop_add(re
, nexthop
);
2998 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
3001 /* Schedule routes of a particular table (address-family) based on event. */
3002 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3004 struct route_node
*rn
;
3005 struct route_entry
*re
, *next
;
3007 /* Walk all routes and queue for processing, if appropriate for
3008 * the trigger event.
3010 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3012 * If we are looking at a route node and the node
3013 * has already been queued we don't
3014 * need to queue it up again
3016 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3017 RIB_ROUTE_ANY_QUEUED
))
3020 case RIB_UPDATE_IF_CHANGE
:
3021 /* Examine all routes that won't get processed by the
3023 * triggered by nexthop evaluation (NHT). This would be
3025 * kernel and certain static routes. Note that NHT will
3027 * triggered upon an interface event as connected routes
3029 * get queued for processing.
3031 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3034 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3035 && re
->type
!= ZEBRA_ROUTE_KERNEL
3036 && re
->type
!= ZEBRA_ROUTE_CONNECT
3037 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3040 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3045 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3046 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3047 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3050 /* If we only have nexthops to a
3059 case RIB_UPDATE_RMAP_CHANGE
:
3060 case RIB_UPDATE_OTHER
:
3061 /* Right now, examine all routes. Can restrict to a
3063 * some cases (TODO).
3065 if (rnode_to_ribs(rn
))
3075 /* RIB update function. */
3076 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3078 struct route_table
*table
;
3080 /* Process routes of interested address-families. */
3081 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3083 if (IS_ZEBRA_DEBUG_EVENT
)
3084 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3085 rib_update_table(table
, event
);
3088 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3090 if (IS_ZEBRA_DEBUG_EVENT
)
3091 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3092 rib_update_table(table
, event
);
3096 /* Delete self installed routes after zebra is relaunched. */
3097 void rib_sweep_table(struct route_table
*table
)
3099 struct route_node
*rn
;
3100 struct route_entry
*re
;
3101 struct route_entry
*next
;
3102 struct nexthop
*nexthop
;
3107 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3108 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3109 if (IS_ZEBRA_DEBUG_RIB
)
3110 route_entry_dump(&rn
->p
, NULL
, re
);
3112 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3115 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3119 * So we are starting up and have received
3120 * routes from the kernel that we have installed
3121 * from a previous run of zebra but not cleaned
3122 * up ( say a kill -9 )
3123 * But since we haven't actually installed
3124 * them yet( we received them from the kernel )
3125 * we don't think they are active.
3126 * So let's pretend they are active to actually
3128 * In all honesty I'm not sure if we should
3129 * mark them as active when we receive them
3130 * This is startup only so probably ok.
3132 * If we ever decide to move rib_sweep_table
3133 * to a different spot (ie startup )
3134 * this decision needs to be revisited
3136 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3137 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3138 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3140 rib_uninstall_kernel(rn
, re
);
3141 rib_delnode(rn
, re
);
3146 /* Sweep all RIB tables. */
3147 void rib_sweep_route(void)
3150 struct zebra_vrf
*zvrf
;
3152 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3153 if ((zvrf
= vrf
->info
) == NULL
)
3156 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3157 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3160 zebra_router_sweep_route();
3163 /* Remove specific by protocol routes from 'table'. */
3164 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3165 struct route_table
*table
)
3167 struct route_node
*rn
;
3168 struct route_entry
*re
;
3169 struct route_entry
*next
;
3170 unsigned long n
= 0;
3173 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3174 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3175 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3177 if (re
->type
== proto
3178 && re
->instance
== instance
) {
3179 rib_delnode(rn
, re
);
3186 /* Remove specific by protocol routes. */
3187 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3190 struct zebra_vrf
*zvrf
;
3191 unsigned long cnt
= 0;
3193 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3194 if ((zvrf
= vrf
->info
) != NULL
)
3195 cnt
+= rib_score_proto_table(
3197 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3198 + rib_score_proto_table(
3200 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3202 cnt
+= zebra_router_score_proto(proto
, instance
);
3207 /* Close RIB and clean up kernel routes. */
3208 void rib_close_table(struct route_table
*table
)
3210 struct route_node
*rn
;
3211 rib_table_info_t
*info
;
3217 info
= route_table_get_info(table
);
3219 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3220 dest
= rib_dest_from_rnode(rn
);
3222 if (dest
&& dest
->selected_fib
) {
3223 if (info
->safi
== SAFI_UNICAST
)
3224 hook_call(rib_update
, rn
, NULL
);
3226 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
3227 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3228 dest
->selected_fib
= NULL
;
3235 * Handler for async dataplane results after a pseudowire installation
3237 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3239 struct zebra_pw
*pw
;
3240 struct zebra_vrf
*vrf
;
3242 /* The pseudowire code assumes success - we act on an error
3243 * result for installation attempts here.
3245 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3248 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3249 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3250 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3252 zebra_pw_install_failure(pw
);
3262 * Handle results from the dataplane system. Dequeue update context
3263 * structs, dispatch to appropriate internal handlers.
3265 static int rib_process_dplane_results(struct thread
*thread
)
3267 struct zebra_dplane_ctx
*ctx
;
3268 struct dplane_ctx_q ctxlist
;
3270 /* Dequeue a list of completed updates with one lock/unlock cycle */
3273 TAILQ_INIT(&ctxlist
);
3275 /* Take lock controlling queue of results */
3276 pthread_mutex_lock(&dplane_mutex
);
3278 /* Dequeue list of context structs */
3279 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3281 pthread_mutex_unlock(&dplane_mutex
);
3283 /* Dequeue context block */
3284 ctx
= dplane_ctx_dequeue(&ctxlist
);
3286 /* If we've emptied the results queue, we're done */
3291 switch (dplane_ctx_get_op(ctx
)) {
3292 case DPLANE_OP_ROUTE_INSTALL
:
3293 case DPLANE_OP_ROUTE_UPDATE
:
3294 case DPLANE_OP_ROUTE_DELETE
:
3295 rib_process_result(ctx
);
3298 case DPLANE_OP_LSP_INSTALL
:
3299 case DPLANE_OP_LSP_UPDATE
:
3300 case DPLANE_OP_LSP_DELETE
:
3301 zebra_mpls_lsp_dplane_result(ctx
);
3304 case DPLANE_OP_PW_INSTALL
:
3305 case DPLANE_OP_PW_UNINSTALL
:
3306 handle_pw_result(ctx
);
3310 /* Don't expect this: just return the struct? */
3311 dplane_ctx_fini(&ctx
);
3313 } /* Dispatch by op code */
3315 ctx
= dplane_ctx_dequeue(&ctxlist
);
3320 /* Check for nexthop tracking processing after finishing with results */
3321 do_nht_processing();
3327 * Results are returned from the dataplane subsystem, in the context of
3328 * the dataplane pthread. We enqueue the results here for processing by
3329 * the main thread later.
3331 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3333 /* Take lock controlling queue of results */
3334 pthread_mutex_lock(&dplane_mutex
);
3336 /* Enqueue context blocks */
3337 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3339 pthread_mutex_unlock(&dplane_mutex
);
3341 /* Ensure event is signalled to zebra main pthread */
3342 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3348 /* Routing information base initialize. */
3353 /* Init dataplane, and register for results */
3354 pthread_mutex_init(&dplane_mutex
, NULL
);
3355 TAILQ_INIT(&rib_dplane_q
);
3356 zebra_dplane_init(rib_dplane_results
);
3362 * Get the first vrf id that is greater than the given vrf id if any.
3364 * Returns TRUE if a vrf id was found, FALSE otherwise.
3366 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3370 vrf
= vrf_lookup_by_id(vrf_id
);
3372 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3374 *next_id_p
= vrf
->vrf_id
;
3383 * rib_tables_iter_next
3385 * Returns the next table in the iteration.
3387 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3389 struct route_table
*table
;
3392 * Array that helps us go over all AFI/SAFI combinations via one
3399 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3400 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3401 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3406 switch (iter
->state
) {
3408 case RIB_TABLES_ITER_S_INIT
:
3409 iter
->vrf_id
= VRF_DEFAULT
;
3410 iter
->afi_safi_ix
= -1;
3414 case RIB_TABLES_ITER_S_ITERATING
:
3415 iter
->afi_safi_ix
++;
3418 while (iter
->afi_safi_ix
3419 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3420 table
= zebra_vrf_table(
3421 afi_safis
[iter
->afi_safi_ix
].afi
,
3422 afi_safis
[iter
->afi_safi_ix
].safi
,
3427 iter
->afi_safi_ix
++;
3431 * Found another table in this vrf.
3437 * Done with all tables in the current vrf, go to the
3441 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3444 iter
->afi_safi_ix
= 0;
3449 case RIB_TABLES_ITER_S_DONE
:
3454 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3456 iter
->state
= RIB_TABLES_ITER_S_DONE
;