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%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_ROUTE_REPLACING
)
305 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
307 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
308 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
313 uint8_t route_distance(int type
)
317 if ((unsigned)type
>= array_size(route_info
))
320 distance
= route_info
[type
].distance
;
325 int is_zebra_valid_kernel_table(uint32_t table_id
)
328 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
329 || (table_id
== RT_TABLE_COMPAT
))
336 int is_zebra_main_routing_table(uint32_t table_id
)
338 if (table_id
== RT_TABLE_MAIN
)
343 int zebra_check_addr(const struct prefix
*p
)
345 if (p
->family
== AF_INET
) {
348 addr
= p
->u
.prefix4
.s_addr
;
351 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
352 || IPV4_LINKLOCAL(addr
))
355 if (p
->family
== AF_INET6
) {
356 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
358 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
364 static void route_entry_attach_ref(struct route_entry
*re
,
365 struct nhg_hash_entry
*new)
368 re
->nhe_id
= new->id
;
369 re
->nhe_installed_id
= 0;
371 zebra_nhg_increment_ref(new);
374 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
375 int route_entry_update_nhe(struct route_entry
*re
,
376 struct nhg_hash_entry
*new_nhghe
)
379 struct nhg_hash_entry
*old_nhg
= NULL
;
381 if (new_nhghe
== NULL
) {
385 re
->nhe_installed_id
= 0;
390 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
391 /* Capture previous nhg, if any */
394 route_entry_attach_ref(re
, new_nhghe
);
396 /* This is the first time it's being attached */
397 route_entry_attach_ref(re
, new_nhghe
);
400 /* Detach / deref previous nhg */
402 zebra_nhg_decrement_ref(old_nhg
);
407 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
408 struct nhg_hash_entry
*new_entry
)
410 struct zebra_router_table
*zrt
;
411 struct route_node
*rn
;
412 struct route_entry
*re
, *next
;
414 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
415 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
416 __func__
, new_entry
->id
, new_entry
, old_entry
);
418 /* We have to do them ALL */
419 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
420 for (rn
= route_top(zrt
->table
); rn
;
421 rn
= srcdest_route_next(rn
)) {
422 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
423 if (re
->nhe
&& re
->nhe
== old_entry
)
424 route_entry_update_nhe(re
, new_entry
);
430 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
431 const union g_addr
*addr
,
432 struct route_node
**rn_out
)
435 struct route_table
*table
;
436 struct route_node
*rn
;
437 struct route_entry
*match
= NULL
;
440 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
444 memset(&p
, 0, sizeof(p
));
447 p
.u
.prefix4
= addr
->ipv4
;
448 p
.prefixlen
= IPV4_MAX_BITLEN
;
450 p
.u
.prefix6
= addr
->ipv6
;
451 p
.prefixlen
= IPV6_MAX_BITLEN
;
454 rn
= route_node_match(table
, &p
);
459 route_unlock_node(rn
);
461 dest
= rib_dest_from_rnode(rn
);
462 if (dest
&& dest
->selected_fib
463 && !CHECK_FLAG(dest
->selected_fib
->status
,
464 ROUTE_ENTRY_REMOVED
))
465 match
= dest
->selected_fib
;
467 /* If there is no selected route or matched route is EGP, go up
472 } while (rn
&& rn
->info
== NULL
);
476 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
477 if (!CHECK_FLAG(match
->status
,
478 ROUTE_ENTRY_INSTALLED
))
490 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
492 struct route_node
**rn_out
)
494 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
495 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
496 union g_addr gaddr
= {.ipv4
= addr
};
498 switch (zrouter
.ipv4_multicast_mode
) {
499 case MCAST_MRIB_ONLY
:
500 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
502 case MCAST_URIB_ONLY
:
503 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
504 case MCAST_NO_CONFIG
:
505 case MCAST_MIX_MRIB_FIRST
:
506 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
509 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
512 case MCAST_MIX_DISTANCE
:
513 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
514 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
516 re
= ure
->distance
< mre
->distance
? ure
: mre
;
522 case MCAST_MIX_PFXLEN
:
523 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
524 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
526 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
535 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
537 if (IS_ZEBRA_DEBUG_RIB
) {
539 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
541 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
542 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
543 mre
? (ure
? "MRIB+URIB" : "MRIB")
544 : ure
? "URIB" : "nothing",
545 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
550 struct route_entry
*rib_match_ipv6_multicast(vrf_id_t vrf_id
,
551 struct in6_addr addr
,
552 struct route_node
**rn_out
)
554 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
555 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
556 union g_addr gaddr
= {.ipv6
= addr
};
558 switch (zrouter
.ipv4_multicast_mode
) {
559 case MCAST_MRIB_ONLY
:
560 return rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
562 case MCAST_URIB_ONLY
:
563 return rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
564 case MCAST_NO_CONFIG
:
565 case MCAST_MIX_MRIB_FIRST
:
566 re
= mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
569 re
= ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
,
572 case MCAST_MIX_DISTANCE
:
573 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
574 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
576 re
= ure
->distance
< mre
->distance
? ure
: mre
;
582 case MCAST_MIX_PFXLEN
:
583 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
584 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
586 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
595 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
597 if (IS_ZEBRA_DEBUG_RIB
)
598 zlog_debug("%s: %pI6: vrf: %s(%u) found %s, using %s", __func__
,
599 &addr
, vrf_id_to_name(vrf_id
), vrf_id
,
600 mre
? (ure
? "MRIB+URIB" : "MRIB")
601 : ure
? "URIB" : "nothing",
602 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
606 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
608 struct route_table
*table
;
609 struct route_node
*rn
;
610 struct route_entry
*match
= NULL
;
614 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
618 rn
= route_node_lookup(table
, (struct prefix
*)p
);
620 /* No route for this prefix. */
625 route_unlock_node(rn
);
626 dest
= rib_dest_from_rnode(rn
);
628 if (dest
&& dest
->selected_fib
629 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
630 match
= dest
->selected_fib
;
635 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
638 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
645 * Is this RIB labeled-unicast? It must be of type BGP and all paths
646 * (nexthops) must have a label.
648 int zebra_rib_labeled_unicast(struct route_entry
*re
)
650 struct nexthop
*nexthop
= NULL
;
652 if (re
->type
!= ZEBRA_ROUTE_BGP
)
655 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
656 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
662 /* Update flag indicates whether this is a "replace" or not. Currently, this
663 * is only used for IPv4.
665 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
666 struct route_entry
*old
)
668 struct nexthop
*nexthop
;
669 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
670 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
671 const struct prefix
*p
, *src_p
;
672 enum zebra_dplane_result ret
;
674 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
676 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
678 if (info
->safi
!= SAFI_UNICAST
) {
679 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
680 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
686 * Install the resolved nexthop object first.
688 zebra_nhg_install_kernel(re
->nhe
);
691 * If this is a replace to a new RE let the originator of the RE
692 * know that they've lost
694 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
695 zsend_route_notify_owner(rn
, old
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
696 info
->afi
, info
->safi
);
698 /* Update fib selection */
699 dest
->selected_fib
= re
;
702 * Make sure we update the FPM any time we send new information to
705 hook_call(rib_update
, rn
, "installing in kernel");
707 /* Send add or update */
709 ret
= dplane_route_update(rn
, re
, old
);
711 ret
= dplane_route_add(rn
, re
);
714 case ZEBRA_DPLANE_REQUEST_QUEUED
:
715 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
718 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
719 SET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
721 /* Free old FIB nexthop group */
722 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
723 if (old
->fib_ng
.nexthop
) {
724 nexthops_free(old
->fib_ng
.nexthop
);
725 old
->fib_ng
.nexthop
= NULL
;
730 zvrf
->installs_queued
++;
732 case ZEBRA_DPLANE_REQUEST_FAILURE
:
734 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
735 "%u:%u:%pRN: Failed to enqueue dataplane install",
736 re
->vrf_id
, re
->table
, rn
);
739 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
748 /* Uninstall the route from kernel. */
749 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
751 struct nexthop
*nexthop
;
752 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
753 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
755 if (info
->safi
!= SAFI_UNICAST
) {
756 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
757 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
758 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
763 * Make sure we update the FPM any time we send new information to
766 hook_call(rib_update
, rn
, "uninstalling from kernel");
768 switch (dplane_route_delete(rn
, re
)) {
769 case ZEBRA_DPLANE_REQUEST_QUEUED
:
771 zvrf
->removals_queued
++;
773 case ZEBRA_DPLANE_REQUEST_FAILURE
:
774 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
775 "%u:%pRN: Failed to enqueue dataplane uninstall",
778 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
788 * rib_can_delete_dest
790 * Returns true if the given dest can be deleted from the table.
792 static int rib_can_delete_dest(rib_dest_t
*dest
)
794 if (re_list_first(&dest
->routes
)) {
799 * Unresolved rnh's are stored on the default route's list
801 * dest->rnode can also be the source prefix node in an
802 * ipv6 sourcedest table. Fortunately the prefix of a
803 * source prefix node can never be the default prefix.
805 if (is_default_prefix(&dest
->rnode
->p
))
809 * Don't delete the dest if we have to update the FPM about this
812 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
813 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
819 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
,
822 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
826 * We are storing the rnh's associated withb
827 * the tracked nexthop as a list of the rn's.
828 * Unresolved rnh's are placed at the top
829 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
830 * As such for each rn we need to walk up the tree
831 * and see if any rnh's need to see if they
832 * would match a more specific route
835 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
837 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
839 dest
? rnh_list_count(&dest
->nht
) : 0);
841 if (rt_delete
&& (!dest
|| !rnh_list_count(&dest
->nht
))) {
842 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
843 zlog_debug("%pRN has no tracking NHTs. Bailing",
850 dest
= rib_dest_from_rnode(rn
);
854 * If we have any rnh's stored in the nht list
855 * then we know that this route node was used for
856 * nht resolution and as such we need to call the
857 * nexthop tracking evaluation code
859 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
860 struct zebra_vrf
*zvrf
=
861 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
862 struct prefix
*p
= &rnh
->node
->p
;
864 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
866 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
867 zvrf_name(zvrf
), zvrf_id(zvrf
), rn
,
868 rnh
->node
, seq
, rnh
->seqno
);
871 * If we have evaluated this node on this pass
872 * already, due to following the tree up
873 * then we know that we can move onto the next
876 * Additionally we call zebra_evaluate_rnh
877 * when we gc the dest. In this case we know
878 * that there must be no other re's where
879 * we were originally as such we know that
880 * that sequence number is ok to respect.
882 if (rnh
->seqno
== seq
) {
883 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
885 " Node processed and moved already");
890 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0, p
,
896 dest
= rib_dest_from_rnode(rn
);
903 * Garbage collect the rib dest corresponding to the given route node
906 * Returns true if the dest was deleted, false otherwise.
908 int rib_gc_dest(struct route_node
*rn
)
912 dest
= rib_dest_from_rnode(rn
);
916 if (!rib_can_delete_dest(dest
))
919 if (IS_ZEBRA_DEBUG_RIB
) {
920 struct zebra_vrf
*zvrf
;
922 zvrf
= rib_dest_vrf(dest
);
923 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
926 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
930 rnh_list_fini(&dest
->nht
);
931 XFREE(MTYPE_RIB_DEST
, dest
);
935 * Release the one reference that we keep on the route node.
937 route_unlock_node(rn
);
941 void zebra_rtable_node_cleanup(struct route_table
*table
,
942 struct route_node
*node
)
944 struct route_entry
*re
, *next
;
946 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
947 rib_unlink(node
, re
);
951 rib_dest_t
*dest
= node
->info
;
953 /* Remove from update queue of FPM module */
954 hook_call(rib_shutdown
, node
);
956 rnh_list_fini(&dest
->nht
);
957 XFREE(MTYPE_RIB_DEST
, node
->info
);
961 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
962 struct route_entry
*new)
964 hook_call(rib_update
, rn
, "new route selected");
966 /* Update real nexthop. This may actually determine if nexthop is active
968 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
969 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
973 if (IS_ZEBRA_DEBUG_RIB
)
974 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
975 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, rn
, rn
,
976 new, zebra_route_string(new->type
));
978 /* If labeled-unicast route, install transit LSP. */
979 if (zebra_rib_labeled_unicast(new))
980 zebra_mpls_lsp_install(zvrf
, rn
, new);
982 rib_install_kernel(rn
, new, NULL
);
984 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
987 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
988 struct route_entry
*old
)
990 hook_call(rib_update
, rn
, "removing existing route");
992 /* Uninstall from kernel. */
993 if (IS_ZEBRA_DEBUG_RIB
)
994 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
995 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, rn
, rn
,
996 old
, zebra_route_string(old
->type
));
998 /* If labeled-unicast route, uninstall transit LSP. */
999 if (zebra_rib_labeled_unicast(old
))
1000 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1002 rib_uninstall_kernel(rn
, old
);
1004 /* Update nexthop for route, reset changed flag. */
1005 /* Note: this code also handles the Linux case when an interface goes
1006 * down, causing the kernel to delete routes without sending DELROUTE
1009 if (RIB_KERNEL_ROUTE(old
))
1010 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
1012 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1015 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1016 struct route_node
*rn
,
1017 struct route_entry
*old
,
1018 struct route_entry
*new)
1023 * We have to install or update if a new route has been selected or
1024 * something has changed.
1026 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1027 hook_call(rib_update
, rn
, "updating existing route");
1029 /* Update the nexthop; we could determine here that nexthop is
1031 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
1034 /* If nexthop is active, install the selected route, if
1036 * the install succeeds, cleanup flags for prior route, if
1041 if (IS_ZEBRA_DEBUG_RIB
) {
1044 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
1045 zvrf_name(zvrf
), zvrf_id(zvrf
),
1046 new->table
, rn
, rn
, new,
1047 zebra_route_string(new->type
),
1049 zebra_route_string(old
->type
));
1052 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
1053 zvrf_name(zvrf
), zvrf_id(zvrf
),
1054 new->table
, rn
, rn
, new,
1055 zebra_route_string(new->type
));
1058 /* If labeled-unicast route, uninstall transit LSP. */
1059 if (zebra_rib_labeled_unicast(old
))
1060 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1063 * Non-system route should be installed.
1064 * If labeled-unicast route, install transit
1067 if (zebra_rib_labeled_unicast(new))
1068 zebra_mpls_lsp_install(zvrf
, rn
, new);
1070 rib_install_kernel(rn
, new, old
);
1074 * If nexthop for selected route is not active or install
1076 * may need to uninstall and delete for redistribution.
1079 if (IS_ZEBRA_DEBUG_RIB
) {
1082 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
1083 zvrf_name(zvrf
), zvrf_id(zvrf
),
1084 new->table
, rn
, rn
, new,
1085 zebra_route_string(new->type
),
1087 zebra_route_string(old
->type
));
1090 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) - nexthop inactive",
1091 zvrf_name(zvrf
), zvrf_id(zvrf
),
1092 new->table
, rn
, rn
, new,
1093 zebra_route_string(new->type
));
1097 * When we have gotten to this point
1098 * the new route entry has no nexthops
1099 * that are usable and as such we need
1100 * to remove the old route, but only
1101 * if we were the one who installed
1104 if (!RIB_SYSTEM_ROUTE(old
)) {
1105 /* If labeled-unicast route, uninstall transit
1107 if (zebra_rib_labeled_unicast(old
))
1108 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1110 rib_uninstall_kernel(rn
, old
);
1115 * Same route selected; check if in the FIB and if not,
1116 * re-install. This is housekeeping code to deal with
1117 * race conditions in kernel with linux netlink reporting
1118 * interface up before IPv4 or IPv6 protocol is ready
1121 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1122 RIB_SYSTEM_ROUTE(new))
1123 rib_install_kernel(rn
, new, NULL
);
1126 /* Update prior route. */
1128 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1130 /* Clear changed flag. */
1131 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1134 /* Check if 'alternate' RIB entry is better than 'current'. */
1135 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1136 struct route_entry
*alternate
)
1138 if (current
== NULL
)
1141 /* filter route selection in following order:
1142 * - connected beats other types
1143 * - if both connected, loopback or vrf wins
1144 * - lower distance beats higher
1145 * - lower metric beats higher for equal distance
1146 * - last, hence oldest, route wins tie break.
1149 /* Connected routes. Check to see if either are a vrf
1150 * or loopback interface. If not, pick the last connected
1151 * route of the set of lowest metric connected routes.
1153 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1154 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1157 /* both are connected. are either loop or vrf? */
1158 struct nexthop
*nexthop
= NULL
;
1160 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1161 struct interface
*ifp
= if_lookup_by_index(
1162 nexthop
->ifindex
, alternate
->vrf_id
);
1164 if (ifp
&& if_is_loopback(ifp
))
1168 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1169 struct interface
*ifp
= if_lookup_by_index(
1170 nexthop
->ifindex
, current
->vrf_id
);
1172 if (ifp
&& if_is_loopback(ifp
))
1176 /* Neither are loop or vrf so pick best metric */
1177 if (alternate
->metric
<= current
->metric
)
1183 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1186 /* higher distance loses */
1187 if (alternate
->distance
< current
->distance
)
1189 if (current
->distance
< alternate
->distance
)
1192 /* metric tie-breaks equal distance */
1193 if (alternate
->metric
<= current
->metric
)
1199 /* Core function for processing routing information base. */
1200 static void rib_process(struct route_node
*rn
)
1202 struct route_entry
*re
;
1203 struct route_entry
*next
;
1204 struct route_entry
*old_selected
= NULL
;
1205 struct route_entry
*new_selected
= NULL
;
1206 struct route_entry
*old_fib
= NULL
;
1207 struct route_entry
*new_fib
= NULL
;
1208 struct route_entry
*best
= NULL
;
1210 struct zebra_vrf
*zvrf
= NULL
;
1213 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1217 dest
= rib_dest_from_rnode(rn
);
1219 * We have an enqueued node with nothing to process here
1220 * let's just finish up and return;
1225 zvrf
= rib_dest_vrf(dest
);
1226 vrf_id
= zvrf_id(zvrf
);
1228 vrf
= vrf_lookup_by_id(vrf_id
);
1231 * we can have rn's that have a NULL info pointer
1232 * (dest). As such let's not let the deref happen
1233 * additionally we know RNODE_FOREACH_RE_SAFE
1234 * will not iterate so we are ok.
1236 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1237 struct route_entry
*re
= re_list_first(&dest
->routes
);
1239 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1240 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
,
1244 old_fib
= dest
->selected_fib
;
1246 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1247 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1248 char flags_buf
[128];
1249 char status_buf
[128];
1252 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1253 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
, re
,
1254 zebra_route_string(re
->type
),
1255 _dump_re_status(re
, status_buf
,
1256 sizeof(status_buf
)),
1257 zclient_dump_route_flags(re
->flags
, flags_buf
,
1259 re
->distance
, re
->metric
);
1262 /* Currently selected re. */
1263 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1264 assert(old_selected
== NULL
);
1268 /* Skip deleted entries from selection */
1269 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1273 * If the route entry has changed, verify/resolve
1274 * the nexthops associated with the entry.
1276 * In any event if we have nexthops that are not active
1277 * then we cannot use this particular route entry so
1280 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1281 if (!nexthop_active_update(rn
, re
)) {
1282 const struct prefix
*p
;
1283 struct rib_table_info
*info
;
1285 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1286 /* XXX: HERE BE DRAGONS!!!!!
1287 * In all honesty, I have not yet
1288 * figured out what this part does or
1289 * why the ROUTE_ENTRY_CHANGED test
1290 * above is correct or why we need to
1291 * delete a route here, and also not
1292 * whether this concerns both selected
1293 * and fib route, or only selected
1296 * This entry was denied by the 'ip
1298 * table' route-map, we need to delete
1300 if (re
!= old_selected
) {
1301 if (IS_ZEBRA_DEBUG_RIB
)
1303 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1310 SET_FLAG(re
->status
,
1311 ROUTE_ENTRY_REMOVED
);
1314 info
= srcdest_rnode_table_info(rn
);
1315 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1316 zsend_route_notify_owner(
1317 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
1318 info
->afi
, info
->safi
);
1323 * If the re has not changed and the nhg we have is
1324 * not usable, then we cannot use this route entry
1325 * for consideration, as that the route will just
1326 * not install if it is selected.
1328 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1332 /* Infinite distance. */
1333 if (re
->distance
== DISTANCE_INFINITY
&&
1334 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1335 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1339 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1340 best
= rib_choose_best(new_fib
, re
);
1341 if (new_fib
&& best
!= new_fib
)
1342 UNSET_FLAG(new_fib
->status
,
1343 ROUTE_ENTRY_CHANGED
);
1346 best
= rib_choose_best(new_selected
, re
);
1347 if (new_selected
&& best
!= new_selected
)
1348 UNSET_FLAG(new_selected
->status
,
1349 ROUTE_ENTRY_CHANGED
);
1350 new_selected
= best
;
1353 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1354 } /* RNODE_FOREACH_RE */
1356 /* If no FIB override route, use the selected route also for FIB */
1357 if (new_fib
== NULL
)
1358 new_fib
= new_selected
;
1360 /* After the cycle is finished, the following pointers will be set:
1361 * old_selected --- RE entry currently having SELECTED
1362 * new_selected --- RE entry that is newly SELECTED
1363 * old_fib --- RE entry currently in kernel FIB
1364 * new_fib --- RE entry that is newly to be in kernel FIB
1366 * new_selected will get SELECTED flag, and is going to be redistributed
1367 * the zclients. new_fib (which can be new_selected) will be installed
1371 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1372 struct route_entry
*entry
;
1374 entry
= old_selected
1379 : new_fib
? new_fib
: NULL
;
1382 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1383 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, rn
,
1384 (void *)old_selected
, (void *)new_selected
,
1385 (void *)old_fib
, (void *)new_fib
);
1388 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1389 * fib == selected */
1390 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1391 ROUTE_ENTRY_CHANGED
);
1393 /* Update SELECTED entry */
1394 if (old_selected
!= new_selected
|| selected_changed
) {
1396 if (new_selected
&& new_selected
!= new_fib
)
1397 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1400 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1404 * If we're removing the old entry, we should tell
1405 * redist subscribers about that *if* they aren't
1406 * going to see a redist for the new entry.
1408 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1409 ROUTE_ENTRY_REMOVED
))
1410 redistribute_delete(rn
, old_selected
,
1413 if (old_selected
!= new_selected
)
1414 UNSET_FLAG(old_selected
->flags
,
1415 ZEBRA_FLAG_SELECTED
);
1419 /* Update fib according to selection results */
1420 if (new_fib
&& old_fib
)
1421 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1423 rib_process_add_fib(zvrf
, rn
, new_fib
);
1425 rib_process_del_fib(zvrf
, rn
, old_fib
);
1427 /* Remove all RE entries queued for removal */
1428 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1429 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1430 if (IS_ZEBRA_DEBUG_RIB
) {
1431 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1432 (void *)rn
, (void *)re
);
1439 * Check if the dest can be deleted now.
1444 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1446 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1447 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1452 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1453 if (IS_ZEBRA_DEBUG_MPLS
)
1455 "%s(%u): Scheduling all LSPs upon RIB completion",
1456 zvrf_name(zvrf
), zvrf_id(zvrf
));
1457 zebra_mpls_lsp_schedule(zvrf
);
1458 mpls_unmark_lsps_for_processing(rn
);
1463 * Utility to match route with dplane context data
1465 static bool rib_route_match_ctx(const struct route_entry
*re
,
1466 const struct zebra_dplane_ctx
*ctx
,
1469 bool result
= false;
1473 * In 'update' case, we test info about the 'previous' or
1476 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1477 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1480 /* We use an extra test for statics, and another for
1483 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1484 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1485 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1487 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1489 dplane_ctx_get_old_metric(ctx
)) {
1496 * Ordinary, single-route case using primary context info
1498 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1499 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1500 /* Skip route that's been deleted */
1504 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1505 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1508 /* We use an extra test for statics, and another for
1511 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1512 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1513 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1515 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1516 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1518 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1519 result
= nexthop_group_equal_no_recurse(
1520 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1529 static void zebra_rib_fixup_system(struct route_node
*rn
)
1531 struct route_entry
*re
;
1533 RNODE_FOREACH_RE(rn
, re
) {
1534 struct nexthop
*nhop
;
1536 if (!RIB_SYSTEM_ROUTE(re
))
1539 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1542 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1543 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1544 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
1546 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1547 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1550 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1555 /* Route comparison logic, with various special cases. */
1556 static bool rib_compare_routes(const struct route_entry
*re1
,
1557 const struct route_entry
*re2
)
1559 if (re1
->type
!= re2
->type
)
1562 if (re1
->instance
!= re2
->instance
)
1565 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1568 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1569 re1
->distance
!= re2
->distance
)
1572 /* We support multiple connected routes: this supports multiple
1573 * v6 link-locals, and we also support multiple addresses in the same
1574 * subnet on a single interface.
1576 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1583 * Compare nexthop lists from a route and a dplane context; test whether
1584 * the list installed in the FIB matches the route's list.
1585 * Set 'changed_p' to 'true' if there were changes to the route's
1586 * installed nexthops.
1588 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1591 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1592 const struct nexthop_group
*ctx_nhg
,
1595 bool matched_p
= true;
1596 struct nexthop
*nexthop
, *ctx_nexthop
;
1598 /* Get the first `installed` one to check against.
1599 * If the dataplane doesn't set these to be what was actually installed,
1600 * it will just be whatever was in re->nhe->nhg?
1602 ctx_nexthop
= ctx_nhg
->nexthop
;
1604 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1605 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1606 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1608 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1610 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1613 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1616 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1617 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1618 /* If the FIB doesn't know about the nexthop,
1619 * it's not installed
1621 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1622 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1623 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1625 (CHECK_FLAG(nexthop
->flags
,
1631 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1634 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1636 /* Keep checking nexthops */
1640 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1641 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1642 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1643 zlog_debug("%s: rib nh %pNHv -> installed",
1649 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1651 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1652 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1653 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1659 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1662 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1669 * Update a route from a dplane context. This consolidates common code
1670 * that can be used in processing of results from FIB updates, and in
1671 * async notification processing.
1672 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1674 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1675 struct route_node
*rn
,
1676 struct zebra_dplane_ctx
*ctx
)
1678 struct nexthop
*nexthop
;
1680 const struct nexthop_group
*ctxnhg
;
1681 struct nexthop_group
*re_nhg
;
1682 bool is_selected
= false; /* Is 're' currently the selected re? */
1683 bool changed_p
= false; /* Change to nexthops? */
1687 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1689 dest
= rib_dest_from_rnode(rn
);
1691 is_selected
= (re
== dest
->selected_fib
);
1693 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1694 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1695 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1696 (is_selected
? "" : "NOT "), re
);
1698 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1699 * If the installed set differs from the set requested by the rib/owner,
1700 * we use the fib-specific nexthop-group to record the actual FIB
1704 ctxnhg
= dplane_ctx_get_ng(ctx
);
1706 /* Check route's fib group and incoming notif group for equivalence.
1708 * Let's assume the nexthops are ordered here to save time.
1710 /* TODO -- this isn't testing or comparing the FIB flags; we should
1711 * do a more explicit loop, checking the incoming notification's flags.
1713 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1714 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1717 /* If the new FIB set matches the existing FIB set, we're done. */
1719 if (IS_ZEBRA_DEBUG_RIB
)
1721 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1722 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1725 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1727 * Free stale fib list and move on to check the rib nhg.
1729 if (IS_ZEBRA_DEBUG_RIB
)
1731 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1732 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1733 nexthops_free(re
->fib_ng
.nexthop
);
1734 re
->fib_ng
.nexthop
= NULL
;
1736 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1738 /* Note that the installed nexthops have changed */
1741 if (IS_ZEBRA_DEBUG_RIB
)
1743 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1744 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1748 * Compare with the rib nexthop group. The comparison here is different:
1749 * the RIB group may be a superset of the list installed in the FIB. We
1750 * walk the RIB group, looking for the 'installable' candidate
1751 * nexthops, and then check those against the set
1752 * that is actually installed.
1754 * Assume nexthops are ordered here as well.
1757 /* If nothing is installed, we can skip some of the checking/comparison
1760 if (ctxnhg
->nexthop
== NULL
) {
1765 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1767 /* If all nexthops were processed, we're done */
1769 if (IS_ZEBRA_DEBUG_RIB
)
1771 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1772 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1773 (changed_p
? "true" : "false"));
1779 /* FIB nexthop set differs from the RIB set:
1780 * create a fib-specific nexthop-group
1782 if (IS_ZEBRA_DEBUG_RIB
)
1784 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1785 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1786 (changed_p
? "true" : "false"),
1787 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1789 /* Set the flag about the dedicated fib list */
1790 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1791 if (ctxnhg
->nexthop
)
1792 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1797 * Check the status of the route's backup nexthops, if any.
1798 * The logic for backups is somewhat different: if any backup is
1799 * installed, a new fib nhg will be attached to the route.
1801 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1803 goto done
; /* No backup nexthops */
1805 /* First check the route's 'fib' list of backups, if it's present
1806 * from some previous event.
1808 re_nhg
= &re
->fib_backup_ng
;
1809 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1812 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1815 /* If the new FIB set matches an existing FIB set, we're done. */
1817 if (IS_ZEBRA_DEBUG_RIB
)
1819 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1820 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1823 } else if (re
->fib_backup_ng
.nexthop
) {
1825 * Free stale fib backup list and move on to check
1826 * the route's backups.
1828 if (IS_ZEBRA_DEBUG_RIB
)
1830 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1831 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1832 nexthops_free(re
->fib_backup_ng
.nexthop
);
1833 re
->fib_backup_ng
.nexthop
= NULL
;
1835 /* Note that the installed nexthops have changed */
1838 if (IS_ZEBRA_DEBUG_RIB
)
1840 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1841 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1845 * If a FIB backup nexthop set exists, attach a copy
1846 * to the route if any backup is installed
1848 if (ctxnhg
&& ctxnhg
->nexthop
) {
1850 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1851 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1855 /* If no installed backups, we're done */
1856 if (nexthop
== NULL
)
1859 if (IS_ZEBRA_DEBUG_RIB
)
1861 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1862 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1863 (changed_p
? "true" : "false"));
1865 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1875 * Helper to locate a zebra route-node from a dplane context. This is used
1876 * when processing dplane results, e.g. Note well: the route-node is returned
1877 * with a ref held - route_unlock_node() must be called eventually.
1879 struct route_node
*rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1881 struct route_table
*table
= NULL
;
1882 struct route_node
*rn
= NULL
;
1883 const struct prefix
*dest_pfx
, *src_pfx
;
1885 /* Locate rn and re(s) from ctx */
1887 table
= zebra_vrf_lookup_table_with_table_id(
1888 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1889 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1890 if (table
== NULL
) {
1891 if (IS_ZEBRA_DEBUG_DPLANE
) {
1893 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1894 dplane_ctx_get_afi(ctx
),
1895 dplane_ctx_get_safi(ctx
),
1896 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1897 dplane_ctx_get_vrf(ctx
));
1902 dest_pfx
= dplane_ctx_get_dest(ctx
);
1903 src_pfx
= dplane_ctx_get_src(ctx
);
1905 rn
= srcdest_rnode_get(table
, dest_pfx
,
1906 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1915 * Route-update results processing after async dataplane update.
1917 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1919 struct zebra_vrf
*zvrf
= NULL
;
1921 struct route_node
*rn
= NULL
;
1922 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1923 bool is_update
= false;
1924 enum dplane_op_e op
;
1925 enum zebra_dplane_result status
;
1928 bool fib_changed
= false;
1929 struct rib_table_info
*info
;
1930 bool rt_delete
= false;
1932 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1933 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1935 /* Locate rn and re(s) from ctx */
1936 rn
= rib_find_rn_from_ctx(ctx
);
1938 if (IS_ZEBRA_DEBUG_DPLANE
) {
1940 "Failed to process dplane results: no route for %s(%u):%pRN",
1941 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
), rn
);
1946 dest
= rib_dest_from_rnode(rn
);
1947 info
= srcdest_rnode_table_info(rn
);
1949 op
= dplane_ctx_get_op(ctx
);
1950 status
= dplane_ctx_get_status(ctx
);
1952 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1954 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1955 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1956 dplane_ctx_get_table(ctx
), rn
, ctx
, dplane_op2str(op
),
1957 dplane_res2str(status
));
1960 * Update is a bit of a special case, where we may have both old and new
1961 * routes to post-process.
1963 is_update
= dplane_ctx_is_update(ctx
);
1966 * Take a pass through the routes, look for matches with the context
1969 RNODE_FOREACH_RE(rn
, rib
) {
1972 if (rib_route_match_ctx(rib
, ctx
, false))
1976 /* Check for old route match */
1977 if (is_update
&& (old_re
== NULL
)) {
1978 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1982 /* Have we found the routes we need to work on? */
1983 if (re
&& ((!is_update
|| old_re
)))
1987 seq
= dplane_ctx_get_seq(ctx
);
1990 * Check sequence number(s) to detect stale results before continuing
1993 if (re
->dplane_sequence
!= seq
) {
1994 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1996 "%s(%u):%pRN Stale dplane result for re %p",
1998 dplane_ctx_get_vrf(ctx
), rn
, re
);
2000 if (!zrouter
.asic_offloaded
||
2001 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
2002 CHECK_FLAG(re
->flags
,
2003 ZEBRA_FLAG_OFFLOAD_FAILED
))) {
2004 UNSET_FLAG(re
->status
,
2005 ROUTE_ENTRY_ROUTE_REPLACING
);
2006 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2012 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
2013 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2015 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
2017 dplane_ctx_get_vrf(ctx
), old_re
->table
,
2020 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
2024 case DPLANE_OP_ROUTE_INSTALL
:
2025 case DPLANE_OP_ROUTE_UPDATE
:
2026 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2028 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2029 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2032 * On an update operation from the same route type
2033 * context retrieval currently has no way to know
2034 * which was the old and which was the new.
2035 * So don't unset our flags that we just set.
2036 * We know redistribution is ok because the
2037 * old_re in this case is used for nothing
2038 * more than knowing whom to contact if necessary.
2040 if (old_re
&& old_re
!= re
) {
2041 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2042 UNSET_FLAG(old_re
->status
,
2043 ROUTE_ENTRY_INSTALLED
);
2046 /* Update zebra route based on the results in
2047 * the context struct.
2051 rib_update_re_from_ctx(re
, rn
, ctx
);
2054 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2056 "%s(%u:%u):%pRN no fib change for re",
2058 dplane_ctx_get_vrf(ctx
),
2059 dplane_ctx_get_table(
2064 /* Redistribute if this is the selected re */
2065 if (dest
&& re
== dest
->selected_fib
)
2066 redistribute_update(rn
, re
, old_re
);
2070 * System routes are weird in that they
2071 * allow multiple to be installed that match
2072 * to the same prefix, so after we get the
2073 * result we need to clean them up so that
2074 * we can actually use them.
2076 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2077 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2078 zebra_rib_fixup_system(rn
);
2083 /* Notify route owner */
2084 if (zebra_router_notify_on_ack())
2085 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2088 if (CHECK_FLAG(re
->flags
,
2089 ZEBRA_FLAG_OFFLOADED
))
2090 zsend_route_notify_owner_ctx(
2092 ZAPI_ROUTE_INSTALLED
);
2095 ZEBRA_FLAG_OFFLOAD_FAILED
))
2096 zsend_route_notify_owner_ctx(
2098 ZAPI_ROUTE_FAIL_INSTALL
);
2103 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2104 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2106 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2108 zsend_route_notify_owner(
2109 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
2110 info
->afi
, info
->safi
);
2112 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2113 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2114 dplane_ctx_get_table(ctx
), rn
);
2117 case DPLANE_OP_ROUTE_DELETE
:
2120 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2122 * In the delete case, the zebra core datastructs were
2123 * updated (or removed) at the time the delete was issued,
2124 * so we're just notifying the route owner.
2126 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2128 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2129 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2131 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2137 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2138 zsend_route_notify_owner_ctx(ctx
,
2139 ZAPI_ROUTE_REMOVE_FAIL
);
2141 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2142 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2143 dplane_ctx_get_table(ctx
), rn
);
2147 * System routes are weird in that they
2148 * allow multiple to be installed that match
2149 * to the same prefix, so after we get the
2150 * result we need to clean them up so that
2151 * we can actually use them.
2153 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2154 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2155 zebra_rib_fixup_system(rn
);
2161 zebra_rib_evaluate_rn_nexthops(rn
, seq
, rt_delete
);
2162 zebra_rib_evaluate_mpls(rn
);
2166 route_unlock_node(rn
);
2170 * Count installed/FIB nexthops
2172 static int rib_count_installed_nh(struct route_entry
*re
)
2175 struct nexthop
*nexthop
;
2176 struct nexthop_group
*nhg
;
2178 nhg
= rib_get_fib_nhg(re
);
2180 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2181 /* The meaningful flag depends on where the installed
2184 if (nhg
== &(re
->fib_ng
)) {
2185 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2188 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2193 nhg
= rib_get_fib_backup_nhg(re
);
2195 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2196 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2205 * Handle notification from async dataplane: the dataplane has detected
2206 * some change to a route, and notifies zebra so that the control plane
2207 * can reflect that change.
2209 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2211 struct route_node
*rn
= NULL
;
2212 struct route_entry
*re
= NULL
;
2214 struct nexthop
*nexthop
;
2216 bool fib_changed
= false;
2217 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2218 int start_count
, end_count
;
2220 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2222 /* Locate rn and re(s) from ctx */
2223 rn
= rib_find_rn_from_ctx(ctx
);
2227 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2228 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2229 dplane_ctx_get_table(ctx
), rn
);
2234 dest
= rib_dest_from_rnode(rn
);
2237 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2238 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2239 dplane_ctx_get_table(ctx
), rn
, ctx
);
2242 * Take a pass through the routes, look for matches with the context
2245 RNODE_FOREACH_RE(rn
, re
) {
2246 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2250 /* No match? Nothing we can do */
2254 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2255 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2256 dplane_ctx_get_table(ctx
), rn
,
2257 zebra_route_string(dplane_ctx_get_type(ctx
)));
2262 /* Ensure we clear the QUEUED flag */
2263 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2264 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
2266 /* Is this a notification that ... matters? We mostly care about
2267 * the route that is currently selected for installation; we may also
2268 * get an un-install notification, and handle that too.
2270 if (re
!= dest
->selected_fib
) {
2272 * If we need to, clean up after a delete that was part of
2273 * an update operation.
2276 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2277 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2281 /* If no nexthops or none installed, ensure that this re
2282 * gets its 'installed' flag cleared.
2284 if (end_count
== 0) {
2285 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2286 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2289 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2291 dplane_ctx_get_vrf(ctx
),
2292 dplane_ctx_get_table(ctx
), rn
,
2294 dplane_ctx_get_type(ctx
)));
2296 /* At least report on the event. */
2299 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2301 dplane_ctx_get_vrf(ctx
),
2302 dplane_ctx_get_table(ctx
), rn
,
2304 dplane_ctx_get_type(ctx
)));
2308 uint32_t flags
= dplane_ctx_get_flags(ctx
);
2310 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOADED
)) {
2311 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2312 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2314 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)) {
2315 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2316 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2318 if (CHECK_FLAG(flags
, ZEBRA_FLAG_TRAPPED
))
2319 SET_FLAG(re
->flags
, ZEBRA_FLAG_TRAPPED
);
2322 /* We'll want to determine whether the installation status of the
2323 * route has changed: we'll check the status before processing,
2324 * and then again if there's been a change.
2328 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2329 start_count
= rib_count_installed_nh(re
);
2331 /* Update zebra's nexthop FIB flags based on the context struct's
2334 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2339 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2340 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2341 dplane_ctx_get_table(ctx
), rn
);
2345 * Perform follow-up work if the actual status of the prefix
2348 end_count
= rib_count_installed_nh(re
);
2350 /* Various fib transitions: changed nexthops; from installed to
2351 * not-installed; or not-installed to installed.
2353 if (zrouter
.asic_notification_nexthop_control
) {
2354 if (start_count
> 0 && end_count
> 0) {
2357 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2359 dplane_ctx_get_vrf(ctx
),
2360 dplane_ctx_get_table(ctx
), rn
);
2362 /* Changed nexthops - update kernel/others */
2363 dplane_route_notif_update(rn
, re
,
2364 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2366 } else if (start_count
== 0 && end_count
> 0) {
2369 "%s(%u:%u):%pRN installed transition from dplane notification",
2371 dplane_ctx_get_vrf(ctx
),
2372 dplane_ctx_get_table(ctx
), rn
);
2374 /* We expect this to be the selected route, so we want
2375 * to tell others about this transition.
2377 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2379 /* Changed nexthops - update kernel/others */
2380 dplane_route_notif_update(rn
, re
,
2381 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2383 /* Redistribute, lsp, and nht update */
2384 redistribute_update(rn
, re
, NULL
);
2386 } else if (start_count
> 0 && end_count
== 0) {
2389 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2391 dplane_ctx_get_vrf(ctx
),
2392 dplane_ctx_get_table(ctx
), rn
);
2394 /* Transition from _something_ installed to _nothing_
2397 /* We expect this to be the selected route, so we want
2398 * to tell others about this transistion.
2400 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2402 /* Changed nexthops - update kernel/others */
2403 dplane_route_notif_update(rn
, re
,
2404 DPLANE_OP_ROUTE_DELETE
, ctx
);
2406 /* Redistribute, lsp, and nht update */
2407 redistribute_delete(rn
, re
, NULL
);
2411 if (!zebra_router_notify_on_ack()) {
2412 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
))
2413 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2414 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
))
2415 zsend_route_notify_owner_ctx(ctx
,
2416 ZAPI_ROUTE_FAIL_INSTALL
);
2419 /* Make any changes visible for lsp and nexthop-tracking processing */
2420 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
2423 zebra_rib_evaluate_mpls(rn
);
2427 route_unlock_node(rn
);
2431 * Process a node from the EVPN/VXLAN subqueue.
2433 static void process_subq_evpn(struct listnode
*lnode
)
2435 struct wq_evpn_wrapper
*w
;
2437 /* In general, the list node points to a wrapper object
2438 * holding the info necessary to make some update.
2440 w
= listgetdata(lnode
);
2444 if (w
->type
== WQ_EVPN_WRAPPER_TYPE_VRFROUTE
) {
2446 zebra_vxlan_evpn_vrf_route_add(w
->vrf_id
, &w
->macaddr
,
2447 &w
->ip
, &w
->prefix
);
2449 zebra_vxlan_evpn_vrf_route_del(w
->vrf_id
, &w
->ip
,
2451 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_ES
) {
2453 zebra_evpn_remote_es_add(&w
->esi
, w
->ip
.ipaddr_v4
,
2454 w
->esr_rxed
, w
->df_alg
,
2457 zebra_evpn_remote_es_del(&w
->esi
, w
->ip
.ipaddr_v4
);
2458 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_MACIP
) {
2459 uint16_t ipa_len
= 0;
2461 if (w
->ip
.ipa_type
== IPADDR_V4
)
2462 ipa_len
= IPV4_MAX_BYTELEN
;
2463 else if (w
->ip
.ipa_type
== IPADDR_V6
)
2464 ipa_len
= IPV6_MAX_BYTELEN
;
2467 zebra_evpn_rem_macip_add(w
->vni
, &w
->macaddr
, ipa_len
,
2468 &w
->ip
, w
->flags
, w
->seq
,
2469 w
->vtep_ip
, &w
->esi
);
2471 zebra_evpn_rem_macip_del(w
->vni
, &w
->macaddr
, ipa_len
,
2472 &w
->ip
, w
->vtep_ip
);
2473 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_VTEP
) {
2475 zebra_vxlan_remote_vtep_add(w
->vrf_id
, w
->vni
,
2476 w
->vtep_ip
, w
->flags
);
2478 zebra_vxlan_remote_vtep_del(w
->vrf_id
, w
->vni
,
2483 XFREE(MTYPE_WQ_WRAPPER
, w
);
2487 * Process the nexthop-group workqueue subqueue
2489 static void process_subq_nhg(struct listnode
*lnode
)
2491 struct nhg_ctx
*ctx
;
2492 struct nhg_hash_entry
*nhe
, *newnhe
;
2493 struct wq_nhg_wrapper
*w
;
2494 uint8_t qindex
= META_QUEUE_NHG
;
2496 w
= listgetdata(lnode
);
2501 /* Two types of object - an update from the local kernel, or
2502 * an nhg update from a daemon.
2504 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2507 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2509 "NHG Context id=%u dequeued from sub-queue %s",
2510 ctx
->id
, subqueue2str(qindex
));
2513 /* Process nexthop group updates coming 'up' from the OS */
2514 nhg_ctx_process(ctx
);
2516 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2519 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2520 zlog_debug("NHG %u dequeued from sub-queue %s", nhe
->id
,
2521 subqueue2str(qindex
));
2523 /* Process incoming nhg update, probably from a proto daemon */
2524 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2526 nhe
->zapi_session
, &nhe
->nhg
, 0);
2528 /* Report error to daemon via ZAPI */
2530 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2531 nhe
->zapi_session
, nhe
->id
,
2532 ZAPI_NHG_FAIL_INSTALL
);
2534 /* Free temp nhe - we own that memory. */
2535 zebra_nhg_free(nhe
);
2538 XFREE(MTYPE_WQ_WRAPPER
, w
);
2541 static void process_subq_early_label(struct listnode
*lnode
)
2543 struct wq_label_wrapper
*w
= listgetdata(lnode
);
2544 struct zebra_vrf
*zvrf
;
2549 zvrf
= vrf_info_lookup(w
->vrf_id
);
2551 XFREE(MTYPE_WQ_WRAPPER
, w
);
2556 case WQ_LABEL_FTN_UNINSTALL
:
2557 zebra_mpls_ftn_uninstall(zvrf
, w
->ltype
, &w
->p
, w
->route_type
,
2560 case WQ_LABEL_LABELS_PROCESS
:
2561 zebra_mpls_zapi_labels_process(w
->add_p
, zvrf
, &w
->zl
);
2565 XFREE(MTYPE_WQ_WRAPPER
, w
);
2568 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2570 struct route_node
*rnode
= NULL
;
2571 rib_dest_t
*dest
= NULL
;
2572 struct zebra_vrf
*zvrf
= NULL
;
2574 rnode
= listgetdata(lnode
);
2575 dest
= rib_dest_from_rnode(rnode
);
2578 zvrf
= rib_dest_vrf(dest
);
2582 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2583 struct route_entry
*re
= NULL
;
2586 * rib_process may have freed the dest
2587 * as part of the garbage collection. Let's
2588 * prevent stupidity from happening.
2590 dest
= rib_dest_from_rnode(rnode
);
2592 re
= re_list_first(&dest
->routes
);
2594 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2595 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2596 rnode
, rnode
, subqueue2str(qindex
));
2600 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2601 RIB_ROUTE_QUEUED(qindex
));
2603 route_unlock_node(rnode
);
2606 static void rib_re_nhg_free(struct route_entry
*re
)
2608 if (re
->nhe
&& re
->nhe_id
) {
2609 assert(re
->nhe
->id
== re
->nhe_id
);
2610 route_entry_update_nhe(re
, NULL
);
2611 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2612 nexthops_free(re
->nhe
->nhg
.nexthop
);
2614 nexthops_free(re
->fib_ng
.nexthop
);
2617 struct zebra_early_route
{
2621 struct prefix_ipv6 src_p
;
2622 bool src_p_provided
;
2623 struct route_entry
*re
;
2624 struct nhg_hash_entry
*re_nhe
;
2630 static void early_route_memory_free(struct zebra_early_route
*ere
)
2633 zebra_nhg_free(ere
->re_nhe
);
2635 XFREE(MTYPE_RE
, ere
->re
);
2636 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2639 static void process_subq_early_route_add(struct zebra_early_route
*ere
)
2641 struct route_entry
*re
= ere
->re
;
2642 struct route_table
*table
;
2643 struct nhg_hash_entry
*nhe
= NULL
;
2644 struct route_node
*rn
;
2645 struct route_entry
*same
= NULL
, *first_same
= NULL
;
2650 table
= zebra_vrf_get_table_with_table_id(ere
->afi
, ere
->safi
,
2651 re
->vrf_id
, re
->table
);
2653 early_route_memory_free(ere
);
2657 if (re
->nhe_id
> 0) {
2658 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2662 * We've received from the kernel a nexthop id
2663 * that we don't have saved yet. More than likely
2664 * it has not been processed and is on the
2665 * queue to be processed. Let's stop what we
2666 * are doing and cause the meta q to be processed
2667 * storing this for later.
2669 * This is being done this way because zebra
2670 * runs with the assumption t
2673 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2674 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2675 re
->nhe_id
, &ere
->p
);
2677 early_route_memory_free(ere
);
2681 /* Lookup nhe from route information */
2682 nhe
= zebra_nhg_rib_find_nhe(ere
->re_nhe
, ere
->afi
);
2684 char buf2
[PREFIX_STRLEN
] = "";
2687 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2688 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2689 &ere
->p
, ere
->src_p_provided
? " from " : "",
2691 ? prefix2str(&ere
->src_p
, buf2
,
2695 early_route_memory_free(ere
);
2701 * Attach the re to the nhe's nexthop group.
2703 * TODO: This will need to change when we start getting IDs from upper
2704 * level protocols, as the refcnt might be wrong, since it checks
2705 * if old_id != new_id.
2707 route_entry_update_nhe(re
, nhe
);
2709 /* Make it sure prefixlen is applied to the prefix. */
2710 apply_mask(&ere
->p
);
2711 if (ere
->src_p_provided
)
2712 apply_mask_ipv6(&ere
->src_p
);
2714 /* Set default distance by route type. */
2715 if (re
->distance
== 0)
2716 re
->distance
= route_distance(re
->type
);
2718 /* Lookup route node.*/
2719 rn
= srcdest_rnode_get(table
, &ere
->p
,
2720 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2723 * If same type of route are installed, treat it as a implicit
2724 * withdraw. If the user has specified the No route replace semantics
2725 * for the install don't do a route replace.
2727 RNODE_FOREACH_RE (rn
, same
) {
2728 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
2733 /* Compare various route_entry properties */
2734 if (rib_compare_routes(re
, same
)) {
2737 if (first_same
== NULL
)
2744 if (!ere
->startup
&& (re
->flags
& ZEBRA_FLAG_SELFROUTE
) &&
2745 zrouter
.asic_offloaded
) {
2747 if (IS_ZEBRA_DEBUG_RIB
)
2749 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2752 * We are not on startup, this is a self route
2753 * and we have asic offload. Which means
2754 * we are getting a callback for a entry
2755 * that was already deleted to the kernel
2756 * but an earlier response was just handed
2757 * back. Drop it on the floor
2759 early_route_memory_free(ere
);
2764 /* If this route is kernel/connected route, notify the dataplane. */
2765 if (RIB_SYSTEM_ROUTE(re
)) {
2766 /* Notify dataplane */
2767 dplane_sys_route_add(rn
, re
);
2770 /* Link new re to node.*/
2771 if (IS_ZEBRA_DEBUG_RIB
) {
2774 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2775 rn
, re
, zebra_route_string(re
->type
), same
, same_count
);
2777 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2780 ere
->src_p_provided
? &ere
->src_p
: NULL
, re
);
2783 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2784 rib_addnode(rn
, re
, 1);
2786 /* Free implicit route.*/
2788 rib_delnode(rn
, same
);
2790 /* See if we can remove some RE entries that are queued for
2791 * removal, but won't be considered in rib processing.
2793 dest
= rib_dest_from_rnode(rn
);
2794 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
2795 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2796 /* If the route was used earlier, must retain it. */
2797 if (dest
&& re
== dest
->selected_fib
)
2800 if (IS_ZEBRA_DEBUG_RIB
)
2801 rnode_debug(rn
, re
->vrf_id
,
2802 "rn %p, removing unneeded re %p",
2809 route_unlock_node(rn
);
2811 zebra_nhg_free(ere
->re_nhe
);
2812 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2815 static void process_subq_early_route_delete(struct zebra_early_route
*ere
)
2817 struct route_table
*table
;
2818 struct route_node
*rn
;
2819 struct route_entry
*re
;
2820 struct route_entry
*fib
= NULL
;
2821 struct route_entry
*same
= NULL
;
2822 struct nexthop
*rtnh
;
2823 char buf2
[INET6_ADDRSTRLEN
];
2826 if (ere
->src_p_provided
)
2827 assert(!ere
->src_p
.prefixlen
|| ere
->afi
== AFI_IP6
);
2830 table
= zebra_vrf_lookup_table_with_table_id(
2831 ere
->afi
, ere
->safi
, ere
->re
->vrf_id
, ere
->re
->table
);
2833 early_route_memory_free(ere
);
2838 apply_mask(&ere
->p
);
2839 if (ere
->src_p_provided
)
2840 apply_mask_ipv6(&ere
->src_p
);
2842 /* Lookup route node. */
2843 rn
= srcdest_rnode_lookup(table
, &ere
->p
,
2844 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2846 if (IS_ZEBRA_DEBUG_RIB
) {
2847 char src_buf
[PREFIX_STRLEN
];
2848 struct vrf
*vrf
= vrf_lookup_by_id(ere
->re
->vrf_id
);
2850 if (ere
->src_p_provided
&& ere
->src_p
.prefixlen
)
2851 prefix2str(&ere
->src_p
, src_buf
,
2856 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2857 vrf
->name
, ere
->re
->table
, rn
,
2858 (src_buf
[0] != '\0') ? " from " : "",
2861 early_route_memory_free(ere
);
2865 dest
= rib_dest_from_rnode(rn
);
2866 fib
= dest
->selected_fib
;
2868 struct nexthop
*nh
= NULL
;
2871 nh
= ere
->re
->nhe
->nhg
.nexthop
;
2873 /* Lookup same type route. */
2874 RNODE_FOREACH_RE (rn
, re
) {
2875 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2878 if (re
->type
!= ere
->re
->type
)
2880 if (re
->instance
!= ere
->re
->instance
)
2882 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2883 ere
->re
->distance
!= re
->distance
)
2886 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2887 re
->metric
!= ere
->re
->metric
)
2889 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= nh
) &&
2890 rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2891 if (rtnh
->ifindex
!= nh
->ifindex
)
2897 /* Make sure that the route found has the same gateway. */
2898 if (ere
->re
->nhe_id
&& re
->nhe_id
== ere
->re
->nhe_id
) {
2907 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
2909 * No guarantee all kernel send nh with labels
2912 if (nexthop_same_no_labels(rtnh
, nh
)) {
2922 * If same type of route can't be found and this message is from
2927 * In the past(HA!) we could get here because
2928 * we were receiving a route delete from the
2929 * kernel and we're not marking the proto
2930 * as coming from it's appropriate originator.
2931 * Now that we are properly noticing the fact
2932 * that the kernel has deleted our route we
2933 * are not going to get called in this path
2934 * I am going to leave this here because
2935 * this might still work this way on non-linux
2936 * platforms as well as some weird state I have
2937 * not properly thought of yet.
2938 * If we can show that this code path is
2939 * dead then we can remove it.
2941 if (fib
&& CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2942 if (IS_ZEBRA_DEBUG_RIB
) {
2944 rn
, ere
->re
->vrf_id
,
2945 "rn %p, re %p (%s) was deleted from kernel, adding",
2946 rn
, fib
, zebra_route_string(fib
->type
));
2948 if (zrouter
.allow_delete
||
2949 CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
2950 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2952 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
2954 UNSET_FLAG(rtnh
->flags
,
2958 * This is a non FRR route
2959 * as such we should mark
2962 dest
->selected_fib
= NULL
;
2965 * This means someone else, other than Zebra,
2966 * has deleted a Zebra router from the kernel.
2967 * We will add it back
2969 rib_install_kernel(rn
, fib
, NULL
);
2972 if (IS_ZEBRA_DEBUG_RIB
) {
2975 rn
, ere
->re
->vrf_id
,
2976 "via %s ifindex %d type %d doesn't exist in rib",
2977 inet_ntop(afi2family(ere
->afi
),
2980 nh
->ifindex
, ere
->re
->type
);
2983 rn
, ere
->re
->vrf_id
,
2984 "type %d doesn't exist in rib",
2987 route_unlock_node(rn
);
2988 early_route_memory_free(ere
);
2994 struct nexthop
*tmp_nh
;
2996 if (ere
->fromkernel
&&
2997 CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
) &&
2998 !zrouter
.allow_delete
) {
2999 rib_install_kernel(rn
, same
, NULL
);
3000 route_unlock_node(rn
);
3002 early_route_memory_free(ere
);
3006 /* Special handling for IPv4 or IPv6 routes sourced from
3007 * EVPN - the nexthop (and associated MAC) need to be
3008 * uninstalled if no more refs.
3010 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3011 struct ipaddr vtep_ip
;
3013 if (CHECK_FLAG(tmp_nh
->flags
, NEXTHOP_FLAG_EVPN
)) {
3014 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3015 if (ere
->afi
== AFI_IP
) {
3016 vtep_ip
.ipa_type
= IPADDR_V4
;
3017 memcpy(&(vtep_ip
.ipaddr_v4
),
3018 &(tmp_nh
->gate
.ipv4
),
3019 sizeof(struct in_addr
));
3021 vtep_ip
.ipa_type
= IPADDR_V6
;
3022 memcpy(&(vtep_ip
.ipaddr_v6
),
3023 &(tmp_nh
->gate
.ipv6
),
3024 sizeof(struct in6_addr
));
3026 zebra_rib_queue_evpn_route_del(
3027 re
->vrf_id
, &vtep_ip
, &ere
->p
);
3031 /* Notify dplane if system route changes */
3032 if (RIB_SYSTEM_ROUTE(re
))
3033 dplane_sys_route_del(rn
, same
);
3035 rib_delnode(rn
, same
);
3038 route_unlock_node(rn
);
3040 early_route_memory_free(ere
);
3044 * When FRR receives a route we need to match the route up to
3045 * nexthop groups. That we also may have just received
3046 * place the data on this queue so that this work of finding
3047 * the nexthop group entries for the route entry is always
3048 * done after the nexthop group has had a chance to be processed
3050 static void process_subq_early_route(struct listnode
*lnode
)
3052 struct zebra_early_route
*ere
= listgetdata(lnode
);
3055 process_subq_early_route_delete(ere
);
3057 process_subq_early_route_add(ere
);
3061 * Examine the specified subqueue; process one entry and return 1 if
3062 * there is a node, return 0 otherwise.
3064 static unsigned int process_subq(struct list
*subq
,
3065 enum meta_queue_indexes qindex
)
3067 struct listnode
*lnode
= listhead(subq
);
3073 case META_QUEUE_EVPN
:
3074 process_subq_evpn(lnode
);
3076 case META_QUEUE_NHG
:
3077 process_subq_nhg(lnode
);
3079 case META_QUEUE_EARLY_ROUTE
:
3080 process_subq_early_route(lnode
);
3082 case META_QUEUE_EARLY_LABEL
:
3083 process_subq_early_label(lnode
);
3085 case META_QUEUE_CONNECTED
:
3086 case META_QUEUE_KERNEL
:
3087 case META_QUEUE_STATIC
:
3088 case META_QUEUE_NOTBGP
:
3089 case META_QUEUE_BGP
:
3090 case META_QUEUE_OTHER
:
3091 process_subq_route(lnode
, qindex
);
3095 list_delete_node(subq
, lnode
);
3100 /* Dispatch the meta queue by picking and processing the next node from
3101 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3102 * data is pointed to the meta queue structure.
3104 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
3106 struct meta_queue
*mq
= data
;
3108 uint32_t queue_len
, queue_limit
;
3110 /* Ensure there's room for more dataplane updates */
3111 queue_limit
= dplane_get_in_queue_limit();
3112 queue_len
= dplane_get_in_queue_len();
3113 if (queue_len
> queue_limit
) {
3114 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3116 "rib queue: dplane queue len %u, limit %u, retrying",
3117 queue_len
, queue_limit
);
3119 /* Ensure that the meta-queue is actually enqueued */
3120 if (work_queue_empty(zrouter
.ribq
))
3121 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3123 return WQ_QUEUE_BLOCKED
;
3126 for (i
= 0; i
< MQ_SIZE
; i
++)
3127 if (process_subq(mq
->subq
[i
], i
)) {
3131 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
3136 * Look into the RN and queue it into the highest priority queue
3137 * at this point in time for processing.
3139 * We will enqueue a route node only once per invocation.
3141 * There are two possibilities here that should be kept in mind.
3142 * If the original invocation has not been pulled off for processing
3143 * yet, A subsuquent invocation can have a route entry with a better
3144 * meta queue index value and we can have a situation where
3145 * we might have the same node enqueued 2 times. Not necessarily
3146 * an optimal situation but it should be ok.
3148 * The other possibility is that the original invocation has not
3149 * been pulled off for processing yet, A subsusquent invocation
3150 * doesn't have a route_entry with a better meta-queue and the
3151 * original metaqueue index value will win and we'll end up with
3152 * the route node enqueued once.
3154 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
3156 struct route_node
*rn
= NULL
;
3157 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
3158 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
3160 rn
= (struct route_node
*)data
;
3162 RNODE_FOREACH_RE (rn
, curr_re
) {
3163 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
3165 if (curr_qindex
<= qindex
) {
3167 qindex
= curr_qindex
;
3174 /* Invariant: at this point we always have rn->info set. */
3175 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3176 RIB_ROUTE_QUEUED(qindex
))) {
3177 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3178 rnode_debug(rn
, re
->vrf_id
,
3179 "rn %p is already queued in sub-queue %s",
3180 (void *)rn
, subqueue2str(qindex
));
3184 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
3185 listnode_add(mq
->subq
[qindex
], rn
);
3186 route_lock_node(rn
);
3189 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3190 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %s",
3191 (void *)rn
, subqueue2str(qindex
));
3196 static int early_label_meta_queue_add(struct meta_queue
*mq
, void *data
)
3198 listnode_add(mq
->subq
[META_QUEUE_EARLY_LABEL
], data
);
3203 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
3205 struct nhg_ctx
*ctx
= NULL
;
3206 uint8_t qindex
= META_QUEUE_NHG
;
3207 struct wq_nhg_wrapper
*w
;
3209 ctx
= (struct nhg_ctx
*)data
;
3214 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3216 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
3219 listnode_add(mq
->subq
[qindex
], w
);
3222 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3223 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3224 ctx
->id
, subqueue2str(qindex
));
3229 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
3231 struct nhg_hash_entry
*nhe
= NULL
;
3232 uint8_t qindex
= META_QUEUE_NHG
;
3233 struct wq_nhg_wrapper
*w
;
3235 nhe
= (struct nhg_hash_entry
*)data
;
3240 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3242 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
3245 listnode_add(mq
->subq
[qindex
], w
);
3248 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3249 zlog_debug("NHG id=%u queued into sub-queue %s", nhe
->id
,
3250 subqueue2str(qindex
));
3255 static int rib_meta_queue_evpn_add(struct meta_queue
*mq
, void *data
)
3257 listnode_add(mq
->subq
[META_QUEUE_EVPN
], data
);
3263 static int mq_add_handler(void *data
,
3264 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
3266 if (zrouter
.ribq
== NULL
) {
3267 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3268 "%s: work_queue does not exist!", __func__
);
3273 * The RIB queue should normally be either empty or holding the only
3274 * work_queue_item element. In the latter case this element would
3275 * hold a pointer to the meta queue structure, which must be used to
3276 * actually queue the route nodes to process. So create the MQ
3277 * holder, if necessary, then push the work into it in any case.
3278 * This semantics was introduced after 0.99.9 release.
3280 if (work_queue_empty(zrouter
.ribq
))
3281 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3283 return mq_add_func(zrouter
.mq
, data
);
3286 void mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3287 struct prefix
*prefix
, uint8_t route_type
,
3288 uint8_t route_instance
)
3290 struct wq_label_wrapper
*w
;
3292 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3294 w
->type
= WQ_LABEL_FTN_UNINSTALL
;
3295 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3298 w
->route_type
= route_type
;
3299 w
->route_instance
= route_instance
;
3301 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3302 zlog_debug("Early Label Handling for %pFX", prefix
);
3304 mq_add_handler(w
, early_label_meta_queue_add
);
3307 void mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
3308 const struct zapi_labels
*zl
)
3310 struct wq_label_wrapper
*w
;
3312 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3313 w
->type
= WQ_LABEL_LABELS_PROCESS
;
3314 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3318 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3319 zlog_debug("Early Label Handling: Labels Process");
3321 mq_add_handler(w
, early_label_meta_queue_add
);
3324 /* Add route_node to work queue and schedule processing */
3325 int rib_queue_add(struct route_node
*rn
)
3329 /* Pointless to queue a route_node with no RIB entries to add or remove
3331 if (!rnode_to_ribs(rn
)) {
3332 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3333 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
3334 zlog_backtrace(LOG_DEBUG
);
3338 return mq_add_handler(rn
, rib_meta_queue_add
);
3342 * Enqueue incoming nhg info from OS for processing
3344 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
3348 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
3352 * Enqueue incoming nhg from proto daemon for processing
3354 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
3359 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
3363 * Enqueue evpn route for processing
3365 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id
, const struct ethaddr
*rmac
,
3366 const struct ipaddr
*vtep_ip
,
3367 const struct prefix
*host_prefix
)
3369 struct wq_evpn_wrapper
*w
;
3371 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3373 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3378 w
->prefix
= *host_prefix
;
3380 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3381 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3382 vrf_id
, vtep_ip
, host_prefix
);
3384 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3387 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id
,
3388 const struct ipaddr
*vtep_ip
,
3389 const struct prefix
*host_prefix
)
3391 struct wq_evpn_wrapper
*w
;
3393 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3395 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3399 w
->prefix
= *host_prefix
;
3401 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3402 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3403 vrf_id
, vtep_ip
, host_prefix
);
3405 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3408 /* Enqueue EVPN remote ES for processing */
3409 int zebra_rib_queue_evpn_rem_es_add(const esi_t
*esi
,
3410 const struct in_addr
*vtep_ip
,
3411 bool esr_rxed
, uint8_t df_alg
,
3414 struct wq_evpn_wrapper
*w
;
3415 char buf
[ESI_STR_LEN
];
3417 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3419 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3422 w
->ip
.ipa_type
= IPADDR_V4
;
3423 w
->ip
.ipaddr_v4
= *vtep_ip
;
3424 w
->esr_rxed
= esr_rxed
;
3426 w
->df_pref
= df_pref
;
3428 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3429 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3430 esi_to_str(esi
, buf
, sizeof(buf
)));
3432 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3435 int zebra_rib_queue_evpn_rem_es_del(const esi_t
*esi
,
3436 const struct in_addr
*vtep_ip
)
3438 struct wq_evpn_wrapper
*w
;
3439 char buf
[ESI_STR_LEN
];
3441 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3443 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3446 w
->ip
.ipa_type
= IPADDR_V4
;
3447 w
->ip
.ipaddr_v4
= *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: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3459 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3463 * Enqueue EVPN remote macip update for processing
3465 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni
, const struct ethaddr
*macaddr
,
3466 const struct ipaddr
*ipaddr
,
3467 uint8_t flags
, uint32_t seq
,
3468 struct in_addr vtep_ip
, const esi_t
*esi
)
3470 struct wq_evpn_wrapper
*w
;
3471 char buf
[ESI_STR_LEN
];
3473 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3475 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3478 w
->macaddr
= *macaddr
;
3482 w
->vtep_ip
= vtep_ip
;
3485 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3486 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3487 esi_to_str(esi
, buf
, sizeof(buf
));
3489 strlcpy(buf
, "-", sizeof(buf
));
3491 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__
,
3492 macaddr
, &vtep_ip
, buf
);
3495 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3498 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni
, const struct ethaddr
*macaddr
,
3499 const struct ipaddr
*ip
,
3500 struct in_addr vtep_ip
)
3502 struct wq_evpn_wrapper
*w
;
3504 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3506 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3509 w
->macaddr
= *macaddr
;
3511 w
->vtep_ip
= vtep_ip
;
3513 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3514 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__
,
3517 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3521 * Enqueue remote VTEP address for processing
3523 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id
, vni_t vni
,
3524 struct in_addr vtep_ip
, int flood_control
)
3526 struct wq_evpn_wrapper
*w
;
3528 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3530 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3534 w
->vtep_ip
= vtep_ip
;
3535 w
->flags
= flood_control
;
3537 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3538 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3541 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3544 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id
, vni_t vni
,
3545 struct in_addr vtep_ip
)
3547 struct wq_evpn_wrapper
*w
;
3549 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3551 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3555 w
->vtep_ip
= vtep_ip
;
3557 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3558 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3561 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3564 /* Create new meta queue.
3565 A destructor function doesn't seem to be necessary here.
3567 static struct meta_queue
*meta_queue_new(void)
3569 struct meta_queue
*new;
3572 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
3574 for (i
= 0; i
< MQ_SIZE
; i
++) {
3575 new->subq
[i
] = list_new();
3576 assert(new->subq
[i
]);
3582 /* Clean up the EVPN meta-queue list */
3583 static void evpn_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3584 struct zebra_vrf
*zvrf
)
3586 struct listnode
*node
, *nnode
;
3587 struct wq_evpn_wrapper
*w
;
3589 /* Free the node wrapper object, and the struct it wraps */
3590 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3592 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3594 if (w
->vrf_id
!= vrf_id
)
3600 XFREE(MTYPE_WQ_WRAPPER
, w
);
3602 list_delete_node(l
, node
);
3607 /* Clean up the nhg meta-queue list */
3608 static void nhg_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3609 struct zebra_vrf
*zvrf
)
3611 struct wq_nhg_wrapper
*w
;
3612 struct listnode
*node
, *nnode
;
3614 /* Free the node wrapper object, and the struct it wraps */
3615 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3617 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3619 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
&&
3620 w
->u
.ctx
->vrf_id
!= vrf_id
)
3622 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
&&
3623 w
->u
.nhe
->vrf_id
!= vrf_id
)
3626 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
3627 nhg_ctx_free(&w
->u
.ctx
);
3628 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
3629 zebra_nhg_free(w
->u
.nhe
);
3632 XFREE(MTYPE_WQ_WRAPPER
, w
);
3634 list_delete_node(l
, node
);
3639 static void early_label_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3640 struct zebra_vrf
*zvrf
)
3642 struct wq_label_wrapper
*w
;
3643 struct listnode
*node
, *nnode
;
3645 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3646 if (zvrf
&& zvrf
->vrf
->vrf_id
!= w
->vrf_id
)
3650 case WQ_LABEL_FTN_UNINSTALL
:
3651 case WQ_LABEL_LABELS_PROCESS
:
3656 XFREE(MTYPE_WQ_WRAPPER
, w
);
3657 list_delete_node(l
, node
);
3662 static void rib_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3663 struct zebra_vrf
*zvrf
)
3665 struct route_node
*rnode
;
3666 struct listnode
*node
, *nnode
;
3668 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, rnode
)) {
3669 rib_dest_t
*dest
= rib_dest_from_rnode(rnode
);
3671 if (dest
&& rib_dest_vrf(dest
) != zvrf
)
3674 route_unlock_node(rnode
);
3676 list_delete_node(l
, node
);
3681 static void early_route_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3682 struct zebra_vrf
*zvrf
)
3684 struct zebra_early_route
*ere
;
3685 struct listnode
*node
, *nnode
;
3687 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, ere
)) {
3688 if (zvrf
&& ere
->re
->vrf_id
!= zvrf
->vrf
->vrf_id
)
3691 early_route_memory_free(ere
);
3693 list_delete_node(l
, node
);
3698 void meta_queue_free(struct meta_queue
*mq
, struct zebra_vrf
*zvrf
)
3700 enum meta_queue_indexes i
;
3702 for (i
= 0; i
< MQ_SIZE
; i
++) {
3703 /* Some subqueues may need cleanup - nhgs for example */
3705 case META_QUEUE_NHG
:
3706 nhg_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3708 case META_QUEUE_EVPN
:
3709 evpn_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3711 case META_QUEUE_EARLY_ROUTE
:
3712 early_route_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3714 case META_QUEUE_EARLY_LABEL
:
3715 early_label_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3717 case META_QUEUE_CONNECTED
:
3718 case META_QUEUE_KERNEL
:
3719 case META_QUEUE_STATIC
:
3720 case META_QUEUE_NOTBGP
:
3721 case META_QUEUE_BGP
:
3722 case META_QUEUE_OTHER
:
3723 rib_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3727 list_delete(&mq
->subq
[i
]);
3731 XFREE(MTYPE_WORK_QUEUE
, mq
);
3734 /* initialise zebra rib work queue */
3735 static void rib_queue_init(void)
3737 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
3738 "route_node processing"))) {
3739 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3740 "%s: could not initialise work queue!", __func__
);
3744 /* fill in the work queue spec */
3745 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
3746 zrouter
.ribq
->spec
.completion_func
= NULL
;
3747 /* XXX: TODO: These should be runtime configurable via vty */
3748 zrouter
.ribq
->spec
.max_retries
= 3;
3749 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
3750 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
3752 if (!(zrouter
.mq
= meta_queue_new())) {
3753 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3754 "%s: could not initialise meta queue!", __func__
);
3760 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
3764 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
3765 rnh_list_init(&dest
->nht
);
3766 re_list_init(&dest
->routes
);
3767 route_lock_node(rn
); /* rn route table reference */
3774 /* RIB updates are processed via a queue of pointers to route_nodes.
3776 * The queue length is bounded by the maximal size of the routing table,
3777 * as a route_node will not be requeued, if already queued.
3779 * REs are submitted via rib_addnode or rib_delnode which set minimal
3780 * state, or static_install_route (when an existing RE is updated)
3781 * and then submit route_node to queue for best-path selection later.
3782 * Order of add/delete state changes are preserved for any given RE.
3784 * Deleted REs are reaped during best-path selection.
3787 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3788 * |-------->| | best RE, if required
3790 * static_install->|->rib_addqueue...... -> rib_process
3792 * |-------->| |-> rib_unlink
3793 * |-> set ROUTE_ENTRY_REMOVE |
3794 * rib_delnode (RE freed)
3796 * The 'info' pointer of a route_node points to a rib_dest_t
3797 * ('dest'). Queueing state for a route_node is kept on the dest. The
3798 * dest is created on-demand by rib_link() and is kept around at least
3799 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3801 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3803 * - route_nodes: refcounted by:
3804 * - dest attached to route_node:
3805 * - managed by: rib_link/rib_gc_dest
3806 * - route_node processing queue
3807 * - managed by: rib_addqueue, rib_process.
3811 /* Add RE to head of the route node. */
3812 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
3816 const char *rmap_name
;
3820 dest
= rib_dest_from_rnode(rn
);
3822 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3823 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
3825 dest
= zebra_rib_create_dest(rn
);
3828 re_list_add_head(&dest
->routes
, re
);
3830 afi
= (rn
->p
.family
== AF_INET
)
3832 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3833 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3834 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3836 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
3837 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
3842 static void rib_addnode(struct route_node
*rn
,
3843 struct route_entry
*re
, int process
)
3845 /* RE node has been un-removed before route-node is processed.
3846 * route_node must hence already be on the queue for processing..
3848 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3849 if (IS_ZEBRA_DEBUG_RIB
)
3850 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
3851 (void *)rn
, (void *)re
);
3853 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3856 rib_link(rn
, re
, process
);
3862 * Detach a rib structure from a route_node.
3864 * Note that a call to rib_unlink() should be followed by a call to
3865 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3866 * longer required to be deleted.
3868 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
3874 if (IS_ZEBRA_DEBUG_RIB
)
3875 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
3878 dest
= rib_dest_from_rnode(rn
);
3880 re_list_del(&dest
->routes
, re
);
3882 if (dest
->selected_fib
== re
)
3883 dest
->selected_fib
= NULL
;
3885 rib_re_nhg_free(re
);
3887 zapi_re_opaque_free(re
->opaque
);
3889 XFREE(MTYPE_RE
, re
);
3892 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
3896 if (IS_ZEBRA_DEBUG_RIB
)
3897 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
3898 (void *)rn
, (void *)re
);
3899 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3901 afi
= (rn
->p
.family
== AF_INET
)
3903 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3904 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3905 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3907 zebra_del_import_table_entry(zvrf
, rn
, re
);
3908 /* Just clean up if non main table */
3909 if (IS_ZEBRA_DEBUG_RIB
)
3910 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3911 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, rn
,
3912 rn
, re
, zebra_route_string(re
->type
));
3921 * Helper that debugs a single nexthop within a route-entry
3923 static void _route_entry_dump_nh(const struct route_entry
*re
,
3924 const char *straddr
,
3925 const struct nexthop
*nexthop
)
3927 char nhname
[PREFIX_STRLEN
];
3928 char backup_str
[50];
3931 char label_str
[MPLS_LABEL_STRLEN
];
3933 struct interface
*ifp
;
3934 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
3936 switch (nexthop
->type
) {
3937 case NEXTHOP_TYPE_BLACKHOLE
:
3938 snprintf(nhname
, sizeof(nhname
), "Blackhole");
3940 case NEXTHOP_TYPE_IFINDEX
:
3941 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
3942 snprintf(nhname
, sizeof(nhname
), "%s",
3943 ifp
? ifp
->name
: "Unknown");
3945 case NEXTHOP_TYPE_IPV4
:
3947 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3948 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3950 case NEXTHOP_TYPE_IPV6
:
3951 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3952 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3957 label_str
[0] = '\0';
3958 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
3959 mpls_label2str(nexthop
->nh_label
->num_labels
,
3960 nexthop
->nh_label
->label
, label_str
,
3961 sizeof(label_str
), 0 /*pretty*/);
3962 strlcat(label_str
, ", ", sizeof(label_str
));
3965 backup_str
[0] = '\0';
3966 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3967 snprintf(backup_str
, sizeof(backup_str
), "backup ");
3968 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
3969 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
3970 nexthop
->backup_idx
[i
]);
3971 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
3976 if (nexthop
->weight
)
3977 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
3979 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
3980 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
3981 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
3983 wgt_str
, backup_str
,
3984 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
3987 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
3990 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
3993 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
3996 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
3999 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
4000 ? "FILTERED " : ""),
4001 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
4003 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
4005 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)
4010 /* This function dumps the contents of a given RE entry into
4011 * standard debug log. Calling function name and IP prefix in
4012 * question are passed as 1st and 2nd arguments.
4014 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
4015 union prefixconstptr src_pp
,
4016 const struct route_entry
*re
)
4018 const struct prefix
*src_p
= src_pp
.p
;
4019 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
4020 char straddr
[PREFIX_STRLEN
];
4021 char srcaddr
[PREFIX_STRLEN
];
4022 char flags_buf
[128];
4023 char status_buf
[128];
4024 struct nexthop
*nexthop
;
4025 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
4026 struct nexthop_group
*nhg
;
4028 prefix2str(pp
, straddr
, sizeof(straddr
));
4030 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
4031 (const void *)re
, straddr
,
4032 is_srcdst
? " from " : "",
4033 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
4035 VRF_LOGNAME(vrf
), re
->vrf_id
);
4036 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4037 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
4040 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4041 straddr
, re
->metric
, re
->mtu
, re
->distance
,
4042 zclient_dump_route_flags(re
->flags
, flags_buf
,
4044 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
4045 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
4046 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
4047 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
4050 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4051 _route_entry_dump_nh(re
, straddr
, nexthop
);
4053 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
4054 zlog_debug("%s: backup nexthops:", straddr
);
4056 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
4057 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
4058 _route_entry_dump_nh(re
, straddr
, nexthop
);
4061 zlog_debug("%s: dump complete", straddr
);
4064 static int rib_meta_queue_early_route_add(struct meta_queue
*mq
, void *data
)
4066 struct zebra_early_route
*ere
= data
;
4068 listnode_add(mq
->subq
[META_QUEUE_EARLY_ROUTE
], data
);
4071 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
4073 "Route %pFX(%u) queued for processing into sub-queue %s",
4074 &ere
->p
, ere
->re
->vrf_id
,
4075 subqueue2str(META_QUEUE_EARLY_ROUTE
));
4080 struct route_entry
*zebra_rib_route_entry_new(vrf_id_t vrf_id
, int type
,
4081 uint8_t instance
, uint32_t flags
,
4084 uint32_t metric
, uint32_t mtu
,
4085 uint8_t distance
, route_tag_t tag
)
4087 struct route_entry
*re
;
4089 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
4091 re
->instance
= instance
;
4092 re
->distance
= distance
;
4094 re
->metric
= metric
;
4096 re
->table
= table_id
;
4097 re
->vrf_id
= vrf_id
;
4098 re
->uptime
= monotime(NULL
);
4100 re
->nhe_id
= nhe_id
;
4105 * Internal route-add implementation; there are a couple of different public
4106 * signatures. Callers in this path are responsible for the memory they
4107 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4108 * must free those objects. If this returns < 0, an error has occurred and the
4109 * route_entry 're' has not been captured; the caller should free that also.
4115 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
4116 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4117 struct nhg_hash_entry
*re_nhe
, bool startup
)
4119 struct zebra_early_route
*ere
;
4124 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
4126 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4131 ere
->src_p
= *src_p
;
4132 ere
->src_p_provided
= !!src_p
;
4134 ere
->re_nhe
= re_nhe
;
4135 ere
->startup
= startup
;
4137 return mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4141 * Add a single route.
4143 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
4144 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4145 struct nexthop_group
*ng
, bool startup
)
4148 struct nhg_hash_entry nhe
, *n
;
4153 /* We either need nexthop(s) or an existing nexthop id */
4154 if (ng
== NULL
&& re
->nhe_id
== 0)
4158 * Use a temporary nhe to convey info to the common/main api.
4160 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
4162 nhe
.nhg
.nexthop
= ng
->nexthop
;
4163 else if (re
->nhe_id
> 0)
4164 nhe
.id
= re
->nhe_id
;
4166 n
= zebra_nhe_copy(&nhe
, 0);
4167 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, n
, startup
);
4169 /* In error cases, free the route also */
4171 XFREE(MTYPE_RE
, re
);
4176 void rib_delete(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
,
4180 uint8_t distance
, bool fromkernel
)
4182 struct zebra_early_route
*ere
;
4183 struct route_entry
*re
= NULL
;
4184 struct nhg_hash_entry
*nhe
= NULL
;
4186 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4187 table_id
, metric
, 0, distance
, 0);
4190 nhe
= zebra_nhg_alloc();
4191 nhe
->nhg
.nexthop
= nexthop_dup(nh
, NULL
);
4194 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4199 ere
->src_p
= *src_p
;
4200 ere
->src_p_provided
= !!src_p
;
4203 ere
->startup
= false;
4204 ere
->deletion
= true;
4205 ere
->fromkernel
= fromkernel
;
4207 mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4211 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4212 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4213 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4214 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
4215 uint8_t distance
, route_tag_t tag
, bool startup
)
4217 struct route_entry
*re
= NULL
;
4218 struct nexthop nexthop
= {};
4219 struct nexthop_group ng
= {};
4221 /* Allocate new route_entry structure. */
4222 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4223 table_id
, metric
, mtu
, distance
, tag
);
4225 /* If the owner of the route supplies a shared nexthop-group id,
4226 * we'll use that. Otherwise, pass the nexthop along directly.
4231 nexthop_group_add_sorted(&ng
, &nexthop
);
4234 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, &ng
, startup
);
4237 static const char *rib_update_event2str(enum rib_update_event event
)
4239 const char *ret
= "UNKNOWN";
4242 case RIB_UPDATE_KERNEL
:
4243 ret
= "RIB_UPDATE_KERNEL";
4245 case RIB_UPDATE_RMAP_CHANGE
:
4246 ret
= "RIB_UPDATE_RMAP_CHANGE";
4248 case RIB_UPDATE_OTHER
:
4249 ret
= "RIB_UPDATE_OTHER";
4251 case RIB_UPDATE_MAX
:
4259 /* Schedule route nodes to be processed if they match the type */
4260 static void rib_update_route_node(struct route_node
*rn
, int type
)
4262 struct route_entry
*re
, *next
;
4263 bool re_changed
= false;
4265 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4266 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
4267 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
4276 /* Schedule routes of a particular table (address-family) based on event. */
4277 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
4280 struct route_node
*rn
;
4282 if (IS_ZEBRA_DEBUG_EVENT
) {
4283 struct zebra_vrf
*zvrf
;
4287 ? ((struct rib_table_info
*)table
->info
)->zvrf
4289 vrf
= zvrf
? zvrf
->vrf
: NULL
;
4291 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
4292 table
->info
? afi2str(
4293 ((struct rib_table_info
*)table
->info
)->afi
)
4295 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
4296 rib_update_event2str(event
), zebra_route_string(rtype
));
4299 /* Walk all routes and queue for processing, if appropriate for
4300 * the trigger event.
4302 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4304 * If we are looking at a route node and the node
4305 * has already been queued we don't
4306 * need to queue it up again
4309 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
4310 RIB_ROUTE_ANY_QUEUED
))
4314 case RIB_UPDATE_KERNEL
:
4315 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
4317 case RIB_UPDATE_RMAP_CHANGE
:
4318 case RIB_UPDATE_OTHER
:
4319 rib_update_route_node(rn
, rtype
);
4327 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
,
4330 struct route_table
*table
;
4332 if (IS_ZEBRA_DEBUG_EVENT
)
4333 zlog_debug("%s: Handling VRF %s event %s", __func__
,
4334 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
4336 /* Process routes of interested address-families. */
4337 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
4339 rib_update_table(table
, event
, rtype
);
4341 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
4343 rib_update_table(table
, event
, rtype
);
4346 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
4348 struct zebra_router_table
*zrt
;
4350 if (IS_ZEBRA_DEBUG_EVENT
)
4351 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
4352 rib_update_event2str(event
));
4354 /* Just iterate over all the route tables, rather than vrf lookups */
4355 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
4356 rib_update_table(zrt
->table
, event
, rtype
);
4359 struct rib_update_ctx
{
4360 enum rib_update_event event
;
4365 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
4366 enum rib_update_event event
)
4368 struct rib_update_ctx
*ctx
;
4370 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
4373 ctx
->vrf_id
= vrf_id
;
4378 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
4380 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
4383 static void rib_update_handler(struct thread
*thread
)
4385 struct rib_update_ctx
*ctx
;
4387 ctx
= THREAD_ARG(thread
);
4390 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
4392 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
, ZEBRA_ROUTE_ALL
);
4394 rib_update_ctx_fini(&ctx
);
4398 * Thread list to ensure we don't schedule a ton of events
4399 * if interfaces are flapping for instance.
4401 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
4403 /* Schedule a RIB update event for all vrfs */
4404 void rib_update(enum rib_update_event event
)
4406 struct rib_update_ctx
*ctx
;
4408 if (thread_is_scheduled(t_rib_update_threads
[event
]))
4411 ctx
= rib_update_ctx_init(0, event
);
4412 ctx
->vrf_all
= true;
4414 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
4415 &t_rib_update_threads
[event
]);
4417 if (IS_ZEBRA_DEBUG_EVENT
)
4418 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
4419 rib_update_event2str(event
));
4422 /* Delete self installed routes after zebra is relaunched. */
4423 void rib_sweep_table(struct route_table
*table
)
4425 struct route_node
*rn
;
4426 struct route_entry
*re
;
4427 struct route_entry
*next
;
4428 struct nexthop
*nexthop
;
4433 if (IS_ZEBRA_DEBUG_RIB
)
4434 zlog_debug("%s: starting", __func__
);
4436 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4437 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4439 if (IS_ZEBRA_DEBUG_RIB
)
4440 route_entry_dump(&rn
->p
, NULL
, re
);
4442 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4445 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
4449 * If routes are older than startup_time then
4450 * we know we read them in from the kernel.
4451 * As such we can safely remove them.
4453 if (zrouter
.startup_time
< re
->uptime
)
4457 * So we are starting up and have received
4458 * routes from the kernel that we have installed
4459 * from a previous run of zebra but not cleaned
4460 * up ( say a kill -9 )
4461 * But since we haven't actually installed
4462 * them yet( we received them from the kernel )
4463 * we don't think they are active.
4464 * So let's pretend they are active to actually
4466 * In all honesty I'm not sure if we should
4467 * mark them as active when we receive them
4468 * This is startup only so probably ok.
4470 * If we ever decide to move rib_sweep_table
4471 * to a different spot (ie startup )
4472 * this decision needs to be revisited
4474 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
4475 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4476 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
4478 rib_uninstall_kernel(rn
, re
);
4479 rib_delnode(rn
, re
);
4483 if (IS_ZEBRA_DEBUG_RIB
)
4484 zlog_debug("%s: ends", __func__
);
4487 /* Sweep all RIB tables. */
4488 void rib_sweep_route(struct thread
*t
)
4491 struct zebra_vrf
*zvrf
;
4493 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4494 if ((zvrf
= vrf
->info
) == NULL
)
4497 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4498 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4501 zebra_router_sweep_route();
4502 zebra_router_sweep_nhgs();
4505 /* Remove specific by protocol routes from 'table'. */
4506 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
4507 struct route_table
*table
)
4509 struct route_node
*rn
;
4510 struct route_entry
*re
;
4511 struct route_entry
*next
;
4512 unsigned long n
= 0;
4515 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
4516 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4517 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4519 if (re
->type
== proto
4520 && re
->instance
== instance
) {
4521 rib_delnode(rn
, re
);
4528 /* Remove specific by protocol routes. */
4529 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
4532 struct zebra_vrf
*zvrf
;
4533 struct other_route_table
*ort
;
4534 unsigned long cnt
= 0;
4536 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4541 cnt
+= rib_score_proto_table(proto
, instance
,
4542 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4543 + rib_score_proto_table(
4545 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4547 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
4548 rib_score_proto_table(proto
, instance
, ort
->table
);
4554 /* Close RIB and clean up kernel routes. */
4555 void rib_close_table(struct route_table
*table
)
4557 struct route_node
*rn
;
4563 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4564 dest
= rib_dest_from_rnode(rn
);
4566 if (dest
&& dest
->selected_fib
) {
4567 rib_uninstall_kernel(rn
, dest
->selected_fib
);
4568 dest
->selected_fib
= NULL
;
4574 * Handler for async dataplane results after a pseudowire installation
4576 static void handle_pw_result(struct zebra_dplane_ctx
*ctx
)
4578 struct zebra_pw
*pw
;
4579 struct zebra_vrf
*vrf
;
4581 /* The pseudowire code assumes success - we act on an error
4582 * result for installation attempts here.
4584 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
4587 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
4588 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
4589 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
4591 zebra_pw_install_failure(pw
,
4592 dplane_ctx_get_pw_status(ctx
));
4597 * Handle results from the dataplane system. Dequeue update context
4598 * structs, dispatch to appropriate internal handlers.
4600 static void rib_process_dplane_results(struct thread
*thread
)
4602 struct zebra_dplane_ctx
*ctx
;
4603 struct dplane_ctx_q ctxlist
;
4604 bool shut_p
= false;
4606 /* Dequeue a list of completed updates with one lock/unlock cycle */
4609 TAILQ_INIT(&ctxlist
);
4611 /* Take lock controlling queue of results */
4612 frr_with_mutex (&dplane_mutex
) {
4613 /* Dequeue list of context structs */
4614 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
4617 /* Dequeue context block */
4618 ctx
= dplane_ctx_dequeue(&ctxlist
);
4620 /* If we've emptied the results queue, we're done */
4624 /* If zebra is shutting down, avoid processing results,
4625 * just drain the results queue.
4627 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
4628 memory_order_relaxed
);
4631 dplane_ctx_fini(&ctx
);
4633 ctx
= dplane_ctx_dequeue(&ctxlist
);
4639 #ifdef HAVE_SCRIPTING
4640 char *script_name
= frrscript_names_get_script_name(
4641 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
4644 struct frrscript
*fs
;
4647 fs
= frrscript_new(script_name
);
4649 ret
= frrscript_load(
4650 fs
, ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4653 #endif /* HAVE_SCRIPTING */
4657 #ifdef HAVE_SCRIPTING
4660 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4662 #endif /* HAVE_SCRIPTING */
4664 switch (dplane_ctx_get_op(ctx
)) {
4665 case DPLANE_OP_ROUTE_INSTALL
:
4666 case DPLANE_OP_ROUTE_UPDATE
:
4667 case DPLANE_OP_ROUTE_DELETE
:
4668 /* Bit of special case for route updates
4669 * that were generated by async notifications:
4670 * we don't want to continue processing these
4673 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4674 rib_process_result(ctx
);
4677 case DPLANE_OP_ROUTE_NOTIFY
:
4678 rib_process_dplane_notify(ctx
);
4681 case DPLANE_OP_NH_INSTALL
:
4682 case DPLANE_OP_NH_UPDATE
:
4683 case DPLANE_OP_NH_DELETE
:
4684 zebra_nhg_dplane_result(ctx
);
4687 case DPLANE_OP_LSP_INSTALL
:
4688 case DPLANE_OP_LSP_UPDATE
:
4689 case DPLANE_OP_LSP_DELETE
:
4690 /* Bit of special case for LSP updates
4691 * that were generated by async notifications:
4692 * we don't want to continue processing these.
4694 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4695 zebra_mpls_lsp_dplane_result(ctx
);
4698 case DPLANE_OP_LSP_NOTIFY
:
4699 zebra_mpls_process_dplane_notify(ctx
);
4702 case DPLANE_OP_PW_INSTALL
:
4703 case DPLANE_OP_PW_UNINSTALL
:
4704 handle_pw_result(ctx
);
4707 case DPLANE_OP_SYS_ROUTE_ADD
:
4708 case DPLANE_OP_SYS_ROUTE_DELETE
:
4711 case DPLANE_OP_MAC_INSTALL
:
4712 case DPLANE_OP_MAC_DELETE
:
4713 zebra_vxlan_handle_result(ctx
);
4716 case DPLANE_OP_RULE_ADD
:
4717 case DPLANE_OP_RULE_DELETE
:
4718 case DPLANE_OP_RULE_UPDATE
:
4719 case DPLANE_OP_IPTABLE_ADD
:
4720 case DPLANE_OP_IPTABLE_DELETE
:
4721 case DPLANE_OP_IPSET_ADD
:
4722 case DPLANE_OP_IPSET_DELETE
:
4723 case DPLANE_OP_IPSET_ENTRY_ADD
:
4724 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4725 zebra_pbr_dplane_result(ctx
);
4728 case DPLANE_OP_INTF_ADDR_ADD
:
4729 case DPLANE_OP_INTF_ADDR_DEL
:
4730 case DPLANE_OP_INTF_INSTALL
:
4731 case DPLANE_OP_INTF_UPDATE
:
4732 case DPLANE_OP_INTF_DELETE
:
4733 case DPLANE_OP_INTF_NETCONFIG
:
4734 zebra_if_dplane_result(ctx
);
4737 case DPLANE_OP_TC_QDISC_INSTALL
:
4738 case DPLANE_OP_TC_QDISC_UNINSTALL
:
4739 case DPLANE_OP_TC_CLASS_ADD
:
4740 case DPLANE_OP_TC_CLASS_DELETE
:
4741 case DPLANE_OP_TC_CLASS_UPDATE
:
4742 case DPLANE_OP_TC_FILTER_ADD
:
4743 case DPLANE_OP_TC_FILTER_DELETE
:
4744 case DPLANE_OP_TC_FILTER_UPDATE
:
4747 /* Some op codes not handled here */
4748 case DPLANE_OP_ADDR_INSTALL
:
4749 case DPLANE_OP_ADDR_UNINSTALL
:
4750 case DPLANE_OP_NEIGH_INSTALL
:
4751 case DPLANE_OP_NEIGH_UPDATE
:
4752 case DPLANE_OP_NEIGH_DELETE
:
4753 case DPLANE_OP_NEIGH_IP_INSTALL
:
4754 case DPLANE_OP_NEIGH_IP_DELETE
:
4755 case DPLANE_OP_VTEP_ADD
:
4756 case DPLANE_OP_VTEP_DELETE
:
4757 case DPLANE_OP_NEIGH_DISCOVER
:
4758 case DPLANE_OP_BR_PORT_UPDATE
:
4759 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4760 case DPLANE_OP_GRE_SET
:
4761 case DPLANE_OP_NONE
:
4764 } /* Dispatch by op code */
4766 dplane_ctx_fini(&ctx
);
4767 ctx
= dplane_ctx_dequeue(&ctxlist
);
4774 * Results are returned from the dataplane subsystem, in the context of
4775 * the dataplane pthread. We enqueue the results here for processing by
4776 * the main thread later.
4778 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
4780 /* Take lock controlling queue of results */
4781 frr_with_mutex (&dplane_mutex
) {
4782 /* Enqueue context blocks */
4783 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4786 /* Ensure event is signalled to zebra main pthread */
4787 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4794 * Ensure there are no empty slots in the route_info array.
4795 * Every route type in zebra should be present there.
4797 static void check_route_info(void)
4799 int len
= array_size(route_info
);
4802 * ZEBRA_ROUTE_SYSTEM is special cased since
4803 * its key is 0 anyway.
4805 * ZEBRA_ROUTE_ALL is also ignored.
4807 for (int i
= 0; i
< len
; i
++) {
4808 assert(route_info
[i
].key
>= ZEBRA_ROUTE_SYSTEM
&&
4809 route_info
[i
].key
< ZEBRA_ROUTE_MAX
);
4810 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4814 /* Routing information base initialize. */
4821 /* Init dataplane, and register for results */
4822 pthread_mutex_init(&dplane_mutex
, NULL
);
4823 TAILQ_INIT(&rib_dplane_q
);
4824 zebra_dplane_init(rib_dplane_results
);
4830 * Get the first vrf id that is greater than the given vrf id if any.
4832 * Returns true if a vrf id was found, false otherwise.
4834 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4838 vrf
= vrf_lookup_by_id(vrf_id
);
4840 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4842 *next_id_p
= vrf
->vrf_id
;
4851 * rib_tables_iter_next
4853 * Returns the next table in the iteration.
4855 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4857 struct route_table
*table
;
4860 * Array that helps us go over all AFI/SAFI combinations via one
4863 static const struct {
4867 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4868 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4869 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4874 switch (iter
->state
) {
4876 case RIB_TABLES_ITER_S_INIT
:
4877 iter
->vrf_id
= VRF_DEFAULT
;
4878 iter
->afi_safi_ix
= -1;
4882 case RIB_TABLES_ITER_S_ITERATING
:
4883 iter
->afi_safi_ix
++;
4886 while (iter
->afi_safi_ix
4887 < (int)array_size(afi_safis
)) {
4888 table
= zebra_vrf_table(
4889 afi_safis
[iter
->afi_safi_ix
].afi
,
4890 afi_safis
[iter
->afi_safi_ix
].safi
,
4895 iter
->afi_safi_ix
++;
4899 * Found another table in this vrf.
4905 * Done with all tables in the current vrf, go to the
4909 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4912 iter
->afi_safi_ix
= 0;
4917 case RIB_TABLES_ITER_S_DONE
:
4922 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4924 iter
->state
= RIB_TABLES_ITER_S_DONE
;