1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h"
35 #include "srcdest_table.h"
39 #include "workqueue.h"
41 #include "zebra/zebra_router.h"
42 #include "zebra/connected.h"
43 #include "zebra/debug.h"
44 #include "zebra/interface.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/rib.h"
48 #include "zebra/zapi_msg.h"
49 #include "zebra/zebra_errors.h"
50 #include "zebra/zebra_memory.h"
51 #include "zebra/zebra_ns.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/zebra_routemap.h"
54 #include "zebra/zebra_vrf.h"
55 #include "zebra/zebra_vxlan.h"
56 #include "zebra/zapi_msg.h"
57 #include "zebra/zebra_dplane.h"
60 * Event, list, and mutex for delivery of dataplane results
62 static pthread_mutex_t dplane_mutex
;
63 static struct thread
*t_dplane
;
64 static struct dplane_ctx_q rib_dplane_q
;
66 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
69 /* Should we allow non Quagga processes to delete our routes */
70 extern int allow_delete
;
72 /* Each route type's string and default distance value. */
77 } route_info
[ZEBRA_ROUTE_MAX
] = {
78 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
79 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
80 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
81 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
82 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
83 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
84 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
85 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
86 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
87 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
88 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
89 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
90 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
91 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
92 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
93 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
94 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
95 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
97 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
99 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
100 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
101 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
103 /* no entry/default: 150 */
106 /* RPF lookup behaviour */
107 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
110 static void __attribute__((format(printf
, 5, 6)))
111 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
112 int priority
, const char *msgfmt
, ...)
114 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
118 va_start(ap
, msgfmt
);
119 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
123 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
124 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
126 if (info
->safi
== SAFI_MULTICAST
)
127 strcat(buf
, " (MRIB)");
129 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
132 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
135 #define rnode_debug(node, vrf_id, ...) \
136 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
137 #define rnode_info(node, ...) \
138 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
140 uint8_t route_distance(int type
)
144 if ((unsigned)type
>= array_size(route_info
))
147 distance
= route_info
[type
].distance
;
152 int is_zebra_valid_kernel_table(uint32_t table_id
)
155 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
156 || (table_id
== RT_TABLE_COMPAT
))
163 int is_zebra_main_routing_table(uint32_t table_id
)
165 if ((table_id
== RT_TABLE_MAIN
)
166 || (table_id
== zebrad
.rtm_table_default
))
171 int zebra_check_addr(const struct prefix
*p
)
173 if (p
->family
== AF_INET
) {
176 addr
= p
->u
.prefix4
.s_addr
;
179 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
180 || IPV4_LINKLOCAL(addr
))
183 if (p
->family
== AF_INET6
) {
184 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
186 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
192 /* Add nexthop to the end of a rib node's nexthop list */
193 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
195 nexthop_add(&re
->ng
.nexthop
, nexthop
);
201 * copy_nexthop - copy a nexthop to the rib structure.
203 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
205 assert(!re
->ng
.nexthop
);
206 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
207 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
211 /* Delete specified nexthop from the list. */
212 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
215 nexthop
->next
->prev
= nexthop
->prev
;
217 nexthop
->prev
->next
= nexthop
->next
;
219 re
->ng
.nexthop
= nexthop
->next
;
224 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
228 struct nexthop
*nexthop
;
230 nexthop
= nexthop_new();
231 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
232 nexthop
->ifindex
= ifindex
;
233 nexthop
->vrf_id
= nh_vrf_id
;
235 route_entry_nexthop_add(re
, nexthop
);
240 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
241 struct in_addr
*ipv4
,
245 struct nexthop
*nexthop
;
247 nexthop
= nexthop_new();
248 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
249 nexthop
->vrf_id
= nh_vrf_id
;
250 nexthop
->gate
.ipv4
= *ipv4
;
252 nexthop
->src
.ipv4
= *src
;
254 route_entry_nexthop_add(re
, nexthop
);
259 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
260 struct in_addr
*ipv4
,
265 struct nexthop
*nexthop
;
266 struct interface
*ifp
;
268 nexthop
= nexthop_new();
269 nexthop
->vrf_id
= nh_vrf_id
;
270 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
271 nexthop
->gate
.ipv4
= *ipv4
;
273 nexthop
->src
.ipv4
= *src
;
274 nexthop
->ifindex
= ifindex
;
275 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
276 /*Pending: need to think if null ifp here is ok during bootup?
277 There was a crash because ifp here was coming to be NULL */
279 if (connected_is_unnumbered(ifp
)
280 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)
281 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
282 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
;
318 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)
319 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
320 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
323 route_entry_nexthop_add(re
, nexthop
);
328 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
329 enum blackhole_type bh_type
)
331 struct nexthop
*nexthop
;
333 nexthop
= nexthop_new();
334 nexthop
->vrf_id
= VRF_DEFAULT
;
335 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
336 nexthop
->bh_type
= bh_type
;
338 route_entry_nexthop_add(re
, nexthop
);
343 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
344 struct nexthop
*nexthop
)
346 struct nexthop
*resolved_hop
;
348 resolved_hop
= nexthop_new();
349 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
351 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
352 switch (newhop
->type
) {
353 case NEXTHOP_TYPE_IPV4
:
354 case NEXTHOP_TYPE_IPV4_IFINDEX
:
355 /* If the resolving route specifies a gateway, use it */
356 resolved_hop
->type
= newhop
->type
;
357 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
359 if (newhop
->ifindex
) {
360 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
361 resolved_hop
->ifindex
= newhop
->ifindex
;
364 case NEXTHOP_TYPE_IPV6
:
365 case NEXTHOP_TYPE_IPV6_IFINDEX
:
366 resolved_hop
->type
= newhop
->type
;
367 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
369 if (newhop
->ifindex
) {
370 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
371 resolved_hop
->ifindex
= newhop
->ifindex
;
374 case NEXTHOP_TYPE_IFINDEX
:
375 /* If the resolving route is an interface route,
376 * it means the gateway we are looking up is connected
377 * to that interface. (The actual network is _not_ onlink).
378 * Therefore, the resolved route should have the original
379 * gateway as nexthop as it is directly connected.
381 * On Linux, we have to set the onlink netlink flag because
382 * otherwise, the kernel won't accept the route.
384 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
386 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
387 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
388 } else if (afi
== AFI_IP6
) {
389 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
390 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
392 resolved_hop
->ifindex
= newhop
->ifindex
;
394 case NEXTHOP_TYPE_BLACKHOLE
:
395 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
396 resolved_hop
->bh_type
= nexthop
->bh_type
;
400 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
401 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
403 /* Copy labels of the resolved route */
404 if (newhop
->nh_label
)
405 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
406 newhop
->nh_label
->num_labels
,
407 &newhop
->nh_label
->label
[0]);
409 resolved_hop
->rparent
= nexthop
;
410 nexthop_add(&nexthop
->resolved
, resolved_hop
);
413 /* If force flag is not set, do not modify falgs at all for uninstall
414 the route from FIB. */
415 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
416 struct nexthop
*nexthop
, bool set
,
417 struct route_node
*top
)
420 struct route_table
*table
;
421 struct route_node
*rn
;
422 struct route_entry
*match
= NULL
;
424 struct nexthop
*newhop
;
425 struct interface
*ifp
;
428 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
429 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
430 nexthop
->ifindex
= 0;
433 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
434 nexthops_free(nexthop
->resolved
);
435 nexthop
->resolved
= NULL
;
439 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
440 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
443 /* Skip nexthops that have been filtered out due to route-map */
444 /* The nexthops are specific to this route and so the same */
445 /* nexthop for a different route may not have this flag set */
446 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
)) {
447 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
448 zlog_debug("\t%s: Nexthop Filtered",
449 __PRETTY_FUNCTION__
);
454 * Check to see if we should trust the passed in information
455 * for UNNUMBERED interfaces as that we won't find the GW
456 * address in the routing table.
458 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
459 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
460 if ((ifp
&& connected_is_unnumbered(ifp
))
461 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ONLINK
)) {
462 if (if_is_operative(ifp
))
465 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
467 "\t%s: Onlink and interface %s is not operative",
468 __PRETTY_FUNCTION__
, ifp
->name
);
472 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
474 "\t%s: Interface %s is not unnumbered",
476 ifp
? ifp
->name
: "Unknown");
481 /* Make lookup prefix. */
482 memset(&p
, 0, sizeof(struct prefix
));
486 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
487 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
491 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
492 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
495 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
499 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
501 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
502 zlog_debug("\t%s: Table not found",
503 __PRETTY_FUNCTION__
);
507 rn
= route_node_match(table
, (struct prefix
*)&p
);
509 route_unlock_node(rn
);
511 /* Lookup should halt if we've matched against ourselves ('top',
512 * if specified) - i.e., we cannot have a nexthop NH1 is
513 * resolved by a route NH1. The exception is if the route is a
516 if (top
&& rn
== top
)
517 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
518 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
519 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
521 "\t%s: Matched against ourself and prefix length is not max bit length",
522 __PRETTY_FUNCTION__
);
526 /* Pick up selected route. */
527 /* However, do not resolve over default route unless explicitly
529 if (is_default_prefix(&rn
->p
)
530 && !rnh_resolve_via_default(p
.family
)) {
531 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
533 "\t:%s: Resolved against default route",
534 __PRETTY_FUNCTION__
);
538 dest
= rib_dest_from_rnode(rn
);
539 if (dest
&& dest
->selected_fib
540 && !CHECK_FLAG(dest
->selected_fib
->status
,
542 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
543 match
= dest
->selected_fib
;
545 /* If there is no selected route or matched route is EGP, go up
550 } while (rn
&& rn
->info
== NULL
);
557 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
558 /* Directly point connected route. */
559 newhop
= match
->ng
.nexthop
;
561 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
562 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
563 nexthop
->ifindex
= newhop
->ifindex
;
566 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
568 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
569 if (!CHECK_FLAG(newhop
->flags
,
572 if (CHECK_FLAG(newhop
->flags
,
573 NEXTHOP_FLAG_RECURSIVE
))
577 SET_FLAG(nexthop
->flags
,
578 NEXTHOP_FLAG_RECURSIVE
);
580 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
581 nexthop_set_resolved(afi
, newhop
,
587 re
->nexthop_mtu
= match
->mtu
;
588 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
589 zlog_debug("\t%s: Recursion failed to find",
590 __PRETTY_FUNCTION__
);
592 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
594 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
595 if (!CHECK_FLAG(newhop
->flags
,
600 SET_FLAG(nexthop
->flags
,
601 NEXTHOP_FLAG_RECURSIVE
);
602 nexthop_set_resolved(afi
, newhop
,
608 re
->nexthop_mtu
= match
->mtu
;
610 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
612 "\t%s: Static route unable to resolve",
613 __PRETTY_FUNCTION__
);
616 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
617 zlog_debug("\t%s: Route Type %s has not turned on recursion",
619 zebra_route_string(re
->type
));
620 if (re
->type
== ZEBRA_ROUTE_BGP
&&
621 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
622 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
627 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
628 zlog_debug("\t%s: Nexthop did not lookup in table",
629 __PRETTY_FUNCTION__
);
633 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
634 union g_addr
*addr
, struct route_node
**rn_out
)
637 struct route_table
*table
;
638 struct route_node
*rn
;
639 struct route_entry
*match
= NULL
;
640 struct nexthop
*newhop
;
643 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
647 memset(&p
, 0, sizeof(struct prefix
));
650 p
.u
.prefix4
= addr
->ipv4
;
651 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
653 p
.u
.prefix6
= addr
->ipv6
;
654 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
657 rn
= route_node_match(table
, (struct prefix
*)&p
);
662 route_unlock_node(rn
);
664 dest
= rib_dest_from_rnode(rn
);
665 if (dest
&& dest
->selected_fib
666 && !CHECK_FLAG(dest
->selected_fib
->status
,
667 ROUTE_ENTRY_REMOVED
))
668 match
= dest
->selected_fib
;
670 /* If there is no selected route or matched route is EGP, go up
675 } while (rn
&& rn
->info
== NULL
);
679 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
681 for (ALL_NEXTHOPS(match
->ng
, newhop
))
682 if (CHECK_FLAG(newhop
->flags
,
699 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
701 struct route_node
**rn_out
)
703 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
704 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
705 union g_addr gaddr
= {.ipv4
= addr
};
707 switch (ipv4_multicast_mode
) {
708 case MCAST_MRIB_ONLY
:
709 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
711 case MCAST_URIB_ONLY
:
712 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
713 case MCAST_NO_CONFIG
:
714 case MCAST_MIX_MRIB_FIRST
:
715 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
718 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
721 case MCAST_MIX_DISTANCE
:
722 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
723 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
725 re
= ure
->distance
< mre
->distance
? ure
: mre
;
731 case MCAST_MIX_PFXLEN
:
732 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
733 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
735 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
744 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
746 if (IS_ZEBRA_DEBUG_RIB
) {
748 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
750 zlog_debug("%s: %s: vrf: %u found %s, using %s",
751 __func__
, buf
, vrf_id
,
752 mre
? (ure
? "MRIB+URIB" : "MRIB")
753 : ure
? "URIB" : "nothing",
754 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
759 void multicast_mode_ipv4_set(enum multicast_mode mode
)
761 if (IS_ZEBRA_DEBUG_RIB
)
762 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
764 ipv4_multicast_mode
= mode
;
767 enum multicast_mode
multicast_mode_ipv4_get(void)
769 return ipv4_multicast_mode
;
772 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
774 struct route_table
*table
;
775 struct route_node
*rn
;
776 struct route_entry
*match
= NULL
;
777 struct nexthop
*nexthop
;
781 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
785 rn
= route_node_lookup(table
, (struct prefix
*)p
);
787 /* No route for this prefix. */
792 route_unlock_node(rn
);
793 dest
= rib_dest_from_rnode(rn
);
795 if (dest
&& dest
->selected_fib
796 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
797 match
= dest
->selected_fib
;
802 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
805 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
806 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
812 #define RIB_SYSTEM_ROUTE(R) \
813 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
815 #define RIB_KERNEL_ROUTE(R) \
816 ((R)->type == ZEBRA_ROUTE_KERNEL)
818 /* This function verifies reachability of one given nexthop, which can be
819 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
820 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
821 * nexthop->ifindex will be updated appropriately as well.
822 * An existing route map can turn (otherwise active) nexthop into inactive, but
825 * The return value is the final value of 'ACTIVE' flag.
828 static unsigned nexthop_active_check(struct route_node
*rn
,
829 struct route_entry
*re
,
830 struct nexthop
*nexthop
, bool set
)
832 struct interface
*ifp
;
833 route_map_result_t ret
= RMAP_MATCH
;
835 char buf
[SRCDEST2STR_BUFFER
];
836 const struct prefix
*p
, *src_p
;
837 struct zebra_vrf
*zvrf
;
839 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
841 if (rn
->p
.family
== AF_INET
)
843 else if (rn
->p
.family
== AF_INET6
)
847 switch (nexthop
->type
) {
848 case NEXTHOP_TYPE_IFINDEX
:
849 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
850 if (ifp
&& if_is_operative(ifp
))
851 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
853 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
855 case NEXTHOP_TYPE_IPV4
:
856 case NEXTHOP_TYPE_IPV4_IFINDEX
:
858 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
859 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
861 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
863 case NEXTHOP_TYPE_IPV6
:
865 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
866 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
868 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 case NEXTHOP_TYPE_IPV6_IFINDEX
:
871 /* RFC 5549, v4 prefix with v6 NH */
872 if (rn
->p
.family
!= AF_INET
)
874 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
875 ifp
= if_lookup_by_index(nexthop
->ifindex
,
877 if (ifp
&& if_is_operative(ifp
))
878 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
880 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
882 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
883 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
885 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
888 case NEXTHOP_TYPE_BLACKHOLE
:
889 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
894 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
895 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
896 zlog_debug("\t%s: Unable to find a active nexthop",
897 __PRETTY_FUNCTION__
);
901 /* XXX: What exactly do those checks do? Do we support
902 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
904 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
905 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
906 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
908 /* The original code didn't determine the family correctly
909 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
910 * from the rib_table_info in those cases.
911 * Possibly it may be better to use only the rib_table_info
915 rib_table_info_t
*info
;
917 info
= srcdest_rnode_table_info(rn
);
921 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
923 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
925 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
926 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
927 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
930 /* It'll get set if required inside */
931 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
932 nexthop
, zvrf
, re
->tag
);
933 if (ret
== RMAP_DENYMATCH
) {
934 if (IS_ZEBRA_DEBUG_RIB
) {
935 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
937 "%u:%s: Filtering out with NH out %s due to route map",
939 ifindex2ifname(nexthop
->ifindex
,
942 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
944 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
947 /* Iterate over all nexthops of the given RIB entry and refresh their
948 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
949 * nexthop is found to toggle the ACTIVE flag, the whole re structure
950 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
951 * transparently passed to nexthop_active_check().
953 * Return value is the new number of active nexthops.
956 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
959 struct nexthop
*nexthop
;
960 union g_addr prev_src
;
961 unsigned int prev_active
, new_active
, old_num_nh
;
962 ifindex_t prev_index
;
964 old_num_nh
= re
->nexthop_active_num
;
966 re
->nexthop_active_num
= 0;
967 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
969 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
970 /* No protocol daemon provides src and so we're skipping
972 prev_src
= nexthop
->rmap_src
;
973 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
974 prev_index
= nexthop
->ifindex
;
976 * We need to respect the multipath_num here
977 * as that what we should be able to install from
978 * a multipath perpsective should not be a data plane
981 new_active
= nexthop_active_check(rn
, re
, nexthop
, set
);
982 if (new_active
&& re
->nexthop_active_num
>= multipath_num
) {
983 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
987 re
->nexthop_active_num
++;
988 /* Don't allow src setting on IPv6 addr for now */
989 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
990 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
991 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
992 && prev_src
.ipv4
.s_addr
993 != nexthop
->rmap_src
.ipv4
.s_addr
)
994 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
995 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
996 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
997 &nexthop
->rmap_src
.ipv6
)))) {
998 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
999 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1003 if (old_num_nh
!= re
->nexthop_active_num
)
1004 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1006 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1007 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1010 return re
->nexthop_active_num
;
1014 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1015 * (nexthops) must have a label.
1017 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1019 struct nexthop
*nexthop
= NULL
;
1021 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1024 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1025 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1031 /* Update flag indicates whether this is a "replace" or not. Currently, this
1032 * is only used for IPv4.
1034 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1035 struct route_entry
*old
)
1037 struct nexthop
*nexthop
;
1038 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1039 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1040 const struct prefix
*p
, *src_p
;
1041 enum zebra_dplane_result ret
;
1043 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1045 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1047 if (info
->safi
!= SAFI_UNICAST
) {
1048 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1049 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1052 struct nexthop
*prev
;
1054 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1055 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1056 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1057 if (prev
== nexthop
)
1059 if (nexthop_same_firsthop(nexthop
, prev
)) {
1060 SET_FLAG(nexthop
->flags
,
1061 NEXTHOP_FLAG_DUPLICATE
);
1069 * If this is a replace to a new RE let the originator of the RE
1070 * know that they've lost
1072 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1073 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1075 /* Update fib selection */
1076 dest
->selected_fib
= re
;
1079 * Make sure we update the FPM any time we send new information to
1082 hook_call(rib_update
, rn
, "installing in kernel");
1084 /* Send add or update */
1085 if (old
&& (old
!= re
))
1086 ret
= dplane_route_update(rn
, re
, old
);
1088 ret
= dplane_route_add(rn
, re
);
1091 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1093 zvrf
->installs_queued
++;
1095 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1097 char str
[SRCDEST2STR_BUFFER
];
1099 srcdest_rnode2str(rn
, str
, sizeof(str
));
1100 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1101 "%u:%s: Failed to enqueue dataplane install",
1105 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1114 /* Uninstall the route from kernel. */
1115 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1117 struct nexthop
*nexthop
;
1118 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1119 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1121 if (info
->safi
!= SAFI_UNICAST
) {
1122 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1123 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1128 * Make sure we update the FPM any time we send new information to
1131 hook_call(rib_update
, rn
, "uninstalling from kernel");
1133 switch (dplane_route_delete(rn
, re
)) {
1134 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1136 zvrf
->removals_queued
++;
1138 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1140 char str
[SRCDEST2STR_BUFFER
];
1142 srcdest_rnode2str(rn
, str
, sizeof(str
));
1143 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1144 "%u:%s: Failed to enqueue dataplane uninstall",
1148 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1157 /* Uninstall the route from kernel. */
1158 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1160 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1161 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1162 struct nexthop
*nexthop
;
1164 if (dest
&& dest
->selected_fib
== re
) {
1165 if (info
->safi
== SAFI_UNICAST
)
1166 hook_call(rib_update
, rn
, "rib_uninstall");
1168 /* If labeled-unicast route, uninstall transit LSP. */
1169 if (zebra_rib_labeled_unicast(re
))
1170 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1172 if (!RIB_SYSTEM_ROUTE(re
))
1173 rib_uninstall_kernel(rn
, re
);
1175 dest
->selected_fib
= NULL
;
1177 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1178 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1181 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1182 const struct prefix
*p
, *src_p
;
1184 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1186 redistribute_delete(p
, src_p
, re
);
1187 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1192 * rib_can_delete_dest
1194 * Returns TRUE if the given dest can be deleted from the table.
1196 static int rib_can_delete_dest(rib_dest_t
*dest
)
1203 * Don't delete the dest if we have to update the FPM about this
1206 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1207 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1216 * Garbage collect the rib dest corresponding to the given route node
1219 * Returns TRUE if the dest was deleted, FALSE otherwise.
1221 int rib_gc_dest(struct route_node
*rn
)
1225 dest
= rib_dest_from_rnode(rn
);
1229 if (!rib_can_delete_dest(dest
))
1232 if (IS_ZEBRA_DEBUG_RIB
) {
1233 struct zebra_vrf
*zvrf
;
1235 zvrf
= rib_dest_vrf(dest
);
1236 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1240 XFREE(MTYPE_RIB_DEST
, dest
);
1244 * Release the one reference that we keep on the route node.
1246 route_unlock_node(rn
);
1250 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1251 struct route_entry
*new)
1253 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1255 hook_call(rib_update
, rn
, "new route selected");
1257 /* Update real nexthop. This may actually determine if nexthop is active
1259 if (!nexthop_active_update(rn
, new, true)) {
1260 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1264 if (IS_ZEBRA_DEBUG_RIB
) {
1265 char buf
[SRCDEST2STR_BUFFER
];
1266 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1267 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1268 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1271 /* If labeled-unicast route, install transit LSP. */
1272 if (zebra_rib_labeled_unicast(new))
1273 zebra_mpls_lsp_install(zvrf
, rn
, new);
1275 if (!RIB_SYSTEM_ROUTE(new))
1276 rib_install_kernel(rn
, new, NULL
);
1278 dest
->selected_fib
= new;
1280 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1283 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1284 struct route_entry
*old
)
1286 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1287 hook_call(rib_update
, rn
, "removing existing route");
1289 /* Uninstall from kernel. */
1290 if (IS_ZEBRA_DEBUG_RIB
) {
1291 char buf
[SRCDEST2STR_BUFFER
];
1292 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1293 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1294 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1297 /* If labeled-unicast route, uninstall transit LSP. */
1298 if (zebra_rib_labeled_unicast(old
))
1299 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1301 if (!RIB_SYSTEM_ROUTE(old
))
1302 rib_uninstall_kernel(rn
, old
);
1305 * We are setting this to NULL here
1306 * because that is what we traditionally
1307 * have been doing. I am not positive
1308 * that this is the right thing to do
1309 * but let's leave the code alone
1310 * for the RIB_SYSTEM_ROUTE case
1312 dest
->selected_fib
= NULL
;
1315 /* Update nexthop for route, reset changed flag. */
1316 /* Note: this code also handles the Linux case when an interface goes
1317 * down, causing the kernel to delete routes without sending DELROUTE
1320 if (!nexthop_active_update(rn
, old
, true) &&
1321 (RIB_KERNEL_ROUTE(old
)))
1322 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1324 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1327 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1328 struct route_node
*rn
,
1329 struct route_entry
*old
,
1330 struct route_entry
*new)
1332 struct nexthop
*nexthop
= NULL
;
1334 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1337 * We have to install or update if a new route has been selected or
1338 * something has changed.
1340 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1341 hook_call(rib_update
, rn
, "updating existing route");
1343 /* Update the nexthop; we could determine here that nexthop is
1345 if (nexthop_active_update(rn
, new, true))
1348 /* If nexthop is active, install the selected route, if
1350 * the install succeeds, cleanup flags for prior route, if
1355 if (IS_ZEBRA_DEBUG_RIB
) {
1356 char buf
[SRCDEST2STR_BUFFER
];
1357 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1360 "%u:%s: Updating route rn %p, re %p (type %d) "
1362 zvrf_id(zvrf
), buf
, rn
, new,
1363 new->type
, old
, old
->type
);
1366 "%u:%s: Updating route rn %p, re %p (type %d)",
1367 zvrf_id(zvrf
), buf
, rn
, new,
1371 /* If labeled-unicast route, uninstall transit LSP. */
1372 if (zebra_rib_labeled_unicast(old
))
1373 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1375 /* Non-system route should be installed. */
1376 if (!RIB_SYSTEM_ROUTE(new)) {
1377 /* If labeled-unicast route, install transit
1379 if (zebra_rib_labeled_unicast(new))
1380 zebra_mpls_lsp_install(zvrf
, rn
, new);
1382 rib_install_kernel(rn
, new, old
);
1385 * We do not need to install the
1386 * selected route because it
1387 * is already isntalled by
1388 * the system( ie not us )
1389 * so just mark it as winning
1390 * we do need to ensure that
1391 * if we uninstall a route
1392 * from ourselves we don't
1393 * over write this pointer
1395 dest
->selected_fib
= NULL
;
1397 /* If install succeeded or system route, cleanup flags
1398 * for prior route. */
1400 if (RIB_SYSTEM_ROUTE(new)) {
1401 if (!RIB_SYSTEM_ROUTE(old
))
1402 rib_uninstall_kernel(rn
, old
);
1404 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1405 nexthop
= nexthop
->next
)
1406 UNSET_FLAG(nexthop
->flags
,
1413 * If nexthop for selected route is not active or install
1415 * may need to uninstall and delete for redistribution.
1418 if (IS_ZEBRA_DEBUG_RIB
) {
1419 char buf
[SRCDEST2STR_BUFFER
];
1420 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1423 "%u:%s: Deleting route rn %p, re %p (type %d) "
1424 "old %p (type %d) - nexthop inactive",
1425 zvrf_id(zvrf
), buf
, rn
, new,
1426 new->type
, old
, old
->type
);
1429 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1430 zvrf_id(zvrf
), buf
, rn
, new,
1434 /* If labeled-unicast route, uninstall transit LSP. */
1435 if (zebra_rib_labeled_unicast(old
))
1436 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1438 if (!RIB_SYSTEM_ROUTE(old
))
1439 rib_uninstall_kernel(rn
, old
);
1441 dest
->selected_fib
= NULL
;
1445 * Same route selected; check if in the FIB and if not,
1447 * is housekeeping code to deal with race conditions in kernel
1449 * netlink reporting interface up before IPv4 or IPv6 protocol
1453 if (!RIB_SYSTEM_ROUTE(new)) {
1454 bool in_fib
= false;
1456 for (ALL_NEXTHOPS(new->ng
, nexthop
))
1457 if (CHECK_FLAG(nexthop
->flags
,
1458 NEXTHOP_FLAG_FIB
)) {
1463 rib_install_kernel(rn
, new, NULL
);
1467 /* Update prior route. */
1469 /* Set real nexthop. */
1470 nexthop_active_update(rn
, old
, true);
1471 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1474 /* Clear changed flag. */
1475 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1478 /* Check if 'alternate' RIB entry is better than 'current'. */
1479 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1480 struct route_entry
*alternate
)
1482 if (current
== NULL
)
1485 /* filter route selection in following order:
1486 * - connected beats other types
1487 * - if both connected, loopback or vrf wins
1488 * - lower distance beats higher
1489 * - lower metric beats higher for equal distance
1490 * - last, hence oldest, route wins tie break.
1493 /* Connected routes. Check to see if either are a vrf
1494 * or loopback interface. If not, pick the last connected
1495 * route of the set of lowest metric connected routes.
1497 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1498 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1501 /* both are connected. are either loop or vrf? */
1502 struct nexthop
*nexthop
= NULL
;
1504 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1505 if (if_is_loopback_or_vrf(if_lookup_by_index(
1506 nexthop
->ifindex
, alternate
->vrf_id
)))
1510 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1511 if (if_is_loopback_or_vrf(if_lookup_by_index(
1512 nexthop
->ifindex
, current
->vrf_id
)))
1516 /* Neither are loop or vrf so pick best metric */
1517 if (alternate
->metric
<= current
->metric
)
1523 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1526 /* higher distance loses */
1527 if (alternate
->distance
< current
->distance
)
1529 if (current
->distance
< alternate
->distance
)
1532 /* metric tie-breaks equal distance */
1533 if (alternate
->metric
<= current
->metric
)
1539 /* Core function for processing routing information base. */
1540 static void rib_process(struct route_node
*rn
)
1542 struct route_entry
*re
;
1543 struct route_entry
*next
;
1544 struct route_entry
*old_selected
= NULL
;
1545 struct route_entry
*new_selected
= NULL
;
1546 struct route_entry
*old_fib
= NULL
;
1547 struct route_entry
*new_fib
= NULL
;
1548 struct route_entry
*best
= NULL
;
1549 char buf
[SRCDEST2STR_BUFFER
];
1551 struct zebra_vrf
*zvrf
= NULL
;
1552 const struct prefix
*p
, *src_p
;
1554 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1555 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1559 dest
= rib_dest_from_rnode(rn
);
1561 zvrf
= rib_dest_vrf(dest
);
1562 vrf_id
= zvrf_id(zvrf
);
1565 if (IS_ZEBRA_DEBUG_RIB
)
1566 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1568 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1569 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1572 * we can have rn's that have a NULL info pointer
1573 * (dest). As such let's not let the deref happen
1574 * additionally we know RNODE_FOREACH_RE_SAFE
1575 * will not iterate so we are ok.
1578 old_fib
= dest
->selected_fib
;
1580 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1581 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1583 "%u:%s: Examine re %p (type %d) status %x flags %x "
1584 "dist %d metric %d",
1585 vrf_id
, buf
, re
, re
->type
, re
->status
,
1586 re
->flags
, re
->distance
, re
->metric
);
1588 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1590 /* Currently selected re. */
1591 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1592 assert(old_selected
== NULL
);
1596 /* Skip deleted entries from selection */
1597 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1600 /* Skip unreachable nexthop. */
1601 /* This first call to nexthop_active_update is merely to
1603 * there's any change to nexthops associated with this RIB
1605 * rib_process() can be invoked due to an external event such as
1607 * down or due to next-hop-tracking evaluation. In the latter
1609 * a decision has already been made that the NHs have changed.
1611 * need to invoke a potentially expensive call again. Further,
1613 * the change might be in a recursive NH which is not caught in
1614 * the nexthop_active_update() code. Thus, we might miss changes
1618 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1619 && !nexthop_active_update(rn
, re
, false)) {
1620 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1621 /* XXX: HERE BE DRAGONS!!!!!
1622 * In all honesty, I have not yet figured out
1624 * does or why the ROUTE_ENTRY_CHANGED test
1626 * or why we need to delete a route here, and
1628 * this concerns both selected and fib route, or
1631 /* This entry was denied by the 'ip protocol
1632 * table' route-map, we
1633 * need to delete it */
1634 if (re
!= old_selected
) {
1635 if (IS_ZEBRA_DEBUG_RIB
)
1637 "%s: %u:%s: imported via import-table but denied "
1638 "by the ip protocol table route-map",
1639 __func__
, vrf_id
, buf
);
1642 SET_FLAG(re
->status
,
1643 ROUTE_ENTRY_REMOVED
);
1649 /* Infinite distance. */
1650 if (re
->distance
== DISTANCE_INFINITY
) {
1651 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1655 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1656 best
= rib_choose_best(new_fib
, re
);
1657 if (new_fib
&& best
!= new_fib
)
1658 UNSET_FLAG(new_fib
->status
,
1659 ROUTE_ENTRY_CHANGED
);
1662 best
= rib_choose_best(new_selected
, re
);
1663 if (new_selected
&& best
!= new_selected
)
1664 UNSET_FLAG(new_selected
->status
,
1665 ROUTE_ENTRY_CHANGED
);
1666 new_selected
= best
;
1669 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1670 } /* RNODE_FOREACH_RE */
1672 /* If no FIB override route, use the selected route also for FIB */
1673 if (new_fib
== NULL
)
1674 new_fib
= new_selected
;
1676 /* After the cycle is finished, the following pointers will be set:
1677 * old_selected --- RE entry currently having SELECTED
1678 * new_selected --- RE entry that is newly SELECTED
1679 * old_fib --- RE entry currently in kernel FIB
1680 * new_fib --- RE entry that is newly to be in kernel FIB
1682 * new_selected will get SELECTED flag, and is going to be redistributed
1683 * the zclients. new_fib (which can be new_selected) will be installed
1687 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1689 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1690 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1691 (void *)old_fib
, (void *)new_fib
);
1694 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1695 * fib == selected */
1696 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1697 ROUTE_ENTRY_CHANGED
);
1699 /* Update fib according to selection results */
1700 if (new_fib
&& old_fib
)
1701 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1703 rib_process_add_fib(zvrf
, rn
, new_fib
);
1705 rib_process_del_fib(zvrf
, rn
, old_fib
);
1707 /* Update SELECTED entry */
1708 if (old_selected
!= new_selected
|| selected_changed
) {
1710 if (new_selected
&& new_selected
!= new_fib
) {
1711 nexthop_active_update(rn
, new_selected
, true);
1712 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1716 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1718 /* Special case: new route is system route, so
1719 * dataplane update will not be done - ensure we
1720 * redistribute the route.
1722 if (RIB_SYSTEM_ROUTE(new_selected
))
1723 redistribute_update(p
, src_p
, new_selected
,
1729 redistribute_delete(p
, src_p
, old_selected
);
1730 if (old_selected
!= new_selected
)
1731 UNSET_FLAG(old_selected
->flags
,
1732 ZEBRA_FLAG_SELECTED
);
1736 /* Remove all RE entries queued for removal */
1737 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1738 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1739 if (IS_ZEBRA_DEBUG_RIB
) {
1740 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1741 (void *)rn
, (void *)re
);
1748 * Check if the dest can be deleted now.
1754 * Utility to match route with dplane context data
1756 static bool rib_route_match_ctx(const struct route_entry
*re
,
1757 const struct zebra_dplane_ctx
*ctx
,
1760 bool result
= false;
1764 * In 'update' case, we test info about the 'previous' or
1767 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1768 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1771 /* TODO -- we're using this extra test, but it's not
1772 * exactly clear why.
1774 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1775 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1776 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1783 * Ordinary, single-route case using primary context info
1785 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1786 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1787 /* Skip route that's been deleted */
1791 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1792 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1795 /* TODO -- we're using this extra test, but it's not
1796 * exactly clear why.
1798 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1799 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1800 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1812 * Route-update results processing after async dataplane update.
1814 static void rib_process_after(struct zebra_dplane_ctx
*ctx
)
1816 struct route_table
*table
= NULL
;
1817 struct zebra_vrf
*zvrf
= NULL
;
1818 struct route_node
*rn
= NULL
;
1819 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1820 bool is_update
= false;
1821 struct nexthop
*nexthop
, *ctx_nexthop
;
1822 char dest_str
[PREFIX_STRLEN
] = "";
1823 enum dplane_op_e op
;
1824 enum zebra_dplane_result status
;
1825 const struct prefix
*dest_pfx
, *src_pfx
;
1827 /* Locate rn and re(s) from ctx */
1829 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1830 dplane_ctx_get_safi(ctx
),
1831 dplane_ctx_get_vrf(ctx
),
1832 dplane_ctx_get_table(ctx
));
1833 if (table
== NULL
) {
1834 if (IS_ZEBRA_DEBUG_DPLANE
) {
1835 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1836 dplane_ctx_get_afi(ctx
),
1837 dplane_ctx_get_safi(ctx
),
1838 dplane_ctx_get_vrf(ctx
));
1843 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1845 dest_pfx
= dplane_ctx_get_dest(ctx
);
1847 /* Note well: only capturing the prefix string if debug is enabled here;
1848 * unconditional log messages will have to generate the string.
1850 if (IS_ZEBRA_DEBUG_DPLANE
)
1851 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1853 src_pfx
= dplane_ctx_get_src(ctx
);
1854 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1855 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1857 if (IS_ZEBRA_DEBUG_DPLANE
) {
1858 zlog_debug("Failed to process dplane results: no route for %u:%s",
1859 dplane_ctx_get_vrf(ctx
), dest_str
);
1864 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1866 op
= dplane_ctx_get_op(ctx
);
1867 status
= dplane_ctx_get_status(ctx
);
1869 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1870 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1871 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1872 dplane_op2str(op
), dplane_res2str(status
));
1875 * Update is a bit of a special case, where we may have both old and new
1876 * routes to post-process.
1878 is_update
= dplane_ctx_is_update(ctx
);
1881 * Take a pass through the routes, look for matches with the context
1884 RNODE_FOREACH_RE(rn
, rib
) {
1887 if (rib_route_match_ctx(rib
, ctx
, false))
1891 /* Check for old route match */
1892 if (is_update
&& (old_re
== NULL
)) {
1893 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1897 /* Have we found the routes we need to work on? */
1898 if (re
&& ((!is_update
|| old_re
)))
1903 * Check sequence number(s) to detect stale results before continuing
1905 if (re
&& (re
->dplane_sequence
!= dplane_ctx_get_seq(ctx
))) {
1906 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1907 zlog_debug("%u:%s Stale dplane result for re %p",
1908 dplane_ctx_get_vrf(ctx
), dest_str
, re
);
1914 (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
))) {
1915 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
) {
1916 zlog_debug("%u:%s Stale dplane result for old_re %p",
1917 dplane_ctx_get_vrf(ctx
), dest_str
, old_re
);
1923 * Here's sort of a tough one: the route update result is stale.
1924 * Is it better to use the context block info to generate
1925 * redist and owner notification, or is it better to wait
1926 * for the up-to-date result to arrive?
1929 /* TODO -- for now, only expose up-to-date results */
1934 case DPLANE_OP_NONE
:
1936 case DPLANE_OP_ROUTE_INSTALL
:
1937 case DPLANE_OP_ROUTE_UPDATE
:
1938 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1939 /* Update zebra nexthop FIB flag for each
1940 * nexthop that was installed.
1942 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1945 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1946 if (nexthop_same(ctx_nexthop
, nexthop
))
1950 if (nexthop
== NULL
)
1953 if (CHECK_FLAG(nexthop
->flags
,
1954 NEXTHOP_FLAG_RECURSIVE
))
1957 if (CHECK_FLAG(ctx_nexthop
->flags
,
1959 SET_FLAG(nexthop
->flags
,
1962 UNSET_FLAG(nexthop
->flags
,
1968 /* Set flag for nexthop tracking processing */
1969 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1974 * TODO -- still calling the redist api using the
1975 * route_entries, and there's a corner-case here:
1976 * if there's no client for the 'new' route, a redist
1977 * deleting the 'old' route will be sent. But if the
1978 * 'old' context info was stale, 'old_re' will be
1979 * NULL here and that delete will not be sent.
1981 redistribute_update(dest_pfx
, src_pfx
, re
, old_re
);
1983 /* Notify route owner */
1984 zsend_route_notify_owner(re
, dest_pfx
,
1985 ZAPI_ROUTE_INSTALLED
);
1988 zsend_route_notify_owner(re
, dest_pfx
,
1989 ZAPI_ROUTE_FAIL_INSTALL
);
1991 zlog_warn("%u:%s: Route install failed",
1992 dplane_ctx_get_vrf(ctx
),
1993 prefix2str(dest_pfx
,
1994 dest_str
, sizeof(dest_str
)));
1997 case DPLANE_OP_ROUTE_DELETE
:
1999 * In the delete case, the zebra core datastructs were
2000 * updated (or removed) at the time the delete was issued,
2001 * so we're just notifying the route owner.
2003 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2004 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2009 zsend_route_notify_owner_ctx(ctx
,
2010 ZAPI_ROUTE_REMOVE_FAIL
);
2012 zlog_warn("%u:%s: Route Deletion failure",
2013 dplane_ctx_get_vrf(ctx
),
2014 prefix2str(dest_pfx
,
2015 dest_str
, sizeof(dest_str
)));
2021 /* Return context to dataplane module */
2022 dplane_ctx_fini(&ctx
);
2025 /* Take a list of route_node structs and return 1, if there was a record
2026 * picked from it and processed by rib_process(). Don't process more,
2027 * than one RN record; operate only in the specified sub-queue.
2029 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2031 struct listnode
*lnode
= listhead(subq
);
2032 struct route_node
*rnode
;
2034 struct zebra_vrf
*zvrf
= NULL
;
2039 rnode
= listgetdata(lnode
);
2040 dest
= rib_dest_from_rnode(rnode
);
2042 zvrf
= rib_dest_vrf(dest
);
2046 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2047 char buf
[SRCDEST2STR_BUFFER
];
2048 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2049 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2050 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2054 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2055 RIB_ROUTE_QUEUED(qindex
));
2060 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2061 __func__
, rnode
, rnode
->lock
);
2062 zlog_backtrace(LOG_DEBUG
);
2065 route_unlock_node(rnode
);
2066 list_delete_node(subq
, lnode
);
2071 * Perform next-hop tracking processing after RIB updates.
2073 static void do_nht_processing(void)
2076 struct zebra_vrf
*zvrf
;
2078 /* Evaluate nexthops for those VRFs which underwent route processing.
2080 * should limit the evaluation to the necessary VRFs in most common
2083 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2085 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2088 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHT
)
2089 zlog_debug("NHT processing check for zvrf %s",
2092 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2093 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2094 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_IMPORT_CHECK_TYPE
,
2096 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2097 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_IMPORT_CHECK_TYPE
,
2101 /* Schedule LSPs for processing, if needed. */
2102 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2103 if (mpls_should_lsps_be_processed(zvrf
)) {
2104 if (IS_ZEBRA_DEBUG_MPLS
)
2106 "%u: Scheduling all LSPs upon RIB completion",
2108 zebra_mpls_lsp_schedule(zvrf
);
2109 mpls_unmark_lsps_for_processing(zvrf
);
2114 * All meta queues have been processed. Trigger next-hop evaluation.
2116 static void meta_queue_process_complete(struct work_queue
*dummy
)
2118 do_nht_processing();
2121 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2122 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2124 * is pointed to the meta queue structure.
2126 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2128 struct meta_queue
*mq
= data
;
2130 uint32_t queue_len
, queue_limit
;
2132 /* Ensure there's room for more dataplane updates */
2133 queue_limit
= dplane_get_in_queue_limit();
2134 queue_len
= dplane_get_in_queue_len();
2135 if (queue_len
> queue_limit
) {
2136 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2137 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2138 queue_len
, queue_limit
);
2140 /* Ensure that the meta-queue is actually enqueued */
2141 if (work_queue_empty(zebrad
.ribq
))
2142 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2144 return WQ_QUEUE_BLOCKED
;
2147 for (i
= 0; i
< MQ_SIZE
; i
++)
2148 if (process_subq(mq
->subq
[i
], i
)) {
2152 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2157 * Look into the RN and queue it into the highest priority queue
2158 * at this point in time for processing.
2160 * We will enqueue a route node only once per invocation.
2162 * There are two possibilities here that should be kept in mind.
2163 * If the original invocation has not been pulled off for processing
2164 * yet, A subsuquent invocation can have a route entry with a better
2165 * meta queue index value and we can have a situation where
2166 * we might have the same node enqueued 2 times. Not necessarily
2167 * an optimal situation but it should be ok.
2169 * The other possibility is that the original invocation has not
2170 * been pulled off for processing yet, A subsusquent invocation
2171 * doesn't have a route_entry with a better meta-queue and the
2172 * original metaqueue index value will win and we'll end up with
2173 * the route node enqueued once.
2175 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2177 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2178 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2179 struct zebra_vrf
*zvrf
;
2181 RNODE_FOREACH_RE (rn
, curr_re
) {
2182 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2184 if (curr_qindex
<= qindex
) {
2186 qindex
= curr_qindex
;
2193 /* Invariant: at this point we always have rn->info set. */
2194 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2195 RIB_ROUTE_QUEUED(qindex
))) {
2196 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2197 rnode_debug(rn
, re
->vrf_id
,
2198 "rn %p is already queued in sub-queue %u",
2199 (void *)rn
, qindex
);
2203 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2204 listnode_add(mq
->subq
[qindex
], rn
);
2205 route_lock_node(rn
);
2208 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2209 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2210 (void *)rn
, qindex
);
2212 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2214 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2217 /* Add route_node to work queue and schedule processing */
2218 void rib_queue_add(struct route_node
*rn
)
2222 /* Pointless to queue a route_node with no RIB entries to add or remove
2224 if (!rnode_to_ribs(rn
)) {
2225 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2226 __func__
, (void *)rn
, rn
->lock
);
2227 zlog_backtrace(LOG_DEBUG
);
2231 if (zebrad
.ribq
== NULL
) {
2232 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2233 "%s: work_queue does not exist!", __func__
);
2238 * The RIB queue should normally be either empty or holding the only
2239 * work_queue_item element. In the latter case this element would
2240 * hold a pointer to the meta queue structure, which must be used to
2241 * actually queue the route nodes to process. So create the MQ
2242 * holder, if necessary, then push the work into it in any case.
2243 * This semantics was introduced after 0.99.9 release.
2245 if (work_queue_empty(zebrad
.ribq
))
2246 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
2248 rib_meta_queue_add(zebrad
.mq
, rn
);
2253 /* Create new meta queue.
2254 A destructor function doesn't seem to be necessary here.
2256 static struct meta_queue
*meta_queue_new(void)
2258 struct meta_queue
*new;
2261 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2263 for (i
= 0; i
< MQ_SIZE
; i
++) {
2264 new->subq
[i
] = list_new();
2265 assert(new->subq
[i
]);
2271 void meta_queue_free(struct meta_queue
*mq
)
2275 for (i
= 0; i
< MQ_SIZE
; i
++)
2276 list_delete(&mq
->subq
[i
]);
2278 XFREE(MTYPE_WORK_QUEUE
, mq
);
2281 /* initialise zebra rib work queue */
2282 static void rib_queue_init(struct zebra_t
*zebra
)
2287 work_queue_new(zebra
->master
, "route_node processing"))) {
2288 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2289 "%s: could not initialise work queue!", __func__
);
2293 /* fill in the work queue spec */
2294 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2295 zebra
->ribq
->spec
.errorfunc
= NULL
;
2296 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2297 /* XXX: TODO: These should be runtime configurable via vty */
2298 zebra
->ribq
->spec
.max_retries
= 3;
2299 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2300 zebra
->ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2302 if (!(zebra
->mq
= meta_queue_new())) {
2303 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2304 "%s: could not initialise meta queue!", __func__
);
2310 /* RIB updates are processed via a queue of pointers to route_nodes.
2312 * The queue length is bounded by the maximal size of the routing table,
2313 * as a route_node will not be requeued, if already queued.
2315 * REs are submitted via rib_addnode or rib_delnode which set minimal
2316 * state, or static_install_route (when an existing RE is updated)
2317 * and then submit route_node to queue for best-path selection later.
2318 * Order of add/delete state changes are preserved for any given RE.
2320 * Deleted REs are reaped during best-path selection.
2323 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2324 * |-------->| | best RE, if required
2326 * static_install->|->rib_addqueue...... -> rib_process
2328 * |-------->| |-> rib_unlink
2329 * |-> set ROUTE_ENTRY_REMOVE |
2330 * rib_delnode (RE freed)
2332 * The 'info' pointer of a route_node points to a rib_dest_t
2333 * ('dest'). Queueing state for a route_node is kept on the dest. The
2334 * dest is created on-demand by rib_link() and is kept around at least
2335 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2337 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2339 * - route_nodes: refcounted by:
2340 * - dest attached to route_node:
2341 * - managed by: rib_link/rib_gc_dest
2342 * - route_node processing queue
2343 * - managed by: rib_addqueue, rib_process.
2347 /* Add RE to head of the route node. */
2348 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2350 struct route_entry
*head
;
2353 const char *rmap_name
;
2357 dest
= rib_dest_from_rnode(rn
);
2359 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2360 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2362 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2363 route_lock_node(rn
); /* rn route table reference */
2368 head
= dest
->routes
;
2375 afi
= (rn
->p
.family
== AF_INET
)
2377 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2378 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2379 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2380 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2385 static void rib_addnode(struct route_node
*rn
,
2386 struct route_entry
*re
, int process
)
2388 /* RE node has been un-removed before route-node is processed.
2389 * route_node must hence already be on the queue for processing..
2391 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2392 if (IS_ZEBRA_DEBUG_RIB
)
2393 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2394 (void *)rn
, (void *)re
);
2396 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2399 rib_link(rn
, re
, process
);
2405 * Detach a rib structure from a route_node.
2407 * Note that a call to rib_unlink() should be followed by a call to
2408 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2409 * longer required to be deleted.
2411 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2417 if (IS_ZEBRA_DEBUG_RIB
)
2418 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2421 dest
= rib_dest_from_rnode(rn
);
2424 re
->next
->prev
= re
->prev
;
2427 re
->prev
->next
= re
->next
;
2429 dest
->routes
= re
->next
;
2432 if (dest
->selected_fib
== re
)
2433 dest
->selected_fib
= NULL
;
2435 nexthops_free(re
->ng
.nexthop
);
2436 XFREE(MTYPE_RE
, re
);
2439 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2443 if (IS_ZEBRA_DEBUG_RIB
)
2444 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2445 (void *)rn
, (void *)re
);
2446 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2448 afi
= (rn
->p
.family
== AF_INET
)
2450 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2451 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2452 zebra_del_import_table_entry(rn
, re
);
2453 /* Just clean up if non main table */
2454 if (IS_ZEBRA_DEBUG_RIB
) {
2455 char buf
[SRCDEST2STR_BUFFER
];
2456 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2458 "%u:%s: Freeing route rn %p, re %p (type %d)",
2459 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2468 /* This function dumps the contents of a given RE entry into
2469 * standard debug log. Calling function name and IP prefix in
2470 * question are passed as 1st and 2nd arguments.
2473 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2474 union prefixconstptr src_pp
,
2475 const struct route_entry
*re
)
2477 const struct prefix
*src_p
= src_pp
.p
;
2478 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2479 char straddr
[PREFIX_STRLEN
];
2480 char srcaddr
[PREFIX_STRLEN
];
2481 struct nexthop
*nexthop
;
2483 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2484 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2485 is_srcdst
? " from " : "",
2486 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2489 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2490 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2493 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2494 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2495 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2496 re
->nexthop_num
, re
->nexthop_active_num
);
2498 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2499 struct interface
*ifp
;
2500 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2502 switch (nexthop
->type
) {
2503 case NEXTHOP_TYPE_BLACKHOLE
:
2504 sprintf(straddr
, "Blackhole");
2506 case NEXTHOP_TYPE_IFINDEX
:
2507 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2509 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2511 case NEXTHOP_TYPE_IPV4
:
2513 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2514 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2517 case NEXTHOP_TYPE_IPV6
:
2518 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2519 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2523 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2524 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2525 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2527 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2530 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2533 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2537 zlog_debug("%s: dump complete", func
);
2540 /* This is an exported helper to rtm_read() to dump the strange
2541 * RE entry found by rib_lookup_ipv4_route()
2544 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2546 struct route_table
*table
;
2547 struct route_node
*rn
;
2548 struct route_entry
*re
;
2549 char prefix_buf
[INET_ADDRSTRLEN
];
2552 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2554 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2555 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2560 /* Scan the RIB table for exactly matching RE entry. */
2561 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2563 /* No route for this prefix. */
2565 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2566 prefix2str((struct prefix
*)p
, prefix_buf
,
2567 sizeof(prefix_buf
)));
2572 route_unlock_node(rn
);
2575 RNODE_FOREACH_RE (rn
, re
) {
2576 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2578 (void *)rn
, (void *)re
,
2579 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2582 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2585 route_entry_dump(p
, NULL
, re
);
2589 /* Check if requested address assignment will fail due to another
2590 * route being installed by zebra in FIB already. Take necessary
2591 * actions, if needed: remove such a route from FIB and deSELECT
2592 * corresponding RE entry. Then put affected RN into RIBQ head.
2594 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2596 struct route_table
*table
;
2597 struct route_node
*rn
;
2598 unsigned changed
= 0;
2601 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2602 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2603 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2608 /* No matches would be the simplest case. */
2609 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2613 route_unlock_node(rn
);
2615 dest
= rib_dest_from_rnode(rn
);
2616 /* Check all RE entries. In case any changes have to be done, requeue
2617 * the RN into RIBQ head. If the routing message about the new connected
2618 * route (generated by the IP address we are going to assign very soon)
2619 * comes before the RIBQ is processed, the new RE entry will join
2620 * RIBQ record already on head. This is necessary for proper
2622 * of the rest of the RE.
2624 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2626 if (IS_ZEBRA_DEBUG_RIB
) {
2627 char buf
[PREFIX_STRLEN
];
2629 zlog_debug("%u:%s: freeing way for connected prefix",
2630 dest
->selected_fib
->vrf_id
,
2631 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2632 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2634 rib_uninstall(rn
, dest
->selected_fib
);
2640 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2641 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2643 struct route_table
*table
;
2644 struct route_node
*rn
;
2645 struct route_entry
*same
= NULL
;
2646 struct nexthop
*nexthop
;
2652 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2655 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2657 XFREE(MTYPE_RE
, re
);
2661 /* Make it sure prefixlen is applied to the prefix. */
2664 apply_mask_ipv6(src_p
);
2666 /* Set default distance by route type. */
2667 if (re
->distance
== 0) {
2668 re
->distance
= route_distance(re
->type
);
2670 /* iBGP distance is 200. */
2671 if (re
->type
== ZEBRA_ROUTE_BGP
2672 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2676 /* Lookup route node.*/
2677 rn
= srcdest_rnode_get(table
, p
, src_p
);
2680 * If same type of route are installed, treat it as a implicit
2682 * If the user has specified the No route replace semantics
2683 * for the install don't do a route replace.
2685 RNODE_FOREACH_RE (rn
, same
) {
2686 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2689 if (same
->type
!= re
->type
)
2691 if (same
->instance
!= re
->instance
)
2693 if (same
->type
== ZEBRA_ROUTE_KERNEL
2694 && same
->metric
!= re
->metric
)
2697 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2698 same
->distance
!= re
->distance
)
2702 * We should allow duplicate connected routes
2703 * because of IPv6 link-local routes and unnumbered
2704 * interfaces on Linux.
2706 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2710 /* If this route is kernel route, set FIB flag to the route. */
2711 if (RIB_SYSTEM_ROUTE(re
))
2712 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2713 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2715 /* Link new re to node.*/
2716 if (IS_ZEBRA_DEBUG_RIB
) {
2719 "Inserting route rn %p, re %p (type %d) existing %p",
2720 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2722 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2723 route_entry_dump(p
, src_p
, re
);
2725 rib_addnode(rn
, re
, 1);
2728 /* Free implicit route.*/
2730 rib_delnode(rn
, same
);
2734 route_unlock_node(rn
);
2738 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2739 unsigned short instance
, int flags
, struct prefix
*p
,
2740 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2741 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2744 struct route_table
*table
;
2745 struct route_node
*rn
;
2746 struct route_entry
*re
;
2747 struct route_entry
*fib
= NULL
;
2748 struct route_entry
*same
= NULL
;
2749 struct nexthop
*rtnh
;
2750 char buf2
[INET6_ADDRSTRLEN
];
2753 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2756 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2763 apply_mask_ipv6(src_p
);
2765 /* Lookup route node. */
2766 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2768 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2770 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2771 if (src_p
&& src_p
->prefixlen
)
2772 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2776 if (IS_ZEBRA_DEBUG_RIB
)
2777 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2779 (src_buf
[0] != '\0') ? " from " : "",
2784 dest
= rib_dest_from_rnode(rn
);
2785 fib
= dest
->selected_fib
;
2787 /* Lookup same type route. */
2788 RNODE_FOREACH_RE (rn
, re
) {
2789 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2792 if (re
->type
!= type
)
2794 if (re
->instance
!= instance
)
2796 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2797 distance
!= re
->distance
)
2800 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2802 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2803 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2804 if (rtnh
->ifindex
!= nh
->ifindex
)
2809 /* Make sure that the route found has the same gateway. */
2815 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2816 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2824 /* If same type of route can't be found and this message is from
2828 * In the past(HA!) we could get here because
2829 * we were receiving a route delete from the
2830 * kernel and we're not marking the proto
2831 * as coming from it's appropriate originator.
2832 * Now that we are properly noticing the fact
2833 * that the kernel has deleted our route we
2834 * are not going to get called in this path
2835 * I am going to leave this here because
2836 * this might still work this way on non-linux
2837 * platforms as well as some weird state I have
2838 * not properly thought of yet.
2839 * If we can show that this code path is
2840 * dead then we can remove it.
2842 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2843 if (IS_ZEBRA_DEBUG_RIB
) {
2846 "rn %p, re %p (type %d) was deleted from kernel, adding",
2847 rn
, fib
, fib
->type
);
2851 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2853 UNSET_FLAG(rtnh
->flags
,
2857 * This is a non FRR route
2858 * as such we should mark
2861 dest
->selected_fib
= NULL
;
2863 /* This means someone else, other than Zebra,
2865 * a Zebra router from the kernel. We will add
2867 rib_install_kernel(rn
, fib
, NULL
);
2870 if (IS_ZEBRA_DEBUG_RIB
) {
2874 "via %s ifindex %d type %d "
2875 "doesn't exist in rib",
2876 inet_ntop(afi2family(afi
),
2883 "type %d doesn't exist in rib",
2886 route_unlock_node(rn
);
2892 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2894 rib_install_kernel(rn
, same
, NULL
);
2895 route_unlock_node(rn
);
2900 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2901 struct nexthop
*tmp_nh
;
2903 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2904 struct ipaddr vtep_ip
;
2906 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2907 if (afi
== AFI_IP
) {
2908 vtep_ip
.ipa_type
= IPADDR_V4
;
2909 memcpy(&(vtep_ip
.ipaddr_v4
),
2910 &(tmp_nh
->gate
.ipv4
),
2911 sizeof(struct in_addr
));
2913 vtep_ip
.ipa_type
= IPADDR_V6
;
2914 memcpy(&(vtep_ip
.ipaddr_v6
),
2915 &(tmp_nh
->gate
.ipv6
),
2916 sizeof(struct in6_addr
));
2918 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2922 rib_delnode(rn
, same
);
2925 route_unlock_node(rn
);
2930 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2931 unsigned short instance
, int flags
, struct prefix
*p
,
2932 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2933 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2936 struct route_entry
*re
;
2937 struct nexthop
*nexthop
;
2939 /* Allocate new route_entry structure. */
2940 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2942 re
->instance
= instance
;
2943 re
->distance
= distance
;
2945 re
->metric
= metric
;
2947 re
->table
= table_id
;
2948 re
->vrf_id
= vrf_id
;
2949 re
->nexthop_num
= 0;
2950 re
->uptime
= time(NULL
);
2954 nexthop
= nexthop_new();
2956 route_entry_nexthop_add(re
, nexthop
);
2958 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2961 /* Schedule routes of a particular table (address-family) based on event. */
2962 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2964 struct route_node
*rn
;
2965 struct route_entry
*re
, *next
;
2967 /* Walk all routes and queue for processing, if appropriate for
2968 * the trigger event.
2970 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2972 * If we are looking at a route node and the node
2973 * has already been queued we don't
2974 * need to queue it up again
2976 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2977 RIB_ROUTE_ANY_QUEUED
))
2980 case RIB_UPDATE_IF_CHANGE
:
2981 /* Examine all routes that won't get processed by the
2983 * triggered by nexthop evaluation (NHT). This would be
2985 * kernel and certain static routes. Note that NHT will
2987 * triggered upon an interface event as connected routes
2989 * get queued for processing.
2991 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2994 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
2995 && re
->type
!= ZEBRA_ROUTE_KERNEL
2996 && re
->type
!= ZEBRA_ROUTE_CONNECT
2997 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3000 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3005 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3006 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3007 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3010 /* If we only have nexthops to a
3019 case RIB_UPDATE_RMAP_CHANGE
:
3020 case RIB_UPDATE_OTHER
:
3021 /* Right now, examine all routes. Can restrict to a
3023 * some cases (TODO).
3025 if (rnode_to_ribs(rn
))
3035 /* RIB update function. */
3036 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3038 struct route_table
*table
;
3040 /* Process routes of interested address-families. */
3041 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3043 if (IS_ZEBRA_DEBUG_EVENT
)
3044 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3045 rib_update_table(table
, event
);
3048 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3050 if (IS_ZEBRA_DEBUG_EVENT
)
3051 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3052 rib_update_table(table
, event
);
3056 /* Delete self installed routes after zebra is relaunched. */
3057 void rib_sweep_table(struct route_table
*table
)
3059 struct route_node
*rn
;
3060 struct route_entry
*re
;
3061 struct route_entry
*next
;
3062 struct nexthop
*nexthop
;
3067 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3068 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3069 if (IS_ZEBRA_DEBUG_RIB
)
3070 route_entry_dump(&rn
->p
, NULL
, re
);
3072 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3075 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3079 * So we are starting up and have received
3080 * routes from the kernel that we have installed
3081 * from a previous run of zebra but not cleaned
3082 * up ( say a kill -9 )
3083 * But since we haven't actually installed
3084 * them yet( we received them from the kernel )
3085 * we don't think they are active.
3086 * So let's pretend they are active to actually
3088 * In all honesty I'm not sure if we should
3089 * mark them as active when we receive them
3090 * This is startup only so probably ok.
3092 * If we ever decide to move rib_sweep_table
3093 * to a different spot (ie startup )
3094 * this decision needs to be revisited
3096 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3097 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3099 rib_uninstall_kernel(rn
, re
);
3100 rib_delnode(rn
, re
);
3105 /* Sweep all RIB tables. */
3106 void rib_sweep_route(void)
3109 struct zebra_vrf
*zvrf
;
3111 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3112 if ((zvrf
= vrf
->info
) == NULL
)
3115 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3116 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3119 zebra_router_sweep_route();
3122 /* Remove specific by protocol routes from 'table'. */
3123 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3124 struct route_table
*table
)
3126 struct route_node
*rn
;
3127 struct route_entry
*re
;
3128 struct route_entry
*next
;
3129 unsigned long n
= 0;
3132 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3133 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3134 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3136 if (re
->type
== proto
3137 && re
->instance
== instance
) {
3138 rib_delnode(rn
, re
);
3145 /* Remove specific by protocol routes. */
3146 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3149 struct zebra_vrf
*zvrf
;
3150 unsigned long cnt
= 0;
3152 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3153 if ((zvrf
= vrf
->info
) != NULL
)
3154 cnt
+= rib_score_proto_table(
3156 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3157 + rib_score_proto_table(
3159 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3161 cnt
+= zebra_router_score_proto(proto
, instance
);
3166 /* Close RIB and clean up kernel routes. */
3167 void rib_close_table(struct route_table
*table
)
3169 struct route_node
*rn
;
3170 rib_table_info_t
*info
;
3176 info
= route_table_get_info(table
);
3178 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3179 dest
= rib_dest_from_rnode(rn
);
3181 if (dest
&& dest
->selected_fib
) {
3182 if (info
->safi
== SAFI_UNICAST
)
3183 hook_call(rib_update
, rn
, NULL
);
3185 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
3186 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3187 dest
->selected_fib
= NULL
;
3196 static int rib_process_dplane_results(struct thread
*thread
)
3198 struct zebra_dplane_ctx
*ctx
;
3199 struct dplane_ctx_q ctxlist
;
3201 /* Dequeue a list of completed updates with one lock/unlock cycle */
3204 TAILQ_INIT(&ctxlist
);
3206 /* Take lock controlling queue of results */
3207 pthread_mutex_lock(&dplane_mutex
);
3209 /* Dequeue context block */
3210 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3212 pthread_mutex_unlock(&dplane_mutex
);
3214 /* Dequeue context block */
3215 ctx
= dplane_ctx_dequeue(&ctxlist
);
3217 /* If we've emptied the results queue, we're done */
3222 rib_process_after(ctx
);
3224 ctx
= dplane_ctx_dequeue(&ctxlist
);
3229 /* Check for nexthop tracking processing after finishing with results */
3230 do_nht_processing();
3236 * Results are returned from the dataplane subsystem, in the context of
3237 * the dataplane pthread. We enqueue the results here for processing by
3238 * the main thread later.
3240 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3242 /* Take lock controlling queue of results */
3243 pthread_mutex_lock(&dplane_mutex
);
3245 /* Enqueue context blocks */
3246 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3248 pthread_mutex_unlock(&dplane_mutex
);
3250 /* Ensure event is signalled to zebra main pthread */
3251 thread_add_event(zebrad
.master
, rib_process_dplane_results
, NULL
, 0,
3257 /* Routing information base initialize. */
3260 rib_queue_init(&zebrad
);
3262 /* Init dataplane, and register for results */
3263 pthread_mutex_init(&dplane_mutex
, NULL
);
3264 TAILQ_INIT(&rib_dplane_q
);
3265 zebra_dplane_init(rib_dplane_results
);
3271 * Get the first vrf id that is greater than the given vrf id if any.
3273 * Returns TRUE if a vrf id was found, FALSE otherwise.
3275 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3279 vrf
= vrf_lookup_by_id(vrf_id
);
3281 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3283 *next_id_p
= vrf
->vrf_id
;
3292 * rib_tables_iter_next
3294 * Returns the next table in the iteration.
3296 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3298 struct route_table
*table
;
3301 * Array that helps us go over all AFI/SAFI combinations via one
3308 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3309 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3310 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3315 switch (iter
->state
) {
3317 case RIB_TABLES_ITER_S_INIT
:
3318 iter
->vrf_id
= VRF_DEFAULT
;
3319 iter
->afi_safi_ix
= -1;
3323 case RIB_TABLES_ITER_S_ITERATING
:
3324 iter
->afi_safi_ix
++;
3327 while (iter
->afi_safi_ix
3328 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3329 table
= zebra_vrf_table(
3330 afi_safis
[iter
->afi_safi_ix
].afi
,
3331 afi_safis
[iter
->afi_safi_ix
].safi
,
3336 iter
->afi_safi_ix
++;
3340 * Found another table in this vrf.
3346 * Done with all tables in the current vrf, go to the
3350 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3353 iter
->afi_safi_ix
= 0;
3358 case RIB_TABLES_ITER_S_DONE
:
3363 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3365 iter
->state
= RIB_TABLES_ITER_S_DONE
;