1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
39 #include "nexthop_group_private.h"
40 #include "frr_pthread.h"
42 #include "frrscript.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra/connected.h"
46 #include "zebra/debug.h"
47 #include "zebra/interface.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/rib.h"
51 #include "zebra/zapi_msg.h"
52 #include "zebra/zebra_errors.h"
53 #include "zebra/zebra_ns.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/zebra_routemap.h"
56 #include "zebra/zebra_vrf.h"
57 #include "zebra/zebra_vxlan.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_dplane.h"
60 #include "zebra/zebra_evpn_mh.h"
61 #include "zebra/zebra_script.h"
63 DEFINE_MGROUP(ZEBRA
, "zebra");
65 DEFINE_MTYPE(ZEBRA
, RE
, "Route Entry");
66 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_DEST
, "RIB destination");
67 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
68 DEFINE_MTYPE_STATIC(ZEBRA
, WQ_WRAPPER
, "WQ wrapper");
71 * Event, list, and mutex for delivery of dataplane results
73 static pthread_mutex_t dplane_mutex
;
74 static struct thread
*t_dplane
;
75 static struct dplane_ctx_list_head rib_dplane_q
;
77 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
79 DEFINE_HOOK(rib_shutdown
, (struct route_node
* rn
), (rn
));
82 /* Meta Q's specific names */
83 enum meta_queue_indexes
{
86 META_QUEUE_EARLY_ROUTE
,
87 META_QUEUE_EARLY_LABEL
,
96 /* Each route type's string and default distance value. */
100 enum meta_queue_indexes meta_q_map
;
101 } route_info
[ZEBRA_ROUTE_MAX
] = {
104 ZEBRA_MAX_DISTANCE_DEFAULT
/* Unneeded for nhg's */,
106 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
,
107 ZEBRA_KERNEL_DISTANCE_DEFAULT
,
109 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
,
110 ZEBRA_KERNEL_DISTANCE_DEFAULT
,
112 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
,
113 ZEBRA_CONNECT_DISTANCE_DEFAULT
,
114 META_QUEUE_CONNECTED
},
115 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
,
116 ZEBRA_STATIC_DISTANCE_DEFAULT
,
118 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, ZEBRA_RIP_DISTANCE_DEFAULT
,
120 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, ZEBRA_RIP_DISTANCE_DEFAULT
,
122 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, ZEBRA_OSPF_DISTANCE_DEFAULT
,
124 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, ZEBRA_OSPF6_DISTANCE_DEFAULT
,
126 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, ZEBRA_ISIS_DISTANCE_DEFAULT
,
128 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
,
129 ZEBRA_EBGP_DISTANCE_DEFAULT
/* IBGP is 200. */,
131 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, ZEBRA_MAX_DISTANCE_DEFAULT
,
133 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, ZEBRA_EIGRP_DISTANCE_DEFAULT
,
135 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, ZEBRA_NHRP_DISTANCE_DEFAULT
,
137 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, ZEBRA_MAX_DISTANCE_DEFAULT
,
139 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, ZEBRA_MAX_DISTANCE_DEFAULT
,
141 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, ZEBRA_TABLE_DISTANCE_DEFAULT
, META_QUEUE_STATIC
},
142 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, ZEBRA_LDP_DISTANCE_DEFAULT
,
144 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, ZEBRA_EBGP_DISTANCE_DEFAULT
,
146 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
,
147 ZEBRA_EBGP_DISTANCE_DEFAULT
,
149 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
,
150 ZEBRA_EBGP_DISTANCE_DEFAULT
,
152 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
,
153 ZEBRA_EBGP_DISTANCE_DEFAULT
,
155 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
,
156 ZEBRA_EBGP_DISTANCE_DEFAULT
,
158 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, ZEBRA_BABEL_DISTANCE_DEFAULT
,
160 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, ZEBRA_SHARP_DISTANCE_DEFAULT
,
162 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, ZEBRA_PBR_DISTANCE_DEFAULT
,
164 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, ZEBRA_MAX_DISTANCE_DEFAULT
,
166 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
,
167 ZEBRA_OPENFABRIC_DISTANCE_DEFAULT
,
169 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, ZEBRA_MAX_DISTANCE_DEFAULT
,
171 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, ZEBRA_MAX_DISTANCE_DEFAULT
,
173 [ZEBRA_ROUTE_ALL
] = {ZEBRA_ROUTE_ALL
, ZEBRA_MAX_DISTANCE_DEFAULT
,
175 /* Any new route type added to zebra, should be mirrored here */
177 /* no entry/default: 150 */
180 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
181 * from the OS, and an 'nhe' is a nhe update.
183 struct wq_nhg_wrapper
{
187 struct nhg_hash_entry
*nhe
;
191 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
192 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
194 /* Wrapper structs for evpn/vxlan workqueue items. */
195 struct wq_evpn_wrapper
{
207 struct ethaddr macaddr
;
208 struct prefix prefix
;
209 struct in_addr vtep_ip
;
212 #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
213 #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
214 #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
215 #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
217 enum wq_label_types
{
218 WQ_LABEL_FTN_UNINSTALL
,
219 WQ_LABEL_LABELS_PROCESS
,
222 struct wq_label_wrapper
{
223 enum wq_label_types type
;
227 enum lsp_types_t ltype
;
229 uint8_t route_instance
;
232 struct zapi_labels zl
;
237 static void rib_addnode(struct route_node
*rn
, struct route_entry
*re
,
240 /* %pRN is already a printer for route_nodes that just prints the prefix */
241 #ifdef _FRR_ATTRIBUTE_PRINTFRR
242 #pragma FRR printfrr_ext "%pZN" (struct route_node *)
245 static const char *subqueue2str(enum meta_queue_indexes index
)
249 return "NHG Objects";
250 case META_QUEUE_EVPN
:
251 return "EVPN/VxLan Objects";
252 case META_QUEUE_EARLY_ROUTE
:
253 return "Early Route Processing";
254 case META_QUEUE_EARLY_LABEL
:
255 return "Early Label Handling";
256 case META_QUEUE_CONNECTED
:
257 return "Connected Routes";
258 case META_QUEUE_KERNEL
:
259 return "Kernel Routes";
260 case META_QUEUE_STATIC
:
261 return "Static Routes";
262 case META_QUEUE_NOTBGP
:
263 return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
266 case META_QUEUE_OTHER
:
267 return "Other Routes";
273 printfrr_ext_autoreg_p("ZN", printfrr_zebra_node
);
274 static ssize_t
printfrr_zebra_node(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
277 struct route_node
*rn
= (struct route_node
*)ptr
;
280 /* just the table number? */
281 if (ea
->fmt
[0] == 't') {
283 struct route_entry
*re
= NULL
;
288 return bputch(buf
, '!');
290 dest
= rib_dest_from_rnode(rn
);
292 re
= re_list_first(&dest
->routes
);
294 rv
+= bprintfrr(buf
, "%u", re
->table
);
296 rv
+= bputch(buf
, '?');
299 char cbuf
[PREFIX_STRLEN
* 2 + 6];
300 struct rib_table_info
*info
;
303 return bputs(buf
, "{(route_node *) NULL}");
305 srcdest_rnode2str(rn
, cbuf
, sizeof(cbuf
));
306 rv
+= bputs(buf
, cbuf
);
308 info
= srcdest_rnode_table_info(rn
);
309 if (info
->safi
== SAFI_MULTICAST
)
310 rv
+= bputs(buf
, " (MRIB)");
315 #define rnode_debug(node, vrf_id, msg, ...) \
316 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
319 #define rnode_info(node, vrf_id, msg, ...) \
320 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
323 static char *_dump_re_status(const struct route_entry
*re
, char *buf
,
326 if (re
->status
== 0) {
327 snprintfrr(buf
, len
, "None ");
332 buf
, len
, "%s%s%s%s%s%s%s%s",
333 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
) ? "Removed " : "",
334 CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) ? "Changed " : "",
335 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
338 CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
) ? "Queued " : "",
339 CHECK_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
)
342 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
344 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
345 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
350 uint8_t route_distance(int type
)
354 if ((unsigned)type
>= array_size(route_info
))
357 distance
= route_info
[type
].distance
;
362 int is_zebra_valid_kernel_table(uint32_t table_id
)
365 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
366 || (table_id
== RT_TABLE_COMPAT
))
373 int is_zebra_main_routing_table(uint32_t table_id
)
375 if (table_id
== RT_TABLE_MAIN
)
380 int zebra_check_addr(const struct prefix
*p
)
382 if (p
->family
== AF_INET
) {
385 addr
= p
->u
.prefix4
.s_addr
;
388 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
389 || IPV4_LINKLOCAL(addr
))
392 if (p
->family
== AF_INET6
) {
393 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
395 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
401 static void route_entry_attach_ref(struct route_entry
*re
,
402 struct nhg_hash_entry
*new)
405 re
->nhe_id
= new->id
;
406 re
->nhe_installed_id
= 0;
408 zebra_nhg_increment_ref(new);
411 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
412 int route_entry_update_nhe(struct route_entry
*re
,
413 struct nhg_hash_entry
*new_nhghe
)
416 struct nhg_hash_entry
*old_nhg
= NULL
;
418 if (new_nhghe
== NULL
) {
422 re
->nhe_installed_id
= 0;
427 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
428 /* Capture previous nhg, if any */
431 route_entry_attach_ref(re
, new_nhghe
);
433 /* This is the first time it's being attached */
434 route_entry_attach_ref(re
, new_nhghe
);
437 /* Detach / deref previous nhg */
439 zebra_nhg_decrement_ref(old_nhg
);
444 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
445 struct nhg_hash_entry
*new_entry
)
447 struct zebra_router_table
*zrt
;
448 struct route_node
*rn
;
449 struct route_entry
*re
, *next
;
451 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
452 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
453 __func__
, new_entry
->id
, new_entry
, old_entry
);
455 /* We have to do them ALL */
456 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
457 for (rn
= route_top(zrt
->table
); rn
;
458 rn
= srcdest_route_next(rn
)) {
459 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
460 if (re
->nhe
&& re
->nhe
== old_entry
)
461 route_entry_update_nhe(re
, new_entry
);
467 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
468 const union g_addr
*addr
,
469 struct route_node
**rn_out
)
472 struct route_table
*table
;
473 struct route_node
*rn
;
474 struct route_entry
*match
= NULL
;
477 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
481 memset(&p
, 0, sizeof(p
));
484 p
.u
.prefix4
= addr
->ipv4
;
485 p
.prefixlen
= IPV4_MAX_BITLEN
;
487 p
.u
.prefix6
= addr
->ipv6
;
488 p
.prefixlen
= IPV6_MAX_BITLEN
;
491 rn
= route_node_match(table
, &p
);
496 route_unlock_node(rn
);
498 dest
= rib_dest_from_rnode(rn
);
499 if (dest
&& dest
->selected_fib
500 && !CHECK_FLAG(dest
->selected_fib
->status
,
501 ROUTE_ENTRY_REMOVED
))
502 match
= dest
->selected_fib
;
504 /* If there is no selected route or matched route is EGP, go up
509 } while (rn
&& rn
->info
== NULL
);
513 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
514 if (!CHECK_FLAG(match
->status
,
515 ROUTE_ENTRY_INSTALLED
))
527 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
529 struct route_node
**rn_out
)
531 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
532 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
533 union g_addr gaddr
= {.ipv4
= addr
};
535 switch (zrouter
.ipv4_multicast_mode
) {
536 case MCAST_MRIB_ONLY
:
537 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
539 case MCAST_URIB_ONLY
:
540 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
541 case MCAST_NO_CONFIG
:
542 case MCAST_MIX_MRIB_FIRST
:
543 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
546 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
549 case MCAST_MIX_DISTANCE
:
550 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
551 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
553 re
= ure
->distance
< mre
->distance
? ure
: mre
;
559 case MCAST_MIX_PFXLEN
:
560 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
561 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
563 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
572 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
574 if (IS_ZEBRA_DEBUG_RIB
) {
576 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
578 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
579 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
580 mre
? (ure
? "MRIB+URIB" : "MRIB")
581 : ure
? "URIB" : "nothing",
582 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
587 struct route_entry
*rib_match_ipv6_multicast(vrf_id_t vrf_id
,
588 struct in6_addr addr
,
589 struct route_node
**rn_out
)
591 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
592 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
593 union g_addr gaddr
= {.ipv6
= addr
};
595 switch (zrouter
.ipv4_multicast_mode
) {
596 case MCAST_MRIB_ONLY
:
597 return rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
599 case MCAST_URIB_ONLY
:
600 return rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
601 case MCAST_NO_CONFIG
:
602 case MCAST_MIX_MRIB_FIRST
:
603 re
= mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
606 re
= ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
,
609 case MCAST_MIX_DISTANCE
:
610 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
611 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
613 re
= ure
->distance
< mre
->distance
? ure
: mre
;
619 case MCAST_MIX_PFXLEN
:
620 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
621 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
623 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
632 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
634 if (IS_ZEBRA_DEBUG_RIB
)
635 zlog_debug("%s: %pI6: vrf: %s(%u) found %s, using %s", __func__
,
636 &addr
, vrf_id_to_name(vrf_id
), vrf_id
,
637 mre
? (ure
? "MRIB+URIB" : "MRIB")
638 : ure
? "URIB" : "nothing",
639 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
643 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
645 struct route_table
*table
;
646 struct route_node
*rn
;
647 struct route_entry
*match
= NULL
;
651 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
655 rn
= route_node_lookup(table
, (struct prefix
*)p
);
657 /* No route for this prefix. */
662 route_unlock_node(rn
);
663 dest
= rib_dest_from_rnode(rn
);
665 if (dest
&& dest
->selected_fib
666 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
667 match
= dest
->selected_fib
;
672 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
675 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
682 * Is this RIB labeled-unicast? It must be of type BGP and all paths
683 * (nexthops) must have a label.
685 int zebra_rib_labeled_unicast(struct route_entry
*re
)
687 struct nexthop
*nexthop
= NULL
;
689 if (re
->type
!= ZEBRA_ROUTE_BGP
)
692 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
693 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
699 /* Update flag indicates whether this is a "replace" or not. Currently, this
700 * is only used for IPv4.
702 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
703 struct route_entry
*old
)
705 struct nexthop
*nexthop
;
706 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
707 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
708 const struct prefix
*p
, *src_p
;
709 enum zebra_dplane_result ret
;
711 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
713 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
715 if (info
->safi
!= SAFI_UNICAST
) {
716 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
717 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
723 * Install the resolved nexthop object first.
725 zebra_nhg_install_kernel(re
->nhe
);
728 * If this is a replace to a new RE let the originator of the RE
729 * know that they've lost
731 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
732 zsend_route_notify_owner(rn
, old
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
733 info
->afi
, info
->safi
);
735 /* Update fib selection */
736 dest
->selected_fib
= re
;
739 * Make sure we update the FPM any time we send new information to
742 hook_call(rib_update
, rn
, "installing in kernel");
744 /* Send add or update */
746 ret
= dplane_route_update(rn
, re
, old
);
748 ret
= dplane_route_add(rn
, re
);
751 case ZEBRA_DPLANE_REQUEST_QUEUED
:
752 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
755 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
756 SET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
758 /* Free old FIB nexthop group */
759 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
760 if (old
->fib_ng
.nexthop
) {
761 nexthops_free(old
->fib_ng
.nexthop
);
762 old
->fib_ng
.nexthop
= NULL
;
767 zvrf
->installs_queued
++;
769 case ZEBRA_DPLANE_REQUEST_FAILURE
:
771 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
772 "%u:%u:%pRN: Failed to enqueue dataplane install",
773 re
->vrf_id
, re
->table
, rn
);
776 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
785 /* Uninstall the route from kernel. */
786 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
788 struct nexthop
*nexthop
;
789 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
790 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
792 if (info
->safi
!= SAFI_UNICAST
) {
793 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
794 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
795 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
800 * Make sure we update the FPM any time we send new information to
803 hook_call(rib_update
, rn
, "uninstalling from kernel");
805 switch (dplane_route_delete(rn
, re
)) {
806 case ZEBRA_DPLANE_REQUEST_QUEUED
:
808 zvrf
->removals_queued
++;
810 case ZEBRA_DPLANE_REQUEST_FAILURE
:
811 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
812 "%u:%pRN: Failed to enqueue dataplane uninstall",
815 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
825 * rib_can_delete_dest
827 * Returns true if the given dest can be deleted from the table.
829 static int rib_can_delete_dest(rib_dest_t
*dest
)
831 if (re_list_first(&dest
->routes
)) {
836 * Unresolved rnh's are stored on the default route's list
838 * dest->rnode can also be the source prefix node in an
839 * ipv6 sourcedest table. Fortunately the prefix of a
840 * source prefix node can never be the default prefix.
842 if (is_default_prefix(&dest
->rnode
->p
))
846 * Don't delete the dest if we have to update the FPM about this
849 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
850 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
856 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
,
859 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
863 * We are storing the rnh's associated withb
864 * the tracked nexthop as a list of the rn's.
865 * Unresolved rnh's are placed at the top
866 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
867 * As such for each rn we need to walk up the tree
868 * and see if any rnh's need to see if they
869 * would match a more specific route
872 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
874 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
876 dest
? rnh_list_count(&dest
->nht
) : 0);
878 if (rt_delete
&& (!dest
|| !rnh_list_count(&dest
->nht
))) {
879 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
880 zlog_debug("%pRN has no tracking NHTs. Bailing",
887 dest
= rib_dest_from_rnode(rn
);
891 * If we have any rnh's stored in the nht list
892 * then we know that this route node was used for
893 * nht resolution and as such we need to call the
894 * nexthop tracking evaluation code
896 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
897 struct zebra_vrf
*zvrf
=
898 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
899 struct prefix
*p
= &rnh
->node
->p
;
901 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
903 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
904 zvrf_name(zvrf
), zvrf_id(zvrf
), rn
,
905 rnh
->node
, seq
, rnh
->seqno
);
908 * If we have evaluated this node on this pass
909 * already, due to following the tree up
910 * then we know that we can move onto the next
913 * Additionally we call zebra_evaluate_rnh
914 * when we gc the dest. In this case we know
915 * that there must be no other re's where
916 * we were originally as such we know that
917 * that sequence number is ok to respect.
919 if (rnh
->seqno
== seq
) {
920 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
922 " Node processed and moved already");
927 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0, p
,
933 dest
= rib_dest_from_rnode(rn
);
940 * Garbage collect the rib dest corresponding to the given route node
943 * Returns true if the dest was deleted, false otherwise.
945 int rib_gc_dest(struct route_node
*rn
)
949 dest
= rib_dest_from_rnode(rn
);
953 if (!rib_can_delete_dest(dest
))
956 if (IS_ZEBRA_DEBUG_RIB
) {
957 struct zebra_vrf
*zvrf
;
959 zvrf
= rib_dest_vrf(dest
);
960 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
963 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
967 rnh_list_fini(&dest
->nht
);
968 XFREE(MTYPE_RIB_DEST
, dest
);
972 * Release the one reference that we keep on the route node.
974 route_unlock_node(rn
);
978 void zebra_rtable_node_cleanup(struct route_table
*table
,
979 struct route_node
*node
)
981 struct route_entry
*re
, *next
;
983 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
984 rib_unlink(node
, re
);
988 rib_dest_t
*dest
= node
->info
;
990 /* Remove from update queue of FPM module */
991 hook_call(rib_shutdown
, node
);
993 rnh_list_fini(&dest
->nht
);
994 XFREE(MTYPE_RIB_DEST
, node
->info
);
998 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
999 struct route_entry
*new)
1001 hook_call(rib_update
, rn
, "new route selected");
1003 /* Update real nexthop. This may actually determine if nexthop is active
1005 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
1006 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1010 if (IS_ZEBRA_DEBUG_RIB
)
1011 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
1012 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, rn
, rn
,
1013 new, zebra_route_string(new->type
));
1015 /* If labeled-unicast route, install transit LSP. */
1016 if (zebra_rib_labeled_unicast(new))
1017 zebra_mpls_lsp_install(zvrf
, rn
, new);
1019 rib_install_kernel(rn
, new, NULL
);
1021 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1024 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1025 struct route_entry
*old
)
1027 hook_call(rib_update
, rn
, "removing existing route");
1029 /* Uninstall from kernel. */
1030 if (IS_ZEBRA_DEBUG_RIB
)
1031 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
1032 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, rn
, rn
,
1033 old
, zebra_route_string(old
->type
));
1035 /* If labeled-unicast route, uninstall transit LSP. */
1036 if (zebra_rib_labeled_unicast(old
))
1037 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1039 rib_uninstall_kernel(rn
, old
);
1041 /* Update nexthop for route, reset changed flag. */
1042 /* Note: this code also handles the Linux case when an interface goes
1043 * down, causing the kernel to delete routes without sending DELROUTE
1046 if (RIB_KERNEL_ROUTE(old
))
1047 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1049 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1052 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1053 struct route_node
*rn
,
1054 struct route_entry
*old
,
1055 struct route_entry
*new)
1060 * We have to install or update if a new route has been selected or
1061 * something has changed.
1063 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1064 hook_call(rib_update
, rn
, "updating existing route");
1066 /* Update the nexthop; we could determine here that nexthop is
1068 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
1071 /* If nexthop is active, install the selected route, if
1073 * the install succeeds, cleanup flags for prior route, if
1078 if (IS_ZEBRA_DEBUG_RIB
) {
1081 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
1082 zvrf_name(zvrf
), zvrf_id(zvrf
),
1083 new->table
, rn
, rn
, new,
1084 zebra_route_string(new->type
),
1086 zebra_route_string(old
->type
));
1089 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
1090 zvrf_name(zvrf
), zvrf_id(zvrf
),
1091 new->table
, rn
, rn
, new,
1092 zebra_route_string(new->type
));
1095 /* If labeled-unicast route, uninstall transit LSP. */
1096 if (zebra_rib_labeled_unicast(old
))
1097 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1100 * Non-system route should be installed.
1101 * If labeled-unicast route, install transit
1104 if (zebra_rib_labeled_unicast(new))
1105 zebra_mpls_lsp_install(zvrf
, rn
, new);
1107 rib_install_kernel(rn
, new, old
);
1111 * If nexthop for selected route is not active or install
1113 * may need to uninstall and delete for redistribution.
1116 if (IS_ZEBRA_DEBUG_RIB
) {
1119 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1120 zvrf_name(zvrf
), zvrf_id(zvrf
),
1121 new->table
, rn
, rn
, new,
1122 zebra_route_string(new->type
),
1124 zebra_route_string(old
->type
));
1127 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
1128 zvrf_name(zvrf
), zvrf_id(zvrf
),
1129 new->table
, rn
, rn
, new,
1130 zebra_route_string(new->type
));
1134 * When we have gotten to this point
1135 * the new route entry has no nexthops
1136 * that are usable and as such we need
1137 * to remove the old route, but only
1138 * if we were the one who installed
1141 if (!RIB_SYSTEM_ROUTE(old
)) {
1142 /* If labeled-unicast route, uninstall transit
1144 if (zebra_rib_labeled_unicast(old
))
1145 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1147 rib_uninstall_kernel(rn
, old
);
1152 * Same route selected; check if in the FIB and if not,
1153 * re-install. This is housekeeping code to deal with
1154 * race conditions in kernel with linux netlink reporting
1155 * interface up before IPv4 or IPv6 protocol is ready
1158 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1159 RIB_SYSTEM_ROUTE(new))
1160 rib_install_kernel(rn
, new, NULL
);
1163 /* Update prior route. */
1165 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1167 /* Clear changed flag. */
1168 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1171 /* Check if 'alternate' RIB entry is better than 'current'. */
1172 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1173 struct route_entry
*alternate
)
1175 if (current
== NULL
)
1178 /* filter route selection in following order:
1179 * - connected beats other types
1180 * - if both connected, loopback or vrf wins
1181 * - lower distance beats higher
1182 * - lower metric beats higher for equal distance
1183 * - last, hence oldest, route wins tie break.
1186 /* Connected routes. Check to see if either are a vrf
1187 * or loopback interface. If not, pick the last connected
1188 * route of the set of lowest metric connected routes.
1190 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1191 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1194 /* both are connected. are either loop or vrf? */
1195 struct nexthop
*nexthop
= NULL
;
1197 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1198 struct interface
*ifp
= if_lookup_by_index(
1199 nexthop
->ifindex
, alternate
->vrf_id
);
1201 if (ifp
&& if_is_loopback(ifp
))
1205 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1206 struct interface
*ifp
= if_lookup_by_index(
1207 nexthop
->ifindex
, current
->vrf_id
);
1209 if (ifp
&& if_is_loopback(ifp
))
1213 /* Neither are loop or vrf so pick best metric */
1214 if (alternate
->metric
<= current
->metric
)
1220 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1223 /* higher distance loses */
1224 if (alternate
->distance
< current
->distance
)
1226 if (current
->distance
< alternate
->distance
)
1229 /* metric tie-breaks equal distance */
1230 if (alternate
->metric
<= current
->metric
)
1236 /* Core function for processing routing information base. */
1237 static void rib_process(struct route_node
*rn
)
1239 struct route_entry
*re
;
1240 struct route_entry
*next
;
1241 struct route_entry
*old_selected
= NULL
;
1242 struct route_entry
*new_selected
= NULL
;
1243 struct route_entry
*old_fib
= NULL
;
1244 struct route_entry
*new_fib
= NULL
;
1245 struct route_entry
*best
= NULL
;
1247 struct zebra_vrf
*zvrf
= NULL
;
1250 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1254 dest
= rib_dest_from_rnode(rn
);
1256 * We have an enqueued node with nothing to process here
1257 * let's just finish up and return;
1262 zvrf
= rib_dest_vrf(dest
);
1263 vrf_id
= zvrf_id(zvrf
);
1265 vrf
= vrf_lookup_by_id(vrf_id
);
1268 * we can have rn's that have a NULL info pointer
1269 * (dest). As such let's not let the deref happen
1270 * additionally we know RNODE_FOREACH_RE_SAFE
1271 * will not iterate so we are ok.
1273 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1274 struct route_entry
*re
= re_list_first(&dest
->routes
);
1276 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1277 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
,
1281 old_fib
= dest
->selected_fib
;
1283 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1284 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1285 char flags_buf
[128];
1286 char status_buf
[128];
1289 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1290 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
, re
,
1291 zebra_route_string(re
->type
),
1292 _dump_re_status(re
, status_buf
,
1293 sizeof(status_buf
)),
1294 zclient_dump_route_flags(re
->flags
, flags_buf
,
1296 re
->distance
, re
->metric
);
1299 /* Currently selected re. */
1300 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1301 assert(old_selected
== NULL
);
1305 /* Skip deleted entries from selection */
1306 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1310 * If the route entry has changed, verify/resolve
1311 * the nexthops associated with the entry.
1313 * In any event if we have nexthops that are not active
1314 * then we cannot use this particular route entry so
1317 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1318 if (!nexthop_active_update(rn
, re
)) {
1319 const struct prefix
*p
;
1320 struct rib_table_info
*info
;
1322 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1323 /* XXX: HERE BE DRAGONS!!!!!
1324 * In all honesty, I have not yet
1325 * figured out what this part does or
1326 * why the ROUTE_ENTRY_CHANGED test
1327 * above is correct or why we need to
1328 * delete a route here, and also not
1329 * whether this concerns both selected
1330 * and fib route, or only selected
1333 * This entry was denied by the 'ip
1335 * table' route-map, we need to delete
1337 if (re
!= old_selected
) {
1338 if (IS_ZEBRA_DEBUG_RIB
)
1340 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1348 SET_FLAG(re
->status
,
1349 ROUTE_ENTRY_REMOVED
);
1352 info
= srcdest_rnode_table_info(rn
);
1353 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1354 zsend_route_notify_owner(
1355 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
1356 info
->afi
, info
->safi
);
1361 * If the re has not changed and the nhg we have is
1362 * not usable, then we cannot use this route entry
1363 * for consideration, as that the route will just
1364 * not install if it is selected.
1366 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1370 /* Infinite distance. */
1371 if (re
->distance
== DISTANCE_INFINITY
&&
1372 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1373 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1377 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1378 best
= rib_choose_best(new_fib
, re
);
1379 if (new_fib
&& best
!= new_fib
)
1380 UNSET_FLAG(new_fib
->status
,
1381 ROUTE_ENTRY_CHANGED
);
1384 best
= rib_choose_best(new_selected
, re
);
1385 if (new_selected
&& best
!= new_selected
)
1386 UNSET_FLAG(new_selected
->status
,
1387 ROUTE_ENTRY_CHANGED
);
1388 new_selected
= best
;
1391 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1392 } /* RNODE_FOREACH_RE */
1394 /* If no FIB override route, use the selected route also for FIB */
1395 if (new_fib
== NULL
)
1396 new_fib
= new_selected
;
1398 /* After the cycle is finished, the following pointers will be set:
1399 * old_selected --- RE entry currently having SELECTED
1400 * new_selected --- RE entry that is newly SELECTED
1401 * old_fib --- RE entry currently in kernel FIB
1402 * new_fib --- RE entry that is newly to be in kernel FIB
1404 * new_selected will get SELECTED flag, and is going to be redistributed
1405 * the zclients. new_fib (which can be new_selected) will be installed
1409 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1410 struct route_entry
*entry
;
1412 entry
= old_selected
1417 : new_fib
? new_fib
: NULL
;
1420 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1421 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, rn
,
1422 (void *)old_selected
, (void *)new_selected
,
1423 (void *)old_fib
, (void *)new_fib
);
1426 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1427 * fib == selected */
1428 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1429 ROUTE_ENTRY_CHANGED
);
1431 /* Update SELECTED entry */
1432 if (old_selected
!= new_selected
|| selected_changed
) {
1434 if (new_selected
&& new_selected
!= new_fib
)
1435 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1438 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1442 * If we're removing the old entry, we should tell
1443 * redist subscribers about that *if* they aren't
1444 * going to see a redist for the new entry.
1446 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1447 ROUTE_ENTRY_REMOVED
))
1448 redistribute_delete(rn
, old_selected
,
1451 if (old_selected
!= new_selected
)
1452 UNSET_FLAG(old_selected
->flags
,
1453 ZEBRA_FLAG_SELECTED
);
1457 /* Update fib according to selection results */
1458 if (new_fib
&& old_fib
)
1459 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1461 rib_process_add_fib(zvrf
, rn
, new_fib
);
1463 rib_process_del_fib(zvrf
, rn
, old_fib
);
1465 /* Remove all RE entries queued for removal */
1466 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1467 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1468 if (IS_ZEBRA_DEBUG_RIB
) {
1469 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1470 (void *)rn
, (void *)re
);
1477 * Check if the dest can be deleted now.
1482 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1484 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1485 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1490 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1491 if (IS_ZEBRA_DEBUG_MPLS
)
1493 "%s(%u): Scheduling all LSPs upon RIB completion",
1494 zvrf_name(zvrf
), zvrf_id(zvrf
));
1495 zebra_mpls_lsp_schedule(zvrf
);
1496 mpls_unmark_lsps_for_processing(rn
);
1501 * Utility to match route with dplane context data
1503 static bool rib_route_match_ctx(const struct route_entry
*re
,
1504 const struct zebra_dplane_ctx
*ctx
,
1507 bool result
= false;
1511 * In 'update' case, we test info about the 'previous' or
1514 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1515 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1518 /* We use an extra test for statics, and another for
1521 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1522 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1523 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1525 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1527 dplane_ctx_get_old_metric(ctx
)) {
1534 * Ordinary, single-route case using primary context info
1536 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1537 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1538 /* Skip route that's been deleted */
1542 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1543 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1546 /* We use an extra test for statics, and another for
1549 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1550 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1551 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1553 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1554 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1556 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1557 result
= nexthop_group_equal_no_recurse(
1558 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1567 static void zebra_rib_fixup_system(struct route_node
*rn
)
1569 struct route_entry
*re
;
1571 RNODE_FOREACH_RE(rn
, re
) {
1572 struct nexthop
*nhop
;
1574 if (!RIB_SYSTEM_ROUTE(re
))
1577 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1580 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1581 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1582 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
1584 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1585 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1588 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1593 /* Route comparison logic, with various special cases. */
1594 static bool rib_compare_routes(const struct route_entry
*re1
,
1595 const struct route_entry
*re2
)
1597 if (re1
->type
!= re2
->type
)
1600 if (re1
->instance
!= re2
->instance
)
1603 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1606 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1607 re1
->distance
!= re2
->distance
)
1610 /* We support multiple connected routes: this supports multiple
1611 * v6 link-locals, and we also support multiple addresses in the same
1612 * subnet on a single interface.
1614 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1621 * Compare nexthop lists from a route and a dplane context; test whether
1622 * the list installed in the FIB matches the route's list.
1623 * Set 'changed_p' to 'true' if there were changes to the route's
1624 * installed nexthops.
1626 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1629 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1630 const struct nexthop_group
*ctx_nhg
,
1633 bool matched_p
= true;
1634 struct nexthop
*nexthop
, *ctx_nexthop
;
1636 /* Get the first `installed` one to check against.
1637 * If the dataplane doesn't set these to be what was actually installed,
1638 * it will just be whatever was in re->nhe->nhg?
1640 ctx_nexthop
= ctx_nhg
->nexthop
;
1642 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1643 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1644 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1646 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1648 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1651 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1654 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1655 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1656 /* If the FIB doesn't know about the nexthop,
1657 * it's not installed
1659 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1660 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1661 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1663 (CHECK_FLAG(nexthop
->flags
,
1669 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1672 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1674 /* Keep checking nexthops */
1678 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1679 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1680 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1681 zlog_debug("%s: rib nh %pNHv -> installed",
1687 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1689 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1690 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1691 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1697 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1700 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1707 * Update a route from a dplane context. This consolidates common code
1708 * that can be used in processing of results from FIB updates, and in
1709 * async notification processing.
1710 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1712 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1713 struct route_node
*rn
,
1714 struct zebra_dplane_ctx
*ctx
)
1716 struct nexthop
*nexthop
;
1718 const struct nexthop_group
*ctxnhg
;
1719 struct nexthop_group
*re_nhg
;
1720 bool is_selected
= false; /* Is 're' currently the selected re? */
1721 bool changed_p
= false; /* Change to nexthops? */
1725 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1727 dest
= rib_dest_from_rnode(rn
);
1729 is_selected
= (re
== dest
->selected_fib
);
1731 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1732 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1733 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1734 (is_selected
? "" : "NOT "), re
);
1736 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1737 * If the installed set differs from the set requested by the rib/owner,
1738 * we use the fib-specific nexthop-group to record the actual FIB
1742 ctxnhg
= dplane_ctx_get_ng(ctx
);
1744 /* Check route's fib group and incoming notif group for equivalence.
1746 * Let's assume the nexthops are ordered here to save time.
1748 /* TODO -- this isn't testing or comparing the FIB flags; we should
1749 * do a more explicit loop, checking the incoming notification's flags.
1751 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1752 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1755 /* If the new FIB set matches the existing FIB set, we're done. */
1757 if (IS_ZEBRA_DEBUG_RIB
)
1759 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1760 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1763 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1765 * Free stale fib list and move on to check the rib nhg.
1767 if (IS_ZEBRA_DEBUG_RIB
)
1769 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1770 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1771 nexthops_free(re
->fib_ng
.nexthop
);
1772 re
->fib_ng
.nexthop
= NULL
;
1774 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1776 /* Note that the installed nexthops have changed */
1779 if (IS_ZEBRA_DEBUG_RIB
)
1781 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1782 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1786 * Compare with the rib nexthop group. The comparison here is different:
1787 * the RIB group may be a superset of the list installed in the FIB. We
1788 * walk the RIB group, looking for the 'installable' candidate
1789 * nexthops, and then check those against the set
1790 * that is actually installed.
1792 * Assume nexthops are ordered here as well.
1795 /* If nothing is installed, we can skip some of the checking/comparison
1798 if (ctxnhg
->nexthop
== NULL
) {
1803 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1805 /* If all nexthops were processed, we're done */
1807 if (IS_ZEBRA_DEBUG_RIB
)
1809 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1810 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1811 (changed_p
? "true" : "false"));
1817 /* FIB nexthop set differs from the RIB set:
1818 * create a fib-specific nexthop-group
1820 if (IS_ZEBRA_DEBUG_RIB
)
1822 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1823 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1824 (changed_p
? "true" : "false"),
1825 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1827 /* Set the flag about the dedicated fib list */
1828 if (zrouter
.asic_notification_nexthop_control
) {
1829 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1830 if (ctxnhg
->nexthop
)
1831 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
,
1838 * Check the status of the route's backup nexthops, if any.
1839 * The logic for backups is somewhat different: if any backup is
1840 * installed, a new fib nhg will be attached to the route.
1842 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1844 goto done
; /* No backup nexthops */
1846 /* First check the route's 'fib' list of backups, if it's present
1847 * from some previous event.
1849 re_nhg
= &re
->fib_backup_ng
;
1850 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1853 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1856 /* If the new FIB set matches an existing FIB set, we're done. */
1858 if (IS_ZEBRA_DEBUG_RIB
)
1860 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1861 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1864 } else if (re
->fib_backup_ng
.nexthop
) {
1866 * Free stale fib backup list and move on to check
1867 * the route's backups.
1869 if (IS_ZEBRA_DEBUG_RIB
)
1871 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1872 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1873 nexthops_free(re
->fib_backup_ng
.nexthop
);
1874 re
->fib_backup_ng
.nexthop
= NULL
;
1876 /* Note that the installed nexthops have changed */
1879 if (IS_ZEBRA_DEBUG_RIB
)
1881 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1882 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1886 * If a FIB backup nexthop set exists, attach a copy
1887 * to the route if any backup is installed
1889 if (ctxnhg
&& ctxnhg
->nexthop
) {
1891 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1892 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1896 /* If no installed backups, we're done */
1897 if (nexthop
== NULL
)
1900 if (IS_ZEBRA_DEBUG_RIB
)
1902 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1903 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1904 (changed_p
? "true" : "false"));
1906 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1916 * Helper to locate a zebra route-node from a dplane context. This is used
1917 * when processing dplane results, e.g. Note well: the route-node is returned
1918 * with a ref held - route_unlock_node() must be called eventually.
1920 struct route_node
*rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1922 struct route_table
*table
= NULL
;
1923 struct route_node
*rn
= NULL
;
1924 const struct prefix
*dest_pfx
, *src_pfx
;
1926 /* Locate rn and re(s) from ctx */
1928 table
= zebra_vrf_lookup_table_with_table_id(
1929 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1930 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1931 if (table
== NULL
) {
1932 if (IS_ZEBRA_DEBUG_DPLANE
) {
1934 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1935 dplane_ctx_get_afi(ctx
),
1936 dplane_ctx_get_safi(ctx
),
1937 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1938 dplane_ctx_get_vrf(ctx
));
1943 dest_pfx
= dplane_ctx_get_dest(ctx
);
1944 src_pfx
= dplane_ctx_get_src(ctx
);
1946 rn
= srcdest_rnode_get(table
, dest_pfx
,
1947 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1956 * Route-update results processing after async dataplane update.
1958 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1960 struct zebra_vrf
*zvrf
= NULL
;
1962 struct route_node
*rn
= NULL
;
1963 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1964 bool is_update
= false;
1965 enum dplane_op_e op
;
1966 enum zebra_dplane_result status
;
1969 bool fib_changed
= false;
1970 struct rib_table_info
*info
;
1971 bool rt_delete
= false;
1973 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1974 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1976 /* Locate rn and re(s) from ctx */
1977 rn
= rib_find_rn_from_ctx(ctx
);
1979 if (IS_ZEBRA_DEBUG_DPLANE
) {
1981 "Failed to process dplane results: no route for %s(%u):%pRN",
1982 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
), rn
);
1987 dest
= rib_dest_from_rnode(rn
);
1988 info
= srcdest_rnode_table_info(rn
);
1990 op
= dplane_ctx_get_op(ctx
);
1991 status
= dplane_ctx_get_status(ctx
);
1993 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1995 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1996 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1997 dplane_ctx_get_table(ctx
), rn
, ctx
, dplane_op2str(op
),
1998 dplane_res2str(status
));
2001 * Update is a bit of a special case, where we may have both old and new
2002 * routes to post-process.
2004 is_update
= dplane_ctx_is_update(ctx
);
2007 * Take a pass through the routes, look for matches with the context
2010 RNODE_FOREACH_RE(rn
, rib
) {
2013 if (rib_route_match_ctx(rib
, ctx
, false))
2017 /* Check for old route match */
2018 if (is_update
&& (old_re
== NULL
)) {
2019 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
2023 /* Have we found the routes we need to work on? */
2024 if (re
&& ((!is_update
|| old_re
)))
2028 seq
= dplane_ctx_get_seq(ctx
);
2031 * Check sequence number(s) to detect stale results before continuing
2034 if (re
->dplane_sequence
!= seq
) {
2035 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2037 "%s(%u):%pRN Stale dplane result for re %p",
2039 dplane_ctx_get_vrf(ctx
), rn
, re
);
2041 if (!zrouter
.asic_offloaded
||
2042 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
2043 CHECK_FLAG(re
->flags
,
2044 ZEBRA_FLAG_OFFLOAD_FAILED
))) {
2045 UNSET_FLAG(re
->status
,
2046 ROUTE_ENTRY_ROUTE_REPLACING
);
2047 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2053 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
2054 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2056 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
2058 dplane_ctx_get_vrf(ctx
), old_re
->table
,
2061 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
2065 case DPLANE_OP_ROUTE_INSTALL
:
2066 case DPLANE_OP_ROUTE_UPDATE
:
2067 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2069 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2070 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2073 * On an update operation from the same route type
2074 * context retrieval currently has no way to know
2075 * which was the old and which was the new.
2076 * So don't unset our flags that we just set.
2077 * We know redistribution is ok because the
2078 * old_re in this case is used for nothing
2079 * more than knowing whom to contact if necessary.
2081 if (old_re
&& old_re
!= re
) {
2082 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2083 UNSET_FLAG(old_re
->status
,
2084 ROUTE_ENTRY_INSTALLED
);
2087 /* Update zebra route based on the results in
2088 * the context struct.
2092 rib_update_re_from_ctx(re
, rn
, ctx
);
2095 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2097 "%s(%u:%u):%pRN no fib change for re",
2099 dplane_ctx_get_vrf(ctx
),
2100 dplane_ctx_get_table(
2105 /* Redistribute if this is the selected re */
2106 if (dest
&& re
== dest
->selected_fib
)
2107 redistribute_update(rn
, re
, old_re
);
2111 * System routes are weird in that they
2112 * allow multiple to be installed that match
2113 * to the same prefix, so after we get the
2114 * result we need to clean them up so that
2115 * we can actually use them.
2117 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2118 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2119 zebra_rib_fixup_system(rn
);
2124 /* Notify route owner */
2125 if (zebra_router_notify_on_ack())
2126 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2129 if (CHECK_FLAG(re
->flags
,
2130 ZEBRA_FLAG_OFFLOADED
))
2131 zsend_route_notify_owner_ctx(
2133 ZAPI_ROUTE_INSTALLED
);
2136 ZEBRA_FLAG_OFFLOAD_FAILED
))
2137 zsend_route_notify_owner_ctx(
2139 ZAPI_ROUTE_FAIL_INSTALL
);
2144 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2145 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2147 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2149 zsend_route_notify_owner(
2150 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
2151 info
->afi
, info
->safi
);
2153 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2154 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2155 dplane_ctx_get_table(ctx
), rn
);
2158 case DPLANE_OP_ROUTE_DELETE
:
2161 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2163 * In the delete case, the zebra core datastructs were
2164 * updated (or removed) at the time the delete was issued,
2165 * so we're just notifying the route owner.
2167 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2169 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2170 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2172 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2178 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2179 zsend_route_notify_owner_ctx(ctx
,
2180 ZAPI_ROUTE_REMOVE_FAIL
);
2182 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2183 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2184 dplane_ctx_get_table(ctx
), rn
);
2188 * System routes are weird in that they
2189 * allow multiple to be installed that match
2190 * to the same prefix, so after we get the
2191 * result we need to clean them up so that
2192 * we can actually use them.
2194 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2195 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2196 zebra_rib_fixup_system(rn
);
2199 case DPLANE_OP_NONE
:
2200 case DPLANE_OP_ROUTE_NOTIFY
:
2201 case DPLANE_OP_NH_INSTALL
:
2202 case DPLANE_OP_NH_UPDATE
:
2203 case DPLANE_OP_NH_DELETE
:
2204 case DPLANE_OP_LSP_INSTALL
:
2205 case DPLANE_OP_LSP_UPDATE
:
2206 case DPLANE_OP_LSP_DELETE
:
2207 case DPLANE_OP_LSP_NOTIFY
:
2208 case DPLANE_OP_PW_INSTALL
:
2209 case DPLANE_OP_PW_UNINSTALL
:
2210 case DPLANE_OP_SYS_ROUTE_ADD
:
2211 case DPLANE_OP_SYS_ROUTE_DELETE
:
2212 case DPLANE_OP_ADDR_INSTALL
:
2213 case DPLANE_OP_ADDR_UNINSTALL
:
2214 case DPLANE_OP_MAC_INSTALL
:
2215 case DPLANE_OP_MAC_DELETE
:
2216 case DPLANE_OP_NEIGH_INSTALL
:
2217 case DPLANE_OP_NEIGH_UPDATE
:
2218 case DPLANE_OP_NEIGH_DELETE
:
2219 case DPLANE_OP_VTEP_ADD
:
2220 case DPLANE_OP_VTEP_DELETE
:
2221 case DPLANE_OP_RULE_ADD
:
2222 case DPLANE_OP_RULE_DELETE
:
2223 case DPLANE_OP_RULE_UPDATE
:
2224 case DPLANE_OP_NEIGH_DISCOVER
:
2225 case DPLANE_OP_BR_PORT_UPDATE
:
2226 case DPLANE_OP_IPTABLE_ADD
:
2227 case DPLANE_OP_IPTABLE_DELETE
:
2228 case DPLANE_OP_IPSET_ADD
:
2229 case DPLANE_OP_IPSET_DELETE
:
2230 case DPLANE_OP_IPSET_ENTRY_ADD
:
2231 case DPLANE_OP_IPSET_ENTRY_DELETE
:
2232 case DPLANE_OP_NEIGH_IP_INSTALL
:
2233 case DPLANE_OP_NEIGH_IP_DELETE
:
2234 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
2235 case DPLANE_OP_GRE_SET
:
2236 case DPLANE_OP_INTF_ADDR_ADD
:
2237 case DPLANE_OP_INTF_ADDR_DEL
:
2238 case DPLANE_OP_INTF_NETCONFIG
:
2239 case DPLANE_OP_INTF_INSTALL
:
2240 case DPLANE_OP_INTF_UPDATE
:
2241 case DPLANE_OP_INTF_DELETE
:
2242 case DPLANE_OP_TC_QDISC_INSTALL
:
2243 case DPLANE_OP_TC_QDISC_UNINSTALL
:
2244 case DPLANE_OP_TC_CLASS_ADD
:
2245 case DPLANE_OP_TC_CLASS_DELETE
:
2246 case DPLANE_OP_TC_CLASS_UPDATE
:
2247 case DPLANE_OP_TC_FILTER_ADD
:
2248 case DPLANE_OP_TC_FILTER_DELETE
:
2249 case DPLANE_OP_TC_FILTER_UPDATE
:
2253 zebra_rib_evaluate_rn_nexthops(rn
, seq
, rt_delete
);
2254 zebra_rib_evaluate_mpls(rn
);
2258 route_unlock_node(rn
);
2262 * Count installed/FIB nexthops
2264 static int rib_count_installed_nh(struct route_entry
*re
)
2267 struct nexthop
*nexthop
;
2268 struct nexthop_group
*nhg
;
2270 nhg
= rib_get_fib_nhg(re
);
2272 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2273 /* The meaningful flag depends on where the installed
2276 if (nhg
== &(re
->fib_ng
)) {
2277 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2280 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2285 nhg
= rib_get_fib_backup_nhg(re
);
2287 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2288 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2297 * Handle notification from async dataplane: the dataplane has detected
2298 * some change to a route, and notifies zebra so that the control plane
2299 * can reflect that change.
2301 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2303 struct route_node
*rn
= NULL
;
2304 struct route_entry
*re
= NULL
;
2306 struct nexthop
*nexthop
;
2308 bool fib_changed
= false;
2309 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2310 int start_count
, end_count
;
2312 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2314 /* Locate rn and re(s) from ctx */
2315 rn
= rib_find_rn_from_ctx(ctx
);
2319 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2320 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2321 dplane_ctx_get_table(ctx
), rn
);
2326 dest
= rib_dest_from_rnode(rn
);
2329 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2330 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2331 dplane_ctx_get_table(ctx
), rn
, ctx
);
2334 * Take a pass through the routes, look for matches with the context
2337 RNODE_FOREACH_RE(rn
, re
) {
2338 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2342 /* No match? Nothing we can do */
2346 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2347 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2348 dplane_ctx_get_table(ctx
), rn
,
2349 zebra_route_string(dplane_ctx_get_type(ctx
)));
2354 /* Ensure we clear the QUEUED flag */
2355 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2356 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
2358 /* Is this a notification that ... matters? We mostly care about
2359 * the route that is currently selected for installation; we may also
2360 * get an un-install notification, and handle that too.
2362 if (re
!= dest
->selected_fib
) {
2364 * If we need to, clean up after a delete that was part of
2365 * an update operation.
2368 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2369 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2373 /* If no nexthops or none installed, ensure that this re
2374 * gets its 'installed' flag cleared.
2376 if (end_count
== 0) {
2377 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2378 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2381 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2383 dplane_ctx_get_vrf(ctx
),
2384 dplane_ctx_get_table(ctx
), rn
,
2386 dplane_ctx_get_type(ctx
)));
2388 /* At least report on the event. */
2391 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2393 dplane_ctx_get_vrf(ctx
),
2394 dplane_ctx_get_table(ctx
), rn
,
2396 dplane_ctx_get_type(ctx
)));
2400 uint32_t flags
= dplane_ctx_get_flags(ctx
);
2402 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOADED
)) {
2403 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2404 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2406 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)) {
2407 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2408 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2410 if (CHECK_FLAG(flags
, ZEBRA_FLAG_TRAPPED
))
2411 SET_FLAG(re
->flags
, ZEBRA_FLAG_TRAPPED
);
2414 /* We'll want to determine whether the installation status of the
2415 * route has changed: we'll check the status before processing,
2416 * and then again if there's been a change.
2420 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2421 start_count
= rib_count_installed_nh(re
);
2423 /* Update zebra's nexthop FIB flags based on the context struct's
2426 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2431 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2432 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2433 dplane_ctx_get_table(ctx
), rn
);
2437 * Perform follow-up work if the actual status of the prefix
2440 end_count
= rib_count_installed_nh(re
);
2442 /* Various fib transitions: changed nexthops; from installed to
2443 * not-installed; or not-installed to installed.
2445 if (zrouter
.asic_notification_nexthop_control
) {
2446 if (start_count
> 0 && end_count
> 0) {
2449 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2451 dplane_ctx_get_vrf(ctx
),
2452 dplane_ctx_get_table(ctx
), rn
);
2454 /* Changed nexthops - update kernel/others */
2455 dplane_route_notif_update(rn
, re
,
2456 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2458 } else if (start_count
== 0 && end_count
> 0) {
2461 "%s(%u:%u):%pRN installed transition from dplane notification",
2463 dplane_ctx_get_vrf(ctx
),
2464 dplane_ctx_get_table(ctx
), rn
);
2466 /* We expect this to be the selected route, so we want
2467 * to tell others about this transition.
2469 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2471 /* Changed nexthops - update kernel/others */
2472 dplane_route_notif_update(rn
, re
,
2473 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2475 /* Redistribute, lsp, and nht update */
2476 redistribute_update(rn
, re
, NULL
);
2478 } else if (start_count
> 0 && end_count
== 0) {
2481 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2483 dplane_ctx_get_vrf(ctx
),
2484 dplane_ctx_get_table(ctx
), rn
);
2486 /* Transition from _something_ installed to _nothing_
2489 /* We expect this to be the selected route, so we want
2490 * to tell others about this transistion.
2492 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2494 /* Changed nexthops - update kernel/others */
2495 dplane_route_notif_update(rn
, re
,
2496 DPLANE_OP_ROUTE_DELETE
, ctx
);
2498 /* Redistribute, lsp, and nht update */
2499 redistribute_delete(rn
, re
, NULL
);
2503 if (!zebra_router_notify_on_ack()) {
2504 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
))
2505 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2506 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
))
2507 zsend_route_notify_owner_ctx(ctx
,
2508 ZAPI_ROUTE_FAIL_INSTALL
);
2511 /* Make any changes visible for lsp and nexthop-tracking processing */
2512 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
2515 zebra_rib_evaluate_mpls(rn
);
2519 route_unlock_node(rn
);
2523 * Process a node from the EVPN/VXLAN subqueue.
2525 static void process_subq_evpn(struct listnode
*lnode
)
2527 struct wq_evpn_wrapper
*w
;
2529 /* In general, the list node points to a wrapper object
2530 * holding the info necessary to make some update.
2532 w
= listgetdata(lnode
);
2536 if (w
->type
== WQ_EVPN_WRAPPER_TYPE_VRFROUTE
) {
2538 zebra_vxlan_evpn_vrf_route_add(w
->vrf_id
, &w
->macaddr
,
2539 &w
->ip
, &w
->prefix
);
2541 zebra_vxlan_evpn_vrf_route_del(w
->vrf_id
, &w
->ip
,
2543 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_ES
) {
2545 zebra_evpn_remote_es_add(&w
->esi
, w
->ip
.ipaddr_v4
,
2546 w
->esr_rxed
, w
->df_alg
,
2549 zebra_evpn_remote_es_del(&w
->esi
, w
->ip
.ipaddr_v4
);
2550 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_MACIP
) {
2551 uint16_t ipa_len
= 0;
2553 if (w
->ip
.ipa_type
== IPADDR_V4
)
2554 ipa_len
= IPV4_MAX_BYTELEN
;
2555 else if (w
->ip
.ipa_type
== IPADDR_V6
)
2556 ipa_len
= IPV6_MAX_BYTELEN
;
2559 zebra_evpn_rem_macip_add(w
->vni
, &w
->macaddr
, ipa_len
,
2560 &w
->ip
, w
->flags
, w
->seq
,
2561 w
->vtep_ip
, &w
->esi
);
2563 zebra_evpn_rem_macip_del(w
->vni
, &w
->macaddr
, ipa_len
,
2564 &w
->ip
, w
->vtep_ip
);
2565 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_VTEP
) {
2567 zebra_vxlan_remote_vtep_add(w
->vrf_id
, w
->vni
,
2568 w
->vtep_ip
, w
->flags
);
2570 zebra_vxlan_remote_vtep_del(w
->vrf_id
, w
->vni
,
2575 XFREE(MTYPE_WQ_WRAPPER
, w
);
2579 * Process the nexthop-group workqueue subqueue
2581 static void process_subq_nhg(struct listnode
*lnode
)
2583 struct nhg_ctx
*ctx
;
2584 struct nhg_hash_entry
*nhe
, *newnhe
;
2585 struct wq_nhg_wrapper
*w
;
2586 uint8_t qindex
= META_QUEUE_NHG
;
2588 w
= listgetdata(lnode
);
2593 /* Two types of object - an update from the local kernel, or
2594 * an nhg update from a daemon.
2596 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2599 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2601 "NHG Context id=%u dequeued from sub-queue %s",
2602 ctx
->id
, subqueue2str(qindex
));
2605 /* Process nexthop group updates coming 'up' from the OS */
2606 nhg_ctx_process(ctx
);
2608 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2611 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2612 zlog_debug("NHG %u dequeued from sub-queue %s", nhe
->id
,
2613 subqueue2str(qindex
));
2615 /* Process incoming nhg update, probably from a proto daemon */
2616 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2618 nhe
->zapi_session
, &nhe
->nhg
, 0);
2620 /* Report error to daemon via ZAPI */
2622 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2623 nhe
->zapi_session
, nhe
->id
,
2624 ZAPI_NHG_FAIL_INSTALL
);
2626 /* Free temp nhe - we own that memory. */
2627 zebra_nhg_free(nhe
);
2630 XFREE(MTYPE_WQ_WRAPPER
, w
);
2633 static void process_subq_early_label(struct listnode
*lnode
)
2635 struct wq_label_wrapper
*w
= listgetdata(lnode
);
2636 struct zebra_vrf
*zvrf
;
2641 zvrf
= vrf_info_lookup(w
->vrf_id
);
2643 XFREE(MTYPE_WQ_WRAPPER
, w
);
2648 case WQ_LABEL_FTN_UNINSTALL
:
2649 zebra_mpls_ftn_uninstall(zvrf
, w
->ltype
, &w
->p
, w
->route_type
,
2652 case WQ_LABEL_LABELS_PROCESS
:
2653 zebra_mpls_zapi_labels_process(w
->add_p
, zvrf
, &w
->zl
);
2657 XFREE(MTYPE_WQ_WRAPPER
, w
);
2660 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2662 struct route_node
*rnode
= NULL
;
2663 rib_dest_t
*dest
= NULL
;
2664 struct zebra_vrf
*zvrf
= NULL
;
2666 rnode
= listgetdata(lnode
);
2667 dest
= rib_dest_from_rnode(rnode
);
2670 zvrf
= rib_dest_vrf(dest
);
2674 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2675 struct route_entry
*re
= NULL
;
2678 * rib_process may have freed the dest
2679 * as part of the garbage collection. Let's
2680 * prevent stupidity from happening.
2682 dest
= rib_dest_from_rnode(rnode
);
2684 re
= re_list_first(&dest
->routes
);
2686 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2687 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2688 rnode
, rnode
, subqueue2str(qindex
));
2692 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2693 RIB_ROUTE_QUEUED(qindex
));
2695 route_unlock_node(rnode
);
2698 static void rib_re_nhg_free(struct route_entry
*re
)
2700 if (re
->nhe
&& re
->nhe_id
) {
2701 assert(re
->nhe
->id
== re
->nhe_id
);
2702 route_entry_update_nhe(re
, NULL
);
2703 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2704 nexthops_free(re
->nhe
->nhg
.nexthop
);
2706 nexthops_free(re
->fib_ng
.nexthop
);
2709 struct zebra_early_route
{
2713 struct prefix_ipv6 src_p
;
2714 bool src_p_provided
;
2715 struct route_entry
*re
;
2716 struct nhg_hash_entry
*re_nhe
;
2722 static void early_route_memory_free(struct zebra_early_route
*ere
)
2725 zebra_nhg_free(ere
->re_nhe
);
2727 XFREE(MTYPE_RE
, ere
->re
);
2728 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2731 static void process_subq_early_route_add(struct zebra_early_route
*ere
)
2733 struct route_entry
*re
= ere
->re
;
2734 struct route_table
*table
;
2735 struct nhg_hash_entry
*nhe
= NULL
;
2736 struct route_node
*rn
;
2737 struct route_entry
*same
= NULL
, *first_same
= NULL
;
2742 table
= zebra_vrf_get_table_with_table_id(ere
->afi
, ere
->safi
,
2743 re
->vrf_id
, re
->table
);
2745 early_route_memory_free(ere
);
2749 if (re
->nhe_id
> 0) {
2750 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2754 * We've received from the kernel a nexthop id
2755 * that we don't have saved yet. More than likely
2756 * it has not been processed and is on the
2757 * queue to be processed. Let's stop what we
2758 * are doing and cause the meta q to be processed
2759 * storing this for later.
2761 * This is being done this way because zebra
2762 * runs with the assumption t
2765 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2766 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2767 re
->nhe_id
, &ere
->p
);
2769 early_route_memory_free(ere
);
2773 /* Lookup nhe from route information */
2774 nhe
= zebra_nhg_rib_find_nhe(ere
->re_nhe
, ere
->afi
);
2776 char buf2
[PREFIX_STRLEN
] = "";
2779 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2780 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2781 &ere
->p
, ere
->src_p_provided
? " from " : "",
2783 ? prefix2str(&ere
->src_p
, buf2
,
2787 early_route_memory_free(ere
);
2793 * Attach the re to the nhe's nexthop group.
2795 * TODO: This will need to change when we start getting IDs from upper
2796 * level protocols, as the refcnt might be wrong, since it checks
2797 * if old_id != new_id.
2799 route_entry_update_nhe(re
, nhe
);
2801 /* Make it sure prefixlen is applied to the prefix. */
2802 apply_mask(&ere
->p
);
2803 if (ere
->src_p_provided
)
2804 apply_mask_ipv6(&ere
->src_p
);
2806 /* Lookup route node.*/
2807 rn
= srcdest_rnode_get(table
, &ere
->p
,
2808 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2811 * If same type of route are installed, treat it as a implicit
2812 * withdraw. If the user has specified the No route replace semantics
2813 * for the install don't do a route replace.
2815 RNODE_FOREACH_RE (rn
, same
) {
2816 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
2821 /* Compare various route_entry properties */
2822 if (rib_compare_routes(re
, same
)) {
2825 if (first_same
== NULL
)
2832 if (!ere
->startup
&& (re
->flags
& ZEBRA_FLAG_SELFROUTE
) &&
2833 zrouter
.asic_offloaded
) {
2835 if (IS_ZEBRA_DEBUG_RIB
)
2837 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2840 * We are not on startup, this is a self route
2841 * and we have asic offload. Which means
2842 * we are getting a callback for a entry
2843 * that was already deleted to the kernel
2844 * but an earlier response was just handed
2845 * back. Drop it on the floor
2847 early_route_memory_free(ere
);
2852 /* Set default distance by route type. */
2853 if (re
->distance
== 0) {
2854 if (same
&& !zebra_router_notify_on_ack())
2855 re
->distance
= same
->distance
;
2857 re
->distance
= route_distance(re
->type
);
2860 if (re
->metric
== ROUTE_INSTALLATION_METRIC
&&
2861 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2862 if (same
&& !zebra_router_notify_on_ack())
2863 re
->metric
= same
->metric
;
2868 /* If this route is kernel/connected route, notify the dataplane. */
2869 if (RIB_SYSTEM_ROUTE(re
)) {
2870 /* Notify dataplane */
2871 dplane_sys_route_add(rn
, re
);
2874 /* Link new re to node.*/
2875 if (IS_ZEBRA_DEBUG_RIB
) {
2878 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2879 rn
, re
, zebra_route_string(re
->type
), same
, same_count
);
2881 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2884 ere
->src_p_provided
? &ere
->src_p
: NULL
, re
);
2887 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2888 rib_addnode(rn
, re
, 1);
2890 /* Free implicit route.*/
2892 rib_delnode(rn
, same
);
2894 /* See if we can remove some RE entries that are queued for
2895 * removal, but won't be considered in rib processing.
2897 dest
= rib_dest_from_rnode(rn
);
2898 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
2899 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2900 /* If the route was used earlier, must retain it. */
2901 if (dest
&& re
== dest
->selected_fib
)
2904 if (IS_ZEBRA_DEBUG_RIB
)
2905 rnode_debug(rn
, re
->vrf_id
,
2906 "rn %p, removing unneeded re %p",
2913 route_unlock_node(rn
);
2915 zebra_nhg_free(ere
->re_nhe
);
2916 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2919 static void process_subq_early_route_delete(struct zebra_early_route
*ere
)
2921 struct route_table
*table
;
2922 struct route_node
*rn
;
2923 struct route_entry
*re
;
2924 struct route_entry
*fib
= NULL
;
2925 struct route_entry
*same
= NULL
;
2926 struct nexthop
*rtnh
;
2927 char buf2
[INET6_ADDRSTRLEN
];
2930 if (ere
->src_p_provided
)
2931 assert(!ere
->src_p
.prefixlen
|| ere
->afi
== AFI_IP6
);
2934 table
= zebra_vrf_lookup_table_with_table_id(
2935 ere
->afi
, ere
->safi
, ere
->re
->vrf_id
, ere
->re
->table
);
2937 early_route_memory_free(ere
);
2942 apply_mask(&ere
->p
);
2943 if (ere
->src_p_provided
)
2944 apply_mask_ipv6(&ere
->src_p
);
2946 /* Lookup route node. */
2947 rn
= srcdest_rnode_lookup(table
, &ere
->p
,
2948 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2950 if (IS_ZEBRA_DEBUG_RIB
) {
2951 char src_buf
[PREFIX_STRLEN
];
2952 struct vrf
*vrf
= vrf_lookup_by_id(ere
->re
->vrf_id
);
2954 if (ere
->src_p_provided
&& ere
->src_p
.prefixlen
)
2955 prefix2str(&ere
->src_p
, src_buf
,
2960 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2961 vrf
->name
, ere
->re
->table
, rn
,
2962 (src_buf
[0] != '\0') ? " from " : "",
2965 early_route_memory_free(ere
);
2969 dest
= rib_dest_from_rnode(rn
);
2970 fib
= dest
->selected_fib
;
2972 struct nexthop
*nh
= NULL
;
2975 nh
= ere
->re
->nhe
->nhg
.nexthop
;
2977 /* Lookup same type route. */
2978 RNODE_FOREACH_RE (rn
, re
) {
2979 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2982 if (re
->type
!= ere
->re
->type
)
2984 if (re
->instance
!= ere
->re
->instance
)
2986 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2987 ere
->re
->distance
!= re
->distance
)
2990 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2991 re
->metric
!= ere
->re
->metric
)
2993 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= nh
) &&
2994 rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2995 if (rtnh
->ifindex
!= nh
->ifindex
)
3001 /* Make sure that the route found has the same gateway. */
3002 if (ere
->re
->nhe_id
&& re
->nhe_id
== ere
->re
->nhe_id
) {
3011 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3013 * No guarantee all kernel send nh with labels
3016 if (nexthop_same_no_labels(rtnh
, nh
)) {
3026 * If same type of route can't be found and this message is from
3031 * In the past(HA!) we could get here because
3032 * we were receiving a route delete from the
3033 * kernel and we're not marking the proto
3034 * as coming from it's appropriate originator.
3035 * Now that we are properly noticing the fact
3036 * that the kernel has deleted our route we
3037 * are not going to get called in this path
3038 * I am going to leave this here because
3039 * this might still work this way on non-linux
3040 * platforms as well as some weird state I have
3041 * not properly thought of yet.
3042 * If we can show that this code path is
3043 * dead then we can remove it.
3045 if (fib
&& CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
3046 if (IS_ZEBRA_DEBUG_RIB
) {
3048 rn
, ere
->re
->vrf_id
,
3049 "rn %p, re %p (%s) was deleted from kernel, adding",
3050 rn
, fib
, zebra_route_string(fib
->type
));
3052 if (zrouter
.allow_delete
||
3053 CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3054 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3056 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3058 UNSET_FLAG(rtnh
->flags
,
3062 * This is a non FRR route
3063 * as such we should mark
3066 dest
->selected_fib
= NULL
;
3069 * This means someone else, other than Zebra,
3070 * has deleted a Zebra router from the kernel.
3071 * We will add it back
3073 rib_install_kernel(rn
, fib
, NULL
);
3076 if (IS_ZEBRA_DEBUG_RIB
) {
3079 rn
, ere
->re
->vrf_id
,
3080 "via %s ifindex %d type %d doesn't exist in rib",
3081 inet_ntop(afi2family(ere
->afi
),
3084 nh
->ifindex
, ere
->re
->type
);
3087 rn
, ere
->re
->vrf_id
,
3088 "type %d doesn't exist in rib",
3091 route_unlock_node(rn
);
3092 early_route_memory_free(ere
);
3098 struct nexthop
*tmp_nh
;
3100 if (ere
->fromkernel
&&
3101 CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
) &&
3102 !zrouter
.allow_delete
) {
3103 rib_install_kernel(rn
, same
, NULL
);
3104 route_unlock_node(rn
);
3106 early_route_memory_free(ere
);
3110 /* Special handling for IPv4 or IPv6 routes sourced from
3111 * EVPN - the nexthop (and associated MAC) need to be
3112 * uninstalled if no more refs.
3114 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3115 struct ipaddr vtep_ip
;
3117 if (CHECK_FLAG(tmp_nh
->flags
, NEXTHOP_FLAG_EVPN
)) {
3118 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3119 if (ere
->afi
== AFI_IP
) {
3120 vtep_ip
.ipa_type
= IPADDR_V4
;
3121 memcpy(&(vtep_ip
.ipaddr_v4
),
3122 &(tmp_nh
->gate
.ipv4
),
3123 sizeof(struct in_addr
));
3125 vtep_ip
.ipa_type
= IPADDR_V6
;
3126 memcpy(&(vtep_ip
.ipaddr_v6
),
3127 &(tmp_nh
->gate
.ipv6
),
3128 sizeof(struct in6_addr
));
3130 zebra_rib_queue_evpn_route_del(
3131 re
->vrf_id
, &vtep_ip
, &ere
->p
);
3135 /* Notify dplane if system route changes */
3136 if (RIB_SYSTEM_ROUTE(re
))
3137 dplane_sys_route_del(rn
, same
);
3139 rib_delnode(rn
, same
);
3142 route_unlock_node(rn
);
3144 early_route_memory_free(ere
);
3148 * When FRR receives a route we need to match the route up to
3149 * nexthop groups. That we also may have just received
3150 * place the data on this queue so that this work of finding
3151 * the nexthop group entries for the route entry is always
3152 * done after the nexthop group has had a chance to be processed
3154 static void process_subq_early_route(struct listnode
*lnode
)
3156 struct zebra_early_route
*ere
= listgetdata(lnode
);
3159 process_subq_early_route_delete(ere
);
3161 process_subq_early_route_add(ere
);
3165 * Examine the specified subqueue; process one entry and return 1 if
3166 * there is a node, return 0 otherwise.
3168 static unsigned int process_subq(struct list
*subq
,
3169 enum meta_queue_indexes qindex
)
3171 struct listnode
*lnode
= listhead(subq
);
3177 case META_QUEUE_EVPN
:
3178 process_subq_evpn(lnode
);
3180 case META_QUEUE_NHG
:
3181 process_subq_nhg(lnode
);
3183 case META_QUEUE_EARLY_ROUTE
:
3184 process_subq_early_route(lnode
);
3186 case META_QUEUE_EARLY_LABEL
:
3187 process_subq_early_label(lnode
);
3189 case META_QUEUE_CONNECTED
:
3190 case META_QUEUE_KERNEL
:
3191 case META_QUEUE_STATIC
:
3192 case META_QUEUE_NOTBGP
:
3193 case META_QUEUE_BGP
:
3194 case META_QUEUE_OTHER
:
3195 process_subq_route(lnode
, qindex
);
3199 list_delete_node(subq
, lnode
);
3204 /* Dispatch the meta queue by picking and processing the next node from
3205 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3206 * data is pointed to the meta queue structure.
3208 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
3210 struct meta_queue
*mq
= data
;
3212 uint32_t queue_len
, queue_limit
;
3214 /* Ensure there's room for more dataplane updates */
3215 queue_limit
= dplane_get_in_queue_limit();
3216 queue_len
= dplane_get_in_queue_len();
3217 if (queue_len
> queue_limit
) {
3218 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3220 "rib queue: dplane queue len %u, limit %u, retrying",
3221 queue_len
, queue_limit
);
3223 /* Ensure that the meta-queue is actually enqueued */
3224 if (work_queue_empty(zrouter
.ribq
))
3225 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3227 return WQ_QUEUE_BLOCKED
;
3230 for (i
= 0; i
< MQ_SIZE
; i
++)
3231 if (process_subq(mq
->subq
[i
], i
)) {
3235 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
3240 * Look into the RN and queue it into the highest priority queue
3241 * at this point in time for processing.
3243 * We will enqueue a route node only once per invocation.
3245 * There are two possibilities here that should be kept in mind.
3246 * If the original invocation has not been pulled off for processing
3247 * yet, A subsuquent invocation can have a route entry with a better
3248 * meta queue index value and we can have a situation where
3249 * we might have the same node enqueued 2 times. Not necessarily
3250 * an optimal situation but it should be ok.
3252 * The other possibility is that the original invocation has not
3253 * been pulled off for processing yet, A subsusquent invocation
3254 * doesn't have a route_entry with a better meta-queue and the
3255 * original metaqueue index value will win and we'll end up with
3256 * the route node enqueued once.
3258 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
3260 struct route_node
*rn
= NULL
;
3261 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
3262 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
3264 rn
= (struct route_node
*)data
;
3266 RNODE_FOREACH_RE (rn
, curr_re
) {
3267 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
3269 if (curr_qindex
<= qindex
) {
3271 qindex
= curr_qindex
;
3278 /* Invariant: at this point we always have rn->info set. */
3279 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3280 RIB_ROUTE_QUEUED(qindex
))) {
3281 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3282 rnode_debug(rn
, re
->vrf_id
,
3283 "rn %p is already queued in sub-queue %s",
3284 (void *)rn
, subqueue2str(qindex
));
3288 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
3289 listnode_add(mq
->subq
[qindex
], rn
);
3290 route_lock_node(rn
);
3293 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3294 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %s",
3295 (void *)rn
, subqueue2str(qindex
));
3300 static int early_label_meta_queue_add(struct meta_queue
*mq
, void *data
)
3302 listnode_add(mq
->subq
[META_QUEUE_EARLY_LABEL
], data
);
3307 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
3309 struct nhg_ctx
*ctx
= NULL
;
3310 uint8_t qindex
= META_QUEUE_NHG
;
3311 struct wq_nhg_wrapper
*w
;
3313 ctx
= (struct nhg_ctx
*)data
;
3318 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3320 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
3323 listnode_add(mq
->subq
[qindex
], w
);
3326 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3327 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3328 ctx
->id
, subqueue2str(qindex
));
3333 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
3335 struct nhg_hash_entry
*nhe
= NULL
;
3336 uint8_t qindex
= META_QUEUE_NHG
;
3337 struct wq_nhg_wrapper
*w
;
3339 nhe
= (struct nhg_hash_entry
*)data
;
3344 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3346 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
3349 listnode_add(mq
->subq
[qindex
], w
);
3352 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3353 zlog_debug("NHG id=%u queued into sub-queue %s", nhe
->id
,
3354 subqueue2str(qindex
));
3359 static int rib_meta_queue_evpn_add(struct meta_queue
*mq
, void *data
)
3361 listnode_add(mq
->subq
[META_QUEUE_EVPN
], data
);
3367 static int mq_add_handler(void *data
,
3368 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
3370 if (zrouter
.ribq
== NULL
) {
3371 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3372 "%s: work_queue does not exist!", __func__
);
3377 * The RIB queue should normally be either empty or holding the only
3378 * work_queue_item element. In the latter case this element would
3379 * hold a pointer to the meta queue structure, which must be used to
3380 * actually queue the route nodes to process. So create the MQ
3381 * holder, if necessary, then push the work into it in any case.
3382 * This semantics was introduced after 0.99.9 release.
3384 if (work_queue_empty(zrouter
.ribq
))
3385 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3387 return mq_add_func(zrouter
.mq
, data
);
3390 void mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3391 struct prefix
*prefix
, uint8_t route_type
,
3392 uint8_t route_instance
)
3394 struct wq_label_wrapper
*w
;
3396 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3398 w
->type
= WQ_LABEL_FTN_UNINSTALL
;
3399 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3402 w
->route_type
= route_type
;
3403 w
->route_instance
= route_instance
;
3405 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3406 zlog_debug("Early Label Handling for %pFX", prefix
);
3408 mq_add_handler(w
, early_label_meta_queue_add
);
3411 void mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
3412 const struct zapi_labels
*zl
)
3414 struct wq_label_wrapper
*w
;
3416 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3417 w
->type
= WQ_LABEL_LABELS_PROCESS
;
3418 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3422 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3423 zlog_debug("Early Label Handling: Labels Process");
3425 mq_add_handler(w
, early_label_meta_queue_add
);
3428 /* Add route_node to work queue and schedule processing */
3429 int rib_queue_add(struct route_node
*rn
)
3433 /* Pointless to queue a route_node with no RIB entries to add or remove
3435 if (!rnode_to_ribs(rn
)) {
3436 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3437 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
3438 zlog_backtrace(LOG_DEBUG
);
3442 return mq_add_handler(rn
, rib_meta_queue_add
);
3446 * Enqueue incoming nhg info from OS for processing
3448 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
3452 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
3456 * Enqueue incoming nhg from proto daemon for processing
3458 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
3463 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
3467 * Enqueue evpn route for processing
3469 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id
, const struct ethaddr
*rmac
,
3470 const struct ipaddr
*vtep_ip
,
3471 const struct prefix
*host_prefix
)
3473 struct wq_evpn_wrapper
*w
;
3475 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3477 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3482 w
->prefix
= *host_prefix
;
3484 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3485 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3486 vrf_id
, vtep_ip
, host_prefix
);
3488 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3491 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id
,
3492 const struct ipaddr
*vtep_ip
,
3493 const struct prefix
*host_prefix
)
3495 struct wq_evpn_wrapper
*w
;
3497 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3499 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3503 w
->prefix
= *host_prefix
;
3505 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3506 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3507 vrf_id
, vtep_ip
, host_prefix
);
3509 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3512 /* Enqueue EVPN remote ES for processing */
3513 int zebra_rib_queue_evpn_rem_es_add(const esi_t
*esi
,
3514 const struct in_addr
*vtep_ip
,
3515 bool esr_rxed
, uint8_t df_alg
,
3518 struct wq_evpn_wrapper
*w
;
3519 char buf
[ESI_STR_LEN
];
3521 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3523 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3526 w
->ip
.ipa_type
= IPADDR_V4
;
3527 w
->ip
.ipaddr_v4
= *vtep_ip
;
3528 w
->esr_rxed
= esr_rxed
;
3530 w
->df_pref
= df_pref
;
3532 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3533 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3534 esi_to_str(esi
, buf
, sizeof(buf
)));
3536 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3539 int zebra_rib_queue_evpn_rem_es_del(const esi_t
*esi
,
3540 const struct in_addr
*vtep_ip
)
3542 struct wq_evpn_wrapper
*w
;
3543 char buf
[ESI_STR_LEN
];
3545 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3547 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3550 w
->ip
.ipa_type
= IPADDR_V4
;
3551 w
->ip
.ipaddr_v4
= *vtep_ip
;
3553 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3554 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3555 esi_to_str(esi
, buf
, sizeof(buf
));
3557 strlcpy(buf
, "-", sizeof(buf
));
3559 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3563 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3567 * Enqueue EVPN remote macip update for processing
3569 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni
, const struct ethaddr
*macaddr
,
3570 const struct ipaddr
*ipaddr
,
3571 uint8_t flags
, uint32_t seq
,
3572 struct in_addr vtep_ip
, const esi_t
*esi
)
3574 struct wq_evpn_wrapper
*w
;
3575 char buf
[ESI_STR_LEN
];
3577 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3579 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3582 w
->macaddr
= *macaddr
;
3586 w
->vtep_ip
= vtep_ip
;
3589 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3590 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3591 esi_to_str(esi
, buf
, sizeof(buf
));
3593 strlcpy(buf
, "-", sizeof(buf
));
3595 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__
,
3596 macaddr
, &vtep_ip
, buf
);
3599 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3602 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni
, const struct ethaddr
*macaddr
,
3603 const struct ipaddr
*ip
,
3604 struct in_addr vtep_ip
)
3606 struct wq_evpn_wrapper
*w
;
3608 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3610 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3613 w
->macaddr
= *macaddr
;
3615 w
->vtep_ip
= vtep_ip
;
3617 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3618 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__
,
3621 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3625 * Enqueue remote VTEP address for processing
3627 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id
, vni_t vni
,
3628 struct in_addr vtep_ip
, int flood_control
)
3630 struct wq_evpn_wrapper
*w
;
3632 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3634 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3638 w
->vtep_ip
= vtep_ip
;
3639 w
->flags
= flood_control
;
3641 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3642 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3645 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3648 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id
, vni_t vni
,
3649 struct in_addr vtep_ip
)
3651 struct wq_evpn_wrapper
*w
;
3653 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3655 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3659 w
->vtep_ip
= vtep_ip
;
3661 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3662 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3665 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3668 /* Create new meta queue.
3669 A destructor function doesn't seem to be necessary here.
3671 static struct meta_queue
*meta_queue_new(void)
3673 struct meta_queue
*new;
3676 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
3678 for (i
= 0; i
< MQ_SIZE
; i
++) {
3679 new->subq
[i
] = list_new();
3680 assert(new->subq
[i
]);
3686 /* Clean up the EVPN meta-queue list */
3687 static void evpn_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3688 struct zebra_vrf
*zvrf
)
3690 struct listnode
*node
, *nnode
;
3691 struct wq_evpn_wrapper
*w
;
3693 /* Free the node wrapper object, and the struct it wraps */
3694 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3696 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3698 if (w
->vrf_id
!= vrf_id
)
3704 XFREE(MTYPE_WQ_WRAPPER
, w
);
3706 list_delete_node(l
, node
);
3711 /* Clean up the nhg meta-queue list */
3712 static void nhg_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3713 struct zebra_vrf
*zvrf
)
3715 struct wq_nhg_wrapper
*w
;
3716 struct listnode
*node
, *nnode
;
3718 /* Free the node wrapper object, and the struct it wraps */
3719 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3721 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3723 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
&&
3724 w
->u
.ctx
->vrf_id
!= vrf_id
)
3726 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
&&
3727 w
->u
.nhe
->vrf_id
!= vrf_id
)
3730 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
3731 nhg_ctx_free(&w
->u
.ctx
);
3732 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
3733 zebra_nhg_free(w
->u
.nhe
);
3736 XFREE(MTYPE_WQ_WRAPPER
, w
);
3738 list_delete_node(l
, node
);
3743 static void early_label_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3744 struct zebra_vrf
*zvrf
)
3746 struct wq_label_wrapper
*w
;
3747 struct listnode
*node
, *nnode
;
3749 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3750 if (zvrf
&& zvrf
->vrf
->vrf_id
!= w
->vrf_id
)
3754 case WQ_LABEL_FTN_UNINSTALL
:
3755 case WQ_LABEL_LABELS_PROCESS
:
3760 XFREE(MTYPE_WQ_WRAPPER
, w
);
3761 list_delete_node(l
, node
);
3766 static void rib_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3767 struct zebra_vrf
*zvrf
)
3769 struct route_node
*rnode
;
3770 struct listnode
*node
, *nnode
;
3772 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, rnode
)) {
3773 rib_dest_t
*dest
= rib_dest_from_rnode(rnode
);
3775 if (dest
&& rib_dest_vrf(dest
) != zvrf
)
3778 route_unlock_node(rnode
);
3780 list_delete_node(l
, node
);
3785 static void early_route_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3786 struct zebra_vrf
*zvrf
)
3788 struct zebra_early_route
*ere
;
3789 struct listnode
*node
, *nnode
;
3791 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, ere
)) {
3792 if (zvrf
&& ere
->re
->vrf_id
!= zvrf
->vrf
->vrf_id
)
3795 early_route_memory_free(ere
);
3797 list_delete_node(l
, node
);
3802 void meta_queue_free(struct meta_queue
*mq
, struct zebra_vrf
*zvrf
)
3804 enum meta_queue_indexes i
;
3806 for (i
= 0; i
< MQ_SIZE
; i
++) {
3807 /* Some subqueues may need cleanup - nhgs for example */
3809 case META_QUEUE_NHG
:
3810 nhg_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3812 case META_QUEUE_EVPN
:
3813 evpn_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3815 case META_QUEUE_EARLY_ROUTE
:
3816 early_route_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3818 case META_QUEUE_EARLY_LABEL
:
3819 early_label_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3821 case META_QUEUE_CONNECTED
:
3822 case META_QUEUE_KERNEL
:
3823 case META_QUEUE_STATIC
:
3824 case META_QUEUE_NOTBGP
:
3825 case META_QUEUE_BGP
:
3826 case META_QUEUE_OTHER
:
3827 rib_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3831 list_delete(&mq
->subq
[i
]);
3835 XFREE(MTYPE_WORK_QUEUE
, mq
);
3838 /* initialise zebra rib work queue */
3839 static void rib_queue_init(void)
3841 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
3842 "route_node processing"))) {
3843 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3844 "%s: could not initialise work queue!", __func__
);
3848 /* fill in the work queue spec */
3849 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
3850 zrouter
.ribq
->spec
.completion_func
= NULL
;
3851 /* XXX: TODO: These should be runtime configurable via vty */
3852 zrouter
.ribq
->spec
.max_retries
= 3;
3853 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
3854 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
3856 if (!(zrouter
.mq
= meta_queue_new())) {
3857 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3858 "%s: could not initialise meta queue!", __func__
);
3864 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
3868 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
3869 rnh_list_init(&dest
->nht
);
3870 re_list_init(&dest
->routes
);
3871 route_lock_node(rn
); /* rn route table reference */
3878 /* RIB updates are processed via a queue of pointers to route_nodes.
3880 * The queue length is bounded by the maximal size of the routing table,
3881 * as a route_node will not be requeued, if already queued.
3883 * REs are submitted via rib_addnode or rib_delnode which set minimal
3884 * state, or static_install_route (when an existing RE is updated)
3885 * and then submit route_node to queue for best-path selection later.
3886 * Order of add/delete state changes are preserved for any given RE.
3888 * Deleted REs are reaped during best-path selection.
3891 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3892 * |-------->| | best RE, if required
3894 * static_install->|->rib_addqueue...... -> rib_process
3896 * |-------->| |-> rib_unlink
3897 * |-> set ROUTE_ENTRY_REMOVE |
3898 * rib_delnode (RE freed)
3900 * The 'info' pointer of a route_node points to a rib_dest_t
3901 * ('dest'). Queueing state for a route_node is kept on the dest. The
3902 * dest is created on-demand by rib_link() and is kept around at least
3903 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3905 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3907 * - route_nodes: refcounted by:
3908 * - dest attached to route_node:
3909 * - managed by: rib_link/rib_gc_dest
3910 * - route_node processing queue
3911 * - managed by: rib_addqueue, rib_process.
3915 /* Add RE to head of the route node. */
3916 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
3920 const char *rmap_name
;
3924 dest
= rib_dest_from_rnode(rn
);
3926 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3927 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
3929 dest
= zebra_rib_create_dest(rn
);
3932 re_list_add_head(&dest
->routes
, re
);
3934 afi
= (rn
->p
.family
== AF_INET
)
3936 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3937 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3938 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3940 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
3941 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
3946 static void rib_addnode(struct route_node
*rn
,
3947 struct route_entry
*re
, int process
)
3949 /* RE node has been un-removed before route-node is processed.
3950 * route_node must hence already be on the queue for processing..
3952 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3953 if (IS_ZEBRA_DEBUG_RIB
)
3954 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
3955 (void *)rn
, (void *)re
);
3957 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3960 rib_link(rn
, re
, process
);
3966 * Detach a rib structure from a route_node.
3968 * Note that a call to rib_unlink() should be followed by a call to
3969 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3970 * longer required to be deleted.
3972 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
3978 if (IS_ZEBRA_DEBUG_RIB
)
3979 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
3982 dest
= rib_dest_from_rnode(rn
);
3984 re_list_del(&dest
->routes
, re
);
3986 if (dest
->selected_fib
== re
)
3987 dest
->selected_fib
= NULL
;
3989 rib_re_nhg_free(re
);
3991 zapi_re_opaque_free(re
->opaque
);
3993 XFREE(MTYPE_RE
, re
);
3996 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
4000 if (IS_ZEBRA_DEBUG_RIB
)
4001 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
4002 (void *)rn
, (void *)re
);
4003 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
4005 afi
= (rn
->p
.family
== AF_INET
)
4007 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
4008 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
4009 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
4011 zebra_del_import_table_entry(zvrf
, rn
, re
);
4012 /* Just clean up if non main table */
4013 if (IS_ZEBRA_DEBUG_RIB
)
4014 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
4015 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, rn
,
4016 rn
, re
, zebra_route_string(re
->type
));
4025 * Helper that debugs a single nexthop within a route-entry
4027 static void _route_entry_dump_nh(const struct route_entry
*re
,
4028 const char *straddr
,
4029 const struct nexthop
*nexthop
)
4031 char nhname
[PREFIX_STRLEN
];
4032 char backup_str
[50];
4035 char label_str
[MPLS_LABEL_STRLEN
];
4037 struct interface
*ifp
;
4038 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
4040 switch (nexthop
->type
) {
4041 case NEXTHOP_TYPE_BLACKHOLE
:
4042 snprintf(nhname
, sizeof(nhname
), "Blackhole");
4044 case NEXTHOP_TYPE_IFINDEX
:
4045 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
4046 snprintf(nhname
, sizeof(nhname
), "%s",
4047 ifp
? ifp
->name
: "Unknown");
4049 case NEXTHOP_TYPE_IPV4
:
4051 case NEXTHOP_TYPE_IPV4_IFINDEX
:
4052 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
4054 case NEXTHOP_TYPE_IPV6
:
4055 case NEXTHOP_TYPE_IPV6_IFINDEX
:
4056 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
4061 label_str
[0] = '\0';
4062 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
4063 mpls_label2str(nexthop
->nh_label
->num_labels
,
4064 nexthop
->nh_label
->label
, label_str
,
4065 sizeof(label_str
), nexthop
->nh_label_type
,
4067 strlcat(label_str
, ", ", sizeof(label_str
));
4070 backup_str
[0] = '\0';
4071 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
4072 snprintf(backup_str
, sizeof(backup_str
), "backup ");
4073 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
4074 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
4075 nexthop
->backup_idx
[i
]);
4076 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
4081 if (nexthop
->weight
)
4082 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
4084 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
4085 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
4086 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
4088 wgt_str
, backup_str
,
4089 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
4092 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
4095 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
4098 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
4101 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
4104 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
4105 ? "FILTERED " : ""),
4106 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
4108 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
4110 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)
4115 /* This function dumps the contents of a given RE entry into
4116 * standard debug log. Calling function name and IP prefix in
4117 * question are passed as 1st and 2nd arguments.
4119 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
4120 union prefixconstptr src_pp
,
4121 const struct route_entry
*re
)
4123 const struct prefix
*src_p
= src_pp
.p
;
4124 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
4125 char straddr
[PREFIX_STRLEN
];
4126 char srcaddr
[PREFIX_STRLEN
];
4127 char flags_buf
[128];
4128 char status_buf
[128];
4129 struct nexthop
*nexthop
;
4130 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
4131 struct nexthop_group
*nhg
;
4133 prefix2str(pp
, straddr
, sizeof(straddr
));
4135 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
4136 (const void *)re
, straddr
,
4137 is_srcdst
? " from " : "",
4138 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
4140 VRF_LOGNAME(vrf
), re
->vrf_id
);
4141 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4142 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
4145 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4146 straddr
, re
->metric
, re
->mtu
, re
->distance
,
4147 zclient_dump_route_flags(re
->flags
, flags_buf
,
4149 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
4150 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
4151 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
4152 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
4155 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4156 _route_entry_dump_nh(re
, straddr
, nexthop
);
4158 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
4159 zlog_debug("%s: backup nexthops:", straddr
);
4161 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
4162 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
4163 _route_entry_dump_nh(re
, straddr
, nexthop
);
4166 zlog_debug("%s: dump complete", straddr
);
4169 static int rib_meta_queue_early_route_add(struct meta_queue
*mq
, void *data
)
4171 struct zebra_early_route
*ere
= data
;
4173 listnode_add(mq
->subq
[META_QUEUE_EARLY_ROUTE
], data
);
4176 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
4178 "Route %pFX(%u) queued for processing into sub-queue %s",
4179 &ere
->p
, ere
->re
->vrf_id
,
4180 subqueue2str(META_QUEUE_EARLY_ROUTE
));
4185 struct route_entry
*zebra_rib_route_entry_new(vrf_id_t vrf_id
, int type
,
4186 uint8_t instance
, uint32_t flags
,
4189 uint32_t metric
, uint32_t mtu
,
4190 uint8_t distance
, route_tag_t tag
)
4192 struct route_entry
*re
;
4194 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
4196 re
->instance
= instance
;
4197 re
->distance
= distance
;
4199 re
->metric
= metric
;
4201 re
->table
= table_id
;
4202 re
->vrf_id
= vrf_id
;
4203 re
->uptime
= monotime(NULL
);
4205 re
->nhe_id
= nhe_id
;
4210 * Internal route-add implementation; there are a couple of different public
4211 * signatures. Callers in this path are responsible for the memory they
4212 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4213 * must free those objects. If this returns < 0, an error has occurred and the
4214 * route_entry 're' has not been captured; the caller should free that also.
4220 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
4221 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4222 struct nhg_hash_entry
*re_nhe
, bool startup
)
4224 struct zebra_early_route
*ere
;
4229 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
4231 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4236 ere
->src_p
= *src_p
;
4237 ere
->src_p_provided
= !!src_p
;
4239 ere
->re_nhe
= re_nhe
;
4240 ere
->startup
= startup
;
4242 return mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4246 * Add a single route.
4248 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
4249 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4250 struct nexthop_group
*ng
, bool startup
)
4253 struct nhg_hash_entry nhe
, *n
;
4258 /* We either need nexthop(s) or an existing nexthop id */
4259 if (ng
== NULL
&& re
->nhe_id
== 0)
4263 * Use a temporary nhe to convey info to the common/main api.
4265 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
4267 nhe
.nhg
.nexthop
= ng
->nexthop
;
4268 else if (re
->nhe_id
> 0)
4269 nhe
.id
= re
->nhe_id
;
4271 n
= zebra_nhe_copy(&nhe
, 0);
4272 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, n
, startup
);
4274 /* In error cases, free the route also */
4276 XFREE(MTYPE_RE
, re
);
4281 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4282 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4283 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4284 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
4285 uint8_t distance
, bool fromkernel
)
4287 struct zebra_early_route
*ere
;
4288 struct route_entry
*re
= NULL
;
4289 struct nhg_hash_entry
*nhe
= NULL
;
4291 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4292 table_id
, metric
, 0, distance
, 0);
4295 nhe
= zebra_nhg_alloc();
4296 nhe
->nhg
.nexthop
= nexthop_dup(nh
, NULL
);
4299 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4304 ere
->src_p
= *src_p
;
4305 ere
->src_p_provided
= !!src_p
;
4308 ere
->startup
= false;
4309 ere
->deletion
= true;
4310 ere
->fromkernel
= fromkernel
;
4312 mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4316 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4317 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4318 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4319 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
4320 uint8_t distance
, route_tag_t tag
, bool startup
)
4322 struct route_entry
*re
= NULL
;
4323 struct nexthop nexthop
= {};
4324 struct nexthop_group ng
= {};
4326 /* Allocate new route_entry structure. */
4327 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4328 table_id
, metric
, mtu
, distance
, tag
);
4330 /* If the owner of the route supplies a shared nexthop-group id,
4331 * we'll use that. Otherwise, pass the nexthop along directly.
4336 nexthop_group_add_sorted(&ng
, &nexthop
);
4339 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, &ng
, startup
);
4342 static const char *rib_update_event2str(enum rib_update_event event
)
4344 const char *ret
= "UNKNOWN";
4347 case RIB_UPDATE_KERNEL
:
4348 ret
= "RIB_UPDATE_KERNEL";
4350 case RIB_UPDATE_RMAP_CHANGE
:
4351 ret
= "RIB_UPDATE_RMAP_CHANGE";
4353 case RIB_UPDATE_OTHER
:
4354 ret
= "RIB_UPDATE_OTHER";
4356 case RIB_UPDATE_MAX
:
4364 /* Schedule route nodes to be processed if they match the type */
4365 static void rib_update_route_node(struct route_node
*rn
, int type
)
4367 struct route_entry
*re
, *next
;
4368 bool re_changed
= false;
4370 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4371 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
4372 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
4381 /* Schedule routes of a particular table (address-family) based on event. */
4382 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
4385 struct route_node
*rn
;
4387 if (IS_ZEBRA_DEBUG_EVENT
) {
4388 struct zebra_vrf
*zvrf
;
4392 ? ((struct rib_table_info
*)table
->info
)->zvrf
4394 vrf
= zvrf
? zvrf
->vrf
: NULL
;
4396 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
4397 table
->info
? afi2str(
4398 ((struct rib_table_info
*)table
->info
)->afi
)
4400 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
4401 rib_update_event2str(event
), zebra_route_string(rtype
));
4404 /* Walk all routes and queue for processing, if appropriate for
4405 * the trigger event.
4407 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4409 * If we are looking at a route node and the node
4410 * has already been queued we don't
4411 * need to queue it up again
4414 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
4415 RIB_ROUTE_ANY_QUEUED
))
4419 case RIB_UPDATE_KERNEL
:
4420 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
4422 case RIB_UPDATE_RMAP_CHANGE
:
4423 case RIB_UPDATE_OTHER
:
4424 rib_update_route_node(rn
, rtype
);
4426 case RIB_UPDATE_MAX
:
4432 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
4434 struct zebra_router_table
*zrt
;
4436 if (IS_ZEBRA_DEBUG_EVENT
)
4437 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
4438 rib_update_event2str(event
));
4440 /* Just iterate over all the route tables, rather than vrf lookups */
4441 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
4442 rib_update_table(zrt
->table
, event
, rtype
);
4445 struct rib_update_ctx
{
4446 enum rib_update_event event
;
4450 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
4451 enum rib_update_event event
)
4453 struct rib_update_ctx
*ctx
;
4455 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
4458 ctx
->vrf_id
= vrf_id
;
4463 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
4465 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
4468 static void rib_update_handler(struct thread
*thread
)
4470 struct rib_update_ctx
*ctx
;
4472 ctx
= THREAD_ARG(thread
);
4474 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
4476 rib_update_ctx_fini(&ctx
);
4480 * Thread list to ensure we don't schedule a ton of events
4481 * if interfaces are flapping for instance.
4483 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
4485 /* Schedule a RIB update event for all vrfs */
4486 void rib_update(enum rib_update_event event
)
4488 struct rib_update_ctx
*ctx
;
4490 if (thread_is_scheduled(t_rib_update_threads
[event
]))
4493 ctx
= rib_update_ctx_init(0, event
);
4495 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
4496 &t_rib_update_threads
[event
]);
4498 if (IS_ZEBRA_DEBUG_EVENT
)
4499 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
4500 rib_update_event2str(event
));
4503 /* Delete self installed routes after zebra is relaunched. */
4504 void rib_sweep_table(struct route_table
*table
)
4506 struct route_node
*rn
;
4507 struct route_entry
*re
;
4508 struct route_entry
*next
;
4509 struct nexthop
*nexthop
;
4514 if (IS_ZEBRA_DEBUG_RIB
)
4515 zlog_debug("%s: starting", __func__
);
4517 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4518 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4520 if (IS_ZEBRA_DEBUG_RIB
)
4521 route_entry_dump(&rn
->p
, NULL
, re
);
4523 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4526 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
4530 * If routes are older than startup_time then
4531 * we know we read them in from the kernel.
4532 * As such we can safely remove them.
4534 if (zrouter
.startup_time
< re
->uptime
)
4538 * So we are starting up and have received
4539 * routes from the kernel that we have installed
4540 * from a previous run of zebra but not cleaned
4541 * up ( say a kill -9 )
4542 * But since we haven't actually installed
4543 * them yet( we received them from the kernel )
4544 * we don't think they are active.
4545 * So let's pretend they are active to actually
4547 * In all honesty I'm not sure if we should
4548 * mark them as active when we receive them
4549 * This is startup only so probably ok.
4551 * If we ever decide to move rib_sweep_table
4552 * to a different spot (ie startup )
4553 * this decision needs to be revisited
4555 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
4556 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4557 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
4559 rib_uninstall_kernel(rn
, re
);
4560 rib_delnode(rn
, re
);
4564 if (IS_ZEBRA_DEBUG_RIB
)
4565 zlog_debug("%s: ends", __func__
);
4568 /* Sweep all RIB tables. */
4569 void rib_sweep_route(struct thread
*t
)
4572 struct zebra_vrf
*zvrf
;
4574 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4575 if ((zvrf
= vrf
->info
) == NULL
)
4578 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4579 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4582 zebra_router_sweep_route();
4583 zebra_router_sweep_nhgs();
4586 /* Remove specific by protocol routes from 'table'. */
4587 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
4588 struct route_table
*table
)
4590 struct route_node
*rn
;
4591 struct route_entry
*re
;
4592 struct route_entry
*next
;
4593 unsigned long n
= 0;
4596 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
4597 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4598 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4600 if (re
->type
== proto
4601 && re
->instance
== instance
) {
4602 rib_delnode(rn
, re
);
4609 /* Remove specific by protocol routes. */
4610 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
4613 struct zebra_vrf
*zvrf
;
4614 struct other_route_table
*ort
;
4615 unsigned long cnt
= 0;
4617 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4622 cnt
+= rib_score_proto_table(proto
, instance
,
4623 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4624 + rib_score_proto_table(
4626 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4628 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
4629 rib_score_proto_table(proto
, instance
, ort
->table
);
4635 /* Close RIB and clean up kernel routes. */
4636 void rib_close_table(struct route_table
*table
)
4638 struct route_node
*rn
;
4644 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4645 dest
= rib_dest_from_rnode(rn
);
4647 if (dest
&& dest
->selected_fib
) {
4648 rib_uninstall_kernel(rn
, dest
->selected_fib
);
4649 dest
->selected_fib
= NULL
;
4655 * Handler for async dataplane results after a pseudowire installation
4657 static void handle_pw_result(struct zebra_dplane_ctx
*ctx
)
4659 struct zebra_pw
*pw
;
4660 struct zebra_vrf
*vrf
;
4662 /* The pseudowire code assumes success - we act on an error
4663 * result for installation attempts here.
4665 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
4668 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
4669 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
4670 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
4672 zebra_pw_install_failure(pw
,
4673 dplane_ctx_get_pw_status(ctx
));
4678 * Handle results from the dataplane system. Dequeue update context
4679 * structs, dispatch to appropriate internal handlers.
4681 static void rib_process_dplane_results(struct thread
*thread
)
4683 struct zebra_dplane_ctx
*ctx
;
4684 struct dplane_ctx_list_head ctxlist
;
4685 bool shut_p
= false;
4687 /* Dequeue a list of completed updates with one lock/unlock cycle */
4690 dplane_ctx_q_init(&ctxlist
);
4692 /* Take lock controlling queue of results */
4693 frr_with_mutex (&dplane_mutex
) {
4694 /* Dequeue list of context structs */
4695 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
4698 /* Dequeue context block */
4699 ctx
= dplane_ctx_dequeue(&ctxlist
);
4701 /* If we've emptied the results queue, we're done */
4705 /* If zebra is shutting down, avoid processing results,
4706 * just drain the results queue.
4708 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
4709 memory_order_relaxed
);
4712 dplane_ctx_fini(&ctx
);
4714 ctx
= dplane_ctx_dequeue(&ctxlist
);
4720 #ifdef HAVE_SCRIPTING
4721 char *script_name
= frrscript_names_get_script_name(
4722 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
4725 struct frrscript
*fs
;
4728 fs
= frrscript_new(script_name
);
4730 ret
= frrscript_load(
4731 fs
, ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4734 #endif /* HAVE_SCRIPTING */
4738 #ifdef HAVE_SCRIPTING
4741 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4743 #endif /* HAVE_SCRIPTING */
4745 switch (dplane_ctx_get_op(ctx
)) {
4746 case DPLANE_OP_ROUTE_INSTALL
:
4747 case DPLANE_OP_ROUTE_UPDATE
:
4748 case DPLANE_OP_ROUTE_DELETE
:
4749 /* Bit of special case for route updates
4750 * that were generated by async notifications:
4751 * we don't want to continue processing these
4754 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4755 rib_process_result(ctx
);
4758 case DPLANE_OP_ROUTE_NOTIFY
:
4759 rib_process_dplane_notify(ctx
);
4762 case DPLANE_OP_NH_INSTALL
:
4763 case DPLANE_OP_NH_UPDATE
:
4764 case DPLANE_OP_NH_DELETE
:
4765 zebra_nhg_dplane_result(ctx
);
4768 case DPLANE_OP_LSP_INSTALL
:
4769 case DPLANE_OP_LSP_UPDATE
:
4770 case DPLANE_OP_LSP_DELETE
:
4771 /* Bit of special case for LSP updates
4772 * that were generated by async notifications:
4773 * we don't want to continue processing these.
4775 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4776 zebra_mpls_lsp_dplane_result(ctx
);
4779 case DPLANE_OP_LSP_NOTIFY
:
4780 zebra_mpls_process_dplane_notify(ctx
);
4783 case DPLANE_OP_PW_INSTALL
:
4784 case DPLANE_OP_PW_UNINSTALL
:
4785 handle_pw_result(ctx
);
4788 case DPLANE_OP_SYS_ROUTE_ADD
:
4789 case DPLANE_OP_SYS_ROUTE_DELETE
:
4792 case DPLANE_OP_MAC_INSTALL
:
4793 case DPLANE_OP_MAC_DELETE
:
4794 zebra_vxlan_handle_result(ctx
);
4797 case DPLANE_OP_RULE_ADD
:
4798 case DPLANE_OP_RULE_DELETE
:
4799 case DPLANE_OP_RULE_UPDATE
:
4800 case DPLANE_OP_IPTABLE_ADD
:
4801 case DPLANE_OP_IPTABLE_DELETE
:
4802 case DPLANE_OP_IPSET_ADD
:
4803 case DPLANE_OP_IPSET_DELETE
:
4804 case DPLANE_OP_IPSET_ENTRY_ADD
:
4805 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4806 zebra_pbr_dplane_result(ctx
);
4809 case DPLANE_OP_INTF_ADDR_ADD
:
4810 case DPLANE_OP_INTF_ADDR_DEL
:
4811 case DPLANE_OP_INTF_INSTALL
:
4812 case DPLANE_OP_INTF_UPDATE
:
4813 case DPLANE_OP_INTF_DELETE
:
4814 case DPLANE_OP_INTF_NETCONFIG
:
4815 zebra_if_dplane_result(ctx
);
4818 case DPLANE_OP_TC_QDISC_INSTALL
:
4819 case DPLANE_OP_TC_QDISC_UNINSTALL
:
4820 case DPLANE_OP_TC_CLASS_ADD
:
4821 case DPLANE_OP_TC_CLASS_DELETE
:
4822 case DPLANE_OP_TC_CLASS_UPDATE
:
4823 case DPLANE_OP_TC_FILTER_ADD
:
4824 case DPLANE_OP_TC_FILTER_DELETE
:
4825 case DPLANE_OP_TC_FILTER_UPDATE
:
4828 /* Some op codes not handled here */
4829 case DPLANE_OP_ADDR_INSTALL
:
4830 case DPLANE_OP_ADDR_UNINSTALL
:
4831 case DPLANE_OP_NEIGH_INSTALL
:
4832 case DPLANE_OP_NEIGH_UPDATE
:
4833 case DPLANE_OP_NEIGH_DELETE
:
4834 case DPLANE_OP_NEIGH_IP_INSTALL
:
4835 case DPLANE_OP_NEIGH_IP_DELETE
:
4836 case DPLANE_OP_VTEP_ADD
:
4837 case DPLANE_OP_VTEP_DELETE
:
4838 case DPLANE_OP_NEIGH_DISCOVER
:
4839 case DPLANE_OP_BR_PORT_UPDATE
:
4840 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4841 case DPLANE_OP_GRE_SET
:
4842 case DPLANE_OP_NONE
:
4845 } /* Dispatch by op code */
4847 dplane_ctx_fini(&ctx
);
4848 ctx
= dplane_ctx_dequeue(&ctxlist
);
4855 * Results are returned from the dataplane subsystem, in the context of
4856 * the dataplane pthread. We enqueue the results here for processing by
4857 * the main thread later.
4859 static int rib_dplane_results(struct dplane_ctx_list_head
*ctxlist
)
4861 /* Take lock controlling queue of results */
4862 frr_with_mutex (&dplane_mutex
) {
4863 /* Enqueue context blocks */
4864 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4867 /* Ensure event is signalled to zebra main pthread */
4868 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4875 * Ensure there are no empty slots in the route_info array.
4876 * Every route type in zebra should be present there.
4878 static void check_route_info(void)
4880 int len
= array_size(route_info
);
4883 * ZEBRA_ROUTE_SYSTEM is special cased since
4884 * its key is 0 anyway.
4886 * ZEBRA_ROUTE_ALL is also ignored.
4888 for (int i
= 0; i
< len
; i
++) {
4889 assert(route_info
[i
].key
>= ZEBRA_ROUTE_SYSTEM
&&
4890 route_info
[i
].key
< ZEBRA_ROUTE_MAX
);
4891 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4895 /* Routing information base initialize. */
4902 /* Init dataplane, and register for results */
4903 pthread_mutex_init(&dplane_mutex
, NULL
);
4904 dplane_ctx_q_init(&rib_dplane_q
);
4905 zebra_dplane_init(rib_dplane_results
);
4911 * Get the first vrf id that is greater than the given vrf id if any.
4913 * Returns true if a vrf id was found, false otherwise.
4915 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4919 vrf
= vrf_lookup_by_id(vrf_id
);
4921 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4923 *next_id_p
= vrf
->vrf_id
;
4932 * rib_tables_iter_next
4934 * Returns the next table in the iteration.
4936 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4938 struct route_table
*table
;
4941 * Array that helps us go over all AFI/SAFI combinations via one
4944 static const struct {
4948 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4949 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4950 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4955 switch (iter
->state
) {
4957 case RIB_TABLES_ITER_S_INIT
:
4958 iter
->vrf_id
= VRF_DEFAULT
;
4959 iter
->afi_safi_ix
= -1;
4963 case RIB_TABLES_ITER_S_ITERATING
:
4964 iter
->afi_safi_ix
++;
4967 while (iter
->afi_safi_ix
4968 < (int)array_size(afi_safis
)) {
4969 table
= zebra_vrf_table(
4970 afi_safis
[iter
->afi_safi_ix
].afi
,
4971 afi_safis
[iter
->afi_safi_ix
].safi
,
4976 iter
->afi_safi_ix
++;
4980 * Found another table in this vrf.
4986 * Done with all tables in the current vrf, go to the
4990 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4993 iter
->afi_safi_ix
= 0;
4998 case RIB_TABLES_ITER_S_DONE
:
5003 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
5005 iter
->state
= RIB_TABLES_ITER_S_DONE
;