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",
1311 SET_FLAG(re
->status
,
1312 ROUTE_ENTRY_REMOVED
);
1315 info
= srcdest_rnode_table_info(rn
);
1316 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1317 zsend_route_notify_owner(
1318 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
1319 info
->afi
, info
->safi
);
1324 * If the re has not changed and the nhg we have is
1325 * not usable, then we cannot use this route entry
1326 * for consideration, as that the route will just
1327 * not install if it is selected.
1329 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1333 /* Infinite distance. */
1334 if (re
->distance
== DISTANCE_INFINITY
&&
1335 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1336 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1340 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1341 best
= rib_choose_best(new_fib
, re
);
1342 if (new_fib
&& best
!= new_fib
)
1343 UNSET_FLAG(new_fib
->status
,
1344 ROUTE_ENTRY_CHANGED
);
1347 best
= rib_choose_best(new_selected
, re
);
1348 if (new_selected
&& best
!= new_selected
)
1349 UNSET_FLAG(new_selected
->status
,
1350 ROUTE_ENTRY_CHANGED
);
1351 new_selected
= best
;
1354 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1355 } /* RNODE_FOREACH_RE */
1357 /* If no FIB override route, use the selected route also for FIB */
1358 if (new_fib
== NULL
)
1359 new_fib
= new_selected
;
1361 /* After the cycle is finished, the following pointers will be set:
1362 * old_selected --- RE entry currently having SELECTED
1363 * new_selected --- RE entry that is newly SELECTED
1364 * old_fib --- RE entry currently in kernel FIB
1365 * new_fib --- RE entry that is newly to be in kernel FIB
1367 * new_selected will get SELECTED flag, and is going to be redistributed
1368 * the zclients. new_fib (which can be new_selected) will be installed
1372 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1373 struct route_entry
*entry
;
1375 entry
= old_selected
1380 : new_fib
? new_fib
: NULL
;
1383 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1384 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, rn
,
1385 (void *)old_selected
, (void *)new_selected
,
1386 (void *)old_fib
, (void *)new_fib
);
1389 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1390 * fib == selected */
1391 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1392 ROUTE_ENTRY_CHANGED
);
1394 /* Update SELECTED entry */
1395 if (old_selected
!= new_selected
|| selected_changed
) {
1397 if (new_selected
&& new_selected
!= new_fib
)
1398 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1401 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1405 * If we're removing the old entry, we should tell
1406 * redist subscribers about that *if* they aren't
1407 * going to see a redist for the new entry.
1409 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1410 ROUTE_ENTRY_REMOVED
))
1411 redistribute_delete(rn
, old_selected
,
1414 if (old_selected
!= new_selected
)
1415 UNSET_FLAG(old_selected
->flags
,
1416 ZEBRA_FLAG_SELECTED
);
1420 /* Update fib according to selection results */
1421 if (new_fib
&& old_fib
)
1422 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1424 rib_process_add_fib(zvrf
, rn
, new_fib
);
1426 rib_process_del_fib(zvrf
, rn
, old_fib
);
1428 /* Remove all RE entries queued for removal */
1429 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1430 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1431 if (IS_ZEBRA_DEBUG_RIB
) {
1432 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1433 (void *)rn
, (void *)re
);
1440 * Check if the dest can be deleted now.
1445 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1447 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1448 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1453 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1454 if (IS_ZEBRA_DEBUG_MPLS
)
1456 "%s(%u): Scheduling all LSPs upon RIB completion",
1457 zvrf_name(zvrf
), zvrf_id(zvrf
));
1458 zebra_mpls_lsp_schedule(zvrf
);
1459 mpls_unmark_lsps_for_processing(rn
);
1464 * Utility to match route with dplane context data
1466 static bool rib_route_match_ctx(const struct route_entry
*re
,
1467 const struct zebra_dplane_ctx
*ctx
,
1470 bool result
= false;
1474 * In 'update' case, we test info about the 'previous' or
1477 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1478 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1481 /* We use an extra test for statics, and another for
1484 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1485 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1486 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1488 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1490 dplane_ctx_get_old_metric(ctx
)) {
1497 * Ordinary, single-route case using primary context info
1499 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1500 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1501 /* Skip route that's been deleted */
1505 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1506 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1509 /* We use an extra test for statics, and another for
1512 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1513 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1514 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1516 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1517 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1519 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1520 result
= nexthop_group_equal_no_recurse(
1521 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1530 static void zebra_rib_fixup_system(struct route_node
*rn
)
1532 struct route_entry
*re
;
1534 RNODE_FOREACH_RE(rn
, re
) {
1535 struct nexthop
*nhop
;
1537 if (!RIB_SYSTEM_ROUTE(re
))
1540 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1543 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1544 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1545 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
1547 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1548 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1551 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1556 /* Route comparison logic, with various special cases. */
1557 static bool rib_compare_routes(const struct route_entry
*re1
,
1558 const struct route_entry
*re2
)
1560 if (re1
->type
!= re2
->type
)
1563 if (re1
->instance
!= re2
->instance
)
1566 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1569 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1570 re1
->distance
!= re2
->distance
)
1573 /* We support multiple connected routes: this supports multiple
1574 * v6 link-locals, and we also support multiple addresses in the same
1575 * subnet on a single interface.
1577 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1584 * Compare nexthop lists from a route and a dplane context; test whether
1585 * the list installed in the FIB matches the route's list.
1586 * Set 'changed_p' to 'true' if there were changes to the route's
1587 * installed nexthops.
1589 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1592 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1593 const struct nexthop_group
*ctx_nhg
,
1596 bool matched_p
= true;
1597 struct nexthop
*nexthop
, *ctx_nexthop
;
1599 /* Get the first `installed` one to check against.
1600 * If the dataplane doesn't set these to be what was actually installed,
1601 * it will just be whatever was in re->nhe->nhg?
1603 ctx_nexthop
= ctx_nhg
->nexthop
;
1605 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1606 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1607 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1609 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1611 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1614 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1617 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1618 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1619 /* If the FIB doesn't know about the nexthop,
1620 * it's not installed
1622 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1623 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1624 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1626 (CHECK_FLAG(nexthop
->flags
,
1632 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1635 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1637 /* Keep checking nexthops */
1641 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1642 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1643 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1644 zlog_debug("%s: rib nh %pNHv -> installed",
1650 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1652 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1653 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1654 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1660 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1663 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1670 * Update a route from a dplane context. This consolidates common code
1671 * that can be used in processing of results from FIB updates, and in
1672 * async notification processing.
1673 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1675 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1676 struct route_node
*rn
,
1677 struct zebra_dplane_ctx
*ctx
)
1679 struct nexthop
*nexthop
;
1681 const struct nexthop_group
*ctxnhg
;
1682 struct nexthop_group
*re_nhg
;
1683 bool is_selected
= false; /* Is 're' currently the selected re? */
1684 bool changed_p
= false; /* Change to nexthops? */
1688 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1690 dest
= rib_dest_from_rnode(rn
);
1692 is_selected
= (re
== dest
->selected_fib
);
1694 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1695 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1696 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1697 (is_selected
? "" : "NOT "), re
);
1699 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1700 * If the installed set differs from the set requested by the rib/owner,
1701 * we use the fib-specific nexthop-group to record the actual FIB
1705 ctxnhg
= dplane_ctx_get_ng(ctx
);
1707 /* Check route's fib group and incoming notif group for equivalence.
1709 * Let's assume the nexthops are ordered here to save time.
1711 /* TODO -- this isn't testing or comparing the FIB flags; we should
1712 * do a more explicit loop, checking the incoming notification's flags.
1714 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1715 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1718 /* If the new FIB set matches the existing FIB set, we're done. */
1720 if (IS_ZEBRA_DEBUG_RIB
)
1722 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1723 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1726 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1728 * Free stale fib list and move on to check the rib nhg.
1730 if (IS_ZEBRA_DEBUG_RIB
)
1732 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1733 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1734 nexthops_free(re
->fib_ng
.nexthop
);
1735 re
->fib_ng
.nexthop
= NULL
;
1737 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1739 /* Note that the installed nexthops have changed */
1742 if (IS_ZEBRA_DEBUG_RIB
)
1744 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1745 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1749 * Compare with the rib nexthop group. The comparison here is different:
1750 * the RIB group may be a superset of the list installed in the FIB. We
1751 * walk the RIB group, looking for the 'installable' candidate
1752 * nexthops, and then check those against the set
1753 * that is actually installed.
1755 * Assume nexthops are ordered here as well.
1758 /* If nothing is installed, we can skip some of the checking/comparison
1761 if (ctxnhg
->nexthop
== NULL
) {
1766 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1768 /* If all nexthops were processed, we're done */
1770 if (IS_ZEBRA_DEBUG_RIB
)
1772 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1773 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1774 (changed_p
? "true" : "false"));
1780 /* FIB nexthop set differs from the RIB set:
1781 * create a fib-specific nexthop-group
1783 if (IS_ZEBRA_DEBUG_RIB
)
1785 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1786 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1787 (changed_p
? "true" : "false"),
1788 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1790 /* Set the flag about the dedicated fib list */
1791 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1792 if (ctxnhg
->nexthop
)
1793 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1798 * Check the status of the route's backup nexthops, if any.
1799 * The logic for backups is somewhat different: if any backup is
1800 * installed, a new fib nhg will be attached to the route.
1802 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1804 goto done
; /* No backup nexthops */
1806 /* First check the route's 'fib' list of backups, if it's present
1807 * from some previous event.
1809 re_nhg
= &re
->fib_backup_ng
;
1810 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1813 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1816 /* If the new FIB set matches an existing FIB set, we're done. */
1818 if (IS_ZEBRA_DEBUG_RIB
)
1820 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1821 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1824 } else if (re
->fib_backup_ng
.nexthop
) {
1826 * Free stale fib backup list and move on to check
1827 * the route's backups.
1829 if (IS_ZEBRA_DEBUG_RIB
)
1831 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1832 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1833 nexthops_free(re
->fib_backup_ng
.nexthop
);
1834 re
->fib_backup_ng
.nexthop
= NULL
;
1836 /* Note that the installed nexthops have changed */
1839 if (IS_ZEBRA_DEBUG_RIB
)
1841 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1842 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1846 * If a FIB backup nexthop set exists, attach a copy
1847 * to the route if any backup is installed
1849 if (ctxnhg
&& ctxnhg
->nexthop
) {
1851 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1852 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1856 /* If no installed backups, we're done */
1857 if (nexthop
== NULL
)
1860 if (IS_ZEBRA_DEBUG_RIB
)
1862 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1863 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1864 (changed_p
? "true" : "false"));
1866 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1876 * Helper to locate a zebra route-node from a dplane context. This is used
1877 * when processing dplane results, e.g. Note well: the route-node is returned
1878 * with a ref held - route_unlock_node() must be called eventually.
1880 struct route_node
*rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1882 struct route_table
*table
= NULL
;
1883 struct route_node
*rn
= NULL
;
1884 const struct prefix
*dest_pfx
, *src_pfx
;
1886 /* Locate rn and re(s) from ctx */
1888 table
= zebra_vrf_lookup_table_with_table_id(
1889 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1890 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1891 if (table
== NULL
) {
1892 if (IS_ZEBRA_DEBUG_DPLANE
) {
1894 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1895 dplane_ctx_get_afi(ctx
),
1896 dplane_ctx_get_safi(ctx
),
1897 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1898 dplane_ctx_get_vrf(ctx
));
1903 dest_pfx
= dplane_ctx_get_dest(ctx
);
1904 src_pfx
= dplane_ctx_get_src(ctx
);
1906 rn
= srcdest_rnode_get(table
, dest_pfx
,
1907 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1916 * Route-update results processing after async dataplane update.
1918 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1920 struct zebra_vrf
*zvrf
= NULL
;
1922 struct route_node
*rn
= NULL
;
1923 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1924 bool is_update
= false;
1925 enum dplane_op_e op
;
1926 enum zebra_dplane_result status
;
1929 bool fib_changed
= false;
1930 struct rib_table_info
*info
;
1931 bool rt_delete
= false;
1933 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1934 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1936 /* Locate rn and re(s) from ctx */
1937 rn
= rib_find_rn_from_ctx(ctx
);
1939 if (IS_ZEBRA_DEBUG_DPLANE
) {
1941 "Failed to process dplane results: no route for %s(%u):%pRN",
1942 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
), rn
);
1947 dest
= rib_dest_from_rnode(rn
);
1948 info
= srcdest_rnode_table_info(rn
);
1950 op
= dplane_ctx_get_op(ctx
);
1951 status
= dplane_ctx_get_status(ctx
);
1953 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1955 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1956 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1957 dplane_ctx_get_table(ctx
), rn
, ctx
, dplane_op2str(op
),
1958 dplane_res2str(status
));
1961 * Update is a bit of a special case, where we may have both old and new
1962 * routes to post-process.
1964 is_update
= dplane_ctx_is_update(ctx
);
1967 * Take a pass through the routes, look for matches with the context
1970 RNODE_FOREACH_RE(rn
, rib
) {
1973 if (rib_route_match_ctx(rib
, ctx
, false))
1977 /* Check for old route match */
1978 if (is_update
&& (old_re
== NULL
)) {
1979 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1983 /* Have we found the routes we need to work on? */
1984 if (re
&& ((!is_update
|| old_re
)))
1988 seq
= dplane_ctx_get_seq(ctx
);
1991 * Check sequence number(s) to detect stale results before continuing
1994 if (re
->dplane_sequence
!= seq
) {
1995 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1997 "%s(%u):%pRN Stale dplane result for re %p",
1999 dplane_ctx_get_vrf(ctx
), rn
, re
);
2001 if (!zrouter
.asic_offloaded
||
2002 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
2003 CHECK_FLAG(re
->flags
,
2004 ZEBRA_FLAG_OFFLOAD_FAILED
))) {
2005 UNSET_FLAG(re
->status
,
2006 ROUTE_ENTRY_ROUTE_REPLACING
);
2007 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2013 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
2014 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2016 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
2018 dplane_ctx_get_vrf(ctx
), old_re
->table
,
2021 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
2025 case DPLANE_OP_ROUTE_INSTALL
:
2026 case DPLANE_OP_ROUTE_UPDATE
:
2027 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2029 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2030 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2033 * On an update operation from the same route type
2034 * context retrieval currently has no way to know
2035 * which was the old and which was the new.
2036 * So don't unset our flags that we just set.
2037 * We know redistribution is ok because the
2038 * old_re in this case is used for nothing
2039 * more than knowing whom to contact if necessary.
2041 if (old_re
&& old_re
!= re
) {
2042 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2043 UNSET_FLAG(old_re
->status
,
2044 ROUTE_ENTRY_INSTALLED
);
2047 /* Update zebra route based on the results in
2048 * the context struct.
2052 rib_update_re_from_ctx(re
, rn
, ctx
);
2055 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2057 "%s(%u:%u):%pRN no fib change for re",
2059 dplane_ctx_get_vrf(ctx
),
2060 dplane_ctx_get_table(
2065 /* Redistribute if this is the selected re */
2066 if (dest
&& re
== dest
->selected_fib
)
2067 redistribute_update(rn
, re
, old_re
);
2071 * System routes are weird in that they
2072 * allow multiple to be installed that match
2073 * to the same prefix, so after we get the
2074 * result we need to clean them up so that
2075 * we can actually use them.
2077 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2078 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2079 zebra_rib_fixup_system(rn
);
2084 /* Notify route owner */
2085 if (zebra_router_notify_on_ack())
2086 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2089 if (CHECK_FLAG(re
->flags
,
2090 ZEBRA_FLAG_OFFLOADED
))
2091 zsend_route_notify_owner_ctx(
2093 ZAPI_ROUTE_INSTALLED
);
2096 ZEBRA_FLAG_OFFLOAD_FAILED
))
2097 zsend_route_notify_owner_ctx(
2099 ZAPI_ROUTE_FAIL_INSTALL
);
2104 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2105 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2107 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2109 zsend_route_notify_owner(
2110 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
2111 info
->afi
, info
->safi
);
2113 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2114 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2115 dplane_ctx_get_table(ctx
), rn
);
2118 case DPLANE_OP_ROUTE_DELETE
:
2121 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2123 * In the delete case, the zebra core datastructs were
2124 * updated (or removed) at the time the delete was issued,
2125 * so we're just notifying the route owner.
2127 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2129 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2130 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2132 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2138 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2139 zsend_route_notify_owner_ctx(ctx
,
2140 ZAPI_ROUTE_REMOVE_FAIL
);
2142 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2143 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2144 dplane_ctx_get_table(ctx
), rn
);
2148 * System routes are weird in that they
2149 * allow multiple to be installed that match
2150 * to the same prefix, so after we get the
2151 * result we need to clean them up so that
2152 * we can actually use them.
2154 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2155 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2156 zebra_rib_fixup_system(rn
);
2162 zebra_rib_evaluate_rn_nexthops(rn
, seq
, rt_delete
);
2163 zebra_rib_evaluate_mpls(rn
);
2167 route_unlock_node(rn
);
2171 * Count installed/FIB nexthops
2173 static int rib_count_installed_nh(struct route_entry
*re
)
2176 struct nexthop
*nexthop
;
2177 struct nexthop_group
*nhg
;
2179 nhg
= rib_get_fib_nhg(re
);
2181 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2182 /* The meaningful flag depends on where the installed
2185 if (nhg
== &(re
->fib_ng
)) {
2186 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2189 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2194 nhg
= rib_get_fib_backup_nhg(re
);
2196 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2197 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2206 * Handle notification from async dataplane: the dataplane has detected
2207 * some change to a route, and notifies zebra so that the control plane
2208 * can reflect that change.
2210 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2212 struct route_node
*rn
= NULL
;
2213 struct route_entry
*re
= NULL
;
2215 struct nexthop
*nexthop
;
2217 bool fib_changed
= false;
2218 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2219 int start_count
, end_count
;
2221 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2223 /* Locate rn and re(s) from ctx */
2224 rn
= rib_find_rn_from_ctx(ctx
);
2228 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2229 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2230 dplane_ctx_get_table(ctx
), rn
);
2235 dest
= rib_dest_from_rnode(rn
);
2238 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2239 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2240 dplane_ctx_get_table(ctx
), rn
, ctx
);
2243 * Take a pass through the routes, look for matches with the context
2246 RNODE_FOREACH_RE(rn
, re
) {
2247 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2251 /* No match? Nothing we can do */
2255 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2256 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2257 dplane_ctx_get_table(ctx
), rn
,
2258 zebra_route_string(dplane_ctx_get_type(ctx
)));
2263 /* Ensure we clear the QUEUED flag */
2264 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2265 UNSET_FLAG(re
->status
, ROUTE_ENTRY_ROUTE_REPLACING
);
2267 /* Is this a notification that ... matters? We mostly care about
2268 * the route that is currently selected for installation; we may also
2269 * get an un-install notification, and handle that too.
2271 if (re
!= dest
->selected_fib
) {
2273 * If we need to, clean up after a delete that was part of
2274 * an update operation.
2277 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2278 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2282 /* If no nexthops or none installed, ensure that this re
2283 * gets its 'installed' flag cleared.
2285 if (end_count
== 0) {
2286 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2287 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2290 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2292 dplane_ctx_get_vrf(ctx
),
2293 dplane_ctx_get_table(ctx
), rn
,
2295 dplane_ctx_get_type(ctx
)));
2297 /* At least report on the event. */
2300 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2302 dplane_ctx_get_vrf(ctx
),
2303 dplane_ctx_get_table(ctx
), rn
,
2305 dplane_ctx_get_type(ctx
)));
2309 uint32_t flags
= dplane_ctx_get_flags(ctx
);
2311 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOADED
)) {
2312 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2313 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2315 if (CHECK_FLAG(flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)) {
2316 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
);
2317 SET_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
);
2319 if (CHECK_FLAG(flags
, ZEBRA_FLAG_TRAPPED
))
2320 SET_FLAG(re
->flags
, ZEBRA_FLAG_TRAPPED
);
2323 /* We'll want to determine whether the installation status of the
2324 * route has changed: we'll check the status before processing,
2325 * and then again if there's been a change.
2329 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2330 start_count
= rib_count_installed_nh(re
);
2332 /* Update zebra's nexthop FIB flags based on the context struct's
2335 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2340 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2341 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2342 dplane_ctx_get_table(ctx
), rn
);
2346 * Perform follow-up work if the actual status of the prefix
2349 end_count
= rib_count_installed_nh(re
);
2351 /* Various fib transitions: changed nexthops; from installed to
2352 * not-installed; or not-installed to installed.
2354 if (zrouter
.asic_notification_nexthop_control
) {
2355 if (start_count
> 0 && end_count
> 0) {
2358 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2360 dplane_ctx_get_vrf(ctx
),
2361 dplane_ctx_get_table(ctx
), rn
);
2363 /* Changed nexthops - update kernel/others */
2364 dplane_route_notif_update(rn
, re
,
2365 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2367 } else if (start_count
== 0 && end_count
> 0) {
2370 "%s(%u:%u):%pRN installed transition from dplane notification",
2372 dplane_ctx_get_vrf(ctx
),
2373 dplane_ctx_get_table(ctx
), rn
);
2375 /* We expect this to be the selected route, so we want
2376 * to tell others about this transition.
2378 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2380 /* Changed nexthops - update kernel/others */
2381 dplane_route_notif_update(rn
, re
,
2382 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2384 /* Redistribute, lsp, and nht update */
2385 redistribute_update(rn
, re
, NULL
);
2387 } else if (start_count
> 0 && end_count
== 0) {
2390 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2392 dplane_ctx_get_vrf(ctx
),
2393 dplane_ctx_get_table(ctx
), rn
);
2395 /* Transition from _something_ installed to _nothing_
2398 /* We expect this to be the selected route, so we want
2399 * to tell others about this transistion.
2401 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2403 /* Changed nexthops - update kernel/others */
2404 dplane_route_notif_update(rn
, re
,
2405 DPLANE_OP_ROUTE_DELETE
, ctx
);
2407 /* Redistribute, lsp, and nht update */
2408 redistribute_delete(rn
, re
, NULL
);
2412 if (!zebra_router_notify_on_ack()) {
2413 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
))
2414 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2415 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
))
2416 zsend_route_notify_owner_ctx(ctx
,
2417 ZAPI_ROUTE_FAIL_INSTALL
);
2420 /* Make any changes visible for lsp and nexthop-tracking processing */
2421 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
2424 zebra_rib_evaluate_mpls(rn
);
2428 route_unlock_node(rn
);
2432 * Process a node from the EVPN/VXLAN subqueue.
2434 static void process_subq_evpn(struct listnode
*lnode
)
2436 struct wq_evpn_wrapper
*w
;
2438 /* In general, the list node points to a wrapper object
2439 * holding the info necessary to make some update.
2441 w
= listgetdata(lnode
);
2445 if (w
->type
== WQ_EVPN_WRAPPER_TYPE_VRFROUTE
) {
2447 zebra_vxlan_evpn_vrf_route_add(w
->vrf_id
, &w
->macaddr
,
2448 &w
->ip
, &w
->prefix
);
2450 zebra_vxlan_evpn_vrf_route_del(w
->vrf_id
, &w
->ip
,
2452 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_ES
) {
2454 zebra_evpn_remote_es_add(&w
->esi
, w
->ip
.ipaddr_v4
,
2455 w
->esr_rxed
, w
->df_alg
,
2458 zebra_evpn_remote_es_del(&w
->esi
, w
->ip
.ipaddr_v4
);
2459 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_MACIP
) {
2460 uint16_t ipa_len
= 0;
2462 if (w
->ip
.ipa_type
== IPADDR_V4
)
2463 ipa_len
= IPV4_MAX_BYTELEN
;
2464 else if (w
->ip
.ipa_type
== IPADDR_V6
)
2465 ipa_len
= IPV6_MAX_BYTELEN
;
2468 zebra_evpn_rem_macip_add(w
->vni
, &w
->macaddr
, ipa_len
,
2469 &w
->ip
, w
->flags
, w
->seq
,
2470 w
->vtep_ip
, &w
->esi
);
2472 zebra_evpn_rem_macip_del(w
->vni
, &w
->macaddr
, ipa_len
,
2473 &w
->ip
, w
->vtep_ip
);
2474 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_VTEP
) {
2476 zebra_vxlan_remote_vtep_add(w
->vrf_id
, w
->vni
,
2477 w
->vtep_ip
, w
->flags
);
2479 zebra_vxlan_remote_vtep_del(w
->vrf_id
, w
->vni
,
2484 XFREE(MTYPE_WQ_WRAPPER
, w
);
2488 * Process the nexthop-group workqueue subqueue
2490 static void process_subq_nhg(struct listnode
*lnode
)
2492 struct nhg_ctx
*ctx
;
2493 struct nhg_hash_entry
*nhe
, *newnhe
;
2494 struct wq_nhg_wrapper
*w
;
2495 uint8_t qindex
= META_QUEUE_NHG
;
2497 w
= listgetdata(lnode
);
2502 /* Two types of object - an update from the local kernel, or
2503 * an nhg update from a daemon.
2505 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2508 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2510 "NHG Context id=%u dequeued from sub-queue %s",
2511 ctx
->id
, subqueue2str(qindex
));
2514 /* Process nexthop group updates coming 'up' from the OS */
2515 nhg_ctx_process(ctx
);
2517 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2520 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2521 zlog_debug("NHG %u dequeued from sub-queue %s", nhe
->id
,
2522 subqueue2str(qindex
));
2524 /* Process incoming nhg update, probably from a proto daemon */
2525 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2527 nhe
->zapi_session
, &nhe
->nhg
, 0);
2529 /* Report error to daemon via ZAPI */
2531 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2532 nhe
->zapi_session
, nhe
->id
,
2533 ZAPI_NHG_FAIL_INSTALL
);
2535 /* Free temp nhe - we own that memory. */
2536 zebra_nhg_free(nhe
);
2539 XFREE(MTYPE_WQ_WRAPPER
, w
);
2542 static void process_subq_early_label(struct listnode
*lnode
)
2544 struct wq_label_wrapper
*w
= listgetdata(lnode
);
2545 struct zebra_vrf
*zvrf
;
2550 zvrf
= vrf_info_lookup(w
->vrf_id
);
2552 XFREE(MTYPE_WQ_WRAPPER
, w
);
2557 case WQ_LABEL_FTN_UNINSTALL
:
2558 zebra_mpls_ftn_uninstall(zvrf
, w
->ltype
, &w
->p
, w
->route_type
,
2561 case WQ_LABEL_LABELS_PROCESS
:
2562 zebra_mpls_zapi_labels_process(w
->add_p
, zvrf
, &w
->zl
);
2566 XFREE(MTYPE_WQ_WRAPPER
, w
);
2569 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2571 struct route_node
*rnode
= NULL
;
2572 rib_dest_t
*dest
= NULL
;
2573 struct zebra_vrf
*zvrf
= NULL
;
2575 rnode
= listgetdata(lnode
);
2576 dest
= rib_dest_from_rnode(rnode
);
2579 zvrf
= rib_dest_vrf(dest
);
2583 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2584 struct route_entry
*re
= NULL
;
2587 * rib_process may have freed the dest
2588 * as part of the garbage collection. Let's
2589 * prevent stupidity from happening.
2591 dest
= rib_dest_from_rnode(rnode
);
2593 re
= re_list_first(&dest
->routes
);
2595 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2596 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2597 rnode
, rnode
, subqueue2str(qindex
));
2601 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2602 RIB_ROUTE_QUEUED(qindex
));
2604 route_unlock_node(rnode
);
2607 static void rib_re_nhg_free(struct route_entry
*re
)
2609 if (re
->nhe
&& re
->nhe_id
) {
2610 assert(re
->nhe
->id
== re
->nhe_id
);
2611 route_entry_update_nhe(re
, NULL
);
2612 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2613 nexthops_free(re
->nhe
->nhg
.nexthop
);
2615 nexthops_free(re
->fib_ng
.nexthop
);
2618 struct zebra_early_route
{
2622 struct prefix_ipv6 src_p
;
2623 bool src_p_provided
;
2624 struct route_entry
*re
;
2625 struct nhg_hash_entry
*re_nhe
;
2631 static void early_route_memory_free(struct zebra_early_route
*ere
)
2634 zebra_nhg_free(ere
->re_nhe
);
2636 XFREE(MTYPE_RE
, ere
->re
);
2637 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2640 static void process_subq_early_route_add(struct zebra_early_route
*ere
)
2642 struct route_entry
*re
= ere
->re
;
2643 struct route_table
*table
;
2644 struct nhg_hash_entry
*nhe
= NULL
;
2645 struct route_node
*rn
;
2646 struct route_entry
*same
= NULL
, *first_same
= NULL
;
2651 table
= zebra_vrf_get_table_with_table_id(ere
->afi
, ere
->safi
,
2652 re
->vrf_id
, re
->table
);
2654 early_route_memory_free(ere
);
2658 if (re
->nhe_id
> 0) {
2659 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2663 * We've received from the kernel a nexthop id
2664 * that we don't have saved yet. More than likely
2665 * it has not been processed and is on the
2666 * queue to be processed. Let's stop what we
2667 * are doing and cause the meta q to be processed
2668 * storing this for later.
2670 * This is being done this way because zebra
2671 * runs with the assumption t
2674 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2675 "Zebra failed to find the nexthop hash entry for id=%u in a route entry %pFX",
2676 re
->nhe_id
, &ere
->p
);
2678 early_route_memory_free(ere
);
2682 /* Lookup nhe from route information */
2683 nhe
= zebra_nhg_rib_find_nhe(ere
->re_nhe
, ere
->afi
);
2685 char buf2
[PREFIX_STRLEN
] = "";
2688 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2689 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2690 &ere
->p
, ere
->src_p_provided
? " from " : "",
2692 ? prefix2str(&ere
->src_p
, buf2
,
2696 early_route_memory_free(ere
);
2702 * Attach the re to the nhe's nexthop group.
2704 * TODO: This will need to change when we start getting IDs from upper
2705 * level protocols, as the refcnt might be wrong, since it checks
2706 * if old_id != new_id.
2708 route_entry_update_nhe(re
, nhe
);
2710 /* Make it sure prefixlen is applied to the prefix. */
2711 apply_mask(&ere
->p
);
2712 if (ere
->src_p_provided
)
2713 apply_mask_ipv6(&ere
->src_p
);
2715 /* Lookup route node.*/
2716 rn
= srcdest_rnode_get(table
, &ere
->p
,
2717 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2720 * If same type of route are installed, treat it as a implicit
2721 * withdraw. If the user has specified the No route replace semantics
2722 * for the install don't do a route replace.
2724 RNODE_FOREACH_RE (rn
, same
) {
2725 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
2730 /* Compare various route_entry properties */
2731 if (rib_compare_routes(re
, same
)) {
2734 if (first_same
== NULL
)
2741 if (!ere
->startup
&& (re
->flags
& ZEBRA_FLAG_SELFROUTE
) &&
2742 zrouter
.asic_offloaded
) {
2744 if (IS_ZEBRA_DEBUG_RIB
)
2746 "prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless",
2749 * We are not on startup, this is a self route
2750 * and we have asic offload. Which means
2751 * we are getting a callback for a entry
2752 * that was already deleted to the kernel
2753 * but an earlier response was just handed
2754 * back. Drop it on the floor
2756 early_route_memory_free(ere
);
2761 /* Set default distance by route type. */
2762 if (re
->distance
== 0) {
2763 if (same
&& !zebra_router_notify_on_ack())
2764 re
->distance
= same
->distance
;
2766 re
->distance
= route_distance(re
->type
);
2769 if (re
->metric
== ROUTE_INSTALLATION_METRIC
&&
2770 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2771 if (same
&& !zebra_router_notify_on_ack())
2772 re
->metric
= same
->metric
;
2777 /* If this route is kernel/connected route, notify the dataplane. */
2778 if (RIB_SYSTEM_ROUTE(re
)) {
2779 /* Notify dataplane */
2780 dplane_sys_route_add(rn
, re
);
2783 /* Link new re to node.*/
2784 if (IS_ZEBRA_DEBUG_RIB
) {
2787 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
2788 rn
, re
, zebra_route_string(re
->type
), same
, same_count
);
2790 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2793 ere
->src_p_provided
? &ere
->src_p
: NULL
, re
);
2796 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2797 rib_addnode(rn
, re
, 1);
2799 /* Free implicit route.*/
2801 rib_delnode(rn
, same
);
2803 /* See if we can remove some RE entries that are queued for
2804 * removal, but won't be considered in rib processing.
2806 dest
= rib_dest_from_rnode(rn
);
2807 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
2808 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2809 /* If the route was used earlier, must retain it. */
2810 if (dest
&& re
== dest
->selected_fib
)
2813 if (IS_ZEBRA_DEBUG_RIB
)
2814 rnode_debug(rn
, re
->vrf_id
,
2815 "rn %p, removing unneeded re %p",
2822 route_unlock_node(rn
);
2824 zebra_nhg_free(ere
->re_nhe
);
2825 XFREE(MTYPE_WQ_WRAPPER
, ere
);
2828 static void process_subq_early_route_delete(struct zebra_early_route
*ere
)
2830 struct route_table
*table
;
2831 struct route_node
*rn
;
2832 struct route_entry
*re
;
2833 struct route_entry
*fib
= NULL
;
2834 struct route_entry
*same
= NULL
;
2835 struct nexthop
*rtnh
;
2836 char buf2
[INET6_ADDRSTRLEN
];
2839 if (ere
->src_p_provided
)
2840 assert(!ere
->src_p
.prefixlen
|| ere
->afi
== AFI_IP6
);
2843 table
= zebra_vrf_lookup_table_with_table_id(
2844 ere
->afi
, ere
->safi
, ere
->re
->vrf_id
, ere
->re
->table
);
2846 early_route_memory_free(ere
);
2851 apply_mask(&ere
->p
);
2852 if (ere
->src_p_provided
)
2853 apply_mask_ipv6(&ere
->src_p
);
2855 /* Lookup route node. */
2856 rn
= srcdest_rnode_lookup(table
, &ere
->p
,
2857 ere
->src_p_provided
? &ere
->src_p
: NULL
);
2859 if (IS_ZEBRA_DEBUG_RIB
) {
2860 char src_buf
[PREFIX_STRLEN
];
2861 struct vrf
*vrf
= vrf_lookup_by_id(ere
->re
->vrf_id
);
2863 if (ere
->src_p_provided
&& ere
->src_p
.prefixlen
)
2864 prefix2str(&ere
->src_p
, src_buf
,
2869 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
2870 vrf
->name
, ere
->re
->table
, rn
,
2871 (src_buf
[0] != '\0') ? " from " : "",
2874 early_route_memory_free(ere
);
2878 dest
= rib_dest_from_rnode(rn
);
2879 fib
= dest
->selected_fib
;
2881 struct nexthop
*nh
= NULL
;
2884 nh
= ere
->re
->nhe
->nhg
.nexthop
;
2886 /* Lookup same type route. */
2887 RNODE_FOREACH_RE (rn
, re
) {
2888 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2891 if (re
->type
!= ere
->re
->type
)
2893 if (re
->instance
!= ere
->re
->instance
)
2895 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2896 ere
->re
->distance
!= re
->distance
)
2899 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2900 re
->metric
!= ere
->re
->metric
)
2902 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= nh
) &&
2903 rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2904 if (rtnh
->ifindex
!= nh
->ifindex
)
2910 /* Make sure that the route found has the same gateway. */
2911 if (ere
->re
->nhe_id
&& re
->nhe_id
== ere
->re
->nhe_id
) {
2920 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
2922 * No guarantee all kernel send nh with labels
2925 if (nexthop_same_no_labels(rtnh
, nh
)) {
2935 * If same type of route can't be found and this message is from
2940 * In the past(HA!) we could get here because
2941 * we were receiving a route delete from the
2942 * kernel and we're not marking the proto
2943 * as coming from it's appropriate originator.
2944 * Now that we are properly noticing the fact
2945 * that the kernel has deleted our route we
2946 * are not going to get called in this path
2947 * I am going to leave this here because
2948 * this might still work this way on non-linux
2949 * platforms as well as some weird state I have
2950 * not properly thought of yet.
2951 * If we can show that this code path is
2952 * dead then we can remove it.
2954 if (fib
&& CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
)) {
2955 if (IS_ZEBRA_DEBUG_RIB
) {
2957 rn
, ere
->re
->vrf_id
,
2958 "rn %p, re %p (%s) was deleted from kernel, adding",
2959 rn
, fib
, zebra_route_string(fib
->type
));
2961 if (zrouter
.allow_delete
||
2962 CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
2963 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2965 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
2967 UNSET_FLAG(rtnh
->flags
,
2971 * This is a non FRR route
2972 * as such we should mark
2975 dest
->selected_fib
= NULL
;
2978 * This means someone else, other than Zebra,
2979 * has deleted a Zebra router from the kernel.
2980 * We will add it back
2982 rib_install_kernel(rn
, fib
, NULL
);
2985 if (IS_ZEBRA_DEBUG_RIB
) {
2988 rn
, ere
->re
->vrf_id
,
2989 "via %s ifindex %d type %d doesn't exist in rib",
2990 inet_ntop(afi2family(ere
->afi
),
2993 nh
->ifindex
, ere
->re
->type
);
2996 rn
, ere
->re
->vrf_id
,
2997 "type %d doesn't exist in rib",
3000 route_unlock_node(rn
);
3001 early_route_memory_free(ere
);
3007 struct nexthop
*tmp_nh
;
3009 if (ere
->fromkernel
&&
3010 CHECK_FLAG(ere
->re
->flags
, ZEBRA_FLAG_SELFROUTE
) &&
3011 !zrouter
.allow_delete
) {
3012 rib_install_kernel(rn
, same
, NULL
);
3013 route_unlock_node(rn
);
3015 early_route_memory_free(ere
);
3019 /* Special handling for IPv4 or IPv6 routes sourced from
3020 * EVPN - the nexthop (and associated MAC) need to be
3021 * uninstalled if no more refs.
3023 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3024 struct ipaddr vtep_ip
;
3026 if (CHECK_FLAG(tmp_nh
->flags
, NEXTHOP_FLAG_EVPN
)) {
3027 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3028 if (ere
->afi
== AFI_IP
) {
3029 vtep_ip
.ipa_type
= IPADDR_V4
;
3030 memcpy(&(vtep_ip
.ipaddr_v4
),
3031 &(tmp_nh
->gate
.ipv4
),
3032 sizeof(struct in_addr
));
3034 vtep_ip
.ipa_type
= IPADDR_V6
;
3035 memcpy(&(vtep_ip
.ipaddr_v6
),
3036 &(tmp_nh
->gate
.ipv6
),
3037 sizeof(struct in6_addr
));
3039 zebra_rib_queue_evpn_route_del(
3040 re
->vrf_id
, &vtep_ip
, &ere
->p
);
3044 /* Notify dplane if system route changes */
3045 if (RIB_SYSTEM_ROUTE(re
))
3046 dplane_sys_route_del(rn
, same
);
3048 rib_delnode(rn
, same
);
3051 route_unlock_node(rn
);
3053 early_route_memory_free(ere
);
3057 * When FRR receives a route we need to match the route up to
3058 * nexthop groups. That we also may have just received
3059 * place the data on this queue so that this work of finding
3060 * the nexthop group entries for the route entry is always
3061 * done after the nexthop group has had a chance to be processed
3063 static void process_subq_early_route(struct listnode
*lnode
)
3065 struct zebra_early_route
*ere
= listgetdata(lnode
);
3068 process_subq_early_route_delete(ere
);
3070 process_subq_early_route_add(ere
);
3074 * Examine the specified subqueue; process one entry and return 1 if
3075 * there is a node, return 0 otherwise.
3077 static unsigned int process_subq(struct list
*subq
,
3078 enum meta_queue_indexes qindex
)
3080 struct listnode
*lnode
= listhead(subq
);
3086 case META_QUEUE_EVPN
:
3087 process_subq_evpn(lnode
);
3089 case META_QUEUE_NHG
:
3090 process_subq_nhg(lnode
);
3092 case META_QUEUE_EARLY_ROUTE
:
3093 process_subq_early_route(lnode
);
3095 case META_QUEUE_EARLY_LABEL
:
3096 process_subq_early_label(lnode
);
3098 case META_QUEUE_CONNECTED
:
3099 case META_QUEUE_KERNEL
:
3100 case META_QUEUE_STATIC
:
3101 case META_QUEUE_NOTBGP
:
3102 case META_QUEUE_BGP
:
3103 case META_QUEUE_OTHER
:
3104 process_subq_route(lnode
, qindex
);
3108 list_delete_node(subq
, lnode
);
3113 /* Dispatch the meta queue by picking and processing the next node from
3114 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
3115 * data is pointed to the meta queue structure.
3117 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
3119 struct meta_queue
*mq
= data
;
3121 uint32_t queue_len
, queue_limit
;
3123 /* Ensure there's room for more dataplane updates */
3124 queue_limit
= dplane_get_in_queue_limit();
3125 queue_len
= dplane_get_in_queue_len();
3126 if (queue_len
> queue_limit
) {
3127 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3129 "rib queue: dplane queue len %u, limit %u, retrying",
3130 queue_len
, queue_limit
);
3132 /* Ensure that the meta-queue is actually enqueued */
3133 if (work_queue_empty(zrouter
.ribq
))
3134 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3136 return WQ_QUEUE_BLOCKED
;
3139 for (i
= 0; i
< MQ_SIZE
; i
++)
3140 if (process_subq(mq
->subq
[i
], i
)) {
3144 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
3149 * Look into the RN and queue it into the highest priority queue
3150 * at this point in time for processing.
3152 * We will enqueue a route node only once per invocation.
3154 * There are two possibilities here that should be kept in mind.
3155 * If the original invocation has not been pulled off for processing
3156 * yet, A subsuquent invocation can have a route entry with a better
3157 * meta queue index value and we can have a situation where
3158 * we might have the same node enqueued 2 times. Not necessarily
3159 * an optimal situation but it should be ok.
3161 * The other possibility is that the original invocation has not
3162 * been pulled off for processing yet, A subsusquent invocation
3163 * doesn't have a route_entry with a better meta-queue and the
3164 * original metaqueue index value will win and we'll end up with
3165 * the route node enqueued once.
3167 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
3169 struct route_node
*rn
= NULL
;
3170 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
3171 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
3173 rn
= (struct route_node
*)data
;
3175 RNODE_FOREACH_RE (rn
, curr_re
) {
3176 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
3178 if (curr_qindex
<= qindex
) {
3180 qindex
= curr_qindex
;
3187 /* Invariant: at this point we always have rn->info set. */
3188 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3189 RIB_ROUTE_QUEUED(qindex
))) {
3190 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3191 rnode_debug(rn
, re
->vrf_id
,
3192 "rn %p is already queued in sub-queue %s",
3193 (void *)rn
, subqueue2str(qindex
));
3197 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
3198 listnode_add(mq
->subq
[qindex
], rn
);
3199 route_lock_node(rn
);
3202 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3203 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %s",
3204 (void *)rn
, subqueue2str(qindex
));
3209 static int early_label_meta_queue_add(struct meta_queue
*mq
, void *data
)
3211 listnode_add(mq
->subq
[META_QUEUE_EARLY_LABEL
], data
);
3216 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
3218 struct nhg_ctx
*ctx
= NULL
;
3219 uint8_t qindex
= META_QUEUE_NHG
;
3220 struct wq_nhg_wrapper
*w
;
3222 ctx
= (struct nhg_ctx
*)data
;
3227 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3229 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
3232 listnode_add(mq
->subq
[qindex
], w
);
3235 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3236 zlog_debug("NHG Context id=%u queued into sub-queue %s",
3237 ctx
->id
, subqueue2str(qindex
));
3242 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
3244 struct nhg_hash_entry
*nhe
= NULL
;
3245 uint8_t qindex
= META_QUEUE_NHG
;
3246 struct wq_nhg_wrapper
*w
;
3248 nhe
= (struct nhg_hash_entry
*)data
;
3253 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
3255 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
3258 listnode_add(mq
->subq
[qindex
], w
);
3261 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3262 zlog_debug("NHG id=%u queued into sub-queue %s", nhe
->id
,
3263 subqueue2str(qindex
));
3268 static int rib_meta_queue_evpn_add(struct meta_queue
*mq
, void *data
)
3270 listnode_add(mq
->subq
[META_QUEUE_EVPN
], data
);
3276 static int mq_add_handler(void *data
,
3277 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
3279 if (zrouter
.ribq
== NULL
) {
3280 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3281 "%s: work_queue does not exist!", __func__
);
3286 * The RIB queue should normally be either empty or holding the only
3287 * work_queue_item element. In the latter case this element would
3288 * hold a pointer to the meta queue structure, which must be used to
3289 * actually queue the route nodes to process. So create the MQ
3290 * holder, if necessary, then push the work into it in any case.
3291 * This semantics was introduced after 0.99.9 release.
3293 if (work_queue_empty(zrouter
.ribq
))
3294 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
3296 return mq_add_func(zrouter
.mq
, data
);
3299 void mpls_ftn_uninstall(struct zebra_vrf
*zvrf
, enum lsp_types_t type
,
3300 struct prefix
*prefix
, uint8_t route_type
,
3301 uint8_t route_instance
)
3303 struct wq_label_wrapper
*w
;
3305 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3307 w
->type
= WQ_LABEL_FTN_UNINSTALL
;
3308 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3311 w
->route_type
= route_type
;
3312 w
->route_instance
= route_instance
;
3314 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3315 zlog_debug("Early Label Handling for %pFX", prefix
);
3317 mq_add_handler(w
, early_label_meta_queue_add
);
3320 void mpls_zapi_labels_process(bool add_p
, struct zebra_vrf
*zvrf
,
3321 const struct zapi_labels
*zl
)
3323 struct wq_label_wrapper
*w
;
3325 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_label_wrapper
));
3326 w
->type
= WQ_LABEL_LABELS_PROCESS
;
3327 w
->vrf_id
= zvrf
->vrf
->vrf_id
;
3331 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3332 zlog_debug("Early Label Handling: Labels Process");
3334 mq_add_handler(w
, early_label_meta_queue_add
);
3337 /* Add route_node to work queue and schedule processing */
3338 int rib_queue_add(struct route_node
*rn
)
3342 /* Pointless to queue a route_node with no RIB entries to add or remove
3344 if (!rnode_to_ribs(rn
)) {
3345 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
3346 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
3347 zlog_backtrace(LOG_DEBUG
);
3351 return mq_add_handler(rn
, rib_meta_queue_add
);
3355 * Enqueue incoming nhg info from OS for processing
3357 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
3361 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
3365 * Enqueue incoming nhg from proto daemon for processing
3367 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
3372 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
3376 * Enqueue evpn route for processing
3378 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id
, const struct ethaddr
*rmac
,
3379 const struct ipaddr
*vtep_ip
,
3380 const struct prefix
*host_prefix
)
3382 struct wq_evpn_wrapper
*w
;
3384 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3386 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3391 w
->prefix
= *host_prefix
;
3393 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3394 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3395 vrf_id
, vtep_ip
, host_prefix
);
3397 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3400 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id
,
3401 const struct ipaddr
*vtep_ip
,
3402 const struct prefix
*host_prefix
)
3404 struct wq_evpn_wrapper
*w
;
3406 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3408 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
3412 w
->prefix
= *host_prefix
;
3414 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3415 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
3416 vrf_id
, vtep_ip
, host_prefix
);
3418 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3421 /* Enqueue EVPN remote ES for processing */
3422 int zebra_rib_queue_evpn_rem_es_add(const esi_t
*esi
,
3423 const struct in_addr
*vtep_ip
,
3424 bool esr_rxed
, uint8_t df_alg
,
3427 struct wq_evpn_wrapper
*w
;
3428 char buf
[ESI_STR_LEN
];
3430 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3432 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3435 w
->ip
.ipa_type
= IPADDR_V4
;
3436 w
->ip
.ipaddr_v4
= *vtep_ip
;
3437 w
->esr_rxed
= esr_rxed
;
3439 w
->df_pref
= df_pref
;
3441 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3442 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3443 esi_to_str(esi
, buf
, sizeof(buf
)));
3445 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3448 int zebra_rib_queue_evpn_rem_es_del(const esi_t
*esi
,
3449 const struct in_addr
*vtep_ip
)
3451 struct wq_evpn_wrapper
*w
;
3452 char buf
[ESI_STR_LEN
];
3454 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3456 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
3459 w
->ip
.ipa_type
= IPADDR_V4
;
3460 w
->ip
.ipaddr_v4
= *vtep_ip
;
3462 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3463 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3464 esi_to_str(esi
, buf
, sizeof(buf
));
3466 strlcpy(buf
, "-", sizeof(buf
));
3468 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
3472 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3476 * Enqueue EVPN remote macip update for processing
3478 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni
, const struct ethaddr
*macaddr
,
3479 const struct ipaddr
*ipaddr
,
3480 uint8_t flags
, uint32_t seq
,
3481 struct in_addr vtep_ip
, const esi_t
*esi
)
3483 struct wq_evpn_wrapper
*w
;
3484 char buf
[ESI_STR_LEN
];
3486 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3488 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3491 w
->macaddr
= *macaddr
;
3495 w
->vtep_ip
= vtep_ip
;
3498 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
3499 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
3500 esi_to_str(esi
, buf
, sizeof(buf
));
3502 strlcpy(buf
, "-", sizeof(buf
));
3504 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__
,
3505 macaddr
, &vtep_ip
, buf
);
3508 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3511 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni
, const struct ethaddr
*macaddr
,
3512 const struct ipaddr
*ip
,
3513 struct in_addr vtep_ip
)
3515 struct wq_evpn_wrapper
*w
;
3517 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3519 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
3522 w
->macaddr
= *macaddr
;
3524 w
->vtep_ip
= vtep_ip
;
3526 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3527 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__
,
3530 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3534 * Enqueue remote VTEP address for processing
3536 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id
, vni_t vni
,
3537 struct in_addr vtep_ip
, int flood_control
)
3539 struct wq_evpn_wrapper
*w
;
3541 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3543 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3547 w
->vtep_ip
= vtep_ip
;
3548 w
->flags
= flood_control
;
3550 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3551 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3554 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3557 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id
, vni_t vni
,
3558 struct in_addr vtep_ip
)
3560 struct wq_evpn_wrapper
*w
;
3562 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
3564 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
3568 w
->vtep_ip
= vtep_ip
;
3570 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3571 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
3574 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
3577 /* Create new meta queue.
3578 A destructor function doesn't seem to be necessary here.
3580 static struct meta_queue
*meta_queue_new(void)
3582 struct meta_queue
*new;
3585 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
3587 for (i
= 0; i
< MQ_SIZE
; i
++) {
3588 new->subq
[i
] = list_new();
3589 assert(new->subq
[i
]);
3595 /* Clean up the EVPN meta-queue list */
3596 static void evpn_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3597 struct zebra_vrf
*zvrf
)
3599 struct listnode
*node
, *nnode
;
3600 struct wq_evpn_wrapper
*w
;
3602 /* Free the node wrapper object, and the struct it wraps */
3603 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3605 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3607 if (w
->vrf_id
!= vrf_id
)
3613 XFREE(MTYPE_WQ_WRAPPER
, w
);
3615 list_delete_node(l
, node
);
3620 /* Clean up the nhg meta-queue list */
3621 static void nhg_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3622 struct zebra_vrf
*zvrf
)
3624 struct wq_nhg_wrapper
*w
;
3625 struct listnode
*node
, *nnode
;
3627 /* Free the node wrapper object, and the struct it wraps */
3628 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3630 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3632 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
&&
3633 w
->u
.ctx
->vrf_id
!= vrf_id
)
3635 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
&&
3636 w
->u
.nhe
->vrf_id
!= vrf_id
)
3639 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
3640 nhg_ctx_free(&w
->u
.ctx
);
3641 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
3642 zebra_nhg_free(w
->u
.nhe
);
3645 XFREE(MTYPE_WQ_WRAPPER
, w
);
3647 list_delete_node(l
, node
);
3652 static void early_label_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3653 struct zebra_vrf
*zvrf
)
3655 struct wq_label_wrapper
*w
;
3656 struct listnode
*node
, *nnode
;
3658 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3659 if (zvrf
&& zvrf
->vrf
->vrf_id
!= w
->vrf_id
)
3663 case WQ_LABEL_FTN_UNINSTALL
:
3664 case WQ_LABEL_LABELS_PROCESS
:
3669 XFREE(MTYPE_WQ_WRAPPER
, w
);
3670 list_delete_node(l
, node
);
3675 static void rib_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3676 struct zebra_vrf
*zvrf
)
3678 struct route_node
*rnode
;
3679 struct listnode
*node
, *nnode
;
3681 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, rnode
)) {
3682 rib_dest_t
*dest
= rib_dest_from_rnode(rnode
);
3684 if (dest
&& rib_dest_vrf(dest
) != zvrf
)
3687 route_unlock_node(rnode
);
3689 list_delete_node(l
, node
);
3694 static void early_route_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3695 struct zebra_vrf
*zvrf
)
3697 struct zebra_early_route
*ere
;
3698 struct listnode
*node
, *nnode
;
3700 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, ere
)) {
3701 if (zvrf
&& ere
->re
->vrf_id
!= zvrf
->vrf
->vrf_id
)
3704 early_route_memory_free(ere
);
3706 list_delete_node(l
, node
);
3711 void meta_queue_free(struct meta_queue
*mq
, struct zebra_vrf
*zvrf
)
3713 enum meta_queue_indexes i
;
3715 for (i
= 0; i
< MQ_SIZE
; i
++) {
3716 /* Some subqueues may need cleanup - nhgs for example */
3718 case META_QUEUE_NHG
:
3719 nhg_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3721 case META_QUEUE_EVPN
:
3722 evpn_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3724 case META_QUEUE_EARLY_ROUTE
:
3725 early_route_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3727 case META_QUEUE_EARLY_LABEL
:
3728 early_label_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3730 case META_QUEUE_CONNECTED
:
3731 case META_QUEUE_KERNEL
:
3732 case META_QUEUE_STATIC
:
3733 case META_QUEUE_NOTBGP
:
3734 case META_QUEUE_BGP
:
3735 case META_QUEUE_OTHER
:
3736 rib_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3740 list_delete(&mq
->subq
[i
]);
3744 XFREE(MTYPE_WORK_QUEUE
, mq
);
3747 /* initialise zebra rib work queue */
3748 static void rib_queue_init(void)
3750 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
3751 "route_node processing"))) {
3752 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3753 "%s: could not initialise work queue!", __func__
);
3757 /* fill in the work queue spec */
3758 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
3759 zrouter
.ribq
->spec
.completion_func
= NULL
;
3760 /* XXX: TODO: These should be runtime configurable via vty */
3761 zrouter
.ribq
->spec
.max_retries
= 3;
3762 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
3763 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
3765 if (!(zrouter
.mq
= meta_queue_new())) {
3766 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3767 "%s: could not initialise meta queue!", __func__
);
3773 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
3777 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
3778 rnh_list_init(&dest
->nht
);
3779 re_list_init(&dest
->routes
);
3780 route_lock_node(rn
); /* rn route table reference */
3787 /* RIB updates are processed via a queue of pointers to route_nodes.
3789 * The queue length is bounded by the maximal size of the routing table,
3790 * as a route_node will not be requeued, if already queued.
3792 * REs are submitted via rib_addnode or rib_delnode which set minimal
3793 * state, or static_install_route (when an existing RE is updated)
3794 * and then submit route_node to queue for best-path selection later.
3795 * Order of add/delete state changes are preserved for any given RE.
3797 * Deleted REs are reaped during best-path selection.
3800 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3801 * |-------->| | best RE, if required
3803 * static_install->|->rib_addqueue...... -> rib_process
3805 * |-------->| |-> rib_unlink
3806 * |-> set ROUTE_ENTRY_REMOVE |
3807 * rib_delnode (RE freed)
3809 * The 'info' pointer of a route_node points to a rib_dest_t
3810 * ('dest'). Queueing state for a route_node is kept on the dest. The
3811 * dest is created on-demand by rib_link() and is kept around at least
3812 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3814 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3816 * - route_nodes: refcounted by:
3817 * - dest attached to route_node:
3818 * - managed by: rib_link/rib_gc_dest
3819 * - route_node processing queue
3820 * - managed by: rib_addqueue, rib_process.
3824 /* Add RE to head of the route node. */
3825 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
3829 const char *rmap_name
;
3833 dest
= rib_dest_from_rnode(rn
);
3835 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3836 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
3838 dest
= zebra_rib_create_dest(rn
);
3841 re_list_add_head(&dest
->routes
, re
);
3843 afi
= (rn
->p
.family
== AF_INET
)
3845 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3846 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3847 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3849 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
3850 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
3855 static void rib_addnode(struct route_node
*rn
,
3856 struct route_entry
*re
, int process
)
3858 /* RE node has been un-removed before route-node is processed.
3859 * route_node must hence already be on the queue for processing..
3861 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3862 if (IS_ZEBRA_DEBUG_RIB
)
3863 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
3864 (void *)rn
, (void *)re
);
3866 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3869 rib_link(rn
, re
, process
);
3875 * Detach a rib structure from a route_node.
3877 * Note that a call to rib_unlink() should be followed by a call to
3878 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3879 * longer required to be deleted.
3881 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
3887 if (IS_ZEBRA_DEBUG_RIB
)
3888 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
3891 dest
= rib_dest_from_rnode(rn
);
3893 re_list_del(&dest
->routes
, re
);
3895 if (dest
->selected_fib
== re
)
3896 dest
->selected_fib
= NULL
;
3898 rib_re_nhg_free(re
);
3900 zapi_re_opaque_free(re
->opaque
);
3902 XFREE(MTYPE_RE
, re
);
3905 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
3909 if (IS_ZEBRA_DEBUG_RIB
)
3910 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
3911 (void *)rn
, (void *)re
);
3912 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3914 afi
= (rn
->p
.family
== AF_INET
)
3916 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3917 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3918 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3920 zebra_del_import_table_entry(zvrf
, rn
, re
);
3921 /* Just clean up if non main table */
3922 if (IS_ZEBRA_DEBUG_RIB
)
3923 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3924 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, rn
,
3925 rn
, re
, zebra_route_string(re
->type
));
3934 * Helper that debugs a single nexthop within a route-entry
3936 static void _route_entry_dump_nh(const struct route_entry
*re
,
3937 const char *straddr
,
3938 const struct nexthop
*nexthop
)
3940 char nhname
[PREFIX_STRLEN
];
3941 char backup_str
[50];
3944 char label_str
[MPLS_LABEL_STRLEN
];
3946 struct interface
*ifp
;
3947 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
3949 switch (nexthop
->type
) {
3950 case NEXTHOP_TYPE_BLACKHOLE
:
3951 snprintf(nhname
, sizeof(nhname
), "Blackhole");
3953 case NEXTHOP_TYPE_IFINDEX
:
3954 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
3955 snprintf(nhname
, sizeof(nhname
), "%s",
3956 ifp
? ifp
->name
: "Unknown");
3958 case NEXTHOP_TYPE_IPV4
:
3960 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3961 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3963 case NEXTHOP_TYPE_IPV6
:
3964 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3965 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3970 label_str
[0] = '\0';
3971 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
3972 mpls_label2str(nexthop
->nh_label
->num_labels
,
3973 nexthop
->nh_label
->label
, label_str
,
3974 sizeof(label_str
), 0 /*pretty*/);
3975 strlcat(label_str
, ", ", sizeof(label_str
));
3978 backup_str
[0] = '\0';
3979 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3980 snprintf(backup_str
, sizeof(backup_str
), "backup ");
3981 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
3982 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
3983 nexthop
->backup_idx
[i
]);
3984 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
3989 if (nexthop
->weight
)
3990 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
3992 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
3993 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
3994 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
3996 wgt_str
, backup_str
,
3997 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
4000 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
4003 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
4006 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
4009 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
4012 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
4013 ? "FILTERED " : ""),
4014 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
4016 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
4018 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)
4023 /* This function dumps the contents of a given RE entry into
4024 * standard debug log. Calling function name and IP prefix in
4025 * question are passed as 1st and 2nd arguments.
4027 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
4028 union prefixconstptr src_pp
,
4029 const struct route_entry
*re
)
4031 const struct prefix
*src_p
= src_pp
.p
;
4032 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
4033 char straddr
[PREFIX_STRLEN
];
4034 char srcaddr
[PREFIX_STRLEN
];
4035 char flags_buf
[128];
4036 char status_buf
[128];
4037 struct nexthop
*nexthop
;
4038 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
4039 struct nexthop_group
*nhg
;
4041 prefix2str(pp
, straddr
, sizeof(straddr
));
4043 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
4044 (const void *)re
, straddr
,
4045 is_srcdst
? " from " : "",
4046 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
4048 VRF_LOGNAME(vrf
), re
->vrf_id
);
4049 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
4050 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
4053 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
4054 straddr
, re
->metric
, re
->mtu
, re
->distance
,
4055 zclient_dump_route_flags(re
->flags
, flags_buf
,
4057 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
4058 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
4059 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
4060 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
4063 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4064 _route_entry_dump_nh(re
, straddr
, nexthop
);
4066 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
4067 zlog_debug("%s: backup nexthops:", straddr
);
4069 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
4070 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
4071 _route_entry_dump_nh(re
, straddr
, nexthop
);
4074 zlog_debug("%s: dump complete", straddr
);
4077 static int rib_meta_queue_early_route_add(struct meta_queue
*mq
, void *data
)
4079 struct zebra_early_route
*ere
= data
;
4081 listnode_add(mq
->subq
[META_QUEUE_EARLY_ROUTE
], data
);
4084 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
4086 "Route %pFX(%u) queued for processing into sub-queue %s",
4087 &ere
->p
, ere
->re
->vrf_id
,
4088 subqueue2str(META_QUEUE_EARLY_ROUTE
));
4093 struct route_entry
*zebra_rib_route_entry_new(vrf_id_t vrf_id
, int type
,
4094 uint8_t instance
, uint32_t flags
,
4097 uint32_t metric
, uint32_t mtu
,
4098 uint8_t distance
, route_tag_t tag
)
4100 struct route_entry
*re
;
4102 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
4104 re
->instance
= instance
;
4105 re
->distance
= distance
;
4107 re
->metric
= metric
;
4109 re
->table
= table_id
;
4110 re
->vrf_id
= vrf_id
;
4111 re
->uptime
= monotime(NULL
);
4113 re
->nhe_id
= nhe_id
;
4118 * Internal route-add implementation; there are a couple of different public
4119 * signatures. Callers in this path are responsible for the memory they
4120 * allocate: if they allocate a nexthop_group or backup nexthop info, they
4121 * must free those objects. If this returns < 0, an error has occurred and the
4122 * route_entry 're' has not been captured; the caller should free that also.
4128 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
4129 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4130 struct nhg_hash_entry
*re_nhe
, bool startup
)
4132 struct zebra_early_route
*ere
;
4137 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
4139 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4144 ere
->src_p
= *src_p
;
4145 ere
->src_p_provided
= !!src_p
;
4147 ere
->re_nhe
= re_nhe
;
4148 ere
->startup
= startup
;
4150 return mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4154 * Add a single route.
4156 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
4157 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
4158 struct nexthop_group
*ng
, bool startup
)
4161 struct nhg_hash_entry nhe
, *n
;
4166 /* We either need nexthop(s) or an existing nexthop id */
4167 if (ng
== NULL
&& re
->nhe_id
== 0)
4171 * Use a temporary nhe to convey info to the common/main api.
4173 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
4175 nhe
.nhg
.nexthop
= ng
->nexthop
;
4176 else if (re
->nhe_id
> 0)
4177 nhe
.id
= re
->nhe_id
;
4179 n
= zebra_nhe_copy(&nhe
, 0);
4180 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, n
, startup
);
4182 /* In error cases, free the route also */
4184 XFREE(MTYPE_RE
, re
);
4189 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4190 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4191 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4192 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
4193 uint8_t distance
, bool fromkernel
)
4195 struct zebra_early_route
*ere
;
4196 struct route_entry
*re
= NULL
;
4197 struct nhg_hash_entry
*nhe
= NULL
;
4199 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4200 table_id
, metric
, 0, distance
, 0);
4203 nhe
= zebra_nhg_alloc();
4204 nhe
->nhg
.nexthop
= nexthop_dup(nh
, NULL
);
4207 ere
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(*ere
));
4212 ere
->src_p
= *src_p
;
4213 ere
->src_p_provided
= !!src_p
;
4216 ere
->startup
= false;
4217 ere
->deletion
= true;
4218 ere
->fromkernel
= fromkernel
;
4220 mq_add_handler(ere
, rib_meta_queue_early_route_add
);
4224 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
4225 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
4226 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
4227 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
4228 uint8_t distance
, route_tag_t tag
, bool startup
)
4230 struct route_entry
*re
= NULL
;
4231 struct nexthop nexthop
= {};
4232 struct nexthop_group ng
= {};
4234 /* Allocate new route_entry structure. */
4235 re
= zebra_rib_route_entry_new(vrf_id
, type
, instance
, flags
, nhe_id
,
4236 table_id
, metric
, mtu
, distance
, tag
);
4238 /* If the owner of the route supplies a shared nexthop-group id,
4239 * we'll use that. Otherwise, pass the nexthop along directly.
4244 nexthop_group_add_sorted(&ng
, &nexthop
);
4247 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, &ng
, startup
);
4250 static const char *rib_update_event2str(enum rib_update_event event
)
4252 const char *ret
= "UNKNOWN";
4255 case RIB_UPDATE_KERNEL
:
4256 ret
= "RIB_UPDATE_KERNEL";
4258 case RIB_UPDATE_RMAP_CHANGE
:
4259 ret
= "RIB_UPDATE_RMAP_CHANGE";
4261 case RIB_UPDATE_OTHER
:
4262 ret
= "RIB_UPDATE_OTHER";
4264 case RIB_UPDATE_MAX
:
4272 /* Schedule route nodes to be processed if they match the type */
4273 static void rib_update_route_node(struct route_node
*rn
, int type
)
4275 struct route_entry
*re
, *next
;
4276 bool re_changed
= false;
4278 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4279 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
4280 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
4289 /* Schedule routes of a particular table (address-family) based on event. */
4290 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
4293 struct route_node
*rn
;
4295 if (IS_ZEBRA_DEBUG_EVENT
) {
4296 struct zebra_vrf
*zvrf
;
4300 ? ((struct rib_table_info
*)table
->info
)->zvrf
4302 vrf
= zvrf
? zvrf
->vrf
: NULL
;
4304 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
4305 table
->info
? afi2str(
4306 ((struct rib_table_info
*)table
->info
)->afi
)
4308 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
4309 rib_update_event2str(event
), zebra_route_string(rtype
));
4312 /* Walk all routes and queue for processing, if appropriate for
4313 * the trigger event.
4315 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4317 * If we are looking at a route node and the node
4318 * has already been queued we don't
4319 * need to queue it up again
4322 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
4323 RIB_ROUTE_ANY_QUEUED
))
4327 case RIB_UPDATE_KERNEL
:
4328 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
4330 case RIB_UPDATE_RMAP_CHANGE
:
4331 case RIB_UPDATE_OTHER
:
4332 rib_update_route_node(rn
, rtype
);
4340 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
,
4343 struct route_table
*table
;
4345 if (IS_ZEBRA_DEBUG_EVENT
)
4346 zlog_debug("%s: Handling VRF %s event %s", __func__
,
4347 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
4349 /* Process routes of interested address-families. */
4350 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
4352 rib_update_table(table
, event
, rtype
);
4354 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
4356 rib_update_table(table
, event
, rtype
);
4359 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
4361 struct zebra_router_table
*zrt
;
4363 if (IS_ZEBRA_DEBUG_EVENT
)
4364 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
4365 rib_update_event2str(event
));
4367 /* Just iterate over all the route tables, rather than vrf lookups */
4368 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
4369 rib_update_table(zrt
->table
, event
, rtype
);
4372 struct rib_update_ctx
{
4373 enum rib_update_event event
;
4378 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
4379 enum rib_update_event event
)
4381 struct rib_update_ctx
*ctx
;
4383 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
4386 ctx
->vrf_id
= vrf_id
;
4391 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
4393 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
4396 static void rib_update_handler(struct thread
*thread
)
4398 struct rib_update_ctx
*ctx
;
4400 ctx
= THREAD_ARG(thread
);
4403 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
4405 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
, ZEBRA_ROUTE_ALL
);
4407 rib_update_ctx_fini(&ctx
);
4411 * Thread list to ensure we don't schedule a ton of events
4412 * if interfaces are flapping for instance.
4414 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
4416 /* Schedule a RIB update event for all vrfs */
4417 void rib_update(enum rib_update_event event
)
4419 struct rib_update_ctx
*ctx
;
4421 if (thread_is_scheduled(t_rib_update_threads
[event
]))
4424 ctx
= rib_update_ctx_init(0, event
);
4425 ctx
->vrf_all
= true;
4427 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
4428 &t_rib_update_threads
[event
]);
4430 if (IS_ZEBRA_DEBUG_EVENT
)
4431 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
4432 rib_update_event2str(event
));
4435 /* Delete self installed routes after zebra is relaunched. */
4436 void rib_sweep_table(struct route_table
*table
)
4438 struct route_node
*rn
;
4439 struct route_entry
*re
;
4440 struct route_entry
*next
;
4441 struct nexthop
*nexthop
;
4446 if (IS_ZEBRA_DEBUG_RIB
)
4447 zlog_debug("%s: starting", __func__
);
4449 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4450 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4452 if (IS_ZEBRA_DEBUG_RIB
)
4453 route_entry_dump(&rn
->p
, NULL
, re
);
4455 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4458 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
4462 * If routes are older than startup_time then
4463 * we know we read them in from the kernel.
4464 * As such we can safely remove them.
4466 if (zrouter
.startup_time
< re
->uptime
)
4470 * So we are starting up and have received
4471 * routes from the kernel that we have installed
4472 * from a previous run of zebra but not cleaned
4473 * up ( say a kill -9 )
4474 * But since we haven't actually installed
4475 * them yet( we received them from the kernel )
4476 * we don't think they are active.
4477 * So let's pretend they are active to actually
4479 * In all honesty I'm not sure if we should
4480 * mark them as active when we receive them
4481 * This is startup only so probably ok.
4483 * If we ever decide to move rib_sweep_table
4484 * to a different spot (ie startup )
4485 * this decision needs to be revisited
4487 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
4488 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4489 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
4491 rib_uninstall_kernel(rn
, re
);
4492 rib_delnode(rn
, re
);
4496 if (IS_ZEBRA_DEBUG_RIB
)
4497 zlog_debug("%s: ends", __func__
);
4500 /* Sweep all RIB tables. */
4501 void rib_sweep_route(struct thread
*t
)
4504 struct zebra_vrf
*zvrf
;
4506 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4507 if ((zvrf
= vrf
->info
) == NULL
)
4510 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4511 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4514 zebra_router_sweep_route();
4515 zebra_router_sweep_nhgs();
4518 /* Remove specific by protocol routes from 'table'. */
4519 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
4520 struct route_table
*table
)
4522 struct route_node
*rn
;
4523 struct route_entry
*re
;
4524 struct route_entry
*next
;
4525 unsigned long n
= 0;
4528 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
4529 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4530 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4532 if (re
->type
== proto
4533 && re
->instance
== instance
) {
4534 rib_delnode(rn
, re
);
4541 /* Remove specific by protocol routes. */
4542 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
4545 struct zebra_vrf
*zvrf
;
4546 struct other_route_table
*ort
;
4547 unsigned long cnt
= 0;
4549 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4554 cnt
+= rib_score_proto_table(proto
, instance
,
4555 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4556 + rib_score_proto_table(
4558 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4560 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
4561 rib_score_proto_table(proto
, instance
, ort
->table
);
4567 /* Close RIB and clean up kernel routes. */
4568 void rib_close_table(struct route_table
*table
)
4570 struct route_node
*rn
;
4576 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4577 dest
= rib_dest_from_rnode(rn
);
4579 if (dest
&& dest
->selected_fib
) {
4580 rib_uninstall_kernel(rn
, dest
->selected_fib
);
4581 dest
->selected_fib
= NULL
;
4587 * Handler for async dataplane results after a pseudowire installation
4589 static void handle_pw_result(struct zebra_dplane_ctx
*ctx
)
4591 struct zebra_pw
*pw
;
4592 struct zebra_vrf
*vrf
;
4594 /* The pseudowire code assumes success - we act on an error
4595 * result for installation attempts here.
4597 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
4600 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
4601 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
4602 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
4604 zebra_pw_install_failure(pw
,
4605 dplane_ctx_get_pw_status(ctx
));
4610 * Handle results from the dataplane system. Dequeue update context
4611 * structs, dispatch to appropriate internal handlers.
4613 static void rib_process_dplane_results(struct thread
*thread
)
4615 struct zebra_dplane_ctx
*ctx
;
4616 struct dplane_ctx_q ctxlist
;
4617 bool shut_p
= false;
4619 /* Dequeue a list of completed updates with one lock/unlock cycle */
4622 TAILQ_INIT(&ctxlist
);
4624 /* Take lock controlling queue of results */
4625 frr_with_mutex (&dplane_mutex
) {
4626 /* Dequeue list of context structs */
4627 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
4630 /* Dequeue context block */
4631 ctx
= dplane_ctx_dequeue(&ctxlist
);
4633 /* If we've emptied the results queue, we're done */
4637 /* If zebra is shutting down, avoid processing results,
4638 * just drain the results queue.
4640 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
4641 memory_order_relaxed
);
4644 dplane_ctx_fini(&ctx
);
4646 ctx
= dplane_ctx_dequeue(&ctxlist
);
4652 #ifdef HAVE_SCRIPTING
4653 char *script_name
= frrscript_names_get_script_name(
4654 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
4657 struct frrscript
*fs
;
4660 fs
= frrscript_new(script_name
);
4662 ret
= frrscript_load(
4663 fs
, ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4666 #endif /* HAVE_SCRIPTING */
4670 #ifdef HAVE_SCRIPTING
4673 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4675 #endif /* HAVE_SCRIPTING */
4677 switch (dplane_ctx_get_op(ctx
)) {
4678 case DPLANE_OP_ROUTE_INSTALL
:
4679 case DPLANE_OP_ROUTE_UPDATE
:
4680 case DPLANE_OP_ROUTE_DELETE
:
4681 /* Bit of special case for route updates
4682 * that were generated by async notifications:
4683 * we don't want to continue processing these
4686 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4687 rib_process_result(ctx
);
4690 case DPLANE_OP_ROUTE_NOTIFY
:
4691 rib_process_dplane_notify(ctx
);
4694 case DPLANE_OP_NH_INSTALL
:
4695 case DPLANE_OP_NH_UPDATE
:
4696 case DPLANE_OP_NH_DELETE
:
4697 zebra_nhg_dplane_result(ctx
);
4700 case DPLANE_OP_LSP_INSTALL
:
4701 case DPLANE_OP_LSP_UPDATE
:
4702 case DPLANE_OP_LSP_DELETE
:
4703 /* Bit of special case for LSP updates
4704 * that were generated by async notifications:
4705 * we don't want to continue processing these.
4707 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4708 zebra_mpls_lsp_dplane_result(ctx
);
4711 case DPLANE_OP_LSP_NOTIFY
:
4712 zebra_mpls_process_dplane_notify(ctx
);
4715 case DPLANE_OP_PW_INSTALL
:
4716 case DPLANE_OP_PW_UNINSTALL
:
4717 handle_pw_result(ctx
);
4720 case DPLANE_OP_SYS_ROUTE_ADD
:
4721 case DPLANE_OP_SYS_ROUTE_DELETE
:
4724 case DPLANE_OP_MAC_INSTALL
:
4725 case DPLANE_OP_MAC_DELETE
:
4726 zebra_vxlan_handle_result(ctx
);
4729 case DPLANE_OP_RULE_ADD
:
4730 case DPLANE_OP_RULE_DELETE
:
4731 case DPLANE_OP_RULE_UPDATE
:
4732 case DPLANE_OP_IPTABLE_ADD
:
4733 case DPLANE_OP_IPTABLE_DELETE
:
4734 case DPLANE_OP_IPSET_ADD
:
4735 case DPLANE_OP_IPSET_DELETE
:
4736 case DPLANE_OP_IPSET_ENTRY_ADD
:
4737 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4738 zebra_pbr_dplane_result(ctx
);
4741 case DPLANE_OP_INTF_ADDR_ADD
:
4742 case DPLANE_OP_INTF_ADDR_DEL
:
4743 case DPLANE_OP_INTF_INSTALL
:
4744 case DPLANE_OP_INTF_UPDATE
:
4745 case DPLANE_OP_INTF_DELETE
:
4746 case DPLANE_OP_INTF_NETCONFIG
:
4747 zebra_if_dplane_result(ctx
);
4750 case DPLANE_OP_TC_QDISC_INSTALL
:
4751 case DPLANE_OP_TC_QDISC_UNINSTALL
:
4752 case DPLANE_OP_TC_CLASS_ADD
:
4753 case DPLANE_OP_TC_CLASS_DELETE
:
4754 case DPLANE_OP_TC_CLASS_UPDATE
:
4755 case DPLANE_OP_TC_FILTER_ADD
:
4756 case DPLANE_OP_TC_FILTER_DELETE
:
4757 case DPLANE_OP_TC_FILTER_UPDATE
:
4760 /* Some op codes not handled here */
4761 case DPLANE_OP_ADDR_INSTALL
:
4762 case DPLANE_OP_ADDR_UNINSTALL
:
4763 case DPLANE_OP_NEIGH_INSTALL
:
4764 case DPLANE_OP_NEIGH_UPDATE
:
4765 case DPLANE_OP_NEIGH_DELETE
:
4766 case DPLANE_OP_NEIGH_IP_INSTALL
:
4767 case DPLANE_OP_NEIGH_IP_DELETE
:
4768 case DPLANE_OP_VTEP_ADD
:
4769 case DPLANE_OP_VTEP_DELETE
:
4770 case DPLANE_OP_NEIGH_DISCOVER
:
4771 case DPLANE_OP_BR_PORT_UPDATE
:
4772 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4773 case DPLANE_OP_GRE_SET
:
4774 case DPLANE_OP_NONE
:
4777 } /* Dispatch by op code */
4779 dplane_ctx_fini(&ctx
);
4780 ctx
= dplane_ctx_dequeue(&ctxlist
);
4787 * Results are returned from the dataplane subsystem, in the context of
4788 * the dataplane pthread. We enqueue the results here for processing by
4789 * the main thread later.
4791 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
4793 /* Take lock controlling queue of results */
4794 frr_with_mutex (&dplane_mutex
) {
4795 /* Enqueue context blocks */
4796 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4799 /* Ensure event is signalled to zebra main pthread */
4800 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4807 * Ensure there are no empty slots in the route_info array.
4808 * Every route type in zebra should be present there.
4810 static void check_route_info(void)
4812 int len
= array_size(route_info
);
4815 * ZEBRA_ROUTE_SYSTEM is special cased since
4816 * its key is 0 anyway.
4818 * ZEBRA_ROUTE_ALL is also ignored.
4820 for (int i
= 0; i
< len
; i
++) {
4821 assert(route_info
[i
].key
>= ZEBRA_ROUTE_SYSTEM
&&
4822 route_info
[i
].key
< ZEBRA_ROUTE_MAX
);
4823 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4827 /* Routing information base initialize. */
4834 /* Init dataplane, and register for results */
4835 pthread_mutex_init(&dplane_mutex
, NULL
);
4836 TAILQ_INIT(&rib_dplane_q
);
4837 zebra_dplane_init(rib_dplane_results
);
4843 * Get the first vrf id that is greater than the given vrf id if any.
4845 * Returns true if a vrf id was found, false otherwise.
4847 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4851 vrf
= vrf_lookup_by_id(vrf_id
);
4853 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4855 *next_id_p
= vrf
->vrf_id
;
4864 * rib_tables_iter_next
4866 * Returns the next table in the iteration.
4868 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4870 struct route_table
*table
;
4873 * Array that helps us go over all AFI/SAFI combinations via one
4876 static const struct {
4880 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4881 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4882 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4887 switch (iter
->state
) {
4889 case RIB_TABLES_ITER_S_INIT
:
4890 iter
->vrf_id
= VRF_DEFAULT
;
4891 iter
->afi_safi_ix
= -1;
4895 case RIB_TABLES_ITER_S_ITERATING
:
4896 iter
->afi_safi_ix
++;
4899 while (iter
->afi_safi_ix
4900 < (int)array_size(afi_safis
)) {
4901 table
= zebra_vrf_table(
4902 afi_safis
[iter
->afi_safi_ix
].afi
,
4903 afi_safis
[iter
->afi_safi_ix
].safi
,
4908 iter
->afi_safi_ix
++;
4912 * Found another table in this vrf.
4918 * Done with all tables in the current vrf, go to the
4922 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4925 iter
->afi_safi_ix
= 0;
4930 case RIB_TABLES_ITER_S_DONE
:
4935 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4937 iter
->state
= RIB_TABLES_ITER_S_DONE
;