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"
41 #include "zebra/zebra_router.h"
42 #include "zebra/connected.h"
43 #include "zebra/debug.h"
44 #include "zebra/interface.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/rib.h"
48 #include "zebra/zapi_msg.h"
49 #include "zebra/zebra_errors.h"
50 #include "zebra/zebra_memory.h"
51 #include "zebra/zebra_ns.h"
52 #include "zebra/zebra_rnh.h"
53 #include "zebra/zebra_routemap.h"
54 #include "zebra/zebra_vrf.h"
55 #include "zebra/zebra_vxlan.h"
56 #include "zebra/zapi_msg.h"
57 #include "zebra/zebra_dplane.h"
58 #include "zebra/zebra_nhg.h"
61 * Event, list, and mutex for delivery of dataplane results
63 static pthread_mutex_t dplane_mutex
;
64 static struct thread
*t_dplane
;
65 static struct dplane_ctx_q rib_dplane_q
;
67 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
70 /* Should we allow non Quagga processes to delete our routes */
71 extern int allow_delete
;
73 /* Each route type's string and default distance value. */
78 } route_info
[ZEBRA_ROUTE_MAX
] = {
79 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
80 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
81 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
82 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
83 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
84 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
85 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
86 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
87 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
88 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
89 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
90 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
91 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
92 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
93 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
94 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
95 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
96 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
97 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
98 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
99 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
100 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
101 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
102 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
103 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 4},
104 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 4},
105 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 2},
106 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 4}
107 /* Any new route type added to zebra, should be mirrored here */
109 /* no entry/default: 150 */
112 static void __attribute__((format(printf
, 5, 6)))
113 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
114 int priority
, const char *msgfmt
, ...)
116 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
120 va_start(ap
, msgfmt
);
121 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
125 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
126 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
128 if (info
->safi
== SAFI_MULTICAST
)
129 strlcat(buf
, " (MRIB)", sizeof(buf
));
131 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
134 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
137 #define rnode_debug(node, vrf_id, ...) \
138 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
139 #define rnode_info(node, ...) \
140 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
142 uint8_t route_distance(int type
)
146 if ((unsigned)type
>= array_size(route_info
))
149 distance
= route_info
[type
].distance
;
154 int is_zebra_valid_kernel_table(uint32_t table_id
)
157 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
158 || (table_id
== RT_TABLE_COMPAT
))
165 int is_zebra_main_routing_table(uint32_t table_id
)
167 if (table_id
== RT_TABLE_MAIN
)
172 int zebra_check_addr(const struct prefix
*p
)
174 if (p
->family
== AF_INET
) {
177 addr
= p
->u
.prefix4
.s_addr
;
180 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
181 || IPV4_LINKLOCAL(addr
))
184 if (p
->family
== AF_INET6
) {
185 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
187 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
193 /* Add nexthop to the end of a rib node's nexthop list */
194 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
196 _nexthop_group_add_sorted(&re
->ng
, nexthop
);
202 * copy_nexthop - copy a nexthop to the rib structure.
204 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
206 assert(!re
->ng
.nexthop
);
207 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
208 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
212 /* Delete specified nexthop from the list. */
213 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
216 nexthop
->next
->prev
= nexthop
->prev
;
218 nexthop
->prev
->next
= nexthop
->next
;
220 re
->ng
.nexthop
= nexthop
->next
;
225 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
229 struct nexthop
*nexthop
;
231 nexthop
= nexthop_new();
232 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
233 nexthop
->ifindex
= ifindex
;
234 nexthop
->vrf_id
= nh_vrf_id
;
236 route_entry_nexthop_add(re
, nexthop
);
241 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
242 struct in_addr
*ipv4
,
246 struct nexthop
*nexthop
;
248 nexthop
= nexthop_new();
249 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
250 nexthop
->vrf_id
= nh_vrf_id
;
251 nexthop
->gate
.ipv4
= *ipv4
;
253 nexthop
->src
.ipv4
= *src
;
255 route_entry_nexthop_add(re
, nexthop
);
260 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
261 struct in_addr
*ipv4
,
266 struct nexthop
*nexthop
;
267 struct interface
*ifp
;
269 nexthop
= nexthop_new();
270 nexthop
->vrf_id
= nh_vrf_id
;
271 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
272 nexthop
->gate
.ipv4
= *ipv4
;
274 nexthop
->src
.ipv4
= *src
;
275 nexthop
->ifindex
= ifindex
;
276 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
277 /*Pending: need to think if null ifp here is ok during bootup?
278 There was a crash because ifp here was coming to be NULL */
280 if (connected_is_unnumbered(ifp
))
281 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
283 route_entry_nexthop_add(re
, nexthop
);
288 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
289 struct in6_addr
*ipv6
,
292 struct nexthop
*nexthop
;
294 nexthop
= nexthop_new();
295 nexthop
->vrf_id
= nh_vrf_id
;
296 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
297 nexthop
->gate
.ipv6
= *ipv6
;
299 route_entry_nexthop_add(re
, nexthop
);
304 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
305 struct in6_addr
*ipv6
,
309 struct nexthop
*nexthop
;
311 nexthop
= nexthop_new();
312 nexthop
->vrf_id
= nh_vrf_id
;
313 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
314 nexthop
->gate
.ipv6
= *ipv6
;
315 nexthop
->ifindex
= ifindex
;
317 route_entry_nexthop_add(re
, nexthop
);
322 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
323 enum blackhole_type bh_type
)
325 struct nexthop
*nexthop
;
327 nexthop
= nexthop_new();
328 nexthop
->vrf_id
= VRF_DEFAULT
;
329 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
330 nexthop
->bh_type
= bh_type
;
332 route_entry_nexthop_add(re
, nexthop
);
337 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
338 union g_addr
*addr
, struct route_node
**rn_out
)
341 struct route_table
*table
;
342 struct route_node
*rn
;
343 struct route_entry
*match
= NULL
;
346 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
350 memset(&p
, 0, sizeof(struct prefix
));
353 p
.u
.prefix4
= addr
->ipv4
;
354 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
356 p
.u
.prefix6
= addr
->ipv6
;
357 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
360 rn
= route_node_match(table
, (struct prefix
*)&p
);
365 route_unlock_node(rn
);
367 dest
= rib_dest_from_rnode(rn
);
368 if (dest
&& dest
->selected_fib
369 && !CHECK_FLAG(dest
->selected_fib
->status
,
370 ROUTE_ENTRY_REMOVED
))
371 match
= dest
->selected_fib
;
373 /* If there is no selected route or matched route is EGP, go up
378 } while (rn
&& rn
->info
== NULL
);
382 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
383 if (!CHECK_FLAG(match
->status
,
384 ROUTE_ENTRY_INSTALLED
))
396 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
398 struct route_node
**rn_out
)
400 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
401 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
402 union g_addr gaddr
= {.ipv4
= addr
};
404 switch (zrouter
.ipv4_multicast_mode
) {
405 case MCAST_MRIB_ONLY
:
406 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
408 case MCAST_URIB_ONLY
:
409 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
410 case MCAST_NO_CONFIG
:
411 case MCAST_MIX_MRIB_FIRST
:
412 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
415 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
418 case MCAST_MIX_DISTANCE
:
419 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
420 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
422 re
= ure
->distance
< mre
->distance
? ure
: mre
;
428 case MCAST_MIX_PFXLEN
:
429 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
430 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
432 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
441 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
443 if (IS_ZEBRA_DEBUG_RIB
) {
445 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
447 zlog_debug("%s: %s: vrf: %u found %s, using %s",
448 __func__
, buf
, vrf_id
,
449 mre
? (ure
? "MRIB+URIB" : "MRIB")
450 : ure
? "URIB" : "nothing",
451 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
456 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
458 struct route_table
*table
;
459 struct route_node
*rn
;
460 struct route_entry
*match
= NULL
;
464 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
468 rn
= route_node_lookup(table
, (struct prefix
*)p
);
470 /* No route for this prefix. */
475 route_unlock_node(rn
);
476 dest
= rib_dest_from_rnode(rn
);
478 if (dest
&& dest
->selected_fib
479 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
480 match
= dest
->selected_fib
;
485 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
488 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
495 * Is this RIB labeled-unicast? It must be of type BGP and all paths
496 * (nexthops) must have a label.
498 int zebra_rib_labeled_unicast(struct route_entry
*re
)
500 struct nexthop
*nexthop
= NULL
;
502 if (re
->type
!= ZEBRA_ROUTE_BGP
)
505 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
506 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
512 /* Update flag indicates whether this is a "replace" or not. Currently, this
513 * is only used for IPv4.
515 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
516 struct route_entry
*old
)
518 struct nexthop
*nexthop
;
519 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
520 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
521 const struct prefix
*p
, *src_p
;
522 enum zebra_dplane_result ret
;
524 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
526 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
528 if (info
->safi
!= SAFI_UNICAST
) {
529 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
530 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
533 struct nexthop
*prev
;
535 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
536 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
537 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
540 if (nexthop_same_firsthop(nexthop
, prev
)) {
541 SET_FLAG(nexthop
->flags
,
542 NEXTHOP_FLAG_DUPLICATE
);
550 * If this is a replace to a new RE let the originator of the RE
551 * know that they've lost
553 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
554 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
556 /* Update fib selection */
557 dest
->selected_fib
= re
;
560 * Make sure we update the FPM any time we send new information to
563 hook_call(rib_update
, rn
, "installing in kernel");
565 /* Send add or update */
567 ret
= dplane_route_update(rn
, re
, old
);
569 ret
= dplane_route_add(rn
, re
);
572 case ZEBRA_DPLANE_REQUEST_QUEUED
:
573 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
576 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
578 /* Free old FIB nexthop group */
579 if (old
->fib_ng
.nexthop
) {
580 nexthops_free(old
->fib_ng
.nexthop
);
581 old
->fib_ng
.nexthop
= NULL
;
584 if (!RIB_SYSTEM_ROUTE(old
)) {
585 /* Clear old route's FIB flags */
586 for (ALL_NEXTHOPS(old
->ng
, nexthop
)) {
587 UNSET_FLAG(nexthop
->flags
,
594 zvrf
->installs_queued
++;
596 case ZEBRA_DPLANE_REQUEST_FAILURE
:
598 char str
[SRCDEST2STR_BUFFER
];
600 srcdest_rnode2str(rn
, str
, sizeof(str
));
601 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
602 "%u:%s: Failed to enqueue dataplane install",
606 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
615 /* Uninstall the route from kernel. */
616 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
618 struct nexthop
*nexthop
;
619 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
620 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
622 if (info
->safi
!= SAFI_UNICAST
) {
623 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
624 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
625 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
630 * Make sure we update the FPM any time we send new information to
633 hook_call(rib_update
, rn
, "uninstalling from kernel");
635 switch (dplane_route_delete(rn
, re
)) {
636 case ZEBRA_DPLANE_REQUEST_QUEUED
:
638 zvrf
->removals_queued
++;
640 case ZEBRA_DPLANE_REQUEST_FAILURE
:
642 char str
[SRCDEST2STR_BUFFER
];
644 srcdest_rnode2str(rn
, str
, sizeof(str
));
645 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
646 "%u:%s: Failed to enqueue dataplane uninstall",
650 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
659 /* Uninstall the route from kernel. */
660 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
662 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
663 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
664 struct nexthop
*nexthop
;
666 if (dest
&& dest
->selected_fib
== re
) {
667 if (info
->safi
== SAFI_UNICAST
)
668 hook_call(rib_update
, rn
, "rib_uninstall");
670 /* If labeled-unicast route, uninstall transit LSP. */
671 if (zebra_rib_labeled_unicast(re
))
672 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
674 rib_uninstall_kernel(rn
, re
);
676 dest
->selected_fib
= NULL
;
678 /* Free FIB nexthop group, if present */
679 if (re
->fib_ng
.nexthop
) {
680 nexthops_free(re
->fib_ng
.nexthop
);
681 re
->fib_ng
.nexthop
= NULL
;
684 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
685 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
688 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
689 const struct prefix
*p
, *src_p
;
691 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
693 redistribute_delete(p
, src_p
, re
);
694 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
699 * rib_can_delete_dest
701 * Returns true if the given dest can be deleted from the table.
703 static int rib_can_delete_dest(rib_dest_t
*dest
)
705 if (re_list_first(&dest
->routes
)) {
710 * Unresolved rnh's are stored on the default route's list
712 * dest->rnode can also be the source prefix node in an
713 * ipv6 sourcedest table. Fortunately the prefix of a
714 * source prefix node can never be the default prefix.
716 if (is_default_prefix(&dest
->rnode
->p
))
720 * Don't delete the dest if we have to update the FPM about this
723 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
724 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
730 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
732 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
736 * We are storing the rnh's associated withb
737 * the tracked nexthop as a list of the rn's.
738 * Unresolved rnh's are placed at the top
739 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
740 * As such for each rn we need to walk up the tree
741 * and see if any rnh's need to see if they
742 * would match a more specific route
745 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
746 char buf
[PREFIX_STRLEN
];
748 zlog_debug("%s: %s Being examined for Nexthop Tracking Count: %zd",
750 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
751 dest
? rnh_list_count(&dest
->nht
) : 0);
756 dest
= rib_dest_from_rnode(rn
);
760 * If we have any rnh's stored in the nht list
761 * then we know that this route node was used for
762 * nht resolution and as such we need to call the
763 * nexthop tracking evaluation code
765 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
766 struct zebra_vrf
*zvrf
=
767 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
768 struct prefix
*p
= &rnh
->node
->p
;
770 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
771 char buf1
[PREFIX_STRLEN
];
772 char buf2
[PREFIX_STRLEN
];
774 zlog_debug("%u:%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u",
776 srcdest_rnode2str(rn
, buf1
,
778 prefix2str(p
, buf2
, sizeof(buf2
)),
779 rnh_type2str(rnh
->type
),
784 * If we have evaluated this node on this pass
785 * already, due to following the tree up
786 * then we know that we can move onto the next
789 * Additionally we call zebra_evaluate_rnh
790 * when we gc the dest. In this case we know
791 * that there must be no other re's where
792 * we were originally as such we know that
793 * that sequence number is ok to respect.
795 if (rnh
->seqno
== seq
) {
796 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
798 "\tNode processed and moved already");
803 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
809 dest
= rib_dest_from_rnode(rn
);
816 * Garbage collect the rib dest corresponding to the given route node
819 * Returns true if the dest was deleted, false otherwise.
821 int rib_gc_dest(struct route_node
*rn
)
825 dest
= rib_dest_from_rnode(rn
);
829 if (!rib_can_delete_dest(dest
))
832 if (IS_ZEBRA_DEBUG_RIB
) {
833 struct zebra_vrf
*zvrf
;
835 zvrf
= rib_dest_vrf(dest
);
836 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
839 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
842 rnh_list_fini(&dest
->nht
);
843 XFREE(MTYPE_RIB_DEST
, dest
);
847 * Release the one reference that we keep on the route node.
849 route_unlock_node(rn
);
853 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
854 struct route_entry
*new)
856 hook_call(rib_update
, rn
, "new route selected");
858 /* Update real nexthop. This may actually determine if nexthop is active
860 if (!nexthop_group_active_nexthop_num(&new->ng
)) {
861 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
865 if (IS_ZEBRA_DEBUG_RIB
) {
866 char buf
[SRCDEST2STR_BUFFER
];
867 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
868 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
869 zvrf_id(zvrf
), buf
, rn
, new,
870 zebra_route_string(new->type
));
873 /* If labeled-unicast route, install transit LSP. */
874 if (zebra_rib_labeled_unicast(new))
875 zebra_mpls_lsp_install(zvrf
, rn
, new);
877 rib_install_kernel(rn
, new, NULL
);
879 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
882 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
883 struct route_entry
*old
)
885 hook_call(rib_update
, rn
, "removing existing route");
887 /* Uninstall from kernel. */
888 if (IS_ZEBRA_DEBUG_RIB
) {
889 char buf
[SRCDEST2STR_BUFFER
];
890 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
891 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
892 zvrf_id(zvrf
), buf
, rn
, old
,
893 zebra_route_string(old
->type
));
896 /* If labeled-unicast route, uninstall transit LSP. */
897 if (zebra_rib_labeled_unicast(old
))
898 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
900 rib_uninstall_kernel(rn
, old
);
902 /* Update nexthop for route, reset changed flag. */
903 /* Note: this code also handles the Linux case when an interface goes
904 * down, causing the kernel to delete routes without sending DELROUTE
907 if (RIB_KERNEL_ROUTE(old
))
908 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
910 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
913 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
914 struct route_node
*rn
,
915 struct route_entry
*old
,
916 struct route_entry
*new)
921 * We have to install or update if a new route has been selected or
922 * something has changed.
924 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
925 hook_call(rib_update
, rn
, "updating existing route");
927 /* Update the nexthop; we could determine here that nexthop is
929 if (nexthop_group_active_nexthop_num(&new->ng
))
932 /* If nexthop is active, install the selected route, if
934 * the install succeeds, cleanup flags for prior route, if
939 if (IS_ZEBRA_DEBUG_RIB
) {
940 char buf
[SRCDEST2STR_BUFFER
];
941 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
944 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
945 zvrf_id(zvrf
), buf
, rn
, new,
946 zebra_route_string(new->type
),
948 zebra_route_string(old
->type
));
951 "%u:%s: Updating route rn %p, re %p (%s)",
952 zvrf_id(zvrf
), buf
, rn
, new,
953 zebra_route_string(new->type
));
956 /* If labeled-unicast route, uninstall transit LSP. */
957 if (zebra_rib_labeled_unicast(old
))
958 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
961 * Non-system route should be installed.
962 * If labeled-unicast route, install transit
965 if (zebra_rib_labeled_unicast(new))
966 zebra_mpls_lsp_install(zvrf
, rn
, new);
968 rib_install_kernel(rn
, new, old
);
972 * If nexthop for selected route is not active or install
974 * may need to uninstall and delete for redistribution.
977 if (IS_ZEBRA_DEBUG_RIB
) {
978 char buf
[SRCDEST2STR_BUFFER
];
979 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
982 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
983 zvrf_id(zvrf
), buf
, rn
, new,
984 zebra_route_string(new->type
),
986 zebra_route_string(old
->type
));
989 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
990 zvrf_id(zvrf
), buf
, rn
, new,
991 zebra_route_string(new->type
));
994 /* If labeled-unicast route, uninstall transit LSP. */
995 if (zebra_rib_labeled_unicast(old
))
996 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
998 rib_uninstall_kernel(rn
, old
);
1002 * Same route selected; check if in the FIB and if not,
1003 * re-install. This is housekeeping code to deal with
1004 * race conditions in kernel with linux netlink reporting
1005 * interface up before IPv4 or IPv6 protocol is ready
1008 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1009 RIB_SYSTEM_ROUTE(new))
1010 rib_install_kernel(rn
, new, NULL
);
1013 /* Update prior route. */
1015 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1017 /* Clear changed flag. */
1018 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1021 /* Check if 'alternate' RIB entry is better than 'current'. */
1022 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1023 struct route_entry
*alternate
)
1025 if (current
== NULL
)
1028 /* filter route selection in following order:
1029 * - connected beats other types
1030 * - if both connected, loopback or vrf wins
1031 * - lower distance beats higher
1032 * - lower metric beats higher for equal distance
1033 * - last, hence oldest, route wins tie break.
1036 /* Connected routes. Check to see if either are a vrf
1037 * or loopback interface. If not, pick the last connected
1038 * route of the set of lowest metric connected routes.
1040 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1041 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1044 /* both are connected. are either loop or vrf? */
1045 struct nexthop
*nexthop
= NULL
;
1047 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1048 if (if_is_loopback_or_vrf(if_lookup_by_index(
1049 nexthop
->ifindex
, alternate
->vrf_id
)))
1053 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1054 if (if_is_loopback_or_vrf(if_lookup_by_index(
1055 nexthop
->ifindex
, current
->vrf_id
)))
1059 /* Neither are loop or vrf so pick best metric */
1060 if (alternate
->metric
<= current
->metric
)
1066 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1069 /* higher distance loses */
1070 if (alternate
->distance
< current
->distance
)
1072 if (current
->distance
< alternate
->distance
)
1075 /* metric tie-breaks equal distance */
1076 if (alternate
->metric
<= current
->metric
)
1082 /* Core function for processing routing information base. */
1083 static void rib_process(struct route_node
*rn
)
1085 struct route_entry
*re
;
1086 struct route_entry
*next
;
1087 struct route_entry
*old_selected
= NULL
;
1088 struct route_entry
*new_selected
= NULL
;
1089 struct route_entry
*old_fib
= NULL
;
1090 struct route_entry
*new_fib
= NULL
;
1091 struct route_entry
*best
= NULL
;
1092 char buf
[SRCDEST2STR_BUFFER
];
1094 struct zebra_vrf
*zvrf
= NULL
;
1095 const struct prefix
*p
, *src_p
;
1097 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1098 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1102 dest
= rib_dest_from_rnode(rn
);
1104 zvrf
= rib_dest_vrf(dest
);
1105 vrf_id
= zvrf_id(zvrf
);
1108 if (IS_ZEBRA_DEBUG_RIB
)
1109 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1111 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1112 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1115 * we can have rn's that have a NULL info pointer
1116 * (dest). As such let's not let the deref happen
1117 * additionally we know RNODE_FOREACH_RE_SAFE
1118 * will not iterate so we are ok.
1121 old_fib
= dest
->selected_fib
;
1123 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1124 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1126 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1127 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1128 re
->status
, re
->flags
, re
->distance
,
1131 /* Currently selected re. */
1132 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1133 assert(old_selected
== NULL
);
1137 /* Skip deleted entries from selection */
1138 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1141 /* Skip unreachable nexthop. */
1142 /* This first call to nexthop_active_update is merely to
1143 * determine if there's any change to nexthops associated
1144 * with this RIB entry. Now, rib_process() can be invoked due
1145 * to an external event such as link down or due to
1146 * next-hop-tracking evaluation. In the latter case,
1147 * a decision has already been made that the NHs have changed.
1148 * So, no need to invoke a potentially expensive call again.
1149 * Further, since the change might be in a recursive NH which
1150 * is not caught in the nexthop_active_update() code. Thus, we
1151 * might miss changes to recursive NHs.
1153 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1154 && !nexthop_active_update(rn
, re
)) {
1155 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1156 /* XXX: HERE BE DRAGONS!!!!!
1157 * In all honesty, I have not yet figured out
1158 * what this part does or why the
1159 * ROUTE_ENTRY_CHANGED test above is correct
1160 * or why we need to delete a route here, and
1161 * also not whether this concerns both selected
1162 * and fib route, or only selected
1165 * This entry was denied by the 'ip protocol
1166 * table' route-map, we need to delete it */
1167 if (re
!= old_selected
) {
1168 if (IS_ZEBRA_DEBUG_RIB
)
1170 "%s: %u:%s: imported via import-table but denied "
1171 "by the ip protocol table route-map",
1172 __func__
, vrf_id
, buf
);
1175 SET_FLAG(re
->status
,
1176 ROUTE_ENTRY_REMOVED
);
1182 /* Infinite distance. */
1183 if (re
->distance
== DISTANCE_INFINITY
) {
1184 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1188 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1189 best
= rib_choose_best(new_fib
, re
);
1190 if (new_fib
&& best
!= new_fib
)
1191 UNSET_FLAG(new_fib
->status
,
1192 ROUTE_ENTRY_CHANGED
);
1195 best
= rib_choose_best(new_selected
, re
);
1196 if (new_selected
&& best
!= new_selected
)
1197 UNSET_FLAG(new_selected
->status
,
1198 ROUTE_ENTRY_CHANGED
);
1199 new_selected
= best
;
1202 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1203 } /* RNODE_FOREACH_RE */
1205 /* If no FIB override route, use the selected route also for FIB */
1206 if (new_fib
== NULL
)
1207 new_fib
= new_selected
;
1209 /* After the cycle is finished, the following pointers will be set:
1210 * old_selected --- RE entry currently having SELECTED
1211 * new_selected --- RE entry that is newly SELECTED
1212 * old_fib --- RE entry currently in kernel FIB
1213 * new_fib --- RE entry that is newly to be in kernel FIB
1215 * new_selected will get SELECTED flag, and is going to be redistributed
1216 * the zclients. new_fib (which can be new_selected) will be installed
1220 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1222 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1223 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1224 (void *)old_fib
, (void *)new_fib
);
1227 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1228 * fib == selected */
1229 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1230 ROUTE_ENTRY_CHANGED
);
1232 /* Update fib according to selection results */
1233 if (new_fib
&& old_fib
)
1234 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1236 rib_process_add_fib(zvrf
, rn
, new_fib
);
1238 rib_process_del_fib(zvrf
, rn
, old_fib
);
1240 /* Update SELECTED entry */
1241 if (old_selected
!= new_selected
|| selected_changed
) {
1243 if (new_selected
&& new_selected
!= new_fib
)
1244 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1247 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1251 redistribute_delete(p
, src_p
, old_selected
);
1252 if (old_selected
!= new_selected
)
1253 UNSET_FLAG(old_selected
->flags
,
1254 ZEBRA_FLAG_SELECTED
);
1258 /* Remove all RE entries queued for removal */
1259 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1260 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1261 if (IS_ZEBRA_DEBUG_RIB
) {
1262 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1263 (void *)rn
, (void *)re
);
1270 * Check if the dest can be deleted now.
1275 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1277 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1278 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1283 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1284 if (IS_ZEBRA_DEBUG_MPLS
)
1286 "%u: Scheduling all LSPs upon RIB completion",
1288 zebra_mpls_lsp_schedule(zvrf
);
1289 mpls_unmark_lsps_for_processing(rn
);
1294 * Utility to match route with dplane context data
1296 static bool rib_route_match_ctx(const struct route_entry
*re
,
1297 const struct zebra_dplane_ctx
*ctx
,
1300 bool result
= false;
1304 * In 'update' case, we test info about the 'previous' or
1307 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1308 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1311 /* TODO -- we're using this extra test, but it's not
1312 * exactly clear why.
1314 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1315 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1316 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1323 * Ordinary, single-route case using primary context info
1325 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1326 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1327 /* Skip route that's been deleted */
1331 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1332 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1335 /* TODO -- we're using this extra test, but it's not
1336 * exactly clear why.
1338 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1339 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1340 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1351 static void zebra_rib_fixup_system(struct route_node
*rn
)
1353 struct route_entry
*re
;
1355 RNODE_FOREACH_RE(rn
, re
) {
1356 struct nexthop
*nhop
;
1358 if (!RIB_SYSTEM_ROUTE(re
))
1361 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1364 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1365 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1367 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1368 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1371 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1377 * Update a route from a dplane context. This consolidates common code
1378 * that can be used in processing of results from FIB updates, and in
1379 * async notification processing.
1380 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1382 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1383 struct route_node
*rn
,
1384 struct zebra_dplane_ctx
*ctx
)
1386 char dest_str
[PREFIX_STRLEN
] = "";
1387 char nh_str
[NEXTHOP_STRLEN
];
1388 struct nexthop
*nexthop
, *ctx_nexthop
;
1390 const struct nexthop_group
*ctxnhg
;
1391 bool is_selected
= false; /* Is 're' currently the selected re? */
1392 bool changed_p
= false; /* Change to nexthops? */
1395 /* Note well: only capturing the prefix string if debug is enabled here;
1396 * unconditional log messages will have to generate the string.
1398 if (IS_ZEBRA_DEBUG_RIB
)
1399 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1401 dest
= rib_dest_from_rnode(rn
);
1403 is_selected
= (re
== dest
->selected_fib
);
1405 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1406 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1407 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1409 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1410 * If the installed set differs from the set requested by the rib/owner,
1411 * we use the fib-specific nexthop-group to record the actual FIB
1416 * First check the fib nexthop-group, if it's present. The comparison
1417 * here is quite strict: we require that the fib sets match exactly.
1421 if (re
->fib_ng
.nexthop
== NULL
)
1426 /* First check the route's fib nexthops */
1427 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1429 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1433 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1435 if (nexthop_same(ctx_nexthop
, nexthop
))
1439 if (ctx_nexthop
== NULL
) {
1440 /* Nexthop not in the new installed set */
1441 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1442 nexthop2str(nexthop
, nh_str
,
1444 zlog_debug("update_from_ctx: no match for fib nh %s",
1456 /* Check the new installed set */
1458 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1460 if (CHECK_FLAG(ctx_nexthop
->flags
,
1461 NEXTHOP_FLAG_RECURSIVE
))
1464 /* Compare with the current group's nexthops */
1466 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1467 if (nexthop_same(nexthop
, ctx_nexthop
))
1471 if (nexthop
== NULL
) {
1472 /* Nexthop not in the old installed set */
1473 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1474 nexthop2str(ctx_nexthop
, nh_str
,
1476 zlog_debug("update_from_ctx: no fib match for notif nh %s",
1486 /* If the new FIB set matches the existing FIB set, we're done. */
1488 if (IS_ZEBRA_DEBUG_RIB
)
1489 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1490 re
->vrf_id
, dest_str
);
1493 } else if (re
->fib_ng
.nexthop
) {
1495 * Free stale fib list and move on to check the rib nhg.
1497 if (IS_ZEBRA_DEBUG_RIB
)
1498 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1499 re
->vrf_id
, dest_str
);
1500 nexthops_free(re
->fib_ng
.nexthop
);
1501 re
->fib_ng
.nexthop
= NULL
;
1503 /* Note that the installed nexthops have changed */
1506 if (IS_ZEBRA_DEBUG_RIB
)
1507 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1508 re
->vrf_id
, dest_str
);
1512 * Compare with the rib nexthop group. The comparison here is different:
1513 * the RIB group may be a superset of the list installed in the FIB. We
1514 * walk the RIB group, looking for the 'installable' candidate
1515 * nexthops, and then check those against the set
1516 * that is actually installed.
1519 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1521 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1524 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1527 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1529 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1530 if (nexthop_same(ctx_nexthop
, nexthop
))
1534 /* If the FIB doesn't know about the nexthop,
1535 * it's not installed
1537 if (ctx_nexthop
== NULL
) {
1538 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1539 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1540 zlog_debug("update_from_ctx: no notif match for rib nh %s",
1545 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1548 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1552 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1553 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1556 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1558 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1561 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1565 /* If all nexthops were processed, we're done */
1567 if (IS_ZEBRA_DEBUG_RIB
)
1568 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1569 re
->vrf_id
, dest_str
,
1570 (changed_p
? "true" : "false"));
1574 /* FIB nexthop set differs from the RIB set:
1575 * create a fib-specific nexthop-group
1577 if (IS_ZEBRA_DEBUG_RIB
)
1578 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1579 re
->vrf_id
, dest_str
,
1580 (changed_p
? "true" : "false"));
1582 ctxnhg
= dplane_ctx_get_ng(ctx
);
1584 if (ctxnhg
->nexthop
)
1585 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1587 /* Bit of a special case when the fib has _no_ installed
1590 nexthop
= nexthop_new();
1591 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1592 _nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1600 * Helper to locate a zebra route-node from a dplane context. This is used
1601 * when processing dplane results, e.g. Note well: the route-node is returned
1602 * with a ref held - route_unlock_node() must be called eventually.
1604 static struct route_node
*
1605 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1607 struct route_table
*table
= NULL
;
1608 struct route_node
*rn
= NULL
;
1609 const struct prefix
*dest_pfx
, *src_pfx
;
1611 /* Locate rn and re(s) from ctx */
1613 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1614 dplane_ctx_get_safi(ctx
),
1615 dplane_ctx_get_vrf(ctx
),
1616 dplane_ctx_get_table(ctx
));
1617 if (table
== NULL
) {
1618 if (IS_ZEBRA_DEBUG_DPLANE
) {
1619 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1620 dplane_ctx_get_afi(ctx
),
1621 dplane_ctx_get_safi(ctx
),
1622 dplane_ctx_get_vrf(ctx
));
1627 dest_pfx
= dplane_ctx_get_dest(ctx
);
1628 src_pfx
= dplane_ctx_get_src(ctx
);
1630 rn
= srcdest_rnode_get(table
, dest_pfx
,
1631 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1640 * Route-update results processing after async dataplane update.
1642 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1644 struct zebra_vrf
*zvrf
= NULL
;
1645 struct route_node
*rn
= NULL
;
1646 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1647 bool is_update
= false;
1648 char dest_str
[PREFIX_STRLEN
] = "";
1649 enum dplane_op_e op
;
1650 enum zebra_dplane_result status
;
1651 const struct prefix
*dest_pfx
, *src_pfx
;
1653 bool fib_changed
= false;
1655 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1656 dest_pfx
= dplane_ctx_get_dest(ctx
);
1658 /* Note well: only capturing the prefix string if debug is enabled here;
1659 * unconditional log messages will have to generate the string.
1661 if (IS_ZEBRA_DEBUG_DPLANE
)
1662 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1664 /* Locate rn and re(s) from ctx */
1665 rn
= rib_find_rn_from_ctx(ctx
);
1667 if (IS_ZEBRA_DEBUG_DPLANE
) {
1668 zlog_debug("Failed to process dplane results: no route for %u:%s",
1669 dplane_ctx_get_vrf(ctx
), dest_str
);
1674 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1676 op
= dplane_ctx_get_op(ctx
);
1677 status
= dplane_ctx_get_status(ctx
);
1679 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1680 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1681 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1682 dplane_op2str(op
), dplane_res2str(status
));
1685 * Update is a bit of a special case, where we may have both old and new
1686 * routes to post-process.
1688 is_update
= dplane_ctx_is_update(ctx
);
1691 * Take a pass through the routes, look for matches with the context
1694 RNODE_FOREACH_RE(rn
, rib
) {
1697 if (rib_route_match_ctx(rib
, ctx
, false))
1701 /* Check for old route match */
1702 if (is_update
&& (old_re
== NULL
)) {
1703 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1707 /* Have we found the routes we need to work on? */
1708 if (re
&& ((!is_update
|| old_re
)))
1712 seq
= dplane_ctx_get_seq(ctx
);
1715 * Check sequence number(s) to detect stale results before continuing
1718 if (re
->dplane_sequence
!= seq
) {
1719 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1720 zlog_debug("%u:%s Stale dplane result for re %p",
1721 dplane_ctx_get_vrf(ctx
),
1724 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1728 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1729 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1730 zlog_debug("%u:%s Stale dplane result for old_re %p",
1731 dplane_ctx_get_vrf(ctx
),
1734 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1738 case DPLANE_OP_ROUTE_INSTALL
:
1739 case DPLANE_OP_ROUTE_UPDATE
:
1740 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1742 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1743 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1746 * On an update operation from the same route type
1747 * context retrieval currently has no way to know
1748 * which was the old and which was the new.
1749 * So don't unset our flags that we just set.
1750 * We know redistribution is ok because the
1751 * old_re in this case is used for nothing
1752 * more than knowing whom to contact if necessary.
1754 if (old_re
&& old_re
!= re
) {
1755 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1756 UNSET_FLAG(old_re
->status
,
1757 ROUTE_ENTRY_INSTALLED
);
1760 /* Update zebra route based on the results in
1761 * the context struct.
1765 rib_update_re_from_ctx(re
, rn
, ctx
);
1768 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1769 zlog_debug("%u:%s no fib change for re",
1776 redistribute_update(dest_pfx
, src_pfx
,
1781 * System routes are weird in that they
1782 * allow multiple to be installed that match
1783 * to the same prefix, so after we get the
1784 * result we need to clean them up so that
1785 * we can actually use them.
1787 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1788 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1789 zebra_rib_fixup_system(rn
);
1794 /* Notify route owner */
1795 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1799 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1800 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1802 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1804 zsend_route_notify_owner(re
, dest_pfx
,
1805 ZAPI_ROUTE_FAIL_INSTALL
);
1807 zlog_warn("%u:%s: Route install failed",
1808 dplane_ctx_get_vrf(ctx
),
1809 prefix2str(dest_pfx
,
1810 dest_str
, sizeof(dest_str
)));
1813 case DPLANE_OP_ROUTE_DELETE
:
1815 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1817 * In the delete case, the zebra core datastructs were
1818 * updated (or removed) at the time the delete was issued,
1819 * so we're just notifying the route owner.
1821 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1823 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1824 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1826 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1832 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1833 zsend_route_notify_owner_ctx(ctx
,
1834 ZAPI_ROUTE_REMOVE_FAIL
);
1836 zlog_warn("%u:%s: Route Deletion failure",
1837 dplane_ctx_get_vrf(ctx
),
1838 prefix2str(dest_pfx
,
1839 dest_str
, sizeof(dest_str
)));
1843 * System routes are weird in that they
1844 * allow multiple to be installed that match
1845 * to the same prefix, so after we get the
1846 * result we need to clean them up so that
1847 * we can actually use them.
1849 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1850 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1851 zebra_rib_fixup_system(rn
);
1857 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1858 zebra_rib_evaluate_mpls(rn
);
1862 route_unlock_node(rn
);
1864 /* Return context to dataplane module */
1865 dplane_ctx_fini(&ctx
);
1869 * Handle notification from async dataplane: the dataplane has detected
1870 * some change to a route, and notifies zebra so that the control plane
1871 * can reflect that change.
1873 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1875 struct route_node
*rn
= NULL
;
1876 struct route_entry
*re
= NULL
;
1877 struct nexthop
*nexthop
;
1878 char dest_str
[PREFIX_STRLEN
] = "";
1879 const struct prefix
*dest_pfx
, *src_pfx
;
1881 bool fib_changed
= false;
1882 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1883 int start_count
, end_count
;
1884 dest_pfx
= dplane_ctx_get_dest(ctx
);
1886 /* Note well: only capturing the prefix string if debug is enabled here;
1887 * unconditional log messages will have to generate the string.
1890 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1892 /* Locate rn and re(s) from ctx */
1893 rn
= rib_find_rn_from_ctx(ctx
);
1896 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1897 dplane_ctx_get_vrf(ctx
), dest_str
);
1902 dest
= rib_dest_from_rnode(rn
);
1903 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1906 zlog_debug("%u:%s Processing dplane notif ctx %p",
1907 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1910 * Take a pass through the routes, look for matches with the context
1913 RNODE_FOREACH_RE(rn
, re
) {
1914 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1918 /* No match? Nothing we can do */
1921 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1922 dplane_ctx_get_vrf(ctx
), dest_str
,
1924 dplane_ctx_get_type(ctx
)));
1929 /* Is this a notification that ... matters? We only really care about
1930 * the route that is currently selected for installation.
1932 if (re
!= dest
->selected_fib
) {
1933 /* TODO -- don't skip processing entirely? We might like to
1934 * at least report on the event.
1937 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1938 dplane_ctx_get_vrf(ctx
), dest_str
,
1940 dplane_ctx_get_type(ctx
)));
1944 /* We'll want to determine whether the installation status of the
1945 * route has changed: we'll check the status before processing,
1946 * and then again if there's been a change.
1949 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1950 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1954 /* Update zebra's nexthop FIB flags based on the context struct's
1957 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1961 zlog_debug("%u:%s No change from dplane notification",
1962 dplane_ctx_get_vrf(ctx
), dest_str
);
1968 * Perform follow-up work if the actual status of the prefix
1973 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1974 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1978 /* Various fib transitions: changed nexthops; from installed to
1979 * not-installed; or not-installed to installed.
1981 if (start_count
> 0 && end_count
> 0) {
1983 /* Changed nexthops - update kernel/others */
1984 dplane_route_notif_update(rn
, re
,
1985 DPLANE_OP_ROUTE_UPDATE
, ctx
);
1987 } else if (start_count
== 0 && end_count
> 0) {
1989 zlog_debug("%u:%s installed transition from dplane notification",
1990 dplane_ctx_get_vrf(ctx
), dest_str
);
1992 /* We expect this to be the selected route, so we want
1993 * to tell others about this transistion.
1995 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1997 /* Changed nexthops - update kernel/others */
1998 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
2000 /* Redistribute, lsp, and nht update */
2001 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2003 zebra_rib_evaluate_rn_nexthops(
2004 rn
, zebra_router_get_next_sequence());
2006 zebra_rib_evaluate_mpls(rn
);
2008 } else if (start_count
> 0 && end_count
== 0) {
2010 zlog_debug("%u:%s un-installed transition from dplane notification",
2011 dplane_ctx_get_vrf(ctx
), dest_str
);
2013 /* Transition from _something_ installed to _nothing_
2016 /* We expect this to be the selected route, so we want
2017 * to tell others about this transistion.
2019 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2021 /* Changed nexthops - update kernel/others */
2022 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2024 /* Redistribute, lsp, and nht update */
2025 redistribute_delete(dest_pfx
, src_pfx
, re
);
2027 zebra_rib_evaluate_rn_nexthops(
2028 rn
, zebra_router_get_next_sequence());
2030 zebra_rib_evaluate_mpls(rn
);
2035 route_unlock_node(rn
);
2037 /* Return context to dataplane module */
2038 dplane_ctx_fini(&ctx
);
2041 /* Take a list of route_node structs and return 1, if there was a record
2042 * picked from it and processed by rib_process(). Don't process more,
2043 * than one RN record; operate only in the specified sub-queue.
2045 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2047 struct listnode
*lnode
= listhead(subq
);
2048 struct route_node
*rnode
;
2050 struct zebra_vrf
*zvrf
= NULL
;
2055 rnode
= listgetdata(lnode
);
2056 dest
= rib_dest_from_rnode(rnode
);
2058 zvrf
= rib_dest_vrf(dest
);
2062 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2063 char buf
[SRCDEST2STR_BUFFER
];
2065 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2066 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2067 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2071 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2072 RIB_ROUTE_QUEUED(qindex
));
2077 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2078 __func__
, rnode
, rnode
->lock
);
2079 zlog_backtrace(LOG_DEBUG
);
2082 route_unlock_node(rnode
);
2083 list_delete_node(subq
, lnode
);
2089 * Perform next-hop tracking processing after RIB updates.
2091 static void do_nht_processing(void)
2095 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2096 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2098 * is pointed to the meta queue structure.
2100 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2102 struct meta_queue
*mq
= data
;
2104 uint32_t queue_len
, queue_limit
;
2106 /* Ensure there's room for more dataplane updates */
2107 queue_limit
= dplane_get_in_queue_limit();
2108 queue_len
= dplane_get_in_queue_len();
2109 if (queue_len
> queue_limit
) {
2110 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2111 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2112 queue_len
, queue_limit
);
2114 /* Ensure that the meta-queue is actually enqueued */
2115 if (work_queue_empty(zrouter
.ribq
))
2116 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2118 return WQ_QUEUE_BLOCKED
;
2121 for (i
= 0; i
< MQ_SIZE
; i
++)
2122 if (process_subq(mq
->subq
[i
], i
)) {
2126 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2131 * Look into the RN and queue it into the highest priority queue
2132 * at this point in time for processing.
2134 * We will enqueue a route node only once per invocation.
2136 * There are two possibilities here that should be kept in mind.
2137 * If the original invocation has not been pulled off for processing
2138 * yet, A subsuquent invocation can have a route entry with a better
2139 * meta queue index value and we can have a situation where
2140 * we might have the same node enqueued 2 times. Not necessarily
2141 * an optimal situation but it should be ok.
2143 * The other possibility is that the original invocation has not
2144 * been pulled off for processing yet, A subsusquent invocation
2145 * doesn't have a route_entry with a better meta-queue and the
2146 * original metaqueue index value will win and we'll end up with
2147 * the route node enqueued once.
2149 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2151 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2152 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2154 RNODE_FOREACH_RE (rn
, curr_re
) {
2155 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2157 if (curr_qindex
<= qindex
) {
2159 qindex
= curr_qindex
;
2166 /* Invariant: at this point we always have rn->info set. */
2167 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2168 RIB_ROUTE_QUEUED(qindex
))) {
2169 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2170 rnode_debug(rn
, re
->vrf_id
,
2171 "rn %p is already queued in sub-queue %u",
2172 (void *)rn
, qindex
);
2176 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2177 listnode_add(mq
->subq
[qindex
], rn
);
2178 route_lock_node(rn
);
2181 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2182 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2183 (void *)rn
, qindex
);
2186 /* Add route_node to work queue and schedule processing */
2187 void rib_queue_add(struct route_node
*rn
)
2191 /* Pointless to queue a route_node with no RIB entries to add or remove
2193 if (!rnode_to_ribs(rn
)) {
2194 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2195 __func__
, (void *)rn
, rn
->lock
);
2196 zlog_backtrace(LOG_DEBUG
);
2200 if (zrouter
.ribq
== NULL
) {
2201 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2202 "%s: work_queue does not exist!", __func__
);
2207 * The RIB queue should normally be either empty or holding the only
2208 * work_queue_item element. In the latter case this element would
2209 * hold a pointer to the meta queue structure, which must be used to
2210 * actually queue the route nodes to process. So create the MQ
2211 * holder, if necessary, then push the work into it in any case.
2212 * This semantics was introduced after 0.99.9 release.
2214 if (work_queue_empty(zrouter
.ribq
))
2215 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2217 rib_meta_queue_add(zrouter
.mq
, rn
);
2222 /* Create new meta queue.
2223 A destructor function doesn't seem to be necessary here.
2225 static struct meta_queue
*meta_queue_new(void)
2227 struct meta_queue
*new;
2230 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2232 for (i
= 0; i
< MQ_SIZE
; i
++) {
2233 new->subq
[i
] = list_new();
2234 assert(new->subq
[i
]);
2240 void meta_queue_free(struct meta_queue
*mq
)
2244 for (i
= 0; i
< MQ_SIZE
; i
++)
2245 list_delete(&mq
->subq
[i
]);
2247 XFREE(MTYPE_WORK_QUEUE
, mq
);
2250 /* initialise zebra rib work queue */
2251 static void rib_queue_init(void)
2253 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2254 "route_node processing"))) {
2255 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2256 "%s: could not initialise work queue!", __func__
);
2260 /* fill in the work queue spec */
2261 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2262 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2263 zrouter
.ribq
->spec
.completion_func
= NULL
;
2264 /* XXX: TODO: These should be runtime configurable via vty */
2265 zrouter
.ribq
->spec
.max_retries
= 3;
2266 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2267 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2269 if (!(zrouter
.mq
= meta_queue_new())) {
2270 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2271 "%s: could not initialise meta queue!", __func__
);
2277 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2281 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2282 rnh_list_init(&dest
->nht
);
2283 route_lock_node(rn
); /* rn route table reference */
2290 /* RIB updates are processed via a queue of pointers to route_nodes.
2292 * The queue length is bounded by the maximal size of the routing table,
2293 * as a route_node will not be requeued, if already queued.
2295 * REs are submitted via rib_addnode or rib_delnode which set minimal
2296 * state, or static_install_route (when an existing RE is updated)
2297 * and then submit route_node to queue for best-path selection later.
2298 * Order of add/delete state changes are preserved for any given RE.
2300 * Deleted REs are reaped during best-path selection.
2303 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2304 * |-------->| | best RE, if required
2306 * static_install->|->rib_addqueue...... -> rib_process
2308 * |-------->| |-> rib_unlink
2309 * |-> set ROUTE_ENTRY_REMOVE |
2310 * rib_delnode (RE freed)
2312 * The 'info' pointer of a route_node points to a rib_dest_t
2313 * ('dest'). Queueing state for a route_node is kept on the dest. The
2314 * dest is created on-demand by rib_link() and is kept around at least
2315 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2317 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2319 * - route_nodes: refcounted by:
2320 * - dest attached to route_node:
2321 * - managed by: rib_link/rib_gc_dest
2322 * - route_node processing queue
2323 * - managed by: rib_addqueue, rib_process.
2327 /* Add RE to head of the route node. */
2328 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2332 const char *rmap_name
;
2336 dest
= rib_dest_from_rnode(rn
);
2338 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2339 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2341 dest
= zebra_rib_create_dest(rn
);
2344 re_list_add_head(&dest
->routes
, re
);
2346 afi
= (rn
->p
.family
== AF_INET
)
2348 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2349 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2350 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2352 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2353 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2358 static void rib_addnode(struct route_node
*rn
,
2359 struct route_entry
*re
, int process
)
2361 /* RE node has been un-removed before route-node is processed.
2362 * route_node must hence already be on the queue for processing..
2364 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2365 if (IS_ZEBRA_DEBUG_RIB
)
2366 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2367 (void *)rn
, (void *)re
);
2369 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2372 rib_link(rn
, re
, process
);
2378 * Detach a rib structure from a route_node.
2380 * Note that a call to rib_unlink() should be followed by a call to
2381 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2382 * longer required to be deleted.
2384 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2390 if (IS_ZEBRA_DEBUG_RIB
)
2391 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2394 dest
= rib_dest_from_rnode(rn
);
2396 re_list_del(&dest
->routes
, re
);
2398 if (dest
->selected_fib
== re
)
2399 dest
->selected_fib
= NULL
;
2401 nexthops_free(re
->ng
.nexthop
);
2402 nexthops_free(re
->fib_ng
.nexthop
);
2404 XFREE(MTYPE_RE
, re
);
2407 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2411 if (IS_ZEBRA_DEBUG_RIB
)
2412 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2413 (void *)rn
, (void *)re
);
2414 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2416 afi
= (rn
->p
.family
== AF_INET
)
2418 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2419 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2420 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2422 zebra_del_import_table_entry(zvrf
, rn
, re
);
2423 /* Just clean up if non main table */
2424 if (IS_ZEBRA_DEBUG_RIB
) {
2425 char buf
[SRCDEST2STR_BUFFER
];
2426 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2427 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2428 re
->vrf_id
, buf
, rn
, re
,
2429 zebra_route_string(re
->type
));
2438 /* This function dumps the contents of a given RE entry into
2439 * standard debug log. Calling function name and IP prefix in
2440 * question are passed as 1st and 2nd arguments.
2443 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2444 union prefixconstptr src_pp
,
2445 const struct route_entry
*re
)
2447 const struct prefix
*src_p
= src_pp
.p
;
2448 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2449 char straddr
[PREFIX_STRLEN
];
2450 char srcaddr
[PREFIX_STRLEN
];
2451 char nhname
[PREFIX_STRLEN
];
2452 struct nexthop
*nexthop
;
2454 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2455 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2456 is_srcdst
? " from " : "",
2457 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2460 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2461 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2464 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2465 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2466 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2467 re
->nexthop_num
, re
->nexthop_active_num
);
2469 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2470 struct interface
*ifp
;
2471 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2473 switch (nexthop
->type
) {
2474 case NEXTHOP_TYPE_BLACKHOLE
:
2475 sprintf(nhname
, "Blackhole");
2477 case NEXTHOP_TYPE_IFINDEX
:
2478 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2480 sprintf(nhname
, "%s", ifp
? ifp
->name
: "Unknown");
2482 case NEXTHOP_TYPE_IPV4
:
2484 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2485 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
,
2488 case NEXTHOP_TYPE_IPV6
:
2489 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2490 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
,
2494 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2495 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2496 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2498 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2501 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2504 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2507 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2510 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2513 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2517 zlog_debug("%s: dump complete", straddr
);
2520 /* This is an exported helper to rtm_read() to dump the strange
2521 * RE entry found by rib_lookup_ipv4_route()
2524 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2526 struct route_table
*table
;
2527 struct route_node
*rn
;
2528 struct route_entry
*re
;
2529 char prefix_buf
[INET_ADDRSTRLEN
];
2532 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2534 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2535 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2540 /* Scan the RIB table for exactly matching RE entry. */
2541 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2543 /* No route for this prefix. */
2545 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2546 prefix2str((struct prefix
*)p
, prefix_buf
,
2547 sizeof(prefix_buf
)));
2552 route_unlock_node(rn
);
2555 RNODE_FOREACH_RE (rn
, re
) {
2556 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2558 (void *)rn
, (void *)re
,
2559 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2562 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2565 route_entry_dump(p
, NULL
, re
);
2569 /* Check if requested address assignment will fail due to another
2570 * route being installed by zebra in FIB already. Take necessary
2571 * actions, if needed: remove such a route from FIB and deSELECT
2572 * corresponding RE entry. Then put affected RN into RIBQ head.
2574 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2576 struct route_table
*table
;
2577 struct route_node
*rn
;
2580 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2581 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2582 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2587 /* No matches would be the simplest case. */
2588 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2592 route_unlock_node(rn
);
2594 dest
= rib_dest_from_rnode(rn
);
2595 /* Check all RE entries. In case any changes have to be done, requeue
2596 * the RN into RIBQ head. If the routing message about the new connected
2597 * route (generated by the IP address we are going to assign very soon)
2598 * comes before the RIBQ is processed, the new RE entry will join
2599 * RIBQ record already on head. This is necessary for proper
2601 * of the rest of the RE.
2603 if (dest
->selected_fib
) {
2604 if (IS_ZEBRA_DEBUG_RIB
) {
2605 char buf
[PREFIX_STRLEN
];
2607 zlog_debug("%u:%s: freeing way for connected prefix",
2608 dest
->selected_fib
->vrf_id
,
2609 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2610 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2612 rib_uninstall(rn
, dest
->selected_fib
);
2617 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2618 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2620 struct route_table
*table
;
2621 struct route_node
*rn
;
2622 struct route_entry
*same
= NULL
;
2628 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2631 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2633 XFREE(MTYPE_RE
, re
);
2637 /* Make it sure prefixlen is applied to the prefix. */
2640 apply_mask_ipv6(src_p
);
2642 /* Set default distance by route type. */
2643 if (re
->distance
== 0)
2644 re
->distance
= route_distance(re
->type
);
2646 /* Lookup route node.*/
2647 rn
= srcdest_rnode_get(table
, p
, src_p
);
2650 * If same type of route are installed, treat it as a implicit
2652 * If the user has specified the No route replace semantics
2653 * for the install don't do a route replace.
2655 RNODE_FOREACH_RE (rn
, same
) {
2656 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2659 if (same
->type
!= re
->type
)
2661 if (same
->instance
!= re
->instance
)
2663 if (same
->type
== ZEBRA_ROUTE_KERNEL
2664 && same
->metric
!= re
->metric
)
2667 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2668 same
->distance
!= re
->distance
)
2672 * We should allow duplicate connected routes
2673 * because of IPv6 link-local routes and unnumbered
2674 * interfaces on Linux.
2676 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2680 /* If this route is kernel/connected route, notify the dataplane. */
2681 if (RIB_SYSTEM_ROUTE(re
)) {
2682 /* Notify dataplane */
2683 dplane_sys_route_add(rn
, re
);
2686 /* Link new re to node.*/
2687 if (IS_ZEBRA_DEBUG_RIB
) {
2688 rnode_debug(rn
, re
->vrf_id
,
2689 "Inserting route rn %p, re %p (%s) existing %p",
2690 rn
, re
, zebra_route_string(re
->type
), same
);
2692 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2693 route_entry_dump(p
, src_p
, re
);
2696 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2697 rib_addnode(rn
, re
, 1);
2700 /* Free implicit route.*/
2702 rib_delnode(rn
, same
);
2706 route_unlock_node(rn
);
2710 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2711 unsigned short instance
, int flags
, struct prefix
*p
,
2712 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2713 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2716 struct route_table
*table
;
2717 struct route_node
*rn
;
2718 struct route_entry
*re
;
2719 struct route_entry
*fib
= NULL
;
2720 struct route_entry
*same
= NULL
;
2721 struct nexthop
*rtnh
;
2722 char buf2
[INET6_ADDRSTRLEN
];
2725 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2728 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2735 apply_mask_ipv6(src_p
);
2737 /* Lookup route node. */
2738 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2740 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2742 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2743 if (src_p
&& src_p
->prefixlen
)
2744 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2748 if (IS_ZEBRA_DEBUG_RIB
) {
2749 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2751 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
2752 vrf
->name
, table_id
, dst_buf
,
2753 (src_buf
[0] != '\0') ? " from " : "",
2759 dest
= rib_dest_from_rnode(rn
);
2760 fib
= dest
->selected_fib
;
2762 /* Lookup same type route. */
2763 RNODE_FOREACH_RE (rn
, re
) {
2764 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2767 if (re
->type
!= type
)
2769 if (re
->instance
!= instance
)
2771 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2772 distance
!= re
->distance
)
2775 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2777 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2778 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2779 if (rtnh
->ifindex
!= nh
->ifindex
)
2784 /* Make sure that the route found has the same gateway. */
2790 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2792 * No guarantee all kernel send nh with labels
2795 if (nexthop_same_no_labels(rtnh
, nh
)) {
2803 /* If same type of route can't be found and this message is from
2807 * In the past(HA!) we could get here because
2808 * we were receiving a route delete from the
2809 * kernel and we're not marking the proto
2810 * as coming from it's appropriate originator.
2811 * Now that we are properly noticing the fact
2812 * that the kernel has deleted our route we
2813 * are not going to get called in this path
2814 * I am going to leave this here because
2815 * this might still work this way on non-linux
2816 * platforms as well as some weird state I have
2817 * not properly thought of yet.
2818 * If we can show that this code path is
2819 * dead then we can remove it.
2821 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2822 if (IS_ZEBRA_DEBUG_RIB
) {
2823 rnode_debug(rn
, vrf_id
,
2824 "rn %p, re %p (%s) was deleted from kernel, adding",
2826 zebra_route_string(fib
->type
));
2829 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2831 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2833 UNSET_FLAG(rtnh
->flags
,
2837 * This is a non FRR route
2838 * as such we should mark
2841 dest
->selected_fib
= NULL
;
2843 /* This means someone else, other than Zebra,
2845 * a Zebra router from the kernel. We will add
2847 rib_install_kernel(rn
, fib
, NULL
);
2850 if (IS_ZEBRA_DEBUG_RIB
) {
2854 "via %s ifindex %d type %d "
2855 "doesn't exist in rib",
2856 inet_ntop(afi2family(afi
),
2863 "type %d doesn't exist in rib",
2866 route_unlock_node(rn
);
2872 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2874 rib_install_kernel(rn
, same
, NULL
);
2875 route_unlock_node(rn
);
2880 /* Special handling for IPv4 or IPv6 routes sourced from
2881 * EVPN - the nexthop (and associated MAC) need to be
2882 * uninstalled if no more refs.
2884 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2885 struct nexthop
*tmp_nh
;
2887 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2888 struct ipaddr vtep_ip
;
2890 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2891 if (afi
== AFI_IP
) {
2892 vtep_ip
.ipa_type
= IPADDR_V4
;
2893 memcpy(&(vtep_ip
.ipaddr_v4
),
2894 &(tmp_nh
->gate
.ipv4
),
2895 sizeof(struct in_addr
));
2897 vtep_ip
.ipa_type
= IPADDR_V6
;
2898 memcpy(&(vtep_ip
.ipaddr_v6
),
2899 &(tmp_nh
->gate
.ipv6
),
2900 sizeof(struct in6_addr
));
2902 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2907 /* Notify dplane if system route changes */
2908 if (RIB_SYSTEM_ROUTE(re
))
2909 dplane_sys_route_del(rn
, same
);
2911 rib_delnode(rn
, same
);
2914 route_unlock_node(rn
);
2919 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2920 unsigned short instance
, int flags
, struct prefix
*p
,
2921 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2922 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2925 struct route_entry
*re
;
2926 struct nexthop
*nexthop
;
2928 /* Allocate new route_entry structure. */
2929 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2931 re
->instance
= instance
;
2932 re
->distance
= distance
;
2934 re
->metric
= metric
;
2936 re
->table
= table_id
;
2937 re
->vrf_id
= vrf_id
;
2938 re
->nexthop_num
= 0;
2939 re
->uptime
= monotime(NULL
);
2943 nexthop
= nexthop_new();
2945 route_entry_nexthop_add(re
, nexthop
);
2947 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2950 /* Schedule routes of a particular table (address-family) based on event. */
2951 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2953 struct route_node
*rn
;
2954 struct route_entry
*re
, *next
;
2956 /* Walk all routes and queue for processing, if appropriate for
2957 * the trigger event.
2959 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2961 * If we are looking at a route node and the node
2962 * has already been queued we don't
2963 * need to queue it up again
2965 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2966 RIB_ROUTE_ANY_QUEUED
))
2969 case RIB_UPDATE_RMAP_CHANGE
:
2970 case RIB_UPDATE_OTHER
:
2971 /* Right now, examine all routes. Can restrict to a
2973 * some cases (TODO).
2975 if (rnode_to_ribs(rn
)) {
2976 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
2977 SET_FLAG(re
->status
,
2978 ROUTE_ENTRY_CHANGED
);
2989 /* RIB update function. */
2990 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2992 struct route_table
*table
;
2994 /* Process routes of interested address-families. */
2995 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2997 if (IS_ZEBRA_DEBUG_EVENT
)
2998 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
2999 rib_update_table(table
, event
);
3002 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3004 if (IS_ZEBRA_DEBUG_EVENT
)
3005 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3006 rib_update_table(table
, event
);
3010 /* Delete self installed routes after zebra is relaunched. */
3011 void rib_sweep_table(struct route_table
*table
)
3013 struct route_node
*rn
;
3014 struct route_entry
*re
;
3015 struct route_entry
*next
;
3016 struct nexthop
*nexthop
;
3021 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3022 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3024 if (IS_ZEBRA_DEBUG_RIB
)
3025 route_entry_dump(&rn
->p
, NULL
, re
);
3027 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3030 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3034 * If routes are older than startup_time then
3035 * we know we read them in from the kernel.
3036 * As such we can safely remove them.
3038 if (zrouter
.startup_time
< re
->uptime
)
3042 * So we are starting up and have received
3043 * routes from the kernel that we have installed
3044 * from a previous run of zebra but not cleaned
3045 * up ( say a kill -9 )
3046 * But since we haven't actually installed
3047 * them yet( we received them from the kernel )
3048 * we don't think they are active.
3049 * So let's pretend they are active to actually
3051 * In all honesty I'm not sure if we should
3052 * mark them as active when we receive them
3053 * This is startup only so probably ok.
3055 * If we ever decide to move rib_sweep_table
3056 * to a different spot (ie startup )
3057 * this decision needs to be revisited
3059 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3060 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3061 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3063 rib_uninstall_kernel(rn
, re
);
3064 rib_delnode(rn
, re
);
3069 /* Sweep all RIB tables. */
3070 int rib_sweep_route(struct thread
*t
)
3073 struct zebra_vrf
*zvrf
;
3075 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3076 if ((zvrf
= vrf
->info
) == NULL
)
3079 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3080 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3083 zebra_router_sweep_route();
3088 /* Remove specific by protocol routes from 'table'. */
3089 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3090 struct route_table
*table
)
3092 struct route_node
*rn
;
3093 struct route_entry
*re
;
3094 struct route_entry
*next
;
3095 unsigned long n
= 0;
3098 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3099 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3100 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3102 if (re
->type
== proto
3103 && re
->instance
== instance
) {
3104 rib_delnode(rn
, re
);
3111 /* Remove specific by protocol routes. */
3112 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3115 struct zebra_vrf
*zvrf
;
3116 struct other_route_table
*ort
;
3117 unsigned long cnt
= 0;
3119 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3124 cnt
+= rib_score_proto_table(proto
, instance
,
3125 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3126 + rib_score_proto_table(
3128 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3130 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3131 rib_score_proto_table(proto
, instance
, ort
->table
);
3137 /* Close RIB and clean up kernel routes. */
3138 void rib_close_table(struct route_table
*table
)
3140 struct route_node
*rn
;
3141 rib_table_info_t
*info
;
3147 info
= route_table_get_info(table
);
3149 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3150 dest
= rib_dest_from_rnode(rn
);
3152 if (dest
&& dest
->selected_fib
) {
3153 if (info
->safi
== SAFI_UNICAST
)
3154 hook_call(rib_update
, rn
, NULL
);
3156 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3157 dest
->selected_fib
= NULL
;
3163 * Handler for async dataplane results after a pseudowire installation
3165 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3167 struct zebra_pw
*pw
;
3168 struct zebra_vrf
*vrf
;
3170 /* The pseudowire code assumes success - we act on an error
3171 * result for installation attempts here.
3173 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3176 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3177 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3178 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3180 zebra_pw_install_failure(pw
);
3190 * Handle results from the dataplane system. Dequeue update context
3191 * structs, dispatch to appropriate internal handlers.
3193 static int rib_process_dplane_results(struct thread
*thread
)
3195 struct zebra_dplane_ctx
*ctx
;
3196 struct dplane_ctx_q ctxlist
;
3198 /* Dequeue a list of completed updates with one lock/unlock cycle */
3201 TAILQ_INIT(&ctxlist
);
3203 /* Take lock controlling queue of results */
3204 pthread_mutex_lock(&dplane_mutex
);
3206 /* Dequeue list of context structs */
3207 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3209 pthread_mutex_unlock(&dplane_mutex
);
3211 /* Dequeue context block */
3212 ctx
= dplane_ctx_dequeue(&ctxlist
);
3214 /* If we've emptied the results queue, we're done */
3219 switch (dplane_ctx_get_op(ctx
)) {
3220 case DPLANE_OP_ROUTE_INSTALL
:
3221 case DPLANE_OP_ROUTE_UPDATE
:
3222 case DPLANE_OP_ROUTE_DELETE
:
3224 /* Bit of special case for route updates
3225 * that were generated by async notifications:
3226 * we don't want to continue processing these
3229 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3230 rib_process_result(ctx
);
3232 dplane_ctx_fini(&ctx
);
3236 case DPLANE_OP_ROUTE_NOTIFY
:
3237 rib_process_dplane_notify(ctx
);
3240 case DPLANE_OP_LSP_INSTALL
:
3241 case DPLANE_OP_LSP_UPDATE
:
3242 case DPLANE_OP_LSP_DELETE
:
3244 /* Bit of special case for LSP updates
3245 * that were generated by async notifications:
3246 * we don't want to continue processing these.
3248 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3249 zebra_mpls_lsp_dplane_result(ctx
);
3251 dplane_ctx_fini(&ctx
);
3255 case DPLANE_OP_LSP_NOTIFY
:
3256 zebra_mpls_process_dplane_notify(ctx
);
3259 case DPLANE_OP_PW_INSTALL
:
3260 case DPLANE_OP_PW_UNINSTALL
:
3261 handle_pw_result(ctx
);
3264 case DPLANE_OP_SYS_ROUTE_ADD
:
3265 case DPLANE_OP_SYS_ROUTE_DELETE
:
3266 /* No further processing in zebra for these. */
3267 dplane_ctx_fini(&ctx
);
3271 /* Don't expect this: just return the struct? */
3272 dplane_ctx_fini(&ctx
);
3274 } /* Dispatch by op code */
3276 ctx
= dplane_ctx_dequeue(&ctxlist
);
3281 /* Check for nexthop tracking processing after finishing with results */
3282 do_nht_processing();
3288 * Results are returned from the dataplane subsystem, in the context of
3289 * the dataplane pthread. We enqueue the results here for processing by
3290 * the main thread later.
3292 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3294 /* Take lock controlling queue of results */
3295 pthread_mutex_lock(&dplane_mutex
);
3297 /* Enqueue context blocks */
3298 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3300 pthread_mutex_unlock(&dplane_mutex
);
3302 /* Ensure event is signalled to zebra main pthread */
3303 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3310 * Ensure there are no empty slots in the route_info array.
3311 * Every route type in zebra should be present there.
3313 static void check_route_info(void)
3315 int len
= array_size(route_info
);
3318 * ZEBRA_ROUTE_SYSTEM is special cased since
3319 * its key is 0 anyway.
3321 * ZEBRA_ROUTE_ALL is also ignored.
3323 for (int i
= 0; i
< len
; i
++) {
3324 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3326 assert(route_info
[i
].key
);
3327 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3331 /* Routing information base initialize. */
3338 /* Init dataplane, and register for results */
3339 pthread_mutex_init(&dplane_mutex
, NULL
);
3340 TAILQ_INIT(&rib_dplane_q
);
3341 zebra_dplane_init(rib_dplane_results
);
3347 * Get the first vrf id that is greater than the given vrf id if any.
3349 * Returns true if a vrf id was found, false otherwise.
3351 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3355 vrf
= vrf_lookup_by_id(vrf_id
);
3357 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3359 *next_id_p
= vrf
->vrf_id
;
3368 * rib_tables_iter_next
3370 * Returns the next table in the iteration.
3372 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3374 struct route_table
*table
;
3377 * Array that helps us go over all AFI/SAFI combinations via one
3384 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3385 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3386 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3391 switch (iter
->state
) {
3393 case RIB_TABLES_ITER_S_INIT
:
3394 iter
->vrf_id
= VRF_DEFAULT
;
3395 iter
->afi_safi_ix
= -1;
3399 case RIB_TABLES_ITER_S_ITERATING
:
3400 iter
->afi_safi_ix
++;
3403 while (iter
->afi_safi_ix
3404 < (int)array_size(afi_safis
)) {
3405 table
= zebra_vrf_table(
3406 afi_safis
[iter
->afi_safi_ix
].afi
,
3407 afi_safis
[iter
->afi_safi_ix
].safi
,
3412 iter
->afi_safi_ix
++;
3416 * Found another table in this vrf.
3422 * Done with all tables in the current vrf, go to the
3426 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3429 iter
->afi_safi_ix
= 0;
3434 case RIB_TABLES_ITER_S_DONE
:
3439 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3441 iter
->state
= RIB_TABLES_ITER_S_DONE
;