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"
43 #include "zebra/zebra_router.h"
44 #include "zebra/connected.h"
45 #include "zebra/debug.h"
46 #include "zebra/interface.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/rib.h"
50 #include "zebra/zapi_msg.h"
51 #include "zebra/zebra_errors.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_MGROUP(ZEBRA
, "zebra");
62 DEFINE_MTYPE(ZEBRA
, RE
, "Route Entry");
63 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_DEST
, "RIB destination");
64 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
65 DEFINE_MTYPE_STATIC(ZEBRA
, WQ_NHG_WRAPPER
, "WQ nhg wrapper");
68 * Event, list, and mutex for delivery of dataplane results
70 static pthread_mutex_t dplane_mutex
;
71 static struct thread
*t_dplane
;
72 static struct dplane_ctx_q rib_dplane_q
;
74 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
77 /* Should we allow non Quagga processes to delete our routes */
78 extern int allow_delete
;
80 /* Each route type's string and default distance value. */
85 } route_info
[ZEBRA_ROUTE_MAX
] = {
86 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Uneeded for nhg's */, 0},
87 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 6},
88 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 2},
89 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 1},
90 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 3},
91 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 4},
92 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 4},
93 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 4},
94 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 4},
95 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 4},
96 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 5},
97 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 6},
98 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 4},
99 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 4},
100 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 6},
101 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 6},
102 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 3},
103 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 6},
104 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 5},
105 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 5},
106 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 5},
107 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 5},
108 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 5},
109 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 4},
110 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 6},
111 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 6},
112 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 6},
113 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 4},
114 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 6},
115 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, 6},
116 /* Any new route type added to zebra, should be mirrored here */
118 /* no entry/default: 150 */
121 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
122 * from the OS, and an 'nhe' is a nhe update.
124 struct wq_nhg_wrapper
{
128 struct nhg_hash_entry
*nhe
;
132 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
133 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
135 static void PRINTFRR(5, 6)
136 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
137 int priority
, const char *msgfmt
, ...)
139 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
144 va_start(ap
, msgfmt
);
145 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
149 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
150 rib_dest_t
*dest
= NULL
;
151 struct route_entry
*re
= NULL
;
153 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
155 if (info
->safi
== SAFI_MULTICAST
)
156 strlcat(buf
, " (MRIB)", sizeof(buf
));
158 dest
= rib_dest_from_rnode(rn
);
160 re
= re_list_first(&dest
->routes
);
164 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
167 zlog(priority
, "%s: (%u:%u):%s: %s", _func
, vrf_id
, table
, buf
, msgbuf
);
170 static char *_dump_re_status(const struct route_entry
*re
, char *buf
,
173 if (re
->status
== 0) {
174 snprintfrr(buf
, len
, "None ");
179 buf
, len
, "%s%s%s%s%s%s%s",
180 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
) ? "Removed " : "",
181 CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) ? "Changed " : "",
182 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
185 CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
) ? "Queued " : "",
186 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
188 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
189 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
194 #define rnode_debug(node, vrf_id, ...) \
195 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
196 #define rnode_info(node, ...) \
197 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
199 uint8_t route_distance(int type
)
203 if ((unsigned)type
>= array_size(route_info
))
206 distance
= route_info
[type
].distance
;
211 int is_zebra_valid_kernel_table(uint32_t table_id
)
214 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
215 || (table_id
== RT_TABLE_COMPAT
))
222 int is_zebra_main_routing_table(uint32_t table_id
)
224 if (table_id
== RT_TABLE_MAIN
)
229 int zebra_check_addr(const struct prefix
*p
)
231 if (p
->family
== AF_INET
) {
234 addr
= p
->u
.prefix4
.s_addr
;
237 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
238 || IPV4_LINKLOCAL(addr
))
241 if (p
->family
== AF_INET6
) {
242 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
244 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
251 * copy_nexthop - copy a nexthop to the rib structure.
253 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
255 assert(!re
->nhe
->nhg
.nexthop
);
256 copy_nexthops(&re
->nhe
->nhg
.nexthop
, nh
, NULL
);
259 static void route_entry_attach_ref(struct route_entry
*re
,
260 struct nhg_hash_entry
*new)
263 re
->nhe_id
= new->id
;
265 zebra_nhg_increment_ref(new);
268 int route_entry_update_nhe(struct route_entry
*re
,
269 struct nhg_hash_entry
*new_nhghe
)
271 struct nhg_hash_entry
*old
;
274 if (new_nhghe
== NULL
) {
276 zebra_nhg_decrement_ref(re
->nhe
);
281 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
284 route_entry_attach_ref(re
, new_nhghe
);
287 zebra_nhg_decrement_ref(old
);
289 /* This is the first time it's being attached */
290 route_entry_attach_ref(re
, new_nhghe
);
296 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
297 struct nhg_hash_entry
*new_entry
)
299 struct zebra_router_table
*zrt
;
300 struct route_node
*rn
;
301 struct route_entry
*re
, *next
;
303 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
304 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
305 __func__
, new_entry
->id
, new_entry
, old_entry
);
307 /* We have to do them ALL */
308 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
309 for (rn
= route_top(zrt
->table
); rn
;
310 rn
= srcdest_route_next(rn
)) {
311 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
312 if (re
->nhe
&& re
->nhe
== old_entry
)
313 route_entry_update_nhe(re
, new_entry
);
319 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
320 union g_addr
*addr
, struct route_node
**rn_out
)
323 struct route_table
*table
;
324 struct route_node
*rn
;
325 struct route_entry
*match
= NULL
;
328 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
332 memset(&p
, 0, sizeof(struct prefix
));
335 p
.u
.prefix4
= addr
->ipv4
;
336 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
338 p
.u
.prefix6
= addr
->ipv6
;
339 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
342 rn
= route_node_match(table
, &p
);
347 route_unlock_node(rn
);
349 dest
= rib_dest_from_rnode(rn
);
350 if (dest
&& dest
->selected_fib
351 && !CHECK_FLAG(dest
->selected_fib
->status
,
352 ROUTE_ENTRY_REMOVED
))
353 match
= dest
->selected_fib
;
355 /* If there is no selected route or matched route is EGP, go up
360 } while (rn
&& rn
->info
== NULL
);
364 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
365 if (!CHECK_FLAG(match
->status
,
366 ROUTE_ENTRY_INSTALLED
))
378 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
380 struct route_node
**rn_out
)
382 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
383 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
384 union g_addr gaddr
= {.ipv4
= addr
};
386 switch (zrouter
.ipv4_multicast_mode
) {
387 case MCAST_MRIB_ONLY
:
388 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
390 case MCAST_URIB_ONLY
:
391 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
392 case MCAST_NO_CONFIG
:
393 case MCAST_MIX_MRIB_FIRST
:
394 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
397 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
400 case MCAST_MIX_DISTANCE
:
401 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
402 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
404 re
= ure
->distance
< mre
->distance
? ure
: mre
;
410 case MCAST_MIX_PFXLEN
:
411 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
412 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
414 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
423 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
425 if (IS_ZEBRA_DEBUG_RIB
) {
427 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
429 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
430 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
431 mre
? (ure
? "MRIB+URIB" : "MRIB")
432 : ure
? "URIB" : "nothing",
433 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
438 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
440 struct route_table
*table
;
441 struct route_node
*rn
;
442 struct route_entry
*match
= NULL
;
446 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
450 rn
= route_node_lookup(table
, (struct prefix
*)p
);
452 /* No route for this prefix. */
457 route_unlock_node(rn
);
458 dest
= rib_dest_from_rnode(rn
);
460 if (dest
&& dest
->selected_fib
461 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
462 match
= dest
->selected_fib
;
467 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
470 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
477 * Is this RIB labeled-unicast? It must be of type BGP and all paths
478 * (nexthops) must have a label.
480 int zebra_rib_labeled_unicast(struct route_entry
*re
)
482 struct nexthop
*nexthop
= NULL
;
484 if (re
->type
!= ZEBRA_ROUTE_BGP
)
487 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
488 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
494 /* Update flag indicates whether this is a "replace" or not. Currently, this
495 * is only used for IPv4.
497 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
498 struct route_entry
*old
)
500 struct nexthop
*nexthop
;
501 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
502 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
503 const struct prefix
*p
, *src_p
;
504 enum zebra_dplane_result ret
;
506 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
508 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
510 if (info
->safi
!= SAFI_UNICAST
) {
511 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
512 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
518 * Install the resolved nexthop object first.
520 zebra_nhg_install_kernel(re
->nhe
);
523 * If this is a replace to a new RE let the originator of the RE
524 * know that they've lost
526 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
527 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
528 info
->afi
, info
->safi
);
530 /* Update fib selection */
531 dest
->selected_fib
= re
;
534 * Make sure we update the FPM any time we send new information to
537 hook_call(rib_update
, rn
, "installing in kernel");
539 /* Send add or update */
541 ret
= dplane_route_update(rn
, re
, old
);
543 ret
= dplane_route_add(rn
, re
);
546 case ZEBRA_DPLANE_REQUEST_QUEUED
:
547 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
550 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
552 /* Free old FIB nexthop group */
553 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
554 if (old
->fib_ng
.nexthop
) {
555 nexthops_free(old
->fib_ng
.nexthop
);
556 old
->fib_ng
.nexthop
= NULL
;
561 zvrf
->installs_queued
++;
563 case ZEBRA_DPLANE_REQUEST_FAILURE
:
565 char str
[SRCDEST2STR_BUFFER
];
567 srcdest_rnode2str(rn
, str
, sizeof(str
));
568 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
569 "%u:%u:%s: Failed to enqueue dataplane install",
570 re
->vrf_id
, re
->table
, str
);
573 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
582 /* Uninstall the route from kernel. */
583 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
585 struct nexthop
*nexthop
;
586 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
587 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
589 if (info
->safi
!= SAFI_UNICAST
) {
590 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
591 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
592 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
597 * Make sure we update the FPM any time we send new information to
600 hook_call(rib_update
, rn
, "uninstalling from kernel");
602 switch (dplane_route_delete(rn
, re
)) {
603 case ZEBRA_DPLANE_REQUEST_QUEUED
:
605 zvrf
->removals_queued
++;
607 case ZEBRA_DPLANE_REQUEST_FAILURE
:
609 char str
[SRCDEST2STR_BUFFER
];
611 srcdest_rnode2str(rn
, str
, sizeof(str
));
612 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
613 "%u:%s: Failed to enqueue dataplane uninstall",
617 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
626 /* Uninstall the route from kernel. */
627 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
629 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
630 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
631 struct nexthop
*nexthop
;
633 if (dest
&& dest
->selected_fib
== re
) {
634 if (info
->safi
== SAFI_UNICAST
)
635 hook_call(rib_update
, rn
, "rib_uninstall");
637 /* If labeled-unicast route, uninstall transit LSP. */
638 if (zebra_rib_labeled_unicast(re
))
639 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
641 rib_uninstall_kernel(rn
, re
);
643 dest
->selected_fib
= NULL
;
645 /* Free FIB nexthop group, if present */
646 if (re
->fib_ng
.nexthop
) {
647 nexthops_free(re
->fib_ng
.nexthop
);
648 re
->fib_ng
.nexthop
= NULL
;
650 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
652 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
653 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
656 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
657 const struct prefix
*p
, *src_p
;
659 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
661 redistribute_delete(p
, src_p
, re
, NULL
);
662 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
667 * rib_can_delete_dest
669 * Returns true if the given dest can be deleted from the table.
671 static int rib_can_delete_dest(rib_dest_t
*dest
)
673 if (re_list_first(&dest
->routes
)) {
678 * Unresolved rnh's are stored on the default route's list
680 * dest->rnode can also be the source prefix node in an
681 * ipv6 sourcedest table. Fortunately the prefix of a
682 * source prefix node can never be the default prefix.
684 if (is_default_prefix(&dest
->rnode
->p
))
688 * Don't delete the dest if we have to update the FPM about this
691 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
692 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
698 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
700 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
704 * We are storing the rnh's associated withb
705 * the tracked nexthop as a list of the rn's.
706 * Unresolved rnh's are placed at the top
707 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
708 * As such for each rn we need to walk up the tree
709 * and see if any rnh's need to see if they
710 * would match a more specific route
713 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
714 char buf
[PREFIX_STRLEN
];
717 "%s: %s Being examined for Nexthop Tracking Count: %zd",
719 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
720 dest
? rnh_list_count(&dest
->nht
) : 0);
725 dest
= rib_dest_from_rnode(rn
);
729 * If we have any rnh's stored in the nht list
730 * then we know that this route node was used for
731 * nht resolution and as such we need to call the
732 * nexthop tracking evaluation code
734 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
735 struct zebra_vrf
*zvrf
=
736 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
737 struct prefix
*p
= &rnh
->node
->p
;
739 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
740 char buf1
[PREFIX_STRLEN
];
743 "%s(%u):%s has Nexthop(%pFX) Type: %s depending on it, evaluating %u:%u",
744 zvrf_name(zvrf
), zvrf_id(zvrf
),
745 srcdest_rnode2str(rn
, buf1
,
747 p
, rnh_type2str(rnh
->type
), seq
,
752 * If we have evaluated this node on this pass
753 * already, due to following the tree up
754 * then we know that we can move onto the next
757 * Additionally we call zebra_evaluate_rnh
758 * when we gc the dest. In this case we know
759 * that there must be no other re's where
760 * we were originally as such we know that
761 * that sequence number is ok to respect.
763 if (rnh
->seqno
== seq
) {
764 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
766 " Node processed and moved already");
771 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
777 dest
= rib_dest_from_rnode(rn
);
784 * Garbage collect the rib dest corresponding to the given route node
787 * Returns true if the dest was deleted, false otherwise.
789 int rib_gc_dest(struct route_node
*rn
)
793 dest
= rib_dest_from_rnode(rn
);
797 if (!rib_can_delete_dest(dest
))
800 if (IS_ZEBRA_DEBUG_RIB
) {
801 struct zebra_vrf
*zvrf
;
803 zvrf
= rib_dest_vrf(dest
);
804 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
807 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
810 rnh_list_fini(&dest
->nht
);
811 XFREE(MTYPE_RIB_DEST
, dest
);
815 * Release the one reference that we keep on the route node.
817 route_unlock_node(rn
);
821 void zebra_rtable_node_cleanup(struct route_table
*table
,
822 struct route_node
*node
)
824 struct route_entry
*re
, *next
;
826 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
827 rib_unlink(node
, re
);
831 rib_dest_t
*dest
= node
->info
;
833 rnh_list_fini(&dest
->nht
);
834 XFREE(MTYPE_RIB_DEST
, node
->info
);
838 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
839 struct route_entry
*new)
841 hook_call(rib_update
, rn
, "new route selected");
843 /* Update real nexthop. This may actually determine if nexthop is active
845 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
846 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
850 if (IS_ZEBRA_DEBUG_RIB
) {
851 char buf
[SRCDEST2STR_BUFFER
];
852 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
853 zlog_debug("%s(%u:%u):%s: Adding route rn %p, re %p (%s)",
854 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, buf
, rn
,
855 new, zebra_route_string(new->type
));
858 /* If labeled-unicast route, install transit LSP. */
859 if (zebra_rib_labeled_unicast(new))
860 zebra_mpls_lsp_install(zvrf
, rn
, new);
862 rib_install_kernel(rn
, new, NULL
);
864 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
867 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
868 struct route_entry
*old
)
870 hook_call(rib_update
, rn
, "removing existing route");
872 /* Uninstall from kernel. */
873 if (IS_ZEBRA_DEBUG_RIB
) {
874 char buf
[SRCDEST2STR_BUFFER
];
875 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
876 zlog_debug("%s(%u:%u):%s: Deleting route rn %p, re %p (%s)",
877 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, buf
, rn
,
878 old
, zebra_route_string(old
->type
));
881 /* If labeled-unicast route, uninstall transit LSP. */
882 if (zebra_rib_labeled_unicast(old
))
883 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
885 rib_uninstall_kernel(rn
, old
);
887 /* Update nexthop for route, reset changed flag. */
888 /* Note: this code also handles the Linux case when an interface goes
889 * down, causing the kernel to delete routes without sending DELROUTE
892 if (RIB_KERNEL_ROUTE(old
))
893 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
895 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
898 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
899 struct route_node
*rn
,
900 struct route_entry
*old
,
901 struct route_entry
*new)
906 * We have to install or update if a new route has been selected or
907 * something has changed.
909 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
910 hook_call(rib_update
, rn
, "updating existing route");
912 /* Update the nexthop; we could determine here that nexthop is
914 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
917 /* If nexthop is active, install the selected route, if
919 * the install succeeds, cleanup flags for prior route, if
924 if (IS_ZEBRA_DEBUG_RIB
) {
925 char buf
[SRCDEST2STR_BUFFER
];
927 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
930 "%s(%u:%u):%s: Updating route rn %p, re %p (%s) old %p (%s)",
931 zvrf_name(zvrf
), zvrf_id(zvrf
),
932 new->table
, buf
, rn
, new,
933 zebra_route_string(new->type
),
935 zebra_route_string(old
->type
));
938 "%s(%u:%u):%s: Updating route rn %p, re %p (%s)",
939 zvrf_name(zvrf
), zvrf_id(zvrf
),
940 new->table
, buf
, rn
, new,
941 zebra_route_string(new->type
));
944 /* If labeled-unicast route, uninstall transit LSP. */
945 if (zebra_rib_labeled_unicast(old
))
946 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
949 * Non-system route should be installed.
950 * If labeled-unicast route, install transit
953 if (zebra_rib_labeled_unicast(new))
954 zebra_mpls_lsp_install(zvrf
, rn
, new);
956 rib_install_kernel(rn
, new, old
);
960 * If nexthop for selected route is not active or install
962 * may need to uninstall and delete for redistribution.
965 if (IS_ZEBRA_DEBUG_RIB
) {
966 char buf
[SRCDEST2STR_BUFFER
];
967 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
970 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
971 zvrf_name(zvrf
), zvrf_id(zvrf
),
972 new->table
, buf
, rn
, new,
973 zebra_route_string(new->type
),
975 zebra_route_string(old
->type
));
978 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
979 zvrf_name(zvrf
), zvrf_id(zvrf
),
980 new->table
, buf
, rn
, new,
981 zebra_route_string(new->type
));
985 * When we have gotten to this point
986 * the new route entry has no nexthops
987 * that are usable and as such we need
988 * to remove the old route, but only
989 * if we were the one who installed
992 if (!RIB_SYSTEM_ROUTE(old
)) {
993 /* If labeled-unicast route, uninstall transit
995 if (zebra_rib_labeled_unicast(old
))
996 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
998 rib_uninstall_kernel(rn
, old
);
1003 * Same route selected; check if in the FIB and if not,
1004 * re-install. This is housekeeping code to deal with
1005 * race conditions in kernel with linux netlink reporting
1006 * interface up before IPv4 or IPv6 protocol is ready
1009 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1010 RIB_SYSTEM_ROUTE(new))
1011 rib_install_kernel(rn
, new, NULL
);
1014 /* Update prior route. */
1016 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1018 /* Clear changed flag. */
1019 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1022 /* Check if 'alternate' RIB entry is better than 'current'. */
1023 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1024 struct route_entry
*alternate
)
1026 if (current
== NULL
)
1029 /* filter route selection in following order:
1030 * - connected beats other types
1031 * - if both connected, loopback or vrf wins
1032 * - lower distance beats higher
1033 * - lower metric beats higher for equal distance
1034 * - last, hence oldest, route wins tie break.
1037 /* Connected routes. Check to see if either are a vrf
1038 * or loopback interface. If not, pick the last connected
1039 * route of the set of lowest metric connected routes.
1041 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1042 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1045 /* both are connected. are either loop or vrf? */
1046 struct nexthop
*nexthop
= NULL
;
1048 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1049 struct interface
*ifp
= if_lookup_by_index(
1050 nexthop
->ifindex
, alternate
->vrf_id
);
1052 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1056 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1057 struct interface
*ifp
= if_lookup_by_index(
1058 nexthop
->ifindex
, current
->vrf_id
);
1060 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1064 /* Neither are loop or vrf so pick best metric */
1065 if (alternate
->metric
<= current
->metric
)
1071 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1074 /* higher distance loses */
1075 if (alternate
->distance
< current
->distance
)
1077 if (current
->distance
< alternate
->distance
)
1080 /* metric tie-breaks equal distance */
1081 if (alternate
->metric
<= current
->metric
)
1087 /* Core function for processing routing information base. */
1088 static void rib_process(struct route_node
*rn
)
1090 struct route_entry
*re
;
1091 struct route_entry
*next
;
1092 struct route_entry
*old_selected
= NULL
;
1093 struct route_entry
*new_selected
= NULL
;
1094 struct route_entry
*old_fib
= NULL
;
1095 struct route_entry
*new_fib
= NULL
;
1096 struct route_entry
*best
= NULL
;
1097 char buf
[SRCDEST2STR_BUFFER
];
1099 struct zebra_vrf
*zvrf
= NULL
;
1101 const struct prefix
*p
, *src_p
;
1103 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1104 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1108 dest
= rib_dest_from_rnode(rn
);
1110 zvrf
= rib_dest_vrf(dest
);
1111 vrf_id
= zvrf_id(zvrf
);
1114 vrf
= vrf_lookup_by_id(vrf_id
);
1116 if (IS_ZEBRA_DEBUG_RIB
)
1117 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1120 * we can have rn's that have a NULL info pointer
1121 * (dest). As such let's not let the deref happen
1122 * additionally we know RNODE_FOREACH_RE_SAFE
1123 * will not iterate so we are ok.
1126 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1127 struct route_entry
*re
= re_list_first(&dest
->routes
);
1129 zlog_debug("%s(%u:%u):%s: Processing rn %p",
1130 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
,
1134 old_fib
= dest
->selected_fib
;
1137 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1138 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1139 char flags_buf
[128];
1140 char status_buf
[128];
1143 "%s(%u:%u):%s: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1144 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
, re
,
1145 zebra_route_string(re
->type
),
1146 _dump_re_status(re
, status_buf
,
1147 sizeof(status_buf
)),
1148 zclient_dump_route_flags(re
->flags
, flags_buf
,
1150 re
->distance
, re
->metric
);
1153 /* Currently selected re. */
1154 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1155 assert(old_selected
== NULL
);
1159 /* Skip deleted entries from selection */
1160 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1164 * If the route entry has changed, verify/resolve
1165 * the nexthops associated with the entry.
1167 * In any event if we have nexthops that are not active
1168 * then we cannot use this particular route entry so
1171 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1172 if (!nexthop_active_update(rn
, re
)) {
1173 const struct prefix
*p
;
1174 struct rib_table_info
*info
;
1176 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1177 /* XXX: HERE BE DRAGONS!!!!!
1178 * In all honesty, I have not yet
1179 * figured out what this part does or
1180 * why the ROUTE_ENTRY_CHANGED test
1181 * above is correct or why we need to
1182 * delete a route here, and also not
1183 * whether this concerns both selected
1184 * and fib route, or only selected
1187 * This entry was denied by the 'ip
1189 * table' route-map, we need to delete
1191 if (re
!= old_selected
) {
1192 if (IS_ZEBRA_DEBUG_RIB
)
1194 "%s: %s(%u):%s: imported via import-table but denied by the ip protocol table route-map",
1201 SET_FLAG(re
->status
,
1202 ROUTE_ENTRY_REMOVED
);
1205 info
= srcdest_rnode_table_info(rn
);
1206 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1207 zsend_route_notify_owner(re
, p
,
1208 ZAPI_ROUTE_FAIL_INSTALL
,
1209 info
->afi
, info
->safi
);
1214 * If the re has not changed and the nhg we have is
1215 * not usable, then we cannot use this route entry
1216 * for consideration, as that the route will just
1217 * not install if it is selected.
1219 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1223 /* Infinite distance. */
1224 if (re
->distance
== DISTANCE_INFINITY
&&
1225 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1226 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1230 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1231 best
= rib_choose_best(new_fib
, re
);
1232 if (new_fib
&& best
!= new_fib
)
1233 UNSET_FLAG(new_fib
->status
,
1234 ROUTE_ENTRY_CHANGED
);
1237 best
= rib_choose_best(new_selected
, re
);
1238 if (new_selected
&& best
!= new_selected
)
1239 UNSET_FLAG(new_selected
->status
,
1240 ROUTE_ENTRY_CHANGED
);
1241 new_selected
= best
;
1244 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1245 } /* RNODE_FOREACH_RE */
1247 /* If no FIB override route, use the selected route also for FIB */
1248 if (new_fib
== NULL
)
1249 new_fib
= new_selected
;
1251 /* After the cycle is finished, the following pointers will be set:
1252 * old_selected --- RE entry currently having SELECTED
1253 * new_selected --- RE entry that is newly SELECTED
1254 * old_fib --- RE entry currently in kernel FIB
1255 * new_fib --- RE entry that is newly to be in kernel FIB
1257 * new_selected will get SELECTED flag, and is going to be redistributed
1258 * the zclients. new_fib (which can be new_selected) will be installed
1262 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1263 struct route_entry
*entry
;
1265 entry
= old_selected
1270 : new_fib
? new_fib
: NULL
;
1273 "%s(%u:%u):%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1274 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, buf
,
1275 (void *)old_selected
, (void *)new_selected
,
1276 (void *)old_fib
, (void *)new_fib
);
1279 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1280 * fib == selected */
1281 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1282 ROUTE_ENTRY_CHANGED
);
1284 /* Update fib according to selection results */
1285 if (new_fib
&& old_fib
)
1286 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1288 rib_process_add_fib(zvrf
, rn
, new_fib
);
1290 rib_process_del_fib(zvrf
, rn
, old_fib
);
1292 /* Update SELECTED entry */
1293 if (old_selected
!= new_selected
|| selected_changed
) {
1295 if (new_selected
&& new_selected
!= new_fib
)
1296 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1299 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1303 * If we're removing the old entry, we should tell
1304 * redist subscribers about that *if* they aren't
1305 * going to see a redist for the new entry.
1307 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1308 ROUTE_ENTRY_REMOVED
))
1309 redistribute_delete(p
, src_p
,
1313 if (old_selected
!= new_selected
)
1314 UNSET_FLAG(old_selected
->flags
,
1315 ZEBRA_FLAG_SELECTED
);
1319 /* Remove all RE entries queued for removal */
1320 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1321 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1322 if (IS_ZEBRA_DEBUG_RIB
) {
1323 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1324 (void *)rn
, (void *)re
);
1331 * Check if the dest can be deleted now.
1336 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1338 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1339 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1344 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1345 if (IS_ZEBRA_DEBUG_MPLS
)
1347 "%s(%u): Scheduling all LSPs upon RIB completion",
1348 zvrf_name(zvrf
), zvrf_id(zvrf
));
1349 zebra_mpls_lsp_schedule(zvrf
);
1350 mpls_unmark_lsps_for_processing(rn
);
1355 * Utility to match route with dplane context data
1357 static bool rib_route_match_ctx(const struct route_entry
*re
,
1358 const struct zebra_dplane_ctx
*ctx
,
1361 bool result
= false;
1365 * In 'update' case, we test info about the 'previous' or
1368 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1369 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1372 /* We use an extra test for statics, and another for
1375 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1376 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1377 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1379 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1381 dplane_ctx_get_old_metric(ctx
)) {
1388 * Ordinary, single-route case using primary context info
1390 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1391 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1392 /* Skip route that's been deleted */
1396 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1397 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1400 /* We use an extra test for statics, and another for
1403 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1404 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1405 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1407 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1408 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1410 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1411 result
= nexthop_group_equal_no_recurse(
1412 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1422 static void zebra_rib_fixup_system(struct route_node
*rn
)
1424 struct route_entry
*re
;
1426 RNODE_FOREACH_RE(rn
, re
) {
1427 struct nexthop
*nhop
;
1429 if (!RIB_SYSTEM_ROUTE(re
))
1432 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1435 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1436 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1438 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1439 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1442 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1447 /* Route comparison logic, with various special cases. */
1448 static bool rib_compare_routes(const struct route_entry
*re1
,
1449 const struct route_entry
*re2
)
1451 if (re1
->type
!= re2
->type
)
1454 if (re1
->instance
!= re2
->instance
)
1457 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1460 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1461 re1
->distance
!= re2
->distance
)
1464 /* We support multiple connected routes: this supports multiple
1465 * v6 link-locals, and we also support multiple addresses in the same
1466 * subnet on a single interface.
1468 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1475 * Compare nexthop lists from a route and a dplane context; test whether
1476 * the list installed in the FIB matches the route's list.
1477 * Set 'changed_p' to 'true' if there were changes to the route's
1478 * installed nexthops.
1480 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1483 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1484 const struct nexthop_group
*ctx_nhg
,
1487 bool matched_p
= true;
1488 struct nexthop
*nexthop
, *ctx_nexthop
;
1490 /* Get the first `installed` one to check against.
1491 * If the dataplane doesn't set these to be what was actually installed,
1492 * it will just be whatever was in re->nhe->nhg?
1494 ctx_nexthop
= ctx_nhg
->nexthop
;
1496 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1497 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1498 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1500 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1502 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1505 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1508 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1509 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1510 /* If the FIB doesn't know about the nexthop,
1511 * it's not installed
1513 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1514 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1515 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1517 (CHECK_FLAG(nexthop
->flags
,
1523 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1526 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1528 /* Keep checking nexthops */
1532 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1533 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1534 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1535 zlog_debug("%s: rib nh %pNHv -> installed",
1541 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1543 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1544 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1545 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1551 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1554 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1561 * Update a route from a dplane context. This consolidates common code
1562 * that can be used in processing of results from FIB updates, and in
1563 * async notification processing.
1564 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1566 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1567 struct route_node
*rn
,
1568 struct zebra_dplane_ctx
*ctx
)
1570 struct nexthop
*nexthop
;
1572 const struct nexthop_group
*ctxnhg
;
1573 struct nexthop_group
*re_nhg
;
1574 bool is_selected
= false; /* Is 're' currently the selected re? */
1575 bool changed_p
= false; /* Change to nexthops? */
1579 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1581 dest
= rib_dest_from_rnode(rn
);
1583 is_selected
= (re
== dest
->selected_fib
);
1585 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1586 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1587 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1588 (is_selected
? "" : "NOT "), re
);
1590 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1591 * If the installed set differs from the set requested by the rib/owner,
1592 * we use the fib-specific nexthop-group to record the actual FIB
1596 ctxnhg
= dplane_ctx_get_ng(ctx
);
1598 /* Check route's fib group and incoming notif group for equivalence.
1600 * Let's assume the nexthops are ordered here to save time.
1602 /* TODO -- this isn't testing or comparing the FIB flags; we should
1603 * do a more explicit loop, checking the incoming notification's flags.
1605 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1606 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1609 /* If the new FIB set matches the existing FIB set, we're done. */
1611 if (IS_ZEBRA_DEBUG_RIB
)
1613 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1614 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1617 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1619 * Free stale fib list and move on to check the rib nhg.
1621 if (IS_ZEBRA_DEBUG_RIB
)
1623 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1624 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1625 nexthops_free(re
->fib_ng
.nexthop
);
1626 re
->fib_ng
.nexthop
= NULL
;
1628 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1630 /* Note that the installed nexthops have changed */
1633 if (IS_ZEBRA_DEBUG_RIB
)
1635 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1636 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1640 * Compare with the rib nexthop group. The comparison here is different:
1641 * the RIB group may be a superset of the list installed in the FIB. We
1642 * walk the RIB group, looking for the 'installable' candidate
1643 * nexthops, and then check those against the set
1644 * that is actually installed.
1646 * Assume nexthops are ordered here as well.
1649 /* If nothing is installed, we can skip some of the checking/comparison
1652 if (ctxnhg
->nexthop
== NULL
) {
1657 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1659 /* If all nexthops were processed, we're done */
1661 if (IS_ZEBRA_DEBUG_RIB
)
1663 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1664 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1665 (changed_p
? "true" : "false"));
1671 /* FIB nexthop set differs from the RIB set:
1672 * create a fib-specific nexthop-group
1674 if (IS_ZEBRA_DEBUG_RIB
)
1676 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1677 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1678 (changed_p
? "true" : "false"),
1679 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1681 /* Set the flag about the dedicated fib list */
1682 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1683 if (ctxnhg
->nexthop
)
1684 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1689 * Check the status of the route's backup nexthops, if any.
1690 * The logic for backups is somewhat different: if any backup is
1691 * installed, a new fib nhg will be attached to the route.
1693 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1695 goto done
; /* No backup nexthops */
1697 /* First check the route's 'fib' list of backups, if it's present
1698 * from some previous event.
1700 re_nhg
= &re
->fib_backup_ng
;
1701 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1704 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1707 /* If the new FIB set matches an existing FIB set, we're done. */
1709 if (IS_ZEBRA_DEBUG_RIB
)
1711 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1712 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1715 } else if (re
->fib_backup_ng
.nexthop
) {
1717 * Free stale fib backup list and move on to check
1718 * the route's backups.
1720 if (IS_ZEBRA_DEBUG_RIB
)
1722 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1723 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1724 nexthops_free(re
->fib_backup_ng
.nexthop
);
1725 re
->fib_backup_ng
.nexthop
= NULL
;
1727 /* Note that the installed nexthops have changed */
1730 if (IS_ZEBRA_DEBUG_RIB
)
1732 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1733 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1737 * If a FIB backup nexthop set exists, attach a copy
1738 * to the route if any backup is installed
1740 if (ctxnhg
&& ctxnhg
->nexthop
) {
1742 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1743 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1747 /* If no installed backups, we're done */
1748 if (nexthop
== NULL
)
1751 if (IS_ZEBRA_DEBUG_RIB
)
1753 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1754 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1755 (changed_p
? "true" : "false"));
1757 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1767 * Helper to locate a zebra route-node from a dplane context. This is used
1768 * when processing dplane results, e.g. Note well: the route-node is returned
1769 * with a ref held - route_unlock_node() must be called eventually.
1771 static struct route_node
*
1772 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1774 struct route_table
*table
= NULL
;
1775 struct route_node
*rn
= NULL
;
1776 const struct prefix
*dest_pfx
, *src_pfx
;
1778 /* Locate rn and re(s) from ctx */
1780 table
= zebra_vrf_lookup_table_with_table_id(
1781 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1782 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1783 if (table
== NULL
) {
1784 if (IS_ZEBRA_DEBUG_DPLANE
) {
1786 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1787 dplane_ctx_get_afi(ctx
),
1788 dplane_ctx_get_safi(ctx
),
1789 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1790 dplane_ctx_get_vrf(ctx
));
1795 dest_pfx
= dplane_ctx_get_dest(ctx
);
1796 src_pfx
= dplane_ctx_get_src(ctx
);
1798 rn
= srcdest_rnode_get(table
, dest_pfx
,
1799 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1808 * Route-update results processing after async dataplane update.
1810 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1812 struct zebra_vrf
*zvrf
= NULL
;
1814 struct route_node
*rn
= NULL
;
1815 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1816 bool is_update
= false;
1817 enum dplane_op_e op
;
1818 enum zebra_dplane_result status
;
1819 const struct prefix
*dest_pfx
, *src_pfx
;
1822 bool fib_changed
= false;
1823 struct rib_table_info
*info
;
1825 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1826 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1827 dest_pfx
= dplane_ctx_get_dest(ctx
);
1829 /* Locate rn and re(s) from ctx */
1830 rn
= rib_find_rn_from_ctx(ctx
);
1832 if (IS_ZEBRA_DEBUG_DPLANE
) {
1834 "Failed to process dplane results: no route for %s(%u):%pFX",
1835 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1841 dest
= rib_dest_from_rnode(rn
);
1842 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1843 info
= srcdest_rnode_table_info(rn
);
1845 op
= dplane_ctx_get_op(ctx
);
1846 status
= dplane_ctx_get_status(ctx
);
1848 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1850 "%s(%u:%u):%pFX Processing dplane result ctx %p, op %s result %s",
1851 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1852 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
,
1853 dplane_op2str(op
), dplane_res2str(status
));
1856 * Update is a bit of a special case, where we may have both old and new
1857 * routes to post-process.
1859 is_update
= dplane_ctx_is_update(ctx
);
1862 * Take a pass through the routes, look for matches with the context
1865 RNODE_FOREACH_RE(rn
, rib
) {
1868 if (rib_route_match_ctx(rib
, ctx
, false))
1872 /* Check for old route match */
1873 if (is_update
&& (old_re
== NULL
)) {
1874 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1878 /* Have we found the routes we need to work on? */
1879 if (re
&& ((!is_update
|| old_re
)))
1883 seq
= dplane_ctx_get_seq(ctx
);
1886 * Check sequence number(s) to detect stale results before continuing
1889 if (re
->dplane_sequence
!= seq
) {
1890 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1892 "%s(%u):%pFX Stale dplane result for re %p",
1894 dplane_ctx_get_vrf(ctx
), dest_pfx
, re
);
1896 if (!zrouter
.asic_offloaded
||
1897 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
1898 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)))
1899 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1904 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1905 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1907 "%s(%u:%u):%pFX Stale dplane result for old_re %p",
1909 dplane_ctx_get_vrf(ctx
), old_re
->table
,
1912 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1916 case DPLANE_OP_ROUTE_INSTALL
:
1917 case DPLANE_OP_ROUTE_UPDATE
:
1918 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1920 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1921 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1924 * On an update operation from the same route type
1925 * context retrieval currently has no way to know
1926 * which was the old and which was the new.
1927 * So don't unset our flags that we just set.
1928 * We know redistribution is ok because the
1929 * old_re in this case is used for nothing
1930 * more than knowing whom to contact if necessary.
1932 if (old_re
&& old_re
!= re
) {
1933 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1934 UNSET_FLAG(old_re
->status
,
1935 ROUTE_ENTRY_INSTALLED
);
1938 /* Update zebra route based on the results in
1939 * the context struct.
1943 rib_update_re_from_ctx(re
, rn
, ctx
);
1946 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1948 "%s(%u:%u):%pFX no fib change for re",
1950 dplane_ctx_get_vrf(ctx
),
1951 dplane_ctx_get_table(
1956 /* Redistribute if this is the selected re */
1957 if (dest
&& re
== dest
->selected_fib
)
1958 redistribute_update(dest_pfx
, src_pfx
,
1963 * System routes are weird in that they
1964 * allow multiple to be installed that match
1965 * to the same prefix, so after we get the
1966 * result we need to clean them up so that
1967 * we can actually use them.
1969 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1970 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1971 zebra_rib_fixup_system(rn
);
1976 /* Notify route owner */
1977 if (zebra_router_notify_on_ack())
1978 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1981 if (CHECK_FLAG(re
->flags
,
1982 ZEBRA_FLAG_OFFLOADED
))
1983 zsend_route_notify_owner_ctx(
1985 ZAPI_ROUTE_INSTALLED
);
1988 ZEBRA_FLAG_OFFLOAD_FAILED
))
1989 zsend_route_notify_owner_ctx(
1991 ZAPI_ROUTE_FAIL_INSTALL
);
1996 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1997 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1999 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2001 zsend_route_notify_owner(re
, dest_pfx
,
2002 ZAPI_ROUTE_FAIL_INSTALL
,
2003 info
->afi
, info
->safi
);
2005 zlog_warn("%s(%u:%u):%pFX: Route install failed",
2006 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2007 dplane_ctx_get_table(ctx
), dest_pfx
);
2010 case DPLANE_OP_ROUTE_DELETE
:
2012 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2014 * In the delete case, the zebra core datastructs were
2015 * updated (or removed) at the time the delete was issued,
2016 * so we're just notifying the route owner.
2018 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2020 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2021 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2023 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2029 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2030 zsend_route_notify_owner_ctx(ctx
,
2031 ZAPI_ROUTE_REMOVE_FAIL
);
2033 zlog_warn("%s(%u:%u):%pFX: Route Deletion failure",
2034 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2035 dplane_ctx_get_table(ctx
), dest_pfx
);
2039 * System routes are weird in that they
2040 * allow multiple to be installed that match
2041 * to the same prefix, so after we get the
2042 * result we need to clean them up so that
2043 * we can actually use them.
2045 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2046 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2047 zebra_rib_fixup_system(rn
);
2053 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
2054 zebra_rib_evaluate_mpls(rn
);
2058 route_unlock_node(rn
);
2060 /* Return context to dataplane module */
2061 dplane_ctx_fini(&ctx
);
2065 * Count installed/FIB nexthops
2067 static int rib_count_installed_nh(struct route_entry
*re
)
2070 struct nexthop
*nexthop
;
2071 struct nexthop_group
*nhg
;
2073 nhg
= rib_get_fib_nhg(re
);
2075 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2076 /* The meaningful flag depends on where the installed
2079 if (nhg
== &(re
->fib_ng
)) {
2080 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2083 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2088 nhg
= rib_get_fib_backup_nhg(re
);
2090 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2091 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2100 * Handle notification from async dataplane: the dataplane has detected
2101 * some change to a route, and notifies zebra so that the control plane
2102 * can reflect that change.
2104 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2106 struct route_node
*rn
= NULL
;
2107 struct route_entry
*re
= NULL
;
2109 struct nexthop
*nexthop
;
2110 const struct prefix
*dest_pfx
, *src_pfx
;
2112 bool fib_changed
= false;
2113 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2114 int start_count
, end_count
;
2115 dest_pfx
= dplane_ctx_get_dest(ctx
);
2116 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2118 /* Locate rn and re(s) from ctx */
2119 rn
= rib_find_rn_from_ctx(ctx
);
2123 "Failed to process dplane notification: no routes for %s(%u:%u):%pFX",
2124 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2125 dplane_ctx_get_table(ctx
), dest_pfx
);
2130 dest
= rib_dest_from_rnode(rn
);
2131 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
2134 zlog_debug("%s(%u:%u):%pFX Processing dplane notif ctx %p",
2135 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2136 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
);
2139 * Take a pass through the routes, look for matches with the context
2142 RNODE_FOREACH_RE(rn
, re
) {
2143 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2147 /* No match? Nothing we can do */
2151 "%s(%u:%u):%pFX Unable to process dplane notification: no entry for type %s",
2152 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2153 dplane_ctx_get_table(ctx
), dest_pfx
,
2154 zebra_route_string(dplane_ctx_get_type(ctx
)));
2159 /* Ensure we clear the QUEUED flag */
2160 if (!zrouter
.asic_offloaded
)
2161 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2163 /* Is this a notification that ... matters? We mostly care about
2164 * the route that is currently selected for installation; we may also
2165 * get an un-install notification, and handle that too.
2167 if (re
!= dest
->selected_fib
) {
2169 * If we need to, clean up after a delete that was part of
2170 * an update operation.
2173 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2174 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2178 /* If no nexthops or none installed, ensure that this re
2179 * gets its 'installed' flag cleared.
2181 if (end_count
== 0) {
2182 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2183 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2186 "%s(%u:%u):%pFX dplane notif, uninstalled type %s route",
2188 dplane_ctx_get_vrf(ctx
),
2189 dplane_ctx_get_table(ctx
), dest_pfx
,
2191 dplane_ctx_get_type(ctx
)));
2193 /* At least report on the event. */
2196 "%s(%u:%u):%pFX dplane notif, but type %s not selected_fib",
2198 dplane_ctx_get_vrf(ctx
),
2199 dplane_ctx_get_table(ctx
), dest_pfx
,
2201 dplane_ctx_get_type(ctx
)));
2206 /* We'll want to determine whether the installation status of the
2207 * route has changed: we'll check the status before processing,
2208 * and then again if there's been a change.
2212 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2213 start_count
= rib_count_installed_nh(re
);
2215 /* Update zebra's nexthop FIB flags based on the context struct's
2218 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2223 "%s(%u:%u):%pFX dplane notification: rib_update returns FALSE",
2224 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2225 dplane_ctx_get_table(ctx
), dest_pfx
);
2229 * Perform follow-up work if the actual status of the prefix
2232 end_count
= rib_count_installed_nh(re
);
2234 /* Various fib transitions: changed nexthops; from installed to
2235 * not-installed; or not-installed to installed.
2237 if (start_count
> 0 && end_count
> 0) {
2240 "%s(%u:%u):%pFX applied nexthop changes from dplane notification",
2241 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2242 dplane_ctx_get_table(ctx
), dest_pfx
);
2244 /* Changed nexthops - update kernel/others */
2245 dplane_route_notif_update(rn
, re
,
2246 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2248 } else if (start_count
== 0 && end_count
> 0) {
2251 "%s(%u:%u):%pFX installed transition from dplane notification",
2252 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2253 dplane_ctx_get_table(ctx
), dest_pfx
);
2255 /* We expect this to be the selected route, so we want
2256 * to tell others about this transition.
2258 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2260 /* Changed nexthops - update kernel/others */
2261 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2263 /* Redistribute, lsp, and nht update */
2264 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2266 } else if (start_count
> 0 && end_count
== 0) {
2269 "%s(%u:%u):%pFX un-installed transition from dplane notification",
2270 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2271 dplane_ctx_get_table(ctx
), dest_pfx
);
2273 /* Transition from _something_ installed to _nothing_
2276 /* We expect this to be the selected route, so we want
2277 * to tell others about this transistion.
2279 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2281 /* Changed nexthops - update kernel/others */
2282 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2284 /* Redistribute, lsp, and nht update */
2285 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
2288 /* Make any changes visible for lsp and nexthop-tracking processing */
2289 zebra_rib_evaluate_rn_nexthops(
2290 rn
, zebra_router_get_next_sequence());
2292 zebra_rib_evaluate_mpls(rn
);
2296 route_unlock_node(rn
);
2298 /* Return context to dataplane module */
2299 dplane_ctx_fini(&ctx
);
2303 * Process the nexthop-group workqueue subqueue
2305 static void process_subq_nhg(struct listnode
*lnode
)
2307 struct nhg_ctx
*ctx
;
2308 struct nhg_hash_entry
*nhe
, *newnhe
;
2309 struct wq_nhg_wrapper
*w
;
2310 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2312 w
= listgetdata(lnode
);
2317 /* Two types of object - an update from the local kernel, or
2318 * an nhg update from a daemon.
2320 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2323 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2325 "NHG Context id=%u dequeued from sub-queue %u",
2329 /* Process nexthop group updates coming 'up' from the OS */
2330 nhg_ctx_process(ctx
);
2332 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2335 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2336 zlog_debug("NHG %u dequeued from sub-queue %u",
2339 /* Process incoming nhg update, probably from a proto daemon */
2340 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2345 /* Report error to daemon via ZAPI */
2347 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2348 nhe
->zapi_session
, nhe
->id
,
2349 ZAPI_NHG_FAIL_INSTALL
);
2351 /* Free temp nhe - we own that memory. */
2352 zebra_nhg_free(nhe
);
2355 XFREE(MTYPE_WQ_NHG_WRAPPER
, w
);
2358 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2360 struct route_node
*rnode
= NULL
;
2361 rib_dest_t
*dest
= NULL
;
2362 struct zebra_vrf
*zvrf
= NULL
;
2364 rnode
= listgetdata(lnode
);
2365 dest
= rib_dest_from_rnode(rnode
);
2368 zvrf
= rib_dest_vrf(dest
);
2372 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2373 struct route_entry
*re
= NULL
;
2374 char buf
[SRCDEST2STR_BUFFER
];
2377 * rib_process may have freed the dest
2378 * as part of the garbage collection. Let's
2379 * prevent stupidity from happening.
2381 dest
= rib_dest_from_rnode(rnode
);
2383 re
= re_list_first(&dest
->routes
);
2385 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2386 zlog_debug("%s(%u:%u):%s: rn %p dequeued from sub-queue %u",
2387 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2388 buf
, rnode
, qindex
);
2392 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2393 RIB_ROUTE_QUEUED(qindex
));
2395 route_unlock_node(rnode
);
2398 /* Take a list of route_node structs and return 1, if there was a record
2399 * picked from it and processed by rib_process(). Don't process more,
2400 * than one RN record; operate only in the specified sub-queue.
2402 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2404 struct listnode
*lnode
= listhead(subq
);
2409 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2410 process_subq_nhg(lnode
);
2412 process_subq_route(lnode
, qindex
);
2414 list_delete_node(subq
, lnode
);
2419 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2420 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2421 * data is pointed to the meta queue structure.
2423 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2425 struct meta_queue
*mq
= data
;
2427 uint32_t queue_len
, queue_limit
;
2429 /* Ensure there's room for more dataplane updates */
2430 queue_limit
= dplane_get_in_queue_limit();
2431 queue_len
= dplane_get_in_queue_len();
2432 if (queue_len
> queue_limit
) {
2433 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2434 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2435 queue_len
, queue_limit
);
2437 /* Ensure that the meta-queue is actually enqueued */
2438 if (work_queue_empty(zrouter
.ribq
))
2439 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2441 return WQ_QUEUE_BLOCKED
;
2444 for (i
= 0; i
< MQ_SIZE
; i
++)
2445 if (process_subq(mq
->subq
[i
], i
)) {
2449 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2454 * Look into the RN and queue it into the highest priority queue
2455 * at this point in time for processing.
2457 * We will enqueue a route node only once per invocation.
2459 * There are two possibilities here that should be kept in mind.
2460 * If the original invocation has not been pulled off for processing
2461 * yet, A subsuquent invocation can have a route entry with a better
2462 * meta queue index value and we can have a situation where
2463 * we might have the same node enqueued 2 times. Not necessarily
2464 * an optimal situation but it should be ok.
2466 * The other possibility is that the original invocation has not
2467 * been pulled off for processing yet, A subsusquent invocation
2468 * doesn't have a route_entry with a better meta-queue and the
2469 * original metaqueue index value will win and we'll end up with
2470 * the route node enqueued once.
2472 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2474 struct route_node
*rn
= NULL
;
2475 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2476 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2478 rn
= (struct route_node
*)data
;
2480 RNODE_FOREACH_RE (rn
, curr_re
) {
2481 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2483 if (curr_qindex
<= qindex
) {
2485 qindex
= curr_qindex
;
2492 /* Invariant: at this point we always have rn->info set. */
2493 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2494 RIB_ROUTE_QUEUED(qindex
))) {
2495 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2496 rnode_debug(rn
, re
->vrf_id
,
2497 "rn %p is already queued in sub-queue %u",
2498 (void *)rn
, qindex
);
2502 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2503 listnode_add(mq
->subq
[qindex
], rn
);
2504 route_lock_node(rn
);
2507 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2508 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2509 (void *)rn
, qindex
);
2514 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
2516 struct nhg_ctx
*ctx
= NULL
;
2517 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2518 struct wq_nhg_wrapper
*w
;
2520 ctx
= (struct nhg_ctx
*)data
;
2525 w
= XCALLOC(MTYPE_WQ_NHG_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
2527 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
2530 listnode_add(mq
->subq
[qindex
], w
);
2533 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2534 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2540 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2542 struct nhg_hash_entry
*nhe
= NULL
;
2543 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2544 struct wq_nhg_wrapper
*w
;
2546 nhe
= (struct nhg_hash_entry
*)data
;
2551 w
= XCALLOC(MTYPE_WQ_NHG_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
2553 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
2556 listnode_add(mq
->subq
[qindex
], w
);
2559 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2560 zlog_debug("NHG id=%u queued into sub-queue %u",
2566 static int mq_add_handler(void *data
,
2567 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2569 if (zrouter
.ribq
== NULL
) {
2570 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2571 "%s: work_queue does not exist!", __func__
);
2576 * The RIB queue should normally be either empty or holding the only
2577 * work_queue_item element. In the latter case this element would
2578 * hold a pointer to the meta queue structure, which must be used to
2579 * actually queue the route nodes to process. So create the MQ
2580 * holder, if necessary, then push the work into it in any case.
2581 * This semantics was introduced after 0.99.9 release.
2583 if (work_queue_empty(zrouter
.ribq
))
2584 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2586 return mq_add_func(zrouter
.mq
, data
);
2589 /* Add route_node to work queue and schedule processing */
2590 int rib_queue_add(struct route_node
*rn
)
2594 /* Pointless to queue a route_node with no RIB entries to add or remove
2596 if (!rnode_to_ribs(rn
)) {
2597 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
2598 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
2599 zlog_backtrace(LOG_DEBUG
);
2603 return mq_add_handler(rn
, rib_meta_queue_add
);
2607 * Enqueue incoming nhg info from OS for processing
2609 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
2613 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
2617 * Enqueue incoming nhg from proto daemon for processing
2619 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
2624 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
2627 /* Clean up the nhg meta-queue list */
2628 static void nhg_meta_queue_free(struct list
*l
)
2630 struct wq_nhg_wrapper
*w
;
2631 struct listnode
*node
;
2633 /* Free the node wrapper object, and the struct it wraps */
2634 while ((node
= listhead(l
)) != NULL
) {
2638 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
2639 nhg_ctx_free(&w
->u
.ctx
);
2640 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
2641 zebra_nhg_free(w
->u
.nhe
);
2643 XFREE(MTYPE_WQ_NHG_WRAPPER
, w
);
2645 list_delete_node(l
, node
);
2649 /* Create new meta queue.
2650 A destructor function doesn't seem to be necessary here.
2652 static struct meta_queue
*meta_queue_new(void)
2654 struct meta_queue
*new;
2657 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2659 for (i
= 0; i
< MQ_SIZE
; i
++) {
2660 new->subq
[i
] = list_new();
2661 assert(new->subq
[i
]);
2667 void meta_queue_free(struct meta_queue
*mq
)
2671 for (i
= 0; i
< MQ_SIZE
; i
++) {
2672 /* Some subqueues may need cleanup - nhgs for example */
2673 if (i
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2674 nhg_meta_queue_free(mq
->subq
[i
]);
2676 list_delete(&mq
->subq
[i
]);
2679 XFREE(MTYPE_WORK_QUEUE
, mq
);
2682 /* initialise zebra rib work queue */
2683 static void rib_queue_init(void)
2685 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2686 "route_node processing"))) {
2687 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2688 "%s: could not initialise work queue!", __func__
);
2692 /* fill in the work queue spec */
2693 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2694 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2695 zrouter
.ribq
->spec
.completion_func
= NULL
;
2696 /* XXX: TODO: These should be runtime configurable via vty */
2697 zrouter
.ribq
->spec
.max_retries
= 3;
2698 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2699 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2701 if (!(zrouter
.mq
= meta_queue_new())) {
2702 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2703 "%s: could not initialise meta queue!", __func__
);
2709 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2713 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2714 rnh_list_init(&dest
->nht
);
2715 re_list_init(&dest
->routes
);
2716 route_lock_node(rn
); /* rn route table reference */
2723 /* RIB updates are processed via a queue of pointers to route_nodes.
2725 * The queue length is bounded by the maximal size of the routing table,
2726 * as a route_node will not be requeued, if already queued.
2728 * REs are submitted via rib_addnode or rib_delnode which set minimal
2729 * state, or static_install_route (when an existing RE is updated)
2730 * and then submit route_node to queue for best-path selection later.
2731 * Order of add/delete state changes are preserved for any given RE.
2733 * Deleted REs are reaped during best-path selection.
2736 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2737 * |-------->| | best RE, if required
2739 * static_install->|->rib_addqueue...... -> rib_process
2741 * |-------->| |-> rib_unlink
2742 * |-> set ROUTE_ENTRY_REMOVE |
2743 * rib_delnode (RE freed)
2745 * The 'info' pointer of a route_node points to a rib_dest_t
2746 * ('dest'). Queueing state for a route_node is kept on the dest. The
2747 * dest is created on-demand by rib_link() and is kept around at least
2748 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2750 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2752 * - route_nodes: refcounted by:
2753 * - dest attached to route_node:
2754 * - managed by: rib_link/rib_gc_dest
2755 * - route_node processing queue
2756 * - managed by: rib_addqueue, rib_process.
2760 /* Add RE to head of the route node. */
2761 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2765 const char *rmap_name
;
2769 dest
= rib_dest_from_rnode(rn
);
2771 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2772 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2774 dest
= zebra_rib_create_dest(rn
);
2777 re_list_add_head(&dest
->routes
, re
);
2779 afi
= (rn
->p
.family
== AF_INET
)
2781 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2782 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2783 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2785 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2786 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2791 static void rib_addnode(struct route_node
*rn
,
2792 struct route_entry
*re
, int process
)
2794 /* RE node has been un-removed before route-node is processed.
2795 * route_node must hence already be on the queue for processing..
2797 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2798 if (IS_ZEBRA_DEBUG_RIB
)
2799 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2800 (void *)rn
, (void *)re
);
2802 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2805 rib_link(rn
, re
, process
);
2811 * Detach a rib structure from a route_node.
2813 * Note that a call to rib_unlink() should be followed by a call to
2814 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2815 * longer required to be deleted.
2817 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2823 if (IS_ZEBRA_DEBUG_RIB
)
2824 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2827 dest
= rib_dest_from_rnode(rn
);
2829 re_list_del(&dest
->routes
, re
);
2831 if (dest
->selected_fib
== re
)
2832 dest
->selected_fib
= NULL
;
2834 if (re
->nhe
&& re
->nhe_id
) {
2835 assert(re
->nhe
->id
== re
->nhe_id
);
2836 zebra_nhg_decrement_ref(re
->nhe
);
2837 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2838 nexthops_free(re
->nhe
->nhg
.nexthop
);
2840 nexthops_free(re
->fib_ng
.nexthop
);
2842 zapi_opaque_free(re
->opaque
);
2844 XFREE(MTYPE_RE
, re
);
2847 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2851 if (IS_ZEBRA_DEBUG_RIB
)
2852 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2853 (void *)rn
, (void *)re
);
2854 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2856 afi
= (rn
->p
.family
== AF_INET
)
2858 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2859 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2860 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2862 zebra_del_import_table_entry(zvrf
, rn
, re
);
2863 /* Just clean up if non main table */
2864 if (IS_ZEBRA_DEBUG_RIB
) {
2865 char buf
[SRCDEST2STR_BUFFER
];
2866 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2867 zlog_debug("%s(%u):%s: Freeing route rn %p, re %p (%s)",
2868 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, buf
,
2869 rn
, re
, zebra_route_string(re
->type
));
2879 * Helper that debugs a single nexthop within a route-entry
2881 static void _route_entry_dump_nh(const struct route_entry
*re
,
2882 const char *straddr
,
2883 const struct nexthop
*nexthop
)
2885 char nhname
[PREFIX_STRLEN
];
2886 char backup_str
[50];
2889 char label_str
[MPLS_LABEL_STRLEN
];
2891 struct interface
*ifp
;
2892 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2894 switch (nexthop
->type
) {
2895 case NEXTHOP_TYPE_BLACKHOLE
:
2896 snprintf(nhname
, sizeof(nhname
), "Blackhole");
2898 case NEXTHOP_TYPE_IFINDEX
:
2899 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2900 snprintf(nhname
, sizeof(nhname
), "%s",
2901 ifp
? ifp
->name
: "Unknown");
2903 case NEXTHOP_TYPE_IPV4
:
2905 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2906 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2908 case NEXTHOP_TYPE_IPV6
:
2909 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2910 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2915 label_str
[0] = '\0';
2916 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
2917 mpls_label2str(nexthop
->nh_label
->num_labels
,
2918 nexthop
->nh_label
->label
, label_str
,
2919 sizeof(label_str
), 0 /*pretty*/);
2920 strlcat(label_str
, ", ", sizeof(label_str
));
2923 backup_str
[0] = '\0';
2924 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
2925 snprintf(backup_str
, sizeof(backup_str
), "backup ");
2926 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
2927 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
2928 nexthop
->backup_idx
[i
]);
2929 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
2934 if (nexthop
->weight
)
2935 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
2937 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s",
2938 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2939 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
2941 wgt_str
, backup_str
,
2942 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2945 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2948 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2951 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2954 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2957 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
2958 ? "FILTERED " : ""),
2959 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
2961 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
2966 /* This function dumps the contents of a given RE entry into
2967 * standard debug log. Calling function name and IP prefix in
2968 * question are passed as 1st and 2nd arguments.
2970 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2971 union prefixconstptr src_pp
,
2972 const struct route_entry
*re
)
2974 const struct prefix
*src_p
= src_pp
.p
;
2975 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2976 char straddr
[PREFIX_STRLEN
];
2977 char srcaddr
[PREFIX_STRLEN
];
2978 char flags_buf
[128];
2979 char status_buf
[128];
2980 struct nexthop
*nexthop
;
2981 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
2982 struct nexthop_group
*nhg
;
2984 prefix2str(pp
, straddr
, sizeof(straddr
));
2986 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
2987 (const void *)re
, straddr
,
2988 is_srcdst
? " from " : "",
2989 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2991 VRF_LOGNAME(vrf
), re
->vrf_id
);
2992 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2993 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2996 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
2997 straddr
, re
->metric
, re
->mtu
, re
->distance
,
2998 zclient_dump_route_flags(re
->flags
, flags_buf
,
3000 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
3001 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
3002 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
3003 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
3006 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3007 _route_entry_dump_nh(re
, straddr
, nexthop
);
3009 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
3010 zlog_debug("%s: backup nexthops:", straddr
);
3012 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
3013 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
3014 _route_entry_dump_nh(re
, straddr
, nexthop
);
3017 zlog_debug("%s: dump complete", straddr
);
3021 * This is an exported helper to rtm_read() to dump the strange
3022 * RE entry found by rib_lookup_ipv4_route()
3024 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
3026 struct route_table
*table
;
3027 struct route_node
*rn
;
3028 struct route_entry
*re
;
3031 vrf
= vrf_lookup_by_id(vrf_id
);
3034 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3036 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3037 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
3038 VRF_LOGNAME(vrf
), vrf_id
);
3042 /* Scan the RIB table for exactly matching RE entry. */
3043 rn
= route_node_lookup(table
, (struct prefix
*)p
);
3045 /* No route for this prefix. */
3047 zlog_debug("%s:%s(%u) lookup failed for %pFX", __func__
,
3048 VRF_LOGNAME(vrf
), vrf_id
, (struct prefix
*)p
);
3053 route_unlock_node(rn
);
3056 RNODE_FOREACH_RE (rn
, re
) {
3057 zlog_debug("%s:%s(%u) rn %p, re %p: %s, %s", __func__
,
3058 VRF_LOGNAME(vrf
), vrf_id
, (void *)rn
, (void *)re
,
3059 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
3062 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
3065 route_entry_dump(p
, NULL
, re
);
3069 /* Check if requested address assignment will fail due to another
3070 * route being installed by zebra in FIB already. Take necessary
3071 * actions, if needed: remove such a route from FIB and deSELECT
3072 * corresponding RE entry. Then put affected RN into RIBQ head.
3074 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
3076 struct route_table
*table
;
3077 struct route_node
*rn
;
3080 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
3081 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3083 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3084 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
3085 VRF_LOGNAME(vrf
), vrf_id
);
3089 /* No matches would be the simplest case. */
3090 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
3094 route_unlock_node(rn
);
3096 dest
= rib_dest_from_rnode(rn
);
3097 /* Check all RE entries. In case any changes have to be done, requeue
3098 * the RN into RIBQ head. If the routing message about the new connected
3099 * route (generated by the IP address we are going to assign very soon)
3100 * comes before the RIBQ is processed, the new RE entry will join
3101 * RIBQ record already on head. This is necessary for proper
3103 * of the rest of the RE.
3105 if (dest
->selected_fib
) {
3106 if (IS_ZEBRA_DEBUG_RIB
) {
3108 vrf_lookup_by_id(dest
->selected_fib
->vrf_id
);
3111 "%s(%u):%pFX: freeing way for connected prefix",
3112 VRF_LOGNAME(vrf
), dest
->selected_fib
->vrf_id
,
3114 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
3116 rib_uninstall(rn
, dest
->selected_fib
);
3122 * Internal route-add implementation; there are a couple of different public
3123 * signatures. Callers in this path are responsible for the memory they
3124 * allocate: if they allocate a nexthop_group or backup nexthop info, they
3125 * must free those objects. If this returns < 0, an error has occurred and the
3126 * route_entry 're' has not been captured; the caller should free that also.
3128 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
3129 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3130 struct nhg_hash_entry
*re_nhe
)
3132 struct nhg_hash_entry
*nhe
= NULL
;
3133 struct route_table
*table
;
3134 struct route_node
*rn
;
3135 struct route_entry
*same
= NULL
, *first_same
= NULL
;
3143 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3146 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
3151 if (re
->nhe_id
> 0) {
3152 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
3156 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3157 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
3163 /* Lookup nhe from route information */
3164 nhe
= zebra_nhg_rib_find_nhe(re_nhe
, afi
);
3166 char buf2
[PREFIX_STRLEN
] = "";
3169 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3170 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
3171 p
, src_p
? " from " : "",
3172 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
3180 * Attach the re to the nhe's nexthop group.
3182 * TODO: This will need to change when we start getting IDs from upper
3183 * level protocols, as the refcnt might be wrong, since it checks
3184 * if old_id != new_id.
3186 route_entry_update_nhe(re
, nhe
);
3188 /* Make it sure prefixlen is applied to the prefix. */
3191 apply_mask_ipv6(src_p
);
3193 /* Set default distance by route type. */
3194 if (re
->distance
== 0)
3195 re
->distance
= route_distance(re
->type
);
3197 /* Lookup route node.*/
3198 rn
= srcdest_rnode_get(table
, p
, src_p
);
3201 * If same type of route are installed, treat it as a implicit
3202 * withdraw. If the user has specified the No route replace semantics
3203 * for the install don't do a route replace.
3205 RNODE_FOREACH_RE (rn
, same
) {
3206 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
3211 /* Compare various route_entry properties */
3212 if (rib_compare_routes(re
, same
)) {
3215 if (first_same
== NULL
)
3222 /* If this route is kernel/connected route, notify the dataplane. */
3223 if (RIB_SYSTEM_ROUTE(re
)) {
3224 /* Notify dataplane */
3225 dplane_sys_route_add(rn
, re
);
3228 /* Link new re to node.*/
3229 if (IS_ZEBRA_DEBUG_RIB
) {
3230 rnode_debug(rn
, re
->vrf_id
,
3231 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
3232 rn
, re
, zebra_route_string(re
->type
), same
,
3235 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3236 route_entry_dump(p
, src_p
, re
);
3239 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3240 rib_addnode(rn
, re
, 1);
3243 /* Free implicit route.*/
3245 rib_delnode(rn
, same
);
3247 /* See if we can remove some RE entries that are queued for
3248 * removal, but won't be considered in rib processing.
3250 dest
= rib_dest_from_rnode(rn
);
3251 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
3252 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3253 /* If the route was used earlier, must retain it. */
3254 if (dest
&& re
== dest
->selected_fib
)
3257 if (IS_ZEBRA_DEBUG_RIB
)
3258 rnode_debug(rn
, re
->vrf_id
, "rn %p, removing unneeded re %p",
3265 route_unlock_node(rn
);
3270 * Add a single route.
3272 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
3273 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3274 struct nexthop_group
*ng
)
3277 struct nhg_hash_entry nhe
;
3282 /* We either need nexthop(s) or an existing nexthop id */
3283 if (ng
== NULL
&& re
->nhe_id
== 0)
3287 * Use a temporary nhe to convey info to the common/main api.
3289 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
3291 nhe
.nhg
.nexthop
= ng
->nexthop
;
3292 else if (re
->nhe_id
> 0)
3293 nhe
.id
= re
->nhe_id
;
3295 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, &nhe
);
3297 /* In this path, the callers expect memory to be freed. */
3298 nexthop_group_delete(&ng
);
3300 /* In error cases, free the route also */
3302 XFREE(MTYPE_RE
, re
);
3307 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3308 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3309 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3310 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
3311 uint8_t distance
, bool fromkernel
)
3313 struct route_table
*table
;
3314 struct route_node
*rn
;
3315 struct route_entry
*re
;
3316 struct route_entry
*fib
= NULL
;
3317 struct route_entry
*same
= NULL
;
3318 struct nexthop
*rtnh
;
3319 char buf2
[INET6_ADDRSTRLEN
];
3322 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3325 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
3333 apply_mask_ipv6(src_p
);
3335 /* Lookup route node. */
3336 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
3338 if (IS_ZEBRA_DEBUG_RIB
) {
3339 char src_buf
[PREFIX_STRLEN
];
3340 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3342 if (src_p
&& src_p
->prefixlen
)
3343 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
3347 zlog_debug("%s[%d]:%pFX%s%s doesn't exist in rib",
3348 vrf
->name
, table_id
, p
,
3349 (src_buf
[0] != '\0') ? " from " : "",
3355 dest
= rib_dest_from_rnode(rn
);
3356 fib
= dest
->selected_fib
;
3358 /* Lookup same type route. */
3359 RNODE_FOREACH_RE (rn
, re
) {
3360 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3363 if (re
->type
!= type
)
3365 if (re
->instance
!= instance
)
3367 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
3368 distance
!= re
->distance
)
3371 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
3373 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
3374 (rtnh
= re
->nhe
->nhg
.nexthop
)
3375 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
3376 if (rtnh
->ifindex
!= nh
->ifindex
)
3382 /* Make sure that the route found has the same gateway. */
3383 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
3392 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3394 * No guarantee all kernel send nh with labels
3397 if (nexthop_same_no_labels(rtnh
, nh
)) {
3406 /* If same type of route can't be found and this message is from
3410 * In the past(HA!) we could get here because
3411 * we were receiving a route delete from the
3412 * kernel and we're not marking the proto
3413 * as coming from it's appropriate originator.
3414 * Now that we are properly noticing the fact
3415 * that the kernel has deleted our route we
3416 * are not going to get called in this path
3417 * I am going to leave this here because
3418 * this might still work this way on non-linux
3419 * platforms as well as some weird state I have
3420 * not properly thought of yet.
3421 * If we can show that this code path is
3422 * dead then we can remove it.
3424 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
3425 if (IS_ZEBRA_DEBUG_RIB
) {
3426 rnode_debug(rn
, vrf_id
,
3427 "rn %p, re %p (%s) was deleted from kernel, adding",
3429 zebra_route_string(fib
->type
));
3432 || CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3433 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3435 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3437 UNSET_FLAG(rtnh
->flags
,
3441 * This is a non FRR route
3442 * as such we should mark
3445 dest
->selected_fib
= NULL
;
3447 /* This means someone else, other than Zebra,
3449 * a Zebra router from the kernel. We will add
3451 rib_install_kernel(rn
, fib
, NULL
);
3454 if (IS_ZEBRA_DEBUG_RIB
) {
3458 "via %s ifindex %d type %d doesn't exist in rib",
3459 inet_ntop(afi2family(afi
),
3466 "type %d doesn't exist in rib",
3469 route_unlock_node(rn
);
3475 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
3477 rib_install_kernel(rn
, same
, NULL
);
3478 route_unlock_node(rn
);
3483 /* Special handling for IPv4 or IPv6 routes sourced from
3484 * EVPN - the nexthop (and associated MAC) need to be
3485 * uninstalled if no more refs.
3487 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
3488 struct nexthop
*tmp_nh
;
3490 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3491 struct ipaddr vtep_ip
;
3493 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3494 if (afi
== AFI_IP
) {
3495 vtep_ip
.ipa_type
= IPADDR_V4
;
3496 memcpy(&(vtep_ip
.ipaddr_v4
),
3497 &(tmp_nh
->gate
.ipv4
),
3498 sizeof(struct in_addr
));
3500 vtep_ip
.ipa_type
= IPADDR_V6
;
3501 memcpy(&(vtep_ip
.ipaddr_v6
),
3502 &(tmp_nh
->gate
.ipv6
),
3503 sizeof(struct in6_addr
));
3505 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
3510 /* Notify dplane if system route changes */
3511 if (RIB_SYSTEM_ROUTE(re
))
3512 dplane_sys_route_del(rn
, same
);
3514 rib_delnode(rn
, same
);
3517 route_unlock_node(rn
);
3522 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3523 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3524 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3525 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3526 uint8_t distance
, route_tag_t tag
)
3528 struct route_entry
*re
= NULL
;
3529 struct nexthop
*nexthop
= NULL
;
3530 struct nexthop_group
*ng
= NULL
;
3532 /* Allocate new route_entry structure. */
3533 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3535 re
->instance
= instance
;
3536 re
->distance
= distance
;
3538 re
->metric
= metric
;
3540 re
->table
= table_id
;
3541 re
->vrf_id
= vrf_id
;
3542 re
->uptime
= monotime(NULL
);
3544 re
->nhe_id
= nhe_id
;
3546 /* If the owner of the route supplies a shared nexthop-group id,
3547 * we'll use that. Otherwise, pass the nexthop along directly.
3550 ng
= nexthop_group_new();
3553 nexthop
= nexthop_new();
3555 nexthop_group_add_sorted(ng
, nexthop
);
3558 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
3561 static const char *rib_update_event2str(enum rib_update_event event
)
3563 const char *ret
= "UNKNOWN";
3566 case RIB_UPDATE_KERNEL
:
3567 ret
= "RIB_UPDATE_KERNEL";
3569 case RIB_UPDATE_RMAP_CHANGE
:
3570 ret
= "RIB_UPDATE_RMAP_CHANGE";
3572 case RIB_UPDATE_OTHER
:
3573 ret
= "RIB_UPDATE_OTHER";
3575 case RIB_UPDATE_MAX
:
3583 /* Schedule route nodes to be processed if they match the type */
3584 static void rib_update_route_node(struct route_node
*rn
, int type
)
3586 struct route_entry
*re
, *next
;
3587 bool re_changed
= false;
3589 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3590 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3591 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3600 /* Schedule routes of a particular table (address-family) based on event. */
3601 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
3604 struct route_node
*rn
;
3606 if (IS_ZEBRA_DEBUG_EVENT
) {
3607 struct zebra_vrf
*zvrf
;
3611 ? ((struct rib_table_info
*)table
->info
)->zvrf
3613 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3615 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
3616 table
->info
? afi2str(
3617 ((struct rib_table_info
*)table
->info
)->afi
)
3619 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
3620 rib_update_event2str(event
), zebra_route_string(rtype
));
3623 /* Walk all routes and queue for processing, if appropriate for
3624 * the trigger event.
3626 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3628 * If we are looking at a route node and the node
3629 * has already been queued we don't
3630 * need to queue it up again
3633 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3634 RIB_ROUTE_ANY_QUEUED
))
3638 case RIB_UPDATE_KERNEL
:
3639 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3641 case RIB_UPDATE_RMAP_CHANGE
:
3642 case RIB_UPDATE_OTHER
:
3643 rib_update_route_node(rn
, rtype
);
3651 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
,
3654 struct route_table
*table
;
3656 if (IS_ZEBRA_DEBUG_EVENT
)
3657 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3658 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3660 /* Process routes of interested address-families. */
3661 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3663 rib_update_table(table
, event
, rtype
);
3665 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3667 rib_update_table(table
, event
, rtype
);
3670 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
3672 struct zebra_router_table
*zrt
;
3674 if (IS_ZEBRA_DEBUG_EVENT
)
3675 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3676 rib_update_event2str(event
));
3678 /* Just iterate over all the route tables, rather than vrf lookups */
3679 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3680 rib_update_table(zrt
->table
, event
, rtype
);
3683 struct rib_update_ctx
{
3684 enum rib_update_event event
;
3689 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3690 enum rib_update_event event
)
3692 struct rib_update_ctx
*ctx
;
3694 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3697 ctx
->vrf_id
= vrf_id
;
3702 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3704 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3707 static int rib_update_handler(struct thread
*thread
)
3709 struct rib_update_ctx
*ctx
;
3711 ctx
= THREAD_ARG(thread
);
3714 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
3716 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
, ZEBRA_ROUTE_ALL
);
3718 rib_update_ctx_fini(&ctx
);
3724 * Thread list to ensure we don't schedule a ton of events
3725 * if interfaces are flapping for instance.
3727 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3729 /* Schedule a RIB update event for all vrfs */
3730 void rib_update(enum rib_update_event event
)
3732 struct rib_update_ctx
*ctx
;
3734 ctx
= rib_update_ctx_init(0, event
);
3736 ctx
->vrf_all
= true;
3738 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3739 &t_rib_update_threads
[event
]))
3740 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3741 else if (IS_ZEBRA_DEBUG_EVENT
)
3742 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
3743 rib_update_event2str(event
));
3746 /* Delete self installed routes after zebra is relaunched. */
3747 void rib_sweep_table(struct route_table
*table
)
3749 struct route_node
*rn
;
3750 struct route_entry
*re
;
3751 struct route_entry
*next
;
3752 struct nexthop
*nexthop
;
3757 if (IS_ZEBRA_DEBUG_RIB
)
3758 zlog_debug("%s: starting", __func__
);
3760 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3761 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3763 if (IS_ZEBRA_DEBUG_RIB
)
3764 route_entry_dump(&rn
->p
, NULL
, re
);
3766 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3769 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3773 * If routes are older than startup_time then
3774 * we know we read them in from the kernel.
3775 * As such we can safely remove them.
3777 if (zrouter
.startup_time
< re
->uptime
)
3781 * So we are starting up and have received
3782 * routes from the kernel that we have installed
3783 * from a previous run of zebra but not cleaned
3784 * up ( say a kill -9 )
3785 * But since we haven't actually installed
3786 * them yet( we received them from the kernel )
3787 * we don't think they are active.
3788 * So let's pretend they are active to actually
3790 * In all honesty I'm not sure if we should
3791 * mark them as active when we receive them
3792 * This is startup only so probably ok.
3794 * If we ever decide to move rib_sweep_table
3795 * to a different spot (ie startup )
3796 * this decision needs to be revisited
3798 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3799 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3800 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3802 rib_uninstall_kernel(rn
, re
);
3803 rib_delnode(rn
, re
);
3807 if (IS_ZEBRA_DEBUG_RIB
)
3808 zlog_debug("%s: ends", __func__
);
3811 /* Sweep all RIB tables. */
3812 int rib_sweep_route(struct thread
*t
)
3815 struct zebra_vrf
*zvrf
;
3817 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3818 if ((zvrf
= vrf
->info
) == NULL
)
3821 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3822 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3825 zebra_router_sweep_route();
3826 zebra_router_sweep_nhgs();
3831 /* Remove specific by protocol routes from 'table'. */
3832 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3833 struct route_table
*table
)
3835 struct route_node
*rn
;
3836 struct route_entry
*re
;
3837 struct route_entry
*next
;
3838 unsigned long n
= 0;
3841 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3842 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3843 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3845 if (re
->type
== proto
3846 && re
->instance
== instance
) {
3847 rib_delnode(rn
, re
);
3854 /* Remove specific by protocol routes. */
3855 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3858 struct zebra_vrf
*zvrf
;
3859 struct other_route_table
*ort
;
3860 unsigned long cnt
= 0;
3862 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3867 cnt
+= rib_score_proto_table(proto
, instance
,
3868 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3869 + rib_score_proto_table(
3871 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3873 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3874 rib_score_proto_table(proto
, instance
, ort
->table
);
3880 /* Close RIB and clean up kernel routes. */
3881 void rib_close_table(struct route_table
*table
)
3883 struct route_node
*rn
;
3884 struct rib_table_info
*info
;
3890 info
= route_table_get_info(table
);
3892 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3893 dest
= rib_dest_from_rnode(rn
);
3895 if (dest
&& dest
->selected_fib
) {
3896 if (info
->safi
== SAFI_UNICAST
)
3897 hook_call(rib_update
, rn
, NULL
);
3899 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3900 dest
->selected_fib
= NULL
;
3906 * Handler for async dataplane results after a pseudowire installation
3908 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3910 struct zebra_pw
*pw
;
3911 struct zebra_vrf
*vrf
;
3913 /* The pseudowire code assumes success - we act on an error
3914 * result for installation attempts here.
3916 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3919 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3920 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3921 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3923 zebra_pw_install_failure(pw
,
3924 dplane_ctx_get_pw_status(ctx
));
3928 dplane_ctx_fini(&ctx
);
3935 * Handle results from the dataplane system. Dequeue update context
3936 * structs, dispatch to appropriate internal handlers.
3938 static int rib_process_dplane_results(struct thread
*thread
)
3940 struct zebra_dplane_ctx
*ctx
;
3941 struct dplane_ctx_q ctxlist
;
3942 bool shut_p
= false;
3944 /* Dequeue a list of completed updates with one lock/unlock cycle */
3947 TAILQ_INIT(&ctxlist
);
3949 /* Take lock controlling queue of results */
3950 frr_with_mutex(&dplane_mutex
) {
3951 /* Dequeue list of context structs */
3952 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3955 /* Dequeue context block */
3956 ctx
= dplane_ctx_dequeue(&ctxlist
);
3958 /* If we've emptied the results queue, we're done */
3962 /* If zebra is shutting down, avoid processing results,
3963 * just drain the results queue.
3965 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3966 memory_order_relaxed
);
3969 dplane_ctx_fini(&ctx
);
3971 ctx
= dplane_ctx_dequeue(&ctxlist
);
3978 switch (dplane_ctx_get_op(ctx
)) {
3979 case DPLANE_OP_ROUTE_INSTALL
:
3980 case DPLANE_OP_ROUTE_UPDATE
:
3981 case DPLANE_OP_ROUTE_DELETE
:
3983 /* Bit of special case for route updates
3984 * that were generated by async notifications:
3985 * we don't want to continue processing these
3988 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3989 rib_process_result(ctx
);
3991 dplane_ctx_fini(&ctx
);
3995 case DPLANE_OP_ROUTE_NOTIFY
:
3996 rib_process_dplane_notify(ctx
);
3999 case DPLANE_OP_NH_INSTALL
:
4000 case DPLANE_OP_NH_UPDATE
:
4001 case DPLANE_OP_NH_DELETE
:
4002 zebra_nhg_dplane_result(ctx
);
4005 case DPLANE_OP_LSP_INSTALL
:
4006 case DPLANE_OP_LSP_UPDATE
:
4007 case DPLANE_OP_LSP_DELETE
:
4009 /* Bit of special case for LSP updates
4010 * that were generated by async notifications:
4011 * we don't want to continue processing these.
4013 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4014 zebra_mpls_lsp_dplane_result(ctx
);
4016 dplane_ctx_fini(&ctx
);
4020 case DPLANE_OP_LSP_NOTIFY
:
4021 zebra_mpls_process_dplane_notify(ctx
);
4024 case DPLANE_OP_PW_INSTALL
:
4025 case DPLANE_OP_PW_UNINSTALL
:
4026 handle_pw_result(ctx
);
4029 case DPLANE_OP_SYS_ROUTE_ADD
:
4030 case DPLANE_OP_SYS_ROUTE_DELETE
:
4031 /* No further processing in zebra for these. */
4032 dplane_ctx_fini(&ctx
);
4035 case DPLANE_OP_MAC_INSTALL
:
4036 case DPLANE_OP_MAC_DELETE
:
4037 zebra_vxlan_handle_result(ctx
);
4040 case DPLANE_OP_RULE_ADD
:
4041 case DPLANE_OP_RULE_DELETE
:
4042 case DPLANE_OP_RULE_UPDATE
:
4043 case DPLANE_OP_IPTABLE_ADD
:
4044 case DPLANE_OP_IPTABLE_DELETE
:
4045 case DPLANE_OP_IPSET_ADD
:
4046 case DPLANE_OP_IPSET_DELETE
:
4047 case DPLANE_OP_IPSET_ENTRY_ADD
:
4048 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4049 zebra_pbr_dplane_result(ctx
);
4052 /* Some op codes not handled here */
4053 case DPLANE_OP_ADDR_INSTALL
:
4054 case DPLANE_OP_ADDR_UNINSTALL
:
4055 case DPLANE_OP_NEIGH_INSTALL
:
4056 case DPLANE_OP_NEIGH_UPDATE
:
4057 case DPLANE_OP_NEIGH_DELETE
:
4058 case DPLANE_OP_NEIGH_IP_INSTALL
:
4059 case DPLANE_OP_NEIGH_IP_DELETE
:
4060 case DPLANE_OP_VTEP_ADD
:
4061 case DPLANE_OP_VTEP_DELETE
:
4062 case DPLANE_OP_NEIGH_DISCOVER
:
4063 case DPLANE_OP_BR_PORT_UPDATE
:
4064 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4065 case DPLANE_OP_NONE
:
4066 /* Don't expect this: just return the struct? */
4067 dplane_ctx_fini(&ctx
);
4070 } /* Dispatch by op code */
4072 ctx
= dplane_ctx_dequeue(&ctxlist
);
4081 * Results are returned from the dataplane subsystem, in the context of
4082 * the dataplane pthread. We enqueue the results here for processing by
4083 * the main thread later.
4085 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
4087 /* Take lock controlling queue of results */
4088 frr_with_mutex(&dplane_mutex
) {
4089 /* Enqueue context blocks */
4090 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4093 /* Ensure event is signalled to zebra main pthread */
4094 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4101 * Ensure there are no empty slots in the route_info array.
4102 * Every route type in zebra should be present there.
4104 static void check_route_info(void)
4106 int len
= array_size(route_info
);
4109 * ZEBRA_ROUTE_SYSTEM is special cased since
4110 * its key is 0 anyway.
4112 * ZEBRA_ROUTE_ALL is also ignored.
4114 for (int i
= 0; i
< len
; i
++) {
4115 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
4117 assert(route_info
[i
].key
);
4118 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4122 /* Routing information base initialize. */
4129 /* Init dataplane, and register for results */
4130 pthread_mutex_init(&dplane_mutex
, NULL
);
4131 TAILQ_INIT(&rib_dplane_q
);
4132 zebra_dplane_init(rib_dplane_results
);
4138 * Get the first vrf id that is greater than the given vrf id if any.
4140 * Returns true if a vrf id was found, false otherwise.
4142 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4146 vrf
= vrf_lookup_by_id(vrf_id
);
4148 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4150 *next_id_p
= vrf
->vrf_id
;
4159 * rib_tables_iter_next
4161 * Returns the next table in the iteration.
4163 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4165 struct route_table
*table
;
4168 * Array that helps us go over all AFI/SAFI combinations via one
4171 static const struct {
4175 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4176 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4177 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4182 switch (iter
->state
) {
4184 case RIB_TABLES_ITER_S_INIT
:
4185 iter
->vrf_id
= VRF_DEFAULT
;
4186 iter
->afi_safi_ix
= -1;
4190 case RIB_TABLES_ITER_S_ITERATING
:
4191 iter
->afi_safi_ix
++;
4194 while (iter
->afi_safi_ix
4195 < (int)array_size(afi_safis
)) {
4196 table
= zebra_vrf_table(
4197 afi_safis
[iter
->afi_safi_ix
].afi
,
4198 afi_safis
[iter
->afi_safi_ix
].safi
,
4203 iter
->afi_safi_ix
++;
4207 * Found another table in this vrf.
4213 * Done with all tables in the current vrf, go to the
4217 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4220 iter
->afi_safi_ix
= 0;
4225 case RIB_TABLES_ITER_S_DONE
:
4230 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4232 iter
->state
= RIB_TABLES_ITER_S_DONE
;