1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Routing Information Base.
3 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
18 #include "sockunion.h"
19 #include "srcdest_table.h"
23 #include "workqueue.h"
24 #include "nexthop_group_private.h"
25 #include "frr_pthread.h"
27 #include "frrscript.h"
29 #include "zebra/zebra_router.h"
30 #include "zebra/connected.h"
31 #include "zebra/debug.h"
32 #include "zebra/interface.h"
33 #include "zebra/redistribute.h"
34 #include "zebra/rib.h"
36 #include "zebra/zapi_msg.h"
37 #include "zebra/zebra_errors.h"
38 #include "zebra/zebra_ns.h"
39 #include "zebra/zebra_rnh.h"
40 #include "zebra/zebra_routemap.h"
41 #include "zebra/zebra_vrf.h"
42 #include "zebra/zebra_vxlan.h"
43 #include "zebra/zapi_msg.h"
44 #include "zebra/zebra_dplane.h"
45 #include "zebra/zebra_evpn_mh.h"
46 #include "zebra/zebra_script.h"
48 DEFINE_MGROUP(ZEBRA
, "zebra");
50 DEFINE_MTYPE(ZEBRA
, RE
, "Route Entry");
51 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_DEST
, "RIB destination");
52 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
53 DEFINE_MTYPE_STATIC(ZEBRA
, WQ_WRAPPER
, "WQ wrapper");
56 * Event, list, and mutex for delivery of dataplane results
58 static pthread_mutex_t dplane_mutex
;
59 static struct thread
*t_dplane
;
60 static struct dplane_ctx_list_head rib_dplane_q
;
62 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
64 DEFINE_HOOK(rib_shutdown
, (struct route_node
* rn
), (rn
));
67 /* Meta Q's specific names */
68 enum meta_queue_indexes
{
71 META_QUEUE_EARLY_ROUTE
,
72 META_QUEUE_EARLY_LABEL
,
81 /* Each route type's string and default distance value. */
85 enum meta_queue_indexes meta_q_map
;
86 } route_info
[ZEBRA_ROUTE_MAX
] = {
87 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Unneeded for nhg's */,
89 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, META_QUEUE_KERNEL
},
90 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, META_QUEUE_KERNEL
},
91 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, META_QUEUE_CONNECTED
},
92 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, META_QUEUE_STATIC
},
93 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, META_QUEUE_NOTBGP
},
94 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, META_QUEUE_NOTBGP
},
95 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, META_QUEUE_NOTBGP
},
96 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, META_QUEUE_NOTBGP
},
97 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, META_QUEUE_NOTBGP
},
98 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */,
100 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, META_QUEUE_OTHER
},
101 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, META_QUEUE_NOTBGP
},
102 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, META_QUEUE_NOTBGP
},
103 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, META_QUEUE_OTHER
},
104 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, META_QUEUE_OTHER
},
105 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, META_QUEUE_STATIC
},
106 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, META_QUEUE_OTHER
},
107 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, META_QUEUE_BGP
},
108 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, META_QUEUE_BGP
},
109 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20,
111 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, META_QUEUE_BGP
},
112 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20,
114 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, META_QUEUE_NOTBGP
},
115 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, META_QUEUE_OTHER
},
116 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, META_QUEUE_OTHER
},
117 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, META_QUEUE_OTHER
},
118 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115,
120 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, META_QUEUE_OTHER
},
121 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, META_QUEUE_OTHER
},
122 [ZEBRA_ROUTE_ALL
] = {ZEBRA_ROUTE_ALL
, 255, META_QUEUE_OTHER
},
123 /* Any new route type added to zebra, should be mirrored here */
125 /* no entry/default: 150 */
128 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
129 * from the OS, and an 'nhe' is a nhe update.
131 struct wq_nhg_wrapper
{
135 struct nhg_hash_entry
*nhe
;
139 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
140 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
142 /* Wrapper structs for evpn/vxlan workqueue items. */
143 struct wq_evpn_wrapper
{
155 struct ethaddr macaddr
;
156 struct prefix prefix
;
157 struct in_addr vtep_ip
;
160 #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
161 #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
162 #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
163 #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
165 enum wq_label_types
{
166 WQ_LABEL_FTN_UNINSTALL
,
167 WQ_LABEL_LABELS_PROCESS
,
170 struct wq_label_wrapper
{
171 enum wq_label_types type
;
175 enum lsp_types_t ltype
;
177 uint8_t route_instance
;
180 struct zapi_labels zl
;
185 static void rib_addnode(struct route_node
*rn
, struct route_entry
*re
,
188 /* %pRN is already a printer for route_nodes that just prints the prefix */
189 #ifdef _FRR_ATTRIBUTE_PRINTFRR
190 #pragma FRR printfrr_ext "%pZN" (struct route_node *)
193 static const char *subqueue2str(enum meta_queue_indexes index
)
197 return "NHG Objects";
198 case META_QUEUE_EVPN
:
199 return "EVPN/VxLan Objects";
200 case META_QUEUE_EARLY_ROUTE
:
201 return "Early Route Processing";
202 case META_QUEUE_EARLY_LABEL
:
203 return "Early Label Handling";
204 case META_QUEUE_CONNECTED
:
205 return "Connected Routes";
206 case META_QUEUE_KERNEL
:
207 return "Kernel Routes";
208 case META_QUEUE_STATIC
:
209 return "Static Routes";
210 case META_QUEUE_NOTBGP
:
211 return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
214 case META_QUEUE_OTHER
:
215 return "Other Routes";
221 printfrr_ext_autoreg_p("ZN", printfrr_zebra_node
);
222 static ssize_t
printfrr_zebra_node(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
225 struct route_node
*rn
= (struct route_node
*)ptr
;
228 /* just the table number? */
229 if (ea
->fmt
[0] == 't') {
231 struct route_entry
*re
= NULL
;
236 return bputch(buf
, '!');
238 dest
= rib_dest_from_rnode(rn
);
240 re
= re_list_first(&dest
->routes
);
242 rv
+= bprintfrr(buf
, "%u", re
->table
);
244 rv
+= bputch(buf
, '?');
247 char cbuf
[PREFIX_STRLEN
* 2 + 6];
248 struct rib_table_info
*info
;
251 return bputs(buf
, "{(route_node *) NULL}");
253 srcdest_rnode2str(rn
, cbuf
, sizeof(cbuf
));
254 rv
+= bputs(buf
, cbuf
);
256 info
= srcdest_rnode_table_info(rn
);
257 if (info
->safi
== SAFI_MULTICAST
)
258 rv
+= bputs(buf
, " (MRIB)");
263 #define rnode_debug(node, vrf_id, msg, ...) \
264 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
267 #define rnode_info(node, vrf_id, msg, ...) \
268 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
271 static char *_dump_re_status(const struct route_entry
*re
, char *buf
,
274 if (re
->status
== 0) {
275 snprintfrr(buf
, len
, "None ");
280 buf
, len
, "%s%s%s%s%s%s%s%s",
281 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
) ? "Removed " : "",
282 CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) ? "Changed " : "",
283 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
286 CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
) ? "Queued " : "",
287 CHECK_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
)
290 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
292 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
293 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
298 uint8_t route_distance(int type
)
302 if ((unsigned)type
>= array_size(route_info
))
305 distance
= route_info
[type
].distance
;
310 int is_zebra_valid_kernel_table(uint32_t table_id
)
313 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
314 || (table_id
== RT_TABLE_COMPAT
))
321 int is_zebra_main_routing_table(uint32_t table_id
)
323 if (table_id
== RT_TABLE_MAIN
)
328 int zebra_check_addr(const struct prefix
*p
)
330 if (p
->family
== AF_INET
) {
333 addr
= p
->u
.prefix4
.s_addr
;
336 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
337 || IPV4_LINKLOCAL(addr
))
340 if (p
->family
== AF_INET6
) {
341 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
343 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
349 static void route_entry_attach_ref(struct route_entry
*re
,
350 struct nhg_hash_entry
*new)
353 re
->nhe_id
= new->id
;
354 re
->nhe_installed_id
= 0;
356 zebra_nhg_increment_ref(new);
359 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
360 int route_entry_update_nhe(struct route_entry
*re
,
361 struct nhg_hash_entry
*new_nhghe
)
364 struct nhg_hash_entry
*old_nhg
= NULL
;
366 if (new_nhghe
== NULL
) {
370 re
->nhe_installed_id
= 0;
375 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
376 /* Capture previous nhg, if any */
379 route_entry_attach_ref(re
, new_nhghe
);
381 /* This is the first time it's being attached */
382 route_entry_attach_ref(re
, new_nhghe
);
385 /* Detach / deref previous nhg */
387 zebra_nhg_decrement_ref(old_nhg
);
392 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
393 struct nhg_hash_entry
*new_entry
)
395 struct zebra_router_table
*zrt
;
396 struct route_node
*rn
;
397 struct route_entry
*re
, *next
;
399 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
400 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
401 __func__
, new_entry
->id
, new_entry
, old_entry
);
403 /* We have to do them ALL */
404 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
405 for (rn
= route_top(zrt
->table
); rn
;
406 rn
= srcdest_route_next(rn
)) {
407 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
408 if (re
->nhe
&& re
->nhe
== old_entry
)
409 route_entry_update_nhe(re
, new_entry
);
415 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
416 const union g_addr
*addr
,
417 struct route_node
**rn_out
)
420 struct route_table
*table
;
421 struct route_node
*rn
;
422 struct route_entry
*match
= NULL
;
425 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
429 memset(&p
, 0, sizeof(p
));
432 p
.u
.prefix4
= addr
->ipv4
;
433 p
.prefixlen
= IPV4_MAX_BITLEN
;
435 p
.u
.prefix6
= addr
->ipv6
;
436 p
.prefixlen
= IPV6_MAX_BITLEN
;
439 rn
= route_node_match(table
, &p
);
444 route_unlock_node(rn
);
446 dest
= rib_dest_from_rnode(rn
);
447 if (dest
&& dest
->selected_fib
448 && !CHECK_FLAG(dest
->selected_fib
->status
,
449 ROUTE_ENTRY_REMOVED
))
450 match
= dest
->selected_fib
;
452 /* If there is no selected route or matched route is EGP, go up
457 } while (rn
&& rn
->info
== NULL
);
461 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
462 if (!CHECK_FLAG(match
->status
,
463 ROUTE_ENTRY_INSTALLED
))
475 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
477 struct route_node
**rn_out
)
479 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
480 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
481 union g_addr gaddr
= {.ipv4
= addr
};
483 switch (zrouter
.ipv4_multicast_mode
) {
484 case MCAST_MRIB_ONLY
:
485 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
487 case MCAST_URIB_ONLY
:
488 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
489 case MCAST_NO_CONFIG
:
490 case MCAST_MIX_MRIB_FIRST
:
491 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
494 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
497 case MCAST_MIX_DISTANCE
:
498 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
499 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
501 re
= ure
->distance
< mre
->distance
? ure
: mre
;
507 case MCAST_MIX_PFXLEN
:
508 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
509 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
511 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
520 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
522 if (IS_ZEBRA_DEBUG_RIB
) {
524 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
526 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
527 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
528 mre
? (ure
? "MRIB+URIB" : "MRIB")
529 : ure
? "URIB" : "nothing",
530 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
535 struct route_entry
*rib_match_ipv6_multicast(vrf_id_t vrf_id
,
536 struct in6_addr addr
,
537 struct route_node
**rn_out
)
539 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
540 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
541 union g_addr gaddr
= {.ipv6
= addr
};
543 switch (zrouter
.ipv4_multicast_mode
) {
544 case MCAST_MRIB_ONLY
:
545 return rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
547 case MCAST_URIB_ONLY
:
548 return rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
549 case MCAST_NO_CONFIG
:
550 case MCAST_MIX_MRIB_FIRST
:
551 re
= mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
554 re
= ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
,
557 case MCAST_MIX_DISTANCE
:
558 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
559 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
561 re
= ure
->distance
< mre
->distance
? ure
: mre
;
567 case MCAST_MIX_PFXLEN
:
568 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
569 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
571 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
580 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
582 if (IS_ZEBRA_DEBUG_RIB
)
583 zlog_debug("%s: %pI6: vrf: %s(%u) found %s, using %s", __func__
,
584 &addr
, vrf_id_to_name(vrf_id
), vrf_id
,
585 mre
? (ure
? "MRIB+URIB" : "MRIB")
586 : ure
? "URIB" : "nothing",
587 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
591 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
593 struct route_table
*table
;
594 struct route_node
*rn
;
595 struct route_entry
*match
= NULL
;
599 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
603 rn
= route_node_lookup(table
, (struct prefix
*)p
);
605 /* No route for this prefix. */
610 route_unlock_node(rn
);
611 dest
= rib_dest_from_rnode(rn
);
613 if (dest
&& dest
->selected_fib
614 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
615 match
= dest
->selected_fib
;
620 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
623 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
630 * Is this RIB labeled-unicast? It must be of type BGP and all paths
631 * (nexthops) must have a label.
633 int zebra_rib_labeled_unicast(struct route_entry
*re
)
635 struct nexthop
*nexthop
= NULL
;
637 if (re
->type
!= ZEBRA_ROUTE_BGP
)
640 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
641 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
647 /* Update flag indicates whether this is a "replace" or not. Currently, this
648 * is only used for IPv4.
650 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
651 struct route_entry
*old
)
653 struct nexthop
*nexthop
;
654 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
655 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
656 const struct prefix
*p
, *src_p
;
657 enum zebra_dplane_result ret
;
659 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
661 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
663 if (info
->safi
!= SAFI_UNICAST
) {
664 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
665 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
671 * Install the resolved nexthop object first.
673 zebra_nhg_install_kernel(re
->nhe
);
676 * If this is a replace to a new RE let the originator of the RE
677 * know that they've lost
679 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
680 zsend_route_notify_owner(rn
, old
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
681 info
->afi
, info
->safi
);
683 /* Update fib selection */
684 dest
->selected_fib
= re
;
687 * Make sure we update the FPM any time we send new information to
690 hook_call(rib_update
, rn
, "installing in kernel");
692 /* Send add or update */
694 ret
= dplane_route_update(rn
, re
, old
);
696 ret
= dplane_route_add(rn
, re
);
699 case ZEBRA_DPLANE_REQUEST_QUEUED
:
700 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
703 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
704 SET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
706 /* Free old FIB nexthop group */
707 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
708 if (old
->fib_ng
.nexthop
) {
709 nexthops_free(old
->fib_ng
.nexthop
);
710 old
->fib_ng
.nexthop
= NULL
;
715 zvrf
->installs_queued
++;
717 case ZEBRA_DPLANE_REQUEST_FAILURE
:
719 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
720 "%u:%u:%pRN: Failed to enqueue dataplane install",
721 re
->vrf_id
, re
->table
, rn
);
724 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
733 /* Uninstall the route from kernel. */
734 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
736 struct nexthop
*nexthop
;
737 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
738 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
740 if (info
->safi
!= SAFI_UNICAST
) {
741 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
742 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
743 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
748 * Make sure we update the FPM any time we send new information to
751 hook_call(rib_update
, rn
, "uninstalling from kernel");
753 switch (dplane_route_delete(rn
, re
)) {
754 case ZEBRA_DPLANE_REQUEST_QUEUED
:
756 zvrf
->removals_queued
++;
758 case ZEBRA_DPLANE_REQUEST_FAILURE
:
759 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
760 "%u:%pRN: Failed to enqueue dataplane uninstall",
763 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
773 * rib_can_delete_dest
775 * Returns true if the given dest can be deleted from the table.
777 static int rib_can_delete_dest(rib_dest_t
*dest
)
779 if (re_list_first(&dest
->routes
)) {
784 * Unresolved rnh's are stored on the default route's list
786 * dest->rnode can also be the source prefix node in an
787 * ipv6 sourcedest table. Fortunately the prefix of a
788 * source prefix node can never be the default prefix.
790 if (is_default_prefix(&dest
->rnode
->p
))
794 * Don't delete the dest if we have to update the FPM about this
797 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
798 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
804 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
,
807 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
811 * We are storing the rnh's associated withb
812 * the tracked nexthop as a list of the rn's.
813 * Unresolved rnh's are placed at the top
814 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
815 * As such for each rn we need to walk up the tree
816 * and see if any rnh's need to see if they
817 * would match a more specific route
820 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
822 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
824 dest
? rnh_list_count(&dest
->nht
) : 0);
826 if (rt_delete
&& (!dest
|| !rnh_list_count(&dest
->nht
))) {
827 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
828 zlog_debug("%pRN has no tracking NHTs. Bailing",
835 dest
= rib_dest_from_rnode(rn
);
839 * If we have any rnh's stored in the nht list
840 * then we know that this route node was used for
841 * nht resolution and as such we need to call the
842 * nexthop tracking evaluation code
844 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
845 struct zebra_vrf
*zvrf
=
846 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
847 struct prefix
*p
= &rnh
->node
->p
;
849 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
851 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
852 zvrf_name(zvrf
), zvrf_id(zvrf
), rn
,
853 rnh
->node
, seq
, rnh
->seqno
);
856 * If we have evaluated this node on this pass
857 * already, due to following the tree up
858 * then we know that we can move onto the next
861 * Additionally we call zebra_evaluate_rnh
862 * when we gc the dest. In this case we know
863 * that there must be no other re's where
864 * we were originally as such we know that
865 * that sequence number is ok to respect.
867 if (rnh
->seqno
== seq
) {
868 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
870 " Node processed and moved already");
875 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0, p
,
881 dest
= rib_dest_from_rnode(rn
);
888 * Garbage collect the rib dest corresponding to the given route node
891 * Returns true if the dest was deleted, false otherwise.
893 int rib_gc_dest(struct route_node
*rn
)
897 dest
= rib_dest_from_rnode(rn
);
901 if (!rib_can_delete_dest(dest
))
904 if (IS_ZEBRA_DEBUG_RIB
) {
905 struct zebra_vrf
*zvrf
;
907 zvrf
= rib_dest_vrf(dest
);
908 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
911 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
915 rnh_list_fini(&dest
->nht
);
916 XFREE(MTYPE_RIB_DEST
, dest
);
920 * Release the one reference that we keep on the route node.
922 route_unlock_node(rn
);
926 void zebra_rtable_node_cleanup(struct route_table
*table
,
927 struct route_node
*node
)
929 struct route_entry
*re
, *next
;
931 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
932 rib_unlink(node
, re
);
936 rib_dest_t
*dest
= node
->info
;
938 /* Remove from update queue of FPM module */
939 hook_call(rib_shutdown
, node
);
941 rnh_list_fini(&dest
->nht
);
942 XFREE(MTYPE_RIB_DEST
, node
->info
);
946 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
947 struct route_entry
*new)
949 hook_call(rib_update
, rn
, "new route selected");
951 /* Update real nexthop. This may actually determine if nexthop is active
953 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
954 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
958 if (IS_ZEBRA_DEBUG_RIB
)
959 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
960 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, rn
, rn
,
961 new, zebra_route_string(new->type
));
963 /* If labeled-unicast route, install transit LSP. */
964 if (zebra_rib_labeled_unicast(new))
965 zebra_mpls_lsp_install(zvrf
, rn
, new);
967 rib_install_kernel(rn
, new, NULL
);
969 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
972 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
973 struct route_entry
*old
)
975 hook_call(rib_update
, rn
, "removing existing route");
977 /* Uninstall from kernel. */
978 if (IS_ZEBRA_DEBUG_RIB
)
979 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
980 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, rn
, rn
,
981 old
, zebra_route_string(old
->type
));
983 /* If labeled-unicast route, uninstall transit LSP. */
984 if (zebra_rib_labeled_unicast(old
))
985 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
987 rib_uninstall_kernel(rn
, old
);
989 /* Update nexthop for route, reset changed flag. */
990 /* Note: this code also handles the Linux case when an interface goes
991 * down, causing the kernel to delete routes without sending DELROUTE
994 if (RIB_KERNEL_ROUTE(old
))
995 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
997 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1000 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1001 struct route_node
*rn
,
1002 struct route_entry
*old
,
1003 struct route_entry
*new)
1008 * We have to install or update if a new route has been selected or
1009 * something has changed.
1011 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1012 hook_call(rib_update
, rn
, "updating existing route");
1014 /* Update the nexthop; we could determine here that nexthop is
1016 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
1019 /* If nexthop is active, install the selected route, if
1021 * the install succeeds, cleanup flags for prior route, if
1026 if (IS_ZEBRA_DEBUG_RIB
) {
1029 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
1030 zvrf_name(zvrf
), zvrf_id(zvrf
),
1031 new->table
, rn
, rn
, new,
1032 zebra_route_string(new->type
),
1034 zebra_route_string(old
->type
));
1037 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
1038 zvrf_name(zvrf
), zvrf_id(zvrf
),
1039 new->table
, rn
, rn
, new,
1040 zebra_route_string(new->type
));
1043 /* If labeled-unicast route, uninstall transit LSP. */
1044 if (zebra_rib_labeled_unicast(old
))
1045 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1048 * Non-system route should be installed.
1049 * If labeled-unicast route, install transit
1052 if (zebra_rib_labeled_unicast(new))
1053 zebra_mpls_lsp_install(zvrf
, rn
, new);
1055 rib_install_kernel(rn
, new, old
);
1059 * If nexthop for selected route is not active or install
1061 * may need to uninstall and delete for redistribution.
1064 if (IS_ZEBRA_DEBUG_RIB
) {
1067 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1068 zvrf_name(zvrf
), zvrf_id(zvrf
),
1069 new->table
, rn
, rn
, new,
1070 zebra_route_string(new->type
),
1072 zebra_route_string(old
->type
));
1075 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
1076 zvrf_name(zvrf
), zvrf_id(zvrf
),
1077 new->table
, rn
, rn
, new,
1078 zebra_route_string(new->type
));
1082 * When we have gotten to this point
1083 * the new route entry has no nexthops
1084 * that are usable and as such we need
1085 * to remove the old route, but only
1086 * if we were the one who installed
1089 if (!RIB_SYSTEM_ROUTE(old
)) {
1090 /* If labeled-unicast route, uninstall transit
1092 if (zebra_rib_labeled_unicast(old
))
1093 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1095 rib_uninstall_kernel(rn
, old
);
1100 * Same route selected; check if in the FIB and if not,
1101 * re-install. This is housekeeping code to deal with
1102 * race conditions in kernel with linux netlink reporting
1103 * interface up before IPv4 or IPv6 protocol is ready
1106 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1107 RIB_SYSTEM_ROUTE(new))
1108 rib_install_kernel(rn
, new, NULL
);
1111 /* Update prior route. */
1113 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1115 /* Clear changed flag. */
1116 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1119 /* Check if 'alternate' RIB entry is better than 'current'. */
1120 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1121 struct route_entry
*alternate
)
1123 if (current
== NULL
)
1126 /* filter route selection in following order:
1127 * - connected beats other types
1128 * - if both connected, loopback or vrf wins
1129 * - lower distance beats higher
1130 * - lower metric beats higher for equal distance
1131 * - last, hence oldest, route wins tie break.
1134 /* Connected routes. Check to see if either are a vrf
1135 * or loopback interface. If not, pick the last connected
1136 * route of the set of lowest metric connected routes.
1138 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1139 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1142 /* both are connected. are either loop or vrf? */
1143 struct nexthop
*nexthop
= NULL
;
1145 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1146 struct interface
*ifp
= if_lookup_by_index(
1147 nexthop
->ifindex
, alternate
->vrf_id
);
1149 if (ifp
&& if_is_loopback(ifp
))
1153 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1154 struct interface
*ifp
= if_lookup_by_index(
1155 nexthop
->ifindex
, current
->vrf_id
);
1157 if (ifp
&& if_is_loopback(ifp
))
1161 /* Neither are loop or vrf so pick best metric */
1162 if (alternate
->metric
<= current
->metric
)
1168 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1171 /* higher distance loses */
1172 if (alternate
->distance
< current
->distance
)
1174 if (current
->distance
< alternate
->distance
)
1177 /* metric tie-breaks equal distance */
1178 if (alternate
->metric
<= current
->metric
)
1184 /* Core function for processing routing information base. */
1185 static void rib_process(struct route_node
*rn
)
1187 struct route_entry
*re
;
1188 struct route_entry
*next
;
1189 struct route_entry
*old_selected
= NULL
;
1190 struct route_entry
*new_selected
= NULL
;
1191 struct route_entry
*old_fib
= NULL
;
1192 struct route_entry
*new_fib
= NULL
;
1193 struct route_entry
*best
= NULL
;
1195 struct zebra_vrf
*zvrf
= NULL
;
1198 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1202 dest
= rib_dest_from_rnode(rn
);
1204 * We have an enqueued node with nothing to process here
1205 * let's just finish up and return;
1210 zvrf
= rib_dest_vrf(dest
);
1211 vrf_id
= zvrf_id(zvrf
);
1213 vrf
= vrf_lookup_by_id(vrf_id
);
1216 * we can have rn's that have a NULL info pointer
1217 * (dest). As such let's not let the deref happen
1218 * additionally we know RNODE_FOREACH_RE_SAFE
1219 * will not iterate so we are ok.
1221 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1222 struct route_entry
*re
= re_list_first(&dest
->routes
);
1224 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1225 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
,
1229 old_fib
= dest
->selected_fib
;
1231 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1232 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1233 char flags_buf
[128];
1234 char status_buf
[128];
1237 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1238 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
, re
,
1239 zebra_route_string(re
->type
),
1240 _dump_re_status(re
, status_buf
,
1241 sizeof(status_buf
)),
1242 zclient_dump_route_flags(re
->flags
, flags_buf
,
1244 re
->distance
, re
->metric
);
1247 /* Currently selected re. */
1248 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1249 assert(old_selected
== NULL
);
1253 /* Skip deleted entries from selection */
1254 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1258 * If the route entry has changed, verify/resolve
1259 * the nexthops associated with the entry.
1261 * In any event if we have nexthops that are not active
1262 * then we cannot use this particular route entry so
1265 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1266 if (!nexthop_active_update(rn
, re
)) {
1267 const struct prefix
*p
;
1268 struct rib_table_info
*info
;
1270 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1271 /* XXX: HERE BE DRAGONS!!!!!
1272 * In all honesty, I have not yet
1273 * figured out what this part does or
1274 * why the ROUTE_ENTRY_CHANGED test
1275 * above is correct or why we need to
1276 * delete a route here, and also not
1277 * whether this concerns both selected
1278 * and fib route, or only selected
1281 * This entry was denied by the 'ip
1283 * table' route-map, we need to delete
1285 if (re
!= old_selected
) {
1286 if (IS_ZEBRA_DEBUG_RIB
)
1288 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1296 SET_FLAG(re
->status
,
1297 ROUTE_ENTRY_REMOVED
);
1300 info
= srcdest_rnode_table_info(rn
);
1301 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1302 zsend_route_notify_owner(
1303 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
1304 info
->afi
, info
->safi
);
1309 * If the re has not changed and the nhg we have is
1310 * not usable, then we cannot use this route entry
1311 * for consideration, as that the route will just
1312 * not install if it is selected.
1314 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1318 /* Infinite distance. */
1319 if (re
->distance
== DISTANCE_INFINITY
&&
1320 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1321 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1325 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1326 best
= rib_choose_best(new_fib
, re
);
1327 if (new_fib
&& best
!= new_fib
)
1328 UNSET_FLAG(new_fib
->status
,
1329 ROUTE_ENTRY_CHANGED
);
1332 best
= rib_choose_best(new_selected
, re
);
1333 if (new_selected
&& best
!= new_selected
)
1334 UNSET_FLAG(new_selected
->status
,
1335 ROUTE_ENTRY_CHANGED
);
1336 new_selected
= best
;
1339 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1340 } /* RNODE_FOREACH_RE */
1342 /* If no FIB override route, use the selected route also for FIB */
1343 if (new_fib
== NULL
)
1344 new_fib
= new_selected
;
1346 /* After the cycle is finished, the following pointers will be set:
1347 * old_selected --- RE entry currently having SELECTED
1348 * new_selected --- RE entry that is newly SELECTED
1349 * old_fib --- RE entry currently in kernel FIB
1350 * new_fib --- RE entry that is newly to be in kernel FIB
1352 * new_selected will get SELECTED flag, and is going to be redistributed
1353 * the zclients. new_fib (which can be new_selected) will be installed
1357 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1358 struct route_entry
*entry
;
1360 entry
= old_selected
1365 : new_fib
? new_fib
: NULL
;
1368 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1369 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, rn
,
1370 (void *)old_selected
, (void *)new_selected
,
1371 (void *)old_fib
, (void *)new_fib
);
1374 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1375 * fib == selected */
1376 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1377 ROUTE_ENTRY_CHANGED
);
1379 /* Update SELECTED entry */
1380 if (old_selected
!= new_selected
|| selected_changed
) {
1382 if (new_selected
&& new_selected
!= new_fib
)
1383 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1386 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1390 * If we're removing the old entry, we should tell
1391 * redist subscribers about that *if* they aren't
1392 * going to see a redist for the new entry.
1394 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1395 ROUTE_ENTRY_REMOVED
))
1396 redistribute_delete(rn
, old_selected
,
1399 if (old_selected
!= new_selected
)
1400 UNSET_FLAG(old_selected
->flags
,
1401 ZEBRA_FLAG_SELECTED
);
1405 /* Update fib according to selection results */
1406 if (new_fib
&& old_fib
)
1407 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1409 rib_process_add_fib(zvrf
, rn
, new_fib
);
1411 rib_process_del_fib(zvrf
, rn
, old_fib
);
1413 /* Remove all RE entries queued for removal */
1414 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1415 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1416 if (IS_ZEBRA_DEBUG_RIB
) {
1417 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1418 (void *)rn
, (void *)re
);
1425 * Check if the dest can be deleted now.
1430 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1432 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1433 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1438 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1439 if (IS_ZEBRA_DEBUG_MPLS
)
1441 "%s(%u): Scheduling all LSPs upon RIB completion",
1442 zvrf_name(zvrf
), zvrf_id(zvrf
));
1443 zebra_mpls_lsp_schedule(zvrf
);
1444 mpls_unmark_lsps_for_processing(rn
);
1449 * Utility to match route with dplane context data
1451 static bool rib_route_match_ctx(const struct route_entry
*re
,
1452 const struct zebra_dplane_ctx
*ctx
,
1455 bool result
= false;
1459 * In 'update' case, we test info about the 'previous' or
1462 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1463 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1466 /* We use an extra test for statics, and another for
1469 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1470 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1471 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1473 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1475 dplane_ctx_get_old_metric(ctx
)) {
1482 * Ordinary, single-route case using primary context info
1484 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1485 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1486 /* Skip route that's been deleted */
1490 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1491 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1494 /* We use an extra test for statics, and another for
1497 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1498 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1499 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1501 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1502 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1504 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1505 result
= nexthop_group_equal_no_recurse(
1506 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1515 static void zebra_rib_fixup_system(struct route_node
*rn
)
1517 struct route_entry
*re
;
1519 RNODE_FOREACH_RE(rn
, re
) {
1520 struct nexthop
*nhop
;
1522 if (!RIB_SYSTEM_ROUTE(re
))
1525 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1528 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1529 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1530 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
1532 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1533 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1536 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1541 /* Route comparison logic, with various special cases. */
1542 static bool rib_compare_routes(const struct route_entry
*re1
,
1543 const struct route_entry
*re2
)
1545 if (re1
->type
!= re2
->type
)
1548 if (re1
->instance
!= re2
->instance
)
1551 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1554 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1555 re1
->distance
!= re2
->distance
)
1558 /* We support multiple connected routes: this supports multiple
1559 * v6 link-locals, and we also support multiple addresses in the same
1560 * subnet on a single interface.
1562 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1569 * Compare nexthop lists from a route and a dplane context; test whether
1570 * the list installed in the FIB matches the route's list.
1571 * Set 'changed_p' to 'true' if there were changes to the route's
1572 * installed nexthops.
1574 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1577 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1578 const struct nexthop_group
*ctx_nhg
,
1581 bool matched_p
= true;
1582 struct nexthop
*nexthop
, *ctx_nexthop
;
1584 /* Get the first `installed` one to check against.
1585 * If the dataplane doesn't set these to be what was actually installed,
1586 * it will just be whatever was in re->nhe->nhg?
1588 ctx_nexthop
= ctx_nhg
->nexthop
;
1590 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1591 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1592 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1594 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1596 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1599 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1602 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1603 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1604 /* If the FIB doesn't know about the nexthop,
1605 * it's not installed
1607 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1608 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1609 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1611 (CHECK_FLAG(nexthop
->flags
,
1617 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1620 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1622 /* Keep checking nexthops */
1626 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1627 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1628 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1629 zlog_debug("%s: rib nh %pNHv -> installed",
1635 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1637 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1638 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1639 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1645 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1648 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1655 * Update a route from a dplane context. This consolidates common code
1656 * that can be used in processing of results from FIB updates, and in
1657 * async notification processing.
1658 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1660 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1661 struct route_node
*rn
,
1662 struct zebra_dplane_ctx
*ctx
)
1664 struct nexthop
*nexthop
;
1666 const struct nexthop_group
*ctxnhg
;
1667 struct nexthop_group
*re_nhg
;
1668 bool is_selected
= false; /* Is 're' currently the selected re? */
1669 bool changed_p
= false; /* Change to nexthops? */
1673 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1675 dest
= rib_dest_from_rnode(rn
);
1677 is_selected
= (re
== dest
->selected_fib
);
1679 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1680 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1681 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1682 (is_selected
? "" : "NOT "), re
);
1684 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1685 * If the installed set differs from the set requested by the rib/owner,
1686 * we use the fib-specific nexthop-group to record the actual FIB
1690 ctxnhg
= dplane_ctx_get_ng(ctx
);
1692 /* Check route's fib group and incoming notif group for equivalence.
1694 * Let's assume the nexthops are ordered here to save time.
1696 /* TODO -- this isn't testing or comparing the FIB flags; we should
1697 * do a more explicit loop, checking the incoming notification's flags.
1699 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1700 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1703 /* If the new FIB set matches the existing FIB set, we're done. */
1705 if (IS_ZEBRA_DEBUG_RIB
)
1707 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1708 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1711 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1713 * Free stale fib list and move on to check the rib nhg.
1715 if (IS_ZEBRA_DEBUG_RIB
)
1717 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1718 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1719 nexthops_free(re
->fib_ng
.nexthop
);
1720 re
->fib_ng
.nexthop
= NULL
;
1722 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1724 /* Note that the installed nexthops have changed */
1727 if (IS_ZEBRA_DEBUG_RIB
)
1729 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1730 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1734 * Compare with the rib nexthop group. The comparison here is different:
1735 * the RIB group may be a superset of the list installed in the FIB. We
1736 * walk the RIB group, looking for the 'installable' candidate
1737 * nexthops, and then check those against the set
1738 * that is actually installed.
1740 * Assume nexthops are ordered here as well.
1743 /* If nothing is installed, we can skip some of the checking/comparison
1746 if (ctxnhg
->nexthop
== NULL
) {
1751 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1753 /* If all nexthops were processed, we're done */
1755 if (IS_ZEBRA_DEBUG_RIB
)
1757 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1758 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1759 (changed_p
? "true" : "false"));
1765 /* FIB nexthop set differs from the RIB set:
1766 * create a fib-specific nexthop-group
1768 if (IS_ZEBRA_DEBUG_RIB
)
1770 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1771 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1772 (changed_p
? "true" : "false"),
1773 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1775 /* Set the flag about the dedicated fib list */
1776 if (zrouter
.asic_notification_nexthop_control
) {
1777 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1778 if (ctxnhg
->nexthop
)
1779 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
,
1786 * Check the status of the route's backup nexthops, if any.
1787 * The logic for backups is somewhat different: if any backup is
1788 * installed, a new fib nhg will be attached to the route.
1790 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1792 goto done
; /* No backup nexthops */
1794 /* First check the route's 'fib' list of backups, if it's present
1795 * from some previous event.
1797 re_nhg
= &re
->fib_backup_ng
;
1798 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1801 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1804 /* If the new FIB set matches an existing FIB set, we're done. */
1806 if (IS_ZEBRA_DEBUG_RIB
)
1808 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1809 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1812 } else if (re
->fib_backup_ng
.nexthop
) {
1814 * Free stale fib backup list and move on to check
1815 * the route's backups.
1817 if (IS_ZEBRA_DEBUG_RIB
)
1819 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1820 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1821 nexthops_free(re
->fib_backup_ng
.nexthop
);
1822 re
->fib_backup_ng
.nexthop
= NULL
;
1824 /* Note that the installed nexthops have changed */
1827 if (IS_ZEBRA_DEBUG_RIB
)
1829 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1830 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1834 * If a FIB backup nexthop set exists, attach a copy
1835 * to the route if any backup is installed
1837 if (ctxnhg
&& ctxnhg
->nexthop
) {
1839 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1840 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1844 /* If no installed backups, we're done */
1845 if (nexthop
== NULL
)
1848 if (IS_ZEBRA_DEBUG_RIB
)
1850 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1851 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1852 (changed_p
? "true" : "false"));
1854 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1864 * Helper to locate a zebra route-node from a dplane context. This is used
1865 * when processing dplane results, e.g. Note well: the route-node is returned
1866 * with a ref held - route_unlock_node() must be called eventually.
1868 struct route_node
*rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1870 struct route_table
*table
= NULL
;
1871 struct route_node
*rn
= NULL
;
1872 const struct prefix
*dest_pfx
, *src_pfx
;
1874 /* Locate rn and re(s) from ctx */
1876 table
= zebra_vrf_lookup_table_with_table_id(
1877 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1878 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1879 if (table
== NULL
) {
1880 if (IS_ZEBRA_DEBUG_DPLANE
) {
1882 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1883 dplane_ctx_get_afi(ctx
),
1884 dplane_ctx_get_safi(ctx
),
1885 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1886 dplane_ctx_get_vrf(ctx
));
1891 dest_pfx
= dplane_ctx_get_dest(ctx
);
1892 src_pfx
= dplane_ctx_get_src(ctx
);
1894 rn
= srcdest_rnode_get(table
, dest_pfx
,
1895 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1904 * Route-update results processing after async dataplane update.
1906 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1908 struct zebra_vrf
*zvrf
= NULL
;
1910 struct route_node
*rn
= NULL
;
1911 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1912 bool is_update
= false;
1913 enum dplane_op_e op
;
1914 enum zebra_dplane_result status
;
1917 bool fib_changed
= false;
1918 struct rib_table_info
*info
;
1919 bool rt_delete
= false;
1921 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1922 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1924 /* Locate rn and re(s) from ctx */
1925 rn
= rib_find_rn_from_ctx(ctx
);
1927 if (IS_ZEBRA_DEBUG_DPLANE
) {
1929 "Failed to process dplane results: no route for %s(%u):%pRN",
1930 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
), rn
);
1935 dest
= rib_dest_from_rnode(rn
);
1936 info
= srcdest_rnode_table_info(rn
);
1938 op
= dplane_ctx_get_op(ctx
);
1939 status
= dplane_ctx_get_status(ctx
);
1941 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1943 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1944 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1945 dplane_ctx_get_table(ctx
), rn
, ctx
, dplane_op2str(op
),
1946 dplane_res2str(status
));
1949 * Update is a bit of a special case, where we may have both old and new
1950 * routes to post-process.
1952 is_update
= dplane_ctx_is_update(ctx
);
1955 * Take a pass through the routes, look for matches with the context
1958 RNODE_FOREACH_RE(rn
, rib
) {
1961 if (rib_route_match_ctx(rib
, ctx
, false))
1965 /* Check for old route match */
1966 if (is_update
&& (old_re
== NULL
)) {
1967 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1971 /* Have we found the routes we need to work on? */
1972 if (re
&& ((!is_update
|| old_re
)))
1976 seq
= dplane_ctx_get_seq(ctx
);
1979 * Check sequence number(s) to detect stale results before continuing
1982 if (re
->dplane_sequence
!= seq
) {
1983 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1985 "%s(%u):%pRN Stale dplane result for re %p",
1987 dplane_ctx_get_vrf(ctx
), rn
, re
);
1989 if (!zrouter
.asic_offloaded
||
1990 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
1991 CHECK_FLAG(re
->flags
,
1992 ZEBRA_FLAG_OFFLOAD_FAILED
))) {
1993 UNSET_FLAG(re
->status
,
1994 ROUTE_ENTRY_ROUTE_REPLACING
);
1995 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2001 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
2002 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2004 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
2006 dplane_ctx_get_vrf(ctx
), old_re
->table
,
2009 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
2013 case DPLANE_OP_ROUTE_INSTALL
:
2014 case DPLANE_OP_ROUTE_UPDATE
:
2015 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2017 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2018 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2021 * On an update operation from the same route type
2022 * context retrieval currently has no way to know
2023 * which was the old and which was the new.
2024 * So don't unset our flags that we just set.
2025 * We know redistribution is ok because the
2026 * old_re in this case is used for nothing
2027 * more than knowing whom to contact if necessary.
2029 if (old_re
&& old_re
!= re
) {
2030 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2031 UNSET_FLAG(old_re
->status
,
2032 ROUTE_ENTRY_INSTALLED
);
2035 /* Update zebra route based on the results in
2036 * the context struct.
2040 rib_update_re_from_ctx(re
, rn
, ctx
);
2043 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2045 "%s(%u:%u):%pRN no fib change for re",
2047 dplane_ctx_get_vrf(ctx
),
2048 dplane_ctx_get_table(
2053 /* Redistribute if this is the selected re */
2054 if (dest
&& re
== dest
->selected_fib
)
2055 redistribute_update(rn
, re
, old_re
);
2059 * System routes are weird in that they
2060 * allow multiple to be installed that match
2061 * to the same prefix, so after we get the
2062 * result we need to clean them up so that
2063 * we can actually use them.
2065 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2066 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2067 zebra_rib_fixup_system(rn
);
2072 /* Notify route owner */
2073 if (zebra_router_notify_on_ack())
2074 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2077 if (CHECK_FLAG(re
->flags
,
2078 ZEBRA_FLAG_OFFLOADED
))
2079 zsend_route_notify_owner_ctx(
2081 ZAPI_ROUTE_INSTALLED
);
2084 ZEBRA_FLAG_OFFLOAD_FAILED
))
2085 zsend_route_notify_owner_ctx(
2087 ZAPI_ROUTE_FAIL_INSTALL
);
2092 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2093 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2095 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2097 zsend_route_notify_owner(
2098 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
2099 info
->afi
, info
->safi
);
2101 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2102 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2103 dplane_ctx_get_table(ctx
), rn
);
2106 case DPLANE_OP_ROUTE_DELETE
:
2109 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2111 * In the delete case, the zebra core datastructs were
2112 * updated (or removed) at the time the delete was issued,
2113 * so we're just notifying the route owner.
2115 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2117 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2118 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2120 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2126 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2127 zsend_route_notify_owner_ctx(ctx
,
2128 ZAPI_ROUTE_REMOVE_FAIL
);
2130 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2131 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2132 dplane_ctx_get_table(ctx
), rn
);
2136 * System routes are weird in that they
2137 * allow multiple to be installed that match
2138 * to the same prefix, so after we get the
2139 * result we need to clean them up so that
2140 * we can actually use them.
2142 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2143 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2144 zebra_rib_fixup_system(rn
);
2147 case DPLANE_OP_NONE
:
2148 case DPLANE_OP_ROUTE_NOTIFY
:
2149 case DPLANE_OP_NH_INSTALL
:
2150 case DPLANE_OP_NH_UPDATE
:
2151 case DPLANE_OP_NH_DELETE
:
2152 case DPLANE_OP_LSP_INSTALL
:
2153 case DPLANE_OP_LSP_UPDATE
:
2154 case DPLANE_OP_LSP_DELETE
:
2155 case DPLANE_OP_LSP_NOTIFY
:
2156 case DPLANE_OP_PW_INSTALL
:
2157 case DPLANE_OP_PW_UNINSTALL
:
2158 case DPLANE_OP_SYS_ROUTE_ADD
:
2159 case DPLANE_OP_SYS_ROUTE_DELETE
:
2160 case DPLANE_OP_ADDR_INSTALL
:
2161 case DPLANE_OP_ADDR_UNINSTALL
:
2162 case DPLANE_OP_MAC_INSTALL
:
2163 case DPLANE_OP_MAC_DELETE
:
2164 case DPLANE_OP_NEIGH_INSTALL
:
2165 case DPLANE_OP_NEIGH_UPDATE
:
2166 case DPLANE_OP_NEIGH_DELETE
:
2167 case DPLANE_OP_VTEP_ADD
:
2168 case DPLANE_OP_VTEP_DELETE
:
2169 case DPLANE_OP_RULE_ADD
:
2170 case DPLANE_OP_RULE_DELETE
:
2171 case DPLANE_OP_RULE_UPDATE
:
2172 case DPLANE_OP_NEIGH_DISCOVER
:
2173 case DPLANE_OP_BR_PORT_UPDATE
:
2174 case DPLANE_OP_IPTABLE_ADD
:
2175 case DPLANE_OP_IPTABLE_DELETE
:
2176 case DPLANE_OP_IPSET_ADD
:
2177 case DPLANE_OP_IPSET_DELETE
:
2178 case DPLANE_OP_IPSET_ENTRY_ADD
:
2179 case DPLANE_OP_IPSET_ENTRY_DELETE
:
2180 case DPLANE_OP_NEIGH_IP_INSTALL
:
2181 case DPLANE_OP_NEIGH_IP_DELETE
:
2182 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
2183 case DPLANE_OP_GRE_SET
:
2184 case DPLANE_OP_INTF_ADDR_ADD
:
2185 case DPLANE_OP_INTF_ADDR_DEL
:
2186 case DPLANE_OP_INTF_NETCONFIG
:
2187 case DPLANE_OP_INTF_INSTALL
:
2188 case DPLANE_OP_INTF_UPDATE
:
2189 case DPLANE_OP_INTF_DELETE
:
2190 case DPLANE_OP_TC_QDISC_INSTALL
:
2191 case DPLANE_OP_TC_QDISC_UNINSTALL
:
2192 case DPLANE_OP_TC_CLASS_ADD
:
2193 case DPLANE_OP_TC_CLASS_DELETE
:
2194 case DPLANE_OP_TC_CLASS_UPDATE
:
2195 case DPLANE_OP_TC_FILTER_ADD
:
2196 case DPLANE_OP_TC_FILTER_DELETE
:
2197 case DPLANE_OP_TC_FILTER_UPDATE
:
2201 zebra_rib_evaluate_rn_nexthops(rn
, seq
, rt_delete
);
2202 zebra_rib_evaluate_mpls(rn
);
2206 route_unlock_node(rn
);
2210 * Count installed/FIB nexthops
2212 static int rib_count_installed_nh(struct route_entry
*re
)
2215 struct nexthop
*nexthop
;
2216 struct nexthop_group
*nhg
;
2218 nhg
= rib_get_fib_nhg(re
);
2220 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2221 /* The meaningful flag depends on where the installed
2224 if (nhg
== &(re
->fib_ng
)) {
2225 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2228 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2233 nhg
= rib_get_fib_backup_nhg(re
);
2235 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2236 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2245 * Handle notification from async dataplane: the dataplane has detected
2246 * some change to a route, and notifies zebra so that the control plane
2247 * can reflect that change.
2249 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2251 struct route_node
*rn
= NULL
;
2252 struct route_entry
*re
= NULL
;
2254 struct nexthop
*nexthop
;
2256 bool fib_changed
= false;
2257 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2258 int start_count
, end_count
;
2260 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2262 /* Locate rn and re(s) from ctx */
2263 rn
= rib_find_rn_from_ctx(ctx
);
2267 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2268 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2269 dplane_ctx_get_table(ctx
), rn
);
2274 dest
= rib_dest_from_rnode(rn
);
2277 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2278 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2279 dplane_ctx_get_table(ctx
), rn
, ctx
);
2282 * Take a pass through the routes, look for matches with the context
2285 RNODE_FOREACH_RE(rn
, re
) {
2286 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2290 /* No match? Nothing we can do */
2294 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2295 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2296 dplane_ctx_get_table(ctx
), rn
,
2297 zebra_route_string(dplane_ctx_get_type(ctx
)));
2302 /* Ensure we clear the QUEUED flag */
2303 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2304 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
2306 /* Is this a notification that ... matters? We mostly care about
2307 * the route that is currently selected for installation; we may also
2308 * get an un-install notification, and handle that too.
2310 if (re
!= dest
->selected_fib
) {
2312 * If we need to, clean up after a delete that was part of
2313 * an update operation.
2316 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2317 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2321 /* If no nexthops or none installed, ensure that this re
2322 * gets its 'installed' flag cleared.
2324 if (end_count
== 0) {
2325 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2326 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2329 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2331 dplane_ctx_get_vrf(ctx
),
2332 dplane_ctx_get_table(ctx
), rn
,
2334 dplane_ctx_get_type(ctx
)));
2336 /* At least report on the event. */
2339 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2341 dplane_ctx_get_vrf(ctx
),
2342 dplane_ctx_get_table(ctx
), rn
,
2344 dplane_ctx_get_type(ctx
)));
2348 uint32_t flags
= dplane_ctx_get_flags(ctx
);
2350 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOADED
)) {
2351 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2352 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2354 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)) {
2355 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2356 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2358 if (CHECK_FLAG(flags
, ZEBRA_FLAG_TRAPPED
))
2359 SET_FLAG(re
->flags
, ZEBRA_FLAG_TRAPPED
);
2362 /* We'll want to determine whether the installation status of the
2363 * route has changed: we'll check the status before processing,
2364 * and then again if there's been a change.
2368 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2369 start_count
= rib_count_installed_nh(re
);
2371 /* Update zebra's nexthop FIB flags based on the context struct's
2374 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2379 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2380 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2381 dplane_ctx_get_table(ctx
), rn
);
2385 * Perform follow-up work if the actual status of the prefix
2388 end_count
= rib_count_installed_nh(re
);
2390 /* Various fib transitions: changed nexthops; from installed to
2391 * not-installed; or not-installed to installed.
2393 if (zrouter
.asic_notification_nexthop_control
) {
2394 if (start_count
> 0 && end_count
> 0) {
2397 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2399 dplane_ctx_get_vrf(ctx
),
2400 dplane_ctx_get_table(ctx
), rn
);
2402 /* Changed nexthops - update kernel/others */
2403 dplane_route_notif_update(rn
, re
,
2404 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2406 } else if (start_count
== 0 && end_count
> 0) {
2409 "%s(%u:%u):%pRN installed transition from dplane notification",
2411 dplane_ctx_get_vrf(ctx
),
2412 dplane_ctx_get_table(ctx
), rn
);
2414 /* We expect this to be the selected route, so we want
2415 * to tell others about this transition.
2417 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2419 /* Changed nexthops - update kernel/others */
2420 dplane_route_notif_update(rn
, re
,
2421 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2423 /* Redistribute, lsp, and nht update */
2424 redistribute_update(rn
, re
, NULL
);
2426 } else if (start_count
> 0 && end_count
== 0) {
2429 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2431 dplane_ctx_get_vrf(ctx
),
2432 dplane_ctx_get_table(ctx
), rn
);
2434 /* Transition from _something_ installed to _nothing_
2437 /* We expect this to be the selected route, so we want
2438 * to tell others about this transistion.
2440 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2442 /* Changed nexthops - update kernel/others */
2443 dplane_route_notif_update(rn
, re
,
2444 DPLANE_OP_ROUTE_DELETE
, ctx
);
2446 /* Redistribute, lsp, and nht update */
2447 redistribute_delete(rn
, re
, NULL
);
2451 if (!zebra_router_notify_on_ack()) {
2452 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
))
2453 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2454 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
))
2455 zsend_route_notify_owner_ctx(ctx
,
2456 ZAPI_ROUTE_FAIL_INSTALL
);
2459 /* Make any changes visible for lsp and nexthop-tracking processing */
2460 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
2463 zebra_rib_evaluate_mpls(rn
);
2467 route_unlock_node(rn
);
2471 * Process a node from the EVPN/VXLAN subqueue.
2473 static void process_subq_evpn(struct listnode
*lnode
)
2475 struct wq_evpn_wrapper
*w
;
2477 /* In general, the list node points to a wrapper object
2478 * holding the info necessary to make some update.
2480 w
= listgetdata(lnode
);
2484 if (w
->type
== WQ_EVPN_WRAPPER_TYPE_VRFROUTE
) {
2486 zebra_vxlan_evpn_vrf_route_add(w
->vrf_id
, &w
->macaddr
,
2487 &w
->ip
, &w
->prefix
);
2489 zebra_vxlan_evpn_vrf_route_del(w
->vrf_id
, &w
->ip
,
2491 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_ES
) {
2493 zebra_evpn_remote_es_add(&w
->esi
, w
->ip
.ipaddr_v4
,
2494 w
->esr_rxed
, w
->df_alg
,
2497 zebra_evpn_remote_es_del(&w
->esi
, w
->ip
.ipaddr_v4
);
2498 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_MACIP
) {
2499 uint16_t ipa_len
= 0;
2501 if (w
->ip
.ipa_type
== IPADDR_V4
)
2502 ipa_len
= IPV4_MAX_BYTELEN
;
2503 else if (w
->ip
.ipa_type
== IPADDR_V6
)
2504 ipa_len
= IPV6_MAX_BYTELEN
;
2507 zebra_evpn_rem_macip_add(w
->vni
, &w
->macaddr
, ipa_len
,
2508 &w
->ip
, w
->flags
, w
->seq
,
2509 w
->vtep_ip
, &w
->esi
);
2511 zebra_evpn_rem_macip_del(w
->vni
, &w
->macaddr
, ipa_len
,
2512 &w
->ip
, w
->vtep_ip
);
2513 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_VTEP
) {
2515 zebra_vxlan_remote_vtep_add(w
->vrf_id
, w
->vni
,
2516 w
->vtep_ip
, w
->flags
);
2518 zebra_vxlan_remote_vtep_del(w
->vrf_id
, w
->vni
,
2523 XFREE(MTYPE_WQ_WRAPPER
, w
);
2527 * Process the nexthop-group workqueue subqueue
2529 static void process_subq_nhg(struct listnode
*lnode
)
2531 struct nhg_ctx
*ctx
;
2532 struct nhg_hash_entry
*nhe
, *newnhe
;
2533 struct wq_nhg_wrapper
*w
;
2534 uint8_t qindex
= META_QUEUE_NHG
;
2536 w
= listgetdata(lnode
);
2541 /* Two types of object - an update from the local kernel, or
2542 * an nhg update from a daemon.
2544 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2547 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2549 "NHG Context id=%u dequeued from sub-queue %s",
2550 ctx
->id
, subqueue2str(qindex
));
2553 /* Process nexthop group updates coming 'up' from the OS */
2554 nhg_ctx_process(ctx
);
2556 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2559 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2560 zlog_debug("NHG %u dequeued from sub-queue %s", nhe
->id
,
2561 subqueue2str(qindex
));
2563 /* Process incoming nhg update, probably from a proto daemon */
2564 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2566 nhe
->zapi_session
, &nhe
->nhg
, 0);
2568 /* Report error to daemon via ZAPI */
2570 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2571 nhe
->zapi_session
, nhe
->id
,
2572 ZAPI_NHG_FAIL_INSTALL
);
2574 /* Free temp nhe - we own that memory. */
2575 zebra_nhg_free(nhe
);
2578 XFREE(MTYPE_WQ_WRAPPER
, w
);
2581 static void process_subq_early_label(struct listnode
*lnode
)
2583 struct wq_label_wrapper
*w
= listgetdata(lnode
);
2584 struct zebra_vrf
*zvrf
;
2589 zvrf
= vrf_info_lookup(w
->vrf_id
);
2591 XFREE(MTYPE_WQ_WRAPPER
, w
);
2596 case WQ_LABEL_FTN_UNINSTALL
:
2597 zebra_mpls_ftn_uninstall(zvrf
, w
->ltype
, &w
->p
, w
->route_type
,
2600 case WQ_LABEL_LABELS_PROCESS
:
2601 zebra_mpls_zapi_labels_process(w
->add_p
, zvrf
, &w
->zl
);
2605 XFREE(MTYPE_WQ_WRAPPER
, w
);
2608 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2610 struct route_node
*rnode
= NULL
;
2611 rib_dest_t
*dest
= NULL
;
2612 struct zebra_vrf
*zvrf
= NULL
;
2614 rnode
= listgetdata(lnode
);
2615 dest
= rib_dest_from_rnode(rnode
);
2618 zvrf
= rib_dest_vrf(dest
);
2622 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2623 struct route_entry
*re
= NULL
;
2626 * rib_process may have freed the dest
2627 * as part of the garbage collection. Let's
2628 * prevent stupidity from happening.
2630 dest
= rib_dest_from_rnode(rnode
);
2632 re
= re_list_first(&dest
->routes
);
2634 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2635 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2636 rnode
, rnode
, subqueue2str(qindex
));
2640 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2641 RIB_ROUTE_QUEUED(qindex
));
2643 route_unlock_node(rnode
);
2646 static void rib_re_nhg_free(struct route_entry
*re
)
2648 if (re
->nhe
&& re
->nhe_id
) {
2649 assert(re
->nhe
->id
== re
->nhe_id
);
2650 route_entry_update_nhe(re
, NULL
);
2651 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2652 nexthops_free(re
->nhe
->nhg
.nexthop
);
2654 nexthops_free(re
->fib_ng
.nexthop
);
2657 struct zebra_early_route
{
2661 struct prefix_ipv6 src_p
;
2662 bool src_p_provided
;
2663 struct route_entry
*re
;
2664 struct nhg_hash_entry
*re_nhe
;
2670 static void early_route_memory_free(struct zebra_early_route
*ere
)
2673 zebra_nhg_free(ere
->re_nhe
);
2675 XFREE(MTYPE_RE
, ere
->re
);
2676 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2679 static void process_subq_early_route_add(struct zebra_early_route
*ere
)
2681 struct route_entry
*re
= ere
->re
;
2682 struct route_table
*table
;
2683 struct nhg_hash_entry
*nhe
= NULL
;
2684 struct route_node
*rn
;
2685 struct route_entry
*same
= NULL
, *first_same
= NULL
;
2690 table
= zebra_vrf_get_table_with_table_id(ere
->afi
, ere
->safi
,
2691 re
->vrf_id
, re
->table
);
2693 early_route_memory_free(ere
);
2697 if (re
->nhe_id
> 0) {
2698 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2702 * We've received from the kernel a nexthop id
2703 * that we don't have saved yet. More than likely
2704 * it has not been processed and is on the
2705 * queue to be processed. Let's stop what we
2706 * are doing and cause the meta q to be processed
2707 * storing this for later.
2709 * This is being done this way because zebra
2710 * runs with the assumption t
2713 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2714 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2715 re
->nhe_id
, &ere
->p
);
2717 early_route_memory_free(ere
);
2721 /* Lookup nhe from route information */
2722 nhe
= zebra_nhg_rib_find_nhe(ere
->re_nhe
, ere
->afi
);
2724 char buf2
[PREFIX_STRLEN
] = "";
2727 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2728 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2729 &ere
->p
, ere
->src_p_provided
? " from " : "",
2731 ? prefix2str(&ere
->src_p
, buf2
,
2735 early_route_memory_free(ere
);
2741 * Attach the re to the nhe's nexthop group.
2743 * TODO: This will need to change when we start getting IDs from upper
2744 * level protocols, as the refcnt might be wrong, since it checks
2745 * if old_id != new_id.
2747 route_entry_update_nhe(re
, nhe
);
2749 /* Make it sure prefixlen is applied to the prefix. */
2750 apply_mask(&ere
->p
);
2751 if (ere
->src_p_provided
)
2752 apply_mask_ipv6(&ere
->src_p
);
2754 /* Lookup route node.*/
2755 rn
= srcdest_rnode_get(table
, &ere
->p
,
2756 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2759 * If same type of route are installed, treat it as a implicit
2760 * withdraw. If the user has specified the No route replace semantics
2761 * for the install don't do a route replace.
2763 RNODE_FOREACH_RE (rn
, same
) {
2764 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
2769 /* Compare various route_entry properties */
2770 if (rib_compare_routes(re
, same
)) {
2773 if (first_same
== NULL
)
2780 if (!ere
->startup
&& (re
->flags
& ZEBRA_FLAG_SELFROUTE
) &&
2781 zrouter
.asic_offloaded
) {
2783 if (IS_ZEBRA_DEBUG_RIB
)
2785 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2788 * We are not on startup, this is a self route
2789 * and we have asic offload. Which means
2790 * we are getting a callback for a entry
2791 * that was already deleted to the kernel
2792 * but an earlier response was just handed
2793 * back. Drop it on the floor
2795 early_route_memory_free(ere
);
2800 /* Set default distance by route type. */
2801 if (re
->distance
== 0) {
2802 if (same
&& !zebra_router_notify_on_ack())
2803 re
->distance
= same
->distance
;
2805 re
->distance
= route_distance(re
->type
);
2808 if (re
->metric
== ROUTE_INSTALLATION_METRIC
&&
2809 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2810 if (same
&& !zebra_router_notify_on_ack())
2811 re
->metric
= same
->metric
;
2816 /* If this route is kernel/connected route, notify the dataplane. */
2817 if (RIB_SYSTEM_ROUTE(re
)) {
2818 /* Notify dataplane */
2819 dplane_sys_route_add(rn
, re
);
2822 /* Link new re to node.*/
2823 if (IS_ZEBRA_DEBUG_RIB
) {
2826 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2827 rn
, re
, zebra_route_string(re
->type
), same
, same_count
);
2829 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2832 ere
->src_p_provided
? &ere
->src_p
: NULL
, re
);
2835 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2836 rib_addnode(rn
, re
, 1);
2838 /* Free implicit route.*/
2840 rib_delnode(rn
, same
);
2842 /* See if we can remove some RE entries that are queued for
2843 * removal, but won't be considered in rib processing.
2845 dest
= rib_dest_from_rnode(rn
);
2846 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
2847 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2848 /* If the route was used earlier, must retain it. */
2849 if (dest
&& re
== dest
->selected_fib
)
2852 if (IS_ZEBRA_DEBUG_RIB
)
2853 rnode_debug(rn
, re
->vrf_id
,
2854 "rn %p, removing unneeded re %p",
2861 route_unlock_node(rn
);
2863 zebra_nhg_free(ere
->re_nhe
);
2864 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2867 static void process_subq_early_route_delete(struct zebra_early_route
*ere
)
2869 struct route_table
*table
;
2870 struct route_node
*rn
;
2871 struct route_entry
*re
;
2872 struct route_entry
*fib
= NULL
;
2873 struct route_entry
*same
= NULL
;
2874 struct nexthop
*rtnh
;
2875 char buf2
[INET6_ADDRSTRLEN
];
2878 if (ere
->src_p_provided
)
2879 assert(!ere
->src_p
.prefixlen
|| ere
->afi
== AFI_IP6
);
2882 table
= zebra_vrf_lookup_table_with_table_id(
2883 ere
->afi
, ere
->safi
, ere
->re
->vrf_id
, ere
->re
->table
);
2885 early_route_memory_free(ere
);
2890 apply_mask(&ere
->p
);
2891 if (ere
->src_p_provided
)
2892 apply_mask_ipv6(&ere
->src_p
);
2894 /* Lookup route node. */
2895 rn
= srcdest_rnode_lookup(table
, &ere
->p
,
2896 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2898 if (IS_ZEBRA_DEBUG_RIB
) {
2899 char src_buf
[PREFIX_STRLEN
];
2900 struct vrf
*vrf
= vrf_lookup_by_id(ere
->re
->vrf_id
);
2902 if (ere
->src_p_provided
&& ere
->src_p
.prefixlen
)
2903 prefix2str(&ere
->src_p
, src_buf
,
2908 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2909 vrf
->name
, ere
->re
->table
, rn
,
2910 (src_buf
[0] != '\0') ? " from " : "",
2913 early_route_memory_free(ere
);
2917 dest
= rib_dest_from_rnode(rn
);
2918 fib
= dest
->selected_fib
;
2920 struct nexthop
*nh
= NULL
;
2923 nh
= ere
->re
->nhe
->nhg
.nexthop
;
2925 /* Lookup same type route. */
2926 RNODE_FOREACH_RE (rn
, re
) {
2927 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2930 if (re
->type
!= ere
->re
->type
)
2932 if (re
->instance
!= ere
->re
->instance
)
2934 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2935 ere
->re
->distance
!= re
->distance
)
2938 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2939 re
->metric
!= ere
->re
->metric
)
2941 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= nh
) &&
2942 rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2943 if (rtnh
->ifindex
!= nh
->ifindex
)
2949 /* Make sure that the route found has the same gateway. */
2950 if (ere
->re
->nhe_id
&& re
->nhe_id
== ere
->re
->nhe_id
) {
2959 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
2961 * No guarantee all kernel send nh with labels
2964 if (nexthop_same_no_labels(rtnh
, nh
)) {
2974 * If same type of route can't be found and this message is from
2979 * In the past(HA!) we could get here because
2980 * we were receiving a route delete from the
2981 * kernel and we're not marking the proto
2982 * as coming from it's appropriate originator.
2983 * Now that we are properly noticing the fact
2984 * that the kernel has deleted our route we
2985 * are not going to get called in this path
2986 * I am going to leave this here because
2987 * this might still work this way on non-linux
2988 * platforms as well as some weird state I have
2989 * not properly thought of yet.
2990 * If we can show that this code path is
2991 * dead then we can remove it.
2993 if (fib
&& CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2994 if (IS_ZEBRA_DEBUG_RIB
) {
2996 rn
, ere
->re
->vrf_id
,
2997 "rn %p, re %p (%s) was deleted from kernel, adding",
2998 rn
, fib
, zebra_route_string(fib
->type
));
3000 if (zrouter
.allow_delete
||
3001 CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3002 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3004 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3006 UNSET_FLAG(rtnh
->flags
,
3010 * This is a non FRR route
3011 * as such we should mark
3014 dest
->selected_fib
= NULL
;
3017 * This means someone else, other than Zebra,
3018 * has deleted a Zebra router from the kernel.
3019 * We will add it back
3021 rib_install_kernel(rn
, fib
, NULL
);
3024 if (IS_ZEBRA_DEBUG_RIB
) {
3027 rn
, ere
->re
->vrf_id
,
3028 "via %s ifindex %d type %d doesn't exist in rib",
3029 inet_ntop(afi2family(ere
->afi
),
3032 nh
->ifindex
, ere
->re
->type
);
3035 rn
, ere
->re
->vrf_id
,
3036 "type %d doesn't exist in rib",
3039 route_unlock_node(rn
);
3040 early_route_memory_free(ere
);
3046 struct nexthop
*tmp_nh
;
3048 if (ere
->fromkernel
&&
3049 CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
) &&
3050 !zrouter
.allow_delete
) {
3051 rib_install_kernel(rn
, same
, NULL
);
3052 route_unlock_node(rn
);
3054 early_route_memory_free(ere
);
3058 /* Special handling for IPv4 or IPv6 routes sourced from
3059 * EVPN - the nexthop (and associated MAC) need to be
3060 * uninstalled if no more refs.
3062 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3063 struct ipaddr vtep_ip
;
3065 if (CHECK_FLAG(tmp_nh
->flags
, NEXTHOP_FLAG_EVPN
)) {
3066 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3067 if (ere
->afi
== AFI_IP
) {
3068 vtep_ip
.ipa_type
= IPADDR_V4
;
3069 memcpy(&(vtep_ip
.ipaddr_v4
),
3070 &(tmp_nh
->gate
.ipv4
),
3071 sizeof(struct in_addr
));
3073 vtep_ip
.ipa_type
= IPADDR_V6
;
3074 memcpy(&(vtep_ip
.ipaddr_v6
),
3075 &(tmp_nh
->gate
.ipv6
),
3076 sizeof(struct in6_addr
));
3078 zebra_rib_queue_evpn_route_del(
3079 re
->vrf_id
, &vtep_ip
, &ere
->p
);
3083 /* Notify dplane if system route changes */
3084 if (RIB_SYSTEM_ROUTE(re
))
3085 dplane_sys_route_del(rn
, same
);
3087 rib_delnode(rn
, same
);
3090 route_unlock_node(rn
);
3092 early_route_memory_free(ere
);
3096 * When FRR receives a route we need to match the route up to
3097 * nexthop groups. That we also may have just received
3098 * place the data on this queue so that this work of finding
3099 * the nexthop group entries for the route entry is always
3100 * done after the nexthop group has had a chance to be processed
3102 static void process_subq_early_route(struct listnode
*lnode
)
3104 struct zebra_early_route
*ere
= listgetdata(lnode
);
3107 process_subq_early_route_delete(ere
);
3109 process_subq_early_route_add(ere
);
3113 * Examine the specified subqueue; process one entry and return 1 if
3114 * there is a node, return 0 otherwise.
3116 static unsigned int process_subq(struct list
*subq
,
3117 enum meta_queue_indexes qindex
)
3119 struct listnode
*lnode
= listhead(subq
);
3125 case META_QUEUE_EVPN
:
3126 process_subq_evpn(lnode
);
3128 case META_QUEUE_NHG
:
3129 process_subq_nhg(lnode
);
3131 case META_QUEUE_EARLY_ROUTE
:
3132 process_subq_early_route(lnode
);
3134 case META_QUEUE_EARLY_LABEL
:
3135 process_subq_early_label(lnode
);
3137 case META_QUEUE_CONNECTED
:
3138 case META_QUEUE_KERNEL
:
3139 case META_QUEUE_STATIC
:
3140 case META_QUEUE_NOTBGP
:
3141 case META_QUEUE_BGP
:
3142 case META_QUEUE_OTHER
:
3143 process_subq_route(lnode
, qindex
);
3147 list_delete_node(subq
, lnode
);
3152 /* Dispatch the meta queue by picking and processing the next node from
3153 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3154 * data is pointed to the meta queue structure.
3156 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
3158 struct meta_queue
*mq
= data
;
3160 uint32_t queue_len
, queue_limit
;
3162 /* Ensure there's room for more dataplane updates */
3163 queue_limit
= dplane_get_in_queue_limit();
3164 queue_len
= dplane_get_in_queue_len();
3165 if (queue_len
> queue_limit
) {
3166 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3168 "rib queue: dplane queue len %u, limit %u, retrying",
3169 queue_len
, queue_limit
);
3171 /* Ensure that the meta-queue is actually enqueued */
3172 if (work_queue_empty(zrouter
.ribq
))
3173 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3175 return WQ_QUEUE_BLOCKED
;
3178 for (i
= 0; i
< MQ_SIZE
; i
++)
3179 if (process_subq(mq
->subq
[i
], i
)) {
3183 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
3188 * Look into the RN and queue it into the highest priority queue
3189 * at this point in time for processing.
3191 * We will enqueue a route node only once per invocation.
3193 * There are two possibilities here that should be kept in mind.
3194 * If the original invocation has not been pulled off for processing
3195 * yet, A subsuquent invocation can have a route entry with a better
3196 * meta queue index value and we can have a situation where
3197 * we might have the same node enqueued 2 times. Not necessarily
3198 * an optimal situation but it should be ok.
3200 * The other possibility is that the original invocation has not
3201 * been pulled off for processing yet, A subsusquent invocation
3202 * doesn't have a route_entry with a better meta-queue and the
3203 * original metaqueue index value will win and we'll end up with
3204 * the route node enqueued once.
3206 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
3208 struct route_node
*rn
= NULL
;
3209 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
3210 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
3212 rn
= (struct route_node
*)data
;
3214 RNODE_FOREACH_RE (rn
, curr_re
) {
3215 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
3217 if (curr_qindex
<= qindex
) {
3219 qindex
= curr_qindex
;
3226 /* Invariant: at this point we always have rn->info set. */
3227 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3228 RIB_ROUTE_QUEUED(qindex
))) {
3229 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3230 rnode_debug(rn
, re
->vrf_id
,
3231 "rn %p is already queued in sub-queue %s",
3232 (void *)rn
, subqueue2str(qindex
));
3236 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
3237 listnode_add(mq
->subq
[qindex
], rn
);
3238 route_lock_node(rn
);
3241 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3242 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %s",
3243 (void *)rn
, subqueue2str(qindex
));
3248 static int early_label_meta_queue_add(struct meta_queue
*mq
, void *data
)
3250 listnode_add(mq
->subq
[META_QUEUE_EARLY_LABEL
], data
);
3255 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
3257 struct nhg_ctx
*ctx
= NULL
;
3258 uint8_t qindex
= META_QUEUE_NHG
;
3259 struct wq_nhg_wrapper
*w
;
3261 ctx
= (struct nhg_ctx
*)data
;
3266 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3268 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
3271 listnode_add(mq
->subq
[qindex
], w
);
3274 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3275 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3276 ctx
->id
, subqueue2str(qindex
));
3281 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
3283 struct nhg_hash_entry
*nhe
= NULL
;
3284 uint8_t qindex
= META_QUEUE_NHG
;
3285 struct wq_nhg_wrapper
*w
;
3287 nhe
= (struct nhg_hash_entry
*)data
;
3292 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3294 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
3297 listnode_add(mq
->subq
[qindex
], w
);
3300 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3301 zlog_debug("NHG id=%u queued into sub-queue %s", nhe
->id
,
3302 subqueue2str(qindex
));
3307 static int rib_meta_queue_evpn_add(struct meta_queue
*mq
, void *data
)
3309 listnode_add(mq
->subq
[META_QUEUE_EVPN
], data
);
3315 static int mq_add_handler(void *data
,
3316 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
3318 if (zrouter
.ribq
== NULL
) {
3319 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3320 "%s: work_queue does not exist!", __func__
);
3325 * The RIB queue should normally be either empty or holding the only
3326 * work_queue_item element. In the latter case this element would
3327 * hold a pointer to the meta queue structure, which must be used to
3328 * actually queue the route nodes to process. So create the MQ
3329 * holder, if necessary, then push the work into it in any case.
3330 * This semantics was introduced after 0.99.9 release.
3332 if (work_queue_empty(zrouter
.ribq
))
3333 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3335 return mq_add_func(zrouter
.mq
, data
);
3338 void mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3339 struct prefix
*prefix
, uint8_t route_type
,
3340 uint8_t route_instance
)
3342 struct wq_label_wrapper
*w
;
3344 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3346 w
->type
= WQ_LABEL_FTN_UNINSTALL
;
3347 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3350 w
->route_type
= route_type
;
3351 w
->route_instance
= route_instance
;
3353 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3354 zlog_debug("Early Label Handling for %pFX", prefix
);
3356 mq_add_handler(w
, early_label_meta_queue_add
);
3359 void mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
3360 const struct zapi_labels
*zl
)
3362 struct wq_label_wrapper
*w
;
3364 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3365 w
->type
= WQ_LABEL_LABELS_PROCESS
;
3366 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3370 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3371 zlog_debug("Early Label Handling: Labels Process");
3373 mq_add_handler(w
, early_label_meta_queue_add
);
3376 /* Add route_node to work queue and schedule processing */
3377 int rib_queue_add(struct route_node
*rn
)
3381 /* Pointless to queue a route_node with no RIB entries to add or remove
3383 if (!rnode_to_ribs(rn
)) {
3384 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3385 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
3386 zlog_backtrace(LOG_DEBUG
);
3390 return mq_add_handler(rn
, rib_meta_queue_add
);
3394 * Enqueue incoming nhg info from OS for processing
3396 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
3400 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
3404 * Enqueue incoming nhg from proto daemon for processing
3406 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
3411 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
3415 * Enqueue evpn route for processing
3417 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id
, const struct ethaddr
*rmac
,
3418 const struct ipaddr
*vtep_ip
,
3419 const struct prefix
*host_prefix
)
3421 struct wq_evpn_wrapper
*w
;
3423 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3425 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3430 w
->prefix
= *host_prefix
;
3432 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3433 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3434 vrf_id
, vtep_ip
, host_prefix
);
3436 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3439 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id
,
3440 const struct ipaddr
*vtep_ip
,
3441 const struct prefix
*host_prefix
)
3443 struct wq_evpn_wrapper
*w
;
3445 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3447 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3451 w
->prefix
= *host_prefix
;
3453 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3454 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3455 vrf_id
, vtep_ip
, host_prefix
);
3457 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3460 /* Enqueue EVPN remote ES for processing */
3461 int zebra_rib_queue_evpn_rem_es_add(const esi_t
*esi
,
3462 const struct in_addr
*vtep_ip
,
3463 bool esr_rxed
, uint8_t df_alg
,
3466 struct wq_evpn_wrapper
*w
;
3467 char buf
[ESI_STR_LEN
];
3469 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3471 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3474 w
->ip
.ipa_type
= IPADDR_V4
;
3475 w
->ip
.ipaddr_v4
= *vtep_ip
;
3476 w
->esr_rxed
= esr_rxed
;
3478 w
->df_pref
= df_pref
;
3480 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3481 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3482 esi_to_str(esi
, buf
, sizeof(buf
)));
3484 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3487 int zebra_rib_queue_evpn_rem_es_del(const esi_t
*esi
,
3488 const struct in_addr
*vtep_ip
)
3490 struct wq_evpn_wrapper
*w
;
3491 char buf
[ESI_STR_LEN
];
3493 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3495 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3498 w
->ip
.ipa_type
= IPADDR_V4
;
3499 w
->ip
.ipaddr_v4
= *vtep_ip
;
3501 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3502 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3503 esi_to_str(esi
, buf
, sizeof(buf
));
3505 strlcpy(buf
, "-", sizeof(buf
));
3507 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3511 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3515 * Enqueue EVPN remote macip update for processing
3517 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni
, const struct ethaddr
*macaddr
,
3518 const struct ipaddr
*ipaddr
,
3519 uint8_t flags
, uint32_t seq
,
3520 struct in_addr vtep_ip
, const esi_t
*esi
)
3522 struct wq_evpn_wrapper
*w
;
3523 char buf
[ESI_STR_LEN
];
3525 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3527 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3530 w
->macaddr
= *macaddr
;
3534 w
->vtep_ip
= vtep_ip
;
3537 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3538 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3539 esi_to_str(esi
, buf
, sizeof(buf
));
3541 strlcpy(buf
, "-", sizeof(buf
));
3543 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__
,
3544 macaddr
, &vtep_ip
, buf
);
3547 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3550 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni
, const struct ethaddr
*macaddr
,
3551 const struct ipaddr
*ip
,
3552 struct in_addr vtep_ip
)
3554 struct wq_evpn_wrapper
*w
;
3556 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3558 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3561 w
->macaddr
= *macaddr
;
3563 w
->vtep_ip
= vtep_ip
;
3565 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3566 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__
,
3569 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3573 * Enqueue remote VTEP address for processing
3575 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id
, vni_t vni
,
3576 struct in_addr vtep_ip
, int flood_control
)
3578 struct wq_evpn_wrapper
*w
;
3580 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3582 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3586 w
->vtep_ip
= vtep_ip
;
3587 w
->flags
= flood_control
;
3589 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3590 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3593 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3596 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id
, vni_t vni
,
3597 struct in_addr vtep_ip
)
3599 struct wq_evpn_wrapper
*w
;
3601 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3603 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3607 w
->vtep_ip
= vtep_ip
;
3609 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3610 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3613 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3616 /* Create new meta queue.
3617 A destructor function doesn't seem to be necessary here.
3619 static struct meta_queue
*meta_queue_new(void)
3621 struct meta_queue
*new;
3624 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
3626 for (i
= 0; i
< MQ_SIZE
; i
++) {
3627 new->subq
[i
] = list_new();
3628 assert(new->subq
[i
]);
3634 /* Clean up the EVPN meta-queue list */
3635 static void evpn_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3636 struct zebra_vrf
*zvrf
)
3638 struct listnode
*node
, *nnode
;
3639 struct wq_evpn_wrapper
*w
;
3641 /* Free the node wrapper object, and the struct it wraps */
3642 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3644 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3646 if (w
->vrf_id
!= vrf_id
)
3652 XFREE(MTYPE_WQ_WRAPPER
, w
);
3654 list_delete_node(l
, node
);
3659 /* Clean up the nhg meta-queue list */
3660 static void nhg_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3661 struct zebra_vrf
*zvrf
)
3663 struct wq_nhg_wrapper
*w
;
3664 struct listnode
*node
, *nnode
;
3666 /* Free the node wrapper object, and the struct it wraps */
3667 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3669 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3671 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
&&
3672 w
->u
.ctx
->vrf_id
!= vrf_id
)
3674 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
&&
3675 w
->u
.nhe
->vrf_id
!= vrf_id
)
3678 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
3679 nhg_ctx_free(&w
->u
.ctx
);
3680 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
3681 zebra_nhg_free(w
->u
.nhe
);
3684 XFREE(MTYPE_WQ_WRAPPER
, w
);
3686 list_delete_node(l
, node
);
3691 static void early_label_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3692 struct zebra_vrf
*zvrf
)
3694 struct wq_label_wrapper
*w
;
3695 struct listnode
*node
, *nnode
;
3697 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3698 if (zvrf
&& zvrf
->vrf
->vrf_id
!= w
->vrf_id
)
3702 case WQ_LABEL_FTN_UNINSTALL
:
3703 case WQ_LABEL_LABELS_PROCESS
:
3708 XFREE(MTYPE_WQ_WRAPPER
, w
);
3709 list_delete_node(l
, node
);
3714 static void rib_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3715 struct zebra_vrf
*zvrf
)
3717 struct route_node
*rnode
;
3718 struct listnode
*node
, *nnode
;
3720 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, rnode
)) {
3721 rib_dest_t
*dest
= rib_dest_from_rnode(rnode
);
3723 if (dest
&& rib_dest_vrf(dest
) != zvrf
)
3726 route_unlock_node(rnode
);
3728 list_delete_node(l
, node
);
3733 static void early_route_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3734 struct zebra_vrf
*zvrf
)
3736 struct zebra_early_route
*ere
;
3737 struct listnode
*node
, *nnode
;
3739 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, ere
)) {
3740 if (zvrf
&& ere
->re
->vrf_id
!= zvrf
->vrf
->vrf_id
)
3743 early_route_memory_free(ere
);
3745 list_delete_node(l
, node
);
3750 void meta_queue_free(struct meta_queue
*mq
, struct zebra_vrf
*zvrf
)
3752 enum meta_queue_indexes i
;
3754 for (i
= 0; i
< MQ_SIZE
; i
++) {
3755 /* Some subqueues may need cleanup - nhgs for example */
3757 case META_QUEUE_NHG
:
3758 nhg_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3760 case META_QUEUE_EVPN
:
3761 evpn_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3763 case META_QUEUE_EARLY_ROUTE
:
3764 early_route_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3766 case META_QUEUE_EARLY_LABEL
:
3767 early_label_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3769 case META_QUEUE_CONNECTED
:
3770 case META_QUEUE_KERNEL
:
3771 case META_QUEUE_STATIC
:
3772 case META_QUEUE_NOTBGP
:
3773 case META_QUEUE_BGP
:
3774 case META_QUEUE_OTHER
:
3775 rib_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3779 list_delete(&mq
->subq
[i
]);
3783 XFREE(MTYPE_WORK_QUEUE
, mq
);
3786 /* initialise zebra rib work queue */
3787 static void rib_queue_init(void)
3789 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
3790 "route_node processing"))) {
3791 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3792 "%s: could not initialise work queue!", __func__
);
3796 /* fill in the work queue spec */
3797 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
3798 zrouter
.ribq
->spec
.completion_func
= NULL
;
3799 /* XXX: TODO: These should be runtime configurable via vty */
3800 zrouter
.ribq
->spec
.max_retries
= 3;
3801 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
3802 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
3804 if (!(zrouter
.mq
= meta_queue_new())) {
3805 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3806 "%s: could not initialise meta queue!", __func__
);
3812 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
3816 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
3817 rnh_list_init(&dest
->nht
);
3818 re_list_init(&dest
->routes
);
3819 route_lock_node(rn
); /* rn route table reference */
3826 /* RIB updates are processed via a queue of pointers to route_nodes.
3828 * The queue length is bounded by the maximal size of the routing table,
3829 * as a route_node will not be requeued, if already queued.
3831 * REs are submitted via rib_addnode or rib_delnode which set minimal
3832 * state, or static_install_route (when an existing RE is updated)
3833 * and then submit route_node to queue for best-path selection later.
3834 * Order of add/delete state changes are preserved for any given RE.
3836 * Deleted REs are reaped during best-path selection.
3839 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3840 * |-------->| | best RE, if required
3842 * static_install->|->rib_addqueue...... -> rib_process
3844 * |-------->| |-> rib_unlink
3845 * |-> set ROUTE_ENTRY_REMOVE |
3846 * rib_delnode (RE freed)
3848 * The 'info' pointer of a route_node points to a rib_dest_t
3849 * ('dest'). Queueing state for a route_node is kept on the dest. The
3850 * dest is created on-demand by rib_link() and is kept around at least
3851 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3853 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3855 * - route_nodes: refcounted by:
3856 * - dest attached to route_node:
3857 * - managed by: rib_link/rib_gc_dest
3858 * - route_node processing queue
3859 * - managed by: rib_addqueue, rib_process.
3863 /* Add RE to head of the route node. */
3864 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
3868 const char *rmap_name
;
3872 dest
= rib_dest_from_rnode(rn
);
3874 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3875 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
3877 dest
= zebra_rib_create_dest(rn
);
3880 re_list_add_head(&dest
->routes
, re
);
3882 afi
= (rn
->p
.family
== AF_INET
)
3884 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3885 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3886 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3888 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
3889 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
3894 static void rib_addnode(struct route_node
*rn
,
3895 struct route_entry
*re
, int process
)
3897 /* RE node has been un-removed before route-node is processed.
3898 * route_node must hence already be on the queue for processing..
3900 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3901 if (IS_ZEBRA_DEBUG_RIB
)
3902 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
3903 (void *)rn
, (void *)re
);
3905 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3908 rib_link(rn
, re
, process
);
3914 * Detach a rib structure from a route_node.
3916 * Note that a call to rib_unlink() should be followed by a call to
3917 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3918 * longer required to be deleted.
3920 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
3926 if (IS_ZEBRA_DEBUG_RIB
)
3927 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
3930 dest
= rib_dest_from_rnode(rn
);
3932 re_list_del(&dest
->routes
, re
);
3934 if (dest
->selected_fib
== re
)
3935 dest
->selected_fib
= NULL
;
3937 rib_re_nhg_free(re
);
3939 zapi_re_opaque_free(re
->opaque
);
3941 XFREE(MTYPE_RE
, re
);
3944 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
3948 if (IS_ZEBRA_DEBUG_RIB
)
3949 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
3950 (void *)rn
, (void *)re
);
3951 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3953 afi
= (rn
->p
.family
== AF_INET
)
3955 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3956 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3957 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3959 zebra_del_import_table_entry(zvrf
, rn
, re
);
3960 /* Just clean up if non main table */
3961 if (IS_ZEBRA_DEBUG_RIB
)
3962 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3963 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, rn
,
3964 rn
, re
, zebra_route_string(re
->type
));
3973 * Helper that debugs a single nexthop within a route-entry
3975 static void _route_entry_dump_nh(const struct route_entry
*re
,
3976 const char *straddr
,
3977 const struct nexthop
*nexthop
)
3979 char nhname
[PREFIX_STRLEN
];
3980 char backup_str
[50];
3983 char label_str
[MPLS_LABEL_STRLEN
];
3985 struct interface
*ifp
;
3986 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
3988 switch (nexthop
->type
) {
3989 case NEXTHOP_TYPE_BLACKHOLE
:
3990 snprintf(nhname
, sizeof(nhname
), "Blackhole");
3992 case NEXTHOP_TYPE_IFINDEX
:
3993 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
3994 snprintf(nhname
, sizeof(nhname
), "%s",
3995 ifp
? ifp
->name
: "Unknown");
3997 case NEXTHOP_TYPE_IPV4
:
3999 case NEXTHOP_TYPE_IPV4_IFINDEX
:
4000 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
4002 case NEXTHOP_TYPE_IPV6
:
4003 case NEXTHOP_TYPE_IPV6_IFINDEX
:
4004 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
4009 label_str
[0] = '\0';
4010 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
4011 mpls_label2str(nexthop
->nh_label
->num_labels
,
4012 nexthop
->nh_label
->label
, label_str
,
4013 sizeof(label_str
), 0 /*pretty*/);
4014 strlcat(label_str
, ", ", sizeof(label_str
));
4017 backup_str
[0] = '\0';
4018 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
4019 snprintf(backup_str
, sizeof(backup_str
), "backup ");
4020 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
4021 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
4022 nexthop
->backup_idx
[i
]);
4023 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
4028 if (nexthop
->weight
)
4029 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
4031 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
4032 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
4033 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
4035 wgt_str
, backup_str
,
4036 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
4039 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
4042 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
4045 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
4048 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
4051 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
4052 ? "FILTERED " : ""),
4053 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
4055 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
4057 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)
4062 /* This function dumps the contents of a given RE entry into
4063 * standard debug log. Calling function name and IP prefix in
4064 * question are passed as 1st and 2nd arguments.
4066 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
4067 union prefixconstptr src_pp
,
4068 const struct route_entry
*re
)
4070 const struct prefix
*src_p
= src_pp
.p
;
4071 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
4072 char straddr
[PREFIX_STRLEN
];
4073 char srcaddr
[PREFIX_STRLEN
];
4074 char flags_buf
[128];
4075 char status_buf
[128];
4076 struct nexthop
*nexthop
;
4077 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
4078 struct nexthop_group
*nhg
;
4080 prefix2str(pp
, straddr
, sizeof(straddr
));
4082 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
4083 (const void *)re
, straddr
,
4084 is_srcdst
? " from " : "",
4085 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
4087 VRF_LOGNAME(vrf
), re
->vrf_id
);
4088 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4089 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
4092 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4093 straddr
, re
->metric
, re
->mtu
, re
->distance
,
4094 zclient_dump_route_flags(re
->flags
, flags_buf
,
4096 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
4097 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
4098 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
4099 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
4102 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4103 _route_entry_dump_nh(re
, straddr
, nexthop
);
4105 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
4106 zlog_debug("%s: backup nexthops:", straddr
);
4108 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
4109 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
4110 _route_entry_dump_nh(re
, straddr
, nexthop
);
4113 zlog_debug("%s: dump complete", straddr
);
4116 static int rib_meta_queue_early_route_add(struct meta_queue
*mq
, void *data
)
4118 struct zebra_early_route
*ere
= data
;
4120 listnode_add(mq
->subq
[META_QUEUE_EARLY_ROUTE
], data
);
4123 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
4125 "Route %pFX(%u) queued for processing into sub-queue %s",
4126 &ere
->p
, ere
->re
->vrf_id
,
4127 subqueue2str(META_QUEUE_EARLY_ROUTE
));
4132 struct route_entry
*zebra_rib_route_entry_new(vrf_id_t vrf_id
, int type
,
4133 uint8_t instance
, uint32_t flags
,
4136 uint32_t metric
, uint32_t mtu
,
4137 uint8_t distance
, route_tag_t tag
)
4139 struct route_entry
*re
;
4141 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
4143 re
->instance
= instance
;
4144 re
->distance
= distance
;
4146 re
->metric
= metric
;
4148 re
->table
= table_id
;
4149 re
->vrf_id
= vrf_id
;
4150 re
->uptime
= monotime(NULL
);
4152 re
->nhe_id
= nhe_id
;
4157 * Internal route-add implementation; there are a couple of different public
4158 * signatures. Callers in this path are responsible for the memory they
4159 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4160 * must free those objects. If this returns < 0, an error has occurred and the
4161 * route_entry 're' has not been captured; the caller should free that also.
4167 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
4168 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4169 struct nhg_hash_entry
*re_nhe
, bool startup
)
4171 struct zebra_early_route
*ere
;
4176 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
4178 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4183 ere
->src_p
= *src_p
;
4184 ere
->src_p_provided
= !!src_p
;
4186 ere
->re_nhe
= re_nhe
;
4187 ere
->startup
= startup
;
4189 return mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4193 * Add a single route.
4195 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
4196 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4197 struct nexthop_group
*ng
, bool startup
)
4200 struct nhg_hash_entry nhe
, *n
;
4205 /* We either need nexthop(s) or an existing nexthop id */
4206 if (ng
== NULL
&& re
->nhe_id
== 0)
4210 * Use a temporary nhe to convey info to the common/main api.
4212 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
4214 nhe
.nhg
.nexthop
= ng
->nexthop
;
4215 else if (re
->nhe_id
> 0)
4216 nhe
.id
= re
->nhe_id
;
4218 n
= zebra_nhe_copy(&nhe
, 0);
4219 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, n
, startup
);
4221 /* In error cases, free the route also */
4223 XFREE(MTYPE_RE
, re
);
4228 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4229 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4230 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4231 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
4232 uint8_t distance
, bool fromkernel
)
4234 struct zebra_early_route
*ere
;
4235 struct route_entry
*re
= NULL
;
4236 struct nhg_hash_entry
*nhe
= NULL
;
4238 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4239 table_id
, metric
, 0, distance
, 0);
4242 nhe
= zebra_nhg_alloc();
4243 nhe
->nhg
.nexthop
= nexthop_dup(nh
, NULL
);
4246 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4251 ere
->src_p
= *src_p
;
4252 ere
->src_p_provided
= !!src_p
;
4255 ere
->startup
= false;
4256 ere
->deletion
= true;
4257 ere
->fromkernel
= fromkernel
;
4259 mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4263 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4264 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4265 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4266 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
4267 uint8_t distance
, route_tag_t tag
, bool startup
)
4269 struct route_entry
*re
= NULL
;
4270 struct nexthop nexthop
= {};
4271 struct nexthop_group ng
= {};
4273 /* Allocate new route_entry structure. */
4274 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4275 table_id
, metric
, mtu
, distance
, tag
);
4277 /* If the owner of the route supplies a shared nexthop-group id,
4278 * we'll use that. Otherwise, pass the nexthop along directly.
4283 nexthop_group_add_sorted(&ng
, &nexthop
);
4286 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, &ng
, startup
);
4289 static const char *rib_update_event2str(enum rib_update_event event
)
4291 const char *ret
= "UNKNOWN";
4294 case RIB_UPDATE_KERNEL
:
4295 ret
= "RIB_UPDATE_KERNEL";
4297 case RIB_UPDATE_RMAP_CHANGE
:
4298 ret
= "RIB_UPDATE_RMAP_CHANGE";
4300 case RIB_UPDATE_OTHER
:
4301 ret
= "RIB_UPDATE_OTHER";
4303 case RIB_UPDATE_MAX
:
4311 /* Schedule route nodes to be processed if they match the type */
4312 static void rib_update_route_node(struct route_node
*rn
, int type
)
4314 struct route_entry
*re
, *next
;
4315 bool re_changed
= false;
4317 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4318 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
4319 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
4328 /* Schedule routes of a particular table (address-family) based on event. */
4329 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
4332 struct route_node
*rn
;
4334 if (IS_ZEBRA_DEBUG_EVENT
) {
4335 struct zebra_vrf
*zvrf
;
4339 ? ((struct rib_table_info
*)table
->info
)->zvrf
4341 vrf
= zvrf
? zvrf
->vrf
: NULL
;
4343 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
4344 table
->info
? afi2str(
4345 ((struct rib_table_info
*)table
->info
)->afi
)
4347 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
4348 rib_update_event2str(event
), zebra_route_string(rtype
));
4351 /* Walk all routes and queue for processing, if appropriate for
4352 * the trigger event.
4354 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4356 * If we are looking at a route node and the node
4357 * has already been queued we don't
4358 * need to queue it up again
4361 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
4362 RIB_ROUTE_ANY_QUEUED
))
4366 case RIB_UPDATE_KERNEL
:
4367 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
4369 case RIB_UPDATE_RMAP_CHANGE
:
4370 case RIB_UPDATE_OTHER
:
4371 rib_update_route_node(rn
, rtype
);
4373 case RIB_UPDATE_MAX
:
4379 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
4381 struct zebra_router_table
*zrt
;
4383 if (IS_ZEBRA_DEBUG_EVENT
)
4384 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
4385 rib_update_event2str(event
));
4387 /* Just iterate over all the route tables, rather than vrf lookups */
4388 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
4389 rib_update_table(zrt
->table
, event
, rtype
);
4392 struct rib_update_ctx
{
4393 enum rib_update_event event
;
4397 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
4398 enum rib_update_event event
)
4400 struct rib_update_ctx
*ctx
;
4402 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
4405 ctx
->vrf_id
= vrf_id
;
4410 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
4412 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
4415 static void rib_update_handler(struct thread
*thread
)
4417 struct rib_update_ctx
*ctx
;
4419 ctx
= THREAD_ARG(thread
);
4421 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
4423 rib_update_ctx_fini(&ctx
);
4427 * Thread list to ensure we don't schedule a ton of events
4428 * if interfaces are flapping for instance.
4430 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
4432 /* Schedule a RIB update event for all vrfs */
4433 void rib_update(enum rib_update_event event
)
4435 struct rib_update_ctx
*ctx
;
4437 if (thread_is_scheduled(t_rib_update_threads
[event
]))
4440 ctx
= rib_update_ctx_init(0, event
);
4442 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
4443 &t_rib_update_threads
[event
]);
4445 if (IS_ZEBRA_DEBUG_EVENT
)
4446 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
4447 rib_update_event2str(event
));
4450 /* Delete self installed routes after zebra is relaunched. */
4451 void rib_sweep_table(struct route_table
*table
)
4453 struct route_node
*rn
;
4454 struct route_entry
*re
;
4455 struct route_entry
*next
;
4456 struct nexthop
*nexthop
;
4461 if (IS_ZEBRA_DEBUG_RIB
)
4462 zlog_debug("%s: starting", __func__
);
4464 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4465 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4467 if (IS_ZEBRA_DEBUG_RIB
)
4468 route_entry_dump(&rn
->p
, NULL
, re
);
4470 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4473 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
4477 * If routes are older than startup_time then
4478 * we know we read them in from the kernel.
4479 * As such we can safely remove them.
4481 if (zrouter
.startup_time
< re
->uptime
)
4485 * So we are starting up and have received
4486 * routes from the kernel that we have installed
4487 * from a previous run of zebra but not cleaned
4488 * up ( say a kill -9 )
4489 * But since we haven't actually installed
4490 * them yet( we received them from the kernel )
4491 * we don't think they are active.
4492 * So let's pretend they are active to actually
4494 * In all honesty I'm not sure if we should
4495 * mark them as active when we receive them
4496 * This is startup only so probably ok.
4498 * If we ever decide to move rib_sweep_table
4499 * to a different spot (ie startup )
4500 * this decision needs to be revisited
4502 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
4503 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4504 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
4506 rib_uninstall_kernel(rn
, re
);
4507 rib_delnode(rn
, re
);
4511 if (IS_ZEBRA_DEBUG_RIB
)
4512 zlog_debug("%s: ends", __func__
);
4515 /* Sweep all RIB tables. */
4516 void rib_sweep_route(struct thread
*t
)
4519 struct zebra_vrf
*zvrf
;
4521 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4522 if ((zvrf
= vrf
->info
) == NULL
)
4525 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4526 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4529 zebra_router_sweep_route();
4530 zebra_router_sweep_nhgs();
4533 /* Remove specific by protocol routes from 'table'. */
4534 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
4535 struct route_table
*table
)
4537 struct route_node
*rn
;
4538 struct route_entry
*re
;
4539 struct route_entry
*next
;
4540 unsigned long n
= 0;
4543 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
4544 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4545 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4547 if (re
->type
== proto
4548 && re
->instance
== instance
) {
4549 rib_delnode(rn
, re
);
4556 /* Remove specific by protocol routes. */
4557 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
4560 struct zebra_vrf
*zvrf
;
4561 struct other_route_table
*ort
;
4562 unsigned long cnt
= 0;
4564 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4569 cnt
+= rib_score_proto_table(proto
, instance
,
4570 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4571 + rib_score_proto_table(
4573 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4575 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
4576 rib_score_proto_table(proto
, instance
, ort
->table
);
4582 /* Close RIB and clean up kernel routes. */
4583 void rib_close_table(struct route_table
*table
)
4585 struct route_node
*rn
;
4591 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4592 dest
= rib_dest_from_rnode(rn
);
4594 if (dest
&& dest
->selected_fib
) {
4595 rib_uninstall_kernel(rn
, dest
->selected_fib
);
4596 dest
->selected_fib
= NULL
;
4602 * Handler for async dataplane results after a pseudowire installation
4604 static void handle_pw_result(struct zebra_dplane_ctx
*ctx
)
4606 struct zebra_pw
*pw
;
4607 struct zebra_vrf
*vrf
;
4609 /* The pseudowire code assumes success - we act on an error
4610 * result for installation attempts here.
4612 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
4615 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
4616 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
4617 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
4619 zebra_pw_install_failure(pw
,
4620 dplane_ctx_get_pw_status(ctx
));
4625 * Handle results from the dataplane system. Dequeue update context
4626 * structs, dispatch to appropriate internal handlers.
4628 static void rib_process_dplane_results(struct thread
*thread
)
4630 struct zebra_dplane_ctx
*ctx
;
4631 struct dplane_ctx_list_head ctxlist
;
4632 bool shut_p
= false;
4634 /* Dequeue a list of completed updates with one lock/unlock cycle */
4637 dplane_ctx_q_init(&ctxlist
);
4639 /* Take lock controlling queue of results */
4640 frr_with_mutex (&dplane_mutex
) {
4641 /* Dequeue list of context structs */
4642 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
4645 /* Dequeue context block */
4646 ctx
= dplane_ctx_dequeue(&ctxlist
);
4648 /* If we've emptied the results queue, we're done */
4652 /* If zebra is shutting down, avoid processing results,
4653 * just drain the results queue.
4655 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
4656 memory_order_relaxed
);
4659 dplane_ctx_fini(&ctx
);
4661 ctx
= dplane_ctx_dequeue(&ctxlist
);
4667 #ifdef HAVE_SCRIPTING
4668 char *script_name
= frrscript_names_get_script_name(
4669 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
4672 struct frrscript
*fs
;
4675 fs
= frrscript_new(script_name
);
4677 ret
= frrscript_load(
4678 fs
, ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4681 #endif /* HAVE_SCRIPTING */
4685 #ifdef HAVE_SCRIPTING
4688 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4690 #endif /* HAVE_SCRIPTING */
4692 switch (dplane_ctx_get_op(ctx
)) {
4693 case DPLANE_OP_ROUTE_INSTALL
:
4694 case DPLANE_OP_ROUTE_UPDATE
:
4695 case DPLANE_OP_ROUTE_DELETE
:
4696 /* Bit of special case for route updates
4697 * that were generated by async notifications:
4698 * we don't want to continue processing these
4701 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4702 rib_process_result(ctx
);
4705 case DPLANE_OP_ROUTE_NOTIFY
:
4706 rib_process_dplane_notify(ctx
);
4709 case DPLANE_OP_NH_INSTALL
:
4710 case DPLANE_OP_NH_UPDATE
:
4711 case DPLANE_OP_NH_DELETE
:
4712 zebra_nhg_dplane_result(ctx
);
4715 case DPLANE_OP_LSP_INSTALL
:
4716 case DPLANE_OP_LSP_UPDATE
:
4717 case DPLANE_OP_LSP_DELETE
:
4718 /* Bit of special case for LSP updates
4719 * that were generated by async notifications:
4720 * we don't want to continue processing these.
4722 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4723 zebra_mpls_lsp_dplane_result(ctx
);
4726 case DPLANE_OP_LSP_NOTIFY
:
4727 zebra_mpls_process_dplane_notify(ctx
);
4730 case DPLANE_OP_PW_INSTALL
:
4731 case DPLANE_OP_PW_UNINSTALL
:
4732 handle_pw_result(ctx
);
4735 case DPLANE_OP_SYS_ROUTE_ADD
:
4736 case DPLANE_OP_SYS_ROUTE_DELETE
:
4739 case DPLANE_OP_MAC_INSTALL
:
4740 case DPLANE_OP_MAC_DELETE
:
4741 zebra_vxlan_handle_result(ctx
);
4744 case DPLANE_OP_RULE_ADD
:
4745 case DPLANE_OP_RULE_DELETE
:
4746 case DPLANE_OP_RULE_UPDATE
:
4747 case DPLANE_OP_IPTABLE_ADD
:
4748 case DPLANE_OP_IPTABLE_DELETE
:
4749 case DPLANE_OP_IPSET_ADD
:
4750 case DPLANE_OP_IPSET_DELETE
:
4751 case DPLANE_OP_IPSET_ENTRY_ADD
:
4752 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4753 zebra_pbr_dplane_result(ctx
);
4756 case DPLANE_OP_INTF_ADDR_ADD
:
4757 case DPLANE_OP_INTF_ADDR_DEL
:
4758 case DPLANE_OP_INTF_INSTALL
:
4759 case DPLANE_OP_INTF_UPDATE
:
4760 case DPLANE_OP_INTF_DELETE
:
4761 case DPLANE_OP_INTF_NETCONFIG
:
4762 zebra_if_dplane_result(ctx
);
4765 case DPLANE_OP_TC_QDISC_INSTALL
:
4766 case DPLANE_OP_TC_QDISC_UNINSTALL
:
4767 case DPLANE_OP_TC_CLASS_ADD
:
4768 case DPLANE_OP_TC_CLASS_DELETE
:
4769 case DPLANE_OP_TC_CLASS_UPDATE
:
4770 case DPLANE_OP_TC_FILTER_ADD
:
4771 case DPLANE_OP_TC_FILTER_DELETE
:
4772 case DPLANE_OP_TC_FILTER_UPDATE
:
4775 /* Some op codes not handled here */
4776 case DPLANE_OP_ADDR_INSTALL
:
4777 case DPLANE_OP_ADDR_UNINSTALL
:
4778 case DPLANE_OP_NEIGH_INSTALL
:
4779 case DPLANE_OP_NEIGH_UPDATE
:
4780 case DPLANE_OP_NEIGH_DELETE
:
4781 case DPLANE_OP_NEIGH_IP_INSTALL
:
4782 case DPLANE_OP_NEIGH_IP_DELETE
:
4783 case DPLANE_OP_VTEP_ADD
:
4784 case DPLANE_OP_VTEP_DELETE
:
4785 case DPLANE_OP_NEIGH_DISCOVER
:
4786 case DPLANE_OP_BR_PORT_UPDATE
:
4787 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4788 case DPLANE_OP_GRE_SET
:
4789 case DPLANE_OP_NONE
:
4792 } /* Dispatch by op code */
4794 dplane_ctx_fini(&ctx
);
4795 ctx
= dplane_ctx_dequeue(&ctxlist
);
4802 * Results are returned from the dataplane subsystem, in the context of
4803 * the dataplane pthread. We enqueue the results here for processing by
4804 * the main thread later.
4806 static int rib_dplane_results(struct dplane_ctx_list_head
*ctxlist
)
4808 /* Take lock controlling queue of results */
4809 frr_with_mutex (&dplane_mutex
) {
4810 /* Enqueue context blocks */
4811 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4814 /* Ensure event is signalled to zebra main pthread */
4815 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4822 * Ensure there are no empty slots in the route_info array.
4823 * Every route type in zebra should be present there.
4825 static void check_route_info(void)
4827 int len
= array_size(route_info
);
4830 * ZEBRA_ROUTE_SYSTEM is special cased since
4831 * its key is 0 anyway.
4833 * ZEBRA_ROUTE_ALL is also ignored.
4835 for (int i
= 0; i
< len
; i
++) {
4836 assert(route_info
[i
].key
>= ZEBRA_ROUTE_SYSTEM
&&
4837 route_info
[i
].key
< ZEBRA_ROUTE_MAX
);
4838 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4842 /* Routing information base initialize. */
4849 /* Init dataplane, and register for results */
4850 pthread_mutex_init(&dplane_mutex
, NULL
);
4851 dplane_ctx_q_init(&rib_dplane_q
);
4852 zebra_dplane_init(rib_dplane_results
);
4858 * Get the first vrf id that is greater than the given vrf id if any.
4860 * Returns true if a vrf id was found, false otherwise.
4862 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4866 vrf
= vrf_lookup_by_id(vrf_id
);
4868 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4870 *next_id_p
= vrf
->vrf_id
;
4879 * rib_tables_iter_next
4881 * Returns the next table in the iteration.
4883 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4885 struct route_table
*table
;
4888 * Array that helps us go over all AFI/SAFI combinations via one
4891 static const struct {
4895 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4896 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4897 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4902 switch (iter
->state
) {
4904 case RIB_TABLES_ITER_S_INIT
:
4905 iter
->vrf_id
= VRF_DEFAULT
;
4906 iter
->afi_safi_ix
= -1;
4910 case RIB_TABLES_ITER_S_ITERATING
:
4911 iter
->afi_safi_ix
++;
4914 while (iter
->afi_safi_ix
4915 < (int)array_size(afi_safis
)) {
4916 table
= zebra_vrf_table(
4917 afi_safis
[iter
->afi_safi_ix
].afi
,
4918 afi_safis
[iter
->afi_safi_ix
].safi
,
4923 iter
->afi_safi_ix
++;
4927 * Found another table in this vrf.
4933 * Done with all tables in the current vrf, go to the
4937 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4940 iter
->afi_safi_ix
= 0;
4945 case RIB_TABLES_ITER_S_DONE
:
4950 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4952 iter
->state
= RIB_TABLES_ITER_S_DONE
;