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 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 4}
105 /* Any new route type added to zebra, should be mirrored here */
107 /* no entry/default: 150 */
110 /* RPF lookup behaviour */
111 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
114 static void __attribute__((format(printf
, 5, 6)))
115 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
116 int priority
, const char *msgfmt
, ...)
118 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
122 va_start(ap
, msgfmt
);
123 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
127 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
128 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
130 if (info
->safi
== SAFI_MULTICAST
)
131 strcat(buf
, " (MRIB)");
133 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
136 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
139 #define rnode_debug(node, vrf_id, ...) \
140 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
141 #define rnode_info(node, ...) \
142 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
144 uint8_t route_distance(int type
)
148 if ((unsigned)type
>= array_size(route_info
))
151 distance
= route_info
[type
].distance
;
156 int is_zebra_valid_kernel_table(uint32_t table_id
)
159 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
160 || (table_id
== RT_TABLE_COMPAT
))
167 int is_zebra_main_routing_table(uint32_t table_id
)
169 if (table_id
== RT_TABLE_MAIN
)
174 int zebra_check_addr(const struct prefix
*p
)
176 if (p
->family
== AF_INET
) {
179 addr
= p
->u
.prefix4
.s_addr
;
182 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
183 || IPV4_LINKLOCAL(addr
))
186 if (p
->family
== AF_INET6
) {
187 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
189 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
195 /* Add nexthop to the end of a rib node's nexthop list */
196 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
198 nexthop_add(&re
->ng
.nexthop
, nexthop
);
204 * copy_nexthop - copy a nexthop to the rib structure.
206 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
208 assert(!re
->ng
.nexthop
);
209 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
210 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
214 /* Delete specified nexthop from the list. */
215 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
218 nexthop
->next
->prev
= nexthop
->prev
;
220 nexthop
->prev
->next
= nexthop
->next
;
222 re
->ng
.nexthop
= nexthop
->next
;
227 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
231 struct nexthop
*nexthop
;
233 nexthop
= nexthop_new();
234 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
235 nexthop
->ifindex
= ifindex
;
236 nexthop
->vrf_id
= nh_vrf_id
;
238 route_entry_nexthop_add(re
, nexthop
);
243 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
244 struct in_addr
*ipv4
,
248 struct nexthop
*nexthop
;
250 nexthop
= nexthop_new();
251 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
252 nexthop
->vrf_id
= nh_vrf_id
;
253 nexthop
->gate
.ipv4
= *ipv4
;
255 nexthop
->src
.ipv4
= *src
;
257 route_entry_nexthop_add(re
, nexthop
);
262 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
263 struct in_addr
*ipv4
,
268 struct nexthop
*nexthop
;
269 struct interface
*ifp
;
271 nexthop
= nexthop_new();
272 nexthop
->vrf_id
= nh_vrf_id
;
273 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
274 nexthop
->gate
.ipv4
= *ipv4
;
276 nexthop
->src
.ipv4
= *src
;
277 nexthop
->ifindex
= ifindex
;
278 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
279 /*Pending: need to think if null ifp here is ok during bootup?
280 There was a crash because ifp here was coming to be NULL */
282 if (connected_is_unnumbered(ifp
))
283 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
285 route_entry_nexthop_add(re
, nexthop
);
290 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
291 struct in6_addr
*ipv6
,
294 struct nexthop
*nexthop
;
296 nexthop
= nexthop_new();
297 nexthop
->vrf_id
= nh_vrf_id
;
298 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
299 nexthop
->gate
.ipv6
= *ipv6
;
301 route_entry_nexthop_add(re
, nexthop
);
306 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
307 struct in6_addr
*ipv6
,
311 struct nexthop
*nexthop
;
313 nexthop
= nexthop_new();
314 nexthop
->vrf_id
= nh_vrf_id
;
315 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
316 nexthop
->gate
.ipv6
= *ipv6
;
317 nexthop
->ifindex
= ifindex
;
319 route_entry_nexthop_add(re
, nexthop
);
324 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
325 enum blackhole_type bh_type
)
327 struct nexthop
*nexthop
;
329 nexthop
= nexthop_new();
330 nexthop
->vrf_id
= VRF_DEFAULT
;
331 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
332 nexthop
->bh_type
= bh_type
;
334 route_entry_nexthop_add(re
, nexthop
);
339 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
340 struct nexthop
*nexthop
)
342 struct nexthop
*resolved_hop
;
344 resolved_hop
= nexthop_new();
345 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
347 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
348 switch (newhop
->type
) {
349 case NEXTHOP_TYPE_IPV4
:
350 case NEXTHOP_TYPE_IPV4_IFINDEX
:
351 /* If the resolving route specifies a gateway, use it */
352 resolved_hop
->type
= newhop
->type
;
353 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
355 if (newhop
->ifindex
) {
356 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
357 resolved_hop
->ifindex
= newhop
->ifindex
;
360 case NEXTHOP_TYPE_IPV6
:
361 case NEXTHOP_TYPE_IPV6_IFINDEX
:
362 resolved_hop
->type
= newhop
->type
;
363 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
365 if (newhop
->ifindex
) {
366 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
367 resolved_hop
->ifindex
= newhop
->ifindex
;
370 case NEXTHOP_TYPE_IFINDEX
:
371 /* If the resolving route is an interface route,
372 * it means the gateway we are looking up is connected
373 * to that interface. (The actual network is _not_ onlink).
374 * Therefore, the resolved route should have the original
375 * gateway as nexthop as it is directly connected.
377 * On Linux, we have to set the onlink netlink flag because
378 * otherwise, the kernel won't accept the route.
380 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
382 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
383 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
384 } else if (afi
== AFI_IP6
) {
385 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
386 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
388 resolved_hop
->ifindex
= newhop
->ifindex
;
390 case NEXTHOP_TYPE_BLACKHOLE
:
391 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
392 resolved_hop
->bh_type
= nexthop
->bh_type
;
396 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
397 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
399 /* Copy labels of the resolved route */
400 if (newhop
->nh_label
)
401 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
402 newhop
->nh_label
->num_labels
,
403 &newhop
->nh_label
->label
[0]);
405 resolved_hop
->rparent
= nexthop
;
406 nexthop_add(&nexthop
->resolved
, resolved_hop
);
410 * Given a nexthop we need to properly recursively resolve
411 * the route. As such, do a table lookup to find and match
412 * if at all possible. Set the nexthop->ifindex as appropriate
414 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
415 struct nexthop
*nexthop
,
416 struct route_node
*top
)
419 struct route_table
*table
;
420 struct route_node
*rn
;
421 struct route_entry
*match
= NULL
;
423 struct nexthop
*newhop
;
424 struct interface
*ifp
;
427 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
428 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
429 nexthop
->ifindex
= 0;
431 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
432 nexthops_free(nexthop
->resolved
);
433 nexthop
->resolved
= NULL
;
437 * If the kernel has sent us a route, then
438 * by golly gee whiz it's a good route.
440 if (re
->type
== ZEBRA_ROUTE_KERNEL
||
441 re
->type
== ZEBRA_ROUTE_SYSTEM
)
445 * Check to see if we should trust the passed in information
446 * for UNNUMBERED interfaces as that we won't find the GW
447 * address in the routing table.
448 * This check should suffice to handle IPv4 or IPv6 routes
449 * sourced from EVPN routes which are installed with the
450 * next hop as the remote VTEP IP.
452 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
453 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
455 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
457 "\t%s: Onlink and interface: %u[%u] does not exist",
458 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
462 if (connected_is_unnumbered(ifp
)) {
463 if (if_is_operative(ifp
))
466 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
468 "\t%s: Onlink and interface %s is not operative",
469 __PRETTY_FUNCTION__
, ifp
->name
);
473 if (!if_is_operative(ifp
)) {
474 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
476 "\t%s: Interface %s is not unnumbered",
477 __PRETTY_FUNCTION__
, ifp
->name
);
482 /* Make lookup prefix. */
483 memset(&p
, 0, sizeof(struct prefix
));
487 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
488 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
492 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
493 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
496 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
500 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
502 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
503 zlog_debug("\t%s: Table not found",
504 __PRETTY_FUNCTION__
);
508 rn
= route_node_match(table
, (struct prefix
*)&p
);
510 route_unlock_node(rn
);
512 /* Lookup should halt if we've matched against ourselves ('top',
513 * if specified) - i.e., we cannot have a nexthop NH1 is
514 * resolved by a route NH1. The exception is if the route is a
517 if (top
&& rn
== top
)
518 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
519 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
520 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
522 "\t%s: Matched against ourself and prefix length is not max bit length",
523 __PRETTY_FUNCTION__
);
527 /* Pick up selected route. */
528 /* However, do not resolve over default route unless explicitly
530 if (is_default_prefix(&rn
->p
)
531 && !rnh_resolve_via_default(p
.family
)) {
532 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
534 "\t:%s: Resolved against default route",
535 __PRETTY_FUNCTION__
);
539 dest
= rib_dest_from_rnode(rn
);
540 if (dest
&& dest
->selected_fib
541 && !CHECK_FLAG(dest
->selected_fib
->status
,
543 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
544 match
= dest
->selected_fib
;
546 /* If there is no selected route or matched route is EGP, go up
551 } while (rn
&& rn
->info
== NULL
);
558 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
559 /* Directly point connected route. */
560 newhop
= match
->ng
.nexthop
;
562 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
563 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
564 nexthop
->ifindex
= newhop
->ifindex
;
567 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
569 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
570 if (!CHECK_FLAG(match
->status
,
571 ROUTE_ENTRY_INSTALLED
))
573 if (CHECK_FLAG(newhop
->flags
,
574 NEXTHOP_FLAG_RECURSIVE
))
577 SET_FLAG(nexthop
->flags
,
578 NEXTHOP_FLAG_RECURSIVE
);
580 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
581 nexthop_set_resolved(afi
, newhop
, nexthop
);
585 re
->nexthop_mtu
= match
->mtu
;
586 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
587 zlog_debug("\t%s: Recursion failed to find",
588 __PRETTY_FUNCTION__
);
590 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
592 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
593 if (!CHECK_FLAG(match
->status
,
594 ROUTE_ENTRY_INSTALLED
))
596 if (CHECK_FLAG(newhop
->flags
,
597 NEXTHOP_FLAG_RECURSIVE
))
600 SET_FLAG(nexthop
->flags
,
601 NEXTHOP_FLAG_RECURSIVE
);
602 nexthop_set_resolved(afi
, newhop
, nexthop
);
606 re
->nexthop_mtu
= match
->mtu
;
608 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
610 "\t%s: Static route unable to resolve",
611 __PRETTY_FUNCTION__
);
614 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
615 zlog_debug("\t%s: Route Type %s has not turned on recursion",
617 zebra_route_string(re
->type
));
618 if (re
->type
== ZEBRA_ROUTE_BGP
&&
619 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
620 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
625 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
626 zlog_debug("\t%s: Nexthop did not lookup in table",
627 __PRETTY_FUNCTION__
);
631 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
632 union g_addr
*addr
, struct route_node
**rn_out
)
635 struct route_table
*table
;
636 struct route_node
*rn
;
637 struct route_entry
*match
= NULL
;
640 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
644 memset(&p
, 0, sizeof(struct prefix
));
647 p
.u
.prefix4
= addr
->ipv4
;
648 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
650 p
.u
.prefix6
= addr
->ipv6
;
651 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
654 rn
= route_node_match(table
, (struct prefix
*)&p
);
659 route_unlock_node(rn
);
661 dest
= rib_dest_from_rnode(rn
);
662 if (dest
&& dest
->selected_fib
663 && !CHECK_FLAG(dest
->selected_fib
->status
,
664 ROUTE_ENTRY_REMOVED
))
665 match
= dest
->selected_fib
;
667 /* If there is no selected route or matched route is EGP, go up
672 } while (rn
&& rn
->info
== NULL
);
676 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
677 if (!CHECK_FLAG(match
->status
,
678 ROUTE_ENTRY_INSTALLED
))
690 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
692 struct route_node
**rn_out
)
694 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
695 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
696 union g_addr gaddr
= {.ipv4
= addr
};
698 switch (ipv4_multicast_mode
) {
699 case MCAST_MRIB_ONLY
:
700 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
702 case MCAST_URIB_ONLY
:
703 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
704 case MCAST_NO_CONFIG
:
705 case MCAST_MIX_MRIB_FIRST
:
706 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
709 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
712 case MCAST_MIX_DISTANCE
:
713 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
714 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
716 re
= ure
->distance
< mre
->distance
? ure
: mre
;
722 case MCAST_MIX_PFXLEN
:
723 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
724 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
726 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
735 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
737 if (IS_ZEBRA_DEBUG_RIB
) {
739 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
741 zlog_debug("%s: %s: vrf: %u found %s, using %s",
742 __func__
, buf
, vrf_id
,
743 mre
? (ure
? "MRIB+URIB" : "MRIB")
744 : ure
? "URIB" : "nothing",
745 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
750 void multicast_mode_ipv4_set(enum multicast_mode mode
)
752 if (IS_ZEBRA_DEBUG_RIB
)
753 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
755 ipv4_multicast_mode
= mode
;
758 enum multicast_mode
multicast_mode_ipv4_get(void)
760 return ipv4_multicast_mode
;
763 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
765 struct route_table
*table
;
766 struct route_node
*rn
;
767 struct route_entry
*match
= NULL
;
771 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
775 rn
= route_node_lookup(table
, (struct prefix
*)p
);
777 /* No route for this prefix. */
782 route_unlock_node(rn
);
783 dest
= rib_dest_from_rnode(rn
);
785 if (dest
&& dest
->selected_fib
786 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
787 match
= dest
->selected_fib
;
792 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
795 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
801 /* This function verifies reachability of one given nexthop, which can be
802 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
803 * in nexthop->flags field. The nexthop->ifindex will be updated
804 * appropriately as well. An existing route map can turn
805 * (otherwise active) nexthop into inactive, but not vice versa.
807 * The return value is the final value of 'ACTIVE' flag.
809 static unsigned nexthop_active_check(struct route_node
*rn
,
810 struct route_entry
*re
,
811 struct nexthop
*nexthop
)
813 struct interface
*ifp
;
814 route_map_result_t ret
= RMAP_MATCH
;
816 char buf
[SRCDEST2STR_BUFFER
];
817 const struct prefix
*p
, *src_p
;
818 struct zebra_vrf
*zvrf
;
820 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
822 if (rn
->p
.family
== AF_INET
)
824 else if (rn
->p
.family
== AF_INET6
)
828 switch (nexthop
->type
) {
829 case NEXTHOP_TYPE_IFINDEX
:
830 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
831 if (ifp
&& if_is_operative(ifp
))
832 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
834 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
836 case NEXTHOP_TYPE_IPV4
:
837 case NEXTHOP_TYPE_IPV4_IFINDEX
:
839 if (nexthop_active(AFI_IP
, re
, nexthop
, rn
))
840 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
842 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
844 case NEXTHOP_TYPE_IPV6
:
846 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
847 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
849 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
851 case NEXTHOP_TYPE_IPV6_IFINDEX
:
852 /* RFC 5549, v4 prefix with v6 NH */
853 if (rn
->p
.family
!= AF_INET
)
855 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
856 ifp
= if_lookup_by_index(nexthop
->ifindex
,
858 if (ifp
&& if_is_operative(ifp
))
859 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
861 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
863 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
864 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
866 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
869 case NEXTHOP_TYPE_BLACKHOLE
:
870 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
875 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
876 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
877 zlog_debug("\t%s: Unable to find a active nexthop",
878 __PRETTY_FUNCTION__
);
882 /* XXX: What exactly do those checks do? Do we support
883 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
885 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
886 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
887 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
889 /* The original code didn't determine the family correctly
890 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
891 * from the rib_table_info in those cases.
892 * Possibly it may be better to use only the rib_table_info
896 rib_table_info_t
*info
;
898 info
= srcdest_rnode_table_info(rn
);
902 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
904 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
906 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
907 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
908 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
911 /* It'll get set if required inside */
912 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
913 nexthop
, zvrf
, re
->tag
);
914 if (ret
== RMAP_DENYMATCH
) {
915 if (IS_ZEBRA_DEBUG_RIB
) {
916 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
918 "%u:%s: Filtering out with NH out %s due to route map",
920 ifindex2ifname(nexthop
->ifindex
,
923 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
925 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
929 * Iterate over all nexthops of the given RIB entry and refresh their
930 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
931 * nexthop is found to toggle the ACTIVE flag, the whole re structure
932 * is flagged with ROUTE_ENTRY_CHANGED.
934 * Return value is the new number of active nexthops.
936 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
938 struct nexthop
*nexthop
;
939 union g_addr prev_src
;
940 unsigned int prev_active
, new_active
;
941 ifindex_t prev_index
;
943 re
->nexthop_active_num
= 0;
944 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
946 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
947 /* No protocol daemon provides src and so we're skipping
949 prev_src
= nexthop
->rmap_src
;
950 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
951 prev_index
= nexthop
->ifindex
;
953 * We need to respect the multipath_num here
954 * as that what we should be able to install from
955 * a multipath perpsective should not be a data plane
958 new_active
= nexthop_active_check(rn
, re
, nexthop
);
960 && re
->nexthop_active_num
>= zrouter
.multipath_num
) {
961 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
965 re
->nexthop_active_num
++;
966 /* Don't allow src setting on IPv6 addr for now */
967 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
968 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
969 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
970 && prev_src
.ipv4
.s_addr
971 != nexthop
->rmap_src
.ipv4
.s_addr
)
972 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
973 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
974 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
975 &nexthop
->rmap_src
.ipv6
)))
976 || CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)) {
977 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
978 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
982 return re
->nexthop_active_num
;
986 * Is this RIB labeled-unicast? It must be of type BGP and all paths
987 * (nexthops) must have a label.
989 int zebra_rib_labeled_unicast(struct route_entry
*re
)
991 struct nexthop
*nexthop
= NULL
;
993 if (re
->type
!= ZEBRA_ROUTE_BGP
)
996 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
997 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1003 /* Update flag indicates whether this is a "replace" or not. Currently, this
1004 * is only used for IPv4.
1006 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1007 struct route_entry
*old
)
1009 struct nexthop
*nexthop
;
1010 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1011 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1012 const struct prefix
*p
, *src_p
;
1013 enum zebra_dplane_result ret
;
1015 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1017 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1019 if (info
->safi
!= SAFI_UNICAST
) {
1020 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1021 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1024 struct nexthop
*prev
;
1026 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1027 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1028 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1029 if (prev
== nexthop
)
1031 if (nexthop_same_firsthop(nexthop
, prev
)) {
1032 SET_FLAG(nexthop
->flags
,
1033 NEXTHOP_FLAG_DUPLICATE
);
1041 * If this is a replace to a new RE let the originator of the RE
1042 * know that they've lost
1044 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1045 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1047 /* Update fib selection */
1048 dest
->selected_fib
= re
;
1051 * Make sure we update the FPM any time we send new information to
1054 hook_call(rib_update
, rn
, "installing in kernel");
1056 /* Send add or update */
1058 ret
= dplane_route_update(rn
, re
, old
);
1060 ret
= dplane_route_add(rn
, re
);
1063 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1064 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1066 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
1068 zvrf
->installs_queued
++;
1070 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1072 char str
[SRCDEST2STR_BUFFER
];
1074 srcdest_rnode2str(rn
, str
, sizeof(str
));
1075 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1076 "%u:%s: Failed to enqueue dataplane install",
1080 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1089 /* Uninstall the route from kernel. */
1090 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1092 struct nexthop
*nexthop
;
1093 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1094 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1096 if (info
->safi
!= SAFI_UNICAST
) {
1097 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1098 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1099 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1104 * Make sure we update the FPM any time we send new information to
1107 hook_call(rib_update
, rn
, "uninstalling from kernel");
1109 switch (dplane_route_delete(rn
, re
)) {
1110 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1112 zvrf
->removals_queued
++;
1114 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1116 char str
[SRCDEST2STR_BUFFER
];
1118 srcdest_rnode2str(rn
, str
, sizeof(str
));
1119 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1120 "%u:%s: Failed to enqueue dataplane uninstall",
1124 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1133 /* Uninstall the route from kernel. */
1134 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1136 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1137 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1138 struct nexthop
*nexthop
;
1140 if (dest
&& dest
->selected_fib
== re
) {
1141 if (info
->safi
== SAFI_UNICAST
)
1142 hook_call(rib_update
, rn
, "rib_uninstall");
1144 /* If labeled-unicast route, uninstall transit LSP. */
1145 if (zebra_rib_labeled_unicast(re
))
1146 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1148 rib_uninstall_kernel(rn
, re
);
1150 dest
->selected_fib
= NULL
;
1152 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1153 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1156 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1157 const struct prefix
*p
, *src_p
;
1159 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1161 redistribute_delete(p
, src_p
, re
);
1162 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1167 * rib_can_delete_dest
1169 * Returns TRUE if the given dest can be deleted from the table.
1171 static int rib_can_delete_dest(rib_dest_t
*dest
)
1173 if (re_list_first(&dest
->routes
)) {
1178 * Unresolved rnh's are stored on the default route's list
1180 * dest->rnode can also be the source prefix node in an
1181 * ipv6 sourcedest table. Fortunately the prefix of a
1182 * source prefix node can never be the default prefix.
1184 if (is_default_prefix(&dest
->rnode
->p
))
1188 * Don't delete the dest if we have to update the FPM about this
1191 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1192 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1198 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
1200 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1204 * We are storing the rnh's associated withb
1205 * the tracked nexthop as a list of the rn's.
1206 * Unresolved rnh's are placed at the top
1207 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
1208 * As such for each rn we need to walk up the tree
1209 * and see if any rnh's need to see if they
1210 * would match a more specific route
1213 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
1214 char buf
[PREFIX_STRLEN
];
1216 zlog_debug("%s: %s Being examined for Nexthop Tracking",
1217 __PRETTY_FUNCTION__
,
1218 srcdest_rnode2str(rn
, buf
, sizeof(buf
)));
1223 dest
= rib_dest_from_rnode(rn
);
1227 * If we have any rnh's stored in the nht list
1228 * then we know that this route node was used for
1229 * nht resolution and as such we need to call the
1230 * nexthop tracking evaluation code
1232 frr_each (rnh_list
, &dest
->nht
, rnh
) {
1233 struct zebra_vrf
*zvrf
=
1234 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
1235 struct prefix
*p
= &rnh
->node
->p
;
1237 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
1238 char buf1
[PREFIX_STRLEN
];
1239 char buf2
[PREFIX_STRLEN
];
1241 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
1243 srcdest_rnode2str(rn
, buf1
,
1245 prefix2str(p
, buf2
, sizeof(buf2
)),
1250 * If we have evaluated this node on this pass
1251 * already, due to following the tree up
1252 * then we know that we can move onto the next
1255 * Additionally we call zebra_evaluate_rnh
1256 * when we gc the dest. In this case we know
1257 * that there must be no other re's where
1258 * we were originally as such we know that
1259 * that sequence number is ok to respect.
1261 if (rnh
->seqno
== seq
) {
1262 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1264 "\tNode processed and moved already");
1269 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
1275 dest
= rib_dest_from_rnode(rn
);
1282 * Garbage collect the rib dest corresponding to the given route node
1285 * Returns TRUE if the dest was deleted, FALSE otherwise.
1287 int rib_gc_dest(struct route_node
*rn
)
1291 dest
= rib_dest_from_rnode(rn
);
1295 if (!rib_can_delete_dest(dest
))
1298 if (IS_ZEBRA_DEBUG_RIB
) {
1299 struct zebra_vrf
*zvrf
;
1301 zvrf
= rib_dest_vrf(dest
);
1302 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1305 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
1308 rnh_list_fini(&dest
->nht
);
1309 XFREE(MTYPE_RIB_DEST
, dest
);
1313 * Release the one reference that we keep on the route node.
1315 route_unlock_node(rn
);
1319 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1320 struct route_entry
*new)
1322 hook_call(rib_update
, rn
, "new route selected");
1324 /* Update real nexthop. This may actually determine if nexthop is active
1326 if (!nexthop_group_active_nexthop_num(&new->ng
)) {
1327 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1331 if (IS_ZEBRA_DEBUG_RIB
) {
1332 char buf
[SRCDEST2STR_BUFFER
];
1333 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1334 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1335 zvrf_id(zvrf
), buf
, rn
, new,
1336 zebra_route_string(new->type
));
1339 /* If labeled-unicast route, install transit LSP. */
1340 if (zebra_rib_labeled_unicast(new))
1341 zebra_mpls_lsp_install(zvrf
, rn
, new);
1343 rib_install_kernel(rn
, new, NULL
);
1345 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1348 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1349 struct route_entry
*old
)
1351 hook_call(rib_update
, rn
, "removing existing route");
1353 /* Uninstall from kernel. */
1354 if (IS_ZEBRA_DEBUG_RIB
) {
1355 char buf
[SRCDEST2STR_BUFFER
];
1356 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1357 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1358 zvrf_id(zvrf
), buf
, rn
, old
,
1359 zebra_route_string(old
->type
));
1362 /* If labeled-unicast route, uninstall transit LSP. */
1363 if (zebra_rib_labeled_unicast(old
))
1364 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1366 rib_uninstall_kernel(rn
, old
);
1368 /* Update nexthop for route, reset changed flag. */
1369 /* Note: this code also handles the Linux case when an interface goes
1370 * down, causing the kernel to delete routes without sending DELROUTE
1373 if (RIB_KERNEL_ROUTE(old
))
1374 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1376 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1379 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1380 struct route_node
*rn
,
1381 struct route_entry
*old
,
1382 struct route_entry
*new)
1387 * We have to install or update if a new route has been selected or
1388 * something has changed.
1390 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1391 hook_call(rib_update
, rn
, "updating existing route");
1393 /* Update the nexthop; we could determine here that nexthop is
1395 if (nexthop_group_active_nexthop_num(&new->ng
))
1398 /* If nexthop is active, install the selected route, if
1400 * the install succeeds, cleanup flags for prior route, if
1405 if (IS_ZEBRA_DEBUG_RIB
) {
1406 char buf
[SRCDEST2STR_BUFFER
];
1407 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1410 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
1411 zvrf_id(zvrf
), buf
, rn
, new,
1412 zebra_route_string(new->type
),
1414 zebra_route_string(old
->type
));
1417 "%u:%s: Updating route rn %p, re %p (%s)",
1418 zvrf_id(zvrf
), buf
, rn
, new,
1419 zebra_route_string(new->type
));
1422 /* If labeled-unicast route, uninstall transit LSP. */
1423 if (zebra_rib_labeled_unicast(old
))
1424 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1427 * Non-system route should be installed.
1428 * If labeled-unicast route, install transit
1431 if (zebra_rib_labeled_unicast(new))
1432 zebra_mpls_lsp_install(zvrf
, rn
, new);
1434 rib_install_kernel(rn
, new, old
);
1438 * If nexthop for selected route is not active or install
1440 * may need to uninstall and delete for redistribution.
1443 if (IS_ZEBRA_DEBUG_RIB
) {
1444 char buf
[SRCDEST2STR_BUFFER
];
1445 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1448 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1449 zvrf_id(zvrf
), buf
, rn
, new,
1450 zebra_route_string(new->type
),
1452 zebra_route_string(old
->type
));
1455 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
1456 zvrf_id(zvrf
), buf
, rn
, new,
1457 zebra_route_string(new->type
));
1460 /* If labeled-unicast route, uninstall transit LSP. */
1461 if (zebra_rib_labeled_unicast(old
))
1462 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1464 rib_uninstall_kernel(rn
, old
);
1468 * Same route selected; check if in the FIB and if not,
1469 * re-install. This is housekeeping code to deal with
1470 * race conditions in kernel with linux netlink reporting
1471 * interface up before IPv4 or IPv6 protocol is ready
1474 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1475 RIB_SYSTEM_ROUTE(new))
1476 rib_install_kernel(rn
, new, NULL
);
1479 /* Update prior route. */
1481 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1483 /* Clear changed flag. */
1484 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1487 /* Check if 'alternate' RIB entry is better than 'current'. */
1488 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1489 struct route_entry
*alternate
)
1491 if (current
== NULL
)
1494 /* filter route selection in following order:
1495 * - connected beats other types
1496 * - if both connected, loopback or vrf wins
1497 * - lower distance beats higher
1498 * - lower metric beats higher for equal distance
1499 * - last, hence oldest, route wins tie break.
1502 /* Connected routes. Check to see if either are a vrf
1503 * or loopback interface. If not, pick the last connected
1504 * route of the set of lowest metric connected routes.
1506 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1507 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1510 /* both are connected. are either loop or vrf? */
1511 struct nexthop
*nexthop
= NULL
;
1513 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1514 if (if_is_loopback_or_vrf(if_lookup_by_index(
1515 nexthop
->ifindex
, alternate
->vrf_id
)))
1519 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1520 if (if_is_loopback_or_vrf(if_lookup_by_index(
1521 nexthop
->ifindex
, current
->vrf_id
)))
1525 /* Neither are loop or vrf so pick best metric */
1526 if (alternate
->metric
<= current
->metric
)
1532 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1535 /* higher distance loses */
1536 if (alternate
->distance
< current
->distance
)
1538 if (current
->distance
< alternate
->distance
)
1541 /* metric tie-breaks equal distance */
1542 if (alternate
->metric
<= current
->metric
)
1548 /* Core function for processing routing information base. */
1549 static void rib_process(struct route_node
*rn
)
1551 struct route_entry
*re
;
1552 struct route_entry
*next
;
1553 struct route_entry
*old_selected
= NULL
;
1554 struct route_entry
*new_selected
= NULL
;
1555 struct route_entry
*old_fib
= NULL
;
1556 struct route_entry
*new_fib
= NULL
;
1557 struct route_entry
*best
= NULL
;
1558 char buf
[SRCDEST2STR_BUFFER
];
1560 struct zebra_vrf
*zvrf
= NULL
;
1561 const struct prefix
*p
, *src_p
;
1563 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1564 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1568 dest
= rib_dest_from_rnode(rn
);
1570 zvrf
= rib_dest_vrf(dest
);
1571 vrf_id
= zvrf_id(zvrf
);
1574 if (IS_ZEBRA_DEBUG_RIB
)
1575 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1577 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1578 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1581 * we can have rn's that have a NULL info pointer
1582 * (dest). As such let's not let the deref happen
1583 * additionally we know RNODE_FOREACH_RE_SAFE
1584 * will not iterate so we are ok.
1587 old_fib
= dest
->selected_fib
;
1589 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1590 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1592 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1593 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1594 re
->status
, re
->flags
, re
->distance
,
1597 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1599 /* Currently selected re. */
1600 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1601 assert(old_selected
== NULL
);
1605 /* Skip deleted entries from selection */
1606 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1609 /* Skip unreachable nexthop. */
1610 /* This first call to nexthop_active_update is merely to
1611 * determine if there's any change to nexthops associated
1612 * with this RIB entry. Now, rib_process() can be invoked due
1613 * to an external event such as link down or due to
1614 * next-hop-tracking evaluation. In the latter case,
1615 * a decision has already been made that the NHs have changed.
1616 * So, no need to invoke a potentially expensive call again.
1617 * Further, since the change might be in a recursive NH which
1618 * is not caught in the nexthop_active_update() code. Thus, we
1619 * might miss changes to recursive NHs.
1621 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1622 && !nexthop_active_update(rn
, re
)) {
1623 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1624 /* XXX: HERE BE DRAGONS!!!!!
1625 * In all honesty, I have not yet figured out
1626 * what this part does or why the
1627 * ROUTE_ENTRY_CHANGED test above is correct
1628 * or why we need to delete a route here, and
1629 * also not whether this concerns both selected
1630 * and fib route, or only selected
1633 * This entry was denied by the 'ip protocol
1634 * table' route-map, we need to delete it */
1635 if (re
!= old_selected
) {
1636 if (IS_ZEBRA_DEBUG_RIB
)
1638 "%s: %u:%s: imported via import-table but denied "
1639 "by the ip protocol table route-map",
1640 __func__
, vrf_id
, buf
);
1643 SET_FLAG(re
->status
,
1644 ROUTE_ENTRY_REMOVED
);
1650 /* Infinite distance. */
1651 if (re
->distance
== DISTANCE_INFINITY
) {
1652 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1656 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1657 best
= rib_choose_best(new_fib
, re
);
1658 if (new_fib
&& best
!= new_fib
)
1659 UNSET_FLAG(new_fib
->status
,
1660 ROUTE_ENTRY_CHANGED
);
1663 best
= rib_choose_best(new_selected
, re
);
1664 if (new_selected
&& best
!= new_selected
)
1665 UNSET_FLAG(new_selected
->status
,
1666 ROUTE_ENTRY_CHANGED
);
1667 new_selected
= best
;
1670 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1671 } /* RNODE_FOREACH_RE */
1673 /* If no FIB override route, use the selected route also for FIB */
1674 if (new_fib
== NULL
)
1675 new_fib
= new_selected
;
1677 /* After the cycle is finished, the following pointers will be set:
1678 * old_selected --- RE entry currently having SELECTED
1679 * new_selected --- RE entry that is newly SELECTED
1680 * old_fib --- RE entry currently in kernel FIB
1681 * new_fib --- RE entry that is newly to be in kernel FIB
1683 * new_selected will get SELECTED flag, and is going to be redistributed
1684 * the zclients. new_fib (which can be new_selected) will be installed
1688 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1690 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1691 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1692 (void *)old_fib
, (void *)new_fib
);
1695 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1696 * fib == selected */
1697 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1698 ROUTE_ENTRY_CHANGED
);
1700 /* Update fib according to selection results */
1701 if (new_fib
&& old_fib
)
1702 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1704 rib_process_add_fib(zvrf
, rn
, new_fib
);
1706 rib_process_del_fib(zvrf
, rn
, old_fib
);
1708 /* Update SELECTED entry */
1709 if (old_selected
!= new_selected
|| selected_changed
) {
1711 if (new_selected
&& new_selected
!= new_fib
)
1712 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1715 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1719 redistribute_delete(p
, src_p
, old_selected
);
1720 if (old_selected
!= new_selected
)
1721 UNSET_FLAG(old_selected
->flags
,
1722 ZEBRA_FLAG_SELECTED
);
1726 /* Remove all RE entries queued for removal */
1727 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1728 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1729 if (IS_ZEBRA_DEBUG_RIB
) {
1730 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1731 (void *)rn
, (void *)re
);
1738 * Check if the dest can be deleted now.
1743 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1745 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1746 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1751 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1752 if (IS_ZEBRA_DEBUG_MPLS
)
1754 "%u: Scheduling all LSPs upon RIB completion",
1756 zebra_mpls_lsp_schedule(zvrf
);
1757 mpls_unmark_lsps_for_processing(rn
);
1762 * Utility to match route with dplane context data
1764 static bool rib_route_match_ctx(const struct route_entry
*re
,
1765 const struct zebra_dplane_ctx
*ctx
,
1768 bool result
= false;
1772 * In 'update' case, we test info about the 'previous' or
1775 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1776 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1779 /* TODO -- we're using this extra test, but it's not
1780 * exactly clear why.
1782 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1783 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1784 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1791 * Ordinary, single-route case using primary context info
1793 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1794 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1795 /* Skip route that's been deleted */
1799 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1800 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1803 /* TODO -- we're using this extra test, but it's not
1804 * exactly clear why.
1806 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1807 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1808 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1819 static void zebra_rib_fixup_system(struct route_node
*rn
)
1821 struct route_entry
*re
;
1823 RNODE_FOREACH_RE(rn
, re
) {
1824 struct nexthop
*nhop
;
1826 if (!RIB_SYSTEM_ROUTE(re
))
1829 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1832 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1834 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1835 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1838 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1844 * Route-update results processing after async dataplane update.
1846 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1848 struct route_table
*table
= NULL
;
1849 struct zebra_vrf
*zvrf
= NULL
;
1850 struct route_node
*rn
= NULL
;
1851 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1852 bool is_update
= false;
1853 struct nexthop
*nexthop
, *ctx_nexthop
;
1854 char dest_str
[PREFIX_STRLEN
] = "";
1855 enum dplane_op_e op
;
1856 enum zebra_dplane_result status
;
1857 const struct prefix
*dest_pfx
, *src_pfx
;
1860 /* Locate rn and re(s) from ctx */
1862 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1863 dplane_ctx_get_safi(ctx
),
1864 dplane_ctx_get_vrf(ctx
),
1865 dplane_ctx_get_table(ctx
));
1866 if (table
== NULL
) {
1867 if (IS_ZEBRA_DEBUG_DPLANE
) {
1868 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1869 dplane_ctx_get_afi(ctx
),
1870 dplane_ctx_get_safi(ctx
),
1871 dplane_ctx_get_vrf(ctx
));
1876 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1878 dest_pfx
= dplane_ctx_get_dest(ctx
);
1880 /* Note well: only capturing the prefix string if debug is enabled here;
1881 * unconditional log messages will have to generate the string.
1883 if (IS_ZEBRA_DEBUG_DPLANE
)
1884 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1886 src_pfx
= dplane_ctx_get_src(ctx
);
1887 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1888 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1890 if (IS_ZEBRA_DEBUG_DPLANE
) {
1891 zlog_debug("Failed to process dplane results: no route for %u:%s",
1892 dplane_ctx_get_vrf(ctx
), dest_str
);
1897 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1899 op
= dplane_ctx_get_op(ctx
);
1900 status
= dplane_ctx_get_status(ctx
);
1902 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1903 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1904 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1905 dplane_op2str(op
), dplane_res2str(status
));
1908 * Update is a bit of a special case, where we may have both old and new
1909 * routes to post-process.
1911 is_update
= dplane_ctx_is_update(ctx
);
1914 * Take a pass through the routes, look for matches with the context
1917 RNODE_FOREACH_RE(rn
, rib
) {
1920 if (rib_route_match_ctx(rib
, ctx
, false))
1924 /* Check for old route match */
1925 if (is_update
&& (old_re
== NULL
)) {
1926 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1930 /* Have we found the routes we need to work on? */
1931 if (re
&& ((!is_update
|| old_re
)))
1935 seq
= dplane_ctx_get_seq(ctx
);
1938 * Check sequence number(s) to detect stale results before continuing
1941 if (re
->dplane_sequence
!= seq
) {
1942 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1943 zlog_debug("%u:%s Stale dplane result for re %p",
1944 dplane_ctx_get_vrf(ctx
),
1947 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1951 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1952 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1953 zlog_debug("%u:%s Stale dplane result for old_re %p",
1954 dplane_ctx_get_vrf(ctx
),
1957 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1961 case DPLANE_OP_ROUTE_INSTALL
:
1962 case DPLANE_OP_ROUTE_UPDATE
:
1963 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1965 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1966 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1969 * On an update operation from the same route type
1970 * context retrieval currently has no way to know
1971 * which was the old and which was the new.
1972 * So don't unset our flags that we just set.
1973 * We know redistribution is ok because the
1974 * old_re in this case is used for nothing
1975 * more than knowing whom to contact if necessary.
1977 if (old_re
&& old_re
!= re
) {
1978 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1979 UNSET_FLAG(old_re
->status
,
1980 ROUTE_ENTRY_INSTALLED
);
1982 /* Update zebra nexthop FIB flag for each
1983 * nexthop that was installed.
1985 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1991 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1992 if (nexthop_same(ctx_nexthop
, nexthop
))
1996 if (nexthop
== NULL
)
1999 if (CHECK_FLAG(nexthop
->flags
,
2000 NEXTHOP_FLAG_RECURSIVE
))
2003 if (CHECK_FLAG(ctx_nexthop
->flags
,
2005 SET_FLAG(nexthop
->flags
,
2008 UNSET_FLAG(nexthop
->flags
,
2013 * System routes are weird in that they
2014 * allow multiple to be installed that match
2015 * to the same prefix, so after we get the
2016 * result we need to clean them up so that
2017 * we can actually use them.
2019 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2020 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2021 zebra_rib_fixup_system(rn
);
2028 * TODO -- still calling the redist api using the
2029 * route_entries, and there's a corner-case here:
2030 * if there's no client for the 'new' route, a redist
2031 * deleting the 'old' route will be sent. But if the
2032 * 'old' context info was stale, 'old_re' will be
2033 * NULL here and that delete will not be sent.
2036 redistribute_update(dest_pfx
, src_pfx
,
2039 /* Notify route owner */
2040 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2044 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2045 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2047 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2049 zsend_route_notify_owner(re
, dest_pfx
,
2050 ZAPI_ROUTE_FAIL_INSTALL
);
2052 zlog_warn("%u:%s: Route install failed",
2053 dplane_ctx_get_vrf(ctx
),
2054 prefix2str(dest_pfx
,
2055 dest_str
, sizeof(dest_str
)));
2058 case DPLANE_OP_ROUTE_DELETE
:
2060 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2062 * In the delete case, the zebra core datastructs were
2063 * updated (or removed) at the time the delete was issued,
2064 * so we're just notifying the route owner.
2066 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2068 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2069 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2071 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2077 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2078 zsend_route_notify_owner_ctx(ctx
,
2079 ZAPI_ROUTE_REMOVE_FAIL
);
2081 zlog_warn("%u:%s: Route Deletion failure",
2082 dplane_ctx_get_vrf(ctx
),
2083 prefix2str(dest_pfx
,
2084 dest_str
, sizeof(dest_str
)));
2088 * System routes are weird in that they
2089 * allow multiple to be installed that match
2090 * to the same prefix, so after we get the
2091 * result we need to clean them up so that
2092 * we can actually use them.
2094 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2095 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2096 zebra_rib_fixup_system(rn
);
2102 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
2103 zebra_rib_evaluate_mpls(rn
);
2107 route_unlock_node(rn
);
2109 /* Return context to dataplane module */
2110 dplane_ctx_fini(&ctx
);
2113 /* Take a list of route_node structs and return 1, if there was a record
2114 * picked from it and processed by rib_process(). Don't process more,
2115 * than one RN record; operate only in the specified sub-queue.
2117 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2119 struct listnode
*lnode
= listhead(subq
);
2120 struct route_node
*rnode
;
2122 struct zebra_vrf
*zvrf
= NULL
;
2127 rnode
= listgetdata(lnode
);
2128 dest
= rib_dest_from_rnode(rnode
);
2130 zvrf
= rib_dest_vrf(dest
);
2134 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2135 char buf
[SRCDEST2STR_BUFFER
];
2136 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2137 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2138 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2142 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2143 RIB_ROUTE_QUEUED(qindex
));
2148 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2149 __func__
, rnode
, rnode
->lock
);
2150 zlog_backtrace(LOG_DEBUG
);
2153 route_unlock_node(rnode
);
2154 list_delete_node(subq
, lnode
);
2160 * Perform next-hop tracking processing after RIB updates.
2162 static void do_nht_processing(void)
2166 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2167 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2169 * is pointed to the meta queue structure.
2171 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2173 struct meta_queue
*mq
= data
;
2175 uint32_t queue_len
, queue_limit
;
2177 /* Ensure there's room for more dataplane updates */
2178 queue_limit
= dplane_get_in_queue_limit();
2179 queue_len
= dplane_get_in_queue_len();
2180 if (queue_len
> queue_limit
) {
2181 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2182 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2183 queue_len
, queue_limit
);
2185 /* Ensure that the meta-queue is actually enqueued */
2186 if (work_queue_empty(zrouter
.ribq
))
2187 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2189 return WQ_QUEUE_BLOCKED
;
2192 for (i
= 0; i
< MQ_SIZE
; i
++)
2193 if (process_subq(mq
->subq
[i
], i
)) {
2197 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2202 * Look into the RN and queue it into the highest priority queue
2203 * at this point in time for processing.
2205 * We will enqueue a route node only once per invocation.
2207 * There are two possibilities here that should be kept in mind.
2208 * If the original invocation has not been pulled off for processing
2209 * yet, A subsuquent invocation can have a route entry with a better
2210 * meta queue index value and we can have a situation where
2211 * we might have the same node enqueued 2 times. Not necessarily
2212 * an optimal situation but it should be ok.
2214 * The other possibility is that the original invocation has not
2215 * been pulled off for processing yet, A subsusquent invocation
2216 * doesn't have a route_entry with a better meta-queue and the
2217 * original metaqueue index value will win and we'll end up with
2218 * the route node enqueued once.
2220 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2222 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2223 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2225 RNODE_FOREACH_RE (rn
, curr_re
) {
2226 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2228 if (curr_qindex
<= qindex
) {
2230 qindex
= curr_qindex
;
2237 /* Invariant: at this point we always have rn->info set. */
2238 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2239 RIB_ROUTE_QUEUED(qindex
))) {
2240 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2241 rnode_debug(rn
, re
->vrf_id
,
2242 "rn %p is already queued in sub-queue %u",
2243 (void *)rn
, qindex
);
2247 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2248 listnode_add(mq
->subq
[qindex
], rn
);
2249 route_lock_node(rn
);
2252 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2253 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2254 (void *)rn
, qindex
);
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_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2352 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2353 rnh_list_init(&dest
->nht
);
2354 route_lock_node(rn
); /* rn route table reference */
2361 /* RIB updates are processed via a queue of pointers to route_nodes.
2363 * The queue length is bounded by the maximal size of the routing table,
2364 * as a route_node will not be requeued, if already queued.
2366 * REs are submitted via rib_addnode or rib_delnode which set minimal
2367 * state, or static_install_route (when an existing RE is updated)
2368 * and then submit route_node to queue for best-path selection later.
2369 * Order of add/delete state changes are preserved for any given RE.
2371 * Deleted REs are reaped during best-path selection.
2374 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2375 * |-------->| | best RE, if required
2377 * static_install->|->rib_addqueue...... -> rib_process
2379 * |-------->| |-> rib_unlink
2380 * |-> set ROUTE_ENTRY_REMOVE |
2381 * rib_delnode (RE freed)
2383 * The 'info' pointer of a route_node points to a rib_dest_t
2384 * ('dest'). Queueing state for a route_node is kept on the dest. The
2385 * dest is created on-demand by rib_link() and is kept around at least
2386 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2388 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2390 * - route_nodes: refcounted by:
2391 * - dest attached to route_node:
2392 * - managed by: rib_link/rib_gc_dest
2393 * - route_node processing queue
2394 * - managed by: rib_addqueue, rib_process.
2398 /* Add RE to head of the route node. */
2399 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2403 const char *rmap_name
;
2407 dest
= rib_dest_from_rnode(rn
);
2409 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2410 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2412 dest
= zebra_rib_create_dest(rn
);
2415 re_list_add_head(&dest
->routes
, re
);
2417 afi
= (rn
->p
.family
== AF_INET
)
2419 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2420 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2421 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2422 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2427 static void rib_addnode(struct route_node
*rn
,
2428 struct route_entry
*re
, int process
)
2430 /* RE node has been un-removed before route-node is processed.
2431 * route_node must hence already be on the queue for processing..
2433 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2434 if (IS_ZEBRA_DEBUG_RIB
)
2435 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2436 (void *)rn
, (void *)re
);
2438 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2441 rib_link(rn
, re
, process
);
2447 * Detach a rib structure from a route_node.
2449 * Note that a call to rib_unlink() should be followed by a call to
2450 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2451 * longer required to be deleted.
2453 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2459 if (IS_ZEBRA_DEBUG_RIB
)
2460 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2463 dest
= rib_dest_from_rnode(rn
);
2465 re_list_del(&dest
->routes
, re
);
2467 if (dest
->selected_fib
== re
)
2468 dest
->selected_fib
= NULL
;
2470 nexthops_free(re
->ng
.nexthop
);
2471 XFREE(MTYPE_RE
, re
);
2474 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2478 if (IS_ZEBRA_DEBUG_RIB
)
2479 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2480 (void *)rn
, (void *)re
);
2481 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2483 afi
= (rn
->p
.family
== AF_INET
)
2485 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2486 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2487 zebra_del_import_table_entry(rn
, re
);
2488 /* Just clean up if non main table */
2489 if (IS_ZEBRA_DEBUG_RIB
) {
2490 char buf
[SRCDEST2STR_BUFFER
];
2491 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2492 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2493 re
->vrf_id
, buf
, rn
, re
,
2494 zebra_route_string(re
->type
));
2503 /* This function dumps the contents of a given RE entry into
2504 * standard debug log. Calling function name and IP prefix in
2505 * question are passed as 1st and 2nd arguments.
2508 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2509 union prefixconstptr src_pp
,
2510 const struct route_entry
*re
)
2512 const struct prefix
*src_p
= src_pp
.p
;
2513 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2514 char straddr
[PREFIX_STRLEN
];
2515 char srcaddr
[PREFIX_STRLEN
];
2516 struct nexthop
*nexthop
;
2518 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2519 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2520 is_srcdst
? " from " : "",
2521 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2524 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2525 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2528 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2529 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2530 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2531 re
->nexthop_num
, re
->nexthop_active_num
);
2533 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2534 struct interface
*ifp
;
2535 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2537 switch (nexthop
->type
) {
2538 case NEXTHOP_TYPE_BLACKHOLE
:
2539 sprintf(straddr
, "Blackhole");
2541 case NEXTHOP_TYPE_IFINDEX
:
2542 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2544 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2546 case NEXTHOP_TYPE_IPV4
:
2548 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2549 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2552 case NEXTHOP_TYPE_IPV6
:
2553 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2554 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2558 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2559 func
, (nexthop
->rparent
? " NH" : "NH"), straddr
,
2560 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2562 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2565 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2568 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2571 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2574 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2577 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2581 zlog_debug("%s: dump complete", func
);
2584 /* This is an exported helper to rtm_read() to dump the strange
2585 * RE entry found by rib_lookup_ipv4_route()
2588 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2590 struct route_table
*table
;
2591 struct route_node
*rn
;
2592 struct route_entry
*re
;
2593 char prefix_buf
[INET_ADDRSTRLEN
];
2596 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2598 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2599 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2604 /* Scan the RIB table for exactly matching RE entry. */
2605 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2607 /* No route for this prefix. */
2609 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2610 prefix2str((struct prefix
*)p
, prefix_buf
,
2611 sizeof(prefix_buf
)));
2616 route_unlock_node(rn
);
2619 RNODE_FOREACH_RE (rn
, re
) {
2620 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2622 (void *)rn
, (void *)re
,
2623 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2626 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2629 route_entry_dump(p
, NULL
, re
);
2633 /* Check if requested address assignment will fail due to another
2634 * route being installed by zebra in FIB already. Take necessary
2635 * actions, if needed: remove such a route from FIB and deSELECT
2636 * corresponding RE entry. Then put affected RN into RIBQ head.
2638 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2640 struct route_table
*table
;
2641 struct route_node
*rn
;
2644 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2645 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2646 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2651 /* No matches would be the simplest case. */
2652 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2656 route_unlock_node(rn
);
2658 dest
= rib_dest_from_rnode(rn
);
2659 /* Check all RE entries. In case any changes have to be done, requeue
2660 * the RN into RIBQ head. If the routing message about the new connected
2661 * route (generated by the IP address we are going to assign very soon)
2662 * comes before the RIBQ is processed, the new RE entry will join
2663 * RIBQ record already on head. This is necessary for proper
2665 * of the rest of the RE.
2667 if (dest
->selected_fib
) {
2668 if (IS_ZEBRA_DEBUG_RIB
) {
2669 char buf
[PREFIX_STRLEN
];
2671 zlog_debug("%u:%s: freeing way for connected prefix",
2672 dest
->selected_fib
->vrf_id
,
2673 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2674 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2676 rib_uninstall(rn
, dest
->selected_fib
);
2681 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2682 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2684 struct route_table
*table
;
2685 struct route_node
*rn
;
2686 struct route_entry
*same
= NULL
;
2692 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2695 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2697 XFREE(MTYPE_RE
, re
);
2701 /* Make it sure prefixlen is applied to the prefix. */
2704 apply_mask_ipv6(src_p
);
2706 /* Set default distance by route type. */
2707 if (re
->distance
== 0) {
2708 re
->distance
= route_distance(re
->type
);
2710 /* iBGP distance is 200. */
2711 if (re
->type
== ZEBRA_ROUTE_BGP
2712 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2716 /* Lookup route node.*/
2717 rn
= srcdest_rnode_get(table
, p
, src_p
);
2720 * If same type of route are installed, treat it as a implicit
2722 * If the user has specified the No route replace semantics
2723 * for the install don't do a route replace.
2725 RNODE_FOREACH_RE (rn
, same
) {
2726 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2729 if (same
->type
!= re
->type
)
2731 if (same
->instance
!= re
->instance
)
2733 if (same
->type
== ZEBRA_ROUTE_KERNEL
2734 && same
->metric
!= re
->metric
)
2737 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2738 same
->distance
!= re
->distance
)
2742 * We should allow duplicate connected routes
2743 * because of IPv6 link-local routes and unnumbered
2744 * interfaces on Linux.
2746 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2750 /* If this route is kernel/connected route, notify the dataplane. */
2751 if (RIB_SYSTEM_ROUTE(re
)) {
2752 /* Notify dataplane */
2753 dplane_sys_route_add(rn
, re
);
2756 /* Link new re to node.*/
2757 if (IS_ZEBRA_DEBUG_RIB
) {
2758 rnode_debug(rn
, re
->vrf_id
,
2759 "Inserting route rn %p, re %p (%s) existing %p",
2760 rn
, re
, zebra_route_string(re
->type
), same
);
2762 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2763 route_entry_dump(p
, src_p
, re
);
2766 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2767 rib_addnode(rn
, re
, 1);
2770 /* Free implicit route.*/
2772 rib_delnode(rn
, same
);
2776 route_unlock_node(rn
);
2780 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2781 unsigned short instance
, int flags
, struct prefix
*p
,
2782 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2783 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2786 struct route_table
*table
;
2787 struct route_node
*rn
;
2788 struct route_entry
*re
;
2789 struct route_entry
*fib
= NULL
;
2790 struct route_entry
*same
= NULL
;
2791 struct nexthop
*rtnh
;
2792 char buf2
[INET6_ADDRSTRLEN
];
2795 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2798 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2805 apply_mask_ipv6(src_p
);
2807 /* Lookup route node. */
2808 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2810 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2812 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2813 if (src_p
&& src_p
->prefixlen
)
2814 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2818 if (IS_ZEBRA_DEBUG_RIB
)
2819 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2821 (src_buf
[0] != '\0') ? " from " : "",
2826 dest
= rib_dest_from_rnode(rn
);
2827 fib
= dest
->selected_fib
;
2829 /* Lookup same type route. */
2830 RNODE_FOREACH_RE (rn
, re
) {
2831 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2834 if (re
->type
!= type
)
2836 if (re
->instance
!= instance
)
2838 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2839 distance
!= re
->distance
)
2842 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2844 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2845 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2846 if (rtnh
->ifindex
!= nh
->ifindex
)
2851 /* Make sure that the route found has the same gateway. */
2857 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2858 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2866 /* If same type of route can't be found and this message is from
2870 * In the past(HA!) we could get here because
2871 * we were receiving a route delete from the
2872 * kernel and we're not marking the proto
2873 * as coming from it's appropriate originator.
2874 * Now that we are properly noticing the fact
2875 * that the kernel has deleted our route we
2876 * are not going to get called in this path
2877 * I am going to leave this here because
2878 * this might still work this way on non-linux
2879 * platforms as well as some weird state I have
2880 * not properly thought of yet.
2881 * If we can show that this code path is
2882 * dead then we can remove it.
2884 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2885 if (IS_ZEBRA_DEBUG_RIB
) {
2886 rnode_debug(rn
, vrf_id
,
2887 "rn %p, re %p (%s) was deleted from kernel, adding",
2889 zebra_route_string(fib
->type
));
2892 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2894 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2896 UNSET_FLAG(rtnh
->flags
,
2900 * This is a non FRR route
2901 * as such we should mark
2904 dest
->selected_fib
= NULL
;
2906 /* This means someone else, other than Zebra,
2908 * a Zebra router from the kernel. We will add
2910 rib_install_kernel(rn
, fib
, NULL
);
2913 if (IS_ZEBRA_DEBUG_RIB
) {
2917 "via %s ifindex %d type %d "
2918 "doesn't exist in rib",
2919 inet_ntop(afi2family(afi
),
2926 "type %d doesn't exist in rib",
2929 route_unlock_node(rn
);
2935 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2937 rib_install_kernel(rn
, same
, NULL
);
2938 route_unlock_node(rn
);
2943 /* Special handling for IPv4 or IPv6 routes sourced from
2944 * EVPN - the nexthop (and associated MAC) need to be
2945 * uninstalled if no more refs.
2947 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2948 struct nexthop
*tmp_nh
;
2950 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2951 struct ipaddr vtep_ip
;
2953 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2954 if (afi
== AFI_IP
) {
2955 vtep_ip
.ipa_type
= IPADDR_V4
;
2956 memcpy(&(vtep_ip
.ipaddr_v4
),
2957 &(tmp_nh
->gate
.ipv4
),
2958 sizeof(struct in_addr
));
2960 vtep_ip
.ipa_type
= IPADDR_V6
;
2961 memcpy(&(vtep_ip
.ipaddr_v6
),
2962 &(tmp_nh
->gate
.ipv6
),
2963 sizeof(struct in6_addr
));
2965 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2970 /* Notify dplane if system route changes */
2971 if (RIB_SYSTEM_ROUTE(re
))
2972 dplane_sys_route_del(rn
, same
);
2974 rib_delnode(rn
, same
);
2977 route_unlock_node(rn
);
2982 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2983 unsigned short instance
, int flags
, struct prefix
*p
,
2984 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2985 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2988 struct route_entry
*re
;
2989 struct nexthop
*nexthop
;
2991 /* Allocate new route_entry structure. */
2992 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2994 re
->instance
= instance
;
2995 re
->distance
= distance
;
2997 re
->metric
= metric
;
2999 re
->table
= table_id
;
3000 re
->vrf_id
= vrf_id
;
3001 re
->nexthop_num
= 0;
3002 re
->uptime
= monotime(NULL
);
3006 nexthop
= nexthop_new();
3008 route_entry_nexthop_add(re
, nexthop
);
3010 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
3013 /* Schedule routes of a particular table (address-family) based on event. */
3014 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3016 struct route_node
*rn
;
3017 struct route_entry
*re
, *next
;
3019 /* Walk all routes and queue for processing, if appropriate for
3020 * the trigger event.
3022 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3024 * If we are looking at a route node and the node
3025 * has already been queued we don't
3026 * need to queue it up again
3028 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3029 RIB_ROUTE_ANY_QUEUED
))
3032 case RIB_UPDATE_IF_CHANGE
:
3033 /* Examine all routes that won't get processed by the
3035 * triggered by nexthop evaluation (NHT). This would be
3037 * kernel and certain static routes. Note that NHT will
3039 * triggered upon an interface event as connected routes
3041 * get queued for processing.
3043 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3046 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3047 && re
->type
!= ZEBRA_ROUTE_KERNEL
3048 && re
->type
!= ZEBRA_ROUTE_CONNECT
3049 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3052 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3053 SET_FLAG(re
->status
,
3054 ROUTE_ENTRY_CHANGED
);
3059 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3060 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3061 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3064 /* If we only have nexthops to a
3069 SET_FLAG(re
->status
,
3070 ROUTE_ENTRY_CHANGED
);
3076 case RIB_UPDATE_RMAP_CHANGE
:
3077 case RIB_UPDATE_OTHER
:
3078 /* Right now, examine all routes. Can restrict to a
3080 * some cases (TODO).
3082 if (rnode_to_ribs(rn
)) {
3083 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
3084 SET_FLAG(re
->status
,
3085 ROUTE_ENTRY_CHANGED
);
3096 /* RIB update function. */
3097 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3099 struct route_table
*table
;
3101 /* Process routes of interested address-families. */
3102 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3104 if (IS_ZEBRA_DEBUG_EVENT
)
3105 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3106 rib_update_table(table
, event
);
3109 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3111 if (IS_ZEBRA_DEBUG_EVENT
)
3112 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3113 rib_update_table(table
, event
);
3117 /* Delete self installed routes after zebra is relaunched. */
3118 void rib_sweep_table(struct route_table
*table
)
3120 struct route_node
*rn
;
3121 struct route_entry
*re
;
3122 struct route_entry
*next
;
3123 struct nexthop
*nexthop
;
3128 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3129 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3130 if (IS_ZEBRA_DEBUG_RIB
)
3131 route_entry_dump(&rn
->p
, NULL
, re
);
3133 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3136 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3140 * So we are starting up and have received
3141 * routes from the kernel that we have installed
3142 * from a previous run of zebra but not cleaned
3143 * up ( say a kill -9 )
3144 * But since we haven't actually installed
3145 * them yet( we received them from the kernel )
3146 * we don't think they are active.
3147 * So let's pretend they are active to actually
3149 * In all honesty I'm not sure if we should
3150 * mark them as active when we receive them
3151 * This is startup only so probably ok.
3153 * If we ever decide to move rib_sweep_table
3154 * to a different spot (ie startup )
3155 * this decision needs to be revisited
3157 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3158 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3159 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3161 rib_uninstall_kernel(rn
, re
);
3162 rib_delnode(rn
, re
);
3167 /* Sweep all RIB tables. */
3168 void rib_sweep_route(void)
3171 struct zebra_vrf
*zvrf
;
3173 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3174 if ((zvrf
= vrf
->info
) == NULL
)
3177 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3178 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3181 zebra_router_sweep_route();
3184 /* Remove specific by protocol routes from 'table'. */
3185 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3186 struct route_table
*table
)
3188 struct route_node
*rn
;
3189 struct route_entry
*re
;
3190 struct route_entry
*next
;
3191 unsigned long n
= 0;
3194 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3195 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3196 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3198 if (re
->type
== proto
3199 && re
->instance
== instance
) {
3200 rib_delnode(rn
, re
);
3207 /* Remove specific by protocol routes. */
3208 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3211 struct zebra_vrf
*zvrf
;
3212 struct other_route_table
*ort
;
3213 unsigned long cnt
= 0;
3215 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3220 cnt
+= rib_score_proto_table(proto
, instance
,
3221 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3222 + rib_score_proto_table(
3224 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3226 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3227 rib_score_proto_table(proto
, instance
, ort
->table
);
3233 /* Close RIB and clean up kernel routes. */
3234 void rib_close_table(struct route_table
*table
)
3236 struct route_node
*rn
;
3237 rib_table_info_t
*info
;
3243 info
= route_table_get_info(table
);
3245 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3246 dest
= rib_dest_from_rnode(rn
);
3248 if (dest
&& dest
->selected_fib
) {
3249 if (info
->safi
== SAFI_UNICAST
)
3250 hook_call(rib_update
, rn
, NULL
);
3252 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3253 dest
->selected_fib
= NULL
;
3259 * Handler for async dataplane results after a pseudowire installation
3261 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3263 struct zebra_pw
*pw
;
3264 struct zebra_vrf
*vrf
;
3266 /* The pseudowire code assumes success - we act on an error
3267 * result for installation attempts here.
3269 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3272 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3273 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3274 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3276 zebra_pw_install_failure(pw
);
3286 * Handle results from the dataplane system. Dequeue update context
3287 * structs, dispatch to appropriate internal handlers.
3289 static int rib_process_dplane_results(struct thread
*thread
)
3291 struct zebra_dplane_ctx
*ctx
;
3292 struct dplane_ctx_q ctxlist
;
3294 /* Dequeue a list of completed updates with one lock/unlock cycle */
3297 TAILQ_INIT(&ctxlist
);
3299 /* Take lock controlling queue of results */
3300 pthread_mutex_lock(&dplane_mutex
);
3302 /* Dequeue list of context structs */
3303 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3305 pthread_mutex_unlock(&dplane_mutex
);
3307 /* Dequeue context block */
3308 ctx
= dplane_ctx_dequeue(&ctxlist
);
3310 /* If we've emptied the results queue, we're done */
3315 switch (dplane_ctx_get_op(ctx
)) {
3316 case DPLANE_OP_ROUTE_INSTALL
:
3317 case DPLANE_OP_ROUTE_UPDATE
:
3318 case DPLANE_OP_ROUTE_DELETE
:
3319 rib_process_result(ctx
);
3322 case DPLANE_OP_LSP_INSTALL
:
3323 case DPLANE_OP_LSP_UPDATE
:
3324 case DPLANE_OP_LSP_DELETE
:
3325 zebra_mpls_lsp_dplane_result(ctx
);
3328 case DPLANE_OP_PW_INSTALL
:
3329 case DPLANE_OP_PW_UNINSTALL
:
3330 handle_pw_result(ctx
);
3333 case DPLANE_OP_SYS_ROUTE_ADD
:
3334 case DPLANE_OP_SYS_ROUTE_DELETE
:
3335 /* No further processing in zebra for these. */
3336 dplane_ctx_fini(&ctx
);
3340 /* Don't expect this: just return the struct? */
3341 dplane_ctx_fini(&ctx
);
3343 } /* Dispatch by op code */
3345 ctx
= dplane_ctx_dequeue(&ctxlist
);
3350 /* Check for nexthop tracking processing after finishing with results */
3351 do_nht_processing();
3357 * Results are returned from the dataplane subsystem, in the context of
3358 * the dataplane pthread. We enqueue the results here for processing by
3359 * the main thread later.
3361 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3363 /* Take lock controlling queue of results */
3364 pthread_mutex_lock(&dplane_mutex
);
3366 /* Enqueue context blocks */
3367 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3369 pthread_mutex_unlock(&dplane_mutex
);
3371 /* Ensure event is signalled to zebra main pthread */
3372 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3379 * Ensure there are no empty slots in the route_info array.
3380 * Every route type in zebra should be present there.
3382 static void check_route_info(void)
3384 int len
= array_size(route_info
);
3387 * ZEBRA_ROUTE_SYSTEM is special cased since
3388 * its key is 0 anyway.
3390 * ZEBRA_ROUTE_ALL is also ignored.
3392 for (int i
= 0; i
< len
; i
++) {
3393 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3395 assert(route_info
[i
].key
);
3396 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3400 /* Routing information base initialize. */
3407 /* Init dataplane, and register for results */
3408 pthread_mutex_init(&dplane_mutex
, NULL
);
3409 TAILQ_INIT(&rib_dplane_q
);
3410 zebra_dplane_init(rib_dplane_results
);
3416 * Get the first vrf id that is greater than the given vrf id if any.
3418 * Returns TRUE if a vrf id was found, FALSE otherwise.
3420 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3424 vrf
= vrf_lookup_by_id(vrf_id
);
3426 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3428 *next_id_p
= vrf
->vrf_id
;
3437 * rib_tables_iter_next
3439 * Returns the next table in the iteration.
3441 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3443 struct route_table
*table
;
3446 * Array that helps us go over all AFI/SAFI combinations via one
3453 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3454 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3455 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3460 switch (iter
->state
) {
3462 case RIB_TABLES_ITER_S_INIT
:
3463 iter
->vrf_id
= VRF_DEFAULT
;
3464 iter
->afi_safi_ix
= -1;
3468 case RIB_TABLES_ITER_S_ITERATING
:
3469 iter
->afi_safi_ix
++;
3472 while (iter
->afi_safi_ix
3473 < (int)array_size(afi_safis
)) {
3474 table
= zebra_vrf_table(
3475 afi_safis
[iter
->afi_safi_ix
].afi
,
3476 afi_safis
[iter
->afi_safi_ix
].safi
,
3481 iter
->afi_safi_ix
++;
3485 * Found another table in this vrf.
3491 * Done with all tables in the current vrf, go to the
3495 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3498 iter
->afi_safi_ix
= 0;
3503 case RIB_TABLES_ITER_S_DONE
:
3508 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3510 iter
->state
= RIB_TABLES_ITER_S_DONE
;