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
),
80 /* Meta Q's specific names */
81 enum meta_queue_indexes
{
92 /* Each route type's string and default distance value. */
96 enum meta_queue_indexes meta_q_map
;
97 } route_info
[ZEBRA_ROUTE_MAX
] = {
98 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Unneeded for nhg's */,
100 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, META_QUEUE_KERNEL
},
101 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, META_QUEUE_KERNEL
},
102 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, META_QUEUE_CONNECTED
},
103 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, META_QUEUE_STATIC
},
104 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, META_QUEUE_NOTBGP
},
105 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, META_QUEUE_NOTBGP
},
106 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, META_QUEUE_NOTBGP
},
107 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, META_QUEUE_NOTBGP
},
108 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, META_QUEUE_NOTBGP
},
109 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */,
111 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, META_QUEUE_OTHER
},
112 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, META_QUEUE_NOTBGP
},
113 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, META_QUEUE_NOTBGP
},
114 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, META_QUEUE_OTHER
},
115 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, META_QUEUE_OTHER
},
116 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, META_QUEUE_STATIC
},
117 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, META_QUEUE_OTHER
},
118 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, META_QUEUE_BGP
},
119 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, META_QUEUE_BGP
},
120 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20,
122 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, META_QUEUE_BGP
},
123 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20,
125 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, META_QUEUE_NOTBGP
},
126 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, META_QUEUE_OTHER
},
127 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, META_QUEUE_OTHER
},
128 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, META_QUEUE_OTHER
},
129 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115,
131 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, META_QUEUE_OTHER
},
132 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, META_QUEUE_OTHER
},
133 [ZEBRA_ROUTE_ALL
] = {ZEBRA_ROUTE_ALL
, 255, META_QUEUE_OTHER
},
134 /* Any new route type added to zebra, should be mirrored here */
136 /* no entry/default: 150 */
139 /* Wrapper struct for nhg workqueue items; a 'ctx' is an incoming update
140 * from the OS, and an 'nhe' is a nhe update.
142 struct wq_nhg_wrapper
{
146 struct nhg_hash_entry
*nhe
;
150 #define WQ_NHG_WRAPPER_TYPE_CTX 0x01
151 #define WQ_NHG_WRAPPER_TYPE_NHG 0x02
153 /* Wrapper structs for evpn/vxlan workqueue items. */
154 struct wq_evpn_wrapper
{
166 struct ethaddr macaddr
;
167 struct prefix prefix
;
168 struct in_addr vtep_ip
;
171 #define WQ_EVPN_WRAPPER_TYPE_VRFROUTE 0x01
172 #define WQ_EVPN_WRAPPER_TYPE_REM_ES 0x02
173 #define WQ_EVPN_WRAPPER_TYPE_REM_MACIP 0x03
174 #define WQ_EVPN_WRAPPER_TYPE_REM_VTEP 0x04
176 /* %pRN is already a printer for route_nodes that just prints the prefix */
177 #ifdef _FRR_ATTRIBUTE_PRINTFRR
178 #pragma FRR printfrr_ext "%pZN" (struct route_node *)
181 static const char *subqueue2str(enum meta_queue_indexes index
)
185 return "NHG Objects";
186 case META_QUEUE_EVPN
:
187 return "EVPN/VxLan Objects";
188 case META_QUEUE_CONNECTED
:
189 return "Connected Routes";
190 case META_QUEUE_KERNEL
:
191 return "Kernel Routes";
192 case META_QUEUE_STATIC
:
193 return "Static Routes";
194 case META_QUEUE_NOTBGP
:
195 return "RIP/OSPF/ISIS/EIGRP/NHRP Routes";
198 case META_QUEUE_OTHER
:
199 return "Other Routes";
205 printfrr_ext_autoreg_p("ZN", printfrr_zebra_node
);
206 static ssize_t
printfrr_zebra_node(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
209 struct route_node
*rn
= (struct route_node
*)ptr
;
212 /* just the table number? */
213 if (ea
->fmt
[0] == 't') {
215 struct route_entry
*re
= NULL
;
220 return bputch(buf
, '!');
222 dest
= rib_dest_from_rnode(rn
);
224 re
= re_list_first(&dest
->routes
);
226 rv
+= bprintfrr(buf
, "%u", re
->table
);
228 rv
+= bputch(buf
, '?');
231 char cbuf
[PREFIX_STRLEN
* 2 + 6];
232 struct rib_table_info
*info
;
235 return bputs(buf
, "{(route_node *) NULL}");
237 srcdest_rnode2str(rn
, cbuf
, sizeof(cbuf
));
238 rv
+= bputs(buf
, cbuf
);
240 info
= srcdest_rnode_table_info(rn
);
241 if (info
->safi
== SAFI_MULTICAST
)
242 rv
+= bputs(buf
, " (MRIB)");
247 #define rnode_debug(node, vrf_id, msg, ...) \
248 zlog_debug("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
251 #define rnode_info(node, vrf_id, msg, ...) \
252 zlog_info("%s: (%u:%pZNt):%pZN: " msg, __func__, vrf_id, node, node, \
255 static char *_dump_re_status(const struct route_entry
*re
, char *buf
,
258 if (re
->status
== 0) {
259 snprintfrr(buf
, len
, "None ");
264 buf
, len
, "%s%s%s%s%s%s%s",
265 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
) ? "Removed " : "",
266 CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) ? "Changed " : "",
267 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
270 CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
) ? "Queued " : "",
271 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
273 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
274 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
279 uint8_t route_distance(int type
)
283 if ((unsigned)type
>= array_size(route_info
))
286 distance
= route_info
[type
].distance
;
291 int is_zebra_valid_kernel_table(uint32_t table_id
)
294 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
295 || (table_id
== RT_TABLE_COMPAT
))
302 int is_zebra_main_routing_table(uint32_t table_id
)
304 if (table_id
== RT_TABLE_MAIN
)
309 int zebra_check_addr(const struct prefix
*p
)
311 if (p
->family
== AF_INET
) {
314 addr
= p
->u
.prefix4
.s_addr
;
317 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
318 || IPV4_LINKLOCAL(addr
))
321 if (p
->family
== AF_INET6
) {
322 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
324 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
330 static void route_entry_attach_ref(struct route_entry
*re
,
331 struct nhg_hash_entry
*new)
334 re
->nhe_id
= new->id
;
335 re
->nhe_installed_id
= 0;
337 zebra_nhg_increment_ref(new);
340 /* Replace (if 'new_nhghe') or clear (if that's NULL) an re's nhe. */
341 int route_entry_update_nhe(struct route_entry
*re
,
342 struct nhg_hash_entry
*new_nhghe
)
345 struct nhg_hash_entry
*old_nhg
= NULL
;
347 if (new_nhghe
== NULL
) {
351 re
->nhe_installed_id
= 0;
356 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
357 /* Capture previous nhg, if any */
360 route_entry_attach_ref(re
, new_nhghe
);
362 /* This is the first time it's being attached */
363 route_entry_attach_ref(re
, new_nhghe
);
366 /* Detach / deref previous nhg */
368 zebra_nhg_decrement_ref(old_nhg
);
373 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
374 struct nhg_hash_entry
*new_entry
)
376 struct zebra_router_table
*zrt
;
377 struct route_node
*rn
;
378 struct route_entry
*re
, *next
;
380 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
381 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
382 __func__
, new_entry
->id
, new_entry
, old_entry
);
384 /* We have to do them ALL */
385 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
386 for (rn
= route_top(zrt
->table
); rn
;
387 rn
= srcdest_route_next(rn
)) {
388 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
389 if (re
->nhe
&& re
->nhe
== old_entry
)
390 route_entry_update_nhe(re
, new_entry
);
396 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
397 const union g_addr
*addr
,
398 struct route_node
**rn_out
)
401 struct route_table
*table
;
402 struct route_node
*rn
;
403 struct route_entry
*match
= NULL
;
406 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
410 memset(&p
, 0, sizeof(p
));
413 p
.u
.prefix4
= addr
->ipv4
;
414 p
.prefixlen
= IPV4_MAX_BITLEN
;
416 p
.u
.prefix6
= addr
->ipv6
;
417 p
.prefixlen
= IPV6_MAX_BITLEN
;
420 rn
= route_node_match(table
, &p
);
425 route_unlock_node(rn
);
427 dest
= rib_dest_from_rnode(rn
);
428 if (dest
&& dest
->selected_fib
429 && !CHECK_FLAG(dest
->selected_fib
->status
,
430 ROUTE_ENTRY_REMOVED
))
431 match
= dest
->selected_fib
;
433 /* If there is no selected route or matched route is EGP, go up
438 } while (rn
&& rn
->info
== NULL
);
442 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
443 if (!CHECK_FLAG(match
->status
,
444 ROUTE_ENTRY_INSTALLED
))
456 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
458 struct route_node
**rn_out
)
460 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
461 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
462 union g_addr gaddr
= {.ipv4
= addr
};
464 switch (zrouter
.ipv4_multicast_mode
) {
465 case MCAST_MRIB_ONLY
:
466 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
468 case MCAST_URIB_ONLY
:
469 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
470 case MCAST_NO_CONFIG
:
471 case MCAST_MIX_MRIB_FIRST
:
472 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
475 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
478 case MCAST_MIX_DISTANCE
:
479 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
480 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
482 re
= ure
->distance
< mre
->distance
? ure
: mre
;
488 case MCAST_MIX_PFXLEN
:
489 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
490 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
492 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
501 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
503 if (IS_ZEBRA_DEBUG_RIB
) {
505 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
507 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
508 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
509 mre
? (ure
? "MRIB+URIB" : "MRIB")
510 : ure
? "URIB" : "nothing",
511 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
516 struct route_entry
*rib_match_ipv6_multicast(vrf_id_t vrf_id
,
517 struct in6_addr addr
,
518 struct route_node
**rn_out
)
520 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
521 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
522 union g_addr gaddr
= {.ipv6
= addr
};
524 switch (zrouter
.ipv4_multicast_mode
) {
525 case MCAST_MRIB_ONLY
:
526 return rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
528 case MCAST_URIB_ONLY
:
529 return rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
530 case MCAST_NO_CONFIG
:
531 case MCAST_MIX_MRIB_FIRST
:
532 re
= mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
535 re
= ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
,
538 case MCAST_MIX_DISTANCE
:
539 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
540 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
542 re
= ure
->distance
< mre
->distance
? ure
: mre
;
548 case MCAST_MIX_PFXLEN
:
549 mre
= rib_match(AFI_IP6
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
550 ure
= rib_match(AFI_IP6
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
552 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
561 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
563 if (IS_ZEBRA_DEBUG_RIB
)
564 zlog_debug("%s: %pI6: vrf: %s(%u) found %s, using %s", __func__
,
565 &addr
, vrf_id_to_name(vrf_id
), vrf_id
,
566 mre
? (ure
? "MRIB+URIB" : "MRIB")
567 : ure
? "URIB" : "nothing",
568 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
572 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
574 struct route_table
*table
;
575 struct route_node
*rn
;
576 struct route_entry
*match
= NULL
;
580 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
584 rn
= route_node_lookup(table
, (struct prefix
*)p
);
586 /* No route for this prefix. */
591 route_unlock_node(rn
);
592 dest
= rib_dest_from_rnode(rn
);
594 if (dest
&& dest
->selected_fib
595 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
596 match
= dest
->selected_fib
;
601 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
604 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
611 * Is this RIB labeled-unicast? It must be of type BGP and all paths
612 * (nexthops) must have a label.
614 int zebra_rib_labeled_unicast(struct route_entry
*re
)
616 struct nexthop
*nexthop
= NULL
;
618 if (re
->type
!= ZEBRA_ROUTE_BGP
)
621 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
622 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
628 /* Update flag indicates whether this is a "replace" or not. Currently, this
629 * is only used for IPv4.
631 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
632 struct route_entry
*old
)
634 struct nexthop
*nexthop
;
635 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
636 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
637 const struct prefix
*p
, *src_p
;
638 enum zebra_dplane_result ret
;
640 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
642 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
644 if (info
->safi
!= SAFI_UNICAST
) {
645 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
646 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
652 * Install the resolved nexthop object first.
654 zebra_nhg_install_kernel(re
->nhe
);
657 * If this is a replace to a new RE let the originator of the RE
658 * know that they've lost
660 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
661 zsend_route_notify_owner(rn
, old
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
662 info
->afi
, info
->safi
);
664 /* Update fib selection */
665 dest
->selected_fib
= re
;
668 * Make sure we update the FPM any time we send new information to
671 hook_call(rib_update
, rn
, "installing in kernel");
673 /* Send add or update */
675 ret
= dplane_route_update(rn
, re
, old
);
677 ret
= dplane_route_add(rn
, re
);
680 case ZEBRA_DPLANE_REQUEST_QUEUED
:
681 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
684 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
686 /* Free old FIB nexthop group */
687 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
688 if (old
->fib_ng
.nexthop
) {
689 nexthops_free(old
->fib_ng
.nexthop
);
690 old
->fib_ng
.nexthop
= NULL
;
695 zvrf
->installs_queued
++;
697 case ZEBRA_DPLANE_REQUEST_FAILURE
:
699 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
700 "%u:%u:%pRN: Failed to enqueue dataplane install",
701 re
->vrf_id
, re
->table
, rn
);
704 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
713 /* Uninstall the route from kernel. */
714 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
716 struct nexthop
*nexthop
;
717 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
718 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
720 if (info
->safi
!= SAFI_UNICAST
) {
721 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
722 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
723 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
728 * Make sure we update the FPM any time we send new information to
731 hook_call(rib_update
, rn
, "uninstalling from kernel");
733 switch (dplane_route_delete(rn
, re
)) {
734 case ZEBRA_DPLANE_REQUEST_QUEUED
:
736 zvrf
->removals_queued
++;
738 case ZEBRA_DPLANE_REQUEST_FAILURE
:
739 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
740 "%u:%pRN: Failed to enqueue dataplane uninstall",
743 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
753 * rib_can_delete_dest
755 * Returns true if the given dest can be deleted from the table.
757 static int rib_can_delete_dest(rib_dest_t
*dest
)
759 if (re_list_first(&dest
->routes
)) {
764 * Unresolved rnh's are stored on the default route's list
766 * dest->rnode can also be the source prefix node in an
767 * ipv6 sourcedest table. Fortunately the prefix of a
768 * source prefix node can never be the default prefix.
770 if (is_default_prefix(&dest
->rnode
->p
))
774 * Don't delete the dest if we have to update the FPM about this
777 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
778 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
784 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
,
787 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
791 * We are storing the rnh's associated withb
792 * the tracked nexthop as a list of the rn's.
793 * Unresolved rnh's are placed at the top
794 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
795 * As such for each rn we need to walk up the tree
796 * and see if any rnh's need to see if they
797 * would match a more specific route
800 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
802 "%s: %pRN Being examined for Nexthop Tracking Count: %zd",
804 dest
? rnh_list_count(&dest
->nht
) : 0);
806 if (rt_delete
&& (!dest
|| !rnh_list_count(&dest
->nht
))) {
807 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
808 zlog_debug("%pRN has no tracking NHTs. Bailing",
815 dest
= rib_dest_from_rnode(rn
);
819 * If we have any rnh's stored in the nht list
820 * then we know that this route node was used for
821 * nht resolution and as such we need to call the
822 * nexthop tracking evaluation code
824 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
825 struct zebra_vrf
*zvrf
=
826 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
827 struct prefix
*p
= &rnh
->node
->p
;
829 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
831 "%s(%u):%pRN has Nexthop(%pRN) depending on it, evaluating %u:%u",
832 zvrf_name(zvrf
), zvrf_id(zvrf
), rn
,
833 rnh
->node
, seq
, rnh
->seqno
);
836 * If we have evaluated this node on this pass
837 * already, due to following the tree up
838 * then we know that we can move onto the next
841 * Additionally we call zebra_evaluate_rnh
842 * when we gc the dest. In this case we know
843 * that there must be no other re's where
844 * we were originally as such we know that
845 * that sequence number is ok to respect.
847 if (rnh
->seqno
== seq
) {
848 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
850 " Node processed and moved already");
855 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0, p
,
861 dest
= rib_dest_from_rnode(rn
);
868 * Garbage collect the rib dest corresponding to the given route node
871 * Returns true if the dest was deleted, false otherwise.
873 int rib_gc_dest(struct route_node
*rn
)
877 dest
= rib_dest_from_rnode(rn
);
881 if (!rib_can_delete_dest(dest
))
884 if (IS_ZEBRA_DEBUG_RIB
) {
885 struct zebra_vrf
*zvrf
;
887 zvrf
= rib_dest_vrf(dest
);
888 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
891 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
895 rnh_list_fini(&dest
->nht
);
896 XFREE(MTYPE_RIB_DEST
, dest
);
900 * Release the one reference that we keep on the route node.
902 route_unlock_node(rn
);
906 void zebra_rtable_node_cleanup(struct route_table
*table
,
907 struct route_node
*node
)
909 struct route_entry
*re
, *next
;
911 RNODE_FOREACH_RE_SAFE (node
, re
, next
) {
912 rib_unlink(node
, re
);
916 rib_dest_t
*dest
= node
->info
;
918 rnh_list_fini(&dest
->nht
);
919 XFREE(MTYPE_RIB_DEST
, node
->info
);
923 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
924 struct route_entry
*new)
926 hook_call(rib_update
, rn
, "new route selected");
928 /* Update real nexthop. This may actually determine if nexthop is active
930 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
931 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
935 if (IS_ZEBRA_DEBUG_RIB
)
936 zlog_debug("%s(%u:%u):%pRN: Adding route rn %p, re %p (%s)",
937 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, rn
, rn
,
938 new, zebra_route_string(new->type
));
940 /* If labeled-unicast route, install transit LSP. */
941 if (zebra_rib_labeled_unicast(new))
942 zebra_mpls_lsp_install(zvrf
, rn
, new);
944 rib_install_kernel(rn
, new, NULL
);
946 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
949 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
950 struct route_entry
*old
)
952 hook_call(rib_update
, rn
, "removing existing route");
954 /* Uninstall from kernel. */
955 if (IS_ZEBRA_DEBUG_RIB
)
956 zlog_debug("%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s)",
957 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, rn
, rn
,
958 old
, zebra_route_string(old
->type
));
960 /* If labeled-unicast route, uninstall transit LSP. */
961 if (zebra_rib_labeled_unicast(old
))
962 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
964 rib_uninstall_kernel(rn
, old
);
966 /* Update nexthop for route, reset changed flag. */
967 /* Note: this code also handles the Linux case when an interface goes
968 * down, causing the kernel to delete routes without sending DELROUTE
971 if (RIB_KERNEL_ROUTE(old
))
972 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
974 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
977 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
978 struct route_node
*rn
,
979 struct route_entry
*old
,
980 struct route_entry
*new)
985 * We have to install or update if a new route has been selected or
986 * something has changed.
988 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
989 hook_call(rib_update
, rn
, "updating existing route");
991 /* Update the nexthop; we could determine here that nexthop is
993 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
996 /* If nexthop is active, install the selected route, if
998 * the install succeeds, cleanup flags for prior route, if
1003 if (IS_ZEBRA_DEBUG_RIB
) {
1006 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s) old %p (%s)",
1007 zvrf_name(zvrf
), zvrf_id(zvrf
),
1008 new->table
, rn
, rn
, new,
1009 zebra_route_string(new->type
),
1011 zebra_route_string(old
->type
));
1014 "%s(%u:%u):%pRN: Updating route rn %p, re %p (%s)",
1015 zvrf_name(zvrf
), zvrf_id(zvrf
),
1016 new->table
, rn
, rn
, new,
1017 zebra_route_string(new->type
));
1020 /* If labeled-unicast route, uninstall transit LSP. */
1021 if (zebra_rib_labeled_unicast(old
))
1022 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1025 * Non-system route should be installed.
1026 * If labeled-unicast route, install transit
1029 if (zebra_rib_labeled_unicast(new))
1030 zebra_mpls_lsp_install(zvrf
, rn
, new);
1032 rib_install_kernel(rn
, new, old
);
1036 * If nexthop for selected route is not active or install
1038 * may need to uninstall and delete for redistribution.
1041 if (IS_ZEBRA_DEBUG_RIB
) {
1044 "%s(%u:%u):%pRN: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
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: Deleting route rn %p, re %p (%s) - nexthop inactive",
1053 zvrf_name(zvrf
), zvrf_id(zvrf
),
1054 new->table
, rn
, rn
, new,
1055 zebra_route_string(new->type
));
1059 * When we have gotten to this point
1060 * the new route entry has no nexthops
1061 * that are usable and as such we need
1062 * to remove the old route, but only
1063 * if we were the one who installed
1066 if (!RIB_SYSTEM_ROUTE(old
)) {
1067 /* If labeled-unicast route, uninstall transit
1069 if (zebra_rib_labeled_unicast(old
))
1070 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1072 rib_uninstall_kernel(rn
, old
);
1077 * Same route selected; check if in the FIB and if not,
1078 * re-install. This is housekeeping code to deal with
1079 * race conditions in kernel with linux netlink reporting
1080 * interface up before IPv4 or IPv6 protocol is ready
1083 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1084 RIB_SYSTEM_ROUTE(new))
1085 rib_install_kernel(rn
, new, NULL
);
1088 /* Update prior route. */
1090 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1092 /* Clear changed flag. */
1093 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1096 /* Check if 'alternate' RIB entry is better than 'current'. */
1097 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1098 struct route_entry
*alternate
)
1100 if (current
== NULL
)
1103 /* filter route selection in following order:
1104 * - connected beats other types
1105 * - if both connected, loopback or vrf wins
1106 * - lower distance beats higher
1107 * - lower metric beats higher for equal distance
1108 * - last, hence oldest, route wins tie break.
1111 /* Connected routes. Check to see if either are a vrf
1112 * or loopback interface. If not, pick the last connected
1113 * route of the set of lowest metric connected routes.
1115 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1116 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1119 /* both are connected. are either loop or vrf? */
1120 struct nexthop
*nexthop
= NULL
;
1122 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1123 struct interface
*ifp
= if_lookup_by_index(
1124 nexthop
->ifindex
, alternate
->vrf_id
);
1126 if (ifp
&& if_is_loopback(ifp
))
1130 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1131 struct interface
*ifp
= if_lookup_by_index(
1132 nexthop
->ifindex
, current
->vrf_id
);
1134 if (ifp
&& if_is_loopback(ifp
))
1138 /* Neither are loop or vrf so pick best metric */
1139 if (alternate
->metric
<= current
->metric
)
1145 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1148 /* higher distance loses */
1149 if (alternate
->distance
< current
->distance
)
1151 if (current
->distance
< alternate
->distance
)
1154 /* metric tie-breaks equal distance */
1155 if (alternate
->metric
<= current
->metric
)
1161 /* Core function for processing routing information base. */
1162 static void rib_process(struct route_node
*rn
)
1164 struct route_entry
*re
;
1165 struct route_entry
*next
;
1166 struct route_entry
*old_selected
= NULL
;
1167 struct route_entry
*new_selected
= NULL
;
1168 struct route_entry
*old_fib
= NULL
;
1169 struct route_entry
*new_fib
= NULL
;
1170 struct route_entry
*best
= NULL
;
1172 struct zebra_vrf
*zvrf
= NULL
;
1175 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1179 dest
= rib_dest_from_rnode(rn
);
1181 * We have an enqueued node with nothing to process here
1182 * let's just finish up and return;
1187 zvrf
= rib_dest_vrf(dest
);
1188 vrf_id
= zvrf_id(zvrf
);
1190 vrf
= vrf_lookup_by_id(vrf_id
);
1193 * we can have rn's that have a NULL info pointer
1194 * (dest). As such let's not let the deref happen
1195 * additionally we know RNODE_FOREACH_RE_SAFE
1196 * will not iterate so we are ok.
1198 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1199 struct route_entry
*re
= re_list_first(&dest
->routes
);
1201 zlog_debug("%s(%u:%u):%pRN: Processing rn %p",
1202 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
,
1206 old_fib
= dest
->selected_fib
;
1208 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1209 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1210 char flags_buf
[128];
1211 char status_buf
[128];
1214 "%s(%u:%u):%pRN: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1215 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, rn
, re
,
1216 zebra_route_string(re
->type
),
1217 _dump_re_status(re
, status_buf
,
1218 sizeof(status_buf
)),
1219 zclient_dump_route_flags(re
->flags
, flags_buf
,
1221 re
->distance
, re
->metric
);
1224 /* Currently selected re. */
1225 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1226 assert(old_selected
== NULL
);
1230 /* Skip deleted entries from selection */
1231 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1235 * If the route entry has changed, verify/resolve
1236 * the nexthops associated with the entry.
1238 * In any event if we have nexthops that are not active
1239 * then we cannot use this particular route entry so
1242 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1243 if (!nexthop_active_update(rn
, re
)) {
1244 const struct prefix
*p
;
1245 struct rib_table_info
*info
;
1247 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1248 /* XXX: HERE BE DRAGONS!!!!!
1249 * In all honesty, I have not yet
1250 * figured out what this part does or
1251 * why the ROUTE_ENTRY_CHANGED test
1252 * above is correct or why we need to
1253 * delete a route here, and also not
1254 * whether this concerns both selected
1255 * and fib route, or only selected
1258 * This entry was denied by the 'ip
1260 * table' route-map, we need to delete
1262 if (re
!= old_selected
) {
1263 if (IS_ZEBRA_DEBUG_RIB
)
1265 "%s: %s(%u):%pRN: imported via import-table but denied by the ip protocol table route-map",
1272 SET_FLAG(re
->status
,
1273 ROUTE_ENTRY_REMOVED
);
1276 info
= srcdest_rnode_table_info(rn
);
1277 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1278 zsend_route_notify_owner(
1279 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
1280 info
->afi
, info
->safi
);
1285 * If the re has not changed and the nhg we have is
1286 * not usable, then we cannot use this route entry
1287 * for consideration, as that the route will just
1288 * not install if it is selected.
1290 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1294 /* Infinite distance. */
1295 if (re
->distance
== DISTANCE_INFINITY
&&
1296 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1297 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1301 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1302 best
= rib_choose_best(new_fib
, re
);
1303 if (new_fib
&& best
!= new_fib
)
1304 UNSET_FLAG(new_fib
->status
,
1305 ROUTE_ENTRY_CHANGED
);
1308 best
= rib_choose_best(new_selected
, re
);
1309 if (new_selected
&& best
!= new_selected
)
1310 UNSET_FLAG(new_selected
->status
,
1311 ROUTE_ENTRY_CHANGED
);
1312 new_selected
= best
;
1315 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1316 } /* RNODE_FOREACH_RE */
1318 /* If no FIB override route, use the selected route also for FIB */
1319 if (new_fib
== NULL
)
1320 new_fib
= new_selected
;
1322 /* After the cycle is finished, the following pointers will be set:
1323 * old_selected --- RE entry currently having SELECTED
1324 * new_selected --- RE entry that is newly SELECTED
1325 * old_fib --- RE entry currently in kernel FIB
1326 * new_fib --- RE entry that is newly to be in kernel FIB
1328 * new_selected will get SELECTED flag, and is going to be redistributed
1329 * the zclients. new_fib (which can be new_selected) will be installed
1333 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1334 struct route_entry
*entry
;
1336 entry
= old_selected
1341 : new_fib
? new_fib
: NULL
;
1344 "%s(%u:%u):%pRN: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1345 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, rn
,
1346 (void *)old_selected
, (void *)new_selected
,
1347 (void *)old_fib
, (void *)new_fib
);
1350 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1351 * fib == selected */
1352 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1353 ROUTE_ENTRY_CHANGED
);
1355 /* Update SELECTED entry */
1356 if (old_selected
!= new_selected
|| selected_changed
) {
1358 if (new_selected
&& new_selected
!= new_fib
)
1359 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1362 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1366 * If we're removing the old entry, we should tell
1367 * redist subscribers about that *if* they aren't
1368 * going to see a redist for the new entry.
1370 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1371 ROUTE_ENTRY_REMOVED
))
1372 redistribute_delete(rn
, old_selected
,
1375 if (old_selected
!= new_selected
)
1376 UNSET_FLAG(old_selected
->flags
,
1377 ZEBRA_FLAG_SELECTED
);
1381 /* Update fib according to selection results */
1382 if (new_fib
&& old_fib
)
1383 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1385 rib_process_add_fib(zvrf
, rn
, new_fib
);
1387 rib_process_del_fib(zvrf
, rn
, old_fib
);
1389 /* Remove all RE entries queued for removal */
1390 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1391 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1392 if (IS_ZEBRA_DEBUG_RIB
) {
1393 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1394 (void *)rn
, (void *)re
);
1401 * Check if the dest can be deleted now.
1406 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1408 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1409 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1414 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1415 if (IS_ZEBRA_DEBUG_MPLS
)
1417 "%s(%u): Scheduling all LSPs upon RIB completion",
1418 zvrf_name(zvrf
), zvrf_id(zvrf
));
1419 zebra_mpls_lsp_schedule(zvrf
);
1420 mpls_unmark_lsps_for_processing(rn
);
1425 * Utility to match route with dplane context data
1427 static bool rib_route_match_ctx(const struct route_entry
*re
,
1428 const struct zebra_dplane_ctx
*ctx
,
1431 bool result
= false;
1435 * In 'update' case, we test info about the 'previous' or
1438 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1439 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1442 /* We use an extra test for statics, and another for
1445 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1446 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1447 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1449 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1451 dplane_ctx_get_old_metric(ctx
)) {
1458 * Ordinary, single-route case using primary context info
1460 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1461 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1462 /* Skip route that's been deleted */
1466 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1467 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1470 /* We use an extra test for statics, and another for
1473 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1474 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1475 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1477 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1478 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1480 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1481 result
= nexthop_group_equal_no_recurse(
1482 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1492 static void zebra_rib_fixup_system(struct route_node
*rn
)
1494 struct route_entry
*re
;
1496 RNODE_FOREACH_RE(rn
, re
) {
1497 struct nexthop
*nhop
;
1499 if (!RIB_SYSTEM_ROUTE(re
))
1502 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1505 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1506 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1508 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1509 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1512 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1517 /* Route comparison logic, with various special cases. */
1518 static bool rib_compare_routes(const struct route_entry
*re1
,
1519 const struct route_entry
*re2
)
1521 if (re1
->type
!= re2
->type
)
1524 if (re1
->instance
!= re2
->instance
)
1527 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1530 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1531 re1
->distance
!= re2
->distance
)
1534 /* We support multiple connected routes: this supports multiple
1535 * v6 link-locals, and we also support multiple addresses in the same
1536 * subnet on a single interface.
1538 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1545 * Compare nexthop lists from a route and a dplane context; test whether
1546 * the list installed in the FIB matches the route's list.
1547 * Set 'changed_p' to 'true' if there were changes to the route's
1548 * installed nexthops.
1550 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1553 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1554 const struct nexthop_group
*ctx_nhg
,
1557 bool matched_p
= true;
1558 struct nexthop
*nexthop
, *ctx_nexthop
;
1560 /* Get the first `installed` one to check against.
1561 * If the dataplane doesn't set these to be what was actually installed,
1562 * it will just be whatever was in re->nhe->nhg?
1564 ctx_nexthop
= ctx_nhg
->nexthop
;
1566 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1567 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1568 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1570 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1572 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1575 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1578 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1579 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1580 /* If the FIB doesn't know about the nexthop,
1581 * it's not installed
1583 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1584 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1585 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1587 (CHECK_FLAG(nexthop
->flags
,
1593 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1596 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1598 /* Keep checking nexthops */
1602 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1603 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1604 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1605 zlog_debug("%s: rib nh %pNHv -> installed",
1611 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1613 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1614 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1615 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1621 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1624 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1631 * Update a route from a dplane context. This consolidates common code
1632 * that can be used in processing of results from FIB updates, and in
1633 * async notification processing.
1634 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1636 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1637 struct route_node
*rn
,
1638 struct zebra_dplane_ctx
*ctx
)
1640 struct nexthop
*nexthop
;
1642 const struct nexthop_group
*ctxnhg
;
1643 struct nexthop_group
*re_nhg
;
1644 bool is_selected
= false; /* Is 're' currently the selected re? */
1645 bool changed_p
= false; /* Change to nexthops? */
1649 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1651 dest
= rib_dest_from_rnode(rn
);
1653 is_selected
= (re
== dest
->selected_fib
);
1655 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1656 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1657 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1658 (is_selected
? "" : "NOT "), re
);
1660 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1661 * If the installed set differs from the set requested by the rib/owner,
1662 * we use the fib-specific nexthop-group to record the actual FIB
1666 ctxnhg
= dplane_ctx_get_ng(ctx
);
1668 /* Check route's fib group and incoming notif group for equivalence.
1670 * Let's assume the nexthops are ordered here to save time.
1672 /* TODO -- this isn't testing or comparing the FIB flags; we should
1673 * do a more explicit loop, checking the incoming notification's flags.
1675 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1676 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1679 /* If the new FIB set matches the existing FIB set, we're done. */
1681 if (IS_ZEBRA_DEBUG_RIB
)
1683 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1684 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1687 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1689 * Free stale fib list and move on to check the rib nhg.
1691 if (IS_ZEBRA_DEBUG_RIB
)
1693 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1694 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1695 nexthops_free(re
->fib_ng
.nexthop
);
1696 re
->fib_ng
.nexthop
= NULL
;
1698 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1700 /* Note that the installed nexthops have changed */
1703 if (IS_ZEBRA_DEBUG_RIB
)
1705 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1706 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1710 * Compare with the rib nexthop group. The comparison here is different:
1711 * the RIB group may be a superset of the list installed in the FIB. We
1712 * walk the RIB group, looking for the 'installable' candidate
1713 * nexthops, and then check those against the set
1714 * that is actually installed.
1716 * Assume nexthops are ordered here as well.
1719 /* If nothing is installed, we can skip some of the checking/comparison
1722 if (ctxnhg
->nexthop
== NULL
) {
1727 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1729 /* If all nexthops were processed, we're done */
1731 if (IS_ZEBRA_DEBUG_RIB
)
1733 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1734 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1735 (changed_p
? "true" : "false"));
1741 /* FIB nexthop set differs from the RIB set:
1742 * create a fib-specific nexthop-group
1744 if (IS_ZEBRA_DEBUG_RIB
)
1746 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1747 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1748 (changed_p
? "true" : "false"),
1749 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1751 /* Set the flag about the dedicated fib list */
1752 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1753 if (ctxnhg
->nexthop
)
1754 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1759 * Check the status of the route's backup nexthops, if any.
1760 * The logic for backups is somewhat different: if any backup is
1761 * installed, a new fib nhg will be attached to the route.
1763 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1765 goto done
; /* No backup nexthops */
1767 /* First check the route's 'fib' list of backups, if it's present
1768 * from some previous event.
1770 re_nhg
= &re
->fib_backup_ng
;
1771 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1774 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1777 /* If the new FIB set matches an existing FIB set, we're done. */
1779 if (IS_ZEBRA_DEBUG_RIB
)
1781 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1782 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1785 } else if (re
->fib_backup_ng
.nexthop
) {
1787 * Free stale fib backup list and move on to check
1788 * the route's backups.
1790 if (IS_ZEBRA_DEBUG_RIB
)
1792 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1793 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1794 nexthops_free(re
->fib_backup_ng
.nexthop
);
1795 re
->fib_backup_ng
.nexthop
= NULL
;
1797 /* Note that the installed nexthops have changed */
1800 if (IS_ZEBRA_DEBUG_RIB
)
1802 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1803 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1807 * If a FIB backup nexthop set exists, attach a copy
1808 * to the route if any backup is installed
1810 if (ctxnhg
&& ctxnhg
->nexthop
) {
1812 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1813 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1817 /* If no installed backups, we're done */
1818 if (nexthop
== NULL
)
1821 if (IS_ZEBRA_DEBUG_RIB
)
1823 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1824 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1825 (changed_p
? "true" : "false"));
1827 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1837 * Helper to locate a zebra route-node from a dplane context. This is used
1838 * when processing dplane results, e.g. Note well: the route-node is returned
1839 * with a ref held - route_unlock_node() must be called eventually.
1841 struct route_node
*rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1843 struct route_table
*table
= NULL
;
1844 struct route_node
*rn
= NULL
;
1845 const struct prefix
*dest_pfx
, *src_pfx
;
1847 /* Locate rn and re(s) from ctx */
1849 table
= zebra_vrf_lookup_table_with_table_id(
1850 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1851 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1852 if (table
== NULL
) {
1853 if (IS_ZEBRA_DEBUG_DPLANE
) {
1855 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1856 dplane_ctx_get_afi(ctx
),
1857 dplane_ctx_get_safi(ctx
),
1858 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1859 dplane_ctx_get_vrf(ctx
));
1864 dest_pfx
= dplane_ctx_get_dest(ctx
);
1865 src_pfx
= dplane_ctx_get_src(ctx
);
1867 rn
= srcdest_rnode_get(table
, dest_pfx
,
1868 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1877 * Route-update results processing after async dataplane update.
1879 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1881 struct zebra_vrf
*zvrf
= NULL
;
1883 struct route_node
*rn
= NULL
;
1884 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1885 bool is_update
= false;
1886 enum dplane_op_e op
;
1887 enum zebra_dplane_result status
;
1890 bool fib_changed
= false;
1891 struct rib_table_info
*info
;
1892 bool rt_delete
= false;
1894 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1895 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1897 /* Locate rn and re(s) from ctx */
1898 rn
= rib_find_rn_from_ctx(ctx
);
1900 if (IS_ZEBRA_DEBUG_DPLANE
) {
1902 "Failed to process dplane results: no route for %s(%u):%pRN",
1903 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
), rn
);
1908 dest
= rib_dest_from_rnode(rn
);
1909 info
= srcdest_rnode_table_info(rn
);
1911 op
= dplane_ctx_get_op(ctx
);
1912 status
= dplane_ctx_get_status(ctx
);
1914 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1916 "%s(%u:%u):%pRN Processing dplane result ctx %p, op %s result %s",
1917 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1918 dplane_ctx_get_table(ctx
), rn
, ctx
, dplane_op2str(op
),
1919 dplane_res2str(status
));
1922 * Update is a bit of a special case, where we may have both old and new
1923 * routes to post-process.
1925 is_update
= dplane_ctx_is_update(ctx
);
1928 * Take a pass through the routes, look for matches with the context
1931 RNODE_FOREACH_RE(rn
, rib
) {
1934 if (rib_route_match_ctx(rib
, ctx
, false))
1938 /* Check for old route match */
1939 if (is_update
&& (old_re
== NULL
)) {
1940 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1944 /* Have we found the routes we need to work on? */
1945 if (re
&& ((!is_update
|| old_re
)))
1949 seq
= dplane_ctx_get_seq(ctx
);
1952 * Check sequence number(s) to detect stale results before continuing
1955 if (re
->dplane_sequence
!= seq
) {
1956 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1958 "%s(%u):%pRN Stale dplane result for re %p",
1960 dplane_ctx_get_vrf(ctx
), rn
, re
);
1962 if (!zrouter
.asic_offloaded
||
1963 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
1964 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)))
1965 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1970 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1971 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1973 "%s(%u:%u):%pRN Stale dplane result for old_re %p",
1975 dplane_ctx_get_vrf(ctx
), old_re
->table
,
1978 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1982 case DPLANE_OP_ROUTE_INSTALL
:
1983 case DPLANE_OP_ROUTE_UPDATE
:
1984 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1986 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1987 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1990 * On an update operation from the same route type
1991 * context retrieval currently has no way to know
1992 * which was the old and which was the new.
1993 * So don't unset our flags that we just set.
1994 * We know redistribution is ok because the
1995 * old_re in this case is used for nothing
1996 * more than knowing whom to contact if necessary.
1998 if (old_re
&& old_re
!= re
) {
1999 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2000 UNSET_FLAG(old_re
->status
,
2001 ROUTE_ENTRY_INSTALLED
);
2004 /* Update zebra route based on the results in
2005 * the context struct.
2009 rib_update_re_from_ctx(re
, rn
, ctx
);
2012 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
2014 "%s(%u:%u):%pRN no fib change for re",
2016 dplane_ctx_get_vrf(ctx
),
2017 dplane_ctx_get_table(
2022 /* Redistribute if this is the selected re */
2023 if (dest
&& re
== dest
->selected_fib
)
2024 redistribute_update(rn
, re
, old_re
);
2028 * System routes are weird in that they
2029 * allow multiple to be installed that match
2030 * to the same prefix, so after we get the
2031 * result we need to clean them up so that
2032 * we can actually use them.
2034 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2035 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2036 zebra_rib_fixup_system(rn
);
2041 /* Notify route owner */
2042 if (zebra_router_notify_on_ack())
2043 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
2046 if (CHECK_FLAG(re
->flags
,
2047 ZEBRA_FLAG_OFFLOADED
))
2048 zsend_route_notify_owner_ctx(
2050 ZAPI_ROUTE_INSTALLED
);
2053 ZEBRA_FLAG_OFFLOAD_FAILED
))
2054 zsend_route_notify_owner_ctx(
2056 ZAPI_ROUTE_FAIL_INSTALL
);
2061 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2062 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2064 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
2066 zsend_route_notify_owner(
2067 rn
, re
, ZAPI_ROUTE_FAIL_INSTALL
,
2068 info
->afi
, info
->safi
);
2070 zlog_warn("%s(%u:%u):%pRN: Route install failed",
2071 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2072 dplane_ctx_get_table(ctx
), rn
);
2075 case DPLANE_OP_ROUTE_DELETE
:
2078 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2080 * In the delete case, the zebra core datastructs were
2081 * updated (or removed) at the time the delete was issued,
2082 * so we're just notifying the route owner.
2084 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
2086 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2087 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2089 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
2095 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2096 zsend_route_notify_owner_ctx(ctx
,
2097 ZAPI_ROUTE_REMOVE_FAIL
);
2099 zlog_warn("%s(%u:%u):%pRN: Route Deletion failure",
2100 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2101 dplane_ctx_get_table(ctx
), rn
);
2105 * System routes are weird in that they
2106 * allow multiple to be installed that match
2107 * to the same prefix, so after we get the
2108 * result we need to clean them up so that
2109 * we can actually use them.
2111 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2112 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2113 zebra_rib_fixup_system(rn
);
2119 zebra_rib_evaluate_rn_nexthops(rn
, seq
, rt_delete
);
2120 zebra_rib_evaluate_mpls(rn
);
2124 route_unlock_node(rn
);
2128 * Count installed/FIB nexthops
2130 static int rib_count_installed_nh(struct route_entry
*re
)
2133 struct nexthop
*nexthop
;
2134 struct nexthop_group
*nhg
;
2136 nhg
= rib_get_fib_nhg(re
);
2138 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2139 /* The meaningful flag depends on where the installed
2142 if (nhg
== &(re
->fib_ng
)) {
2143 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2146 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2151 nhg
= rib_get_fib_backup_nhg(re
);
2153 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2154 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2163 * Handle notification from async dataplane: the dataplane has detected
2164 * some change to a route, and notifies zebra so that the control plane
2165 * can reflect that change.
2167 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2169 struct route_node
*rn
= NULL
;
2170 struct route_entry
*re
= NULL
;
2172 struct nexthop
*nexthop
;
2174 bool fib_changed
= false;
2175 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2176 int start_count
, end_count
;
2178 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2180 /* Locate rn and re(s) from ctx */
2181 rn
= rib_find_rn_from_ctx(ctx
);
2185 "Failed to process dplane notification: no routes for %s(%u:%u):%pRN",
2186 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2187 dplane_ctx_get_table(ctx
), rn
);
2192 dest
= rib_dest_from_rnode(rn
);
2195 zlog_debug("%s(%u:%u):%pRN Processing dplane notif ctx %p",
2196 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2197 dplane_ctx_get_table(ctx
), rn
, ctx
);
2200 * Take a pass through the routes, look for matches with the context
2203 RNODE_FOREACH_RE(rn
, re
) {
2204 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2208 /* No match? Nothing we can do */
2212 "%s(%u:%u):%pRN Unable to process dplane notification: no entry for type %s",
2213 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2214 dplane_ctx_get_table(ctx
), rn
,
2215 zebra_route_string(dplane_ctx_get_type(ctx
)));
2220 /* Ensure we clear the QUEUED flag */
2221 if (!zrouter
.asic_offloaded
)
2222 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2224 /* Is this a notification that ... matters? We mostly care about
2225 * the route that is currently selected for installation; we may also
2226 * get an un-install notification, and handle that too.
2228 if (re
!= dest
->selected_fib
) {
2230 * If we need to, clean up after a delete that was part of
2231 * an update operation.
2234 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2235 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2239 /* If no nexthops or none installed, ensure that this re
2240 * gets its 'installed' flag cleared.
2242 if (end_count
== 0) {
2243 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2244 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2247 "%s(%u:%u):%pRN dplane notif, uninstalled type %s route",
2249 dplane_ctx_get_vrf(ctx
),
2250 dplane_ctx_get_table(ctx
), rn
,
2252 dplane_ctx_get_type(ctx
)));
2254 /* At least report on the event. */
2257 "%s(%u:%u):%pRN dplane notif, but type %s not selected_fib",
2259 dplane_ctx_get_vrf(ctx
),
2260 dplane_ctx_get_table(ctx
), rn
,
2262 dplane_ctx_get_type(ctx
)));
2267 /* We'll want to determine whether the installation status of the
2268 * route has changed: we'll check the status before processing,
2269 * and then again if there's been a change.
2273 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2274 start_count
= rib_count_installed_nh(re
);
2276 /* Update zebra's nexthop FIB flags based on the context struct's
2279 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2284 "%s(%u:%u):%pRN dplane notification: rib_update returns FALSE",
2285 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2286 dplane_ctx_get_table(ctx
), rn
);
2290 * Perform follow-up work if the actual status of the prefix
2293 end_count
= rib_count_installed_nh(re
);
2295 /* Various fib transitions: changed nexthops; from installed to
2296 * not-installed; or not-installed to installed.
2298 if (start_count
> 0 && end_count
> 0) {
2301 "%s(%u:%u):%pRN applied nexthop changes from dplane notification",
2302 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2303 dplane_ctx_get_table(ctx
), rn
);
2305 /* Changed nexthops - update kernel/others */
2306 dplane_route_notif_update(rn
, re
,
2307 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2309 } else if (start_count
== 0 && end_count
> 0) {
2312 "%s(%u:%u):%pRN installed transition from dplane notification",
2313 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2314 dplane_ctx_get_table(ctx
), rn
);
2316 /* We expect this to be the selected route, so we want
2317 * to tell others about this transition.
2319 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2321 /* Changed nexthops - update kernel/others */
2322 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2324 /* Redistribute, lsp, and nht update */
2325 redistribute_update(rn
, re
, NULL
);
2327 } else if (start_count
> 0 && end_count
== 0) {
2330 "%s(%u:%u):%pRN un-installed transition from dplane notification",
2331 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2332 dplane_ctx_get_table(ctx
), rn
);
2334 /* Transition from _something_ installed to _nothing_
2337 /* We expect this to be the selected route, so we want
2338 * to tell others about this transistion.
2340 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2342 /* Changed nexthops - update kernel/others */
2343 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2345 /* Redistribute, lsp, and nht update */
2346 redistribute_delete(rn
, re
, NULL
);
2349 /* Make any changes visible for lsp and nexthop-tracking processing */
2350 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence(),
2353 zebra_rib_evaluate_mpls(rn
);
2357 route_unlock_node(rn
);
2361 * Process a node from the EVPN/VXLAN subqueue.
2363 static void process_subq_evpn(struct listnode
*lnode
)
2365 struct wq_evpn_wrapper
*w
;
2367 /* In general, the list node points to a wrapper object
2368 * holding the info necessary to make some update.
2370 w
= listgetdata(lnode
);
2374 if (w
->type
== WQ_EVPN_WRAPPER_TYPE_VRFROUTE
) {
2376 zebra_vxlan_evpn_vrf_route_add(w
->vrf_id
, &w
->macaddr
,
2377 &w
->ip
, &w
->prefix
);
2379 zebra_vxlan_evpn_vrf_route_del(w
->vrf_id
, &w
->ip
,
2381 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_ES
) {
2383 zebra_evpn_remote_es_add(&w
->esi
, w
->ip
.ipaddr_v4
,
2384 w
->esr_rxed
, w
->df_alg
,
2387 zebra_evpn_remote_es_del(&w
->esi
, w
->ip
.ipaddr_v4
);
2388 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_MACIP
) {
2389 uint16_t ipa_len
= 0;
2391 if (w
->ip
.ipa_type
== IPADDR_V4
)
2392 ipa_len
= IPV4_MAX_BYTELEN
;
2393 else if (w
->ip
.ipa_type
== IPADDR_V6
)
2394 ipa_len
= IPV6_MAX_BYTELEN
;
2397 zebra_evpn_rem_macip_add(w
->vni
, &w
->macaddr
, ipa_len
,
2398 &w
->ip
, w
->flags
, w
->seq
,
2399 w
->vtep_ip
, &w
->esi
);
2401 zebra_evpn_rem_macip_del(w
->vni
, &w
->macaddr
, ipa_len
,
2402 &w
->ip
, w
->vtep_ip
);
2403 } else if (w
->type
== WQ_EVPN_WRAPPER_TYPE_REM_VTEP
) {
2405 zebra_vxlan_remote_vtep_add(w
->vrf_id
, w
->vni
,
2406 w
->vtep_ip
, w
->flags
);
2408 zebra_vxlan_remote_vtep_del(w
->vrf_id
, w
->vni
,
2413 XFREE(MTYPE_WQ_WRAPPER
, w
);
2417 * Process the nexthop-group workqueue subqueue
2419 static void process_subq_nhg(struct listnode
*lnode
)
2421 struct nhg_ctx
*ctx
;
2422 struct nhg_hash_entry
*nhe
, *newnhe
;
2423 struct wq_nhg_wrapper
*w
;
2424 uint8_t qindex
= META_QUEUE_NHG
;
2426 w
= listgetdata(lnode
);
2431 /* Two types of object - an update from the local kernel, or
2432 * an nhg update from a daemon.
2434 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
) {
2437 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2439 "NHG Context id=%u dequeued from sub-queue %s",
2440 ctx
->id
, subqueue2str(qindex
));
2443 /* Process nexthop group updates coming 'up' from the OS */
2444 nhg_ctx_process(ctx
);
2446 } else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
) {
2449 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2450 zlog_debug("NHG %u dequeued from sub-queue %s", nhe
->id
,
2451 subqueue2str(qindex
));
2453 /* Process incoming nhg update, probably from a proto daemon */
2454 newnhe
= zebra_nhg_proto_add(nhe
->id
, nhe
->type
,
2456 nhe
->zapi_session
, &nhe
->nhg
, 0);
2458 /* Report error to daemon via ZAPI */
2460 zsend_nhg_notify(nhe
->type
, nhe
->zapi_instance
,
2461 nhe
->zapi_session
, nhe
->id
,
2462 ZAPI_NHG_FAIL_INSTALL
);
2464 /* Free temp nhe - we own that memory. */
2465 zebra_nhg_free(nhe
);
2468 XFREE(MTYPE_WQ_WRAPPER
, w
);
2471 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2473 struct route_node
*rnode
= NULL
;
2474 rib_dest_t
*dest
= NULL
;
2475 struct zebra_vrf
*zvrf
= NULL
;
2477 rnode
= listgetdata(lnode
);
2478 dest
= rib_dest_from_rnode(rnode
);
2481 zvrf
= rib_dest_vrf(dest
);
2485 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2486 struct route_entry
*re
= NULL
;
2489 * rib_process may have freed the dest
2490 * as part of the garbage collection. Let's
2491 * prevent stupidity from happening.
2493 dest
= rib_dest_from_rnode(rnode
);
2495 re
= re_list_first(&dest
->routes
);
2497 zlog_debug("%s(%u:%u):%pRN rn %p dequeued from sub-queue %s",
2498 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0,
2499 rnode
, rnode
, subqueue2str(qindex
));
2503 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2504 RIB_ROUTE_QUEUED(qindex
));
2506 route_unlock_node(rnode
);
2510 * Examine the specified subqueue; process one entry and return 1 if
2511 * there is a node, return 0 otherwise.
2513 static unsigned int process_subq(struct list
*subq
,
2514 enum meta_queue_indexes qindex
)
2516 struct listnode
*lnode
= listhead(subq
);
2522 case META_QUEUE_EVPN
:
2523 process_subq_evpn(lnode
);
2525 case META_QUEUE_NHG
:
2526 process_subq_nhg(lnode
);
2528 case META_QUEUE_CONNECTED
:
2529 case META_QUEUE_KERNEL
:
2530 case META_QUEUE_STATIC
:
2531 case META_QUEUE_NOTBGP
:
2532 case META_QUEUE_BGP
:
2533 case META_QUEUE_OTHER
:
2534 process_subq_route(lnode
, qindex
);
2538 list_delete_node(subq
, lnode
);
2543 /* Dispatch the meta queue by picking and processing the next node from
2544 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2545 * data is pointed to the meta queue structure.
2547 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2549 struct meta_queue
*mq
= data
;
2551 uint32_t queue_len
, queue_limit
;
2553 /* Ensure there's room for more dataplane updates */
2554 queue_limit
= dplane_get_in_queue_limit();
2555 queue_len
= dplane_get_in_queue_len();
2556 if (queue_len
> queue_limit
) {
2557 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2558 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2559 queue_len
, queue_limit
);
2561 /* Ensure that the meta-queue is actually enqueued */
2562 if (work_queue_empty(zrouter
.ribq
))
2563 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2565 return WQ_QUEUE_BLOCKED
;
2568 for (i
= 0; i
< MQ_SIZE
; i
++)
2569 if (process_subq(mq
->subq
[i
], i
)) {
2573 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2578 * Look into the RN and queue it into the highest priority queue
2579 * at this point in time for processing.
2581 * We will enqueue a route node only once per invocation.
2583 * There are two possibilities here that should be kept in mind.
2584 * If the original invocation has not been pulled off for processing
2585 * yet, A subsuquent invocation can have a route entry with a better
2586 * meta queue index value and we can have a situation where
2587 * we might have the same node enqueued 2 times. Not necessarily
2588 * an optimal situation but it should be ok.
2590 * The other possibility is that the original invocation has not
2591 * been pulled off for processing yet, A subsusquent invocation
2592 * doesn't have a route_entry with a better meta-queue and the
2593 * original metaqueue index value will win and we'll end up with
2594 * the route node enqueued once.
2596 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2598 struct route_node
*rn
= NULL
;
2599 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2600 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2602 rn
= (struct route_node
*)data
;
2604 RNODE_FOREACH_RE (rn
, curr_re
) {
2605 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2607 if (curr_qindex
<= qindex
) {
2609 qindex
= curr_qindex
;
2616 /* Invariant: at this point we always have rn->info set. */
2617 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2618 RIB_ROUTE_QUEUED(qindex
))) {
2619 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2620 rnode_debug(rn
, re
->vrf_id
,
2621 "rn %p is already queued in sub-queue %s",
2622 (void *)rn
, subqueue2str(qindex
));
2626 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2627 listnode_add(mq
->subq
[qindex
], rn
);
2628 route_lock_node(rn
);
2631 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2632 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %s",
2633 (void *)rn
, subqueue2str(qindex
));
2638 static int rib_meta_queue_nhg_ctx_add(struct meta_queue
*mq
, void *data
)
2640 struct nhg_ctx
*ctx
= NULL
;
2641 uint8_t qindex
= META_QUEUE_NHG
;
2642 struct wq_nhg_wrapper
*w
;
2644 ctx
= (struct nhg_ctx
*)data
;
2649 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
2651 w
->type
= WQ_NHG_WRAPPER_TYPE_CTX
;
2654 listnode_add(mq
->subq
[qindex
], w
);
2657 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2658 zlog_debug("NHG Context id=%u queued into sub-queue %s",
2659 ctx
->id
, subqueue2str(qindex
));
2664 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2666 struct nhg_hash_entry
*nhe
= NULL
;
2667 uint8_t qindex
= META_QUEUE_NHG
;
2668 struct wq_nhg_wrapper
*w
;
2670 nhe
= (struct nhg_hash_entry
*)data
;
2675 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_nhg_wrapper
));
2677 w
->type
= WQ_NHG_WRAPPER_TYPE_NHG
;
2680 listnode_add(mq
->subq
[qindex
], w
);
2683 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2684 zlog_debug("NHG id=%u queued into sub-queue %s", nhe
->id
,
2685 subqueue2str(qindex
));
2690 static int rib_meta_queue_evpn_add(struct meta_queue
*mq
, void *data
)
2692 listnode_add(mq
->subq
[META_QUEUE_EVPN
], data
);
2698 static int mq_add_handler(void *data
,
2699 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2701 if (zrouter
.ribq
== NULL
) {
2702 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2703 "%s: work_queue does not exist!", __func__
);
2708 * The RIB queue should normally be either empty or holding the only
2709 * work_queue_item element. In the latter case this element would
2710 * hold a pointer to the meta queue structure, which must be used to
2711 * actually queue the route nodes to process. So create the MQ
2712 * holder, if necessary, then push the work into it in any case.
2713 * This semantics was introduced after 0.99.9 release.
2715 if (work_queue_empty(zrouter
.ribq
))
2716 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2718 return mq_add_func(zrouter
.mq
, data
);
2721 /* Add route_node to work queue and schedule processing */
2722 int rib_queue_add(struct route_node
*rn
)
2726 /* Pointless to queue a route_node with no RIB entries to add or remove
2728 if (!rnode_to_ribs(rn
)) {
2729 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
2730 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
2731 zlog_backtrace(LOG_DEBUG
);
2735 return mq_add_handler(rn
, rib_meta_queue_add
);
2739 * Enqueue incoming nhg info from OS for processing
2741 int rib_queue_nhg_ctx_add(struct nhg_ctx
*ctx
)
2745 return mq_add_handler(ctx
, rib_meta_queue_nhg_ctx_add
);
2749 * Enqueue incoming nhg from proto daemon for processing
2751 int rib_queue_nhe_add(struct nhg_hash_entry
*nhe
)
2756 return mq_add_handler(nhe
, rib_meta_queue_nhg_add
);
2760 * Enqueue evpn route for processing
2762 int zebra_rib_queue_evpn_route_add(vrf_id_t vrf_id
, const struct ethaddr
*rmac
,
2763 const struct ipaddr
*vtep_ip
,
2764 const struct prefix
*host_prefix
)
2766 struct wq_evpn_wrapper
*w
;
2768 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2770 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
2775 w
->prefix
= *host_prefix
;
2777 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2778 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
2779 vrf_id
, vtep_ip
, host_prefix
);
2781 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2784 int zebra_rib_queue_evpn_route_del(vrf_id_t vrf_id
,
2785 const struct ipaddr
*vtep_ip
,
2786 const struct prefix
*host_prefix
)
2788 struct wq_evpn_wrapper
*w
;
2790 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2792 w
->type
= WQ_EVPN_WRAPPER_TYPE_VRFROUTE
;
2796 w
->prefix
= *host_prefix
;
2798 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2799 zlog_debug("%s: (%u)%pIA, host prefix %pFX enqueued", __func__
,
2800 vrf_id
, vtep_ip
, host_prefix
);
2802 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2805 /* Enqueue EVPN remote ES for processing */
2806 int zebra_rib_queue_evpn_rem_es_add(const esi_t
*esi
,
2807 const struct in_addr
*vtep_ip
,
2808 bool esr_rxed
, uint8_t df_alg
,
2811 struct wq_evpn_wrapper
*w
;
2812 char buf
[ESI_STR_LEN
];
2814 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2816 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
2819 w
->ip
.ipa_type
= IPADDR_V4
;
2820 w
->ip
.ipaddr_v4
= *vtep_ip
;
2821 w
->esr_rxed
= esr_rxed
;
2823 w
->df_pref
= df_pref
;
2825 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2826 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
2827 esi_to_str(esi
, buf
, sizeof(buf
)));
2829 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2832 int zebra_rib_queue_evpn_rem_es_del(const esi_t
*esi
,
2833 const struct in_addr
*vtep_ip
)
2835 struct wq_evpn_wrapper
*w
;
2836 char buf
[ESI_STR_LEN
];
2838 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2840 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_ES
;
2843 w
->ip
.ipa_type
= IPADDR_V4
;
2844 w
->ip
.ipaddr_v4
= *vtep_ip
;
2846 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2847 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
2848 esi_to_str(esi
, buf
, sizeof(buf
));
2850 strlcpy(buf
, "-", sizeof(buf
));
2852 zlog_debug("%s: vtep %pI4, esi %s enqueued", __func__
, vtep_ip
,
2856 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2860 * Enqueue EVPN remote macip update for processing
2862 int zebra_rib_queue_evpn_rem_macip_add(vni_t vni
, const struct ethaddr
*macaddr
,
2863 const struct ipaddr
*ipaddr
,
2864 uint8_t flags
, uint32_t seq
,
2865 struct in_addr vtep_ip
, const esi_t
*esi
)
2867 struct wq_evpn_wrapper
*w
;
2868 char buf
[ESI_STR_LEN
];
2870 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2872 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
2875 w
->macaddr
= *macaddr
;
2879 w
->vtep_ip
= vtep_ip
;
2882 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2883 if (memcmp(esi
, zero_esi
, sizeof(esi_t
)) != 0)
2884 esi_to_str(esi
, buf
, sizeof(buf
));
2886 strlcpy(buf
, "-", sizeof(buf
));
2888 zlog_debug("%s: mac %pEA, vtep %pI4, esi %s enqueued", __func__
,
2889 macaddr
, &vtep_ip
, buf
);
2892 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2895 int zebra_rib_queue_evpn_rem_macip_del(vni_t vni
, const struct ethaddr
*macaddr
,
2896 const struct ipaddr
*ip
,
2897 struct in_addr vtep_ip
)
2899 struct wq_evpn_wrapper
*w
;
2901 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2903 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_MACIP
;
2906 w
->macaddr
= *macaddr
;
2908 w
->vtep_ip
= vtep_ip
;
2910 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2911 zlog_debug("%s: mac %pEA, vtep %pI4 enqueued", __func__
,
2914 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2918 * Enqueue remote VTEP address for processing
2920 int zebra_rib_queue_evpn_rem_vtep_add(vrf_id_t vrf_id
, vni_t vni
,
2921 struct in_addr vtep_ip
, int flood_control
)
2923 struct wq_evpn_wrapper
*w
;
2925 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2927 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
2931 w
->vtep_ip
= vtep_ip
;
2932 w
->flags
= flood_control
;
2934 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2935 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
2938 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2941 int zebra_rib_queue_evpn_rem_vtep_del(vrf_id_t vrf_id
, vni_t vni
,
2942 struct in_addr vtep_ip
)
2944 struct wq_evpn_wrapper
*w
;
2946 w
= XCALLOC(MTYPE_WQ_WRAPPER
, sizeof(struct wq_evpn_wrapper
));
2948 w
->type
= WQ_EVPN_WRAPPER_TYPE_REM_VTEP
;
2952 w
->vtep_ip
= vtep_ip
;
2954 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2955 zlog_debug("%s: vrf %u, vtep %pI4 enqueued", __func__
, vrf_id
,
2958 return mq_add_handler(w
, rib_meta_queue_evpn_add
);
2962 /* Create new meta queue.
2963 A destructor function doesn't seem to be necessary here.
2965 static struct meta_queue
*meta_queue_new(void)
2967 struct meta_queue
*new;
2970 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2972 for (i
= 0; i
< MQ_SIZE
; i
++) {
2973 new->subq
[i
] = list_new();
2974 assert(new->subq
[i
]);
2980 /* Clean up the EVPN meta-queue list */
2981 static void evpn_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
2982 struct zebra_vrf
*zvrf
)
2984 struct listnode
*node
, *nnode
;
2985 struct wq_evpn_wrapper
*w
;
2987 /* Free the node wrapper object, and the struct it wraps */
2988 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
2990 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
2992 if (w
->vrf_id
!= vrf_id
)
2998 XFREE(MTYPE_WQ_WRAPPER
, w
);
3000 list_delete_node(l
, node
);
3005 /* Clean up the nhg meta-queue list */
3006 static void nhg_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3007 struct zebra_vrf
*zvrf
)
3009 struct wq_nhg_wrapper
*w
;
3010 struct listnode
*node
, *nnode
;
3012 /* Free the node wrapper object, and the struct it wraps */
3013 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, w
)) {
3015 vrf_id_t vrf_id
= zvrf
->vrf
->vrf_id
;
3017 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
&&
3018 w
->u
.ctx
->vrf_id
!= vrf_id
)
3020 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
&&
3021 w
->u
.nhe
->vrf_id
!= vrf_id
)
3024 if (w
->type
== WQ_NHG_WRAPPER_TYPE_CTX
)
3025 nhg_ctx_free(&w
->u
.ctx
);
3026 else if (w
->type
== WQ_NHG_WRAPPER_TYPE_NHG
)
3027 zebra_nhg_free(w
->u
.nhe
);
3030 XFREE(MTYPE_WQ_WRAPPER
, w
);
3032 list_delete_node(l
, node
);
3037 static void rib_meta_queue_free(struct meta_queue
*mq
, struct list
*l
,
3038 struct zebra_vrf
*zvrf
)
3040 struct route_node
*rnode
;
3041 struct listnode
*node
, *nnode
;
3043 for (ALL_LIST_ELEMENTS(l
, node
, nnode
, rnode
)) {
3044 rib_dest_t
*dest
= rib_dest_from_rnode(rnode
);
3046 if (dest
&& rib_dest_vrf(dest
) != zvrf
)
3049 route_unlock_node(rnode
);
3051 list_delete_node(l
, node
);
3057 void meta_queue_free(struct meta_queue
*mq
, struct zebra_vrf
*zvrf
)
3059 enum meta_queue_indexes i
;
3061 for (i
= 0; i
< MQ_SIZE
; i
++) {
3062 /* Some subqueues may need cleanup - nhgs for example */
3064 case META_QUEUE_NHG
:
3065 nhg_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3067 case META_QUEUE_EVPN
:
3068 evpn_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3070 case META_QUEUE_CONNECTED
:
3071 case META_QUEUE_KERNEL
:
3072 case META_QUEUE_STATIC
:
3073 case META_QUEUE_NOTBGP
:
3074 case META_QUEUE_BGP
:
3075 case META_QUEUE_OTHER
:
3076 rib_meta_queue_free(mq
, mq
->subq
[i
], zvrf
);
3080 list_delete(&mq
->subq
[i
]);
3084 XFREE(MTYPE_WORK_QUEUE
, mq
);
3087 /* initialise zebra rib work queue */
3088 static void rib_queue_init(void)
3090 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
3091 "route_node processing"))) {
3092 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3093 "%s: could not initialise work queue!", __func__
);
3097 /* fill in the work queue spec */
3098 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
3099 zrouter
.ribq
->spec
.errorfunc
= NULL
;
3100 zrouter
.ribq
->spec
.completion_func
= NULL
;
3101 /* XXX: TODO: These should be runtime configurable via vty */
3102 zrouter
.ribq
->spec
.max_retries
= 3;
3103 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
3104 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
3106 if (!(zrouter
.mq
= meta_queue_new())) {
3107 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
3108 "%s: could not initialise meta queue!", __func__
);
3114 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
3118 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
3119 rnh_list_init(&dest
->nht
);
3120 re_list_init(&dest
->routes
);
3121 route_lock_node(rn
); /* rn route table reference */
3128 /* RIB updates are processed via a queue of pointers to route_nodes.
3130 * The queue length is bounded by the maximal size of the routing table,
3131 * as a route_node will not be requeued, if already queued.
3133 * REs are submitted via rib_addnode or rib_delnode which set minimal
3134 * state, or static_install_route (when an existing RE is updated)
3135 * and then submit route_node to queue for best-path selection later.
3136 * Order of add/delete state changes are preserved for any given RE.
3138 * Deleted REs are reaped during best-path selection.
3141 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
3142 * |-------->| | best RE, if required
3144 * static_install->|->rib_addqueue...... -> rib_process
3146 * |-------->| |-> rib_unlink
3147 * |-> set ROUTE_ENTRY_REMOVE |
3148 * rib_delnode (RE freed)
3150 * The 'info' pointer of a route_node points to a rib_dest_t
3151 * ('dest'). Queueing state for a route_node is kept on the dest. The
3152 * dest is created on-demand by rib_link() and is kept around at least
3153 * as long as there are ribs hanging off it (@see rib_gc_dest()).
3155 * Refcounting (aka "locking" throughout the Zebra and FRR code):
3157 * - route_nodes: refcounted by:
3158 * - dest attached to route_node:
3159 * - managed by: rib_link/rib_gc_dest
3160 * - route_node processing queue
3161 * - managed by: rib_addqueue, rib_process.
3165 /* Add RE to head of the route node. */
3166 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
3170 const char *rmap_name
;
3174 dest
= rib_dest_from_rnode(rn
);
3176 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3177 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
3179 dest
= zebra_rib_create_dest(rn
);
3182 re_list_add_head(&dest
->routes
, re
);
3184 afi
= (rn
->p
.family
== AF_INET
)
3186 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3187 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3188 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3190 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
3191 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
3196 static void rib_addnode(struct route_node
*rn
,
3197 struct route_entry
*re
, int process
)
3199 /* RE node has been un-removed before route-node is processed.
3200 * route_node must hence already be on the queue for processing..
3202 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3203 if (IS_ZEBRA_DEBUG_RIB
)
3204 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
3205 (void *)rn
, (void *)re
);
3207 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3210 rib_link(rn
, re
, process
);
3213 static void rib_re_nhg_free(struct route_entry
*re
)
3215 if (re
->nhe
&& re
->nhe_id
) {
3216 assert(re
->nhe
->id
== re
->nhe_id
);
3217 route_entry_update_nhe(re
, NULL
);
3218 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
3219 nexthops_free(re
->nhe
->nhg
.nexthop
);
3221 nexthops_free(re
->fib_ng
.nexthop
);
3227 * Detach a rib structure from a route_node.
3229 * Note that a call to rib_unlink() should be followed by a call to
3230 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
3231 * longer required to be deleted.
3233 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
3239 if (IS_ZEBRA_DEBUG_RIB
)
3240 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
3243 dest
= rib_dest_from_rnode(rn
);
3245 re_list_del(&dest
->routes
, re
);
3247 if (dest
->selected_fib
== re
)
3248 dest
->selected_fib
= NULL
;
3250 rib_re_nhg_free(re
);
3252 zapi_re_opaque_free(re
->opaque
);
3254 XFREE(MTYPE_RE
, re
);
3257 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
3261 if (IS_ZEBRA_DEBUG_RIB
)
3262 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
3263 (void *)rn
, (void *)re
);
3264 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
3266 afi
= (rn
->p
.family
== AF_INET
)
3268 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
3269 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
3270 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
3272 zebra_del_import_table_entry(zvrf
, rn
, re
);
3273 /* Just clean up if non main table */
3274 if (IS_ZEBRA_DEBUG_RIB
)
3275 zlog_debug("%s(%u):%pRN: Freeing route rn %p, re %p (%s)",
3276 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, rn
,
3277 rn
, re
, zebra_route_string(re
->type
));
3286 * Helper that debugs a single nexthop within a route-entry
3288 static void _route_entry_dump_nh(const struct route_entry
*re
,
3289 const char *straddr
,
3290 const struct nexthop
*nexthop
)
3292 char nhname
[PREFIX_STRLEN
];
3293 char backup_str
[50];
3296 char label_str
[MPLS_LABEL_STRLEN
];
3298 struct interface
*ifp
;
3299 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
3301 switch (nexthop
->type
) {
3302 case NEXTHOP_TYPE_BLACKHOLE
:
3303 snprintf(nhname
, sizeof(nhname
), "Blackhole");
3305 case NEXTHOP_TYPE_IFINDEX
:
3306 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
3307 snprintf(nhname
, sizeof(nhname
), "%s",
3308 ifp
? ifp
->name
: "Unknown");
3310 case NEXTHOP_TYPE_IPV4
:
3312 case NEXTHOP_TYPE_IPV4_IFINDEX
:
3313 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3315 case NEXTHOP_TYPE_IPV6
:
3316 case NEXTHOP_TYPE_IPV6_IFINDEX
:
3317 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
3322 label_str
[0] = '\0';
3323 if (nexthop
->nh_label
&& nexthop
->nh_label
->num_labels
> 0) {
3324 mpls_label2str(nexthop
->nh_label
->num_labels
,
3325 nexthop
->nh_label
->label
, label_str
,
3326 sizeof(label_str
), 0 /*pretty*/);
3327 strlcat(label_str
, ", ", sizeof(label_str
));
3330 backup_str
[0] = '\0';
3331 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
3332 snprintf(backup_str
, sizeof(backup_str
), "backup ");
3333 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
3334 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
3335 nexthop
->backup_idx
[i
]);
3336 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
3341 if (nexthop
->weight
)
3342 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
3344 zlog_debug("%s: %s %s[%u] %svrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s%s",
3345 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
3346 nexthop
->ifindex
, label_str
, vrf
? vrf
->name
: "Unknown",
3348 wgt_str
, backup_str
,
3349 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
3352 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
3355 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
3358 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
3361 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
3364 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
3365 ? "FILTERED " : ""),
3366 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
3368 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
3370 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)
3375 /* This function dumps the contents of a given RE entry into
3376 * standard debug log. Calling function name and IP prefix in
3377 * question are passed as 1st and 2nd arguments.
3379 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
3380 union prefixconstptr src_pp
,
3381 const struct route_entry
*re
)
3383 const struct prefix
*src_p
= src_pp
.p
;
3384 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
3385 char straddr
[PREFIX_STRLEN
];
3386 char srcaddr
[PREFIX_STRLEN
];
3387 char flags_buf
[128];
3388 char status_buf
[128];
3389 struct nexthop
*nexthop
;
3390 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
3391 struct nexthop_group
*nhg
;
3393 prefix2str(pp
, straddr
, sizeof(straddr
));
3395 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
3396 (const void *)re
, straddr
,
3397 is_srcdst
? " from " : "",
3398 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
3400 VRF_LOGNAME(vrf
), re
->vrf_id
);
3401 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
3402 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
3405 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
3406 straddr
, re
->metric
, re
->mtu
, re
->distance
,
3407 zclient_dump_route_flags(re
->flags
, flags_buf
,
3409 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
3410 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
3411 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
3412 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
3415 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3416 _route_entry_dump_nh(re
, straddr
, nexthop
);
3418 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
3419 zlog_debug("%s: backup nexthops:", straddr
);
3421 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
3422 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
3423 _route_entry_dump_nh(re
, straddr
, nexthop
);
3426 zlog_debug("%s: dump complete", straddr
);
3430 * Internal route-add implementation; there are a couple of different public
3431 * signatures. Callers in this path are responsible for the memory they
3432 * allocate: if they allocate a nexthop_group or backup nexthop info, they
3433 * must free those objects. If this returns < 0, an error has occurred and the
3434 * route_entry 're' has not been captured; the caller should free that also.
3440 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
3441 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3442 struct nhg_hash_entry
*re_nhe
, bool startup
)
3444 struct nhg_hash_entry
*nhe
= NULL
;
3445 struct route_table
*table
;
3446 struct route_node
*rn
;
3447 struct route_entry
*same
= NULL
, *first_same
= NULL
;
3455 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3458 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
3463 if (re
->nhe_id
> 0) {
3464 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
3468 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3469 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
3475 /* Lookup nhe from route information */
3476 nhe
= zebra_nhg_rib_find_nhe(re_nhe
, afi
);
3478 char buf2
[PREFIX_STRLEN
] = "";
3481 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3482 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
3483 p
, src_p
? " from " : "",
3484 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
3492 * Attach the re to the nhe's nexthop group.
3494 * TODO: This will need to change when we start getting IDs from upper
3495 * level protocols, as the refcnt might be wrong, since it checks
3496 * if old_id != new_id.
3498 route_entry_update_nhe(re
, nhe
);
3500 /* Make it sure prefixlen is applied to the prefix. */
3503 apply_mask_ipv6(src_p
);
3505 /* Set default distance by route type. */
3506 if (re
->distance
== 0)
3507 re
->distance
= route_distance(re
->type
);
3509 /* Lookup route node.*/
3510 rn
= srcdest_rnode_get(table
, p
, src_p
);
3513 * If same type of route are installed, treat it as a implicit
3514 * withdraw. If the user has specified the No route replace semantics
3515 * for the install don't do a route replace.
3517 RNODE_FOREACH_RE (rn
, same
) {
3518 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
3523 /* Compare various route_entry properties */
3524 if (rib_compare_routes(re
, same
)) {
3527 if (first_same
== NULL
)
3535 (re
->flags
& ZEBRA_FLAG_SELFROUTE
) && zrouter
.asic_offloaded
) {
3537 if (IS_ZEBRA_DEBUG_RIB
)
3538 zlog_debug("prefix: %pRN is a self route where we do not have an entry for it. Dropping this update, it's useless", rn
);
3540 * We are not on startup, this is a self route
3541 * and we have asic offload. Which means
3542 * we are getting a callback for a entry
3543 * that was already deleted to the kernel
3544 * but an earlier response was just handed
3545 * back. Drop it on the floor
3547 rib_re_nhg_free(re
);
3549 XFREE(MTYPE_RE
, re
);
3554 /* If this route is kernel/connected route, notify the dataplane. */
3555 if (RIB_SYSTEM_ROUTE(re
)) {
3556 /* Notify dataplane */
3557 dplane_sys_route_add(rn
, re
);
3560 /* Link new re to node.*/
3561 if (IS_ZEBRA_DEBUG_RIB
) {
3562 rnode_debug(rn
, re
->vrf_id
,
3563 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
3564 rn
, re
, zebra_route_string(re
->type
), same
,
3567 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3568 route_entry_dump(p
, src_p
, re
);
3571 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3572 rib_addnode(rn
, re
, 1);
3574 /* Free implicit route.*/
3577 rib_delnode(rn
, same
);
3580 /* See if we can remove some RE entries that are queued for
3581 * removal, but won't be considered in rib processing.
3583 dest
= rib_dest_from_rnode(rn
);
3584 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
3585 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3586 /* If the route was used earlier, must retain it. */
3587 if (dest
&& re
== dest
->selected_fib
)
3590 if (IS_ZEBRA_DEBUG_RIB
)
3591 rnode_debug(rn
, re
->vrf_id
, "rn %p, removing unneeded re %p",
3598 route_unlock_node(rn
);
3603 * Add a single route.
3605 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
3606 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3607 struct nexthop_group
*ng
, bool startup
)
3610 struct nhg_hash_entry nhe
;
3615 /* We either need nexthop(s) or an existing nexthop id */
3616 if (ng
== NULL
&& re
->nhe_id
== 0)
3620 * Use a temporary nhe to convey info to the common/main api.
3622 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
3624 nhe
.nhg
.nexthop
= ng
->nexthop
;
3625 else if (re
->nhe_id
> 0)
3626 nhe
.id
= re
->nhe_id
;
3628 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, &nhe
, startup
);
3630 /* In this path, the callers expect memory to be freed. */
3631 nexthop_group_delete(&ng
);
3633 /* In error cases, free the route also */
3635 XFREE(MTYPE_RE
, re
);
3640 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3641 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3642 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3643 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
3644 uint8_t distance
, bool fromkernel
)
3646 struct route_table
*table
;
3647 struct route_node
*rn
;
3648 struct route_entry
*re
;
3649 struct route_entry
*fib
= NULL
;
3650 struct route_entry
*same
= NULL
;
3651 struct nexthop
*rtnh
;
3652 char buf2
[INET6_ADDRSTRLEN
];
3655 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3658 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
3666 apply_mask_ipv6(src_p
);
3668 /* Lookup route node. */
3669 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
3671 if (IS_ZEBRA_DEBUG_RIB
) {
3672 char src_buf
[PREFIX_STRLEN
];
3673 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3675 if (src_p
&& src_p
->prefixlen
)
3676 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
3680 zlog_debug("%s[%d]:%pRN%s%s doesn't exist in rib",
3681 vrf
->name
, table_id
, rn
,
3682 (src_buf
[0] != '\0') ? " from " : "",
3688 dest
= rib_dest_from_rnode(rn
);
3689 fib
= dest
->selected_fib
;
3691 /* Lookup same type route. */
3692 RNODE_FOREACH_RE (rn
, re
) {
3693 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3696 if (re
->type
!= type
)
3698 if (re
->instance
!= instance
)
3700 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
3701 distance
!= re
->distance
)
3704 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
3706 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
3707 (rtnh
= re
->nhe
->nhg
.nexthop
)
3708 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
3709 if (rtnh
->ifindex
!= nh
->ifindex
)
3715 /* Make sure that the route found has the same gateway. */
3716 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
3725 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3727 * No guarantee all kernel send nh with labels
3730 if (nexthop_same_no_labels(rtnh
, nh
)) {
3739 /* If same type of route can't be found and this message is from
3743 * In the past(HA!) we could get here because
3744 * we were receiving a route delete from the
3745 * kernel and we're not marking the proto
3746 * as coming from it's appropriate originator.
3747 * Now that we are properly noticing the fact
3748 * that the kernel has deleted our route we
3749 * are not going to get called in this path
3750 * I am going to leave this here because
3751 * this might still work this way on non-linux
3752 * platforms as well as some weird state I have
3753 * not properly thought of yet.
3754 * If we can show that this code path is
3755 * dead then we can remove it.
3757 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
3758 if (IS_ZEBRA_DEBUG_RIB
) {
3759 rnode_debug(rn
, vrf_id
,
3760 "rn %p, re %p (%s) was deleted from kernel, adding",
3762 zebra_route_string(fib
->type
));
3764 if (zrouter
.allow_delete
||
3765 CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3766 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3768 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3770 UNSET_FLAG(rtnh
->flags
,
3774 * This is a non FRR route
3775 * as such we should mark
3778 dest
->selected_fib
= NULL
;
3780 /* This means someone else, other than Zebra,
3782 * a Zebra router from the kernel. We will add
3784 rib_install_kernel(rn
, fib
, NULL
);
3787 if (IS_ZEBRA_DEBUG_RIB
) {
3791 "via %s ifindex %d type %d doesn't exist in rib",
3792 inet_ntop(afi2family(afi
),
3799 "type %d doesn't exist in rib",
3802 route_unlock_node(rn
);
3808 struct nexthop
*tmp_nh
;
3810 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) &&
3811 !zrouter
.allow_delete
) {
3812 rib_install_kernel(rn
, same
, NULL
);
3813 route_unlock_node(rn
);
3818 /* Special handling for IPv4 or IPv6 routes sourced from
3819 * EVPN - the nexthop (and associated MAC) need to be
3820 * uninstalled if no more refs.
3822 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3823 struct ipaddr vtep_ip
;
3825 if (CHECK_FLAG(tmp_nh
->flags
, NEXTHOP_FLAG_EVPN
)) {
3826 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3827 if (afi
== AFI_IP
) {
3828 vtep_ip
.ipa_type
= IPADDR_V4
;
3829 memcpy(&(vtep_ip
.ipaddr_v4
),
3830 &(tmp_nh
->gate
.ipv4
),
3831 sizeof(struct in_addr
));
3833 vtep_ip
.ipa_type
= IPADDR_V6
;
3834 memcpy(&(vtep_ip
.ipaddr_v6
),
3835 &(tmp_nh
->gate
.ipv6
),
3836 sizeof(struct in6_addr
));
3838 zebra_rib_queue_evpn_route_del(re
->vrf_id
,
3843 /* Notify dplane if system route changes */
3844 if (RIB_SYSTEM_ROUTE(re
))
3845 dplane_sys_route_del(rn
, same
);
3847 rib_delnode(rn
, same
);
3850 route_unlock_node(rn
);
3855 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3856 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3857 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3858 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3859 uint8_t distance
, route_tag_t tag
, bool startup
)
3861 struct route_entry
*re
= NULL
;
3862 struct nexthop
*nexthop
= NULL
;
3863 struct nexthop_group
*ng
= NULL
;
3865 /* Allocate new route_entry structure. */
3866 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3868 re
->instance
= instance
;
3869 re
->distance
= distance
;
3871 re
->metric
= metric
;
3873 re
->table
= table_id
;
3874 re
->vrf_id
= vrf_id
;
3875 re
->uptime
= monotime(NULL
);
3877 re
->nhe_id
= nhe_id
;
3879 /* If the owner of the route supplies a shared nexthop-group id,
3880 * we'll use that. Otherwise, pass the nexthop along directly.
3883 ng
= nexthop_group_new();
3886 nexthop
= nexthop_new();
3888 nexthop_group_add_sorted(ng
, nexthop
);
3891 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
, startup
);
3894 static const char *rib_update_event2str(enum rib_update_event event
)
3896 const char *ret
= "UNKNOWN";
3899 case RIB_UPDATE_KERNEL
:
3900 ret
= "RIB_UPDATE_KERNEL";
3902 case RIB_UPDATE_RMAP_CHANGE
:
3903 ret
= "RIB_UPDATE_RMAP_CHANGE";
3905 case RIB_UPDATE_OTHER
:
3906 ret
= "RIB_UPDATE_OTHER";
3908 case RIB_UPDATE_MAX
:
3916 /* Schedule route nodes to be processed if they match the type */
3917 static void rib_update_route_node(struct route_node
*rn
, int type
)
3919 struct route_entry
*re
, *next
;
3920 bool re_changed
= false;
3922 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3923 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3924 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3933 /* Schedule routes of a particular table (address-family) based on event. */
3934 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
3937 struct route_node
*rn
;
3939 if (IS_ZEBRA_DEBUG_EVENT
) {
3940 struct zebra_vrf
*zvrf
;
3944 ? ((struct rib_table_info
*)table
->info
)->zvrf
3946 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3948 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
3949 table
->info
? afi2str(
3950 ((struct rib_table_info
*)table
->info
)->afi
)
3952 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
3953 rib_update_event2str(event
), zebra_route_string(rtype
));
3956 /* Walk all routes and queue for processing, if appropriate for
3957 * the trigger event.
3959 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3961 * If we are looking at a route node and the node
3962 * has already been queued we don't
3963 * need to queue it up again
3966 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3967 RIB_ROUTE_ANY_QUEUED
))
3971 case RIB_UPDATE_KERNEL
:
3972 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3974 case RIB_UPDATE_RMAP_CHANGE
:
3975 case RIB_UPDATE_OTHER
:
3976 rib_update_route_node(rn
, rtype
);
3984 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
,
3987 struct route_table
*table
;
3989 if (IS_ZEBRA_DEBUG_EVENT
)
3990 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3991 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3993 /* Process routes of interested address-families. */
3994 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3996 rib_update_table(table
, event
, rtype
);
3998 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
4000 rib_update_table(table
, event
, rtype
);
4003 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
4005 struct zebra_router_table
*zrt
;
4007 if (IS_ZEBRA_DEBUG_EVENT
)
4008 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
4009 rib_update_event2str(event
));
4011 /* Just iterate over all the route tables, rather than vrf lookups */
4012 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
4013 rib_update_table(zrt
->table
, event
, rtype
);
4016 struct rib_update_ctx
{
4017 enum rib_update_event event
;
4022 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
4023 enum rib_update_event event
)
4025 struct rib_update_ctx
*ctx
;
4027 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
4030 ctx
->vrf_id
= vrf_id
;
4035 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
4037 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
4040 static void rib_update_handler(struct thread
*thread
)
4042 struct rib_update_ctx
*ctx
;
4044 ctx
= THREAD_ARG(thread
);
4047 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
4049 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
, ZEBRA_ROUTE_ALL
);
4051 rib_update_ctx_fini(&ctx
);
4055 * Thread list to ensure we don't schedule a ton of events
4056 * if interfaces are flapping for instance.
4058 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
4060 /* Schedule a RIB update event for all vrfs */
4061 void rib_update(enum rib_update_event event
)
4063 struct rib_update_ctx
*ctx
;
4065 if (thread_is_scheduled(t_rib_update_threads
[event
]))
4068 ctx
= rib_update_ctx_init(0, event
);
4069 ctx
->vrf_all
= true;
4071 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
4072 &t_rib_update_threads
[event
]);
4074 if (IS_ZEBRA_DEBUG_EVENT
)
4075 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
4076 rib_update_event2str(event
));
4079 /* Delete self installed routes after zebra is relaunched. */
4080 void rib_sweep_table(struct route_table
*table
)
4082 struct route_node
*rn
;
4083 struct route_entry
*re
;
4084 struct route_entry
*next
;
4085 struct nexthop
*nexthop
;
4090 if (IS_ZEBRA_DEBUG_RIB
)
4091 zlog_debug("%s: starting", __func__
);
4093 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4094 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4096 if (IS_ZEBRA_DEBUG_RIB
)
4097 route_entry_dump(&rn
->p
, NULL
, re
);
4099 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4102 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
4106 * If routes are older than startup_time then
4107 * we know we read them in from the kernel.
4108 * As such we can safely remove them.
4110 if (zrouter
.startup_time
< re
->uptime
)
4114 * So we are starting up and have received
4115 * routes from the kernel that we have installed
4116 * from a previous run of zebra but not cleaned
4117 * up ( say a kill -9 )
4118 * But since we haven't actually installed
4119 * them yet( we received them from the kernel )
4120 * we don't think they are active.
4121 * So let's pretend they are active to actually
4123 * In all honesty I'm not sure if we should
4124 * mark them as active when we receive them
4125 * This is startup only so probably ok.
4127 * If we ever decide to move rib_sweep_table
4128 * to a different spot (ie startup )
4129 * this decision needs to be revisited
4131 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
4132 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
4133 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
4135 rib_uninstall_kernel(rn
, re
);
4136 rib_delnode(rn
, re
);
4140 if (IS_ZEBRA_DEBUG_RIB
)
4141 zlog_debug("%s: ends", __func__
);
4144 /* Sweep all RIB tables. */
4145 void rib_sweep_route(struct thread
*t
)
4148 struct zebra_vrf
*zvrf
;
4150 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4151 if ((zvrf
= vrf
->info
) == NULL
)
4154 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4155 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4158 zebra_router_sweep_route();
4159 zebra_router_sweep_nhgs();
4162 /* Remove specific by protocol routes from 'table'. */
4163 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
4164 struct route_table
*table
)
4166 struct route_node
*rn
;
4167 struct route_entry
*re
;
4168 struct route_entry
*next
;
4169 unsigned long n
= 0;
4172 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
4173 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
4174 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
4176 if (re
->type
== proto
4177 && re
->instance
== instance
) {
4178 rib_delnode(rn
, re
);
4185 /* Remove specific by protocol routes. */
4186 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
4189 struct zebra_vrf
*zvrf
;
4190 struct other_route_table
*ort
;
4191 unsigned long cnt
= 0;
4193 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
4198 cnt
+= rib_score_proto_table(proto
, instance
,
4199 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4200 + rib_score_proto_table(
4202 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4204 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
4205 rib_score_proto_table(proto
, instance
, ort
->table
);
4211 /* Close RIB and clean up kernel routes. */
4212 void rib_close_table(struct route_table
*table
)
4214 struct route_node
*rn
;
4220 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
4221 dest
= rib_dest_from_rnode(rn
);
4223 if (dest
&& dest
->selected_fib
) {
4224 rib_uninstall_kernel(rn
, dest
->selected_fib
);
4225 dest
->selected_fib
= NULL
;
4231 * Handler for async dataplane results after a pseudowire installation
4233 static void handle_pw_result(struct zebra_dplane_ctx
*ctx
)
4235 struct zebra_pw
*pw
;
4236 struct zebra_vrf
*vrf
;
4238 /* The pseudowire code assumes success - we act on an error
4239 * result for installation attempts here.
4241 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
4244 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
4245 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
4246 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
4248 zebra_pw_install_failure(pw
,
4249 dplane_ctx_get_pw_status(ctx
));
4254 * Handle results from the dataplane system. Dequeue update context
4255 * structs, dispatch to appropriate internal handlers.
4257 static void rib_process_dplane_results(struct thread
*thread
)
4259 struct zebra_dplane_ctx
*ctx
;
4260 struct dplane_ctx_q ctxlist
;
4261 bool shut_p
= false;
4263 /* Dequeue a list of completed updates with one lock/unlock cycle */
4266 TAILQ_INIT(&ctxlist
);
4268 /* Take lock controlling queue of results */
4269 frr_with_mutex (&dplane_mutex
) {
4270 /* Dequeue list of context structs */
4271 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
4274 /* Dequeue context block */
4275 ctx
= dplane_ctx_dequeue(&ctxlist
);
4277 /* If we've emptied the results queue, we're done */
4281 /* If zebra is shutting down, avoid processing results,
4282 * just drain the results queue.
4284 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
4285 memory_order_relaxed
);
4288 dplane_ctx_fini(&ctx
);
4290 ctx
= dplane_ctx_dequeue(&ctxlist
);
4296 #ifdef HAVE_SCRIPTING
4297 char *script_name
= frrscript_names_get_script_name(
4298 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
4301 struct frrscript
*fs
;
4304 fs
= frrscript_new(script_name
);
4306 ret
= frrscript_load(
4307 fs
, ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4310 #endif /* HAVE_SCRIPTING */
4314 #ifdef HAVE_SCRIPTING
4317 ZEBRA_ON_RIB_PROCESS_HOOK_CALL
,
4319 #endif /* HAVE_SCRIPTING */
4321 switch (dplane_ctx_get_op(ctx
)) {
4322 case DPLANE_OP_ROUTE_INSTALL
:
4323 case DPLANE_OP_ROUTE_UPDATE
:
4324 case DPLANE_OP_ROUTE_DELETE
:
4325 /* Bit of special case for route updates
4326 * that were generated by async notifications:
4327 * we don't want to continue processing these
4330 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4331 rib_process_result(ctx
);
4334 case DPLANE_OP_ROUTE_NOTIFY
:
4335 rib_process_dplane_notify(ctx
);
4338 case DPLANE_OP_NH_INSTALL
:
4339 case DPLANE_OP_NH_UPDATE
:
4340 case DPLANE_OP_NH_DELETE
:
4341 zebra_nhg_dplane_result(ctx
);
4344 case DPLANE_OP_LSP_INSTALL
:
4345 case DPLANE_OP_LSP_UPDATE
:
4346 case DPLANE_OP_LSP_DELETE
:
4347 /* Bit of special case for LSP updates
4348 * that were generated by async notifications:
4349 * we don't want to continue processing these.
4351 if (dplane_ctx_get_notif_provider(ctx
) == 0)
4352 zebra_mpls_lsp_dplane_result(ctx
);
4355 case DPLANE_OP_LSP_NOTIFY
:
4356 zebra_mpls_process_dplane_notify(ctx
);
4359 case DPLANE_OP_PW_INSTALL
:
4360 case DPLANE_OP_PW_UNINSTALL
:
4361 handle_pw_result(ctx
);
4364 case DPLANE_OP_SYS_ROUTE_ADD
:
4365 case DPLANE_OP_SYS_ROUTE_DELETE
:
4368 case DPLANE_OP_MAC_INSTALL
:
4369 case DPLANE_OP_MAC_DELETE
:
4370 zebra_vxlan_handle_result(ctx
);
4373 case DPLANE_OP_RULE_ADD
:
4374 case DPLANE_OP_RULE_DELETE
:
4375 case DPLANE_OP_RULE_UPDATE
:
4376 case DPLANE_OP_IPTABLE_ADD
:
4377 case DPLANE_OP_IPTABLE_DELETE
:
4378 case DPLANE_OP_IPSET_ADD
:
4379 case DPLANE_OP_IPSET_DELETE
:
4380 case DPLANE_OP_IPSET_ENTRY_ADD
:
4381 case DPLANE_OP_IPSET_ENTRY_DELETE
:
4382 zebra_pbr_dplane_result(ctx
);
4385 case DPLANE_OP_INTF_ADDR_ADD
:
4386 case DPLANE_OP_INTF_ADDR_DEL
:
4387 case DPLANE_OP_INTF_INSTALL
:
4388 case DPLANE_OP_INTF_UPDATE
:
4389 case DPLANE_OP_INTF_DELETE
:
4390 case DPLANE_OP_INTF_NETCONFIG
:
4391 zebra_if_dplane_result(ctx
);
4394 /* Some op codes not handled here */
4395 case DPLANE_OP_ADDR_INSTALL
:
4396 case DPLANE_OP_ADDR_UNINSTALL
:
4397 case DPLANE_OP_NEIGH_INSTALL
:
4398 case DPLANE_OP_NEIGH_UPDATE
:
4399 case DPLANE_OP_NEIGH_DELETE
:
4400 case DPLANE_OP_NEIGH_IP_INSTALL
:
4401 case DPLANE_OP_NEIGH_IP_DELETE
:
4402 case DPLANE_OP_VTEP_ADD
:
4403 case DPLANE_OP_VTEP_DELETE
:
4404 case DPLANE_OP_NEIGH_DISCOVER
:
4405 case DPLANE_OP_BR_PORT_UPDATE
:
4406 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
4407 case DPLANE_OP_GRE_SET
:
4408 case DPLANE_OP_NONE
:
4411 } /* Dispatch by op code */
4413 dplane_ctx_fini(&ctx
);
4414 ctx
= dplane_ctx_dequeue(&ctxlist
);
4421 * Results are returned from the dataplane subsystem, in the context of
4422 * the dataplane pthread. We enqueue the results here for processing by
4423 * the main thread later.
4425 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
4427 /* Take lock controlling queue of results */
4428 frr_with_mutex (&dplane_mutex
) {
4429 /* Enqueue context blocks */
4430 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
4433 /* Ensure event is signalled to zebra main pthread */
4434 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
4441 * Ensure there are no empty slots in the route_info array.
4442 * Every route type in zebra should be present there.
4444 static void check_route_info(void)
4446 int len
= array_size(route_info
);
4449 * ZEBRA_ROUTE_SYSTEM is special cased since
4450 * its key is 0 anyway.
4452 * ZEBRA_ROUTE_ALL is also ignored.
4454 for (int i
= 0; i
< len
; i
++) {
4455 assert(route_info
[i
].key
>= ZEBRA_ROUTE_SYSTEM
&&
4456 route_info
[i
].key
< ZEBRA_ROUTE_MAX
);
4457 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
4461 /* Routing information base initialize. */
4468 /* Init dataplane, and register for results */
4469 pthread_mutex_init(&dplane_mutex
, NULL
);
4470 TAILQ_INIT(&rib_dplane_q
);
4471 zebra_dplane_init(rib_dplane_results
);
4477 * Get the first vrf id that is greater than the given vrf id if any.
4479 * Returns true if a vrf id was found, false otherwise.
4481 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4485 vrf
= vrf_lookup_by_id(vrf_id
);
4487 vrf
= RB_NEXT(vrf_id_head
, vrf
);
4489 *next_id_p
= vrf
->vrf_id
;
4498 * rib_tables_iter_next
4500 * Returns the next table in the iteration.
4502 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4504 struct route_table
*table
;
4507 * Array that helps us go over all AFI/SAFI combinations via one
4510 static const struct {
4514 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4515 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4516 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4521 switch (iter
->state
) {
4523 case RIB_TABLES_ITER_S_INIT
:
4524 iter
->vrf_id
= VRF_DEFAULT
;
4525 iter
->afi_safi_ix
= -1;
4529 case RIB_TABLES_ITER_S_ITERATING
:
4530 iter
->afi_safi_ix
++;
4533 while (iter
->afi_safi_ix
4534 < (int)array_size(afi_safis
)) {
4535 table
= zebra_vrf_table(
4536 afi_safis
[iter
->afi_safi_ix
].afi
,
4537 afi_safis
[iter
->afi_safi_ix
].safi
,
4542 iter
->afi_safi_ix
++;
4546 * Found another table in this vrf.
4552 * Done with all tables in the current vrf, go to the
4556 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4559 iter
->afi_safi_ix
= 0;
4564 case RIB_TABLES_ITER_S_DONE
:
4569 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4571 iter
->state
= RIB_TABLES_ITER_S_DONE
;