1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h"
35 #include "srcdest_table.h"
39 #include "workqueue.h"
41 #include "zebra/zebra_router.h"
42 #include "zebra/connected.h"
43 #include "zebra/debug.h"
44 #include "zebra/interface.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/rib.h"
48 #include "zebra/zapi_msg.h"
49 #include "zebra/zebra_errors.h"
50 #include "zebra/zebra_memory.h"
51 #include "zebra/zebra_ns.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/zebra_routemap.h"
54 #include "zebra/zebra_vrf.h"
55 #include "zebra/zebra_vxlan.h"
56 #include "zebra/zapi_msg.h"
57 #include "zebra/zebra_dplane.h"
60 * Event, list, and mutex for delivery of dataplane results
62 static pthread_mutex_t dplane_mutex
;
63 static struct thread
*t_dplane
;
64 static struct dplane_ctx_q rib_dplane_q
;
66 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
69 /* Should we allow non Quagga processes to delete our routes */
70 extern int allow_delete
;
72 /* Each route type's string and default distance value. */
77 } route_info
[ZEBRA_ROUTE_MAX
] = {
78 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
79 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
80 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
81 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
82 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
83 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
84 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
85 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
86 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
87 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
88 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
89 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
90 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
91 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
92 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
93 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
94 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
95 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
97 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
99 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
100 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
101 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
103 /* no entry/default: 150 */
106 /* RPF lookup behaviour */
107 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
110 static void __attribute__((format(printf
, 5, 6)))
111 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
112 int priority
, const char *msgfmt
, ...)
114 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
118 va_start(ap
, msgfmt
);
119 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
123 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
124 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
126 if (info
->safi
== SAFI_MULTICAST
)
127 strcat(buf
, " (MRIB)");
129 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
132 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
135 #define rnode_debug(node, vrf_id, ...) \
136 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
137 #define rnode_info(node, ...) \
138 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
140 uint8_t route_distance(int type
)
144 if ((unsigned)type
>= array_size(route_info
))
147 distance
= route_info
[type
].distance
;
152 int is_zebra_valid_kernel_table(uint32_t table_id
)
155 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
156 || (table_id
== RT_TABLE_COMPAT
))
163 int is_zebra_main_routing_table(uint32_t table_id
)
165 if ((table_id
== RT_TABLE_MAIN
)
166 || (table_id
== zrouter
.rtm_table_default
))
171 int zebra_check_addr(const struct prefix
*p
)
173 if (p
->family
== AF_INET
) {
176 addr
= p
->u
.prefix4
.s_addr
;
179 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
180 || IPV4_LINKLOCAL(addr
))
183 if (p
->family
== AF_INET6
) {
184 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
186 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
192 /* Add nexthop to the end of a rib node's nexthop list */
193 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
195 nexthop_add(&re
->ng
.nexthop
, nexthop
);
201 * copy_nexthop - copy a nexthop to the rib structure.
203 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
205 assert(!re
->ng
.nexthop
);
206 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
207 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
211 /* Delete specified nexthop from the list. */
212 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
215 nexthop
->next
->prev
= nexthop
->prev
;
217 nexthop
->prev
->next
= nexthop
->next
;
219 re
->ng
.nexthop
= nexthop
->next
;
224 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
228 struct nexthop
*nexthop
;
230 nexthop
= nexthop_new();
231 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
232 nexthop
->ifindex
= ifindex
;
233 nexthop
->vrf_id
= nh_vrf_id
;
235 route_entry_nexthop_add(re
, nexthop
);
240 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
241 struct in_addr
*ipv4
,
245 struct nexthop
*nexthop
;
247 nexthop
= nexthop_new();
248 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
249 nexthop
->vrf_id
= nh_vrf_id
;
250 nexthop
->gate
.ipv4
= *ipv4
;
252 nexthop
->src
.ipv4
= *src
;
254 route_entry_nexthop_add(re
, nexthop
);
259 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
260 struct in_addr
*ipv4
,
265 struct nexthop
*nexthop
;
266 struct interface
*ifp
;
268 nexthop
= nexthop_new();
269 nexthop
->vrf_id
= nh_vrf_id
;
270 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
271 nexthop
->gate
.ipv4
= *ipv4
;
273 nexthop
->src
.ipv4
= *src
;
274 nexthop
->ifindex
= ifindex
;
275 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
276 /*Pending: need to think if null ifp here is ok during bootup?
277 There was a crash because ifp here was coming to be NULL */
279 if (connected_is_unnumbered(ifp
))
280 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
282 route_entry_nexthop_add(re
, nexthop
);
287 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
288 struct in6_addr
*ipv6
,
291 struct nexthop
*nexthop
;
293 nexthop
= nexthop_new();
294 nexthop
->vrf_id
= nh_vrf_id
;
295 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
296 nexthop
->gate
.ipv6
= *ipv6
;
298 route_entry_nexthop_add(re
, nexthop
);
303 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
304 struct in6_addr
*ipv6
,
308 struct nexthop
*nexthop
;
310 nexthop
= nexthop_new();
311 nexthop
->vrf_id
= nh_vrf_id
;
312 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
313 nexthop
->gate
.ipv6
= *ipv6
;
314 nexthop
->ifindex
= ifindex
;
316 route_entry_nexthop_add(re
, nexthop
);
321 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
322 enum blackhole_type bh_type
)
324 struct nexthop
*nexthop
;
326 nexthop
= nexthop_new();
327 nexthop
->vrf_id
= VRF_DEFAULT
;
328 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
329 nexthop
->bh_type
= bh_type
;
331 route_entry_nexthop_add(re
, nexthop
);
336 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
337 struct nexthop
*nexthop
)
339 struct nexthop
*resolved_hop
;
341 resolved_hop
= nexthop_new();
342 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
344 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
345 switch (newhop
->type
) {
346 case NEXTHOP_TYPE_IPV4
:
347 case NEXTHOP_TYPE_IPV4_IFINDEX
:
348 /* If the resolving route specifies a gateway, use it */
349 resolved_hop
->type
= newhop
->type
;
350 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
352 if (newhop
->ifindex
) {
353 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
354 resolved_hop
->ifindex
= newhop
->ifindex
;
357 case NEXTHOP_TYPE_IPV6
:
358 case NEXTHOP_TYPE_IPV6_IFINDEX
:
359 resolved_hop
->type
= newhop
->type
;
360 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
362 if (newhop
->ifindex
) {
363 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
364 resolved_hop
->ifindex
= newhop
->ifindex
;
367 case NEXTHOP_TYPE_IFINDEX
:
368 /* If the resolving route is an interface route,
369 * it means the gateway we are looking up is connected
370 * to that interface. (The actual network is _not_ onlink).
371 * Therefore, the resolved route should have the original
372 * gateway as nexthop as it is directly connected.
374 * On Linux, we have to set the onlink netlink flag because
375 * otherwise, the kernel won't accept the route.
377 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
379 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
380 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
381 } else if (afi
== AFI_IP6
) {
382 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
383 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
385 resolved_hop
->ifindex
= newhop
->ifindex
;
387 case NEXTHOP_TYPE_BLACKHOLE
:
388 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
389 resolved_hop
->bh_type
= nexthop
->bh_type
;
393 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
394 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
396 /* Copy labels of the resolved route */
397 if (newhop
->nh_label
)
398 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
399 newhop
->nh_label
->num_labels
,
400 &newhop
->nh_label
->label
[0]);
402 resolved_hop
->rparent
= nexthop
;
403 nexthop_add(&nexthop
->resolved
, resolved_hop
);
406 /* If force flag is not set, do not modify falgs at all for uninstall
407 the route from FIB. */
408 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
409 struct nexthop
*nexthop
, bool set
,
410 struct route_node
*top
)
413 struct route_table
*table
;
414 struct route_node
*rn
;
415 struct route_entry
*match
= NULL
;
417 struct nexthop
*newhop
;
418 struct interface
*ifp
;
421 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
422 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
423 nexthop
->ifindex
= 0;
426 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
427 nexthops_free(nexthop
->resolved
);
428 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
)
440 /* Skip nexthops that have been filtered out due to route-map */
441 /* The nexthops are specific to this route and so the same */
442 /* nexthop for a different route may not have this flag set */
443 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
)) {
444 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
445 zlog_debug("\t%s: Nexthop Filtered",
446 __PRETTY_FUNCTION__
);
451 * Check to see if we should trust the passed in information
452 * for UNNUMBERED interfaces as that we won't find the GW
453 * address in the routing table.
454 * This check should suffice to handle IPv4 or IPv6 routes
455 * sourced from EVPN routes which are installed with the
456 * next hop as the remote VTEP IP.
458 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
459 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
461 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
463 "\t%s: Onlink and interface: %u[%u] does not exist",
464 __PRETTY_FUNCTION__
, nexthop
->ifindex
,
468 if (connected_is_unnumbered(ifp
)) {
469 if (if_is_operative(ifp
))
472 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
474 "\t%s: Onlink and interface %s is not operative",
475 __PRETTY_FUNCTION__
, ifp
->name
);
479 if (!if_is_operative(ifp
)) {
480 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
482 "\t%s: Interface %s is not unnumbered",
484 ifp
? ifp
->name
: "Unknown");
489 /* Make lookup prefix. */
490 memset(&p
, 0, sizeof(struct prefix
));
494 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
495 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
499 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
500 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
503 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
507 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
509 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
510 zlog_debug("\t%s: Table not found",
511 __PRETTY_FUNCTION__
);
515 rn
= route_node_match(table
, (struct prefix
*)&p
);
517 route_unlock_node(rn
);
519 /* Lookup should halt if we've matched against ourselves ('top',
520 * if specified) - i.e., we cannot have a nexthop NH1 is
521 * resolved by a route NH1. The exception is if the route is a
524 if (top
&& rn
== top
)
525 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
526 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128))) {
527 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
529 "\t%s: Matched against ourself and prefix length is not max bit length",
530 __PRETTY_FUNCTION__
);
534 /* Pick up selected route. */
535 /* However, do not resolve over default route unless explicitly
537 if (is_default_prefix(&rn
->p
)
538 && !rnh_resolve_via_default(p
.family
)) {
539 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
541 "\t:%s: Resolved against default route",
542 __PRETTY_FUNCTION__
);
546 dest
= rib_dest_from_rnode(rn
);
547 if (dest
&& dest
->selected_fib
548 && !CHECK_FLAG(dest
->selected_fib
->status
,
550 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
551 match
= dest
->selected_fib
;
553 /* If there is no selected route or matched route is EGP, go up
558 } while (rn
&& rn
->info
== NULL
);
565 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
566 /* Directly point connected route. */
567 newhop
= match
->ng
.nexthop
;
569 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
570 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
571 nexthop
->ifindex
= newhop
->ifindex
;
574 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
576 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
577 if (!CHECK_FLAG(match
->status
,
578 ROUTE_ENTRY_INSTALLED
))
580 if (CHECK_FLAG(newhop
->flags
,
581 NEXTHOP_FLAG_RECURSIVE
))
585 SET_FLAG(nexthop
->flags
,
586 NEXTHOP_FLAG_RECURSIVE
);
588 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
589 nexthop_set_resolved(afi
, newhop
,
595 re
->nexthop_mtu
= match
->mtu
;
596 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
597 zlog_debug("\t%s: Recursion failed to find",
598 __PRETTY_FUNCTION__
);
600 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
602 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
603 if (!CHECK_FLAG(match
->status
,
604 ROUTE_ENTRY_INSTALLED
))
606 if (CHECK_FLAG(newhop
->flags
,
607 NEXTHOP_FLAG_RECURSIVE
))
611 SET_FLAG(nexthop
->flags
,
612 NEXTHOP_FLAG_RECURSIVE
);
613 nexthop_set_resolved(afi
, newhop
,
619 re
->nexthop_mtu
= match
->mtu
;
621 if (!resolved
&& IS_ZEBRA_DEBUG_RIB_DETAILED
)
623 "\t%s: Static route unable to resolve",
624 __PRETTY_FUNCTION__
);
627 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
628 zlog_debug("\t%s: Route Type %s has not turned on recursion",
630 zebra_route_string(re
->type
));
631 if (re
->type
== ZEBRA_ROUTE_BGP
&&
632 !CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
633 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
638 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
639 zlog_debug("\t%s: Nexthop did not lookup in table",
640 __PRETTY_FUNCTION__
);
644 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
645 union g_addr
*addr
, struct route_node
**rn_out
)
648 struct route_table
*table
;
649 struct route_node
*rn
;
650 struct route_entry
*match
= NULL
;
653 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
657 memset(&p
, 0, sizeof(struct prefix
));
660 p
.u
.prefix4
= addr
->ipv4
;
661 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
663 p
.u
.prefix6
= addr
->ipv6
;
664 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
667 rn
= route_node_match(table
, (struct prefix
*)&p
);
672 route_unlock_node(rn
);
674 dest
= rib_dest_from_rnode(rn
);
675 if (dest
&& dest
->selected_fib
676 && !CHECK_FLAG(dest
->selected_fib
->status
,
677 ROUTE_ENTRY_REMOVED
))
678 match
= dest
->selected_fib
;
680 /* If there is no selected route or matched route is EGP, go up
685 } while (rn
&& rn
->info
== NULL
);
689 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
690 if (!CHECK_FLAG(match
->status
,
691 ROUTE_ENTRY_INSTALLED
))
703 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
705 struct route_node
**rn_out
)
707 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
708 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
709 union g_addr gaddr
= {.ipv4
= addr
};
711 switch (ipv4_multicast_mode
) {
712 case MCAST_MRIB_ONLY
:
713 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
715 case MCAST_URIB_ONLY
:
716 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
717 case MCAST_NO_CONFIG
:
718 case MCAST_MIX_MRIB_FIRST
:
719 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
722 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
725 case MCAST_MIX_DISTANCE
:
726 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
727 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
729 re
= ure
->distance
< mre
->distance
? ure
: mre
;
735 case MCAST_MIX_PFXLEN
:
736 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
737 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
739 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
748 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
750 if (IS_ZEBRA_DEBUG_RIB
) {
752 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
754 zlog_debug("%s: %s: vrf: %u found %s, using %s",
755 __func__
, buf
, vrf_id
,
756 mre
? (ure
? "MRIB+URIB" : "MRIB")
757 : ure
? "URIB" : "nothing",
758 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
763 void multicast_mode_ipv4_set(enum multicast_mode mode
)
765 if (IS_ZEBRA_DEBUG_RIB
)
766 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
768 ipv4_multicast_mode
= mode
;
771 enum multicast_mode
multicast_mode_ipv4_get(void)
773 return ipv4_multicast_mode
;
776 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
778 struct route_table
*table
;
779 struct route_node
*rn
;
780 struct route_entry
*match
= NULL
;
784 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
788 rn
= route_node_lookup(table
, (struct prefix
*)p
);
790 /* No route for this prefix. */
795 route_unlock_node(rn
);
796 dest
= rib_dest_from_rnode(rn
);
798 if (dest
&& dest
->selected_fib
799 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
800 match
= dest
->selected_fib
;
805 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
808 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
814 #define RIB_SYSTEM_ROUTE(R) \
815 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
817 #define RIB_KERNEL_ROUTE(R) \
818 ((R)->type == ZEBRA_ROUTE_KERNEL)
820 /* This function verifies reachability of one given nexthop, which can be
821 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
822 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
823 * nexthop->ifindex will be updated appropriately as well.
824 * An existing route map can turn (otherwise active) nexthop into inactive, but
827 * The return value is the final value of 'ACTIVE' flag.
830 static unsigned nexthop_active_check(struct route_node
*rn
,
831 struct route_entry
*re
,
832 struct nexthop
*nexthop
, bool set
)
834 struct interface
*ifp
;
835 route_map_result_t ret
= RMAP_MATCH
;
837 char buf
[SRCDEST2STR_BUFFER
];
838 const struct prefix
*p
, *src_p
;
839 struct zebra_vrf
*zvrf
;
841 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
843 if (rn
->p
.family
== AF_INET
)
845 else if (rn
->p
.family
== AF_INET6
)
849 switch (nexthop
->type
) {
850 case NEXTHOP_TYPE_IFINDEX
:
851 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
852 if (ifp
&& if_is_operative(ifp
))
853 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
855 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
857 case NEXTHOP_TYPE_IPV4
:
858 case NEXTHOP_TYPE_IPV4_IFINDEX
:
860 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
861 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
863 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
865 case NEXTHOP_TYPE_IPV6
:
867 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
868 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
872 case NEXTHOP_TYPE_IPV6_IFINDEX
:
873 /* RFC 5549, v4 prefix with v6 NH */
874 if (rn
->p
.family
!= AF_INET
)
876 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
877 ifp
= if_lookup_by_index(nexthop
->ifindex
,
879 if (ifp
&& if_is_operative(ifp
))
880 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
882 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
884 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
885 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
887 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
890 case NEXTHOP_TYPE_BLACKHOLE
:
891 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
896 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)) {
897 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
898 zlog_debug("\t%s: Unable to find a active nexthop",
899 __PRETTY_FUNCTION__
);
903 /* XXX: What exactly do those checks do? Do we support
904 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
906 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
907 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
908 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
910 /* The original code didn't determine the family correctly
911 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
912 * from the rib_table_info in those cases.
913 * Possibly it may be better to use only the rib_table_info
917 rib_table_info_t
*info
;
919 info
= srcdest_rnode_table_info(rn
);
923 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
925 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
927 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
928 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__
);
929 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
932 /* It'll get set if required inside */
933 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
,
934 nexthop
, zvrf
, re
->tag
);
935 if (ret
== RMAP_DENYMATCH
) {
936 if (IS_ZEBRA_DEBUG_RIB
) {
937 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
939 "%u:%s: Filtering out with NH out %s due to route map",
941 ifindex2ifname(nexthop
->ifindex
,
944 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
946 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
949 /* Iterate over all nexthops of the given RIB entry and refresh their
950 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
951 * nexthop is found to toggle the ACTIVE flag, the whole re structure
952 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
953 * transparently passed to nexthop_active_check().
955 * Return value is the new number of active nexthops.
958 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
961 struct nexthop
*nexthop
;
962 union g_addr prev_src
;
963 unsigned int prev_active
, new_active
, old_num_nh
;
964 ifindex_t prev_index
;
966 old_num_nh
= re
->nexthop_active_num
;
968 re
->nexthop_active_num
= 0;
969 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
971 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
972 /* No protocol daemon provides src and so we're skipping
974 prev_src
= nexthop
->rmap_src
;
975 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
976 prev_index
= nexthop
->ifindex
;
978 * We need to respect the multipath_num here
979 * as that what we should be able to install from
980 * a multipath perpsective should not be a data plane
983 new_active
= nexthop_active_check(rn
, re
, nexthop
, set
);
984 if (new_active
&& re
->nexthop_active_num
>= multipath_num
) {
985 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
989 re
->nexthop_active_num
++;
990 /* Don't allow src setting on IPv6 addr for now */
991 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
992 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
993 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
994 && prev_src
.ipv4
.s_addr
995 != nexthop
->rmap_src
.ipv4
.s_addr
)
996 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
997 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
998 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
999 &nexthop
->rmap_src
.ipv6
)))) {
1000 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1001 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1005 if (old_num_nh
!= re
->nexthop_active_num
)
1006 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1008 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1009 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1012 return re
->nexthop_active_num
;
1016 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1017 * (nexthops) must have a label.
1019 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1021 struct nexthop
*nexthop
= NULL
;
1023 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1026 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1027 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1033 /* Update flag indicates whether this is a "replace" or not. Currently, this
1034 * is only used for IPv4.
1036 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1037 struct route_entry
*old
)
1039 struct nexthop
*nexthop
;
1040 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1041 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1042 const struct prefix
*p
, *src_p
;
1043 enum zebra_dplane_result ret
;
1045 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1047 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1049 if (info
->safi
!= SAFI_UNICAST
) {
1050 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1051 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1054 struct nexthop
*prev
;
1056 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1057 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1058 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1059 if (prev
== nexthop
)
1061 if (nexthop_same_firsthop(nexthop
, prev
)) {
1062 SET_FLAG(nexthop
->flags
,
1063 NEXTHOP_FLAG_DUPLICATE
);
1071 * If this is a replace to a new RE let the originator of the RE
1072 * know that they've lost
1074 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1075 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1077 /* Update fib selection */
1078 dest
->selected_fib
= re
;
1081 * Make sure we update the FPM any time we send new information to
1084 hook_call(rib_update
, rn
, "installing in kernel");
1086 /* Send add or update */
1088 ret
= dplane_route_update(rn
, re
, old
);
1090 ret
= dplane_route_add(rn
, re
);
1093 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1094 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1096 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
1098 zvrf
->installs_queued
++;
1100 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1102 char str
[SRCDEST2STR_BUFFER
];
1104 srcdest_rnode2str(rn
, str
, sizeof(str
));
1105 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1106 "%u:%s: Failed to enqueue dataplane install",
1110 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1119 /* Uninstall the route from kernel. */
1120 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1122 struct nexthop
*nexthop
;
1123 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1124 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1126 if (info
->safi
!= SAFI_UNICAST
) {
1127 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1128 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1129 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1134 * Make sure we update the FPM any time we send new information to
1137 hook_call(rib_update
, rn
, "uninstalling from kernel");
1139 switch (dplane_route_delete(rn
, re
)) {
1140 case ZEBRA_DPLANE_REQUEST_QUEUED
:
1142 zvrf
->removals_queued
++;
1144 case ZEBRA_DPLANE_REQUEST_FAILURE
:
1146 char str
[SRCDEST2STR_BUFFER
];
1148 srcdest_rnode2str(rn
, str
, sizeof(str
));
1149 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
1150 "%u:%s: Failed to enqueue dataplane uninstall",
1154 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
1163 /* Uninstall the route from kernel. */
1164 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1166 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1167 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1168 struct nexthop
*nexthop
;
1170 if (dest
&& dest
->selected_fib
== re
) {
1171 if (info
->safi
== SAFI_UNICAST
)
1172 hook_call(rib_update
, rn
, "rib_uninstall");
1174 /* If labeled-unicast route, uninstall transit LSP. */
1175 if (zebra_rib_labeled_unicast(re
))
1176 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1178 if (!RIB_SYSTEM_ROUTE(re
))
1179 rib_uninstall_kernel(rn
, re
);
1181 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1183 dest
->selected_fib
= NULL
;
1185 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1186 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1189 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1190 const struct prefix
*p
, *src_p
;
1192 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1194 redistribute_delete(p
, src_p
, re
);
1195 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1200 * rib_can_delete_dest
1202 * Returns TRUE if the given dest can be deleted from the table.
1204 static int rib_can_delete_dest(rib_dest_t
*dest
)
1211 * Don't delete the dest if we have to update the FPM about this
1214 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1215 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1224 * Garbage collect the rib dest corresponding to the given route node
1227 * Returns TRUE if the dest was deleted, FALSE otherwise.
1229 int rib_gc_dest(struct route_node
*rn
)
1233 dest
= rib_dest_from_rnode(rn
);
1237 if (!rib_can_delete_dest(dest
))
1240 if (IS_ZEBRA_DEBUG_RIB
) {
1241 struct zebra_vrf
*zvrf
;
1243 zvrf
= rib_dest_vrf(dest
);
1244 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1248 XFREE(MTYPE_RIB_DEST
, dest
);
1252 * Release the one reference that we keep on the route node.
1254 route_unlock_node(rn
);
1258 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1259 struct route_entry
*new)
1261 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1263 hook_call(rib_update
, rn
, "new route selected");
1265 /* Update real nexthop. This may actually determine if nexthop is active
1267 if (!nexthop_active_update(rn
, new, true)) {
1268 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1272 if (IS_ZEBRA_DEBUG_RIB
) {
1273 char buf
[SRCDEST2STR_BUFFER
];
1274 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1275 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
1276 zvrf_id(zvrf
), buf
, rn
, new,
1277 zebra_route_string(new->type
));
1280 /* If labeled-unicast route, install transit LSP. */
1281 if (zebra_rib_labeled_unicast(new))
1282 zebra_mpls_lsp_install(zvrf
, rn
, new);
1284 if (!RIB_SYSTEM_ROUTE(new))
1285 rib_install_kernel(rn
, new, NULL
);
1287 dest
->selected_fib
= new;
1289 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1292 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1293 struct route_entry
*old
)
1295 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1296 hook_call(rib_update
, rn
, "removing existing route");
1298 /* Uninstall from kernel. */
1299 if (IS_ZEBRA_DEBUG_RIB
) {
1300 char buf
[SRCDEST2STR_BUFFER
];
1301 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1302 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
1303 zvrf_id(zvrf
), buf
, rn
, old
,
1304 zebra_route_string(old
->type
));
1307 /* If labeled-unicast route, uninstall transit LSP. */
1308 if (zebra_rib_labeled_unicast(old
))
1309 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1311 if (!RIB_SYSTEM_ROUTE(old
))
1312 rib_uninstall_kernel(rn
, old
);
1314 UNSET_FLAG(old
->status
, ROUTE_ENTRY_INSTALLED
);
1316 * We are setting this to NULL here
1317 * because that is what we traditionally
1318 * have been doing. I am not positive
1319 * that this is the right thing to do
1320 * but let's leave the code alone
1321 * for the RIB_SYSTEM_ROUTE case
1323 dest
->selected_fib
= NULL
;
1326 /* Update nexthop for route, reset changed flag. */
1327 /* Note: this code also handles the Linux case when an interface goes
1328 * down, causing the kernel to delete routes without sending DELROUTE
1331 if (!nexthop_active_update(rn
, old
, true) &&
1332 (RIB_KERNEL_ROUTE(old
)))
1333 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1335 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1338 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1339 struct route_node
*rn
,
1340 struct route_entry
*old
,
1341 struct route_entry
*new)
1343 struct nexthop
*nexthop
= NULL
;
1345 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1348 * We have to install or update if a new route has been selected or
1349 * something has changed.
1351 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1352 hook_call(rib_update
, rn
, "updating existing route");
1354 /* Update the nexthop; we could determine here that nexthop is
1356 if (nexthop_active_update(rn
, new, true))
1359 /* If nexthop is active, install the selected route, if
1361 * the install succeeds, cleanup flags for prior route, if
1366 if (IS_ZEBRA_DEBUG_RIB
) {
1367 char buf
[SRCDEST2STR_BUFFER
];
1368 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1371 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
1372 zvrf_id(zvrf
), buf
, rn
, new,
1373 zebra_route_string(new->type
),
1375 zebra_route_string(old
->type
));
1378 "%u:%s: Updating route rn %p, re %p (%s)",
1379 zvrf_id(zvrf
), buf
, rn
, new,
1380 zebra_route_string(new->type
));
1383 /* If labeled-unicast route, uninstall transit LSP. */
1384 if (zebra_rib_labeled_unicast(old
))
1385 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1387 /* Non-system route should be installed. */
1388 if (!RIB_SYSTEM_ROUTE(new)) {
1389 /* If labeled-unicast route, install transit
1391 if (zebra_rib_labeled_unicast(new))
1392 zebra_mpls_lsp_install(zvrf
, rn
, new);
1394 rib_install_kernel(rn
, new, old
);
1396 UNSET_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
);
1398 * We do not need to install the
1399 * selected route because it
1400 * is already isntalled by
1401 * the system( ie not us )
1402 * so just mark it as winning
1403 * we do need to ensure that
1404 * if we uninstall a route
1405 * from ourselves we don't
1406 * over write this pointer
1408 dest
->selected_fib
= NULL
;
1410 /* If install succeeded or system route, cleanup flags
1411 * for prior route. */
1413 if (RIB_SYSTEM_ROUTE(new)) {
1414 if (!RIB_SYSTEM_ROUTE(old
))
1415 rib_uninstall_kernel(rn
, old
);
1419 ROUTE_ENTRY_INSTALLED
);
1421 UNSET_FLAG(old
->status
,
1422 ROUTE_ENTRY_INSTALLED
);
1423 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1424 nexthop
= nexthop
->next
)
1425 UNSET_FLAG(nexthop
->flags
,
1432 * If nexthop for selected route is not active or install
1434 * may need to uninstall and delete for redistribution.
1437 if (IS_ZEBRA_DEBUG_RIB
) {
1438 char buf
[SRCDEST2STR_BUFFER
];
1439 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1442 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1443 zvrf_id(zvrf
), buf
, rn
, new,
1444 zebra_route_string(new->type
),
1446 zebra_route_string(old
->type
));
1449 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
1450 zvrf_id(zvrf
), buf
, rn
, new,
1451 zebra_route_string(new->type
));
1454 /* If labeled-unicast route, uninstall transit LSP. */
1455 if (zebra_rib_labeled_unicast(old
))
1456 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1458 if (!RIB_SYSTEM_ROUTE(old
))
1459 rib_uninstall_kernel(rn
, old
);
1461 UNSET_FLAG(old
->status
, ROUTE_ENTRY_INSTALLED
);
1462 dest
->selected_fib
= NULL
;
1467 * Same route selected; check if in the FIB and if not,
1469 * is housekeeping code to deal with race conditions in kernel
1471 * netlink reporting interface up before IPv4 or IPv6 protocol
1475 if (!RIB_SYSTEM_ROUTE(new)
1476 && !CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
))
1477 rib_install_kernel(rn
, new, NULL
);
1480 /* Update prior route. */
1482 /* Set real nexthop. */
1483 nexthop_active_update(rn
, old
, true);
1484 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1487 /* Clear changed flag. */
1488 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1491 /* Check if 'alternate' RIB entry is better than 'current'. */
1492 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1493 struct route_entry
*alternate
)
1495 if (current
== NULL
)
1498 /* filter route selection in following order:
1499 * - connected beats other types
1500 * - if both connected, loopback or vrf wins
1501 * - lower distance beats higher
1502 * - lower metric beats higher for equal distance
1503 * - last, hence oldest, route wins tie break.
1506 /* Connected routes. Check to see if either are a vrf
1507 * or loopback interface. If not, pick the last connected
1508 * route of the set of lowest metric connected routes.
1510 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1511 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1514 /* both are connected. are either loop or vrf? */
1515 struct nexthop
*nexthop
= NULL
;
1517 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1518 if (if_is_loopback_or_vrf(if_lookup_by_index(
1519 nexthop
->ifindex
, alternate
->vrf_id
)))
1523 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1524 if (if_is_loopback_or_vrf(if_lookup_by_index(
1525 nexthop
->ifindex
, current
->vrf_id
)))
1529 /* Neither are loop or vrf so pick best metric */
1530 if (alternate
->metric
<= current
->metric
)
1536 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1539 /* higher distance loses */
1540 if (alternate
->distance
< current
->distance
)
1542 if (current
->distance
< alternate
->distance
)
1545 /* metric tie-breaks equal distance */
1546 if (alternate
->metric
<= current
->metric
)
1552 /* Core function for processing routing information base. */
1553 static void rib_process(struct route_node
*rn
)
1555 struct route_entry
*re
;
1556 struct route_entry
*next
;
1557 struct route_entry
*old_selected
= NULL
;
1558 struct route_entry
*new_selected
= NULL
;
1559 struct route_entry
*old_fib
= NULL
;
1560 struct route_entry
*new_fib
= NULL
;
1561 struct route_entry
*best
= NULL
;
1562 char buf
[SRCDEST2STR_BUFFER
];
1564 struct zebra_vrf
*zvrf
= NULL
;
1565 const struct prefix
*p
, *src_p
;
1567 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1568 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1572 dest
= rib_dest_from_rnode(rn
);
1574 zvrf
= rib_dest_vrf(dest
);
1575 vrf_id
= zvrf_id(zvrf
);
1578 if (IS_ZEBRA_DEBUG_RIB
)
1579 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1581 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1582 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1585 * we can have rn's that have a NULL info pointer
1586 * (dest). As such let's not let the deref happen
1587 * additionally we know RNODE_FOREACH_RE_SAFE
1588 * will not iterate so we are ok.
1591 old_fib
= dest
->selected_fib
;
1593 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1594 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1596 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1597 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1598 re
->status
, re
->flags
, re
->distance
,
1601 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1603 /* Currently selected re. */
1604 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1605 assert(old_selected
== NULL
);
1609 /* Skip deleted entries from selection */
1610 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1613 /* Skip unreachable nexthop. */
1614 /* This first call to nexthop_active_update is merely to
1616 * there's any change to nexthops associated with this RIB
1618 * rib_process() can be invoked due to an external event such as
1620 * down or due to next-hop-tracking evaluation. In the latter
1622 * a decision has already been made that the NHs have changed.
1624 * need to invoke a potentially expensive call again. Further,
1626 * the change might be in a recursive NH which is not caught in
1627 * the nexthop_active_update() code. Thus, we might miss changes
1631 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1632 && !nexthop_active_update(rn
, re
, false)) {
1633 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1634 /* XXX: HERE BE DRAGONS!!!!!
1635 * In all honesty, I have not yet figured out
1637 * does or why the ROUTE_ENTRY_CHANGED test
1639 * or why we need to delete a route here, and
1641 * this concerns both selected and fib route, or
1644 /* This entry was denied by the 'ip protocol
1645 * table' route-map, we
1646 * need to delete it */
1647 if (re
!= old_selected
) {
1648 if (IS_ZEBRA_DEBUG_RIB
)
1650 "%s: %u:%s: imported via import-table but denied "
1651 "by the ip protocol table route-map",
1652 __func__
, vrf_id
, buf
);
1655 SET_FLAG(re
->status
,
1656 ROUTE_ENTRY_REMOVED
);
1662 /* Infinite distance. */
1663 if (re
->distance
== DISTANCE_INFINITY
) {
1664 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1668 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1669 best
= rib_choose_best(new_fib
, re
);
1670 if (new_fib
&& best
!= new_fib
)
1671 UNSET_FLAG(new_fib
->status
,
1672 ROUTE_ENTRY_CHANGED
);
1675 best
= rib_choose_best(new_selected
, re
);
1676 if (new_selected
&& best
!= new_selected
)
1677 UNSET_FLAG(new_selected
->status
,
1678 ROUTE_ENTRY_CHANGED
);
1679 new_selected
= best
;
1682 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1683 } /* RNODE_FOREACH_RE */
1685 /* If no FIB override route, use the selected route also for FIB */
1686 if (new_fib
== NULL
)
1687 new_fib
= new_selected
;
1689 /* After the cycle is finished, the following pointers will be set:
1690 * old_selected --- RE entry currently having SELECTED
1691 * new_selected --- RE entry that is newly SELECTED
1692 * old_fib --- RE entry currently in kernel FIB
1693 * new_fib --- RE entry that is newly to be in kernel FIB
1695 * new_selected will get SELECTED flag, and is going to be redistributed
1696 * the zclients. new_fib (which can be new_selected) will be installed
1700 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1702 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1703 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1704 (void *)old_fib
, (void *)new_fib
);
1707 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1708 * fib == selected */
1709 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1710 ROUTE_ENTRY_CHANGED
);
1712 /* Update fib according to selection results */
1713 if (new_fib
&& old_fib
)
1714 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1716 rib_process_add_fib(zvrf
, rn
, new_fib
);
1718 rib_process_del_fib(zvrf
, rn
, old_fib
);
1720 /* Update SELECTED entry */
1721 if (old_selected
!= new_selected
|| selected_changed
) {
1723 if (new_selected
&& new_selected
!= new_fib
) {
1724 nexthop_active_update(rn
, new_selected
, true);
1725 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1729 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1731 /* Special case: new route is system route, so
1732 * dataplane update will not be done - ensure we
1733 * redistribute the route.
1735 if (RIB_SYSTEM_ROUTE(new_selected
))
1736 redistribute_update(p
, src_p
, new_selected
,
1742 redistribute_delete(p
, src_p
, old_selected
);
1743 if (old_selected
!= new_selected
)
1744 UNSET_FLAG(old_selected
->flags
,
1745 ZEBRA_FLAG_SELECTED
);
1749 /* Remove all RE entries queued for removal */
1750 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1751 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1752 if (IS_ZEBRA_DEBUG_RIB
) {
1753 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1754 (void *)rn
, (void *)re
);
1761 * Check if the dest can be deleted now.
1767 * Utility to match route with dplane context data
1769 static bool rib_route_match_ctx(const struct route_entry
*re
,
1770 const struct zebra_dplane_ctx
*ctx
,
1773 bool result
= false;
1777 * In 'update' case, we test info about the 'previous' or
1780 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1781 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1784 /* TODO -- we're using this extra test, but it's not
1785 * exactly clear why.
1787 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1788 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1789 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1796 * Ordinary, single-route case using primary context info
1798 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1799 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1800 /* Skip route that's been deleted */
1804 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1805 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1808 /* TODO -- we're using this extra test, but it's not
1809 * exactly clear why.
1811 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1812 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1813 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1825 * Route-update results processing after async dataplane update.
1827 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1829 struct route_table
*table
= NULL
;
1830 struct zebra_vrf
*zvrf
= NULL
;
1831 struct route_node
*rn
= NULL
;
1832 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1833 bool is_update
= false;
1834 struct nexthop
*nexthop
, *ctx_nexthop
;
1835 char dest_str
[PREFIX_STRLEN
] = "";
1836 enum dplane_op_e op
;
1837 enum zebra_dplane_result status
;
1838 const struct prefix
*dest_pfx
, *src_pfx
;
1840 /* Locate rn and re(s) from ctx */
1842 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1843 dplane_ctx_get_safi(ctx
),
1844 dplane_ctx_get_vrf(ctx
),
1845 dplane_ctx_get_table(ctx
));
1846 if (table
== NULL
) {
1847 if (IS_ZEBRA_DEBUG_DPLANE
) {
1848 zlog_debug("Failed to process dplane results: no table for afi %d, safi %d, vrf %u",
1849 dplane_ctx_get_afi(ctx
),
1850 dplane_ctx_get_safi(ctx
),
1851 dplane_ctx_get_vrf(ctx
));
1856 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1858 dest_pfx
= dplane_ctx_get_dest(ctx
);
1860 /* Note well: only capturing the prefix string if debug is enabled here;
1861 * unconditional log messages will have to generate the string.
1863 if (IS_ZEBRA_DEBUG_DPLANE
)
1864 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1866 src_pfx
= dplane_ctx_get_src(ctx
);
1867 rn
= srcdest_rnode_get(table
, dplane_ctx_get_dest(ctx
),
1868 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1870 if (IS_ZEBRA_DEBUG_DPLANE
) {
1871 zlog_debug("Failed to process dplane results: no route for %u:%s",
1872 dplane_ctx_get_vrf(ctx
), dest_str
);
1877 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1879 op
= dplane_ctx_get_op(ctx
);
1880 status
= dplane_ctx_get_status(ctx
);
1882 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1883 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1884 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1885 dplane_op2str(op
), dplane_res2str(status
));
1888 * Update is a bit of a special case, where we may have both old and new
1889 * routes to post-process.
1891 is_update
= dplane_ctx_is_update(ctx
);
1894 * Take a pass through the routes, look for matches with the context
1897 RNODE_FOREACH_RE(rn
, rib
) {
1900 if (rib_route_match_ctx(rib
, ctx
, false))
1904 /* Check for old route match */
1905 if (is_update
&& (old_re
== NULL
)) {
1906 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1910 /* Have we found the routes we need to work on? */
1911 if (re
&& ((!is_update
|| old_re
)))
1916 * Check sequence number(s) to detect stale results before continuing
1919 if (re
->dplane_sequence
!= dplane_ctx_get_seq(ctx
)) {
1920 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1921 zlog_debug("%u:%s Stale dplane result for re %p",
1922 dplane_ctx_get_vrf(ctx
),
1925 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1929 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1930 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1931 zlog_debug("%u:%s Stale dplane result for old_re %p",
1932 dplane_ctx_get_vrf(ctx
),
1935 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1939 case DPLANE_OP_ROUTE_INSTALL
:
1940 case DPLANE_OP_ROUTE_UPDATE
:
1941 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1943 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1944 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1947 * On an update operation from the same route type
1948 * context retrieval currently has no way to know
1949 * which was the old and which was the new.
1950 * So don't unset our flags that we just set.
1951 * We know redistribution is ok because the
1952 * old_re in this case is used for nothing
1953 * more than knowing whom to contact if necessary.
1955 if (old_re
&& old_re
!= re
) {
1956 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1957 UNSET_FLAG(old_re
->status
,
1958 ROUTE_ENTRY_INSTALLED
);
1960 /* Update zebra nexthop FIB flag for each
1961 * nexthop that was installed.
1963 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1969 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1970 if (nexthop_same(ctx_nexthop
, nexthop
))
1974 if (nexthop
== NULL
)
1977 if (CHECK_FLAG(nexthop
->flags
,
1978 NEXTHOP_FLAG_RECURSIVE
))
1981 if (CHECK_FLAG(ctx_nexthop
->flags
,
1983 SET_FLAG(nexthop
->flags
,
1986 UNSET_FLAG(nexthop
->flags
,
1992 /* Set flag for nexthop tracking processing */
1993 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1998 * TODO -- still calling the redist api using the
1999 * route_entries, and there's a corner-case here:
2000 * if there's no client for the 'new' route, a redist
2001 * deleting the 'old' route will be sent. But if the
2002 * 'old' context info was stale, 'old_re' will be
2003 * NULL here and that delete will not be sent.
2006 redistribute_update(dest_pfx
, src_pfx
,
2009 /* Notify route owner */
2010 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2014 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2016 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2018 zsend_route_notify_owner(re
, dest_pfx
,
2019 ZAPI_ROUTE_FAIL_INSTALL
);
2021 zlog_warn("%u:%s: Route install failed",
2022 dplane_ctx_get_vrf(ctx
),
2023 prefix2str(dest_pfx
,
2024 dest_str
, sizeof(dest_str
)));
2027 case DPLANE_OP_ROUTE_DELETE
:
2029 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2031 * In the delete case, the zebra core datastructs were
2032 * updated (or removed) at the time the delete was issued,
2033 * so we're just notifying the route owner.
2035 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2037 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2038 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2040 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2046 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2047 zsend_route_notify_owner_ctx(ctx
,
2048 ZAPI_ROUTE_REMOVE_FAIL
);
2050 zlog_warn("%u:%s: Route Deletion failure",
2051 dplane_ctx_get_vrf(ctx
),
2052 prefix2str(dest_pfx
,
2053 dest_str
, sizeof(dest_str
)));
2062 route_unlock_node(rn
);
2064 /* Return context to dataplane module */
2065 dplane_ctx_fini(&ctx
);
2068 /* Take a list of route_node structs and return 1, if there was a record
2069 * picked from it and processed by rib_process(). Don't process more,
2070 * than one RN record; operate only in the specified sub-queue.
2072 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2074 struct listnode
*lnode
= listhead(subq
);
2075 struct route_node
*rnode
;
2077 struct zebra_vrf
*zvrf
= NULL
;
2082 rnode
= listgetdata(lnode
);
2083 dest
= rib_dest_from_rnode(rnode
);
2085 zvrf
= rib_dest_vrf(dest
);
2089 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2090 char buf
[SRCDEST2STR_BUFFER
];
2091 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2092 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2093 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2097 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2098 RIB_ROUTE_QUEUED(qindex
));
2103 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2104 __func__
, rnode
, rnode
->lock
);
2105 zlog_backtrace(LOG_DEBUG
);
2108 route_unlock_node(rnode
);
2109 list_delete_node(subq
, lnode
);
2114 * Perform next-hop tracking processing after RIB updates.
2116 static void do_nht_processing(void)
2119 struct zebra_vrf
*zvrf
;
2121 /* Evaluate nexthops for those VRFs which underwent route processing.
2123 * should limit the evaluation to the necessary VRFs in most common
2126 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2128 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2131 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHT
)
2132 zlog_debug("NHT processing check for zvrf %s",
2135 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2136 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2137 zebra_evaluate_rnh(zvrf
, AFI_IP
, 0, RNH_IMPORT_CHECK_TYPE
,
2139 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2140 zebra_evaluate_rnh(zvrf
, AFI_IP6
, 0, RNH_IMPORT_CHECK_TYPE
,
2144 /* Schedule LSPs for processing, if needed. */
2145 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2146 if (mpls_should_lsps_be_processed(zvrf
)) {
2147 if (IS_ZEBRA_DEBUG_MPLS
)
2149 "%u: Scheduling all LSPs upon RIB completion",
2151 zebra_mpls_lsp_schedule(zvrf
);
2152 mpls_unmark_lsps_for_processing(zvrf
);
2156 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2157 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2159 * is pointed to the meta queue structure.
2161 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2163 struct meta_queue
*mq
= data
;
2165 uint32_t queue_len
, queue_limit
;
2167 /* Ensure there's room for more dataplane updates */
2168 queue_limit
= dplane_get_in_queue_limit();
2169 queue_len
= dplane_get_in_queue_len();
2170 if (queue_len
> queue_limit
) {
2171 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2172 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2173 queue_len
, queue_limit
);
2175 /* Ensure that the meta-queue is actually enqueued */
2176 if (work_queue_empty(zrouter
.ribq
))
2177 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2179 return WQ_QUEUE_BLOCKED
;
2182 for (i
= 0; i
< MQ_SIZE
; i
++)
2183 if (process_subq(mq
->subq
[i
], i
)) {
2187 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2192 * Look into the RN and queue it into the highest priority queue
2193 * at this point in time for processing.
2195 * We will enqueue a route node only once per invocation.
2197 * There are two possibilities here that should be kept in mind.
2198 * If the original invocation has not been pulled off for processing
2199 * yet, A subsuquent invocation can have a route entry with a better
2200 * meta queue index value and we can have a situation where
2201 * we might have the same node enqueued 2 times. Not necessarily
2202 * an optimal situation but it should be ok.
2204 * The other possibility is that the original invocation has not
2205 * been pulled off for processing yet, A subsusquent invocation
2206 * doesn't have a route_entry with a better meta-queue and the
2207 * original metaqueue index value will win and we'll end up with
2208 * the route node enqueued once.
2210 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2212 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2213 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2214 struct zebra_vrf
*zvrf
;
2216 RNODE_FOREACH_RE (rn
, curr_re
) {
2217 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2219 if (curr_qindex
<= qindex
) {
2221 qindex
= curr_qindex
;
2228 /* Invariant: at this point we always have rn->info set. */
2229 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2230 RIB_ROUTE_QUEUED(qindex
))) {
2231 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2232 rnode_debug(rn
, re
->vrf_id
,
2233 "rn %p is already queued in sub-queue %u",
2234 (void *)rn
, qindex
);
2238 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2239 listnode_add(mq
->subq
[qindex
], rn
);
2240 route_lock_node(rn
);
2243 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2244 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2245 (void *)rn
, qindex
);
2247 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2249 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2252 /* Add route_node to work queue and schedule processing */
2253 void rib_queue_add(struct route_node
*rn
)
2257 /* Pointless to queue a route_node with no RIB entries to add or remove
2259 if (!rnode_to_ribs(rn
)) {
2260 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2261 __func__
, (void *)rn
, rn
->lock
);
2262 zlog_backtrace(LOG_DEBUG
);
2266 if (zrouter
.ribq
== NULL
) {
2267 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2268 "%s: work_queue does not exist!", __func__
);
2273 * The RIB queue should normally be either empty or holding the only
2274 * work_queue_item element. In the latter case this element would
2275 * hold a pointer to the meta queue structure, which must be used to
2276 * actually queue the route nodes to process. So create the MQ
2277 * holder, if necessary, then push the work into it in any case.
2278 * This semantics was introduced after 0.99.9 release.
2280 if (work_queue_empty(zrouter
.ribq
))
2281 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2283 rib_meta_queue_add(zrouter
.mq
, rn
);
2288 /* Create new meta queue.
2289 A destructor function doesn't seem to be necessary here.
2291 static struct meta_queue
*meta_queue_new(void)
2293 struct meta_queue
*new;
2296 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2298 for (i
= 0; i
< MQ_SIZE
; i
++) {
2299 new->subq
[i
] = list_new();
2300 assert(new->subq
[i
]);
2306 void meta_queue_free(struct meta_queue
*mq
)
2310 for (i
= 0; i
< MQ_SIZE
; i
++)
2311 list_delete(&mq
->subq
[i
]);
2313 XFREE(MTYPE_WORK_QUEUE
, mq
);
2316 /* initialise zebra rib work queue */
2317 static void rib_queue_init(void)
2319 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2320 "route_node processing"))) {
2321 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2322 "%s: could not initialise work queue!", __func__
);
2326 /* fill in the work queue spec */
2327 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2328 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2329 zrouter
.ribq
->spec
.completion_func
= NULL
;
2330 /* XXX: TODO: These should be runtime configurable via vty */
2331 zrouter
.ribq
->spec
.max_retries
= 3;
2332 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2333 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2335 if (!(zrouter
.mq
= meta_queue_new())) {
2336 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2337 "%s: could not initialise meta queue!", __func__
);
2343 /* RIB updates are processed via a queue of pointers to route_nodes.
2345 * The queue length is bounded by the maximal size of the routing table,
2346 * as a route_node will not be requeued, if already queued.
2348 * REs are submitted via rib_addnode or rib_delnode which set minimal
2349 * state, or static_install_route (when an existing RE is updated)
2350 * and then submit route_node to queue for best-path selection later.
2351 * Order of add/delete state changes are preserved for any given RE.
2353 * Deleted REs are reaped during best-path selection.
2356 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2357 * |-------->| | best RE, if required
2359 * static_install->|->rib_addqueue...... -> rib_process
2361 * |-------->| |-> rib_unlink
2362 * |-> set ROUTE_ENTRY_REMOVE |
2363 * rib_delnode (RE freed)
2365 * The 'info' pointer of a route_node points to a rib_dest_t
2366 * ('dest'). Queueing state for a route_node is kept on the dest. The
2367 * dest is created on-demand by rib_link() and is kept around at least
2368 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2370 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2372 * - route_nodes: refcounted by:
2373 * - dest attached to route_node:
2374 * - managed by: rib_link/rib_gc_dest
2375 * - route_node processing queue
2376 * - managed by: rib_addqueue, rib_process.
2380 /* Add RE to head of the route node. */
2381 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2383 struct route_entry
*head
;
2386 const char *rmap_name
;
2390 dest
= rib_dest_from_rnode(rn
);
2392 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2393 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2395 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2396 route_lock_node(rn
); /* rn route table reference */
2401 head
= dest
->routes
;
2408 afi
= (rn
->p
.family
== AF_INET
)
2410 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2411 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2412 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2413 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2418 static void rib_addnode(struct route_node
*rn
,
2419 struct route_entry
*re
, int process
)
2421 /* RE node has been un-removed before route-node is processed.
2422 * route_node must hence already be on the queue for processing..
2424 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2425 if (IS_ZEBRA_DEBUG_RIB
)
2426 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2427 (void *)rn
, (void *)re
);
2429 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2432 rib_link(rn
, re
, process
);
2438 * Detach a rib structure from a route_node.
2440 * Note that a call to rib_unlink() should be followed by a call to
2441 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2442 * longer required to be deleted.
2444 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2450 if (IS_ZEBRA_DEBUG_RIB
)
2451 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2454 dest
= rib_dest_from_rnode(rn
);
2457 re
->next
->prev
= re
->prev
;
2460 re
->prev
->next
= re
->next
;
2462 dest
->routes
= re
->next
;
2465 if (dest
->selected_fib
== re
)
2466 dest
->selected_fib
= NULL
;
2468 nexthops_free(re
->ng
.nexthop
);
2469 XFREE(MTYPE_RE
, re
);
2472 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2476 if (IS_ZEBRA_DEBUG_RIB
)
2477 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2478 (void *)rn
, (void *)re
);
2479 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2481 afi
= (rn
->p
.family
== AF_INET
)
2483 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2484 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2485 zebra_del_import_table_entry(rn
, re
);
2486 /* Just clean up if non main table */
2487 if (IS_ZEBRA_DEBUG_RIB
) {
2488 char buf
[SRCDEST2STR_BUFFER
];
2489 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2490 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2491 re
->vrf_id
, buf
, rn
, re
,
2492 zebra_route_string(re
->type
));
2501 /* This function dumps the contents of a given RE entry into
2502 * standard debug log. Calling function name and IP prefix in
2503 * question are passed as 1st and 2nd arguments.
2506 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2507 union prefixconstptr src_pp
,
2508 const struct route_entry
*re
)
2510 const struct prefix
*src_p
= src_pp
.p
;
2511 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2512 char straddr
[PREFIX_STRLEN
];
2513 char srcaddr
[PREFIX_STRLEN
];
2514 struct nexthop
*nexthop
;
2516 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2517 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2518 is_srcdst
? " from " : "",
2519 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2522 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2523 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2526 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2527 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2528 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2529 re
->nexthop_num
, re
->nexthop_active_num
);
2531 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2532 struct interface
*ifp
;
2533 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2535 switch (nexthop
->type
) {
2536 case NEXTHOP_TYPE_BLACKHOLE
:
2537 sprintf(straddr
, "Blackhole");
2539 case NEXTHOP_TYPE_IFINDEX
:
2540 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2542 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2544 case NEXTHOP_TYPE_IPV4
:
2546 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2547 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2550 case NEXTHOP_TYPE_IPV6
:
2551 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2552 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2556 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2557 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2558 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2560 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2563 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2566 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2570 zlog_debug("%s: dump complete", func
);
2573 /* This is an exported helper to rtm_read() to dump the strange
2574 * RE entry found by rib_lookup_ipv4_route()
2577 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2579 struct route_table
*table
;
2580 struct route_node
*rn
;
2581 struct route_entry
*re
;
2582 char prefix_buf
[INET_ADDRSTRLEN
];
2585 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2587 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2588 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2593 /* Scan the RIB table for exactly matching RE entry. */
2594 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2596 /* No route for this prefix. */
2598 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2599 prefix2str((struct prefix
*)p
, prefix_buf
,
2600 sizeof(prefix_buf
)));
2605 route_unlock_node(rn
);
2608 RNODE_FOREACH_RE (rn
, re
) {
2609 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2611 (void *)rn
, (void *)re
,
2612 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2615 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2618 route_entry_dump(p
, NULL
, re
);
2622 /* Check if requested address assignment will fail due to another
2623 * route being installed by zebra in FIB already. Take necessary
2624 * actions, if needed: remove such a route from FIB and deSELECT
2625 * corresponding RE entry. Then put affected RN into RIBQ head.
2627 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2629 struct route_table
*table
;
2630 struct route_node
*rn
;
2631 unsigned changed
= 0;
2634 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2635 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2636 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2641 /* No matches would be the simplest case. */
2642 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2646 route_unlock_node(rn
);
2648 dest
= rib_dest_from_rnode(rn
);
2649 /* Check all RE entries. In case any changes have to be done, requeue
2650 * the RN into RIBQ head. If the routing message about the new connected
2651 * route (generated by the IP address we are going to assign very soon)
2652 * comes before the RIBQ is processed, the new RE entry will join
2653 * RIBQ record already on head. This is necessary for proper
2655 * of the rest of the RE.
2657 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2659 if (IS_ZEBRA_DEBUG_RIB
) {
2660 char buf
[PREFIX_STRLEN
];
2662 zlog_debug("%u:%s: freeing way for connected prefix",
2663 dest
->selected_fib
->vrf_id
,
2664 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2665 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2667 rib_uninstall(rn
, dest
->selected_fib
);
2673 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2674 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2676 struct route_table
*table
;
2677 struct route_node
*rn
;
2678 struct route_entry
*same
= NULL
;
2679 struct nexthop
*nexthop
;
2685 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2688 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2690 XFREE(MTYPE_RE
, re
);
2694 /* Make it sure prefixlen is applied to the prefix. */
2697 apply_mask_ipv6(src_p
);
2699 /* Set default distance by route type. */
2700 if (re
->distance
== 0) {
2701 re
->distance
= route_distance(re
->type
);
2703 /* iBGP distance is 200. */
2704 if (re
->type
== ZEBRA_ROUTE_BGP
2705 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2709 /* Lookup route node.*/
2710 rn
= srcdest_rnode_get(table
, p
, src_p
);
2713 * If same type of route are installed, treat it as a implicit
2715 * If the user has specified the No route replace semantics
2716 * for the install don't do a route replace.
2718 RNODE_FOREACH_RE (rn
, same
) {
2719 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2722 if (same
->type
!= re
->type
)
2724 if (same
->instance
!= re
->instance
)
2726 if (same
->type
== ZEBRA_ROUTE_KERNEL
2727 && same
->metric
!= re
->metric
)
2730 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2731 same
->distance
!= re
->distance
)
2735 * We should allow duplicate connected routes
2736 * because of IPv6 link-local routes and unnumbered
2737 * interfaces on Linux.
2739 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2743 /* If this route is kernel route, set FIB flag to the route. */
2744 if (RIB_SYSTEM_ROUTE(re
)) {
2745 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2746 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2747 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2750 /* Link new re to node.*/
2751 if (IS_ZEBRA_DEBUG_RIB
) {
2752 rnode_debug(rn
, re
->vrf_id
,
2753 "Inserting route rn %p, re %p (%s) existing %p",
2754 rn
, re
, zebra_route_string(re
->type
), same
);
2756 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2757 route_entry_dump(p
, src_p
, re
);
2759 rib_addnode(rn
, re
, 1);
2762 /* Free implicit route.*/
2764 rib_delnode(rn
, same
);
2768 route_unlock_node(rn
);
2772 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2773 unsigned short instance
, int flags
, struct prefix
*p
,
2774 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2775 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2778 struct route_table
*table
;
2779 struct route_node
*rn
;
2780 struct route_entry
*re
;
2781 struct route_entry
*fib
= NULL
;
2782 struct route_entry
*same
= NULL
;
2783 struct nexthop
*rtnh
;
2784 char buf2
[INET6_ADDRSTRLEN
];
2787 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2790 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2797 apply_mask_ipv6(src_p
);
2799 /* Lookup route node. */
2800 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2802 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2804 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2805 if (src_p
&& src_p
->prefixlen
)
2806 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2810 if (IS_ZEBRA_DEBUG_RIB
)
2811 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2813 (src_buf
[0] != '\0') ? " from " : "",
2818 dest
= rib_dest_from_rnode(rn
);
2819 fib
= dest
->selected_fib
;
2821 /* Lookup same type route. */
2822 RNODE_FOREACH_RE (rn
, re
) {
2823 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2826 if (re
->type
!= type
)
2828 if (re
->instance
!= instance
)
2830 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2831 distance
!= re
->distance
)
2834 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2836 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2837 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2838 if (rtnh
->ifindex
!= nh
->ifindex
)
2843 /* Make sure that the route found has the same gateway. */
2849 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2850 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2858 /* If same type of route can't be found and this message is from
2862 * In the past(HA!) we could get here because
2863 * we were receiving a route delete from the
2864 * kernel and we're not marking the proto
2865 * as coming from it's appropriate originator.
2866 * Now that we are properly noticing the fact
2867 * that the kernel has deleted our route we
2868 * are not going to get called in this path
2869 * I am going to leave this here because
2870 * this might still work this way on non-linux
2871 * platforms as well as some weird state I have
2872 * not properly thought of yet.
2873 * If we can show that this code path is
2874 * dead then we can remove it.
2876 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2877 if (IS_ZEBRA_DEBUG_RIB
) {
2878 rnode_debug(rn
, vrf_id
,
2879 "rn %p, re %p (%s) was deleted from kernel, adding",
2881 zebra_route_string(fib
->type
));
2884 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2886 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2888 UNSET_FLAG(rtnh
->flags
,
2892 * This is a non FRR route
2893 * as such we should mark
2896 dest
->selected_fib
= NULL
;
2898 /* This means someone else, other than Zebra,
2900 * a Zebra router from the kernel. We will add
2902 rib_install_kernel(rn
, fib
, NULL
);
2905 if (IS_ZEBRA_DEBUG_RIB
) {
2909 "via %s ifindex %d type %d "
2910 "doesn't exist in rib",
2911 inet_ntop(afi2family(afi
),
2918 "type %d doesn't exist in rib",
2921 route_unlock_node(rn
);
2927 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2929 rib_install_kernel(rn
, same
, NULL
);
2930 route_unlock_node(rn
);
2935 /* Special handling for IPv4 or IPv6 routes sourced from
2936 * EVPN - the nexthop (and associated MAC) need to be
2937 * uninstalled if no more refs.
2939 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2940 struct nexthop
*tmp_nh
;
2942 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2943 struct ipaddr vtep_ip
;
2945 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2946 if (afi
== AFI_IP
) {
2947 vtep_ip
.ipa_type
= IPADDR_V4
;
2948 memcpy(&(vtep_ip
.ipaddr_v4
),
2949 &(tmp_nh
->gate
.ipv4
),
2950 sizeof(struct in_addr
));
2952 vtep_ip
.ipa_type
= IPADDR_V6
;
2953 memcpy(&(vtep_ip
.ipaddr_v6
),
2954 &(tmp_nh
->gate
.ipv6
),
2955 sizeof(struct in6_addr
));
2957 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2961 rib_delnode(rn
, same
);
2964 route_unlock_node(rn
);
2969 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2970 unsigned short instance
, int flags
, struct prefix
*p
,
2971 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2972 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2975 struct route_entry
*re
;
2976 struct nexthop
*nexthop
;
2978 /* Allocate new route_entry structure. */
2979 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2981 re
->instance
= instance
;
2982 re
->distance
= distance
;
2984 re
->metric
= metric
;
2986 re
->table
= table_id
;
2987 re
->vrf_id
= vrf_id
;
2988 re
->nexthop_num
= 0;
2989 re
->uptime
= time(NULL
);
2993 nexthop
= nexthop_new();
2995 route_entry_nexthop_add(re
, nexthop
);
2997 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
3000 /* Schedule routes of a particular table (address-family) based on event. */
3001 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3003 struct route_node
*rn
;
3004 struct route_entry
*re
, *next
;
3006 /* Walk all routes and queue for processing, if appropriate for
3007 * the trigger event.
3009 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3011 * If we are looking at a route node and the node
3012 * has already been queued we don't
3013 * need to queue it up again
3015 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3016 RIB_ROUTE_ANY_QUEUED
))
3019 case RIB_UPDATE_IF_CHANGE
:
3020 /* Examine all routes that won't get processed by the
3022 * triggered by nexthop evaluation (NHT). This would be
3024 * kernel and certain static routes. Note that NHT will
3026 * triggered upon an interface event as connected routes
3028 * get queued for processing.
3030 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3033 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
3034 && re
->type
!= ZEBRA_ROUTE_KERNEL
3035 && re
->type
!= ZEBRA_ROUTE_CONNECT
3036 && re
->type
!= ZEBRA_ROUTE_STATIC
)
3039 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
3044 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3045 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3046 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3049 /* If we only have nexthops to a
3058 case RIB_UPDATE_RMAP_CHANGE
:
3059 case RIB_UPDATE_OTHER
:
3060 /* Right now, examine all routes. Can restrict to a
3062 * some cases (TODO).
3064 if (rnode_to_ribs(rn
))
3074 /* RIB update function. */
3075 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3077 struct route_table
*table
;
3079 /* Process routes of interested address-families. */
3080 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3082 if (IS_ZEBRA_DEBUG_EVENT
)
3083 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3084 rib_update_table(table
, event
);
3087 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3089 if (IS_ZEBRA_DEBUG_EVENT
)
3090 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3091 rib_update_table(table
, event
);
3095 /* Delete self installed routes after zebra is relaunched. */
3096 void rib_sweep_table(struct route_table
*table
)
3098 struct route_node
*rn
;
3099 struct route_entry
*re
;
3100 struct route_entry
*next
;
3101 struct nexthop
*nexthop
;
3106 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3107 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3108 if (IS_ZEBRA_DEBUG_RIB
)
3109 route_entry_dump(&rn
->p
, NULL
, re
);
3111 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3114 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3118 * So we are starting up and have received
3119 * routes from the kernel that we have installed
3120 * from a previous run of zebra but not cleaned
3121 * up ( say a kill -9 )
3122 * But since we haven't actually installed
3123 * them yet( we received them from the kernel )
3124 * we don't think they are active.
3125 * So let's pretend they are active to actually
3127 * In all honesty I'm not sure if we should
3128 * mark them as active when we receive them
3129 * This is startup only so probably ok.
3131 * If we ever decide to move rib_sweep_table
3132 * to a different spot (ie startup )
3133 * this decision needs to be revisited
3135 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3136 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3137 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3139 rib_uninstall_kernel(rn
, re
);
3140 rib_delnode(rn
, re
);
3145 /* Sweep all RIB tables. */
3146 void rib_sweep_route(void)
3149 struct zebra_vrf
*zvrf
;
3151 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3152 if ((zvrf
= vrf
->info
) == NULL
)
3155 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3156 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3159 zebra_router_sweep_route();
3162 /* Remove specific by protocol routes from 'table'. */
3163 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3164 struct route_table
*table
)
3166 struct route_node
*rn
;
3167 struct route_entry
*re
;
3168 struct route_entry
*next
;
3169 unsigned long n
= 0;
3172 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3173 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3174 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3176 if (re
->type
== proto
3177 && re
->instance
== instance
) {
3178 rib_delnode(rn
, re
);
3185 /* Remove specific by protocol routes. */
3186 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3189 struct zebra_vrf
*zvrf
;
3190 unsigned long cnt
= 0;
3192 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3193 if ((zvrf
= vrf
->info
) != NULL
)
3194 cnt
+= rib_score_proto_table(
3196 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3197 + rib_score_proto_table(
3199 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3201 cnt
+= zebra_router_score_proto(proto
, instance
);
3206 /* Close RIB and clean up kernel routes. */
3207 void rib_close_table(struct route_table
*table
)
3209 struct route_node
*rn
;
3210 rib_table_info_t
*info
;
3216 info
= route_table_get_info(table
);
3218 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3219 dest
= rib_dest_from_rnode(rn
);
3221 if (dest
&& dest
->selected_fib
) {
3222 if (info
->safi
== SAFI_UNICAST
)
3223 hook_call(rib_update
, rn
, NULL
);
3225 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
3226 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3227 dest
->selected_fib
= NULL
;
3234 * Handler for async dataplane results after a pseudowire installation
3236 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3238 struct zebra_pw
*pw
;
3239 struct zebra_vrf
*vrf
;
3241 /* The pseudowire code assumes success - we act on an error
3242 * result for installation attempts here.
3244 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3247 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3248 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3249 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3251 zebra_pw_install_failure(pw
);
3261 * Handle results from the dataplane system. Dequeue update context
3262 * structs, dispatch to appropriate internal handlers.
3264 static int rib_process_dplane_results(struct thread
*thread
)
3266 struct zebra_dplane_ctx
*ctx
;
3267 struct dplane_ctx_q ctxlist
;
3269 /* Dequeue a list of completed updates with one lock/unlock cycle */
3272 TAILQ_INIT(&ctxlist
);
3274 /* Take lock controlling queue of results */
3275 pthread_mutex_lock(&dplane_mutex
);
3277 /* Dequeue list of context structs */
3278 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3280 pthread_mutex_unlock(&dplane_mutex
);
3282 /* Dequeue context block */
3283 ctx
= dplane_ctx_dequeue(&ctxlist
);
3285 /* If we've emptied the results queue, we're done */
3290 switch (dplane_ctx_get_op(ctx
)) {
3291 case DPLANE_OP_ROUTE_INSTALL
:
3292 case DPLANE_OP_ROUTE_UPDATE
:
3293 case DPLANE_OP_ROUTE_DELETE
:
3294 rib_process_result(ctx
);
3297 case DPLANE_OP_LSP_INSTALL
:
3298 case DPLANE_OP_LSP_UPDATE
:
3299 case DPLANE_OP_LSP_DELETE
:
3300 zebra_mpls_lsp_dplane_result(ctx
);
3303 case DPLANE_OP_PW_INSTALL
:
3304 case DPLANE_OP_PW_UNINSTALL
:
3305 handle_pw_result(ctx
);
3309 /* Don't expect this: just return the struct? */
3310 dplane_ctx_fini(&ctx
);
3312 } /* Dispatch by op code */
3314 ctx
= dplane_ctx_dequeue(&ctxlist
);
3319 /* Check for nexthop tracking processing after finishing with results */
3320 do_nht_processing();
3326 * Results are returned from the dataplane subsystem, in the context of
3327 * the dataplane pthread. We enqueue the results here for processing by
3328 * the main thread later.
3330 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3332 /* Take lock controlling queue of results */
3333 pthread_mutex_lock(&dplane_mutex
);
3335 /* Enqueue context blocks */
3336 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3338 pthread_mutex_unlock(&dplane_mutex
);
3340 /* Ensure event is signalled to zebra main pthread */
3341 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3347 /* Routing information base initialize. */
3352 /* Init dataplane, and register for results */
3353 pthread_mutex_init(&dplane_mutex
, NULL
);
3354 TAILQ_INIT(&rib_dplane_q
);
3355 zebra_dplane_init(rib_dplane_results
);
3361 * Get the first vrf id that is greater than the given vrf id if any.
3363 * Returns TRUE if a vrf id was found, FALSE otherwise.
3365 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3369 vrf
= vrf_lookup_by_id(vrf_id
);
3371 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3373 *next_id_p
= vrf
->vrf_id
;
3382 * rib_tables_iter_next
3384 * Returns the next table in the iteration.
3386 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3388 struct route_table
*table
;
3391 * Array that helps us go over all AFI/SAFI combinations via one
3398 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3399 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3400 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3405 switch (iter
->state
) {
3407 case RIB_TABLES_ITER_S_INIT
:
3408 iter
->vrf_id
= VRF_DEFAULT
;
3409 iter
->afi_safi_ix
= -1;
3413 case RIB_TABLES_ITER_S_ITERATING
:
3414 iter
->afi_safi_ix
++;
3417 while (iter
->afi_safi_ix
3418 < (int)ZEBRA_NUM_OF(afi_safis
)) {
3419 table
= zebra_vrf_table(
3420 afi_safis
[iter
->afi_safi_ix
].afi
,
3421 afi_safis
[iter
->afi_safi_ix
].safi
,
3426 iter
->afi_safi_ix
++;
3430 * Found another table in this vrf.
3436 * Done with all tables in the current vrf, go to the
3440 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3443 iter
->afi_safi_ix
= 0;
3448 case RIB_TABLES_ITER_S_DONE
:
3453 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3455 iter
->state
= RIB_TABLES_ITER_S_DONE
;