1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
40 #include "zebra/zebra_router.h"
41 #include "zebra/connected.h"
42 #include "zebra/debug.h"
43 #include "zebra/interface.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/rib.h"
47 #include "zebra/zapi_msg.h"
48 #include "zebra/zebra_errors.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/zebra_routemap.h"
53 #include "zebra/zebra_vrf.h"
54 #include "zebra/zebra_vxlan.h"
55 #include "zebra/zapi_msg.h"
56 #include "zebra/zebra_dplane.h"
57 #include "zebra/zebra_nhg.h"
60 * Event, list, and mutex for delivery of dataplane results
62 static pthread_mutex_t dplane_mutex
;
63 static struct thread
*t_dplane
;
64 static struct dplane_ctx_q rib_dplane_q
;
66 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
69 /* Should we allow non Quagga processes to delete our routes */
70 extern int allow_delete
;
72 /* Each route type's string and default distance value. */
77 } route_info
[ZEBRA_ROUTE_MAX
] = {
78 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 4},
79 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 0},
80 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 0},
81 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 1},
82 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 2},
83 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 2},
84 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 2},
85 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 2},
86 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 2},
87 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 3},
88 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 4},
89 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 2},
90 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 2},
91 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 4},
92 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 4},
93 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 1},
94 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 4},
95 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 3},
96 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 3},
97 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 3},
98 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 3},
99 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 3},
100 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 2},
101 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 4},
102 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 4},
103 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 4},
104 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 2},
105 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 4}
106 /* Any new route type added to zebra, should be mirrored here */
108 /* no entry/default: 150 */
111 static void __attribute__((format(printf
, 5, 6)))
112 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
113 int priority
, const char *msgfmt
, ...)
115 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
119 va_start(ap
, msgfmt
);
120 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
124 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
125 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
127 if (info
->safi
== SAFI_MULTICAST
)
128 strlcat(buf
, " (MRIB)", sizeof(buf
));
130 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
133 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
136 #define rnode_debug(node, vrf_id, ...) \
137 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
138 #define rnode_info(node, ...) \
139 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
141 uint8_t route_distance(int type
)
145 if ((unsigned)type
>= array_size(route_info
))
148 distance
= route_info
[type
].distance
;
153 int is_zebra_valid_kernel_table(uint32_t table_id
)
156 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
157 || (table_id
== RT_TABLE_COMPAT
))
164 int is_zebra_main_routing_table(uint32_t table_id
)
166 if (table_id
== RT_TABLE_MAIN
)
171 int zebra_check_addr(const struct prefix
*p
)
173 if (p
->family
== AF_INET
) {
176 addr
= p
->u
.prefix4
.s_addr
;
179 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
180 || IPV4_LINKLOCAL(addr
))
183 if (p
->family
== AF_INET6
) {
184 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
186 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
192 /* Add nexthop to the end of a rib node's nexthop list */
193 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
195 nexthop_add(&re
->ng
.nexthop
, nexthop
);
201 * copy_nexthop - copy a nexthop to the rib structure.
203 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
205 assert(!re
->ng
.nexthop
);
206 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
207 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
211 /* Delete specified nexthop from the list. */
212 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
215 nexthop
->next
->prev
= nexthop
->prev
;
217 nexthop
->prev
->next
= nexthop
->next
;
219 re
->ng
.nexthop
= nexthop
->next
;
224 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
228 struct nexthop
*nexthop
;
230 nexthop
= nexthop_new();
231 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
232 nexthop
->ifindex
= ifindex
;
233 nexthop
->vrf_id
= nh_vrf_id
;
235 route_entry_nexthop_add(re
, nexthop
);
240 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
241 struct in_addr
*ipv4
,
245 struct nexthop
*nexthop
;
247 nexthop
= nexthop_new();
248 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
249 nexthop
->vrf_id
= nh_vrf_id
;
250 nexthop
->gate
.ipv4
= *ipv4
;
252 nexthop
->src
.ipv4
= *src
;
254 route_entry_nexthop_add(re
, nexthop
);
259 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
260 struct in_addr
*ipv4
,
265 struct nexthop
*nexthop
;
266 struct interface
*ifp
;
268 nexthop
= nexthop_new();
269 nexthop
->vrf_id
= nh_vrf_id
;
270 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
271 nexthop
->gate
.ipv4
= *ipv4
;
273 nexthop
->src
.ipv4
= *src
;
274 nexthop
->ifindex
= ifindex
;
275 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
276 /*Pending: need to think if null ifp here is ok during bootup?
277 There was a crash because ifp here was coming to be NULL */
279 if (connected_is_unnumbered(ifp
))
280 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
282 route_entry_nexthop_add(re
, nexthop
);
287 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
288 struct in6_addr
*ipv6
,
291 struct nexthop
*nexthop
;
293 nexthop
= nexthop_new();
294 nexthop
->vrf_id
= nh_vrf_id
;
295 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
296 nexthop
->gate
.ipv6
= *ipv6
;
298 route_entry_nexthop_add(re
, nexthop
);
303 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
304 struct in6_addr
*ipv6
,
308 struct nexthop
*nexthop
;
310 nexthop
= nexthop_new();
311 nexthop
->vrf_id
= nh_vrf_id
;
312 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
313 nexthop
->gate
.ipv6
= *ipv6
;
314 nexthop
->ifindex
= ifindex
;
316 route_entry_nexthop_add(re
, nexthop
);
321 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
322 enum blackhole_type bh_type
)
324 struct nexthop
*nexthop
;
326 nexthop
= nexthop_new();
327 nexthop
->vrf_id
= VRF_DEFAULT
;
328 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
329 nexthop
->bh_type
= bh_type
;
331 route_entry_nexthop_add(re
, nexthop
);
336 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
337 union g_addr
*addr
, struct route_node
**rn_out
)
340 struct route_table
*table
;
341 struct route_node
*rn
;
342 struct route_entry
*match
= NULL
;
345 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
349 memset(&p
, 0, sizeof(struct prefix
));
352 p
.u
.prefix4
= addr
->ipv4
;
353 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
355 p
.u
.prefix6
= addr
->ipv6
;
356 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
359 rn
= route_node_match(table
, (struct prefix
*)&p
);
364 route_unlock_node(rn
);
366 dest
= rib_dest_from_rnode(rn
);
367 if (dest
&& dest
->selected_fib
368 && !CHECK_FLAG(dest
->selected_fib
->status
,
369 ROUTE_ENTRY_REMOVED
))
370 match
= dest
->selected_fib
;
372 /* If there is no selected route or matched route is EGP, go up
377 } while (rn
&& rn
->info
== NULL
);
381 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
382 if (!CHECK_FLAG(match
->status
,
383 ROUTE_ENTRY_INSTALLED
))
395 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
397 struct route_node
**rn_out
)
399 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
400 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
401 union g_addr gaddr
= {.ipv4
= addr
};
403 switch (zrouter
.ipv4_multicast_mode
) {
404 case MCAST_MRIB_ONLY
:
405 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
407 case MCAST_URIB_ONLY
:
408 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
409 case MCAST_NO_CONFIG
:
410 case MCAST_MIX_MRIB_FIRST
:
411 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
414 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
417 case MCAST_MIX_DISTANCE
:
418 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
419 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
421 re
= ure
->distance
< mre
->distance
? ure
: mre
;
427 case MCAST_MIX_PFXLEN
:
428 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
429 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
431 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
440 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
442 if (IS_ZEBRA_DEBUG_RIB
) {
444 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
446 zlog_debug("%s: %s: vrf: %u found %s, using %s",
447 __func__
, buf
, vrf_id
,
448 mre
? (ure
? "MRIB+URIB" : "MRIB")
449 : ure
? "URIB" : "nothing",
450 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
455 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
457 struct route_table
*table
;
458 struct route_node
*rn
;
459 struct route_entry
*match
= NULL
;
463 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
467 rn
= route_node_lookup(table
, (struct prefix
*)p
);
469 /* No route for this prefix. */
474 route_unlock_node(rn
);
475 dest
= rib_dest_from_rnode(rn
);
477 if (dest
&& dest
->selected_fib
478 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
479 match
= dest
->selected_fib
;
484 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
487 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
494 * Is this RIB labeled-unicast? It must be of type BGP and all paths
495 * (nexthops) must have a label.
497 int zebra_rib_labeled_unicast(struct route_entry
*re
)
499 struct nexthop
*nexthop
= NULL
;
501 if (re
->type
!= ZEBRA_ROUTE_BGP
)
504 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
505 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
511 /* Update flag indicates whether this is a "replace" or not. Currently, this
512 * is only used for IPv4.
514 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
515 struct route_entry
*old
)
517 struct nexthop
*nexthop
;
518 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
519 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
520 const struct prefix
*p
, *src_p
;
521 enum zebra_dplane_result ret
;
523 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
525 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
527 if (info
->safi
!= SAFI_UNICAST
) {
528 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
529 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
532 struct nexthop
*prev
;
534 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
535 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
536 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
539 if (nexthop_same_firsthop(nexthop
, prev
)) {
540 SET_FLAG(nexthop
->flags
,
541 NEXTHOP_FLAG_DUPLICATE
);
549 * If this is a replace to a new RE let the originator of the RE
550 * know that they've lost
552 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
553 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
555 /* Update fib selection */
556 dest
->selected_fib
= re
;
559 * Make sure we update the FPM any time we send new information to
562 hook_call(rib_update
, rn
, "installing in kernel");
564 /* Send add or update */
566 ret
= dplane_route_update(rn
, re
, old
);
568 ret
= dplane_route_add(rn
, re
);
571 case ZEBRA_DPLANE_REQUEST_QUEUED
:
572 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
575 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
577 /* Free old FIB nexthop group */
578 if (old
->fib_ng
.nexthop
) {
579 nexthops_free(old
->fib_ng
.nexthop
);
580 old
->fib_ng
.nexthop
= NULL
;
583 if (!RIB_SYSTEM_ROUTE(old
)) {
584 /* Clear old route's FIB flags */
585 for (ALL_NEXTHOPS(old
->ng
, nexthop
)) {
586 UNSET_FLAG(nexthop
->flags
,
593 zvrf
->installs_queued
++;
595 case ZEBRA_DPLANE_REQUEST_FAILURE
:
597 char str
[SRCDEST2STR_BUFFER
];
599 srcdest_rnode2str(rn
, str
, sizeof(str
));
600 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
601 "%u:%s: Failed to enqueue dataplane install",
605 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
614 /* Uninstall the route from kernel. */
615 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
617 struct nexthop
*nexthop
;
618 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
619 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
621 if (info
->safi
!= SAFI_UNICAST
) {
622 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
623 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
624 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
629 * Make sure we update the FPM any time we send new information to
632 hook_call(rib_update
, rn
, "uninstalling from kernel");
634 switch (dplane_route_delete(rn
, re
)) {
635 case ZEBRA_DPLANE_REQUEST_QUEUED
:
637 zvrf
->removals_queued
++;
639 case ZEBRA_DPLANE_REQUEST_FAILURE
:
641 char str
[SRCDEST2STR_BUFFER
];
643 srcdest_rnode2str(rn
, str
, sizeof(str
));
644 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
645 "%u:%s: Failed to enqueue dataplane uninstall",
649 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
658 /* Uninstall the route from kernel. */
659 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
661 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
662 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
663 struct nexthop
*nexthop
;
665 if (dest
&& dest
->selected_fib
== re
) {
666 if (info
->safi
== SAFI_UNICAST
)
667 hook_call(rib_update
, rn
, "rib_uninstall");
669 /* If labeled-unicast route, uninstall transit LSP. */
670 if (zebra_rib_labeled_unicast(re
))
671 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
673 rib_uninstall_kernel(rn
, re
);
675 dest
->selected_fib
= NULL
;
677 /* Free FIB nexthop group, if present */
678 if (re
->fib_ng
.nexthop
) {
679 nexthops_free(re
->fib_ng
.nexthop
);
680 re
->fib_ng
.nexthop
= NULL
;
683 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
684 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
687 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
688 const struct prefix
*p
, *src_p
;
690 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
692 redistribute_delete(p
, src_p
, re
);
693 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
698 * rib_can_delete_dest
700 * Returns TRUE if the given dest can be deleted from the table.
702 static int rib_can_delete_dest(rib_dest_t
*dest
)
704 if (re_list_first(&dest
->routes
)) {
709 * Unresolved rnh's are stored on the default route's list
711 * dest->rnode can also be the source prefix node in an
712 * ipv6 sourcedest table. Fortunately the prefix of a
713 * source prefix node can never be the default prefix.
715 if (is_default_prefix(&dest
->rnode
->p
))
719 * Don't delete the dest if we have to update the FPM about this
722 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
723 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
729 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
731 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
735 * We are storing the rnh's associated withb
736 * the tracked nexthop as a list of the rn's.
737 * Unresolved rnh's are placed at the top
738 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
739 * As such for each rn we need to walk up the tree
740 * and see if any rnh's need to see if they
741 * would match a more specific route
744 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
745 char buf
[PREFIX_STRLEN
];
747 zlog_debug("%s: %s Being examined for Nexthop Tracking",
749 srcdest_rnode2str(rn
, buf
, sizeof(buf
)));
754 dest
= rib_dest_from_rnode(rn
);
758 * If we have any rnh's stored in the nht list
759 * then we know that this route node was used for
760 * nht resolution and as such we need to call the
761 * nexthop tracking evaluation code
763 frr_each (rnh_list
, &dest
->nht
, rnh
) {
764 struct zebra_vrf
*zvrf
=
765 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
766 struct prefix
*p
= &rnh
->node
->p
;
768 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
769 char buf1
[PREFIX_STRLEN
];
770 char buf2
[PREFIX_STRLEN
];
772 zlog_debug("%u:%s has Nexthop(%s) depending on it, evaluating %u:%u",
774 srcdest_rnode2str(rn
, buf1
,
776 prefix2str(p
, buf2
, sizeof(buf2
)),
781 * If we have evaluated this node on this pass
782 * already, due to following the tree up
783 * then we know that we can move onto the next
786 * Additionally we call zebra_evaluate_rnh
787 * when we gc the dest. In this case we know
788 * that there must be no other re's where
789 * we were originally as such we know that
790 * that sequence number is ok to respect.
792 if (rnh
->seqno
== seq
) {
793 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
795 "\tNode processed and moved already");
800 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
806 dest
= rib_dest_from_rnode(rn
);
813 * Garbage collect the rib dest corresponding to the given route node
816 * Returns TRUE if the dest was deleted, FALSE otherwise.
818 int rib_gc_dest(struct route_node
*rn
)
822 dest
= rib_dest_from_rnode(rn
);
826 if (!rib_can_delete_dest(dest
))
829 if (IS_ZEBRA_DEBUG_RIB
) {
830 struct zebra_vrf
*zvrf
;
832 zvrf
= rib_dest_vrf(dest
);
833 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
836 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
839 rnh_list_fini(&dest
->nht
);
840 XFREE(MTYPE_RIB_DEST
, dest
);
844 * Release the one reference that we keep on the route node.
846 route_unlock_node(rn
);
850 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
851 struct route_entry
*new)
853 hook_call(rib_update
, rn
, "new route selected");
855 /* Update real nexthop. This may actually determine if nexthop is active
857 if (!nexthop_group_active_nexthop_num(&new->ng
)) {
858 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
862 if (IS_ZEBRA_DEBUG_RIB
) {
863 char buf
[SRCDEST2STR_BUFFER
];
864 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
865 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
866 zvrf_id(zvrf
), buf
, rn
, new,
867 zebra_route_string(new->type
));
870 /* If labeled-unicast route, install transit LSP. */
871 if (zebra_rib_labeled_unicast(new))
872 zebra_mpls_lsp_install(zvrf
, rn
, new);
874 rib_install_kernel(rn
, new, NULL
);
876 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
879 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
880 struct route_entry
*old
)
882 hook_call(rib_update
, rn
, "removing existing route");
884 /* Uninstall from kernel. */
885 if (IS_ZEBRA_DEBUG_RIB
) {
886 char buf
[SRCDEST2STR_BUFFER
];
887 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
888 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
889 zvrf_id(zvrf
), buf
, rn
, old
,
890 zebra_route_string(old
->type
));
893 /* If labeled-unicast route, uninstall transit LSP. */
894 if (zebra_rib_labeled_unicast(old
))
895 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
897 rib_uninstall_kernel(rn
, old
);
899 /* Update nexthop for route, reset changed flag. */
900 /* Note: this code also handles the Linux case when an interface goes
901 * down, causing the kernel to delete routes without sending DELROUTE
904 if (RIB_KERNEL_ROUTE(old
))
905 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
907 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
910 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
911 struct route_node
*rn
,
912 struct route_entry
*old
,
913 struct route_entry
*new)
918 * We have to install or update if a new route has been selected or
919 * something has changed.
921 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
922 hook_call(rib_update
, rn
, "updating existing route");
924 /* Update the nexthop; we could determine here that nexthop is
926 if (nexthop_group_active_nexthop_num(&new->ng
))
929 /* If nexthop is active, install the selected route, if
931 * the install succeeds, cleanup flags for prior route, if
936 if (IS_ZEBRA_DEBUG_RIB
) {
937 char buf
[SRCDEST2STR_BUFFER
];
938 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
941 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
942 zvrf_id(zvrf
), buf
, rn
, new,
943 zebra_route_string(new->type
),
945 zebra_route_string(old
->type
));
948 "%u:%s: Updating route rn %p, re %p (%s)",
949 zvrf_id(zvrf
), buf
, rn
, new,
950 zebra_route_string(new->type
));
953 /* If labeled-unicast route, uninstall transit LSP. */
954 if (zebra_rib_labeled_unicast(old
))
955 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
958 * Non-system route should be installed.
959 * If labeled-unicast route, install transit
962 if (zebra_rib_labeled_unicast(new))
963 zebra_mpls_lsp_install(zvrf
, rn
, new);
965 rib_install_kernel(rn
, new, old
);
969 * If nexthop for selected route is not active or install
971 * may need to uninstall and delete for redistribution.
974 if (IS_ZEBRA_DEBUG_RIB
) {
975 char buf
[SRCDEST2STR_BUFFER
];
976 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
979 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
980 zvrf_id(zvrf
), buf
, rn
, new,
981 zebra_route_string(new->type
),
983 zebra_route_string(old
->type
));
986 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
987 zvrf_id(zvrf
), buf
, rn
, new,
988 zebra_route_string(new->type
));
991 /* If labeled-unicast route, uninstall transit LSP. */
992 if (zebra_rib_labeled_unicast(old
))
993 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
995 rib_uninstall_kernel(rn
, old
);
999 * Same route selected; check if in the FIB and if not,
1000 * re-install. This is housekeeping code to deal with
1001 * race conditions in kernel with linux netlink reporting
1002 * interface up before IPv4 or IPv6 protocol is ready
1005 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
1006 RIB_SYSTEM_ROUTE(new))
1007 rib_install_kernel(rn
, new, NULL
);
1010 /* Update prior route. */
1012 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1014 /* Clear changed flag. */
1015 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1018 /* Check if 'alternate' RIB entry is better than 'current'. */
1019 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1020 struct route_entry
*alternate
)
1022 if (current
== NULL
)
1025 /* filter route selection in following order:
1026 * - connected beats other types
1027 * - if both connected, loopback or vrf wins
1028 * - lower distance beats higher
1029 * - lower metric beats higher for equal distance
1030 * - last, hence oldest, route wins tie break.
1033 /* Connected routes. Check to see if either are a vrf
1034 * or loopback interface. If not, pick the last connected
1035 * route of the set of lowest metric connected routes.
1037 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1038 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1041 /* both are connected. are either loop or vrf? */
1042 struct nexthop
*nexthop
= NULL
;
1044 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1045 if (if_is_loopback_or_vrf(if_lookup_by_index(
1046 nexthop
->ifindex
, alternate
->vrf_id
)))
1050 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1051 if (if_is_loopback_or_vrf(if_lookup_by_index(
1052 nexthop
->ifindex
, current
->vrf_id
)))
1056 /* Neither are loop or vrf so pick best metric */
1057 if (alternate
->metric
<= current
->metric
)
1063 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1066 /* higher distance loses */
1067 if (alternate
->distance
< current
->distance
)
1069 if (current
->distance
< alternate
->distance
)
1072 /* metric tie-breaks equal distance */
1073 if (alternate
->metric
<= current
->metric
)
1079 /* Core function for processing routing information base. */
1080 static void rib_process(struct route_node
*rn
)
1082 struct route_entry
*re
;
1083 struct route_entry
*next
;
1084 struct route_entry
*old_selected
= NULL
;
1085 struct route_entry
*new_selected
= NULL
;
1086 struct route_entry
*old_fib
= NULL
;
1087 struct route_entry
*new_fib
= NULL
;
1088 struct route_entry
*best
= NULL
;
1089 char buf
[SRCDEST2STR_BUFFER
];
1091 struct zebra_vrf
*zvrf
= NULL
;
1092 const struct prefix
*p
, *src_p
;
1094 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1095 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1099 dest
= rib_dest_from_rnode(rn
);
1101 zvrf
= rib_dest_vrf(dest
);
1102 vrf_id
= zvrf_id(zvrf
);
1105 if (IS_ZEBRA_DEBUG_RIB
)
1106 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1108 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1109 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1112 * we can have rn's that have a NULL info pointer
1113 * (dest). As such let's not let the deref happen
1114 * additionally we know RNODE_FOREACH_RE_SAFE
1115 * will not iterate so we are ok.
1118 old_fib
= dest
->selected_fib
;
1120 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1121 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1123 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1124 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1125 re
->status
, re
->flags
, re
->distance
,
1128 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1130 /* Currently selected re. */
1131 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1132 assert(old_selected
== NULL
);
1136 /* Skip deleted entries from selection */
1137 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1140 /* Skip unreachable nexthop. */
1141 /* This first call to nexthop_active_update is merely to
1142 * determine if there's any change to nexthops associated
1143 * with this RIB entry. Now, rib_process() can be invoked due
1144 * to an external event such as link down or due to
1145 * next-hop-tracking evaluation. In the latter case,
1146 * a decision has already been made that the NHs have changed.
1147 * So, no need to invoke a potentially expensive call again.
1148 * Further, since the change might be in a recursive NH which
1149 * is not caught in the nexthop_active_update() code. Thus, we
1150 * might miss changes to recursive NHs.
1152 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1153 && !nexthop_active_update(rn
, re
)) {
1154 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1155 /* XXX: HERE BE DRAGONS!!!!!
1156 * In all honesty, I have not yet figured out
1157 * what this part does or why the
1158 * ROUTE_ENTRY_CHANGED test above is correct
1159 * or why we need to delete a route here, and
1160 * also not whether this concerns both selected
1161 * and fib route, or only selected
1164 * This entry was denied by the 'ip protocol
1165 * table' route-map, we need to delete it */
1166 if (re
!= old_selected
) {
1167 if (IS_ZEBRA_DEBUG_RIB
)
1169 "%s: %u:%s: imported via import-table but denied "
1170 "by the ip protocol table route-map",
1171 __func__
, vrf_id
, buf
);
1174 SET_FLAG(re
->status
,
1175 ROUTE_ENTRY_REMOVED
);
1181 /* Infinite distance. */
1182 if (re
->distance
== DISTANCE_INFINITY
) {
1183 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1187 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1188 best
= rib_choose_best(new_fib
, re
);
1189 if (new_fib
&& best
!= new_fib
)
1190 UNSET_FLAG(new_fib
->status
,
1191 ROUTE_ENTRY_CHANGED
);
1194 best
= rib_choose_best(new_selected
, re
);
1195 if (new_selected
&& best
!= new_selected
)
1196 UNSET_FLAG(new_selected
->status
,
1197 ROUTE_ENTRY_CHANGED
);
1198 new_selected
= best
;
1201 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1202 } /* RNODE_FOREACH_RE */
1204 /* If no FIB override route, use the selected route also for FIB */
1205 if (new_fib
== NULL
)
1206 new_fib
= new_selected
;
1208 /* After the cycle is finished, the following pointers will be set:
1209 * old_selected --- RE entry currently having SELECTED
1210 * new_selected --- RE entry that is newly SELECTED
1211 * old_fib --- RE entry currently in kernel FIB
1212 * new_fib --- RE entry that is newly to be in kernel FIB
1214 * new_selected will get SELECTED flag, and is going to be redistributed
1215 * the zclients. new_fib (which can be new_selected) will be installed
1219 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1221 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1222 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1223 (void *)old_fib
, (void *)new_fib
);
1226 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1227 * fib == selected */
1228 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1229 ROUTE_ENTRY_CHANGED
);
1231 /* Update fib according to selection results */
1232 if (new_fib
&& old_fib
)
1233 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1235 rib_process_add_fib(zvrf
, rn
, new_fib
);
1237 rib_process_del_fib(zvrf
, rn
, old_fib
);
1239 /* Update SELECTED entry */
1240 if (old_selected
!= new_selected
|| selected_changed
) {
1242 if (new_selected
&& new_selected
!= new_fib
)
1243 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1246 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1250 redistribute_delete(p
, src_p
, old_selected
);
1251 if (old_selected
!= new_selected
)
1252 UNSET_FLAG(old_selected
->flags
,
1253 ZEBRA_FLAG_SELECTED
);
1257 /* Remove all RE entries queued for removal */
1258 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1259 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1260 if (IS_ZEBRA_DEBUG_RIB
) {
1261 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1262 (void *)rn
, (void *)re
);
1269 * Check if the dest can be deleted now.
1274 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1276 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1277 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1282 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1283 if (IS_ZEBRA_DEBUG_MPLS
)
1285 "%u: Scheduling all LSPs upon RIB completion",
1287 zebra_mpls_lsp_schedule(zvrf
);
1288 mpls_unmark_lsps_for_processing(rn
);
1293 * Utility to match route with dplane context data
1295 static bool rib_route_match_ctx(const struct route_entry
*re
,
1296 const struct zebra_dplane_ctx
*ctx
,
1299 bool result
= false;
1303 * In 'update' case, we test info about the 'previous' or
1306 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1307 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1310 /* TODO -- we're using this extra test, but it's not
1311 * exactly clear why.
1313 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1314 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1315 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1322 * Ordinary, single-route case using primary context info
1324 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1325 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1326 /* Skip route that's been deleted */
1330 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1331 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1334 /* TODO -- we're using this extra test, but it's not
1335 * exactly clear why.
1337 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1338 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1339 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1350 static void zebra_rib_fixup_system(struct route_node
*rn
)
1352 struct route_entry
*re
;
1354 RNODE_FOREACH_RE(rn
, re
) {
1355 struct nexthop
*nhop
;
1357 if (!RIB_SYSTEM_ROUTE(re
))
1360 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1363 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1365 for (ALL_NEXTHOPS(re
->ng
, nhop
)) {
1366 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1369 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1375 * Update a route from a dplane context. This consolidates common code
1376 * that can be used in processing of results from FIB updates, and in
1377 * async notification processing.
1378 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1380 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1381 struct route_node
*rn
,
1382 struct zebra_dplane_ctx
*ctx
)
1384 char dest_str
[PREFIX_STRLEN
] = "";
1385 char nh_str
[NEXTHOP_STRLEN
];
1386 struct nexthop
*nexthop
, *ctx_nexthop
;
1388 const struct nexthop_group
*ctxnhg
;
1389 bool is_selected
= false; /* Is 're' currently the selected re? */
1390 bool changed_p
= false; /* Change to nexthops? */
1393 /* Note well: only capturing the prefix string if debug is enabled here;
1394 * unconditional log messages will have to generate the string.
1396 if (IS_ZEBRA_DEBUG_RIB
)
1397 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1399 dest
= rib_dest_from_rnode(rn
);
1401 is_selected
= (re
== dest
->selected_fib
);
1403 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1404 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1405 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1407 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1408 * If the installed set differs from the set requested by the rib/owner,
1409 * we use the fib-specific nexthop-group to record the actual FIB
1414 * First check the fib nexthop-group, if it's present. The comparison
1415 * here is quite strict: we require that the fib sets match exactly.
1419 if (re
->fib_ng
.nexthop
== NULL
)
1424 /* First check the route's fib nexthops */
1425 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1427 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1431 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
),
1433 if (nexthop_same(ctx_nexthop
, nexthop
))
1437 if (ctx_nexthop
== NULL
) {
1438 /* Nexthop not in the new installed set */
1439 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1440 nexthop2str(nexthop
, nh_str
,
1442 zlog_debug("update_from_ctx: no match for fib nh %s",
1454 /* Check the new installed set */
1456 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1458 if (CHECK_FLAG(ctx_nexthop
->flags
,
1459 NEXTHOP_FLAG_RECURSIVE
))
1462 /* Compare with the current group's nexthops */
1464 for (ALL_NEXTHOPS(re
->fib_ng
, nexthop
)) {
1465 if (nexthop_same(nexthop
, ctx_nexthop
))
1469 if (nexthop
== NULL
) {
1470 /* Nexthop not in the old installed set */
1471 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1472 nexthop2str(ctx_nexthop
, nh_str
,
1474 zlog_debug("update_from_ctx: no fib match for notif nh %s",
1484 /* If the new FIB set matches the existing FIB set, we're done. */
1486 if (IS_ZEBRA_DEBUG_RIB
)
1487 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1488 re
->vrf_id
, dest_str
);
1491 } else if (re
->fib_ng
.nexthop
) {
1493 * Free stale fib list and move on to check the rib nhg.
1495 if (IS_ZEBRA_DEBUG_RIB
)
1496 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1497 re
->vrf_id
, dest_str
);
1498 nexthops_free(re
->fib_ng
.nexthop
);
1499 re
->fib_ng
.nexthop
= NULL
;
1501 /* Note that the installed nexthops have changed */
1504 if (IS_ZEBRA_DEBUG_RIB
)
1505 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1506 re
->vrf_id
, dest_str
);
1510 * Compare with the rib nexthop group. The comparison here is different:
1511 * the RIB group may be a superset of the list installed in the FIB. We
1512 * walk the RIB group, looking for the 'installable' candidate
1513 * nexthops, and then check those against the set
1514 * that is actually installed.
1517 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1519 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1522 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1525 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1527 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), ctx_nexthop
)) {
1528 if (nexthop_same(ctx_nexthop
, nexthop
))
1532 /* If the FIB doesn't know about the nexthop,
1533 * it's not installed
1535 if (ctx_nexthop
== NULL
) {
1536 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1537 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1538 zlog_debug("update_from_ctx: no notif match for rib nh %s",
1543 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1546 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1550 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1551 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1554 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1556 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1559 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1563 /* If all nexthops were processed, we're done */
1565 if (IS_ZEBRA_DEBUG_RIB
)
1566 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1567 re
->vrf_id
, dest_str
,
1568 (changed_p
? "true" : "false"));
1572 /* FIB nexthop set differs from the RIB set:
1573 * create a fib-specific nexthop-group
1575 if (IS_ZEBRA_DEBUG_RIB
)
1576 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1577 re
->vrf_id
, dest_str
,
1578 (changed_p
? "true" : "false"));
1580 ctxnhg
= dplane_ctx_get_ng(ctx
);
1582 if (ctxnhg
->nexthop
)
1583 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1585 /* Bit of a special case when the fib has _no_ installed
1588 nexthop
= nexthop_new();
1589 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1590 nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1598 * Helper to locate a zebra route-node from a dplane context. This is used
1599 * when processing dplane results, e.g. Note well: the route-node is returned
1600 * with a ref held - route_unlock_node() must be called eventually.
1602 static struct route_node
*
1603 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1605 struct route_table
*table
= NULL
;
1606 struct route_node
*rn
= NULL
;
1607 const struct prefix
*dest_pfx
, *src_pfx
;
1609 /* Locate rn and re(s) from ctx */
1611 table
= zebra_vrf_table_with_table_id(dplane_ctx_get_afi(ctx
),
1612 dplane_ctx_get_safi(ctx
),
1613 dplane_ctx_get_vrf(ctx
),
1614 dplane_ctx_get_table(ctx
));
1615 if (table
== NULL
) {
1616 if (IS_ZEBRA_DEBUG_DPLANE
) {
1617 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1618 dplane_ctx_get_afi(ctx
),
1619 dplane_ctx_get_safi(ctx
),
1620 dplane_ctx_get_vrf(ctx
));
1625 dest_pfx
= dplane_ctx_get_dest(ctx
);
1626 src_pfx
= dplane_ctx_get_src(ctx
);
1628 rn
= srcdest_rnode_get(table
, dest_pfx
,
1629 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1638 * Route-update results processing after async dataplane update.
1640 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1642 struct zebra_vrf
*zvrf
= NULL
;
1643 struct route_node
*rn
= NULL
;
1644 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1645 bool is_update
= false;
1646 char dest_str
[PREFIX_STRLEN
] = "";
1647 enum dplane_op_e op
;
1648 enum zebra_dplane_result status
;
1649 const struct prefix
*dest_pfx
, *src_pfx
;
1651 bool fib_changed
= false;
1653 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1654 dest_pfx
= dplane_ctx_get_dest(ctx
);
1656 /* Note well: only capturing the prefix string if debug is enabled here;
1657 * unconditional log messages will have to generate the string.
1659 if (IS_ZEBRA_DEBUG_DPLANE
)
1660 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1662 /* Locate rn and re(s) from ctx */
1663 rn
= rib_find_rn_from_ctx(ctx
);
1665 if (IS_ZEBRA_DEBUG_DPLANE
) {
1666 zlog_debug("Failed to process dplane results: no route for %u:%s",
1667 dplane_ctx_get_vrf(ctx
), dest_str
);
1672 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1674 op
= dplane_ctx_get_op(ctx
);
1675 status
= dplane_ctx_get_status(ctx
);
1677 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1678 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1679 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1680 dplane_op2str(op
), dplane_res2str(status
));
1683 * Update is a bit of a special case, where we may have both old and new
1684 * routes to post-process.
1686 is_update
= dplane_ctx_is_update(ctx
);
1689 * Take a pass through the routes, look for matches with the context
1692 RNODE_FOREACH_RE(rn
, rib
) {
1695 if (rib_route_match_ctx(rib
, ctx
, false))
1699 /* Check for old route match */
1700 if (is_update
&& (old_re
== NULL
)) {
1701 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1705 /* Have we found the routes we need to work on? */
1706 if (re
&& ((!is_update
|| old_re
)))
1710 seq
= dplane_ctx_get_seq(ctx
);
1713 * Check sequence number(s) to detect stale results before continuing
1716 if (re
->dplane_sequence
!= seq
) {
1717 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1718 zlog_debug("%u:%s Stale dplane result for re %p",
1719 dplane_ctx_get_vrf(ctx
),
1722 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1726 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1727 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1728 zlog_debug("%u:%s Stale dplane result for old_re %p",
1729 dplane_ctx_get_vrf(ctx
),
1732 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1736 case DPLANE_OP_ROUTE_INSTALL
:
1737 case DPLANE_OP_ROUTE_UPDATE
:
1738 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1740 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1741 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1744 * On an update operation from the same route type
1745 * context retrieval currently has no way to know
1746 * which was the old and which was the new.
1747 * So don't unset our flags that we just set.
1748 * We know redistribution is ok because the
1749 * old_re in this case is used for nothing
1750 * more than knowing whom to contact if necessary.
1752 if (old_re
&& old_re
!= re
) {
1753 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1754 UNSET_FLAG(old_re
->status
,
1755 ROUTE_ENTRY_INSTALLED
);
1758 /* Update zebra route based on the results in
1759 * the context struct.
1763 rib_update_re_from_ctx(re
, rn
, ctx
);
1766 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1767 zlog_debug("%u:%s no fib change for re",
1774 redistribute_update(dest_pfx
, src_pfx
,
1779 * System routes are weird in that they
1780 * allow multiple to be installed that match
1781 * to the same prefix, so after we get the
1782 * result we need to clean them up so that
1783 * we can actually use them.
1785 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1786 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1787 zebra_rib_fixup_system(rn
);
1792 /* Notify route owner */
1793 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1797 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1798 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1800 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1802 zsend_route_notify_owner(re
, dest_pfx
,
1803 ZAPI_ROUTE_FAIL_INSTALL
);
1805 zlog_warn("%u:%s: Route install failed",
1806 dplane_ctx_get_vrf(ctx
),
1807 prefix2str(dest_pfx
,
1808 dest_str
, sizeof(dest_str
)));
1811 case DPLANE_OP_ROUTE_DELETE
:
1813 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1815 * In the delete case, the zebra core datastructs were
1816 * updated (or removed) at the time the delete was issued,
1817 * so we're just notifying the route owner.
1819 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1821 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1822 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1824 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1830 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1831 zsend_route_notify_owner_ctx(ctx
,
1832 ZAPI_ROUTE_REMOVE_FAIL
);
1834 zlog_warn("%u:%s: Route Deletion failure",
1835 dplane_ctx_get_vrf(ctx
),
1836 prefix2str(dest_pfx
,
1837 dest_str
, sizeof(dest_str
)));
1841 * System routes are weird in that they
1842 * allow multiple to be installed that match
1843 * to the same prefix, so after we get the
1844 * result we need to clean them up so that
1845 * we can actually use them.
1847 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1848 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1849 zebra_rib_fixup_system(rn
);
1855 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1856 zebra_rib_evaluate_mpls(rn
);
1860 route_unlock_node(rn
);
1862 /* Return context to dataplane module */
1863 dplane_ctx_fini(&ctx
);
1867 * Handle notification from async dataplane: the dataplane has detected
1868 * some change to a route, and notifies zebra so that the control plane
1869 * can reflect that change.
1871 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1873 struct route_node
*rn
= NULL
;
1874 struct route_entry
*re
= NULL
;
1875 struct nexthop
*nexthop
;
1876 char dest_str
[PREFIX_STRLEN
] = "";
1877 const struct prefix
*dest_pfx
, *src_pfx
;
1879 bool fib_changed
= false;
1880 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1881 int start_count
, end_count
;
1882 dest_pfx
= dplane_ctx_get_dest(ctx
);
1884 /* Note well: only capturing the prefix string if debug is enabled here;
1885 * unconditional log messages will have to generate the string.
1888 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1890 /* Locate rn and re(s) from ctx */
1891 rn
= rib_find_rn_from_ctx(ctx
);
1894 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1895 dplane_ctx_get_vrf(ctx
), dest_str
);
1900 dest
= rib_dest_from_rnode(rn
);
1901 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1904 zlog_debug("%u:%s Processing dplane notif ctx %p",
1905 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1908 * Take a pass through the routes, look for matches with the context
1911 RNODE_FOREACH_RE(rn
, re
) {
1912 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1916 /* No match? Nothing we can do */
1919 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1920 dplane_ctx_get_vrf(ctx
), dest_str
,
1922 dplane_ctx_get_type(ctx
)));
1927 /* Is this a notification that ... matters? We only really care about
1928 * the route that is currently selected for installation.
1930 if (re
!= dest
->selected_fib
) {
1931 /* TODO -- don't skip processing entirely? We might like to
1932 * at least report on the event.
1935 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1936 dplane_ctx_get_vrf(ctx
), dest_str
,
1938 dplane_ctx_get_type(ctx
)));
1942 /* We'll want to determine whether the installation status of the
1943 * route has changed: we'll check the status before processing,
1944 * and then again if there's been a change.
1947 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1948 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1952 /* Update zebra's nexthop FIB flags based on the context struct's
1955 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1959 zlog_debug("%u:%s No change from dplane notification",
1960 dplane_ctx_get_vrf(ctx
), dest_str
);
1966 * Perform follow-up work if the actual status of the prefix
1971 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1972 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1976 /* Various fib transitions: changed nexthops; from installed to
1977 * not-installed; or not-installed to installed.
1979 if (start_count
> 0 && end_count
> 0) {
1981 /* Changed nexthops - update kernel/others */
1982 dplane_route_notif_update(rn
, re
,
1983 DPLANE_OP_ROUTE_UPDATE
, ctx
);
1985 } else if (start_count
== 0 && end_count
> 0) {
1987 zlog_debug("%u:%s installed transition from dplane notification",
1988 dplane_ctx_get_vrf(ctx
), dest_str
);
1990 /* We expect this to be the selected route, so we want
1991 * to tell others about this transistion.
1993 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1995 /* Changed nexthops - update kernel/others */
1996 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
1998 /* Redistribute, lsp, and nht update */
1999 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
2001 zebra_rib_evaluate_rn_nexthops(
2002 rn
, zebra_router_get_next_sequence());
2004 zebra_rib_evaluate_mpls(rn
);
2006 } else if (start_count
> 0 && end_count
== 0) {
2008 zlog_debug("%u:%s un-installed transition from dplane notification",
2009 dplane_ctx_get_vrf(ctx
), dest_str
);
2011 /* Transition from _something_ installed to _nothing_
2014 /* We expect this to be the selected route, so we want
2015 * to tell others about this transistion.
2017 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
2019 /* Changed nexthops - update kernel/others */
2020 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
2022 /* Redistribute, lsp, and nht update */
2023 redistribute_delete(dest_pfx
, src_pfx
, re
);
2025 zebra_rib_evaluate_rn_nexthops(
2026 rn
, zebra_router_get_next_sequence());
2028 zebra_rib_evaluate_mpls(rn
);
2033 route_unlock_node(rn
);
2035 /* Return context to dataplane module */
2036 dplane_ctx_fini(&ctx
);
2039 /* Take a list of route_node structs and return 1, if there was a record
2040 * picked from it and processed by rib_process(). Don't process more,
2041 * than one RN record; operate only in the specified sub-queue.
2043 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
2045 struct listnode
*lnode
= listhead(subq
);
2046 struct route_node
*rnode
;
2048 struct zebra_vrf
*zvrf
= NULL
;
2053 rnode
= listgetdata(lnode
);
2054 dest
= rib_dest_from_rnode(rnode
);
2056 zvrf
= rib_dest_vrf(dest
);
2060 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
2061 char buf
[SRCDEST2STR_BUFFER
];
2063 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
2064 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
2065 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
2069 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
2070 RIB_ROUTE_QUEUED(qindex
));
2075 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2076 __func__
, rnode
, rnode
->lock
);
2077 zlog_backtrace(LOG_DEBUG
);
2080 route_unlock_node(rnode
);
2081 list_delete_node(subq
, lnode
);
2087 * Perform next-hop tracking processing after RIB updates.
2089 static void do_nht_processing(void)
2093 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2094 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2096 * is pointed to the meta queue structure.
2098 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2100 struct meta_queue
*mq
= data
;
2102 uint32_t queue_len
, queue_limit
;
2104 /* Ensure there's room for more dataplane updates */
2105 queue_limit
= dplane_get_in_queue_limit();
2106 queue_len
= dplane_get_in_queue_len();
2107 if (queue_len
> queue_limit
) {
2108 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2109 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2110 queue_len
, queue_limit
);
2112 /* Ensure that the meta-queue is actually enqueued */
2113 if (work_queue_empty(zrouter
.ribq
))
2114 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2116 return WQ_QUEUE_BLOCKED
;
2119 for (i
= 0; i
< MQ_SIZE
; i
++)
2120 if (process_subq(mq
->subq
[i
], i
)) {
2124 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2129 * Look into the RN and queue it into the highest priority queue
2130 * at this point in time for processing.
2132 * We will enqueue a route node only once per invocation.
2134 * There are two possibilities here that should be kept in mind.
2135 * If the original invocation has not been pulled off for processing
2136 * yet, A subsuquent invocation can have a route entry with a better
2137 * meta queue index value and we can have a situation where
2138 * we might have the same node enqueued 2 times. Not necessarily
2139 * an optimal situation but it should be ok.
2141 * The other possibility is that the original invocation has not
2142 * been pulled off for processing yet, A subsusquent invocation
2143 * doesn't have a route_entry with a better meta-queue and the
2144 * original metaqueue index value will win and we'll end up with
2145 * the route node enqueued once.
2147 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
2149 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2150 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2152 RNODE_FOREACH_RE (rn
, curr_re
) {
2153 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2155 if (curr_qindex
<= qindex
) {
2157 qindex
= curr_qindex
;
2164 /* Invariant: at this point we always have rn->info set. */
2165 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2166 RIB_ROUTE_QUEUED(qindex
))) {
2167 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2168 rnode_debug(rn
, re
->vrf_id
,
2169 "rn %p is already queued in sub-queue %u",
2170 (void *)rn
, qindex
);
2174 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2175 listnode_add(mq
->subq
[qindex
], rn
);
2176 route_lock_node(rn
);
2179 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2180 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2181 (void *)rn
, qindex
);
2184 /* Add route_node to work queue and schedule processing */
2185 void rib_queue_add(struct route_node
*rn
)
2189 /* Pointless to queue a route_node with no RIB entries to add or remove
2191 if (!rnode_to_ribs(rn
)) {
2192 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2193 __func__
, (void *)rn
, rn
->lock
);
2194 zlog_backtrace(LOG_DEBUG
);
2198 if (zrouter
.ribq
== NULL
) {
2199 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2200 "%s: work_queue does not exist!", __func__
);
2205 * The RIB queue should normally be either empty or holding the only
2206 * work_queue_item element. In the latter case this element would
2207 * hold a pointer to the meta queue structure, which must be used to
2208 * actually queue the route nodes to process. So create the MQ
2209 * holder, if necessary, then push the work into it in any case.
2210 * This semantics was introduced after 0.99.9 release.
2212 if (work_queue_empty(zrouter
.ribq
))
2213 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2215 rib_meta_queue_add(zrouter
.mq
, rn
);
2220 /* Create new meta queue.
2221 A destructor function doesn't seem to be necessary here.
2223 static struct meta_queue
*meta_queue_new(void)
2225 struct meta_queue
*new;
2228 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2230 for (i
= 0; i
< MQ_SIZE
; i
++) {
2231 new->subq
[i
] = list_new();
2232 assert(new->subq
[i
]);
2238 void meta_queue_free(struct meta_queue
*mq
)
2242 for (i
= 0; i
< MQ_SIZE
; i
++)
2243 list_delete(&mq
->subq
[i
]);
2245 XFREE(MTYPE_WORK_QUEUE
, mq
);
2248 /* initialise zebra rib work queue */
2249 static void rib_queue_init(void)
2251 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2252 "route_node processing"))) {
2253 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2254 "%s: could not initialise work queue!", __func__
);
2258 /* fill in the work queue spec */
2259 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2260 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2261 zrouter
.ribq
->spec
.completion_func
= NULL
;
2262 /* XXX: TODO: These should be runtime configurable via vty */
2263 zrouter
.ribq
->spec
.max_retries
= 3;
2264 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2265 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2267 if (!(zrouter
.mq
= meta_queue_new())) {
2268 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2269 "%s: could not initialise meta queue!", __func__
);
2275 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2279 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2280 rnh_list_init(&dest
->nht
);
2281 route_lock_node(rn
); /* rn route table reference */
2288 /* RIB updates are processed via a queue of pointers to route_nodes.
2290 * The queue length is bounded by the maximal size of the routing table,
2291 * as a route_node will not be requeued, if already queued.
2293 * REs are submitted via rib_addnode or rib_delnode which set minimal
2294 * state, or static_install_route (when an existing RE is updated)
2295 * and then submit route_node to queue for best-path selection later.
2296 * Order of add/delete state changes are preserved for any given RE.
2298 * Deleted REs are reaped during best-path selection.
2301 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2302 * |-------->| | best RE, if required
2304 * static_install->|->rib_addqueue...... -> rib_process
2306 * |-------->| |-> rib_unlink
2307 * |-> set ROUTE_ENTRY_REMOVE |
2308 * rib_delnode (RE freed)
2310 * The 'info' pointer of a route_node points to a rib_dest_t
2311 * ('dest'). Queueing state for a route_node is kept on the dest. The
2312 * dest is created on-demand by rib_link() and is kept around at least
2313 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2315 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2317 * - route_nodes: refcounted by:
2318 * - dest attached to route_node:
2319 * - managed by: rib_link/rib_gc_dest
2320 * - route_node processing queue
2321 * - managed by: rib_addqueue, rib_process.
2325 /* Add RE to head of the route node. */
2326 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2330 const char *rmap_name
;
2334 dest
= rib_dest_from_rnode(rn
);
2336 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2337 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2339 dest
= zebra_rib_create_dest(rn
);
2342 re_list_add_head(&dest
->routes
, re
);
2344 afi
= (rn
->p
.family
== AF_INET
)
2346 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2347 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2348 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2349 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2354 static void rib_addnode(struct route_node
*rn
,
2355 struct route_entry
*re
, int process
)
2357 /* RE node has been un-removed before route-node is processed.
2358 * route_node must hence already be on the queue for processing..
2360 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2361 if (IS_ZEBRA_DEBUG_RIB
)
2362 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2363 (void *)rn
, (void *)re
);
2365 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2368 rib_link(rn
, re
, process
);
2374 * Detach a rib structure from a route_node.
2376 * Note that a call to rib_unlink() should be followed by a call to
2377 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2378 * longer required to be deleted.
2380 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2386 if (IS_ZEBRA_DEBUG_RIB
)
2387 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2390 dest
= rib_dest_from_rnode(rn
);
2392 re_list_del(&dest
->routes
, re
);
2394 if (dest
->selected_fib
== re
)
2395 dest
->selected_fib
= NULL
;
2397 nexthops_free(re
->ng
.nexthop
);
2398 nexthops_free(re
->fib_ng
.nexthop
);
2400 XFREE(MTYPE_RE
, re
);
2403 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2407 if (IS_ZEBRA_DEBUG_RIB
)
2408 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2409 (void *)rn
, (void *)re
);
2410 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2412 afi
= (rn
->p
.family
== AF_INET
)
2414 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2415 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2416 zebra_del_import_table_entry(rn
, re
);
2417 /* Just clean up if non main table */
2418 if (IS_ZEBRA_DEBUG_RIB
) {
2419 char buf
[SRCDEST2STR_BUFFER
];
2420 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2421 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2422 re
->vrf_id
, buf
, rn
, re
,
2423 zebra_route_string(re
->type
));
2432 /* This function dumps the contents of a given RE entry into
2433 * standard debug log. Calling function name and IP prefix in
2434 * question are passed as 1st and 2nd arguments.
2437 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2438 union prefixconstptr src_pp
,
2439 const struct route_entry
*re
)
2441 const struct prefix
*src_p
= src_pp
.p
;
2442 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2443 char straddr
[PREFIX_STRLEN
];
2444 char srcaddr
[PREFIX_STRLEN
];
2445 struct nexthop
*nexthop
;
2447 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2448 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2449 is_srcdst
? " from " : "",
2450 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2453 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2454 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2457 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2458 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2459 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2460 re
->nexthop_num
, re
->nexthop_active_num
);
2462 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2463 struct interface
*ifp
;
2464 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2466 switch (nexthop
->type
) {
2467 case NEXTHOP_TYPE_BLACKHOLE
:
2468 sprintf(straddr
, "Blackhole");
2470 case NEXTHOP_TYPE_IFINDEX
:
2471 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2473 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2475 case NEXTHOP_TYPE_IPV4
:
2477 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2478 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2481 case NEXTHOP_TYPE_IPV6
:
2482 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2483 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2487 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s%s",
2488 func
, (nexthop
->rparent
? " NH" : "NH"), straddr
,
2489 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2491 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2494 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2497 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2500 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2503 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_MATCHED
)
2506 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2510 zlog_debug("%s: dump complete", func
);
2513 /* This is an exported helper to rtm_read() to dump the strange
2514 * RE entry found by rib_lookup_ipv4_route()
2517 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2519 struct route_table
*table
;
2520 struct route_node
*rn
;
2521 struct route_entry
*re
;
2522 char prefix_buf
[INET_ADDRSTRLEN
];
2525 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2527 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2528 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2533 /* Scan the RIB table for exactly matching RE entry. */
2534 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2536 /* No route for this prefix. */
2538 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2539 prefix2str((struct prefix
*)p
, prefix_buf
,
2540 sizeof(prefix_buf
)));
2545 route_unlock_node(rn
);
2548 RNODE_FOREACH_RE (rn
, re
) {
2549 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2551 (void *)rn
, (void *)re
,
2552 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2555 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2558 route_entry_dump(p
, NULL
, re
);
2562 /* Check if requested address assignment will fail due to another
2563 * route being installed by zebra in FIB already. Take necessary
2564 * actions, if needed: remove such a route from FIB and deSELECT
2565 * corresponding RE entry. Then put affected RN into RIBQ head.
2567 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2569 struct route_table
*table
;
2570 struct route_node
*rn
;
2573 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2574 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2575 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2580 /* No matches would be the simplest case. */
2581 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2585 route_unlock_node(rn
);
2587 dest
= rib_dest_from_rnode(rn
);
2588 /* Check all RE entries. In case any changes have to be done, requeue
2589 * the RN into RIBQ head. If the routing message about the new connected
2590 * route (generated by the IP address we are going to assign very soon)
2591 * comes before the RIBQ is processed, the new RE entry will join
2592 * RIBQ record already on head. This is necessary for proper
2594 * of the rest of the RE.
2596 if (dest
->selected_fib
) {
2597 if (IS_ZEBRA_DEBUG_RIB
) {
2598 char buf
[PREFIX_STRLEN
];
2600 zlog_debug("%u:%s: freeing way for connected prefix",
2601 dest
->selected_fib
->vrf_id
,
2602 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2603 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2605 rib_uninstall(rn
, dest
->selected_fib
);
2610 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2611 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2613 struct route_table
*table
;
2614 struct route_node
*rn
;
2615 struct route_entry
*same
= NULL
;
2621 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2624 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2626 XFREE(MTYPE_RE
, re
);
2630 /* Make it sure prefixlen is applied to the prefix. */
2633 apply_mask_ipv6(src_p
);
2635 /* Set default distance by route type. */
2636 if (re
->distance
== 0)
2637 re
->distance
= route_distance(re
->type
);
2639 /* Lookup route node.*/
2640 rn
= srcdest_rnode_get(table
, p
, src_p
);
2643 * If same type of route are installed, treat it as a implicit
2645 * If the user has specified the No route replace semantics
2646 * for the install don't do a route replace.
2648 RNODE_FOREACH_RE (rn
, same
) {
2649 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2652 if (same
->type
!= re
->type
)
2654 if (same
->instance
!= re
->instance
)
2656 if (same
->type
== ZEBRA_ROUTE_KERNEL
2657 && same
->metric
!= re
->metric
)
2660 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2661 same
->distance
!= re
->distance
)
2665 * We should allow duplicate connected routes
2666 * because of IPv6 link-local routes and unnumbered
2667 * interfaces on Linux.
2669 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2673 /* If this route is kernel/connected route, notify the dataplane. */
2674 if (RIB_SYSTEM_ROUTE(re
)) {
2675 /* Notify dataplane */
2676 dplane_sys_route_add(rn
, re
);
2679 /* Link new re to node.*/
2680 if (IS_ZEBRA_DEBUG_RIB
) {
2681 rnode_debug(rn
, re
->vrf_id
,
2682 "Inserting route rn %p, re %p (%s) existing %p",
2683 rn
, re
, zebra_route_string(re
->type
), same
);
2685 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2686 route_entry_dump(p
, src_p
, re
);
2689 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2690 rib_addnode(rn
, re
, 1);
2693 /* Free implicit route.*/
2695 rib_delnode(rn
, same
);
2699 route_unlock_node(rn
);
2703 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2704 unsigned short instance
, int flags
, struct prefix
*p
,
2705 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2706 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2709 struct route_table
*table
;
2710 struct route_node
*rn
;
2711 struct route_entry
*re
;
2712 struct route_entry
*fib
= NULL
;
2713 struct route_entry
*same
= NULL
;
2714 struct nexthop
*rtnh
;
2715 char buf2
[INET6_ADDRSTRLEN
];
2718 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2721 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2728 apply_mask_ipv6(src_p
);
2730 /* Lookup route node. */
2731 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2733 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2735 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2736 if (src_p
&& src_p
->prefixlen
)
2737 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2741 if (IS_ZEBRA_DEBUG_RIB
)
2742 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2744 (src_buf
[0] != '\0') ? " from " : "",
2749 dest
= rib_dest_from_rnode(rn
);
2750 fib
= dest
->selected_fib
;
2752 /* Lookup same type route. */
2753 RNODE_FOREACH_RE (rn
, re
) {
2754 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2757 if (re
->type
!= type
)
2759 if (re
->instance
!= instance
)
2761 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2762 distance
!= re
->distance
)
2765 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2767 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2768 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2769 if (rtnh
->ifindex
!= nh
->ifindex
)
2774 /* Make sure that the route found has the same gateway. */
2780 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2782 * No guarantee all kernel send nh with labels
2785 if (nexthop_same_no_labels(rtnh
, nh
)) {
2793 /* If same type of route can't be found and this message is from
2797 * In the past(HA!) we could get here because
2798 * we were receiving a route delete from the
2799 * kernel and we're not marking the proto
2800 * as coming from it's appropriate originator.
2801 * Now that we are properly noticing the fact
2802 * that the kernel has deleted our route we
2803 * are not going to get called in this path
2804 * I am going to leave this here because
2805 * this might still work this way on non-linux
2806 * platforms as well as some weird state I have
2807 * not properly thought of yet.
2808 * If we can show that this code path is
2809 * dead then we can remove it.
2811 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2812 if (IS_ZEBRA_DEBUG_RIB
) {
2813 rnode_debug(rn
, vrf_id
,
2814 "rn %p, re %p (%s) was deleted from kernel, adding",
2816 zebra_route_string(fib
->type
));
2819 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2821 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2823 UNSET_FLAG(rtnh
->flags
,
2827 * This is a non FRR route
2828 * as such we should mark
2831 dest
->selected_fib
= NULL
;
2833 /* This means someone else, other than Zebra,
2835 * a Zebra router from the kernel. We will add
2837 rib_install_kernel(rn
, fib
, NULL
);
2840 if (IS_ZEBRA_DEBUG_RIB
) {
2844 "via %s ifindex %d type %d "
2845 "doesn't exist in rib",
2846 inet_ntop(afi2family(afi
),
2853 "type %d doesn't exist in rib",
2856 route_unlock_node(rn
);
2862 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2864 rib_install_kernel(rn
, same
, NULL
);
2865 route_unlock_node(rn
);
2870 /* Special handling for IPv4 or IPv6 routes sourced from
2871 * EVPN - the nexthop (and associated MAC) need to be
2872 * uninstalled if no more refs.
2874 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2875 struct nexthop
*tmp_nh
;
2877 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2878 struct ipaddr vtep_ip
;
2880 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2881 if (afi
== AFI_IP
) {
2882 vtep_ip
.ipa_type
= IPADDR_V4
;
2883 memcpy(&(vtep_ip
.ipaddr_v4
),
2884 &(tmp_nh
->gate
.ipv4
),
2885 sizeof(struct in_addr
));
2887 vtep_ip
.ipa_type
= IPADDR_V6
;
2888 memcpy(&(vtep_ip
.ipaddr_v6
),
2889 &(tmp_nh
->gate
.ipv6
),
2890 sizeof(struct in6_addr
));
2892 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2897 /* Notify dplane if system route changes */
2898 if (RIB_SYSTEM_ROUTE(re
))
2899 dplane_sys_route_del(rn
, same
);
2901 rib_delnode(rn
, same
);
2904 route_unlock_node(rn
);
2909 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2910 unsigned short instance
, int flags
, struct prefix
*p
,
2911 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2912 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2915 struct route_entry
*re
;
2916 struct nexthop
*nexthop
;
2918 /* Allocate new route_entry structure. */
2919 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2921 re
->instance
= instance
;
2922 re
->distance
= distance
;
2924 re
->metric
= metric
;
2926 re
->table
= table_id
;
2927 re
->vrf_id
= vrf_id
;
2928 re
->nexthop_num
= 0;
2929 re
->uptime
= monotime(NULL
);
2933 nexthop
= nexthop_new();
2935 route_entry_nexthop_add(re
, nexthop
);
2937 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2940 /* Schedule routes of a particular table (address-family) based on event. */
2941 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2943 struct route_node
*rn
;
2944 struct route_entry
*re
, *next
;
2946 /* Walk all routes and queue for processing, if appropriate for
2947 * the trigger event.
2949 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2951 * If we are looking at a route node and the node
2952 * has already been queued we don't
2953 * need to queue it up again
2955 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2956 RIB_ROUTE_ANY_QUEUED
))
2959 case RIB_UPDATE_IF_CHANGE
:
2960 /* Examine all routes that won't get processed by the
2962 * triggered by nexthop evaluation (NHT). This would be
2964 * kernel and certain static routes. Note that NHT will
2966 * triggered upon an interface event as connected routes
2968 * get queued for processing.
2970 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2973 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
2974 && re
->type
!= ZEBRA_ROUTE_KERNEL
2975 && re
->type
!= ZEBRA_ROUTE_CONNECT
2976 && re
->type
!= ZEBRA_ROUTE_STATIC
)
2979 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2980 SET_FLAG(re
->status
,
2981 ROUTE_ENTRY_CHANGED
);
2986 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
2987 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2988 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2991 /* If we only have nexthops to a
2996 SET_FLAG(re
->status
,
2997 ROUTE_ENTRY_CHANGED
);
3003 case RIB_UPDATE_RMAP_CHANGE
:
3004 case RIB_UPDATE_OTHER
:
3005 /* Right now, examine all routes. Can restrict to a
3007 * some cases (TODO).
3009 if (rnode_to_ribs(rn
)) {
3010 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
3011 SET_FLAG(re
->status
,
3012 ROUTE_ENTRY_CHANGED
);
3023 /* RIB update function. */
3024 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
3026 struct route_table
*table
;
3028 /* Process routes of interested address-families. */
3029 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3031 if (IS_ZEBRA_DEBUG_EVENT
)
3032 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
3033 rib_update_table(table
, event
);
3036 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3038 if (IS_ZEBRA_DEBUG_EVENT
)
3039 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
3040 rib_update_table(table
, event
);
3044 /* Delete self installed routes after zebra is relaunched. */
3045 void rib_sweep_table(struct route_table
*table
)
3047 struct route_node
*rn
;
3048 struct route_entry
*re
;
3049 struct route_entry
*next
;
3050 struct nexthop
*nexthop
;
3055 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3056 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3058 if (IS_ZEBRA_DEBUG_RIB
)
3059 route_entry_dump(&rn
->p
, NULL
, re
);
3061 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3064 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3068 * If routes are older than startup_time then
3069 * we know we read them in from the kernel.
3070 * As such we can safely remove them.
3072 if (zrouter
.startup_time
< re
->uptime
)
3076 * So we are starting up and have received
3077 * routes from the kernel that we have installed
3078 * from a previous run of zebra but not cleaned
3079 * up ( say a kill -9 )
3080 * But since we haven't actually installed
3081 * them yet( we received them from the kernel )
3082 * we don't think they are active.
3083 * So let's pretend they are active to actually
3085 * In all honesty I'm not sure if we should
3086 * mark them as active when we receive them
3087 * This is startup only so probably ok.
3089 * If we ever decide to move rib_sweep_table
3090 * to a different spot (ie startup )
3091 * this decision needs to be revisited
3093 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3094 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
3095 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3097 rib_uninstall_kernel(rn
, re
);
3098 rib_delnode(rn
, re
);
3103 /* Sweep all RIB tables. */
3104 int rib_sweep_route(struct thread
*t
)
3107 struct zebra_vrf
*zvrf
;
3109 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3110 if ((zvrf
= vrf
->info
) == NULL
)
3113 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3114 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3117 zebra_router_sweep_route();
3122 /* Remove specific by protocol routes from 'table'. */
3123 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3124 struct route_table
*table
)
3126 struct route_node
*rn
;
3127 struct route_entry
*re
;
3128 struct route_entry
*next
;
3129 unsigned long n
= 0;
3132 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3133 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3134 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3136 if (re
->type
== proto
3137 && re
->instance
== instance
) {
3138 rib_delnode(rn
, re
);
3145 /* Remove specific by protocol routes. */
3146 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3149 struct zebra_vrf
*zvrf
;
3150 struct other_route_table
*ort
;
3151 unsigned long cnt
= 0;
3153 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3158 cnt
+= rib_score_proto_table(proto
, instance
,
3159 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3160 + rib_score_proto_table(
3162 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3164 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3165 rib_score_proto_table(proto
, instance
, ort
->table
);
3171 /* Close RIB and clean up kernel routes. */
3172 void rib_close_table(struct route_table
*table
)
3174 struct route_node
*rn
;
3175 rib_table_info_t
*info
;
3181 info
= route_table_get_info(table
);
3183 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3184 dest
= rib_dest_from_rnode(rn
);
3186 if (dest
&& dest
->selected_fib
) {
3187 if (info
->safi
== SAFI_UNICAST
)
3188 hook_call(rib_update
, rn
, NULL
);
3190 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3191 dest
->selected_fib
= NULL
;
3197 * Handler for async dataplane results after a pseudowire installation
3199 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3201 struct zebra_pw
*pw
;
3202 struct zebra_vrf
*vrf
;
3204 /* The pseudowire code assumes success - we act on an error
3205 * result for installation attempts here.
3207 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3210 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3211 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3212 pw
= zebra_pw_find(vrf
, dplane_ctx_get_pw_ifname(ctx
));
3214 zebra_pw_install_failure(pw
);
3224 * Handle results from the dataplane system. Dequeue update context
3225 * structs, dispatch to appropriate internal handlers.
3227 static int rib_process_dplane_results(struct thread
*thread
)
3229 struct zebra_dplane_ctx
*ctx
;
3230 struct dplane_ctx_q ctxlist
;
3232 /* Dequeue a list of completed updates with one lock/unlock cycle */
3235 TAILQ_INIT(&ctxlist
);
3237 /* Take lock controlling queue of results */
3238 pthread_mutex_lock(&dplane_mutex
);
3240 /* Dequeue list of context structs */
3241 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3243 pthread_mutex_unlock(&dplane_mutex
);
3245 /* Dequeue context block */
3246 ctx
= dplane_ctx_dequeue(&ctxlist
);
3248 /* If we've emptied the results queue, we're done */
3253 switch (dplane_ctx_get_op(ctx
)) {
3254 case DPLANE_OP_ROUTE_INSTALL
:
3255 case DPLANE_OP_ROUTE_UPDATE
:
3256 case DPLANE_OP_ROUTE_DELETE
:
3258 /* Bit of special case for route updates
3259 * that were generated by async notifications:
3260 * we don't want to continue processing these
3263 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3264 rib_process_result(ctx
);
3266 dplane_ctx_fini(&ctx
);
3270 case DPLANE_OP_ROUTE_NOTIFY
:
3271 rib_process_dplane_notify(ctx
);
3274 case DPLANE_OP_LSP_INSTALL
:
3275 case DPLANE_OP_LSP_UPDATE
:
3276 case DPLANE_OP_LSP_DELETE
:
3278 /* Bit of special case for LSP updates
3279 * that were generated by async notifications:
3280 * we don't want to continue processing these.
3282 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3283 zebra_mpls_lsp_dplane_result(ctx
);
3285 dplane_ctx_fini(&ctx
);
3289 case DPLANE_OP_LSP_NOTIFY
:
3290 zebra_mpls_process_dplane_notify(ctx
);
3293 case DPLANE_OP_PW_INSTALL
:
3294 case DPLANE_OP_PW_UNINSTALL
:
3295 handle_pw_result(ctx
);
3298 case DPLANE_OP_SYS_ROUTE_ADD
:
3299 case DPLANE_OP_SYS_ROUTE_DELETE
:
3300 /* No further processing in zebra for these. */
3301 dplane_ctx_fini(&ctx
);
3305 /* Don't expect this: just return the struct? */
3306 dplane_ctx_fini(&ctx
);
3308 } /* Dispatch by op code */
3310 ctx
= dplane_ctx_dequeue(&ctxlist
);
3315 /* Check for nexthop tracking processing after finishing with results */
3316 do_nht_processing();
3322 * Results are returned from the dataplane subsystem, in the context of
3323 * the dataplane pthread. We enqueue the results here for processing by
3324 * the main thread later.
3326 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3328 /* Take lock controlling queue of results */
3329 pthread_mutex_lock(&dplane_mutex
);
3331 /* Enqueue context blocks */
3332 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3334 pthread_mutex_unlock(&dplane_mutex
);
3336 /* Ensure event is signalled to zebra main pthread */
3337 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3344 * Ensure there are no empty slots in the route_info array.
3345 * Every route type in zebra should be present there.
3347 static void check_route_info(void)
3349 int len
= array_size(route_info
);
3352 * ZEBRA_ROUTE_SYSTEM is special cased since
3353 * its key is 0 anyway.
3355 * ZEBRA_ROUTE_ALL is also ignored.
3357 for (int i
= 0; i
< len
; i
++) {
3358 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3360 assert(route_info
[i
].key
);
3361 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3365 /* Routing information base initialize. */
3372 /* Init dataplane, and register for results */
3373 pthread_mutex_init(&dplane_mutex
, NULL
);
3374 TAILQ_INIT(&rib_dplane_q
);
3375 zebra_dplane_init(rib_dplane_results
);
3381 * Get the first vrf id that is greater than the given vrf id if any.
3383 * Returns TRUE if a vrf id was found, FALSE otherwise.
3385 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3389 vrf
= vrf_lookup_by_id(vrf_id
);
3391 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3393 *next_id_p
= vrf
->vrf_id
;
3402 * rib_tables_iter_next
3404 * Returns the next table in the iteration.
3406 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3408 struct route_table
*table
;
3411 * Array that helps us go over all AFI/SAFI combinations via one
3418 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3419 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3420 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3425 switch (iter
->state
) {
3427 case RIB_TABLES_ITER_S_INIT
:
3428 iter
->vrf_id
= VRF_DEFAULT
;
3429 iter
->afi_safi_ix
= -1;
3433 case RIB_TABLES_ITER_S_ITERATING
:
3434 iter
->afi_safi_ix
++;
3437 while (iter
->afi_safi_ix
3438 < (int)array_size(afi_safis
)) {
3439 table
= zebra_vrf_table(
3440 afi_safis
[iter
->afi_safi_ix
].afi
,
3441 afi_safis
[iter
->afi_safi_ix
].safi
,
3446 iter
->afi_safi_ix
++;
3450 * Found another table in this vrf.
3456 * Done with all tables in the current vrf, go to the
3460 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3463 iter
->afi_safi_ix
= 0;
3468 case RIB_TABLES_ITER_S_DONE
:
3473 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3475 iter
->state
= RIB_TABLES_ITER_S_DONE
;