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 "zebra/zebra_router.h"
43 #include "zebra/connected.h"
44 #include "zebra/debug.h"
45 #include "zebra/interface.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/rib.h"
49 #include "zebra/zapi_msg.h"
50 #include "zebra/zebra_errors.h"
51 #include "zebra/zebra_memory.h"
52 #include "zebra/zebra_ns.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/zebra_routemap.h"
55 #include "zebra/zebra_vrf.h"
56 #include "zebra/zebra_vxlan.h"
57 #include "zebra/zapi_msg.h"
58 #include "zebra/zebra_dplane.h"
60 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
63 * Event, list, and mutex for delivery of dataplane results
65 static pthread_mutex_t dplane_mutex
;
66 static struct thread
*t_dplane
;
67 static struct dplane_ctx_q rib_dplane_q
;
69 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
72 /* Should we allow non Quagga processes to delete our routes */
73 extern int allow_delete
;
75 /* Each route type's string and default distance value. */
80 } route_info
[ZEBRA_ROUTE_MAX
] = {
81 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Uneeded for nhg's */, 0},
82 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 6},
83 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 2},
84 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 1},
85 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 3},
86 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 4},
87 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 4},
88 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 4},
89 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 4},
90 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 4},
91 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 5},
92 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 6},
93 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 4},
94 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 4},
95 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 6},
96 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 6},
97 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 3},
98 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 6},
99 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 5},
100 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 5},
101 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 5},
102 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 5},
103 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 5},
104 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 4},
105 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 6},
106 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 6},
107 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 6},
108 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 4},
109 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 6},
110 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, 6},
111 /* Any new route type added to zebra, should be mirrored here */
113 /* no entry/default: 150 */
116 static void PRINTFRR(5, 6)
117 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
118 int priority
, const char *msgfmt
, ...)
120 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
125 va_start(ap
, msgfmt
);
126 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
130 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
131 rib_dest_t
*dest
= NULL
;
132 struct route_entry
*re
= NULL
;
134 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
136 if (info
->safi
== SAFI_MULTICAST
)
137 strlcat(buf
, " (MRIB)", sizeof(buf
));
139 dest
= rib_dest_from_rnode(rn
);
141 re
= re_list_first(&dest
->routes
);
145 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
148 zlog(priority
, "%s: (%u:%u):%s: %s", _func
, vrf_id
, table
, buf
, msgbuf
);
151 #define rnode_debug(node, vrf_id, ...) \
152 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
153 #define rnode_info(node, ...) \
154 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
156 uint8_t route_distance(int type
)
160 if ((unsigned)type
>= array_size(route_info
))
163 distance
= route_info
[type
].distance
;
168 int is_zebra_valid_kernel_table(uint32_t table_id
)
171 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
172 || (table_id
== RT_TABLE_COMPAT
))
179 int is_zebra_main_routing_table(uint32_t table_id
)
181 if (table_id
== RT_TABLE_MAIN
)
186 int zebra_check_addr(const struct prefix
*p
)
188 if (p
->family
== AF_INET
) {
191 addr
= p
->u
.prefix4
.s_addr
;
194 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
195 || IPV4_LINKLOCAL(addr
))
198 if (p
->family
== AF_INET6
) {
199 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
201 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
208 * copy_nexthop - copy a nexthop to the rib structure.
210 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
212 assert(!re
->nhe
->nhg
.nexthop
);
213 copy_nexthops(&re
->nhe
->nhg
.nexthop
, nh
, NULL
);
216 static void route_entry_attach_ref(struct route_entry
*re
,
217 struct nhg_hash_entry
*new)
220 re
->nhe_id
= new->id
;
222 zebra_nhg_increment_ref(new);
225 int route_entry_update_nhe(struct route_entry
*re
,
226 struct nhg_hash_entry
*new_nhghe
)
228 struct nhg_hash_entry
*old
;
231 if (new_nhghe
== NULL
) {
233 zebra_nhg_decrement_ref(re
->nhe
);
238 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
241 route_entry_attach_ref(re
, new_nhghe
);
244 zebra_nhg_decrement_ref(old
);
246 /* This is the first time it's being attached */
247 route_entry_attach_ref(re
, new_nhghe
);
253 void rib_handle_nhg_replace(struct nhg_hash_entry
*old
,
254 struct nhg_hash_entry
*new)
256 struct zebra_router_table
*zrt
;
257 struct route_node
*rn
;
258 struct route_entry
*re
, *next
;
260 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
261 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
262 __func__
, new->id
, new, old
);
264 /* We have to do them ALL */
265 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
266 for (rn
= route_top(zrt
->table
); rn
;
267 rn
= srcdest_route_next(rn
)) {
268 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
269 if (re
->nhe
&& re
->nhe
== old
)
270 route_entry_update_nhe(re
, new);
276 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
277 union g_addr
*addr
, struct route_node
**rn_out
)
280 struct route_table
*table
;
281 struct route_node
*rn
;
282 struct route_entry
*match
= NULL
;
285 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
289 memset(&p
, 0, sizeof(struct prefix
));
292 p
.u
.prefix4
= addr
->ipv4
;
293 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
295 p
.u
.prefix6
= addr
->ipv6
;
296 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
299 rn
= route_node_match(table
, &p
);
304 route_unlock_node(rn
);
306 dest
= rib_dest_from_rnode(rn
);
307 if (dest
&& dest
->selected_fib
308 && !CHECK_FLAG(dest
->selected_fib
->status
,
309 ROUTE_ENTRY_REMOVED
))
310 match
= dest
->selected_fib
;
312 /* If there is no selected route or matched route is EGP, go up
317 } while (rn
&& rn
->info
== NULL
);
321 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
322 if (!CHECK_FLAG(match
->status
,
323 ROUTE_ENTRY_INSTALLED
))
335 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
337 struct route_node
**rn_out
)
339 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
340 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
341 union g_addr gaddr
= {.ipv4
= addr
};
343 switch (zrouter
.ipv4_multicast_mode
) {
344 case MCAST_MRIB_ONLY
:
345 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
347 case MCAST_URIB_ONLY
:
348 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
349 case MCAST_NO_CONFIG
:
350 case MCAST_MIX_MRIB_FIRST
:
351 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
354 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
357 case MCAST_MIX_DISTANCE
:
358 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
359 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
361 re
= ure
->distance
< mre
->distance
? ure
: mre
;
367 case MCAST_MIX_PFXLEN
:
368 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
369 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
371 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
380 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
382 if (IS_ZEBRA_DEBUG_RIB
) {
384 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
386 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
387 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
388 mre
? (ure
? "MRIB+URIB" : "MRIB")
389 : ure
? "URIB" : "nothing",
390 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
395 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
397 struct route_table
*table
;
398 struct route_node
*rn
;
399 struct route_entry
*match
= NULL
;
403 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
407 rn
= route_node_lookup(table
, (struct prefix
*)p
);
409 /* No route for this prefix. */
414 route_unlock_node(rn
);
415 dest
= rib_dest_from_rnode(rn
);
417 if (dest
&& dest
->selected_fib
418 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
419 match
= dest
->selected_fib
;
424 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
427 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
434 * Is this RIB labeled-unicast? It must be of type BGP and all paths
435 * (nexthops) must have a label.
437 int zebra_rib_labeled_unicast(struct route_entry
*re
)
439 struct nexthop
*nexthop
= NULL
;
441 if (re
->type
!= ZEBRA_ROUTE_BGP
)
444 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
445 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
451 /* Update flag indicates whether this is a "replace" or not. Currently, this
452 * is only used for IPv4.
454 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
455 struct route_entry
*old
)
457 struct nexthop
*nexthop
;
458 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
459 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
460 const struct prefix
*p
, *src_p
;
461 enum zebra_dplane_result ret
;
463 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
465 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
467 if (info
->safi
!= SAFI_UNICAST
) {
468 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
469 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
475 * Install the resolved nexthop object first.
477 zebra_nhg_install_kernel(re
->nhe
);
480 * If this is a replace to a new RE let the originator of the RE
481 * know that they've lost
483 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
484 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
486 /* Update fib selection */
487 dest
->selected_fib
= re
;
490 * Make sure we update the FPM any time we send new information to
493 hook_call(rib_update
, rn
, "installing in kernel");
495 /* Send add or update */
497 ret
= dplane_route_update(rn
, re
, old
);
499 ret
= dplane_route_add(rn
, re
);
502 case ZEBRA_DPLANE_REQUEST_QUEUED
:
503 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
506 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
508 /* Free old FIB nexthop group */
509 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
510 if (old
->fib_ng
.nexthop
) {
511 nexthops_free(old
->fib_ng
.nexthop
);
512 old
->fib_ng
.nexthop
= NULL
;
517 zvrf
->installs_queued
++;
519 case ZEBRA_DPLANE_REQUEST_FAILURE
:
521 char str
[SRCDEST2STR_BUFFER
];
523 srcdest_rnode2str(rn
, str
, sizeof(str
));
524 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
525 "%u:%u:%s: Failed to enqueue dataplane install",
526 re
->vrf_id
, re
->table
, str
);
529 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
538 /* Uninstall the route from kernel. */
539 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
541 struct nexthop
*nexthop
;
542 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
543 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
545 if (info
->safi
!= SAFI_UNICAST
) {
546 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
547 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
548 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
553 * Make sure we update the FPM any time we send new information to
556 hook_call(rib_update
, rn
, "uninstalling from kernel");
558 switch (dplane_route_delete(rn
, re
)) {
559 case ZEBRA_DPLANE_REQUEST_QUEUED
:
561 zvrf
->removals_queued
++;
563 case ZEBRA_DPLANE_REQUEST_FAILURE
:
565 char str
[SRCDEST2STR_BUFFER
];
567 srcdest_rnode2str(rn
, str
, sizeof(str
));
568 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
569 "%u:%s: Failed to enqueue dataplane uninstall",
573 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
582 /* Uninstall the route from kernel. */
583 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
585 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
586 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
587 struct nexthop
*nexthop
;
589 if (dest
&& dest
->selected_fib
== re
) {
590 if (info
->safi
== SAFI_UNICAST
)
591 hook_call(rib_update
, rn
, "rib_uninstall");
593 /* If labeled-unicast route, uninstall transit LSP. */
594 if (zebra_rib_labeled_unicast(re
))
595 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
597 rib_uninstall_kernel(rn
, re
);
599 dest
->selected_fib
= NULL
;
601 /* Free FIB nexthop group, if present */
602 if (re
->fib_ng
.nexthop
) {
603 nexthops_free(re
->fib_ng
.nexthop
);
604 re
->fib_ng
.nexthop
= NULL
;
606 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
608 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
609 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
612 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
613 const struct prefix
*p
, *src_p
;
615 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
617 redistribute_delete(p
, src_p
, re
, NULL
);
618 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
623 * rib_can_delete_dest
625 * Returns true if the given dest can be deleted from the table.
627 static int rib_can_delete_dest(rib_dest_t
*dest
)
629 if (re_list_first(&dest
->routes
)) {
634 * Unresolved rnh's are stored on the default route's list
636 * dest->rnode can also be the source prefix node in an
637 * ipv6 sourcedest table. Fortunately the prefix of a
638 * source prefix node can never be the default prefix.
640 if (is_default_prefix(&dest
->rnode
->p
))
644 * Don't delete the dest if we have to update the FPM about this
647 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
648 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
654 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
656 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
660 * We are storing the rnh's associated withb
661 * the tracked nexthop as a list of the rn's.
662 * Unresolved rnh's are placed at the top
663 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
664 * As such for each rn we need to walk up the tree
665 * and see if any rnh's need to see if they
666 * would match a more specific route
669 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
670 char buf
[PREFIX_STRLEN
];
673 "%s: %s Being examined for Nexthop Tracking Count: %zd",
675 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
676 dest
? rnh_list_count(&dest
->nht
) : 0);
681 dest
= rib_dest_from_rnode(rn
);
685 * If we have any rnh's stored in the nht list
686 * then we know that this route node was used for
687 * nht resolution and as such we need to call the
688 * nexthop tracking evaluation code
690 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
691 struct zebra_vrf
*zvrf
=
692 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
693 struct prefix
*p
= &rnh
->node
->p
;
695 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
696 char buf1
[PREFIX_STRLEN
];
699 "%s(%u):%s has Nexthop(%pFX) Type: %s depending on it, evaluating %u:%u",
700 zvrf_name(zvrf
), zvrf_id(zvrf
),
701 srcdest_rnode2str(rn
, buf1
,
703 p
, rnh_type2str(rnh
->type
), seq
,
708 * If we have evaluated this node on this pass
709 * already, due to following the tree up
710 * then we know that we can move onto the next
713 * Additionally we call zebra_evaluate_rnh
714 * when we gc the dest. In this case we know
715 * that there must be no other re's where
716 * we were originally as such we know that
717 * that sequence number is ok to respect.
719 if (rnh
->seqno
== seq
) {
720 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
722 "\tNode processed and moved already");
727 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
733 dest
= rib_dest_from_rnode(rn
);
740 * Garbage collect the rib dest corresponding to the given route node
743 * Returns true if the dest was deleted, false otherwise.
745 int rib_gc_dest(struct route_node
*rn
)
749 dest
= rib_dest_from_rnode(rn
);
753 if (!rib_can_delete_dest(dest
))
756 if (IS_ZEBRA_DEBUG_RIB
) {
757 struct zebra_vrf
*zvrf
;
759 zvrf
= rib_dest_vrf(dest
);
760 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
763 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
766 rnh_list_fini(&dest
->nht
);
767 XFREE(MTYPE_RIB_DEST
, dest
);
771 * Release the one reference that we keep on the route node.
773 route_unlock_node(rn
);
777 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
778 struct route_entry
*new)
780 hook_call(rib_update
, rn
, "new route selected");
782 /* Update real nexthop. This may actually determine if nexthop is active
784 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
785 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
789 if (IS_ZEBRA_DEBUG_RIB
) {
790 char buf
[SRCDEST2STR_BUFFER
];
791 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
792 zlog_debug("%s(%u:%u):%s: Adding route rn %p, re %p (%s)",
793 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, buf
, rn
,
794 new, zebra_route_string(new->type
));
797 /* If labeled-unicast route, install transit LSP. */
798 if (zebra_rib_labeled_unicast(new))
799 zebra_mpls_lsp_install(zvrf
, rn
, new);
801 rib_install_kernel(rn
, new, NULL
);
803 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
806 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
807 struct route_entry
*old
)
809 hook_call(rib_update
, rn
, "removing existing route");
811 /* Uninstall from kernel. */
812 if (IS_ZEBRA_DEBUG_RIB
) {
813 char buf
[SRCDEST2STR_BUFFER
];
814 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
815 zlog_debug("%s(%u:%u):%s: Deleting route rn %p, re %p (%s)",
816 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, buf
, rn
,
817 old
, zebra_route_string(old
->type
));
820 /* If labeled-unicast route, uninstall transit LSP. */
821 if (zebra_rib_labeled_unicast(old
))
822 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
824 rib_uninstall_kernel(rn
, old
);
826 /* Update nexthop for route, reset changed flag. */
827 /* Note: this code also handles the Linux case when an interface goes
828 * down, causing the kernel to delete routes without sending DELROUTE
831 if (RIB_KERNEL_ROUTE(old
))
832 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
834 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
837 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
838 struct route_node
*rn
,
839 struct route_entry
*old
,
840 struct route_entry
*new)
845 * We have to install or update if a new route has been selected or
846 * something has changed.
848 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
849 hook_call(rib_update
, rn
, "updating existing route");
851 /* Update the nexthop; we could determine here that nexthop is
853 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
856 /* If nexthop is active, install the selected route, if
858 * the install succeeds, cleanup flags for prior route, if
863 if (IS_ZEBRA_DEBUG_RIB
) {
864 char buf
[SRCDEST2STR_BUFFER
];
865 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
868 "%s(%u:%u):%s: Updating route rn %p, re %p (%s) old %p (%s)",
869 zvrf_name(zvrf
), zvrf_id(zvrf
),
870 new->table
, buf
, rn
, new,
871 zebra_route_string(new->type
),
873 zebra_route_string(old
->type
));
876 "%s(%u:%u):%s: Updating route rn %p, re %p (%s)",
877 zvrf_name(zvrf
), zvrf_id(zvrf
),
878 new->table
, buf
, rn
, new,
879 zebra_route_string(new->type
));
882 /* If labeled-unicast route, uninstall transit LSP. */
883 if (zebra_rib_labeled_unicast(old
))
884 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
887 * Non-system route should be installed.
888 * If labeled-unicast route, install transit
891 if (zebra_rib_labeled_unicast(new))
892 zebra_mpls_lsp_install(zvrf
, rn
, new);
894 rib_install_kernel(rn
, new, old
);
898 * If nexthop for selected route is not active or install
900 * may need to uninstall and delete for redistribution.
903 if (IS_ZEBRA_DEBUG_RIB
) {
904 char buf
[SRCDEST2STR_BUFFER
];
905 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
908 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
909 zvrf_name(zvrf
), zvrf_id(zvrf
),
910 new->table
, buf
, rn
, new,
911 zebra_route_string(new->type
),
913 zebra_route_string(old
->type
));
916 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
917 zvrf_name(zvrf
), zvrf_id(zvrf
),
918 new->table
, buf
, rn
, new,
919 zebra_route_string(new->type
));
923 * When we have gotten to this point
924 * the new route entry has no nexthops
925 * that are usable and as such we need
926 * to remove the old route, but only
927 * if we were the one who installed
930 if (!RIB_SYSTEM_ROUTE(old
)) {
931 /* If labeled-unicast route, uninstall transit
933 if (zebra_rib_labeled_unicast(old
))
934 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
936 rib_uninstall_kernel(rn
, old
);
941 * Same route selected; check if in the FIB and if not,
942 * re-install. This is housekeeping code to deal with
943 * race conditions in kernel with linux netlink reporting
944 * interface up before IPv4 or IPv6 protocol is ready
947 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
948 RIB_SYSTEM_ROUTE(new))
949 rib_install_kernel(rn
, new, NULL
);
952 /* Update prior route. */
954 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
956 /* Clear changed flag. */
957 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
960 /* Check if 'alternate' RIB entry is better than 'current'. */
961 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
962 struct route_entry
*alternate
)
967 /* filter route selection in following order:
968 * - connected beats other types
969 * - if both connected, loopback or vrf wins
970 * - lower distance beats higher
971 * - lower metric beats higher for equal distance
972 * - last, hence oldest, route wins tie break.
975 /* Connected routes. Check to see if either are a vrf
976 * or loopback interface. If not, pick the last connected
977 * route of the set of lowest metric connected routes.
979 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
980 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
983 /* both are connected. are either loop or vrf? */
984 struct nexthop
*nexthop
= NULL
;
986 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
987 struct interface
*ifp
= if_lookup_by_index(
988 nexthop
->ifindex
, alternate
->vrf_id
);
990 if (ifp
&& if_is_loopback_or_vrf(ifp
))
994 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
995 struct interface
*ifp
= if_lookup_by_index(
996 nexthop
->ifindex
, current
->vrf_id
);
998 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1002 /* Neither are loop or vrf so pick best metric */
1003 if (alternate
->metric
<= current
->metric
)
1009 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1012 /* higher distance loses */
1013 if (alternate
->distance
< current
->distance
)
1015 if (current
->distance
< alternate
->distance
)
1018 /* metric tie-breaks equal distance */
1019 if (alternate
->metric
<= current
->metric
)
1025 /* Core function for processing nexthop group contexts's off metaq */
1026 static void rib_nhg_process(struct nhg_ctx
*ctx
)
1028 nhg_ctx_process(ctx
);
1031 /* Core function for processing routing information base. */
1032 static void rib_process(struct route_node
*rn
)
1034 struct route_entry
*re
;
1035 struct route_entry
*next
;
1036 struct route_entry
*old_selected
= NULL
;
1037 struct route_entry
*new_selected
= NULL
;
1038 struct route_entry
*old_fib
= NULL
;
1039 struct route_entry
*new_fib
= NULL
;
1040 struct route_entry
*best
= NULL
;
1041 char buf
[SRCDEST2STR_BUFFER
];
1043 struct zebra_vrf
*zvrf
= NULL
;
1045 const struct prefix
*p
, *src_p
;
1047 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1048 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1052 dest
= rib_dest_from_rnode(rn
);
1054 zvrf
= rib_dest_vrf(dest
);
1055 vrf_id
= zvrf_id(zvrf
);
1058 vrf
= vrf_lookup_by_id(vrf_id
);
1060 if (IS_ZEBRA_DEBUG_RIB
)
1061 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1064 * we can have rn's that have a NULL info pointer
1065 * (dest). As such let's not let the deref happen
1066 * additionally we know RNODE_FOREACH_RE_SAFE
1067 * will not iterate so we are ok.
1070 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1071 struct route_entry
*re
= re_list_first(&dest
->routes
);
1073 zlog_debug("%s(%u:%u):%s: Processing rn %p",
1074 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
,
1078 old_fib
= dest
->selected_fib
;
1081 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1082 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1084 "%s(%u:%u):%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1085 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
, re
,
1086 zebra_route_string(re
->type
), re
->status
,
1087 re
->flags
, re
->distance
, re
->metric
);
1089 /* Currently selected re. */
1090 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1091 assert(old_selected
== NULL
);
1095 /* Skip deleted entries from selection */
1096 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1100 * If the route entry has changed, verify/resolve
1101 * the nexthops associated with the entry.
1103 * In any event if we have nexthops that are not active
1104 * then we cannot use this particular route entry so
1107 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1108 if (!nexthop_active_update(rn
, re
)) {
1109 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1110 /* XXX: HERE BE DRAGONS!!!!!
1111 * In all honesty, I have not yet
1112 * figured out what this part does or
1113 * why the ROUTE_ENTRY_CHANGED test
1114 * above is correct or why we need to
1115 * delete a route here, and also not
1116 * whether this concerns both selected
1117 * and fib route, or only selected
1120 * This entry was denied by the 'ip
1122 * table' route-map, we need to delete
1124 if (re
!= old_selected
) {
1125 if (IS_ZEBRA_DEBUG_RIB
)
1127 "%s: %s(%u):%s: imported via import-table but denied by the ip protocol table route-map",
1134 SET_FLAG(re
->status
,
1135 ROUTE_ENTRY_REMOVED
);
1142 * If the re has not changed and the nhg we have is
1143 * not usable, then we cannot use this route entry
1144 * for consideration, as that the route will just
1145 * not install if it is selected.
1147 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1151 /* Infinite distance. */
1152 if (re
->distance
== DISTANCE_INFINITY
&&
1153 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1154 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1158 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1159 best
= rib_choose_best(new_fib
, re
);
1160 if (new_fib
&& best
!= new_fib
)
1161 UNSET_FLAG(new_fib
->status
,
1162 ROUTE_ENTRY_CHANGED
);
1165 best
= rib_choose_best(new_selected
, re
);
1166 if (new_selected
&& best
!= new_selected
)
1167 UNSET_FLAG(new_selected
->status
,
1168 ROUTE_ENTRY_CHANGED
);
1169 new_selected
= best
;
1172 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1173 } /* RNODE_FOREACH_RE */
1175 /* If no FIB override route, use the selected route also for FIB */
1176 if (new_fib
== NULL
)
1177 new_fib
= new_selected
;
1179 /* After the cycle is finished, the following pointers will be set:
1180 * old_selected --- RE entry currently having SELECTED
1181 * new_selected --- RE entry that is newly SELECTED
1182 * old_fib --- RE entry currently in kernel FIB
1183 * new_fib --- RE entry that is newly to be in kernel FIB
1185 * new_selected will get SELECTED flag, and is going to be redistributed
1186 * the zclients. new_fib (which can be new_selected) will be installed
1190 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1191 struct route_entry
*entry
;
1193 entry
= old_selected
1198 : new_fib
? new_fib
: NULL
;
1201 "%s(%u:%u):%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1202 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, buf
,
1203 (void *)old_selected
, (void *)new_selected
,
1204 (void *)old_fib
, (void *)new_fib
);
1207 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1208 * fib == selected */
1209 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1210 ROUTE_ENTRY_CHANGED
);
1212 /* Update fib according to selection results */
1213 if (new_fib
&& old_fib
)
1214 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1216 rib_process_add_fib(zvrf
, rn
, new_fib
);
1218 rib_process_del_fib(zvrf
, rn
, old_fib
);
1220 /* Update SELECTED entry */
1221 if (old_selected
!= new_selected
|| selected_changed
) {
1223 if (new_selected
&& new_selected
!= new_fib
)
1224 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1227 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1231 * If we're removing the old entry, we should tell
1232 * redist subscribers about that *if* they aren't
1233 * going to see a redist for the new entry.
1235 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1236 ROUTE_ENTRY_REMOVED
))
1237 redistribute_delete(p
, src_p
,
1241 if (old_selected
!= new_selected
)
1242 UNSET_FLAG(old_selected
->flags
,
1243 ZEBRA_FLAG_SELECTED
);
1247 /* Remove all RE entries queued for removal */
1248 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1249 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1250 if (IS_ZEBRA_DEBUG_RIB
) {
1251 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1252 (void *)rn
, (void *)re
);
1259 * Check if the dest can be deleted now.
1264 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1266 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1267 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1272 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1273 if (IS_ZEBRA_DEBUG_MPLS
)
1275 "%s(%u): Scheduling all LSPs upon RIB completion",
1276 zvrf_name(zvrf
), zvrf_id(zvrf
));
1277 zebra_mpls_lsp_schedule(zvrf
);
1278 mpls_unmark_lsps_for_processing(rn
);
1283 * Utility to match route with dplane context data
1285 static bool rib_route_match_ctx(const struct route_entry
*re
,
1286 const struct zebra_dplane_ctx
*ctx
,
1289 bool result
= false;
1293 * In 'update' case, we test info about the 'previous' or
1296 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1297 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1300 /* We use an extra test for statics, and another for
1303 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1304 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1305 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1307 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1309 dplane_ctx_get_old_metric(ctx
)) {
1316 * Ordinary, single-route case using primary context info
1318 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1319 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1320 /* Skip route that's been deleted */
1324 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1325 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1328 /* We use an extra test for statics, and another for
1331 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1332 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1333 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1335 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1336 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1338 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1339 result
= nexthop_group_equal_no_recurse(
1340 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1350 static void zebra_rib_fixup_system(struct route_node
*rn
)
1352 struct route_entry
*re
;
1354 RNODE_FOREACH_RE(rn
, re
) {
1355 struct nexthop
*nhop
;
1357 if (!RIB_SYSTEM_ROUTE(re
))
1360 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1363 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1364 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1366 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1367 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1370 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1375 /* Route comparison logic, with various special cases. */
1376 static bool rib_compare_routes(const struct route_entry
*re1
,
1377 const struct route_entry
*re2
)
1379 if (re1
->type
!= re2
->type
)
1382 if (re1
->instance
!= re2
->instance
)
1385 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1388 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1389 re1
->distance
!= re2
->distance
)
1392 /* We support multiple connected routes: this supports multiple
1393 * v6 link-locals, and we also support multiple addresses in the same
1394 * subnet on a single interface.
1396 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1403 * Compare nexthop lists from a route and a dplane context; test whether
1404 * the list installed in the FIB matches the route's list.
1405 * Set 'changed_p' to 'true' if there were changes to the route's
1406 * installed nexthops.
1408 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1411 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1412 const struct nexthop_group
*ctx_nhg
,
1415 bool matched_p
= true;
1416 struct nexthop
*nexthop
, *ctx_nexthop
;
1418 /* Get the first `installed` one to check against.
1419 * If the dataplane doesn't set these to be what was actually installed,
1420 * it will just be whatever was in re->nhe->nhg?
1422 ctx_nexthop
= ctx_nhg
->nexthop
;
1424 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1425 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1426 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1428 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1430 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1433 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1436 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1437 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1438 /* If the FIB doesn't know about the nexthop,
1439 * it's not installed
1441 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1442 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1443 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1445 (CHECK_FLAG(nexthop
->flags
,
1451 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1454 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1456 /* Keep checking nexthops */
1460 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1461 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1462 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1463 zlog_debug("%s: rib nh %pNHv -> installed",
1469 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1471 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1472 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1473 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1479 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1482 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1489 * Update a route from a dplane context. This consolidates common code
1490 * that can be used in processing of results from FIB updates, and in
1491 * async notification processing.
1492 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1494 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1495 struct route_node
*rn
,
1496 struct zebra_dplane_ctx
*ctx
)
1498 struct nexthop
*nexthop
;
1500 const struct nexthop_group
*ctxnhg
;
1501 struct nexthop_group
*re_nhg
;
1502 bool is_selected
= false; /* Is 're' currently the selected re? */
1503 bool changed_p
= false; /* Change to nexthops? */
1507 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1509 dest
= rib_dest_from_rnode(rn
);
1511 is_selected
= (re
== dest
->selected_fib
);
1513 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1514 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1515 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1516 (is_selected
? "" : "NOT "), re
);
1518 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1519 * If the installed set differs from the set requested by the rib/owner,
1520 * we use the fib-specific nexthop-group to record the actual FIB
1524 ctxnhg
= dplane_ctx_get_ng(ctx
);
1526 /* Check route's fib group and incoming notif group for equivalence.
1528 * Let's assume the nexthops are ordered here to save time.
1530 /* TODO -- this isn't testing or comparing the FIB flags; we should
1531 * do a more explicit loop, checking the incoming notification's flags.
1533 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1534 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1537 /* If the new FIB set matches the existing FIB set, we're done. */
1539 if (IS_ZEBRA_DEBUG_RIB
)
1541 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1542 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1545 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1547 * Free stale fib list and move on to check the rib nhg.
1549 if (IS_ZEBRA_DEBUG_RIB
)
1551 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1552 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1553 nexthops_free(re
->fib_ng
.nexthop
);
1554 re
->fib_ng
.nexthop
= NULL
;
1556 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1558 /* Note that the installed nexthops have changed */
1561 if (IS_ZEBRA_DEBUG_RIB
)
1563 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1564 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1568 * Compare with the rib nexthop group. The comparison here is different:
1569 * the RIB group may be a superset of the list installed in the FIB. We
1570 * walk the RIB group, looking for the 'installable' candidate
1571 * nexthops, and then check those against the set
1572 * that is actually installed.
1574 * Assume nexthops are ordered here as well.
1577 /* If nothing is installed, we can skip some of the checking/comparison
1580 if (ctxnhg
->nexthop
== NULL
) {
1585 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1587 /* If all nexthops were processed, we're done */
1589 if (IS_ZEBRA_DEBUG_RIB
)
1591 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1592 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1593 (changed_p
? "true" : "false"));
1599 /* FIB nexthop set differs from the RIB set:
1600 * create a fib-specific nexthop-group
1602 if (IS_ZEBRA_DEBUG_RIB
)
1604 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1605 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1606 (changed_p
? "true" : "false"),
1607 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1609 /* Set the flag about the dedicated fib list */
1610 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1611 if (ctxnhg
->nexthop
)
1612 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1617 * Check the status of the route's backup nexthops, if any.
1618 * The logic for backups is somewhat different: if any backup is
1619 * installed, a new fib nhg will be attached to the route.
1621 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1623 goto done
; /* No backup nexthops */
1625 /* First check the route's 'fib' list of backups, if it's present
1626 * from some previous event.
1628 re_nhg
= &re
->fib_backup_ng
;
1629 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1632 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1635 /* If the new FIB set matches an existing FIB set, we're done. */
1637 if (IS_ZEBRA_DEBUG_RIB
)
1639 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1640 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1643 } else if (re
->fib_backup_ng
.nexthop
) {
1645 * Free stale fib backup list and move on to check
1646 * the route's backups.
1648 if (IS_ZEBRA_DEBUG_RIB
)
1650 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1651 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1652 nexthops_free(re
->fib_backup_ng
.nexthop
);
1653 re
->fib_backup_ng
.nexthop
= NULL
;
1655 /* Note that the installed nexthops have changed */
1658 if (IS_ZEBRA_DEBUG_RIB
)
1660 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1661 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1665 * If a FIB backup nexthop set exists, attach a copy
1666 * to the route if any backup is installed
1668 if (ctxnhg
&& ctxnhg
->nexthop
) {
1670 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1671 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1675 /* If no installed backups, we're done */
1676 if (nexthop
== NULL
)
1679 if (IS_ZEBRA_DEBUG_RIB
)
1681 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1682 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1683 (changed_p
? "true" : "false"));
1685 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1695 * Helper to locate a zebra route-node from a dplane context. This is used
1696 * when processing dplane results, e.g. Note well: the route-node is returned
1697 * with a ref held - route_unlock_node() must be called eventually.
1699 static struct route_node
*
1700 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1702 struct route_table
*table
= NULL
;
1703 struct route_node
*rn
= NULL
;
1704 const struct prefix
*dest_pfx
, *src_pfx
;
1706 /* Locate rn and re(s) from ctx */
1708 table
= zebra_vrf_lookup_table_with_table_id(
1709 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1710 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1711 if (table
== NULL
) {
1712 if (IS_ZEBRA_DEBUG_DPLANE
) {
1714 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1715 dplane_ctx_get_afi(ctx
),
1716 dplane_ctx_get_safi(ctx
),
1717 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1718 dplane_ctx_get_vrf(ctx
));
1723 dest_pfx
= dplane_ctx_get_dest(ctx
);
1724 src_pfx
= dplane_ctx_get_src(ctx
);
1726 rn
= srcdest_rnode_get(table
, dest_pfx
,
1727 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1736 * Route-update results processing after async dataplane update.
1738 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1740 struct zebra_vrf
*zvrf
= NULL
;
1742 struct route_node
*rn
= NULL
;
1743 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1744 bool is_update
= false;
1745 enum dplane_op_e op
;
1746 enum zebra_dplane_result status
;
1747 const struct prefix
*dest_pfx
, *src_pfx
;
1750 bool fib_changed
= false;
1752 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1753 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1754 dest_pfx
= dplane_ctx_get_dest(ctx
);
1756 /* Locate rn and re(s) from ctx */
1757 rn
= rib_find_rn_from_ctx(ctx
);
1759 if (IS_ZEBRA_DEBUG_DPLANE
) {
1761 "Failed to process dplane results: no route for %s(%u):%pFX",
1762 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1768 dest
= rib_dest_from_rnode(rn
);
1769 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1771 op
= dplane_ctx_get_op(ctx
);
1772 status
= dplane_ctx_get_status(ctx
);
1774 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1776 "%s(%u:%u):%pFX Processing dplane result ctx %p, op %s result %s",
1777 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1778 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
,
1779 dplane_op2str(op
), dplane_res2str(status
));
1782 * Update is a bit of a special case, where we may have both old and new
1783 * routes to post-process.
1785 is_update
= dplane_ctx_is_update(ctx
);
1788 * Take a pass through the routes, look for matches with the context
1791 RNODE_FOREACH_RE(rn
, rib
) {
1794 if (rib_route_match_ctx(rib
, ctx
, false))
1798 /* Check for old route match */
1799 if (is_update
&& (old_re
== NULL
)) {
1800 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1804 /* Have we found the routes we need to work on? */
1805 if (re
&& ((!is_update
|| old_re
)))
1809 seq
= dplane_ctx_get_seq(ctx
);
1812 * Check sequence number(s) to detect stale results before continuing
1815 if (re
->dplane_sequence
!= seq
) {
1816 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1818 "%s(%u):%pFX Stale dplane result for re %p",
1820 dplane_ctx_get_vrf(ctx
), dest_pfx
, re
);
1822 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1826 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1827 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1829 "%s(%u:%u):%pFX Stale dplane result for old_re %p",
1831 dplane_ctx_get_vrf(ctx
), old_re
->table
,
1834 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1838 case DPLANE_OP_ROUTE_INSTALL
:
1839 case DPLANE_OP_ROUTE_UPDATE
:
1840 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1842 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1843 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1846 * On an update operation from the same route type
1847 * context retrieval currently has no way to know
1848 * which was the old and which was the new.
1849 * So don't unset our flags that we just set.
1850 * We know redistribution is ok because the
1851 * old_re in this case is used for nothing
1852 * more than knowing whom to contact if necessary.
1854 if (old_re
&& old_re
!= re
) {
1855 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1856 UNSET_FLAG(old_re
->status
,
1857 ROUTE_ENTRY_INSTALLED
);
1860 /* Update zebra route based on the results in
1861 * the context struct.
1865 rib_update_re_from_ctx(re
, rn
, ctx
);
1868 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1870 "%s(%u:%u):%pFX no fib change for re",
1872 dplane_ctx_get_vrf(ctx
),
1873 dplane_ctx_get_table(
1878 /* Redistribute if this is the selected re */
1879 if (dest
&& re
== dest
->selected_fib
)
1880 redistribute_update(dest_pfx
, src_pfx
,
1885 * System routes are weird in that they
1886 * allow multiple to be installed that match
1887 * to the same prefix, so after we get the
1888 * result we need to clean them up so that
1889 * we can actually use them.
1891 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1892 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1893 zebra_rib_fixup_system(rn
);
1898 /* Notify route owner */
1899 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1903 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1904 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1906 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1908 zsend_route_notify_owner(re
, dest_pfx
,
1909 ZAPI_ROUTE_FAIL_INSTALL
);
1911 zlog_warn("%s(%u:%u):%pFX: Route install failed",
1912 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1913 dplane_ctx_get_table(ctx
), dest_pfx
);
1916 case DPLANE_OP_ROUTE_DELETE
:
1918 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1920 * In the delete case, the zebra core datastructs were
1921 * updated (or removed) at the time the delete was issued,
1922 * so we're just notifying the route owner.
1924 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1926 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1927 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1929 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1935 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1936 zsend_route_notify_owner_ctx(ctx
,
1937 ZAPI_ROUTE_REMOVE_FAIL
);
1939 zlog_warn("%s(%u:%u):%pFX: Route Deletion failure",
1940 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1941 dplane_ctx_get_table(ctx
), dest_pfx
);
1945 * System routes are weird in that they
1946 * allow multiple to be installed that match
1947 * to the same prefix, so after we get the
1948 * result we need to clean them up so that
1949 * we can actually use them.
1951 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1952 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1953 zebra_rib_fixup_system(rn
);
1959 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1960 zebra_rib_evaluate_mpls(rn
);
1964 route_unlock_node(rn
);
1966 /* Return context to dataplane module */
1967 dplane_ctx_fini(&ctx
);
1971 * Count installed/FIB nexthops
1973 static int rib_count_installed_nh(struct route_entry
*re
)
1976 struct nexthop
*nexthop
;
1977 struct nexthop_group
*nhg
;
1979 nhg
= rib_get_fib_nhg(re
);
1981 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
1982 /* The meaningful flag depends on where the installed
1985 if (nhg
== &(re
->fib_ng
)) {
1986 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1989 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1994 nhg
= rib_get_fib_backup_nhg(re
);
1996 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
1997 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2006 * Handle notification from async dataplane: the dataplane has detected
2007 * some change to a route, and notifies zebra so that the control plane
2008 * can reflect that change.
2010 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2012 struct route_node
*rn
= NULL
;
2013 struct route_entry
*re
= NULL
;
2015 struct nexthop
*nexthop
;
2016 const struct prefix
*dest_pfx
, *src_pfx
;
2018 bool fib_changed
= false;
2019 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2020 int start_count
, end_count
;
2021 dest_pfx
= dplane_ctx_get_dest(ctx
);
2022 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2024 /* Locate rn and re(s) from ctx */
2025 rn
= rib_find_rn_from_ctx(ctx
);
2029 "Failed to process dplane notification: no routes for %s(%u:%u):%pFX",
2030 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2031 dplane_ctx_get_table(ctx
), dest_pfx
);
2036 dest
= rib_dest_from_rnode(rn
);
2037 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
2040 zlog_debug("%s(%u:%u):%pFX Processing dplane notif ctx %p",
2041 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2042 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
);
2045 * Take a pass through the routes, look for matches with the context
2048 RNODE_FOREACH_RE(rn
, re
) {
2049 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2053 /* No match? Nothing we can do */
2057 "%s(%u:%u):%pFX Unable to process dplane notification: no entry for type %s",
2058 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2059 dplane_ctx_get_table(ctx
), dest_pfx
,
2060 zebra_route_string(dplane_ctx_get_type(ctx
)));
2065 /* Ensure we clear the QUEUED flag */
2066 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2068 /* Is this a notification that ... matters? We mostly care about
2069 * the route that is currently selected for installation; we may also
2070 * get an un-install notification, and handle that too.
2072 if (re
!= dest
->selected_fib
) {
2074 * If we need to, clean up after a delete that was part of
2075 * an update operation.
2078 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2079 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2083 /* If no nexthops or none installed, ensure that this re
2084 * gets its 'installed' flag cleared.
2086 if (end_count
== 0) {
2087 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2088 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2091 "%s(%u:%u):%pFX dplane notif, uninstalled type %s route",
2093 dplane_ctx_get_vrf(ctx
),
2094 dplane_ctx_get_table(ctx
), dest_pfx
,
2096 dplane_ctx_get_type(ctx
)));
2098 /* At least report on the event. */
2101 "%s(%u:%u):%pFX dplane notif, but type %s not selected_fib",
2103 dplane_ctx_get_vrf(ctx
),
2104 dplane_ctx_get_table(ctx
), dest_pfx
,
2106 dplane_ctx_get_type(ctx
)));
2111 /* We'll want to determine whether the installation status of the
2112 * route has changed: we'll check the status before processing,
2113 * and then again if there's been a change.
2117 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2118 start_count
= rib_count_installed_nh(re
);
2120 /* Update zebra's nexthop FIB flags based on the context struct's
2123 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2128 "%s(%u:%u):%pFX dplane notification: rib_update returns FALSE",
2129 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2130 dplane_ctx_get_table(ctx
), dest_pfx
);
2134 * Perform follow-up work if the actual status of the prefix
2137 end_count
= rib_count_installed_nh(re
);
2139 /* Various fib transitions: changed nexthops; from installed to
2140 * not-installed; or not-installed to installed.
2142 if (start_count
> 0 && end_count
> 0) {
2145 "%s(%u:%u):%pFX applied nexthop changes from dplane notification",
2146 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2147 dplane_ctx_get_table(ctx
), dest_pfx
);
2149 /* Changed nexthops - update kernel/others */
2150 dplane_route_notif_update(rn
, re
,
2151 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2153 } else if (start_count
== 0 && end_count
> 0) {
2156 "%s(%u:%u):%pFX installed transition from dplane notification",
2157 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2158 dplane_ctx_get_table(ctx
), dest_pfx
);
2160 /* We expect this to be the selected route, so we want
2161 * to tell others about this transition.
2163 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2165 /* Changed nexthops - update kernel/others */
2166 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2168 /* Redistribute, lsp, and nht update */
2169 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2171 } else if (start_count
> 0 && end_count
== 0) {
2174 "%s(%u:%u):%pFX un-installed transition from dplane notification",
2175 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2176 dplane_ctx_get_table(ctx
), dest_pfx
);
2178 /* Transition from _something_ installed to _nothing_
2181 /* We expect this to be the selected route, so we want
2182 * to tell others about this transistion.
2184 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2186 /* Changed nexthops - update kernel/others */
2187 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2189 /* Redistribute, lsp, and nht update */
2190 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
2193 /* Make any changes visible for lsp and nexthop-tracking processing */
2194 zebra_rib_evaluate_rn_nexthops(
2195 rn
, zebra_router_get_next_sequence());
2197 zebra_rib_evaluate_mpls(rn
);
2201 route_unlock_node(rn
);
2203 /* Return context to dataplane module */
2204 dplane_ctx_fini(&ctx
);
2207 static void process_subq_nhg(struct listnode
*lnode
)
2209 struct nhg_ctx
*ctx
= NULL
;
2210 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2212 ctx
= listgetdata(lnode
);
2217 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2218 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
2221 rib_nhg_process(ctx
);
2224 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2226 struct route_node
*rnode
= NULL
;
2227 rib_dest_t
*dest
= NULL
;
2228 struct zebra_vrf
*zvrf
= NULL
;
2230 rnode
= listgetdata(lnode
);
2231 dest
= rib_dest_from_rnode(rnode
);
2234 zvrf
= rib_dest_vrf(dest
);
2238 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2239 struct route_entry
*re
= NULL
;
2240 char buf
[SRCDEST2STR_BUFFER
];
2243 * rib_process may have freed the dest
2244 * as part of the garbage collection. Let's
2245 * prevent stupidity from happening.
2247 dest
= rib_dest_from_rnode(rnode
);
2249 re
= re_list_first(&dest
->routes
);
2251 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2252 zlog_debug("%s(%u:%u):%s: rn %p dequeued from sub-queue %u",
2253 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0, buf
,
2258 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2259 RIB_ROUTE_QUEUED(qindex
));
2264 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2265 __func__
, rnode
, route_node_get_lock_count(rnode
));
2266 zlog_backtrace(LOG_DEBUG
);
2269 route_unlock_node(rnode
);
2272 /* Take a list of route_node structs and return 1, if there was a record
2273 * picked from it and processed by rib_process(). Don't process more,
2274 * than one RN record; operate only in the specified sub-queue.
2276 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2278 struct listnode
*lnode
= listhead(subq
);
2283 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2284 process_subq_nhg(lnode
);
2286 process_subq_route(lnode
, qindex
);
2288 list_delete_node(subq
, lnode
);
2293 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2294 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2296 * is pointed to the meta queue structure.
2298 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2300 struct meta_queue
*mq
= data
;
2302 uint32_t queue_len
, queue_limit
;
2304 /* Ensure there's room for more dataplane updates */
2305 queue_limit
= dplane_get_in_queue_limit();
2306 queue_len
= dplane_get_in_queue_len();
2307 if (queue_len
> queue_limit
) {
2308 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2309 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2310 queue_len
, queue_limit
);
2312 /* Ensure that the meta-queue is actually enqueued */
2313 if (work_queue_empty(zrouter
.ribq
))
2314 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2316 return WQ_QUEUE_BLOCKED
;
2319 for (i
= 0; i
< MQ_SIZE
; i
++)
2320 if (process_subq(mq
->subq
[i
], i
)) {
2324 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2329 * Look into the RN and queue it into the highest priority queue
2330 * at this point in time for processing.
2332 * We will enqueue a route node only once per invocation.
2334 * There are two possibilities here that should be kept in mind.
2335 * If the original invocation has not been pulled off for processing
2336 * yet, A subsuquent invocation can have a route entry with a better
2337 * meta queue index value and we can have a situation where
2338 * we might have the same node enqueued 2 times. Not necessarily
2339 * an optimal situation but it should be ok.
2341 * The other possibility is that the original invocation has not
2342 * been pulled off for processing yet, A subsusquent invocation
2343 * doesn't have a route_entry with a better meta-queue and the
2344 * original metaqueue index value will win and we'll end up with
2345 * the route node enqueued once.
2347 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2349 struct route_node
*rn
= NULL
;
2350 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2351 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2353 rn
= (struct route_node
*)data
;
2355 RNODE_FOREACH_RE (rn
, curr_re
) {
2356 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2358 if (curr_qindex
<= qindex
) {
2360 qindex
= curr_qindex
;
2367 /* Invariant: at this point we always have rn->info set. */
2368 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2369 RIB_ROUTE_QUEUED(qindex
))) {
2370 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2371 rnode_debug(rn
, re
->vrf_id
,
2372 "rn %p is already queued in sub-queue %u",
2373 (void *)rn
, qindex
);
2377 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2378 listnode_add(mq
->subq
[qindex
], rn
);
2379 route_lock_node(rn
);
2382 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2383 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2384 (void *)rn
, qindex
);
2389 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2391 struct nhg_ctx
*ctx
= NULL
;
2392 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2394 ctx
= (struct nhg_ctx
*)data
;
2399 listnode_add(mq
->subq
[qindex
], ctx
);
2402 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2403 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2409 static int mq_add_handler(void *data
,
2410 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2412 if (zrouter
.ribq
== NULL
) {
2413 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2414 "%s: work_queue does not exist!", __func__
);
2419 * The RIB queue should normally be either empty or holding the only
2420 * work_queue_item element. In the latter case this element would
2421 * hold a pointer to the meta queue structure, which must be used to
2422 * actually queue the route nodes to process. So create the MQ
2423 * holder, if necessary, then push the work into it in any case.
2424 * This semantics was introduced after 0.99.9 release.
2426 if (work_queue_empty(zrouter
.ribq
))
2427 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2429 return mq_add_func(zrouter
.mq
, data
);
2432 /* Add route_node to work queue and schedule processing */
2433 int rib_queue_add(struct route_node
*rn
)
2437 /* Pointless to queue a route_node with no RIB entries to add or remove
2439 if (!rnode_to_ribs(rn
)) {
2440 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
2441 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
2442 zlog_backtrace(LOG_DEBUG
);
2446 return mq_add_handler(rn
, &rib_meta_queue_add
);
2449 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2453 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2456 /* Create new meta queue.
2457 A destructor function doesn't seem to be necessary here.
2459 static struct meta_queue
*meta_queue_new(void)
2461 struct meta_queue
*new;
2464 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2466 for (i
= 0; i
< MQ_SIZE
; i
++) {
2467 new->subq
[i
] = list_new();
2468 assert(new->subq
[i
]);
2474 void meta_queue_free(struct meta_queue
*mq
)
2478 for (i
= 0; i
< MQ_SIZE
; i
++)
2479 list_delete(&mq
->subq
[i
]);
2481 XFREE(MTYPE_WORK_QUEUE
, mq
);
2484 /* initialise zebra rib work queue */
2485 static void rib_queue_init(void)
2487 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2488 "route_node processing"))) {
2489 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2490 "%s: could not initialise work queue!", __func__
);
2494 /* fill in the work queue spec */
2495 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2496 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2497 zrouter
.ribq
->spec
.completion_func
= NULL
;
2498 /* XXX: TODO: These should be runtime configurable via vty */
2499 zrouter
.ribq
->spec
.max_retries
= 3;
2500 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2501 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2503 if (!(zrouter
.mq
= meta_queue_new())) {
2504 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2505 "%s: could not initialise meta queue!", __func__
);
2511 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2515 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2516 rnh_list_init(&dest
->nht
);
2517 re_list_init(&dest
->routes
);
2518 route_lock_node(rn
); /* rn route table reference */
2525 /* RIB updates are processed via a queue of pointers to route_nodes.
2527 * The queue length is bounded by the maximal size of the routing table,
2528 * as a route_node will not be requeued, if already queued.
2530 * REs are submitted via rib_addnode or rib_delnode which set minimal
2531 * state, or static_install_route (when an existing RE is updated)
2532 * and then submit route_node to queue for best-path selection later.
2533 * Order of add/delete state changes are preserved for any given RE.
2535 * Deleted REs are reaped during best-path selection.
2538 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2539 * |-------->| | best RE, if required
2541 * static_install->|->rib_addqueue...... -> rib_process
2543 * |-------->| |-> rib_unlink
2544 * |-> set ROUTE_ENTRY_REMOVE |
2545 * rib_delnode (RE freed)
2547 * The 'info' pointer of a route_node points to a rib_dest_t
2548 * ('dest'). Queueing state for a route_node is kept on the dest. The
2549 * dest is created on-demand by rib_link() and is kept around at least
2550 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2552 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2554 * - route_nodes: refcounted by:
2555 * - dest attached to route_node:
2556 * - managed by: rib_link/rib_gc_dest
2557 * - route_node processing queue
2558 * - managed by: rib_addqueue, rib_process.
2562 /* Add RE to head of the route node. */
2563 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2567 const char *rmap_name
;
2571 dest
= rib_dest_from_rnode(rn
);
2573 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2574 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2576 dest
= zebra_rib_create_dest(rn
);
2579 re_list_add_head(&dest
->routes
, re
);
2581 afi
= (rn
->p
.family
== AF_INET
)
2583 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2584 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2585 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2587 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2588 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2593 static void rib_addnode(struct route_node
*rn
,
2594 struct route_entry
*re
, int process
)
2596 /* RE node has been un-removed before route-node is processed.
2597 * route_node must hence already be on the queue for processing..
2599 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2600 if (IS_ZEBRA_DEBUG_RIB
)
2601 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2602 (void *)rn
, (void *)re
);
2604 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2607 rib_link(rn
, re
, process
);
2613 * Detach a rib structure from a route_node.
2615 * Note that a call to rib_unlink() should be followed by a call to
2616 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2617 * longer required to be deleted.
2619 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2625 if (IS_ZEBRA_DEBUG_RIB
)
2626 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2629 dest
= rib_dest_from_rnode(rn
);
2631 re_list_del(&dest
->routes
, re
);
2633 if (dest
->selected_fib
== re
)
2634 dest
->selected_fib
= NULL
;
2636 if (re
->nhe
&& re
->nhe_id
) {
2637 assert(re
->nhe
->id
== re
->nhe_id
);
2638 zebra_nhg_decrement_ref(re
->nhe
);
2639 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2640 nexthops_free(re
->nhe
->nhg
.nexthop
);
2642 nexthops_free(re
->fib_ng
.nexthop
);
2644 XFREE(MTYPE_RE
, re
);
2647 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2651 if (IS_ZEBRA_DEBUG_RIB
)
2652 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2653 (void *)rn
, (void *)re
);
2654 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2656 afi
= (rn
->p
.family
== AF_INET
)
2658 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2659 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2660 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2662 zebra_del_import_table_entry(zvrf
, rn
, re
);
2663 /* Just clean up if non main table */
2664 if (IS_ZEBRA_DEBUG_RIB
) {
2665 char buf
[SRCDEST2STR_BUFFER
];
2666 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2667 zlog_debug("%s(%u):%s: Freeing route rn %p, re %p (%s)",
2668 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, buf
,
2669 rn
, re
, zebra_route_string(re
->type
));
2679 * Helper that debugs a single nexthop within a route-entry
2681 static void _route_entry_dump_nh(const struct route_entry
*re
,
2682 const char *straddr
,
2683 const struct nexthop
*nexthop
)
2685 char nhname
[PREFIX_STRLEN
];
2686 char backup_str
[50];
2690 struct interface
*ifp
;
2691 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2693 switch (nexthop
->type
) {
2694 case NEXTHOP_TYPE_BLACKHOLE
:
2695 snprintf(nhname
, sizeof(nhname
), "Blackhole");
2697 case NEXTHOP_TYPE_IFINDEX
:
2698 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2699 snprintf(nhname
, sizeof(nhname
), "%s",
2700 ifp
? ifp
->name
: "Unknown");
2702 case NEXTHOP_TYPE_IPV4
:
2704 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2705 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2707 case NEXTHOP_TYPE_IPV6
:
2708 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2709 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2713 backup_str
[0] = '\0';
2714 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
2715 snprintf(backup_str
, sizeof(backup_str
), "backup ");
2716 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
2717 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
2718 nexthop
->backup_idx
[i
]);
2719 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
2724 if (nexthop
->weight
)
2725 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
2727 zlog_debug("%s: %s %s[%u] vrf %s(%u) %s%s with flags %s%s%s%s%s",
2728 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2729 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2731 wgt_str
, backup_str
,
2732 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2735 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2738 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2741 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2744 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2750 /* This function dumps the contents of a given RE entry into
2751 * standard debug log. Calling function name and IP prefix in
2752 * question are passed as 1st and 2nd arguments.
2754 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2755 union prefixconstptr src_pp
,
2756 const struct route_entry
*re
)
2758 const struct prefix
*src_p
= src_pp
.p
;
2759 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2760 char straddr
[PREFIX_STRLEN
];
2761 char srcaddr
[PREFIX_STRLEN
];
2762 struct nexthop
*nexthop
;
2763 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
2764 struct nexthop_group
*nhg
;
2766 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
2767 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2768 is_srcdst
? " from " : "",
2769 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2771 VRF_LOGNAME(vrf
), re
->vrf_id
);
2772 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2773 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2775 zlog_debug("%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2776 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
,
2778 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2779 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
2780 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
2783 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
2784 _route_entry_dump_nh(re
, straddr
, nexthop
);
2786 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
2787 zlog_debug("%s: backup nexthops:", straddr
);
2789 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
2790 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
2791 _route_entry_dump_nh(re
, straddr
, nexthop
);
2794 zlog_debug("%s: dump complete", straddr
);
2798 * This is an exported helper to rtm_read() to dump the strange
2799 * RE entry found by rib_lookup_ipv4_route()
2801 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2803 struct route_table
*table
;
2804 struct route_node
*rn
;
2805 struct route_entry
*re
;
2808 vrf
= vrf_lookup_by_id(vrf_id
);
2811 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2813 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2814 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2815 VRF_LOGNAME(vrf
), vrf_id
);
2819 /* Scan the RIB table for exactly matching RE entry. */
2820 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2822 /* No route for this prefix. */
2824 zlog_debug("%s:%s(%u) lookup failed for %pFX", __func__
,
2825 VRF_LOGNAME(vrf
), vrf_id
, (struct prefix
*)p
);
2830 route_unlock_node(rn
);
2833 RNODE_FOREACH_RE (rn
, re
) {
2834 zlog_debug("%s:%s(%u) rn %p, re %p: %s, %s", __func__
,
2835 VRF_LOGNAME(vrf
), vrf_id
, (void *)rn
, (void *)re
,
2836 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2839 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2842 route_entry_dump(p
, NULL
, re
);
2846 /* Check if requested address assignment will fail due to another
2847 * route being installed by zebra in FIB already. Take necessary
2848 * actions, if needed: remove such a route from FIB and deSELECT
2849 * corresponding RE entry. Then put affected RN into RIBQ head.
2851 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2853 struct route_table
*table
;
2854 struct route_node
*rn
;
2857 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2858 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2860 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2861 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2862 VRF_LOGNAME(vrf
), vrf_id
);
2866 /* No matches would be the simplest case. */
2867 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2871 route_unlock_node(rn
);
2873 dest
= rib_dest_from_rnode(rn
);
2874 /* Check all RE entries. In case any changes have to be done, requeue
2875 * the RN into RIBQ head. If the routing message about the new connected
2876 * route (generated by the IP address we are going to assign very soon)
2877 * comes before the RIBQ is processed, the new RE entry will join
2878 * RIBQ record already on head. This is necessary for proper
2880 * of the rest of the RE.
2882 if (dest
->selected_fib
) {
2883 if (IS_ZEBRA_DEBUG_RIB
) {
2885 vrf_lookup_by_id(dest
->selected_fib
->vrf_id
);
2888 "%s(%u):%pFX: freeing way for connected prefix",
2889 VRF_LOGNAME(vrf
), dest
->selected_fib
->vrf_id
,
2891 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2893 rib_uninstall(rn
, dest
->selected_fib
);
2899 * Internal route-add implementation; there are a couple of different public
2900 * signatures. Callers in this path are responsible for the memory they
2901 * allocate: if they allocate a nexthop_group or backup nexthop info, they
2902 * must free those objects. If this returns < 0, an error has occurred and the
2903 * route_entry 're' has not been captured; the caller should free that also.
2905 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
2906 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
2907 struct nhg_hash_entry
*re_nhe
)
2909 struct nhg_hash_entry
*nhe
= NULL
;
2910 struct route_table
*table
;
2911 struct route_node
*rn
;
2912 struct route_entry
*same
= NULL
;
2918 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2921 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2926 if (re
->nhe_id
> 0) {
2927 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2931 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2932 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
2938 /* Lookup nhe from route information */
2939 nhe
= zebra_nhg_rib_find_nhe(re_nhe
, afi
);
2941 char buf2
[PREFIX_STRLEN
] = "";
2944 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2945 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2946 p
, src_p
? " from " : "",
2947 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
2955 * Attach the re to the nhe's nexthop group.
2957 * TODO: This will need to change when we start getting IDs from upper
2958 * level protocols, as the refcnt might be wrong, since it checks
2959 * if old_id != new_id.
2961 route_entry_update_nhe(re
, nhe
);
2963 /* Make it sure prefixlen is applied to the prefix. */
2966 apply_mask_ipv6(src_p
);
2968 /* Set default distance by route type. */
2969 if (re
->distance
== 0)
2970 re
->distance
= route_distance(re
->type
);
2972 /* Lookup route node.*/
2973 rn
= srcdest_rnode_get(table
, p
, src_p
);
2976 * If same type of route are installed, treat it as a implicit
2977 * withdraw. If the user has specified the No route replace semantics
2978 * for the install don't do a route replace.
2980 RNODE_FOREACH_RE (rn
, same
) {
2981 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2984 /* Compare various route_entry properties */
2985 if (rib_compare_routes(re
, same
))
2989 /* If this route is kernel/connected route, notify the dataplane. */
2990 if (RIB_SYSTEM_ROUTE(re
)) {
2991 /* Notify dataplane */
2992 dplane_sys_route_add(rn
, re
);
2995 /* Link new re to node.*/
2996 if (IS_ZEBRA_DEBUG_RIB
) {
2997 rnode_debug(rn
, re
->vrf_id
,
2998 "Inserting route rn %p, re %p (%s) existing %p",
2999 rn
, re
, zebra_route_string(re
->type
), same
);
3001 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3002 route_entry_dump(p
, src_p
, re
);
3005 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3006 rib_addnode(rn
, re
, 1);
3009 /* Free implicit route.*/
3011 rib_delnode(rn
, same
);
3013 route_unlock_node(rn
);
3018 * Add a single route.
3020 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
3021 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3022 struct nexthop_group
*ng
)
3025 struct nhg_hash_entry nhe
;
3030 /* We either need nexthop(s) or an existing nexthop id */
3031 if (ng
== NULL
&& re
->nhe_id
== 0)
3035 * Use a temporary nhe to convey info to the common/main api.
3037 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
3039 nhe
.nhg
.nexthop
= ng
->nexthop
;
3040 else if (re
->nhe_id
> 0)
3041 nhe
.id
= re
->nhe_id
;
3043 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, &nhe
);
3045 /* In this path, the callers expect memory to be freed. */
3046 nexthop_group_delete(&ng
);
3048 /* In error cases, free the route also */
3050 XFREE(MTYPE_RE
, re
);
3055 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3056 unsigned short instance
, int flags
, struct prefix
*p
,
3057 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3058 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
3059 uint8_t distance
, bool fromkernel
, bool connected_down
)
3061 struct route_table
*table
;
3062 struct route_node
*rn
;
3063 struct route_entry
*re
;
3064 struct route_entry
*fib
= NULL
;
3065 struct route_entry
*same
= NULL
;
3066 struct nexthop
*rtnh
;
3067 char buf2
[INET6_ADDRSTRLEN
];
3070 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3073 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
3081 apply_mask_ipv6(src_p
);
3083 /* Lookup route node. */
3084 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
3086 char src_buf
[PREFIX_STRLEN
];
3088 if (src_p
&& src_p
->prefixlen
)
3089 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
3093 if (IS_ZEBRA_DEBUG_RIB
) {
3094 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3096 zlog_debug("%s[%d]:%pFX%s%s doesn't exist in rib",
3097 vrf
->name
, table_id
, p
,
3098 (src_buf
[0] != '\0') ? " from " : "",
3104 dest
= rib_dest_from_rnode(rn
);
3105 fib
= dest
->selected_fib
;
3107 /* Lookup same type route. */
3108 RNODE_FOREACH_RE (rn
, re
) {
3109 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3112 if (re
->type
!= type
)
3114 if (re
->instance
!= instance
)
3116 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
3117 distance
!= re
->distance
)
3120 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
3122 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
3123 (rtnh
= re
->nhe
->nhg
.nexthop
)
3124 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
3125 if (rtnh
->ifindex
!= nh
->ifindex
)
3131 /* Make sure that the route found has the same gateway. */
3132 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
3141 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3143 * No guarantee all kernel send nh with labels
3146 if (nexthop_same_no_labels(rtnh
, nh
)) {
3155 /* If same type of route can't be found and this message is from
3159 * In the past(HA!) we could get here because
3160 * we were receiving a route delete from the
3161 * kernel and we're not marking the proto
3162 * as coming from it's appropriate originator.
3163 * Now that we are properly noticing the fact
3164 * that the kernel has deleted our route we
3165 * are not going to get called in this path
3166 * I am going to leave this here because
3167 * this might still work this way on non-linux
3168 * platforms as well as some weird state I have
3169 * not properly thought of yet.
3170 * If we can show that this code path is
3171 * dead then we can remove it.
3173 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
3174 if (IS_ZEBRA_DEBUG_RIB
) {
3175 rnode_debug(rn
, vrf_id
,
3176 "rn %p, re %p (%s) was deleted from kernel, adding",
3178 zebra_route_string(fib
->type
));
3181 || CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3182 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3184 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3186 UNSET_FLAG(rtnh
->flags
,
3190 * This is a non FRR route
3191 * as such we should mark
3194 dest
->selected_fib
= NULL
;
3196 /* This means someone else, other than Zebra,
3198 * a Zebra router from the kernel. We will add
3200 rib_install_kernel(rn
, fib
, NULL
);
3203 if (IS_ZEBRA_DEBUG_RIB
) {
3207 "via %s ifindex %d type %d doesn't exist in rib",
3208 inet_ntop(afi2family(afi
),
3215 "type %d doesn't exist in rib",
3218 route_unlock_node(rn
);
3224 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
3226 rib_install_kernel(rn
, same
, NULL
);
3227 route_unlock_node(rn
);
3232 /* Special handling for IPv4 or IPv6 routes sourced from
3233 * EVPN - the nexthop (and associated MAC) need to be
3234 * uninstalled if no more refs.
3236 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
3237 struct nexthop
*tmp_nh
;
3239 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3240 struct ipaddr vtep_ip
;
3242 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3243 if (afi
== AFI_IP
) {
3244 vtep_ip
.ipa_type
= IPADDR_V4
;
3245 memcpy(&(vtep_ip
.ipaddr_v4
),
3246 &(tmp_nh
->gate
.ipv4
),
3247 sizeof(struct in_addr
));
3249 vtep_ip
.ipa_type
= IPADDR_V6
;
3250 memcpy(&(vtep_ip
.ipaddr_v6
),
3251 &(tmp_nh
->gate
.ipv6
),
3252 sizeof(struct in6_addr
));
3254 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
3259 /* Notify dplane if system route changes */
3260 if (RIB_SYSTEM_ROUTE(re
))
3261 dplane_sys_route_del(rn
, same
);
3263 rib_delnode(rn
, same
);
3267 * This is to force an immediate re-eval of this particular
3268 * node via nexthop tracking. Why? Because there are scenarios
3269 * where the interface is flapping and the normal queuing methodology
3270 * will cause down/up events to very very rarely be combined into
3271 * a non-event from nexthop tracking perspective. Leading
3272 * to some fun timing situations with upper level routing protocol
3273 * trying to and failing to install routes during this blip. Especially
3274 * when zebra is under load.
3277 zebra_rib_evaluate_rn_nexthops(rn
,
3278 zebra_router_get_next_sequence());
3279 route_unlock_node(rn
);
3284 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3285 unsigned short instance
, int flags
, struct prefix
*p
,
3286 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3287 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3288 uint8_t distance
, route_tag_t tag
)
3290 struct route_entry
*re
= NULL
;
3291 struct nexthop
*nexthop
= NULL
;
3292 struct nexthop_group
*ng
= NULL
;
3294 /* Allocate new route_entry structure. */
3295 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3297 re
->instance
= instance
;
3298 re
->distance
= distance
;
3300 re
->metric
= metric
;
3302 re
->table
= table_id
;
3303 re
->vrf_id
= vrf_id
;
3304 re
->uptime
= monotime(NULL
);
3306 re
->nhe_id
= nhe_id
;
3308 /* If the owner of the route supplies a shared nexthop-group id,
3309 * we'll use that. Otherwise, pass the nexthop along directly.
3312 ng
= nexthop_group_new();
3315 nexthop
= nexthop_new();
3317 nexthop_group_add_sorted(ng
, nexthop
);
3320 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
3323 static const char *rib_update_event2str(enum rib_update_event event
)
3325 const char *ret
= "UNKNOWN";
3328 case RIB_UPDATE_KERNEL
:
3329 ret
= "RIB_UPDATE_KERNEL";
3331 case RIB_UPDATE_RMAP_CHANGE
:
3332 ret
= "RIB_UPDATE_RMAP_CHANGE";
3334 case RIB_UPDATE_OTHER
:
3335 ret
= "RIB_UPDATE_OTHER";
3337 case RIB_UPDATE_MAX
:
3345 /* Schedule route nodes to be processed if they match the type */
3346 static void rib_update_route_node(struct route_node
*rn
, int type
)
3348 struct route_entry
*re
, *next
;
3349 bool re_changed
= false;
3351 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3352 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3353 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3362 /* Schedule routes of a particular table (address-family) based on event. */
3363 void rib_update_table(struct route_table
*table
, enum rib_update_event event
)
3365 struct route_node
*rn
;
3367 if (IS_ZEBRA_DEBUG_EVENT
) {
3368 struct zebra_vrf
*zvrf
;
3372 ? ((struct rib_table_info
*)table
->info
)->zvrf
3374 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3376 zlog_debug("%s: %s VRF %s Table %u event %s", __func__
,
3377 table
->info
? afi2str(
3378 ((struct rib_table_info
*)table
->info
)->afi
)
3380 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
3381 rib_update_event2str(event
));
3384 /* Walk all routes and queue for processing, if appropriate for
3385 * the trigger event.
3387 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3389 * If we are looking at a route node and the node
3390 * has already been queued we don't
3391 * need to queue it up again
3394 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3395 RIB_ROUTE_ANY_QUEUED
))
3399 case RIB_UPDATE_KERNEL
:
3400 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3402 case RIB_UPDATE_RMAP_CHANGE
:
3403 case RIB_UPDATE_OTHER
:
3404 rib_update_route_node(rn
, ZEBRA_ROUTE_ALL
);
3412 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
)
3414 struct route_table
*table
;
3416 if (IS_ZEBRA_DEBUG_EVENT
)
3417 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3418 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3420 /* Process routes of interested address-families. */
3421 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3423 rib_update_table(table
, event
);
3425 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3427 rib_update_table(table
, event
);
3430 static void rib_update_handle_vrf_all(enum rib_update_event event
)
3432 struct zebra_router_table
*zrt
;
3434 if (IS_ZEBRA_DEBUG_EVENT
)
3435 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3436 rib_update_event2str(event
));
3438 /* Just iterate over all the route tables, rather than vrf lookups */
3439 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3440 rib_update_table(zrt
->table
, event
);
3443 struct rib_update_ctx
{
3444 enum rib_update_event event
;
3449 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3450 enum rib_update_event event
)
3452 struct rib_update_ctx
*ctx
;
3454 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3457 ctx
->vrf_id
= vrf_id
;
3462 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3464 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3467 static int rib_update_handler(struct thread
*thread
)
3469 struct rib_update_ctx
*ctx
;
3471 ctx
= THREAD_ARG(thread
);
3474 rib_update_handle_vrf_all(ctx
->event
);
3476 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
);
3478 rib_update_ctx_fini(&ctx
);
3484 * Thread list to ensure we don't schedule a ton of events
3485 * if interfaces are flapping for instance.
3487 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3489 /* Schedule a RIB update event for specific vrf */
3490 void rib_update_vrf(vrf_id_t vrf_id
, enum rib_update_event event
)
3492 struct rib_update_ctx
*ctx
;
3494 ctx
= rib_update_ctx_init(vrf_id
, event
);
3496 /* Don't worry about making sure multiple rib updates for specific vrf
3497 * are scheduled at once for now. If it becomes a problem, we can use a
3498 * lookup of some sort to keep track of running threads via t_vrf_id
3499 * like how we are doing it in t_rib_update_threads[].
3501 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0, NULL
);
3503 if (IS_ZEBRA_DEBUG_EVENT
)
3504 zlog_debug("%s: Scheduled VRF %s, event %s", __func__
,
3505 vrf_id_to_name(ctx
->vrf_id
),
3506 rib_update_event2str(event
));
3509 /* Schedule a RIB update event for all vrfs */
3510 void rib_update(enum rib_update_event event
)
3512 struct rib_update_ctx
*ctx
;
3514 ctx
= rib_update_ctx_init(0, event
);
3516 ctx
->vrf_all
= true;
3518 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3519 &t_rib_update_threads
[event
]))
3520 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3521 else if (IS_ZEBRA_DEBUG_EVENT
)
3522 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
3523 rib_update_event2str(event
));
3526 /* Delete self installed routes after zebra is relaunched. */
3527 void rib_sweep_table(struct route_table
*table
)
3529 struct route_node
*rn
;
3530 struct route_entry
*re
;
3531 struct route_entry
*next
;
3532 struct nexthop
*nexthop
;
3537 if (IS_ZEBRA_DEBUG_RIB
)
3538 zlog_debug("%s: starting", __func__
);
3540 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3541 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3543 if (IS_ZEBRA_DEBUG_RIB
)
3544 route_entry_dump(&rn
->p
, NULL
, re
);
3546 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3549 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3553 * If routes are older than startup_time then
3554 * we know we read them in from the kernel.
3555 * As such we can safely remove them.
3557 if (zrouter
.startup_time
< re
->uptime
)
3561 * So we are starting up and have received
3562 * routes from the kernel that we have installed
3563 * from a previous run of zebra but not cleaned
3564 * up ( say a kill -9 )
3565 * But since we haven't actually installed
3566 * them yet( we received them from the kernel )
3567 * we don't think they are active.
3568 * So let's pretend they are active to actually
3570 * In all honesty I'm not sure if we should
3571 * mark them as active when we receive them
3572 * This is startup only so probably ok.
3574 * If we ever decide to move rib_sweep_table
3575 * to a different spot (ie startup )
3576 * this decision needs to be revisited
3578 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3579 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3580 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3582 rib_uninstall_kernel(rn
, re
);
3583 rib_delnode(rn
, re
);
3587 if (IS_ZEBRA_DEBUG_RIB
)
3588 zlog_debug("%s: ends", __func__
);
3591 /* Sweep all RIB tables. */
3592 int rib_sweep_route(struct thread
*t
)
3595 struct zebra_vrf
*zvrf
;
3597 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3598 if ((zvrf
= vrf
->info
) == NULL
)
3601 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3602 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3605 zebra_router_sweep_route();
3606 zebra_router_sweep_nhgs();
3611 /* Remove specific by protocol routes from 'table'. */
3612 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3613 struct route_table
*table
)
3615 struct route_node
*rn
;
3616 struct route_entry
*re
;
3617 struct route_entry
*next
;
3618 unsigned long n
= 0;
3621 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3622 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3623 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3625 if (re
->type
== proto
3626 && re
->instance
== instance
) {
3627 rib_delnode(rn
, re
);
3634 /* Remove specific by protocol routes. */
3635 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3638 struct zebra_vrf
*zvrf
;
3639 struct other_route_table
*ort
;
3640 unsigned long cnt
= 0;
3642 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3647 cnt
+= rib_score_proto_table(proto
, instance
,
3648 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3649 + rib_score_proto_table(
3651 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3653 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3654 rib_score_proto_table(proto
, instance
, ort
->table
);
3660 /* Close RIB and clean up kernel routes. */
3661 void rib_close_table(struct route_table
*table
)
3663 struct route_node
*rn
;
3664 struct rib_table_info
*info
;
3670 info
= route_table_get_info(table
);
3672 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3673 dest
= rib_dest_from_rnode(rn
);
3675 if (dest
&& dest
->selected_fib
) {
3676 if (info
->safi
== SAFI_UNICAST
)
3677 hook_call(rib_update
, rn
, NULL
);
3679 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3680 dest
->selected_fib
= NULL
;
3686 * Handler for async dataplane results after a pseudowire installation
3688 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3690 struct zebra_pw
*pw
;
3691 struct zebra_vrf
*vrf
;
3693 /* The pseudowire code assumes success - we act on an error
3694 * result for installation attempts here.
3696 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3699 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3700 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3701 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3703 zebra_pw_install_failure(pw
,
3704 dplane_ctx_get_pw_status(ctx
));
3714 * Handle results from the dataplane system. Dequeue update context
3715 * structs, dispatch to appropriate internal handlers.
3717 static int rib_process_dplane_results(struct thread
*thread
)
3719 struct zebra_dplane_ctx
*ctx
;
3720 struct dplane_ctx_q ctxlist
;
3721 bool shut_p
= false;
3723 /* Dequeue a list of completed updates with one lock/unlock cycle */
3726 TAILQ_INIT(&ctxlist
);
3728 /* Take lock controlling queue of results */
3729 frr_with_mutex(&dplane_mutex
) {
3730 /* Dequeue list of context structs */
3731 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3734 /* Dequeue context block */
3735 ctx
= dplane_ctx_dequeue(&ctxlist
);
3737 /* If we've emptied the results queue, we're done */
3741 /* If zebra is shutting down, avoid processing results,
3742 * just drain the results queue.
3744 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3745 memory_order_relaxed
);
3748 dplane_ctx_fini(&ctx
);
3750 ctx
= dplane_ctx_dequeue(&ctxlist
);
3757 switch (dplane_ctx_get_op(ctx
)) {
3758 case DPLANE_OP_ROUTE_INSTALL
:
3759 case DPLANE_OP_ROUTE_UPDATE
:
3760 case DPLANE_OP_ROUTE_DELETE
:
3762 /* Bit of special case for route updates
3763 * that were generated by async notifications:
3764 * we don't want to continue processing these
3767 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3768 rib_process_result(ctx
);
3770 dplane_ctx_fini(&ctx
);
3774 case DPLANE_OP_ROUTE_NOTIFY
:
3775 rib_process_dplane_notify(ctx
);
3778 case DPLANE_OP_NH_INSTALL
:
3779 case DPLANE_OP_NH_UPDATE
:
3780 case DPLANE_OP_NH_DELETE
:
3781 zebra_nhg_dplane_result(ctx
);
3784 case DPLANE_OP_LSP_INSTALL
:
3785 case DPLANE_OP_LSP_UPDATE
:
3786 case DPLANE_OP_LSP_DELETE
:
3788 /* Bit of special case for LSP updates
3789 * that were generated by async notifications:
3790 * we don't want to continue processing these.
3792 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3793 zebra_mpls_lsp_dplane_result(ctx
);
3795 dplane_ctx_fini(&ctx
);
3799 case DPLANE_OP_LSP_NOTIFY
:
3800 zebra_mpls_process_dplane_notify(ctx
);
3803 case DPLANE_OP_PW_INSTALL
:
3804 case DPLANE_OP_PW_UNINSTALL
:
3805 handle_pw_result(ctx
);
3808 case DPLANE_OP_SYS_ROUTE_ADD
:
3809 case DPLANE_OP_SYS_ROUTE_DELETE
:
3810 /* No further processing in zebra for these. */
3811 dplane_ctx_fini(&ctx
);
3814 case DPLANE_OP_MAC_INSTALL
:
3815 case DPLANE_OP_MAC_DELETE
:
3816 zebra_vxlan_handle_result(ctx
);
3819 case DPLANE_OP_RULE_ADD
:
3820 case DPLANE_OP_RULE_DELETE
:
3821 case DPLANE_OP_RULE_UPDATE
:
3822 zebra_pbr_dplane_result(ctx
);
3825 /* Some op codes not handled here */
3826 case DPLANE_OP_ADDR_INSTALL
:
3827 case DPLANE_OP_ADDR_UNINSTALL
:
3828 case DPLANE_OP_NEIGH_INSTALL
:
3829 case DPLANE_OP_NEIGH_UPDATE
:
3830 case DPLANE_OP_NEIGH_DELETE
:
3831 case DPLANE_OP_VTEP_ADD
:
3832 case DPLANE_OP_VTEP_DELETE
:
3833 case DPLANE_OP_NEIGH_DISCOVER
:
3834 case DPLANE_OP_BR_PORT_UPDATE
:
3835 case DPLANE_OP_NONE
:
3836 /* Don't expect this: just return the struct? */
3837 dplane_ctx_fini(&ctx
);
3840 } /* Dispatch by op code */
3842 ctx
= dplane_ctx_dequeue(&ctxlist
);
3851 * Results are returned from the dataplane subsystem, in the context of
3852 * the dataplane pthread. We enqueue the results here for processing by
3853 * the main thread later.
3855 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3857 /* Take lock controlling queue of results */
3858 frr_with_mutex(&dplane_mutex
) {
3859 /* Enqueue context blocks */
3860 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3863 /* Ensure event is signalled to zebra main pthread */
3864 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3871 * Ensure there are no empty slots in the route_info array.
3872 * Every route type in zebra should be present there.
3874 static void check_route_info(void)
3876 int len
= array_size(route_info
);
3879 * ZEBRA_ROUTE_SYSTEM is special cased since
3880 * its key is 0 anyway.
3882 * ZEBRA_ROUTE_ALL is also ignored.
3884 for (int i
= 0; i
< len
; i
++) {
3885 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3887 assert(route_info
[i
].key
);
3888 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3892 /* Routing information base initialize. */
3899 /* Init dataplane, and register for results */
3900 pthread_mutex_init(&dplane_mutex
, NULL
);
3901 TAILQ_INIT(&rib_dplane_q
);
3902 zebra_dplane_init(rib_dplane_results
);
3908 * Get the first vrf id that is greater than the given vrf id if any.
3910 * Returns true if a vrf id was found, false otherwise.
3912 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3916 vrf
= vrf_lookup_by_id(vrf_id
);
3918 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3920 *next_id_p
= vrf
->vrf_id
;
3929 * rib_tables_iter_next
3931 * Returns the next table in the iteration.
3933 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3935 struct route_table
*table
;
3938 * Array that helps us go over all AFI/SAFI combinations via one
3941 static const struct {
3945 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3946 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3947 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3952 switch (iter
->state
) {
3954 case RIB_TABLES_ITER_S_INIT
:
3955 iter
->vrf_id
= VRF_DEFAULT
;
3956 iter
->afi_safi_ix
= -1;
3960 case RIB_TABLES_ITER_S_ITERATING
:
3961 iter
->afi_safi_ix
++;
3964 while (iter
->afi_safi_ix
3965 < (int)array_size(afi_safis
)) {
3966 table
= zebra_vrf_table(
3967 afi_safis
[iter
->afi_safi_ix
].afi
,
3968 afi_safis
[iter
->afi_safi_ix
].safi
,
3973 iter
->afi_safi_ix
++;
3977 * Found another table in this vrf.
3983 * Done with all tables in the current vrf, go to the
3987 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3990 iter
->afi_safi_ix
= 0;
3995 case RIB_TABLES_ITER_S_DONE
:
4000 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4002 iter
->state
= RIB_TABLES_ITER_S_DONE
;