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"
40 #include "zebra/zebra_router.h"
41 #include "zebra/connected.h"
42 #include "zebra/debug.h"
43 #include "zebra/interface.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/rib.h"
47 #include "zebra/zapi_msg.h"
48 #include "zebra/zebra_errors.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/zebra_routemap.h"
53 #include "zebra/zebra_vrf.h"
54 #include "zebra/zebra_vxlan.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_dplane.h"
57 #include "zebra/zebra_nhg.h"
60 * Event, list, and mutex for delivery of dataplane results
62 static pthread_mutex_t dplane_mutex
;
63 static struct thread
*t_dplane
;
64 static struct dplane_ctx_q rib_dplane_q
;
66 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
69 /* Should we allow non Quagga processes to delete our routes */
70 extern int allow_delete
;
72 /* Each route type's string and default distance value. */
77 } route_info
[ZEBRA_ROUTE_MAX
] = {
78 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
79 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
80 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
81 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
82 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
83 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
84 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
85 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
86 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
87 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
88 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
89 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
90 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
91 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
92 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
93 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
94 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
95 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
97 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
99 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
100 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
101 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
102 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 4},
103 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 4},
104 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 2},
105 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 4}
106 /* Any new route type added to zebra, should be mirrored here */
108 /* no entry/default: 150 */
111 /* RPF lookup behaviour */
112 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
115 static void __attribute__((format(printf
, 5, 6)))
116 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
117 int priority
, const char *msgfmt
, ...)
119 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
123 va_start(ap
, msgfmt
);
124 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
128 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
129 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
131 if (info
->safi
== SAFI_MULTICAST
)
132 strlcat(buf
, " (MRIB)", sizeof(buf
));
134 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
137 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
140 #define rnode_debug(node, vrf_id, ...) \
141 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
142 #define rnode_info(node, ...) \
143 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
145 uint8_t route_distance(int type
)
149 if ((unsigned)type
>= array_size(route_info
))
152 distance
= route_info
[type
].distance
;
157 int is_zebra_valid_kernel_table(uint32_t table_id
)
160 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
161 || (table_id
== RT_TABLE_COMPAT
))
168 int is_zebra_main_routing_table(uint32_t table_id
)
170 if (table_id
== RT_TABLE_MAIN
)
175 int zebra_check_addr(const struct prefix
*p
)
177 if (p
->family
== AF_INET
) {
180 addr
= p
->u
.prefix4
.s_addr
;
183 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
184 || IPV4_LINKLOCAL(addr
))
187 if (p
->family
== AF_INET6
) {
188 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
190 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
196 /* Add nexthop to the end of a rib node's nexthop list */
197 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
199 nexthop_add(&re
->ng
.nexthop
, nexthop
);
205 * copy_nexthop - copy a nexthop to the rib structure.
207 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
209 assert(!re
->ng
.nexthop
);
210 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
211 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
215 /* Delete specified nexthop from the list. */
216 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
219 nexthop
->next
->prev
= nexthop
->prev
;
221 nexthop
->prev
->next
= nexthop
->next
;
223 re
->ng
.nexthop
= nexthop
->next
;
228 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
232 struct nexthop
*nexthop
;
234 nexthop
= nexthop_new();
235 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
236 nexthop
->ifindex
= ifindex
;
237 nexthop
->vrf_id
= nh_vrf_id
;
239 route_entry_nexthop_add(re
, nexthop
);
244 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
245 struct in_addr
*ipv4
,
249 struct nexthop
*nexthop
;
251 nexthop
= nexthop_new();
252 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
253 nexthop
->vrf_id
= nh_vrf_id
;
254 nexthop
->gate
.ipv4
= *ipv4
;
256 nexthop
->src
.ipv4
= *src
;
258 route_entry_nexthop_add(re
, nexthop
);
263 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
264 struct in_addr
*ipv4
,
269 struct nexthop
*nexthop
;
270 struct interface
*ifp
;
272 nexthop
= nexthop_new();
273 nexthop
->vrf_id
= nh_vrf_id
;
274 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
275 nexthop
->gate
.ipv4
= *ipv4
;
277 nexthop
->src
.ipv4
= *src
;
278 nexthop
->ifindex
= ifindex
;
279 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
280 /*Pending: need to think if null ifp here is ok during bootup?
281 There was a crash because ifp here was coming to be NULL */
283 if (connected_is_unnumbered(ifp
))
284 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
286 route_entry_nexthop_add(re
, nexthop
);
291 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
292 struct in6_addr
*ipv6
,
295 struct nexthop
*nexthop
;
297 nexthop
= nexthop_new();
298 nexthop
->vrf_id
= nh_vrf_id
;
299 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
300 nexthop
->gate
.ipv6
= *ipv6
;
302 route_entry_nexthop_add(re
, nexthop
);
307 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
308 struct in6_addr
*ipv6
,
312 struct nexthop
*nexthop
;
314 nexthop
= nexthop_new();
315 nexthop
->vrf_id
= nh_vrf_id
;
316 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
317 nexthop
->gate
.ipv6
= *ipv6
;
318 nexthop
->ifindex
= ifindex
;
320 route_entry_nexthop_add(re
, nexthop
);
325 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
326 enum blackhole_type bh_type
)
328 struct nexthop
*nexthop
;
330 nexthop
= nexthop_new();
331 nexthop
->vrf_id
= VRF_DEFAULT
;
332 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
333 nexthop
->bh_type
= bh_type
;
335 route_entry_nexthop_add(re
, nexthop
);
340 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
341 union g_addr
*addr
, struct route_node
**rn_out
)
344 struct route_table
*table
;
345 struct route_node
*rn
;
346 struct route_entry
*match
= NULL
;
349 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
353 memset(&p
, 0, sizeof(struct prefix
));
356 p
.u
.prefix4
= addr
->ipv4
;
357 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
359 p
.u
.prefix6
= addr
->ipv6
;
360 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
363 rn
= route_node_match(table
, (struct prefix
*)&p
);
368 route_unlock_node(rn
);
370 dest
= rib_dest_from_rnode(rn
);
371 if (dest
&& dest
->selected_fib
372 && !CHECK_FLAG(dest
->selected_fib
->status
,
373 ROUTE_ENTRY_REMOVED
))
374 match
= dest
->selected_fib
;
376 /* If there is no selected route or matched route is EGP, go up
381 } while (rn
&& rn
->info
== NULL
);
385 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
386 if (!CHECK_FLAG(match
->status
,
387 ROUTE_ENTRY_INSTALLED
))
399 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
401 struct route_node
**rn_out
)
403 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
404 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
405 union g_addr gaddr
= {.ipv4
= addr
};
407 switch (ipv4_multicast_mode
) {
408 case MCAST_MRIB_ONLY
:
409 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
411 case MCAST_URIB_ONLY
:
412 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
413 case MCAST_NO_CONFIG
:
414 case MCAST_MIX_MRIB_FIRST
:
415 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
418 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
421 case MCAST_MIX_DISTANCE
:
422 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
423 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
425 re
= ure
->distance
< mre
->distance
? ure
: mre
;
431 case MCAST_MIX_PFXLEN
:
432 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
433 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
435 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
444 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
446 if (IS_ZEBRA_DEBUG_RIB
) {
448 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
450 zlog_debug("%s: %s: vrf: %u found %s, using %s",
451 __func__
, buf
, vrf_id
,
452 mre
? (ure
? "MRIB+URIB" : "MRIB")
453 : ure
? "URIB" : "nothing",
454 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
459 void multicast_mode_ipv4_set(enum multicast_mode mode
)
461 if (IS_ZEBRA_DEBUG_RIB
)
462 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
464 ipv4_multicast_mode
= mode
;
467 enum multicast_mode
multicast_mode_ipv4_get(void)
469 return ipv4_multicast_mode
;
472 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
474 struct route_table
*table
;
475 struct route_node
*rn
;
476 struct route_entry
*match
= NULL
;
480 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
484 rn
= route_node_lookup(table
, (struct prefix
*)p
);
486 /* No route for this prefix. */
491 route_unlock_node(rn
);
492 dest
= rib_dest_from_rnode(rn
);
494 if (dest
&& dest
->selected_fib
495 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
496 match
= dest
->selected_fib
;
501 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
504 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
511 * Is this RIB labeled-unicast? It must be of type BGP and all paths
512 * (nexthops) must have a label.
514 int zebra_rib_labeled_unicast(struct route_entry
*re
)
516 struct nexthop
*nexthop
= NULL
;
518 if (re
->type
!= ZEBRA_ROUTE_BGP
)
521 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
522 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
528 /* Update flag indicates whether this is a "replace" or not. Currently, this
529 * is only used for IPv4.
531 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
532 struct route_entry
*old
)
534 struct nexthop
*nexthop
;
535 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
536 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
537 const struct prefix
*p
, *src_p
;
538 enum zebra_dplane_result ret
;
540 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
542 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
544 if (info
->safi
!= SAFI_UNICAST
) {
545 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
546 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
549 struct nexthop
*prev
;
551 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
552 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
553 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
556 if (nexthop_same_firsthop(nexthop
, prev
)) {
557 SET_FLAG(nexthop
->flags
,
558 NEXTHOP_FLAG_DUPLICATE
);
566 * If this is a replace to a new RE let the originator of the RE
567 * know that they've lost
569 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
570 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
572 /* Update fib selection */
573 dest
->selected_fib
= re
;
576 * Make sure we update the FPM any time we send new information to
579 hook_call(rib_update
, rn
, "installing in kernel");
581 /* Send add or update */
583 ret
= dplane_route_update(rn
, re
, old
);
585 ret
= dplane_route_add(rn
, re
);
588 case ZEBRA_DPLANE_REQUEST_QUEUED
:
589 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
592 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
594 /* Free old FIB nexthop group */
595 if (old
->fib_ng
.nexthop
) {
596 nexthops_free(old
->fib_ng
.nexthop
);
597 old
->fib_ng
.nexthop
= NULL
;
600 if (!RIB_SYSTEM_ROUTE(old
)) {
601 /* Clear old route's FIB flags */
602 for (ALL_NEXTHOPS(old
->ng
, nexthop
)) {
603 UNSET_FLAG(nexthop
->flags
,
610 zvrf
->installs_queued
++;
612 case ZEBRA_DPLANE_REQUEST_FAILURE
:
614 char str
[SRCDEST2STR_BUFFER
];
616 srcdest_rnode2str(rn
, str
, sizeof(str
));
617 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
618 "%u:%s: Failed to enqueue dataplane install",
622 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
631 /* Uninstall the route from kernel. */
632 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
634 struct nexthop
*nexthop
;
635 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
636 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
638 if (info
->safi
!= SAFI_UNICAST
) {
639 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
640 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
641 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
646 * Make sure we update the FPM any time we send new information to
649 hook_call(rib_update
, rn
, "uninstalling from kernel");
651 switch (dplane_route_delete(rn
, re
)) {
652 case ZEBRA_DPLANE_REQUEST_QUEUED
:
654 zvrf
->removals_queued
++;
656 case ZEBRA_DPLANE_REQUEST_FAILURE
:
658 char str
[SRCDEST2STR_BUFFER
];
660 srcdest_rnode2str(rn
, str
, sizeof(str
));
661 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
662 "%u:%s: Failed to enqueue dataplane uninstall",
666 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
675 /* Uninstall the route from kernel. */
676 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
678 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
679 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
680 struct nexthop
*nexthop
;
682 if (dest
&& dest
->selected_fib
== re
) {
683 if (info
->safi
== SAFI_UNICAST
)
684 hook_call(rib_update
, rn
, "rib_uninstall");
686 /* If labeled-unicast route, uninstall transit LSP. */
687 if (zebra_rib_labeled_unicast(re
))
688 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
690 rib_uninstall_kernel(rn
, re
);
692 dest
->selected_fib
= NULL
;
694 /* Free FIB nexthop group, if present */
695 if (re
->fib_ng
.nexthop
) {
696 nexthops_free(re
->fib_ng
.nexthop
);
697 re
->fib_ng
.nexthop
= NULL
;
700 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
701 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
704 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
705 const struct prefix
*p
, *src_p
;
707 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
709 redistribute_delete(p
, src_p
, re
);
710 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
715 * rib_can_delete_dest
717 * Returns TRUE if the given dest can be deleted from the table.
719 static int rib_can_delete_dest(rib_dest_t
*dest
)
721 if (re_list_first(&dest
->routes
)) {
726 * Unresolved rnh's are stored on the default route's list
728 * dest->rnode can also be the source prefix node in an
729 * ipv6 sourcedest table. Fortunately the prefix of a
730 * source prefix node can never be the default prefix.
732 if (is_default_prefix(&dest
->rnode
->p
))
736 * Don't delete the dest if we have to update the FPM about this
739 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
740 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
746 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
748 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
752 * We are storing the rnh's associated withb
753 * the tracked nexthop as a list of the rn's.
754 * Unresolved rnh's are placed at the top
755 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
756 * As such for each rn we need to walk up the tree
757 * and see if any rnh's need to see if they
758 * would match a more specific route
761 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
762 char buf
[PREFIX_STRLEN
];
764 zlog_debug("%s: %s Being examined for Nexthop Tracking",
766 srcdest_rnode2str(rn
, buf
, sizeof(buf
)));
771 dest
= rib_dest_from_rnode(rn
);
775 * If we have any rnh's stored in the nht list
776 * then we know that this route node was used for
777 * nht resolution and as such we need to call the
778 * nexthop tracking evaluation code
780 frr_each (rnh_list
, &dest
->nht
, rnh
) {
781 struct zebra_vrf
*zvrf
=
782 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
783 struct prefix
*p
= &rnh
->node
->p
;
785 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
786 char buf1
[PREFIX_STRLEN
];
787 char buf2
[PREFIX_STRLEN
];
789 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
791 srcdest_rnode2str(rn
, buf1
,
793 prefix2str(p
, buf2
, sizeof(buf2
)),
798 * If we have evaluated this node on this pass
799 * already, due to following the tree up
800 * then we know that we can move onto the next
803 * Additionally we call zebra_evaluate_rnh
804 * when we gc the dest. In this case we know
805 * that there must be no other re's where
806 * we were originally as such we know that
807 * that sequence number is ok to respect.
809 if (rnh
->seqno
== seq
) {
810 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
812 "\tNode processed and moved already");
817 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
823 dest
= rib_dest_from_rnode(rn
);
830 * Garbage collect the rib dest corresponding to the given route node
833 * Returns TRUE if the dest was deleted, FALSE otherwise.
835 int rib_gc_dest(struct route_node
*rn
)
839 dest
= rib_dest_from_rnode(rn
);
843 if (!rib_can_delete_dest(dest
))
846 if (IS_ZEBRA_DEBUG_RIB
) {
847 struct zebra_vrf
*zvrf
;
849 zvrf
= rib_dest_vrf(dest
);
850 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
853 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
856 rnh_list_fini(&dest
->nht
);
857 XFREE(MTYPE_RIB_DEST
, dest
);
861 * Release the one reference that we keep on the route node.
863 route_unlock_node(rn
);
867 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
868 struct route_entry
*new)
870 hook_call(rib_update
, rn
, "new route selected");
872 /* Update real nexthop. This may actually determine if nexthop is active
874 if (!nexthop_group_active_nexthop_num(&new->ng
)) {
875 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
879 if (IS_ZEBRA_DEBUG_RIB
) {
880 char buf
[SRCDEST2STR_BUFFER
];
881 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
882 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
883 zvrf_id(zvrf
), buf
, rn
, new,
884 zebra_route_string(new->type
));
887 /* If labeled-unicast route, install transit LSP. */
888 if (zebra_rib_labeled_unicast(new))
889 zebra_mpls_lsp_install(zvrf
, rn
, new);
891 rib_install_kernel(rn
, new, NULL
);
893 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
896 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
897 struct route_entry
*old
)
899 hook_call(rib_update
, rn
, "removing existing route");
901 /* Uninstall from kernel. */
902 if (IS_ZEBRA_DEBUG_RIB
) {
903 char buf
[SRCDEST2STR_BUFFER
];
904 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
905 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
906 zvrf_id(zvrf
), buf
, rn
, old
,
907 zebra_route_string(old
->type
));
910 /* If labeled-unicast route, uninstall transit LSP. */
911 if (zebra_rib_labeled_unicast(old
))
912 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
914 rib_uninstall_kernel(rn
, old
);
916 /* Update nexthop for route, reset changed flag. */
917 /* Note: this code also handles the Linux case when an interface goes
918 * down, causing the kernel to delete routes without sending DELROUTE
921 if (RIB_KERNEL_ROUTE(old
))
922 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
924 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
927 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
928 struct route_node
*rn
,
929 struct route_entry
*old
,
930 struct route_entry
*new)
935 * We have to install or update if a new route has been selected or
936 * something has changed.
938 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
939 hook_call(rib_update
, rn
, "updating existing route");
941 /* Update the nexthop; we could determine here that nexthop is
943 if (nexthop_group_active_nexthop_num(&new->ng
))
946 /* If nexthop is active, install the selected route, if
948 * the install succeeds, cleanup flags for prior route, if
953 if (IS_ZEBRA_DEBUG_RIB
) {
954 char buf
[SRCDEST2STR_BUFFER
];
955 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
958 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
959 zvrf_id(zvrf
), buf
, rn
, new,
960 zebra_route_string(new->type
),
962 zebra_route_string(old
->type
));
965 "%u:%s: Updating route rn %p, re %p (%s)",
966 zvrf_id(zvrf
), buf
, rn
, new,
967 zebra_route_string(new->type
));
970 /* If labeled-unicast route, uninstall transit LSP. */
971 if (zebra_rib_labeled_unicast(old
))
972 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
975 * Non-system route should be installed.
976 * If labeled-unicast route, install transit
979 if (zebra_rib_labeled_unicast(new))
980 zebra_mpls_lsp_install(zvrf
, rn
, new);
982 rib_install_kernel(rn
, new, old
);
986 * If nexthop for selected route is not active or install
988 * may need to uninstall and delete for redistribution.
991 if (IS_ZEBRA_DEBUG_RIB
) {
992 char buf
[SRCDEST2STR_BUFFER
];
993 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
996 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
997 zvrf_id(zvrf
), buf
, rn
, new,
998 zebra_route_string(new->type
),
1000 zebra_route_string(old
->type
));
1003 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
1004 zvrf_id(zvrf
), buf
, rn
, new,
1005 zebra_route_string(new->type
));
1008 /* If labeled-unicast route, uninstall transit LSP. */
1009 if (zebra_rib_labeled_unicast(old
))
1010 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1012 rib_uninstall_kernel(rn
, old
);
1016 * Same route selected; check if in the FIB and if not,
1017 * re-install. This is housekeeping code to deal with
1018 * race conditions in kernel with linux netlink reporting
1019 * interface up before IPv4 or IPv6 protocol is ready
1022 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1023 RIB_SYSTEM_ROUTE(new))
1024 rib_install_kernel(rn
, new, NULL
);
1027 /* Update prior route. */
1029 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1031 /* Clear changed flag. */
1032 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1035 /* Check if 'alternate' RIB entry is better than 'current'. */
1036 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1037 struct route_entry
*alternate
)
1039 if (current
== NULL
)
1042 /* filter route selection in following order:
1043 * - connected beats other types
1044 * - if both connected, loopback or vrf wins
1045 * - lower distance beats higher
1046 * - lower metric beats higher for equal distance
1047 * - last, hence oldest, route wins tie break.
1050 /* Connected routes. Check to see if either are a vrf
1051 * or loopback interface. If not, pick the last connected
1052 * route of the set of lowest metric connected routes.
1054 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1055 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1058 /* both are connected. are either loop or vrf? */
1059 struct nexthop
*nexthop
= NULL
;
1061 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1062 if (if_is_loopback_or_vrf(if_lookup_by_index(
1063 nexthop
->ifindex
, alternate
->vrf_id
)))
1067 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1068 if (if_is_loopback_or_vrf(if_lookup_by_index(
1069 nexthop
->ifindex
, current
->vrf_id
)))
1073 /* Neither are loop or vrf so pick best metric */
1074 if (alternate
->metric
<= current
->metric
)
1080 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1083 /* higher distance loses */
1084 if (alternate
->distance
< current
->distance
)
1086 if (current
->distance
< alternate
->distance
)
1089 /* metric tie-breaks equal distance */
1090 if (alternate
->metric
<= current
->metric
)
1096 /* Core function for processing routing information base. */
1097 static void rib_process(struct route_node
*rn
)
1099 struct route_entry
*re
;
1100 struct route_entry
*next
;
1101 struct route_entry
*old_selected
= NULL
;
1102 struct route_entry
*new_selected
= NULL
;
1103 struct route_entry
*old_fib
= NULL
;
1104 struct route_entry
*new_fib
= NULL
;
1105 struct route_entry
*best
= NULL
;
1106 char buf
[SRCDEST2STR_BUFFER
];
1108 struct zebra_vrf
*zvrf
= NULL
;
1109 const struct prefix
*p
, *src_p
;
1111 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1112 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1116 dest
= rib_dest_from_rnode(rn
);
1118 zvrf
= rib_dest_vrf(dest
);
1119 vrf_id
= zvrf_id(zvrf
);
1122 if (IS_ZEBRA_DEBUG_RIB
)
1123 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1125 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1126 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1129 * we can have rn's that have a NULL info pointer
1130 * (dest). As such let's not let the deref happen
1131 * additionally we know RNODE_FOREACH_RE_SAFE
1132 * will not iterate so we are ok.
1135 old_fib
= dest
->selected_fib
;
1137 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1138 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1140 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1141 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1142 re
->status
, re
->flags
, re
->distance
,
1145 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1147 /* Currently selected re. */
1148 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1149 assert(old_selected
== NULL
);
1153 /* Skip deleted entries from selection */
1154 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1157 /* Skip unreachable nexthop. */
1158 /* This first call to nexthop_active_update is merely to
1159 * determine if there's any change to nexthops associated
1160 * with this RIB entry. Now, rib_process() can be invoked due
1161 * to an external event such as link down or due to
1162 * next-hop-tracking evaluation. In the latter case,
1163 * a decision has already been made that the NHs have changed.
1164 * So, no need to invoke a potentially expensive call again.
1165 * Further, since the change might be in a recursive NH which
1166 * is not caught in the nexthop_active_update() code. Thus, we
1167 * might miss changes to recursive NHs.
1169 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1170 && !nexthop_active_update(rn
, re
)) {
1171 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1172 /* XXX: HERE BE DRAGONS!!!!!
1173 * In all honesty, I have not yet figured out
1174 * what this part does or why the
1175 * ROUTE_ENTRY_CHANGED test above is correct
1176 * or why we need to delete a route here, and
1177 * also not whether this concerns both selected
1178 * and fib route, or only selected
1181 * This entry was denied by the 'ip protocol
1182 * table' route-map, we need to delete it */
1183 if (re
!= old_selected
) {
1184 if (IS_ZEBRA_DEBUG_RIB
)
1186 "%s: %u:%s: imported via import-table but denied "
1187 "by the ip protocol table route-map",
1188 __func__
, vrf_id
, buf
);
1191 SET_FLAG(re
->status
,
1192 ROUTE_ENTRY_REMOVED
);
1198 /* Infinite distance. */
1199 if (re
->distance
== DISTANCE_INFINITY
) {
1200 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1204 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1205 best
= rib_choose_best(new_fib
, re
);
1206 if (new_fib
&& best
!= new_fib
)
1207 UNSET_FLAG(new_fib
->status
,
1208 ROUTE_ENTRY_CHANGED
);
1211 best
= rib_choose_best(new_selected
, re
);
1212 if (new_selected
&& best
!= new_selected
)
1213 UNSET_FLAG(new_selected
->status
,
1214 ROUTE_ENTRY_CHANGED
);
1215 new_selected
= best
;
1218 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1219 } /* RNODE_FOREACH_RE */
1221 /* If no FIB override route, use the selected route also for FIB */
1222 if (new_fib
== NULL
)
1223 new_fib
= new_selected
;
1225 /* After the cycle is finished, the following pointers will be set:
1226 * old_selected --- RE entry currently having SELECTED
1227 * new_selected --- RE entry that is newly SELECTED
1228 * old_fib --- RE entry currently in kernel FIB
1229 * new_fib --- RE entry that is newly to be in kernel FIB
1231 * new_selected will get SELECTED flag, and is going to be redistributed
1232 * the zclients. new_fib (which can be new_selected) will be installed
1236 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1238 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1239 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1240 (void *)old_fib
, (void *)new_fib
);
1243 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1244 * fib == selected */
1245 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1246 ROUTE_ENTRY_CHANGED
);
1248 /* Update fib according to selection results */
1249 if (new_fib
&& old_fib
)
1250 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1252 rib_process_add_fib(zvrf
, rn
, new_fib
);
1254 rib_process_del_fib(zvrf
, rn
, old_fib
);
1256 /* Update SELECTED entry */
1257 if (old_selected
!= new_selected
|| selected_changed
) {
1259 if (new_selected
&& new_selected
!= new_fib
)
1260 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1263 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1267 redistribute_delete(p
, src_p
, old_selected
);
1268 if (old_selected
!= new_selected
)
1269 UNSET_FLAG(old_selected
->flags
,
1270 ZEBRA_FLAG_SELECTED
);
1274 /* Remove all RE entries queued for removal */
1275 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1276 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1277 if (IS_ZEBRA_DEBUG_RIB
) {
1278 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1279 (void *)rn
, (void *)re
);
1286 * Check if the dest can be deleted now.
1291 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1293 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1294 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1299 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1300 if (IS_ZEBRA_DEBUG_MPLS
)
1302 "%u: Scheduling all LSPs upon RIB completion",
1304 zebra_mpls_lsp_schedule(zvrf
);
1305 mpls_unmark_lsps_for_processing(rn
);
1310 * Utility to match route with dplane context data
1312 static bool rib_route_match_ctx(const struct route_entry
*re
,
1313 const struct zebra_dplane_ctx
*ctx
,
1316 bool result
= false;
1320 * In 'update' case, we test info about the 'previous' or
1323 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1324 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1327 /* TODO -- we're using this extra test, but it's not
1328 * exactly clear why.
1330 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1331 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1332 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1339 * Ordinary, single-route case using primary context info
1341 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1342 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1343 /* Skip route that's been deleted */
1347 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1348 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1351 /* TODO -- we're using this extra test, but it's not
1352 * exactly clear why.
1354 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1355 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1356 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1367 static void zebra_rib_fixup_system(struct route_node
*rn
)
1369 struct route_entry
*re
;
1371 RNODE_FOREACH_RE(rn
, re
) {
1372 struct nexthop
*nhop
;
1374 if (!RIB_SYSTEM_ROUTE(re
))
1377 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1380 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1382 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1383 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1386 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1392 * Update a route from a dplane context. This consolidates common code
1393 * that can be used in processing of results from FIB updates, and in
1394 * async notification processing.
1395 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1397 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1398 struct route_node
*rn
,
1399 struct zebra_dplane_ctx
*ctx
)
1401 char dest_str
[PREFIX_STRLEN
] = "";
1402 char nh_str
[NEXTHOP_STRLEN
];
1403 struct nexthop
*nexthop
, *ctx_nexthop
;
1405 const struct nexthop_group
*ctxnhg
;
1406 bool is_selected
= false; /* Is 're' currently the selected re? */
1407 bool changed_p
= false; /* Change to nexthops? */
1410 /* Note well: only capturing the prefix string if debug is enabled here;
1411 * unconditional log messages will have to generate the string.
1413 if (IS_ZEBRA_DEBUG_RIB
)
1414 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1416 dest
= rib_dest_from_rnode(rn
);
1418 is_selected
= (re
== dest
->selected_fib
);
1420 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1421 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1422 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1424 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1425 * If the installed set differs from the set requested by the rib/owner,
1426 * we use the fib-specific nexthop-group to record the actual FIB
1431 * First check the fib nexthop-group, if it's present. The comparison
1432 * here is quite strict: we require that the fib sets match exactly.
1436 if (re
->fib_ng
.nexthop
== NULL
)
1441 /* First check the route's fib nexthops */
1442 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1444 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1448 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1450 if (nexthop_same(ctx_nexthop
, nexthop
))
1454 if (ctx_nexthop
== NULL
) {
1455 /* Nexthop not in the new installed set */
1456 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1457 nexthop2str(nexthop
, nh_str
,
1459 zlog_debug("update_from_ctx: no match for fib nh %s",
1471 /* Check the new installed set */
1473 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1475 if (CHECK_FLAG(ctx_nexthop
->flags
,
1476 NEXTHOP_FLAG_RECURSIVE
))
1479 /* Compare with the current group's nexthops */
1481 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1482 if (nexthop_same(nexthop
, ctx_nexthop
))
1486 if (nexthop
== NULL
) {
1487 /* Nexthop not in the old installed set */
1488 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1489 nexthop2str(ctx_nexthop
, nh_str
,
1491 zlog_debug("update_from_ctx: no fib match for notif nh %s",
1501 /* If the new FIB set matches the existing FIB set, we're done. */
1503 if (IS_ZEBRA_DEBUG_RIB
)
1504 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1505 re
->vrf_id
, dest_str
);
1508 } else if (re
->fib_ng
.nexthop
) {
1510 * Free stale fib list and move on to check the rib nhg.
1512 if (IS_ZEBRA_DEBUG_RIB
)
1513 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1514 re
->vrf_id
, dest_str
);
1515 nexthops_free(re
->fib_ng
.nexthop
);
1516 re
->fib_ng
.nexthop
= NULL
;
1518 /* Note that the installed nexthops have changed */
1521 if (IS_ZEBRA_DEBUG_RIB
)
1522 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1523 re
->vrf_id
, dest_str
);
1527 * Compare with the rib nexthop group. The comparison here is different:
1528 * the RIB group may be a superset of the list installed in the FIB. We
1529 * walk the RIB group, looking for the 'installable' candidate
1530 * nexthops, and then check those against the set
1531 * that is actually installed.
1534 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1536 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1539 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1542 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1544 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1545 if (nexthop_same(ctx_nexthop
, nexthop
))
1549 /* If the FIB doesn't know about the nexthop,
1550 * it's not installed
1552 if (ctx_nexthop
== NULL
) {
1553 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1554 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1555 zlog_debug("update_from_ctx: no notif match for rib nh %s",
1560 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1563 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1567 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1568 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1571 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1573 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1576 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1580 /* If all nexthops were processed, we're done */
1582 if (IS_ZEBRA_DEBUG_RIB
)
1583 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1584 re
->vrf_id
, dest_str
,
1585 (changed_p
? "true" : "false"));
1589 /* FIB nexthop set differs from the RIB set:
1590 * create a fib-specific nexthop-group
1592 if (IS_ZEBRA_DEBUG_RIB
)
1593 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1594 re
->vrf_id
, dest_str
,
1595 (changed_p
? "true" : "false"));
1597 ctxnhg
= dplane_ctx_get_ng(ctx
);
1599 if (ctxnhg
->nexthop
)
1600 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1602 /* Bit of a special case when the fib has _no_ installed
1605 nexthop
= nexthop_new();
1606 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1607 nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1615 * Helper to locate a zebra route-node from a dplane context. This is used
1616 * when processing dplane results, e.g. Note well: the route-node is returned
1617 * with a ref held - route_unlock_node() must be called eventually.
1619 static struct route_node
*
1620 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1622 struct route_table
*table
= NULL
;
1623 struct route_node
*rn
= NULL
;
1624 const struct prefix
*dest_pfx
, *src_pfx
;
1626 /* Locate rn and re(s) from ctx */
1628 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1629 dplane_ctx_get_safi(ctx
),
1630 dplane_ctx_get_vrf(ctx
),
1631 dplane_ctx_get_table(ctx
));
1632 if (table
== NULL
) {
1633 if (IS_ZEBRA_DEBUG_DPLANE
) {
1634 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1635 dplane_ctx_get_afi(ctx
),
1636 dplane_ctx_get_safi(ctx
),
1637 dplane_ctx_get_vrf(ctx
));
1642 dest_pfx
= dplane_ctx_get_dest(ctx
);
1643 src_pfx
= dplane_ctx_get_src(ctx
);
1645 rn
= srcdest_rnode_get(table
, dest_pfx
,
1646 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1655 * Route-update results processing after async dataplane update.
1657 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1659 struct zebra_vrf
*zvrf
= NULL
;
1660 struct route_node
*rn
= NULL
;
1661 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1662 bool is_update
= false;
1663 char dest_str
[PREFIX_STRLEN
] = "";
1664 enum dplane_op_e op
;
1665 enum zebra_dplane_result status
;
1666 const struct prefix
*dest_pfx
, *src_pfx
;
1668 bool fib_changed
= false;
1670 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1671 dest_pfx
= dplane_ctx_get_dest(ctx
);
1673 /* Note well: only capturing the prefix string if debug is enabled here;
1674 * unconditional log messages will have to generate the string.
1676 if (IS_ZEBRA_DEBUG_DPLANE
)
1677 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1679 /* Locate rn and re(s) from ctx */
1680 rn
= rib_find_rn_from_ctx(ctx
);
1682 if (IS_ZEBRA_DEBUG_DPLANE
) {
1683 zlog_debug("Failed to process dplane results: no route for %u:%s",
1684 dplane_ctx_get_vrf(ctx
), dest_str
);
1689 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1691 op
= dplane_ctx_get_op(ctx
);
1692 status
= dplane_ctx_get_status(ctx
);
1694 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1695 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1696 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1697 dplane_op2str(op
), dplane_res2str(status
));
1700 * Update is a bit of a special case, where we may have both old and new
1701 * routes to post-process.
1703 is_update
= dplane_ctx_is_update(ctx
);
1706 * Take a pass through the routes, look for matches with the context
1709 RNODE_FOREACH_RE(rn
, rib
) {
1712 if (rib_route_match_ctx(rib
, ctx
, false))
1716 /* Check for old route match */
1717 if (is_update
&& (old_re
== NULL
)) {
1718 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1722 /* Have we found the routes we need to work on? */
1723 if (re
&& ((!is_update
|| old_re
)))
1727 seq
= dplane_ctx_get_seq(ctx
);
1730 * Check sequence number(s) to detect stale results before continuing
1733 if (re
->dplane_sequence
!= seq
) {
1734 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1735 zlog_debug("%u:%s Stale dplane result for re %p",
1736 dplane_ctx_get_vrf(ctx
),
1739 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1743 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1744 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1745 zlog_debug("%u:%s Stale dplane result for old_re %p",
1746 dplane_ctx_get_vrf(ctx
),
1749 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1753 case DPLANE_OP_ROUTE_INSTALL
:
1754 case DPLANE_OP_ROUTE_UPDATE
:
1755 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1757 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1758 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1761 * On an update operation from the same route type
1762 * context retrieval currently has no way to know
1763 * which was the old and which was the new.
1764 * So don't unset our flags that we just set.
1765 * We know redistribution is ok because the
1766 * old_re in this case is used for nothing
1767 * more than knowing whom to contact if necessary.
1769 if (old_re
&& old_re
!= re
) {
1770 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1771 UNSET_FLAG(old_re
->status
,
1772 ROUTE_ENTRY_INSTALLED
);
1775 /* Update zebra route based on the results in
1776 * the context struct.
1780 rib_update_re_from_ctx(re
, rn
, ctx
);
1783 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1784 zlog_debug("%u:%s no fib change for re",
1791 redistribute_update(dest_pfx
, src_pfx
,
1796 * System routes are weird in that they
1797 * allow multiple to be installed that match
1798 * to the same prefix, so after we get the
1799 * result we need to clean them up so that
1800 * we can actually use them.
1802 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1803 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1804 zebra_rib_fixup_system(rn
);
1809 /* Notify route owner */
1810 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1814 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1815 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1817 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1819 zsend_route_notify_owner(re
, dest_pfx
,
1820 ZAPI_ROUTE_FAIL_INSTALL
);
1822 zlog_warn("%u:%s: Route install failed",
1823 dplane_ctx_get_vrf(ctx
),
1824 prefix2str(dest_pfx
,
1825 dest_str
, sizeof(dest_str
)));
1828 case DPLANE_OP_ROUTE_DELETE
:
1830 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1832 * In the delete case, the zebra core datastructs were
1833 * updated (or removed) at the time the delete was issued,
1834 * so we're just notifying the route owner.
1836 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1838 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1839 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1841 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1847 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1848 zsend_route_notify_owner_ctx(ctx
,
1849 ZAPI_ROUTE_REMOVE_FAIL
);
1851 zlog_warn("%u:%s: Route Deletion failure",
1852 dplane_ctx_get_vrf(ctx
),
1853 prefix2str(dest_pfx
,
1854 dest_str
, sizeof(dest_str
)));
1858 * System routes are weird in that they
1859 * allow multiple to be installed that match
1860 * to the same prefix, so after we get the
1861 * result we need to clean them up so that
1862 * we can actually use them.
1864 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1865 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1866 zebra_rib_fixup_system(rn
);
1872 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1873 zebra_rib_evaluate_mpls(rn
);
1877 route_unlock_node(rn
);
1879 /* Return context to dataplane module */
1880 dplane_ctx_fini(&ctx
);
1884 * Handle notification from async dataplane: the dataplane has detected
1885 * some change to a route, and notifies zebra so that the control plane
1886 * can reflect that change.
1888 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1890 struct route_node
*rn
= NULL
;
1891 struct route_entry
*re
= NULL
;
1892 struct nexthop
*nexthop
;
1893 char dest_str
[PREFIX_STRLEN
] = "";
1894 const struct prefix
*dest_pfx
, *src_pfx
;
1896 bool fib_changed
= false;
1897 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1898 int start_count
, end_count
;
1899 dest_pfx
= dplane_ctx_get_dest(ctx
);
1901 /* Note well: only capturing the prefix string if debug is enabled here;
1902 * unconditional log messages will have to generate the string.
1905 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1907 /* Locate rn and re(s) from ctx */
1908 rn
= rib_find_rn_from_ctx(ctx
);
1911 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1912 dplane_ctx_get_vrf(ctx
), dest_str
);
1917 dest
= rib_dest_from_rnode(rn
);
1918 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1921 zlog_debug("%u:%s Processing dplane notif ctx %p",
1922 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1925 * Take a pass through the routes, look for matches with the context
1928 RNODE_FOREACH_RE(rn
, re
) {
1929 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1933 /* No match? Nothing we can do */
1936 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1937 dplane_ctx_get_vrf(ctx
), dest_str
,
1939 dplane_ctx_get_type(ctx
)));
1944 /* Is this a notification that ... matters? We only really care about
1945 * the route that is currently selected for installation.
1947 if (re
!= dest
->selected_fib
) {
1948 /* TODO -- don't skip processing entirely? We might like to
1949 * at least report on the event.
1952 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1953 dplane_ctx_get_vrf(ctx
), dest_str
,
1955 dplane_ctx_get_type(ctx
)));
1959 /* We'll want to determine whether the installation status of the
1960 * route has changed: we'll check the status before processing,
1961 * and then again if there's been a change.
1964 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1965 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1969 /* Update zebra's nexthop FIB flags based on the context struct's
1972 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1976 zlog_debug("%u:%s No change from dplane notification",
1977 dplane_ctx_get_vrf(ctx
), dest_str
);
1983 * Perform follow-up work if the actual status of the prefix
1988 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1989 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1993 /* Various fib transitions: changed nexthops; from installed to
1994 * not-installed; or not-installed to installed.
1996 if (start_count
> 0 && end_count
> 0) {
1998 /* Changed nexthops - update kernel/others */
1999 dplane_route_notif_update(rn
, re
,
2000 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2002 } else if (start_count
== 0 && end_count
> 0) {
2004 zlog_debug("%u:%s installed transition from dplane notification",
2005 dplane_ctx_get_vrf(ctx
), dest_str
);
2007 /* We expect this to be the selected route, so we want
2008 * to tell others about this transistion.
2010 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2012 /* Changed nexthops - update kernel/others */
2013 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
2015 /* Redistribute, lsp, and nht update */
2016 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2018 zebra_rib_evaluate_rn_nexthops(
2019 rn
, zebra_router_get_next_sequence());
2021 zebra_rib_evaluate_mpls(rn
);
2023 } else if (start_count
> 0 && end_count
== 0) {
2025 zlog_debug("%u:%s un-installed transition from dplane notification",
2026 dplane_ctx_get_vrf(ctx
), dest_str
);
2028 /* Transition from _something_ installed to _nothing_
2031 /* We expect this to be the selected route, so we want
2032 * to tell others about this transistion.
2034 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2036 /* Changed nexthops - update kernel/others */
2037 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2039 /* Redistribute, lsp, and nht update */
2040 redistribute_delete(dest_pfx
, src_pfx
, re
);
2042 zebra_rib_evaluate_rn_nexthops(
2043 rn
, zebra_router_get_next_sequence());
2045 zebra_rib_evaluate_mpls(rn
);
2050 route_unlock_node(rn
);
2052 /* Return context to dataplane module */
2053 dplane_ctx_fini(&ctx
);
2056 /* Take a list of route_node structs and return 1, if there was a record
2057 * picked from it and processed by rib_process(). Don't process more,
2058 * than one RN record; operate only in the specified sub-queue.
2060 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2062 struct listnode
*lnode
= listhead(subq
);
2063 struct route_node
*rnode
;
2065 struct zebra_vrf
*zvrf
= NULL
;
2070 rnode
= listgetdata(lnode
);
2071 dest
= rib_dest_from_rnode(rnode
);
2073 zvrf
= rib_dest_vrf(dest
);
2077 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2078 char buf
[SRCDEST2STR_BUFFER
];
2080 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2081 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2082 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2086 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2087 RIB_ROUTE_QUEUED(qindex
));
2092 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2093 __func__
, rnode
, rnode
->lock
);
2094 zlog_backtrace(LOG_DEBUG
);
2097 route_unlock_node(rnode
);
2098 list_delete_node(subq
, lnode
);
2104 * Perform next-hop tracking processing after RIB updates.
2106 static void do_nht_processing(void)
2110 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2111 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2113 * is pointed to the meta queue structure.
2115 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2117 struct meta_queue
*mq
= data
;
2119 uint32_t queue_len
, queue_limit
;
2121 /* Ensure there's room for more dataplane updates */
2122 queue_limit
= dplane_get_in_queue_limit();
2123 queue_len
= dplane_get_in_queue_len();
2124 if (queue_len
> queue_limit
) {
2125 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2126 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2127 queue_len
, queue_limit
);
2129 /* Ensure that the meta-queue is actually enqueued */
2130 if (work_queue_empty(zrouter
.ribq
))
2131 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2133 return WQ_QUEUE_BLOCKED
;
2136 for (i
= 0; i
< MQ_SIZE
; i
++)
2137 if (process_subq(mq
->subq
[i
], i
)) {
2141 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2146 * Look into the RN and queue it into the highest priority queue
2147 * at this point in time for processing.
2149 * We will enqueue a route node only once per invocation.
2151 * There are two possibilities here that should be kept in mind.
2152 * If the original invocation has not been pulled off for processing
2153 * yet, A subsuquent invocation can have a route entry with a better
2154 * meta queue index value and we can have a situation where
2155 * we might have the same node enqueued 2 times. Not necessarily
2156 * an optimal situation but it should be ok.
2158 * The other possibility is that the original invocation has not
2159 * been pulled off for processing yet, A subsusquent invocation
2160 * doesn't have a route_entry with a better meta-queue and the
2161 * original metaqueue index value will win and we'll end up with
2162 * the route node enqueued once.
2164 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2166 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2167 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2169 RNODE_FOREACH_RE (rn
, curr_re
) {
2170 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2172 if (curr_qindex
<= qindex
) {
2174 qindex
= curr_qindex
;
2181 /* Invariant: at this point we always have rn->info set. */
2182 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2183 RIB_ROUTE_QUEUED(qindex
))) {
2184 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2185 rnode_debug(rn
, re
->vrf_id
,
2186 "rn %p is already queued in sub-queue %u",
2187 (void *)rn
, qindex
);
2191 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2192 listnode_add(mq
->subq
[qindex
], rn
);
2193 route_lock_node(rn
);
2196 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2197 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2198 (void *)rn
, qindex
);
2201 /* Add route_node to work queue and schedule processing */
2202 void rib_queue_add(struct route_node
*rn
)
2206 /* Pointless to queue a route_node with no RIB entries to add or remove
2208 if (!rnode_to_ribs(rn
)) {
2209 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2210 __func__
, (void *)rn
, rn
->lock
);
2211 zlog_backtrace(LOG_DEBUG
);
2215 if (zrouter
.ribq
== NULL
) {
2216 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2217 "%s: work_queue does not exist!", __func__
);
2222 * The RIB queue should normally be either empty or holding the only
2223 * work_queue_item element. In the latter case this element would
2224 * hold a pointer to the meta queue structure, which must be used to
2225 * actually queue the route nodes to process. So create the MQ
2226 * holder, if necessary, then push the work into it in any case.
2227 * This semantics was introduced after 0.99.9 release.
2229 if (work_queue_empty(zrouter
.ribq
))
2230 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2232 rib_meta_queue_add(zrouter
.mq
, rn
);
2237 /* Create new meta queue.
2238 A destructor function doesn't seem to be necessary here.
2240 static struct meta_queue
*meta_queue_new(void)
2242 struct meta_queue
*new;
2245 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2247 for (i
= 0; i
< MQ_SIZE
; i
++) {
2248 new->subq
[i
] = list_new();
2249 assert(new->subq
[i
]);
2255 void meta_queue_free(struct meta_queue
*mq
)
2259 for (i
= 0; i
< MQ_SIZE
; i
++)
2260 list_delete(&mq
->subq
[i
]);
2262 XFREE(MTYPE_WORK_QUEUE
, mq
);
2265 /* initialise zebra rib work queue */
2266 static void rib_queue_init(void)
2268 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2269 "route_node processing"))) {
2270 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2271 "%s: could not initialise work queue!", __func__
);
2275 /* fill in the work queue spec */
2276 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2277 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2278 zrouter
.ribq
->spec
.completion_func
= NULL
;
2279 /* XXX: TODO: These should be runtime configurable via vty */
2280 zrouter
.ribq
->spec
.max_retries
= 3;
2281 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2282 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2284 if (!(zrouter
.mq
= meta_queue_new())) {
2285 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2286 "%s: could not initialise meta queue!", __func__
);
2292 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2296 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2297 rnh_list_init(&dest
->nht
);
2298 route_lock_node(rn
); /* rn route table reference */
2305 /* RIB updates are processed via a queue of pointers to route_nodes.
2307 * The queue length is bounded by the maximal size of the routing table,
2308 * as a route_node will not be requeued, if already queued.
2310 * REs are submitted via rib_addnode or rib_delnode which set minimal
2311 * state, or static_install_route (when an existing RE is updated)
2312 * and then submit route_node to queue for best-path selection later.
2313 * Order of add/delete state changes are preserved for any given RE.
2315 * Deleted REs are reaped during best-path selection.
2318 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2319 * |-------->| | best RE, if required
2321 * static_install->|->rib_addqueue...... -> rib_process
2323 * |-------->| |-> rib_unlink
2324 * |-> set ROUTE_ENTRY_REMOVE |
2325 * rib_delnode (RE freed)
2327 * The 'info' pointer of a route_node points to a rib_dest_t
2328 * ('dest'). Queueing state for a route_node is kept on the dest. The
2329 * dest is created on-demand by rib_link() and is kept around at least
2330 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2332 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2334 * - route_nodes: refcounted by:
2335 * - dest attached to route_node:
2336 * - managed by: rib_link/rib_gc_dest
2337 * - route_node processing queue
2338 * - managed by: rib_addqueue, rib_process.
2342 /* Add RE to head of the route node. */
2343 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2347 const char *rmap_name
;
2351 dest
= rib_dest_from_rnode(rn
);
2353 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2354 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2356 dest
= zebra_rib_create_dest(rn
);
2359 re_list_add_head(&dest
->routes
, re
);
2361 afi
= (rn
->p
.family
== AF_INET
)
2363 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2364 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2365 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2366 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2371 static void rib_addnode(struct route_node
*rn
,
2372 struct route_entry
*re
, int process
)
2374 /* RE node has been un-removed before route-node is processed.
2375 * route_node must hence already be on the queue for processing..
2377 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2378 if (IS_ZEBRA_DEBUG_RIB
)
2379 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2380 (void *)rn
, (void *)re
);
2382 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2385 rib_link(rn
, re
, process
);
2391 * Detach a rib structure from a route_node.
2393 * Note that a call to rib_unlink() should be followed by a call to
2394 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2395 * longer required to be deleted.
2397 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2403 if (IS_ZEBRA_DEBUG_RIB
)
2404 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2407 dest
= rib_dest_from_rnode(rn
);
2409 re_list_del(&dest
->routes
, re
);
2411 if (dest
->selected_fib
== re
)
2412 dest
->selected_fib
= NULL
;
2414 nexthops_free(re
->ng
.nexthop
);
2415 nexthops_free(re
->fib_ng
.nexthop
);
2417 XFREE(MTYPE_RE
, re
);
2420 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2424 if (IS_ZEBRA_DEBUG_RIB
)
2425 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2426 (void *)rn
, (void *)re
);
2427 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2429 afi
= (rn
->p
.family
== AF_INET
)
2431 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2432 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2433 zebra_del_import_table_entry(rn
, re
);
2434 /* Just clean up if non main table */
2435 if (IS_ZEBRA_DEBUG_RIB
) {
2436 char buf
[SRCDEST2STR_BUFFER
];
2437 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2438 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2439 re
->vrf_id
, buf
, rn
, re
,
2440 zebra_route_string(re
->type
));
2449 /* This function dumps the contents of a given RE entry into
2450 * standard debug log. Calling function name and IP prefix in
2451 * question are passed as 1st and 2nd arguments.
2454 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2455 union prefixconstptr src_pp
,
2456 const struct route_entry
*re
)
2458 const struct prefix
*src_p
= src_pp
.p
;
2459 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2460 char straddr
[PREFIX_STRLEN
];
2461 char srcaddr
[PREFIX_STRLEN
];
2462 struct nexthop
*nexthop
;
2464 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2465 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2466 is_srcdst
? " from " : "",
2467 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2470 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2471 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2474 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2475 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2476 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2477 re
->nexthop_num
, re
->nexthop_active_num
);
2479 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2480 struct interface
*ifp
;
2481 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2483 switch (nexthop
->type
) {
2484 case NEXTHOP_TYPE_BLACKHOLE
:
2485 sprintf(straddr
, "Blackhole");
2487 case NEXTHOP_TYPE_IFINDEX
:
2488 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2490 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2492 case NEXTHOP_TYPE_IPV4
:
2494 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2495 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2498 case NEXTHOP_TYPE_IPV6
:
2499 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2500 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2504 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2505 func
, (nexthop
->rparent
? " NH" : "NH"), straddr
,
2506 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2508 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2511 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2514 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2517 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2520 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2523 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2527 zlog_debug("%s: dump complete", func
);
2530 /* This is an exported helper to rtm_read() to dump the strange
2531 * RE entry found by rib_lookup_ipv4_route()
2534 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2536 struct route_table
*table
;
2537 struct route_node
*rn
;
2538 struct route_entry
*re
;
2539 char prefix_buf
[INET_ADDRSTRLEN
];
2542 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2544 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2545 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2550 /* Scan the RIB table for exactly matching RE entry. */
2551 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2553 /* No route for this prefix. */
2555 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2556 prefix2str((struct prefix
*)p
, prefix_buf
,
2557 sizeof(prefix_buf
)));
2562 route_unlock_node(rn
);
2565 RNODE_FOREACH_RE (rn
, re
) {
2566 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2568 (void *)rn
, (void *)re
,
2569 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2572 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2575 route_entry_dump(p
, NULL
, re
);
2579 /* Check if requested address assignment will fail due to another
2580 * route being installed by zebra in FIB already. Take necessary
2581 * actions, if needed: remove such a route from FIB and deSELECT
2582 * corresponding RE entry. Then put affected RN into RIBQ head.
2584 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2586 struct route_table
*table
;
2587 struct route_node
*rn
;
2590 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2591 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2592 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2597 /* No matches would be the simplest case. */
2598 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2602 route_unlock_node(rn
);
2604 dest
= rib_dest_from_rnode(rn
);
2605 /* Check all RE entries. In case any changes have to be done, requeue
2606 * the RN into RIBQ head. If the routing message about the new connected
2607 * route (generated by the IP address we are going to assign very soon)
2608 * comes before the RIBQ is processed, the new RE entry will join
2609 * RIBQ record already on head. This is necessary for proper
2611 * of the rest of the RE.
2613 if (dest
->selected_fib
) {
2614 if (IS_ZEBRA_DEBUG_RIB
) {
2615 char buf
[PREFIX_STRLEN
];
2617 zlog_debug("%u:%s: freeing way for connected prefix",
2618 dest
->selected_fib
->vrf_id
,
2619 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2620 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2622 rib_uninstall(rn
, dest
->selected_fib
);
2627 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2628 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2630 struct route_table
*table
;
2631 struct route_node
*rn
;
2632 struct route_entry
*same
= NULL
;
2638 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2641 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2643 XFREE(MTYPE_RE
, re
);
2647 /* Make it sure prefixlen is applied to the prefix. */
2650 apply_mask_ipv6(src_p
);
2652 /* Set default distance by route type. */
2653 if (re
->distance
== 0)
2654 re
->distance
= route_distance(re
->type
);
2656 /* Lookup route node.*/
2657 rn
= srcdest_rnode_get(table
, p
, src_p
);
2660 * If same type of route are installed, treat it as a implicit
2662 * If the user has specified the No route replace semantics
2663 * for the install don't do a route replace.
2665 RNODE_FOREACH_RE (rn
, same
) {
2666 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2669 if (same
->type
!= re
->type
)
2671 if (same
->instance
!= re
->instance
)
2673 if (same
->type
== ZEBRA_ROUTE_KERNEL
2674 && same
->metric
!= re
->metric
)
2677 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2678 same
->distance
!= re
->distance
)
2682 * We should allow duplicate connected routes
2683 * because of IPv6 link-local routes and unnumbered
2684 * interfaces on Linux.
2686 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2690 /* If this route is kernel/connected route, notify the dataplane. */
2691 if (RIB_SYSTEM_ROUTE(re
)) {
2692 /* Notify dataplane */
2693 dplane_sys_route_add(rn
, re
);
2696 /* Link new re to node.*/
2697 if (IS_ZEBRA_DEBUG_RIB
) {
2698 rnode_debug(rn
, re
->vrf_id
,
2699 "Inserting route rn %p, re %p (%s) existing %p",
2700 rn
, re
, zebra_route_string(re
->type
), same
);
2702 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2703 route_entry_dump(p
, src_p
, re
);
2706 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2707 rib_addnode(rn
, re
, 1);
2710 /* Free implicit route.*/
2712 rib_delnode(rn
, same
);
2716 route_unlock_node(rn
);
2720 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2721 unsigned short instance
, int flags
, struct prefix
*p
,
2722 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2723 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2726 struct route_table
*table
;
2727 struct route_node
*rn
;
2728 struct route_entry
*re
;
2729 struct route_entry
*fib
= NULL
;
2730 struct route_entry
*same
= NULL
;
2731 struct nexthop
*rtnh
;
2732 char buf2
[INET6_ADDRSTRLEN
];
2735 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2738 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2745 apply_mask_ipv6(src_p
);
2747 /* Lookup route node. */
2748 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2750 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2752 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2753 if (src_p
&& src_p
->prefixlen
)
2754 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2758 if (IS_ZEBRA_DEBUG_RIB
)
2759 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2761 (src_buf
[0] != '\0') ? " from " : "",
2766 dest
= rib_dest_from_rnode(rn
);
2767 fib
= dest
->selected_fib
;
2769 /* Lookup same type route. */
2770 RNODE_FOREACH_RE (rn
, re
) {
2771 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2774 if (re
->type
!= type
)
2776 if (re
->instance
!= instance
)
2778 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2779 distance
!= re
->distance
)
2782 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2784 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2785 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2786 if (rtnh
->ifindex
!= nh
->ifindex
)
2791 /* Make sure that the route found has the same gateway. */
2797 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2799 * No guarantee all kernel send nh with labels
2802 if (nexthop_same_no_labels(rtnh
, nh
)) {
2810 /* If same type of route can't be found and this message is from
2814 * In the past(HA!) we could get here because
2815 * we were receiving a route delete from the
2816 * kernel and we're not marking the proto
2817 * as coming from it's appropriate originator.
2818 * Now that we are properly noticing the fact
2819 * that the kernel has deleted our route we
2820 * are not going to get called in this path
2821 * I am going to leave this here because
2822 * this might still work this way on non-linux
2823 * platforms as well as some weird state I have
2824 * not properly thought of yet.
2825 * If we can show that this code path is
2826 * dead then we can remove it.
2828 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2829 if (IS_ZEBRA_DEBUG_RIB
) {
2830 rnode_debug(rn
, vrf_id
,
2831 "rn %p, re %p (%s) was deleted from kernel, adding",
2833 zebra_route_string(fib
->type
));
2836 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2838 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2840 UNSET_FLAG(rtnh
->flags
,
2844 * This is a non FRR route
2845 * as such we should mark
2848 dest
->selected_fib
= NULL
;
2850 /* This means someone else, other than Zebra,
2852 * a Zebra router from the kernel. We will add
2854 rib_install_kernel(rn
, fib
, NULL
);
2857 if (IS_ZEBRA_DEBUG_RIB
) {
2861 "via %s ifindex %d type %d "
2862 "doesn't exist in rib",
2863 inet_ntop(afi2family(afi
),
2870 "type %d doesn't exist in rib",
2873 route_unlock_node(rn
);
2879 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2881 rib_install_kernel(rn
, same
, NULL
);
2882 route_unlock_node(rn
);
2887 /* Special handling for IPv4 or IPv6 routes sourced from
2888 * EVPN - the nexthop (and associated MAC) need to be
2889 * uninstalled if no more refs.
2891 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2892 struct nexthop
*tmp_nh
;
2894 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2895 struct ipaddr vtep_ip
;
2897 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2898 if (afi
== AFI_IP
) {
2899 vtep_ip
.ipa_type
= IPADDR_V4
;
2900 memcpy(&(vtep_ip
.ipaddr_v4
),
2901 &(tmp_nh
->gate
.ipv4
),
2902 sizeof(struct in_addr
));
2904 vtep_ip
.ipa_type
= IPADDR_V6
;
2905 memcpy(&(vtep_ip
.ipaddr_v6
),
2906 &(tmp_nh
->gate
.ipv6
),
2907 sizeof(struct in6_addr
));
2909 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2914 /* Notify dplane if system route changes */
2915 if (RIB_SYSTEM_ROUTE(re
))
2916 dplane_sys_route_del(rn
, same
);
2918 rib_delnode(rn
, same
);
2921 route_unlock_node(rn
);
2926 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2927 unsigned short instance
, int flags
, struct prefix
*p
,
2928 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2929 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2932 struct route_entry
*re
;
2933 struct nexthop
*nexthop
;
2935 /* Allocate new route_entry structure. */
2936 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2938 re
->instance
= instance
;
2939 re
->distance
= distance
;
2941 re
->metric
= metric
;
2943 re
->table
= table_id
;
2944 re
->vrf_id
= vrf_id
;
2945 re
->nexthop_num
= 0;
2946 re
->uptime
= monotime(NULL
);
2950 nexthop
= nexthop_new();
2952 route_entry_nexthop_add(re
, nexthop
);
2954 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2957 /* Schedule routes of a particular table (address-family) based on event. */
2958 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2960 struct route_node
*rn
;
2961 struct route_entry
*re
, *next
;
2963 /* Walk all routes and queue for processing, if appropriate for
2964 * the trigger event.
2966 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2968 * If we are looking at a route node and the node
2969 * has already been queued we don't
2970 * need to queue it up again
2972 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2973 RIB_ROUTE_ANY_QUEUED
))
2976 case RIB_UPDATE_IF_CHANGE
:
2977 /* Examine all routes that won't get processed by the
2979 * triggered by nexthop evaluation (NHT). This would be
2981 * kernel and certain static routes. Note that NHT will
2983 * triggered upon an interface event as connected routes
2985 * get queued for processing.
2987 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2990 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
2991 && re
->type
!= ZEBRA_ROUTE_KERNEL
2992 && re
->type
!= ZEBRA_ROUTE_CONNECT
2993 && re
->type
!= ZEBRA_ROUTE_STATIC
)
2996 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2997 SET_FLAG(re
->status
,
2998 ROUTE_ENTRY_CHANGED
);
3003 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
3004 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
3005 || nh
->type
== NEXTHOP_TYPE_IPV6
))
3008 /* If we only have nexthops to a
3013 SET_FLAG(re
->status
,
3014 ROUTE_ENTRY_CHANGED
);
3020 case RIB_UPDATE_RMAP_CHANGE
:
3021 case RIB_UPDATE_OTHER
:
3022 /* Right now, examine all routes. Can restrict to a
3024 * some cases (TODO).
3026 if (rnode_to_ribs(rn
)) {
3027 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
3028 SET_FLAG(re
->status
,
3029 ROUTE_ENTRY_CHANGED
);
3040 /* RIB update function. */
3041 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3043 struct route_table
*table
;
3045 /* Process routes of interested address-families. */
3046 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3048 if (IS_ZEBRA_DEBUG_EVENT
)
3049 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3050 rib_update_table(table
, event
);
3053 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3055 if (IS_ZEBRA_DEBUG_EVENT
)
3056 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3057 rib_update_table(table
, event
);
3061 /* Delete self installed routes after zebra is relaunched. */
3062 void rib_sweep_table(struct route_table
*table
)
3064 struct route_node
*rn
;
3065 struct route_entry
*re
;
3066 struct route_entry
*next
;
3067 struct nexthop
*nexthop
;
3072 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3073 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3075 if (IS_ZEBRA_DEBUG_RIB
)
3076 route_entry_dump(&rn
->p
, NULL
, re
);
3078 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3081 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3085 * If routes are older than startup_time then
3086 * we know we read them in from the kernel.
3087 * As such we can safely remove them.
3089 if (zrouter
.startup_time
< re
->uptime
)
3093 * So we are starting up and have received
3094 * routes from the kernel that we have installed
3095 * from a previous run of zebra but not cleaned
3096 * up ( say a kill -9 )
3097 * But since we haven't actually installed
3098 * them yet( we received them from the kernel )
3099 * we don't think they are active.
3100 * So let's pretend they are active to actually
3102 * In all honesty I'm not sure if we should
3103 * mark them as active when we receive them
3104 * This is startup only so probably ok.
3106 * If we ever decide to move rib_sweep_table
3107 * to a different spot (ie startup )
3108 * this decision needs to be revisited
3110 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3111 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3112 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3114 rib_uninstall_kernel(rn
, re
);
3115 rib_delnode(rn
, re
);
3120 /* Sweep all RIB tables. */
3121 int rib_sweep_route(struct thread
*t
)
3124 struct zebra_vrf
*zvrf
;
3126 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3127 if ((zvrf
= vrf
->info
) == NULL
)
3130 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3131 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3134 zebra_router_sweep_route();
3139 /* Remove specific by protocol routes from 'table'. */
3140 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3141 struct route_table
*table
)
3143 struct route_node
*rn
;
3144 struct route_entry
*re
;
3145 struct route_entry
*next
;
3146 unsigned long n
= 0;
3149 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3150 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3151 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3153 if (re
->type
== proto
3154 && re
->instance
== instance
) {
3155 rib_delnode(rn
, re
);
3162 /* Remove specific by protocol routes. */
3163 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3166 struct zebra_vrf
*zvrf
;
3167 struct other_route_table
*ort
;
3168 unsigned long cnt
= 0;
3170 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3175 cnt
+= rib_score_proto_table(proto
, instance
,
3176 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3177 + rib_score_proto_table(
3179 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3181 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3182 rib_score_proto_table(proto
, instance
, ort
->table
);
3188 /* Close RIB and clean up kernel routes. */
3189 void rib_close_table(struct route_table
*table
)
3191 struct route_node
*rn
;
3192 rib_table_info_t
*info
;
3198 info
= route_table_get_info(table
);
3200 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3201 dest
= rib_dest_from_rnode(rn
);
3203 if (dest
&& dest
->selected_fib
) {
3204 if (info
->safi
== SAFI_UNICAST
)
3205 hook_call(rib_update
, rn
, NULL
);
3207 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3208 dest
->selected_fib
= NULL
;
3214 * Handler for async dataplane results after a pseudowire installation
3216 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3218 struct zebra_pw
*pw
;
3219 struct zebra_vrf
*vrf
;
3221 /* The pseudowire code assumes success - we act on an error
3222 * result for installation attempts here.
3224 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3227 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3228 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3229 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3231 zebra_pw_install_failure(pw
);
3241 * Handle results from the dataplane system. Dequeue update context
3242 * structs, dispatch to appropriate internal handlers.
3244 static int rib_process_dplane_results(struct thread
*thread
)
3246 struct zebra_dplane_ctx
*ctx
;
3247 struct dplane_ctx_q ctxlist
;
3249 /* Dequeue a list of completed updates with one lock/unlock cycle */
3252 TAILQ_INIT(&ctxlist
);
3254 /* Take lock controlling queue of results */
3255 pthread_mutex_lock(&dplane_mutex
);
3257 /* Dequeue list of context structs */
3258 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3260 pthread_mutex_unlock(&dplane_mutex
);
3262 /* Dequeue context block */
3263 ctx
= dplane_ctx_dequeue(&ctxlist
);
3265 /* If we've emptied the results queue, we're done */
3270 switch (dplane_ctx_get_op(ctx
)) {
3271 case DPLANE_OP_ROUTE_INSTALL
:
3272 case DPLANE_OP_ROUTE_UPDATE
:
3273 case DPLANE_OP_ROUTE_DELETE
:
3275 /* Bit of special case for route updates
3276 * that were generated by async notifications:
3277 * we don't want to continue processing these
3280 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3281 rib_process_result(ctx
);
3283 dplane_ctx_fini(&ctx
);
3287 case DPLANE_OP_ROUTE_NOTIFY
:
3288 rib_process_dplane_notify(ctx
);
3291 case DPLANE_OP_LSP_INSTALL
:
3292 case DPLANE_OP_LSP_UPDATE
:
3293 case DPLANE_OP_LSP_DELETE
:
3295 /* Bit of special case for LSP updates
3296 * that were generated by async notifications:
3297 * we don't want to continue processing these.
3299 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3300 zebra_mpls_lsp_dplane_result(ctx
);
3302 dplane_ctx_fini(&ctx
);
3306 case DPLANE_OP_LSP_NOTIFY
:
3307 zebra_mpls_process_dplane_notify(ctx
);
3310 case DPLANE_OP_PW_INSTALL
:
3311 case DPLANE_OP_PW_UNINSTALL
:
3312 handle_pw_result(ctx
);
3315 case DPLANE_OP_SYS_ROUTE_ADD
:
3316 case DPLANE_OP_SYS_ROUTE_DELETE
:
3317 /* No further processing in zebra for these. */
3318 dplane_ctx_fini(&ctx
);
3322 /* Don't expect this: just return the struct? */
3323 dplane_ctx_fini(&ctx
);
3325 } /* Dispatch by op code */
3327 ctx
= dplane_ctx_dequeue(&ctxlist
);
3332 /* Check for nexthop tracking processing after finishing with results */
3333 do_nht_processing();
3339 * Results are returned from the dataplane subsystem, in the context of
3340 * the dataplane pthread. We enqueue the results here for processing by
3341 * the main thread later.
3343 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3345 /* Take lock controlling queue of results */
3346 pthread_mutex_lock(&dplane_mutex
);
3348 /* Enqueue context blocks */
3349 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3351 pthread_mutex_unlock(&dplane_mutex
);
3353 /* Ensure event is signalled to zebra main pthread */
3354 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3361 * Ensure there are no empty slots in the route_info array.
3362 * Every route type in zebra should be present there.
3364 static void check_route_info(void)
3366 int len
= array_size(route_info
);
3369 * ZEBRA_ROUTE_SYSTEM is special cased since
3370 * its key is 0 anyway.
3372 * ZEBRA_ROUTE_ALL is also ignored.
3374 for (int i
= 0; i
< len
; i
++) {
3375 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3377 assert(route_info
[i
].key
);
3378 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3382 /* Routing information base initialize. */
3389 /* Init dataplane, and register for results */
3390 pthread_mutex_init(&dplane_mutex
, NULL
);
3391 TAILQ_INIT(&rib_dplane_q
);
3392 zebra_dplane_init(rib_dplane_results
);
3398 * Get the first vrf id that is greater than the given vrf id if any.
3400 * Returns TRUE if a vrf id was found, FALSE otherwise.
3402 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3406 vrf
= vrf_lookup_by_id(vrf_id
);
3408 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3410 *next_id_p
= vrf
->vrf_id
;
3419 * rib_tables_iter_next
3421 * Returns the next table in the iteration.
3423 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3425 struct route_table
*table
;
3428 * Array that helps us go over all AFI/SAFI combinations via one
3435 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3436 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3437 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3442 switch (iter
->state
) {
3444 case RIB_TABLES_ITER_S_INIT
:
3445 iter
->vrf_id
= VRF_DEFAULT
;
3446 iter
->afi_safi_ix
= -1;
3450 case RIB_TABLES_ITER_S_ITERATING
:
3451 iter
->afi_safi_ix
++;
3454 while (iter
->afi_safi_ix
3455 < (int)array_size(afi_safis
)) {
3456 table
= zebra_vrf_table(
3457 afi_safis
[iter
->afi_safi_ix
].afi
,
3458 afi_safis
[iter
->afi_safi_ix
].safi
,
3463 iter
->afi_safi_ix
++;
3467 * Found another table in this vrf.
3473 * Done with all tables in the current vrf, go to the
3477 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3480 iter
->afi_safi_ix
= 0;
3485 case RIB_TABLES_ITER_S_DONE
:
3490 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3492 iter
->state
= RIB_TABLES_ITER_S_DONE
;