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"
59 #include "zebra/zebra_nhg.h"
62 * Event, list, and mutex for delivery of dataplane results
64 static pthread_mutex_t dplane_mutex
;
65 static struct thread
*t_dplane
;
66 static struct dplane_ctx_q rib_dplane_q
;
68 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
71 /* Should we allow non Quagga processes to delete our routes */
72 extern int allow_delete
;
74 /* Each route type's string and default distance value. */
79 } route_info
[ZEBRA_ROUTE_MAX
] = {
80 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
81 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
82 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
83 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
84 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
85 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
86 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
87 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
88 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
89 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
90 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
91 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
92 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
93 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
94 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
95 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
96 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
97 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
98 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
99 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
100 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
101 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
102 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
103 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
104 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 4},
105 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 4},
106 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 2},
107 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 4}
108 /* Any new route type added to zebra, should be mirrored here */
110 /* no entry/default: 150 */
113 static void __attribute__((format(printf
, 5, 6)))
114 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
115 int priority
, const char *msgfmt
, ...)
117 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
121 va_start(ap
, msgfmt
);
122 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
126 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
127 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
129 if (info
->safi
== SAFI_MULTICAST
)
130 strlcat(buf
, " (MRIB)", sizeof(buf
));
132 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
135 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
138 #define rnode_debug(node, vrf_id, ...) \
139 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
140 #define rnode_info(node, ...) \
141 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
143 uint8_t route_distance(int type
)
147 if ((unsigned)type
>= array_size(route_info
))
150 distance
= route_info
[type
].distance
;
155 int is_zebra_valid_kernel_table(uint32_t table_id
)
158 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
159 || (table_id
== RT_TABLE_COMPAT
))
166 int is_zebra_main_routing_table(uint32_t table_id
)
168 if (table_id
== RT_TABLE_MAIN
)
173 int zebra_check_addr(const struct prefix
*p
)
175 if (p
->family
== AF_INET
) {
178 addr
= p
->u
.prefix4
.s_addr
;
181 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
182 || IPV4_LINKLOCAL(addr
))
185 if (p
->family
== AF_INET6
) {
186 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
188 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
194 /* Add nexthop to the end of a rib node's nexthop list */
195 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
197 _nexthop_group_add_sorted(&re
->ng
, nexthop
);
203 * copy_nexthop - copy a nexthop to the rib structure.
205 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
207 assert(!re
->ng
.nexthop
);
208 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
209 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
213 /* Delete specified nexthop from the list. */
214 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
217 nexthop
->next
->prev
= nexthop
->prev
;
219 nexthop
->prev
->next
= nexthop
->next
;
221 re
->ng
.nexthop
= nexthop
->next
;
226 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
230 struct nexthop
*nexthop
;
232 nexthop
= nexthop_new();
233 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
234 nexthop
->ifindex
= ifindex
;
235 nexthop
->vrf_id
= nh_vrf_id
;
237 route_entry_nexthop_add(re
, nexthop
);
242 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
243 struct in_addr
*ipv4
,
247 struct nexthop
*nexthop
;
249 nexthop
= nexthop_new();
250 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
251 nexthop
->vrf_id
= nh_vrf_id
;
252 nexthop
->gate
.ipv4
= *ipv4
;
254 nexthop
->src
.ipv4
= *src
;
256 route_entry_nexthop_add(re
, nexthop
);
261 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
262 struct in_addr
*ipv4
,
267 struct nexthop
*nexthop
;
268 struct interface
*ifp
;
270 nexthop
= nexthop_new();
271 nexthop
->vrf_id
= nh_vrf_id
;
272 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
273 nexthop
->gate
.ipv4
= *ipv4
;
275 nexthop
->src
.ipv4
= *src
;
276 nexthop
->ifindex
= ifindex
;
277 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
278 /*Pending: need to think if null ifp here is ok during bootup?
279 There was a crash because ifp here was coming to be NULL */
281 if (connected_is_unnumbered(ifp
))
282 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
284 route_entry_nexthop_add(re
, nexthop
);
289 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
290 struct in6_addr
*ipv6
,
293 struct nexthop
*nexthop
;
295 nexthop
= nexthop_new();
296 nexthop
->vrf_id
= nh_vrf_id
;
297 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
298 nexthop
->gate
.ipv6
= *ipv6
;
300 route_entry_nexthop_add(re
, nexthop
);
305 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
306 struct in6_addr
*ipv6
,
310 struct nexthop
*nexthop
;
312 nexthop
= nexthop_new();
313 nexthop
->vrf_id
= nh_vrf_id
;
314 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
315 nexthop
->gate
.ipv6
= *ipv6
;
316 nexthop
->ifindex
= ifindex
;
318 route_entry_nexthop_add(re
, nexthop
);
323 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
324 enum blackhole_type bh_type
)
326 struct nexthop
*nexthop
;
328 nexthop
= nexthop_new();
329 nexthop
->vrf_id
= VRF_DEFAULT
;
330 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
331 nexthop
->bh_type
= bh_type
;
333 route_entry_nexthop_add(re
, nexthop
);
338 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
339 union g_addr
*addr
, struct route_node
**rn_out
)
342 struct route_table
*table
;
343 struct route_node
*rn
;
344 struct route_entry
*match
= NULL
;
347 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
351 memset(&p
, 0, sizeof(struct prefix
));
354 p
.u
.prefix4
= addr
->ipv4
;
355 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
357 p
.u
.prefix6
= addr
->ipv6
;
358 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
361 rn
= route_node_match(table
, (struct prefix
*)&p
);
366 route_unlock_node(rn
);
368 dest
= rib_dest_from_rnode(rn
);
369 if (dest
&& dest
->selected_fib
370 && !CHECK_FLAG(dest
->selected_fib
->status
,
371 ROUTE_ENTRY_REMOVED
))
372 match
= dest
->selected_fib
;
374 /* If there is no selected route or matched route is EGP, go up
379 } while (rn
&& rn
->info
== NULL
);
383 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
384 if (!CHECK_FLAG(match
->status
,
385 ROUTE_ENTRY_INSTALLED
))
397 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
399 struct route_node
**rn_out
)
401 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
402 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
403 union g_addr gaddr
= {.ipv4
= addr
};
405 switch (zrouter
.ipv4_multicast_mode
) {
406 case MCAST_MRIB_ONLY
:
407 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
409 case MCAST_URIB_ONLY
:
410 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
411 case MCAST_NO_CONFIG
:
412 case MCAST_MIX_MRIB_FIRST
:
413 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
416 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
419 case MCAST_MIX_DISTANCE
:
420 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
421 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
423 re
= ure
->distance
< mre
->distance
? ure
: mre
;
429 case MCAST_MIX_PFXLEN
:
430 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
431 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
433 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
442 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
444 if (IS_ZEBRA_DEBUG_RIB
) {
446 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
448 zlog_debug("%s: %s: vrf: %u found %s, using %s",
449 __func__
, buf
, vrf_id
,
450 mre
? (ure
? "MRIB+URIB" : "MRIB")
451 : ure
? "URIB" : "nothing",
452 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
457 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
459 struct route_table
*table
;
460 struct route_node
*rn
;
461 struct route_entry
*match
= NULL
;
465 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
469 rn
= route_node_lookup(table
, (struct prefix
*)p
);
471 /* No route for this prefix. */
476 route_unlock_node(rn
);
477 dest
= rib_dest_from_rnode(rn
);
479 if (dest
&& dest
->selected_fib
480 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
481 match
= dest
->selected_fib
;
486 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
489 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
496 * Is this RIB labeled-unicast? It must be of type BGP and all paths
497 * (nexthops) must have a label.
499 int zebra_rib_labeled_unicast(struct route_entry
*re
)
501 struct nexthop
*nexthop
= NULL
;
503 if (re
->type
!= ZEBRA_ROUTE_BGP
)
506 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
507 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
513 /* Update flag indicates whether this is a "replace" or not. Currently, this
514 * is only used for IPv4.
516 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
517 struct route_entry
*old
)
519 struct nexthop
*nexthop
;
520 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
521 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
522 const struct prefix
*p
, *src_p
;
523 enum zebra_dplane_result ret
;
525 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
527 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
529 if (info
->safi
!= SAFI_UNICAST
) {
530 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
531 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
534 struct nexthop
*prev
;
536 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
537 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
538 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
541 if (nexthop_same_firsthop(nexthop
, prev
)) {
542 SET_FLAG(nexthop
->flags
,
543 NEXTHOP_FLAG_DUPLICATE
);
551 * If this is a replace to a new RE let the originator of the RE
552 * know that they've lost
554 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
555 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
557 /* Update fib selection */
558 dest
->selected_fib
= re
;
561 * Make sure we update the FPM any time we send new information to
564 hook_call(rib_update
, rn
, "installing in kernel");
566 /* Send add or update */
568 ret
= dplane_route_update(rn
, re
, old
);
570 ret
= dplane_route_add(rn
, re
);
573 case ZEBRA_DPLANE_REQUEST_QUEUED
:
574 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
577 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
579 /* Free old FIB nexthop group */
580 if (old
->fib_ng
.nexthop
) {
581 nexthops_free(old
->fib_ng
.nexthop
);
582 old
->fib_ng
.nexthop
= NULL
;
585 if (!RIB_SYSTEM_ROUTE(old
)) {
586 /* Clear old route's FIB flags */
587 for (ALL_NEXTHOPS(old
->ng
, nexthop
)) {
588 UNSET_FLAG(nexthop
->flags
,
595 zvrf
->installs_queued
++;
597 case ZEBRA_DPLANE_REQUEST_FAILURE
:
599 char str
[SRCDEST2STR_BUFFER
];
601 srcdest_rnode2str(rn
, str
, sizeof(str
));
602 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
603 "%u:%s: Failed to enqueue dataplane install",
607 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
616 /* Uninstall the route from kernel. */
617 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
619 struct nexthop
*nexthop
;
620 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
621 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
623 if (info
->safi
!= SAFI_UNICAST
) {
624 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
625 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
626 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
631 * Make sure we update the FPM any time we send new information to
634 hook_call(rib_update
, rn
, "uninstalling from kernel");
636 switch (dplane_route_delete(rn
, re
)) {
637 case ZEBRA_DPLANE_REQUEST_QUEUED
:
639 zvrf
->removals_queued
++;
641 case ZEBRA_DPLANE_REQUEST_FAILURE
:
643 char str
[SRCDEST2STR_BUFFER
];
645 srcdest_rnode2str(rn
, str
, sizeof(str
));
646 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
647 "%u:%s: Failed to enqueue dataplane uninstall",
651 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
660 /* Uninstall the route from kernel. */
661 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
663 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
664 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
665 struct nexthop
*nexthop
;
667 if (dest
&& dest
->selected_fib
== re
) {
668 if (info
->safi
== SAFI_UNICAST
)
669 hook_call(rib_update
, rn
, "rib_uninstall");
671 /* If labeled-unicast route, uninstall transit LSP. */
672 if (zebra_rib_labeled_unicast(re
))
673 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
675 rib_uninstall_kernel(rn
, re
);
677 dest
->selected_fib
= NULL
;
679 /* Free FIB nexthop group, if present */
680 if (re
->fib_ng
.nexthop
) {
681 nexthops_free(re
->fib_ng
.nexthop
);
682 re
->fib_ng
.nexthop
= NULL
;
685 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
686 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
689 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
690 const struct prefix
*p
, *src_p
;
692 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
694 redistribute_delete(p
, src_p
, re
, NULL
);
695 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
700 * rib_can_delete_dest
702 * Returns true if the given dest can be deleted from the table.
704 static int rib_can_delete_dest(rib_dest_t
*dest
)
706 if (re_list_first(&dest
->routes
)) {
711 * Unresolved rnh's are stored on the default route's list
713 * dest->rnode can also be the source prefix node in an
714 * ipv6 sourcedest table. Fortunately the prefix of a
715 * source prefix node can never be the default prefix.
717 if (is_default_prefix(&dest
->rnode
->p
))
721 * Don't delete the dest if we have to update the FPM about this
724 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
725 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
731 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
733 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
737 * We are storing the rnh's associated withb
738 * the tracked nexthop as a list of the rn's.
739 * Unresolved rnh's are placed at the top
740 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
741 * As such for each rn we need to walk up the tree
742 * and see if any rnh's need to see if they
743 * would match a more specific route
746 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
747 char buf
[PREFIX_STRLEN
];
749 zlog_debug("%s: %s Being examined for Nexthop Tracking Count: %zd",
751 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
752 dest
? rnh_list_count(&dest
->nht
) : 0);
757 dest
= rib_dest_from_rnode(rn
);
761 * If we have any rnh's stored in the nht list
762 * then we know that this route node was used for
763 * nht resolution and as such we need to call the
764 * nexthop tracking evaluation code
766 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
767 struct zebra_vrf
*zvrf
=
768 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
769 struct prefix
*p
= &rnh
->node
->p
;
771 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
772 char buf1
[PREFIX_STRLEN
];
773 char buf2
[PREFIX_STRLEN
];
775 zlog_debug("%u:%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u",
777 srcdest_rnode2str(rn
, buf1
,
779 prefix2str(p
, buf2
, sizeof(buf2
)),
780 rnh_type2str(rnh
->type
),
785 * If we have evaluated this node on this pass
786 * already, due to following the tree up
787 * then we know that we can move onto the next
790 * Additionally we call zebra_evaluate_rnh
791 * when we gc the dest. In this case we know
792 * that there must be no other re's where
793 * we were originally as such we know that
794 * that sequence number is ok to respect.
796 if (rnh
->seqno
== seq
) {
797 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
799 "\tNode processed and moved already");
804 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
810 dest
= rib_dest_from_rnode(rn
);
817 * Garbage collect the rib dest corresponding to the given route node
820 * Returns true if the dest was deleted, false otherwise.
822 int rib_gc_dest(struct route_node
*rn
)
826 dest
= rib_dest_from_rnode(rn
);
830 if (!rib_can_delete_dest(dest
))
833 if (IS_ZEBRA_DEBUG_RIB
) {
834 struct zebra_vrf
*zvrf
;
836 zvrf
= rib_dest_vrf(dest
);
837 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
840 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
843 rnh_list_fini(&dest
->nht
);
844 XFREE(MTYPE_RIB_DEST
, dest
);
848 * Release the one reference that we keep on the route node.
850 route_unlock_node(rn
);
854 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
855 struct route_entry
*new)
857 hook_call(rib_update
, rn
, "new route selected");
859 /* Update real nexthop. This may actually determine if nexthop is active
861 if (!nexthop_group_active_nexthop_num(&new->ng
)) {
862 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
866 if (IS_ZEBRA_DEBUG_RIB
) {
867 char buf
[SRCDEST2STR_BUFFER
];
868 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
869 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
870 zvrf_id(zvrf
), buf
, rn
, new,
871 zebra_route_string(new->type
));
874 /* If labeled-unicast route, install transit LSP. */
875 if (zebra_rib_labeled_unicast(new))
876 zebra_mpls_lsp_install(zvrf
, rn
, new);
878 rib_install_kernel(rn
, new, NULL
);
880 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
883 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
884 struct route_entry
*old
)
886 hook_call(rib_update
, rn
, "removing existing route");
888 /* Uninstall from kernel. */
889 if (IS_ZEBRA_DEBUG_RIB
) {
890 char buf
[SRCDEST2STR_BUFFER
];
891 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
892 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
893 zvrf_id(zvrf
), buf
, rn
, old
,
894 zebra_route_string(old
->type
));
897 /* If labeled-unicast route, uninstall transit LSP. */
898 if (zebra_rib_labeled_unicast(old
))
899 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
901 rib_uninstall_kernel(rn
, old
);
903 /* Update nexthop for route, reset changed flag. */
904 /* Note: this code also handles the Linux case when an interface goes
905 * down, causing the kernel to delete routes without sending DELROUTE
908 if (RIB_KERNEL_ROUTE(old
))
909 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
911 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
914 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
915 struct route_node
*rn
,
916 struct route_entry
*old
,
917 struct route_entry
*new)
922 * We have to install or update if a new route has been selected or
923 * something has changed.
925 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
926 hook_call(rib_update
, rn
, "updating existing route");
928 /* Update the nexthop; we could determine here that nexthop is
930 if (nexthop_group_active_nexthop_num(&new->ng
))
933 /* If nexthop is active, install the selected route, if
935 * the install succeeds, cleanup flags for prior route, if
940 if (IS_ZEBRA_DEBUG_RIB
) {
941 char buf
[SRCDEST2STR_BUFFER
];
942 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
945 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
946 zvrf_id(zvrf
), buf
, rn
, new,
947 zebra_route_string(new->type
),
949 zebra_route_string(old
->type
));
952 "%u:%s: Updating route rn %p, re %p (%s)",
953 zvrf_id(zvrf
), buf
, rn
, new,
954 zebra_route_string(new->type
));
957 /* If labeled-unicast route, uninstall transit LSP. */
958 if (zebra_rib_labeled_unicast(old
))
959 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
962 * Non-system route should be installed.
963 * If labeled-unicast route, install transit
966 if (zebra_rib_labeled_unicast(new))
967 zebra_mpls_lsp_install(zvrf
, rn
, new);
969 rib_install_kernel(rn
, new, old
);
973 * If nexthop for selected route is not active or install
975 * may need to uninstall and delete for redistribution.
978 if (IS_ZEBRA_DEBUG_RIB
) {
979 char buf
[SRCDEST2STR_BUFFER
];
980 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
983 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
984 zvrf_id(zvrf
), buf
, rn
, new,
985 zebra_route_string(new->type
),
987 zebra_route_string(old
->type
));
990 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
991 zvrf_id(zvrf
), buf
, rn
, new,
992 zebra_route_string(new->type
));
995 /* If labeled-unicast route, uninstall transit LSP. */
996 if (zebra_rib_labeled_unicast(old
))
997 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
999 rib_uninstall_kernel(rn
, old
);
1003 * Same route selected; check if in the FIB and if not,
1004 * re-install. This is housekeeping code to deal with
1005 * race conditions in kernel with linux netlink reporting
1006 * interface up before IPv4 or IPv6 protocol is ready
1009 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1010 RIB_SYSTEM_ROUTE(new))
1011 rib_install_kernel(rn
, new, NULL
);
1014 /* Update prior route. */
1016 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1018 /* Clear changed flag. */
1019 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1022 /* Check if 'alternate' RIB entry is better than 'current'. */
1023 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1024 struct route_entry
*alternate
)
1026 if (current
== NULL
)
1029 /* filter route selection in following order:
1030 * - connected beats other types
1031 * - if both connected, loopback or vrf wins
1032 * - lower distance beats higher
1033 * - lower metric beats higher for equal distance
1034 * - last, hence oldest, route wins tie break.
1037 /* Connected routes. Check to see if either are a vrf
1038 * or loopback interface. If not, pick the last connected
1039 * route of the set of lowest metric connected routes.
1041 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1042 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1045 /* both are connected. are either loop or vrf? */
1046 struct nexthop
*nexthop
= NULL
;
1048 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1049 struct interface
*ifp
= if_lookup_by_index(
1050 nexthop
->ifindex
, alternate
->vrf_id
);
1052 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1056 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1057 struct interface
*ifp
= if_lookup_by_index(
1058 nexthop
->ifindex
, current
->vrf_id
);
1060 if (ifp
&& if_is_loopback_or_vrf(ifp
))
1064 /* Neither are loop or vrf so pick best metric */
1065 if (alternate
->metric
<= current
->metric
)
1071 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1074 /* higher distance loses */
1075 if (alternate
->distance
< current
->distance
)
1077 if (current
->distance
< alternate
->distance
)
1080 /* metric tie-breaks equal distance */
1081 if (alternate
->metric
<= current
->metric
)
1087 /* Core function for processing routing information base. */
1088 static void rib_process(struct route_node
*rn
)
1090 struct route_entry
*re
;
1091 struct route_entry
*next
;
1092 struct route_entry
*old_selected
= NULL
;
1093 struct route_entry
*new_selected
= NULL
;
1094 struct route_entry
*old_fib
= NULL
;
1095 struct route_entry
*new_fib
= NULL
;
1096 struct route_entry
*best
= NULL
;
1097 char buf
[SRCDEST2STR_BUFFER
];
1099 struct zebra_vrf
*zvrf
= NULL
;
1100 const struct prefix
*p
, *src_p
;
1102 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1103 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1107 dest
= rib_dest_from_rnode(rn
);
1109 zvrf
= rib_dest_vrf(dest
);
1110 vrf_id
= zvrf_id(zvrf
);
1113 if (IS_ZEBRA_DEBUG_RIB
)
1114 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1116 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1117 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1120 * we can have rn's that have a NULL info pointer
1121 * (dest). As such let's not let the deref happen
1122 * additionally we know RNODE_FOREACH_RE_SAFE
1123 * will not iterate so we are ok.
1126 old_fib
= dest
->selected_fib
;
1128 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1129 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1131 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1132 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1133 re
->status
, re
->flags
, re
->distance
,
1136 /* Currently selected re. */
1137 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1138 assert(old_selected
== NULL
);
1142 /* Skip deleted entries from selection */
1143 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1146 /* Skip unreachable nexthop. */
1147 /* This first call to nexthop_active_update is merely to
1148 * determine if there's any change to nexthops associated
1149 * with this RIB entry. Now, rib_process() can be invoked due
1150 * to an external event such as link down or due to
1151 * next-hop-tracking evaluation. In the latter case,
1152 * a decision has already been made that the NHs have changed.
1153 * So, no need to invoke a potentially expensive call again.
1154 * Further, since the change might be in a recursive NH which
1155 * is not caught in the nexthop_active_update() code. Thus, we
1156 * might miss changes to recursive NHs.
1158 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1159 && !nexthop_active_update(rn
, re
)) {
1160 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1161 /* XXX: HERE BE DRAGONS!!!!!
1162 * In all honesty, I have not yet figured out
1163 * what this part does or why the
1164 * ROUTE_ENTRY_CHANGED test above is correct
1165 * or why we need to delete a route here, and
1166 * also not whether this concerns both selected
1167 * and fib route, or only selected
1170 * This entry was denied by the 'ip protocol
1171 * table' route-map, we need to delete it */
1172 if (re
!= old_selected
) {
1173 if (IS_ZEBRA_DEBUG_RIB
)
1175 "%s: %u:%s: imported via import-table but denied "
1176 "by the ip protocol table route-map",
1177 __func__
, vrf_id
, buf
);
1180 SET_FLAG(re
->status
,
1181 ROUTE_ENTRY_REMOVED
);
1187 /* Infinite distance. */
1188 if (re
->distance
== DISTANCE_INFINITY
) {
1189 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1193 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1194 best
= rib_choose_best(new_fib
, re
);
1195 if (new_fib
&& best
!= new_fib
)
1196 UNSET_FLAG(new_fib
->status
,
1197 ROUTE_ENTRY_CHANGED
);
1200 best
= rib_choose_best(new_selected
, re
);
1201 if (new_selected
&& best
!= new_selected
)
1202 UNSET_FLAG(new_selected
->status
,
1203 ROUTE_ENTRY_CHANGED
);
1204 new_selected
= best
;
1207 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1208 } /* RNODE_FOREACH_RE */
1210 /* If no FIB override route, use the selected route also for FIB */
1211 if (new_fib
== NULL
)
1212 new_fib
= new_selected
;
1214 /* After the cycle is finished, the following pointers will be set:
1215 * old_selected --- RE entry currently having SELECTED
1216 * new_selected --- RE entry that is newly SELECTED
1217 * old_fib --- RE entry currently in kernel FIB
1218 * new_fib --- RE entry that is newly to be in kernel FIB
1220 * new_selected will get SELECTED flag, and is going to be redistributed
1221 * the zclients. new_fib (which can be new_selected) will be installed
1225 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1227 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1228 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1229 (void *)old_fib
, (void *)new_fib
);
1232 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1233 * fib == selected */
1234 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1235 ROUTE_ENTRY_CHANGED
);
1237 /* Update fib according to selection results */
1238 if (new_fib
&& old_fib
)
1239 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1241 rib_process_add_fib(zvrf
, rn
, new_fib
);
1243 rib_process_del_fib(zvrf
, rn
, old_fib
);
1245 /* Update SELECTED entry */
1246 if (old_selected
!= new_selected
|| selected_changed
) {
1248 if (new_selected
&& new_selected
!= new_fib
)
1249 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1252 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1256 * If we're removing the old entry, we should tell
1257 * redist subscribers about that *if* they aren't
1258 * going to see a redist for the new entry.
1260 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1261 ROUTE_ENTRY_REMOVED
))
1262 redistribute_delete(p
, src_p
,
1266 if (old_selected
!= new_selected
)
1267 UNSET_FLAG(old_selected
->flags
,
1268 ZEBRA_FLAG_SELECTED
);
1272 /* Remove all RE entries queued for removal */
1273 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1274 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1275 if (IS_ZEBRA_DEBUG_RIB
) {
1276 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1277 (void *)rn
, (void *)re
);
1284 * Check if the dest can be deleted now.
1289 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1291 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1292 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1297 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1298 if (IS_ZEBRA_DEBUG_MPLS
)
1300 "%u: Scheduling all LSPs upon RIB completion",
1302 zebra_mpls_lsp_schedule(zvrf
);
1303 mpls_unmark_lsps_for_processing(rn
);
1308 * Utility to match route with dplane context data
1310 static bool rib_route_match_ctx(const struct route_entry
*re
,
1311 const struct zebra_dplane_ctx
*ctx
,
1314 bool result
= false;
1318 * In 'update' case, we test info about the 'previous' or
1321 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1322 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1325 /* TODO -- we're using this extra test, but it's not
1326 * exactly clear why.
1328 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1329 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1330 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1337 * Ordinary, single-route case using primary context info
1339 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1340 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1341 /* Skip route that's been deleted */
1345 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1346 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1349 /* TODO -- we're using this extra test, but it's not
1350 * exactly clear why.
1352 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1353 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1354 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1365 static void zebra_rib_fixup_system(struct route_node
*rn
)
1367 struct route_entry
*re
;
1369 RNODE_FOREACH_RE(rn
, re
) {
1370 struct nexthop
*nhop
;
1372 if (!RIB_SYSTEM_ROUTE(re
))
1375 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1378 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1379 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1381 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1382 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1385 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1391 * Update a route from a dplane context. This consolidates common code
1392 * that can be used in processing of results from FIB updates, and in
1393 * async notification processing.
1394 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1396 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1397 struct route_node
*rn
,
1398 struct zebra_dplane_ctx
*ctx
)
1400 char dest_str
[PREFIX_STRLEN
] = "";
1401 char nh_str
[NEXTHOP_STRLEN
];
1402 struct nexthop
*nexthop
, *ctx_nexthop
;
1404 const struct nexthop_group
*ctxnhg
;
1405 bool is_selected
= false; /* Is 're' currently the selected re? */
1406 bool changed_p
= false; /* Change to nexthops? */
1409 /* Note well: only capturing the prefix string if debug is enabled here;
1410 * unconditional log messages will have to generate the string.
1412 if (IS_ZEBRA_DEBUG_RIB
)
1413 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1415 dest
= rib_dest_from_rnode(rn
);
1417 is_selected
= (re
== dest
->selected_fib
);
1419 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1420 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1421 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1423 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1424 * If the installed set differs from the set requested by the rib/owner,
1425 * we use the fib-specific nexthop-group to record the actual FIB
1430 * First check the fib nexthop-group, if it's present. The comparison
1431 * here is quite strict: we require that the fib sets match exactly.
1435 if (re
->fib_ng
.nexthop
== NULL
)
1440 /* First check the route's fib nexthops */
1441 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1443 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1447 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1449 if (nexthop_same(ctx_nexthop
, nexthop
))
1453 if (ctx_nexthop
== NULL
) {
1454 /* Nexthop not in the new installed set */
1455 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1456 nexthop2str(nexthop
, nh_str
,
1458 zlog_debug("update_from_ctx: no match for fib nh %s",
1470 /* Check the new installed set */
1472 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1474 if (CHECK_FLAG(ctx_nexthop
->flags
,
1475 NEXTHOP_FLAG_RECURSIVE
))
1478 /* Compare with the current group's nexthops */
1480 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1481 if (nexthop_same(nexthop
, ctx_nexthop
))
1485 if (nexthop
== NULL
) {
1486 /* Nexthop not in the old installed set */
1487 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1488 nexthop2str(ctx_nexthop
, nh_str
,
1490 zlog_debug("update_from_ctx: no fib match for notif nh %s",
1500 /* If the new FIB set matches the existing FIB set, we're done. */
1502 if (IS_ZEBRA_DEBUG_RIB
)
1503 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1504 re
->vrf_id
, dest_str
);
1507 } else if (re
->fib_ng
.nexthop
) {
1509 * Free stale fib list and move on to check the rib nhg.
1511 if (IS_ZEBRA_DEBUG_RIB
)
1512 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1513 re
->vrf_id
, dest_str
);
1514 nexthops_free(re
->fib_ng
.nexthop
);
1515 re
->fib_ng
.nexthop
= NULL
;
1517 /* Note that the installed nexthops have changed */
1520 if (IS_ZEBRA_DEBUG_RIB
)
1521 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1522 re
->vrf_id
, dest_str
);
1526 * Compare with the rib nexthop group. The comparison here is different:
1527 * the RIB group may be a superset of the list installed in the FIB. We
1528 * walk the RIB group, looking for the 'installable' candidate
1529 * nexthops, and then check those against the set
1530 * that is actually installed.
1533 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1535 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1538 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1541 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1543 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1544 if (nexthop_same(ctx_nexthop
, nexthop
))
1548 /* If the FIB doesn't know about the nexthop,
1549 * it's not installed
1551 if (ctx_nexthop
== NULL
) {
1552 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1553 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1554 zlog_debug("update_from_ctx: no notif match for rib nh %s",
1559 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1562 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1564 /* Keep checking nexthops */
1568 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1569 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1572 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1574 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1577 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1581 /* If all nexthops were processed, we're done */
1583 if (IS_ZEBRA_DEBUG_RIB
)
1584 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1585 re
->vrf_id
, dest_str
,
1586 (changed_p
? "true" : "false"));
1590 /* FIB nexthop set differs from the RIB set:
1591 * create a fib-specific nexthop-group
1593 if (IS_ZEBRA_DEBUG_RIB
)
1594 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1595 re
->vrf_id
, dest_str
,
1596 (changed_p
? "true" : "false"));
1598 ctxnhg
= dplane_ctx_get_ng(ctx
);
1600 if (ctxnhg
->nexthop
)
1601 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1603 /* Bit of a special case when the fib has _no_ installed
1606 nexthop
= nexthop_new();
1607 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1608 _nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1616 * Helper to locate a zebra route-node from a dplane context. This is used
1617 * when processing dplane results, e.g. Note well: the route-node is returned
1618 * with a ref held - route_unlock_node() must be called eventually.
1620 static struct route_node
*
1621 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1623 struct route_table
*table
= NULL
;
1624 struct route_node
*rn
= NULL
;
1625 const struct prefix
*dest_pfx
, *src_pfx
;
1627 /* Locate rn and re(s) from ctx */
1629 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1630 dplane_ctx_get_safi(ctx
),
1631 dplane_ctx_get_vrf(ctx
),
1632 dplane_ctx_get_table(ctx
));
1633 if (table
== NULL
) {
1634 if (IS_ZEBRA_DEBUG_DPLANE
) {
1635 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1636 dplane_ctx_get_afi(ctx
),
1637 dplane_ctx_get_safi(ctx
),
1638 dplane_ctx_get_vrf(ctx
));
1643 dest_pfx
= dplane_ctx_get_dest(ctx
);
1644 src_pfx
= dplane_ctx_get_src(ctx
);
1646 rn
= srcdest_rnode_get(table
, dest_pfx
,
1647 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1656 * Route-update results processing after async dataplane update.
1658 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1660 struct zebra_vrf
*zvrf
= NULL
;
1661 struct route_node
*rn
= NULL
;
1662 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1663 bool is_update
= false;
1664 char dest_str
[PREFIX_STRLEN
] = "";
1665 enum dplane_op_e op
;
1666 enum zebra_dplane_result status
;
1667 const struct prefix
*dest_pfx
, *src_pfx
;
1669 bool fib_changed
= false;
1671 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1672 dest_pfx
= dplane_ctx_get_dest(ctx
);
1674 /* Note well: only capturing the prefix string if debug is enabled here;
1675 * unconditional log messages will have to generate the string.
1677 if (IS_ZEBRA_DEBUG_DPLANE
)
1678 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1680 /* Locate rn and re(s) from ctx */
1681 rn
= rib_find_rn_from_ctx(ctx
);
1683 if (IS_ZEBRA_DEBUG_DPLANE
) {
1684 zlog_debug("Failed to process dplane results: no route for %u:%s",
1685 dplane_ctx_get_vrf(ctx
), dest_str
);
1690 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1692 op
= dplane_ctx_get_op(ctx
);
1693 status
= dplane_ctx_get_status(ctx
);
1695 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1696 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1697 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1698 dplane_op2str(op
), dplane_res2str(status
));
1701 * Update is a bit of a special case, where we may have both old and new
1702 * routes to post-process.
1704 is_update
= dplane_ctx_is_update(ctx
);
1707 * Take a pass through the routes, look for matches with the context
1710 RNODE_FOREACH_RE(rn
, rib
) {
1713 if (rib_route_match_ctx(rib
, ctx
, false))
1717 /* Check for old route match */
1718 if (is_update
&& (old_re
== NULL
)) {
1719 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1723 /* Have we found the routes we need to work on? */
1724 if (re
&& ((!is_update
|| old_re
)))
1728 seq
= dplane_ctx_get_seq(ctx
);
1731 * Check sequence number(s) to detect stale results before continuing
1734 if (re
->dplane_sequence
!= seq
) {
1735 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1736 zlog_debug("%u:%s Stale dplane result for re %p",
1737 dplane_ctx_get_vrf(ctx
),
1740 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1744 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1745 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1746 zlog_debug("%u:%s Stale dplane result for old_re %p",
1747 dplane_ctx_get_vrf(ctx
),
1750 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1754 case DPLANE_OP_ROUTE_INSTALL
:
1755 case DPLANE_OP_ROUTE_UPDATE
:
1756 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1758 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1759 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1762 * On an update operation from the same route type
1763 * context retrieval currently has no way to know
1764 * which was the old and which was the new.
1765 * So don't unset our flags that we just set.
1766 * We know redistribution is ok because the
1767 * old_re in this case is used for nothing
1768 * more than knowing whom to contact if necessary.
1770 if (old_re
&& old_re
!= re
) {
1771 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1772 UNSET_FLAG(old_re
->status
,
1773 ROUTE_ENTRY_INSTALLED
);
1776 /* Update zebra route based on the results in
1777 * the context struct.
1781 rib_update_re_from_ctx(re
, rn
, ctx
);
1784 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1785 zlog_debug("%u:%s no fib change for re",
1792 redistribute_update(dest_pfx
, src_pfx
,
1797 * System routes are weird in that they
1798 * allow multiple to be installed that match
1799 * to the same prefix, so after we get the
1800 * result we need to clean them up so that
1801 * we can actually use them.
1803 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1804 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1805 zebra_rib_fixup_system(rn
);
1810 /* Notify route owner */
1811 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1815 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1816 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1818 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1820 zsend_route_notify_owner(re
, dest_pfx
,
1821 ZAPI_ROUTE_FAIL_INSTALL
);
1823 zlog_warn("%u:%s: Route install failed",
1824 dplane_ctx_get_vrf(ctx
),
1825 prefix2str(dest_pfx
,
1826 dest_str
, sizeof(dest_str
)));
1829 case DPLANE_OP_ROUTE_DELETE
:
1831 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1833 * In the delete case, the zebra core datastructs were
1834 * updated (or removed) at the time the delete was issued,
1835 * so we're just notifying the route owner.
1837 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1839 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1840 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1842 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1848 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1849 zsend_route_notify_owner_ctx(ctx
,
1850 ZAPI_ROUTE_REMOVE_FAIL
);
1852 zlog_warn("%u:%s: Route Deletion failure",
1853 dplane_ctx_get_vrf(ctx
),
1854 prefix2str(dest_pfx
,
1855 dest_str
, sizeof(dest_str
)));
1859 * System routes are weird in that they
1860 * allow multiple to be installed that match
1861 * to the same prefix, so after we get the
1862 * result we need to clean them up so that
1863 * we can actually use them.
1865 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1866 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1867 zebra_rib_fixup_system(rn
);
1873 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1874 zebra_rib_evaluate_mpls(rn
);
1878 route_unlock_node(rn
);
1880 /* Return context to dataplane module */
1881 dplane_ctx_fini(&ctx
);
1885 * Handle notification from async dataplane: the dataplane has detected
1886 * some change to a route, and notifies zebra so that the control plane
1887 * can reflect that change.
1889 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1891 struct route_node
*rn
= NULL
;
1892 struct route_entry
*re
= NULL
;
1893 struct nexthop
*nexthop
;
1894 char dest_str
[PREFIX_STRLEN
] = "";
1895 const struct prefix
*dest_pfx
, *src_pfx
;
1897 bool fib_changed
= false;
1898 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1899 int start_count
, end_count
;
1900 dest_pfx
= dplane_ctx_get_dest(ctx
);
1902 /* Note well: only capturing the prefix string if debug is enabled here;
1903 * unconditional log messages will have to generate the string.
1906 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1908 /* Locate rn and re(s) from ctx */
1909 rn
= rib_find_rn_from_ctx(ctx
);
1912 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1913 dplane_ctx_get_vrf(ctx
), dest_str
);
1918 dest
= rib_dest_from_rnode(rn
);
1919 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1922 zlog_debug("%u:%s Processing dplane notif ctx %p",
1923 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1926 * Take a pass through the routes, look for matches with the context
1929 RNODE_FOREACH_RE(rn
, re
) {
1930 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1934 /* No match? Nothing we can do */
1937 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1938 dplane_ctx_get_vrf(ctx
), dest_str
,
1940 dplane_ctx_get_type(ctx
)));
1945 /* Ensure we clear the QUEUED flag */
1946 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1948 /* Is this a notification that ... matters? We only really care about
1949 * the route that is currently selected for installation.
1951 if (re
!= dest
->selected_fib
) {
1952 /* TODO -- don't skip processing entirely? We might like to
1953 * at least report on the event.
1956 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1957 dplane_ctx_get_vrf(ctx
), dest_str
,
1959 dplane_ctx_get_type(ctx
)));
1963 /* We'll want to determine whether the installation status of the
1964 * route has changed: we'll check the status before processing,
1965 * and then again if there's been a change.
1968 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1969 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1973 /* Update zebra's nexthop FIB flags based on the context struct's
1976 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1980 zlog_debug("%u:%s No change from dplane notification",
1981 dplane_ctx_get_vrf(ctx
), dest_str
);
1987 * Perform follow-up work if the actual status of the prefix
1992 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1993 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1997 /* Various fib transitions: changed nexthops; from installed to
1998 * not-installed; or not-installed to installed.
2000 if (start_count
> 0 && end_count
> 0) {
2002 zlog_debug("%u:%s applied nexthop changes from dplane notification",
2003 dplane_ctx_get_vrf(ctx
), dest_str
);
2005 /* Changed nexthops - update kernel/others */
2006 dplane_route_notif_update(rn
, re
,
2007 DPLANE_OP_ROUTE_UPDATE
, ctx
);
2009 } else if (start_count
== 0 && end_count
> 0) {
2011 zlog_debug("%u:%s installed transition from dplane notification",
2012 dplane_ctx_get_vrf(ctx
), dest_str
);
2014 /* We expect this to be the selected route, so we want
2015 * to tell others about this transition.
2017 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2019 /* Changed nexthops - update kernel/others */
2020 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
2022 /* Redistribute, lsp, and nht update */
2023 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2025 zebra_rib_evaluate_rn_nexthops(
2026 rn
, zebra_router_get_next_sequence());
2028 zebra_rib_evaluate_mpls(rn
);
2030 } else if (start_count
> 0 && end_count
== 0) {
2032 zlog_debug("%u:%s un-installed transition from dplane notification",
2033 dplane_ctx_get_vrf(ctx
), dest_str
);
2035 /* Transition from _something_ installed to _nothing_
2038 /* We expect this to be the selected route, so we want
2039 * to tell others about this transistion.
2041 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2043 /* Changed nexthops - update kernel/others */
2044 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2046 /* Redistribute, lsp, and nht update */
2047 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
2049 zebra_rib_evaluate_rn_nexthops(
2050 rn
, zebra_router_get_next_sequence());
2052 zebra_rib_evaluate_mpls(rn
);
2057 route_unlock_node(rn
);
2059 /* Return context to dataplane module */
2060 dplane_ctx_fini(&ctx
);
2063 /* Take a list of route_node structs and return 1, if there was a record
2064 * picked from it and processed by rib_process(). Don't process more,
2065 * than one RN record; operate only in the specified sub-queue.
2067 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2069 struct listnode
*lnode
= listhead(subq
);
2070 struct route_node
*rnode
;
2072 struct zebra_vrf
*zvrf
= NULL
;
2077 rnode
= listgetdata(lnode
);
2078 dest
= rib_dest_from_rnode(rnode
);
2080 zvrf
= rib_dest_vrf(dest
);
2084 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2085 char buf
[SRCDEST2STR_BUFFER
];
2087 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2088 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2089 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2093 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2094 RIB_ROUTE_QUEUED(qindex
));
2099 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2100 __func__
, rnode
, rnode
->lock
);
2101 zlog_backtrace(LOG_DEBUG
);
2104 route_unlock_node(rnode
);
2105 list_delete_node(subq
, lnode
);
2109 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2110 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2112 * is pointed to the meta queue structure.
2114 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2116 struct meta_queue
*mq
= data
;
2118 uint32_t queue_len
, queue_limit
;
2120 /* Ensure there's room for more dataplane updates */
2121 queue_limit
= dplane_get_in_queue_limit();
2122 queue_len
= dplane_get_in_queue_len();
2123 if (queue_len
> queue_limit
) {
2124 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2125 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2126 queue_len
, queue_limit
);
2128 /* Ensure that the meta-queue is actually enqueued */
2129 if (work_queue_empty(zrouter
.ribq
))
2130 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2132 return WQ_QUEUE_BLOCKED
;
2135 for (i
= 0; i
< MQ_SIZE
; i
++)
2136 if (process_subq(mq
->subq
[i
], i
)) {
2140 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2145 * Look into the RN and queue it into the highest priority queue
2146 * at this point in time for processing.
2148 * We will enqueue a route node only once per invocation.
2150 * There are two possibilities here that should be kept in mind.
2151 * If the original invocation has not been pulled off for processing
2152 * yet, A subsuquent invocation can have a route entry with a better
2153 * meta queue index value and we can have a situation where
2154 * we might have the same node enqueued 2 times. Not necessarily
2155 * an optimal situation but it should be ok.
2157 * The other possibility is that the original invocation has not
2158 * been pulled off for processing yet, A subsusquent invocation
2159 * doesn't have a route_entry with a better meta-queue and the
2160 * original metaqueue index value will win and we'll end up with
2161 * the route node enqueued once.
2163 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2165 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2166 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2168 RNODE_FOREACH_RE (rn
, curr_re
) {
2169 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2171 if (curr_qindex
<= qindex
) {
2173 qindex
= curr_qindex
;
2180 /* Invariant: at this point we always have rn->info set. */
2181 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2182 RIB_ROUTE_QUEUED(qindex
))) {
2183 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2184 rnode_debug(rn
, re
->vrf_id
,
2185 "rn %p is already queued in sub-queue %u",
2186 (void *)rn
, qindex
);
2190 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2191 listnode_add(mq
->subq
[qindex
], rn
);
2192 route_lock_node(rn
);
2195 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2196 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2197 (void *)rn
, qindex
);
2200 /* Add route_node to work queue and schedule processing */
2201 void rib_queue_add(struct route_node
*rn
)
2205 /* Pointless to queue a route_node with no RIB entries to add or remove
2207 if (!rnode_to_ribs(rn
)) {
2208 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2209 __func__
, (void *)rn
, rn
->lock
);
2210 zlog_backtrace(LOG_DEBUG
);
2214 if (zrouter
.ribq
== NULL
) {
2215 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2216 "%s: work_queue does not exist!", __func__
);
2221 * The RIB queue should normally be either empty or holding the only
2222 * work_queue_item element. In the latter case this element would
2223 * hold a pointer to the meta queue structure, which must be used to
2224 * actually queue the route nodes to process. So create the MQ
2225 * holder, if necessary, then push the work into it in any case.
2226 * This semantics was introduced after 0.99.9 release.
2228 if (work_queue_empty(zrouter
.ribq
))
2229 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2231 rib_meta_queue_add(zrouter
.mq
, rn
);
2236 /* Create new meta queue.
2237 A destructor function doesn't seem to be necessary here.
2239 static struct meta_queue
*meta_queue_new(void)
2241 struct meta_queue
*new;
2244 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2246 for (i
= 0; i
< MQ_SIZE
; i
++) {
2247 new->subq
[i
] = list_new();
2248 assert(new->subq
[i
]);
2254 void meta_queue_free(struct meta_queue
*mq
)
2258 for (i
= 0; i
< MQ_SIZE
; i
++)
2259 list_delete(&mq
->subq
[i
]);
2261 XFREE(MTYPE_WORK_QUEUE
, mq
);
2264 /* initialise zebra rib work queue */
2265 static void rib_queue_init(void)
2267 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2268 "route_node processing"))) {
2269 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2270 "%s: could not initialise work queue!", __func__
);
2274 /* fill in the work queue spec */
2275 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2276 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2277 zrouter
.ribq
->spec
.completion_func
= NULL
;
2278 /* XXX: TODO: These should be runtime configurable via vty */
2279 zrouter
.ribq
->spec
.max_retries
= 3;
2280 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2281 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2283 if (!(zrouter
.mq
= meta_queue_new())) {
2284 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2285 "%s: could not initialise meta queue!", __func__
);
2291 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2295 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2296 rnh_list_init(&dest
->nht
);
2297 route_lock_node(rn
); /* rn route table reference */
2304 /* RIB updates are processed via a queue of pointers to route_nodes.
2306 * The queue length is bounded by the maximal size of the routing table,
2307 * as a route_node will not be requeued, if already queued.
2309 * REs are submitted via rib_addnode or rib_delnode which set minimal
2310 * state, or static_install_route (when an existing RE is updated)
2311 * and then submit route_node to queue for best-path selection later.
2312 * Order of add/delete state changes are preserved for any given RE.
2314 * Deleted REs are reaped during best-path selection.
2317 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2318 * |-------->| | best RE, if required
2320 * static_install->|->rib_addqueue...... -> rib_process
2322 * |-------->| |-> rib_unlink
2323 * |-> set ROUTE_ENTRY_REMOVE |
2324 * rib_delnode (RE freed)
2326 * The 'info' pointer of a route_node points to a rib_dest_t
2327 * ('dest'). Queueing state for a route_node is kept on the dest. The
2328 * dest is created on-demand by rib_link() and is kept around at least
2329 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2331 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2333 * - route_nodes: refcounted by:
2334 * - dest attached to route_node:
2335 * - managed by: rib_link/rib_gc_dest
2336 * - route_node processing queue
2337 * - managed by: rib_addqueue, rib_process.
2341 /* Add RE to head of the route node. */
2342 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2346 const char *rmap_name
;
2350 dest
= rib_dest_from_rnode(rn
);
2352 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2353 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2355 dest
= zebra_rib_create_dest(rn
);
2358 re_list_add_head(&dest
->routes
, re
);
2360 afi
= (rn
->p
.family
== AF_INET
)
2362 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2363 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2364 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2366 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2367 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2372 static void rib_addnode(struct route_node
*rn
,
2373 struct route_entry
*re
, int process
)
2375 /* RE node has been un-removed before route-node is processed.
2376 * route_node must hence already be on the queue for processing..
2378 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2379 if (IS_ZEBRA_DEBUG_RIB
)
2380 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2381 (void *)rn
, (void *)re
);
2383 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2386 rib_link(rn
, re
, process
);
2392 * Detach a rib structure from a route_node.
2394 * Note that a call to rib_unlink() should be followed by a call to
2395 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2396 * longer required to be deleted.
2398 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2404 if (IS_ZEBRA_DEBUG_RIB
)
2405 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2408 dest
= rib_dest_from_rnode(rn
);
2410 re_list_del(&dest
->routes
, re
);
2412 if (dest
->selected_fib
== re
)
2413 dest
->selected_fib
= NULL
;
2415 nexthops_free(re
->ng
.nexthop
);
2416 nexthops_free(re
->fib_ng
.nexthop
);
2418 XFREE(MTYPE_RE
, re
);
2421 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2425 if (IS_ZEBRA_DEBUG_RIB
)
2426 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2427 (void *)rn
, (void *)re
);
2428 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2430 afi
= (rn
->p
.family
== AF_INET
)
2432 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2433 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2434 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2436 zebra_del_import_table_entry(zvrf
, rn
, re
);
2437 /* Just clean up if non main table */
2438 if (IS_ZEBRA_DEBUG_RIB
) {
2439 char buf
[SRCDEST2STR_BUFFER
];
2440 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2441 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2442 re
->vrf_id
, buf
, rn
, re
,
2443 zebra_route_string(re
->type
));
2452 /* This function dumps the contents of a given RE entry into
2453 * standard debug log. Calling function name and IP prefix in
2454 * question are passed as 1st and 2nd arguments.
2457 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2458 union prefixconstptr src_pp
,
2459 const struct route_entry
*re
)
2461 const struct prefix
*src_p
= src_pp
.p
;
2462 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2463 char straddr
[PREFIX_STRLEN
];
2464 char srcaddr
[PREFIX_STRLEN
];
2465 char nhname
[PREFIX_STRLEN
];
2466 struct nexthop
*nexthop
;
2468 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2469 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2470 is_srcdst
? " from " : "",
2471 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2474 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2475 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2478 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2479 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2480 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2481 re
->nexthop_num
, re
->nexthop_active_num
);
2483 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2484 struct interface
*ifp
;
2485 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2487 switch (nexthop
->type
) {
2488 case NEXTHOP_TYPE_BLACKHOLE
:
2489 sprintf(nhname
, "Blackhole");
2491 case NEXTHOP_TYPE_IFINDEX
:
2492 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2494 sprintf(nhname
, "%s", ifp
? ifp
->name
: "Unknown");
2496 case NEXTHOP_TYPE_IPV4
:
2498 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2499 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
,
2502 case NEXTHOP_TYPE_IPV6
:
2503 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2504 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
,
2508 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2509 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2510 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2512 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2515 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2518 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2521 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2524 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2527 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2531 zlog_debug("%s: dump complete", straddr
);
2534 /* This is an exported helper to rtm_read() to dump the strange
2535 * RE entry found by rib_lookup_ipv4_route()
2538 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2540 struct route_table
*table
;
2541 struct route_node
*rn
;
2542 struct route_entry
*re
;
2543 char prefix_buf
[INET_ADDRSTRLEN
];
2546 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2548 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2549 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2554 /* Scan the RIB table for exactly matching RE entry. */
2555 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2557 /* No route for this prefix. */
2559 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2560 prefix2str((struct prefix
*)p
, prefix_buf
,
2561 sizeof(prefix_buf
)));
2566 route_unlock_node(rn
);
2569 RNODE_FOREACH_RE (rn
, re
) {
2570 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2572 (void *)rn
, (void *)re
,
2573 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2576 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2579 route_entry_dump(p
, NULL
, re
);
2583 /* Check if requested address assignment will fail due to another
2584 * route being installed by zebra in FIB already. Take necessary
2585 * actions, if needed: remove such a route from FIB and deSELECT
2586 * corresponding RE entry. Then put affected RN into RIBQ head.
2588 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2590 struct route_table
*table
;
2591 struct route_node
*rn
;
2594 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2595 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2596 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2601 /* No matches would be the simplest case. */
2602 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2606 route_unlock_node(rn
);
2608 dest
= rib_dest_from_rnode(rn
);
2609 /* Check all RE entries. In case any changes have to be done, requeue
2610 * the RN into RIBQ head. If the routing message about the new connected
2611 * route (generated by the IP address we are going to assign very soon)
2612 * comes before the RIBQ is processed, the new RE entry will join
2613 * RIBQ record already on head. This is necessary for proper
2615 * of the rest of the RE.
2617 if (dest
->selected_fib
) {
2618 if (IS_ZEBRA_DEBUG_RIB
) {
2619 char buf
[PREFIX_STRLEN
];
2621 zlog_debug("%u:%s: freeing way for connected prefix",
2622 dest
->selected_fib
->vrf_id
,
2623 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2624 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2626 rib_uninstall(rn
, dest
->selected_fib
);
2631 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2632 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2634 struct route_table
*table
;
2635 struct route_node
*rn
;
2636 struct route_entry
*same
= NULL
;
2642 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2645 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2647 XFREE(MTYPE_RE
, re
);
2651 /* Make it sure prefixlen is applied to the prefix. */
2654 apply_mask_ipv6(src_p
);
2656 /* Set default distance by route type. */
2657 if (re
->distance
== 0)
2658 re
->distance
= route_distance(re
->type
);
2660 /* Lookup route node.*/
2661 rn
= srcdest_rnode_get(table
, p
, src_p
);
2664 * If same type of route are installed, treat it as a implicit
2666 * If the user has specified the No route replace semantics
2667 * for the install don't do a route replace.
2669 RNODE_FOREACH_RE (rn
, same
) {
2670 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2673 if (same
->type
!= re
->type
)
2675 if (same
->instance
!= re
->instance
)
2677 if (same
->type
== ZEBRA_ROUTE_KERNEL
2678 && same
->metric
!= re
->metric
)
2681 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2682 same
->distance
!= re
->distance
)
2686 * We should allow duplicate connected routes
2687 * because of IPv6 link-local routes and unnumbered
2688 * interfaces on Linux.
2690 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2694 /* If this route is kernel/connected route, notify the dataplane. */
2695 if (RIB_SYSTEM_ROUTE(re
)) {
2696 /* Notify dataplane */
2697 dplane_sys_route_add(rn
, re
);
2700 /* Link new re to node.*/
2701 if (IS_ZEBRA_DEBUG_RIB
) {
2702 rnode_debug(rn
, re
->vrf_id
,
2703 "Inserting route rn %p, re %p (%s) existing %p",
2704 rn
, re
, zebra_route_string(re
->type
), same
);
2706 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2707 route_entry_dump(p
, src_p
, re
);
2710 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2711 rib_addnode(rn
, re
, 1);
2714 /* Free implicit route.*/
2716 rib_delnode(rn
, same
);
2720 route_unlock_node(rn
);
2724 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2725 unsigned short instance
, int flags
, struct prefix
*p
,
2726 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2727 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2730 struct route_table
*table
;
2731 struct route_node
*rn
;
2732 struct route_entry
*re
;
2733 struct route_entry
*fib
= NULL
;
2734 struct route_entry
*same
= NULL
;
2735 struct nexthop
*rtnh
;
2736 char buf2
[INET6_ADDRSTRLEN
];
2739 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2742 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2749 apply_mask_ipv6(src_p
);
2751 /* Lookup route node. */
2752 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2754 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2756 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2757 if (src_p
&& src_p
->prefixlen
)
2758 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2762 if (IS_ZEBRA_DEBUG_RIB
) {
2763 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2765 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
2766 vrf
->name
, table_id
, dst_buf
,
2767 (src_buf
[0] != '\0') ? " from " : "",
2773 dest
= rib_dest_from_rnode(rn
);
2774 fib
= dest
->selected_fib
;
2776 /* Lookup same type route. */
2777 RNODE_FOREACH_RE (rn
, re
) {
2778 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2781 if (re
->type
!= type
)
2783 if (re
->instance
!= instance
)
2785 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2786 distance
!= re
->distance
)
2789 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2791 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2792 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2793 if (rtnh
->ifindex
!= nh
->ifindex
)
2798 /* Make sure that the route found has the same gateway. */
2804 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2806 * No guarantee all kernel send nh with labels
2809 if (nexthop_same_no_labels(rtnh
, nh
)) {
2817 /* If same type of route can't be found and this message is from
2821 * In the past(HA!) we could get here because
2822 * we were receiving a route delete from the
2823 * kernel and we're not marking the proto
2824 * as coming from it's appropriate originator.
2825 * Now that we are properly noticing the fact
2826 * that the kernel has deleted our route we
2827 * are not going to get called in this path
2828 * I am going to leave this here because
2829 * this might still work this way on non-linux
2830 * platforms as well as some weird state I have
2831 * not properly thought of yet.
2832 * If we can show that this code path is
2833 * dead then we can remove it.
2835 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2836 if (IS_ZEBRA_DEBUG_RIB
) {
2837 rnode_debug(rn
, vrf_id
,
2838 "rn %p, re %p (%s) was deleted from kernel, adding",
2840 zebra_route_string(fib
->type
));
2843 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2845 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2847 UNSET_FLAG(rtnh
->flags
,
2851 * This is a non FRR route
2852 * as such we should mark
2855 dest
->selected_fib
= NULL
;
2857 /* This means someone else, other than Zebra,
2859 * a Zebra router from the kernel. We will add
2861 rib_install_kernel(rn
, fib
, NULL
);
2864 if (IS_ZEBRA_DEBUG_RIB
) {
2868 "via %s ifindex %d type %d "
2869 "doesn't exist in rib",
2870 inet_ntop(afi2family(afi
),
2877 "type %d doesn't exist in rib",
2880 route_unlock_node(rn
);
2886 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2888 rib_install_kernel(rn
, same
, NULL
);
2889 route_unlock_node(rn
);
2894 /* Special handling for IPv4 or IPv6 routes sourced from
2895 * EVPN - the nexthop (and associated MAC) need to be
2896 * uninstalled if no more refs.
2898 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2899 struct nexthop
*tmp_nh
;
2901 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2902 struct ipaddr vtep_ip
;
2904 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2905 if (afi
== AFI_IP
) {
2906 vtep_ip
.ipa_type
= IPADDR_V4
;
2907 memcpy(&(vtep_ip
.ipaddr_v4
),
2908 &(tmp_nh
->gate
.ipv4
),
2909 sizeof(struct in_addr
));
2911 vtep_ip
.ipa_type
= IPADDR_V6
;
2912 memcpy(&(vtep_ip
.ipaddr_v6
),
2913 &(tmp_nh
->gate
.ipv6
),
2914 sizeof(struct in6_addr
));
2916 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2921 /* Notify dplane if system route changes */
2922 if (RIB_SYSTEM_ROUTE(re
))
2923 dplane_sys_route_del(rn
, same
);
2925 rib_delnode(rn
, same
);
2928 route_unlock_node(rn
);
2933 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2934 unsigned short instance
, int flags
, struct prefix
*p
,
2935 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2936 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2939 struct route_entry
*re
;
2940 struct nexthop
*nexthop
;
2942 /* Allocate new route_entry structure. */
2943 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2945 re
->instance
= instance
;
2946 re
->distance
= distance
;
2948 re
->metric
= metric
;
2950 re
->table
= table_id
;
2951 re
->vrf_id
= vrf_id
;
2952 re
->nexthop_num
= 0;
2953 re
->uptime
= monotime(NULL
);
2957 nexthop
= nexthop_new();
2959 route_entry_nexthop_add(re
, nexthop
);
2961 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2964 /* Schedule routes of a particular table (address-family) based on event. */
2965 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2967 struct route_node
*rn
;
2968 struct route_entry
*re
, *next
;
2970 /* Walk all routes and queue for processing, if appropriate for
2971 * the trigger event.
2973 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2975 * If we are looking at a route node and the node
2976 * has already been queued we don't
2977 * need to queue it up again
2979 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2980 RIB_ROUTE_ANY_QUEUED
))
2983 case RIB_UPDATE_RMAP_CHANGE
:
2984 case RIB_UPDATE_OTHER
:
2985 /* Right now, examine all routes. Can restrict to a
2987 * some cases (TODO).
2989 if (rnode_to_ribs(rn
)) {
2990 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
2991 SET_FLAG(re
->status
,
2992 ROUTE_ENTRY_CHANGED
);
3003 /* RIB update function. */
3004 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3006 struct route_table
*table
;
3008 /* Process routes of interested address-families. */
3009 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3011 if (IS_ZEBRA_DEBUG_EVENT
)
3012 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3013 rib_update_table(table
, event
);
3016 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3018 if (IS_ZEBRA_DEBUG_EVENT
)
3019 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3020 rib_update_table(table
, event
);
3024 /* Delete self installed routes after zebra is relaunched. */
3025 void rib_sweep_table(struct route_table
*table
)
3027 struct route_node
*rn
;
3028 struct route_entry
*re
;
3029 struct route_entry
*next
;
3030 struct nexthop
*nexthop
;
3035 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3036 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3038 if (IS_ZEBRA_DEBUG_RIB
)
3039 route_entry_dump(&rn
->p
, NULL
, re
);
3041 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3044 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3048 * If routes are older than startup_time then
3049 * we know we read them in from the kernel.
3050 * As such we can safely remove them.
3052 if (zrouter
.startup_time
< re
->uptime
)
3056 * So we are starting up and have received
3057 * routes from the kernel that we have installed
3058 * from a previous run of zebra but not cleaned
3059 * up ( say a kill -9 )
3060 * But since we haven't actually installed
3061 * them yet( we received them from the kernel )
3062 * we don't think they are active.
3063 * So let's pretend they are active to actually
3065 * In all honesty I'm not sure if we should
3066 * mark them as active when we receive them
3067 * This is startup only so probably ok.
3069 * If we ever decide to move rib_sweep_table
3070 * to a different spot (ie startup )
3071 * this decision needs to be revisited
3073 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3074 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3075 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3077 rib_uninstall_kernel(rn
, re
);
3078 rib_delnode(rn
, re
);
3083 /* Sweep all RIB tables. */
3084 int rib_sweep_route(struct thread
*t
)
3087 struct zebra_vrf
*zvrf
;
3089 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3090 if ((zvrf
= vrf
->info
) == NULL
)
3093 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3094 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3097 zebra_router_sweep_route();
3102 /* Remove specific by protocol routes from 'table'. */
3103 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3104 struct route_table
*table
)
3106 struct route_node
*rn
;
3107 struct route_entry
*re
;
3108 struct route_entry
*next
;
3109 unsigned long n
= 0;
3112 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3113 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3114 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3116 if (re
->type
== proto
3117 && re
->instance
== instance
) {
3118 rib_delnode(rn
, re
);
3125 /* Remove specific by protocol routes. */
3126 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3129 struct zebra_vrf
*zvrf
;
3130 struct other_route_table
*ort
;
3131 unsigned long cnt
= 0;
3133 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3138 cnt
+= rib_score_proto_table(proto
, instance
,
3139 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3140 + rib_score_proto_table(
3142 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3144 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3145 rib_score_proto_table(proto
, instance
, ort
->table
);
3151 /* Close RIB and clean up kernel routes. */
3152 void rib_close_table(struct route_table
*table
)
3154 struct route_node
*rn
;
3155 rib_table_info_t
*info
;
3161 info
= route_table_get_info(table
);
3163 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3164 dest
= rib_dest_from_rnode(rn
);
3166 if (dest
&& dest
->selected_fib
) {
3167 if (info
->safi
== SAFI_UNICAST
)
3168 hook_call(rib_update
, rn
, NULL
);
3170 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3171 dest
->selected_fib
= NULL
;
3177 * Handler for async dataplane results after a pseudowire installation
3179 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3181 struct zebra_pw
*pw
;
3182 struct zebra_vrf
*vrf
;
3184 /* The pseudowire code assumes success - we act on an error
3185 * result for installation attempts here.
3187 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3190 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3191 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3192 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3194 zebra_pw_install_failure(pw
);
3204 * Handle results from the dataplane system. Dequeue update context
3205 * structs, dispatch to appropriate internal handlers.
3207 static int rib_process_dplane_results(struct thread
*thread
)
3209 struct zebra_dplane_ctx
*ctx
;
3210 struct dplane_ctx_q ctxlist
;
3211 bool shut_p
= false;
3213 /* Dequeue a list of completed updates with one lock/unlock cycle */
3216 TAILQ_INIT(&ctxlist
);
3218 /* Take lock controlling queue of results */
3219 frr_with_mutex(&dplane_mutex
) {
3220 /* Dequeue list of context structs */
3221 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3224 /* Dequeue context block */
3225 ctx
= dplane_ctx_dequeue(&ctxlist
);
3227 /* If we've emptied the results queue, we're done */
3231 /* If zebra is shutting down, avoid processing results,
3232 * just drain the results queue.
3234 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3235 memory_order_relaxed
);
3238 dplane_ctx_fini(&ctx
);
3240 ctx
= dplane_ctx_dequeue(&ctxlist
);
3247 switch (dplane_ctx_get_op(ctx
)) {
3248 case DPLANE_OP_ROUTE_INSTALL
:
3249 case DPLANE_OP_ROUTE_UPDATE
:
3250 case DPLANE_OP_ROUTE_DELETE
:
3252 /* Bit of special case for route updates
3253 * that were generated by async notifications:
3254 * we don't want to continue processing these
3257 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3258 rib_process_result(ctx
);
3260 dplane_ctx_fini(&ctx
);
3264 case DPLANE_OP_ROUTE_NOTIFY
:
3265 rib_process_dplane_notify(ctx
);
3268 case DPLANE_OP_LSP_INSTALL
:
3269 case DPLANE_OP_LSP_UPDATE
:
3270 case DPLANE_OP_LSP_DELETE
:
3272 /* Bit of special case for LSP updates
3273 * that were generated by async notifications:
3274 * we don't want to continue processing these.
3276 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3277 zebra_mpls_lsp_dplane_result(ctx
);
3279 dplane_ctx_fini(&ctx
);
3283 case DPLANE_OP_LSP_NOTIFY
:
3284 zebra_mpls_process_dplane_notify(ctx
);
3287 case DPLANE_OP_PW_INSTALL
:
3288 case DPLANE_OP_PW_UNINSTALL
:
3289 handle_pw_result(ctx
);
3292 case DPLANE_OP_SYS_ROUTE_ADD
:
3293 case DPLANE_OP_SYS_ROUTE_DELETE
:
3294 /* No further processing in zebra for these. */
3295 dplane_ctx_fini(&ctx
);
3298 case DPLANE_OP_MAC_INSTALL
:
3299 case DPLANE_OP_MAC_DELETE
:
3300 zebra_vxlan_handle_result(ctx
);
3303 /* Some op codes not handled here */
3304 case DPLANE_OP_ADDR_INSTALL
:
3305 case DPLANE_OP_ADDR_UNINSTALL
:
3306 case DPLANE_OP_NEIGH_INSTALL
:
3307 case DPLANE_OP_NEIGH_UPDATE
:
3308 case DPLANE_OP_NEIGH_DELETE
:
3309 case DPLANE_OP_VTEP_ADD
:
3310 case DPLANE_OP_VTEP_DELETE
:
3311 case DPLANE_OP_NONE
:
3312 /* Don't expect this: just return the struct? */
3313 dplane_ctx_fini(&ctx
);
3316 } /* Dispatch by op code */
3318 ctx
= dplane_ctx_dequeue(&ctxlist
);
3327 * Results are returned from the dataplane subsystem, in the context of
3328 * the dataplane pthread. We enqueue the results here for processing by
3329 * the main thread later.
3331 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3333 /* Take lock controlling queue of results */
3334 frr_with_mutex(&dplane_mutex
) {
3335 /* Enqueue context blocks */
3336 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3339 /* Ensure event is signalled to zebra main pthread */
3340 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3347 * Ensure there are no empty slots in the route_info array.
3348 * Every route type in zebra should be present there.
3350 static void check_route_info(void)
3352 int len
= array_size(route_info
);
3355 * ZEBRA_ROUTE_SYSTEM is special cased since
3356 * its key is 0 anyway.
3358 * ZEBRA_ROUTE_ALL is also ignored.
3360 for (int i
= 0; i
< len
; i
++) {
3361 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3363 assert(route_info
[i
].key
);
3364 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3368 /* Routing information base initialize. */
3375 /* Init dataplane, and register for results */
3376 pthread_mutex_init(&dplane_mutex
, NULL
);
3377 TAILQ_INIT(&rib_dplane_q
);
3378 zebra_dplane_init(rib_dplane_results
);
3384 * Get the first vrf id that is greater than the given vrf id if any.
3386 * Returns true if a vrf id was found, false otherwise.
3388 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3392 vrf
= vrf_lookup_by_id(vrf_id
);
3394 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3396 *next_id_p
= vrf
->vrf_id
;
3405 * rib_tables_iter_next
3407 * Returns the next table in the iteration.
3409 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3411 struct route_table
*table
;
3414 * Array that helps us go over all AFI/SAFI combinations via one
3421 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3422 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3423 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3428 switch (iter
->state
) {
3430 case RIB_TABLES_ITER_S_INIT
:
3431 iter
->vrf_id
= VRF_DEFAULT
;
3432 iter
->afi_safi_ix
= -1;
3436 case RIB_TABLES_ITER_S_ITERATING
:
3437 iter
->afi_safi_ix
++;
3440 while (iter
->afi_safi_ix
3441 < (int)array_size(afi_safis
)) {
3442 table
= zebra_vrf_table(
3443 afi_safis
[iter
->afi_safi_ix
].afi
,
3444 afi_safis
[iter
->afi_safi_ix
].safi
,
3449 iter
->afi_safi_ix
++;
3453 * Found another table in this vrf.
3459 * Done with all tables in the current vrf, go to the
3463 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3466 iter
->afi_safi_ix
= 0;
3471 case RIB_TABLES_ITER_S_DONE
:
3476 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3478 iter
->state
= RIB_TABLES_ITER_S_DONE
;