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"
43 #include "zebra/zebra_router.h"
44 #include "zebra/connected.h"
45 #include "zebra/debug.h"
46 #include "zebra/interface.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/rib.h"
50 #include "zebra/zapi_msg.h"
51 #include "zebra/zebra_errors.h"
52 #include "zebra/zebra_memory.h"
53 #include "zebra/zebra_ns.h"
54 #include "zebra/zebra_rnh.h"
55 #include "zebra/zebra_routemap.h"
56 #include "zebra/zebra_vrf.h"
57 #include "zebra/zebra_vxlan.h"
58 #include "zebra/zapi_msg.h"
59 #include "zebra/zebra_dplane.h"
61 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
64 * Event, list, and mutex for delivery of dataplane results
66 static pthread_mutex_t dplane_mutex
;
67 static struct thread
*t_dplane
;
68 static struct dplane_ctx_q rib_dplane_q
;
70 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
73 /* Should we allow non Quagga processes to delete our routes */
74 extern int allow_delete
;
76 /* Each route type's string and default distance value. */
81 } route_info
[ZEBRA_ROUTE_MAX
] = {
82 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Uneeded for nhg's */, 0},
83 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 6},
84 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 2},
85 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 1},
86 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 3},
87 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 4},
88 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 4},
89 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 4},
90 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 4},
91 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 4},
92 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 5},
93 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 6},
94 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 4},
95 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 4},
96 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 6},
97 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 6},
98 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 3},
99 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 6},
100 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 5},
101 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 5},
102 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 5},
103 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 5},
104 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 5},
105 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 4},
106 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 6},
107 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 6},
108 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 6},
109 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 4},
110 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 6},
111 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, 6},
112 /* Any new route type added to zebra, should be mirrored here */
114 /* no entry/default: 150 */
117 static void PRINTFRR(5, 6)
118 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
119 int priority
, const char *msgfmt
, ...)
121 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
126 va_start(ap
, msgfmt
);
127 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
131 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
132 rib_dest_t
*dest
= NULL
;
133 struct route_entry
*re
= NULL
;
135 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
137 if (info
->safi
== SAFI_MULTICAST
)
138 strlcat(buf
, " (MRIB)", sizeof(buf
));
140 dest
= rib_dest_from_rnode(rn
);
142 re
= re_list_first(&dest
->routes
);
146 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
149 zlog(priority
, "%s: (%u:%u):%s: %s", _func
, vrf_id
, table
, buf
, msgbuf
);
152 static char *_dump_re_status(const struct route_entry
*re
, char *buf
,
155 if (re
->status
== 0) {
156 snprintfrr(buf
, len
, "None ");
161 buf
, len
, "%s%s%s%s%s%s%s",
162 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
) ? "Removed " : "",
163 CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) ? "Changed " : "",
164 CHECK_FLAG(re
->status
, ROUTE_ENTRY_LABELS_CHANGED
)
167 CHECK_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
) ? "Queued " : "",
168 CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
) ? "Installed "
170 CHECK_FLAG(re
->status
, ROUTE_ENTRY_FAILED
) ? "Failed " : "",
171 CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
) ? "Fib NHG "
176 #define rnode_debug(node, vrf_id, ...) \
177 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
178 #define rnode_info(node, ...) \
179 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
181 uint8_t route_distance(int type
)
185 if ((unsigned)type
>= array_size(route_info
))
188 distance
= route_info
[type
].distance
;
193 int is_zebra_valid_kernel_table(uint32_t table_id
)
196 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
197 || (table_id
== RT_TABLE_COMPAT
))
204 int is_zebra_main_routing_table(uint32_t table_id
)
206 if (table_id
== RT_TABLE_MAIN
)
211 int zebra_check_addr(const struct prefix
*p
)
213 if (p
->family
== AF_INET
) {
216 addr
= p
->u
.prefix4
.s_addr
;
219 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
220 || IPV4_LINKLOCAL(addr
))
223 if (p
->family
== AF_INET6
) {
224 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
226 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
233 * copy_nexthop - copy a nexthop to the rib structure.
235 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
237 assert(!re
->nhe
->nhg
.nexthop
);
238 copy_nexthops(&re
->nhe
->nhg
.nexthop
, nh
, NULL
);
241 static void route_entry_attach_ref(struct route_entry
*re
,
242 struct nhg_hash_entry
*new)
245 re
->nhe_id
= new->id
;
247 zebra_nhg_increment_ref(new);
250 int route_entry_update_nhe(struct route_entry
*re
,
251 struct nhg_hash_entry
*new_nhghe
)
253 struct nhg_hash_entry
*old
;
256 if (new_nhghe
== NULL
) {
258 zebra_nhg_decrement_ref(re
->nhe
);
263 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
266 route_entry_attach_ref(re
, new_nhghe
);
269 zebra_nhg_decrement_ref(old
);
271 /* This is the first time it's being attached */
272 route_entry_attach_ref(re
, new_nhghe
);
278 void rib_handle_nhg_replace(struct nhg_hash_entry
*old_entry
,
279 struct nhg_hash_entry
*new_entry
)
281 struct zebra_router_table
*zrt
;
282 struct route_node
*rn
;
283 struct route_entry
*re
, *next
;
285 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
286 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
287 __func__
, new_entry
->id
, new_entry
, old_entry
);
289 /* We have to do them ALL */
290 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
291 for (rn
= route_top(zrt
->table
); rn
;
292 rn
= srcdest_route_next(rn
)) {
293 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
294 if (re
->nhe
&& re
->nhe
== old_entry
)
295 route_entry_update_nhe(re
, new_entry
);
301 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
302 union g_addr
*addr
, struct route_node
**rn_out
)
305 struct route_table
*table
;
306 struct route_node
*rn
;
307 struct route_entry
*match
= NULL
;
310 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
314 memset(&p
, 0, sizeof(struct prefix
));
317 p
.u
.prefix4
= addr
->ipv4
;
318 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
320 p
.u
.prefix6
= addr
->ipv6
;
321 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
324 rn
= route_node_match(table
, &p
);
329 route_unlock_node(rn
);
331 dest
= rib_dest_from_rnode(rn
);
332 if (dest
&& dest
->selected_fib
333 && !CHECK_FLAG(dest
->selected_fib
->status
,
334 ROUTE_ENTRY_REMOVED
))
335 match
= dest
->selected_fib
;
337 /* If there is no selected route or matched route is EGP, go up
342 } while (rn
&& rn
->info
== NULL
);
346 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
347 if (!CHECK_FLAG(match
->status
,
348 ROUTE_ENTRY_INSTALLED
))
360 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
362 struct route_node
**rn_out
)
364 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
365 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
366 union g_addr gaddr
= {.ipv4
= addr
};
368 switch (zrouter
.ipv4_multicast_mode
) {
369 case MCAST_MRIB_ONLY
:
370 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
372 case MCAST_URIB_ONLY
:
373 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
374 case MCAST_NO_CONFIG
:
375 case MCAST_MIX_MRIB_FIRST
:
376 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
379 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
382 case MCAST_MIX_DISTANCE
:
383 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
384 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
386 re
= ure
->distance
< mre
->distance
? ure
: mre
;
392 case MCAST_MIX_PFXLEN
:
393 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
394 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
396 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
405 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
407 if (IS_ZEBRA_DEBUG_RIB
) {
409 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
411 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
412 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
413 mre
? (ure
? "MRIB+URIB" : "MRIB")
414 : ure
? "URIB" : "nothing",
415 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
420 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
422 struct route_table
*table
;
423 struct route_node
*rn
;
424 struct route_entry
*match
= NULL
;
428 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
432 rn
= route_node_lookup(table
, (struct prefix
*)p
);
434 /* No route for this prefix. */
439 route_unlock_node(rn
);
440 dest
= rib_dest_from_rnode(rn
);
442 if (dest
&& dest
->selected_fib
443 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
444 match
= dest
->selected_fib
;
449 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
452 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
459 * Is this RIB labeled-unicast? It must be of type BGP and all paths
460 * (nexthops) must have a label.
462 int zebra_rib_labeled_unicast(struct route_entry
*re
)
464 struct nexthop
*nexthop
= NULL
;
466 if (re
->type
!= ZEBRA_ROUTE_BGP
)
469 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
470 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
476 /* Update flag indicates whether this is a "replace" or not. Currently, this
477 * is only used for IPv4.
479 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
480 struct route_entry
*old
)
482 struct nexthop
*nexthop
;
483 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
484 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
485 const struct prefix
*p
, *src_p
;
486 enum zebra_dplane_result ret
;
488 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
490 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
492 if (info
->safi
!= SAFI_UNICAST
) {
493 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
494 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
500 * Install the resolved nexthop object first.
502 zebra_nhg_install_kernel(re
->nhe
);
505 * If this is a replace to a new RE let the originator of the RE
506 * know that they've lost
508 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
509 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
,
510 info
->afi
, info
->safi
);
512 /* Update fib selection */
513 dest
->selected_fib
= re
;
516 * Make sure we update the FPM any time we send new information to
519 hook_call(rib_update
, rn
, "installing in kernel");
521 /* Send add or update */
523 ret
= dplane_route_update(rn
, re
, old
);
525 ret
= dplane_route_add(rn
, re
);
528 case ZEBRA_DPLANE_REQUEST_QUEUED
:
529 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
532 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
534 /* Free old FIB nexthop group */
535 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
536 if (old
->fib_ng
.nexthop
) {
537 nexthops_free(old
->fib_ng
.nexthop
);
538 old
->fib_ng
.nexthop
= NULL
;
543 zvrf
->installs_queued
++;
545 case ZEBRA_DPLANE_REQUEST_FAILURE
:
547 char str
[SRCDEST2STR_BUFFER
];
549 srcdest_rnode2str(rn
, str
, sizeof(str
));
550 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
551 "%u:%u:%s: Failed to enqueue dataplane install",
552 re
->vrf_id
, re
->table
, str
);
555 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
564 /* Uninstall the route from kernel. */
565 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
567 struct nexthop
*nexthop
;
568 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
569 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
571 if (info
->safi
!= SAFI_UNICAST
) {
572 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
573 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
574 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
579 * Make sure we update the FPM any time we send new information to
582 hook_call(rib_update
, rn
, "uninstalling from kernel");
584 switch (dplane_route_delete(rn
, re
)) {
585 case ZEBRA_DPLANE_REQUEST_QUEUED
:
587 zvrf
->removals_queued
++;
589 case ZEBRA_DPLANE_REQUEST_FAILURE
:
591 char str
[SRCDEST2STR_BUFFER
];
593 srcdest_rnode2str(rn
, str
, sizeof(str
));
594 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
595 "%u:%s: Failed to enqueue dataplane uninstall",
599 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
608 /* Uninstall the route from kernel. */
609 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
611 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
612 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
613 struct nexthop
*nexthop
;
615 if (dest
&& dest
->selected_fib
== re
) {
616 if (info
->safi
== SAFI_UNICAST
)
617 hook_call(rib_update
, rn
, "rib_uninstall");
619 /* If labeled-unicast route, uninstall transit LSP. */
620 if (zebra_rib_labeled_unicast(re
))
621 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
623 rib_uninstall_kernel(rn
, re
);
625 dest
->selected_fib
= NULL
;
627 /* Free FIB nexthop group, if present */
628 if (re
->fib_ng
.nexthop
) {
629 nexthops_free(re
->fib_ng
.nexthop
);
630 re
->fib_ng
.nexthop
= NULL
;
632 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
634 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
635 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
638 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
639 const struct prefix
*p
, *src_p
;
641 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
643 redistribute_delete(p
, src_p
, re
, NULL
);
644 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
649 * rib_can_delete_dest
651 * Returns true if the given dest can be deleted from the table.
653 static int rib_can_delete_dest(rib_dest_t
*dest
)
655 if (re_list_first(&dest
->routes
)) {
660 * Unresolved rnh's are stored on the default route's list
662 * dest->rnode can also be the source prefix node in an
663 * ipv6 sourcedest table. Fortunately the prefix of a
664 * source prefix node can never be the default prefix.
666 if (is_default_prefix(&dest
->rnode
->p
))
670 * Don't delete the dest if we have to update the FPM about this
673 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
674 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
680 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
682 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
686 * We are storing the rnh's associated withb
687 * the tracked nexthop as a list of the rn's.
688 * Unresolved rnh's are placed at the top
689 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
690 * As such for each rn we need to walk up the tree
691 * and see if any rnh's need to see if they
692 * would match a more specific route
695 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
696 char buf
[PREFIX_STRLEN
];
699 "%s: %s Being examined for Nexthop Tracking Count: %zd",
701 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
702 dest
? rnh_list_count(&dest
->nht
) : 0);
707 dest
= rib_dest_from_rnode(rn
);
711 * If we have any rnh's stored in the nht list
712 * then we know that this route node was used for
713 * nht resolution and as such we need to call the
714 * nexthop tracking evaluation code
716 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
717 struct zebra_vrf
*zvrf
=
718 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
719 struct prefix
*p
= &rnh
->node
->p
;
721 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
722 char buf1
[PREFIX_STRLEN
];
725 "%s(%u):%s has Nexthop(%pFX) Type: %s depending on it, evaluating %u:%u",
726 zvrf_name(zvrf
), zvrf_id(zvrf
),
727 srcdest_rnode2str(rn
, buf1
,
729 p
, rnh_type2str(rnh
->type
), seq
,
734 * If we have evaluated this node on this pass
735 * already, due to following the tree up
736 * then we know that we can move onto the next
739 * Additionally we call zebra_evaluate_rnh
740 * when we gc the dest. In this case we know
741 * that there must be no other re's where
742 * we were originally as such we know that
743 * that sequence number is ok to respect.
745 if (rnh
->seqno
== seq
) {
746 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
748 " Node processed and moved already");
753 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
759 dest
= rib_dest_from_rnode(rn
);
766 * Garbage collect the rib dest corresponding to the given route node
769 * Returns true if the dest was deleted, false otherwise.
771 int rib_gc_dest(struct route_node
*rn
)
775 dest
= rib_dest_from_rnode(rn
);
779 if (!rib_can_delete_dest(dest
))
782 if (IS_ZEBRA_DEBUG_RIB
) {
783 struct zebra_vrf
*zvrf
;
785 zvrf
= rib_dest_vrf(dest
);
786 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
789 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
792 rnh_list_fini(&dest
->nht
);
793 XFREE(MTYPE_RIB_DEST
, dest
);
797 * Release the one reference that we keep on the route node.
799 route_unlock_node(rn
);
803 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
804 struct route_entry
*new)
806 hook_call(rib_update
, rn
, "new route selected");
808 /* Update real nexthop. This may actually determine if nexthop is active
810 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
811 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
815 if (IS_ZEBRA_DEBUG_RIB
) {
816 char buf
[SRCDEST2STR_BUFFER
];
817 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
818 zlog_debug("%s(%u:%u):%s: Adding route rn %p, re %p (%s)",
819 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, buf
, rn
,
820 new, zebra_route_string(new->type
));
823 /* If labeled-unicast route, install transit LSP. */
824 if (zebra_rib_labeled_unicast(new))
825 zebra_mpls_lsp_install(zvrf
, rn
, new);
827 rib_install_kernel(rn
, new, NULL
);
829 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
832 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
833 struct route_entry
*old
)
835 hook_call(rib_update
, rn
, "removing existing route");
837 /* Uninstall from kernel. */
838 if (IS_ZEBRA_DEBUG_RIB
) {
839 char buf
[SRCDEST2STR_BUFFER
];
840 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
841 zlog_debug("%s(%u:%u):%s: Deleting route rn %p, re %p (%s)",
842 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, buf
, rn
,
843 old
, zebra_route_string(old
->type
));
846 /* If labeled-unicast route, uninstall transit LSP. */
847 if (zebra_rib_labeled_unicast(old
))
848 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
850 rib_uninstall_kernel(rn
, old
);
852 /* Update nexthop for route, reset changed flag. */
853 /* Note: this code also handles the Linux case when an interface goes
854 * down, causing the kernel to delete routes without sending DELROUTE
857 if (RIB_KERNEL_ROUTE(old
))
858 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
860 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
863 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
864 struct route_node
*rn
,
865 struct route_entry
*old
,
866 struct route_entry
*new)
871 * We have to install or update if a new route has been selected or
872 * something has changed.
874 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
875 hook_call(rib_update
, rn
, "updating existing route");
877 /* Update the nexthop; we could determine here that nexthop is
879 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
882 /* If nexthop is active, install the selected route, if
884 * the install succeeds, cleanup flags for prior route, if
889 if (IS_ZEBRA_DEBUG_RIB
) {
890 char buf
[SRCDEST2STR_BUFFER
];
891 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
894 "%s(%u:%u):%s: Updating route rn %p, re %p (%s) old %p (%s)",
895 zvrf_name(zvrf
), zvrf_id(zvrf
),
896 new->table
, buf
, rn
, new,
897 zebra_route_string(new->type
),
899 zebra_route_string(old
->type
));
902 "%s(%u:%u):%s: Updating route rn %p, re %p (%s)",
903 zvrf_name(zvrf
), zvrf_id(zvrf
),
904 new->table
, buf
, rn
, new,
905 zebra_route_string(new->type
));
908 /* If labeled-unicast route, uninstall transit LSP. */
909 if (zebra_rib_labeled_unicast(old
))
910 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
913 * Non-system route should be installed.
914 * If labeled-unicast route, install transit
917 if (zebra_rib_labeled_unicast(new))
918 zebra_mpls_lsp_install(zvrf
, rn
, new);
920 rib_install_kernel(rn
, new, old
);
924 * If nexthop for selected route is not active or install
926 * may need to uninstall and delete for redistribution.
929 if (IS_ZEBRA_DEBUG_RIB
) {
930 char buf
[SRCDEST2STR_BUFFER
];
931 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
934 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
935 zvrf_name(zvrf
), zvrf_id(zvrf
),
936 new->table
, buf
, rn
, new,
937 zebra_route_string(new->type
),
939 zebra_route_string(old
->type
));
942 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
943 zvrf_name(zvrf
), zvrf_id(zvrf
),
944 new->table
, buf
, rn
, new,
945 zebra_route_string(new->type
));
949 * When we have gotten to this point
950 * the new route entry has no nexthops
951 * that are usable and as such we need
952 * to remove the old route, but only
953 * if we were the one who installed
956 if (!RIB_SYSTEM_ROUTE(old
)) {
957 /* If labeled-unicast route, uninstall transit
959 if (zebra_rib_labeled_unicast(old
))
960 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
962 rib_uninstall_kernel(rn
, old
);
967 * Same route selected; check if in the FIB and if not,
968 * re-install. This is housekeeping code to deal with
969 * race conditions in kernel with linux netlink reporting
970 * interface up before IPv4 or IPv6 protocol is ready
973 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
974 RIB_SYSTEM_ROUTE(new))
975 rib_install_kernel(rn
, new, NULL
);
978 /* Update prior route. */
980 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
982 /* Clear changed flag. */
983 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
986 /* Check if 'alternate' RIB entry is better than 'current'. */
987 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
988 struct route_entry
*alternate
)
993 /* filter route selection in following order:
994 * - connected beats other types
995 * - if both connected, loopback or vrf wins
996 * - lower distance beats higher
997 * - lower metric beats higher for equal distance
998 * - last, hence oldest, route wins tie break.
1001 /* Connected routes. Check to see if either are a vrf
1002 * or loopback interface. If not, pick the last connected
1003 * route of the set of lowest metric connected routes.
1005 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1006 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1009 /* both are connected. are either loop or vrf? */
1010 struct nexthop
*nexthop
= NULL
;
1012 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
1013 struct interface
*ifp
= if_lookup_by_index(
1014 nexthop
->ifindex
, alternate
->vrf_id
);
1016 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1020 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
1021 struct interface
*ifp
= if_lookup_by_index(
1022 nexthop
->ifindex
, current
->vrf_id
);
1024 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1028 /* Neither are loop or vrf so pick best metric */
1029 if (alternate
->metric
<= current
->metric
)
1035 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1038 /* higher distance loses */
1039 if (alternate
->distance
< current
->distance
)
1041 if (current
->distance
< alternate
->distance
)
1044 /* metric tie-breaks equal distance */
1045 if (alternate
->metric
<= current
->metric
)
1051 /* Core function for processing nexthop group contexts's off metaq */
1052 static void rib_nhg_process(struct nhg_ctx
*ctx
)
1054 nhg_ctx_process(ctx
);
1057 /* Core function for processing routing information base. */
1058 static void rib_process(struct route_node
*rn
)
1060 struct route_entry
*re
;
1061 struct route_entry
*next
;
1062 struct route_entry
*old_selected
= NULL
;
1063 struct route_entry
*new_selected
= NULL
;
1064 struct route_entry
*old_fib
= NULL
;
1065 struct route_entry
*new_fib
= NULL
;
1066 struct route_entry
*best
= NULL
;
1067 char buf
[SRCDEST2STR_BUFFER
];
1069 struct zebra_vrf
*zvrf
= NULL
;
1071 const struct prefix
*p
, *src_p
;
1073 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1074 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1078 dest
= rib_dest_from_rnode(rn
);
1080 zvrf
= rib_dest_vrf(dest
);
1081 vrf_id
= zvrf_id(zvrf
);
1084 vrf
= vrf_lookup_by_id(vrf_id
);
1086 if (IS_ZEBRA_DEBUG_RIB
)
1087 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1090 * we can have rn's that have a NULL info pointer
1091 * (dest). As such let's not let the deref happen
1092 * additionally we know RNODE_FOREACH_RE_SAFE
1093 * will not iterate so we are ok.
1096 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1097 struct route_entry
*re
= re_list_first(&dest
->routes
);
1099 zlog_debug("%s(%u:%u):%s: Processing rn %p",
1100 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
,
1104 old_fib
= dest
->selected_fib
;
1107 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1108 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1109 char flags_buf
[128];
1110 char status_buf
[128];
1113 "%s(%u:%u):%s: Examine re %p (%s) status: %sflags: %sdist %d metric %d",
1114 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
, re
,
1115 zebra_route_string(re
->type
),
1116 _dump_re_status(re
, status_buf
,
1117 sizeof(status_buf
)),
1118 zclient_dump_route_flags(re
->flags
, flags_buf
,
1120 re
->distance
, re
->metric
);
1123 /* Currently selected re. */
1124 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1125 assert(old_selected
== NULL
);
1129 /* Skip deleted entries from selection */
1130 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1134 * If the route entry has changed, verify/resolve
1135 * the nexthops associated with the entry.
1137 * In any event if we have nexthops that are not active
1138 * then we cannot use this particular route entry so
1141 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
1142 if (!nexthop_active_update(rn
, re
)) {
1143 const struct prefix
*p
;
1144 struct rib_table_info
*info
;
1146 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1147 /* XXX: HERE BE DRAGONS!!!!!
1148 * In all honesty, I have not yet
1149 * figured out what this part does or
1150 * why the ROUTE_ENTRY_CHANGED test
1151 * above is correct or why we need to
1152 * delete a route here, and also not
1153 * whether this concerns both selected
1154 * and fib route, or only selected
1157 * This entry was denied by the 'ip
1159 * table' route-map, we need to delete
1161 if (re
!= old_selected
) {
1162 if (IS_ZEBRA_DEBUG_RIB
)
1164 "%s: %s(%u):%s: imported via import-table but denied by the ip protocol table route-map",
1171 SET_FLAG(re
->status
,
1172 ROUTE_ENTRY_REMOVED
);
1175 info
= srcdest_rnode_table_info(rn
);
1176 srcdest_rnode_prefixes(rn
, &p
, NULL
);
1177 zsend_route_notify_owner(re
, p
,
1178 ZAPI_ROUTE_FAIL_INSTALL
,
1179 info
->afi
, info
->safi
);
1184 * If the re has not changed and the nhg we have is
1185 * not usable, then we cannot use this route entry
1186 * for consideration, as that the route will just
1187 * not install if it is selected.
1189 if (!nexthop_group_active_nexthop_num(&re
->nhe
->nhg
))
1193 /* Infinite distance. */
1194 if (re
->distance
== DISTANCE_INFINITY
&&
1195 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1196 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1200 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1201 best
= rib_choose_best(new_fib
, re
);
1202 if (new_fib
&& best
!= new_fib
)
1203 UNSET_FLAG(new_fib
->status
,
1204 ROUTE_ENTRY_CHANGED
);
1207 best
= rib_choose_best(new_selected
, re
);
1208 if (new_selected
&& best
!= new_selected
)
1209 UNSET_FLAG(new_selected
->status
,
1210 ROUTE_ENTRY_CHANGED
);
1211 new_selected
= best
;
1214 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1215 } /* RNODE_FOREACH_RE */
1217 /* If no FIB override route, use the selected route also for FIB */
1218 if (new_fib
== NULL
)
1219 new_fib
= new_selected
;
1221 /* After the cycle is finished, the following pointers will be set:
1222 * old_selected --- RE entry currently having SELECTED
1223 * new_selected --- RE entry that is newly SELECTED
1224 * old_fib --- RE entry currently in kernel FIB
1225 * new_fib --- RE entry that is newly to be in kernel FIB
1227 * new_selected will get SELECTED flag, and is going to be redistributed
1228 * the zclients. new_fib (which can be new_selected) will be installed
1232 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1233 struct route_entry
*entry
;
1235 entry
= old_selected
1240 : new_fib
? new_fib
: NULL
;
1243 "%s(%u:%u):%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1244 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, buf
,
1245 (void *)old_selected
, (void *)new_selected
,
1246 (void *)old_fib
, (void *)new_fib
);
1249 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1250 * fib == selected */
1251 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1252 ROUTE_ENTRY_CHANGED
);
1254 /* Update fib according to selection results */
1255 if (new_fib
&& old_fib
)
1256 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1258 rib_process_add_fib(zvrf
, rn
, new_fib
);
1260 rib_process_del_fib(zvrf
, rn
, old_fib
);
1262 /* Update SELECTED entry */
1263 if (old_selected
!= new_selected
|| selected_changed
) {
1265 if (new_selected
&& new_selected
!= new_fib
)
1266 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1269 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1273 * If we're removing the old entry, we should tell
1274 * redist subscribers about that *if* they aren't
1275 * going to see a redist for the new entry.
1277 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1278 ROUTE_ENTRY_REMOVED
))
1279 redistribute_delete(p
, src_p
,
1283 if (old_selected
!= new_selected
)
1284 UNSET_FLAG(old_selected
->flags
,
1285 ZEBRA_FLAG_SELECTED
);
1289 /* Remove all RE entries queued for removal */
1290 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1291 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1292 if (IS_ZEBRA_DEBUG_RIB
) {
1293 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1294 (void *)rn
, (void *)re
);
1301 * Check if the dest can be deleted now.
1306 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1308 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1309 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1314 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1315 if (IS_ZEBRA_DEBUG_MPLS
)
1317 "%s(%u): Scheduling all LSPs upon RIB completion",
1318 zvrf_name(zvrf
), zvrf_id(zvrf
));
1319 zebra_mpls_lsp_schedule(zvrf
);
1320 mpls_unmark_lsps_for_processing(rn
);
1325 * Utility to match route with dplane context data
1327 static bool rib_route_match_ctx(const struct route_entry
*re
,
1328 const struct zebra_dplane_ctx
*ctx
,
1331 bool result
= false;
1335 * In 'update' case, we test info about the 'previous' or
1338 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1339 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1342 /* We use an extra test for statics, and another for
1345 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1346 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1347 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1349 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1351 dplane_ctx_get_old_metric(ctx
)) {
1358 * Ordinary, single-route case using primary context info
1360 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1361 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1362 /* Skip route that's been deleted */
1366 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1367 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1370 /* We use an extra test for statics, and another for
1373 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1374 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1375 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1377 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1378 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1380 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1381 result
= nexthop_group_equal_no_recurse(
1382 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1392 static void zebra_rib_fixup_system(struct route_node
*rn
)
1394 struct route_entry
*re
;
1396 RNODE_FOREACH_RE(rn
, re
) {
1397 struct nexthop
*nhop
;
1399 if (!RIB_SYSTEM_ROUTE(re
))
1402 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1405 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1406 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1408 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1409 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1412 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1417 /* Route comparison logic, with various special cases. */
1418 static bool rib_compare_routes(const struct route_entry
*re1
,
1419 const struct route_entry
*re2
)
1421 if (re1
->type
!= re2
->type
)
1424 if (re1
->instance
!= re2
->instance
)
1427 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1430 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1431 re1
->distance
!= re2
->distance
)
1434 /* We support multiple connected routes: this supports multiple
1435 * v6 link-locals, and we also support multiple addresses in the same
1436 * subnet on a single interface.
1438 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1445 * Compare nexthop lists from a route and a dplane context; test whether
1446 * the list installed in the FIB matches the route's list.
1447 * Set 'changed_p' to 'true' if there were changes to the route's
1448 * installed nexthops.
1450 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1453 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1454 const struct nexthop_group
*ctx_nhg
,
1457 bool matched_p
= true;
1458 struct nexthop
*nexthop
, *ctx_nexthop
;
1460 /* Get the first `installed` one to check against.
1461 * If the dataplane doesn't set these to be what was actually installed,
1462 * it will just be whatever was in re->nhe->nhg?
1464 ctx_nexthop
= ctx_nhg
->nexthop
;
1466 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1467 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1468 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1470 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1472 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1475 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1478 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1479 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1480 /* If the FIB doesn't know about the nexthop,
1481 * it's not installed
1483 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1484 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1485 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1487 (CHECK_FLAG(nexthop
->flags
,
1493 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1496 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1498 /* Keep checking nexthops */
1502 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1503 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1504 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1505 zlog_debug("%s: rib nh %pNHv -> installed",
1511 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1513 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1514 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1515 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1521 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1524 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1531 * Update a route from a dplane context. This consolidates common code
1532 * that can be used in processing of results from FIB updates, and in
1533 * async notification processing.
1534 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1536 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1537 struct route_node
*rn
,
1538 struct zebra_dplane_ctx
*ctx
)
1540 struct nexthop
*nexthop
;
1542 const struct nexthop_group
*ctxnhg
;
1543 struct nexthop_group
*re_nhg
;
1544 bool is_selected
= false; /* Is 're' currently the selected re? */
1545 bool changed_p
= false; /* Change to nexthops? */
1549 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1551 dest
= rib_dest_from_rnode(rn
);
1553 is_selected
= (re
== dest
->selected_fib
);
1555 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1556 zlog_debug("update_from_ctx: %s(%u:%u):%pRN: %sSELECTED, re %p",
1557 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1558 (is_selected
? "" : "NOT "), re
);
1560 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1561 * If the installed set differs from the set requested by the rib/owner,
1562 * we use the fib-specific nexthop-group to record the actual FIB
1566 ctxnhg
= dplane_ctx_get_ng(ctx
);
1568 /* Check route's fib group and incoming notif group for equivalence.
1570 * Let's assume the nexthops are ordered here to save time.
1572 /* TODO -- this isn't testing or comparing the FIB flags; we should
1573 * do a more explicit loop, checking the incoming notification's flags.
1575 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1576 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1579 /* If the new FIB set matches the existing FIB set, we're done. */
1581 if (IS_ZEBRA_DEBUG_RIB
)
1583 "%s(%u:%u):%pRN update_from_ctx(): existing fib nhg, no change",
1584 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1587 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1589 * Free stale fib list and move on to check the rib nhg.
1591 if (IS_ZEBRA_DEBUG_RIB
)
1593 "%s(%u:%u):%pRN update_from_ctx(): replacing fib nhg",
1594 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1595 nexthops_free(re
->fib_ng
.nexthop
);
1596 re
->fib_ng
.nexthop
= NULL
;
1598 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1600 /* Note that the installed nexthops have changed */
1603 if (IS_ZEBRA_DEBUG_RIB
)
1605 "%s(%u:%u):%pRN update_from_ctx(): no fib nhg",
1606 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
);
1610 * Compare with the rib nexthop group. The comparison here is different:
1611 * the RIB group may be a superset of the list installed in the FIB. We
1612 * walk the RIB group, looking for the 'installable' candidate
1613 * nexthops, and then check those against the set
1614 * that is actually installed.
1616 * Assume nexthops are ordered here as well.
1619 /* If nothing is installed, we can skip some of the checking/comparison
1622 if (ctxnhg
->nexthop
== NULL
) {
1627 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1629 /* If all nexthops were processed, we're done */
1631 if (IS_ZEBRA_DEBUG_RIB
)
1633 "%s(%u:%u):%pRN update_from_ctx(): rib nhg matched, changed '%s'",
1634 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1635 (changed_p
? "true" : "false"));
1641 /* FIB nexthop set differs from the RIB set:
1642 * create a fib-specific nexthop-group
1644 if (IS_ZEBRA_DEBUG_RIB
)
1646 "%s(%u:%u):%pRN update_from_ctx(): changed %s, adding new fib nhg%s",
1647 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, rn
,
1648 (changed_p
? "true" : "false"),
1649 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1651 /* Set the flag about the dedicated fib list */
1652 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1653 if (ctxnhg
->nexthop
)
1654 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1659 * Check the status of the route's backup nexthops, if any.
1660 * The logic for backups is somewhat different: if any backup is
1661 * installed, a new fib nhg will be attached to the route.
1663 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1665 goto done
; /* No backup nexthops */
1667 /* First check the route's 'fib' list of backups, if it's present
1668 * from some previous event.
1670 re_nhg
= &re
->fib_backup_ng
;
1671 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1674 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1677 /* If the new FIB set matches an existing FIB set, we're done. */
1679 if (IS_ZEBRA_DEBUG_RIB
)
1681 "%s(%u):%pRN update_from_ctx(): existing fib backup nhg, no change",
1682 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1685 } else if (re
->fib_backup_ng
.nexthop
) {
1687 * Free stale fib backup list and move on to check
1688 * the route's backups.
1690 if (IS_ZEBRA_DEBUG_RIB
)
1692 "%s(%u):%pRN update_from_ctx(): replacing fib backup nhg",
1693 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1694 nexthops_free(re
->fib_backup_ng
.nexthop
);
1695 re
->fib_backup_ng
.nexthop
= NULL
;
1697 /* Note that the installed nexthops have changed */
1700 if (IS_ZEBRA_DEBUG_RIB
)
1702 "%s(%u):%pRN update_from_ctx(): no fib backup nhg",
1703 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
);
1707 * If a FIB backup nexthop set exists, attach a copy
1708 * to the route if any backup is installed
1710 if (ctxnhg
&& ctxnhg
->nexthop
) {
1712 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1713 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1717 /* If no installed backups, we're done */
1718 if (nexthop
== NULL
)
1721 if (IS_ZEBRA_DEBUG_RIB
)
1723 "%s(%u):%pRN update_from_ctx(): changed %s, adding new backup fib nhg",
1724 VRF_LOGNAME(vrf
), re
->vrf_id
, rn
,
1725 (changed_p
? "true" : "false"));
1727 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1737 * Helper to locate a zebra route-node from a dplane context. This is used
1738 * when processing dplane results, e.g. Note well: the route-node is returned
1739 * with a ref held - route_unlock_node() must be called eventually.
1741 static struct route_node
*
1742 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1744 struct route_table
*table
= NULL
;
1745 struct route_node
*rn
= NULL
;
1746 const struct prefix
*dest_pfx
, *src_pfx
;
1748 /* Locate rn and re(s) from ctx */
1750 table
= zebra_vrf_lookup_table_with_table_id(
1751 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1752 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1753 if (table
== NULL
) {
1754 if (IS_ZEBRA_DEBUG_DPLANE
) {
1756 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1757 dplane_ctx_get_afi(ctx
),
1758 dplane_ctx_get_safi(ctx
),
1759 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1760 dplane_ctx_get_vrf(ctx
));
1765 dest_pfx
= dplane_ctx_get_dest(ctx
);
1766 src_pfx
= dplane_ctx_get_src(ctx
);
1768 rn
= srcdest_rnode_get(table
, dest_pfx
,
1769 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1778 * Route-update results processing after async dataplane update.
1780 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1782 struct zebra_vrf
*zvrf
= NULL
;
1784 struct route_node
*rn
= NULL
;
1785 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1786 bool is_update
= false;
1787 enum dplane_op_e op
;
1788 enum zebra_dplane_result status
;
1789 const struct prefix
*dest_pfx
, *src_pfx
;
1792 bool fib_changed
= false;
1793 struct rib_table_info
*info
;
1795 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1796 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1797 dest_pfx
= dplane_ctx_get_dest(ctx
);
1799 /* Locate rn and re(s) from ctx */
1800 rn
= rib_find_rn_from_ctx(ctx
);
1802 if (IS_ZEBRA_DEBUG_DPLANE
) {
1804 "Failed to process dplane results: no route for %s(%u):%pFX",
1805 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1811 dest
= rib_dest_from_rnode(rn
);
1812 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1813 info
= srcdest_rnode_table_info(rn
);
1815 op
= dplane_ctx_get_op(ctx
);
1816 status
= dplane_ctx_get_status(ctx
);
1818 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1820 "%s(%u:%u):%pFX Processing dplane result ctx %p, op %s result %s",
1821 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1822 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
,
1823 dplane_op2str(op
), dplane_res2str(status
));
1826 * Update is a bit of a special case, where we may have both old and new
1827 * routes to post-process.
1829 is_update
= dplane_ctx_is_update(ctx
);
1832 * Take a pass through the routes, look for matches with the context
1835 RNODE_FOREACH_RE(rn
, rib
) {
1838 if (rib_route_match_ctx(rib
, ctx
, false))
1842 /* Check for old route match */
1843 if (is_update
&& (old_re
== NULL
)) {
1844 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1848 /* Have we found the routes we need to work on? */
1849 if (re
&& ((!is_update
|| old_re
)))
1853 seq
= dplane_ctx_get_seq(ctx
);
1856 * Check sequence number(s) to detect stale results before continuing
1859 if (re
->dplane_sequence
!= seq
) {
1860 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1862 "%s(%u):%pFX Stale dplane result for re %p",
1864 dplane_ctx_get_vrf(ctx
), dest_pfx
, re
);
1866 if (!zrouter
.asic_offloaded
||
1867 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOADED
) ||
1868 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_OFFLOAD_FAILED
)))
1869 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1874 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1875 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1877 "%s(%u:%u):%pFX Stale dplane result for old_re %p",
1879 dplane_ctx_get_vrf(ctx
), old_re
->table
,
1882 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1886 case DPLANE_OP_ROUTE_INSTALL
:
1887 case DPLANE_OP_ROUTE_UPDATE
:
1888 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1890 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1891 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1894 * On an update operation from the same route type
1895 * context retrieval currently has no way to know
1896 * which was the old and which was the new.
1897 * So don't unset our flags that we just set.
1898 * We know redistribution is ok because the
1899 * old_re in this case is used for nothing
1900 * more than knowing whom to contact if necessary.
1902 if (old_re
&& old_re
!= re
) {
1903 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1904 UNSET_FLAG(old_re
->status
,
1905 ROUTE_ENTRY_INSTALLED
);
1908 /* Update zebra route based on the results in
1909 * the context struct.
1913 rib_update_re_from_ctx(re
, rn
, ctx
);
1916 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1918 "%s(%u:%u):%pFX no fib change for re",
1920 dplane_ctx_get_vrf(ctx
),
1921 dplane_ctx_get_table(
1926 /* Redistribute if this is the selected re */
1927 if (dest
&& re
== dest
->selected_fib
)
1928 redistribute_update(dest_pfx
, src_pfx
,
1933 * System routes are weird in that they
1934 * allow multiple to be installed that match
1935 * to the same prefix, so after we get the
1936 * result we need to clean them up so that
1937 * we can actually use them.
1939 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1940 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1941 zebra_rib_fixup_system(rn
);
1946 /* Notify route owner */
1947 if (zebra_router_notify_on_ack())
1948 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1951 if (CHECK_FLAG(re
->flags
,
1952 ZEBRA_FLAG_OFFLOADED
))
1953 zsend_route_notify_owner_ctx(
1955 ZAPI_ROUTE_INSTALLED
);
1958 ZEBRA_FLAG_OFFLOAD_FAILED
))
1959 zsend_route_notify_owner_ctx(
1961 ZAPI_ROUTE_FAIL_INSTALL
);
1966 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1967 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1969 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1971 zsend_route_notify_owner(re
, dest_pfx
,
1972 ZAPI_ROUTE_FAIL_INSTALL
,
1973 info
->afi
, info
->safi
);
1975 zlog_warn("%s(%u:%u):%pFX: Route install failed",
1976 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1977 dplane_ctx_get_table(ctx
), dest_pfx
);
1980 case DPLANE_OP_ROUTE_DELETE
:
1982 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1984 * In the delete case, the zebra core datastructs were
1985 * updated (or removed) at the time the delete was issued,
1986 * so we're just notifying the route owner.
1988 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1990 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1991 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1993 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1999 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
2000 zsend_route_notify_owner_ctx(ctx
,
2001 ZAPI_ROUTE_REMOVE_FAIL
);
2003 zlog_warn("%s(%u:%u):%pFX: Route Deletion failure",
2004 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2005 dplane_ctx_get_table(ctx
), dest_pfx
);
2009 * System routes are weird in that they
2010 * allow multiple to be installed that match
2011 * to the same prefix, so after we get the
2012 * result we need to clean them up so that
2013 * we can actually use them.
2015 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
2016 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
2017 zebra_rib_fixup_system(rn
);
2023 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
2024 zebra_rib_evaluate_mpls(rn
);
2028 route_unlock_node(rn
);
2030 /* Return context to dataplane module */
2031 dplane_ctx_fini(&ctx
);
2035 * Count installed/FIB nexthops
2037 static int rib_count_installed_nh(struct route_entry
*re
)
2040 struct nexthop
*nexthop
;
2041 struct nexthop_group
*nhg
;
2043 nhg
= rib_get_fib_nhg(re
);
2045 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2046 /* The meaningful flag depends on where the installed
2049 if (nhg
== &(re
->fib_ng
)) {
2050 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2053 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
2058 nhg
= rib_get_fib_backup_nhg(re
);
2060 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2061 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2070 * Handle notification from async dataplane: the dataplane has detected
2071 * some change to a route, and notifies zebra so that the control plane
2072 * can reflect that change.
2074 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2076 struct route_node
*rn
= NULL
;
2077 struct route_entry
*re
= NULL
;
2079 struct nexthop
*nexthop
;
2080 const struct prefix
*dest_pfx
, *src_pfx
;
2082 bool fib_changed
= false;
2083 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2084 int start_count
, end_count
;
2085 dest_pfx
= dplane_ctx_get_dest(ctx
);
2086 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2088 /* Locate rn and re(s) from ctx */
2089 rn
= rib_find_rn_from_ctx(ctx
);
2093 "Failed to process dplane notification: no routes for %s(%u:%u):%pFX",
2094 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2095 dplane_ctx_get_table(ctx
), dest_pfx
);
2100 dest
= rib_dest_from_rnode(rn
);
2101 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
2104 zlog_debug("%s(%u:%u):%pFX Processing dplane notif ctx %p",
2105 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2106 dplane_ctx_get_table(ctx
), dest_pfx
, ctx
);
2109 * Take a pass through the routes, look for matches with the context
2112 RNODE_FOREACH_RE(rn
, re
) {
2113 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2117 /* No match? Nothing we can do */
2121 "%s(%u:%u):%pFX Unable to process dplane notification: no entry for type %s",
2122 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2123 dplane_ctx_get_table(ctx
), dest_pfx
,
2124 zebra_route_string(dplane_ctx_get_type(ctx
)));
2129 /* Ensure we clear the QUEUED flag */
2130 if (!zrouter
.asic_offloaded
)
2131 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2133 /* Is this a notification that ... matters? We mostly care about
2134 * the route that is currently selected for installation; we may also
2135 * get an un-install notification, and handle that too.
2137 if (re
!= dest
->selected_fib
) {
2139 * If we need to, clean up after a delete that was part of
2140 * an update operation.
2143 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2144 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2148 /* If no nexthops or none installed, ensure that this re
2149 * gets its 'installed' flag cleared.
2151 if (end_count
== 0) {
2152 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2153 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2156 "%s(%u:%u):%pFX dplane notif, uninstalled type %s route",
2158 dplane_ctx_get_vrf(ctx
),
2159 dplane_ctx_get_table(ctx
), dest_pfx
,
2161 dplane_ctx_get_type(ctx
)));
2163 /* At least report on the event. */
2166 "%s(%u:%u):%pFX dplane notif, but type %s not selected_fib",
2168 dplane_ctx_get_vrf(ctx
),
2169 dplane_ctx_get_table(ctx
), dest_pfx
,
2171 dplane_ctx_get_type(ctx
)));
2176 /* We'll want to determine whether the installation status of the
2177 * route has changed: we'll check the status before processing,
2178 * and then again if there's been a change.
2182 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2183 start_count
= rib_count_installed_nh(re
);
2185 /* Update zebra's nexthop FIB flags based on the context struct's
2188 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2193 "%s(%u:%u):%pFX dplane notification: rib_update returns FALSE",
2194 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2195 dplane_ctx_get_table(ctx
), dest_pfx
);
2199 * Perform follow-up work if the actual status of the prefix
2202 end_count
= rib_count_installed_nh(re
);
2204 /* Various fib transitions: changed nexthops; from installed to
2205 * not-installed; or not-installed to installed.
2207 if (start_count
> 0 && end_count
> 0) {
2210 "%s(%u:%u):%pFX applied nexthop changes from dplane notification",
2211 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2212 dplane_ctx_get_table(ctx
), dest_pfx
);
2214 /* Changed nexthops - update kernel/others */
2215 dplane_route_notif_update(rn
, re
,
2216 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2218 } else if (start_count
== 0 && end_count
> 0) {
2221 "%s(%u:%u):%pFX installed transition from dplane notification",
2222 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2223 dplane_ctx_get_table(ctx
), dest_pfx
);
2225 /* We expect this to be the selected route, so we want
2226 * to tell others about this transition.
2228 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2230 /* Changed nexthops - update kernel/others */
2231 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2233 /* Redistribute, lsp, and nht update */
2234 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2236 } else if (start_count
> 0 && end_count
== 0) {
2239 "%s(%u:%u):%pFX un-installed transition from dplane notification",
2240 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2241 dplane_ctx_get_table(ctx
), dest_pfx
);
2243 /* Transition from _something_ installed to _nothing_
2246 /* We expect this to be the selected route, so we want
2247 * to tell others about this transistion.
2249 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2251 /* Changed nexthops - update kernel/others */
2252 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2254 /* Redistribute, lsp, and nht update */
2255 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
2258 /* Make any changes visible for lsp and nexthop-tracking processing */
2259 zebra_rib_evaluate_rn_nexthops(
2260 rn
, zebra_router_get_next_sequence());
2262 zebra_rib_evaluate_mpls(rn
);
2266 route_unlock_node(rn
);
2268 /* Return context to dataplane module */
2269 dplane_ctx_fini(&ctx
);
2272 static void process_subq_nhg(struct listnode
*lnode
)
2274 struct nhg_ctx
*ctx
= NULL
;
2275 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2277 ctx
= listgetdata(lnode
);
2282 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2283 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
2286 rib_nhg_process(ctx
);
2289 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2291 struct route_node
*rnode
= NULL
;
2292 rib_dest_t
*dest
= NULL
;
2293 struct zebra_vrf
*zvrf
= NULL
;
2295 rnode
= listgetdata(lnode
);
2296 dest
= rib_dest_from_rnode(rnode
);
2299 zvrf
= rib_dest_vrf(dest
);
2303 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2304 struct route_entry
*re
= NULL
;
2305 char buf
[SRCDEST2STR_BUFFER
];
2308 * rib_process may have freed the dest
2309 * as part of the garbage collection. Let's
2310 * prevent stupidity from happening.
2312 dest
= rib_dest_from_rnode(rnode
);
2314 re
= re_list_first(&dest
->routes
);
2316 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2317 zlog_debug("%s(%u:%u):%s: rn %p dequeued from sub-queue %u",
2318 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0, buf
,
2323 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2324 RIB_ROUTE_QUEUED(qindex
));
2326 route_unlock_node(rnode
);
2329 /* Take a list of route_node structs and return 1, if there was a record
2330 * picked from it and processed by rib_process(). Don't process more,
2331 * than one RN record; operate only in the specified sub-queue.
2333 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2335 struct listnode
*lnode
= listhead(subq
);
2340 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2341 process_subq_nhg(lnode
);
2343 process_subq_route(lnode
, qindex
);
2345 list_delete_node(subq
, lnode
);
2350 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2351 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2353 * is pointed to the meta queue structure.
2355 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2357 struct meta_queue
*mq
= data
;
2359 uint32_t queue_len
, queue_limit
;
2361 /* Ensure there's room for more dataplane updates */
2362 queue_limit
= dplane_get_in_queue_limit();
2363 queue_len
= dplane_get_in_queue_len();
2364 if (queue_len
> queue_limit
) {
2365 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2366 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2367 queue_len
, queue_limit
);
2369 /* Ensure that the meta-queue is actually enqueued */
2370 if (work_queue_empty(zrouter
.ribq
))
2371 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2373 return WQ_QUEUE_BLOCKED
;
2376 for (i
= 0; i
< MQ_SIZE
; i
++)
2377 if (process_subq(mq
->subq
[i
], i
)) {
2381 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2386 * Look into the RN and queue it into the highest priority queue
2387 * at this point in time for processing.
2389 * We will enqueue a route node only once per invocation.
2391 * There are two possibilities here that should be kept in mind.
2392 * If the original invocation has not been pulled off for processing
2393 * yet, A subsuquent invocation can have a route entry with a better
2394 * meta queue index value and we can have a situation where
2395 * we might have the same node enqueued 2 times. Not necessarily
2396 * an optimal situation but it should be ok.
2398 * The other possibility is that the original invocation has not
2399 * been pulled off for processing yet, A subsusquent invocation
2400 * doesn't have a route_entry with a better meta-queue and the
2401 * original metaqueue index value will win and we'll end up with
2402 * the route node enqueued once.
2404 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2406 struct route_node
*rn
= NULL
;
2407 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2408 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2410 rn
= (struct route_node
*)data
;
2412 RNODE_FOREACH_RE (rn
, curr_re
) {
2413 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2415 if (curr_qindex
<= qindex
) {
2417 qindex
= curr_qindex
;
2424 /* Invariant: at this point we always have rn->info set. */
2425 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2426 RIB_ROUTE_QUEUED(qindex
))) {
2427 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2428 rnode_debug(rn
, re
->vrf_id
,
2429 "rn %p is already queued in sub-queue %u",
2430 (void *)rn
, qindex
);
2434 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2435 listnode_add(mq
->subq
[qindex
], rn
);
2436 route_lock_node(rn
);
2439 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2440 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2441 (void *)rn
, qindex
);
2446 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2448 struct nhg_ctx
*ctx
= NULL
;
2449 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2451 ctx
= (struct nhg_ctx
*)data
;
2456 listnode_add(mq
->subq
[qindex
], ctx
);
2459 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2460 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2466 static int mq_add_handler(void *data
,
2467 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2469 if (zrouter
.ribq
== NULL
) {
2470 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2471 "%s: work_queue does not exist!", __func__
);
2476 * The RIB queue should normally be either empty or holding the only
2477 * work_queue_item element. In the latter case this element would
2478 * hold a pointer to the meta queue structure, which must be used to
2479 * actually queue the route nodes to process. So create the MQ
2480 * holder, if necessary, then push the work into it in any case.
2481 * This semantics was introduced after 0.99.9 release.
2483 if (work_queue_empty(zrouter
.ribq
))
2484 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2486 return mq_add_func(zrouter
.mq
, data
);
2489 /* Add route_node to work queue and schedule processing */
2490 int rib_queue_add(struct route_node
*rn
)
2494 /* Pointless to queue a route_node with no RIB entries to add or remove
2496 if (!rnode_to_ribs(rn
)) {
2497 zlog_debug("%s: called for route_node (%p, %u) with no ribs",
2498 __func__
, (void *)rn
, route_node_get_lock_count(rn
));
2499 zlog_backtrace(LOG_DEBUG
);
2503 return mq_add_handler(rn
, &rib_meta_queue_add
);
2506 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2510 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2513 /* Create new meta queue.
2514 A destructor function doesn't seem to be necessary here.
2516 static struct meta_queue
*meta_queue_new(void)
2518 struct meta_queue
*new;
2521 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2523 for (i
= 0; i
< MQ_SIZE
; i
++) {
2524 new->subq
[i
] = list_new();
2525 assert(new->subq
[i
]);
2531 void meta_queue_free(struct meta_queue
*mq
)
2535 for (i
= 0; i
< MQ_SIZE
; i
++)
2536 list_delete(&mq
->subq
[i
]);
2538 XFREE(MTYPE_WORK_QUEUE
, mq
);
2541 /* initialise zebra rib work queue */
2542 static void rib_queue_init(void)
2544 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2545 "route_node processing"))) {
2546 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2547 "%s: could not initialise work queue!", __func__
);
2551 /* fill in the work queue spec */
2552 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2553 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2554 zrouter
.ribq
->spec
.completion_func
= NULL
;
2555 /* XXX: TODO: These should be runtime configurable via vty */
2556 zrouter
.ribq
->spec
.max_retries
= 3;
2557 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2558 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2560 if (!(zrouter
.mq
= meta_queue_new())) {
2561 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2562 "%s: could not initialise meta queue!", __func__
);
2568 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2572 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2573 rnh_list_init(&dest
->nht
);
2574 re_list_init(&dest
->routes
);
2575 route_lock_node(rn
); /* rn route table reference */
2582 /* RIB updates are processed via a queue of pointers to route_nodes.
2584 * The queue length is bounded by the maximal size of the routing table,
2585 * as a route_node will not be requeued, if already queued.
2587 * REs are submitted via rib_addnode or rib_delnode which set minimal
2588 * state, or static_install_route (when an existing RE is updated)
2589 * and then submit route_node to queue for best-path selection later.
2590 * Order of add/delete state changes are preserved for any given RE.
2592 * Deleted REs are reaped during best-path selection.
2595 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2596 * |-------->| | best RE, if required
2598 * static_install->|->rib_addqueue...... -> rib_process
2600 * |-------->| |-> rib_unlink
2601 * |-> set ROUTE_ENTRY_REMOVE |
2602 * rib_delnode (RE freed)
2604 * The 'info' pointer of a route_node points to a rib_dest_t
2605 * ('dest'). Queueing state for a route_node is kept on the dest. The
2606 * dest is created on-demand by rib_link() and is kept around at least
2607 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2609 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2611 * - route_nodes: refcounted by:
2612 * - dest attached to route_node:
2613 * - managed by: rib_link/rib_gc_dest
2614 * - route_node processing queue
2615 * - managed by: rib_addqueue, rib_process.
2619 /* Add RE to head of the route node. */
2620 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2624 const char *rmap_name
;
2628 dest
= rib_dest_from_rnode(rn
);
2630 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2631 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2633 dest
= zebra_rib_create_dest(rn
);
2636 re_list_add_head(&dest
->routes
, re
);
2638 afi
= (rn
->p
.family
== AF_INET
)
2640 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2641 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2642 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2644 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2645 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2650 static void rib_addnode(struct route_node
*rn
,
2651 struct route_entry
*re
, int process
)
2653 /* RE node has been un-removed before route-node is processed.
2654 * route_node must hence already be on the queue for processing..
2656 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2657 if (IS_ZEBRA_DEBUG_RIB
)
2658 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2659 (void *)rn
, (void *)re
);
2661 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2664 rib_link(rn
, re
, process
);
2670 * Detach a rib structure from a route_node.
2672 * Note that a call to rib_unlink() should be followed by a call to
2673 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2674 * longer required to be deleted.
2676 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2682 if (IS_ZEBRA_DEBUG_RIB
)
2683 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2686 dest
= rib_dest_from_rnode(rn
);
2688 re_list_del(&dest
->routes
, re
);
2690 if (dest
->selected_fib
== re
)
2691 dest
->selected_fib
= NULL
;
2693 if (re
->nhe
&& re
->nhe_id
) {
2694 assert(re
->nhe
->id
== re
->nhe_id
);
2695 zebra_nhg_decrement_ref(re
->nhe
);
2696 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2697 nexthops_free(re
->nhe
->nhg
.nexthop
);
2699 nexthops_free(re
->fib_ng
.nexthop
);
2701 XFREE(MTYPE_OPAQUE
, re
->opaque
);
2703 XFREE(MTYPE_RE
, re
);
2706 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2710 if (IS_ZEBRA_DEBUG_RIB
)
2711 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2712 (void *)rn
, (void *)re
);
2713 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2715 afi
= (rn
->p
.family
== AF_INET
)
2717 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2718 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2719 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2721 zebra_del_import_table_entry(zvrf
, rn
, re
);
2722 /* Just clean up if non main table */
2723 if (IS_ZEBRA_DEBUG_RIB
) {
2724 char buf
[SRCDEST2STR_BUFFER
];
2725 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2726 zlog_debug("%s(%u):%s: Freeing route rn %p, re %p (%s)",
2727 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, buf
,
2728 rn
, re
, zebra_route_string(re
->type
));
2738 * Helper that debugs a single nexthop within a route-entry
2740 static void _route_entry_dump_nh(const struct route_entry
*re
,
2741 const char *straddr
,
2742 const struct nexthop
*nexthop
)
2744 char nhname
[PREFIX_STRLEN
];
2745 char backup_str
[50];
2749 struct interface
*ifp
;
2750 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2752 switch (nexthop
->type
) {
2753 case NEXTHOP_TYPE_BLACKHOLE
:
2754 snprintf(nhname
, sizeof(nhname
), "Blackhole");
2756 case NEXTHOP_TYPE_IFINDEX
:
2757 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2758 snprintf(nhname
, sizeof(nhname
), "%s",
2759 ifp
? ifp
->name
: "Unknown");
2761 case NEXTHOP_TYPE_IPV4
:
2763 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2764 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2766 case NEXTHOP_TYPE_IPV6
:
2767 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2768 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2772 backup_str
[0] = '\0';
2773 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
2774 snprintf(backup_str
, sizeof(backup_str
), "backup ");
2775 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
2776 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
2777 nexthop
->backup_idx
[i
]);
2778 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
2783 if (nexthop
->weight
)
2784 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
2786 zlog_debug("%s: %s %s[%u] vrf %s(%u) %s%s with flags %s%s%s%s%s%s%s%s",
2787 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2788 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2790 wgt_str
, backup_str
,
2791 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2794 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2797 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2800 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2803 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2806 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RNH_FILTERED
)
2807 ? "FILTERED " : ""),
2808 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)
2810 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_SRTE
)
2815 /* This function dumps the contents of a given RE entry into
2816 * standard debug log. Calling function name and IP prefix in
2817 * question are passed as 1st and 2nd arguments.
2819 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2820 union prefixconstptr src_pp
,
2821 const struct route_entry
*re
)
2823 const struct prefix
*src_p
= src_pp
.p
;
2824 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2825 char straddr
[PREFIX_STRLEN
];
2826 char srcaddr
[PREFIX_STRLEN
];
2827 char flags_buf
[128];
2828 char status_buf
[128];
2829 struct nexthop
*nexthop
;
2830 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
2831 struct nexthop_group
*nhg
;
2833 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
2834 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2835 is_srcdst
? " from " : "",
2836 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2838 VRF_LOGNAME(vrf
), re
->vrf_id
);
2839 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2840 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2843 "%s: metric == %u, mtu == %u, distance == %u, flags == %sstatus == %s",
2844 straddr
, re
->metric
, re
->mtu
, re
->distance
,
2845 zclient_dump_route_flags(re
->flags
, flags_buf
,
2847 _dump_re_status(re
, status_buf
, sizeof(status_buf
)));
2848 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2849 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
2850 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
2853 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
2854 _route_entry_dump_nh(re
, straddr
, nexthop
);
2856 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
2857 zlog_debug("%s: backup nexthops:", straddr
);
2859 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
2860 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
2861 _route_entry_dump_nh(re
, straddr
, nexthop
);
2864 zlog_debug("%s: dump complete", straddr
);
2868 * This is an exported helper to rtm_read() to dump the strange
2869 * RE entry found by rib_lookup_ipv4_route()
2871 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2873 struct route_table
*table
;
2874 struct route_node
*rn
;
2875 struct route_entry
*re
;
2878 vrf
= vrf_lookup_by_id(vrf_id
);
2881 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2883 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2884 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2885 VRF_LOGNAME(vrf
), vrf_id
);
2889 /* Scan the RIB table for exactly matching RE entry. */
2890 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2892 /* No route for this prefix. */
2894 zlog_debug("%s:%s(%u) lookup failed for %pFX", __func__
,
2895 VRF_LOGNAME(vrf
), vrf_id
, (struct prefix
*)p
);
2900 route_unlock_node(rn
);
2903 RNODE_FOREACH_RE (rn
, re
) {
2904 zlog_debug("%s:%s(%u) rn %p, re %p: %s, %s", __func__
,
2905 VRF_LOGNAME(vrf
), vrf_id
, (void *)rn
, (void *)re
,
2906 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2909 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2912 route_entry_dump(p
, NULL
, re
);
2916 /* Check if requested address assignment will fail due to another
2917 * route being installed by zebra in FIB already. Take necessary
2918 * actions, if needed: remove such a route from FIB and deSELECT
2919 * corresponding RE entry. Then put affected RN into RIBQ head.
2921 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2923 struct route_table
*table
;
2924 struct route_node
*rn
;
2927 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2928 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2930 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2931 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2932 VRF_LOGNAME(vrf
), vrf_id
);
2936 /* No matches would be the simplest case. */
2937 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2941 route_unlock_node(rn
);
2943 dest
= rib_dest_from_rnode(rn
);
2944 /* Check all RE entries. In case any changes have to be done, requeue
2945 * the RN into RIBQ head. If the routing message about the new connected
2946 * route (generated by the IP address we are going to assign very soon)
2947 * comes before the RIBQ is processed, the new RE entry will join
2948 * RIBQ record already on head. This is necessary for proper
2950 * of the rest of the RE.
2952 if (dest
->selected_fib
) {
2953 if (IS_ZEBRA_DEBUG_RIB
) {
2955 vrf_lookup_by_id(dest
->selected_fib
->vrf_id
);
2958 "%s(%u):%pFX: freeing way for connected prefix",
2959 VRF_LOGNAME(vrf
), dest
->selected_fib
->vrf_id
,
2961 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2963 rib_uninstall(rn
, dest
->selected_fib
);
2969 * Internal route-add implementation; there are a couple of different public
2970 * signatures. Callers in this path are responsible for the memory they
2971 * allocate: if they allocate a nexthop_group or backup nexthop info, they
2972 * must free those objects. If this returns < 0, an error has occurred and the
2973 * route_entry 're' has not been captured; the caller should free that also.
2975 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
2976 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
2977 struct nhg_hash_entry
*re_nhe
)
2979 struct nhg_hash_entry
*nhe
= NULL
;
2980 struct route_table
*table
;
2981 struct route_node
*rn
;
2982 struct route_entry
*same
= NULL
, *first_same
= NULL
;
2990 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2993 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2998 if (re
->nhe_id
> 0) {
2999 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
3003 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3004 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
3010 /* Lookup nhe from route information */
3011 nhe
= zebra_nhg_rib_find_nhe(re_nhe
, afi
);
3013 char buf2
[PREFIX_STRLEN
] = "";
3016 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
3017 "Zebra failed to find or create a nexthop hash entry for %pFX%s%s",
3018 p
, src_p
? " from " : "",
3019 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
3027 * Attach the re to the nhe's nexthop group.
3029 * TODO: This will need to change when we start getting IDs from upper
3030 * level protocols, as the refcnt might be wrong, since it checks
3031 * if old_id != new_id.
3033 route_entry_update_nhe(re
, nhe
);
3035 /* Make it sure prefixlen is applied to the prefix. */
3038 apply_mask_ipv6(src_p
);
3040 /* Set default distance by route type. */
3041 if (re
->distance
== 0)
3042 re
->distance
= route_distance(re
->type
);
3044 /* Lookup route node.*/
3045 rn
= srcdest_rnode_get(table
, p
, src_p
);
3048 * If same type of route are installed, treat it as a implicit
3049 * withdraw. If the user has specified the No route replace semantics
3050 * for the install don't do a route replace.
3052 RNODE_FOREACH_RE (rn
, same
) {
3053 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
)) {
3058 /* Compare various route_entry properties */
3059 if (rib_compare_routes(re
, same
)) {
3062 if (first_same
== NULL
)
3069 /* If this route is kernel/connected route, notify the dataplane. */
3070 if (RIB_SYSTEM_ROUTE(re
)) {
3071 /* Notify dataplane */
3072 dplane_sys_route_add(rn
, re
);
3075 /* Link new re to node.*/
3076 if (IS_ZEBRA_DEBUG_RIB
) {
3077 rnode_debug(rn
, re
->vrf_id
,
3078 "Inserting route rn %p, re %p (%s) existing %p, same_count %d",
3079 rn
, re
, zebra_route_string(re
->type
), same
,
3082 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3083 route_entry_dump(p
, src_p
, re
);
3086 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3087 rib_addnode(rn
, re
, 1);
3090 /* Free implicit route.*/
3092 rib_delnode(rn
, same
);
3094 /* See if we can remove some RE entries that are queued for
3095 * removal, but won't be considered in rib processing.
3097 dest
= rib_dest_from_rnode(rn
);
3098 RNODE_FOREACH_RE_SAFE (rn
, re
, same
) {
3099 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
3100 /* If the route was used earlier, must retain it. */
3101 if (dest
&& re
== dest
->selected_fib
)
3104 if (IS_ZEBRA_DEBUG_RIB
)
3105 rnode_debug(rn
, re
->vrf_id
, "rn %p, removing unneeded re %p",
3112 route_unlock_node(rn
);
3117 * Add a single route.
3119 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
3120 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3121 struct nexthop_group
*ng
)
3124 struct nhg_hash_entry nhe
;
3129 /* We either need nexthop(s) or an existing nexthop id */
3130 if (ng
== NULL
&& re
->nhe_id
== 0)
3134 * Use a temporary nhe to convey info to the common/main api.
3136 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
3138 nhe
.nhg
.nexthop
= ng
->nexthop
;
3139 else if (re
->nhe_id
> 0)
3140 nhe
.id
= re
->nhe_id
;
3142 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, &nhe
);
3144 /* In this path, the callers expect memory to be freed. */
3145 nexthop_group_delete(&ng
);
3147 /* In error cases, free the route also */
3149 XFREE(MTYPE_RE
, re
);
3154 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3155 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3156 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3157 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
3158 uint8_t distance
, bool fromkernel
)
3160 struct route_table
*table
;
3161 struct route_node
*rn
;
3162 struct route_entry
*re
;
3163 struct route_entry
*fib
= NULL
;
3164 struct route_entry
*same
= NULL
;
3165 struct nexthop
*rtnh
;
3166 char buf2
[INET6_ADDRSTRLEN
];
3169 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3172 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
3180 apply_mask_ipv6(src_p
);
3182 /* Lookup route node. */
3183 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
3185 if (IS_ZEBRA_DEBUG_RIB
) {
3186 char src_buf
[PREFIX_STRLEN
];
3187 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3189 if (src_p
&& src_p
->prefixlen
)
3190 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
3194 zlog_debug("%s[%d]:%pFX%s%s doesn't exist in rib",
3195 vrf
->name
, table_id
, p
,
3196 (src_buf
[0] != '\0') ? " from " : "",
3202 dest
= rib_dest_from_rnode(rn
);
3203 fib
= dest
->selected_fib
;
3205 /* Lookup same type route. */
3206 RNODE_FOREACH_RE (rn
, re
) {
3207 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3210 if (re
->type
!= type
)
3212 if (re
->instance
!= instance
)
3214 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
3215 distance
!= re
->distance
)
3218 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
3220 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
3221 (rtnh
= re
->nhe
->nhg
.nexthop
)
3222 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
3223 if (rtnh
->ifindex
!= nh
->ifindex
)
3229 /* Make sure that the route found has the same gateway. */
3230 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
3239 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3241 * No guarantee all kernel send nh with labels
3244 if (nexthop_same_no_labels(rtnh
, nh
)) {
3253 /* If same type of route can't be found and this message is from
3257 * In the past(HA!) we could get here because
3258 * we were receiving a route delete from the
3259 * kernel and we're not marking the proto
3260 * as coming from it's appropriate originator.
3261 * Now that we are properly noticing the fact
3262 * that the kernel has deleted our route we
3263 * are not going to get called in this path
3264 * I am going to leave this here because
3265 * this might still work this way on non-linux
3266 * platforms as well as some weird state I have
3267 * not properly thought of yet.
3268 * If we can show that this code path is
3269 * dead then we can remove it.
3271 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
3272 if (IS_ZEBRA_DEBUG_RIB
) {
3273 rnode_debug(rn
, vrf_id
,
3274 "rn %p, re %p (%s) was deleted from kernel, adding",
3276 zebra_route_string(fib
->type
));
3279 || CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3280 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3282 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3284 UNSET_FLAG(rtnh
->flags
,
3288 * This is a non FRR route
3289 * as such we should mark
3292 dest
->selected_fib
= NULL
;
3294 /* This means someone else, other than Zebra,
3296 * a Zebra router from the kernel. We will add
3298 rib_install_kernel(rn
, fib
, NULL
);
3301 if (IS_ZEBRA_DEBUG_RIB
) {
3305 "via %s ifindex %d type %d doesn't exist in rib",
3306 inet_ntop(afi2family(afi
),
3313 "type %d doesn't exist in rib",
3316 route_unlock_node(rn
);
3322 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
3324 rib_install_kernel(rn
, same
, NULL
);
3325 route_unlock_node(rn
);
3330 /* Special handling for IPv4 or IPv6 routes sourced from
3331 * EVPN - the nexthop (and associated MAC) need to be
3332 * uninstalled if no more refs.
3334 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
3335 struct nexthop
*tmp_nh
;
3337 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3338 struct ipaddr vtep_ip
;
3340 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3341 if (afi
== AFI_IP
) {
3342 vtep_ip
.ipa_type
= IPADDR_V4
;
3343 memcpy(&(vtep_ip
.ipaddr_v4
),
3344 &(tmp_nh
->gate
.ipv4
),
3345 sizeof(struct in_addr
));
3347 vtep_ip
.ipa_type
= IPADDR_V6
;
3348 memcpy(&(vtep_ip
.ipaddr_v6
),
3349 &(tmp_nh
->gate
.ipv6
),
3350 sizeof(struct in6_addr
));
3352 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
3357 /* Notify dplane if system route changes */
3358 if (RIB_SYSTEM_ROUTE(re
))
3359 dplane_sys_route_del(rn
, same
);
3361 rib_delnode(rn
, same
);
3364 route_unlock_node(rn
);
3369 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3370 unsigned short instance
, uint32_t flags
, struct prefix
*p
,
3371 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3372 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3373 uint8_t distance
, route_tag_t tag
)
3375 struct route_entry
*re
= NULL
;
3376 struct nexthop
*nexthop
= NULL
;
3377 struct nexthop_group
*ng
= NULL
;
3379 /* Allocate new route_entry structure. */
3380 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3382 re
->instance
= instance
;
3383 re
->distance
= distance
;
3385 re
->metric
= metric
;
3387 re
->table
= table_id
;
3388 re
->vrf_id
= vrf_id
;
3389 re
->uptime
= monotime(NULL
);
3391 re
->nhe_id
= nhe_id
;
3393 /* If the owner of the route supplies a shared nexthop-group id,
3394 * we'll use that. Otherwise, pass the nexthop along directly.
3397 ng
= nexthop_group_new();
3400 nexthop
= nexthop_new();
3402 nexthop_group_add_sorted(ng
, nexthop
);
3405 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
3408 static const char *rib_update_event2str(enum rib_update_event event
)
3410 const char *ret
= "UNKNOWN";
3413 case RIB_UPDATE_KERNEL
:
3414 ret
= "RIB_UPDATE_KERNEL";
3416 case RIB_UPDATE_RMAP_CHANGE
:
3417 ret
= "RIB_UPDATE_RMAP_CHANGE";
3419 case RIB_UPDATE_OTHER
:
3420 ret
= "RIB_UPDATE_OTHER";
3422 case RIB_UPDATE_MAX
:
3430 /* Schedule route nodes to be processed if they match the type */
3431 static void rib_update_route_node(struct route_node
*rn
, int type
)
3433 struct route_entry
*re
, *next
;
3434 bool re_changed
= false;
3436 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3437 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3438 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3447 /* Schedule routes of a particular table (address-family) based on event. */
3448 void rib_update_table(struct route_table
*table
, enum rib_update_event event
,
3451 struct route_node
*rn
;
3453 if (IS_ZEBRA_DEBUG_EVENT
) {
3454 struct zebra_vrf
*zvrf
;
3458 ? ((struct rib_table_info
*)table
->info
)->zvrf
3460 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3462 zlog_debug("%s: %s VRF %s Table %u event %s Route type: %s", __func__
,
3463 table
->info
? afi2str(
3464 ((struct rib_table_info
*)table
->info
)->afi
)
3466 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
3467 rib_update_event2str(event
), zebra_route_string(rtype
));
3470 /* Walk all routes and queue for processing, if appropriate for
3471 * the trigger event.
3473 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3475 * If we are looking at a route node and the node
3476 * has already been queued we don't
3477 * need to queue it up again
3480 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3481 RIB_ROUTE_ANY_QUEUED
))
3485 case RIB_UPDATE_KERNEL
:
3486 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3488 case RIB_UPDATE_RMAP_CHANGE
:
3489 case RIB_UPDATE_OTHER
:
3490 rib_update_route_node(rn
, rtype
);
3498 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
,
3501 struct route_table
*table
;
3503 if (IS_ZEBRA_DEBUG_EVENT
)
3504 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3505 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3507 /* Process routes of interested address-families. */
3508 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3510 rib_update_table(table
, event
, rtype
);
3512 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3514 rib_update_table(table
, event
, rtype
);
3517 static void rib_update_handle_vrf_all(enum rib_update_event event
, int rtype
)
3519 struct zebra_router_table
*zrt
;
3521 if (IS_ZEBRA_DEBUG_EVENT
)
3522 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3523 rib_update_event2str(event
));
3525 /* Just iterate over all the route tables, rather than vrf lookups */
3526 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3527 rib_update_table(zrt
->table
, event
, rtype
);
3530 struct rib_update_ctx
{
3531 enum rib_update_event event
;
3536 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3537 enum rib_update_event event
)
3539 struct rib_update_ctx
*ctx
;
3541 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3544 ctx
->vrf_id
= vrf_id
;
3549 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3551 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3554 static int rib_update_handler(struct thread
*thread
)
3556 struct rib_update_ctx
*ctx
;
3558 ctx
= THREAD_ARG(thread
);
3561 rib_update_handle_vrf_all(ctx
->event
, ZEBRA_ROUTE_ALL
);
3563 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
, ZEBRA_ROUTE_ALL
);
3565 rib_update_ctx_fini(&ctx
);
3571 * Thread list to ensure we don't schedule a ton of events
3572 * if interfaces are flapping for instance.
3574 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3576 /* Schedule a RIB update event for all vrfs */
3577 void rib_update(enum rib_update_event event
)
3579 struct rib_update_ctx
*ctx
;
3581 ctx
= rib_update_ctx_init(0, event
);
3583 ctx
->vrf_all
= true;
3585 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3586 &t_rib_update_threads
[event
]))
3587 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3588 else if (IS_ZEBRA_DEBUG_EVENT
)
3589 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
3590 rib_update_event2str(event
));
3593 /* Delete self installed routes after zebra is relaunched. */
3594 void rib_sweep_table(struct route_table
*table
)
3596 struct route_node
*rn
;
3597 struct route_entry
*re
;
3598 struct route_entry
*next
;
3599 struct nexthop
*nexthop
;
3604 if (IS_ZEBRA_DEBUG_RIB
)
3605 zlog_debug("%s: starting", __func__
);
3607 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3608 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3610 if (IS_ZEBRA_DEBUG_RIB
)
3611 route_entry_dump(&rn
->p
, NULL
, re
);
3613 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3616 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3620 * If routes are older than startup_time then
3621 * we know we read them in from the kernel.
3622 * As such we can safely remove them.
3624 if (zrouter
.startup_time
< re
->uptime
)
3628 * So we are starting up and have received
3629 * routes from the kernel that we have installed
3630 * from a previous run of zebra but not cleaned
3631 * up ( say a kill -9 )
3632 * But since we haven't actually installed
3633 * them yet( we received them from the kernel )
3634 * we don't think they are active.
3635 * So let's pretend they are active to actually
3637 * In all honesty I'm not sure if we should
3638 * mark them as active when we receive them
3639 * This is startup only so probably ok.
3641 * If we ever decide to move rib_sweep_table
3642 * to a different spot (ie startup )
3643 * this decision needs to be revisited
3645 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3646 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3647 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3649 rib_uninstall_kernel(rn
, re
);
3650 rib_delnode(rn
, re
);
3654 if (IS_ZEBRA_DEBUG_RIB
)
3655 zlog_debug("%s: ends", __func__
);
3658 /* Sweep all RIB tables. */
3659 int rib_sweep_route(struct thread
*t
)
3662 struct zebra_vrf
*zvrf
;
3664 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3665 if ((zvrf
= vrf
->info
) == NULL
)
3668 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3669 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3672 zebra_router_sweep_route();
3673 zebra_router_sweep_nhgs();
3678 /* Remove specific by protocol routes from 'table'. */
3679 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3680 struct route_table
*table
)
3682 struct route_node
*rn
;
3683 struct route_entry
*re
;
3684 struct route_entry
*next
;
3685 unsigned long n
= 0;
3688 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3689 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3690 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3692 if (re
->type
== proto
3693 && re
->instance
== instance
) {
3694 rib_delnode(rn
, re
);
3701 /* Remove specific by protocol routes. */
3702 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3705 struct zebra_vrf
*zvrf
;
3706 struct other_route_table
*ort
;
3707 unsigned long cnt
= 0;
3709 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3714 cnt
+= rib_score_proto_table(proto
, instance
,
3715 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3716 + rib_score_proto_table(
3718 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3720 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3721 rib_score_proto_table(proto
, instance
, ort
->table
);
3727 /* Close RIB and clean up kernel routes. */
3728 void rib_close_table(struct route_table
*table
)
3730 struct route_node
*rn
;
3731 struct rib_table_info
*info
;
3737 info
= route_table_get_info(table
);
3739 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3740 dest
= rib_dest_from_rnode(rn
);
3742 if (dest
&& dest
->selected_fib
) {
3743 if (info
->safi
== SAFI_UNICAST
)
3744 hook_call(rib_update
, rn
, NULL
);
3746 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3747 dest
->selected_fib
= NULL
;
3753 * Handler for async dataplane results after a pseudowire installation
3755 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3757 struct zebra_pw
*pw
;
3758 struct zebra_vrf
*vrf
;
3760 /* The pseudowire code assumes success - we act on an error
3761 * result for installation attempts here.
3763 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3766 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3767 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3768 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3770 zebra_pw_install_failure(pw
,
3771 dplane_ctx_get_pw_status(ctx
));
3775 dplane_ctx_fini(&ctx
);
3782 * Handle results from the dataplane system. Dequeue update context
3783 * structs, dispatch to appropriate internal handlers.
3785 static int rib_process_dplane_results(struct thread
*thread
)
3787 struct zebra_dplane_ctx
*ctx
;
3788 struct dplane_ctx_q ctxlist
;
3789 bool shut_p
= false;
3791 /* Dequeue a list of completed updates with one lock/unlock cycle */
3794 TAILQ_INIT(&ctxlist
);
3796 /* Take lock controlling queue of results */
3797 frr_with_mutex(&dplane_mutex
) {
3798 /* Dequeue list of context structs */
3799 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3802 /* Dequeue context block */
3803 ctx
= dplane_ctx_dequeue(&ctxlist
);
3805 /* If we've emptied the results queue, we're done */
3809 /* If zebra is shutting down, avoid processing results,
3810 * just drain the results queue.
3812 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3813 memory_order_relaxed
);
3816 dplane_ctx_fini(&ctx
);
3818 ctx
= dplane_ctx_dequeue(&ctxlist
);
3825 switch (dplane_ctx_get_op(ctx
)) {
3826 case DPLANE_OP_ROUTE_INSTALL
:
3827 case DPLANE_OP_ROUTE_UPDATE
:
3828 case DPLANE_OP_ROUTE_DELETE
:
3830 /* Bit of special case for route updates
3831 * that were generated by async notifications:
3832 * we don't want to continue processing these
3835 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3836 rib_process_result(ctx
);
3838 dplane_ctx_fini(&ctx
);
3842 case DPLANE_OP_ROUTE_NOTIFY
:
3843 rib_process_dplane_notify(ctx
);
3846 case DPLANE_OP_NH_INSTALL
:
3847 case DPLANE_OP_NH_UPDATE
:
3848 case DPLANE_OP_NH_DELETE
:
3849 zebra_nhg_dplane_result(ctx
);
3852 case DPLANE_OP_LSP_INSTALL
:
3853 case DPLANE_OP_LSP_UPDATE
:
3854 case DPLANE_OP_LSP_DELETE
:
3856 /* Bit of special case for LSP updates
3857 * that were generated by async notifications:
3858 * we don't want to continue processing these.
3860 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3861 zebra_mpls_lsp_dplane_result(ctx
);
3863 dplane_ctx_fini(&ctx
);
3867 case DPLANE_OP_LSP_NOTIFY
:
3868 zebra_mpls_process_dplane_notify(ctx
);
3871 case DPLANE_OP_PW_INSTALL
:
3872 case DPLANE_OP_PW_UNINSTALL
:
3873 handle_pw_result(ctx
);
3876 case DPLANE_OP_SYS_ROUTE_ADD
:
3877 case DPLANE_OP_SYS_ROUTE_DELETE
:
3878 /* No further processing in zebra for these. */
3879 dplane_ctx_fini(&ctx
);
3882 case DPLANE_OP_MAC_INSTALL
:
3883 case DPLANE_OP_MAC_DELETE
:
3884 zebra_vxlan_handle_result(ctx
);
3887 case DPLANE_OP_RULE_ADD
:
3888 case DPLANE_OP_RULE_DELETE
:
3889 case DPLANE_OP_RULE_UPDATE
:
3890 case DPLANE_OP_IPTABLE_ADD
:
3891 case DPLANE_OP_IPTABLE_DELETE
:
3892 case DPLANE_OP_IPSET_ADD
:
3893 case DPLANE_OP_IPSET_DELETE
:
3894 case DPLANE_OP_IPSET_ENTRY_ADD
:
3895 case DPLANE_OP_IPSET_ENTRY_DELETE
:
3896 zebra_pbr_dplane_result(ctx
);
3899 /* Some op codes not handled here */
3900 case DPLANE_OP_ADDR_INSTALL
:
3901 case DPLANE_OP_ADDR_UNINSTALL
:
3902 case DPLANE_OP_NEIGH_INSTALL
:
3903 case DPLANE_OP_NEIGH_UPDATE
:
3904 case DPLANE_OP_NEIGH_DELETE
:
3905 case DPLANE_OP_VTEP_ADD
:
3906 case DPLANE_OP_VTEP_DELETE
:
3907 case DPLANE_OP_NEIGH_DISCOVER
:
3908 case DPLANE_OP_BR_PORT_UPDATE
:
3909 case DPLANE_OP_NONE
:
3910 /* Don't expect this: just return the struct? */
3911 dplane_ctx_fini(&ctx
);
3914 } /* Dispatch by op code */
3916 ctx
= dplane_ctx_dequeue(&ctxlist
);
3925 * Results are returned from the dataplane subsystem, in the context of
3926 * the dataplane pthread. We enqueue the results here for processing by
3927 * the main thread later.
3929 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3931 /* Take lock controlling queue of results */
3932 frr_with_mutex(&dplane_mutex
) {
3933 /* Enqueue context blocks */
3934 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3937 /* Ensure event is signalled to zebra main pthread */
3938 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3945 * Ensure there are no empty slots in the route_info array.
3946 * Every route type in zebra should be present there.
3948 static void check_route_info(void)
3950 int len
= array_size(route_info
);
3953 * ZEBRA_ROUTE_SYSTEM is special cased since
3954 * its key is 0 anyway.
3956 * ZEBRA_ROUTE_ALL is also ignored.
3958 for (int i
= 0; i
< len
; i
++) {
3959 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3961 assert(route_info
[i
].key
);
3962 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3966 /* Routing information base initialize. */
3973 /* Init dataplane, and register for results */
3974 pthread_mutex_init(&dplane_mutex
, NULL
);
3975 TAILQ_INIT(&rib_dplane_q
);
3976 zebra_dplane_init(rib_dplane_results
);
3982 * Get the first vrf id that is greater than the given vrf id if any.
3984 * Returns true if a vrf id was found, false otherwise.
3986 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3990 vrf
= vrf_lookup_by_id(vrf_id
);
3992 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3994 *next_id_p
= vrf
->vrf_id
;
4003 * rib_tables_iter_next
4005 * Returns the next table in the iteration.
4007 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
4009 struct route_table
*table
;
4012 * Array that helps us go over all AFI/SAFI combinations via one
4015 static const struct {
4019 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
4020 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
4021 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
4026 switch (iter
->state
) {
4028 case RIB_TABLES_ITER_S_INIT
:
4029 iter
->vrf_id
= VRF_DEFAULT
;
4030 iter
->afi_safi_ix
= -1;
4034 case RIB_TABLES_ITER_S_ITERATING
:
4035 iter
->afi_safi_ix
++;
4038 while (iter
->afi_safi_ix
4039 < (int)array_size(afi_safis
)) {
4040 table
= zebra_vrf_table(
4041 afi_safis
[iter
->afi_safi_ix
].afi
,
4042 afi_safis
[iter
->afi_safi_ix
].safi
,
4047 iter
->afi_safi_ix
++;
4051 * Found another table in this vrf.
4057 * Done with all tables in the current vrf, go to the
4061 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
4064 iter
->afi_safi_ix
= 0;
4069 case RIB_TABLES_ITER_S_DONE
:
4074 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4076 iter
->state
= RIB_TABLES_ITER_S_DONE
;