1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
39 #include "nexthop_group_private.h"
40 #include "frr_pthread.h"
42 #include "zebra/zebra_router.h"
43 #include "zebra/connected.h"
44 #include "zebra/debug.h"
45 #include "zebra/interface.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/rib.h"
49 #include "zebra/zapi_msg.h"
50 #include "zebra/zebra_errors.h"
51 #include "zebra/zebra_memory.h"
52 #include "zebra/zebra_ns.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/zebra_routemap.h"
55 #include "zebra/zebra_vrf.h"
56 #include "zebra/zebra_vxlan.h"
57 #include "zebra/zapi_msg.h"
58 #include "zebra/zebra_dplane.h"
60 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
63 * Event, list, and mutex for delivery of dataplane results
65 static pthread_mutex_t dplane_mutex
;
66 static struct thread
*t_dplane
;
67 static struct dplane_ctx_q rib_dplane_q
;
69 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
72 /* Should we allow non Quagga processes to delete our routes */
73 extern int allow_delete
;
75 /* Each route type's string and default distance value. */
80 } route_info
[ZEBRA_ROUTE_MAX
] = {
81 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Uneeded for nhg's */, 0},
82 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 5},
83 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 1},
84 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 1},
85 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 2},
86 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 3},
87 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 3},
88 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 3},
89 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 3},
90 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 3},
91 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 4},
92 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 5},
93 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 3},
94 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 3},
95 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 5},
96 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 5},
97 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 2},
98 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 5},
99 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 4},
100 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 4},
101 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 4},
102 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 4},
103 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 4},
104 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 3},
105 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 5},
106 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 5},
107 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 5},
108 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 3},
109 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 5},
110 [ZEBRA_ROUTE_SRTE
] = {ZEBRA_ROUTE_SRTE
, 255, 5},
111 /* Any new route type added to zebra, should be mirrored here */
113 /* no entry/default: 150 */
116 static void PRINTFRR(5, 6)
117 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
118 int priority
, const char *msgfmt
, ...)
120 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
125 va_start(ap
, msgfmt
);
126 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
130 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
131 rib_dest_t
*dest
= NULL
;
132 struct route_entry
*re
= NULL
;
134 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
136 if (info
->safi
== SAFI_MULTICAST
)
137 strlcat(buf
, " (MRIB)", sizeof(buf
));
139 dest
= rib_dest_from_rnode(rn
);
141 re
= re_list_first(&dest
->routes
);
145 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
148 zlog(priority
, "%s: (%u:%u):%s: %s", _func
, vrf_id
, table
, buf
, msgbuf
);
151 #define rnode_debug(node, vrf_id, ...) \
152 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
153 #define rnode_info(node, ...) \
154 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
156 uint8_t route_distance(int type
)
160 if ((unsigned)type
>= array_size(route_info
))
163 distance
= route_info
[type
].distance
;
168 int is_zebra_valid_kernel_table(uint32_t table_id
)
171 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
172 || (table_id
== RT_TABLE_COMPAT
))
179 int is_zebra_main_routing_table(uint32_t table_id
)
181 if (table_id
== RT_TABLE_MAIN
)
186 int zebra_check_addr(const struct prefix
*p
)
188 if (p
->family
== AF_INET
) {
191 addr
= p
->u
.prefix4
.s_addr
;
194 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
195 || IPV4_LINKLOCAL(addr
))
198 if (p
->family
== AF_INET6
) {
199 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
201 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
208 * copy_nexthop - copy a nexthop to the rib structure.
210 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
212 assert(!re
->nhe
->nhg
.nexthop
);
213 copy_nexthops(&re
->nhe
->nhg
.nexthop
, nh
, NULL
);
216 static void route_entry_attach_ref(struct route_entry
*re
,
217 struct nhg_hash_entry
*new)
220 re
->nhe_id
= new->id
;
222 zebra_nhg_increment_ref(new);
225 int route_entry_update_nhe(struct route_entry
*re
,
226 struct nhg_hash_entry
*new_nhghe
)
228 struct nhg_hash_entry
*old
;
231 if (new_nhghe
== NULL
) {
233 zebra_nhg_decrement_ref(re
->nhe
);
238 if ((re
->nhe_id
!= 0) && re
->nhe
&& (re
->nhe
!= new_nhghe
)) {
241 route_entry_attach_ref(re
, new_nhghe
);
244 zebra_nhg_decrement_ref(old
);
246 /* This is the first time it's being attached */
247 route_entry_attach_ref(re
, new_nhghe
);
253 void rib_handle_nhg_replace(struct nhg_hash_entry
*old
,
254 struct nhg_hash_entry
*new)
256 struct zebra_router_table
*zrt
;
257 struct route_node
*rn
;
258 struct route_entry
*re
, *next
;
260 if (IS_ZEBRA_DEBUG_RIB_DETAILED
|| IS_ZEBRA_DEBUG_NHG_DETAIL
)
261 zlog_debug("%s: replacing routes nhe (%u) OLD %p NEW %p",
262 __func__
, new->id
, new, old
);
264 /* We have to do them ALL */
265 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
) {
266 for (rn
= route_top(zrt
->table
); rn
;
267 rn
= srcdest_route_next(rn
)) {
268 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
269 if (re
->nhe
&& re
->nhe
== old
)
270 route_entry_update_nhe(re
, new);
276 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
277 union g_addr
*addr
, struct route_node
**rn_out
)
280 struct route_table
*table
;
281 struct route_node
*rn
;
282 struct route_entry
*match
= NULL
;
285 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
289 memset(&p
, 0, sizeof(struct prefix
));
292 p
.u
.prefix4
= addr
->ipv4
;
293 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
295 p
.u
.prefix6
= addr
->ipv6
;
296 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
299 rn
= route_node_match(table
, &p
);
304 route_unlock_node(rn
);
306 dest
= rib_dest_from_rnode(rn
);
307 if (dest
&& dest
->selected_fib
308 && !CHECK_FLAG(dest
->selected_fib
->status
,
309 ROUTE_ENTRY_REMOVED
))
310 match
= dest
->selected_fib
;
312 /* If there is no selected route or matched route is EGP, go up
317 } while (rn
&& rn
->info
== NULL
);
321 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
322 if (!CHECK_FLAG(match
->status
,
323 ROUTE_ENTRY_INSTALLED
))
335 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
337 struct route_node
**rn_out
)
339 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
340 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
341 union g_addr gaddr
= {.ipv4
= addr
};
343 switch (zrouter
.ipv4_multicast_mode
) {
344 case MCAST_MRIB_ONLY
:
345 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
347 case MCAST_URIB_ONLY
:
348 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
349 case MCAST_NO_CONFIG
:
350 case MCAST_MIX_MRIB_FIRST
:
351 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
354 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
357 case MCAST_MIX_DISTANCE
:
358 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
359 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
361 re
= ure
->distance
< mre
->distance
? ure
: mre
;
367 case MCAST_MIX_PFXLEN
:
368 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
369 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
371 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
380 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
382 if (IS_ZEBRA_DEBUG_RIB
) {
384 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
386 zlog_debug("%s: %s: vrf: %s(%u) found %s, using %s", __func__
,
387 buf
, vrf_id_to_name(vrf_id
), vrf_id
,
388 mre
? (ure
? "MRIB+URIB" : "MRIB")
389 : ure
? "URIB" : "nothing",
390 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
395 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
397 struct route_table
*table
;
398 struct route_node
*rn
;
399 struct route_entry
*match
= NULL
;
403 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
407 rn
= route_node_lookup(table
, (struct prefix
*)p
);
409 /* No route for this prefix. */
414 route_unlock_node(rn
);
415 dest
= rib_dest_from_rnode(rn
);
417 if (dest
&& dest
->selected_fib
418 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
419 match
= dest
->selected_fib
;
424 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
427 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
434 * Is this RIB labeled-unicast? It must be of type BGP and all paths
435 * (nexthops) must have a label.
437 int zebra_rib_labeled_unicast(struct route_entry
*re
)
439 struct nexthop
*nexthop
= NULL
;
441 if (re
->type
!= ZEBRA_ROUTE_BGP
)
444 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
445 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
451 /* Update flag indicates whether this is a "replace" or not. Currently, this
452 * is only used for IPv4.
454 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
455 struct route_entry
*old
)
457 struct nexthop
*nexthop
;
458 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
459 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
460 const struct prefix
*p
, *src_p
;
461 enum zebra_dplane_result ret
;
463 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
465 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
467 if (info
->safi
!= SAFI_UNICAST
) {
468 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
469 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
475 * Install the resolved nexthop object first.
477 zebra_nhg_install_kernel(re
->nhe
);
480 * If this is a replace to a new RE let the originator of the RE
481 * know that they've lost
483 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
484 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
486 /* Update fib selection */
487 dest
->selected_fib
= re
;
490 * Make sure we update the FPM any time we send new information to
493 hook_call(rib_update
, rn
, "installing in kernel");
495 /* Send add or update */
497 ret
= dplane_route_update(rn
, re
, old
);
499 ret
= dplane_route_add(rn
, re
);
502 case ZEBRA_DPLANE_REQUEST_QUEUED
:
503 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
506 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
508 /* Free old FIB nexthop group */
509 UNSET_FLAG(old
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
510 if (old
->fib_ng
.nexthop
) {
511 nexthops_free(old
->fib_ng
.nexthop
);
512 old
->fib_ng
.nexthop
= NULL
;
517 zvrf
->installs_queued
++;
519 case ZEBRA_DPLANE_REQUEST_FAILURE
:
521 char str
[SRCDEST2STR_BUFFER
];
523 srcdest_rnode2str(rn
, str
, sizeof(str
));
524 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
525 "%u:%u:%s: Failed to enqueue dataplane install",
526 re
->vrf_id
, re
->table
, str
);
529 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
538 /* Uninstall the route from kernel. */
539 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
541 struct nexthop
*nexthop
;
542 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
543 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
545 if (info
->safi
!= SAFI_UNICAST
) {
546 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
547 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
548 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
553 * Make sure we update the FPM any time we send new information to
556 hook_call(rib_update
, rn
, "uninstalling from kernel");
558 switch (dplane_route_delete(rn
, re
)) {
559 case ZEBRA_DPLANE_REQUEST_QUEUED
:
561 zvrf
->removals_queued
++;
563 case ZEBRA_DPLANE_REQUEST_FAILURE
:
565 char str
[SRCDEST2STR_BUFFER
];
567 srcdest_rnode2str(rn
, str
, sizeof(str
));
568 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
569 "%u:%s: Failed to enqueue dataplane uninstall",
573 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
582 /* Uninstall the route from kernel. */
583 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
585 struct rib_table_info
*info
= srcdest_rnode_table_info(rn
);
586 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
587 struct nexthop
*nexthop
;
589 if (dest
&& dest
->selected_fib
== re
) {
590 if (info
->safi
== SAFI_UNICAST
)
591 hook_call(rib_update
, rn
, "rib_uninstall");
593 /* If labeled-unicast route, uninstall transit LSP. */
594 if (zebra_rib_labeled_unicast(re
))
595 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
597 rib_uninstall_kernel(rn
, re
);
599 dest
->selected_fib
= NULL
;
601 /* Free FIB nexthop group, if present */
602 if (re
->fib_ng
.nexthop
) {
603 nexthops_free(re
->fib_ng
.nexthop
);
604 re
->fib_ng
.nexthop
= NULL
;
606 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
608 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
609 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
612 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
613 const struct prefix
*p
, *src_p
;
615 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
617 redistribute_delete(p
, src_p
, re
, NULL
);
618 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
623 * rib_can_delete_dest
625 * Returns true if the given dest can be deleted from the table.
627 static int rib_can_delete_dest(rib_dest_t
*dest
)
629 if (re_list_first(&dest
->routes
)) {
634 * Unresolved rnh's are stored on the default route's list
636 * dest->rnode can also be the source prefix node in an
637 * ipv6 sourcedest table. Fortunately the prefix of a
638 * source prefix node can never be the default prefix.
640 if (is_default_prefix(&dest
->rnode
->p
))
644 * Don't delete the dest if we have to update the FPM about this
647 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
648 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
654 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
656 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
660 * We are storing the rnh's associated withb
661 * the tracked nexthop as a list of the rn's.
662 * Unresolved rnh's are placed at the top
663 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
664 * As such for each rn we need to walk up the tree
665 * and see if any rnh's need to see if they
666 * would match a more specific route
669 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
670 char buf
[PREFIX_STRLEN
];
673 "%s: %s Being examined for Nexthop Tracking Count: %zd",
675 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
676 dest
? rnh_list_count(&dest
->nht
) : 0);
681 dest
= rib_dest_from_rnode(rn
);
685 * If we have any rnh's stored in the nht list
686 * then we know that this route node was used for
687 * nht resolution and as such we need to call the
688 * nexthop tracking evaluation code
690 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
691 struct zebra_vrf
*zvrf
=
692 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
693 struct prefix
*p
= &rnh
->node
->p
;
695 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
696 char buf1
[PREFIX_STRLEN
];
697 char buf2
[PREFIX_STRLEN
];
700 "%s(%u):%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u",
701 zvrf_name(zvrf
), zvrf_id(zvrf
),
702 srcdest_rnode2str(rn
, buf1
,
704 prefix2str(p
, buf2
, sizeof(buf2
)),
705 rnh_type2str(rnh
->type
), seq
,
710 * If we have evaluated this node on this pass
711 * already, due to following the tree up
712 * then we know that we can move onto the next
715 * Additionally we call zebra_evaluate_rnh
716 * when we gc the dest. In this case we know
717 * that there must be no other re's where
718 * we were originally as such we know that
719 * that sequence number is ok to respect.
721 if (rnh
->seqno
== seq
) {
722 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
724 "\tNode processed and moved already");
729 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
735 dest
= rib_dest_from_rnode(rn
);
742 * Garbage collect the rib dest corresponding to the given route node
745 * Returns true if the dest was deleted, false otherwise.
747 int rib_gc_dest(struct route_node
*rn
)
751 dest
= rib_dest_from_rnode(rn
);
755 if (!rib_can_delete_dest(dest
))
758 if (IS_ZEBRA_DEBUG_RIB
) {
759 struct zebra_vrf
*zvrf
;
761 zvrf
= rib_dest_vrf(dest
);
762 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
765 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
768 rnh_list_fini(&dest
->nht
);
769 XFREE(MTYPE_RIB_DEST
, dest
);
773 * Release the one reference that we keep on the route node.
775 route_unlock_node(rn
);
779 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
780 struct route_entry
*new)
782 hook_call(rib_update
, rn
, "new route selected");
784 /* Update real nexthop. This may actually determine if nexthop is active
786 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
787 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
791 if (IS_ZEBRA_DEBUG_RIB
) {
792 char buf
[SRCDEST2STR_BUFFER
];
793 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
794 zlog_debug("%s(%u:%u):%s: Adding route rn %p, re %p (%s)",
795 zvrf_name(zvrf
), zvrf_id(zvrf
), new->table
, buf
, rn
,
796 new, zebra_route_string(new->type
));
799 /* If labeled-unicast route, install transit LSP. */
800 if (zebra_rib_labeled_unicast(new))
801 zebra_mpls_lsp_install(zvrf
, rn
, new);
803 rib_install_kernel(rn
, new, NULL
);
805 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
808 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
809 struct route_entry
*old
)
811 hook_call(rib_update
, rn
, "removing existing route");
813 /* Uninstall from kernel. */
814 if (IS_ZEBRA_DEBUG_RIB
) {
815 char buf
[SRCDEST2STR_BUFFER
];
816 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
817 zlog_debug("%s(%u:%u):%s: Deleting route rn %p, re %p (%s)",
818 zvrf_name(zvrf
), zvrf_id(zvrf
), old
->table
, buf
, rn
,
819 old
, zebra_route_string(old
->type
));
822 /* If labeled-unicast route, uninstall transit LSP. */
823 if (zebra_rib_labeled_unicast(old
))
824 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
826 rib_uninstall_kernel(rn
, old
);
828 /* Update nexthop for route, reset changed flag. */
829 /* Note: this code also handles the Linux case when an interface goes
830 * down, causing the kernel to delete routes without sending DELROUTE
833 if (RIB_KERNEL_ROUTE(old
))
834 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
836 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
839 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
840 struct route_node
*rn
,
841 struct route_entry
*old
,
842 struct route_entry
*new)
847 * We have to install or update if a new route has been selected or
848 * something has changed.
850 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
851 hook_call(rib_update
, rn
, "updating existing route");
853 /* Update the nexthop; we could determine here that nexthop is
855 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
858 /* If nexthop is active, install the selected route, if
860 * the install succeeds, cleanup flags for prior route, if
865 if (IS_ZEBRA_DEBUG_RIB
) {
866 char buf
[SRCDEST2STR_BUFFER
];
867 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
870 "%s(%u:%u):%s: Updating route rn %p, re %p (%s) old %p (%s)",
871 zvrf_name(zvrf
), zvrf_id(zvrf
),
872 new->table
, buf
, rn
, new,
873 zebra_route_string(new->type
),
875 zebra_route_string(old
->type
));
878 "%s(%u:%u):%s: Updating route rn %p, re %p (%s)",
879 zvrf_name(zvrf
), zvrf_id(zvrf
),
880 new->table
, buf
, rn
, new,
881 zebra_route_string(new->type
));
884 /* If labeled-unicast route, uninstall transit LSP. */
885 if (zebra_rib_labeled_unicast(old
))
886 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
889 * Non-system route should be installed.
890 * If labeled-unicast route, install transit
893 if (zebra_rib_labeled_unicast(new))
894 zebra_mpls_lsp_install(zvrf
, rn
, new);
896 rib_install_kernel(rn
, new, old
);
900 * If nexthop for selected route is not active or install
902 * may need to uninstall and delete for redistribution.
905 if (IS_ZEBRA_DEBUG_RIB
) {
906 char buf
[SRCDEST2STR_BUFFER
];
907 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
910 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
911 zvrf_name(zvrf
), zvrf_id(zvrf
),
912 new->table
, buf
, rn
, new,
913 zebra_route_string(new->type
),
915 zebra_route_string(old
->type
));
918 "%s(%u:%u):%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
919 zvrf_name(zvrf
), zvrf_id(zvrf
),
920 new->table
, buf
, rn
, new,
921 zebra_route_string(new->type
));
924 /* If labeled-unicast route, uninstall transit LSP. */
925 if (zebra_rib_labeled_unicast(old
))
926 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
928 rib_uninstall_kernel(rn
, old
);
932 * Same route selected; check if in the FIB and if not,
933 * re-install. This is housekeeping code to deal with
934 * race conditions in kernel with linux netlink reporting
935 * interface up before IPv4 or IPv6 protocol is ready
938 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
939 RIB_SYSTEM_ROUTE(new))
940 rib_install_kernel(rn
, new, NULL
);
943 /* Update prior route. */
945 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
947 /* Clear changed flag. */
948 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
951 /* Check if 'alternate' RIB entry is better than 'current'. */
952 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
953 struct route_entry
*alternate
)
958 /* filter route selection in following order:
959 * - connected beats other types
960 * - if both connected, loopback or vrf wins
961 * - lower distance beats higher
962 * - lower metric beats higher for equal distance
963 * - last, hence oldest, route wins tie break.
966 /* Connected routes. Check to see if either are a vrf
967 * or loopback interface. If not, pick the last connected
968 * route of the set of lowest metric connected routes.
970 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
971 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
974 /* both are connected. are either loop or vrf? */
975 struct nexthop
*nexthop
= NULL
;
977 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
978 struct interface
*ifp
= if_lookup_by_index(
979 nexthop
->ifindex
, alternate
->vrf_id
);
981 if (ifp
&& if_is_loopback_or_vrf(ifp
))
985 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
986 struct interface
*ifp
= if_lookup_by_index(
987 nexthop
->ifindex
, current
->vrf_id
);
989 if (ifp
&& if_is_loopback_or_vrf(ifp
))
993 /* Neither are loop or vrf so pick best metric */
994 if (alternate
->metric
<= current
->metric
)
1000 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1003 /* higher distance loses */
1004 if (alternate
->distance
< current
->distance
)
1006 if (current
->distance
< alternate
->distance
)
1009 /* metric tie-breaks equal distance */
1010 if (alternate
->metric
<= current
->metric
)
1016 /* Core function for processing nexthop group contexts's off metaq */
1017 static void rib_nhg_process(struct nhg_ctx
*ctx
)
1019 nhg_ctx_process(ctx
);
1022 /* Core function for processing routing information base. */
1023 static void rib_process(struct route_node
*rn
)
1025 struct route_entry
*re
;
1026 struct route_entry
*next
;
1027 struct route_entry
*old_selected
= NULL
;
1028 struct route_entry
*new_selected
= NULL
;
1029 struct route_entry
*old_fib
= NULL
;
1030 struct route_entry
*new_fib
= NULL
;
1031 struct route_entry
*best
= NULL
;
1032 char buf
[SRCDEST2STR_BUFFER
];
1034 struct zebra_vrf
*zvrf
= NULL
;
1036 const struct prefix
*p
, *src_p
;
1038 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1039 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1043 dest
= rib_dest_from_rnode(rn
);
1045 zvrf
= rib_dest_vrf(dest
);
1046 vrf_id
= zvrf_id(zvrf
);
1049 vrf
= vrf_lookup_by_id(vrf_id
);
1051 if (IS_ZEBRA_DEBUG_RIB
)
1052 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1055 * we can have rn's that have a NULL info pointer
1056 * (dest). As such let's not let the deref happen
1057 * additionally we know RNODE_FOREACH_RE_SAFE
1058 * will not iterate so we are ok.
1061 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1062 struct route_entry
*re
= re_list_first(&dest
->routes
);
1064 zlog_debug("%s(%u:%u):%s: Processing rn %p",
1065 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
,
1069 old_fib
= dest
->selected_fib
;
1072 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1073 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1075 "%s(%u:%u):%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1076 VRF_LOGNAME(vrf
), vrf_id
, re
->table
, buf
, re
,
1077 zebra_route_string(re
->type
), re
->status
,
1078 re
->flags
, re
->distance
, re
->metric
);
1080 /* Currently selected re. */
1081 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1082 assert(old_selected
== NULL
);
1086 /* Skip deleted entries from selection */
1087 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1090 /* Skip unreachable nexthop. */
1091 /* This first call to nexthop_active_update is merely to
1092 * determine if there's any change to nexthops associated
1093 * with this RIB entry. Now, rib_process() can be invoked due
1094 * to an external event such as link down or due to
1095 * next-hop-tracking evaluation. In the latter case,
1096 * a decision has already been made that the NHs have changed.
1097 * So, no need to invoke a potentially expensive call again.
1098 * Further, since the change might be in a recursive NH which
1099 * is not caught in the nexthop_active_update() code. Thus, we
1100 * might miss changes to recursive NHs.
1102 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1103 && !nexthop_active_update(rn
, re
)) {
1104 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1105 /* XXX: HERE BE DRAGONS!!!!!
1106 * In all honesty, I have not yet figured out
1107 * what this part does or why the
1108 * ROUTE_ENTRY_CHANGED test above is correct
1109 * or why we need to delete a route here, and
1110 * also not whether this concerns both selected
1111 * and fib route, or only selected
1114 * This entry was denied by the 'ip protocol
1115 * table' route-map, we need to delete it */
1116 if (re
!= old_selected
) {
1117 if (IS_ZEBRA_DEBUG_RIB
)
1119 "%s: %s(%u):%s: imported via import-table but denied by the ip protocol table route-map",
1125 SET_FLAG(re
->status
,
1126 ROUTE_ENTRY_REMOVED
);
1132 /* Infinite distance. */
1133 if (re
->distance
== DISTANCE_INFINITY
&&
1134 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1135 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1139 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1140 best
= rib_choose_best(new_fib
, re
);
1141 if (new_fib
&& best
!= new_fib
)
1142 UNSET_FLAG(new_fib
->status
,
1143 ROUTE_ENTRY_CHANGED
);
1146 best
= rib_choose_best(new_selected
, re
);
1147 if (new_selected
&& best
!= new_selected
)
1148 UNSET_FLAG(new_selected
->status
,
1149 ROUTE_ENTRY_CHANGED
);
1150 new_selected
= best
;
1153 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1154 } /* RNODE_FOREACH_RE */
1156 /* If no FIB override route, use the selected route also for FIB */
1157 if (new_fib
== NULL
)
1158 new_fib
= new_selected
;
1160 /* After the cycle is finished, the following pointers will be set:
1161 * old_selected --- RE entry currently having SELECTED
1162 * new_selected --- RE entry that is newly SELECTED
1163 * old_fib --- RE entry currently in kernel FIB
1164 * new_fib --- RE entry that is newly to be in kernel FIB
1166 * new_selected will get SELECTED flag, and is going to be redistributed
1167 * the zclients. new_fib (which can be new_selected) will be installed
1171 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1172 struct route_entry
*entry
;
1174 entry
= old_selected
1179 : new_fib
? new_fib
: NULL
;
1182 "%s(%u:%u):%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1183 VRF_LOGNAME(vrf
), vrf_id
, entry
? entry
->table
: 0, buf
,
1184 (void *)old_selected
, (void *)new_selected
,
1185 (void *)old_fib
, (void *)new_fib
);
1188 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1189 * fib == selected */
1190 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1191 ROUTE_ENTRY_CHANGED
);
1193 /* Update fib according to selection results */
1194 if (new_fib
&& old_fib
)
1195 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1197 rib_process_add_fib(zvrf
, rn
, new_fib
);
1199 rib_process_del_fib(zvrf
, rn
, old_fib
);
1201 /* Update SELECTED entry */
1202 if (old_selected
!= new_selected
|| selected_changed
) {
1204 if (new_selected
&& new_selected
!= new_fib
)
1205 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1208 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1212 * If we're removing the old entry, we should tell
1213 * redist subscribers about that *if* they aren't
1214 * going to see a redist for the new entry.
1216 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1217 ROUTE_ENTRY_REMOVED
))
1218 redistribute_delete(p
, src_p
,
1222 if (old_selected
!= new_selected
)
1223 UNSET_FLAG(old_selected
->flags
,
1224 ZEBRA_FLAG_SELECTED
);
1228 /* Remove all RE entries queued for removal */
1229 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1230 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1231 if (IS_ZEBRA_DEBUG_RIB
) {
1232 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1233 (void *)rn
, (void *)re
);
1240 * Check if the dest can be deleted now.
1245 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1247 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1248 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1253 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1254 if (IS_ZEBRA_DEBUG_MPLS
)
1256 "%s(%u): Scheduling all LSPs upon RIB completion",
1257 zvrf_name(zvrf
), zvrf_id(zvrf
));
1258 zebra_mpls_lsp_schedule(zvrf
);
1259 mpls_unmark_lsps_for_processing(rn
);
1264 * Utility to match route with dplane context data
1266 static bool rib_route_match_ctx(const struct route_entry
*re
,
1267 const struct zebra_dplane_ctx
*ctx
,
1270 bool result
= false;
1274 * In 'update' case, we test info about the 'previous' or
1277 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1278 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1281 /* We use an extra test for statics, and another for
1284 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1285 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1286 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1288 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1290 dplane_ctx_get_old_metric(ctx
)) {
1297 * Ordinary, single-route case using primary context info
1299 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1300 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1301 /* Skip route that's been deleted */
1305 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1306 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1309 /* We use an extra test for statics, and another for
1312 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1313 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1314 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1316 } else if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
1317 re
->metric
!= dplane_ctx_get_metric(ctx
)) {
1319 } else if (re
->type
== ZEBRA_ROUTE_CONNECT
) {
1320 result
= nexthop_group_equal_no_recurse(
1321 &re
->nhe
->nhg
, dplane_ctx_get_ng(ctx
));
1331 static void zebra_rib_fixup_system(struct route_node
*rn
)
1333 struct route_entry
*re
;
1335 RNODE_FOREACH_RE(rn
, re
) {
1336 struct nexthop
*nhop
;
1338 if (!RIB_SYSTEM_ROUTE(re
))
1341 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1344 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1345 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1347 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1348 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1351 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1356 /* Route comparison logic, with various special cases. */
1357 static bool rib_compare_routes(const struct route_entry
*re1
,
1358 const struct route_entry
*re2
)
1360 bool result
= false;
1362 if (re1
->type
!= re2
->type
)
1365 if (re1
->instance
!= re2
->instance
)
1368 if (re1
->type
== ZEBRA_ROUTE_KERNEL
&& re1
->metric
!= re2
->metric
)
1371 if (CHECK_FLAG(re1
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
1372 re1
->distance
!= re2
->distance
)
1375 /* Only connected routes need more checking, nexthop-by-nexthop */
1376 if (re1
->type
!= ZEBRA_ROUTE_CONNECT
)
1379 /* Quick check if shared nhe */
1380 if (re1
->nhe
== re2
->nhe
)
1383 result
= nexthop_group_equal_no_recurse(&re1
->nhe
->nhg
, &re2
->nhe
->nhg
);
1389 * Compare nexthop lists from a route and a dplane context; test whether
1390 * the list installed in the FIB matches the route's list.
1391 * Set 'changed_p' to 'true' if there were changes to the route's
1392 * installed nexthops.
1394 * Return 'false' if any ACTIVE route nexthops are not mentioned in the FIB
1397 static bool rib_update_nhg_from_ctx(struct nexthop_group
*re_nhg
,
1398 const struct nexthop_group
*ctx_nhg
,
1401 bool matched_p
= true;
1402 struct nexthop
*nexthop
, *ctx_nexthop
;
1404 /* Get the first `installed` one to check against.
1405 * If the dataplane doesn't set these to be what was actually installed,
1406 * it will just be whatever was in re->nhe->nhg?
1408 ctx_nexthop
= ctx_nhg
->nexthop
;
1410 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1411 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1412 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1414 for (ALL_NEXTHOPS_PTR(re_nhg
, nexthop
)) {
1416 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1419 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1422 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1423 if (!nexthop_same(ctx_nexthop
, nexthop
)) {
1424 /* If the FIB doesn't know about the nexthop,
1425 * it's not installed
1427 if (IS_ZEBRA_DEBUG_RIB_DETAILED
||
1428 IS_ZEBRA_DEBUG_NHG_DETAIL
) {
1429 zlog_debug("%s: no ctx match for rib nh %pNHv %s",
1431 (CHECK_FLAG(nexthop
->flags
,
1437 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1440 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1442 /* Keep checking nexthops */
1446 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1447 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1448 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1449 zlog_debug("%s: rib nh %pNHv -> installed",
1455 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1457 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1458 if (IS_ZEBRA_DEBUG_NHG_DETAIL
)
1459 zlog_debug("%s: rib nh %pNHv -> uninstalled",
1465 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1468 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1475 * Update a route from a dplane context. This consolidates common code
1476 * that can be used in processing of results from FIB updates, and in
1477 * async notification processing.
1478 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1480 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1481 struct route_node
*rn
,
1482 struct zebra_dplane_ctx
*ctx
)
1484 char dest_str
[PREFIX_STRLEN
] = "";
1485 struct nexthop
*nexthop
;
1487 const struct nexthop_group
*ctxnhg
;
1488 struct nexthop_group
*re_nhg
;
1489 bool is_selected
= false; /* Is 're' currently the selected re? */
1490 bool changed_p
= false; /* Change to nexthops? */
1494 vrf
= vrf_lookup_by_id(re
->vrf_id
);
1496 /* Note well: only capturing the prefix string if debug is enabled here;
1497 * unconditional log messages will have to generate the string.
1499 if (IS_ZEBRA_DEBUG_RIB
)
1500 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1502 dest
= rib_dest_from_rnode(rn
);
1504 is_selected
= (re
== dest
->selected_fib
);
1506 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1507 zlog_debug("update_from_ctx: %s(%u:%u):%s: %sSELECTED, re %p",
1508 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, dest_str
,
1509 (is_selected
? "" : "NOT "), re
);
1511 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1512 * If the installed set differs from the set requested by the rib/owner,
1513 * we use the fib-specific nexthop-group to record the actual FIB
1517 ctxnhg
= dplane_ctx_get_ng(ctx
);
1519 /* Check route's fib group and incoming notif group for equivalence.
1521 * Let's assume the nexthops are ordered here to save time.
1523 /* TODO -- this isn't testing or comparing the FIB flags; we should
1524 * do a more explicit loop, checking the incoming notification's flags.
1526 if (re
->fib_ng
.nexthop
&& ctxnhg
->nexthop
&&
1527 nexthop_group_equal(&re
->fib_ng
, ctxnhg
))
1530 /* If the new FIB set matches the existing FIB set, we're done. */
1532 if (IS_ZEBRA_DEBUG_RIB
)
1534 "%s(%u:%u):%s update_from_ctx(): existing fib nhg, no change",
1535 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
,
1539 } else if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
)) {
1541 * Free stale fib list and move on to check the rib nhg.
1543 if (IS_ZEBRA_DEBUG_RIB
)
1545 "%s(%u:%u):%s update_from_ctx(): replacing fib nhg",
1546 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
,
1548 nexthops_free(re
->fib_ng
.nexthop
);
1549 re
->fib_ng
.nexthop
= NULL
;
1551 UNSET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1553 /* Note that the installed nexthops have changed */
1556 if (IS_ZEBRA_DEBUG_RIB
)
1557 zlog_debug("%s(%u:%u):%s update_from_ctx(): no fib nhg",
1558 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
,
1563 * Compare with the rib nexthop group. The comparison here is different:
1564 * the RIB group may be a superset of the list installed in the FIB. We
1565 * walk the RIB group, looking for the 'installable' candidate
1566 * nexthops, and then check those against the set
1567 * that is actually installed.
1569 * Assume nexthops are ordered here as well.
1572 /* If nothing is installed, we can skip some of the checking/comparison
1575 if (ctxnhg
->nexthop
== NULL
) {
1580 matched
= rib_update_nhg_from_ctx(&(re
->nhe
->nhg
), ctxnhg
, &changed_p
);
1582 /* If all nexthops were processed, we're done */
1584 if (IS_ZEBRA_DEBUG_RIB
)
1586 "%s(%u:%u):%s update_from_ctx(): rib nhg matched, changed '%s'",
1587 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
,
1588 dest_str
, (changed_p
? "true" : "false"));
1594 /* FIB nexthop set differs from the RIB set:
1595 * create a fib-specific nexthop-group
1597 if (IS_ZEBRA_DEBUG_RIB
)
1599 "%s(%u:%u):%s update_from_ctx(): changed %s, adding new fib nhg%s",
1600 VRF_LOGNAME(vrf
), re
->vrf_id
, re
->table
, dest_str
,
1601 (changed_p
? "true" : "false"),
1602 ctxnhg
->nexthop
!= NULL
? "" : " (empty)");
1604 /* Set the flag about the dedicated fib list */
1605 SET_FLAG(re
->status
, ROUTE_ENTRY_USE_FIB_NHG
);
1606 if (ctxnhg
->nexthop
)
1607 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1612 * Check the status of the route's backup nexthops, if any.
1613 * The logic for backups is somewhat different: if any backup is
1614 * installed, a new fib nhg will be attached to the route.
1616 re_nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
1618 goto done
; /* No backup nexthops */
1620 /* First check the route's 'fib' list of backups, if it's present
1621 * from some previous event.
1623 re_nhg
= &re
->fib_backup_ng
;
1624 ctxnhg
= dplane_ctx_get_backup_ng(ctx
);
1627 if (re_nhg
->nexthop
&& ctxnhg
&& nexthop_group_equal(re_nhg
, ctxnhg
))
1630 /* If the new FIB set matches an existing FIB set, we're done. */
1632 if (IS_ZEBRA_DEBUG_RIB
)
1634 "%s(%u):%s update_from_ctx(): existing fib backup nhg, no change",
1635 VRF_LOGNAME(vrf
), re
->vrf_id
, dest_str
);
1638 } else if (re
->fib_backup_ng
.nexthop
) {
1640 * Free stale fib backup list and move on to check
1641 * the route's backups.
1643 if (IS_ZEBRA_DEBUG_RIB
)
1645 "%s(%u):%s update_from_ctx(): replacing fib backup nhg",
1646 VRF_LOGNAME(vrf
), re
->vrf_id
, dest_str
);
1647 nexthops_free(re
->fib_backup_ng
.nexthop
);
1648 re
->fib_backup_ng
.nexthop
= NULL
;
1650 /* Note that the installed nexthops have changed */
1653 if (IS_ZEBRA_DEBUG_RIB
)
1654 zlog_debug("%s(%u):%s update_from_ctx(): no fib backup nhg",
1655 VRF_LOGNAME(vrf
), re
->vrf_id
, dest_str
);
1659 * If a FIB backup nexthop set exists, attach a copy
1660 * to the route if any backup is installed
1662 if (ctxnhg
&& ctxnhg
->nexthop
) {
1664 for (ALL_NEXTHOPS_PTR(ctxnhg
, nexthop
)) {
1665 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1669 /* If no installed backups, we're done */
1670 if (nexthop
== NULL
)
1673 if (IS_ZEBRA_DEBUG_RIB
)
1674 zlog_debug("%s(%u):%s update_from_ctx(): changed %s, adding new backup fib nhg",
1675 VRF_LOGNAME(vrf
), re
->vrf_id
, dest_str
,
1676 (changed_p
? "true" : "false"));
1678 copy_nexthops(&(re
->fib_backup_ng
.nexthop
), ctxnhg
->nexthop
,
1688 * Helper to locate a zebra route-node from a dplane context. This is used
1689 * when processing dplane results, e.g. Note well: the route-node is returned
1690 * with a ref held - route_unlock_node() must be called eventually.
1692 static struct route_node
*
1693 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1695 struct route_table
*table
= NULL
;
1696 struct route_node
*rn
= NULL
;
1697 const struct prefix
*dest_pfx
, *src_pfx
;
1699 /* Locate rn and re(s) from ctx */
1701 table
= zebra_vrf_lookup_table_with_table_id(
1702 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1703 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1704 if (table
== NULL
) {
1705 if (IS_ZEBRA_DEBUG_DPLANE
) {
1707 "Failed to find route for ctx: no table for afi %d, safi %d, vrf %s(%u)",
1708 dplane_ctx_get_afi(ctx
),
1709 dplane_ctx_get_safi(ctx
),
1710 vrf_id_to_name(dplane_ctx_get_vrf(ctx
)),
1711 dplane_ctx_get_vrf(ctx
));
1716 dest_pfx
= dplane_ctx_get_dest(ctx
);
1717 src_pfx
= dplane_ctx_get_src(ctx
);
1719 rn
= srcdest_rnode_get(table
, dest_pfx
,
1720 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1729 * Route-update results processing after async dataplane update.
1731 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1733 struct zebra_vrf
*zvrf
= NULL
;
1735 struct route_node
*rn
= NULL
;
1736 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1737 bool is_update
= false;
1738 char dest_str
[PREFIX_STRLEN
] = "";
1739 enum dplane_op_e op
;
1740 enum zebra_dplane_result status
;
1741 const struct prefix
*dest_pfx
, *src_pfx
;
1744 bool fib_changed
= false;
1746 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1747 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
1748 dest_pfx
= dplane_ctx_get_dest(ctx
);
1750 /* Note well: only capturing the prefix string if debug is enabled here;
1751 * unconditional log messages will have to generate the string.
1753 if (IS_ZEBRA_DEBUG_DPLANE
)
1754 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1756 /* Locate rn and re(s) from ctx */
1757 rn
= rib_find_rn_from_ctx(ctx
);
1759 if (IS_ZEBRA_DEBUG_DPLANE
) {
1761 "Failed to process dplane results: no route for %s(%u):%s",
1762 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1768 dest
= rib_dest_from_rnode(rn
);
1769 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1771 op
= dplane_ctx_get_op(ctx
);
1772 status
= dplane_ctx_get_status(ctx
);
1774 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1776 "%s(%u:%u):%s Processing dplane result ctx %p, op %s result %s",
1777 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1778 dplane_ctx_get_table(ctx
), dest_str
, ctx
,
1779 dplane_op2str(op
), dplane_res2str(status
));
1782 * Update is a bit of a special case, where we may have both old and new
1783 * routes to post-process.
1785 is_update
= dplane_ctx_is_update(ctx
);
1788 * Take a pass through the routes, look for matches with the context
1791 RNODE_FOREACH_RE(rn
, rib
) {
1794 if (rib_route_match_ctx(rib
, ctx
, false))
1798 /* Check for old route match */
1799 if (is_update
&& (old_re
== NULL
)) {
1800 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1804 /* Have we found the routes we need to work on? */
1805 if (re
&& ((!is_update
|| old_re
)))
1809 seq
= dplane_ctx_get_seq(ctx
);
1812 * Check sequence number(s) to detect stale results before continuing
1815 if (re
->dplane_sequence
!= seq
) {
1816 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1818 "%s(%u):%s Stale dplane result for re %p",
1820 dplane_ctx_get_vrf(ctx
), dest_str
, re
);
1822 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1826 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1827 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1829 "%s(%u:%u):%s Stale dplane result for old_re %p",
1831 dplane_ctx_get_vrf(ctx
), old_re
->table
,
1834 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1838 case DPLANE_OP_ROUTE_INSTALL
:
1839 case DPLANE_OP_ROUTE_UPDATE
:
1840 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1842 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1843 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1846 * On an update operation from the same route type
1847 * context retrieval currently has no way to know
1848 * which was the old and which was the new.
1849 * So don't unset our flags that we just set.
1850 * We know redistribution is ok because the
1851 * old_re in this case is used for nothing
1852 * more than knowing whom to contact if necessary.
1854 if (old_re
&& old_re
!= re
) {
1855 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1856 UNSET_FLAG(old_re
->status
,
1857 ROUTE_ENTRY_INSTALLED
);
1860 /* Update zebra route based on the results in
1861 * the context struct.
1865 rib_update_re_from_ctx(re
, rn
, ctx
);
1868 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1870 "%s(%u:%u):%s no fib change for re",
1872 dplane_ctx_get_vrf(ctx
),
1873 dplane_ctx_get_table(
1878 /* Redistribute if this is the selected re */
1879 if (dest
&& re
== dest
->selected_fib
)
1880 redistribute_update(dest_pfx
, src_pfx
,
1885 * System routes are weird in that they
1886 * allow multiple to be installed that match
1887 * to the same prefix, so after we get the
1888 * result we need to clean them up so that
1889 * we can actually use them.
1891 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1892 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1893 zebra_rib_fixup_system(rn
);
1898 /* Notify route owner */
1899 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1903 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1904 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1906 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1908 zsend_route_notify_owner(re
, dest_pfx
,
1909 ZAPI_ROUTE_FAIL_INSTALL
);
1911 zlog_warn("%s(%u:%u):%s: Route install failed",
1912 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1913 dplane_ctx_get_table(ctx
),
1914 prefix2str(dest_pfx
, dest_str
,
1918 case DPLANE_OP_ROUTE_DELETE
:
1920 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1922 * In the delete case, the zebra core datastructs were
1923 * updated (or removed) at the time the delete was issued,
1924 * so we're just notifying the route owner.
1926 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1928 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1929 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1931 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1937 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1938 zsend_route_notify_owner_ctx(ctx
,
1939 ZAPI_ROUTE_REMOVE_FAIL
);
1941 zlog_warn("%s(%u:%u):%s: Route Deletion failure",
1942 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
1943 dplane_ctx_get_table(ctx
),
1944 prefix2str(dest_pfx
, dest_str
,
1949 * System routes are weird in that they
1950 * allow multiple to be installed that match
1951 * to the same prefix, so after we get the
1952 * result we need to clean them up so that
1953 * we can actually use them.
1955 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1956 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1957 zebra_rib_fixup_system(rn
);
1963 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1964 zebra_rib_evaluate_mpls(rn
);
1968 route_unlock_node(rn
);
1970 /* Return context to dataplane module */
1971 dplane_ctx_fini(&ctx
);
1975 * Count installed/FIB nexthops
1977 static int rib_count_installed_nh(struct route_entry
*re
)
1980 struct nexthop
*nexthop
;
1981 struct nexthop_group
*nhg
;
1983 nhg
= rib_get_fib_nhg(re
);
1985 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
1986 /* The meaningful flag depends on where the installed
1989 if (nhg
== &(re
->fib_ng
)) {
1990 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1993 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1998 nhg
= rib_get_fib_backup_nhg(re
);
2000 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
)) {
2001 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2010 * Handle notification from async dataplane: the dataplane has detected
2011 * some change to a route, and notifies zebra so that the control plane
2012 * can reflect that change.
2014 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
2016 struct route_node
*rn
= NULL
;
2017 struct route_entry
*re
= NULL
;
2019 struct nexthop
*nexthop
;
2020 char dest_str
[PREFIX_STRLEN
] = "";
2021 const struct prefix
*dest_pfx
, *src_pfx
;
2023 bool fib_changed
= false;
2024 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
2025 int start_count
, end_count
;
2026 dest_pfx
= dplane_ctx_get_dest(ctx
);
2027 vrf
= vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
2029 /* Note well: only capturing the prefix string if debug is enabled here;
2030 * unconditional log messages will have to generate the string.
2033 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
2035 /* Locate rn and re(s) from ctx */
2036 rn
= rib_find_rn_from_ctx(ctx
);
2040 "Failed to process dplane notification: no routes for %s(%u:%u):%s",
2041 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2042 dplane_ctx_get_table(ctx
), dest_str
);
2047 dest
= rib_dest_from_rnode(rn
);
2048 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
2051 zlog_debug("%s(%u:%u):%s Processing dplane notif ctx %p",
2052 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2053 dplane_ctx_get_table(ctx
), dest_str
, ctx
);
2056 * Take a pass through the routes, look for matches with the context
2059 RNODE_FOREACH_RE(rn
, re
) {
2060 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
2064 /* No match? Nothing we can do */
2068 "%s(%u:%u):%s Unable to process dplane notification: no entry for type %s",
2069 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2070 dplane_ctx_get_table(ctx
), dest_str
,
2071 zebra_route_string(dplane_ctx_get_type(ctx
)));
2076 /* Ensure we clear the QUEUED flag */
2077 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
2079 /* Is this a notification that ... matters? We mostly care about
2080 * the route that is currently selected for installation; we may also
2081 * get an un-install notification, and handle that too.
2083 if (re
!= dest
->selected_fib
) {
2085 * If we need to, clean up after a delete that was part of
2086 * an update operation.
2089 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
2090 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2094 /* If no nexthops or none installed, ensure that this re
2095 * gets its 'installed' flag cleared.
2097 if (end_count
== 0) {
2098 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2099 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2102 "%s(%u:%u):%s dplane notif, uninstalled type %s route",
2104 dplane_ctx_get_vrf(ctx
),
2105 dplane_ctx_get_table(ctx
), dest_str
,
2107 dplane_ctx_get_type(ctx
)));
2109 /* At least report on the event. */
2112 "%s(%u:%u):%s dplane notif, but type %s not selected_fib",
2114 dplane_ctx_get_vrf(ctx
),
2115 dplane_ctx_get_table(ctx
), dest_str
,
2117 dplane_ctx_get_type(ctx
)));
2122 /* We'll want to determine whether the installation status of the
2123 * route has changed: we'll check the status before processing,
2124 * and then again if there's been a change.
2128 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
2129 start_count
= rib_count_installed_nh(re
);
2131 /* Update zebra's nexthop FIB flags based on the context struct's
2134 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
2139 "%s(%u:%u):%s dplane notification: rib_update returns FALSE",
2140 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2141 dplane_ctx_get_table(ctx
), dest_str
);
2145 * Perform follow-up work if the actual status of the prefix
2148 end_count
= rib_count_installed_nh(re
);
2150 /* Various fib transitions: changed nexthops; from installed to
2151 * not-installed; or not-installed to installed.
2153 if (start_count
> 0 && end_count
> 0) {
2156 "%s(%u:%u):%s applied nexthop changes from dplane notification",
2157 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2158 dplane_ctx_get_table(ctx
), dest_str
);
2160 /* Changed nexthops - update kernel/others */
2161 dplane_route_notif_update(rn
, re
,
2162 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2164 } else if (start_count
== 0 && end_count
> 0) {
2167 "%s(%u:%u):%s installed transition from dplane notification",
2168 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2169 dplane_ctx_get_table(ctx
), dest_str
);
2171 /* We expect this to be the selected route, so we want
2172 * to tell others about this transition.
2174 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2176 /* Changed nexthops - update kernel/others */
2177 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_UPDATE
, ctx
);
2179 /* Redistribute, lsp, and nht update */
2180 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2182 } else if (start_count
> 0 && end_count
== 0) {
2185 "%s(%u:%u):%s un-installed transition from dplane notification",
2186 VRF_LOGNAME(vrf
), dplane_ctx_get_vrf(ctx
),
2187 dplane_ctx_get_table(ctx
), dest_str
);
2189 /* Transition from _something_ installed to _nothing_
2192 /* We expect this to be the selected route, so we want
2193 * to tell others about this transistion.
2195 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2197 /* Changed nexthops - update kernel/others */
2198 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2200 /* Redistribute, lsp, and nht update */
2201 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
2204 /* Make any changes visible for lsp and nexthop-tracking processing */
2205 zebra_rib_evaluate_rn_nexthops(
2206 rn
, zebra_router_get_next_sequence());
2208 zebra_rib_evaluate_mpls(rn
);
2212 route_unlock_node(rn
);
2214 /* Return context to dataplane module */
2215 dplane_ctx_fini(&ctx
);
2218 static void process_subq_nhg(struct listnode
*lnode
)
2220 struct nhg_ctx
*ctx
= NULL
;
2221 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2223 ctx
= listgetdata(lnode
);
2228 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2229 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
2232 rib_nhg_process(ctx
);
2235 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
2237 struct route_node
*rnode
= NULL
;
2238 rib_dest_t
*dest
= NULL
;
2239 struct zebra_vrf
*zvrf
= NULL
;
2241 rnode
= listgetdata(lnode
);
2242 dest
= rib_dest_from_rnode(rnode
);
2245 zvrf
= rib_dest_vrf(dest
);
2249 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2250 struct route_entry
*re
= re_list_first(&dest
->routes
);
2251 char buf
[SRCDEST2STR_BUFFER
];
2253 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2254 zlog_debug("%s(%u:%u):%s: rn %p dequeued from sub-queue %u",
2255 zvrf_name(zvrf
), zvrf_id(zvrf
), re
? re
->table
: 0, buf
,
2260 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2261 RIB_ROUTE_QUEUED(qindex
));
2266 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2267 __func__
, rnode
, rnode
->lock
);
2268 zlog_backtrace(LOG_DEBUG
);
2271 route_unlock_node(rnode
);
2274 /* Take a list of route_node structs and return 1, if there was a record
2275 * picked from it and processed by rib_process(). Don't process more,
2276 * than one RN record; operate only in the specified sub-queue.
2278 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2280 struct listnode
*lnode
= listhead(subq
);
2285 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2286 process_subq_nhg(lnode
);
2288 process_subq_route(lnode
, qindex
);
2290 list_delete_node(subq
, lnode
);
2295 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2296 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2298 * is pointed to the meta queue structure.
2300 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2302 struct meta_queue
*mq
= data
;
2304 uint32_t queue_len
, queue_limit
;
2306 /* Ensure there's room for more dataplane updates */
2307 queue_limit
= dplane_get_in_queue_limit();
2308 queue_len
= dplane_get_in_queue_len();
2309 if (queue_len
> queue_limit
) {
2310 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2311 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2312 queue_len
, queue_limit
);
2314 /* Ensure that the meta-queue is actually enqueued */
2315 if (work_queue_empty(zrouter
.ribq
))
2316 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2318 return WQ_QUEUE_BLOCKED
;
2321 for (i
= 0; i
< MQ_SIZE
; i
++)
2322 if (process_subq(mq
->subq
[i
], i
)) {
2326 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2331 * Look into the RN and queue it into the highest priority queue
2332 * at this point in time for processing.
2334 * We will enqueue a route node only once per invocation.
2336 * There are two possibilities here that should be kept in mind.
2337 * If the original invocation has not been pulled off for processing
2338 * yet, A subsuquent invocation can have a route entry with a better
2339 * meta queue index value and we can have a situation where
2340 * we might have the same node enqueued 2 times. Not necessarily
2341 * an optimal situation but it should be ok.
2343 * The other possibility is that the original invocation has not
2344 * been pulled off for processing yet, A subsusquent invocation
2345 * doesn't have a route_entry with a better meta-queue and the
2346 * original metaqueue index value will win and we'll end up with
2347 * the route node enqueued once.
2349 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2351 struct route_node
*rn
= NULL
;
2352 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2353 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2355 rn
= (struct route_node
*)data
;
2357 RNODE_FOREACH_RE (rn
, curr_re
) {
2358 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2360 if (curr_qindex
<= qindex
) {
2362 qindex
= curr_qindex
;
2369 /* Invariant: at this point we always have rn->info set. */
2370 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2371 RIB_ROUTE_QUEUED(qindex
))) {
2372 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2373 rnode_debug(rn
, re
->vrf_id
,
2374 "rn %p is already queued in sub-queue %u",
2375 (void *)rn
, qindex
);
2379 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2380 listnode_add(mq
->subq
[qindex
], rn
);
2381 route_lock_node(rn
);
2384 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2385 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2386 (void *)rn
, qindex
);
2391 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2393 struct nhg_ctx
*ctx
= NULL
;
2394 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2396 ctx
= (struct nhg_ctx
*)data
;
2401 listnode_add(mq
->subq
[qindex
], ctx
);
2404 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2405 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2411 static int mq_add_handler(void *data
,
2412 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2414 if (zrouter
.ribq
== NULL
) {
2415 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2416 "%s: work_queue does not exist!", __func__
);
2421 * The RIB queue should normally be either empty or holding the only
2422 * work_queue_item element. In the latter case this element would
2423 * hold a pointer to the meta queue structure, which must be used to
2424 * actually queue the route nodes to process. So create the MQ
2425 * holder, if necessary, then push the work into it in any case.
2426 * This semantics was introduced after 0.99.9 release.
2428 if (work_queue_empty(zrouter
.ribq
))
2429 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2431 return mq_add_func(zrouter
.mq
, data
);
2434 /* Add route_node to work queue and schedule processing */
2435 int rib_queue_add(struct route_node
*rn
)
2439 /* Pointless to queue a route_node with no RIB entries to add or remove
2441 if (!rnode_to_ribs(rn
)) {
2442 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2443 __func__
, (void *)rn
, rn
->lock
);
2444 zlog_backtrace(LOG_DEBUG
);
2448 return mq_add_handler(rn
, &rib_meta_queue_add
);
2451 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2455 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2458 /* Create new meta queue.
2459 A destructor function doesn't seem to be necessary here.
2461 static struct meta_queue
*meta_queue_new(void)
2463 struct meta_queue
*new;
2466 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2468 for (i
= 0; i
< MQ_SIZE
; i
++) {
2469 new->subq
[i
] = list_new();
2470 assert(new->subq
[i
]);
2476 void meta_queue_free(struct meta_queue
*mq
)
2480 for (i
= 0; i
< MQ_SIZE
; i
++)
2481 list_delete(&mq
->subq
[i
]);
2483 XFREE(MTYPE_WORK_QUEUE
, mq
);
2486 /* initialise zebra rib work queue */
2487 static void rib_queue_init(void)
2489 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2490 "route_node processing"))) {
2491 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2492 "%s: could not initialise work queue!", __func__
);
2496 /* fill in the work queue spec */
2497 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2498 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2499 zrouter
.ribq
->spec
.completion_func
= NULL
;
2500 /* XXX: TODO: These should be runtime configurable via vty */
2501 zrouter
.ribq
->spec
.max_retries
= 3;
2502 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2503 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2505 if (!(zrouter
.mq
= meta_queue_new())) {
2506 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2507 "%s: could not initialise meta queue!", __func__
);
2513 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2517 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2518 rnh_list_init(&dest
->nht
);
2519 re_list_init(&dest
->routes
);
2520 route_lock_node(rn
); /* rn route table reference */
2527 /* RIB updates are processed via a queue of pointers to route_nodes.
2529 * The queue length is bounded by the maximal size of the routing table,
2530 * as a route_node will not be requeued, if already queued.
2532 * REs are submitted via rib_addnode or rib_delnode which set minimal
2533 * state, or static_install_route (when an existing RE is updated)
2534 * and then submit route_node to queue for best-path selection later.
2535 * Order of add/delete state changes are preserved for any given RE.
2537 * Deleted REs are reaped during best-path selection.
2540 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2541 * |-------->| | best RE, if required
2543 * static_install->|->rib_addqueue...... -> rib_process
2545 * |-------->| |-> rib_unlink
2546 * |-> set ROUTE_ENTRY_REMOVE |
2547 * rib_delnode (RE freed)
2549 * The 'info' pointer of a route_node points to a rib_dest_t
2550 * ('dest'). Queueing state for a route_node is kept on the dest. The
2551 * dest is created on-demand by rib_link() and is kept around at least
2552 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2554 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2556 * - route_nodes: refcounted by:
2557 * - dest attached to route_node:
2558 * - managed by: rib_link/rib_gc_dest
2559 * - route_node processing queue
2560 * - managed by: rib_addqueue, rib_process.
2564 /* Add RE to head of the route node. */
2565 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2569 const char *rmap_name
;
2573 dest
= rib_dest_from_rnode(rn
);
2575 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2576 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2578 dest
= zebra_rib_create_dest(rn
);
2581 re_list_add_head(&dest
->routes
, re
);
2583 afi
= (rn
->p
.family
== AF_INET
)
2585 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2586 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2587 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2589 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2590 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2595 static void rib_addnode(struct route_node
*rn
,
2596 struct route_entry
*re
, int process
)
2598 /* RE node has been un-removed before route-node is processed.
2599 * route_node must hence already be on the queue for processing..
2601 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2602 if (IS_ZEBRA_DEBUG_RIB
)
2603 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2604 (void *)rn
, (void *)re
);
2606 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2609 rib_link(rn
, re
, process
);
2615 * Detach a rib structure from a route_node.
2617 * Note that a call to rib_unlink() should be followed by a call to
2618 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2619 * longer required to be deleted.
2621 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2627 if (IS_ZEBRA_DEBUG_RIB
)
2628 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2631 dest
= rib_dest_from_rnode(rn
);
2633 re_list_del(&dest
->routes
, re
);
2635 if (dest
->selected_fib
== re
)
2636 dest
->selected_fib
= NULL
;
2638 if (re
->nhe
&& re
->nhe_id
) {
2639 assert(re
->nhe
->id
== re
->nhe_id
);
2640 zebra_nhg_decrement_ref(re
->nhe
);
2641 } else if (re
->nhe
&& re
->nhe
->nhg
.nexthop
)
2642 nexthops_free(re
->nhe
->nhg
.nexthop
);
2644 nexthops_free(re
->fib_ng
.nexthop
);
2646 XFREE(MTYPE_RE
, re
);
2649 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2653 if (IS_ZEBRA_DEBUG_RIB
)
2654 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2655 (void *)rn
, (void *)re
);
2656 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2658 afi
= (rn
->p
.family
== AF_INET
)
2660 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2661 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2662 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2664 zebra_del_import_table_entry(zvrf
, rn
, re
);
2665 /* Just clean up if non main table */
2666 if (IS_ZEBRA_DEBUG_RIB
) {
2667 char buf
[SRCDEST2STR_BUFFER
];
2668 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2669 zlog_debug("%s(%u):%s: Freeing route rn %p, re %p (%s)",
2670 vrf_id_to_name(re
->vrf_id
), re
->vrf_id
, buf
,
2671 rn
, re
, zebra_route_string(re
->type
));
2681 * Helper that debugs a single nexthop within a route-entry
2683 static void _route_entry_dump_nh(const struct route_entry
*re
,
2684 const char *straddr
,
2685 const struct nexthop
*nexthop
)
2687 char nhname
[PREFIX_STRLEN
];
2688 char backup_str
[50];
2692 struct interface
*ifp
;
2693 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2695 switch (nexthop
->type
) {
2696 case NEXTHOP_TYPE_BLACKHOLE
:
2697 snprintf(nhname
, sizeof(nhname
), "Blackhole");
2699 case NEXTHOP_TYPE_IFINDEX
:
2700 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
2701 snprintf(nhname
, sizeof(nhname
), "%s",
2702 ifp
? ifp
->name
: "Unknown");
2704 case NEXTHOP_TYPE_IPV4
:
2706 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2707 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2709 case NEXTHOP_TYPE_IPV6
:
2710 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2711 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
, INET6_ADDRSTRLEN
);
2715 backup_str
[0] = '\0';
2716 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_HAS_BACKUP
)) {
2717 snprintf(backup_str
, sizeof(backup_str
), "backup ");
2718 for (i
= 0; i
< nexthop
->backup_num
; i
++) {
2719 snprintf(temp_str
, sizeof(temp_str
), "%d, ",
2720 nexthop
->backup_idx
[i
]);
2721 strlcat(backup_str
, temp_str
, sizeof(backup_str
));
2726 if (nexthop
->weight
)
2727 snprintf(wgt_str
, sizeof(wgt_str
), "wgt %d,", nexthop
->weight
);
2729 zlog_debug("%s: %s %s[%u] vrf %s(%u) %s%s with flags %s%s%s%s%s",
2730 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2731 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2733 wgt_str
, backup_str
,
2734 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2737 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2740 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2743 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2746 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2752 /* This function dumps the contents of a given RE entry into
2753 * standard debug log. Calling function name and IP prefix in
2754 * question are passed as 1st and 2nd arguments.
2756 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2757 union prefixconstptr src_pp
,
2758 const struct route_entry
*re
)
2760 const struct prefix
*src_p
= src_pp
.p
;
2761 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2762 char straddr
[PREFIX_STRLEN
];
2763 char srcaddr
[PREFIX_STRLEN
];
2764 struct nexthop
*nexthop
;
2765 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
2766 struct nexthop_group
*nhg
;
2768 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
2769 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2770 is_srcdst
? " from " : "",
2771 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2773 VRF_LOGNAME(vrf
), re
->vrf_id
);
2774 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2775 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2777 zlog_debug("%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2778 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
,
2780 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2781 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
2782 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
2785 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
2786 _route_entry_dump_nh(re
, straddr
, nexthop
);
2788 if (zebra_nhg_get_backup_nhg(re
->nhe
)) {
2789 zlog_debug("%s: backup nexthops:", straddr
);
2791 nhg
= zebra_nhg_get_backup_nhg(re
->nhe
);
2792 for (ALL_NEXTHOPS_PTR(nhg
, nexthop
))
2793 _route_entry_dump_nh(re
, straddr
, nexthop
);
2796 zlog_debug("%s: dump complete", straddr
);
2800 * This is an exported helper to rtm_read() to dump the strange
2801 * RE entry found by rib_lookup_ipv4_route()
2803 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2805 struct route_table
*table
;
2806 struct route_node
*rn
;
2807 struct route_entry
*re
;
2809 char prefix_buf
[INET_ADDRSTRLEN
];
2811 vrf
= vrf_lookup_by_id(vrf_id
);
2814 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2816 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2817 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2818 VRF_LOGNAME(vrf
), vrf_id
);
2822 /* Scan the RIB table for exactly matching RE entry. */
2823 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2825 /* No route for this prefix. */
2827 zlog_debug("%s:%s(%u) lookup failed for %s", __func__
,
2828 VRF_LOGNAME(vrf
), vrf_id
,
2829 prefix2str((struct prefix
*)p
, prefix_buf
,
2830 sizeof(prefix_buf
)));
2835 route_unlock_node(rn
);
2838 RNODE_FOREACH_RE (rn
, re
) {
2839 zlog_debug("%s:%s(%u) rn %p, re %p: %s, %s", __func__
,
2840 VRF_LOGNAME(vrf
), vrf_id
, (void *)rn
, (void *)re
,
2841 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2844 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2847 route_entry_dump(p
, NULL
, re
);
2851 /* Check if requested address assignment will fail due to another
2852 * route being installed by zebra in FIB already. Take necessary
2853 * actions, if needed: remove such a route from FIB and deSELECT
2854 * corresponding RE entry. Then put affected RN into RIBQ head.
2856 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2858 struct route_table
*table
;
2859 struct route_node
*rn
;
2862 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2863 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2865 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2866 "%s:%s(%u) zebra_vrf_table() returned NULL", __func__
,
2867 VRF_LOGNAME(vrf
), vrf_id
);
2871 /* No matches would be the simplest case. */
2872 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2876 route_unlock_node(rn
);
2878 dest
= rib_dest_from_rnode(rn
);
2879 /* Check all RE entries. In case any changes have to be done, requeue
2880 * the RN into RIBQ head. If the routing message about the new connected
2881 * route (generated by the IP address we are going to assign very soon)
2882 * comes before the RIBQ is processed, the new RE entry will join
2883 * RIBQ record already on head. This is necessary for proper
2885 * of the rest of the RE.
2887 if (dest
->selected_fib
) {
2888 if (IS_ZEBRA_DEBUG_RIB
) {
2889 char buf
[PREFIX_STRLEN
];
2891 vrf_lookup_by_id(dest
->selected_fib
->vrf_id
);
2894 "%s(%u):%s: freeing way for connected prefix",
2895 VRF_LOGNAME(vrf
), dest
->selected_fib
->vrf_id
,
2896 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2897 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2899 rib_uninstall(rn
, dest
->selected_fib
);
2905 * Internal route-add implementation; there are a couple of different public
2906 * signatures. Callers in this path are responsible for the memory they
2907 * allocate: if they allocate a nexthop_group or backup nexthop info, they
2908 * must free those objects. If this returns < 0, an error has occurred and the
2909 * route_entry 're' has not been captured; the caller should free that also.
2911 int rib_add_multipath_nhe(afi_t afi
, safi_t safi
, struct prefix
*p
,
2912 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
2913 struct nhg_hash_entry
*re_nhe
)
2915 struct nhg_hash_entry
*nhe
= NULL
;
2916 struct route_table
*table
;
2917 struct route_node
*rn
;
2918 struct route_entry
*same
= NULL
;
2924 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2927 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2932 if (re
->nhe_id
> 0) {
2933 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2937 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2938 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
2944 /* Lookup nhe from route information */
2945 nhe
= zebra_nhg_rib_find_nhe(re_nhe
, afi
);
2947 char buf
[PREFIX_STRLEN
] = "";
2948 char buf2
[PREFIX_STRLEN
] = "";
2951 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2952 "Zebra failed to find or create a nexthop hash entry for %s%s%s",
2953 prefix2str(p
, buf
, sizeof(buf
)),
2954 src_p
? " from " : "",
2955 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
2963 * Attach the re to the nhe's nexthop group.
2965 * TODO: This will need to change when we start getting IDs from upper
2966 * level protocols, as the refcnt might be wrong, since it checks
2967 * if old_id != new_id.
2969 route_entry_update_nhe(re
, nhe
);
2971 /* Make it sure prefixlen is applied to the prefix. */
2974 apply_mask_ipv6(src_p
);
2976 /* Set default distance by route type. */
2977 if (re
->distance
== 0)
2978 re
->distance
= route_distance(re
->type
);
2980 /* Lookup route node.*/
2981 rn
= srcdest_rnode_get(table
, p
, src_p
);
2984 * If same type of route are installed, treat it as a implicit
2985 * withdraw. If the user has specified the No route replace semantics
2986 * for the install don't do a route replace.
2988 RNODE_FOREACH_RE (rn
, same
) {
2989 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2992 /* Compare various route_entry properties */
2993 if (rib_compare_routes(re
, same
))
2997 /* If this route is kernel/connected route, notify the dataplane. */
2998 if (RIB_SYSTEM_ROUTE(re
)) {
2999 /* Notify dataplane */
3000 dplane_sys_route_add(rn
, re
);
3003 /* Link new re to node.*/
3004 if (IS_ZEBRA_DEBUG_RIB
) {
3005 rnode_debug(rn
, re
->vrf_id
,
3006 "Inserting route rn %p, re %p (%s) existing %p",
3007 rn
, re
, zebra_route_string(re
->type
), same
);
3009 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3010 route_entry_dump(p
, src_p
, re
);
3013 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3014 rib_addnode(rn
, re
, 1);
3017 /* Free implicit route.*/
3019 rib_delnode(rn
, same
);
3021 route_unlock_node(rn
);
3026 * Add a single route.
3028 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
3029 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
3030 struct nexthop_group
*ng
)
3033 struct nhg_hash_entry nhe
;
3038 /* We either need nexthop(s) or an existing nexthop id */
3039 if (ng
== NULL
&& re
->nhe_id
== 0)
3043 * Use a temporary nhe to convey info to the common/main api.
3045 zebra_nhe_init(&nhe
, afi
, (ng
? ng
->nexthop
: NULL
));
3047 nhe
.nhg
.nexthop
= ng
->nexthop
;
3048 else if (re
->nhe_id
> 0)
3049 nhe
.id
= re
->nhe_id
;
3051 ret
= rib_add_multipath_nhe(afi
, safi
, p
, src_p
, re
, &nhe
);
3053 /* In this path, the callers expect memory to be freed. */
3054 nexthop_group_delete(&ng
);
3056 /* In error cases, free the route also */
3058 XFREE(MTYPE_RE
, re
);
3063 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3064 unsigned short instance
, int flags
, struct prefix
*p
,
3065 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3066 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
3067 uint8_t distance
, bool fromkernel
, bool connected_down
)
3069 struct route_table
*table
;
3070 struct route_node
*rn
;
3071 struct route_entry
*re
;
3072 struct route_entry
*fib
= NULL
;
3073 struct route_entry
*same
= NULL
;
3074 struct nexthop
*rtnh
;
3075 char buf2
[INET6_ADDRSTRLEN
];
3078 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
3081 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
3089 apply_mask_ipv6(src_p
);
3091 /* Lookup route node. */
3092 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
3094 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
3096 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
3097 if (src_p
&& src_p
->prefixlen
)
3098 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
3102 if (IS_ZEBRA_DEBUG_RIB
) {
3103 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
3105 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
3106 vrf
->name
, table_id
, dst_buf
,
3107 (src_buf
[0] != '\0') ? " from " : "",
3113 dest
= rib_dest_from_rnode(rn
);
3114 fib
= dest
->selected_fib
;
3116 /* Lookup same type route. */
3117 RNODE_FOREACH_RE (rn
, re
) {
3118 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3121 if (re
->type
!= type
)
3123 if (re
->instance
!= instance
)
3125 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
3126 distance
!= re
->distance
)
3129 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
3131 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
3132 (rtnh
= re
->nhe
->nhg
.nexthop
)
3133 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
3134 if (rtnh
->ifindex
!= nh
->ifindex
)
3140 /* Make sure that the route found has the same gateway. */
3141 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
3150 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
3152 * No guarantee all kernel send nh with labels
3155 if (nexthop_same_no_labels(rtnh
, nh
)) {
3164 /* If same type of route can't be found and this message is from
3168 * In the past(HA!) we could get here because
3169 * we were receiving a route delete from the
3170 * kernel and we're not marking the proto
3171 * as coming from it's appropriate originator.
3172 * Now that we are properly noticing the fact
3173 * that the kernel has deleted our route we
3174 * are not going to get called in this path
3175 * I am going to leave this here because
3176 * this might still work this way on non-linux
3177 * platforms as well as some weird state I have
3178 * not properly thought of yet.
3179 * If we can show that this code path is
3180 * dead then we can remove it.
3182 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
3183 if (IS_ZEBRA_DEBUG_RIB
) {
3184 rnode_debug(rn
, vrf_id
,
3185 "rn %p, re %p (%s) was deleted from kernel, adding",
3187 zebra_route_string(fib
->type
));
3190 || CHECK_FLAG(dest
->flags
, RIB_ROUTE_ANY_QUEUED
)) {
3191 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
3193 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
3195 UNSET_FLAG(rtnh
->flags
,
3199 * This is a non FRR route
3200 * as such we should mark
3203 dest
->selected_fib
= NULL
;
3205 /* This means someone else, other than Zebra,
3207 * a Zebra router from the kernel. We will add
3209 rib_install_kernel(rn
, fib
, NULL
);
3212 if (IS_ZEBRA_DEBUG_RIB
) {
3216 "via %s ifindex %d type %d doesn't exist in rib",
3217 inet_ntop(afi2family(afi
),
3224 "type %d doesn't exist in rib",
3227 route_unlock_node(rn
);
3233 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
3235 rib_install_kernel(rn
, same
, NULL
);
3236 route_unlock_node(rn
);
3241 /* Special handling for IPv4 or IPv6 routes sourced from
3242 * EVPN - the nexthop (and associated MAC) need to be
3243 * uninstalled if no more refs.
3245 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
3246 struct nexthop
*tmp_nh
;
3248 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
3249 struct ipaddr vtep_ip
;
3251 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
3252 if (afi
== AFI_IP
) {
3253 vtep_ip
.ipa_type
= IPADDR_V4
;
3254 memcpy(&(vtep_ip
.ipaddr_v4
),
3255 &(tmp_nh
->gate
.ipv4
),
3256 sizeof(struct in_addr
));
3258 vtep_ip
.ipa_type
= IPADDR_V6
;
3259 memcpy(&(vtep_ip
.ipaddr_v6
),
3260 &(tmp_nh
->gate
.ipv6
),
3261 sizeof(struct in6_addr
));
3263 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
3268 /* Notify dplane if system route changes */
3269 if (RIB_SYSTEM_ROUTE(re
))
3270 dplane_sys_route_del(rn
, same
);
3272 rib_delnode(rn
, same
);
3276 * This is to force an immediate re-eval of this particular
3277 * node via nexthop tracking. Why? Because there are scenarios
3278 * where the interface is flapping and the normal queuing methodology
3279 * will cause down/up events to very very rarely be combined into
3280 * a non-event from nexthop tracking perspective. Leading
3281 * to some fun timing situations with upper level routing protocol
3282 * trying to and failing to install routes during this blip. Especially
3283 * when zebra is under load.
3286 zebra_rib_evaluate_rn_nexthops(rn
,
3287 zebra_router_get_next_sequence());
3288 route_unlock_node(rn
);
3293 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
3294 unsigned short instance
, int flags
, struct prefix
*p
,
3295 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
3296 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
3297 uint8_t distance
, route_tag_t tag
)
3299 struct route_entry
*re
= NULL
;
3300 struct nexthop
*nexthop
= NULL
;
3301 struct nexthop_group
*ng
= NULL
;
3303 /* Allocate new route_entry structure. */
3304 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
3306 re
->instance
= instance
;
3307 re
->distance
= distance
;
3309 re
->metric
= metric
;
3311 re
->table
= table_id
;
3312 re
->vrf_id
= vrf_id
;
3313 re
->uptime
= monotime(NULL
);
3315 re
->nhe_id
= nhe_id
;
3317 /* If the owner of the route supplies a shared nexthop-group id,
3318 * we'll use that. Otherwise, pass the nexthop along directly.
3321 ng
= nexthop_group_new();
3324 nexthop
= nexthop_new();
3326 nexthop_group_add_sorted(ng
, nexthop
);
3329 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
3332 static const char *rib_update_event2str(enum rib_update_event event
)
3334 const char *ret
= "UNKNOWN";
3337 case RIB_UPDATE_KERNEL
:
3338 ret
= "RIB_UPDATE_KERNEL";
3340 case RIB_UPDATE_RMAP_CHANGE
:
3341 ret
= "RIB_UPDATE_RMAP_CHANGE";
3343 case RIB_UPDATE_OTHER
:
3344 ret
= "RIB_UPDATE_OTHER";
3346 case RIB_UPDATE_MAX
:
3354 /* Schedule route nodes to be processed if they match the type */
3355 static void rib_update_route_node(struct route_node
*rn
, int type
)
3357 struct route_entry
*re
, *next
;
3358 bool re_changed
= false;
3360 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3361 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3362 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3371 /* Schedule routes of a particular table (address-family) based on event. */
3372 void rib_update_table(struct route_table
*table
, enum rib_update_event event
)
3374 struct route_node
*rn
;
3376 if (IS_ZEBRA_DEBUG_EVENT
) {
3377 struct zebra_vrf
*zvrf
;
3381 ? ((struct rib_table_info
*)table
->info
)->zvrf
3383 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3385 zlog_debug("%s: %s VRF %s Table %u event %s", __func__
,
3386 table
->info
? afi2str(
3387 ((struct rib_table_info
*)table
->info
)->afi
)
3389 VRF_LOGNAME(vrf
), zvrf
? zvrf
->table_id
: 0,
3390 rib_update_event2str(event
));
3393 /* Walk all routes and queue for processing, if appropriate for
3394 * the trigger event.
3396 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3398 * If we are looking at a route node and the node
3399 * has already been queued we don't
3400 * need to queue it up again
3403 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3404 RIB_ROUTE_ANY_QUEUED
))
3408 case RIB_UPDATE_KERNEL
:
3409 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3411 case RIB_UPDATE_RMAP_CHANGE
:
3412 case RIB_UPDATE_OTHER
:
3413 rib_update_route_node(rn
, ZEBRA_ROUTE_ALL
);
3421 static void rib_update_handle_vrf(vrf_id_t vrf_id
, enum rib_update_event event
)
3423 struct route_table
*table
;
3425 if (IS_ZEBRA_DEBUG_EVENT
)
3426 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3427 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3429 /* Process routes of interested address-families. */
3430 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3432 rib_update_table(table
, event
);
3434 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3436 rib_update_table(table
, event
);
3439 static void rib_update_handle_vrf_all(enum rib_update_event event
)
3441 struct zebra_router_table
*zrt
;
3443 if (IS_ZEBRA_DEBUG_EVENT
)
3444 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3445 rib_update_event2str(event
));
3447 /* Just iterate over all the route tables, rather than vrf lookups */
3448 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3449 rib_update_table(zrt
->table
, event
);
3452 struct rib_update_ctx
{
3453 enum rib_update_event event
;
3458 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3459 enum rib_update_event event
)
3461 struct rib_update_ctx
*ctx
;
3463 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3466 ctx
->vrf_id
= vrf_id
;
3471 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3473 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3476 static int rib_update_handler(struct thread
*thread
)
3478 struct rib_update_ctx
*ctx
;
3480 ctx
= THREAD_ARG(thread
);
3483 rib_update_handle_vrf_all(ctx
->event
);
3485 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
);
3487 rib_update_ctx_fini(&ctx
);
3493 * Thread list to ensure we don't schedule a ton of events
3494 * if interfaces are flapping for instance.
3496 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3498 /* Schedule a RIB update event for specific vrf */
3499 void rib_update_vrf(vrf_id_t vrf_id
, enum rib_update_event event
)
3501 struct rib_update_ctx
*ctx
;
3503 ctx
= rib_update_ctx_init(vrf_id
, event
);
3505 /* Don't worry about making sure multiple rib updates for specific vrf
3506 * are scheduled at once for now. If it becomes a problem, we can use a
3507 * lookup of some sort to keep track of running threads via t_vrf_id
3508 * like how we are doing it in t_rib_update_threads[].
3510 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0, NULL
);
3512 if (IS_ZEBRA_DEBUG_EVENT
)
3513 zlog_debug("%s: Scheduled VRF %s, event %s", __func__
,
3514 vrf_id_to_name(ctx
->vrf_id
),
3515 rib_update_event2str(event
));
3518 /* Schedule a RIB update event for all vrfs */
3519 void rib_update(enum rib_update_event event
)
3521 struct rib_update_ctx
*ctx
;
3523 ctx
= rib_update_ctx_init(0, event
);
3525 ctx
->vrf_all
= true;
3527 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3528 &t_rib_update_threads
[event
]))
3529 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3530 else if (IS_ZEBRA_DEBUG_EVENT
)
3531 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
3532 rib_update_event2str(event
));
3535 /* Delete self installed routes after zebra is relaunched. */
3536 void rib_sweep_table(struct route_table
*table
)
3538 struct route_node
*rn
;
3539 struct route_entry
*re
;
3540 struct route_entry
*next
;
3541 struct nexthop
*nexthop
;
3546 if (IS_ZEBRA_DEBUG_RIB
)
3547 zlog_debug("%s: starting", __func__
);
3549 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3550 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3552 if (IS_ZEBRA_DEBUG_RIB
)
3553 route_entry_dump(&rn
->p
, NULL
, re
);
3555 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3558 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3562 * If routes are older than startup_time then
3563 * we know we read them in from the kernel.
3564 * As such we can safely remove them.
3566 if (zrouter
.startup_time
< re
->uptime
)
3570 * So we are starting up and have received
3571 * routes from the kernel that we have installed
3572 * from a previous run of zebra but not cleaned
3573 * up ( say a kill -9 )
3574 * But since we haven't actually installed
3575 * them yet( we received them from the kernel )
3576 * we don't think they are active.
3577 * So let's pretend they are active to actually
3579 * In all honesty I'm not sure if we should
3580 * mark them as active when we receive them
3581 * This is startup only so probably ok.
3583 * If we ever decide to move rib_sweep_table
3584 * to a different spot (ie startup )
3585 * this decision needs to be revisited
3587 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3588 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3589 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3591 rib_uninstall_kernel(rn
, re
);
3592 rib_delnode(rn
, re
);
3596 if (IS_ZEBRA_DEBUG_RIB
)
3597 zlog_debug("%s: ends", __func__
);
3600 /* Sweep all RIB tables. */
3601 int rib_sweep_route(struct thread
*t
)
3604 struct zebra_vrf
*zvrf
;
3606 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3607 if ((zvrf
= vrf
->info
) == NULL
)
3610 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3611 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3614 zebra_router_sweep_route();
3615 zebra_router_sweep_nhgs();
3620 /* Remove specific by protocol routes from 'table'. */
3621 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3622 struct route_table
*table
)
3624 struct route_node
*rn
;
3625 struct route_entry
*re
;
3626 struct route_entry
*next
;
3627 unsigned long n
= 0;
3630 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3631 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3632 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3634 if (re
->type
== proto
3635 && re
->instance
== instance
) {
3636 rib_delnode(rn
, re
);
3643 /* Remove specific by protocol routes. */
3644 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3647 struct zebra_vrf
*zvrf
;
3648 struct other_route_table
*ort
;
3649 unsigned long cnt
= 0;
3651 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3656 cnt
+= rib_score_proto_table(proto
, instance
,
3657 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3658 + rib_score_proto_table(
3660 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3662 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3663 rib_score_proto_table(proto
, instance
, ort
->table
);
3669 /* Close RIB and clean up kernel routes. */
3670 void rib_close_table(struct route_table
*table
)
3672 struct route_node
*rn
;
3673 struct rib_table_info
*info
;
3679 info
= route_table_get_info(table
);
3681 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3682 dest
= rib_dest_from_rnode(rn
);
3684 if (dest
&& dest
->selected_fib
) {
3685 if (info
->safi
== SAFI_UNICAST
)
3686 hook_call(rib_update
, rn
, NULL
);
3688 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3689 dest
->selected_fib
= NULL
;
3695 * Handler for async dataplane results after a pseudowire installation
3697 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3699 struct zebra_pw
*pw
;
3700 struct zebra_vrf
*vrf
;
3702 /* The pseudowire code assumes success - we act on an error
3703 * result for installation attempts here.
3705 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3708 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3709 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3710 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3712 zebra_pw_install_failure(pw
,
3713 dplane_ctx_get_pw_status(ctx
));
3723 * Handle results from the dataplane system. Dequeue update context
3724 * structs, dispatch to appropriate internal handlers.
3726 static int rib_process_dplane_results(struct thread
*thread
)
3728 struct zebra_dplane_ctx
*ctx
;
3729 struct dplane_ctx_q ctxlist
;
3730 bool shut_p
= false;
3732 /* Dequeue a list of completed updates with one lock/unlock cycle */
3735 TAILQ_INIT(&ctxlist
);
3737 /* Take lock controlling queue of results */
3738 frr_with_mutex(&dplane_mutex
) {
3739 /* Dequeue list of context structs */
3740 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3743 /* Dequeue context block */
3744 ctx
= dplane_ctx_dequeue(&ctxlist
);
3746 /* If we've emptied the results queue, we're done */
3750 /* If zebra is shutting down, avoid processing results,
3751 * just drain the results queue.
3753 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3754 memory_order_relaxed
);
3757 dplane_ctx_fini(&ctx
);
3759 ctx
= dplane_ctx_dequeue(&ctxlist
);
3766 switch (dplane_ctx_get_op(ctx
)) {
3767 case DPLANE_OP_ROUTE_INSTALL
:
3768 case DPLANE_OP_ROUTE_UPDATE
:
3769 case DPLANE_OP_ROUTE_DELETE
:
3771 /* Bit of special case for route updates
3772 * that were generated by async notifications:
3773 * we don't want to continue processing these
3776 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3777 rib_process_result(ctx
);
3779 dplane_ctx_fini(&ctx
);
3783 case DPLANE_OP_ROUTE_NOTIFY
:
3784 rib_process_dplane_notify(ctx
);
3787 case DPLANE_OP_NH_INSTALL
:
3788 case DPLANE_OP_NH_UPDATE
:
3789 case DPLANE_OP_NH_DELETE
:
3790 zebra_nhg_dplane_result(ctx
);
3793 case DPLANE_OP_LSP_INSTALL
:
3794 case DPLANE_OP_LSP_UPDATE
:
3795 case DPLANE_OP_LSP_DELETE
:
3797 /* Bit of special case for LSP updates
3798 * that were generated by async notifications:
3799 * we don't want to continue processing these.
3801 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3802 zebra_mpls_lsp_dplane_result(ctx
);
3804 dplane_ctx_fini(&ctx
);
3808 case DPLANE_OP_LSP_NOTIFY
:
3809 zebra_mpls_process_dplane_notify(ctx
);
3812 case DPLANE_OP_PW_INSTALL
:
3813 case DPLANE_OP_PW_UNINSTALL
:
3814 handle_pw_result(ctx
);
3817 case DPLANE_OP_SYS_ROUTE_ADD
:
3818 case DPLANE_OP_SYS_ROUTE_DELETE
:
3819 /* No further processing in zebra for these. */
3820 dplane_ctx_fini(&ctx
);
3823 case DPLANE_OP_MAC_INSTALL
:
3824 case DPLANE_OP_MAC_DELETE
:
3825 zebra_vxlan_handle_result(ctx
);
3828 case DPLANE_OP_RULE_ADD
:
3829 case DPLANE_OP_RULE_DELETE
:
3830 case DPLANE_OP_RULE_UPDATE
:
3831 zebra_pbr_dplane_result(ctx
);
3834 /* Some op codes not handled here */
3835 case DPLANE_OP_ADDR_INSTALL
:
3836 case DPLANE_OP_ADDR_UNINSTALL
:
3837 case DPLANE_OP_NEIGH_INSTALL
:
3838 case DPLANE_OP_NEIGH_UPDATE
:
3839 case DPLANE_OP_NEIGH_DELETE
:
3840 case DPLANE_OP_VTEP_ADD
:
3841 case DPLANE_OP_VTEP_DELETE
:
3842 case DPLANE_OP_NEIGH_DISCOVER
:
3843 case DPLANE_OP_NONE
:
3844 /* Don't expect this: just return the struct? */
3845 dplane_ctx_fini(&ctx
);
3848 } /* Dispatch by op code */
3850 ctx
= dplane_ctx_dequeue(&ctxlist
);
3859 * Results are returned from the dataplane subsystem, in the context of
3860 * the dataplane pthread. We enqueue the results here for processing by
3861 * the main thread later.
3863 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3865 /* Take lock controlling queue of results */
3866 frr_with_mutex(&dplane_mutex
) {
3867 /* Enqueue context blocks */
3868 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3871 /* Ensure event is signalled to zebra main pthread */
3872 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3879 * Ensure there are no empty slots in the route_info array.
3880 * Every route type in zebra should be present there.
3882 static void check_route_info(void)
3884 int len
= array_size(route_info
);
3887 * ZEBRA_ROUTE_SYSTEM is special cased since
3888 * its key is 0 anyway.
3890 * ZEBRA_ROUTE_ALL is also ignored.
3892 for (int i
= 0; i
< len
; i
++) {
3893 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3895 assert(route_info
[i
].key
);
3896 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3900 /* Routing information base initialize. */
3907 /* Init dataplane, and register for results */
3908 pthread_mutex_init(&dplane_mutex
, NULL
);
3909 TAILQ_INIT(&rib_dplane_q
);
3910 zebra_dplane_init(rib_dplane_results
);
3916 * Get the first vrf id that is greater than the given vrf id if any.
3918 * Returns true if a vrf id was found, false otherwise.
3920 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3924 vrf
= vrf_lookup_by_id(vrf_id
);
3926 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3928 *next_id_p
= vrf
->vrf_id
;
3937 * rib_tables_iter_next
3939 * Returns the next table in the iteration.
3941 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3943 struct route_table
*table
;
3946 * Array that helps us go over all AFI/SAFI combinations via one
3949 static const struct {
3953 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3954 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3955 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3960 switch (iter
->state
) {
3962 case RIB_TABLES_ITER_S_INIT
:
3963 iter
->vrf_id
= VRF_DEFAULT
;
3964 iter
->afi_safi_ix
= -1;
3968 case RIB_TABLES_ITER_S_ITERATING
:
3969 iter
->afi_safi_ix
++;
3972 while (iter
->afi_safi_ix
3973 < (int)array_size(afi_safis
)) {
3974 table
= zebra_vrf_table(
3975 afi_safis
[iter
->afi_safi_ix
].afi
,
3976 afi_safis
[iter
->afi_safi_ix
].safi
,
3981 iter
->afi_safi_ix
++;
3985 * Found another table in this vrf.
3991 * Done with all tables in the current vrf, go to the
3995 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3998 iter
->afi_safi_ix
= 0;
4003 case RIB_TABLES_ITER_S_DONE
:
4008 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4010 iter
->state
= RIB_TABLES_ITER_S_DONE
;