1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
39 #include "nexthop_group_private.h"
40 #include "frr_pthread.h"
42 #include "zebra/zebra_router.h"
43 #include "zebra/connected.h"
44 #include "zebra/debug.h"
45 #include "zebra/interface.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/rib.h"
49 #include "zebra/zapi_msg.h"
50 #include "zebra/zebra_errors.h"
51 #include "zebra/zebra_memory.h"
52 #include "zebra/zebra_ns.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/zebra_routemap.h"
55 #include "zebra/zebra_vrf.h"
56 #include "zebra/zebra_vxlan.h"
57 #include "zebra/zapi_msg.h"
58 #include "zebra/zebra_dplane.h"
60 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
63 * Event, list, and mutex for delivery of dataplane results
65 static pthread_mutex_t dplane_mutex
;
66 static struct thread
*t_dplane
;
67 static struct dplane_ctx_q rib_dplane_q
;
69 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
72 /* Should we allow non Quagga processes to delete our routes */
73 extern int allow_delete
;
75 /* Each route type's string and default distance value. */
80 } route_info
[ZEBRA_ROUTE_MAX
] = {
81 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Uneeded for nhg's */, 0},
82 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 5},
83 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 1},
84 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 1},
85 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 2},
86 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 3},
87 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 3},
88 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 3},
89 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 3},
90 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 3},
91 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 4},
92 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 5},
93 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 3},
94 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 3},
95 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 5},
96 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 5},
97 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 2},
98 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 5},
99 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 4},
100 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 4},
101 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 4},
102 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 4},
103 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 4},
104 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 3},
105 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 5},
106 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 5},
107 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 5},
108 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 3},
109 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 5}
110 /* Any new route type added to zebra, should be mirrored here */
112 /* no entry/default: 150 */
115 static void __attribute__((format(printf
, 5, 6)))
116 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
117 int priority
, const char *msgfmt
, ...)
119 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
123 va_start(ap
, msgfmt
);
124 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
128 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
129 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
131 if (info
->safi
== SAFI_MULTICAST
)
132 strlcat(buf
, " (MRIB)", sizeof(buf
));
134 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
137 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
140 #define rnode_debug(node, vrf_id, ...) \
141 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
142 #define rnode_info(node, ...) \
143 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
145 uint8_t route_distance(int type
)
149 if ((unsigned)type
>= array_size(route_info
))
152 distance
= route_info
[type
].distance
;
157 int is_zebra_valid_kernel_table(uint32_t table_id
)
160 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
161 || (table_id
== RT_TABLE_COMPAT
))
168 int is_zebra_main_routing_table(uint32_t table_id
)
170 if (table_id
== RT_TABLE_MAIN
)
175 int zebra_check_addr(const struct prefix
*p
)
177 if (p
->family
== AF_INET
) {
180 addr
= p
->u
.prefix4
.s_addr
;
183 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
184 || IPV4_LINKLOCAL(addr
))
187 if (p
->family
== AF_INET6
) {
188 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
190 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
196 /* Add nexthop to the end of a rib node's nexthop list */
197 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
199 _nexthop_group_add_sorted(re
->ng
, nexthop
);
204 * copy_nexthop - copy a nexthop to the rib structure.
206 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
208 assert(!re
->ng
->nexthop
);
209 copy_nexthops(&re
->ng
->nexthop
, nh
, NULL
);
212 /* Delete specified nexthop from the list. */
213 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
216 nexthop
->next
->prev
= nexthop
->prev
;
218 nexthop
->prev
->next
= nexthop
->next
;
220 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 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
337 union g_addr
*addr
, struct route_node
**rn_out
)
340 struct route_table
*table
;
341 struct route_node
*rn
;
342 struct route_entry
*match
= NULL
;
345 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
349 memset(&p
, 0, sizeof(struct prefix
));
352 p
.u
.prefix4
= addr
->ipv4
;
353 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
355 p
.u
.prefix6
= addr
->ipv6
;
356 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
359 rn
= route_node_match(table
, (struct prefix
*)&p
);
364 route_unlock_node(rn
);
366 dest
= rib_dest_from_rnode(rn
);
367 if (dest
&& dest
->selected_fib
368 && !CHECK_FLAG(dest
->selected_fib
->status
,
369 ROUTE_ENTRY_REMOVED
))
370 match
= dest
->selected_fib
;
372 /* If there is no selected route or matched route is EGP, go up
377 } while (rn
&& rn
->info
== NULL
);
381 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
382 if (!CHECK_FLAG(match
->status
,
383 ROUTE_ENTRY_INSTALLED
))
395 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
397 struct route_node
**rn_out
)
399 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
400 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
401 union g_addr gaddr
= {.ipv4
= addr
};
403 switch (zrouter
.ipv4_multicast_mode
) {
404 case MCAST_MRIB_ONLY
:
405 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
407 case MCAST_URIB_ONLY
:
408 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
409 case MCAST_NO_CONFIG
:
410 case MCAST_MIX_MRIB_FIRST
:
411 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
414 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
417 case MCAST_MIX_DISTANCE
:
418 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
419 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
421 re
= ure
->distance
< mre
->distance
? ure
: mre
;
427 case MCAST_MIX_PFXLEN
:
428 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
429 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
431 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
440 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
442 if (IS_ZEBRA_DEBUG_RIB
) {
444 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
446 zlog_debug("%s: %s: vrf: %u found %s, using %s",
447 __func__
, buf
, vrf_id
,
448 mre
? (ure
? "MRIB+URIB" : "MRIB")
449 : ure
? "URIB" : "nothing",
450 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
455 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
457 struct route_table
*table
;
458 struct route_node
*rn
;
459 struct route_entry
*match
= NULL
;
463 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
467 rn
= route_node_lookup(table
, (struct prefix
*)p
);
469 /* No route for this prefix. */
474 route_unlock_node(rn
);
475 dest
= rib_dest_from_rnode(rn
);
477 if (dest
&& dest
->selected_fib
478 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
479 match
= dest
->selected_fib
;
484 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
487 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
494 * Is this RIB labeled-unicast? It must be of type BGP and all paths
495 * (nexthops) must have a label.
497 int zebra_rib_labeled_unicast(struct route_entry
*re
)
499 struct nexthop
*nexthop
= NULL
;
501 if (re
->type
!= ZEBRA_ROUTE_BGP
)
504 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
))
505 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
511 /* Update flag indicates whether this is a "replace" or not. Currently, this
512 * is only used for IPv4.
514 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
515 struct route_entry
*old
)
517 struct nexthop
*nexthop
;
518 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
519 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
520 const struct prefix
*p
, *src_p
;
521 enum zebra_dplane_result ret
;
523 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
525 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
527 if (info
->safi
!= SAFI_UNICAST
) {
528 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
))
529 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
535 * Install the resolved nexthop object first.
537 zebra_nhg_install_kernel(zebra_nhg_lookup_id(re
->nhe_id
));
540 * If this is a replace to a new RE let the originator of the RE
541 * know that they've lost
543 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
544 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
546 /* Update fib selection */
547 dest
->selected_fib
= re
;
550 * Make sure we update the FPM any time we send new information to
553 hook_call(rib_update
, rn
, "installing in kernel");
555 /* Send add or update */
557 ret
= dplane_route_update(rn
, re
, old
);
559 ret
= dplane_route_add(rn
, re
);
562 case ZEBRA_DPLANE_REQUEST_QUEUED
:
563 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
566 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
568 /* Free old FIB nexthop group */
569 if (old
->fib_ng
.nexthop
) {
570 nexthops_free(old
->fib_ng
.nexthop
);
571 old
->fib_ng
.nexthop
= NULL
;
576 zvrf
->installs_queued
++;
578 case ZEBRA_DPLANE_REQUEST_FAILURE
:
580 char str
[SRCDEST2STR_BUFFER
];
582 srcdest_rnode2str(rn
, str
, sizeof(str
));
583 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
584 "%u:%s: Failed to enqueue dataplane install",
588 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
597 /* Uninstall the route from kernel. */
598 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
600 struct nexthop
*nexthop
;
601 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
602 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
604 if (info
->safi
!= SAFI_UNICAST
) {
605 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
606 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
))
607 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
612 * Make sure we update the FPM any time we send new information to
615 hook_call(rib_update
, rn
, "uninstalling from kernel");
617 switch (dplane_route_delete(rn
, re
)) {
618 case ZEBRA_DPLANE_REQUEST_QUEUED
:
620 zvrf
->removals_queued
++;
622 case ZEBRA_DPLANE_REQUEST_FAILURE
:
624 char str
[SRCDEST2STR_BUFFER
];
626 srcdest_rnode2str(rn
, str
, sizeof(str
));
627 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
628 "%u:%s: Failed to enqueue dataplane uninstall",
632 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
641 /* Uninstall the route from kernel. */
642 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
644 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
645 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
646 struct nexthop
*nexthop
;
648 if (dest
&& dest
->selected_fib
== re
) {
649 if (info
->safi
== SAFI_UNICAST
)
650 hook_call(rib_update
, rn
, "rib_uninstall");
652 /* If labeled-unicast route, uninstall transit LSP. */
653 if (zebra_rib_labeled_unicast(re
))
654 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
656 rib_uninstall_kernel(rn
, re
);
658 dest
->selected_fib
= NULL
;
660 /* Free FIB nexthop group, if present */
661 if (re
->fib_ng
.nexthop
) {
662 nexthops_free(re
->fib_ng
.nexthop
);
663 re
->fib_ng
.nexthop
= NULL
;
666 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
))
667 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
670 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
671 const struct prefix
*p
, *src_p
;
673 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
675 redistribute_delete(p
, src_p
, re
, NULL
);
676 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
681 * rib_can_delete_dest
683 * Returns true if the given dest can be deleted from the table.
685 static int rib_can_delete_dest(rib_dest_t
*dest
)
687 if (re_list_first(&dest
->routes
)) {
692 * Unresolved rnh's are stored on the default route's list
694 * dest->rnode can also be the source prefix node in an
695 * ipv6 sourcedest table. Fortunately the prefix of a
696 * source prefix node can never be the default prefix.
698 if (is_default_prefix(&dest
->rnode
->p
))
702 * Don't delete the dest if we have to update the FPM about this
705 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
706 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
712 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
714 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
718 * We are storing the rnh's associated withb
719 * the tracked nexthop as a list of the rn's.
720 * Unresolved rnh's are placed at the top
721 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
722 * As such for each rn we need to walk up the tree
723 * and see if any rnh's need to see if they
724 * would match a more specific route
727 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
728 char buf
[PREFIX_STRLEN
];
730 zlog_debug("%s: %s Being examined for Nexthop Tracking Count: %zd",
732 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
733 dest
? rnh_list_count(&dest
->nht
) : 0);
738 dest
= rib_dest_from_rnode(rn
);
742 * If we have any rnh's stored in the nht list
743 * then we know that this route node was used for
744 * nht resolution and as such we need to call the
745 * nexthop tracking evaluation code
747 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
748 struct zebra_vrf
*zvrf
=
749 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
750 struct prefix
*p
= &rnh
->node
->p
;
752 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
753 char buf1
[PREFIX_STRLEN
];
754 char buf2
[PREFIX_STRLEN
];
756 zlog_debug("%u:%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u",
758 srcdest_rnode2str(rn
, buf1
,
760 prefix2str(p
, buf2
, sizeof(buf2
)),
761 rnh_type2str(rnh
->type
),
766 * If we have evaluated this node on this pass
767 * already, due to following the tree up
768 * then we know that we can move onto the next
771 * Additionally we call zebra_evaluate_rnh
772 * when we gc the dest. In this case we know
773 * that there must be no other re's where
774 * we were originally as such we know that
775 * that sequence number is ok to respect.
777 if (rnh
->seqno
== seq
) {
778 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
780 "\tNode processed and moved already");
785 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
791 dest
= rib_dest_from_rnode(rn
);
798 * Garbage collect the rib dest corresponding to the given route node
801 * Returns true if the dest was deleted, false otherwise.
803 int rib_gc_dest(struct route_node
*rn
)
807 dest
= rib_dest_from_rnode(rn
);
811 if (!rib_can_delete_dest(dest
))
814 if (IS_ZEBRA_DEBUG_RIB
) {
815 struct zebra_vrf
*zvrf
;
817 zvrf
= rib_dest_vrf(dest
);
818 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
821 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
824 rnh_list_fini(&dest
->nht
);
825 XFREE(MTYPE_RIB_DEST
, dest
);
829 * Release the one reference that we keep on the route node.
831 route_unlock_node(rn
);
835 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
836 struct route_entry
*new)
838 hook_call(rib_update
, rn
, "new route selected");
840 /* Update real nexthop. This may actually determine if nexthop is active
842 if (!nexthop_group_active_nexthop_num(new->ng
)) {
843 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
847 if (IS_ZEBRA_DEBUG_RIB
) {
848 char buf
[SRCDEST2STR_BUFFER
];
849 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
850 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
851 zvrf_id(zvrf
), buf
, rn
, new,
852 zebra_route_string(new->type
));
855 /* If labeled-unicast route, install transit LSP. */
856 if (zebra_rib_labeled_unicast(new))
857 zebra_mpls_lsp_install(zvrf
, rn
, new);
859 rib_install_kernel(rn
, new, NULL
);
861 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
864 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
865 struct route_entry
*old
)
867 hook_call(rib_update
, rn
, "removing existing route");
869 /* Uninstall from kernel. */
870 if (IS_ZEBRA_DEBUG_RIB
) {
871 char buf
[SRCDEST2STR_BUFFER
];
872 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
873 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
874 zvrf_id(zvrf
), buf
, rn
, old
,
875 zebra_route_string(old
->type
));
878 /* If labeled-unicast route, uninstall transit LSP. */
879 if (zebra_rib_labeled_unicast(old
))
880 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
882 rib_uninstall_kernel(rn
, old
);
884 /* Update nexthop for route, reset changed flag. */
885 /* Note: this code also handles the Linux case when an interface goes
886 * down, causing the kernel to delete routes without sending DELROUTE
889 if (RIB_KERNEL_ROUTE(old
))
890 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
892 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
895 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
896 struct route_node
*rn
,
897 struct route_entry
*old
,
898 struct route_entry
*new)
903 * We have to install or update if a new route has been selected or
904 * something has changed.
906 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
907 hook_call(rib_update
, rn
, "updating existing route");
909 /* Update the nexthop; we could determine here that nexthop is
911 if (nexthop_group_active_nexthop_num(new->ng
))
914 /* If nexthop is active, install the selected route, if
916 * the install succeeds, cleanup flags for prior route, if
921 if (IS_ZEBRA_DEBUG_RIB
) {
922 char buf
[SRCDEST2STR_BUFFER
];
923 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
926 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
927 zvrf_id(zvrf
), buf
, rn
, new,
928 zebra_route_string(new->type
),
930 zebra_route_string(old
->type
));
933 "%u:%s: Updating route rn %p, re %p (%s)",
934 zvrf_id(zvrf
), buf
, rn
, new,
935 zebra_route_string(new->type
));
938 /* If labeled-unicast route, uninstall transit LSP. */
939 if (zebra_rib_labeled_unicast(old
))
940 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
943 * Non-system route should be installed.
944 * If labeled-unicast route, install transit
947 if (zebra_rib_labeled_unicast(new))
948 zebra_mpls_lsp_install(zvrf
, rn
, new);
950 rib_install_kernel(rn
, new, old
);
954 * If nexthop for selected route is not active or install
956 * may need to uninstall and delete for redistribution.
959 if (IS_ZEBRA_DEBUG_RIB
) {
960 char buf
[SRCDEST2STR_BUFFER
];
961 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
964 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
965 zvrf_id(zvrf
), buf
, rn
, new,
966 zebra_route_string(new->type
),
968 zebra_route_string(old
->type
));
971 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
972 zvrf_id(zvrf
), buf
, rn
, new,
973 zebra_route_string(new->type
));
976 /* If labeled-unicast route, uninstall transit LSP. */
977 if (zebra_rib_labeled_unicast(old
))
978 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
980 rib_uninstall_kernel(rn
, old
);
984 * Same route selected; check if in the FIB and if not,
985 * re-install. This is housekeeping code to deal with
986 * race conditions in kernel with linux netlink reporting
987 * interface up before IPv4 or IPv6 protocol is ready
990 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
991 RIB_SYSTEM_ROUTE(new))
992 rib_install_kernel(rn
, new, NULL
);
995 /* Update prior route. */
997 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
999 /* Clear changed flag. */
1000 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1003 /* Check if 'alternate' RIB entry is better than 'current'. */
1004 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1005 struct route_entry
*alternate
)
1007 if (current
== NULL
)
1010 /* filter route selection in following order:
1011 * - connected beats other types
1012 * - if both connected, loopback or vrf wins
1013 * - lower distance beats higher
1014 * - lower metric beats higher for equal distance
1015 * - last, hence oldest, route wins tie break.
1018 /* Connected routes. Check to see if either are a vrf
1019 * or loopback interface. If not, pick the last connected
1020 * route of the set of lowest metric connected routes.
1022 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1023 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1026 /* both are connected. are either loop or vrf? */
1027 struct nexthop
*nexthop
= NULL
;
1029 for (ALL_NEXTHOPS_PTR(alternate
->ng
, nexthop
)) {
1030 struct interface
*ifp
= if_lookup_by_index(
1031 nexthop
->ifindex
, alternate
->vrf_id
);
1033 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1037 for (ALL_NEXTHOPS_PTR(current
->ng
, nexthop
)) {
1038 struct interface
*ifp
= if_lookup_by_index(
1039 nexthop
->ifindex
, current
->vrf_id
);
1041 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1045 /* Neither are loop or vrf so pick best metric */
1046 if (alternate
->metric
<= current
->metric
)
1052 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1055 /* higher distance loses */
1056 if (alternate
->distance
< current
->distance
)
1058 if (current
->distance
< alternate
->distance
)
1061 /* metric tie-breaks equal distance */
1062 if (alternate
->metric
<= current
->metric
)
1068 /* Core function for processing nexthop group contexts's off metaq */
1069 static void rib_nhg_process(struct nhg_ctx
*ctx
)
1071 nhg_ctx_process(ctx
);
1074 /* Core function for processing routing information base. */
1075 static void rib_process(struct route_node
*rn
)
1077 struct route_entry
*re
;
1078 struct route_entry
*next
;
1079 struct route_entry
*old_selected
= NULL
;
1080 struct route_entry
*new_selected
= NULL
;
1081 struct route_entry
*old_fib
= NULL
;
1082 struct route_entry
*new_fib
= NULL
;
1083 struct route_entry
*best
= NULL
;
1084 char buf
[SRCDEST2STR_BUFFER
];
1086 struct zebra_vrf
*zvrf
= NULL
;
1087 const struct prefix
*p
, *src_p
;
1089 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1090 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1094 dest
= rib_dest_from_rnode(rn
);
1096 zvrf
= rib_dest_vrf(dest
);
1097 vrf_id
= zvrf_id(zvrf
);
1100 if (IS_ZEBRA_DEBUG_RIB
)
1101 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1103 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1104 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1107 * we can have rn's that have a NULL info pointer
1108 * (dest). As such let's not let the deref happen
1109 * additionally we know RNODE_FOREACH_RE_SAFE
1110 * will not iterate so we are ok.
1113 old_fib
= dest
->selected_fib
;
1115 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1116 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1118 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1119 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1120 re
->status
, re
->flags
, re
->distance
,
1123 /* Currently selected re. */
1124 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1125 assert(old_selected
== NULL
);
1129 /* Skip deleted entries from selection */
1130 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1133 /* Skip unreachable nexthop. */
1134 /* This first call to nexthop_active_update is merely to
1135 * determine if there's any change to nexthops associated
1136 * with this RIB entry. Now, rib_process() can be invoked due
1137 * to an external event such as link down or due to
1138 * next-hop-tracking evaluation. In the latter case,
1139 * a decision has already been made that the NHs have changed.
1140 * So, no need to invoke a potentially expensive call again.
1141 * Further, since the change might be in a recursive NH which
1142 * is not caught in the nexthop_active_update() code. Thus, we
1143 * might miss changes to recursive NHs.
1145 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1146 && !nexthop_active_update(rn
, re
)) {
1147 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1148 /* XXX: HERE BE DRAGONS!!!!!
1149 * In all honesty, I have not yet figured out
1150 * what this part does or why the
1151 * ROUTE_ENTRY_CHANGED test above is correct
1152 * or why we need to delete a route here, and
1153 * also not whether this concerns both selected
1154 * and fib route, or only selected
1157 * This entry was denied by the 'ip protocol
1158 * table' route-map, we need to delete it */
1159 if (re
!= old_selected
) {
1160 if (IS_ZEBRA_DEBUG_RIB
)
1162 "%s: %u:%s: imported via import-table but denied "
1163 "by the ip protocol table route-map",
1164 __func__
, vrf_id
, buf
);
1167 SET_FLAG(re
->status
,
1168 ROUTE_ENTRY_REMOVED
);
1174 /* Infinite distance. */
1175 if (re
->distance
== DISTANCE_INFINITY
) {
1176 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1180 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1181 best
= rib_choose_best(new_fib
, re
);
1182 if (new_fib
&& best
!= new_fib
)
1183 UNSET_FLAG(new_fib
->status
,
1184 ROUTE_ENTRY_CHANGED
);
1187 best
= rib_choose_best(new_selected
, re
);
1188 if (new_selected
&& best
!= new_selected
)
1189 UNSET_FLAG(new_selected
->status
,
1190 ROUTE_ENTRY_CHANGED
);
1191 new_selected
= best
;
1194 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1195 } /* RNODE_FOREACH_RE */
1197 /* If no FIB override route, use the selected route also for FIB */
1198 if (new_fib
== NULL
)
1199 new_fib
= new_selected
;
1201 /* After the cycle is finished, the following pointers will be set:
1202 * old_selected --- RE entry currently having SELECTED
1203 * new_selected --- RE entry that is newly SELECTED
1204 * old_fib --- RE entry currently in kernel FIB
1205 * new_fib --- RE entry that is newly to be in kernel FIB
1207 * new_selected will get SELECTED flag, and is going to be redistributed
1208 * the zclients. new_fib (which can be new_selected) will be installed
1212 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1214 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1215 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1216 (void *)old_fib
, (void *)new_fib
);
1219 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1220 * fib == selected */
1221 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1222 ROUTE_ENTRY_CHANGED
);
1224 /* Update fib according to selection results */
1225 if (new_fib
&& old_fib
)
1226 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1228 rib_process_add_fib(zvrf
, rn
, new_fib
);
1230 rib_process_del_fib(zvrf
, rn
, old_fib
);
1232 /* Update SELECTED entry */
1233 if (old_selected
!= new_selected
|| selected_changed
) {
1235 if (new_selected
&& new_selected
!= new_fib
)
1236 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1239 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1243 * If we're removing the old entry, we should tell
1244 * redist subscribers about that *if* they aren't
1245 * going to see a redist for the new entry.
1247 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1248 ROUTE_ENTRY_REMOVED
))
1249 redistribute_delete(p
, src_p
,
1253 if (old_selected
!= new_selected
)
1254 UNSET_FLAG(old_selected
->flags
,
1255 ZEBRA_FLAG_SELECTED
);
1259 /* Remove all RE entries queued for removal */
1260 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1261 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1262 if (IS_ZEBRA_DEBUG_RIB
) {
1263 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1264 (void *)rn
, (void *)re
);
1271 * Check if the dest can be deleted now.
1276 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1278 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1279 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1284 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1285 if (IS_ZEBRA_DEBUG_MPLS
)
1287 "%u: Scheduling all LSPs upon RIB completion",
1289 zebra_mpls_lsp_schedule(zvrf
);
1290 mpls_unmark_lsps_for_processing(rn
);
1295 * Utility to match route with dplane context data
1297 static bool rib_route_match_ctx(const struct route_entry
*re
,
1298 const struct zebra_dplane_ctx
*ctx
,
1301 bool result
= false;
1305 * In 'update' case, we test info about the 'previous' or
1308 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1309 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1312 /* TODO -- we're using this extra test, but it's not
1313 * exactly clear why.
1315 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1316 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1317 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1324 * Ordinary, single-route case using primary context info
1326 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1327 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1328 /* Skip route that's been deleted */
1332 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1333 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1336 /* TODO -- we're using this extra test, but it's not
1337 * exactly clear why.
1339 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1340 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1341 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1352 static void zebra_rib_fixup_system(struct route_node
*rn
)
1354 struct route_entry
*re
;
1356 RNODE_FOREACH_RE(rn
, re
) {
1357 struct nexthop
*nhop
;
1359 if (!RIB_SYSTEM_ROUTE(re
))
1362 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1365 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1366 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1368 for (ALL_NEXTHOPS_PTR(re
->ng
, nhop
)) {
1369 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1372 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1378 * Update a route from a dplane context. This consolidates common code
1379 * that can be used in processing of results from FIB updates, and in
1380 * async notification processing.
1381 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1383 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1384 struct route_node
*rn
,
1385 struct zebra_dplane_ctx
*ctx
)
1387 char dest_str
[PREFIX_STRLEN
] = "";
1388 char nh_str
[NEXTHOP_STRLEN
];
1389 struct nexthop
*nexthop
, *ctx_nexthop
;
1391 const struct nexthop_group
*ctxnhg
;
1392 bool is_selected
= false; /* Is 're' currently the selected re? */
1393 bool changed_p
= false; /* Change to nexthops? */
1396 /* Note well: only capturing the prefix string if debug is enabled here;
1397 * unconditional log messages will have to generate the string.
1399 if (IS_ZEBRA_DEBUG_RIB
)
1400 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1402 dest
= rib_dest_from_rnode(rn
);
1404 is_selected
= (re
== dest
->selected_fib
);
1406 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1407 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1408 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1410 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1411 * If the installed set differs from the set requested by the rib/owner,
1412 * we use the fib-specific nexthop-group to record the actual FIB
1416 /* Check both fib group and notif group for equivalence.
1418 * Let's assume the nexthops are ordered here to save time.
1420 if (nexthop_group_equal(&re
->fib_ng
, dplane_ctx_get_ng(ctx
)) == false) {
1421 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1423 "%u:%s update_from_ctx: notif nh and fib nh mismatch",
1424 re
->vrf_id
, dest_str
);
1431 /* If the new FIB set matches the existing FIB set, we're done. */
1433 if (IS_ZEBRA_DEBUG_RIB
)
1434 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1435 re
->vrf_id
, dest_str
);
1438 } else if (re
->fib_ng
.nexthop
) {
1440 * Free stale fib list and move on to check the rib nhg.
1442 if (IS_ZEBRA_DEBUG_RIB
)
1443 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1444 re
->vrf_id
, dest_str
);
1445 nexthops_free(re
->fib_ng
.nexthop
);
1446 re
->fib_ng
.nexthop
= NULL
;
1448 /* Note that the installed nexthops have changed */
1451 if (IS_ZEBRA_DEBUG_RIB
)
1452 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1453 re
->vrf_id
, dest_str
);
1457 * Compare with the rib nexthop group. The comparison here is different:
1458 * the RIB group may be a superset of the list installed in the FIB. We
1459 * walk the RIB group, looking for the 'installable' candidate
1460 * nexthops, and then check those against the set
1461 * that is actually installed.
1463 * Assume nexthops are ordered here as well.
1467 ctx_nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
1469 /* Get the first `installed` one to check against.
1470 * If the dataplane doesn't set these to be what was actually installed,
1471 * it will just be whatever was in re->ng?
1473 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1474 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1475 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1477 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
)) {
1479 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1482 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1485 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1486 if (nexthop_same(ctx_nexthop
, nexthop
) == false) {
1487 /* If the FIB doesn't know about the nexthop,
1488 * it's not installed
1490 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1491 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1493 "update_from_ctx: no notif match for rib nh %s",
1498 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1501 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1503 /* Keep checking nexthops */
1507 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1508 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1511 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1513 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1516 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1519 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1522 /* If all nexthops were processed, we're done */
1524 if (IS_ZEBRA_DEBUG_RIB
)
1525 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1526 re
->vrf_id
, dest_str
,
1527 (changed_p
? "true" : "false"));
1531 /* FIB nexthop set differs from the RIB set:
1532 * create a fib-specific nexthop-group
1534 if (IS_ZEBRA_DEBUG_RIB
)
1535 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1536 re
->vrf_id
, dest_str
,
1537 (changed_p
? "true" : "false"));
1539 ctxnhg
= dplane_ctx_get_ng(ctx
);
1541 if (ctxnhg
->nexthop
)
1542 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1544 /* Bit of a special case when the fib has _no_ installed
1547 nexthop
= nexthop_new();
1548 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1549 _nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1557 * Helper to locate a zebra route-node from a dplane context. This is used
1558 * when processing dplane results, e.g. Note well: the route-node is returned
1559 * with a ref held - route_unlock_node() must be called eventually.
1561 static struct route_node
*
1562 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1564 struct route_table
*table
= NULL
;
1565 struct route_node
*rn
= NULL
;
1566 const struct prefix
*dest_pfx
, *src_pfx
;
1568 /* Locate rn and re(s) from ctx */
1570 table
= zebra_vrf_lookup_table_with_table_id(
1571 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1572 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1573 if (table
== NULL
) {
1574 if (IS_ZEBRA_DEBUG_DPLANE
) {
1575 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1576 dplane_ctx_get_afi(ctx
),
1577 dplane_ctx_get_safi(ctx
),
1578 dplane_ctx_get_vrf(ctx
));
1583 dest_pfx
= dplane_ctx_get_dest(ctx
);
1584 src_pfx
= dplane_ctx_get_src(ctx
);
1586 rn
= srcdest_rnode_get(table
, dest_pfx
,
1587 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1596 * Route-update results processing after async dataplane update.
1598 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1600 struct zebra_vrf
*zvrf
= NULL
;
1601 struct route_node
*rn
= NULL
;
1602 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1603 bool is_update
= false;
1604 char dest_str
[PREFIX_STRLEN
] = "";
1605 enum dplane_op_e op
;
1606 enum zebra_dplane_result status
;
1607 const struct prefix
*dest_pfx
, *src_pfx
;
1609 bool fib_changed
= false;
1611 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1612 dest_pfx
= dplane_ctx_get_dest(ctx
);
1614 /* Note well: only capturing the prefix string if debug is enabled here;
1615 * unconditional log messages will have to generate the string.
1617 if (IS_ZEBRA_DEBUG_DPLANE
)
1618 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1620 /* Locate rn and re(s) from ctx */
1621 rn
= rib_find_rn_from_ctx(ctx
);
1623 if (IS_ZEBRA_DEBUG_DPLANE
) {
1624 zlog_debug("Failed to process dplane results: no route for %u:%s",
1625 dplane_ctx_get_vrf(ctx
), dest_str
);
1630 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1632 op
= dplane_ctx_get_op(ctx
);
1633 status
= dplane_ctx_get_status(ctx
);
1635 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1636 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1637 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1638 dplane_op2str(op
), dplane_res2str(status
));
1641 * Update is a bit of a special case, where we may have both old and new
1642 * routes to post-process.
1644 is_update
= dplane_ctx_is_update(ctx
);
1647 * Take a pass through the routes, look for matches with the context
1650 RNODE_FOREACH_RE(rn
, rib
) {
1653 if (rib_route_match_ctx(rib
, ctx
, false))
1657 /* Check for old route match */
1658 if (is_update
&& (old_re
== NULL
)) {
1659 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1663 /* Have we found the routes we need to work on? */
1664 if (re
&& ((!is_update
|| old_re
)))
1668 seq
= dplane_ctx_get_seq(ctx
);
1671 * Check sequence number(s) to detect stale results before continuing
1674 if (re
->dplane_sequence
!= seq
) {
1675 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1676 zlog_debug("%u:%s Stale dplane result for re %p",
1677 dplane_ctx_get_vrf(ctx
),
1680 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1684 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1685 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1686 zlog_debug("%u:%s Stale dplane result for old_re %p",
1687 dplane_ctx_get_vrf(ctx
),
1690 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1694 case DPLANE_OP_ROUTE_INSTALL
:
1695 case DPLANE_OP_ROUTE_UPDATE
:
1696 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1698 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1699 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1702 * On an update operation from the same route type
1703 * context retrieval currently has no way to know
1704 * which was the old and which was the new.
1705 * So don't unset our flags that we just set.
1706 * We know redistribution is ok because the
1707 * old_re in this case is used for nothing
1708 * more than knowing whom to contact if necessary.
1710 if (old_re
&& old_re
!= re
) {
1711 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1712 UNSET_FLAG(old_re
->status
,
1713 ROUTE_ENTRY_INSTALLED
);
1716 /* Update zebra route based on the results in
1717 * the context struct.
1721 rib_update_re_from_ctx(re
, rn
, ctx
);
1724 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1725 zlog_debug("%u:%s no fib change for re",
1732 redistribute_update(dest_pfx
, src_pfx
,
1737 * System routes are weird in that they
1738 * allow multiple to be installed that match
1739 * to the same prefix, so after we get the
1740 * result we need to clean them up so that
1741 * we can actually use them.
1743 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1744 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1745 zebra_rib_fixup_system(rn
);
1750 /* Notify route owner */
1751 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1755 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1756 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1758 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1760 zsend_route_notify_owner(re
, dest_pfx
,
1761 ZAPI_ROUTE_FAIL_INSTALL
);
1763 zlog_warn("%u:%s: Route install failed",
1764 dplane_ctx_get_vrf(ctx
),
1765 prefix2str(dest_pfx
,
1766 dest_str
, sizeof(dest_str
)));
1769 case DPLANE_OP_ROUTE_DELETE
:
1771 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1773 * In the delete case, the zebra core datastructs were
1774 * updated (or removed) at the time the delete was issued,
1775 * so we're just notifying the route owner.
1777 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1779 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1780 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1782 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1788 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1789 zsend_route_notify_owner_ctx(ctx
,
1790 ZAPI_ROUTE_REMOVE_FAIL
);
1792 zlog_warn("%u:%s: Route Deletion failure",
1793 dplane_ctx_get_vrf(ctx
),
1794 prefix2str(dest_pfx
,
1795 dest_str
, sizeof(dest_str
)));
1799 * System routes are weird in that they
1800 * allow multiple to be installed that match
1801 * to the same prefix, so after we get the
1802 * result we need to clean them up so that
1803 * we can actually use them.
1805 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1806 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1807 zebra_rib_fixup_system(rn
);
1813 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1814 zebra_rib_evaluate_mpls(rn
);
1818 route_unlock_node(rn
);
1820 /* Return context to dataplane module */
1821 dplane_ctx_fini(&ctx
);
1825 * Handle notification from async dataplane: the dataplane has detected
1826 * some change to a route, and notifies zebra so that the control plane
1827 * can reflect that change.
1829 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1831 struct route_node
*rn
= NULL
;
1832 struct route_entry
*re
= NULL
;
1833 struct nexthop
*nexthop
;
1834 char dest_str
[PREFIX_STRLEN
] = "";
1835 const struct prefix
*dest_pfx
, *src_pfx
;
1837 bool fib_changed
= false;
1838 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1839 int start_count
, end_count
;
1840 dest_pfx
= dplane_ctx_get_dest(ctx
);
1842 /* Note well: only capturing the prefix string if debug is enabled here;
1843 * unconditional log messages will have to generate the string.
1846 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1848 /* Locate rn and re(s) from ctx */
1849 rn
= rib_find_rn_from_ctx(ctx
);
1852 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1853 dplane_ctx_get_vrf(ctx
), dest_str
);
1858 dest
= rib_dest_from_rnode(rn
);
1859 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1862 zlog_debug("%u:%s Processing dplane notif ctx %p",
1863 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1866 * Take a pass through the routes, look for matches with the context
1869 RNODE_FOREACH_RE(rn
, re
) {
1870 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1874 /* No match? Nothing we can do */
1877 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1878 dplane_ctx_get_vrf(ctx
), dest_str
,
1880 dplane_ctx_get_type(ctx
)));
1885 /* Ensure we clear the QUEUED flag */
1886 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1888 /* Is this a notification that ... matters? We only really care about
1889 * the route that is currently selected for installation.
1891 if (re
!= dest
->selected_fib
) {
1892 /* TODO -- don't skip processing entirely? We might like to
1893 * at least report on the event.
1896 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1897 dplane_ctx_get_vrf(ctx
), dest_str
,
1899 dplane_ctx_get_type(ctx
)));
1903 /* We'll want to determine whether the installation status of the
1904 * route has changed: we'll check the status before processing,
1905 * and then again if there's been a change.
1908 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1909 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1913 /* Update zebra's nexthop FIB flags based on the context struct's
1916 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1920 zlog_debug("%u:%s No change from dplane notification",
1921 dplane_ctx_get_vrf(ctx
), dest_str
);
1927 * Perform follow-up work if the actual status of the prefix
1932 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1933 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1937 /* Various fib transitions: changed nexthops; from installed to
1938 * not-installed; or not-installed to installed.
1940 if (start_count
> 0 && end_count
> 0) {
1942 zlog_debug("%u:%s applied nexthop changes from dplane notification",
1943 dplane_ctx_get_vrf(ctx
), dest_str
);
1945 /* Changed nexthops - update kernel/others */
1946 dplane_route_notif_update(rn
, re
,
1947 DPLANE_OP_ROUTE_UPDATE
, ctx
);
1949 } else if (start_count
== 0 && end_count
> 0) {
1951 zlog_debug("%u:%s installed transition from dplane notification",
1952 dplane_ctx_get_vrf(ctx
), dest_str
);
1954 /* We expect this to be the selected route, so we want
1955 * to tell others about this transition.
1957 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1959 /* Changed nexthops - update kernel/others */
1960 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
1962 /* Redistribute, lsp, and nht update */
1963 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
1965 zebra_rib_evaluate_rn_nexthops(
1966 rn
, zebra_router_get_next_sequence());
1968 zebra_rib_evaluate_mpls(rn
);
1970 } else if (start_count
> 0 && end_count
== 0) {
1972 zlog_debug("%u:%s un-installed transition from dplane notification",
1973 dplane_ctx_get_vrf(ctx
), dest_str
);
1975 /* Transition from _something_ installed to _nothing_
1978 /* We expect this to be the selected route, so we want
1979 * to tell others about this transistion.
1981 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1983 /* Changed nexthops - update kernel/others */
1984 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
1986 /* Redistribute, lsp, and nht update */
1987 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
1989 zebra_rib_evaluate_rn_nexthops(
1990 rn
, zebra_router_get_next_sequence());
1992 zebra_rib_evaluate_mpls(rn
);
1997 route_unlock_node(rn
);
1999 /* Return context to dataplane module */
2000 dplane_ctx_fini(&ctx
);
2003 static void process_subq_nhg(struct listnode
*lnode
)
2005 struct nhg_ctx
*ctx
= NULL
;
2006 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2008 ctx
= listgetdata(lnode
);
2013 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2014 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
2017 rib_nhg_process(ctx
);
2020 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2022 struct route_node
*rnode
= NULL
;
2023 rib_dest_t
*dest
= NULL
;
2024 struct zebra_vrf
*zvrf
= NULL
;
2026 rnode
= listgetdata(lnode
);
2027 dest
= rib_dest_from_rnode(rnode
);
2029 zvrf
= rib_dest_vrf(dest
);
2033 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2034 char buf
[SRCDEST2STR_BUFFER
];
2036 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2037 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2038 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2042 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2043 RIB_ROUTE_QUEUED(qindex
));
2048 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2049 __func__
, rnode
, rnode
->lock
);
2050 zlog_backtrace(LOG_DEBUG
);
2053 route_unlock_node(rnode
);
2056 /* Take a list of route_node structs and return 1, if there was a record
2057 * picked from it and processed by rib_process(). Don't process more,
2058 * than one RN record; operate only in the specified sub-queue.
2060 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2062 struct listnode
*lnode
= listhead(subq
);
2067 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2068 process_subq_nhg(lnode
);
2070 process_subq_route(lnode
, qindex
);
2072 list_delete_node(subq
, lnode
);
2077 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2078 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2080 * is pointed to the meta queue structure.
2082 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2084 struct meta_queue
*mq
= data
;
2086 uint32_t queue_len
, queue_limit
;
2088 /* Ensure there's room for more dataplane updates */
2089 queue_limit
= dplane_get_in_queue_limit();
2090 queue_len
= dplane_get_in_queue_len();
2091 if (queue_len
> queue_limit
) {
2092 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2093 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2094 queue_len
, queue_limit
);
2096 /* Ensure that the meta-queue is actually enqueued */
2097 if (work_queue_empty(zrouter
.ribq
))
2098 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2100 return WQ_QUEUE_BLOCKED
;
2103 for (i
= 0; i
< MQ_SIZE
; i
++)
2104 if (process_subq(mq
->subq
[i
], i
)) {
2108 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2113 * Look into the RN and queue it into the highest priority queue
2114 * at this point in time for processing.
2116 * We will enqueue a route node only once per invocation.
2118 * There are two possibilities here that should be kept in mind.
2119 * If the original invocation has not been pulled off for processing
2120 * yet, A subsuquent invocation can have a route entry with a better
2121 * meta queue index value and we can have a situation where
2122 * we might have the same node enqueued 2 times. Not necessarily
2123 * an optimal situation but it should be ok.
2125 * The other possibility is that the original invocation has not
2126 * been pulled off for processing yet, A subsusquent invocation
2127 * doesn't have a route_entry with a better meta-queue and the
2128 * original metaqueue index value will win and we'll end up with
2129 * the route node enqueued once.
2131 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2133 struct route_node
*rn
= NULL
;
2134 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2135 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2137 rn
= (struct route_node
*)data
;
2139 RNODE_FOREACH_RE (rn
, curr_re
) {
2140 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2142 if (curr_qindex
<= qindex
) {
2144 qindex
= curr_qindex
;
2151 /* Invariant: at this point we always have rn->info set. */
2152 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2153 RIB_ROUTE_QUEUED(qindex
))) {
2154 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2155 rnode_debug(rn
, re
->vrf_id
,
2156 "rn %p is already queued in sub-queue %u",
2157 (void *)rn
, qindex
);
2161 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2162 listnode_add(mq
->subq
[qindex
], rn
);
2163 route_lock_node(rn
);
2166 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2167 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2168 (void *)rn
, qindex
);
2173 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2175 struct nhg_ctx
*ctx
= NULL
;
2176 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2178 ctx
= (struct nhg_ctx
*)data
;
2183 listnode_add(mq
->subq
[qindex
], ctx
);
2186 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2187 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2193 static int mq_add_handler(void *data
,
2194 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2196 if (zrouter
.ribq
== NULL
) {
2197 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2198 "%s: work_queue does not exist!", __func__
);
2203 * The RIB queue should normally be either empty or holding the only
2204 * work_queue_item element. In the latter case this element would
2205 * hold a pointer to the meta queue structure, which must be used to
2206 * actually queue the route nodes to process. So create the MQ
2207 * holder, if necessary, then push the work into it in any case.
2208 * This semantics was introduced after 0.99.9 release.
2210 if (work_queue_empty(zrouter
.ribq
))
2211 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2213 return mq_add_func(zrouter
.mq
, data
);
2216 /* Add route_node to work queue and schedule processing */
2217 int rib_queue_add(struct route_node
*rn
)
2221 /* Pointless to queue a route_node with no RIB entries to add or remove
2223 if (!rnode_to_ribs(rn
)) {
2224 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2225 __func__
, (void *)rn
, rn
->lock
);
2226 zlog_backtrace(LOG_DEBUG
);
2230 return mq_add_handler(rn
, &rib_meta_queue_add
);
2233 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2237 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2240 /* Create new meta queue.
2241 A destructor function doesn't seem to be necessary here.
2243 static struct meta_queue
*meta_queue_new(void)
2245 struct meta_queue
*new;
2248 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2250 for (i
= 0; i
< MQ_SIZE
; i
++) {
2251 new->subq
[i
] = list_new();
2252 assert(new->subq
[i
]);
2258 void meta_queue_free(struct meta_queue
*mq
)
2262 for (i
= 0; i
< MQ_SIZE
; i
++)
2263 list_delete(&mq
->subq
[i
]);
2265 XFREE(MTYPE_WORK_QUEUE
, mq
);
2268 /* initialise zebra rib work queue */
2269 static void rib_queue_init(void)
2271 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2272 "route_node processing"))) {
2273 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2274 "%s: could not initialise work queue!", __func__
);
2278 /* fill in the work queue spec */
2279 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2280 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2281 zrouter
.ribq
->spec
.completion_func
= NULL
;
2282 /* XXX: TODO: These should be runtime configurable via vty */
2283 zrouter
.ribq
->spec
.max_retries
= 3;
2284 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2285 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2287 if (!(zrouter
.mq
= meta_queue_new())) {
2288 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2289 "%s: could not initialise meta queue!", __func__
);
2295 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2299 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2300 rnh_list_init(&dest
->nht
);
2301 route_lock_node(rn
); /* rn route table reference */
2308 /* RIB updates are processed via a queue of pointers to route_nodes.
2310 * The queue length is bounded by the maximal size of the routing table,
2311 * as a route_node will not be requeued, if already queued.
2313 * REs are submitted via rib_addnode or rib_delnode which set minimal
2314 * state, or static_install_route (when an existing RE is updated)
2315 * and then submit route_node to queue for best-path selection later.
2316 * Order of add/delete state changes are preserved for any given RE.
2318 * Deleted REs are reaped during best-path selection.
2321 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2322 * |-------->| | best RE, if required
2324 * static_install->|->rib_addqueue...... -> rib_process
2326 * |-------->| |-> rib_unlink
2327 * |-> set ROUTE_ENTRY_REMOVE |
2328 * rib_delnode (RE freed)
2330 * The 'info' pointer of a route_node points to a rib_dest_t
2331 * ('dest'). Queueing state for a route_node is kept on the dest. The
2332 * dest is created on-demand by rib_link() and is kept around at least
2333 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2335 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2337 * - route_nodes: refcounted by:
2338 * - dest attached to route_node:
2339 * - managed by: rib_link/rib_gc_dest
2340 * - route_node processing queue
2341 * - managed by: rib_addqueue, rib_process.
2345 /* Add RE to head of the route node. */
2346 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2350 const char *rmap_name
;
2354 dest
= rib_dest_from_rnode(rn
);
2356 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2357 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2359 dest
= zebra_rib_create_dest(rn
);
2362 re_list_add_head(&dest
->routes
, re
);
2364 afi
= (rn
->p
.family
== AF_INET
)
2366 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2367 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2368 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2370 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2371 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2376 static void rib_addnode(struct route_node
*rn
,
2377 struct route_entry
*re
, int process
)
2379 /* RE node has been un-removed before route-node is processed.
2380 * route_node must hence already be on the queue for processing..
2382 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2383 if (IS_ZEBRA_DEBUG_RIB
)
2384 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2385 (void *)rn
, (void *)re
);
2387 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2390 rib_link(rn
, re
, process
);
2396 * Detach a rib structure from a route_node.
2398 * Note that a call to rib_unlink() should be followed by a call to
2399 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2400 * longer required to be deleted.
2402 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2405 struct nhg_hash_entry
*nhe
= NULL
;
2409 if (IS_ZEBRA_DEBUG_RIB
)
2410 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2413 dest
= rib_dest_from_rnode(rn
);
2415 re_list_del(&dest
->routes
, re
);
2417 if (dest
->selected_fib
== re
)
2418 dest
->selected_fib
= NULL
;
2421 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2423 zebra_nhg_decrement_ref(nhe
);
2425 nexthop_group_delete(&re
->ng
);
2427 nexthops_free(re
->fib_ng
.nexthop
);
2429 XFREE(MTYPE_RE
, re
);
2432 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2436 if (IS_ZEBRA_DEBUG_RIB
)
2437 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2438 (void *)rn
, (void *)re
);
2439 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2441 afi
= (rn
->p
.family
== AF_INET
)
2443 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2444 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2445 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2447 zebra_del_import_table_entry(zvrf
, rn
, re
);
2448 /* Just clean up if non main table */
2449 if (IS_ZEBRA_DEBUG_RIB
) {
2450 char buf
[SRCDEST2STR_BUFFER
];
2451 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2452 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2453 re
->vrf_id
, buf
, rn
, re
,
2454 zebra_route_string(re
->type
));
2463 /* This function dumps the contents of a given RE entry into
2464 * standard debug log. Calling function name and IP prefix in
2465 * question are passed as 1st and 2nd arguments.
2468 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2469 union prefixconstptr src_pp
,
2470 const struct route_entry
*re
)
2472 const struct prefix
*src_p
= src_pp
.p
;
2473 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2474 char straddr
[PREFIX_STRLEN
];
2475 char srcaddr
[PREFIX_STRLEN
];
2476 char nhname
[PREFIX_STRLEN
];
2477 struct nexthop
*nexthop
;
2479 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2480 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2481 is_srcdst
? " from " : "",
2482 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2485 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2486 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2489 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2490 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2491 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2492 nexthop_group_nexthop_num(re
->ng
),
2493 nexthop_group_active_nexthop_num(re
->ng
));
2495 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
)) {
2496 struct interface
*ifp
;
2497 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2499 switch (nexthop
->type
) {
2500 case NEXTHOP_TYPE_BLACKHOLE
:
2501 sprintf(nhname
, "Blackhole");
2503 case NEXTHOP_TYPE_IFINDEX
:
2504 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2506 sprintf(nhname
, "%s", ifp
? ifp
->name
: "Unknown");
2508 case NEXTHOP_TYPE_IPV4
:
2510 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2511 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
,
2514 case NEXTHOP_TYPE_IPV6
:
2515 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2516 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
,
2520 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2521 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2522 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2524 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2527 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2530 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2533 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2536 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2539 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2543 zlog_debug("%s: dump complete", straddr
);
2546 /* This is an exported helper to rtm_read() to dump the strange
2547 * RE entry found by rib_lookup_ipv4_route()
2550 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2552 struct route_table
*table
;
2553 struct route_node
*rn
;
2554 struct route_entry
*re
;
2555 char prefix_buf
[INET_ADDRSTRLEN
];
2558 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2560 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2561 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2566 /* Scan the RIB table for exactly matching RE entry. */
2567 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2569 /* No route for this prefix. */
2571 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2572 prefix2str((struct prefix
*)p
, prefix_buf
,
2573 sizeof(prefix_buf
)));
2578 route_unlock_node(rn
);
2581 RNODE_FOREACH_RE (rn
, re
) {
2582 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2584 (void *)rn
, (void *)re
,
2585 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2588 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2591 route_entry_dump(p
, NULL
, re
);
2595 /* Check if requested address assignment will fail due to another
2596 * route being installed by zebra in FIB already. Take necessary
2597 * actions, if needed: remove such a route from FIB and deSELECT
2598 * corresponding RE entry. Then put affected RN into RIBQ head.
2600 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2602 struct route_table
*table
;
2603 struct route_node
*rn
;
2606 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2607 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2608 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2613 /* No matches would be the simplest case. */
2614 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2618 route_unlock_node(rn
);
2620 dest
= rib_dest_from_rnode(rn
);
2621 /* Check all RE entries. In case any changes have to be done, requeue
2622 * the RN into RIBQ head. If the routing message about the new connected
2623 * route (generated by the IP address we are going to assign very soon)
2624 * comes before the RIBQ is processed, the new RE entry will join
2625 * RIBQ record already on head. This is necessary for proper
2627 * of the rest of the RE.
2629 if (dest
->selected_fib
) {
2630 if (IS_ZEBRA_DEBUG_RIB
) {
2631 char buf
[PREFIX_STRLEN
];
2633 zlog_debug("%u:%s: freeing way for connected prefix",
2634 dest
->selected_fib
->vrf_id
,
2635 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2636 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2638 rib_uninstall(rn
, dest
->selected_fib
);
2643 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2644 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2646 struct nhg_hash_entry
*nhe
= NULL
;
2647 struct route_table
*table
;
2648 struct route_node
*rn
;
2649 struct route_entry
*same
= NULL
;
2655 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2658 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2662 nexthop_group_delete(&re
->ng
);
2663 XFREE(MTYPE_RE
, re
);
2668 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2672 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2673 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
2675 XFREE(MTYPE_RE
, re
);
2679 nhe
= zebra_nhg_rib_find(0, re
->ng
, afi
);
2682 * The nexthops got copied over into an nhe,
2685 nexthop_group_delete(&re
->ng
);
2688 char buf
[PREFIX_STRLEN
] = "";
2689 char buf2
[PREFIX_STRLEN
] = "";
2692 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2693 "Zebra failed to find or create a nexthop hash entry for %s%s%s",
2694 prefix2str(p
, buf
, sizeof(buf
)),
2695 src_p
? " from " : "",
2696 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
2699 XFREE(MTYPE_RE
, re
);
2705 * Attach the re to the nhe's nexthop group.
2707 * TODO: This will need to change when we start getting IDs from upper
2708 * level protocols, as the refcnt might be wrong, since it checks
2709 * if old_id != new_id.
2711 zebra_nhg_re_update_ref(re
, nhe
);
2713 /* Make it sure prefixlen is applied to the prefix. */
2716 apply_mask_ipv6(src_p
);
2718 /* Set default distance by route type. */
2719 if (re
->distance
== 0)
2720 re
->distance
= route_distance(re
->type
);
2722 /* Lookup route node.*/
2723 rn
= srcdest_rnode_get(table
, p
, src_p
);
2726 * If same type of route are installed, treat it as a implicit
2728 * If the user has specified the No route replace semantics
2729 * for the install don't do a route replace.
2731 RNODE_FOREACH_RE (rn
, same
) {
2732 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2735 if (same
->type
!= re
->type
)
2737 if (same
->instance
!= re
->instance
)
2739 if (same
->type
== ZEBRA_ROUTE_KERNEL
2740 && same
->metric
!= re
->metric
)
2743 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2744 same
->distance
!= re
->distance
)
2748 * We should allow duplicate connected routes
2749 * because of IPv6 link-local routes and unnumbered
2750 * interfaces on Linux.
2752 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2756 /* If this route is kernel/connected route, notify the dataplane. */
2757 if (RIB_SYSTEM_ROUTE(re
)) {
2758 /* Notify dataplane */
2759 dplane_sys_route_add(rn
, re
);
2762 /* Link new re to node.*/
2763 if (IS_ZEBRA_DEBUG_RIB
) {
2764 rnode_debug(rn
, re
->vrf_id
,
2765 "Inserting route rn %p, re %p (%s) existing %p",
2766 rn
, re
, zebra_route_string(re
->type
), same
);
2768 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2769 route_entry_dump(p
, src_p
, re
);
2772 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2773 rib_addnode(rn
, re
, 1);
2776 /* Free implicit route.*/
2778 rib_delnode(rn
, same
);
2782 route_unlock_node(rn
);
2786 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2787 unsigned short instance
, int flags
, struct prefix
*p
,
2788 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2789 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
2790 uint8_t distance
, bool fromkernel
)
2792 struct route_table
*table
;
2793 struct route_node
*rn
;
2794 struct route_entry
*re
;
2795 struct route_entry
*fib
= NULL
;
2796 struct route_entry
*same
= NULL
;
2797 struct nexthop
*rtnh
;
2798 char buf2
[INET6_ADDRSTRLEN
];
2801 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2804 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
2812 apply_mask_ipv6(src_p
);
2814 /* Lookup route node. */
2815 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2817 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2819 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2820 if (src_p
&& src_p
->prefixlen
)
2821 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2825 if (IS_ZEBRA_DEBUG_RIB
) {
2826 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2828 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
2829 vrf
->name
, table_id
, dst_buf
,
2830 (src_buf
[0] != '\0') ? " from " : "",
2836 dest
= rib_dest_from_rnode(rn
);
2837 fib
= dest
->selected_fib
;
2839 /* Lookup same type route. */
2840 RNODE_FOREACH_RE (rn
, re
) {
2841 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2844 if (re
->type
!= type
)
2846 if (re
->instance
!= instance
)
2848 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2849 distance
!= re
->distance
)
2852 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2854 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
->nexthop
)
2855 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2856 if (rtnh
->ifindex
!= nh
->ifindex
)
2862 /* Make sure that the route found has the same gateway. */
2863 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
2872 for (ALL_NEXTHOPS_PTR(re
->ng
, rtnh
)) {
2874 * No guarantee all kernel send nh with labels
2877 if (nexthop_same_no_labels(rtnh
, nh
)) {
2886 /* If same type of route can't be found and this message is from
2890 * In the past(HA!) we could get here because
2891 * we were receiving a route delete from the
2892 * kernel and we're not marking the proto
2893 * as coming from it's appropriate originator.
2894 * Now that we are properly noticing the fact
2895 * that the kernel has deleted our route we
2896 * are not going to get called in this path
2897 * I am going to leave this here because
2898 * this might still work this way on non-linux
2899 * platforms as well as some weird state I have
2900 * not properly thought of yet.
2901 * If we can show that this code path is
2902 * dead then we can remove it.
2904 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2905 if (IS_ZEBRA_DEBUG_RIB
) {
2906 rnode_debug(rn
, vrf_id
,
2907 "rn %p, re %p (%s) was deleted from kernel, adding",
2909 zebra_route_string(fib
->type
));
2912 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2914 for (rtnh
= fib
->ng
->nexthop
; rtnh
;
2916 UNSET_FLAG(rtnh
->flags
,
2920 * This is a non FRR route
2921 * as such we should mark
2924 dest
->selected_fib
= NULL
;
2926 /* This means someone else, other than Zebra,
2928 * a Zebra router from the kernel. We will add
2930 rib_install_kernel(rn
, fib
, NULL
);
2933 if (IS_ZEBRA_DEBUG_RIB
) {
2937 "via %s ifindex %d type %d "
2938 "doesn't exist in rib",
2939 inet_ntop(afi2family(afi
),
2946 "type %d doesn't exist in rib",
2949 route_unlock_node(rn
);
2955 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2957 rib_install_kernel(rn
, same
, NULL
);
2958 route_unlock_node(rn
);
2963 /* Special handling for IPv4 or IPv6 routes sourced from
2964 * EVPN - the nexthop (and associated MAC) need to be
2965 * uninstalled if no more refs.
2967 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2968 struct nexthop
*tmp_nh
;
2970 for (ALL_NEXTHOPS_PTR(re
->ng
, tmp_nh
)) {
2971 struct ipaddr vtep_ip
;
2973 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2974 if (afi
== AFI_IP
) {
2975 vtep_ip
.ipa_type
= IPADDR_V4
;
2976 memcpy(&(vtep_ip
.ipaddr_v4
),
2977 &(tmp_nh
->gate
.ipv4
),
2978 sizeof(struct in_addr
));
2980 vtep_ip
.ipa_type
= IPADDR_V6
;
2981 memcpy(&(vtep_ip
.ipaddr_v6
),
2982 &(tmp_nh
->gate
.ipv6
),
2983 sizeof(struct in6_addr
));
2985 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2990 /* Notify dplane if system route changes */
2991 if (RIB_SYSTEM_ROUTE(re
))
2992 dplane_sys_route_del(rn
, same
);
2994 rib_delnode(rn
, same
);
2997 route_unlock_node(rn
);
3002 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3003 unsigned short instance
, int flags
, struct prefix
*p
,
3004 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3005 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3006 uint8_t distance
, route_tag_t tag
)
3008 struct route_entry
*re
= NULL
;
3009 struct nexthop
*nexthop
= NULL
;
3011 /* Allocate new route_entry structure. */
3012 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3014 re
->instance
= instance
;
3015 re
->distance
= distance
;
3017 re
->metric
= metric
;
3019 re
->table
= table_id
;
3020 re
->vrf_id
= vrf_id
;
3021 re
->uptime
= monotime(NULL
);
3023 re
->nhe_id
= nhe_id
;
3026 re
->ng
= nexthop_group_new();
3029 nexthop
= nexthop_new();
3031 route_entry_nexthop_add(re
, nexthop
);
3034 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
3037 static const char *rib_update_event2str(rib_update_event_t event
)
3039 const char *ret
= "UNKNOWN";
3042 case RIB_UPDATE_KERNEL
:
3043 ret
= "RIB_UPDATE_KERNEL";
3045 case RIB_UPDATE_RMAP_CHANGE
:
3046 ret
= "RIB_UPDATE_RMAP_CHANGE";
3048 case RIB_UPDATE_OTHER
:
3049 ret
= "RIB_UPDATE_OTHER";
3051 case RIB_UPDATE_MAX
:
3059 /* Schedule route nodes to be processed if they match the type */
3060 static void rib_update_route_node(struct route_node
*rn
, int type
)
3062 struct route_entry
*re
, *next
;
3063 bool re_changed
= false;
3065 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3066 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3067 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3076 /* Schedule routes of a particular table (address-family) based on event. */
3077 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3079 struct route_node
*rn
;
3081 if (IS_ZEBRA_DEBUG_EVENT
) {
3082 struct zebra_vrf
*zvrf
;
3085 zvrf
= table
->info
? ((rib_table_info_t
*)table
->info
)->zvrf
3087 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3089 zlog_debug("%s: %s VRF %s Table %u event %s", __func__
,
3090 table
->info
? afi2str(
3091 ((rib_table_info_t
*)table
->info
)->afi
)
3093 vrf
? vrf
->name
: "Unknown",
3094 zvrf
? zvrf
->table_id
: 0,
3095 rib_update_event2str(event
));
3098 /* Walk all routes and queue for processing, if appropriate for
3099 * the trigger event.
3101 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3103 * If we are looking at a route node and the node
3104 * has already been queued we don't
3105 * need to queue it up again
3108 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3109 RIB_ROUTE_ANY_QUEUED
))
3113 case RIB_UPDATE_KERNEL
:
3114 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3116 case RIB_UPDATE_RMAP_CHANGE
:
3117 case RIB_UPDATE_OTHER
:
3118 rib_update_route_node(rn
, ZEBRA_ROUTE_ALL
);
3126 static void rib_update_handle_vrf(vrf_id_t vrf_id
, rib_update_event_t event
)
3128 struct route_table
*table
;
3130 if (IS_ZEBRA_DEBUG_EVENT
)
3131 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3132 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3134 /* Process routes of interested address-families. */
3135 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3137 rib_update_table(table
, event
);
3139 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3141 rib_update_table(table
, event
);
3144 static void rib_update_handle_vrf_all(rib_update_event_t event
)
3146 struct zebra_router_table
*zrt
;
3148 if (IS_ZEBRA_DEBUG_EVENT
)
3149 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3150 rib_update_event2str(event
));
3152 /* Just iterate over all the route tables, rather than vrf lookups */
3153 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3154 rib_update_table(zrt
->table
, event
);
3157 struct rib_update_ctx
{
3158 rib_update_event_t event
;
3163 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3164 rib_update_event_t event
)
3166 struct rib_update_ctx
*ctx
;
3168 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3171 ctx
->vrf_id
= vrf_id
;
3176 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3178 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3183 static int rib_update_handler(struct thread
*thread
)
3185 struct rib_update_ctx
*ctx
;
3187 ctx
= THREAD_ARG(thread
);
3190 rib_update_handle_vrf_all(ctx
->event
);
3192 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
);
3194 rib_update_ctx_fini(&ctx
);
3200 * Thread list to ensure we don't schedule a ton of events
3201 * if interfaces are flapping for instance.
3203 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3205 /* Schedule a RIB update event for specific vrf */
3206 void rib_update_vrf(vrf_id_t vrf_id
, rib_update_event_t event
)
3208 struct rib_update_ctx
*ctx
;
3210 ctx
= rib_update_ctx_init(vrf_id
, event
);
3212 /* Don't worry about making sure multiple rib updates for specific vrf
3213 * are scheduled at once for now. If it becomes a problem, we can use a
3214 * lookup of some sort to keep track of running threads via t_vrf_id
3215 * like how we are doing it in t_rib_update_threads[].
3217 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0, NULL
);
3219 if (IS_ZEBRA_DEBUG_EVENT
)
3220 zlog_debug("%s: Scheduled VRF %s, event %s", __func__
,
3221 vrf_id_to_name(ctx
->vrf_id
),
3222 rib_update_event2str(event
));
3225 /* Schedule a RIB update event for all vrfs */
3226 void rib_update(rib_update_event_t event
)
3228 struct rib_update_ctx
*ctx
;
3230 ctx
= rib_update_ctx_init(0, event
);
3232 ctx
->vrf_all
= true;
3234 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3235 &t_rib_update_threads
[event
]))
3236 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3237 else if (IS_ZEBRA_DEBUG_EVENT
)
3238 zlog_debug("%s: Schedued VRF (ALL), event %s", __func__
,
3239 rib_update_event2str(event
));
3242 /* Delete self installed routes after zebra is relaunched. */
3243 void rib_sweep_table(struct route_table
*table
)
3245 struct route_node
*rn
;
3246 struct route_entry
*re
;
3247 struct route_entry
*next
;
3248 struct nexthop
*nexthop
;
3253 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3254 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3256 if (IS_ZEBRA_DEBUG_RIB
)
3257 route_entry_dump(&rn
->p
, NULL
, re
);
3259 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3262 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3266 * If routes are older than startup_time then
3267 * we know we read them in from the kernel.
3268 * As such we can safely remove them.
3270 if (zrouter
.startup_time
< re
->uptime
)
3274 * So we are starting up and have received
3275 * routes from the kernel that we have installed
3276 * from a previous run of zebra but not cleaned
3277 * up ( say a kill -9 )
3278 * But since we haven't actually installed
3279 * them yet( we received them from the kernel )
3280 * we don't think they are active.
3281 * So let's pretend they are active to actually
3283 * In all honesty I'm not sure if we should
3284 * mark them as active when we receive them
3285 * This is startup only so probably ok.
3287 * If we ever decide to move rib_sweep_table
3288 * to a different spot (ie startup )
3289 * this decision needs to be revisited
3291 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3292 for (ALL_NEXTHOPS_PTR(re
->ng
, nexthop
))
3293 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3295 rib_uninstall_kernel(rn
, re
);
3296 rib_delnode(rn
, re
);
3301 /* Sweep all RIB tables. */
3302 int rib_sweep_route(struct thread
*t
)
3305 struct zebra_vrf
*zvrf
;
3307 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3308 if ((zvrf
= vrf
->info
) == NULL
)
3311 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3312 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3315 zebra_router_sweep_route();
3316 zebra_router_sweep_nhgs();
3321 /* Remove specific by protocol routes from 'table'. */
3322 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3323 struct route_table
*table
)
3325 struct route_node
*rn
;
3326 struct route_entry
*re
;
3327 struct route_entry
*next
;
3328 unsigned long n
= 0;
3331 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3332 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3333 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3335 if (re
->type
== proto
3336 && re
->instance
== instance
) {
3337 rib_delnode(rn
, re
);
3344 /* Remove specific by protocol routes. */
3345 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3348 struct zebra_vrf
*zvrf
;
3349 struct other_route_table
*ort
;
3350 unsigned long cnt
= 0;
3352 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3357 cnt
+= rib_score_proto_table(proto
, instance
,
3358 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3359 + rib_score_proto_table(
3361 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3363 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3364 rib_score_proto_table(proto
, instance
, ort
->table
);
3370 /* Close RIB and clean up kernel routes. */
3371 void rib_close_table(struct route_table
*table
)
3373 struct route_node
*rn
;
3374 rib_table_info_t
*info
;
3380 info
= route_table_get_info(table
);
3382 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3383 dest
= rib_dest_from_rnode(rn
);
3385 if (dest
&& dest
->selected_fib
) {
3386 if (info
->safi
== SAFI_UNICAST
)
3387 hook_call(rib_update
, rn
, NULL
);
3389 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3390 dest
->selected_fib
= NULL
;
3396 * Handler for async dataplane results after a pseudowire installation
3398 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3400 struct zebra_pw
*pw
;
3401 struct zebra_vrf
*vrf
;
3403 /* The pseudowire code assumes success - we act on an error
3404 * result for installation attempts here.
3406 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3409 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3410 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3411 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3413 zebra_pw_install_failure(pw
);
3423 * Handle results from the dataplane system. Dequeue update context
3424 * structs, dispatch to appropriate internal handlers.
3426 static int rib_process_dplane_results(struct thread
*thread
)
3428 struct zebra_dplane_ctx
*ctx
;
3429 struct dplane_ctx_q ctxlist
;
3430 bool shut_p
= false;
3432 /* Dequeue a list of completed updates with one lock/unlock cycle */
3435 TAILQ_INIT(&ctxlist
);
3437 /* Take lock controlling queue of results */
3438 frr_with_mutex(&dplane_mutex
) {
3439 /* Dequeue list of context structs */
3440 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3443 /* Dequeue context block */
3444 ctx
= dplane_ctx_dequeue(&ctxlist
);
3446 /* If we've emptied the results queue, we're done */
3450 /* If zebra is shutting down, avoid processing results,
3451 * just drain the results queue.
3453 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3454 memory_order_relaxed
);
3457 dplane_ctx_fini(&ctx
);
3459 ctx
= dplane_ctx_dequeue(&ctxlist
);
3466 switch (dplane_ctx_get_op(ctx
)) {
3467 case DPLANE_OP_ROUTE_INSTALL
:
3468 case DPLANE_OP_ROUTE_UPDATE
:
3469 case DPLANE_OP_ROUTE_DELETE
:
3471 /* Bit of special case for route updates
3472 * that were generated by async notifications:
3473 * we don't want to continue processing these
3476 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3477 rib_process_result(ctx
);
3479 dplane_ctx_fini(&ctx
);
3483 case DPLANE_OP_ROUTE_NOTIFY
:
3484 rib_process_dplane_notify(ctx
);
3487 case DPLANE_OP_NH_INSTALL
:
3488 case DPLANE_OP_NH_UPDATE
:
3489 case DPLANE_OP_NH_DELETE
:
3490 zebra_nhg_dplane_result(ctx
);
3493 case DPLANE_OP_LSP_INSTALL
:
3494 case DPLANE_OP_LSP_UPDATE
:
3495 case DPLANE_OP_LSP_DELETE
:
3497 /* Bit of special case for LSP updates
3498 * that were generated by async notifications:
3499 * we don't want to continue processing these.
3501 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3502 zebra_mpls_lsp_dplane_result(ctx
);
3504 dplane_ctx_fini(&ctx
);
3508 case DPLANE_OP_LSP_NOTIFY
:
3509 zebra_mpls_process_dplane_notify(ctx
);
3512 case DPLANE_OP_PW_INSTALL
:
3513 case DPLANE_OP_PW_UNINSTALL
:
3514 handle_pw_result(ctx
);
3517 case DPLANE_OP_SYS_ROUTE_ADD
:
3518 case DPLANE_OP_SYS_ROUTE_DELETE
:
3519 /* No further processing in zebra for these. */
3520 dplane_ctx_fini(&ctx
);
3523 case DPLANE_OP_MAC_INSTALL
:
3524 case DPLANE_OP_MAC_DELETE
:
3525 zebra_vxlan_handle_result(ctx
);
3528 /* Some op codes not handled here */
3529 case DPLANE_OP_ADDR_INSTALL
:
3530 case DPLANE_OP_ADDR_UNINSTALL
:
3531 case DPLANE_OP_NEIGH_INSTALL
:
3532 case DPLANE_OP_NEIGH_UPDATE
:
3533 case DPLANE_OP_NEIGH_DELETE
:
3534 case DPLANE_OP_VTEP_ADD
:
3535 case DPLANE_OP_VTEP_DELETE
:
3536 case DPLANE_OP_NONE
:
3537 /* Don't expect this: just return the struct? */
3538 dplane_ctx_fini(&ctx
);
3541 } /* Dispatch by op code */
3543 ctx
= dplane_ctx_dequeue(&ctxlist
);
3552 * Results are returned from the dataplane subsystem, in the context of
3553 * the dataplane pthread. We enqueue the results here for processing by
3554 * the main thread later.
3556 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3558 /* Take lock controlling queue of results */
3559 frr_with_mutex(&dplane_mutex
) {
3560 /* Enqueue context blocks */
3561 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3564 /* Ensure event is signalled to zebra main pthread */
3565 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3572 * Ensure there are no empty slots in the route_info array.
3573 * Every route type in zebra should be present there.
3575 static void check_route_info(void)
3577 int len
= array_size(route_info
);
3580 * ZEBRA_ROUTE_SYSTEM is special cased since
3581 * its key is 0 anyway.
3583 * ZEBRA_ROUTE_ALL is also ignored.
3585 for (int i
= 0; i
< len
; i
++) {
3586 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3588 assert(route_info
[i
].key
);
3589 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3593 /* Routing information base initialize. */
3600 /* Init dataplane, and register for results */
3601 pthread_mutex_init(&dplane_mutex
, NULL
);
3602 TAILQ_INIT(&rib_dplane_q
);
3603 zebra_dplane_init(rib_dplane_results
);
3609 * Get the first vrf id that is greater than the given vrf id if any.
3611 * Returns true if a vrf id was found, false otherwise.
3613 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3617 vrf
= vrf_lookup_by_id(vrf_id
);
3619 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3621 *next_id_p
= vrf
->vrf_id
;
3630 * rib_tables_iter_next
3632 * Returns the next table in the iteration.
3634 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3636 struct route_table
*table
;
3639 * Array that helps us go over all AFI/SAFI combinations via one
3646 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3647 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3648 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3653 switch (iter
->state
) {
3655 case RIB_TABLES_ITER_S_INIT
:
3656 iter
->vrf_id
= VRF_DEFAULT
;
3657 iter
->afi_safi_ix
= -1;
3661 case RIB_TABLES_ITER_S_ITERATING
:
3662 iter
->afi_safi_ix
++;
3665 while (iter
->afi_safi_ix
3666 < (int)array_size(afi_safis
)) {
3667 table
= zebra_vrf_table(
3668 afi_safis
[iter
->afi_safi_ix
].afi
,
3669 afi_safis
[iter
->afi_safi_ix
].safi
,
3674 iter
->afi_safi_ix
++;
3678 * Found another table in this vrf.
3684 * Done with all tables in the current vrf, go to the
3688 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3691 iter
->afi_safi_ix
= 0;
3696 case RIB_TABLES_ITER_S_DONE
:
3701 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3703 iter
->state
= RIB_TABLES_ITER_S_DONE
;