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
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
40 #include "zebra/zebra_router.h"
41 #include "zebra/connected.h"
42 #include "zebra/debug.h"
43 #include "zebra/interface.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/rib.h"
47 #include "zebra/zapi_msg.h"
48 #include "zebra/zebra_errors.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/zebra_routemap.h"
53 #include "zebra/zebra_vrf.h"
54 #include "zebra/zebra_vxlan.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_dplane.h"
59 * Event, list, and mutex for delivery of dataplane results
61 static pthread_mutex_t dplane_mutex
;
62 static struct thread
*t_dplane
;
63 static struct dplane_ctx_q rib_dplane_q
;
65 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
68 /* Should we allow non Quagga processes to delete our routes */
69 extern int allow_delete
;
71 /* Each route type's string and default distance value. */
76 } route_info
[ZEBRA_ROUTE_MAX
] = {
77 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
78 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
79 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
80 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
81 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
82 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
83 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
84 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
85 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
86 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
87 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
88 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
89 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
90 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
91 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
92 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
93 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
94 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
95 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
97 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
99 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
100 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
101 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 4},
102 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 4},
103 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 2},
104 /* Any new route type added to zebra, should be mirrored here */
106 /* no entry/default: 150 */
109 /* RPF lookup behaviour */
110 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
113 static void __attribute__((format(printf
, 5, 6)))
114 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
115 int priority
, const char *msgfmt
, ...)
117 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
121 va_start(ap
, msgfmt
);
122 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
126 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
127 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
129 if (info
->safi
== SAFI_MULTICAST
)
130 strcat(buf
, " (MRIB)");
132 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
135 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
138 #define rnode_debug(node, vrf_id, ...) \
139 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
140 #define rnode_info(node, ...) \
141 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
143 uint8_t route_distance(int type
)
147 if ((unsigned)type
>= array_size(route_info
))
150 distance
= route_info
[type
].distance
;
155 int is_zebra_valid_kernel_table(uint32_t table_id
)
158 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
159 || (table_id
== RT_TABLE_COMPAT
))
166 int is_zebra_main_routing_table(uint32_t table_id
)
168 if (table_id
== RT_TABLE_MAIN
)
173 int zebra_check_addr(const struct prefix
*p
)
175 if (p
->family
== AF_INET
) {
178 addr
= p
->u
.prefix4
.s_addr
;
181 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
182 || IPV4_LINKLOCAL(addr
))
185 if (p
->family
== AF_INET6
) {
186 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
188 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
194 /* Add nexthop to the end of a rib node's nexthop list */
195 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
197 nexthop_add(&re
->ng
.nexthop
, nexthop
);
203 * copy_nexthop - copy a nexthop to the rib structure.
205 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
207 assert(!re
->ng
.nexthop
);
208 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
209 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
213 /* Delete specified nexthop from the list. */
214 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
217 nexthop
->next
->prev
= nexthop
->prev
;
219 nexthop
->prev
->next
= nexthop
->next
;
221 re
->ng
.nexthop
= nexthop
->next
;
226 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
230 struct nexthop
*nexthop
;
232 nexthop
= nexthop_new();
233 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
234 nexthop
->ifindex
= ifindex
;
235 nexthop
->vrf_id
= nh_vrf_id
;
237 route_entry_nexthop_add(re
, nexthop
);
242 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
243 struct in_addr
*ipv4
,
247 struct nexthop
*nexthop
;
249 nexthop
= nexthop_new();
250 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
251 nexthop
->vrf_id
= nh_vrf_id
;
252 nexthop
->gate
.ipv4
= *ipv4
;
254 nexthop
->src
.ipv4
= *src
;
256 route_entry_nexthop_add(re
, nexthop
);
261 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
262 struct in_addr
*ipv4
,
267 struct nexthop
*nexthop
;
268 struct interface
*ifp
;
270 nexthop
= nexthop_new();
271 nexthop
->vrf_id
= nh_vrf_id
;
272 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
273 nexthop
->gate
.ipv4
= *ipv4
;
275 nexthop
->src
.ipv4
= *src
;
276 nexthop
->ifindex
= ifindex
;
277 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
278 /*Pending: need to think if null ifp here is ok during bootup?
279 There was a crash because ifp here was coming to be NULL */
281 if (connected_is_unnumbered(ifp
))
282 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
;
318 route_entry_nexthop_add(re
, nexthop
);
323 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
324 enum blackhole_type bh_type
)
326 struct nexthop
*nexthop
;
328 nexthop
= nexthop_new();
329 nexthop
->vrf_id
= VRF_DEFAULT
;
330 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
331 nexthop
->bh_type
= bh_type
;
333 route_entry_nexthop_add(re
, nexthop
);
338 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
339 struct nexthop
*nexthop
)
341 struct nexthop
*resolved_hop
;
343 resolved_hop
= nexthop_new();
344 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
346 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
347 switch (newhop
->type
) {
348 case NEXTHOP_TYPE_IPV4
:
349 case NEXTHOP_TYPE_IPV4_IFINDEX
:
350 /* If the resolving route specifies a gateway, use it */
351 resolved_hop
->type
= newhop
->type
;
352 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
354 if (newhop
->ifindex
) {
355 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
356 resolved_hop
->ifindex
= newhop
->ifindex
;
359 case NEXTHOP_TYPE_IPV6
:
360 case NEXTHOP_TYPE_IPV6_IFINDEX
:
361 resolved_hop
->type
= newhop
->type
;
362 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
364 if (newhop
->ifindex
) {
365 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
366 resolved_hop
->ifindex
= newhop
->ifindex
;
369 case NEXTHOP_TYPE_IFINDEX
:
370 /* If the resolving route is an interface route,
371 * it means the gateway we are looking up is connected
372 * to that interface. (The actual network is _not_ onlink).
373 * Therefore, the resolved route should have the original
374 * gateway as nexthop as it is directly connected.
376 * On Linux, we have to set the onlink netlink flag because
377 * otherwise, the kernel won't accept the route.
379 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
381 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
382 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
383 } else if (afi
== AFI_IP6
) {
384 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
385 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
387 resolved_hop
->ifindex
= newhop
->ifindex
;
389 case NEXTHOP_TYPE_BLACKHOLE
:
390 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
391 resolved_hop
->bh_type
= nexthop
->bh_type
;
395 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
396 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
398 /* Copy labels of the resolved route */
399 if (newhop
->nh_label
)
400 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
401 newhop
->nh_label
->num_labels
,
402 &newhop
->nh_label
->label
[0]);
404 resolved_hop
->rparent
= nexthop
;
405 nexthop_add(&nexthop
->resolved
, resolved_hop
);
409 * Given a nexthop we need to properly recursively resolve
410 * the route. As such, do a table lookup to find and match
411 * if at all possible. Set the nexthop->ifindex as appropriate
413 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
414 struct nexthop
*nexthop
,
415 struct route_node
*top
)
418 struct route_table
*table
;
419 struct route_node
*rn
;
420 struct route_entry
*match
= NULL
;
422 struct nexthop
*newhop
;
423 struct interface
*ifp
;
426 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
427 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
428 nexthop
->ifindex
= 0;
430 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
431 nexthops_free(nexthop
->resolved
);
432 nexthop
->resolved
= NULL
;
436 * If the kernel has sent us a route, then
437 * by golly gee whiz it's a good route.
439 if (re
->type
== ZEBRA_ROUTE_KERNEL
||
440 re
->type
== ZEBRA_ROUTE_SYSTEM
)
444 * Check to see if we should trust the passed in information
445 * for UNNUMBERED interfaces as that we won't find the GW
446 * address in the routing table.
447 * This check should suffice to handle IPv4 or IPv6 routes
448 * sourced from EVPN routes which are installed with the
449 * next hop as the remote VTEP IP.
451 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
452 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
454 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
456 "\t%s: Onlink and interface: %u[%u] does not exist",
457 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
461 if (connected_is_unnumbered(ifp
)) {
462 if (if_is_operative(ifp
))
465 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
467 "\t%s: Onlink and interface %s is not operative",
468 __PRETTY_FUNCTION__
, ifp
->name
);
472 if (!if_is_operative(ifp
)) {
473 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
475 "\t%s: Interface %s is not unnumbered",
476 __PRETTY_FUNCTION__
, ifp
->name
);
481 /* Make lookup prefix. */
482 memset(&p
, 0, sizeof(struct prefix
));
486 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
487 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
491 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
492 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
495 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
499 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
501 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
502 zlog_debug("\t%s: Table not found",
503 __PRETTY_FUNCTION__
);
507 rn
= route_node_match(table
, (struct prefix
*)&p
);
509 route_unlock_node(rn
);
511 /* Lookup should halt if we've matched against ourselves ('top',
512 * if specified) - i.e., we cannot have a nexthop NH1 is
513 * resolved by a route NH1. The exception is if the route is a
516 if (top
&& rn
== top
)
517 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
518 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
519 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
521 "\t%s: Matched against ourself and prefix length is not max bit length",
522 __PRETTY_FUNCTION__
);
526 /* Pick up selected route. */
527 /* However, do not resolve over default route unless explicitly
529 if (is_default_prefix(&rn
->p
)
530 && !rnh_resolve_via_default(p
.family
)) {
531 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
533 "\t:%s: Resolved against default route",
534 __PRETTY_FUNCTION__
);
538 dest
= rib_dest_from_rnode(rn
);
539 if (dest
&& dest
->selected_fib
540 && !CHECK_FLAG(dest
->selected_fib
->status
,
542 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
543 match
= dest
->selected_fib
;
545 /* If there is no selected route or matched route is EGP, go up
550 } while (rn
&& rn
->info
== NULL
);
557 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
558 /* Directly point connected route. */
559 newhop
= match
->ng
.nexthop
;
561 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
562 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
563 nexthop
->ifindex
= newhop
->ifindex
;
566 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
568 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
569 if (!CHECK_FLAG(match
->status
,
570 ROUTE_ENTRY_INSTALLED
))
572 if (CHECK_FLAG(newhop
->flags
,
573 NEXTHOP_FLAG_RECURSIVE
))
576 SET_FLAG(nexthop
->flags
,
577 NEXTHOP_FLAG_RECURSIVE
);
579 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
580 nexthop_set_resolved(afi
, newhop
, nexthop
);
584 re
->nexthop_mtu
= match
->mtu
;
585 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
586 zlog_debug("\t%s: Recursion failed to find",
587 __PRETTY_FUNCTION__
);
589 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
591 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
592 if (!CHECK_FLAG(match
->status
,
593 ROUTE_ENTRY_INSTALLED
))
595 if (CHECK_FLAG(newhop
->flags
,
596 NEXTHOP_FLAG_RECURSIVE
))
599 SET_FLAG(nexthop
->flags
,
600 NEXTHOP_FLAG_RECURSIVE
);
601 nexthop_set_resolved(afi
, newhop
, nexthop
);
605 re
->nexthop_mtu
= match
->mtu
;
607 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
609 "\t%s: Static route unable to resolve",
610 __PRETTY_FUNCTION__
);
613 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
614 zlog_debug("\t%s: Route Type %s has not turned on recursion",
616 zebra_route_string(re
->type
));
617 if (re
->type
== ZEBRA_ROUTE_BGP
&&
618 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
619 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
624 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
625 zlog_debug("\t%s: Nexthop did not lookup in table",
626 __PRETTY_FUNCTION__
);
630 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
631 union g_addr
*addr
, struct route_node
**rn_out
)
634 struct route_table
*table
;
635 struct route_node
*rn
;
636 struct route_entry
*match
= NULL
;
639 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
643 memset(&p
, 0, sizeof(struct prefix
));
646 p
.u
.prefix4
= addr
->ipv4
;
647 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
649 p
.u
.prefix6
= addr
->ipv6
;
650 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
653 rn
= route_node_match(table
, (struct prefix
*)&p
);
658 route_unlock_node(rn
);
660 dest
= rib_dest_from_rnode(rn
);
661 if (dest
&& dest
->selected_fib
662 && !CHECK_FLAG(dest
->selected_fib
->status
,
663 ROUTE_ENTRY_REMOVED
))
664 match
= dest
->selected_fib
;
666 /* If there is no selected route or matched route is EGP, go up
671 } while (rn
&& rn
->info
== NULL
);
675 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
676 if (!CHECK_FLAG(match
->status
,
677 ROUTE_ENTRY_INSTALLED
))
689 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
691 struct route_node
**rn_out
)
693 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
694 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
695 union g_addr gaddr
= {.ipv4
= addr
};
697 switch (ipv4_multicast_mode
) {
698 case MCAST_MRIB_ONLY
:
699 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
701 case MCAST_URIB_ONLY
:
702 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
703 case MCAST_NO_CONFIG
:
704 case MCAST_MIX_MRIB_FIRST
:
705 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
708 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
711 case MCAST_MIX_DISTANCE
:
712 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
713 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
715 re
= ure
->distance
< mre
->distance
? ure
: mre
;
721 case MCAST_MIX_PFXLEN
:
722 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
723 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
725 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
734 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
736 if (IS_ZEBRA_DEBUG_RIB
) {
738 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
740 zlog_debug("%s: %s: vrf: %u found %s, using %s",
741 __func__
, buf
, vrf_id
,
742 mre
? (ure
? "MRIB+URIB" : "MRIB")
743 : ure
? "URIB" : "nothing",
744 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
749 void multicast_mode_ipv4_set(enum multicast_mode mode
)
751 if (IS_ZEBRA_DEBUG_RIB
)
752 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
754 ipv4_multicast_mode
= mode
;
757 enum multicast_mode
multicast_mode_ipv4_get(void)
759 return ipv4_multicast_mode
;
762 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
764 struct route_table
*table
;
765 struct route_node
*rn
;
766 struct route_entry
*match
= NULL
;
770 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
774 rn
= route_node_lookup(table
, (struct prefix
*)p
);
776 /* No route for this prefix. */
781 route_unlock_node(rn
);
782 dest
= rib_dest_from_rnode(rn
);
784 if (dest
&& dest
->selected_fib
785 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
786 match
= dest
->selected_fib
;
791 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
794 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
800 #define RIB_SYSTEM_ROUTE(R) \
801 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
803 #define RIB_KERNEL_ROUTE(R) \
804 ((R)->type == ZEBRA_ROUTE_KERNEL)
806 /* This function verifies reachability of one given nexthop, which can be
807 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
808 * in nexthop->flags field. The nexthop->ifindex will be updated
809 * appropriately as well. An existing route map can turn
810 * (otherwise active) nexthop into inactive, but not vice versa.
812 * The return value is the final value of 'ACTIVE' flag.
814 static unsigned nexthop_active_check(struct route_node
*rn
,
815 struct route_entry
*re
,
816 struct nexthop
*nexthop
)
818 struct interface
*ifp
;
819 route_map_result_t ret
= RMAP_MATCH
;
821 char buf
[SRCDEST2STR_BUFFER
];
822 const struct prefix
*p
, *src_p
;
823 struct zebra_vrf
*zvrf
;
825 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
827 if (rn
->p
.family
== AF_INET
)
829 else if (rn
->p
.family
== AF_INET6
)
833 switch (nexthop
->type
) {
834 case NEXTHOP_TYPE_IFINDEX
:
835 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
836 if (ifp
&& if_is_operative(ifp
))
837 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
839 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
841 case NEXTHOP_TYPE_IPV4
:
842 case NEXTHOP_TYPE_IPV4_IFINDEX
:
844 if (nexthop_active(AFI_IP
, re
, nexthop
, rn
))
845 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
847 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
849 case NEXTHOP_TYPE_IPV6
:
851 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
852 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
854 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
856 case NEXTHOP_TYPE_IPV6_IFINDEX
:
857 /* RFC 5549, v4 prefix with v6 NH */
858 if (rn
->p
.family
!= AF_INET
)
860 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
861 ifp
= if_lookup_by_index(nexthop
->ifindex
,
863 if (ifp
&& if_is_operative(ifp
))
864 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
866 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
868 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
869 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
871 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
874 case NEXTHOP_TYPE_BLACKHOLE
:
875 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
880 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
881 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
882 zlog_debug("\t%s: Unable to find a active nexthop",
883 __PRETTY_FUNCTION__
);
887 /* XXX: What exactly do those checks do? Do we support
888 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
890 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
891 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
892 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
894 /* The original code didn't determine the family correctly
895 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
896 * from the rib_table_info in those cases.
897 * Possibly it may be better to use only the rib_table_info
901 rib_table_info_t
*info
;
903 info
= srcdest_rnode_table_info(rn
);
907 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
909 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
911 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
912 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
913 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
916 /* It'll get set if required inside */
917 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
918 nexthop
, zvrf
, re
->tag
);
919 if (ret
== RMAP_DENYMATCH
) {
920 if (IS_ZEBRA_DEBUG_RIB
) {
921 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
923 "%u:%s: Filtering out with NH out %s due to route map",
925 ifindex2ifname(nexthop
->ifindex
,
928 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
930 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
934 * Iterate over all nexthops of the given RIB entry and refresh their
935 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
936 * nexthop is found to toggle the ACTIVE flag, the whole re structure
937 * is flagged with ROUTE_ENTRY_CHANGED.
939 * Return value is the new number of active nexthops.
941 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
943 struct nexthop
*nexthop
;
944 union g_addr prev_src
;
945 unsigned int prev_active
, new_active
;
946 ifindex_t prev_index
;
948 re
->nexthop_active_num
= 0;
949 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
951 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
952 /* No protocol daemon provides src and so we're skipping
954 prev_src
= nexthop
->rmap_src
;
955 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
956 prev_index
= nexthop
->ifindex
;
958 * We need to respect the multipath_num here
959 * as that what we should be able to install from
960 * a multipath perpsective should not be a data plane
963 new_active
= nexthop_active_check(rn
, re
, nexthop
);
964 if (new_active
&& re
->nexthop_active_num
>= multipath_num
) {
965 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
969 re
->nexthop_active_num
++;
970 /* Don't allow src setting on IPv6 addr for now */
971 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
972 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
973 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
974 && prev_src
.ipv4
.s_addr
975 != nexthop
->rmap_src
.ipv4
.s_addr
)
976 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
977 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
978 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
979 &nexthop
->rmap_src
.ipv6
)))
980 || CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)) {
981 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
982 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
986 return re
->nexthop_active_num
;
990 * Is this RIB labeled-unicast? It must be of type BGP and all paths
991 * (nexthops) must have a label.
993 int zebra_rib_labeled_unicast(struct route_entry
*re
)
995 struct nexthop
*nexthop
= NULL
;
997 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1000 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1001 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1007 /* Update flag indicates whether this is a "replace" or not. Currently, this
1008 * is only used for IPv4.
1010 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1011 struct route_entry
*old
)
1013 struct nexthop
*nexthop
;
1014 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1015 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1016 const struct prefix
*p
, *src_p
;
1017 enum zebra_dplane_result ret
;
1019 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1021 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1023 if (info
->safi
!= SAFI_UNICAST
) {
1024 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1025 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1028 struct nexthop
*prev
;
1030 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1031 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1032 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1033 if (prev
== nexthop
)
1035 if (nexthop_same_firsthop(nexthop
, prev
)) {
1036 SET_FLAG(nexthop
->flags
,
1037 NEXTHOP_FLAG_DUPLICATE
);
1045 * If this is a replace to a new RE let the originator of the RE
1046 * know that they've lost
1048 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1049 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1051 /* Update fib selection */
1052 dest
->selected_fib
= re
;
1055 * Make sure we update the FPM any time we send new information to
1058 hook_call(rib_update
, rn
, "installing in kernel");
1060 /* Send add or update */
1062 ret
= dplane_route_update(rn
, re
, old
);
1064 ret
= dplane_route_add(rn
, re
);
1067 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1068 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1070 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
1072 zvrf
->installs_queued
++;
1074 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1076 char str
[SRCDEST2STR_BUFFER
];
1078 srcdest_rnode2str(rn
, str
, sizeof(str
));
1079 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1080 "%u:%s: Failed to enqueue dataplane install",
1084 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1093 /* Uninstall the route from kernel. */
1094 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1096 struct nexthop
*nexthop
;
1097 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1098 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1100 if (info
->safi
!= SAFI_UNICAST
) {
1101 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1102 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1103 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1108 * Make sure we update the FPM any time we send new information to
1111 hook_call(rib_update
, rn
, "uninstalling from kernel");
1113 switch (dplane_route_delete(rn
, re
)) {
1114 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1116 zvrf
->removals_queued
++;
1118 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1120 char str
[SRCDEST2STR_BUFFER
];
1122 srcdest_rnode2str(rn
, str
, sizeof(str
));
1123 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1124 "%u:%s: Failed to enqueue dataplane uninstall",
1128 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1137 /* Uninstall the route from kernel. */
1138 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1140 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1141 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1142 struct nexthop
*nexthop
;
1144 if (dest
&& dest
->selected_fib
== re
) {
1145 if (info
->safi
== SAFI_UNICAST
)
1146 hook_call(rib_update
, rn
, "rib_uninstall");
1148 /* If labeled-unicast route, uninstall transit LSP. */
1149 if (zebra_rib_labeled_unicast(re
))
1150 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1152 rib_uninstall_kernel(rn
, re
);
1154 dest
->selected_fib
= NULL
;
1156 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1157 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1160 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1161 const struct prefix
*p
, *src_p
;
1163 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1165 redistribute_delete(p
, src_p
, re
);
1166 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1171 * rib_can_delete_dest
1173 * Returns TRUE if the given dest can be deleted from the table.
1175 static int rib_can_delete_dest(rib_dest_t
*dest
)
1177 if (re_list_first(&dest
->routes
)) {
1182 * Unresolved rnh's are stored on the default route's list
1184 * dest->rnode can also be the source prefix node in an
1185 * ipv6 sourcedest table. Fortunately the prefix of a
1186 * source prefix node can never be the default prefix.
1188 if (is_default_prefix(&dest
->rnode
->p
))
1192 * Don't delete the dest if we have to update the FPM about this
1195 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1196 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1202 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
1204 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1208 * We are storing the rnh's associated withb
1209 * the tracked nexthop as a list of the rn's.
1210 * Unresolved rnh's are placed at the top
1211 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
1212 * As such for each rn we need to walk up the tree
1213 * and see if any rnh's need to see if they
1214 * would match a more specific route
1217 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
1218 char buf
[PREFIX_STRLEN
];
1220 zlog_debug("%s: %s Being examined for Nexthop Tracking",
1221 __PRETTY_FUNCTION__
,
1222 srcdest_rnode2str(rn
, buf
, sizeof(buf
)));
1227 dest
= rib_dest_from_rnode(rn
);
1231 * If we have any rnh's stored in the nht list
1232 * then we know that this route node was used for
1233 * nht resolution and as such we need to call the
1234 * nexthop tracking evaluation code
1236 for_each (rnh_list
, &dest
->nht
, rnh
) {
1237 struct zebra_vrf
*zvrf
=
1238 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
1239 struct prefix
*p
= &rnh
->node
->p
;
1241 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
1242 char buf1
[PREFIX_STRLEN
];
1243 char buf2
[PREFIX_STRLEN
];
1245 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
1247 srcdest_rnode2str(rn
, buf1
,
1249 prefix2str(p
, buf2
, sizeof(buf2
)),
1254 * If we have evaluated this node on this pass
1255 * already, due to following the tree up
1256 * then we know that we can move onto the next
1259 * Additionally we call zebra_evaluate_rnh
1260 * when we gc the dest. In this case we know
1261 * that there must be no other re's where
1262 * we were originally as such we know that
1263 * that sequence number is ok to respect.
1265 if (rnh
->seqno
== seq
) {
1266 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1268 "\tNode processed and moved already");
1273 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
1279 dest
= rib_dest_from_rnode(rn
);
1286 * Garbage collect the rib dest corresponding to the given route node
1289 * Returns TRUE if the dest was deleted, FALSE otherwise.
1291 int rib_gc_dest(struct route_node
*rn
)
1295 dest
= rib_dest_from_rnode(rn
);
1299 if (!rib_can_delete_dest(dest
))
1302 if (IS_ZEBRA_DEBUG_RIB
) {
1303 struct zebra_vrf
*zvrf
;
1305 zvrf
= rib_dest_vrf(dest
);
1306 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1309 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
1312 rnh_list_fini(&dest
->nht
);
1313 XFREE(MTYPE_RIB_DEST
, dest
);
1317 * Release the one reference that we keep on the route node.
1319 route_unlock_node(rn
);
1323 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1324 struct route_entry
*new)
1326 hook_call(rib_update
, rn
, "new route selected");
1328 /* Update real nexthop. This may actually determine if nexthop is active
1330 if (!nexthop_group_active_nexthop_num(&new->ng
)) {
1331 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1335 if (IS_ZEBRA_DEBUG_RIB
) {
1336 char buf
[SRCDEST2STR_BUFFER
];
1337 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1338 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1339 zvrf_id(zvrf
), buf
, rn
, new,
1340 zebra_route_string(new->type
));
1343 /* If labeled-unicast route, install transit LSP. */
1344 if (zebra_rib_labeled_unicast(new))
1345 zebra_mpls_lsp_install(zvrf
, rn
, new);
1347 rib_install_kernel(rn
, new, NULL
);
1349 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1352 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1353 struct route_entry
*old
)
1355 hook_call(rib_update
, rn
, "removing existing route");
1357 /* Uninstall from kernel. */
1358 if (IS_ZEBRA_DEBUG_RIB
) {
1359 char buf
[SRCDEST2STR_BUFFER
];
1360 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1361 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1362 zvrf_id(zvrf
), buf
, rn
, old
,
1363 zebra_route_string(old
->type
));
1366 /* If labeled-unicast route, uninstall transit LSP. */
1367 if (zebra_rib_labeled_unicast(old
))
1368 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1370 rib_uninstall_kernel(rn
, old
);
1372 /* Update nexthop for route, reset changed flag. */
1373 /* Note: this code also handles the Linux case when an interface goes
1374 * down, causing the kernel to delete routes without sending DELROUTE
1377 if (RIB_KERNEL_ROUTE(old
))
1378 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1380 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1383 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1384 struct route_node
*rn
,
1385 struct route_entry
*old
,
1386 struct route_entry
*new)
1391 * We have to install or update if a new route has been selected or
1392 * something has changed.
1394 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1395 hook_call(rib_update
, rn
, "updating existing route");
1397 /* Update the nexthop; we could determine here that nexthop is
1399 if (nexthop_group_active_nexthop_num(&new->ng
))
1402 /* If nexthop is active, install the selected route, if
1404 * the install succeeds, cleanup flags for prior route, if
1409 if (IS_ZEBRA_DEBUG_RIB
) {
1410 char buf
[SRCDEST2STR_BUFFER
];
1411 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1414 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
1415 zvrf_id(zvrf
), buf
, rn
, new,
1416 zebra_route_string(new->type
),
1418 zebra_route_string(old
->type
));
1421 "%u:%s: Updating route rn %p, re %p (%s)",
1422 zvrf_id(zvrf
), buf
, rn
, new,
1423 zebra_route_string(new->type
));
1426 /* If labeled-unicast route, uninstall transit LSP. */
1427 if (zebra_rib_labeled_unicast(old
))
1428 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1431 * Non-system route should be installed.
1432 * If labeled-unicast route, install transit
1435 if (zebra_rib_labeled_unicast(new))
1436 zebra_mpls_lsp_install(zvrf
, rn
, new);
1438 rib_install_kernel(rn
, new, old
);
1442 * If nexthop for selected route is not active or install
1444 * may need to uninstall and delete for redistribution.
1447 if (IS_ZEBRA_DEBUG_RIB
) {
1448 char buf
[SRCDEST2STR_BUFFER
];
1449 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1452 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1453 zvrf_id(zvrf
), buf
, rn
, new,
1454 zebra_route_string(new->type
),
1456 zebra_route_string(old
->type
));
1459 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
1460 zvrf_id(zvrf
), buf
, rn
, new,
1461 zebra_route_string(new->type
));
1464 /* If labeled-unicast route, uninstall transit LSP. */
1465 if (zebra_rib_labeled_unicast(old
))
1466 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1468 rib_uninstall_kernel(rn
, old
);
1472 * Same route selected; check if in the FIB and if not,
1473 * re-install. This is housekeeping code to deal with
1474 * race conditions in kernel with linux netlink reporting
1475 * interface up before IPv4 or IPv6 protocol is ready
1478 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1479 RIB_SYSTEM_ROUTE(new))
1480 rib_install_kernel(rn
, new, NULL
);
1483 /* Update prior route. */
1485 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1487 /* Clear changed flag. */
1488 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1491 /* Check if 'alternate' RIB entry is better than 'current'. */
1492 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1493 struct route_entry
*alternate
)
1495 if (current
== NULL
)
1498 /* filter route selection in following order:
1499 * - connected beats other types
1500 * - if both connected, loopback or vrf wins
1501 * - lower distance beats higher
1502 * - lower metric beats higher for equal distance
1503 * - last, hence oldest, route wins tie break.
1506 /* Connected routes. Check to see if either are a vrf
1507 * or loopback interface. If not, pick the last connected
1508 * route of the set of lowest metric connected routes.
1510 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1511 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1514 /* both are connected. are either loop or vrf? */
1515 struct nexthop
*nexthop
= NULL
;
1517 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1518 if (if_is_loopback_or_vrf(if_lookup_by_index(
1519 nexthop
->ifindex
, alternate
->vrf_id
)))
1523 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1524 if (if_is_loopback_or_vrf(if_lookup_by_index(
1525 nexthop
->ifindex
, current
->vrf_id
)))
1529 /* Neither are loop or vrf so pick best metric */
1530 if (alternate
->metric
<= current
->metric
)
1536 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1539 /* higher distance loses */
1540 if (alternate
->distance
< current
->distance
)
1542 if (current
->distance
< alternate
->distance
)
1545 /* metric tie-breaks equal distance */
1546 if (alternate
->metric
<= current
->metric
)
1552 /* Core function for processing routing information base. */
1553 static void rib_process(struct route_node
*rn
)
1555 struct route_entry
*re
;
1556 struct route_entry
*next
;
1557 struct route_entry
*old_selected
= NULL
;
1558 struct route_entry
*new_selected
= NULL
;
1559 struct route_entry
*old_fib
= NULL
;
1560 struct route_entry
*new_fib
= NULL
;
1561 struct route_entry
*best
= NULL
;
1562 char buf
[SRCDEST2STR_BUFFER
];
1564 struct zebra_vrf
*zvrf
= NULL
;
1565 const struct prefix
*p
, *src_p
;
1567 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1568 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1572 dest
= rib_dest_from_rnode(rn
);
1574 zvrf
= rib_dest_vrf(dest
);
1575 vrf_id
= zvrf_id(zvrf
);
1578 if (IS_ZEBRA_DEBUG_RIB
)
1579 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1581 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1582 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1585 * we can have rn's that have a NULL info pointer
1586 * (dest). As such let's not let the deref happen
1587 * additionally we know RNODE_FOREACH_RE_SAFE
1588 * will not iterate so we are ok.
1591 old_fib
= dest
->selected_fib
;
1593 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1594 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1596 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1597 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1598 re
->status
, re
->flags
, re
->distance
,
1601 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1603 /* Currently selected re. */
1604 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1605 assert(old_selected
== NULL
);
1609 /* Skip deleted entries from selection */
1610 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1613 /* Skip unreachable nexthop. */
1614 /* This first call to nexthop_active_update is merely to
1615 * determine if there's any change to nexthops associated
1616 * with this RIB entry. Now, rib_process() can be invoked due
1617 * to an external event such as link down or due to
1618 * next-hop-tracking evaluation. In the latter case,
1619 * a decision has already been made that the NHs have changed.
1620 * So, no need to invoke a potentially expensive call again.
1621 * Further, since the change might be in a recursive NH which
1622 * is not caught in the nexthop_active_update() code. Thus, we
1623 * might miss changes to recursive NHs.
1625 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1626 && !nexthop_active_update(rn
, re
)) {
1627 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1628 /* XXX: HERE BE DRAGONS!!!!!
1629 * In all honesty, I have not yet figured out
1630 * what this part does or why the
1631 * ROUTE_ENTRY_CHANGED test above is correct
1632 * or why we need to delete a route here, and
1633 * also not whether this concerns both selected
1634 * and fib route, or only selected
1637 * This entry was denied by the 'ip protocol
1638 * table' route-map, we need to delete it */
1639 if (re
!= old_selected
) {
1640 if (IS_ZEBRA_DEBUG_RIB
)
1642 "%s: %u:%s: imported via import-table but denied "
1643 "by the ip protocol table route-map",
1644 __func__
, vrf_id
, buf
);
1647 SET_FLAG(re
->status
,
1648 ROUTE_ENTRY_REMOVED
);
1654 /* Infinite distance. */
1655 if (re
->distance
== DISTANCE_INFINITY
) {
1656 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1660 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1661 best
= rib_choose_best(new_fib
, re
);
1662 if (new_fib
&& best
!= new_fib
)
1663 UNSET_FLAG(new_fib
->status
,
1664 ROUTE_ENTRY_CHANGED
);
1667 best
= rib_choose_best(new_selected
, re
);
1668 if (new_selected
&& best
!= new_selected
)
1669 UNSET_FLAG(new_selected
->status
,
1670 ROUTE_ENTRY_CHANGED
);
1671 new_selected
= best
;
1674 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1675 } /* RNODE_FOREACH_RE */
1677 /* If no FIB override route, use the selected route also for FIB */
1678 if (new_fib
== NULL
)
1679 new_fib
= new_selected
;
1681 /* After the cycle is finished, the following pointers will be set:
1682 * old_selected --- RE entry currently having SELECTED
1683 * new_selected --- RE entry that is newly SELECTED
1684 * old_fib --- RE entry currently in kernel FIB
1685 * new_fib --- RE entry that is newly to be in kernel FIB
1687 * new_selected will get SELECTED flag, and is going to be redistributed
1688 * the zclients. new_fib (which can be new_selected) will be installed
1692 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1694 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1695 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1696 (void *)old_fib
, (void *)new_fib
);
1699 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1700 * fib == selected */
1701 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1702 ROUTE_ENTRY_CHANGED
);
1704 /* Update fib according to selection results */
1705 if (new_fib
&& old_fib
)
1706 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1708 rib_process_add_fib(zvrf
, rn
, new_fib
);
1710 rib_process_del_fib(zvrf
, rn
, old_fib
);
1712 /* Update SELECTED entry */
1713 if (old_selected
!= new_selected
|| selected_changed
) {
1715 if (new_selected
&& new_selected
!= new_fib
)
1716 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1719 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1723 redistribute_delete(p
, src_p
, old_selected
);
1724 if (old_selected
!= new_selected
)
1725 UNSET_FLAG(old_selected
->flags
,
1726 ZEBRA_FLAG_SELECTED
);
1730 /* Remove all RE entries queued for removal */
1731 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1732 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1733 if (IS_ZEBRA_DEBUG_RIB
) {
1734 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1735 (void *)rn
, (void *)re
);
1742 * Check if the dest can be deleted now.
1747 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1749 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1750 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1755 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1756 if (IS_ZEBRA_DEBUG_MPLS
)
1758 "%u: Scheduling all LSPs upon RIB completion",
1760 zebra_mpls_lsp_schedule(zvrf
);
1761 mpls_unmark_lsps_for_processing(rn
);
1766 * Utility to match route with dplane context data
1768 static bool rib_route_match_ctx(const struct route_entry
*re
,
1769 const struct zebra_dplane_ctx
*ctx
,
1772 bool result
= false;
1776 * In 'update' case, we test info about the 'previous' or
1779 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1780 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1783 /* TODO -- we're using this extra test, but it's not
1784 * exactly clear why.
1786 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1787 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1788 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1795 * Ordinary, single-route case using primary context info
1797 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1798 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1799 /* Skip route that's been deleted */
1803 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1804 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1807 /* TODO -- we're using this extra test, but it's not
1808 * exactly clear why.
1810 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1811 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1812 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1823 static void zebra_rib_fixup_system(struct route_node
*rn
)
1825 struct route_entry
*re
;
1827 RNODE_FOREACH_RE(rn
, re
) {
1828 struct nexthop
*nhop
;
1830 if (!RIB_SYSTEM_ROUTE(re
))
1833 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1836 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1838 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1839 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1842 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1848 * Route-update results processing after async dataplane update.
1850 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1852 struct route_table
*table
= NULL
;
1853 struct zebra_vrf
*zvrf
= NULL
;
1854 struct route_node
*rn
= NULL
;
1855 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1856 bool is_update
= false;
1857 struct nexthop
*nexthop
, *ctx_nexthop
;
1858 char dest_str
[PREFIX_STRLEN
] = "";
1859 enum dplane_op_e op
;
1860 enum zebra_dplane_result status
;
1861 const struct prefix
*dest_pfx
, *src_pfx
;
1864 /* Locate rn and re(s) from ctx */
1866 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1867 dplane_ctx_get_safi(ctx
),
1868 dplane_ctx_get_vrf(ctx
),
1869 dplane_ctx_get_table(ctx
));
1870 if (table
== NULL
) {
1871 if (IS_ZEBRA_DEBUG_DPLANE
) {
1872 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1873 dplane_ctx_get_afi(ctx
),
1874 dplane_ctx_get_safi(ctx
),
1875 dplane_ctx_get_vrf(ctx
));
1880 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1882 dest_pfx
= dplane_ctx_get_dest(ctx
);
1884 /* Note well: only capturing the prefix string if debug is enabled here;
1885 * unconditional log messages will have to generate the string.
1887 if (IS_ZEBRA_DEBUG_DPLANE
)
1888 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1890 src_pfx
= dplane_ctx_get_src(ctx
);
1891 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1892 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1894 if (IS_ZEBRA_DEBUG_DPLANE
) {
1895 zlog_debug("Failed to process dplane results: no route for %u:%s",
1896 dplane_ctx_get_vrf(ctx
), dest_str
);
1901 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1903 op
= dplane_ctx_get_op(ctx
);
1904 status
= dplane_ctx_get_status(ctx
);
1906 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1907 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1908 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1909 dplane_op2str(op
), dplane_res2str(status
));
1912 * Update is a bit of a special case, where we may have both old and new
1913 * routes to post-process.
1915 is_update
= dplane_ctx_is_update(ctx
);
1918 * Take a pass through the routes, look for matches with the context
1921 RNODE_FOREACH_RE(rn
, rib
) {
1924 if (rib_route_match_ctx(rib
, ctx
, false))
1928 /* Check for old route match */
1929 if (is_update
&& (old_re
== NULL
)) {
1930 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1934 /* Have we found the routes we need to work on? */
1935 if (re
&& ((!is_update
|| old_re
)))
1939 seq
= dplane_ctx_get_seq(ctx
);
1942 * Check sequence number(s) to detect stale results before continuing
1945 if (re
->dplane_sequence
!= seq
) {
1946 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1947 zlog_debug("%u:%s Stale dplane result for re %p",
1948 dplane_ctx_get_vrf(ctx
),
1951 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1955 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1956 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1957 zlog_debug("%u:%s Stale dplane result for old_re %p",
1958 dplane_ctx_get_vrf(ctx
),
1961 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1965 case DPLANE_OP_ROUTE_INSTALL
:
1966 case DPLANE_OP_ROUTE_UPDATE
:
1967 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1969 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1970 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1973 * On an update operation from the same route type
1974 * context retrieval currently has no way to know
1975 * which was the old and which was the new.
1976 * So don't unset our flags that we just set.
1977 * We know redistribution is ok because the
1978 * old_re in this case is used for nothing
1979 * more than knowing whom to contact if necessary.
1981 if (old_re
&& old_re
!= re
) {
1982 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1983 UNSET_FLAG(old_re
->status
,
1984 ROUTE_ENTRY_INSTALLED
);
1986 /* Update zebra nexthop FIB flag for each
1987 * nexthop that was installed.
1989 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1995 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1996 if (nexthop_same(ctx_nexthop
, nexthop
))
2000 if (nexthop
== NULL
)
2003 if (CHECK_FLAG(nexthop
->flags
,
2004 NEXTHOP_FLAG_RECURSIVE
))
2007 if (CHECK_FLAG(ctx_nexthop
->flags
,
2009 SET_FLAG(nexthop
->flags
,
2012 UNSET_FLAG(nexthop
->flags
,
2017 * System routes are weird in that they
2018 * allow multiple to be installed that match
2019 * to the same prefix, so after we get the
2020 * result we need to clean them up so that
2021 * we can actually use them.
2023 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2024 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2025 zebra_rib_fixup_system(rn
);
2032 * TODO -- still calling the redist api using the
2033 * route_entries, and there's a corner-case here:
2034 * if there's no client for the 'new' route, a redist
2035 * deleting the 'old' route will be sent. But if the
2036 * 'old' context info was stale, 'old_re' will be
2037 * NULL here and that delete will not be sent.
2040 redistribute_update(dest_pfx
, src_pfx
,
2043 /* Notify route owner */
2044 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2048 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2049 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2051 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2053 zsend_route_notify_owner(re
, dest_pfx
,
2054 ZAPI_ROUTE_FAIL_INSTALL
);
2056 zlog_warn("%u:%s: Route install failed",
2057 dplane_ctx_get_vrf(ctx
),
2058 prefix2str(dest_pfx
,
2059 dest_str
, sizeof(dest_str
)));
2062 case DPLANE_OP_ROUTE_DELETE
:
2064 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2066 * In the delete case, the zebra core datastructs were
2067 * updated (or removed) at the time the delete was issued,
2068 * so we're just notifying the route owner.
2070 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2072 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2073 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2075 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2081 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2082 zsend_route_notify_owner_ctx(ctx
,
2083 ZAPI_ROUTE_REMOVE_FAIL
);
2085 zlog_warn("%u:%s: Route Deletion failure",
2086 dplane_ctx_get_vrf(ctx
),
2087 prefix2str(dest_pfx
,
2088 dest_str
, sizeof(dest_str
)));
2092 * System routes are weird in that they
2093 * allow multiple to be installed that match
2094 * to the same prefix, so after we get the
2095 * result we need to clean them up so that
2096 * we can actually use them.
2098 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2099 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2100 zebra_rib_fixup_system(rn
);
2106 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
2107 zebra_rib_evaluate_mpls(rn
);
2111 route_unlock_node(rn
);
2113 /* Return context to dataplane module */
2114 dplane_ctx_fini(&ctx
);
2117 /* Take a list of route_node structs and return 1, if there was a record
2118 * picked from it and processed by rib_process(). Don't process more,
2119 * than one RN record; operate only in the specified sub-queue.
2121 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2123 struct listnode
*lnode
= listhead(subq
);
2124 struct route_node
*rnode
;
2126 struct zebra_vrf
*zvrf
= NULL
;
2131 rnode
= listgetdata(lnode
);
2132 dest
= rib_dest_from_rnode(rnode
);
2134 zvrf
= rib_dest_vrf(dest
);
2138 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2139 char buf
[SRCDEST2STR_BUFFER
];
2140 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2141 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2142 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2146 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2147 RIB_ROUTE_QUEUED(qindex
));
2152 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2153 __func__
, rnode
, rnode
->lock
);
2154 zlog_backtrace(LOG_DEBUG
);
2157 route_unlock_node(rnode
);
2158 list_delete_node(subq
, lnode
);
2164 * Perform next-hop tracking processing after RIB updates.
2166 static void do_nht_processing(void)
2170 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2171 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2173 * is pointed to the meta queue structure.
2175 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2177 struct meta_queue
*mq
= data
;
2179 uint32_t queue_len
, queue_limit
;
2181 /* Ensure there's room for more dataplane updates */
2182 queue_limit
= dplane_get_in_queue_limit();
2183 queue_len
= dplane_get_in_queue_len();
2184 if (queue_len
> queue_limit
) {
2185 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2186 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2187 queue_len
, queue_limit
);
2189 /* Ensure that the meta-queue is actually enqueued */
2190 if (work_queue_empty(zrouter
.ribq
))
2191 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2193 return WQ_QUEUE_BLOCKED
;
2196 for (i
= 0; i
< MQ_SIZE
; i
++)
2197 if (process_subq(mq
->subq
[i
], i
)) {
2201 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2206 * Look into the RN and queue it into the highest priority queue
2207 * at this point in time for processing.
2209 * We will enqueue a route node only once per invocation.
2211 * There are two possibilities here that should be kept in mind.
2212 * If the original invocation has not been pulled off for processing
2213 * yet, A subsuquent invocation can have a route entry with a better
2214 * meta queue index value and we can have a situation where
2215 * we might have the same node enqueued 2 times. Not necessarily
2216 * an optimal situation but it should be ok.
2218 * The other possibility is that the original invocation has not
2219 * been pulled off for processing yet, A subsusquent invocation
2220 * doesn't have a route_entry with a better meta-queue and the
2221 * original metaqueue index value will win and we'll end up with
2222 * the route node enqueued once.
2224 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2226 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2227 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2229 RNODE_FOREACH_RE (rn
, curr_re
) {
2230 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2232 if (curr_qindex
<= qindex
) {
2234 qindex
= curr_qindex
;
2241 /* Invariant: at this point we always have rn->info set. */
2242 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2243 RIB_ROUTE_QUEUED(qindex
))) {
2244 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2245 rnode_debug(rn
, re
->vrf_id
,
2246 "rn %p is already queued in sub-queue %u",
2247 (void *)rn
, qindex
);
2251 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2252 listnode_add(mq
->subq
[qindex
], rn
);
2253 route_lock_node(rn
);
2256 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2257 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2258 (void *)rn
, qindex
);
2261 /* Add route_node to work queue and schedule processing */
2262 void rib_queue_add(struct route_node
*rn
)
2266 /* Pointless to queue a route_node with no RIB entries to add or remove
2268 if (!rnode_to_ribs(rn
)) {
2269 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2270 __func__
, (void *)rn
, rn
->lock
);
2271 zlog_backtrace(LOG_DEBUG
);
2275 if (zrouter
.ribq
== NULL
) {
2276 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2277 "%s: work_queue does not exist!", __func__
);
2282 * The RIB queue should normally be either empty or holding the only
2283 * work_queue_item element. In the latter case this element would
2284 * hold a pointer to the meta queue structure, which must be used to
2285 * actually queue the route nodes to process. So create the MQ
2286 * holder, if necessary, then push the work into it in any case.
2287 * This semantics was introduced after 0.99.9 release.
2289 if (work_queue_empty(zrouter
.ribq
))
2290 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2292 rib_meta_queue_add(zrouter
.mq
, rn
);
2297 /* Create new meta queue.
2298 A destructor function doesn't seem to be necessary here.
2300 static struct meta_queue
*meta_queue_new(void)
2302 struct meta_queue
*new;
2305 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2307 for (i
= 0; i
< MQ_SIZE
; i
++) {
2308 new->subq
[i
] = list_new();
2309 assert(new->subq
[i
]);
2315 void meta_queue_free(struct meta_queue
*mq
)
2319 for (i
= 0; i
< MQ_SIZE
; i
++)
2320 list_delete(&mq
->subq
[i
]);
2322 XFREE(MTYPE_WORK_QUEUE
, mq
);
2325 /* initialise zebra rib work queue */
2326 static void rib_queue_init(void)
2328 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2329 "route_node processing"))) {
2330 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2331 "%s: could not initialise work queue!", __func__
);
2335 /* fill in the work queue spec */
2336 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2337 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2338 zrouter
.ribq
->spec
.completion_func
= NULL
;
2339 /* XXX: TODO: These should be runtime configurable via vty */
2340 zrouter
.ribq
->spec
.max_retries
= 3;
2341 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2342 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2344 if (!(zrouter
.mq
= meta_queue_new())) {
2345 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2346 "%s: could not initialise meta queue!", __func__
);
2352 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2356 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2357 rnh_list_init(&dest
->nht
);
2358 route_lock_node(rn
); /* rn route table reference */
2365 /* RIB updates are processed via a queue of pointers to route_nodes.
2367 * The queue length is bounded by the maximal size of the routing table,
2368 * as a route_node will not be requeued, if already queued.
2370 * REs are submitted via rib_addnode or rib_delnode which set minimal
2371 * state, or static_install_route (when an existing RE is updated)
2372 * and then submit route_node to queue for best-path selection later.
2373 * Order of add/delete state changes are preserved for any given RE.
2375 * Deleted REs are reaped during best-path selection.
2378 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2379 * |-------->| | best RE, if required
2381 * static_install->|->rib_addqueue...... -> rib_process
2383 * |-------->| |-> rib_unlink
2384 * |-> set ROUTE_ENTRY_REMOVE |
2385 * rib_delnode (RE freed)
2387 * The 'info' pointer of a route_node points to a rib_dest_t
2388 * ('dest'). Queueing state for a route_node is kept on the dest. The
2389 * dest is created on-demand by rib_link() and is kept around at least
2390 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2392 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2394 * - route_nodes: refcounted by:
2395 * - dest attached to route_node:
2396 * - managed by: rib_link/rib_gc_dest
2397 * - route_node processing queue
2398 * - managed by: rib_addqueue, rib_process.
2402 /* Add RE to head of the route node. */
2403 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2407 const char *rmap_name
;
2411 dest
= rib_dest_from_rnode(rn
);
2413 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2414 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2416 dest
= zebra_rib_create_dest(rn
);
2419 re_list_add_head(&dest
->routes
, re
);
2421 afi
= (rn
->p
.family
== AF_INET
)
2423 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2424 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2425 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2426 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2431 static void rib_addnode(struct route_node
*rn
,
2432 struct route_entry
*re
, int process
)
2434 /* RE node has been un-removed before route-node is processed.
2435 * route_node must hence already be on the queue for processing..
2437 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2438 if (IS_ZEBRA_DEBUG_RIB
)
2439 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2440 (void *)rn
, (void *)re
);
2442 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2445 rib_link(rn
, re
, process
);
2451 * Detach a rib structure from a route_node.
2453 * Note that a call to rib_unlink() should be followed by a call to
2454 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2455 * longer required to be deleted.
2457 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2463 if (IS_ZEBRA_DEBUG_RIB
)
2464 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2467 dest
= rib_dest_from_rnode(rn
);
2469 re_list_del(&dest
->routes
, re
);
2471 if (dest
->selected_fib
== re
)
2472 dest
->selected_fib
= NULL
;
2474 nexthops_free(re
->ng
.nexthop
);
2475 XFREE(MTYPE_RE
, re
);
2478 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2482 if (IS_ZEBRA_DEBUG_RIB
)
2483 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2484 (void *)rn
, (void *)re
);
2485 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2487 afi
= (rn
->p
.family
== AF_INET
)
2489 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2490 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2491 zebra_del_import_table_entry(rn
, re
);
2492 /* Just clean up if non main table */
2493 if (IS_ZEBRA_DEBUG_RIB
) {
2494 char buf
[SRCDEST2STR_BUFFER
];
2495 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2496 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2497 re
->vrf_id
, buf
, rn
, re
,
2498 zebra_route_string(re
->type
));
2507 /* This function dumps the contents of a given RE entry into
2508 * standard debug log. Calling function name and IP prefix in
2509 * question are passed as 1st and 2nd arguments.
2512 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2513 union prefixconstptr src_pp
,
2514 const struct route_entry
*re
)
2516 const struct prefix
*src_p
= src_pp
.p
;
2517 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2518 char straddr
[PREFIX_STRLEN
];
2519 char srcaddr
[PREFIX_STRLEN
];
2520 struct nexthop
*nexthop
;
2522 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2523 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2524 is_srcdst
? " from " : "",
2525 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2528 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2529 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2532 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2533 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2534 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2535 re
->nexthop_num
, re
->nexthop_active_num
);
2537 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2538 struct interface
*ifp
;
2539 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2541 switch (nexthop
->type
) {
2542 case NEXTHOP_TYPE_BLACKHOLE
:
2543 sprintf(straddr
, "Blackhole");
2545 case NEXTHOP_TYPE_IFINDEX
:
2546 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2548 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2550 case NEXTHOP_TYPE_IPV4
:
2552 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2553 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2556 case NEXTHOP_TYPE_IPV6
:
2557 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2558 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2562 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2563 func
, (nexthop
->rparent
? " NH" : "NH"), straddr
,
2564 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2566 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2569 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2572 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2575 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2578 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2581 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2585 zlog_debug("%s: dump complete", func
);
2588 /* This is an exported helper to rtm_read() to dump the strange
2589 * RE entry found by rib_lookup_ipv4_route()
2592 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2594 struct route_table
*table
;
2595 struct route_node
*rn
;
2596 struct route_entry
*re
;
2597 char prefix_buf
[INET_ADDRSTRLEN
];
2600 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2602 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2603 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2608 /* Scan the RIB table for exactly matching RE entry. */
2609 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2611 /* No route for this prefix. */
2613 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2614 prefix2str((struct prefix
*)p
, prefix_buf
,
2615 sizeof(prefix_buf
)));
2620 route_unlock_node(rn
);
2623 RNODE_FOREACH_RE (rn
, re
) {
2624 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2626 (void *)rn
, (void *)re
,
2627 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2630 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2633 route_entry_dump(p
, NULL
, re
);
2637 /* Check if requested address assignment will fail due to another
2638 * route being installed by zebra in FIB already. Take necessary
2639 * actions, if needed: remove such a route from FIB and deSELECT
2640 * corresponding RE entry. Then put affected RN into RIBQ head.
2642 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2644 struct route_table
*table
;
2645 struct route_node
*rn
;
2648 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2649 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2650 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2655 /* No matches would be the simplest case. */
2656 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2660 route_unlock_node(rn
);
2662 dest
= rib_dest_from_rnode(rn
);
2663 /* Check all RE entries. In case any changes have to be done, requeue
2664 * the RN into RIBQ head. If the routing message about the new connected
2665 * route (generated by the IP address we are going to assign very soon)
2666 * comes before the RIBQ is processed, the new RE entry will join
2667 * RIBQ record already on head. This is necessary for proper
2669 * of the rest of the RE.
2671 if (dest
->selected_fib
) {
2672 if (IS_ZEBRA_DEBUG_RIB
) {
2673 char buf
[PREFIX_STRLEN
];
2675 zlog_debug("%u:%s: freeing way for connected prefix",
2676 dest
->selected_fib
->vrf_id
,
2677 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2678 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2680 rib_uninstall(rn
, dest
->selected_fib
);
2685 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2686 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2688 struct route_table
*table
;
2689 struct route_node
*rn
;
2690 struct route_entry
*same
= NULL
;
2696 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2699 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2701 XFREE(MTYPE_RE
, re
);
2705 /* Make it sure prefixlen is applied to the prefix. */
2708 apply_mask_ipv6(src_p
);
2710 /* Set default distance by route type. */
2711 if (re
->distance
== 0) {
2712 re
->distance
= route_distance(re
->type
);
2714 /* iBGP distance is 200. */
2715 if (re
->type
== ZEBRA_ROUTE_BGP
2716 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2720 /* Lookup route node.*/
2721 rn
= srcdest_rnode_get(table
, p
, src_p
);
2724 * If same type of route are installed, treat it as a implicit
2726 * If the user has specified the No route replace semantics
2727 * for the install don't do a route replace.
2729 RNODE_FOREACH_RE (rn
, same
) {
2730 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2733 if (same
->type
!= re
->type
)
2735 if (same
->instance
!= re
->instance
)
2737 if (same
->type
== ZEBRA_ROUTE_KERNEL
2738 && same
->metric
!= re
->metric
)
2741 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2742 same
->distance
!= re
->distance
)
2746 * We should allow duplicate connected routes
2747 * because of IPv6 link-local routes and unnumbered
2748 * interfaces on Linux.
2750 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2754 /* If this route is kernel/connected route, notify the dataplane. */
2755 if (RIB_SYSTEM_ROUTE(re
)) {
2756 /* Notify dataplane */
2757 dplane_sys_route_add(rn
, re
);
2760 /* Link new re to node.*/
2761 if (IS_ZEBRA_DEBUG_RIB
) {
2762 rnode_debug(rn
, re
->vrf_id
,
2763 "Inserting route rn %p, re %p (%s) existing %p",
2764 rn
, re
, zebra_route_string(re
->type
), same
);
2766 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2767 route_entry_dump(p
, src_p
, re
);
2770 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2771 rib_addnode(rn
, re
, 1);
2774 /* Free implicit route.*/
2776 rib_delnode(rn
, same
);
2780 route_unlock_node(rn
);
2784 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2785 unsigned short instance
, int flags
, struct prefix
*p
,
2786 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2787 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2790 struct route_table
*table
;
2791 struct route_node
*rn
;
2792 struct route_entry
*re
;
2793 struct route_entry
*fib
= NULL
;
2794 struct route_entry
*same
= NULL
;
2795 struct nexthop
*rtnh
;
2796 char buf2
[INET6_ADDRSTRLEN
];
2799 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2802 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2809 apply_mask_ipv6(src_p
);
2811 /* Lookup route node. */
2812 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2814 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2816 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2817 if (src_p
&& src_p
->prefixlen
)
2818 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2822 if (IS_ZEBRA_DEBUG_RIB
)
2823 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2825 (src_buf
[0] != '\0') ? " from " : "",
2830 dest
= rib_dest_from_rnode(rn
);
2831 fib
= dest
->selected_fib
;
2833 /* Lookup same type route. */
2834 RNODE_FOREACH_RE (rn
, re
) {
2835 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2838 if (re
->type
!= type
)
2840 if (re
->instance
!= instance
)
2842 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2843 distance
!= re
->distance
)
2846 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2848 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2849 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2850 if (rtnh
->ifindex
!= nh
->ifindex
)
2855 /* Make sure that the route found has the same gateway. */
2861 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2862 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2870 /* If same type of route can't be found and this message is from
2874 * In the past(HA!) we could get here because
2875 * we were receiving a route delete from the
2876 * kernel and we're not marking the proto
2877 * as coming from it's appropriate originator.
2878 * Now that we are properly noticing the fact
2879 * that the kernel has deleted our route we
2880 * are not going to get called in this path
2881 * I am going to leave this here because
2882 * this might still work this way on non-linux
2883 * platforms as well as some weird state I have
2884 * not properly thought of yet.
2885 * If we can show that this code path is
2886 * dead then we can remove it.
2888 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2889 if (IS_ZEBRA_DEBUG_RIB
) {
2890 rnode_debug(rn
, vrf_id
,
2891 "rn %p, re %p (%s) was deleted from kernel, adding",
2893 zebra_route_string(fib
->type
));
2896 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2898 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2900 UNSET_FLAG(rtnh
->flags
,
2904 * This is a non FRR route
2905 * as such we should mark
2908 dest
->selected_fib
= NULL
;
2910 /* This means someone else, other than Zebra,
2912 * a Zebra router from the kernel. We will add
2914 rib_install_kernel(rn
, fib
, NULL
);
2917 if (IS_ZEBRA_DEBUG_RIB
) {
2921 "via %s ifindex %d type %d "
2922 "doesn't exist in rib",
2923 inet_ntop(afi2family(afi
),
2930 "type %d doesn't exist in rib",
2933 route_unlock_node(rn
);
2939 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2941 rib_install_kernel(rn
, same
, NULL
);
2942 route_unlock_node(rn
);
2947 /* Special handling for IPv4 or IPv6 routes sourced from
2948 * EVPN - the nexthop (and associated MAC) need to be
2949 * uninstalled if no more refs.
2951 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2952 struct nexthop
*tmp_nh
;
2954 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2955 struct ipaddr vtep_ip
;
2957 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2958 if (afi
== AFI_IP
) {
2959 vtep_ip
.ipa_type
= IPADDR_V4
;
2960 memcpy(&(vtep_ip
.ipaddr_v4
),
2961 &(tmp_nh
->gate
.ipv4
),
2962 sizeof(struct in_addr
));
2964 vtep_ip
.ipa_type
= IPADDR_V6
;
2965 memcpy(&(vtep_ip
.ipaddr_v6
),
2966 &(tmp_nh
->gate
.ipv6
),
2967 sizeof(struct in6_addr
));
2969 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2974 /* Notify dplane if system route changes */
2975 if (RIB_SYSTEM_ROUTE(re
))
2976 dplane_sys_route_del(rn
, same
);
2978 rib_delnode(rn
, same
);
2981 route_unlock_node(rn
);
2986 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2987 unsigned short instance
, int flags
, struct prefix
*p
,
2988 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2989 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2992 struct route_entry
*re
;
2993 struct nexthop
*nexthop
;
2995 /* Allocate new route_entry structure. */
2996 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2998 re
->instance
= instance
;
2999 re
->distance
= distance
;
3001 re
->metric
= metric
;
3003 re
->table
= table_id
;
3004 re
->vrf_id
= vrf_id
;
3005 re
->nexthop_num
= 0;
3006 re
->uptime
= time(NULL
);
3010 nexthop
= nexthop_new();
3012 route_entry_nexthop_add(re
, nexthop
);
3014 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
3017 /* Schedule routes of a particular table (address-family) based on event. */
3018 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3020 struct route_node
*rn
;
3021 struct route_entry
*re
, *next
;
3023 /* Walk all routes and queue for processing, if appropriate for
3024 * the trigger event.
3026 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3028 * If we are looking at a route node and the node
3029 * has already been queued we don't
3030 * need to queue it up again
3032 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3033 RIB_ROUTE_ANY_QUEUED
))
3036 case RIB_UPDATE_IF_CHANGE
:
3037 /* Examine all routes that won't get processed by the
3039 * triggered by nexthop evaluation (NHT). This would be
3041 * kernel and certain static routes. Note that NHT will
3043 * triggered upon an interface event as connected routes
3045 * get queued for processing.
3047 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3050 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3051 && re
->type
!= ZEBRA_ROUTE_KERNEL
3052 && re
->type
!= ZEBRA_ROUTE_CONNECT
3053 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3056 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3057 SET_FLAG(re
->status
,
3058 ROUTE_ENTRY_CHANGED
);
3063 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3064 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3065 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3068 /* If we only have nexthops to a
3073 SET_FLAG(re
->status
,
3074 ROUTE_ENTRY_CHANGED
);
3080 case RIB_UPDATE_RMAP_CHANGE
:
3081 case RIB_UPDATE_OTHER
:
3082 /* Right now, examine all routes. Can restrict to a
3084 * some cases (TODO).
3086 if (rnode_to_ribs(rn
)) {
3087 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
3088 SET_FLAG(re
->status
,
3089 ROUTE_ENTRY_CHANGED
);
3100 /* RIB update function. */
3101 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3103 struct route_table
*table
;
3105 /* Process routes of interested address-families. */
3106 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3108 if (IS_ZEBRA_DEBUG_EVENT
)
3109 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3110 rib_update_table(table
, event
);
3113 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3115 if (IS_ZEBRA_DEBUG_EVENT
)
3116 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3117 rib_update_table(table
, event
);
3121 /* Delete self installed routes after zebra is relaunched. */
3122 void rib_sweep_table(struct route_table
*table
)
3124 struct route_node
*rn
;
3125 struct route_entry
*re
;
3126 struct route_entry
*next
;
3127 struct nexthop
*nexthop
;
3132 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3133 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3134 if (IS_ZEBRA_DEBUG_RIB
)
3135 route_entry_dump(&rn
->p
, NULL
, re
);
3137 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3140 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3144 * So we are starting up and have received
3145 * routes from the kernel that we have installed
3146 * from a previous run of zebra but not cleaned
3147 * up ( say a kill -9 )
3148 * But since we haven't actually installed
3149 * them yet( we received them from the kernel )
3150 * we don't think they are active.
3151 * So let's pretend they are active to actually
3153 * In all honesty I'm not sure if we should
3154 * mark them as active when we receive them
3155 * This is startup only so probably ok.
3157 * If we ever decide to move rib_sweep_table
3158 * to a different spot (ie startup )
3159 * this decision needs to be revisited
3161 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3162 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3163 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3165 rib_uninstall_kernel(rn
, re
);
3166 rib_delnode(rn
, re
);
3171 /* Sweep all RIB tables. */
3172 void rib_sweep_route(void)
3175 struct zebra_vrf
*zvrf
;
3177 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3178 if ((zvrf
= vrf
->info
) == NULL
)
3181 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3182 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3185 zebra_router_sweep_route();
3188 /* Remove specific by protocol routes from 'table'. */
3189 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3190 struct route_table
*table
)
3192 struct route_node
*rn
;
3193 struct route_entry
*re
;
3194 struct route_entry
*next
;
3195 unsigned long n
= 0;
3198 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3199 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3200 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3202 if (re
->type
== proto
3203 && re
->instance
== instance
) {
3204 rib_delnode(rn
, re
);
3211 /* Remove specific by protocol routes. */
3212 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3215 struct zebra_vrf
*zvrf
;
3216 unsigned long cnt
= 0;
3218 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3219 if ((zvrf
= vrf
->info
) != NULL
)
3220 cnt
+= rib_score_proto_table(
3222 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3223 + rib_score_proto_table(
3225 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3227 cnt
+= zebra_router_score_proto(proto
, instance
);
3232 /* Close RIB and clean up kernel routes. */
3233 void rib_close_table(struct route_table
*table
)
3235 struct route_node
*rn
;
3236 rib_table_info_t
*info
;
3242 info
= route_table_get_info(table
);
3244 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3245 dest
= rib_dest_from_rnode(rn
);
3247 if (dest
&& dest
->selected_fib
) {
3248 if (info
->safi
== SAFI_UNICAST
)
3249 hook_call(rib_update
, rn
, NULL
);
3251 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3252 dest
->selected_fib
= NULL
;
3258 * Handler for async dataplane results after a pseudowire installation
3260 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3262 struct zebra_pw
*pw
;
3263 struct zebra_vrf
*vrf
;
3265 /* The pseudowire code assumes success - we act on an error
3266 * result for installation attempts here.
3268 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3271 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3272 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3273 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3275 zebra_pw_install_failure(pw
);
3285 * Handle results from the dataplane system. Dequeue update context
3286 * structs, dispatch to appropriate internal handlers.
3288 static int rib_process_dplane_results(struct thread
*thread
)
3290 struct zebra_dplane_ctx
*ctx
;
3291 struct dplane_ctx_q ctxlist
;
3293 /* Dequeue a list of completed updates with one lock/unlock cycle */
3296 TAILQ_INIT(&ctxlist
);
3298 /* Take lock controlling queue of results */
3299 pthread_mutex_lock(&dplane_mutex
);
3301 /* Dequeue list of context structs */
3302 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3304 pthread_mutex_unlock(&dplane_mutex
);
3306 /* Dequeue context block */
3307 ctx
= dplane_ctx_dequeue(&ctxlist
);
3309 /* If we've emptied the results queue, we're done */
3314 switch (dplane_ctx_get_op(ctx
)) {
3315 case DPLANE_OP_ROUTE_INSTALL
:
3316 case DPLANE_OP_ROUTE_UPDATE
:
3317 case DPLANE_OP_ROUTE_DELETE
:
3318 rib_process_result(ctx
);
3321 case DPLANE_OP_LSP_INSTALL
:
3322 case DPLANE_OP_LSP_UPDATE
:
3323 case DPLANE_OP_LSP_DELETE
:
3324 zebra_mpls_lsp_dplane_result(ctx
);
3327 case DPLANE_OP_PW_INSTALL
:
3328 case DPLANE_OP_PW_UNINSTALL
:
3329 handle_pw_result(ctx
);
3332 case DPLANE_OP_SYS_ROUTE_ADD
:
3333 case DPLANE_OP_SYS_ROUTE_DELETE
:
3334 /* No further processing in zebra for these. */
3335 dplane_ctx_fini(&ctx
);
3339 /* Don't expect this: just return the struct? */
3340 dplane_ctx_fini(&ctx
);
3342 } /* Dispatch by op code */
3344 ctx
= dplane_ctx_dequeue(&ctxlist
);
3349 /* Check for nexthop tracking processing after finishing with results */
3350 do_nht_processing();
3356 * Results are returned from the dataplane subsystem, in the context of
3357 * the dataplane pthread. We enqueue the results here for processing by
3358 * the main thread later.
3360 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3362 /* Take lock controlling queue of results */
3363 pthread_mutex_lock(&dplane_mutex
);
3365 /* Enqueue context blocks */
3366 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3368 pthread_mutex_unlock(&dplane_mutex
);
3370 /* Ensure event is signalled to zebra main pthread */
3371 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3378 * Ensure there are no empty slots in the route_info array.
3379 * Every route type in zebra should be present there.
3381 static void check_route_info(void)
3383 int len
= array_size(route_info
);
3386 * ZEBRA_ROUTE_SYSTEM is special cased since
3387 * its key is 0 anyway.
3389 * ZEBRA_ROUTE_ALL is also ignored.
3391 for (int i
= 0; i
< len
; i
++) {
3392 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3394 assert(route_info
[i
].key
);
3395 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3399 /* Routing information base initialize. */
3406 /* Init dataplane, and register for results */
3407 pthread_mutex_init(&dplane_mutex
, NULL
);
3408 TAILQ_INIT(&rib_dplane_q
);
3409 zebra_dplane_init(rib_dplane_results
);
3415 * Get the first vrf id that is greater than the given vrf id if any.
3417 * Returns TRUE if a vrf id was found, FALSE otherwise.
3419 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3423 vrf
= vrf_lookup_by_id(vrf_id
);
3425 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3427 *next_id_p
= vrf
->vrf_id
;
3436 * rib_tables_iter_next
3438 * Returns the next table in the iteration.
3440 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3442 struct route_table
*table
;
3445 * Array that helps us go over all AFI/SAFI combinations via one
3452 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3453 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3454 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3459 switch (iter
->state
) {
3461 case RIB_TABLES_ITER_S_INIT
:
3462 iter
->vrf_id
= VRF_DEFAULT
;
3463 iter
->afi_safi_ix
= -1;
3467 case RIB_TABLES_ITER_S_ITERATING
:
3468 iter
->afi_safi_ix
++;
3471 while (iter
->afi_safi_ix
3472 < (int)array_size(afi_safis
)) {
3473 table
= zebra_vrf_table(
3474 afi_safis
[iter
->afi_safi_ix
].afi
,
3475 afi_safis
[iter
->afi_safi_ix
].safi
,
3480 iter
->afi_safi_ix
++;
3484 * Found another table in this vrf.
3490 * Done with all tables in the current vrf, go to the
3494 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3497 iter
->afi_safi_ix
= 0;
3502 case RIB_TABLES_ITER_S_DONE
:
3507 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3509 iter
->state
= RIB_TABLES_ITER_S_DONE
;