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},
102 /* no entry/default: 150 */
105 /* RPF lookup behaviour */
106 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
109 static void __attribute__((format(printf
, 5, 6)))
110 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
111 int priority
, const char *msgfmt
, ...)
113 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
117 va_start(ap
, msgfmt
);
118 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
122 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
123 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
125 if (info
->safi
== SAFI_MULTICAST
)
126 strcat(buf
, " (MRIB)");
128 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
131 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
134 #define rnode_debug(node, vrf_id, ...) \
135 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
136 #define rnode_info(node, ...) \
137 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
139 uint8_t route_distance(int type
)
143 if ((unsigned)type
>= array_size(route_info
))
146 distance
= route_info
[type
].distance
;
151 int is_zebra_valid_kernel_table(uint32_t table_id
)
154 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
155 || (table_id
== RT_TABLE_COMPAT
))
162 int is_zebra_main_routing_table(uint32_t table_id
)
164 if ((table_id
== RT_TABLE_MAIN
)
165 || (table_id
== zrouter
.rtm_table_default
))
170 int zebra_check_addr(const struct prefix
*p
)
172 if (p
->family
== AF_INET
) {
175 addr
= p
->u
.prefix4
.s_addr
;
178 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
179 || IPV4_LINKLOCAL(addr
))
182 if (p
->family
== AF_INET6
) {
183 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
185 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
191 /* Add nexthop to the end of a rib node's nexthop list */
192 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
194 nexthop_add(&re
->ng
.nexthop
, nexthop
);
200 * copy_nexthop - copy a nexthop to the rib structure.
202 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
204 assert(!re
->ng
.nexthop
);
205 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
206 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
210 /* Delete specified nexthop from the list. */
211 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
214 nexthop
->next
->prev
= nexthop
->prev
;
216 nexthop
->prev
->next
= nexthop
->next
;
218 re
->ng
.nexthop
= nexthop
->next
;
223 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
227 struct nexthop
*nexthop
;
229 nexthop
= nexthop_new();
230 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
231 nexthop
->ifindex
= ifindex
;
232 nexthop
->vrf_id
= nh_vrf_id
;
234 route_entry_nexthop_add(re
, nexthop
);
239 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
240 struct in_addr
*ipv4
,
244 struct nexthop
*nexthop
;
246 nexthop
= nexthop_new();
247 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
248 nexthop
->vrf_id
= nh_vrf_id
;
249 nexthop
->gate
.ipv4
= *ipv4
;
251 nexthop
->src
.ipv4
= *src
;
253 route_entry_nexthop_add(re
, nexthop
);
258 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
259 struct in_addr
*ipv4
,
264 struct nexthop
*nexthop
;
265 struct interface
*ifp
;
267 nexthop
= nexthop_new();
268 nexthop
->vrf_id
= nh_vrf_id
;
269 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
270 nexthop
->gate
.ipv4
= *ipv4
;
272 nexthop
->src
.ipv4
= *src
;
273 nexthop
->ifindex
= ifindex
;
274 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
275 /*Pending: need to think if null ifp here is ok during bootup?
276 There was a crash because ifp here was coming to be NULL */
278 if (connected_is_unnumbered(ifp
))
279 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
281 route_entry_nexthop_add(re
, nexthop
);
286 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
287 struct in6_addr
*ipv6
,
290 struct nexthop
*nexthop
;
292 nexthop
= nexthop_new();
293 nexthop
->vrf_id
= nh_vrf_id
;
294 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
295 nexthop
->gate
.ipv6
= *ipv6
;
297 route_entry_nexthop_add(re
, nexthop
);
302 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
303 struct in6_addr
*ipv6
,
307 struct nexthop
*nexthop
;
309 nexthop
= nexthop_new();
310 nexthop
->vrf_id
= nh_vrf_id
;
311 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
312 nexthop
->gate
.ipv6
= *ipv6
;
313 nexthop
->ifindex
= ifindex
;
315 route_entry_nexthop_add(re
, nexthop
);
320 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
321 enum blackhole_type bh_type
)
323 struct nexthop
*nexthop
;
325 nexthop
= nexthop_new();
326 nexthop
->vrf_id
= VRF_DEFAULT
;
327 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
328 nexthop
->bh_type
= bh_type
;
330 route_entry_nexthop_add(re
, nexthop
);
335 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
336 struct nexthop
*nexthop
)
338 struct nexthop
*resolved_hop
;
340 resolved_hop
= nexthop_new();
341 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
343 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
344 switch (newhop
->type
) {
345 case NEXTHOP_TYPE_IPV4
:
346 case NEXTHOP_TYPE_IPV4_IFINDEX
:
347 /* If the resolving route specifies a gateway, use it */
348 resolved_hop
->type
= newhop
->type
;
349 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
351 if (newhop
->ifindex
) {
352 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
353 resolved_hop
->ifindex
= newhop
->ifindex
;
356 case NEXTHOP_TYPE_IPV6
:
357 case NEXTHOP_TYPE_IPV6_IFINDEX
:
358 resolved_hop
->type
= newhop
->type
;
359 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
361 if (newhop
->ifindex
) {
362 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
363 resolved_hop
->ifindex
= newhop
->ifindex
;
366 case NEXTHOP_TYPE_IFINDEX
:
367 /* If the resolving route is an interface route,
368 * it means the gateway we are looking up is connected
369 * to that interface. (The actual network is _not_ onlink).
370 * Therefore, the resolved route should have the original
371 * gateway as nexthop as it is directly connected.
373 * On Linux, we have to set the onlink netlink flag because
374 * otherwise, the kernel won't accept the route.
376 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
378 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
379 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
380 } else if (afi
== AFI_IP6
) {
381 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
382 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
384 resolved_hop
->ifindex
= newhop
->ifindex
;
386 case NEXTHOP_TYPE_BLACKHOLE
:
387 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
388 resolved_hop
->bh_type
= nexthop
->bh_type
;
392 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
393 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
395 /* Copy labels of the resolved route */
396 if (newhop
->nh_label
)
397 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
398 newhop
->nh_label
->num_labels
,
399 &newhop
->nh_label
->label
[0]);
401 resolved_hop
->rparent
= nexthop
;
402 nexthop_add(&nexthop
->resolved
, resolved_hop
);
406 * Given a nexthop we need to properly recursively resolve
407 * the route. As such, do a table lookup to find and match
408 * if at all possible. Set the nexthop->ifindex as appropriate
410 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
411 struct nexthop
*nexthop
,
412 struct route_node
*top
)
415 struct route_table
*table
;
416 struct route_node
*rn
;
417 struct route_entry
*match
= NULL
;
419 struct nexthop
*newhop
;
420 struct interface
*ifp
;
423 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
424 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
425 nexthop
->ifindex
= 0;
427 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
428 nexthops_free(nexthop
->resolved
);
429 nexthop
->resolved
= NULL
;
433 * If the kernel has sent us a route, then
434 * by golly gee whiz it's a good route.
436 if (re
->type
== ZEBRA_ROUTE_KERNEL
||
437 re
->type
== ZEBRA_ROUTE_SYSTEM
)
441 * Check to see if we should trust the passed in information
442 * for UNNUMBERED interfaces as that we won't find the GW
443 * address in the routing table.
444 * This check should suffice to handle IPv4 or IPv6 routes
445 * sourced from EVPN routes which are installed with the
446 * next hop as the remote VTEP IP.
448 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
449 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
451 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
453 "\t%s: Onlink and interface: %u[%u] does not exist",
454 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
458 if (connected_is_unnumbered(ifp
)) {
459 if (if_is_operative(ifp
))
462 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
464 "\t%s: Onlink and interface %s is not operative",
465 __PRETTY_FUNCTION__
, ifp
->name
);
469 if (!if_is_operative(ifp
)) {
470 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
472 "\t%s: Interface %s is not unnumbered",
474 ifp
? ifp
->name
: "Unknown");
479 /* Make lookup prefix. */
480 memset(&p
, 0, sizeof(struct prefix
));
484 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
485 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
489 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
490 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
493 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
497 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
499 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
500 zlog_debug("\t%s: Table not found",
501 __PRETTY_FUNCTION__
);
505 rn
= route_node_match(table
, (struct prefix
*)&p
);
507 route_unlock_node(rn
);
509 /* Lookup should halt if we've matched against ourselves ('top',
510 * if specified) - i.e., we cannot have a nexthop NH1 is
511 * resolved by a route NH1. The exception is if the route is a
514 if (top
&& rn
== top
)
515 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
516 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
517 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
519 "\t%s: Matched against ourself and prefix length is not max bit length",
520 __PRETTY_FUNCTION__
);
524 /* Pick up selected route. */
525 /* However, do not resolve over default route unless explicitly
527 if (is_default_prefix(&rn
->p
)
528 && !rnh_resolve_via_default(p
.family
)) {
529 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
531 "\t:%s: Resolved against default route",
532 __PRETTY_FUNCTION__
);
536 dest
= rib_dest_from_rnode(rn
);
537 if (dest
&& dest
->selected_fib
538 && !CHECK_FLAG(dest
->selected_fib
->status
,
540 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
541 match
= dest
->selected_fib
;
543 /* If there is no selected route or matched route is EGP, go up
548 } while (rn
&& rn
->info
== NULL
);
555 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
556 /* Directly point connected route. */
557 newhop
= match
->ng
.nexthop
;
559 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
560 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
561 nexthop
->ifindex
= newhop
->ifindex
;
564 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
566 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
567 if (!CHECK_FLAG(match
->status
,
568 ROUTE_ENTRY_INSTALLED
))
570 if (CHECK_FLAG(newhop
->flags
,
571 NEXTHOP_FLAG_RECURSIVE
))
574 SET_FLAG(nexthop
->flags
,
575 NEXTHOP_FLAG_RECURSIVE
);
577 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
578 nexthop_set_resolved(afi
, newhop
, nexthop
);
582 re
->nexthop_mtu
= match
->mtu
;
583 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
584 zlog_debug("\t%s: Recursion failed to find",
585 __PRETTY_FUNCTION__
);
587 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
589 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
590 if (!CHECK_FLAG(match
->status
,
591 ROUTE_ENTRY_INSTALLED
))
593 if (CHECK_FLAG(newhop
->flags
,
594 NEXTHOP_FLAG_RECURSIVE
))
597 SET_FLAG(nexthop
->flags
,
598 NEXTHOP_FLAG_RECURSIVE
);
599 nexthop_set_resolved(afi
, newhop
, nexthop
);
603 re
->nexthop_mtu
= match
->mtu
;
605 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
607 "\t%s: Static route unable to resolve",
608 __PRETTY_FUNCTION__
);
611 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
612 zlog_debug("\t%s: Route Type %s has not turned on recursion",
614 zebra_route_string(re
->type
));
615 if (re
->type
== ZEBRA_ROUTE_BGP
&&
616 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
617 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
622 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
623 zlog_debug("\t%s: Nexthop did not lookup in table",
624 __PRETTY_FUNCTION__
);
628 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
629 union g_addr
*addr
, struct route_node
**rn_out
)
632 struct route_table
*table
;
633 struct route_node
*rn
;
634 struct route_entry
*match
= NULL
;
637 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
641 memset(&p
, 0, sizeof(struct prefix
));
644 p
.u
.prefix4
= addr
->ipv4
;
645 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
647 p
.u
.prefix6
= addr
->ipv6
;
648 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
651 rn
= route_node_match(table
, (struct prefix
*)&p
);
656 route_unlock_node(rn
);
658 dest
= rib_dest_from_rnode(rn
);
659 if (dest
&& dest
->selected_fib
660 && !CHECK_FLAG(dest
->selected_fib
->status
,
661 ROUTE_ENTRY_REMOVED
))
662 match
= dest
->selected_fib
;
664 /* If there is no selected route or matched route is EGP, go up
669 } while (rn
&& rn
->info
== NULL
);
673 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
674 if (!CHECK_FLAG(match
->status
,
675 ROUTE_ENTRY_INSTALLED
))
687 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
689 struct route_node
**rn_out
)
691 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
692 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
693 union g_addr gaddr
= {.ipv4
= addr
};
695 switch (ipv4_multicast_mode
) {
696 case MCAST_MRIB_ONLY
:
697 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
699 case MCAST_URIB_ONLY
:
700 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
701 case MCAST_NO_CONFIG
:
702 case MCAST_MIX_MRIB_FIRST
:
703 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
706 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
709 case MCAST_MIX_DISTANCE
:
710 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
711 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
713 re
= ure
->distance
< mre
->distance
? ure
: mre
;
719 case MCAST_MIX_PFXLEN
:
720 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
721 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
723 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
732 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
734 if (IS_ZEBRA_DEBUG_RIB
) {
736 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
738 zlog_debug("%s: %s: vrf: %u found %s, using %s",
739 __func__
, buf
, vrf_id
,
740 mre
? (ure
? "MRIB+URIB" : "MRIB")
741 : ure
? "URIB" : "nothing",
742 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
747 void multicast_mode_ipv4_set(enum multicast_mode mode
)
749 if (IS_ZEBRA_DEBUG_RIB
)
750 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
752 ipv4_multicast_mode
= mode
;
755 enum multicast_mode
multicast_mode_ipv4_get(void)
757 return ipv4_multicast_mode
;
760 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
762 struct route_table
*table
;
763 struct route_node
*rn
;
764 struct route_entry
*match
= NULL
;
768 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
772 rn
= route_node_lookup(table
, (struct prefix
*)p
);
774 /* No route for this prefix. */
779 route_unlock_node(rn
);
780 dest
= rib_dest_from_rnode(rn
);
782 if (dest
&& dest
->selected_fib
783 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
784 match
= dest
->selected_fib
;
789 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
792 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
798 #define RIB_SYSTEM_ROUTE(R) \
799 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
801 #define RIB_KERNEL_ROUTE(R) \
802 ((R)->type == ZEBRA_ROUTE_KERNEL)
804 /* This function verifies reachability of one given nexthop, which can be
805 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
806 * in nexthop->flags field. The nexthop->ifindex will be updated
807 * appropriately as well. An existing route map can turn
808 * (otherwise active) nexthop into inactive, but not vice versa.
810 * The return value is the final value of 'ACTIVE' flag.
812 static unsigned nexthop_active_check(struct route_node
*rn
,
813 struct route_entry
*re
,
814 struct nexthop
*nexthop
)
816 struct interface
*ifp
;
817 route_map_result_t ret
= RMAP_MATCH
;
819 char buf
[SRCDEST2STR_BUFFER
];
820 const struct prefix
*p
, *src_p
;
821 struct zebra_vrf
*zvrf
;
823 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
825 if (rn
->p
.family
== AF_INET
)
827 else if (rn
->p
.family
== AF_INET6
)
831 switch (nexthop
->type
) {
832 case NEXTHOP_TYPE_IFINDEX
:
833 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
834 if (ifp
&& if_is_operative(ifp
))
835 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
837 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
839 case NEXTHOP_TYPE_IPV4
:
840 case NEXTHOP_TYPE_IPV4_IFINDEX
:
842 if (nexthop_active(AFI_IP
, re
, nexthop
, rn
))
843 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
845 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
847 case NEXTHOP_TYPE_IPV6
:
849 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
850 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
852 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
854 case NEXTHOP_TYPE_IPV6_IFINDEX
:
855 /* RFC 5549, v4 prefix with v6 NH */
856 if (rn
->p
.family
!= AF_INET
)
858 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
859 ifp
= if_lookup_by_index(nexthop
->ifindex
,
861 if (ifp
&& if_is_operative(ifp
))
862 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
864 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
866 if (nexthop_active(AFI_IP6
, re
, nexthop
, rn
))
867 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
869 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
872 case NEXTHOP_TYPE_BLACKHOLE
:
873 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
878 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
879 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
880 zlog_debug("\t%s: Unable to find a active nexthop",
881 __PRETTY_FUNCTION__
);
885 /* XXX: What exactly do those checks do? Do we support
886 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
888 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
889 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
890 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
892 /* The original code didn't determine the family correctly
893 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
894 * from the rib_table_info in those cases.
895 * Possibly it may be better to use only the rib_table_info
899 rib_table_info_t
*info
;
901 info
= srcdest_rnode_table_info(rn
);
905 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
907 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
909 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
910 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
911 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
914 /* It'll get set if required inside */
915 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
916 nexthop
, zvrf
, re
->tag
);
917 if (ret
== RMAP_DENYMATCH
) {
918 if (IS_ZEBRA_DEBUG_RIB
) {
919 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
921 "%u:%s: Filtering out with NH out %s due to route map",
923 ifindex2ifname(nexthop
->ifindex
,
926 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
928 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
932 * Iterate over all nexthops of the given RIB entry and refresh their
933 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
934 * nexthop is found to toggle the ACTIVE flag, the whole re structure
935 * is flagged with ROUTE_ENTRY_CHANGED.
937 * Return value is the new number of active nexthops.
939 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
)
941 struct nexthop
*nexthop
;
942 union g_addr prev_src
;
943 unsigned int prev_active
, new_active
, old_num_nh
;
944 ifindex_t prev_index
;
946 old_num_nh
= re
->nexthop_active_num
;
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 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
981 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
985 if (old_num_nh
!= re
->nexthop_active_num
)
986 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
988 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
989 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
992 return re
->nexthop_active_num
;
996 * Is this RIB labeled-unicast? It must be of type BGP and all paths
997 * (nexthops) must have a label.
999 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1001 struct nexthop
*nexthop
= NULL
;
1003 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1006 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1007 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1013 /* Update flag indicates whether this is a "replace" or not. Currently, this
1014 * is only used for IPv4.
1016 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1017 struct route_entry
*old
)
1019 struct nexthop
*nexthop
;
1020 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1021 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1022 const struct prefix
*p
, *src_p
;
1023 enum zebra_dplane_result ret
;
1025 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1027 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1029 if (info
->safi
!= SAFI_UNICAST
) {
1030 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1031 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1034 struct nexthop
*prev
;
1036 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1037 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1038 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1039 if (prev
== nexthop
)
1041 if (nexthop_same_firsthop(nexthop
, prev
)) {
1042 SET_FLAG(nexthop
->flags
,
1043 NEXTHOP_FLAG_DUPLICATE
);
1051 * If this is a replace to a new RE let the originator of the RE
1052 * know that they've lost
1054 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1055 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1057 /* Update fib selection */
1058 dest
->selected_fib
= re
;
1061 * Make sure we update the FPM any time we send new information to
1064 hook_call(rib_update
, rn
, "installing in kernel");
1066 /* Send add or update */
1068 ret
= dplane_route_update(rn
, re
, old
);
1070 ret
= dplane_route_add(rn
, re
);
1073 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1074 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1076 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
1078 zvrf
->installs_queued
++;
1080 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1082 char str
[SRCDEST2STR_BUFFER
];
1084 srcdest_rnode2str(rn
, str
, sizeof(str
));
1085 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1086 "%u:%s: Failed to enqueue dataplane install",
1090 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1099 /* Uninstall the route from kernel. */
1100 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1102 struct nexthop
*nexthop
;
1103 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1104 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1106 if (info
->safi
!= SAFI_UNICAST
) {
1107 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1108 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1109 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1114 * Make sure we update the FPM any time we send new information to
1117 hook_call(rib_update
, rn
, "uninstalling from kernel");
1119 switch (dplane_route_delete(rn
, re
)) {
1120 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1122 zvrf
->removals_queued
++;
1124 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1126 char str
[SRCDEST2STR_BUFFER
];
1128 srcdest_rnode2str(rn
, str
, sizeof(str
));
1129 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1130 "%u:%s: Failed to enqueue dataplane uninstall",
1134 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1143 /* Uninstall the route from kernel. */
1144 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1146 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1147 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1148 struct nexthop
*nexthop
;
1150 if (dest
&& dest
->selected_fib
== re
) {
1151 if (info
->safi
== SAFI_UNICAST
)
1152 hook_call(rib_update
, rn
, "rib_uninstall");
1154 /* If labeled-unicast route, uninstall transit LSP. */
1155 if (zebra_rib_labeled_unicast(re
))
1156 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1158 rib_uninstall_kernel(rn
, re
);
1160 dest
->selected_fib
= NULL
;
1162 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1163 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1166 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1167 const struct prefix
*p
, *src_p
;
1169 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1171 redistribute_delete(p
, src_p
, re
);
1172 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1177 * rib_can_delete_dest
1179 * Returns TRUE if the given dest can be deleted from the table.
1181 static int rib_can_delete_dest(rib_dest_t
*dest
)
1188 * Unresolved rnh's are stored on the default route's list
1190 * dest->rnode can also be the source prefix node in an
1191 * ipv6 sourcedest table. Fortunately the prefix of a
1192 * source prefix node can never be the default prefix.
1194 if (is_default_prefix(&dest
->rnode
->p
))
1198 * Don't delete the dest if we have to update the FPM about this
1201 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1202 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1208 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
1210 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1211 struct listnode
*node
, *nnode
;
1215 * We are storing the rnh's associated withb
1216 * the tracked nexthop as a list of the rn's.
1217 * Unresolved rnh's are placed at the top
1218 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
1219 * As such for each rn we need to walk up the tree
1220 * and see if any rnh's need to see if they
1221 * would match a more specific route
1224 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
1225 char buf
[PREFIX_STRLEN
];
1227 zlog_debug("%s: %s Being examined for Nexthop Tracking",
1228 __PRETTY_FUNCTION__
,
1229 srcdest_rnode2str(rn
, buf
, sizeof(buf
)));
1234 dest
= rib_dest_from_rnode(rn
);
1238 * If we have any rnh's stored in the nht list
1239 * then we know that this route node was used for
1240 * nht resolution and as such we need to call the
1241 * nexthop tracking evaluation code
1243 for (ALL_LIST_ELEMENTS(dest
->nht
, node
, nnode
, rnh
)) {
1244 struct zebra_vrf
*zvrf
=
1245 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
1246 struct prefix
*p
= &rnh
->node
->p
;
1248 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
1249 char buf1
[PREFIX_STRLEN
];
1250 char buf2
[PREFIX_STRLEN
];
1252 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
1254 srcdest_rnode2str(rn
, buf1
,
1256 prefix2str(p
, buf2
, sizeof(buf2
)),
1261 * If we have evaluated this node on this pass
1262 * already, due to following the tree up
1263 * then we know that we can move onto the next
1266 * Additionally we call zebra_evaluate_rnh
1267 * when we gc the dest. In this case we know
1268 * that there must be no other re's where
1269 * we were originally as such we know that
1270 * that sequence number is ok to respect.
1272 if (rnh
->seqno
== seq
) {
1273 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
1275 "\tNode processed and moved already");
1280 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
1286 dest
= rib_dest_from_rnode(rn
);
1293 * Garbage collect the rib dest corresponding to the given route node
1296 * Returns TRUE if the dest was deleted, FALSE otherwise.
1298 int rib_gc_dest(struct route_node
*rn
)
1302 dest
= rib_dest_from_rnode(rn
);
1306 if (!rib_can_delete_dest(dest
))
1309 if (IS_ZEBRA_DEBUG_RIB
) {
1310 struct zebra_vrf
*zvrf
;
1312 zvrf
= rib_dest_vrf(dest
);
1313 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1316 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
1319 list_delete(&dest
->nht
);
1320 XFREE(MTYPE_RIB_DEST
, dest
);
1324 * Release the one reference that we keep on the route node.
1326 route_unlock_node(rn
);
1330 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1331 struct route_entry
*new)
1333 hook_call(rib_update
, rn
, "new route selected");
1335 /* Update real nexthop. This may actually determine if nexthop is active
1337 if (!nexthop_active_update(rn
, new)) {
1338 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1342 if (IS_ZEBRA_DEBUG_RIB
) {
1343 char buf
[SRCDEST2STR_BUFFER
];
1344 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1345 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1346 zvrf_id(zvrf
), buf
, rn
, new,
1347 zebra_route_string(new->type
));
1350 /* If labeled-unicast route, install transit LSP. */
1351 if (zebra_rib_labeled_unicast(new))
1352 zebra_mpls_lsp_install(zvrf
, rn
, new);
1354 rib_install_kernel(rn
, new, NULL
);
1356 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1359 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1360 struct route_entry
*old
)
1362 hook_call(rib_update
, rn
, "removing existing route");
1364 /* Uninstall from kernel. */
1365 if (IS_ZEBRA_DEBUG_RIB
) {
1366 char buf
[SRCDEST2STR_BUFFER
];
1367 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1368 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1369 zvrf_id(zvrf
), buf
, rn
, old
,
1370 zebra_route_string(old
->type
));
1373 /* If labeled-unicast route, uninstall transit LSP. */
1374 if (zebra_rib_labeled_unicast(old
))
1375 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1377 rib_uninstall_kernel(rn
, old
);
1379 /* Update nexthop for route, reset changed flag. */
1380 /* Note: this code also handles the Linux case when an interface goes
1381 * down, causing the kernel to delete routes without sending DELROUTE
1384 if (!nexthop_active_update(rn
, old
) && (RIB_KERNEL_ROUTE(old
)))
1385 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1387 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1390 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1391 struct route_node
*rn
,
1392 struct route_entry
*old
,
1393 struct route_entry
*new)
1398 * We have to install or update if a new route has been selected or
1399 * something has changed.
1401 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1402 hook_call(rib_update
, rn
, "updating existing route");
1404 /* Update the nexthop; we could determine here that nexthop is
1406 if (nexthop_active_update(rn
, new))
1409 /* If nexthop is active, install the selected route, if
1411 * the install succeeds, cleanup flags for prior route, if
1416 if (IS_ZEBRA_DEBUG_RIB
) {
1417 char buf
[SRCDEST2STR_BUFFER
];
1418 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1421 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
1422 zvrf_id(zvrf
), buf
, rn
, new,
1423 zebra_route_string(new->type
),
1425 zebra_route_string(old
->type
));
1428 "%u:%s: Updating route rn %p, re %p (%s)",
1429 zvrf_id(zvrf
), buf
, rn
, new,
1430 zebra_route_string(new->type
));
1433 /* If labeled-unicast route, uninstall transit LSP. */
1434 if (zebra_rib_labeled_unicast(old
))
1435 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1438 * Non-system route should be installed.
1439 * If labeled-unicast route, install transit
1442 if (zebra_rib_labeled_unicast(new))
1443 zebra_mpls_lsp_install(zvrf
, rn
, new);
1445 rib_install_kernel(rn
, new, old
);
1449 * If nexthop for selected route is not active or install
1451 * may need to uninstall and delete for redistribution.
1454 if (IS_ZEBRA_DEBUG_RIB
) {
1455 char buf
[SRCDEST2STR_BUFFER
];
1456 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1459 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1460 zvrf_id(zvrf
), buf
, rn
, new,
1461 zebra_route_string(new->type
),
1463 zebra_route_string(old
->type
));
1466 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
1467 zvrf_id(zvrf
), buf
, rn
, new,
1468 zebra_route_string(new->type
));
1471 /* If labeled-unicast route, uninstall transit LSP. */
1472 if (zebra_rib_labeled_unicast(old
))
1473 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1475 rib_uninstall_kernel(rn
, old
);
1479 * Same route selected; check if in the FIB and if not,
1480 * re-install. This is housekeeping code to deal with
1481 * race conditions in kernel with linux netlink reporting
1482 * interface up before IPv4 or IPv6 protocol is ready
1485 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1486 RIB_SYSTEM_ROUTE(new))
1487 rib_install_kernel(rn
, new, NULL
);
1490 /* Update prior route. */
1492 /* Set real nexthop. */
1493 nexthop_active_update(rn
, old
);
1494 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1497 /* Clear changed flag. */
1498 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1501 /* Check if 'alternate' RIB entry is better than 'current'. */
1502 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1503 struct route_entry
*alternate
)
1505 if (current
== NULL
)
1508 /* filter route selection in following order:
1509 * - connected beats other types
1510 * - if both connected, loopback or vrf wins
1511 * - lower distance beats higher
1512 * - lower metric beats higher for equal distance
1513 * - last, hence oldest, route wins tie break.
1516 /* Connected routes. Check to see if either are a vrf
1517 * or loopback interface. If not, pick the last connected
1518 * route of the set of lowest metric connected routes.
1520 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1521 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1524 /* both are connected. are either loop or vrf? */
1525 struct nexthop
*nexthop
= NULL
;
1527 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1528 if (if_is_loopback_or_vrf(if_lookup_by_index(
1529 nexthop
->ifindex
, alternate
->vrf_id
)))
1533 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1534 if (if_is_loopback_or_vrf(if_lookup_by_index(
1535 nexthop
->ifindex
, current
->vrf_id
)))
1539 /* Neither are loop or vrf so pick best metric */
1540 if (alternate
->metric
<= current
->metric
)
1546 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1549 /* higher distance loses */
1550 if (alternate
->distance
< current
->distance
)
1552 if (current
->distance
< alternate
->distance
)
1555 /* metric tie-breaks equal distance */
1556 if (alternate
->metric
<= current
->metric
)
1562 /* Core function for processing routing information base. */
1563 static void rib_process(struct route_node
*rn
)
1565 struct route_entry
*re
;
1566 struct route_entry
*next
;
1567 struct route_entry
*old_selected
= NULL
;
1568 struct route_entry
*new_selected
= NULL
;
1569 struct route_entry
*old_fib
= NULL
;
1570 struct route_entry
*new_fib
= NULL
;
1571 struct route_entry
*best
= NULL
;
1572 char buf
[SRCDEST2STR_BUFFER
];
1574 struct zebra_vrf
*zvrf
= NULL
;
1575 const struct prefix
*p
, *src_p
;
1577 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1578 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1582 dest
= rib_dest_from_rnode(rn
);
1584 zvrf
= rib_dest_vrf(dest
);
1585 vrf_id
= zvrf_id(zvrf
);
1588 if (IS_ZEBRA_DEBUG_RIB
)
1589 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1591 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1592 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1595 * we can have rn's that have a NULL info pointer
1596 * (dest). As such let's not let the deref happen
1597 * additionally we know RNODE_FOREACH_RE_SAFE
1598 * will not iterate so we are ok.
1601 old_fib
= dest
->selected_fib
;
1603 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1604 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1606 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1607 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1608 re
->status
, re
->flags
, re
->distance
,
1611 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1613 /* Currently selected re. */
1614 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1615 assert(old_selected
== NULL
);
1619 /* Skip deleted entries from selection */
1620 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1623 /* Skip unreachable nexthop. */
1624 /* This first call to nexthop_active_update is merely to
1626 * there's any change to nexthops associated with this RIB
1628 * rib_process() can be invoked due to an external event such as
1630 * down or due to next-hop-tracking evaluation. In the latter
1632 * a decision has already been made that the NHs have changed.
1634 * need to invoke a potentially expensive call again. Further,
1636 * the change might be in a recursive NH which is not caught in
1637 * the nexthop_active_update() code. Thus, we might miss changes
1641 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1642 && !nexthop_active_update(rn
, re
)) {
1643 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1644 /* XXX: HERE BE DRAGONS!!!!!
1645 * In all honesty, I have not yet figured out
1647 * does or why the ROUTE_ENTRY_CHANGED test
1649 * or why we need to delete a route here, and
1651 * this concerns both selected and fib route, or
1654 /* This entry was denied by the 'ip protocol
1655 * table' route-map, we
1656 * need to delete it */
1657 if (re
!= old_selected
) {
1658 if (IS_ZEBRA_DEBUG_RIB
)
1660 "%s: %u:%s: imported via import-table but denied "
1661 "by the ip protocol table route-map",
1662 __func__
, vrf_id
, buf
);
1665 SET_FLAG(re
->status
,
1666 ROUTE_ENTRY_REMOVED
);
1672 /* Infinite distance. */
1673 if (re
->distance
== DISTANCE_INFINITY
) {
1674 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1678 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1679 best
= rib_choose_best(new_fib
, re
);
1680 if (new_fib
&& best
!= new_fib
)
1681 UNSET_FLAG(new_fib
->status
,
1682 ROUTE_ENTRY_CHANGED
);
1685 best
= rib_choose_best(new_selected
, re
);
1686 if (new_selected
&& best
!= new_selected
)
1687 UNSET_FLAG(new_selected
->status
,
1688 ROUTE_ENTRY_CHANGED
);
1689 new_selected
= best
;
1692 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1693 } /* RNODE_FOREACH_RE */
1695 /* If no FIB override route, use the selected route also for FIB */
1696 if (new_fib
== NULL
)
1697 new_fib
= new_selected
;
1699 /* After the cycle is finished, the following pointers will be set:
1700 * old_selected --- RE entry currently having SELECTED
1701 * new_selected --- RE entry that is newly SELECTED
1702 * old_fib --- RE entry currently in kernel FIB
1703 * new_fib --- RE entry that is newly to be in kernel FIB
1705 * new_selected will get SELECTED flag, and is going to be redistributed
1706 * the zclients. new_fib (which can be new_selected) will be installed
1710 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1712 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1713 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1714 (void *)old_fib
, (void *)new_fib
);
1717 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1718 * fib == selected */
1719 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1720 ROUTE_ENTRY_CHANGED
);
1722 /* Update fib according to selection results */
1723 if (new_fib
&& old_fib
)
1724 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1726 rib_process_add_fib(zvrf
, rn
, new_fib
);
1728 rib_process_del_fib(zvrf
, rn
, old_fib
);
1730 /* Update SELECTED entry */
1731 if (old_selected
!= new_selected
|| selected_changed
) {
1733 if (new_selected
&& new_selected
!= new_fib
) {
1734 nexthop_active_update(rn
, new_selected
);
1735 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1739 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1743 redistribute_delete(p
, src_p
, old_selected
);
1744 if (old_selected
!= new_selected
)
1745 UNSET_FLAG(old_selected
->flags
,
1746 ZEBRA_FLAG_SELECTED
);
1750 /* Remove all RE entries queued for removal */
1751 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1752 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1753 if (IS_ZEBRA_DEBUG_RIB
) {
1754 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1755 (void *)rn
, (void *)re
);
1762 * Check if the dest can be deleted now.
1767 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1769 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1770 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1775 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1776 if (IS_ZEBRA_DEBUG_MPLS
)
1778 "%u: Scheduling all LSPs upon RIB completion",
1780 zebra_mpls_lsp_schedule(zvrf
);
1781 mpls_unmark_lsps_for_processing(rn
);
1786 * Utility to match route with dplane context data
1788 static bool rib_route_match_ctx(const struct route_entry
*re
,
1789 const struct zebra_dplane_ctx
*ctx
,
1792 bool result
= false;
1796 * In 'update' case, we test info about the 'previous' or
1799 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1800 (re
->instance
== dplane_ctx_get_old_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_old_distance(ctx
) ||
1808 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1815 * Ordinary, single-route case using primary context info
1817 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1818 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1819 /* Skip route that's been deleted */
1823 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1824 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1827 /* TODO -- we're using this extra test, but it's not
1828 * exactly clear why.
1830 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1831 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1832 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1843 static void zebra_rib_fixup_system(struct route_node
*rn
)
1845 struct route_entry
*re
;
1847 RNODE_FOREACH_RE(rn
, re
) {
1848 struct nexthop
*nhop
;
1850 if (!RIB_SYSTEM_ROUTE(re
))
1853 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1856 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1858 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1859 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1862 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1868 * Route-update results processing after async dataplane update.
1870 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1872 struct route_table
*table
= NULL
;
1873 struct zebra_vrf
*zvrf
= NULL
;
1874 struct route_node
*rn
= NULL
;
1875 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1876 bool is_update
= false;
1877 struct nexthop
*nexthop
, *ctx_nexthop
;
1878 char dest_str
[PREFIX_STRLEN
] = "";
1879 enum dplane_op_e op
;
1880 enum zebra_dplane_result status
;
1881 const struct prefix
*dest_pfx
, *src_pfx
;
1884 /* Locate rn and re(s) from ctx */
1886 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1887 dplane_ctx_get_safi(ctx
),
1888 dplane_ctx_get_vrf(ctx
),
1889 dplane_ctx_get_table(ctx
));
1890 if (table
== NULL
) {
1891 if (IS_ZEBRA_DEBUG_DPLANE
) {
1892 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1893 dplane_ctx_get_afi(ctx
),
1894 dplane_ctx_get_safi(ctx
),
1895 dplane_ctx_get_vrf(ctx
));
1900 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1902 dest_pfx
= dplane_ctx_get_dest(ctx
);
1904 /* Note well: only capturing the prefix string if debug is enabled here;
1905 * unconditional log messages will have to generate the string.
1907 if (IS_ZEBRA_DEBUG_DPLANE
)
1908 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1910 src_pfx
= dplane_ctx_get_src(ctx
);
1911 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1912 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1914 if (IS_ZEBRA_DEBUG_DPLANE
) {
1915 zlog_debug("Failed to process dplane results: no route for %u:%s",
1916 dplane_ctx_get_vrf(ctx
), dest_str
);
1921 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1923 op
= dplane_ctx_get_op(ctx
);
1924 status
= dplane_ctx_get_status(ctx
);
1926 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1927 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1928 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1929 dplane_op2str(op
), dplane_res2str(status
));
1932 * Update is a bit of a special case, where we may have both old and new
1933 * routes to post-process.
1935 is_update
= dplane_ctx_is_update(ctx
);
1938 * Take a pass through the routes, look for matches with the context
1941 RNODE_FOREACH_RE(rn
, rib
) {
1944 if (rib_route_match_ctx(rib
, ctx
, false))
1948 /* Check for old route match */
1949 if (is_update
&& (old_re
== NULL
)) {
1950 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1954 /* Have we found the routes we need to work on? */
1955 if (re
&& ((!is_update
|| old_re
)))
1959 seq
= dplane_ctx_get_seq(ctx
);
1962 * Check sequence number(s) to detect stale results before continuing
1965 if (re
->dplane_sequence
!= seq
) {
1966 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1967 zlog_debug("%u:%s Stale dplane result for re %p",
1968 dplane_ctx_get_vrf(ctx
),
1971 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1975 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1976 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1977 zlog_debug("%u:%s Stale dplane result for old_re %p",
1978 dplane_ctx_get_vrf(ctx
),
1981 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1985 case DPLANE_OP_ROUTE_INSTALL
:
1986 case DPLANE_OP_ROUTE_UPDATE
:
1987 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1989 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1990 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1993 * On an update operation from the same route type
1994 * context retrieval currently has no way to know
1995 * which was the old and which was the new.
1996 * So don't unset our flags that we just set.
1997 * We know redistribution is ok because the
1998 * old_re in this case is used for nothing
1999 * more than knowing whom to contact if necessary.
2001 if (old_re
&& old_re
!= re
) {
2002 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2003 UNSET_FLAG(old_re
->status
,
2004 ROUTE_ENTRY_INSTALLED
);
2006 /* Update zebra nexthop FIB flag for each
2007 * nexthop that was installed.
2009 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
2015 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2016 if (nexthop_same(ctx_nexthop
, nexthop
))
2020 if (nexthop
== NULL
)
2023 if (CHECK_FLAG(nexthop
->flags
,
2024 NEXTHOP_FLAG_RECURSIVE
))
2027 if (CHECK_FLAG(ctx_nexthop
->flags
,
2029 SET_FLAG(nexthop
->flags
,
2032 UNSET_FLAG(nexthop
->flags
,
2037 * System routes are weird in that they
2038 * allow multiple to be installed that match
2039 * to the same prefix, so after we get the
2040 * result we need to clean them up so that
2041 * we can actually use them.
2043 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2044 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2045 zebra_rib_fixup_system(rn
);
2052 * TODO -- still calling the redist api using the
2053 * route_entries, and there's a corner-case here:
2054 * if there's no client for the 'new' route, a redist
2055 * deleting the 'old' route will be sent. But if the
2056 * 'old' context info was stale, 'old_re' will be
2057 * NULL here and that delete will not be sent.
2060 redistribute_update(dest_pfx
, src_pfx
,
2063 /* Notify route owner */
2064 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2068 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2069 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2071 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2073 zsend_route_notify_owner(re
, dest_pfx
,
2074 ZAPI_ROUTE_FAIL_INSTALL
);
2076 zlog_warn("%u:%s: Route install failed",
2077 dplane_ctx_get_vrf(ctx
),
2078 prefix2str(dest_pfx
,
2079 dest_str
, sizeof(dest_str
)));
2082 case DPLANE_OP_ROUTE_DELETE
:
2084 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2086 * In the delete case, the zebra core datastructs were
2087 * updated (or removed) at the time the delete was issued,
2088 * so we're just notifying the route owner.
2090 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2092 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2093 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2095 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2101 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2102 zsend_route_notify_owner_ctx(ctx
,
2103 ZAPI_ROUTE_REMOVE_FAIL
);
2105 zlog_warn("%u:%s: Route Deletion failure",
2106 dplane_ctx_get_vrf(ctx
),
2107 prefix2str(dest_pfx
,
2108 dest_str
, sizeof(dest_str
)));
2112 * System routes are weird in that they
2113 * allow multiple to be installed that match
2114 * to the same prefix, so after we get the
2115 * result we need to clean them up so that
2116 * we can actually use them.
2118 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2119 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2120 zebra_rib_fixup_system(rn
);
2126 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
2127 zebra_rib_evaluate_mpls(rn
);
2131 route_unlock_node(rn
);
2133 /* Return context to dataplane module */
2134 dplane_ctx_fini(&ctx
);
2137 /* Take a list of route_node structs and return 1, if there was a record
2138 * picked from it and processed by rib_process(). Don't process more,
2139 * than one RN record; operate only in the specified sub-queue.
2141 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2143 struct listnode
*lnode
= listhead(subq
);
2144 struct route_node
*rnode
;
2146 struct zebra_vrf
*zvrf
= NULL
;
2151 rnode
= listgetdata(lnode
);
2152 dest
= rib_dest_from_rnode(rnode
);
2154 zvrf
= rib_dest_vrf(dest
);
2158 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2159 char buf
[SRCDEST2STR_BUFFER
];
2160 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2161 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2162 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2166 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2167 RIB_ROUTE_QUEUED(qindex
));
2172 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2173 __func__
, rnode
, rnode
->lock
);
2174 zlog_backtrace(LOG_DEBUG
);
2177 route_unlock_node(rnode
);
2178 list_delete_node(subq
, lnode
);
2184 * Perform next-hop tracking processing after RIB updates.
2186 static void do_nht_processing(void)
2190 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2191 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2193 * is pointed to the meta queue structure.
2195 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2197 struct meta_queue
*mq
= data
;
2199 uint32_t queue_len
, queue_limit
;
2201 /* Ensure there's room for more dataplane updates */
2202 queue_limit
= dplane_get_in_queue_limit();
2203 queue_len
= dplane_get_in_queue_len();
2204 if (queue_len
> queue_limit
) {
2205 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2206 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2207 queue_len
, queue_limit
);
2209 /* Ensure that the meta-queue is actually enqueued */
2210 if (work_queue_empty(zrouter
.ribq
))
2211 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2213 return WQ_QUEUE_BLOCKED
;
2216 for (i
= 0; i
< MQ_SIZE
; i
++)
2217 if (process_subq(mq
->subq
[i
], i
)) {
2221 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2226 * Look into the RN and queue it into the highest priority queue
2227 * at this point in time for processing.
2229 * We will enqueue a route node only once per invocation.
2231 * There are two possibilities here that should be kept in mind.
2232 * If the original invocation has not been pulled off for processing
2233 * yet, A subsuquent invocation can have a route entry with a better
2234 * meta queue index value and we can have a situation where
2235 * we might have the same node enqueued 2 times. Not necessarily
2236 * an optimal situation but it should be ok.
2238 * The other possibility is that the original invocation has not
2239 * been pulled off for processing yet, A subsusquent invocation
2240 * doesn't have a route_entry with a better meta-queue and the
2241 * original metaqueue index value will win and we'll end up with
2242 * the route node enqueued once.
2244 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2246 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2247 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2249 RNODE_FOREACH_RE (rn
, curr_re
) {
2250 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2252 if (curr_qindex
<= qindex
) {
2254 qindex
= curr_qindex
;
2261 /* Invariant: at this point we always have rn->info set. */
2262 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2263 RIB_ROUTE_QUEUED(qindex
))) {
2264 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2265 rnode_debug(rn
, re
->vrf_id
,
2266 "rn %p is already queued in sub-queue %u",
2267 (void *)rn
, qindex
);
2271 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2272 listnode_add(mq
->subq
[qindex
], rn
);
2273 route_lock_node(rn
);
2276 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2277 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2278 (void *)rn
, qindex
);
2281 /* Add route_node to work queue and schedule processing */
2282 void rib_queue_add(struct route_node
*rn
)
2286 /* Pointless to queue a route_node with no RIB entries to add or remove
2288 if (!rnode_to_ribs(rn
)) {
2289 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2290 __func__
, (void *)rn
, rn
->lock
);
2291 zlog_backtrace(LOG_DEBUG
);
2295 if (zrouter
.ribq
== NULL
) {
2296 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2297 "%s: work_queue does not exist!", __func__
);
2302 * The RIB queue should normally be either empty or holding the only
2303 * work_queue_item element. In the latter case this element would
2304 * hold a pointer to the meta queue structure, which must be used to
2305 * actually queue the route nodes to process. So create the MQ
2306 * holder, if necessary, then push the work into it in any case.
2307 * This semantics was introduced after 0.99.9 release.
2309 if (work_queue_empty(zrouter
.ribq
))
2310 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2312 rib_meta_queue_add(zrouter
.mq
, rn
);
2317 /* Create new meta queue.
2318 A destructor function doesn't seem to be necessary here.
2320 static struct meta_queue
*meta_queue_new(void)
2322 struct meta_queue
*new;
2325 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2327 for (i
= 0; i
< MQ_SIZE
; i
++) {
2328 new->subq
[i
] = list_new();
2329 assert(new->subq
[i
]);
2335 void meta_queue_free(struct meta_queue
*mq
)
2339 for (i
= 0; i
< MQ_SIZE
; i
++)
2340 list_delete(&mq
->subq
[i
]);
2342 XFREE(MTYPE_WORK_QUEUE
, mq
);
2345 /* initialise zebra rib work queue */
2346 static void rib_queue_init(void)
2348 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2349 "route_node processing"))) {
2350 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2351 "%s: could not initialise work queue!", __func__
);
2355 /* fill in the work queue spec */
2356 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2357 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2358 zrouter
.ribq
->spec
.completion_func
= NULL
;
2359 /* XXX: TODO: These should be runtime configurable via vty */
2360 zrouter
.ribq
->spec
.max_retries
= 3;
2361 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2362 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2364 if (!(zrouter
.mq
= meta_queue_new())) {
2365 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2366 "%s: could not initialise meta queue!", __func__
);
2372 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2376 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2377 dest
->nht
= list_new();
2378 route_lock_node(rn
); /* rn route table reference */
2385 /* RIB updates are processed via a queue of pointers to route_nodes.
2387 * The queue length is bounded by the maximal size of the routing table,
2388 * as a route_node will not be requeued, if already queued.
2390 * REs are submitted via rib_addnode or rib_delnode which set minimal
2391 * state, or static_install_route (when an existing RE is updated)
2392 * and then submit route_node to queue for best-path selection later.
2393 * Order of add/delete state changes are preserved for any given RE.
2395 * Deleted REs are reaped during best-path selection.
2398 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2399 * |-------->| | best RE, if required
2401 * static_install->|->rib_addqueue...... -> rib_process
2403 * |-------->| |-> rib_unlink
2404 * |-> set ROUTE_ENTRY_REMOVE |
2405 * rib_delnode (RE freed)
2407 * The 'info' pointer of a route_node points to a rib_dest_t
2408 * ('dest'). Queueing state for a route_node is kept on the dest. The
2409 * dest is created on-demand by rib_link() and is kept around at least
2410 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2412 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2414 * - route_nodes: refcounted by:
2415 * - dest attached to route_node:
2416 * - managed by: rib_link/rib_gc_dest
2417 * - route_node processing queue
2418 * - managed by: rib_addqueue, rib_process.
2422 /* Add RE to head of the route node. */
2423 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2425 struct route_entry
*head
;
2428 const char *rmap_name
;
2432 dest
= rib_dest_from_rnode(rn
);
2434 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2435 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2437 dest
= zebra_rib_create_dest(rn
);
2440 head
= dest
->routes
;
2447 afi
= (rn
->p
.family
== AF_INET
)
2449 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2450 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2451 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2452 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2457 static void rib_addnode(struct route_node
*rn
,
2458 struct route_entry
*re
, int process
)
2460 /* RE node has been un-removed before route-node is processed.
2461 * route_node must hence already be on the queue for processing..
2463 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2464 if (IS_ZEBRA_DEBUG_RIB
)
2465 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2466 (void *)rn
, (void *)re
);
2468 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2471 rib_link(rn
, re
, process
);
2477 * Detach a rib structure from a route_node.
2479 * Note that a call to rib_unlink() should be followed by a call to
2480 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2481 * longer required to be deleted.
2483 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2489 if (IS_ZEBRA_DEBUG_RIB
)
2490 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2493 dest
= rib_dest_from_rnode(rn
);
2496 re
->next
->prev
= re
->prev
;
2499 re
->prev
->next
= re
->next
;
2501 dest
->routes
= re
->next
;
2504 if (dest
->selected_fib
== re
)
2505 dest
->selected_fib
= NULL
;
2507 nexthops_free(re
->ng
.nexthop
);
2508 XFREE(MTYPE_RE
, re
);
2511 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2515 if (IS_ZEBRA_DEBUG_RIB
)
2516 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2517 (void *)rn
, (void *)re
);
2518 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2520 afi
= (rn
->p
.family
== AF_INET
)
2522 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2523 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2524 zebra_del_import_table_entry(rn
, re
);
2525 /* Just clean up if non main table */
2526 if (IS_ZEBRA_DEBUG_RIB
) {
2527 char buf
[SRCDEST2STR_BUFFER
];
2528 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2529 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2530 re
->vrf_id
, buf
, rn
, re
,
2531 zebra_route_string(re
->type
));
2540 /* This function dumps the contents of a given RE entry into
2541 * standard debug log. Calling function name and IP prefix in
2542 * question are passed as 1st and 2nd arguments.
2545 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2546 union prefixconstptr src_pp
,
2547 const struct route_entry
*re
)
2549 const struct prefix
*src_p
= src_pp
.p
;
2550 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2551 char straddr
[PREFIX_STRLEN
];
2552 char srcaddr
[PREFIX_STRLEN
];
2553 struct nexthop
*nexthop
;
2555 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2556 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2557 is_srcdst
? " from " : "",
2558 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2561 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2562 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2565 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2566 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2567 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2568 re
->nexthop_num
, re
->nexthop_active_num
);
2570 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2571 struct interface
*ifp
;
2572 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2574 switch (nexthop
->type
) {
2575 case NEXTHOP_TYPE_BLACKHOLE
:
2576 sprintf(straddr
, "Blackhole");
2578 case NEXTHOP_TYPE_IFINDEX
:
2579 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2581 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2583 case NEXTHOP_TYPE_IPV4
:
2585 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2586 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2589 case NEXTHOP_TYPE_IPV6
:
2590 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2591 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2595 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2596 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2597 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2599 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2602 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2605 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2609 zlog_debug("%s: dump complete", func
);
2612 /* This is an exported helper to rtm_read() to dump the strange
2613 * RE entry found by rib_lookup_ipv4_route()
2616 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2618 struct route_table
*table
;
2619 struct route_node
*rn
;
2620 struct route_entry
*re
;
2621 char prefix_buf
[INET_ADDRSTRLEN
];
2624 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2626 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2627 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2632 /* Scan the RIB table for exactly matching RE entry. */
2633 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2635 /* No route for this prefix. */
2637 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2638 prefix2str((struct prefix
*)p
, prefix_buf
,
2639 sizeof(prefix_buf
)));
2644 route_unlock_node(rn
);
2647 RNODE_FOREACH_RE (rn
, re
) {
2648 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2650 (void *)rn
, (void *)re
,
2651 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2654 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2657 route_entry_dump(p
, NULL
, re
);
2661 /* Check if requested address assignment will fail due to another
2662 * route being installed by zebra in FIB already. Take necessary
2663 * actions, if needed: remove such a route from FIB and deSELECT
2664 * corresponding RE entry. Then put affected RN into RIBQ head.
2666 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2668 struct route_table
*table
;
2669 struct route_node
*rn
;
2670 unsigned changed
= 0;
2673 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2674 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2675 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2680 /* No matches would be the simplest case. */
2681 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2685 route_unlock_node(rn
);
2687 dest
= rib_dest_from_rnode(rn
);
2688 /* Check all RE entries. In case any changes have to be done, requeue
2689 * the RN into RIBQ head. If the routing message about the new connected
2690 * route (generated by the IP address we are going to assign very soon)
2691 * comes before the RIBQ is processed, the new RE entry will join
2692 * RIBQ record already on head. This is necessary for proper
2694 * of the rest of the RE.
2696 if (dest
->selected_fib
) {
2698 if (IS_ZEBRA_DEBUG_RIB
) {
2699 char buf
[PREFIX_STRLEN
];
2701 zlog_debug("%u:%s: freeing way for connected prefix",
2702 dest
->selected_fib
->vrf_id
,
2703 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2704 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2706 rib_uninstall(rn
, dest
->selected_fib
);
2712 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2713 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2715 struct route_table
*table
;
2716 struct route_node
*rn
;
2717 struct route_entry
*same
= NULL
;
2723 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2726 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2728 XFREE(MTYPE_RE
, re
);
2732 /* Make it sure prefixlen is applied to the prefix. */
2735 apply_mask_ipv6(src_p
);
2737 /* Set default distance by route type. */
2738 if (re
->distance
== 0) {
2739 re
->distance
= route_distance(re
->type
);
2741 /* iBGP distance is 200. */
2742 if (re
->type
== ZEBRA_ROUTE_BGP
2743 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2747 /* Lookup route node.*/
2748 rn
= srcdest_rnode_get(table
, p
, src_p
);
2751 * If same type of route are installed, treat it as a implicit
2753 * If the user has specified the No route replace semantics
2754 * for the install don't do a route replace.
2756 RNODE_FOREACH_RE (rn
, same
) {
2757 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2760 if (same
->type
!= re
->type
)
2762 if (same
->instance
!= re
->instance
)
2764 if (same
->type
== ZEBRA_ROUTE_KERNEL
2765 && same
->metric
!= re
->metric
)
2768 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2769 same
->distance
!= re
->distance
)
2773 * We should allow duplicate connected routes
2774 * because of IPv6 link-local routes and unnumbered
2775 * interfaces on Linux.
2777 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2781 /* If this route is kernel/connected route, notify the dataplane. */
2782 if (RIB_SYSTEM_ROUTE(re
)) {
2783 /* Notify dataplane */
2784 dplane_sys_route_add(rn
, re
);
2787 /* Link new re to node.*/
2788 if (IS_ZEBRA_DEBUG_RIB
) {
2789 rnode_debug(rn
, re
->vrf_id
,
2790 "Inserting route rn %p, re %p (%s) existing %p",
2791 rn
, re
, zebra_route_string(re
->type
), same
);
2793 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2794 route_entry_dump(p
, src_p
, re
);
2796 rib_addnode(rn
, re
, 1);
2799 /* Free implicit route.*/
2801 rib_delnode(rn
, same
);
2805 route_unlock_node(rn
);
2809 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2810 unsigned short instance
, int flags
, struct prefix
*p
,
2811 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2812 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2815 struct route_table
*table
;
2816 struct route_node
*rn
;
2817 struct route_entry
*re
;
2818 struct route_entry
*fib
= NULL
;
2819 struct route_entry
*same
= NULL
;
2820 struct nexthop
*rtnh
;
2821 char buf2
[INET6_ADDRSTRLEN
];
2824 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2827 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2834 apply_mask_ipv6(src_p
);
2836 /* Lookup route node. */
2837 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2839 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2841 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2842 if (src_p
&& src_p
->prefixlen
)
2843 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2847 if (IS_ZEBRA_DEBUG_RIB
)
2848 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2850 (src_buf
[0] != '\0') ? " from " : "",
2855 dest
= rib_dest_from_rnode(rn
);
2856 fib
= dest
->selected_fib
;
2858 /* Lookup same type route. */
2859 RNODE_FOREACH_RE (rn
, re
) {
2860 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2863 if (re
->type
!= type
)
2865 if (re
->instance
!= instance
)
2867 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2868 distance
!= re
->distance
)
2871 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2873 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2874 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2875 if (rtnh
->ifindex
!= nh
->ifindex
)
2880 /* Make sure that the route found has the same gateway. */
2886 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2887 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2895 /* If same type of route can't be found and this message is from
2899 * In the past(HA!) we could get here because
2900 * we were receiving a route delete from the
2901 * kernel and we're not marking the proto
2902 * as coming from it's appropriate originator.
2903 * Now that we are properly noticing the fact
2904 * that the kernel has deleted our route we
2905 * are not going to get called in this path
2906 * I am going to leave this here because
2907 * this might still work this way on non-linux
2908 * platforms as well as some weird state I have
2909 * not properly thought of yet.
2910 * If we can show that this code path is
2911 * dead then we can remove it.
2913 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2914 if (IS_ZEBRA_DEBUG_RIB
) {
2915 rnode_debug(rn
, vrf_id
,
2916 "rn %p, re %p (%s) was deleted from kernel, adding",
2918 zebra_route_string(fib
->type
));
2921 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2923 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2925 UNSET_FLAG(rtnh
->flags
,
2929 * This is a non FRR route
2930 * as such we should mark
2933 dest
->selected_fib
= NULL
;
2935 /* This means someone else, other than Zebra,
2937 * a Zebra router from the kernel. We will add
2939 rib_install_kernel(rn
, fib
, NULL
);
2942 if (IS_ZEBRA_DEBUG_RIB
) {
2946 "via %s ifindex %d type %d "
2947 "doesn't exist in rib",
2948 inet_ntop(afi2family(afi
),
2955 "type %d doesn't exist in rib",
2958 route_unlock_node(rn
);
2964 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2966 rib_install_kernel(rn
, same
, NULL
);
2967 route_unlock_node(rn
);
2972 /* Special handling for IPv4 or IPv6 routes sourced from
2973 * EVPN - the nexthop (and associated MAC) need to be
2974 * uninstalled if no more refs.
2976 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2977 struct nexthop
*tmp_nh
;
2979 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2980 struct ipaddr vtep_ip
;
2982 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2983 if (afi
== AFI_IP
) {
2984 vtep_ip
.ipa_type
= IPADDR_V4
;
2985 memcpy(&(vtep_ip
.ipaddr_v4
),
2986 &(tmp_nh
->gate
.ipv4
),
2987 sizeof(struct in_addr
));
2989 vtep_ip
.ipa_type
= IPADDR_V6
;
2990 memcpy(&(vtep_ip
.ipaddr_v6
),
2991 &(tmp_nh
->gate
.ipv6
),
2992 sizeof(struct in6_addr
));
2994 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2999 /* Notify dplane if system route changes */
3000 if (RIB_SYSTEM_ROUTE(re
))
3001 dplane_sys_route_del(rn
, same
);
3003 rib_delnode(rn
, same
);
3006 route_unlock_node(rn
);
3011 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3012 unsigned short instance
, int flags
, struct prefix
*p
,
3013 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3014 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
3017 struct route_entry
*re
;
3018 struct nexthop
*nexthop
;
3020 /* Allocate new route_entry structure. */
3021 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3023 re
->instance
= instance
;
3024 re
->distance
= distance
;
3026 re
->metric
= metric
;
3028 re
->table
= table_id
;
3029 re
->vrf_id
= vrf_id
;
3030 re
->nexthop_num
= 0;
3031 re
->uptime
= time(NULL
);
3035 nexthop
= nexthop_new();
3037 route_entry_nexthop_add(re
, nexthop
);
3039 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
3042 /* Schedule routes of a particular table (address-family) based on event. */
3043 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3045 struct route_node
*rn
;
3046 struct route_entry
*re
, *next
;
3048 /* Walk all routes and queue for processing, if appropriate for
3049 * the trigger event.
3051 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3053 * If we are looking at a route node and the node
3054 * has already been queued we don't
3055 * need to queue it up again
3057 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3058 RIB_ROUTE_ANY_QUEUED
))
3061 case RIB_UPDATE_IF_CHANGE
:
3062 /* Examine all routes that won't get processed by the
3064 * triggered by nexthop evaluation (NHT). This would be
3066 * kernel and certain static routes. Note that NHT will
3068 * triggered upon an interface event as connected routes
3070 * get queued for processing.
3072 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3075 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3076 && re
->type
!= ZEBRA_ROUTE_KERNEL
3077 && re
->type
!= ZEBRA_ROUTE_CONNECT
3078 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3081 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3086 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3087 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3088 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3091 /* If we only have nexthops to a
3100 case RIB_UPDATE_RMAP_CHANGE
:
3101 case RIB_UPDATE_OTHER
:
3102 /* Right now, examine all routes. Can restrict to a
3104 * some cases (TODO).
3106 if (rnode_to_ribs(rn
))
3116 /* RIB update function. */
3117 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3119 struct route_table
*table
;
3121 /* Process routes of interested address-families. */
3122 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3124 if (IS_ZEBRA_DEBUG_EVENT
)
3125 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3126 rib_update_table(table
, event
);
3129 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3131 if (IS_ZEBRA_DEBUG_EVENT
)
3132 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3133 rib_update_table(table
, event
);
3137 /* Delete self installed routes after zebra is relaunched. */
3138 void rib_sweep_table(struct route_table
*table
)
3140 struct route_node
*rn
;
3141 struct route_entry
*re
;
3142 struct route_entry
*next
;
3143 struct nexthop
*nexthop
;
3148 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3149 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3150 if (IS_ZEBRA_DEBUG_RIB
)
3151 route_entry_dump(&rn
->p
, NULL
, re
);
3153 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3156 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3160 * So we are starting up and have received
3161 * routes from the kernel that we have installed
3162 * from a previous run of zebra but not cleaned
3163 * up ( say a kill -9 )
3164 * But since we haven't actually installed
3165 * them yet( we received them from the kernel )
3166 * we don't think they are active.
3167 * So let's pretend they are active to actually
3169 * In all honesty I'm not sure if we should
3170 * mark them as active when we receive them
3171 * This is startup only so probably ok.
3173 * If we ever decide to move rib_sweep_table
3174 * to a different spot (ie startup )
3175 * this decision needs to be revisited
3177 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3178 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3179 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3181 rib_uninstall_kernel(rn
, re
);
3182 rib_delnode(rn
, re
);
3187 /* Sweep all RIB tables. */
3188 void rib_sweep_route(void)
3191 struct zebra_vrf
*zvrf
;
3193 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3194 if ((zvrf
= vrf
->info
) == NULL
)
3197 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3198 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3201 zebra_router_sweep_route();
3204 /* Remove specific by protocol routes from 'table'. */
3205 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3206 struct route_table
*table
)
3208 struct route_node
*rn
;
3209 struct route_entry
*re
;
3210 struct route_entry
*next
;
3211 unsigned long n
= 0;
3214 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3215 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3216 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3218 if (re
->type
== proto
3219 && re
->instance
== instance
) {
3220 rib_delnode(rn
, re
);
3227 /* Remove specific by protocol routes. */
3228 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3231 struct zebra_vrf
*zvrf
;
3232 unsigned long cnt
= 0;
3234 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3235 if ((zvrf
= vrf
->info
) != NULL
)
3236 cnt
+= rib_score_proto_table(
3238 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3239 + rib_score_proto_table(
3241 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3243 cnt
+= zebra_router_score_proto(proto
, instance
);
3248 /* Close RIB and clean up kernel routes. */
3249 void rib_close_table(struct route_table
*table
)
3251 struct route_node
*rn
;
3252 rib_table_info_t
*info
;
3258 info
= route_table_get_info(table
);
3260 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3261 dest
= rib_dest_from_rnode(rn
);
3263 if (dest
&& dest
->selected_fib
) {
3264 if (info
->safi
== SAFI_UNICAST
)
3265 hook_call(rib_update
, rn
, NULL
);
3267 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3268 dest
->selected_fib
= NULL
;
3274 * Handler for async dataplane results after a pseudowire installation
3276 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3278 struct zebra_pw
*pw
;
3279 struct zebra_vrf
*vrf
;
3281 /* The pseudowire code assumes success - we act on an error
3282 * result for installation attempts here.
3284 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3287 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3288 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3289 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3291 zebra_pw_install_failure(pw
);
3301 * Handle results from the dataplane system. Dequeue update context
3302 * structs, dispatch to appropriate internal handlers.
3304 static int rib_process_dplane_results(struct thread
*thread
)
3306 struct zebra_dplane_ctx
*ctx
;
3307 struct dplane_ctx_q ctxlist
;
3309 /* Dequeue a list of completed updates with one lock/unlock cycle */
3312 TAILQ_INIT(&ctxlist
);
3314 /* Take lock controlling queue of results */
3315 pthread_mutex_lock(&dplane_mutex
);
3317 /* Dequeue list of context structs */
3318 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3320 pthread_mutex_unlock(&dplane_mutex
);
3322 /* Dequeue context block */
3323 ctx
= dplane_ctx_dequeue(&ctxlist
);
3325 /* If we've emptied the results queue, we're done */
3330 switch (dplane_ctx_get_op(ctx
)) {
3331 case DPLANE_OP_ROUTE_INSTALL
:
3332 case DPLANE_OP_ROUTE_UPDATE
:
3333 case DPLANE_OP_ROUTE_DELETE
:
3334 rib_process_result(ctx
);
3337 case DPLANE_OP_LSP_INSTALL
:
3338 case DPLANE_OP_LSP_UPDATE
:
3339 case DPLANE_OP_LSP_DELETE
:
3340 zebra_mpls_lsp_dplane_result(ctx
);
3343 case DPLANE_OP_PW_INSTALL
:
3344 case DPLANE_OP_PW_UNINSTALL
:
3345 handle_pw_result(ctx
);
3348 case DPLANE_OP_SYS_ROUTE_ADD
:
3349 case DPLANE_OP_SYS_ROUTE_DELETE
:
3350 /* No further processing in zebra for these. */
3351 dplane_ctx_fini(&ctx
);
3355 /* Don't expect this: just return the struct? */
3356 dplane_ctx_fini(&ctx
);
3358 } /* Dispatch by op code */
3360 ctx
= dplane_ctx_dequeue(&ctxlist
);
3365 /* Check for nexthop tracking processing after finishing with results */
3366 do_nht_processing();
3372 * Results are returned from the dataplane subsystem, in the context of
3373 * the dataplane pthread. We enqueue the results here for processing by
3374 * the main thread later.
3376 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3378 /* Take lock controlling queue of results */
3379 pthread_mutex_lock(&dplane_mutex
);
3381 /* Enqueue context blocks */
3382 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3384 pthread_mutex_unlock(&dplane_mutex
);
3386 /* Ensure event is signalled to zebra main pthread */
3387 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3393 /* Routing information base initialize. */
3398 /* Init dataplane, and register for results */
3399 pthread_mutex_init(&dplane_mutex
, NULL
);
3400 TAILQ_INIT(&rib_dplane_q
);
3401 zebra_dplane_init(rib_dplane_results
);
3407 * Get the first vrf id that is greater than the given vrf id if any.
3409 * Returns TRUE if a vrf id was found, FALSE otherwise.
3411 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3415 vrf
= vrf_lookup_by_id(vrf_id
);
3417 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3419 *next_id_p
= vrf
->vrf_id
;
3428 * rib_tables_iter_next
3430 * Returns the next table in the iteration.
3432 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3434 struct route_table
*table
;
3437 * Array that helps us go over all AFI/SAFI combinations via one
3444 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3445 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3446 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3451 switch (iter
->state
) {
3453 case RIB_TABLES_ITER_S_INIT
:
3454 iter
->vrf_id
= VRF_DEFAULT
;
3455 iter
->afi_safi_ix
= -1;
3459 case RIB_TABLES_ITER_S_ITERATING
:
3460 iter
->afi_safi_ix
++;
3463 while (iter
->afi_safi_ix
3464 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3465 table
= zebra_vrf_table(
3466 afi_safis
[iter
->afi_safi_ix
].afi
,
3467 afi_safis
[iter
->afi_safi_ix
].safi
,
3472 iter
->afi_safi_ix
++;
3476 * Found another table in this vrf.
3482 * Done with all tables in the current vrf, go to the
3486 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3489 iter
->afi_safi_ix
= 0;
3494 case RIB_TABLES_ITER_S_DONE
:
3499 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3501 iter
->state
= RIB_TABLES_ITER_S_DONE
;