1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
39 #include "nexthop_group_private.h"
40 #include "frr_pthread.h"
42 #include "frrscript.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra/connected.h"
46 #include "zebra/debug.h"
47 #include "zebra/interface.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/rib.h"
51 #include "zebra/zapi_msg.h"
52 #include "zebra/zebra_errors.h"
53 #include "zebra/zebra_ns.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/zebra_routemap.h"
56 #include "zebra/zebra_vrf.h"
57 #include "zebra/zebra_vxlan.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_dplane.h"
60 #include "zebra/zebra_evpn_mh.h"
61 #include "zebra/zebra_script.h"
63 DEFINE_MGROUP(ZEBRA
, "zebra");
65 DEFINE_MTYPE(ZEBRA
, RE
, "Route Entry");
66 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_DEST
, "RIB destination");
67 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
68 DEFINE_MTYPE_STATIC(ZEBRA
, WQ_WRAPPER
, "WQ wrapper");
71 * Event, list, and mutex for delivery of dataplane results
73 static pthread_mutex_t dplane_mutex
;
74 static struct thread
*t_dplane
;
75 static struct dplane_ctx_q rib_dplane_q
;
77 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
79 DEFINE_HOOK(rib_shutdown
, (struct route_node
* rn
), (rn
));
82 /* Meta Q's specific names */
83 enum meta_queue_indexes
{
86 META_QUEUE_EARLY_ROUTE
,
87 META_QUEUE_EARLY_LABEL
,
96 /* Each route type's string and default distance value. */
100 enum meta_queue_indexes meta_q_map
;
101 } route_info
[ZEBRA_ROUTE_MAX
] = {
102 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Unneeded for nhg's */,
104 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, META_QUEUE_KERNEL
},
105 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, META_QUEUE_KERNEL
},
106 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, META_QUEUE_CONNECTED
},
107 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, META_QUEUE_STATIC
},
108 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, META_QUEUE_NOTBGP
},
109 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, META_QUEUE_NOTBGP
},
110 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, META_QUEUE_NOTBGP
},
111 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, META_QUEUE_NOTBGP
},
112 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, META_QUEUE_NOTBGP
},
113 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */,
115 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, META_QUEUE_OTHER
},
116 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, META_QUEUE_NOTBGP
},
117 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, META_QUEUE_NOTBGP
},
118 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, META_QUEUE_OTHER
},
119 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, META_QUEUE_OTHER
},
120 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, META_QUEUE_STATIC
},
121 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, META_QUEUE_OTHER
},
122 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, META_QUEUE_BGP
},
123 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, META_QUEUE_BGP
},
124 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20,
126 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, META_QUEUE_BGP
},
127 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20,
129 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, META_QUEUE_NOTBGP
},
130 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, META_QUEUE_OTHER
},
131 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, META_QUEUE_OTHER
},
132 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, META_QUEUE_OTHER
},
133 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115,
135 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, META_QUEUE_OTHER
},
136 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, META_QUEUE_OTHER
},
137 [ZEBRA_ROUTE_ALL
] = {ZEBRA_ROUTE_ALL
, 255, META_QUEUE_OTHER
},
138 /* Any new route type added to zebra, should be mirrored here */
140 /* no entry/default: 150 */
143 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
144 * from the OS, and an 'nhe' is a nhe update.
146 struct wq_nhg_wrapper
{
150 struct nhg_hash_entry
*nhe
;
154 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
155 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
157 /* Wrapper structs for evpn/vxlan workqueue items. */
158 struct wq_evpn_wrapper
{
170 struct ethaddr macaddr
;
171 struct prefix prefix
;
172 struct in_addr vtep_ip
;
175 #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
176 #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
177 #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
178 #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
180 enum wq_label_types
{
181 WQ_LABEL_FTN_UNINSTALL
,
182 WQ_LABEL_LABELS_PROCESS
,
185 struct wq_label_wrapper
{
186 enum wq_label_types type
;
190 enum lsp_types_t ltype
;
192 uint8_t route_instance
;
195 struct zapi_labels zl
;
200 static void rib_addnode(struct route_node
*rn
, struct route_entry
*re
,
203 /* %pRN is already a printer for route_nodes that just prints the prefix */
204 #ifdef _FRR_ATTRIBUTE_PRINTFRR
205 #pragma FRR printfrr_ext "%pZN" (struct route_node *)
208 static const char *subqueue2str(enum meta_queue_indexes index
)
212 return "NHG Objects";
213 case META_QUEUE_EVPN
:
214 return "EVPN/VxLan Objects";
215 case META_QUEUE_EARLY_ROUTE
:
216 return "Early Route Processing";
217 case META_QUEUE_EARLY_LABEL
:
218 return "Early Label Handling";
219 case META_QUEUE_CONNECTED
:
220 return "Connected Routes";
221 case META_QUEUE_KERNEL
:
222 return "Kernel Routes";
223 case META_QUEUE_STATIC
:
224 return "Static Routes";
225 case META_QUEUE_NOTBGP
:
226 return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
229 case META_QUEUE_OTHER
:
230 return "Other Routes";
236 printfrr_ext_autoreg_p("ZN", printfrr_zebra_node
);
237 static ssize_t
printfrr_zebra_node(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
240 struct route_node
*rn
= (struct route_node
*)ptr
;
243 /* just the table number? */
244 if (ea
->fmt
[0] == 't') {
246 struct route_entry
*re
= NULL
;
251 return bputch(buf
, '!');
253 dest
= rib_dest_from_rnode(rn
);
255 re
= re_list_first(&dest
->routes
);
257 rv
+= bprintfrr(buf
, "%u", re
->table
);
259 rv
+= bputch(buf
, '?');
262 char cbuf
[PREFIX_STRLEN
* 2 + 6];
263 struct rib_table_info
*info
;
266 return bputs(buf
, "{(route_node *) NULL}");
268 srcdest_rnode2str(rn
, cbuf
, sizeof(cbuf
));
269 rv
+= bputs(buf
, cbuf
);
271 info
= srcdest_rnode_table_info(rn
);
272 if (info
->safi
== SAFI_MULTICAST
)
273 rv
+= bputs(buf
, " (MRIB)");
278 #define rnode_debug(node, vrf_id, msg, ...) \
279 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
282 #define rnode_info(node, vrf_id, msg, ...) \
283 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
286 static char *_dump_re_status(const struct route_entry
*re
, char *buf
,
289 if (re
->status
== 0) {
290 snprintfrr(buf
, len
, "None ");
295 buf
, len
, "%s%s%s%s%s%s%s",
296 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
) ? "Removed " : "",
297 CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) ? "Changed " : "",
298 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
301 CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
) ? "Queued " : "",
302 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
304 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
305 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
310 uint8_t route_distance(int type
)
314 if ((unsigned)type
>= array_size(route_info
))
317 distance
= route_info
[type
].distance
;
322 int is_zebra_valid_kernel_table(uint32_t table_id
)
325 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
326 || (table_id
== RT_TABLE_COMPAT
))
333 int is_zebra_main_routing_table(uint32_t table_id
)
335 if (table_id
== RT_TABLE_MAIN
)
340 int zebra_check_addr(const struct prefix
*p
)
342 if (p
->family
== AF_INET
) {
345 addr
= p
->u
.prefix4
.s_addr
;
348 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
349 || IPV4_LINKLOCAL(addr
))
352 if (p
->family
== AF_INET6
) {
353 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
355 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
361 static void route_entry_attach_ref(struct route_entry
*re
,
362 struct nhg_hash_entry
*new)
365 re
->nhe_id
= new->id
;
366 re
->nhe_installed_id
= 0;
368 zebra_nhg_increment_ref(new);
371 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
372 int route_entry_update_nhe(struct route_entry
*re
,
373 struct nhg_hash_entry
*new_nhghe
)
376 struct nhg_hash_entry
*old_nhg
= NULL
;
378 if (new_nhghe
== NULL
) {
382 re
->nhe_installed_id
= 0;
387 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
388 /* Capture previous nhg, if any */
391 route_entry_attach_ref(re
, new_nhghe
);
393 /* This is the first time it's being attached */
394 route_entry_attach_ref(re
, new_nhghe
);
397 /* Detach / deref previous nhg */
399 zebra_nhg_decrement_ref(old_nhg
);
404 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
405 struct nhg_hash_entry
*new_entry
)
407 struct zebra_router_table
*zrt
;
408 struct route_node
*rn
;
409 struct route_entry
*re
, *next
;
411 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
412 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
413 __func__
, new_entry
->id
, new_entry
, old_entry
);
415 /* We have to do them ALL */
416 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
417 for (rn
= route_top(zrt
->table
); rn
;
418 rn
= srcdest_route_next(rn
)) {
419 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
420 if (re
->nhe
&& re
->nhe
== old_entry
)
421 route_entry_update_nhe(re
, new_entry
);
427 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
428 const union g_addr
*addr
,
429 struct route_node
**rn_out
)
432 struct route_table
*table
;
433 struct route_node
*rn
;
434 struct route_entry
*match
= NULL
;
437 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
441 memset(&p
, 0, sizeof(p
));
444 p
.u
.prefix4
= addr
->ipv4
;
445 p
.prefixlen
= IPV4_MAX_BITLEN
;
447 p
.u
.prefix6
= addr
->ipv6
;
448 p
.prefixlen
= IPV6_MAX_BITLEN
;
451 rn
= route_node_match(table
, &p
);
456 route_unlock_node(rn
);
458 dest
= rib_dest_from_rnode(rn
);
459 if (dest
&& dest
->selected_fib
460 && !CHECK_FLAG(dest
->selected_fib
->status
,
461 ROUTE_ENTRY_REMOVED
))
462 match
= dest
->selected_fib
;
464 /* If there is no selected route or matched route is EGP, go up
469 } while (rn
&& rn
->info
== NULL
);
473 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
474 if (!CHECK_FLAG(match
->status
,
475 ROUTE_ENTRY_INSTALLED
))
487 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
489 struct route_node
**rn_out
)
491 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
492 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
493 union g_addr gaddr
= {.ipv4
= addr
};
495 switch (zrouter
.ipv4_multicast_mode
) {
496 case MCAST_MRIB_ONLY
:
497 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
499 case MCAST_URIB_ONLY
:
500 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
501 case MCAST_NO_CONFIG
:
502 case MCAST_MIX_MRIB_FIRST
:
503 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
506 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
509 case MCAST_MIX_DISTANCE
:
510 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
511 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
513 re
= ure
->distance
< mre
->distance
? ure
: mre
;
519 case MCAST_MIX_PFXLEN
:
520 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
521 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
523 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
532 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
534 if (IS_ZEBRA_DEBUG_RIB
) {
536 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
538 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
539 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
540 mre
? (ure
? "MRIB+URIB" : "MRIB")
541 : ure
? "URIB" : "nothing",
542 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
547 struct route_entry
*rib_match_ipv6_multicast(vrf_id_t vrf_id
,
548 struct in6_addr addr
,
549 struct route_node
**rn_out
)
551 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
552 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
553 union g_addr gaddr
= {.ipv6
= addr
};
555 switch (zrouter
.ipv4_multicast_mode
) {
556 case MCAST_MRIB_ONLY
:
557 return rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
559 case MCAST_URIB_ONLY
:
560 return rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
561 case MCAST_NO_CONFIG
:
562 case MCAST_MIX_MRIB_FIRST
:
563 re
= mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
566 re
= ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
,
569 case MCAST_MIX_DISTANCE
:
570 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
571 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
573 re
= ure
->distance
< mre
->distance
? ure
: mre
;
579 case MCAST_MIX_PFXLEN
:
580 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
581 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
583 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
592 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
594 if (IS_ZEBRA_DEBUG_RIB
)
595 zlog_debug("%s: %pI6: vrf: %s(%u) found %s, using %s", __func__
,
596 &addr
, vrf_id_to_name(vrf_id
), vrf_id
,
597 mre
? (ure
? "MRIB+URIB" : "MRIB")
598 : ure
? "URIB" : "nothing",
599 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
603 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
605 struct route_table
*table
;
606 struct route_node
*rn
;
607 struct route_entry
*match
= NULL
;
611 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
615 rn
= route_node_lookup(table
, (struct prefix
*)p
);
617 /* No route for this prefix. */
622 route_unlock_node(rn
);
623 dest
= rib_dest_from_rnode(rn
);
625 if (dest
&& dest
->selected_fib
626 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
627 match
= dest
->selected_fib
;
632 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
635 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
642 * Is this RIB labeled-unicast? It must be of type BGP and all paths
643 * (nexthops) must have a label.
645 int zebra_rib_labeled_unicast(struct route_entry
*re
)
647 struct nexthop
*nexthop
= NULL
;
649 if (re
->type
!= ZEBRA_ROUTE_BGP
)
652 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
653 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
659 /* Update flag indicates whether this is a "replace" or not. Currently, this
660 * is only used for IPv4.
662 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
663 struct route_entry
*old
)
665 struct nexthop
*nexthop
;
666 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
667 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
668 const struct prefix
*p
, *src_p
;
669 enum zebra_dplane_result ret
;
671 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
673 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
675 if (info
->safi
!= SAFI_UNICAST
) {
676 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
677 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
683 * Install the resolved nexthop object first.
685 zebra_nhg_install_kernel(re
->nhe
);
688 * If this is a replace to a new RE let the originator of the RE
689 * know that they've lost
691 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
692 zsend_route_notify_owner(rn
, old
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
693 info
->afi
, info
->safi
);
695 /* Update fib selection */
696 dest
->selected_fib
= re
;
699 * Make sure we update the FPM any time we send new information to
702 hook_call(rib_update
, rn
, "installing in kernel");
704 /* Send add or update */
706 ret
= dplane_route_update(rn
, re
, old
);
708 ret
= dplane_route_add(rn
, re
);
711 case ZEBRA_DPLANE_REQUEST_QUEUED
:
712 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
715 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
717 /* Free old FIB nexthop group */
718 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
719 if (old
->fib_ng
.nexthop
) {
720 nexthops_free(old
->fib_ng
.nexthop
);
721 old
->fib_ng
.nexthop
= NULL
;
726 zvrf
->installs_queued
++;
728 case ZEBRA_DPLANE_REQUEST_FAILURE
:
730 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
731 "%u:%u:%pRN: Failed to enqueue dataplane install",
732 re
->vrf_id
, re
->table
, rn
);
735 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
744 /* Uninstall the route from kernel. */
745 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
747 struct nexthop
*nexthop
;
748 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
749 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
751 if (info
->safi
!= SAFI_UNICAST
) {
752 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
753 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
754 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
759 * Make sure we update the FPM any time we send new information to
762 hook_call(rib_update
, rn
, "uninstalling from kernel");
764 switch (dplane_route_delete(rn
, re
)) {
765 case ZEBRA_DPLANE_REQUEST_QUEUED
:
767 zvrf
->removals_queued
++;
769 case ZEBRA_DPLANE_REQUEST_FAILURE
:
770 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
771 "%u:%pRN: Failed to enqueue dataplane uninstall",
774 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
784 * rib_can_delete_dest
786 * Returns true if the given dest can be deleted from the table.
788 static int rib_can_delete_dest(rib_dest_t
*dest
)
790 if (re_list_first(&dest
->routes
)) {
795 * Unresolved rnh's are stored on the default route's list
797 * dest->rnode can also be the source prefix node in an
798 * ipv6 sourcedest table. Fortunately the prefix of a
799 * source prefix node can never be the default prefix.
801 if (is_default_prefix(&dest
->rnode
->p
))
805 * Don't delete the dest if we have to update the FPM about this
808 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
809 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
815 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
,
818 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
822 * We are storing the rnh's associated withb
823 * the tracked nexthop as a list of the rn's.
824 * Unresolved rnh's are placed at the top
825 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
826 * As such for each rn we need to walk up the tree
827 * and see if any rnh's need to see if they
828 * would match a more specific route
831 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
833 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
835 dest
? rnh_list_count(&dest
->nht
) : 0);
837 if (rt_delete
&& (!dest
|| !rnh_list_count(&dest
->nht
))) {
838 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
839 zlog_debug("%pRN has no tracking NHTs. Bailing",
846 dest
= rib_dest_from_rnode(rn
);
850 * If we have any rnh's stored in the nht list
851 * then we know that this route node was used for
852 * nht resolution and as such we need to call the
853 * nexthop tracking evaluation code
855 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
856 struct zebra_vrf
*zvrf
=
857 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
858 struct prefix
*p
= &rnh
->node
->p
;
860 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
862 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
863 zvrf_name(zvrf
), zvrf_id(zvrf
), rn
,
864 rnh
->node
, seq
, rnh
->seqno
);
867 * If we have evaluated this node on this pass
868 * already, due to following the tree up
869 * then we know that we can move onto the next
872 * Additionally we call zebra_evaluate_rnh
873 * when we gc the dest. In this case we know
874 * that there must be no other re's where
875 * we were originally as such we know that
876 * that sequence number is ok to respect.
878 if (rnh
->seqno
== seq
) {
879 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
881 " Node processed and moved already");
886 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0, p
,
892 dest
= rib_dest_from_rnode(rn
);
899 * Garbage collect the rib dest corresponding to the given route node
902 * Returns true if the dest was deleted, false otherwise.
904 int rib_gc_dest(struct route_node
*rn
)
908 dest
= rib_dest_from_rnode(rn
);
912 if (!rib_can_delete_dest(dest
))
915 if (IS_ZEBRA_DEBUG_RIB
) {
916 struct zebra_vrf
*zvrf
;
918 zvrf
= rib_dest_vrf(dest
);
919 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
922 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
926 rnh_list_fini(&dest
->nht
);
927 XFREE(MTYPE_RIB_DEST
, dest
);
931 * Release the one reference that we keep on the route node.
933 route_unlock_node(rn
);
937 void zebra_rtable_node_cleanup(struct route_table
*table
,
938 struct route_node
*node
)
940 struct route_entry
*re
, *next
;
942 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
943 rib_unlink(node
, re
);
947 rib_dest_t
*dest
= node
->info
;
949 /* Remove from update queue of FPM module */
950 hook_call(rib_shutdown
, node
);
952 rnh_list_fini(&dest
->nht
);
953 XFREE(MTYPE_RIB_DEST
, node
->info
);
957 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
958 struct route_entry
*new)
960 hook_call(rib_update
, rn
, "new route selected");
962 /* Update real nexthop. This may actually determine if nexthop is active
964 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
965 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
969 if (IS_ZEBRA_DEBUG_RIB
)
970 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
971 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, rn
, rn
,
972 new, zebra_route_string(new->type
));
974 /* If labeled-unicast route, install transit LSP. */
975 if (zebra_rib_labeled_unicast(new))
976 zebra_mpls_lsp_install(zvrf
, rn
, new);
978 rib_install_kernel(rn
, new, NULL
);
980 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
983 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
984 struct route_entry
*old
)
986 hook_call(rib_update
, rn
, "removing existing route");
988 /* Uninstall from kernel. */
989 if (IS_ZEBRA_DEBUG_RIB
)
990 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
991 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, rn
, rn
,
992 old
, zebra_route_string(old
->type
));
994 /* If labeled-unicast route, uninstall transit LSP. */
995 if (zebra_rib_labeled_unicast(old
))
996 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
998 rib_uninstall_kernel(rn
, old
);
1000 /* Update nexthop for route, reset changed flag. */
1001 /* Note: this code also handles the Linux case when an interface goes
1002 * down, causing the kernel to delete routes without sending DELROUTE
1005 if (RIB_KERNEL_ROUTE(old
))
1006 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1008 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1011 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1012 struct route_node
*rn
,
1013 struct route_entry
*old
,
1014 struct route_entry
*new)
1019 * We have to install or update if a new route has been selected or
1020 * something has changed.
1022 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1023 hook_call(rib_update
, rn
, "updating existing route");
1025 /* Update the nexthop; we could determine here that nexthop is
1027 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
1030 /* If nexthop is active, install the selected route, if
1032 * the install succeeds, cleanup flags for prior route, if
1037 if (IS_ZEBRA_DEBUG_RIB
) {
1040 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
1041 zvrf_name(zvrf
), zvrf_id(zvrf
),
1042 new->table
, rn
, rn
, new,
1043 zebra_route_string(new->type
),
1045 zebra_route_string(old
->type
));
1048 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
1049 zvrf_name(zvrf
), zvrf_id(zvrf
),
1050 new->table
, rn
, rn
, new,
1051 zebra_route_string(new->type
));
1054 /* If labeled-unicast route, uninstall transit LSP. */
1055 if (zebra_rib_labeled_unicast(old
))
1056 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1059 * Non-system route should be installed.
1060 * If labeled-unicast route, install transit
1063 if (zebra_rib_labeled_unicast(new))
1064 zebra_mpls_lsp_install(zvrf
, rn
, new);
1066 rib_install_kernel(rn
, new, old
);
1070 * If nexthop for selected route is not active or install
1072 * may need to uninstall and delete for redistribution.
1075 if (IS_ZEBRA_DEBUG_RIB
) {
1078 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1079 zvrf_name(zvrf
), zvrf_id(zvrf
),
1080 new->table
, rn
, rn
, new,
1081 zebra_route_string(new->type
),
1083 zebra_route_string(old
->type
));
1086 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
1087 zvrf_name(zvrf
), zvrf_id(zvrf
),
1088 new->table
, rn
, rn
, new,
1089 zebra_route_string(new->type
));
1093 * When we have gotten to this point
1094 * the new route entry has no nexthops
1095 * that are usable and as such we need
1096 * to remove the old route, but only
1097 * if we were the one who installed
1100 if (!RIB_SYSTEM_ROUTE(old
)) {
1101 /* If labeled-unicast route, uninstall transit
1103 if (zebra_rib_labeled_unicast(old
))
1104 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1106 rib_uninstall_kernel(rn
, old
);
1111 * Same route selected; check if in the FIB and if not,
1112 * re-install. This is housekeeping code to deal with
1113 * race conditions in kernel with linux netlink reporting
1114 * interface up before IPv4 or IPv6 protocol is ready
1117 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1118 RIB_SYSTEM_ROUTE(new))
1119 rib_install_kernel(rn
, new, NULL
);
1122 /* Update prior route. */
1124 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1126 /* Clear changed flag. */
1127 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1130 /* Check if 'alternate' RIB entry is better than 'current'. */
1131 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1132 struct route_entry
*alternate
)
1134 if (current
== NULL
)
1137 /* filter route selection in following order:
1138 * - connected beats other types
1139 * - if both connected, loopback or vrf wins
1140 * - lower distance beats higher
1141 * - lower metric beats higher for equal distance
1142 * - last, hence oldest, route wins tie break.
1145 /* Connected routes. Check to see if either are a vrf
1146 * or loopback interface. If not, pick the last connected
1147 * route of the set of lowest metric connected routes.
1149 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1150 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1153 /* both are connected. are either loop or vrf? */
1154 struct nexthop
*nexthop
= NULL
;
1156 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1157 struct interface
*ifp
= if_lookup_by_index(
1158 nexthop
->ifindex
, alternate
->vrf_id
);
1160 if (ifp
&& if_is_loopback(ifp
))
1164 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1165 struct interface
*ifp
= if_lookup_by_index(
1166 nexthop
->ifindex
, current
->vrf_id
);
1168 if (ifp
&& if_is_loopback(ifp
))
1172 /* Neither are loop or vrf so pick best metric */
1173 if (alternate
->metric
<= current
->metric
)
1179 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1182 /* higher distance loses */
1183 if (alternate
->distance
< current
->distance
)
1185 if (current
->distance
< alternate
->distance
)
1188 /* metric tie-breaks equal distance */
1189 if (alternate
->metric
<= current
->metric
)
1195 /* Core function for processing routing information base. */
1196 static void rib_process(struct route_node
*rn
)
1198 struct route_entry
*re
;
1199 struct route_entry
*next
;
1200 struct route_entry
*old_selected
= NULL
;
1201 struct route_entry
*new_selected
= NULL
;
1202 struct route_entry
*old_fib
= NULL
;
1203 struct route_entry
*new_fib
= NULL
;
1204 struct route_entry
*best
= NULL
;
1206 struct zebra_vrf
*zvrf
= NULL
;
1209 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1213 dest
= rib_dest_from_rnode(rn
);
1215 * We have an enqueued node with nothing to process here
1216 * let's just finish up and return;
1221 zvrf
= rib_dest_vrf(dest
);
1222 vrf_id
= zvrf_id(zvrf
);
1224 vrf
= vrf_lookup_by_id(vrf_id
);
1227 * we can have rn's that have a NULL info pointer
1228 * (dest). As such let's not let the deref happen
1229 * additionally we know RNODE_FOREACH_RE_SAFE
1230 * will not iterate so we are ok.
1232 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1233 struct route_entry
*re
= re_list_first(&dest
->routes
);
1235 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1236 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
,
1240 old_fib
= dest
->selected_fib
;
1242 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1243 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1244 char flags_buf
[128];
1245 char status_buf
[128];
1248 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1249 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
, re
,
1250 zebra_route_string(re
->type
),
1251 _dump_re_status(re
, status_buf
,
1252 sizeof(status_buf
)),
1253 zclient_dump_route_flags(re
->flags
, flags_buf
,
1255 re
->distance
, re
->metric
);
1258 /* Currently selected re. */
1259 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1260 assert(old_selected
== NULL
);
1264 /* Skip deleted entries from selection */
1265 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1269 * If the route entry has changed, verify/resolve
1270 * the nexthops associated with the entry.
1272 * In any event if we have nexthops that are not active
1273 * then we cannot use this particular route entry so
1276 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1277 if (!nexthop_active_update(rn
, re
)) {
1278 const struct prefix
*p
;
1279 struct rib_table_info
*info
;
1281 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1282 /* XXX: HERE BE DRAGONS!!!!!
1283 * In all honesty, I have not yet
1284 * figured out what this part does or
1285 * why the ROUTE_ENTRY_CHANGED test
1286 * above is correct or why we need to
1287 * delete a route here, and also not
1288 * whether this concerns both selected
1289 * and fib route, or only selected
1292 * This entry was denied by the 'ip
1294 * table' route-map, we need to delete
1296 if (re
!= old_selected
) {
1297 if (IS_ZEBRA_DEBUG_RIB
)
1299 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1306 SET_FLAG(re
->status
,
1307 ROUTE_ENTRY_REMOVED
);
1310 info
= srcdest_rnode_table_info(rn
);
1311 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1312 zsend_route_notify_owner(
1313 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
1314 info
->afi
, info
->safi
);
1319 * If the re has not changed and the nhg we have is
1320 * not usable, then we cannot use this route entry
1321 * for consideration, as that the route will just
1322 * not install if it is selected.
1324 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1328 /* Infinite distance. */
1329 if (re
->distance
== DISTANCE_INFINITY
&&
1330 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1331 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1335 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1336 best
= rib_choose_best(new_fib
, re
);
1337 if (new_fib
&& best
!= new_fib
)
1338 UNSET_FLAG(new_fib
->status
,
1339 ROUTE_ENTRY_CHANGED
);
1342 best
= rib_choose_best(new_selected
, re
);
1343 if (new_selected
&& best
!= new_selected
)
1344 UNSET_FLAG(new_selected
->status
,
1345 ROUTE_ENTRY_CHANGED
);
1346 new_selected
= best
;
1349 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1350 } /* RNODE_FOREACH_RE */
1352 /* If no FIB override route, use the selected route also for FIB */
1353 if (new_fib
== NULL
)
1354 new_fib
= new_selected
;
1356 /* After the cycle is finished, the following pointers will be set:
1357 * old_selected --- RE entry currently having SELECTED
1358 * new_selected --- RE entry that is newly SELECTED
1359 * old_fib --- RE entry currently in kernel FIB
1360 * new_fib --- RE entry that is newly to be in kernel FIB
1362 * new_selected will get SELECTED flag, and is going to be redistributed
1363 * the zclients. new_fib (which can be new_selected) will be installed
1367 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1368 struct route_entry
*entry
;
1370 entry
= old_selected
1375 : new_fib
? new_fib
: NULL
;
1378 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1379 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, rn
,
1380 (void *)old_selected
, (void *)new_selected
,
1381 (void *)old_fib
, (void *)new_fib
);
1384 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1385 * fib == selected */
1386 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1387 ROUTE_ENTRY_CHANGED
);
1389 /* Update SELECTED entry */
1390 if (old_selected
!= new_selected
|| selected_changed
) {
1392 if (new_selected
&& new_selected
!= new_fib
)
1393 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1396 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1400 * If we're removing the old entry, we should tell
1401 * redist subscribers about that *if* they aren't
1402 * going to see a redist for the new entry.
1404 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1405 ROUTE_ENTRY_REMOVED
))
1406 redistribute_delete(rn
, old_selected
,
1409 if (old_selected
!= new_selected
)
1410 UNSET_FLAG(old_selected
->flags
,
1411 ZEBRA_FLAG_SELECTED
);
1415 /* Update fib according to selection results */
1416 if (new_fib
&& old_fib
)
1417 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1419 rib_process_add_fib(zvrf
, rn
, new_fib
);
1421 rib_process_del_fib(zvrf
, rn
, old_fib
);
1423 /* Remove all RE entries queued for removal */
1424 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1425 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1426 if (IS_ZEBRA_DEBUG_RIB
) {
1427 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1428 (void *)rn
, (void *)re
);
1435 * Check if the dest can be deleted now.
1440 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1442 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1443 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1448 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1449 if (IS_ZEBRA_DEBUG_MPLS
)
1451 "%s(%u): Scheduling all LSPs upon RIB completion",
1452 zvrf_name(zvrf
), zvrf_id(zvrf
));
1453 zebra_mpls_lsp_schedule(zvrf
);
1454 mpls_unmark_lsps_for_processing(rn
);
1459 * Utility to match route with dplane context data
1461 static bool rib_route_match_ctx(const struct route_entry
*re
,
1462 const struct zebra_dplane_ctx
*ctx
,
1465 bool result
= false;
1469 * In 'update' case, we test info about the 'previous' or
1472 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1473 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1476 /* We use an extra test for statics, and another for
1479 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1480 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1481 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1483 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1485 dplane_ctx_get_old_metric(ctx
)) {
1492 * Ordinary, single-route case using primary context info
1494 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1495 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1496 /* Skip route that's been deleted */
1500 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1501 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1504 /* We use an extra test for statics, and another for
1507 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1508 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1509 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1511 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1512 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1514 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1515 result
= nexthop_group_equal_no_recurse(
1516 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1526 static void zebra_rib_fixup_system(struct route_node
*rn
)
1528 struct route_entry
*re
;
1530 RNODE_FOREACH_RE(rn
, re
) {
1531 struct nexthop
*nhop
;
1533 if (!RIB_SYSTEM_ROUTE(re
))
1536 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1539 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1540 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1542 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1543 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1546 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1551 /* Route comparison logic, with various special cases. */
1552 static bool rib_compare_routes(const struct route_entry
*re1
,
1553 const struct route_entry
*re2
)
1555 if (re1
->type
!= re2
->type
)
1558 if (re1
->instance
!= re2
->instance
)
1561 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1564 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1565 re1
->distance
!= re2
->distance
)
1568 /* We support multiple connected routes: this supports multiple
1569 * v6 link-locals, and we also support multiple addresses in the same
1570 * subnet on a single interface.
1572 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1579 * Compare nexthop lists from a route and a dplane context; test whether
1580 * the list installed in the FIB matches the route's list.
1581 * Set 'changed_p' to 'true' if there were changes to the route's
1582 * installed nexthops.
1584 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1587 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1588 const struct nexthop_group
*ctx_nhg
,
1591 bool matched_p
= true;
1592 struct nexthop
*nexthop
, *ctx_nexthop
;
1594 /* Get the first `installed` one to check against.
1595 * If the dataplane doesn't set these to be what was actually installed,
1596 * it will just be whatever was in re->nhe->nhg?
1598 ctx_nexthop
= ctx_nhg
->nexthop
;
1600 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1601 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1602 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1604 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1606 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1609 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1612 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1613 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1614 /* If the FIB doesn't know about the nexthop,
1615 * it's not installed
1617 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1618 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1619 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1621 (CHECK_FLAG(nexthop
->flags
,
1627 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1630 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1632 /* Keep checking nexthops */
1636 if (CHECK_FLAG(ctx_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 -> installed",
1645 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1647 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1648 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1649 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1655 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1658 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1665 * Update a route from a dplane context. This consolidates common code
1666 * that can be used in processing of results from FIB updates, and in
1667 * async notification processing.
1668 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1670 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1671 struct route_node
*rn
,
1672 struct zebra_dplane_ctx
*ctx
)
1674 struct nexthop
*nexthop
;
1676 const struct nexthop_group
*ctxnhg
;
1677 struct nexthop_group
*re_nhg
;
1678 bool is_selected
= false; /* Is 're' currently the selected re? */
1679 bool changed_p
= false; /* Change to nexthops? */
1683 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1685 dest
= rib_dest_from_rnode(rn
);
1687 is_selected
= (re
== dest
->selected_fib
);
1689 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1690 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1691 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1692 (is_selected
? "" : "NOT "), re
);
1694 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1695 * If the installed set differs from the set requested by the rib/owner,
1696 * we use the fib-specific nexthop-group to record the actual FIB
1700 ctxnhg
= dplane_ctx_get_ng(ctx
);
1702 /* Check route's fib group and incoming notif group for equivalence.
1704 * Let's assume the nexthops are ordered here to save time.
1706 /* TODO -- this isn't testing or comparing the FIB flags; we should
1707 * do a more explicit loop, checking the incoming notification's flags.
1709 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1710 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1713 /* If the new FIB set matches the existing FIB set, we're done. */
1715 if (IS_ZEBRA_DEBUG_RIB
)
1717 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1718 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1721 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1723 * Free stale fib list and move on to check the rib nhg.
1725 if (IS_ZEBRA_DEBUG_RIB
)
1727 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1728 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1729 nexthops_free(re
->fib_ng
.nexthop
);
1730 re
->fib_ng
.nexthop
= NULL
;
1732 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1734 /* Note that the installed nexthops have changed */
1737 if (IS_ZEBRA_DEBUG_RIB
)
1739 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1740 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1744 * Compare with the rib nexthop group. The comparison here is different:
1745 * the RIB group may be a superset of the list installed in the FIB. We
1746 * walk the RIB group, looking for the 'installable' candidate
1747 * nexthops, and then check those against the set
1748 * that is actually installed.
1750 * Assume nexthops are ordered here as well.
1753 /* If nothing is installed, we can skip some of the checking/comparison
1756 if (ctxnhg
->nexthop
== NULL
) {
1761 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1763 /* If all nexthops were processed, we're done */
1765 if (IS_ZEBRA_DEBUG_RIB
)
1767 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1768 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1769 (changed_p
? "true" : "false"));
1775 /* FIB nexthop set differs from the RIB set:
1776 * create a fib-specific nexthop-group
1778 if (IS_ZEBRA_DEBUG_RIB
)
1780 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1781 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1782 (changed_p
? "true" : "false"),
1783 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1785 /* Set the flag about the dedicated fib list */
1786 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1787 if (ctxnhg
->nexthop
)
1788 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1793 * Check the status of the route's backup nexthops, if any.
1794 * The logic for backups is somewhat different: if any backup is
1795 * installed, a new fib nhg will be attached to the route.
1797 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1799 goto done
; /* No backup nexthops */
1801 /* First check the route's 'fib' list of backups, if it's present
1802 * from some previous event.
1804 re_nhg
= &re
->fib_backup_ng
;
1805 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1808 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1811 /* If the new FIB set matches an existing FIB set, we're done. */
1813 if (IS_ZEBRA_DEBUG_RIB
)
1815 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1816 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1819 } else if (re
->fib_backup_ng
.nexthop
) {
1821 * Free stale fib backup list and move on to check
1822 * the route's backups.
1824 if (IS_ZEBRA_DEBUG_RIB
)
1826 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1827 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1828 nexthops_free(re
->fib_backup_ng
.nexthop
);
1829 re
->fib_backup_ng
.nexthop
= NULL
;
1831 /* Note that the installed nexthops have changed */
1834 if (IS_ZEBRA_DEBUG_RIB
)
1836 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1837 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1841 * If a FIB backup nexthop set exists, attach a copy
1842 * to the route if any backup is installed
1844 if (ctxnhg
&& ctxnhg
->nexthop
) {
1846 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1847 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1851 /* If no installed backups, we're done */
1852 if (nexthop
== NULL
)
1855 if (IS_ZEBRA_DEBUG_RIB
)
1857 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1858 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1859 (changed_p
? "true" : "false"));
1861 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1871 * Helper to locate a zebra route-node from a dplane context. This is used
1872 * when processing dplane results, e.g. Note well: the route-node is returned
1873 * with a ref held - route_unlock_node() must be called eventually.
1875 struct route_node
*rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1877 struct route_table
*table
= NULL
;
1878 struct route_node
*rn
= NULL
;
1879 const struct prefix
*dest_pfx
, *src_pfx
;
1881 /* Locate rn and re(s) from ctx */
1883 table
= zebra_vrf_lookup_table_with_table_id(
1884 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1885 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1886 if (table
== NULL
) {
1887 if (IS_ZEBRA_DEBUG_DPLANE
) {
1889 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1890 dplane_ctx_get_afi(ctx
),
1891 dplane_ctx_get_safi(ctx
),
1892 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1893 dplane_ctx_get_vrf(ctx
));
1898 dest_pfx
= dplane_ctx_get_dest(ctx
);
1899 src_pfx
= dplane_ctx_get_src(ctx
);
1901 rn
= srcdest_rnode_get(table
, dest_pfx
,
1902 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1911 * Route-update results processing after async dataplane update.
1913 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1915 struct zebra_vrf
*zvrf
= NULL
;
1917 struct route_node
*rn
= NULL
;
1918 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1919 bool is_update
= false;
1920 enum dplane_op_e op
;
1921 enum zebra_dplane_result status
;
1924 bool fib_changed
= false;
1925 struct rib_table_info
*info
;
1926 bool rt_delete
= false;
1928 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1929 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1931 /* Locate rn and re(s) from ctx */
1932 rn
= rib_find_rn_from_ctx(ctx
);
1934 if (IS_ZEBRA_DEBUG_DPLANE
) {
1936 "Failed to process dplane results: no route for %s(%u):%pRN",
1937 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
), rn
);
1942 dest
= rib_dest_from_rnode(rn
);
1943 info
= srcdest_rnode_table_info(rn
);
1945 op
= dplane_ctx_get_op(ctx
);
1946 status
= dplane_ctx_get_status(ctx
);
1948 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1950 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1951 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1952 dplane_ctx_get_table(ctx
), rn
, ctx
, dplane_op2str(op
),
1953 dplane_res2str(status
));
1956 * Update is a bit of a special case, where we may have both old and new
1957 * routes to post-process.
1959 is_update
= dplane_ctx_is_update(ctx
);
1962 * Take a pass through the routes, look for matches with the context
1965 RNODE_FOREACH_RE(rn
, rib
) {
1968 if (rib_route_match_ctx(rib
, ctx
, false))
1972 /* Check for old route match */
1973 if (is_update
&& (old_re
== NULL
)) {
1974 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1978 /* Have we found the routes we need to work on? */
1979 if (re
&& ((!is_update
|| old_re
)))
1983 seq
= dplane_ctx_get_seq(ctx
);
1986 * Check sequence number(s) to detect stale results before continuing
1989 if (re
->dplane_sequence
!= seq
) {
1990 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1992 "%s(%u):%pRN Stale dplane result for re %p",
1994 dplane_ctx_get_vrf(ctx
), rn
, re
);
1996 if (!zrouter
.asic_offloaded
||
1997 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
1998 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)))
1999 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2004 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
2005 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2007 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
2009 dplane_ctx_get_vrf(ctx
), old_re
->table
,
2012 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
2016 case DPLANE_OP_ROUTE_INSTALL
:
2017 case DPLANE_OP_ROUTE_UPDATE
:
2018 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2020 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2021 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2024 * On an update operation from the same route type
2025 * context retrieval currently has no way to know
2026 * which was the old and which was the new.
2027 * So don't unset our flags that we just set.
2028 * We know redistribution is ok because the
2029 * old_re in this case is used for nothing
2030 * more than knowing whom to contact if necessary.
2032 if (old_re
&& old_re
!= re
) {
2033 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2034 UNSET_FLAG(old_re
->status
,
2035 ROUTE_ENTRY_INSTALLED
);
2038 /* Update zebra route based on the results in
2039 * the context struct.
2043 rib_update_re_from_ctx(re
, rn
, ctx
);
2046 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2048 "%s(%u:%u):%pRN no fib change for re",
2050 dplane_ctx_get_vrf(ctx
),
2051 dplane_ctx_get_table(
2056 /* Redistribute if this is the selected re */
2057 if (dest
&& re
== dest
->selected_fib
)
2058 redistribute_update(rn
, re
, old_re
);
2062 * System routes are weird in that they
2063 * allow multiple to be installed that match
2064 * to the same prefix, so after we get the
2065 * result we need to clean them up so that
2066 * we can actually use them.
2068 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2069 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2070 zebra_rib_fixup_system(rn
);
2075 /* Notify route owner */
2076 if (zebra_router_notify_on_ack())
2077 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2080 if (CHECK_FLAG(re
->flags
,
2081 ZEBRA_FLAG_OFFLOADED
))
2082 zsend_route_notify_owner_ctx(
2084 ZAPI_ROUTE_INSTALLED
);
2087 ZEBRA_FLAG_OFFLOAD_FAILED
))
2088 zsend_route_notify_owner_ctx(
2090 ZAPI_ROUTE_FAIL_INSTALL
);
2095 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2096 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2098 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2100 zsend_route_notify_owner(
2101 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
2102 info
->afi
, info
->safi
);
2104 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2105 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2106 dplane_ctx_get_table(ctx
), rn
);
2109 case DPLANE_OP_ROUTE_DELETE
:
2112 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2114 * In the delete case, the zebra core datastructs were
2115 * updated (or removed) at the time the delete was issued,
2116 * so we're just notifying the route owner.
2118 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2120 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2121 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2123 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2129 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2130 zsend_route_notify_owner_ctx(ctx
,
2131 ZAPI_ROUTE_REMOVE_FAIL
);
2133 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2134 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2135 dplane_ctx_get_table(ctx
), rn
);
2139 * System routes are weird in that they
2140 * allow multiple to be installed that match
2141 * to the same prefix, so after we get the
2142 * result we need to clean them up so that
2143 * we can actually use them.
2145 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2146 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2147 zebra_rib_fixup_system(rn
);
2153 zebra_rib_evaluate_rn_nexthops(rn
, seq
, rt_delete
);
2154 zebra_rib_evaluate_mpls(rn
);
2158 route_unlock_node(rn
);
2162 * Count installed/FIB nexthops
2164 static int rib_count_installed_nh(struct route_entry
*re
)
2167 struct nexthop
*nexthop
;
2168 struct nexthop_group
*nhg
;
2170 nhg
= rib_get_fib_nhg(re
);
2172 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2173 /* The meaningful flag depends on where the installed
2176 if (nhg
== &(re
->fib_ng
)) {
2177 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2180 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2185 nhg
= rib_get_fib_backup_nhg(re
);
2187 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2188 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2197 * Handle notification from async dataplane: the dataplane has detected
2198 * some change to a route, and notifies zebra so that the control plane
2199 * can reflect that change.
2201 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2203 struct route_node
*rn
= NULL
;
2204 struct route_entry
*re
= NULL
;
2206 struct nexthop
*nexthop
;
2208 bool fib_changed
= false;
2209 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2210 int start_count
, end_count
;
2212 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2214 /* Locate rn and re(s) from ctx */
2215 rn
= rib_find_rn_from_ctx(ctx
);
2219 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2220 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2221 dplane_ctx_get_table(ctx
), rn
);
2226 dest
= rib_dest_from_rnode(rn
);
2229 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2230 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2231 dplane_ctx_get_table(ctx
), rn
, ctx
);
2234 * Take a pass through the routes, look for matches with the context
2237 RNODE_FOREACH_RE(rn
, re
) {
2238 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2242 /* No match? Nothing we can do */
2246 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2247 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2248 dplane_ctx_get_table(ctx
), rn
,
2249 zebra_route_string(dplane_ctx_get_type(ctx
)));
2254 /* Ensure we clear the QUEUED flag */
2255 if (!zrouter
.asic_offloaded
)
2256 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2258 /* Is this a notification that ... matters? We mostly care about
2259 * the route that is currently selected for installation; we may also
2260 * get an un-install notification, and handle that too.
2262 if (re
!= dest
->selected_fib
) {
2264 * If we need to, clean up after a delete that was part of
2265 * an update operation.
2268 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2269 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2273 /* If no nexthops or none installed, ensure that this re
2274 * gets its 'installed' flag cleared.
2276 if (end_count
== 0) {
2277 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2278 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2281 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2283 dplane_ctx_get_vrf(ctx
),
2284 dplane_ctx_get_table(ctx
), rn
,
2286 dplane_ctx_get_type(ctx
)));
2288 /* At least report on the event. */
2291 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2293 dplane_ctx_get_vrf(ctx
),
2294 dplane_ctx_get_table(ctx
), rn
,
2296 dplane_ctx_get_type(ctx
)));
2301 /* We'll want to determine whether the installation status of the
2302 * route has changed: we'll check the status before processing,
2303 * and then again if there's been a change.
2307 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2308 start_count
= rib_count_installed_nh(re
);
2310 /* Update zebra's nexthop FIB flags based on the context struct's
2313 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2318 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2319 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2320 dplane_ctx_get_table(ctx
), rn
);
2324 * Perform follow-up work if the actual status of the prefix
2327 end_count
= rib_count_installed_nh(re
);
2329 /* Various fib transitions: changed nexthops; from installed to
2330 * not-installed; or not-installed to installed.
2332 if (start_count
> 0 && end_count
> 0) {
2335 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2336 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2337 dplane_ctx_get_table(ctx
), rn
);
2339 /* Changed nexthops - update kernel/others */
2340 dplane_route_notif_update(rn
, re
,
2341 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2343 } else if (start_count
== 0 && end_count
> 0) {
2346 "%s(%u:%u):%pRN installed transition from dplane notification",
2347 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2348 dplane_ctx_get_table(ctx
), rn
);
2350 /* We expect this to be the selected route, so we want
2351 * to tell others about this transition.
2353 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2355 /* Changed nexthops - update kernel/others */
2356 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2358 /* Redistribute, lsp, and nht update */
2359 redistribute_update(rn
, re
, NULL
);
2361 } else if (start_count
> 0 && end_count
== 0) {
2364 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2365 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2366 dplane_ctx_get_table(ctx
), rn
);
2368 /* Transition from _something_ installed to _nothing_
2371 /* We expect this to be the selected route, so we want
2372 * to tell others about this transistion.
2374 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2376 /* Changed nexthops - update kernel/others */
2377 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2379 /* Redistribute, lsp, and nht update */
2380 redistribute_delete(rn
, re
, NULL
);
2383 /* Make any changes visible for lsp and nexthop-tracking processing */
2384 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
2387 zebra_rib_evaluate_mpls(rn
);
2391 route_unlock_node(rn
);
2395 * Process a node from the EVPN/VXLAN subqueue.
2397 static void process_subq_evpn(struct listnode
*lnode
)
2399 struct wq_evpn_wrapper
*w
;
2401 /* In general, the list node points to a wrapper object
2402 * holding the info necessary to make some update.
2404 w
= listgetdata(lnode
);
2408 if (w
->type
== WQ_EVPN_WRAPPER_TYPE_VRFROUTE
) {
2410 zebra_vxlan_evpn_vrf_route_add(w
->vrf_id
, &w
->macaddr
,
2411 &w
->ip
, &w
->prefix
);
2413 zebra_vxlan_evpn_vrf_route_del(w
->vrf_id
, &w
->ip
,
2415 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_ES
) {
2417 zebra_evpn_remote_es_add(&w
->esi
, w
->ip
.ipaddr_v4
,
2418 w
->esr_rxed
, w
->df_alg
,
2421 zebra_evpn_remote_es_del(&w
->esi
, w
->ip
.ipaddr_v4
);
2422 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_MACIP
) {
2423 uint16_t ipa_len
= 0;
2425 if (w
->ip
.ipa_type
== IPADDR_V4
)
2426 ipa_len
= IPV4_MAX_BYTELEN
;
2427 else if (w
->ip
.ipa_type
== IPADDR_V6
)
2428 ipa_len
= IPV6_MAX_BYTELEN
;
2431 zebra_evpn_rem_macip_add(w
->vni
, &w
->macaddr
, ipa_len
,
2432 &w
->ip
, w
->flags
, w
->seq
,
2433 w
->vtep_ip
, &w
->esi
);
2435 zebra_evpn_rem_macip_del(w
->vni
, &w
->macaddr
, ipa_len
,
2436 &w
->ip
, w
->vtep_ip
);
2437 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_VTEP
) {
2439 zebra_vxlan_remote_vtep_add(w
->vrf_id
, w
->vni
,
2440 w
->vtep_ip
, w
->flags
);
2442 zebra_vxlan_remote_vtep_del(w
->vrf_id
, w
->vni
,
2447 XFREE(MTYPE_WQ_WRAPPER
, w
);
2451 * Process the nexthop-group workqueue subqueue
2453 static void process_subq_nhg(struct listnode
*lnode
)
2455 struct nhg_ctx
*ctx
;
2456 struct nhg_hash_entry
*nhe
, *newnhe
;
2457 struct wq_nhg_wrapper
*w
;
2458 uint8_t qindex
= META_QUEUE_NHG
;
2460 w
= listgetdata(lnode
);
2465 /* Two types of object - an update from the local kernel, or
2466 * an nhg update from a daemon.
2468 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2471 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2473 "NHG Context id=%u dequeued from sub-queue %s",
2474 ctx
->id
, subqueue2str(qindex
));
2477 /* Process nexthop group updates coming 'up' from the OS */
2478 nhg_ctx_process(ctx
);
2480 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2483 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2484 zlog_debug("NHG %u dequeued from sub-queue %s", nhe
->id
,
2485 subqueue2str(qindex
));
2487 /* Process incoming nhg update, probably from a proto daemon */
2488 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2490 nhe
->zapi_session
, &nhe
->nhg
, 0);
2492 /* Report error to daemon via ZAPI */
2494 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2495 nhe
->zapi_session
, nhe
->id
,
2496 ZAPI_NHG_FAIL_INSTALL
);
2498 /* Free temp nhe - we own that memory. */
2499 zebra_nhg_free(nhe
);
2502 XFREE(MTYPE_WQ_WRAPPER
, w
);
2505 static void process_subq_early_label(struct listnode
*lnode
)
2507 struct wq_label_wrapper
*w
= listgetdata(lnode
);
2508 struct zebra_vrf
*zvrf
;
2513 zvrf
= vrf_info_lookup(w
->vrf_id
);
2515 XFREE(MTYPE_WQ_WRAPPER
, w
);
2520 case WQ_LABEL_FTN_UNINSTALL
:
2521 zebra_mpls_ftn_uninstall(zvrf
, w
->ltype
, &w
->p
, w
->route_type
,
2524 case WQ_LABEL_LABELS_PROCESS
:
2525 zebra_mpls_zapi_labels_process(w
->add_p
, zvrf
, &w
->zl
);
2529 XFREE(MTYPE_WQ_WRAPPER
, w
);
2532 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2534 struct route_node
*rnode
= NULL
;
2535 rib_dest_t
*dest
= NULL
;
2536 struct zebra_vrf
*zvrf
= NULL
;
2538 rnode
= listgetdata(lnode
);
2539 dest
= rib_dest_from_rnode(rnode
);
2542 zvrf
= rib_dest_vrf(dest
);
2546 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2547 struct route_entry
*re
= NULL
;
2550 * rib_process may have freed the dest
2551 * as part of the garbage collection. Let's
2552 * prevent stupidity from happening.
2554 dest
= rib_dest_from_rnode(rnode
);
2556 re
= re_list_first(&dest
->routes
);
2558 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2559 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2560 rnode
, rnode
, subqueue2str(qindex
));
2564 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2565 RIB_ROUTE_QUEUED(qindex
));
2567 route_unlock_node(rnode
);
2570 static void rib_re_nhg_free(struct route_entry
*re
)
2572 if (re
->nhe
&& re
->nhe_id
) {
2573 assert(re
->nhe
->id
== re
->nhe_id
);
2574 route_entry_update_nhe(re
, NULL
);
2575 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2576 nexthops_free(re
->nhe
->nhg
.nexthop
);
2578 nexthops_free(re
->fib_ng
.nexthop
);
2581 struct zebra_early_route
{
2585 struct prefix_ipv6 src_p
;
2586 bool src_p_provided
;
2587 struct route_entry
*re
;
2588 struct nhg_hash_entry
*re_nhe
;
2594 static void early_route_memory_free(struct zebra_early_route
*ere
)
2597 zebra_nhg_free(ere
->re_nhe
);
2599 XFREE(MTYPE_RE
, ere
->re
);
2600 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2603 static void process_subq_early_route_add(struct zebra_early_route
*ere
)
2605 struct route_entry
*re
= ere
->re
;
2606 struct route_table
*table
;
2607 struct nhg_hash_entry
*nhe
= NULL
;
2608 struct route_node
*rn
;
2609 struct route_entry
*same
= NULL
, *first_same
= NULL
;
2614 table
= zebra_vrf_get_table_with_table_id(ere
->afi
, ere
->safi
,
2615 re
->vrf_id
, re
->table
);
2617 early_route_memory_free(ere
);
2621 if (re
->nhe_id
> 0) {
2622 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2626 * We've received from the kernel a nexthop id
2627 * that we don't have saved yet. More than likely
2628 * it has not been processed and is on the
2629 * queue to be processed. Let's stop what we
2630 * are doing and cause the meta q to be processed
2631 * storing this for later.
2633 * This is being done this way because zebra
2634 * runs with the assumption t
2637 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2638 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2639 re
->nhe_id
, &ere
->p
);
2641 early_route_memory_free(ere
);
2645 /* Lookup nhe from route information */
2646 nhe
= zebra_nhg_rib_find_nhe(ere
->re_nhe
, ere
->afi
);
2648 char buf2
[PREFIX_STRLEN
] = "";
2651 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2652 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2653 &ere
->p
, ere
->src_p_provided
? " from " : "",
2655 ? prefix2str(&ere
->src_p
, buf2
,
2659 early_route_memory_free(ere
);
2665 * Attach the re to the nhe's nexthop group.
2667 * TODO: This will need to change when we start getting IDs from upper
2668 * level protocols, as the refcnt might be wrong, since it checks
2669 * if old_id != new_id.
2671 route_entry_update_nhe(re
, nhe
);
2673 /* Make it sure prefixlen is applied to the prefix. */
2674 apply_mask(&ere
->p
);
2675 if (ere
->src_p_provided
)
2676 apply_mask_ipv6(&ere
->src_p
);
2678 /* Set default distance by route type. */
2679 if (re
->distance
== 0)
2680 re
->distance
= route_distance(re
->type
);
2682 /* Lookup route node.*/
2683 rn
= srcdest_rnode_get(table
, &ere
->p
,
2684 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2687 * If same type of route are installed, treat it as a implicit
2688 * withdraw. If the user has specified the No route replace semantics
2689 * for the install don't do a route replace.
2691 RNODE_FOREACH_RE (rn
, same
) {
2692 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
2697 /* Compare various route_entry properties */
2698 if (rib_compare_routes(re
, same
)) {
2701 if (first_same
== NULL
)
2708 if (!ere
->startup
&& (re
->flags
& ZEBRA_FLAG_SELFROUTE
) &&
2709 zrouter
.asic_offloaded
) {
2711 if (IS_ZEBRA_DEBUG_RIB
)
2713 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2716 * We are not on startup, this is a self route
2717 * and we have asic offload. Which means
2718 * we are getting a callback for a entry
2719 * that was already deleted to the kernel
2720 * but an earlier response was just handed
2721 * back. Drop it on the floor
2723 early_route_memory_free(ere
);
2728 /* If this route is kernel/connected route, notify the dataplane. */
2729 if (RIB_SYSTEM_ROUTE(re
)) {
2730 /* Notify dataplane */
2731 dplane_sys_route_add(rn
, re
);
2734 /* Link new re to node.*/
2735 if (IS_ZEBRA_DEBUG_RIB
) {
2738 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2739 rn
, re
, zebra_route_string(re
->type
), same
, same_count
);
2741 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2744 ere
->src_p_provided
? &ere
->src_p
: NULL
, re
);
2747 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2748 rib_addnode(rn
, re
, 1);
2750 /* Free implicit route.*/
2752 rib_delnode(rn
, same
);
2754 /* See if we can remove some RE entries that are queued for
2755 * removal, but won't be considered in rib processing.
2757 dest
= rib_dest_from_rnode(rn
);
2758 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
2759 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2760 /* If the route was used earlier, must retain it. */
2761 if (dest
&& re
== dest
->selected_fib
)
2764 if (IS_ZEBRA_DEBUG_RIB
)
2765 rnode_debug(rn
, re
->vrf_id
,
2766 "rn %p, removing unneeded re %p",
2773 route_unlock_node(rn
);
2775 zebra_nhg_free(ere
->re_nhe
);
2776 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2779 static void process_subq_early_route_delete(struct zebra_early_route
*ere
)
2781 struct route_table
*table
;
2782 struct route_node
*rn
;
2783 struct route_entry
*re
;
2784 struct route_entry
*fib
= NULL
;
2785 struct route_entry
*same
= NULL
;
2786 struct nexthop
*rtnh
;
2787 char buf2
[INET6_ADDRSTRLEN
];
2790 if (ere
->src_p_provided
)
2791 assert(!ere
->src_p
.prefixlen
|| ere
->afi
== AFI_IP6
);
2794 table
= zebra_vrf_lookup_table_with_table_id(
2795 ere
->afi
, ere
->safi
, ere
->re
->vrf_id
, ere
->re
->table
);
2797 early_route_memory_free(ere
);
2802 apply_mask(&ere
->p
);
2803 if (ere
->src_p_provided
)
2804 apply_mask_ipv6(&ere
->src_p
);
2806 /* Lookup route node. */
2807 rn
= srcdest_rnode_lookup(table
, &ere
->p
,
2808 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2810 if (IS_ZEBRA_DEBUG_RIB
) {
2811 char src_buf
[PREFIX_STRLEN
];
2812 struct vrf
*vrf
= vrf_lookup_by_id(ere
->re
->vrf_id
);
2814 if (ere
->src_p_provided
&& ere
->src_p
.prefixlen
)
2815 prefix2str(&ere
->src_p
, src_buf
,
2820 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2821 vrf
->name
, ere
->re
->table
, rn
,
2822 (src_buf
[0] != '\0') ? " from " : "",
2825 early_route_memory_free(ere
);
2829 dest
= rib_dest_from_rnode(rn
);
2830 fib
= dest
->selected_fib
;
2832 struct nexthop
*nh
= NULL
;
2835 nh
= ere
->re
->nhe
->nhg
.nexthop
;
2837 /* Lookup same type route. */
2838 RNODE_FOREACH_RE (rn
, re
) {
2839 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2842 if (re
->type
!= ere
->re
->type
)
2844 if (re
->instance
!= ere
->re
->instance
)
2846 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2847 ere
->re
->distance
!= re
->distance
)
2850 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2851 re
->metric
!= ere
->re
->metric
)
2853 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= nh
) &&
2854 rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2855 if (rtnh
->ifindex
!= nh
->ifindex
)
2861 /* Make sure that the route found has the same gateway. */
2862 if (ere
->re
->nhe_id
&& re
->nhe_id
== ere
->re
->nhe_id
) {
2871 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
2873 * No guarantee all kernel send nh with labels
2876 if (nexthop_same_no_labels(rtnh
, nh
)) {
2886 * If same type of route can't be found and this message is from
2891 * In the past(HA!) we could get here because
2892 * we were receiving a route delete from the
2893 * kernel and we're not marking the proto
2894 * as coming from it's appropriate originator.
2895 * Now that we are properly noticing the fact
2896 * that the kernel has deleted our route we
2897 * are not going to get called in this path
2898 * I am going to leave this here because
2899 * this might still work this way on non-linux
2900 * platforms as well as some weird state I have
2901 * not properly thought of yet.
2902 * If we can show that this code path is
2903 * dead then we can remove it.
2905 if (fib
&& CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2906 if (IS_ZEBRA_DEBUG_RIB
) {
2908 rn
, ere
->re
->vrf_id
,
2909 "rn %p, re %p (%s) was deleted from kernel, adding",
2910 rn
, fib
, zebra_route_string(fib
->type
));
2912 if (zrouter
.allow_delete
||
2913 CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
2914 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2916 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
2918 UNSET_FLAG(rtnh
->flags
,
2922 * This is a non FRR route
2923 * as such we should mark
2926 dest
->selected_fib
= NULL
;
2929 * This means someone else, other than Zebra,
2930 * has deleted a Zebra router from the kernel.
2931 * We will add it back
2933 rib_install_kernel(rn
, fib
, NULL
);
2936 if (IS_ZEBRA_DEBUG_RIB
) {
2939 rn
, ere
->re
->vrf_id
,
2940 "via %s ifindex %d type %d doesn't exist in rib",
2941 inet_ntop(afi2family(ere
->afi
),
2944 nh
->ifindex
, ere
->re
->type
);
2947 rn
, ere
->re
->vrf_id
,
2948 "type %d doesn't exist in rib",
2951 route_unlock_node(rn
);
2952 early_route_memory_free(ere
);
2958 struct nexthop
*tmp_nh
;
2960 if (ere
->fromkernel
&&
2961 CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
) &&
2962 !zrouter
.allow_delete
) {
2963 rib_install_kernel(rn
, same
, NULL
);
2964 route_unlock_node(rn
);
2966 early_route_memory_free(ere
);
2970 /* Special handling for IPv4 or IPv6 routes sourced from
2971 * EVPN - the nexthop (and associated MAC) need to be
2972 * uninstalled if no more refs.
2974 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
2975 struct ipaddr vtep_ip
;
2977 if (CHECK_FLAG(tmp_nh
->flags
, NEXTHOP_FLAG_EVPN
)) {
2978 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2979 if (ere
->afi
== AFI_IP
) {
2980 vtep_ip
.ipa_type
= IPADDR_V4
;
2981 memcpy(&(vtep_ip
.ipaddr_v4
),
2982 &(tmp_nh
->gate
.ipv4
),
2983 sizeof(struct in_addr
));
2985 vtep_ip
.ipa_type
= IPADDR_V6
;
2986 memcpy(&(vtep_ip
.ipaddr_v6
),
2987 &(tmp_nh
->gate
.ipv6
),
2988 sizeof(struct in6_addr
));
2990 zebra_rib_queue_evpn_route_del(
2991 re
->vrf_id
, &vtep_ip
, &ere
->p
);
2995 /* Notify dplane if system route changes */
2996 if (RIB_SYSTEM_ROUTE(re
))
2997 dplane_sys_route_del(rn
, same
);
2999 rib_delnode(rn
, same
);
3002 route_unlock_node(rn
);
3004 early_route_memory_free(ere
);
3008 * When FRR receives a route we need to match the route up to
3009 * nexthop groups. That we also may have just received
3010 * place the data on this queue so that this work of finding
3011 * the nexthop group entries for the route entry is always
3012 * done after the nexthop group has had a chance to be processed
3014 static void process_subq_early_route(struct listnode
*lnode
)
3016 struct zebra_early_route
*ere
= listgetdata(lnode
);
3019 process_subq_early_route_delete(ere
);
3021 process_subq_early_route_add(ere
);
3025 * Examine the specified subqueue; process one entry and return 1 if
3026 * there is a node, return 0 otherwise.
3028 static unsigned int process_subq(struct list
*subq
,
3029 enum meta_queue_indexes qindex
)
3031 struct listnode
*lnode
= listhead(subq
);
3037 case META_QUEUE_EVPN
:
3038 process_subq_evpn(lnode
);
3040 case META_QUEUE_NHG
:
3041 process_subq_nhg(lnode
);
3043 case META_QUEUE_EARLY_ROUTE
:
3044 process_subq_early_route(lnode
);
3046 case META_QUEUE_EARLY_LABEL
:
3047 process_subq_early_label(lnode
);
3049 case META_QUEUE_CONNECTED
:
3050 case META_QUEUE_KERNEL
:
3051 case META_QUEUE_STATIC
:
3052 case META_QUEUE_NOTBGP
:
3053 case META_QUEUE_BGP
:
3054 case META_QUEUE_OTHER
:
3055 process_subq_route(lnode
, qindex
);
3059 list_delete_node(subq
, lnode
);
3064 /* Dispatch the meta queue by picking and processing the next node from
3065 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3066 * data is pointed to the meta queue structure.
3068 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
3070 struct meta_queue
*mq
= data
;
3072 uint32_t queue_len
, queue_limit
;
3074 /* Ensure there's room for more dataplane updates */
3075 queue_limit
= dplane_get_in_queue_limit();
3076 queue_len
= dplane_get_in_queue_len();
3077 if (queue_len
> queue_limit
) {
3078 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3080 "rib queue: dplane queue len %u, limit %u, retrying",
3081 queue_len
, queue_limit
);
3083 /* Ensure that the meta-queue is actually enqueued */
3084 if (work_queue_empty(zrouter
.ribq
))
3085 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3087 return WQ_QUEUE_BLOCKED
;
3090 for (i
= 0; i
< MQ_SIZE
; i
++)
3091 if (process_subq(mq
->subq
[i
], i
)) {
3095 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
3100 * Look into the RN and queue it into the highest priority queue
3101 * at this point in time for processing.
3103 * We will enqueue a route node only once per invocation.
3105 * There are two possibilities here that should be kept in mind.
3106 * If the original invocation has not been pulled off for processing
3107 * yet, A subsuquent invocation can have a route entry with a better
3108 * meta queue index value and we can have a situation where
3109 * we might have the same node enqueued 2 times. Not necessarily
3110 * an optimal situation but it should be ok.
3112 * The other possibility is that the original invocation has not
3113 * been pulled off for processing yet, A subsusquent invocation
3114 * doesn't have a route_entry with a better meta-queue and the
3115 * original metaqueue index value will win and we'll end up with
3116 * the route node enqueued once.
3118 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
3120 struct route_node
*rn
= NULL
;
3121 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
3122 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
3124 rn
= (struct route_node
*)data
;
3126 RNODE_FOREACH_RE (rn
, curr_re
) {
3127 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
3129 if (curr_qindex
<= qindex
) {
3131 qindex
= curr_qindex
;
3138 /* Invariant: at this point we always have rn->info set. */
3139 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3140 RIB_ROUTE_QUEUED(qindex
))) {
3141 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3142 rnode_debug(rn
, re
->vrf_id
,
3143 "rn %p is already queued in sub-queue %s",
3144 (void *)rn
, subqueue2str(qindex
));
3148 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
3149 listnode_add(mq
->subq
[qindex
], rn
);
3150 route_lock_node(rn
);
3153 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3154 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %s",
3155 (void *)rn
, subqueue2str(qindex
));
3160 static int early_label_meta_queue_add(struct meta_queue
*mq
, void *data
)
3162 listnode_add(mq
->subq
[META_QUEUE_EARLY_LABEL
], data
);
3167 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
3169 struct nhg_ctx
*ctx
= NULL
;
3170 uint8_t qindex
= META_QUEUE_NHG
;
3171 struct wq_nhg_wrapper
*w
;
3173 ctx
= (struct nhg_ctx
*)data
;
3178 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3180 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
3183 listnode_add(mq
->subq
[qindex
], w
);
3186 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3187 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3188 ctx
->id
, subqueue2str(qindex
));
3193 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
3195 struct nhg_hash_entry
*nhe
= NULL
;
3196 uint8_t qindex
= META_QUEUE_NHG
;
3197 struct wq_nhg_wrapper
*w
;
3199 nhe
= (struct nhg_hash_entry
*)data
;
3204 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3206 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
3209 listnode_add(mq
->subq
[qindex
], w
);
3212 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3213 zlog_debug("NHG id=%u queued into sub-queue %s", nhe
->id
,
3214 subqueue2str(qindex
));
3219 static int rib_meta_queue_evpn_add(struct meta_queue
*mq
, void *data
)
3221 listnode_add(mq
->subq
[META_QUEUE_EVPN
], data
);
3227 static int mq_add_handler(void *data
,
3228 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
3230 if (zrouter
.ribq
== NULL
) {
3231 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3232 "%s: work_queue does not exist!", __func__
);
3237 * The RIB queue should normally be either empty or holding the only
3238 * work_queue_item element. In the latter case this element would
3239 * hold a pointer to the meta queue structure, which must be used to
3240 * actually queue the route nodes to process. So create the MQ
3241 * holder, if necessary, then push the work into it in any case.
3242 * This semantics was introduced after 0.99.9 release.
3244 if (work_queue_empty(zrouter
.ribq
))
3245 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3247 return mq_add_func(zrouter
.mq
, data
);
3250 void mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3251 struct prefix
*prefix
, uint8_t route_type
,
3252 uint8_t route_instance
)
3254 struct wq_label_wrapper
*w
;
3256 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3258 w
->type
= WQ_LABEL_FTN_UNINSTALL
;
3259 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3262 w
->route_type
= route_type
;
3263 w
->route_instance
= route_instance
;
3265 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3266 zlog_debug("Early Label Handling for %pFX", prefix
);
3268 mq_add_handler(w
, early_label_meta_queue_add
);
3271 void mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
3272 const struct zapi_labels
*zl
)
3274 struct wq_label_wrapper
*w
;
3276 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3277 w
->type
= WQ_LABEL_LABELS_PROCESS
;
3278 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3282 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3283 zlog_debug("Early Label Handling: Labels Process");
3285 mq_add_handler(w
, early_label_meta_queue_add
);
3288 /* Add route_node to work queue and schedule processing */
3289 int rib_queue_add(struct route_node
*rn
)
3293 /* Pointless to queue a route_node with no RIB entries to add or remove
3295 if (!rnode_to_ribs(rn
)) {
3296 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3297 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
3298 zlog_backtrace(LOG_DEBUG
);
3302 return mq_add_handler(rn
, rib_meta_queue_add
);
3306 * Enqueue incoming nhg info from OS for processing
3308 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
3312 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
3316 * Enqueue incoming nhg from proto daemon for processing
3318 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
3323 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
3327 * Enqueue evpn route for processing
3329 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id
, const struct ethaddr
*rmac
,
3330 const struct ipaddr
*vtep_ip
,
3331 const struct prefix
*host_prefix
)
3333 struct wq_evpn_wrapper
*w
;
3335 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3337 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3342 w
->prefix
= *host_prefix
;
3344 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3345 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3346 vrf_id
, vtep_ip
, host_prefix
);
3348 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3351 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id
,
3352 const struct ipaddr
*vtep_ip
,
3353 const struct prefix
*host_prefix
)
3355 struct wq_evpn_wrapper
*w
;
3357 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3359 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3363 w
->prefix
= *host_prefix
;
3365 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3366 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3367 vrf_id
, vtep_ip
, host_prefix
);
3369 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3372 /* Enqueue EVPN remote ES for processing */
3373 int zebra_rib_queue_evpn_rem_es_add(const esi_t
*esi
,
3374 const struct in_addr
*vtep_ip
,
3375 bool esr_rxed
, uint8_t df_alg
,
3378 struct wq_evpn_wrapper
*w
;
3379 char buf
[ESI_STR_LEN
];
3381 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3383 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3386 w
->ip
.ipa_type
= IPADDR_V4
;
3387 w
->ip
.ipaddr_v4
= *vtep_ip
;
3388 w
->esr_rxed
= esr_rxed
;
3390 w
->df_pref
= df_pref
;
3392 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3393 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3394 esi_to_str(esi
, buf
, sizeof(buf
)));
3396 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3399 int zebra_rib_queue_evpn_rem_es_del(const esi_t
*esi
,
3400 const struct in_addr
*vtep_ip
)
3402 struct wq_evpn_wrapper
*w
;
3403 char buf
[ESI_STR_LEN
];
3405 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3407 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3410 w
->ip
.ipa_type
= IPADDR_V4
;
3411 w
->ip
.ipaddr_v4
= *vtep_ip
;
3413 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3414 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3415 esi_to_str(esi
, buf
, sizeof(buf
));
3417 strlcpy(buf
, "-", sizeof(buf
));
3419 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3423 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3427 * Enqueue EVPN remote macip update for processing
3429 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni
, const struct ethaddr
*macaddr
,
3430 const struct ipaddr
*ipaddr
,
3431 uint8_t flags
, uint32_t seq
,
3432 struct in_addr vtep_ip
, const esi_t
*esi
)
3434 struct wq_evpn_wrapper
*w
;
3435 char buf
[ESI_STR_LEN
];
3437 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3439 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3442 w
->macaddr
= *macaddr
;
3446 w
->vtep_ip
= vtep_ip
;
3449 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3450 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3451 esi_to_str(esi
, buf
, sizeof(buf
));
3453 strlcpy(buf
, "-", sizeof(buf
));
3455 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__
,
3456 macaddr
, &vtep_ip
, buf
);
3459 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3462 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni
, const struct ethaddr
*macaddr
,
3463 const struct ipaddr
*ip
,
3464 struct in_addr vtep_ip
)
3466 struct wq_evpn_wrapper
*w
;
3468 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3470 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3473 w
->macaddr
= *macaddr
;
3475 w
->vtep_ip
= vtep_ip
;
3477 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3478 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__
,
3481 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3485 * Enqueue remote VTEP address for processing
3487 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id
, vni_t vni
,
3488 struct in_addr vtep_ip
, int flood_control
)
3490 struct wq_evpn_wrapper
*w
;
3492 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3494 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3498 w
->vtep_ip
= vtep_ip
;
3499 w
->flags
= flood_control
;
3501 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3502 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3505 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3508 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id
, vni_t vni
,
3509 struct in_addr vtep_ip
)
3511 struct wq_evpn_wrapper
*w
;
3513 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3515 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3519 w
->vtep_ip
= vtep_ip
;
3521 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3522 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3525 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3528 /* Create new meta queue.
3529 A destructor function doesn't seem to be necessary here.
3531 static struct meta_queue
*meta_queue_new(void)
3533 struct meta_queue
*new;
3536 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
3538 for (i
= 0; i
< MQ_SIZE
; i
++) {
3539 new->subq
[i
] = list_new();
3540 assert(new->subq
[i
]);
3546 /* Clean up the EVPN meta-queue list */
3547 static void evpn_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3548 struct zebra_vrf
*zvrf
)
3550 struct listnode
*node
, *nnode
;
3551 struct wq_evpn_wrapper
*w
;
3553 /* Free the node wrapper object, and the struct it wraps */
3554 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3556 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3558 if (w
->vrf_id
!= vrf_id
)
3564 XFREE(MTYPE_WQ_WRAPPER
, w
);
3566 list_delete_node(l
, node
);
3571 /* Clean up the nhg meta-queue list */
3572 static void nhg_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3573 struct zebra_vrf
*zvrf
)
3575 struct wq_nhg_wrapper
*w
;
3576 struct listnode
*node
, *nnode
;
3578 /* Free the node wrapper object, and the struct it wraps */
3579 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3581 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3583 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
&&
3584 w
->u
.ctx
->vrf_id
!= vrf_id
)
3586 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
&&
3587 w
->u
.nhe
->vrf_id
!= vrf_id
)
3590 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
3591 nhg_ctx_free(&w
->u
.ctx
);
3592 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
3593 zebra_nhg_free(w
->u
.nhe
);
3596 XFREE(MTYPE_WQ_WRAPPER
, w
);
3598 list_delete_node(l
, node
);
3603 static void early_label_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3604 struct zebra_vrf
*zvrf
)
3606 struct wq_label_wrapper
*w
;
3607 struct listnode
*node
, *nnode
;
3609 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3610 if (zvrf
&& zvrf
->vrf
->vrf_id
!= w
->vrf_id
)
3614 case WQ_LABEL_FTN_UNINSTALL
:
3615 case WQ_LABEL_LABELS_PROCESS
:
3620 XFREE(MTYPE_WQ_WRAPPER
, w
);
3621 list_delete_node(l
, node
);
3626 static void rib_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3627 struct zebra_vrf
*zvrf
)
3629 struct route_node
*rnode
;
3630 struct listnode
*node
, *nnode
;
3632 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, rnode
)) {
3633 rib_dest_t
*dest
= rib_dest_from_rnode(rnode
);
3635 if (dest
&& rib_dest_vrf(dest
) != zvrf
)
3638 route_unlock_node(rnode
);
3640 list_delete_node(l
, node
);
3645 static void early_route_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3646 struct zebra_vrf
*zvrf
)
3648 struct zebra_early_route
*zer
;
3649 struct listnode
*node
, *nnode
;
3651 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, zer
)) {
3652 if (zvrf
&& zer
->re
->vrf_id
!= zvrf
->vrf
->vrf_id
)
3655 XFREE(MTYPE_RE
, zer
);
3657 list_delete_node(l
, node
);
3662 void meta_queue_free(struct meta_queue
*mq
, struct zebra_vrf
*zvrf
)
3664 enum meta_queue_indexes i
;
3666 for (i
= 0; i
< MQ_SIZE
; i
++) {
3667 /* Some subqueues may need cleanup - nhgs for example */
3669 case META_QUEUE_NHG
:
3670 nhg_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3672 case META_QUEUE_EVPN
:
3673 evpn_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3675 case META_QUEUE_EARLY_ROUTE
:
3676 early_route_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3678 case META_QUEUE_EARLY_LABEL
:
3679 early_label_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3681 case META_QUEUE_CONNECTED
:
3682 case META_QUEUE_KERNEL
:
3683 case META_QUEUE_STATIC
:
3684 case META_QUEUE_NOTBGP
:
3685 case META_QUEUE_BGP
:
3686 case META_QUEUE_OTHER
:
3687 rib_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3691 list_delete(&mq
->subq
[i
]);
3695 XFREE(MTYPE_WORK_QUEUE
, mq
);
3698 /* initialise zebra rib work queue */
3699 static void rib_queue_init(void)
3701 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
3702 "route_node processing"))) {
3703 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3704 "%s: could not initialise work queue!", __func__
);
3708 /* fill in the work queue spec */
3709 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
3710 zrouter
.ribq
->spec
.errorfunc
= NULL
;
3711 zrouter
.ribq
->spec
.completion_func
= NULL
;
3712 /* XXX: TODO: These should be runtime configurable via vty */
3713 zrouter
.ribq
->spec
.max_retries
= 3;
3714 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
3715 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
3717 if (!(zrouter
.mq
= meta_queue_new())) {
3718 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3719 "%s: could not initialise meta queue!", __func__
);
3725 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
3729 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
3730 rnh_list_init(&dest
->nht
);
3731 re_list_init(&dest
->routes
);
3732 route_lock_node(rn
); /* rn route table reference */
3739 /* RIB updates are processed via a queue of pointers to route_nodes.
3741 * The queue length is bounded by the maximal size of the routing table,
3742 * as a route_node will not be requeued, if already queued.
3744 * REs are submitted via rib_addnode or rib_delnode which set minimal
3745 * state, or static_install_route (when an existing RE is updated)
3746 * and then submit route_node to queue for best-path selection later.
3747 * Order of add/delete state changes are preserved for any given RE.
3749 * Deleted REs are reaped during best-path selection.
3752 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3753 * |-------->| | best RE, if required
3755 * static_install->|->rib_addqueue...... -> rib_process
3757 * |-------->| |-> rib_unlink
3758 * |-> set ROUTE_ENTRY_REMOVE |
3759 * rib_delnode (RE freed)
3761 * The 'info' pointer of a route_node points to a rib_dest_t
3762 * ('dest'). Queueing state for a route_node is kept on the dest. The
3763 * dest is created on-demand by rib_link() and is kept around at least
3764 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3766 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3768 * - route_nodes: refcounted by:
3769 * - dest attached to route_node:
3770 * - managed by: rib_link/rib_gc_dest
3771 * - route_node processing queue
3772 * - managed by: rib_addqueue, rib_process.
3776 /* Add RE to head of the route node. */
3777 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
3781 const char *rmap_name
;
3785 dest
= rib_dest_from_rnode(rn
);
3787 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3788 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
3790 dest
= zebra_rib_create_dest(rn
);
3793 re_list_add_head(&dest
->routes
, re
);
3795 afi
= (rn
->p
.family
== AF_INET
)
3797 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3798 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3799 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3801 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
3802 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
3807 static void rib_addnode(struct route_node
*rn
,
3808 struct route_entry
*re
, int process
)
3810 /* RE node has been un-removed before route-node is processed.
3811 * route_node must hence already be on the queue for processing..
3813 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3814 if (IS_ZEBRA_DEBUG_RIB
)
3815 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
3816 (void *)rn
, (void *)re
);
3818 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3821 rib_link(rn
, re
, process
);
3827 * Detach a rib structure from a route_node.
3829 * Note that a call to rib_unlink() should be followed by a call to
3830 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3831 * longer required to be deleted.
3833 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
3839 if (IS_ZEBRA_DEBUG_RIB
)
3840 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
3843 dest
= rib_dest_from_rnode(rn
);
3845 re_list_del(&dest
->routes
, re
);
3847 if (dest
->selected_fib
== re
)
3848 dest
->selected_fib
= NULL
;
3850 rib_re_nhg_free(re
);
3852 zapi_re_opaque_free(re
->opaque
);
3854 XFREE(MTYPE_RE
, re
);
3857 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
3861 if (IS_ZEBRA_DEBUG_RIB
)
3862 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
3863 (void *)rn
, (void *)re
);
3864 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3866 afi
= (rn
->p
.family
== AF_INET
)
3868 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3869 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3870 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3872 zebra_del_import_table_entry(zvrf
, rn
, re
);
3873 /* Just clean up if non main table */
3874 if (IS_ZEBRA_DEBUG_RIB
)
3875 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3876 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, rn
,
3877 rn
, re
, zebra_route_string(re
->type
));
3886 * Helper that debugs a single nexthop within a route-entry
3888 static void _route_entry_dump_nh(const struct route_entry
*re
,
3889 const char *straddr
,
3890 const struct nexthop
*nexthop
)
3892 char nhname
[PREFIX_STRLEN
];
3893 char backup_str
[50];
3896 char label_str
[MPLS_LABEL_STRLEN
];
3898 struct interface
*ifp
;
3899 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
3901 switch (nexthop
->type
) {
3902 case NEXTHOP_TYPE_BLACKHOLE
:
3903 snprintf(nhname
, sizeof(nhname
), "Blackhole");
3905 case NEXTHOP_TYPE_IFINDEX
:
3906 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
3907 snprintf(nhname
, sizeof(nhname
), "%s",
3908 ifp
? ifp
->name
: "Unknown");
3910 case NEXTHOP_TYPE_IPV4
:
3912 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3913 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3915 case NEXTHOP_TYPE_IPV6
:
3916 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3917 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3922 label_str
[0] = '\0';
3923 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
3924 mpls_label2str(nexthop
->nh_label
->num_labels
,
3925 nexthop
->nh_label
->label
, label_str
,
3926 sizeof(label_str
), 0 /*pretty*/);
3927 strlcat(label_str
, ", ", sizeof(label_str
));
3930 backup_str
[0] = '\0';
3931 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3932 snprintf(backup_str
, sizeof(backup_str
), "backup ");
3933 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
3934 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
3935 nexthop
->backup_idx
[i
]);
3936 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
3941 if (nexthop
->weight
)
3942 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
3944 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
3945 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
3946 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
3948 wgt_str
, backup_str
,
3949 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
3952 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
3955 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
3958 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
3961 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
3964 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
3965 ? "FILTERED " : ""),
3966 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
3968 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
3970 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)
3975 /* This function dumps the contents of a given RE entry into
3976 * standard debug log. Calling function name and IP prefix in
3977 * question are passed as 1st and 2nd arguments.
3979 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
3980 union prefixconstptr src_pp
,
3981 const struct route_entry
*re
)
3983 const struct prefix
*src_p
= src_pp
.p
;
3984 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
3985 char straddr
[PREFIX_STRLEN
];
3986 char srcaddr
[PREFIX_STRLEN
];
3987 char flags_buf
[128];
3988 char status_buf
[128];
3989 struct nexthop
*nexthop
;
3990 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
3991 struct nexthop_group
*nhg
;
3993 prefix2str(pp
, straddr
, sizeof(straddr
));
3995 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
3996 (const void *)re
, straddr
,
3997 is_srcdst
? " from " : "",
3998 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
4000 VRF_LOGNAME(vrf
), re
->vrf_id
);
4001 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4002 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
4005 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4006 straddr
, re
->metric
, re
->mtu
, re
->distance
,
4007 zclient_dump_route_flags(re
->flags
, flags_buf
,
4009 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
4010 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
4011 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
4012 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
4015 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4016 _route_entry_dump_nh(re
, straddr
, nexthop
);
4018 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
4019 zlog_debug("%s: backup nexthops:", straddr
);
4021 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
4022 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
4023 _route_entry_dump_nh(re
, straddr
, nexthop
);
4026 zlog_debug("%s: dump complete", straddr
);
4029 static int rib_meta_queue_early_route_add(struct meta_queue
*mq
, void *data
)
4031 struct zebra_early_route
*ere
= data
;
4033 listnode_add(mq
->subq
[META_QUEUE_EARLY_ROUTE
], data
);
4036 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
4038 "Route %pFX(%u) queued for processing into sub-queue %s",
4039 &ere
->p
, ere
->re
->vrf_id
,
4040 subqueue2str(META_QUEUE_EARLY_ROUTE
));
4045 struct route_entry
*zebra_rib_route_entry_new(vrf_id_t vrf_id
, int type
,
4046 uint8_t instance
, uint32_t flags
,
4049 uint32_t metric
, uint32_t mtu
,
4050 uint8_t distance
, route_tag_t tag
)
4052 struct route_entry
*re
;
4054 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
4056 re
->instance
= instance
;
4057 re
->distance
= distance
;
4059 re
->metric
= metric
;
4061 re
->table
= table_id
;
4062 re
->vrf_id
= vrf_id
;
4063 re
->uptime
= monotime(NULL
);
4065 re
->nhe_id
= nhe_id
;
4070 * Internal route-add implementation; there are a couple of different public
4071 * signatures. Callers in this path are responsible for the memory they
4072 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4073 * must free those objects. If this returns < 0, an error has occurred and the
4074 * route_entry 're' has not been captured; the caller should free that also.
4080 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
4081 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4082 struct nhg_hash_entry
*re_nhe
, bool startup
)
4084 struct zebra_early_route
*ere
;
4089 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
4091 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4096 ere
->src_p
= *src_p
;
4097 ere
->src_p_provided
= !!src_p
;
4099 ere
->re_nhe
= re_nhe
;
4100 ere
->startup
= startup
;
4102 return mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4106 * Add a single route.
4108 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
4109 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4110 struct nexthop_group
*ng
, bool startup
)
4113 struct nhg_hash_entry nhe
, *n
;
4118 /* We either need nexthop(s) or an existing nexthop id */
4119 if (ng
== NULL
&& re
->nhe_id
== 0)
4123 * Use a temporary nhe to convey info to the common/main api.
4125 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
4127 nhe
.nhg
.nexthop
= ng
->nexthop
;
4128 else if (re
->nhe_id
> 0)
4129 nhe
.id
= re
->nhe_id
;
4131 n
= zebra_nhe_copy(&nhe
, 0);
4132 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, n
, startup
);
4134 /* In error cases, free the route also */
4136 XFREE(MTYPE_RE
, re
);
4141 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4142 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4143 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4144 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
4145 uint8_t distance
, bool fromkernel
)
4147 struct zebra_early_route
*ere
;
4148 struct route_entry
*re
= NULL
;
4149 struct nhg_hash_entry
*nhe
= NULL
;
4151 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4152 table_id
, metric
, 0, distance
, 0);
4155 nhe
= zebra_nhg_alloc();
4156 nhe
->nhg
.nexthop
= nexthop_dup(nh
, NULL
);
4159 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4164 ere
->src_p
= *src_p
;
4165 ere
->src_p_provided
= !!src_p
;
4168 ere
->startup
= false;
4169 ere
->deletion
= true;
4170 ere
->fromkernel
= fromkernel
;
4172 mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4176 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4177 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4178 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4179 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
4180 uint8_t distance
, route_tag_t tag
, bool startup
)
4182 struct route_entry
*re
= NULL
;
4183 struct nexthop nexthop
= {};
4184 struct nexthop_group ng
= {};
4186 /* Allocate new route_entry structure. */
4187 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4188 table_id
, metric
, mtu
, distance
, tag
);
4190 /* If the owner of the route supplies a shared nexthop-group id,
4191 * we'll use that. Otherwise, pass the nexthop along directly.
4196 nexthop_group_add_sorted(&ng
, &nexthop
);
4199 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, &ng
, startup
);
4202 static const char *rib_update_event2str(enum rib_update_event event
)
4204 const char *ret
= "UNKNOWN";
4207 case RIB_UPDATE_KERNEL
:
4208 ret
= "RIB_UPDATE_KERNEL";
4210 case RIB_UPDATE_RMAP_CHANGE
:
4211 ret
= "RIB_UPDATE_RMAP_CHANGE";
4213 case RIB_UPDATE_OTHER
:
4214 ret
= "RIB_UPDATE_OTHER";
4216 case RIB_UPDATE_MAX
:
4224 /* Schedule route nodes to be processed if they match the type */
4225 static void rib_update_route_node(struct route_node
*rn
, int type
)
4227 struct route_entry
*re
, *next
;
4228 bool re_changed
= false;
4230 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4231 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
4232 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
4241 /* Schedule routes of a particular table (address-family) based on event. */
4242 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
4245 struct route_node
*rn
;
4247 if (IS_ZEBRA_DEBUG_EVENT
) {
4248 struct zebra_vrf
*zvrf
;
4252 ? ((struct rib_table_info
*)table
->info
)->zvrf
4254 vrf
= zvrf
? zvrf
->vrf
: NULL
;
4256 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
4257 table
->info
? afi2str(
4258 ((struct rib_table_info
*)table
->info
)->afi
)
4260 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
4261 rib_update_event2str(event
), zebra_route_string(rtype
));
4264 /* Walk all routes and queue for processing, if appropriate for
4265 * the trigger event.
4267 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4269 * If we are looking at a route node and the node
4270 * has already been queued we don't
4271 * need to queue it up again
4274 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
4275 RIB_ROUTE_ANY_QUEUED
))
4279 case RIB_UPDATE_KERNEL
:
4280 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
4282 case RIB_UPDATE_RMAP_CHANGE
:
4283 case RIB_UPDATE_OTHER
:
4284 rib_update_route_node(rn
, rtype
);
4292 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
,
4295 struct route_table
*table
;
4297 if (IS_ZEBRA_DEBUG_EVENT
)
4298 zlog_debug("%s: Handling VRF %s event %s", __func__
,
4299 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
4301 /* Process routes of interested address-families. */
4302 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
4304 rib_update_table(table
, event
, rtype
);
4306 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
4308 rib_update_table(table
, event
, rtype
);
4311 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
4313 struct zebra_router_table
*zrt
;
4315 if (IS_ZEBRA_DEBUG_EVENT
)
4316 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
4317 rib_update_event2str(event
));
4319 /* Just iterate over all the route tables, rather than vrf lookups */
4320 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
4321 rib_update_table(zrt
->table
, event
, rtype
);
4324 struct rib_update_ctx
{
4325 enum rib_update_event event
;
4330 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
4331 enum rib_update_event event
)
4333 struct rib_update_ctx
*ctx
;
4335 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
4338 ctx
->vrf_id
= vrf_id
;
4343 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
4345 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
4348 static void rib_update_handler(struct thread
*thread
)
4350 struct rib_update_ctx
*ctx
;
4352 ctx
= THREAD_ARG(thread
);
4355 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
4357 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
, ZEBRA_ROUTE_ALL
);
4359 rib_update_ctx_fini(&ctx
);
4363 * Thread list to ensure we don't schedule a ton of events
4364 * if interfaces are flapping for instance.
4366 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
4368 /* Schedule a RIB update event for all vrfs */
4369 void rib_update(enum rib_update_event event
)
4371 struct rib_update_ctx
*ctx
;
4373 if (thread_is_scheduled(t_rib_update_threads
[event
]))
4376 ctx
= rib_update_ctx_init(0, event
);
4377 ctx
->vrf_all
= true;
4379 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
4380 &t_rib_update_threads
[event
]);
4382 if (IS_ZEBRA_DEBUG_EVENT
)
4383 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
4384 rib_update_event2str(event
));
4387 /* Delete self installed routes after zebra is relaunched. */
4388 void rib_sweep_table(struct route_table
*table
)
4390 struct route_node
*rn
;
4391 struct route_entry
*re
;
4392 struct route_entry
*next
;
4393 struct nexthop
*nexthop
;
4398 if (IS_ZEBRA_DEBUG_RIB
)
4399 zlog_debug("%s: starting", __func__
);
4401 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4402 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4404 if (IS_ZEBRA_DEBUG_RIB
)
4405 route_entry_dump(&rn
->p
, NULL
, re
);
4407 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4410 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
4414 * If routes are older than startup_time then
4415 * we know we read them in from the kernel.
4416 * As such we can safely remove them.
4418 if (zrouter
.startup_time
< re
->uptime
)
4422 * So we are starting up and have received
4423 * routes from the kernel that we have installed
4424 * from a previous run of zebra but not cleaned
4425 * up ( say a kill -9 )
4426 * But since we haven't actually installed
4427 * them yet( we received them from the kernel )
4428 * we don't think they are active.
4429 * So let's pretend they are active to actually
4431 * In all honesty I'm not sure if we should
4432 * mark them as active when we receive them
4433 * This is startup only so probably ok.
4435 * If we ever decide to move rib_sweep_table
4436 * to a different spot (ie startup )
4437 * this decision needs to be revisited
4439 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
4440 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4441 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
4443 rib_uninstall_kernel(rn
, re
);
4444 rib_delnode(rn
, re
);
4448 if (IS_ZEBRA_DEBUG_RIB
)
4449 zlog_debug("%s: ends", __func__
);
4452 /* Sweep all RIB tables. */
4453 void rib_sweep_route(struct thread
*t
)
4456 struct zebra_vrf
*zvrf
;
4458 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4459 if ((zvrf
= vrf
->info
) == NULL
)
4462 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4463 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4466 zebra_router_sweep_route();
4467 zebra_router_sweep_nhgs();
4470 /* Remove specific by protocol routes from 'table'. */
4471 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
4472 struct route_table
*table
)
4474 struct route_node
*rn
;
4475 struct route_entry
*re
;
4476 struct route_entry
*next
;
4477 unsigned long n
= 0;
4480 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
4481 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4482 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4484 if (re
->type
== proto
4485 && re
->instance
== instance
) {
4486 rib_delnode(rn
, re
);
4493 /* Remove specific by protocol routes. */
4494 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
4497 struct zebra_vrf
*zvrf
;
4498 struct other_route_table
*ort
;
4499 unsigned long cnt
= 0;
4501 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4506 cnt
+= rib_score_proto_table(proto
, instance
,
4507 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4508 + rib_score_proto_table(
4510 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4512 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
4513 rib_score_proto_table(proto
, instance
, ort
->table
);
4519 /* Close RIB and clean up kernel routes. */
4520 void rib_close_table(struct route_table
*table
)
4522 struct route_node
*rn
;
4528 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4529 dest
= rib_dest_from_rnode(rn
);
4531 if (dest
&& dest
->selected_fib
) {
4532 rib_uninstall_kernel(rn
, dest
->selected_fib
);
4533 dest
->selected_fib
= NULL
;
4539 * Handler for async dataplane results after a pseudowire installation
4541 static void handle_pw_result(struct zebra_dplane_ctx
*ctx
)
4543 struct zebra_pw
*pw
;
4544 struct zebra_vrf
*vrf
;
4546 /* The pseudowire code assumes success - we act on an error
4547 * result for installation attempts here.
4549 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
4552 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
4553 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
4554 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
4556 zebra_pw_install_failure(pw
,
4557 dplane_ctx_get_pw_status(ctx
));
4562 * Handle results from the dataplane system. Dequeue update context
4563 * structs, dispatch to appropriate internal handlers.
4565 static void rib_process_dplane_results(struct thread
*thread
)
4567 struct zebra_dplane_ctx
*ctx
;
4568 struct dplane_ctx_q ctxlist
;
4569 bool shut_p
= false;
4571 /* Dequeue a list of completed updates with one lock/unlock cycle */
4574 TAILQ_INIT(&ctxlist
);
4576 /* Take lock controlling queue of results */
4577 frr_with_mutex (&dplane_mutex
) {
4578 /* Dequeue list of context structs */
4579 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
4582 /* Dequeue context block */
4583 ctx
= dplane_ctx_dequeue(&ctxlist
);
4585 /* If we've emptied the results queue, we're done */
4589 /* If zebra is shutting down, avoid processing results,
4590 * just drain the results queue.
4592 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
4593 memory_order_relaxed
);
4596 dplane_ctx_fini(&ctx
);
4598 ctx
= dplane_ctx_dequeue(&ctxlist
);
4604 #ifdef HAVE_SCRIPTING
4605 char *script_name
= frrscript_names_get_script_name(
4606 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
4609 struct frrscript
*fs
;
4612 fs
= frrscript_new(script_name
);
4614 ret
= frrscript_load(
4615 fs
, ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4618 #endif /* HAVE_SCRIPTING */
4622 #ifdef HAVE_SCRIPTING
4625 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4627 #endif /* HAVE_SCRIPTING */
4629 switch (dplane_ctx_get_op(ctx
)) {
4630 case DPLANE_OP_ROUTE_INSTALL
:
4631 case DPLANE_OP_ROUTE_UPDATE
:
4632 case DPLANE_OP_ROUTE_DELETE
:
4633 /* Bit of special case for route updates
4634 * that were generated by async notifications:
4635 * we don't want to continue processing these
4638 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4639 rib_process_result(ctx
);
4642 case DPLANE_OP_ROUTE_NOTIFY
:
4643 rib_process_dplane_notify(ctx
);
4646 case DPLANE_OP_NH_INSTALL
:
4647 case DPLANE_OP_NH_UPDATE
:
4648 case DPLANE_OP_NH_DELETE
:
4649 zebra_nhg_dplane_result(ctx
);
4652 case DPLANE_OP_LSP_INSTALL
:
4653 case DPLANE_OP_LSP_UPDATE
:
4654 case DPLANE_OP_LSP_DELETE
:
4655 /* Bit of special case for LSP updates
4656 * that were generated by async notifications:
4657 * we don't want to continue processing these.
4659 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4660 zebra_mpls_lsp_dplane_result(ctx
);
4663 case DPLANE_OP_LSP_NOTIFY
:
4664 zebra_mpls_process_dplane_notify(ctx
);
4667 case DPLANE_OP_PW_INSTALL
:
4668 case DPLANE_OP_PW_UNINSTALL
:
4669 handle_pw_result(ctx
);
4672 case DPLANE_OP_SYS_ROUTE_ADD
:
4673 case DPLANE_OP_SYS_ROUTE_DELETE
:
4676 case DPLANE_OP_MAC_INSTALL
:
4677 case DPLANE_OP_MAC_DELETE
:
4678 zebra_vxlan_handle_result(ctx
);
4681 case DPLANE_OP_RULE_ADD
:
4682 case DPLANE_OP_RULE_DELETE
:
4683 case DPLANE_OP_RULE_UPDATE
:
4684 case DPLANE_OP_IPTABLE_ADD
:
4685 case DPLANE_OP_IPTABLE_DELETE
:
4686 case DPLANE_OP_IPSET_ADD
:
4687 case DPLANE_OP_IPSET_DELETE
:
4688 case DPLANE_OP_IPSET_ENTRY_ADD
:
4689 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4690 zebra_pbr_dplane_result(ctx
);
4693 case DPLANE_OP_INTF_ADDR_ADD
:
4694 case DPLANE_OP_INTF_ADDR_DEL
:
4695 case DPLANE_OP_INTF_INSTALL
:
4696 case DPLANE_OP_INTF_UPDATE
:
4697 case DPLANE_OP_INTF_DELETE
:
4698 case DPLANE_OP_INTF_NETCONFIG
:
4699 zebra_if_dplane_result(ctx
);
4702 case DPLANE_OP_TC_INSTALL
:
4703 case DPLANE_OP_TC_UPDATE
:
4704 case DPLANE_OP_TC_DELETE
:
4707 /* Some op codes not handled here */
4708 case DPLANE_OP_ADDR_INSTALL
:
4709 case DPLANE_OP_ADDR_UNINSTALL
:
4710 case DPLANE_OP_NEIGH_INSTALL
:
4711 case DPLANE_OP_NEIGH_UPDATE
:
4712 case DPLANE_OP_NEIGH_DELETE
:
4713 case DPLANE_OP_NEIGH_IP_INSTALL
:
4714 case DPLANE_OP_NEIGH_IP_DELETE
:
4715 case DPLANE_OP_VTEP_ADD
:
4716 case DPLANE_OP_VTEP_DELETE
:
4717 case DPLANE_OP_NEIGH_DISCOVER
:
4718 case DPLANE_OP_BR_PORT_UPDATE
:
4719 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4720 case DPLANE_OP_GRE_SET
:
4721 case DPLANE_OP_NONE
:
4724 } /* Dispatch by op code */
4726 dplane_ctx_fini(&ctx
);
4727 ctx
= dplane_ctx_dequeue(&ctxlist
);
4734 * Results are returned from the dataplane subsystem, in the context of
4735 * the dataplane pthread. We enqueue the results here for processing by
4736 * the main thread later.
4738 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
4740 /* Take lock controlling queue of results */
4741 frr_with_mutex (&dplane_mutex
) {
4742 /* Enqueue context blocks */
4743 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4746 /* Ensure event is signalled to zebra main pthread */
4747 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4754 * Ensure there are no empty slots in the route_info array.
4755 * Every route type in zebra should be present there.
4757 static void check_route_info(void)
4759 int len
= array_size(route_info
);
4762 * ZEBRA_ROUTE_SYSTEM is special cased since
4763 * its key is 0 anyway.
4765 * ZEBRA_ROUTE_ALL is also ignored.
4767 for (int i
= 0; i
< len
; i
++) {
4768 assert(route_info
[i
].key
>= ZEBRA_ROUTE_SYSTEM
&&
4769 route_info
[i
].key
< ZEBRA_ROUTE_MAX
);
4770 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4774 /* Routing information base initialize. */
4781 /* Init dataplane, and register for results */
4782 pthread_mutex_init(&dplane_mutex
, NULL
);
4783 TAILQ_INIT(&rib_dplane_q
);
4784 zebra_dplane_init(rib_dplane_results
);
4790 * Get the first vrf id that is greater than the given vrf id if any.
4792 * Returns true if a vrf id was found, false otherwise.
4794 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4798 vrf
= vrf_lookup_by_id(vrf_id
);
4800 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4802 *next_id_p
= vrf
->vrf_id
;
4811 * rib_tables_iter_next
4813 * Returns the next table in the iteration.
4815 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4817 struct route_table
*table
;
4820 * Array that helps us go over all AFI/SAFI combinations via one
4823 static const struct {
4827 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4828 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4829 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4834 switch (iter
->state
) {
4836 case RIB_TABLES_ITER_S_INIT
:
4837 iter
->vrf_id
= VRF_DEFAULT
;
4838 iter
->afi_safi_ix
= -1;
4842 case RIB_TABLES_ITER_S_ITERATING
:
4843 iter
->afi_safi_ix
++;
4846 while (iter
->afi_safi_ix
4847 < (int)array_size(afi_safis
)) {
4848 table
= zebra_vrf_table(
4849 afi_safis
[iter
->afi_safi_ix
].afi
,
4850 afi_safis
[iter
->afi_safi_ix
].safi
,
4855 iter
->afi_safi_ix
++;
4859 * Found another table in this vrf.
4865 * Done with all tables in the current vrf, go to the
4869 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4872 iter
->afi_safi_ix
= 0;
4877 case RIB_TABLES_ITER_S_DONE
:
4882 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4884 iter
->state
= RIB_TABLES_ITER_S_DONE
;