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_entry
,
254 struct nhg_hash_entry
*new_entry
)
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_entry
->id
, new_entry
, old_entry
);
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_entry
)
270 route_entry_update_nhe(re
, new_entry
);
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
,
485 info
->afi
, info
->safi
);
487 /* Update fib selection */
488 dest
->selected_fib
= re
;
491 * Make sure we update the FPM any time we send new information to
494 hook_call(rib_update
, rn
, "installing in kernel");
496 /* Send add or update */
498 ret
= dplane_route_update(rn
, re
, old
);
500 ret
= dplane_route_add(rn
, re
);
503 case ZEBRA_DPLANE_REQUEST_QUEUED
:
504 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
507 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
509 /* Free old FIB nexthop group */
510 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
511 if (old
->fib_ng
.nexthop
) {
512 nexthops_free(old
->fib_ng
.nexthop
);
513 old
->fib_ng
.nexthop
= NULL
;
518 zvrf
->installs_queued
++;
520 case ZEBRA_DPLANE_REQUEST_FAILURE
:
522 char str
[SRCDEST2STR_BUFFER
];
524 srcdest_rnode2str(rn
, str
, sizeof(str
));
525 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
526 "%u:%u:%s: Failed to enqueue dataplane install",
527 re
->vrf_id
, re
->table
, str
);
530 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
539 /* Uninstall the route from kernel. */
540 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
542 struct nexthop
*nexthop
;
543 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
544 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
546 if (info
->safi
!= SAFI_UNICAST
) {
547 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
548 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
549 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
554 * Make sure we update the FPM any time we send new information to
557 hook_call(rib_update
, rn
, "uninstalling from kernel");
559 switch (dplane_route_delete(rn
, re
)) {
560 case ZEBRA_DPLANE_REQUEST_QUEUED
:
562 zvrf
->removals_queued
++;
564 case ZEBRA_DPLANE_REQUEST_FAILURE
:
566 char str
[SRCDEST2STR_BUFFER
];
568 srcdest_rnode2str(rn
, str
, sizeof(str
));
569 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
570 "%u:%s: Failed to enqueue dataplane uninstall",
574 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
583 /* Uninstall the route from kernel. */
584 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
586 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
587 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
588 struct nexthop
*nexthop
;
590 if (dest
&& dest
->selected_fib
== re
) {
591 if (info
->safi
== SAFI_UNICAST
)
592 hook_call(rib_update
, rn
, "rib_uninstall");
594 /* If labeled-unicast route, uninstall transit LSP. */
595 if (zebra_rib_labeled_unicast(re
))
596 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
598 rib_uninstall_kernel(rn
, re
);
600 dest
->selected_fib
= NULL
;
602 /* Free FIB nexthop group, if present */
603 if (re
->fib_ng
.nexthop
) {
604 nexthops_free(re
->fib_ng
.nexthop
);
605 re
->fib_ng
.nexthop
= NULL
;
607 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
609 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
610 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
613 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
614 const struct prefix
*p
, *src_p
;
616 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
618 redistribute_delete(p
, src_p
, re
, NULL
);
619 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
624 * rib_can_delete_dest
626 * Returns true if the given dest can be deleted from the table.
628 static int rib_can_delete_dest(rib_dest_t
*dest
)
630 if (re_list_first(&dest
->routes
)) {
635 * Unresolved rnh's are stored on the default route's list
637 * dest->rnode can also be the source prefix node in an
638 * ipv6 sourcedest table. Fortunately the prefix of a
639 * source prefix node can never be the default prefix.
641 if (is_default_prefix(&dest
->rnode
->p
))
645 * Don't delete the dest if we have to update the FPM about this
648 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
649 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
655 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
657 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
661 * We are storing the rnh's associated withb
662 * the tracked nexthop as a list of the rn's.
663 * Unresolved rnh's are placed at the top
664 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
665 * As such for each rn we need to walk up the tree
666 * and see if any rnh's need to see if they
667 * would match a more specific route
670 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
671 char buf
[PREFIX_STRLEN
];
674 "%s: %s Being examined for Nexthop Tracking Count: %zd",
676 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
677 dest
? rnh_list_count(&dest
->nht
) : 0);
682 dest
= rib_dest_from_rnode(rn
);
686 * If we have any rnh's stored in the nht list
687 * then we know that this route node was used for
688 * nht resolution and as such we need to call the
689 * nexthop tracking evaluation code
691 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
692 struct zebra_vrf
*zvrf
=
693 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
694 struct prefix
*p
= &rnh
->node
->p
;
696 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
697 char buf1
[PREFIX_STRLEN
];
700 "%s(%u):%s has Nexthop(%pFX) Type: %s depending on it, evaluating %u:%u",
701 zvrf_name(zvrf
), zvrf_id(zvrf
),
702 srcdest_rnode2str(rn
, buf1
,
704 p
, rnh_type2str(rnh
->type
), seq
,
709 * If we have evaluated this node on this pass
710 * already, due to following the tree up
711 * then we know that we can move onto the next
714 * Additionally we call zebra_evaluate_rnh
715 * when we gc the dest. In this case we know
716 * that there must be no other re's where
717 * we were originally as such we know that
718 * that sequence number is ok to respect.
720 if (rnh
->seqno
== seq
) {
721 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
723 "\tNode processed and moved already");
728 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
734 dest
= rib_dest_from_rnode(rn
);
741 * Garbage collect the rib dest corresponding to the given route node
744 * Returns true if the dest was deleted, false otherwise.
746 int rib_gc_dest(struct route_node
*rn
)
750 dest
= rib_dest_from_rnode(rn
);
754 if (!rib_can_delete_dest(dest
))
757 if (IS_ZEBRA_DEBUG_RIB
) {
758 struct zebra_vrf
*zvrf
;
760 zvrf
= rib_dest_vrf(dest
);
761 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
764 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
767 rnh_list_fini(&dest
->nht
);
768 XFREE(MTYPE_RIB_DEST
, dest
);
772 * Release the one reference that we keep on the route node.
774 route_unlock_node(rn
);
778 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
779 struct route_entry
*new)
781 hook_call(rib_update
, rn
, "new route selected");
783 /* Update real nexthop. This may actually determine if nexthop is active
785 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
786 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
790 if (IS_ZEBRA_DEBUG_RIB
) {
791 char buf
[SRCDEST2STR_BUFFER
];
792 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
793 zlog_debug("%s(%u:%u):%s: Adding route rn %p, re %p (%s)",
794 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, buf
, rn
,
795 new, zebra_route_string(new->type
));
798 /* If labeled-unicast route, install transit LSP. */
799 if (zebra_rib_labeled_unicast(new))
800 zebra_mpls_lsp_install(zvrf
, rn
, new);
802 rib_install_kernel(rn
, new, NULL
);
804 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
807 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
808 struct route_entry
*old
)
810 hook_call(rib_update
, rn
, "removing existing route");
812 /* Uninstall from kernel. */
813 if (IS_ZEBRA_DEBUG_RIB
) {
814 char buf
[SRCDEST2STR_BUFFER
];
815 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
816 zlog_debug("%s(%u:%u):%s: Deleting route rn %p, re %p (%s)",
817 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, buf
, rn
,
818 old
, zebra_route_string(old
->type
));
821 /* If labeled-unicast route, uninstall transit LSP. */
822 if (zebra_rib_labeled_unicast(old
))
823 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
825 rib_uninstall_kernel(rn
, old
);
827 /* Update nexthop for route, reset changed flag. */
828 /* Note: this code also handles the Linux case when an interface goes
829 * down, causing the kernel to delete routes without sending DELROUTE
832 if (RIB_KERNEL_ROUTE(old
))
833 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
835 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
838 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
839 struct route_node
*rn
,
840 struct route_entry
*old
,
841 struct route_entry
*new)
846 * We have to install or update if a new route has been selected or
847 * something has changed.
849 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
850 hook_call(rib_update
, rn
, "updating existing route");
852 /* Update the nexthop; we could determine here that nexthop is
854 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
857 /* If nexthop is active, install the selected route, if
859 * the install succeeds, cleanup flags for prior route, if
864 if (IS_ZEBRA_DEBUG_RIB
) {
865 char buf
[SRCDEST2STR_BUFFER
];
866 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
869 "%s(%u:%u):%s: Updating route rn %p, re %p (%s) old %p (%s)",
870 zvrf_name(zvrf
), zvrf_id(zvrf
),
871 new->table
, buf
, rn
, new,
872 zebra_route_string(new->type
),
874 zebra_route_string(old
->type
));
877 "%s(%u:%u):%s: Updating route rn %p, re %p (%s)",
878 zvrf_name(zvrf
), zvrf_id(zvrf
),
879 new->table
, buf
, rn
, new,
880 zebra_route_string(new->type
));
883 /* If labeled-unicast route, uninstall transit LSP. */
884 if (zebra_rib_labeled_unicast(old
))
885 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
888 * Non-system route should be installed.
889 * If labeled-unicast route, install transit
892 if (zebra_rib_labeled_unicast(new))
893 zebra_mpls_lsp_install(zvrf
, rn
, new);
895 rib_install_kernel(rn
, new, old
);
899 * If nexthop for selected route is not active or install
901 * may need to uninstall and delete for redistribution.
904 if (IS_ZEBRA_DEBUG_RIB
) {
905 char buf
[SRCDEST2STR_BUFFER
];
906 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
909 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
910 zvrf_name(zvrf
), zvrf_id(zvrf
),
911 new->table
, buf
, rn
, new,
912 zebra_route_string(new->type
),
914 zebra_route_string(old
->type
));
917 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
918 zvrf_name(zvrf
), zvrf_id(zvrf
),
919 new->table
, buf
, rn
, new,
920 zebra_route_string(new->type
));
924 * When we have gotten to this point
925 * the new route entry has no nexthops
926 * that are usable and as such we need
927 * to remove the old route, but only
928 * if we were the one who installed
931 if (!RIB_SYSTEM_ROUTE(old
)) {
932 /* If labeled-unicast route, uninstall transit
934 if (zebra_rib_labeled_unicast(old
))
935 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
937 rib_uninstall_kernel(rn
, old
);
942 * Same route selected; check if in the FIB and if not,
943 * re-install. This is housekeeping code to deal with
944 * race conditions in kernel with linux netlink reporting
945 * interface up before IPv4 or IPv6 protocol is ready
948 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
949 RIB_SYSTEM_ROUTE(new))
950 rib_install_kernel(rn
, new, NULL
);
953 /* Update prior route. */
955 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
957 /* Clear changed flag. */
958 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
961 /* Check if 'alternate' RIB entry is better than 'current'. */
962 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
963 struct route_entry
*alternate
)
968 /* filter route selection in following order:
969 * - connected beats other types
970 * - if both connected, loopback or vrf wins
971 * - lower distance beats higher
972 * - lower metric beats higher for equal distance
973 * - last, hence oldest, route wins tie break.
976 /* Connected routes. Check to see if either are a vrf
977 * or loopback interface. If not, pick the last connected
978 * route of the set of lowest metric connected routes.
980 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
981 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
984 /* both are connected. are either loop or vrf? */
985 struct nexthop
*nexthop
= NULL
;
987 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
988 struct interface
*ifp
= if_lookup_by_index(
989 nexthop
->ifindex
, alternate
->vrf_id
);
991 if (ifp
&& if_is_loopback_or_vrf(ifp
))
995 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
996 struct interface
*ifp
= if_lookup_by_index(
997 nexthop
->ifindex
, current
->vrf_id
);
999 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1003 /* Neither are loop or vrf so pick best metric */
1004 if (alternate
->metric
<= current
->metric
)
1010 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1013 /* higher distance loses */
1014 if (alternate
->distance
< current
->distance
)
1016 if (current
->distance
< alternate
->distance
)
1019 /* metric tie-breaks equal distance */
1020 if (alternate
->metric
<= current
->metric
)
1026 /* Core function for processing nexthop group contexts's off metaq */
1027 static void rib_nhg_process(struct nhg_ctx
*ctx
)
1029 nhg_ctx_process(ctx
);
1032 /* Core function for processing routing information base. */
1033 static void rib_process(struct route_node
*rn
)
1035 struct route_entry
*re
;
1036 struct route_entry
*next
;
1037 struct route_entry
*old_selected
= NULL
;
1038 struct route_entry
*new_selected
= NULL
;
1039 struct route_entry
*old_fib
= NULL
;
1040 struct route_entry
*new_fib
= NULL
;
1041 struct route_entry
*best
= NULL
;
1042 char buf
[SRCDEST2STR_BUFFER
];
1044 struct zebra_vrf
*zvrf
= NULL
;
1046 const struct prefix
*p
, *src_p
;
1048 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1049 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1053 dest
= rib_dest_from_rnode(rn
);
1055 zvrf
= rib_dest_vrf(dest
);
1056 vrf_id
= zvrf_id(zvrf
);
1059 vrf
= vrf_lookup_by_id(vrf_id
);
1061 if (IS_ZEBRA_DEBUG_RIB
)
1062 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1065 * we can have rn's that have a NULL info pointer
1066 * (dest). As such let's not let the deref happen
1067 * additionally we know RNODE_FOREACH_RE_SAFE
1068 * will not iterate so we are ok.
1071 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1072 struct route_entry
*re
= re_list_first(&dest
->routes
);
1074 zlog_debug("%s(%u:%u):%s: Processing rn %p",
1075 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
,
1079 old_fib
= dest
->selected_fib
;
1082 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1083 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1085 "%s(%u:%u):%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1086 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
, re
,
1087 zebra_route_string(re
->type
), re
->status
,
1088 re
->flags
, re
->distance
, re
->metric
);
1090 /* Currently selected re. */
1091 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1092 assert(old_selected
== NULL
);
1096 /* Skip deleted entries from selection */
1097 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1101 * If the route entry has changed, verify/resolve
1102 * the nexthops associated with the entry.
1104 * In any event if we have nexthops that are not active
1105 * then we cannot use this particular route entry so
1108 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1109 if (!nexthop_active_update(rn
, re
)) {
1110 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1111 /* XXX: HERE BE DRAGONS!!!!!
1112 * In all honesty, I have not yet
1113 * figured out what this part does or
1114 * why the ROUTE_ENTRY_CHANGED test
1115 * above is correct or why we need to
1116 * delete a route here, and also not
1117 * whether this concerns both selected
1118 * and fib route, or only selected
1121 * This entry was denied by the 'ip
1123 * table' route-map, we need to delete
1125 if (re
!= old_selected
) {
1126 if (IS_ZEBRA_DEBUG_RIB
)
1128 "%s: %s(%u):%s: imported via import-table but denied by the ip protocol table route-map",
1135 SET_FLAG(re
->status
,
1136 ROUTE_ENTRY_REMOVED
);
1143 * If the re has not changed and the nhg we have is
1144 * not usable, then we cannot use this route entry
1145 * for consideration, as that the route will just
1146 * not install if it is selected.
1148 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1152 /* Infinite distance. */
1153 if (re
->distance
== DISTANCE_INFINITY
&&
1154 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1155 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1159 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1160 best
= rib_choose_best(new_fib
, re
);
1161 if (new_fib
&& best
!= new_fib
)
1162 UNSET_FLAG(new_fib
->status
,
1163 ROUTE_ENTRY_CHANGED
);
1166 best
= rib_choose_best(new_selected
, re
);
1167 if (new_selected
&& best
!= new_selected
)
1168 UNSET_FLAG(new_selected
->status
,
1169 ROUTE_ENTRY_CHANGED
);
1170 new_selected
= best
;
1173 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1174 } /* RNODE_FOREACH_RE */
1176 /* If no FIB override route, use the selected route also for FIB */
1177 if (new_fib
== NULL
)
1178 new_fib
= new_selected
;
1180 /* After the cycle is finished, the following pointers will be set:
1181 * old_selected --- RE entry currently having SELECTED
1182 * new_selected --- RE entry that is newly SELECTED
1183 * old_fib --- RE entry currently in kernel FIB
1184 * new_fib --- RE entry that is newly to be in kernel FIB
1186 * new_selected will get SELECTED flag, and is going to be redistributed
1187 * the zclients. new_fib (which can be new_selected) will be installed
1191 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1192 struct route_entry
*entry
;
1194 entry
= old_selected
1199 : new_fib
? new_fib
: NULL
;
1202 "%s(%u:%u):%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1203 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, buf
,
1204 (void *)old_selected
, (void *)new_selected
,
1205 (void *)old_fib
, (void *)new_fib
);
1208 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1209 * fib == selected */
1210 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1211 ROUTE_ENTRY_CHANGED
);
1213 /* Update fib according to selection results */
1214 if (new_fib
&& old_fib
)
1215 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1217 rib_process_add_fib(zvrf
, rn
, new_fib
);
1219 rib_process_del_fib(zvrf
, rn
, old_fib
);
1221 /* Update SELECTED entry */
1222 if (old_selected
!= new_selected
|| selected_changed
) {
1224 if (new_selected
&& new_selected
!= new_fib
)
1225 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1228 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1232 * If we're removing the old entry, we should tell
1233 * redist subscribers about that *if* they aren't
1234 * going to see a redist for the new entry.
1236 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1237 ROUTE_ENTRY_REMOVED
))
1238 redistribute_delete(p
, src_p
,
1242 if (old_selected
!= new_selected
)
1243 UNSET_FLAG(old_selected
->flags
,
1244 ZEBRA_FLAG_SELECTED
);
1248 /* Remove all RE entries queued for removal */
1249 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1250 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1251 if (IS_ZEBRA_DEBUG_RIB
) {
1252 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1253 (void *)rn
, (void *)re
);
1260 * Check if the dest can be deleted now.
1265 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1267 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1268 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1273 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1274 if (IS_ZEBRA_DEBUG_MPLS
)
1276 "%s(%u): Scheduling all LSPs upon RIB completion",
1277 zvrf_name(zvrf
), zvrf_id(zvrf
));
1278 zebra_mpls_lsp_schedule(zvrf
);
1279 mpls_unmark_lsps_for_processing(rn
);
1284 * Utility to match route with dplane context data
1286 static bool rib_route_match_ctx(const struct route_entry
*re
,
1287 const struct zebra_dplane_ctx
*ctx
,
1290 bool result
= false;
1294 * In 'update' case, we test info about the 'previous' or
1297 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1298 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1301 /* We use an extra test for statics, and another for
1304 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1305 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1306 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1308 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1310 dplane_ctx_get_old_metric(ctx
)) {
1317 * Ordinary, single-route case using primary context info
1319 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1320 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1321 /* Skip route that's been deleted */
1325 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1326 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1329 /* We use an extra test for statics, and another for
1332 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1333 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1334 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1336 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1337 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1339 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1340 result
= nexthop_group_equal_no_recurse(
1341 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1351 static void zebra_rib_fixup_system(struct route_node
*rn
)
1353 struct route_entry
*re
;
1355 RNODE_FOREACH_RE(rn
, re
) {
1356 struct nexthop
*nhop
;
1358 if (!RIB_SYSTEM_ROUTE(re
))
1361 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1364 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1365 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1367 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1368 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1371 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1376 /* Route comparison logic, with various special cases. */
1377 static bool rib_compare_routes(const struct route_entry
*re1
,
1378 const struct route_entry
*re2
)
1380 if (re1
->type
!= re2
->type
)
1383 if (re1
->instance
!= re2
->instance
)
1386 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1389 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1390 re1
->distance
!= re2
->distance
)
1393 /* We support multiple connected routes: this supports multiple
1394 * v6 link-locals, and we also support multiple addresses in the same
1395 * subnet on a single interface.
1397 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1404 * Compare nexthop lists from a route and a dplane context; test whether
1405 * the list installed in the FIB matches the route's list.
1406 * Set 'changed_p' to 'true' if there were changes to the route's
1407 * installed nexthops.
1409 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1412 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1413 const struct nexthop_group
*ctx_nhg
,
1416 bool matched_p
= true;
1417 struct nexthop
*nexthop
, *ctx_nexthop
;
1419 /* Get the first `installed` one to check against.
1420 * If the dataplane doesn't set these to be what was actually installed,
1421 * it will just be whatever was in re->nhe->nhg?
1423 ctx_nexthop
= ctx_nhg
->nexthop
;
1425 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1426 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1427 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1429 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1431 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1434 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1437 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1438 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1439 /* If the FIB doesn't know about the nexthop,
1440 * it's not installed
1442 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1443 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1444 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1446 (CHECK_FLAG(nexthop
->flags
,
1452 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1455 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1457 /* Keep checking nexthops */
1461 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1462 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1463 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1464 zlog_debug("%s: rib nh %pNHv -> installed",
1470 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1472 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1473 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1474 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1480 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1483 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1490 * Update a route from a dplane context. This consolidates common code
1491 * that can be used in processing of results from FIB updates, and in
1492 * async notification processing.
1493 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1495 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1496 struct route_node
*rn
,
1497 struct zebra_dplane_ctx
*ctx
)
1499 struct nexthop
*nexthop
;
1501 const struct nexthop_group
*ctxnhg
;
1502 struct nexthop_group
*re_nhg
;
1503 bool is_selected
= false; /* Is 're' currently the selected re? */
1504 bool changed_p
= false; /* Change to nexthops? */
1508 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1510 dest
= rib_dest_from_rnode(rn
);
1512 is_selected
= (re
== dest
->selected_fib
);
1514 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1515 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1516 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1517 (is_selected
? "" : "NOT "), re
);
1519 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1520 * If the installed set differs from the set requested by the rib/owner,
1521 * we use the fib-specific nexthop-group to record the actual FIB
1525 ctxnhg
= dplane_ctx_get_ng(ctx
);
1527 /* Check route's fib group and incoming notif group for equivalence.
1529 * Let's assume the nexthops are ordered here to save time.
1531 /* TODO -- this isn't testing or comparing the FIB flags; we should
1532 * do a more explicit loop, checking the incoming notification's flags.
1534 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1535 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1538 /* If the new FIB set matches the existing FIB set, we're done. */
1540 if (IS_ZEBRA_DEBUG_RIB
)
1542 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1543 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1546 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1548 * Free stale fib list and move on to check the rib nhg.
1550 if (IS_ZEBRA_DEBUG_RIB
)
1552 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1553 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1554 nexthops_free(re
->fib_ng
.nexthop
);
1555 re
->fib_ng
.nexthop
= NULL
;
1557 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1559 /* Note that the installed nexthops have changed */
1562 if (IS_ZEBRA_DEBUG_RIB
)
1564 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1565 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1569 * Compare with the rib nexthop group. The comparison here is different:
1570 * the RIB group may be a superset of the list installed in the FIB. We
1571 * walk the RIB group, looking for the 'installable' candidate
1572 * nexthops, and then check those against the set
1573 * that is actually installed.
1575 * Assume nexthops are ordered here as well.
1578 /* If nothing is installed, we can skip some of the checking/comparison
1581 if (ctxnhg
->nexthop
== NULL
) {
1586 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1588 /* If all nexthops were processed, we're done */
1590 if (IS_ZEBRA_DEBUG_RIB
)
1592 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1593 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1594 (changed_p
? "true" : "false"));
1600 /* FIB nexthop set differs from the RIB set:
1601 * create a fib-specific nexthop-group
1603 if (IS_ZEBRA_DEBUG_RIB
)
1605 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1606 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1607 (changed_p
? "true" : "false"),
1608 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1610 /* Set the flag about the dedicated fib list */
1611 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1612 if (ctxnhg
->nexthop
)
1613 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1618 * Check the status of the route's backup nexthops, if any.
1619 * The logic for backups is somewhat different: if any backup is
1620 * installed, a new fib nhg will be attached to the route.
1622 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1624 goto done
; /* No backup nexthops */
1626 /* First check the route's 'fib' list of backups, if it's present
1627 * from some previous event.
1629 re_nhg
= &re
->fib_backup_ng
;
1630 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1633 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1636 /* If the new FIB set matches an existing FIB set, we're done. */
1638 if (IS_ZEBRA_DEBUG_RIB
)
1640 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1641 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1644 } else if (re
->fib_backup_ng
.nexthop
) {
1646 * Free stale fib backup list and move on to check
1647 * the route's backups.
1649 if (IS_ZEBRA_DEBUG_RIB
)
1651 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1652 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1653 nexthops_free(re
->fib_backup_ng
.nexthop
);
1654 re
->fib_backup_ng
.nexthop
= NULL
;
1656 /* Note that the installed nexthops have changed */
1659 if (IS_ZEBRA_DEBUG_RIB
)
1661 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1662 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1666 * If a FIB backup nexthop set exists, attach a copy
1667 * to the route if any backup is installed
1669 if (ctxnhg
&& ctxnhg
->nexthop
) {
1671 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1672 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1676 /* If no installed backups, we're done */
1677 if (nexthop
== NULL
)
1680 if (IS_ZEBRA_DEBUG_RIB
)
1682 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1683 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1684 (changed_p
? "true" : "false"));
1686 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1696 * Helper to locate a zebra route-node from a dplane context. This is used
1697 * when processing dplane results, e.g. Note well: the route-node is returned
1698 * with a ref held - route_unlock_node() must be called eventually.
1700 static struct route_node
*
1701 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1703 struct route_table
*table
= NULL
;
1704 struct route_node
*rn
= NULL
;
1705 const struct prefix
*dest_pfx
, *src_pfx
;
1707 /* Locate rn and re(s) from ctx */
1709 table
= zebra_vrf_lookup_table_with_table_id(
1710 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1711 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1712 if (table
== NULL
) {
1713 if (IS_ZEBRA_DEBUG_DPLANE
) {
1715 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1716 dplane_ctx_get_afi(ctx
),
1717 dplane_ctx_get_safi(ctx
),
1718 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1719 dplane_ctx_get_vrf(ctx
));
1724 dest_pfx
= dplane_ctx_get_dest(ctx
);
1725 src_pfx
= dplane_ctx_get_src(ctx
);
1727 rn
= srcdest_rnode_get(table
, dest_pfx
,
1728 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1737 * Route-update results processing after async dataplane update.
1739 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1741 struct zebra_vrf
*zvrf
= NULL
;
1743 struct route_node
*rn
= NULL
;
1744 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1745 bool is_update
= false;
1746 enum dplane_op_e op
;
1747 enum zebra_dplane_result status
;
1748 const struct prefix
*dest_pfx
, *src_pfx
;
1751 bool fib_changed
= false;
1752 struct rib_table_info
*info
;
1754 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1755 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1756 dest_pfx
= dplane_ctx_get_dest(ctx
);
1758 /* Locate rn and re(s) from ctx */
1759 rn
= rib_find_rn_from_ctx(ctx
);
1761 if (IS_ZEBRA_DEBUG_DPLANE
) {
1763 "Failed to process dplane results: no route for %s(%u):%pFX",
1764 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1770 dest
= rib_dest_from_rnode(rn
);
1771 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1772 info
= srcdest_rnode_table_info(rn
);
1774 op
= dplane_ctx_get_op(ctx
);
1775 status
= dplane_ctx_get_status(ctx
);
1777 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1779 "%s(%u:%u):%pFX Processing dplane result ctx %p, op %s result %s",
1780 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1781 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
,
1782 dplane_op2str(op
), dplane_res2str(status
));
1785 * Update is a bit of a special case, where we may have both old and new
1786 * routes to post-process.
1788 is_update
= dplane_ctx_is_update(ctx
);
1791 * Take a pass through the routes, look for matches with the context
1794 RNODE_FOREACH_RE(rn
, rib
) {
1797 if (rib_route_match_ctx(rib
, ctx
, false))
1801 /* Check for old route match */
1802 if (is_update
&& (old_re
== NULL
)) {
1803 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1807 /* Have we found the routes we need to work on? */
1808 if (re
&& ((!is_update
|| old_re
)))
1812 seq
= dplane_ctx_get_seq(ctx
);
1815 * Check sequence number(s) to detect stale results before continuing
1818 if (re
->dplane_sequence
!= seq
) {
1819 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1821 "%s(%u):%pFX Stale dplane result for re %p",
1823 dplane_ctx_get_vrf(ctx
), dest_pfx
, re
);
1825 if (!zrouter
.asic_offloaded
||
1826 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
1827 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)))
1828 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1833 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1834 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1836 "%s(%u:%u):%pFX Stale dplane result for old_re %p",
1838 dplane_ctx_get_vrf(ctx
), old_re
->table
,
1841 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1845 case DPLANE_OP_ROUTE_INSTALL
:
1846 case DPLANE_OP_ROUTE_UPDATE
:
1847 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1849 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1850 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1853 * On an update operation from the same route type
1854 * context retrieval currently has no way to know
1855 * which was the old and which was the new.
1856 * So don't unset our flags that we just set.
1857 * We know redistribution is ok because the
1858 * old_re in this case is used for nothing
1859 * more than knowing whom to contact if necessary.
1861 if (old_re
&& old_re
!= re
) {
1862 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1863 UNSET_FLAG(old_re
->status
,
1864 ROUTE_ENTRY_INSTALLED
);
1867 /* Update zebra route based on the results in
1868 * the context struct.
1872 rib_update_re_from_ctx(re
, rn
, ctx
);
1875 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1877 "%s(%u:%u):%pFX no fib change for re",
1879 dplane_ctx_get_vrf(ctx
),
1880 dplane_ctx_get_table(
1885 /* Redistribute if this is the selected re */
1886 if (dest
&& re
== dest
->selected_fib
)
1887 redistribute_update(dest_pfx
, src_pfx
,
1892 * System routes are weird in that they
1893 * allow multiple to be installed that match
1894 * to the same prefix, so after we get the
1895 * result we need to clean them up so that
1896 * we can actually use them.
1898 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1899 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1900 zebra_rib_fixup_system(rn
);
1905 /* Notify route owner */
1906 if (zebra_router_notify_on_ack())
1907 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1910 if (CHECK_FLAG(re
->flags
,
1911 ZEBRA_FLAG_OFFLOADED
))
1912 zsend_route_notify_owner_ctx(
1914 ZAPI_ROUTE_INSTALLED
);
1917 ZEBRA_FLAG_OFFLOAD_FAILED
))
1918 zsend_route_notify_owner_ctx(
1920 ZAPI_ROUTE_FAIL_INSTALL
);
1925 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1926 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1928 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1930 zsend_route_notify_owner(re
, dest_pfx
,
1931 ZAPI_ROUTE_FAIL_INSTALL
,
1932 info
->afi
, info
->safi
);
1934 zlog_warn("%s(%u:%u):%pFX: Route install failed",
1935 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1936 dplane_ctx_get_table(ctx
), dest_pfx
);
1939 case DPLANE_OP_ROUTE_DELETE
:
1941 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1943 * In the delete case, the zebra core datastructs were
1944 * updated (or removed) at the time the delete was issued,
1945 * so we're just notifying the route owner.
1947 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1949 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1950 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1952 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1958 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1959 zsend_route_notify_owner_ctx(ctx
,
1960 ZAPI_ROUTE_REMOVE_FAIL
);
1962 zlog_warn("%s(%u:%u):%pFX: Route Deletion failure",
1963 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1964 dplane_ctx_get_table(ctx
), dest_pfx
);
1968 * System routes are weird in that they
1969 * allow multiple to be installed that match
1970 * to the same prefix, so after we get the
1971 * result we need to clean them up so that
1972 * we can actually use them.
1974 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1975 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1976 zebra_rib_fixup_system(rn
);
1982 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1983 zebra_rib_evaluate_mpls(rn
);
1987 route_unlock_node(rn
);
1989 /* Return context to dataplane module */
1990 dplane_ctx_fini(&ctx
);
1994 * Count installed/FIB nexthops
1996 static int rib_count_installed_nh(struct route_entry
*re
)
1999 struct nexthop
*nexthop
;
2000 struct nexthop_group
*nhg
;
2002 nhg
= rib_get_fib_nhg(re
);
2004 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2005 /* The meaningful flag depends on where the installed
2008 if (nhg
== &(re
->fib_ng
)) {
2009 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2012 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2017 nhg
= rib_get_fib_backup_nhg(re
);
2019 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2020 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2029 * Handle notification from async dataplane: the dataplane has detected
2030 * some change to a route, and notifies zebra so that the control plane
2031 * can reflect that change.
2033 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2035 struct route_node
*rn
= NULL
;
2036 struct route_entry
*re
= NULL
;
2038 struct nexthop
*nexthop
;
2039 const struct prefix
*dest_pfx
, *src_pfx
;
2041 bool fib_changed
= false;
2042 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2043 int start_count
, end_count
;
2044 dest_pfx
= dplane_ctx_get_dest(ctx
);
2045 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2047 /* Locate rn and re(s) from ctx */
2048 rn
= rib_find_rn_from_ctx(ctx
);
2052 "Failed to process dplane notification: no routes for %s(%u:%u):%pFX",
2053 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2054 dplane_ctx_get_table(ctx
), dest_pfx
);
2059 dest
= rib_dest_from_rnode(rn
);
2060 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
2063 zlog_debug("%s(%u:%u):%pFX Processing dplane notif ctx %p",
2064 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2065 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
);
2068 * Take a pass through the routes, look for matches with the context
2071 RNODE_FOREACH_RE(rn
, re
) {
2072 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2076 /* No match? Nothing we can do */
2080 "%s(%u:%u):%pFX Unable to process dplane notification: no entry for type %s",
2081 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2082 dplane_ctx_get_table(ctx
), dest_pfx
,
2083 zebra_route_string(dplane_ctx_get_type(ctx
)));
2088 /* Ensure we clear the QUEUED flag */
2089 if (!zrouter
.asic_offloaded
)
2090 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2092 /* Is this a notification that ... matters? We mostly care about
2093 * the route that is currently selected for installation; we may also
2094 * get an un-install notification, and handle that too.
2096 if (re
!= dest
->selected_fib
) {
2098 * If we need to, clean up after a delete that was part of
2099 * an update operation.
2102 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2103 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2107 /* If no nexthops or none installed, ensure that this re
2108 * gets its 'installed' flag cleared.
2110 if (end_count
== 0) {
2111 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2112 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2115 "%s(%u:%u):%pFX dplane notif, uninstalled type %s route",
2117 dplane_ctx_get_vrf(ctx
),
2118 dplane_ctx_get_table(ctx
), dest_pfx
,
2120 dplane_ctx_get_type(ctx
)));
2122 /* At least report on the event. */
2125 "%s(%u:%u):%pFX dplane notif, but type %s not selected_fib",
2127 dplane_ctx_get_vrf(ctx
),
2128 dplane_ctx_get_table(ctx
), dest_pfx
,
2130 dplane_ctx_get_type(ctx
)));
2135 /* We'll want to determine whether the installation status of the
2136 * route has changed: we'll check the status before processing,
2137 * and then again if there's been a change.
2141 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2142 start_count
= rib_count_installed_nh(re
);
2144 /* Update zebra's nexthop FIB flags based on the context struct's
2147 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2152 "%s(%u:%u):%pFX dplane notification: rib_update returns FALSE",
2153 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2154 dplane_ctx_get_table(ctx
), dest_pfx
);
2158 * Perform follow-up work if the actual status of the prefix
2161 end_count
= rib_count_installed_nh(re
);
2163 /* Various fib transitions: changed nexthops; from installed to
2164 * not-installed; or not-installed to installed.
2166 if (start_count
> 0 && end_count
> 0) {
2169 "%s(%u:%u):%pFX applied nexthop changes from dplane notification",
2170 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2171 dplane_ctx_get_table(ctx
), dest_pfx
);
2173 /* Changed nexthops - update kernel/others */
2174 dplane_route_notif_update(rn
, re
,
2175 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2177 } else if (start_count
== 0 && end_count
> 0) {
2180 "%s(%u:%u):%pFX installed transition from dplane notification",
2181 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2182 dplane_ctx_get_table(ctx
), dest_pfx
);
2184 /* We expect this to be the selected route, so we want
2185 * to tell others about this transition.
2187 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2189 /* Changed nexthops - update kernel/others */
2190 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2192 /* Redistribute, lsp, and nht update */
2193 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2195 } else if (start_count
> 0 && end_count
== 0) {
2198 "%s(%u:%u):%pFX un-installed transition from dplane notification",
2199 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2200 dplane_ctx_get_table(ctx
), dest_pfx
);
2202 /* Transition from _something_ installed to _nothing_
2205 /* We expect this to be the selected route, so we want
2206 * to tell others about this transistion.
2208 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2210 /* Changed nexthops - update kernel/others */
2211 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2213 /* Redistribute, lsp, and nht update */
2214 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
2217 /* Make any changes visible for lsp and nexthop-tracking processing */
2218 zebra_rib_evaluate_rn_nexthops(
2219 rn
, zebra_router_get_next_sequence());
2221 zebra_rib_evaluate_mpls(rn
);
2225 route_unlock_node(rn
);
2227 /* Return context to dataplane module */
2228 dplane_ctx_fini(&ctx
);
2231 static void process_subq_nhg(struct listnode
*lnode
)
2233 struct nhg_ctx
*ctx
= NULL
;
2234 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2236 ctx
= listgetdata(lnode
);
2241 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2242 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
2245 rib_nhg_process(ctx
);
2248 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2250 struct route_node
*rnode
= NULL
;
2251 rib_dest_t
*dest
= NULL
;
2252 struct zebra_vrf
*zvrf
= NULL
;
2254 rnode
= listgetdata(lnode
);
2255 dest
= rib_dest_from_rnode(rnode
);
2258 zvrf
= rib_dest_vrf(dest
);
2262 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2263 struct route_entry
*re
= NULL
;
2264 char buf
[SRCDEST2STR_BUFFER
];
2267 * rib_process may have freed the dest
2268 * as part of the garbage collection. Let's
2269 * prevent stupidity from happening.
2271 dest
= rib_dest_from_rnode(rnode
);
2273 re
= re_list_first(&dest
->routes
);
2275 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2276 zlog_debug("%s(%u:%u):%s: rn %p dequeued from sub-queue %u",
2277 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0, buf
,
2282 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2283 RIB_ROUTE_QUEUED(qindex
));
2288 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2289 __func__
, rnode
, route_node_get_lock_count(rnode
));
2290 zlog_backtrace(LOG_DEBUG
);
2293 route_unlock_node(rnode
);
2296 /* Take a list of route_node structs and return 1, if there was a record
2297 * picked from it and processed by rib_process(). Don't process more,
2298 * than one RN record; operate only in the specified sub-queue.
2300 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2302 struct listnode
*lnode
= listhead(subq
);
2307 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2308 process_subq_nhg(lnode
);
2310 process_subq_route(lnode
, qindex
);
2312 list_delete_node(subq
, lnode
);
2317 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2318 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2320 * is pointed to the meta queue structure.
2322 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2324 struct meta_queue
*mq
= data
;
2326 uint32_t queue_len
, queue_limit
;
2328 /* Ensure there's room for more dataplane updates */
2329 queue_limit
= dplane_get_in_queue_limit();
2330 queue_len
= dplane_get_in_queue_len();
2331 if (queue_len
> queue_limit
) {
2332 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2333 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2334 queue_len
, queue_limit
);
2336 /* Ensure that the meta-queue is actually enqueued */
2337 if (work_queue_empty(zrouter
.ribq
))
2338 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2340 return WQ_QUEUE_BLOCKED
;
2343 for (i
= 0; i
< MQ_SIZE
; i
++)
2344 if (process_subq(mq
->subq
[i
], i
)) {
2348 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2353 * Look into the RN and queue it into the highest priority queue
2354 * at this point in time for processing.
2356 * We will enqueue a route node only once per invocation.
2358 * There are two possibilities here that should be kept in mind.
2359 * If the original invocation has not been pulled off for processing
2360 * yet, A subsuquent invocation can have a route entry with a better
2361 * meta queue index value and we can have a situation where
2362 * we might have the same node enqueued 2 times. Not necessarily
2363 * an optimal situation but it should be ok.
2365 * The other possibility is that the original invocation has not
2366 * been pulled off for processing yet, A subsusquent invocation
2367 * doesn't have a route_entry with a better meta-queue and the
2368 * original metaqueue index value will win and we'll end up with
2369 * the route node enqueued once.
2371 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2373 struct route_node
*rn
= NULL
;
2374 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2375 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2377 rn
= (struct route_node
*)data
;
2379 RNODE_FOREACH_RE (rn
, curr_re
) {
2380 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2382 if (curr_qindex
<= qindex
) {
2384 qindex
= curr_qindex
;
2391 /* Invariant: at this point we always have rn->info set. */
2392 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2393 RIB_ROUTE_QUEUED(qindex
))) {
2394 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2395 rnode_debug(rn
, re
->vrf_id
,
2396 "rn %p is already queued in sub-queue %u",
2397 (void *)rn
, qindex
);
2401 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2402 listnode_add(mq
->subq
[qindex
], rn
);
2403 route_lock_node(rn
);
2406 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2407 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2408 (void *)rn
, qindex
);
2413 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2415 struct nhg_ctx
*ctx
= NULL
;
2416 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2418 ctx
= (struct nhg_ctx
*)data
;
2423 listnode_add(mq
->subq
[qindex
], ctx
);
2426 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2427 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2433 static int mq_add_handler(void *data
,
2434 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2436 if (zrouter
.ribq
== NULL
) {
2437 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2438 "%s: work_queue does not exist!", __func__
);
2443 * The RIB queue should normally be either empty or holding the only
2444 * work_queue_item element. In the latter case this element would
2445 * hold a pointer to the meta queue structure, which must be used to
2446 * actually queue the route nodes to process. So create the MQ
2447 * holder, if necessary, then push the work into it in any case.
2448 * This semantics was introduced after 0.99.9 release.
2450 if (work_queue_empty(zrouter
.ribq
))
2451 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2453 return mq_add_func(zrouter
.mq
, data
);
2456 /* Add route_node to work queue and schedule processing */
2457 int rib_queue_add(struct route_node
*rn
)
2461 /* Pointless to queue a route_node with no RIB entries to add or remove
2463 if (!rnode_to_ribs(rn
)) {
2464 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
2465 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
2466 zlog_backtrace(LOG_DEBUG
);
2470 return mq_add_handler(rn
, &rib_meta_queue_add
);
2473 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2477 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2480 /* Create new meta queue.
2481 A destructor function doesn't seem to be necessary here.
2483 static struct meta_queue
*meta_queue_new(void)
2485 struct meta_queue
*new;
2488 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2490 for (i
= 0; i
< MQ_SIZE
; i
++) {
2491 new->subq
[i
] = list_new();
2492 assert(new->subq
[i
]);
2498 void meta_queue_free(struct meta_queue
*mq
)
2502 for (i
= 0; i
< MQ_SIZE
; i
++)
2503 list_delete(&mq
->subq
[i
]);
2505 XFREE(MTYPE_WORK_QUEUE
, mq
);
2508 /* initialise zebra rib work queue */
2509 static void rib_queue_init(void)
2511 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2512 "route_node processing"))) {
2513 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2514 "%s: could not initialise work queue!", __func__
);
2518 /* fill in the work queue spec */
2519 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2520 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2521 zrouter
.ribq
->spec
.completion_func
= NULL
;
2522 /* XXX: TODO: These should be runtime configurable via vty */
2523 zrouter
.ribq
->spec
.max_retries
= 3;
2524 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2525 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2527 if (!(zrouter
.mq
= meta_queue_new())) {
2528 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2529 "%s: could not initialise meta queue!", __func__
);
2535 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2539 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2540 rnh_list_init(&dest
->nht
);
2541 re_list_init(&dest
->routes
);
2542 route_lock_node(rn
); /* rn route table reference */
2549 /* RIB updates are processed via a queue of pointers to route_nodes.
2551 * The queue length is bounded by the maximal size of the routing table,
2552 * as a route_node will not be requeued, if already queued.
2554 * REs are submitted via rib_addnode or rib_delnode which set minimal
2555 * state, or static_install_route (when an existing RE is updated)
2556 * and then submit route_node to queue for best-path selection later.
2557 * Order of add/delete state changes are preserved for any given RE.
2559 * Deleted REs are reaped during best-path selection.
2562 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2563 * |-------->| | best RE, if required
2565 * static_install->|->rib_addqueue...... -> rib_process
2567 * |-------->| |-> rib_unlink
2568 * |-> set ROUTE_ENTRY_REMOVE |
2569 * rib_delnode (RE freed)
2571 * The 'info' pointer of a route_node points to a rib_dest_t
2572 * ('dest'). Queueing state for a route_node is kept on the dest. The
2573 * dest is created on-demand by rib_link() and is kept around at least
2574 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2576 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2578 * - route_nodes: refcounted by:
2579 * - dest attached to route_node:
2580 * - managed by: rib_link/rib_gc_dest
2581 * - route_node processing queue
2582 * - managed by: rib_addqueue, rib_process.
2586 /* Add RE to head of the route node. */
2587 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2591 const char *rmap_name
;
2595 dest
= rib_dest_from_rnode(rn
);
2597 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2598 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2600 dest
= zebra_rib_create_dest(rn
);
2603 re_list_add_head(&dest
->routes
, re
);
2605 afi
= (rn
->p
.family
== AF_INET
)
2607 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2608 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2609 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2611 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2612 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2617 static void rib_addnode(struct route_node
*rn
,
2618 struct route_entry
*re
, int process
)
2620 /* RE node has been un-removed before route-node is processed.
2621 * route_node must hence already be on the queue for processing..
2623 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2624 if (IS_ZEBRA_DEBUG_RIB
)
2625 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2626 (void *)rn
, (void *)re
);
2628 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2631 rib_link(rn
, re
, process
);
2637 * Detach a rib structure from a route_node.
2639 * Note that a call to rib_unlink() should be followed by a call to
2640 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2641 * longer required to be deleted.
2643 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2649 if (IS_ZEBRA_DEBUG_RIB
)
2650 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2653 dest
= rib_dest_from_rnode(rn
);
2655 re_list_del(&dest
->routes
, re
);
2657 if (dest
->selected_fib
== re
)
2658 dest
->selected_fib
= NULL
;
2660 if (re
->nhe
&& re
->nhe_id
) {
2661 assert(re
->nhe
->id
== re
->nhe_id
);
2662 zebra_nhg_decrement_ref(re
->nhe
);
2663 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2664 nexthops_free(re
->nhe
->nhg
.nexthop
);
2666 nexthops_free(re
->fib_ng
.nexthop
);
2668 XFREE(MTYPE_OPAQUE
, re
->opaque
);
2670 XFREE(MTYPE_RE
, re
);
2673 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2677 if (IS_ZEBRA_DEBUG_RIB
)
2678 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2679 (void *)rn
, (void *)re
);
2680 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2682 afi
= (rn
->p
.family
== AF_INET
)
2684 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2685 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2686 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2688 zebra_del_import_table_entry(zvrf
, rn
, re
);
2689 /* Just clean up if non main table */
2690 if (IS_ZEBRA_DEBUG_RIB
) {
2691 char buf
[SRCDEST2STR_BUFFER
];
2692 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2693 zlog_debug("%s(%u):%s: Freeing route rn %p, re %p (%s)",
2694 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, buf
,
2695 rn
, re
, zebra_route_string(re
->type
));
2705 * Helper that debugs a single nexthop within a route-entry
2707 static void _route_entry_dump_nh(const struct route_entry
*re
,
2708 const char *straddr
,
2709 const struct nexthop
*nexthop
)
2711 char nhname
[PREFIX_STRLEN
];
2712 char backup_str
[50];
2716 struct interface
*ifp
;
2717 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2719 switch (nexthop
->type
) {
2720 case NEXTHOP_TYPE_BLACKHOLE
:
2721 snprintf(nhname
, sizeof(nhname
), "Blackhole");
2723 case NEXTHOP_TYPE_IFINDEX
:
2724 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2725 snprintf(nhname
, sizeof(nhname
), "%s",
2726 ifp
? ifp
->name
: "Unknown");
2728 case NEXTHOP_TYPE_IPV4
:
2730 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2731 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2733 case NEXTHOP_TYPE_IPV6
:
2734 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2735 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2739 backup_str
[0] = '\0';
2740 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
2741 snprintf(backup_str
, sizeof(backup_str
), "backup ");
2742 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
2743 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
2744 nexthop
->backup_idx
[i
]);
2745 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
2750 if (nexthop
->weight
)
2751 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
2753 zlog_debug("%s: %s %s[%u] vrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s",
2754 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2755 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2757 wgt_str
, backup_str
,
2758 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2761 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2764 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2767 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2770 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2773 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
2774 ? "FILTERED " : ""),
2775 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
2777 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
2782 /* This function dumps the contents of a given RE entry into
2783 * standard debug log. Calling function name and IP prefix in
2784 * question are passed as 1st and 2nd arguments.
2786 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2787 union prefixconstptr src_pp
,
2788 const struct route_entry
*re
)
2790 const struct prefix
*src_p
= src_pp
.p
;
2791 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2792 char straddr
[PREFIX_STRLEN
];
2793 char srcaddr
[PREFIX_STRLEN
];
2794 struct nexthop
*nexthop
;
2795 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
2796 struct nexthop_group
*nhg
;
2798 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
2799 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2800 is_srcdst
? " from " : "",
2801 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2803 VRF_LOGNAME(vrf
), re
->vrf_id
);
2804 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2805 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2807 zlog_debug("%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2808 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
,
2810 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2811 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
2812 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
2815 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
2816 _route_entry_dump_nh(re
, straddr
, nexthop
);
2818 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
2819 zlog_debug("%s: backup nexthops:", straddr
);
2821 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
2822 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
2823 _route_entry_dump_nh(re
, straddr
, nexthop
);
2826 zlog_debug("%s: dump complete", straddr
);
2830 * This is an exported helper to rtm_read() to dump the strange
2831 * RE entry found by rib_lookup_ipv4_route()
2833 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2835 struct route_table
*table
;
2836 struct route_node
*rn
;
2837 struct route_entry
*re
;
2840 vrf
= vrf_lookup_by_id(vrf_id
);
2843 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2845 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2846 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2847 VRF_LOGNAME(vrf
), vrf_id
);
2851 /* Scan the RIB table for exactly matching RE entry. */
2852 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2854 /* No route for this prefix. */
2856 zlog_debug("%s:%s(%u) lookup failed for %pFX", __func__
,
2857 VRF_LOGNAME(vrf
), vrf_id
, (struct prefix
*)p
);
2862 route_unlock_node(rn
);
2865 RNODE_FOREACH_RE (rn
, re
) {
2866 zlog_debug("%s:%s(%u) rn %p, re %p: %s, %s", __func__
,
2867 VRF_LOGNAME(vrf
), vrf_id
, (void *)rn
, (void *)re
,
2868 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2871 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2874 route_entry_dump(p
, NULL
, re
);
2878 /* Check if requested address assignment will fail due to another
2879 * route being installed by zebra in FIB already. Take necessary
2880 * actions, if needed: remove such a route from FIB and deSELECT
2881 * corresponding RE entry. Then put affected RN into RIBQ head.
2883 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2885 struct route_table
*table
;
2886 struct route_node
*rn
;
2889 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2890 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2892 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2893 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2894 VRF_LOGNAME(vrf
), vrf_id
);
2898 /* No matches would be the simplest case. */
2899 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2903 route_unlock_node(rn
);
2905 dest
= rib_dest_from_rnode(rn
);
2906 /* Check all RE entries. In case any changes have to be done, requeue
2907 * the RN into RIBQ head. If the routing message about the new connected
2908 * route (generated by the IP address we are going to assign very soon)
2909 * comes before the RIBQ is processed, the new RE entry will join
2910 * RIBQ record already on head. This is necessary for proper
2912 * of the rest of the RE.
2914 if (dest
->selected_fib
) {
2915 if (IS_ZEBRA_DEBUG_RIB
) {
2917 vrf_lookup_by_id(dest
->selected_fib
->vrf_id
);
2920 "%s(%u):%pFX: freeing way for connected prefix",
2921 VRF_LOGNAME(vrf
), dest
->selected_fib
->vrf_id
,
2923 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2925 rib_uninstall(rn
, dest
->selected_fib
);
2931 * Internal route-add implementation; there are a couple of different public
2932 * signatures. Callers in this path are responsible for the memory they
2933 * allocate: if they allocate a nexthop_group or backup nexthop info, they
2934 * must free those objects. If this returns < 0, an error has occurred and the
2935 * route_entry 're' has not been captured; the caller should free that also.
2937 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
2938 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
2939 struct nhg_hash_entry
*re_nhe
)
2941 struct nhg_hash_entry
*nhe
= NULL
;
2942 struct route_table
*table
;
2943 struct route_node
*rn
;
2944 struct route_entry
*same
= NULL
;
2950 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2953 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2958 if (re
->nhe_id
> 0) {
2959 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2963 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2964 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
2970 /* Lookup nhe from route information */
2971 nhe
= zebra_nhg_rib_find_nhe(re_nhe
, afi
);
2973 char buf2
[PREFIX_STRLEN
] = "";
2976 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2977 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
2978 p
, src_p
? " from " : "",
2979 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
2987 * Attach the re to the nhe's nexthop group.
2989 * TODO: This will need to change when we start getting IDs from upper
2990 * level protocols, as the refcnt might be wrong, since it checks
2991 * if old_id != new_id.
2993 route_entry_update_nhe(re
, nhe
);
2995 /* Make it sure prefixlen is applied to the prefix. */
2998 apply_mask_ipv6(src_p
);
3000 /* Set default distance by route type. */
3001 if (re
->distance
== 0)
3002 re
->distance
= route_distance(re
->type
);
3004 /* Lookup route node.*/
3005 rn
= srcdest_rnode_get(table
, p
, src_p
);
3008 * If same type of route are installed, treat it as a implicit
3009 * withdraw. If the user has specified the No route replace semantics
3010 * for the install don't do a route replace.
3012 RNODE_FOREACH_RE (rn
, same
) {
3013 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
3016 /* Compare various route_entry properties */
3017 if (rib_compare_routes(re
, same
))
3021 /* If this route is kernel/connected route, notify the dataplane. */
3022 if (RIB_SYSTEM_ROUTE(re
)) {
3023 /* Notify dataplane */
3024 dplane_sys_route_add(rn
, re
);
3027 /* Link new re to node.*/
3028 if (IS_ZEBRA_DEBUG_RIB
) {
3029 rnode_debug(rn
, re
->vrf_id
,
3030 "Inserting route rn %p, re %p (%s) existing %p",
3031 rn
, re
, zebra_route_string(re
->type
), same
);
3033 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3034 route_entry_dump(p
, src_p
, re
);
3037 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3038 rib_addnode(rn
, re
, 1);
3041 /* Free implicit route.*/
3043 rib_delnode(rn
, same
);
3045 route_unlock_node(rn
);
3050 * Add a single route.
3052 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
3053 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3054 struct nexthop_group
*ng
)
3057 struct nhg_hash_entry nhe
;
3062 /* We either need nexthop(s) or an existing nexthop id */
3063 if (ng
== NULL
&& re
->nhe_id
== 0)
3067 * Use a temporary nhe to convey info to the common/main api.
3069 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
3071 nhe
.nhg
.nexthop
= ng
->nexthop
;
3072 else if (re
->nhe_id
> 0)
3073 nhe
.id
= re
->nhe_id
;
3075 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, &nhe
);
3077 /* In this path, the callers expect memory to be freed. */
3078 nexthop_group_delete(&ng
);
3080 /* In error cases, free the route also */
3082 XFREE(MTYPE_RE
, re
);
3087 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3088 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3089 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3090 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
3091 uint8_t distance
, bool fromkernel
)
3093 struct route_table
*table
;
3094 struct route_node
*rn
;
3095 struct route_entry
*re
;
3096 struct route_entry
*fib
= NULL
;
3097 struct route_entry
*same
= NULL
;
3098 struct nexthop
*rtnh
;
3099 char buf2
[INET6_ADDRSTRLEN
];
3102 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3105 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
3113 apply_mask_ipv6(src_p
);
3115 /* Lookup route node. */
3116 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
3118 if (IS_ZEBRA_DEBUG_RIB
) {
3119 char src_buf
[PREFIX_STRLEN
];
3120 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3122 if (src_p
&& src_p
->prefixlen
)
3123 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
3127 zlog_debug("%s[%d]:%pFX%s%s doesn't exist in rib",
3128 vrf
->name
, table_id
, p
,
3129 (src_buf
[0] != '\0') ? " from " : "",
3135 dest
= rib_dest_from_rnode(rn
);
3136 fib
= dest
->selected_fib
;
3138 /* Lookup same type route. */
3139 RNODE_FOREACH_RE (rn
, re
) {
3140 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3143 if (re
->type
!= type
)
3145 if (re
->instance
!= instance
)
3147 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
3148 distance
!= re
->distance
)
3151 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
3153 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
3154 (rtnh
= re
->nhe
->nhg
.nexthop
)
3155 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
3156 if (rtnh
->ifindex
!= nh
->ifindex
)
3162 /* Make sure that the route found has the same gateway. */
3163 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
3172 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3174 * No guarantee all kernel send nh with labels
3177 if (nexthop_same_no_labels(rtnh
, nh
)) {
3186 /* If same type of route can't be found and this message is from
3190 * In the past(HA!) we could get here because
3191 * we were receiving a route delete from the
3192 * kernel and we're not marking the proto
3193 * as coming from it's appropriate originator.
3194 * Now that we are properly noticing the fact
3195 * that the kernel has deleted our route we
3196 * are not going to get called in this path
3197 * I am going to leave this here because
3198 * this might still work this way on non-linux
3199 * platforms as well as some weird state I have
3200 * not properly thought of yet.
3201 * If we can show that this code path is
3202 * dead then we can remove it.
3204 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
3205 if (IS_ZEBRA_DEBUG_RIB
) {
3206 rnode_debug(rn
, vrf_id
,
3207 "rn %p, re %p (%s) was deleted from kernel, adding",
3209 zebra_route_string(fib
->type
));
3212 || CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3213 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3215 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3217 UNSET_FLAG(rtnh
->flags
,
3221 * This is a non FRR route
3222 * as such we should mark
3225 dest
->selected_fib
= NULL
;
3227 /* This means someone else, other than Zebra,
3229 * a Zebra router from the kernel. We will add
3231 rib_install_kernel(rn
, fib
, NULL
);
3234 if (IS_ZEBRA_DEBUG_RIB
) {
3238 "via %s ifindex %d type %d doesn't exist in rib",
3239 inet_ntop(afi2family(afi
),
3246 "type %d doesn't exist in rib",
3249 route_unlock_node(rn
);
3255 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
3257 rib_install_kernel(rn
, same
, NULL
);
3258 route_unlock_node(rn
);
3263 /* Special handling for IPv4 or IPv6 routes sourced from
3264 * EVPN - the nexthop (and associated MAC) need to be
3265 * uninstalled if no more refs.
3267 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
3268 struct nexthop
*tmp_nh
;
3270 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3271 struct ipaddr vtep_ip
;
3273 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3274 if (afi
== AFI_IP
) {
3275 vtep_ip
.ipa_type
= IPADDR_V4
;
3276 memcpy(&(vtep_ip
.ipaddr_v4
),
3277 &(tmp_nh
->gate
.ipv4
),
3278 sizeof(struct in_addr
));
3280 vtep_ip
.ipa_type
= IPADDR_V6
;
3281 memcpy(&(vtep_ip
.ipaddr_v6
),
3282 &(tmp_nh
->gate
.ipv6
),
3283 sizeof(struct in6_addr
));
3285 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
3290 /* Notify dplane if system route changes */
3291 if (RIB_SYSTEM_ROUTE(re
))
3292 dplane_sys_route_del(rn
, same
);
3294 rib_delnode(rn
, same
);
3297 route_unlock_node(rn
);
3302 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3303 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3304 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3305 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3306 uint8_t distance
, route_tag_t tag
)
3308 struct route_entry
*re
= NULL
;
3309 struct nexthop
*nexthop
= NULL
;
3310 struct nexthop_group
*ng
= NULL
;
3312 /* Allocate new route_entry structure. */
3313 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3315 re
->instance
= instance
;
3316 re
->distance
= distance
;
3318 re
->metric
= metric
;
3320 re
->table
= table_id
;
3321 re
->vrf_id
= vrf_id
;
3322 re
->uptime
= monotime(NULL
);
3324 re
->nhe_id
= nhe_id
;
3326 /* If the owner of the route supplies a shared nexthop-group id,
3327 * we'll use that. Otherwise, pass the nexthop along directly.
3330 ng
= nexthop_group_new();
3333 nexthop
= nexthop_new();
3335 nexthop_group_add_sorted(ng
, nexthop
);
3338 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
3341 static const char *rib_update_event2str(enum rib_update_event event
)
3343 const char *ret
= "UNKNOWN";
3346 case RIB_UPDATE_KERNEL
:
3347 ret
= "RIB_UPDATE_KERNEL";
3349 case RIB_UPDATE_RMAP_CHANGE
:
3350 ret
= "RIB_UPDATE_RMAP_CHANGE";
3352 case RIB_UPDATE_OTHER
:
3353 ret
= "RIB_UPDATE_OTHER";
3355 case RIB_UPDATE_MAX
:
3363 /* Schedule route nodes to be processed if they match the type */
3364 static void rib_update_route_node(struct route_node
*rn
, int type
)
3366 struct route_entry
*re
, *next
;
3367 bool re_changed
= false;
3369 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3370 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3371 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3380 /* Schedule routes of a particular table (address-family) based on event. */
3381 void rib_update_table(struct route_table
*table
, enum rib_update_event event
)
3383 struct route_node
*rn
;
3385 if (IS_ZEBRA_DEBUG_EVENT
) {
3386 struct zebra_vrf
*zvrf
;
3390 ? ((struct rib_table_info
*)table
->info
)->zvrf
3392 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3394 zlog_debug("%s: %s VRF %s Table %u event %s", __func__
,
3395 table
->info
? afi2str(
3396 ((struct rib_table_info
*)table
->info
)->afi
)
3398 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
3399 rib_update_event2str(event
));
3402 /* Walk all routes and queue for processing, if appropriate for
3403 * the trigger event.
3405 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3407 * If we are looking at a route node and the node
3408 * has already been queued we don't
3409 * need to queue it up again
3412 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3413 RIB_ROUTE_ANY_QUEUED
))
3417 case RIB_UPDATE_KERNEL
:
3418 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3420 case RIB_UPDATE_RMAP_CHANGE
:
3421 case RIB_UPDATE_OTHER
:
3422 rib_update_route_node(rn
, ZEBRA_ROUTE_ALL
);
3430 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
)
3432 struct route_table
*table
;
3434 if (IS_ZEBRA_DEBUG_EVENT
)
3435 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3436 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3438 /* Process routes of interested address-families. */
3439 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3441 rib_update_table(table
, event
);
3443 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3445 rib_update_table(table
, event
);
3448 static void rib_update_handle_vrf_all(enum rib_update_event event
)
3450 struct zebra_router_table
*zrt
;
3452 if (IS_ZEBRA_DEBUG_EVENT
)
3453 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3454 rib_update_event2str(event
));
3456 /* Just iterate over all the route tables, rather than vrf lookups */
3457 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3458 rib_update_table(zrt
->table
, event
);
3461 struct rib_update_ctx
{
3462 enum rib_update_event event
;
3467 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3468 enum rib_update_event event
)
3470 struct rib_update_ctx
*ctx
;
3472 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3475 ctx
->vrf_id
= vrf_id
;
3480 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3482 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3485 static int rib_update_handler(struct thread
*thread
)
3487 struct rib_update_ctx
*ctx
;
3489 ctx
= THREAD_ARG(thread
);
3492 rib_update_handle_vrf_all(ctx
->event
);
3494 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
);
3496 rib_update_ctx_fini(&ctx
);
3502 * Thread list to ensure we don't schedule a ton of events
3503 * if interfaces are flapping for instance.
3505 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3507 /* Schedule a RIB update event for specific vrf */
3508 void rib_update_vrf(vrf_id_t vrf_id
, enum rib_update_event event
)
3510 struct rib_update_ctx
*ctx
;
3512 ctx
= rib_update_ctx_init(vrf_id
, event
);
3514 /* Don't worry about making sure multiple rib updates for specific vrf
3515 * are scheduled at once for now. If it becomes a problem, we can use a
3516 * lookup of some sort to keep track of running threads via t_vrf_id
3517 * like how we are doing it in t_rib_update_threads[].
3519 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0, NULL
);
3521 if (IS_ZEBRA_DEBUG_EVENT
)
3522 zlog_debug("%s: Scheduled VRF %s, event %s", __func__
,
3523 vrf_id_to_name(ctx
->vrf_id
),
3524 rib_update_event2str(event
));
3527 /* Schedule a RIB update event for all vrfs */
3528 void rib_update(enum rib_update_event event
)
3530 struct rib_update_ctx
*ctx
;
3532 ctx
= rib_update_ctx_init(0, event
);
3534 ctx
->vrf_all
= true;
3536 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3537 &t_rib_update_threads
[event
]))
3538 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3539 else if (IS_ZEBRA_DEBUG_EVENT
)
3540 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
3541 rib_update_event2str(event
));
3544 /* Delete self installed routes after zebra is relaunched. */
3545 void rib_sweep_table(struct route_table
*table
)
3547 struct route_node
*rn
;
3548 struct route_entry
*re
;
3549 struct route_entry
*next
;
3550 struct nexthop
*nexthop
;
3555 if (IS_ZEBRA_DEBUG_RIB
)
3556 zlog_debug("%s: starting", __func__
);
3558 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3559 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3561 if (IS_ZEBRA_DEBUG_RIB
)
3562 route_entry_dump(&rn
->p
, NULL
, re
);
3564 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3567 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3571 * If routes are older than startup_time then
3572 * we know we read them in from the kernel.
3573 * As such we can safely remove them.
3575 if (zrouter
.startup_time
< re
->uptime
)
3579 * So we are starting up and have received
3580 * routes from the kernel that we have installed
3581 * from a previous run of zebra but not cleaned
3582 * up ( say a kill -9 )
3583 * But since we haven't actually installed
3584 * them yet( we received them from the kernel )
3585 * we don't think they are active.
3586 * So let's pretend they are active to actually
3588 * In all honesty I'm not sure if we should
3589 * mark them as active when we receive them
3590 * This is startup only so probably ok.
3592 * If we ever decide to move rib_sweep_table
3593 * to a different spot (ie startup )
3594 * this decision needs to be revisited
3596 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3597 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3598 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3600 rib_uninstall_kernel(rn
, re
);
3601 rib_delnode(rn
, re
);
3605 if (IS_ZEBRA_DEBUG_RIB
)
3606 zlog_debug("%s: ends", __func__
);
3609 /* Sweep all RIB tables. */
3610 int rib_sweep_route(struct thread
*t
)
3613 struct zebra_vrf
*zvrf
;
3615 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3616 if ((zvrf
= vrf
->info
) == NULL
)
3619 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3620 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3623 zebra_router_sweep_route();
3624 zebra_router_sweep_nhgs();
3629 /* Remove specific by protocol routes from 'table'. */
3630 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3631 struct route_table
*table
)
3633 struct route_node
*rn
;
3634 struct route_entry
*re
;
3635 struct route_entry
*next
;
3636 unsigned long n
= 0;
3639 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3640 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3641 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3643 if (re
->type
== proto
3644 && re
->instance
== instance
) {
3645 rib_delnode(rn
, re
);
3652 /* Remove specific by protocol routes. */
3653 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3656 struct zebra_vrf
*zvrf
;
3657 struct other_route_table
*ort
;
3658 unsigned long cnt
= 0;
3660 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3665 cnt
+= rib_score_proto_table(proto
, instance
,
3666 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3667 + rib_score_proto_table(
3669 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3671 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3672 rib_score_proto_table(proto
, instance
, ort
->table
);
3678 /* Close RIB and clean up kernel routes. */
3679 void rib_close_table(struct route_table
*table
)
3681 struct route_node
*rn
;
3682 struct rib_table_info
*info
;
3688 info
= route_table_get_info(table
);
3690 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3691 dest
= rib_dest_from_rnode(rn
);
3693 if (dest
&& dest
->selected_fib
) {
3694 if (info
->safi
== SAFI_UNICAST
)
3695 hook_call(rib_update
, rn
, NULL
);
3697 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3698 dest
->selected_fib
= NULL
;
3704 * Handler for async dataplane results after a pseudowire installation
3706 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3708 struct zebra_pw
*pw
;
3709 struct zebra_vrf
*vrf
;
3711 /* The pseudowire code assumes success - we act on an error
3712 * result for installation attempts here.
3714 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3717 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3718 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3719 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3721 zebra_pw_install_failure(pw
,
3722 dplane_ctx_get_pw_status(ctx
));
3726 dplane_ctx_fini(&ctx
);
3733 * Handle results from the dataplane system. Dequeue update context
3734 * structs, dispatch to appropriate internal handlers.
3736 static int rib_process_dplane_results(struct thread
*thread
)
3738 struct zebra_dplane_ctx
*ctx
;
3739 struct dplane_ctx_q ctxlist
;
3740 bool shut_p
= false;
3742 /* Dequeue a list of completed updates with one lock/unlock cycle */
3745 TAILQ_INIT(&ctxlist
);
3747 /* Take lock controlling queue of results */
3748 frr_with_mutex(&dplane_mutex
) {
3749 /* Dequeue list of context structs */
3750 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3753 /* Dequeue context block */
3754 ctx
= dplane_ctx_dequeue(&ctxlist
);
3756 /* If we've emptied the results queue, we're done */
3760 /* If zebra is shutting down, avoid processing results,
3761 * just drain the results queue.
3763 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3764 memory_order_relaxed
);
3767 dplane_ctx_fini(&ctx
);
3769 ctx
= dplane_ctx_dequeue(&ctxlist
);
3776 switch (dplane_ctx_get_op(ctx
)) {
3777 case DPLANE_OP_ROUTE_INSTALL
:
3778 case DPLANE_OP_ROUTE_UPDATE
:
3779 case DPLANE_OP_ROUTE_DELETE
:
3781 /* Bit of special case for route updates
3782 * that were generated by async notifications:
3783 * we don't want to continue processing these
3786 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3787 rib_process_result(ctx
);
3789 dplane_ctx_fini(&ctx
);
3793 case DPLANE_OP_ROUTE_NOTIFY
:
3794 rib_process_dplane_notify(ctx
);
3797 case DPLANE_OP_NH_INSTALL
:
3798 case DPLANE_OP_NH_UPDATE
:
3799 case DPLANE_OP_NH_DELETE
:
3800 zebra_nhg_dplane_result(ctx
);
3803 case DPLANE_OP_LSP_INSTALL
:
3804 case DPLANE_OP_LSP_UPDATE
:
3805 case DPLANE_OP_LSP_DELETE
:
3807 /* Bit of special case for LSP updates
3808 * that were generated by async notifications:
3809 * we don't want to continue processing these.
3811 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3812 zebra_mpls_lsp_dplane_result(ctx
);
3814 dplane_ctx_fini(&ctx
);
3818 case DPLANE_OP_LSP_NOTIFY
:
3819 zebra_mpls_process_dplane_notify(ctx
);
3822 case DPLANE_OP_PW_INSTALL
:
3823 case DPLANE_OP_PW_UNINSTALL
:
3824 handle_pw_result(ctx
);
3827 case DPLANE_OP_SYS_ROUTE_ADD
:
3828 case DPLANE_OP_SYS_ROUTE_DELETE
:
3829 /* No further processing in zebra for these. */
3830 dplane_ctx_fini(&ctx
);
3833 case DPLANE_OP_MAC_INSTALL
:
3834 case DPLANE_OP_MAC_DELETE
:
3835 zebra_vxlan_handle_result(ctx
);
3838 case DPLANE_OP_RULE_ADD
:
3839 case DPLANE_OP_RULE_DELETE
:
3840 case DPLANE_OP_RULE_UPDATE
:
3841 zebra_pbr_dplane_result(ctx
);
3844 /* Some op codes not handled here */
3845 case DPLANE_OP_ADDR_INSTALL
:
3846 case DPLANE_OP_ADDR_UNINSTALL
:
3847 case DPLANE_OP_NEIGH_INSTALL
:
3848 case DPLANE_OP_NEIGH_UPDATE
:
3849 case DPLANE_OP_NEIGH_DELETE
:
3850 case DPLANE_OP_VTEP_ADD
:
3851 case DPLANE_OP_VTEP_DELETE
:
3852 case DPLANE_OP_NEIGH_DISCOVER
:
3853 case DPLANE_OP_BR_PORT_UPDATE
:
3854 case DPLANE_OP_NONE
:
3855 /* Don't expect this: just return the struct? */
3856 dplane_ctx_fini(&ctx
);
3859 } /* Dispatch by op code */
3861 ctx
= dplane_ctx_dequeue(&ctxlist
);
3870 * Results are returned from the dataplane subsystem, in the context of
3871 * the dataplane pthread. We enqueue the results here for processing by
3872 * the main thread later.
3874 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3876 /* Take lock controlling queue of results */
3877 frr_with_mutex(&dplane_mutex
) {
3878 /* Enqueue context blocks */
3879 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3882 /* Ensure event is signalled to zebra main pthread */
3883 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3890 * Ensure there are no empty slots in the route_info array.
3891 * Every route type in zebra should be present there.
3893 static void check_route_info(void)
3895 int len
= array_size(route_info
);
3898 * ZEBRA_ROUTE_SYSTEM is special cased since
3899 * its key is 0 anyway.
3901 * ZEBRA_ROUTE_ALL is also ignored.
3903 for (int i
= 0; i
< len
; i
++) {
3904 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3906 assert(route_info
[i
].key
);
3907 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3911 /* Routing information base initialize. */
3918 /* Init dataplane, and register for results */
3919 pthread_mutex_init(&dplane_mutex
, NULL
);
3920 TAILQ_INIT(&rib_dplane_q
);
3921 zebra_dplane_init(rib_dplane_results
);
3927 * Get the first vrf id that is greater than the given vrf id if any.
3929 * Returns true if a vrf id was found, false otherwise.
3931 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3935 vrf
= vrf_lookup_by_id(vrf_id
);
3937 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3939 *next_id_p
= vrf
->vrf_id
;
3948 * rib_tables_iter_next
3950 * Returns the next table in the iteration.
3952 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3954 struct route_table
*table
;
3957 * Array that helps us go over all AFI/SAFI combinations via one
3960 static const struct {
3964 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3965 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3966 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3971 switch (iter
->state
) {
3973 case RIB_TABLES_ITER_S_INIT
:
3974 iter
->vrf_id
= VRF_DEFAULT
;
3975 iter
->afi_safi_ix
= -1;
3979 case RIB_TABLES_ITER_S_ITERATING
:
3980 iter
->afi_safi_ix
++;
3983 while (iter
->afi_safi_ix
3984 < (int)array_size(afi_safis
)) {
3985 table
= zebra_vrf_table(
3986 afi_safis
[iter
->afi_safi_ix
].afi
,
3987 afi_safis
[iter
->afi_safi_ix
].safi
,
3992 iter
->afi_safi_ix
++;
3996 * Found another table in this vrf.
4002 * Done with all tables in the current vrf, go to the
4006 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4009 iter
->afi_safi_ix
= 0;
4014 case RIB_TABLES_ITER_S_DONE
:
4019 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4021 iter
->state
= RIB_TABLES_ITER_S_DONE
;