1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "sockunion.h"
34 #include "srcdest_table.h"
38 #include "workqueue.h"
39 #include "nexthop_group_private.h"
40 #include "frr_pthread.h"
42 #include "zebra/zebra_router.h"
43 #include "zebra/connected.h"
44 #include "zebra/debug.h"
45 #include "zebra/interface.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/rib.h"
49 #include "zebra/zapi_msg.h"
50 #include "zebra/zebra_errors.h"
51 #include "zebra/zebra_memory.h"
52 #include "zebra/zebra_ns.h"
53 #include "zebra/zebra_rnh.h"
54 #include "zebra/zebra_routemap.h"
55 #include "zebra/zebra_vrf.h"
56 #include "zebra/zebra_vxlan.h"
57 #include "zebra/zapi_msg.h"
58 #include "zebra/zebra_dplane.h"
60 DEFINE_MTYPE_STATIC(ZEBRA
, RIB_UPDATE_CTX
, "Rib update context object");
63 * Event, list, and mutex for delivery of dataplane results
65 static pthread_mutex_t dplane_mutex
;
66 static struct thread
*t_dplane
;
67 static struct dplane_ctx_q rib_dplane_q
;
69 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
72 /* Should we allow non Quagga processes to delete our routes */
73 extern int allow_delete
;
75 /* Each route type's string and default distance value. */
80 } route_info
[ZEBRA_ROUTE_MAX
] = {
81 [ZEBRA_ROUTE_NHG
] = {ZEBRA_ROUTE_NHG
, 255 /* Uneeded for nhg's */, 0},
82 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0, 5},
83 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0, 1},
84 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0, 1},
85 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1, 2},
86 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120, 3},
87 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120, 3},
88 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110, 3},
89 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110, 3},
90 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115, 3},
91 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */, 4},
92 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255, 5},
93 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90, 3},
94 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10, 3},
95 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255, 5},
96 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255, 5},
97 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150, 2},
98 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150, 5},
99 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20, 4},
100 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20, 4},
101 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20, 4},
102 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20, 4},
103 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20, 4},
104 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100, 3},
105 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150, 5},
106 [ZEBRA_ROUTE_PBR
] = {ZEBRA_ROUTE_PBR
, 200, 5},
107 [ZEBRA_ROUTE_BFD
] = {ZEBRA_ROUTE_BFD
, 255, 5},
108 [ZEBRA_ROUTE_OPENFABRIC
] = {ZEBRA_ROUTE_OPENFABRIC
, 115, 3},
109 [ZEBRA_ROUTE_VRRP
] = {ZEBRA_ROUTE_VRRP
, 255, 5}
110 /* Any new route type added to zebra, should be mirrored here */
112 /* no entry/default: 150 */
115 static void __attribute__((format(printf
, 5, 6)))
116 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
117 int priority
, const char *msgfmt
, ...)
119 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
123 va_start(ap
, msgfmt
);
124 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
128 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
129 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
131 if (info
->safi
== SAFI_MULTICAST
)
132 strlcat(buf
, " (MRIB)", sizeof(buf
));
134 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
137 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
140 #define rnode_debug(node, vrf_id, ...) \
141 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
142 #define rnode_info(node, ...) \
143 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
145 uint8_t route_distance(int type
)
149 if ((unsigned)type
>= array_size(route_info
))
152 distance
= route_info
[type
].distance
;
157 int is_zebra_valid_kernel_table(uint32_t table_id
)
160 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
161 || (table_id
== RT_TABLE_COMPAT
))
168 int is_zebra_main_routing_table(uint32_t table_id
)
170 if (table_id
== RT_TABLE_MAIN
)
175 int zebra_check_addr(const struct prefix
*p
)
177 if (p
->family
== AF_INET
) {
180 addr
= p
->u
.prefix4
.s_addr
;
183 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
184 || IPV4_LINKLOCAL(addr
))
187 if (p
->family
== AF_INET6
) {
188 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
190 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
197 * copy_nexthop - copy a nexthop to the rib structure.
199 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
201 assert(!re
->nhe
->nhg
->nexthop
);
202 copy_nexthops(&re
->nhe
->nhg
->nexthop
, nh
, NULL
);
205 static void route_entry_attach_ref(struct route_entry
*re
,
206 struct nhg_hash_entry
*new)
209 re
->nhe_id
= new->id
;
211 zebra_nhg_increment_ref(new);
214 int route_entry_update_nhe(struct route_entry
*re
, struct nhg_hash_entry
*new)
216 struct nhg_hash_entry
*old
= NULL
;
224 if (re
->nhe_id
!= new->id
) {
225 old
= zebra_nhg_lookup_id(re
->nhe_id
);
227 route_entry_attach_ref(re
, new);
230 zebra_nhg_decrement_ref(old
);
232 /* This is the first time it's being attached */
233 route_entry_attach_ref(re
, new);
239 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
240 union g_addr
*addr
, struct route_node
**rn_out
)
243 struct route_table
*table
;
244 struct route_node
*rn
;
245 struct route_entry
*match
= NULL
;
248 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
252 memset(&p
, 0, sizeof(struct prefix
));
255 p
.u
.prefix4
= addr
->ipv4
;
256 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
258 p
.u
.prefix6
= addr
->ipv6
;
259 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
262 rn
= route_node_match(table
, (struct prefix
*)&p
);
267 route_unlock_node(rn
);
269 dest
= rib_dest_from_rnode(rn
);
270 if (dest
&& dest
->selected_fib
271 && !CHECK_FLAG(dest
->selected_fib
->status
,
272 ROUTE_ENTRY_REMOVED
))
273 match
= dest
->selected_fib
;
275 /* If there is no selected route or matched route is EGP, go up
280 } while (rn
&& rn
->info
== NULL
);
284 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
285 if (!CHECK_FLAG(match
->status
,
286 ROUTE_ENTRY_INSTALLED
))
298 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
300 struct route_node
**rn_out
)
302 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
303 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
304 union g_addr gaddr
= {.ipv4
= addr
};
306 switch (zrouter
.ipv4_multicast_mode
) {
307 case MCAST_MRIB_ONLY
:
308 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
310 case MCAST_URIB_ONLY
:
311 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
312 case MCAST_NO_CONFIG
:
313 case MCAST_MIX_MRIB_FIRST
:
314 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
317 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
320 case MCAST_MIX_DISTANCE
:
321 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
322 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
324 re
= ure
->distance
< mre
->distance
? ure
: mre
;
330 case MCAST_MIX_PFXLEN
:
331 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
332 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
334 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
343 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
345 if (IS_ZEBRA_DEBUG_RIB
) {
347 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
349 zlog_debug("%s: %s: vrf: %u found %s, using %s",
350 __func__
, buf
, vrf_id
,
351 mre
? (ure
? "MRIB+URIB" : "MRIB")
352 : ure
? "URIB" : "nothing",
353 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
358 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
360 struct route_table
*table
;
361 struct route_node
*rn
;
362 struct route_entry
*match
= NULL
;
366 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
370 rn
= route_node_lookup(table
, (struct prefix
*)p
);
372 /* No route for this prefix. */
377 route_unlock_node(rn
);
378 dest
= rib_dest_from_rnode(rn
);
380 if (dest
&& dest
->selected_fib
381 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
382 match
= dest
->selected_fib
;
387 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
390 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
397 * Is this RIB labeled-unicast? It must be of type BGP and all paths
398 * (nexthops) must have a label.
400 int zebra_rib_labeled_unicast(struct route_entry
*re
)
402 struct nexthop
*nexthop
= NULL
;
404 if (re
->type
!= ZEBRA_ROUTE_BGP
)
407 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
))
408 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
414 /* Update flag indicates whether this is a "replace" or not. Currently, this
415 * is only used for IPv4.
417 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
418 struct route_entry
*old
)
420 struct nexthop
*nexthop
;
421 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
422 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
423 const struct prefix
*p
, *src_p
;
424 enum zebra_dplane_result ret
;
426 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
428 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
430 if (info
->safi
!= SAFI_UNICAST
) {
431 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
))
432 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
438 * Install the resolved nexthop object first.
440 zebra_nhg_install_kernel(zebra_nhg_lookup_id(re
->nhe_id
));
443 * If this is a replace to a new RE let the originator of the RE
444 * know that they've lost
446 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
447 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
449 /* Update fib selection */
450 dest
->selected_fib
= re
;
453 * Make sure we update the FPM any time we send new information to
456 hook_call(rib_update
, rn
, "installing in kernel");
458 /* Send add or update */
460 ret
= dplane_route_update(rn
, re
, old
);
462 ret
= dplane_route_add(rn
, re
);
465 case ZEBRA_DPLANE_REQUEST_QUEUED
:
466 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
469 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
471 /* Free old FIB nexthop group */
472 if (old
->fib_ng
.nexthop
) {
473 nexthops_free(old
->fib_ng
.nexthop
);
474 old
->fib_ng
.nexthop
= NULL
;
479 zvrf
->installs_queued
++;
481 case ZEBRA_DPLANE_REQUEST_FAILURE
:
483 char str
[SRCDEST2STR_BUFFER
];
485 srcdest_rnode2str(rn
, str
, sizeof(str
));
486 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
487 "%u:%s: Failed to enqueue dataplane install",
491 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
500 /* Uninstall the route from kernel. */
501 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
503 struct nexthop
*nexthop
;
504 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
505 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
507 if (info
->safi
!= SAFI_UNICAST
) {
508 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
509 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
))
510 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
515 * Make sure we update the FPM any time we send new information to
518 hook_call(rib_update
, rn
, "uninstalling from kernel");
520 switch (dplane_route_delete(rn
, re
)) {
521 case ZEBRA_DPLANE_REQUEST_QUEUED
:
523 zvrf
->removals_queued
++;
525 case ZEBRA_DPLANE_REQUEST_FAILURE
:
527 char str
[SRCDEST2STR_BUFFER
];
529 srcdest_rnode2str(rn
, str
, sizeof(str
));
530 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
531 "%u:%s: Failed to enqueue dataplane uninstall",
535 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
544 /* Uninstall the route from kernel. */
545 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
547 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
548 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
549 struct nexthop
*nexthop
;
551 if (dest
&& dest
->selected_fib
== re
) {
552 if (info
->safi
== SAFI_UNICAST
)
553 hook_call(rib_update
, rn
, "rib_uninstall");
555 /* If labeled-unicast route, uninstall transit LSP. */
556 if (zebra_rib_labeled_unicast(re
))
557 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
559 rib_uninstall_kernel(rn
, re
);
561 dest
->selected_fib
= NULL
;
563 /* Free FIB nexthop group, if present */
564 if (re
->fib_ng
.nexthop
) {
565 nexthops_free(re
->fib_ng
.nexthop
);
566 re
->fib_ng
.nexthop
= NULL
;
569 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
))
570 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
573 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
574 const struct prefix
*p
, *src_p
;
576 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
578 redistribute_delete(p
, src_p
, re
, NULL
);
579 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
584 * rib_can_delete_dest
586 * Returns true if the given dest can be deleted from the table.
588 static int rib_can_delete_dest(rib_dest_t
*dest
)
590 if (re_list_first(&dest
->routes
)) {
595 * Unresolved rnh's are stored on the default route's list
597 * dest->rnode can also be the source prefix node in an
598 * ipv6 sourcedest table. Fortunately the prefix of a
599 * source prefix node can never be the default prefix.
601 if (is_default_prefix(&dest
->rnode
->p
))
605 * Don't delete the dest if we have to update the FPM about this
608 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
609 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
615 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
617 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
621 * We are storing the rnh's associated withb
622 * the tracked nexthop as a list of the rn's.
623 * Unresolved rnh's are placed at the top
624 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
625 * As such for each rn we need to walk up the tree
626 * and see if any rnh's need to see if they
627 * would match a more specific route
630 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
631 char buf
[PREFIX_STRLEN
];
633 zlog_debug("%s: %s Being examined for Nexthop Tracking Count: %zd",
635 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
636 dest
? rnh_list_count(&dest
->nht
) : 0);
641 dest
= rib_dest_from_rnode(rn
);
645 * If we have any rnh's stored in the nht list
646 * then we know that this route node was used for
647 * nht resolution and as such we need to call the
648 * nexthop tracking evaluation code
650 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
651 struct zebra_vrf
*zvrf
=
652 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
653 struct prefix
*p
= &rnh
->node
->p
;
655 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
656 char buf1
[PREFIX_STRLEN
];
657 char buf2
[PREFIX_STRLEN
];
659 zlog_debug("%u:%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u",
661 srcdest_rnode2str(rn
, buf1
,
663 prefix2str(p
, buf2
, sizeof(buf2
)),
664 rnh_type2str(rnh
->type
),
669 * If we have evaluated this node on this pass
670 * already, due to following the tree up
671 * then we know that we can move onto the next
674 * Additionally we call zebra_evaluate_rnh
675 * when we gc the dest. In this case we know
676 * that there must be no other re's where
677 * we were originally as such we know that
678 * that sequence number is ok to respect.
680 if (rnh
->seqno
== seq
) {
681 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
683 "\tNode processed and moved already");
688 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
694 dest
= rib_dest_from_rnode(rn
);
701 * Garbage collect the rib dest corresponding to the given route node
704 * Returns true if the dest was deleted, false otherwise.
706 int rib_gc_dest(struct route_node
*rn
)
710 dest
= rib_dest_from_rnode(rn
);
714 if (!rib_can_delete_dest(dest
))
717 if (IS_ZEBRA_DEBUG_RIB
) {
718 struct zebra_vrf
*zvrf
;
720 zvrf
= rib_dest_vrf(dest
);
721 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
724 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
727 rnh_list_fini(&dest
->nht
);
728 XFREE(MTYPE_RIB_DEST
, dest
);
732 * Release the one reference that we keep on the route node.
734 route_unlock_node(rn
);
738 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
739 struct route_entry
*new)
741 hook_call(rib_update
, rn
, "new route selected");
743 /* Update real nexthop. This may actually determine if nexthop is active
745 if (!nexthop_group_active_nexthop_num(new->nhe
->nhg
)) {
746 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
750 if (IS_ZEBRA_DEBUG_RIB
) {
751 char buf
[SRCDEST2STR_BUFFER
];
752 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
753 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
754 zvrf_id(zvrf
), buf
, rn
, new,
755 zebra_route_string(new->type
));
758 /* If labeled-unicast route, install transit LSP. */
759 if (zebra_rib_labeled_unicast(new))
760 zebra_mpls_lsp_install(zvrf
, rn
, new);
762 rib_install_kernel(rn
, new, NULL
);
764 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
767 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
768 struct route_entry
*old
)
770 hook_call(rib_update
, rn
, "removing existing route");
772 /* Uninstall from kernel. */
773 if (IS_ZEBRA_DEBUG_RIB
) {
774 char buf
[SRCDEST2STR_BUFFER
];
775 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
776 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
777 zvrf_id(zvrf
), buf
, rn
, old
,
778 zebra_route_string(old
->type
));
781 /* If labeled-unicast route, uninstall transit LSP. */
782 if (zebra_rib_labeled_unicast(old
))
783 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
785 rib_uninstall_kernel(rn
, old
);
787 /* Update nexthop for route, reset changed flag. */
788 /* Note: this code also handles the Linux case when an interface goes
789 * down, causing the kernel to delete routes without sending DELROUTE
792 if (RIB_KERNEL_ROUTE(old
))
793 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
795 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
798 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
799 struct route_node
*rn
,
800 struct route_entry
*old
,
801 struct route_entry
*new)
806 * We have to install or update if a new route has been selected or
807 * something has changed.
809 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
810 hook_call(rib_update
, rn
, "updating existing route");
812 /* Update the nexthop; we could determine here that nexthop is
814 if (nexthop_group_active_nexthop_num(new->nhe
->nhg
))
817 /* If nexthop is active, install the selected route, if
819 * the install succeeds, cleanup flags for prior route, if
824 if (IS_ZEBRA_DEBUG_RIB
) {
825 char buf
[SRCDEST2STR_BUFFER
];
826 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
829 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
830 zvrf_id(zvrf
), buf
, rn
, new,
831 zebra_route_string(new->type
),
833 zebra_route_string(old
->type
));
836 "%u:%s: Updating route rn %p, re %p (%s)",
837 zvrf_id(zvrf
), buf
, rn
, new,
838 zebra_route_string(new->type
));
841 /* If labeled-unicast route, uninstall transit LSP. */
842 if (zebra_rib_labeled_unicast(old
))
843 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
846 * Non-system route should be installed.
847 * If labeled-unicast route, install transit
850 if (zebra_rib_labeled_unicast(new))
851 zebra_mpls_lsp_install(zvrf
, rn
, new);
853 rib_install_kernel(rn
, new, old
);
857 * If nexthop for selected route is not active or install
859 * may need to uninstall and delete for redistribution.
862 if (IS_ZEBRA_DEBUG_RIB
) {
863 char buf
[SRCDEST2STR_BUFFER
];
864 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
867 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
868 zvrf_id(zvrf
), buf
, rn
, new,
869 zebra_route_string(new->type
),
871 zebra_route_string(old
->type
));
874 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
875 zvrf_id(zvrf
), buf
, rn
, new,
876 zebra_route_string(new->type
));
879 /* If labeled-unicast route, uninstall transit LSP. */
880 if (zebra_rib_labeled_unicast(old
))
881 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
883 rib_uninstall_kernel(rn
, old
);
887 * Same route selected; check if in the FIB and if not,
888 * re-install. This is housekeeping code to deal with
889 * race conditions in kernel with linux netlink reporting
890 * interface up before IPv4 or IPv6 protocol is ready
893 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
894 RIB_SYSTEM_ROUTE(new))
895 rib_install_kernel(rn
, new, NULL
);
898 /* Update prior route. */
900 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
902 /* Clear changed flag. */
903 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
906 /* Check if 'alternate' RIB entry is better than 'current'. */
907 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
908 struct route_entry
*alternate
)
913 /* filter route selection in following order:
914 * - connected beats other types
915 * - if both connected, loopback or vrf wins
916 * - lower distance beats higher
917 * - lower metric beats higher for equal distance
918 * - last, hence oldest, route wins tie break.
921 /* Connected routes. Check to see if either are a vrf
922 * or loopback interface. If not, pick the last connected
923 * route of the set of lowest metric connected routes.
925 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
926 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
929 /* both are connected. are either loop or vrf? */
930 struct nexthop
*nexthop
= NULL
;
932 for (ALL_NEXTHOPS_PTR(alternate
->nhe
->nhg
, nexthop
)) {
933 struct interface
*ifp
= if_lookup_by_index(
934 nexthop
->ifindex
, alternate
->vrf_id
);
936 if (ifp
&& if_is_loopback_or_vrf(ifp
))
940 for (ALL_NEXTHOPS_PTR(current
->nhe
->nhg
, nexthop
)) {
941 struct interface
*ifp
= if_lookup_by_index(
942 nexthop
->ifindex
, current
->vrf_id
);
944 if (ifp
&& if_is_loopback_or_vrf(ifp
))
948 /* Neither are loop or vrf so pick best metric */
949 if (alternate
->metric
<= current
->metric
)
955 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
958 /* higher distance loses */
959 if (alternate
->distance
< current
->distance
)
961 if (current
->distance
< alternate
->distance
)
964 /* metric tie-breaks equal distance */
965 if (alternate
->metric
<= current
->metric
)
971 /* Core function for processing nexthop group contexts's off metaq */
972 static void rib_nhg_process(struct nhg_ctx
*ctx
)
974 nhg_ctx_process(ctx
);
977 /* Core function for processing routing information base. */
978 static void rib_process(struct route_node
*rn
)
980 struct route_entry
*re
;
981 struct route_entry
*next
;
982 struct route_entry
*old_selected
= NULL
;
983 struct route_entry
*new_selected
= NULL
;
984 struct route_entry
*old_fib
= NULL
;
985 struct route_entry
*new_fib
= NULL
;
986 struct route_entry
*best
= NULL
;
987 char buf
[SRCDEST2STR_BUFFER
];
989 struct zebra_vrf
*zvrf
= NULL
;
990 const struct prefix
*p
, *src_p
;
992 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
993 vrf_id_t vrf_id
= VRF_UNKNOWN
;
997 dest
= rib_dest_from_rnode(rn
);
999 zvrf
= rib_dest_vrf(dest
);
1000 vrf_id
= zvrf_id(zvrf
);
1003 if (IS_ZEBRA_DEBUG_RIB
)
1004 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1006 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1007 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1010 * we can have rn's that have a NULL info pointer
1011 * (dest). As such let's not let the deref happen
1012 * additionally we know RNODE_FOREACH_RE_SAFE
1013 * will not iterate so we are ok.
1016 old_fib
= dest
->selected_fib
;
1018 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1019 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1021 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1022 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1023 re
->status
, re
->flags
, re
->distance
,
1026 /* Currently selected re. */
1027 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1028 assert(old_selected
== NULL
);
1032 /* Skip deleted entries from selection */
1033 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1036 /* Skip unreachable nexthop. */
1037 /* This first call to nexthop_active_update is merely to
1038 * determine if there's any change to nexthops associated
1039 * with this RIB entry. Now, rib_process() can be invoked due
1040 * to an external event such as link down or due to
1041 * next-hop-tracking evaluation. In the latter case,
1042 * a decision has already been made that the NHs have changed.
1043 * So, no need to invoke a potentially expensive call again.
1044 * Further, since the change might be in a recursive NH which
1045 * is not caught in the nexthop_active_update() code. Thus, we
1046 * might miss changes to recursive NHs.
1048 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1049 && !nexthop_active_update(rn
, re
)) {
1050 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1051 /* XXX: HERE BE DRAGONS!!!!!
1052 * In all honesty, I have not yet figured out
1053 * what this part does or why the
1054 * ROUTE_ENTRY_CHANGED test above is correct
1055 * or why we need to delete a route here, and
1056 * also not whether this concerns both selected
1057 * and fib route, or only selected
1060 * This entry was denied by the 'ip protocol
1061 * table' route-map, we need to delete it */
1062 if (re
!= old_selected
) {
1063 if (IS_ZEBRA_DEBUG_RIB
)
1065 "%s: %u:%s: imported via import-table but denied "
1066 "by the ip protocol table route-map",
1067 __func__
, vrf_id
, buf
);
1070 SET_FLAG(re
->status
,
1071 ROUTE_ENTRY_REMOVED
);
1077 /* Infinite distance. */
1078 if (re
->distance
== DISTANCE_INFINITY
&&
1079 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1080 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1084 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1085 best
= rib_choose_best(new_fib
, re
);
1086 if (new_fib
&& best
!= new_fib
)
1087 UNSET_FLAG(new_fib
->status
,
1088 ROUTE_ENTRY_CHANGED
);
1091 best
= rib_choose_best(new_selected
, re
);
1092 if (new_selected
&& best
!= new_selected
)
1093 UNSET_FLAG(new_selected
->status
,
1094 ROUTE_ENTRY_CHANGED
);
1095 new_selected
= best
;
1098 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1099 } /* RNODE_FOREACH_RE */
1101 /* If no FIB override route, use the selected route also for FIB */
1102 if (new_fib
== NULL
)
1103 new_fib
= new_selected
;
1105 /* After the cycle is finished, the following pointers will be set:
1106 * old_selected --- RE entry currently having SELECTED
1107 * new_selected --- RE entry that is newly SELECTED
1108 * old_fib --- RE entry currently in kernel FIB
1109 * new_fib --- RE entry that is newly to be in kernel FIB
1111 * new_selected will get SELECTED flag, and is going to be redistributed
1112 * the zclients. new_fib (which can be new_selected) will be installed
1116 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1118 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1119 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1120 (void *)old_fib
, (void *)new_fib
);
1123 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1124 * fib == selected */
1125 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1126 ROUTE_ENTRY_CHANGED
);
1128 /* Update fib according to selection results */
1129 if (new_fib
&& old_fib
)
1130 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1132 rib_process_add_fib(zvrf
, rn
, new_fib
);
1134 rib_process_del_fib(zvrf
, rn
, old_fib
);
1136 /* Update SELECTED entry */
1137 if (old_selected
!= new_selected
|| selected_changed
) {
1139 if (new_selected
&& new_selected
!= new_fib
)
1140 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1143 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1147 * If we're removing the old entry, we should tell
1148 * redist subscribers about that *if* they aren't
1149 * going to see a redist for the new entry.
1151 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1152 ROUTE_ENTRY_REMOVED
))
1153 redistribute_delete(p
, src_p
,
1157 if (old_selected
!= new_selected
)
1158 UNSET_FLAG(old_selected
->flags
,
1159 ZEBRA_FLAG_SELECTED
);
1163 /* Remove all RE entries queued for removal */
1164 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1165 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1166 if (IS_ZEBRA_DEBUG_RIB
) {
1167 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1168 (void *)rn
, (void *)re
);
1175 * Check if the dest can be deleted now.
1180 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1182 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1183 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1188 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1189 if (IS_ZEBRA_DEBUG_MPLS
)
1191 "%u: Scheduling all LSPs upon RIB completion",
1193 zebra_mpls_lsp_schedule(zvrf
);
1194 mpls_unmark_lsps_for_processing(rn
);
1199 * Utility to match route with dplane context data
1201 static bool rib_route_match_ctx(const struct route_entry
*re
,
1202 const struct zebra_dplane_ctx
*ctx
,
1205 bool result
= false;
1209 * In 'update' case, we test info about the 'previous' or
1212 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1213 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1216 /* TODO -- we're using this extra test, but it's not
1217 * exactly clear why.
1219 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1220 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1221 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1228 * Ordinary, single-route case using primary context info
1230 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1231 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1232 /* Skip route that's been deleted */
1236 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1237 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1240 /* TODO -- we're using this extra test, but it's not
1241 * exactly clear why.
1243 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1244 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1245 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1256 static void zebra_rib_fixup_system(struct route_node
*rn
)
1258 struct route_entry
*re
;
1260 RNODE_FOREACH_RE(rn
, re
) {
1261 struct nexthop
*nhop
;
1263 if (!RIB_SYSTEM_ROUTE(re
))
1266 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1269 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1270 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1272 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nhop
)) {
1273 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1276 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1282 * Update a route from a dplane context. This consolidates common code
1283 * that can be used in processing of results from FIB updates, and in
1284 * async notification processing.
1285 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1287 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1288 struct route_node
*rn
,
1289 struct zebra_dplane_ctx
*ctx
)
1291 char dest_str
[PREFIX_STRLEN
] = "";
1292 char nh_str
[NEXTHOP_STRLEN
];
1293 struct nexthop
*nexthop
, *ctx_nexthop
;
1295 const struct nexthop_group
*ctxnhg
;
1296 bool is_selected
= false; /* Is 're' currently the selected re? */
1297 bool changed_p
= false; /* Change to nexthops? */
1300 /* Note well: only capturing the prefix string if debug is enabled here;
1301 * unconditional log messages will have to generate the string.
1303 if (IS_ZEBRA_DEBUG_RIB
)
1304 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1306 dest
= rib_dest_from_rnode(rn
);
1308 is_selected
= (re
== dest
->selected_fib
);
1310 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1311 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1312 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1314 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1315 * If the installed set differs from the set requested by the rib/owner,
1316 * we use the fib-specific nexthop-group to record the actual FIB
1320 /* Check both fib group and notif group for equivalence.
1322 * Let's assume the nexthops are ordered here to save time.
1324 if (nexthop_group_equal(&re
->fib_ng
, dplane_ctx_get_ng(ctx
)) == false) {
1325 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1327 "%u:%s update_from_ctx: notif nh and fib nh mismatch",
1328 re
->vrf_id
, dest_str
);
1335 /* If the new FIB set matches the existing FIB set, we're done. */
1337 if (IS_ZEBRA_DEBUG_RIB
)
1338 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1339 re
->vrf_id
, dest_str
);
1342 } else if (re
->fib_ng
.nexthop
) {
1344 * Free stale fib list and move on to check the rib nhg.
1346 if (IS_ZEBRA_DEBUG_RIB
)
1347 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1348 re
->vrf_id
, dest_str
);
1349 nexthops_free(re
->fib_ng
.nexthop
);
1350 re
->fib_ng
.nexthop
= NULL
;
1352 /* Note that the installed nexthops have changed */
1355 if (IS_ZEBRA_DEBUG_RIB
)
1356 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1357 re
->vrf_id
, dest_str
);
1361 * Compare with the rib nexthop group. The comparison here is different:
1362 * the RIB group may be a superset of the list installed in the FIB. We
1363 * walk the RIB group, looking for the 'installable' candidate
1364 * nexthops, and then check those against the set
1365 * that is actually installed.
1367 * Assume nexthops are ordered here as well.
1371 ctx_nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
1373 /* Nothing installed - we can skip some of the checking/comparison
1376 if (ctx_nexthop
== NULL
) {
1381 /* Get the first `installed` one to check against.
1382 * If the dataplane doesn't set these to be what was actually installed,
1383 * it will just be whatever was in re->nhe->nhg?
1385 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1386 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1387 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1389 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
)) {
1391 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1394 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1397 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1398 if (nexthop_same(ctx_nexthop
, nexthop
) == false) {
1399 /* If the FIB doesn't know about the nexthop,
1400 * it's not installed
1402 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1403 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1405 "update_from_ctx: no notif match for rib nh %s",
1410 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1413 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1415 /* Keep checking nexthops */
1419 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1420 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1423 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1425 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1428 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1431 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1434 /* If all nexthops were processed, we're done */
1436 if (IS_ZEBRA_DEBUG_RIB
)
1437 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1438 re
->vrf_id
, dest_str
,
1439 (changed_p
? "true" : "false"));
1445 /* FIB nexthop set differs from the RIB set:
1446 * create a fib-specific nexthop-group
1448 if (IS_ZEBRA_DEBUG_RIB
)
1449 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1450 re
->vrf_id
, dest_str
,
1451 (changed_p
? "true" : "false"));
1453 ctxnhg
= dplane_ctx_get_ng(ctx
);
1455 if (ctxnhg
->nexthop
)
1456 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1458 /* Bit of a special case when the fib has _no_ installed
1461 nexthop
= nexthop_new();
1462 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1463 _nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1471 * Helper to locate a zebra route-node from a dplane context. This is used
1472 * when processing dplane results, e.g. Note well: the route-node is returned
1473 * with a ref held - route_unlock_node() must be called eventually.
1475 static struct route_node
*
1476 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1478 struct route_table
*table
= NULL
;
1479 struct route_node
*rn
= NULL
;
1480 const struct prefix
*dest_pfx
, *src_pfx
;
1482 /* Locate rn and re(s) from ctx */
1484 table
= zebra_vrf_lookup_table_with_table_id(
1485 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1486 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1487 if (table
== NULL
) {
1488 if (IS_ZEBRA_DEBUG_DPLANE
) {
1489 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1490 dplane_ctx_get_afi(ctx
),
1491 dplane_ctx_get_safi(ctx
),
1492 dplane_ctx_get_vrf(ctx
));
1497 dest_pfx
= dplane_ctx_get_dest(ctx
);
1498 src_pfx
= dplane_ctx_get_src(ctx
);
1500 rn
= srcdest_rnode_get(table
, dest_pfx
,
1501 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1510 * Route-update results processing after async dataplane update.
1512 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1514 struct zebra_vrf
*zvrf
= NULL
;
1515 struct route_node
*rn
= NULL
;
1516 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1517 bool is_update
= false;
1518 char dest_str
[PREFIX_STRLEN
] = "";
1519 enum dplane_op_e op
;
1520 enum zebra_dplane_result status
;
1521 const struct prefix
*dest_pfx
, *src_pfx
;
1523 bool fib_changed
= false;
1525 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1526 dest_pfx
= dplane_ctx_get_dest(ctx
);
1528 /* Note well: only capturing the prefix string if debug is enabled here;
1529 * unconditional log messages will have to generate the string.
1531 if (IS_ZEBRA_DEBUG_DPLANE
)
1532 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1534 /* Locate rn and re(s) from ctx */
1535 rn
= rib_find_rn_from_ctx(ctx
);
1537 if (IS_ZEBRA_DEBUG_DPLANE
) {
1538 zlog_debug("Failed to process dplane results: no route for %u:%s",
1539 dplane_ctx_get_vrf(ctx
), dest_str
);
1544 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1546 op
= dplane_ctx_get_op(ctx
);
1547 status
= dplane_ctx_get_status(ctx
);
1549 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1550 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1551 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1552 dplane_op2str(op
), dplane_res2str(status
));
1555 * Update is a bit of a special case, where we may have both old and new
1556 * routes to post-process.
1558 is_update
= dplane_ctx_is_update(ctx
);
1561 * Take a pass through the routes, look for matches with the context
1564 RNODE_FOREACH_RE(rn
, rib
) {
1567 if (rib_route_match_ctx(rib
, ctx
, false))
1571 /* Check for old route match */
1572 if (is_update
&& (old_re
== NULL
)) {
1573 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1577 /* Have we found the routes we need to work on? */
1578 if (re
&& ((!is_update
|| old_re
)))
1582 seq
= dplane_ctx_get_seq(ctx
);
1585 * Check sequence number(s) to detect stale results before continuing
1588 if (re
->dplane_sequence
!= seq
) {
1589 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1590 zlog_debug("%u:%s Stale dplane result for re %p",
1591 dplane_ctx_get_vrf(ctx
),
1594 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1598 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1599 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1600 zlog_debug("%u:%s Stale dplane result for old_re %p",
1601 dplane_ctx_get_vrf(ctx
),
1604 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1608 case DPLANE_OP_ROUTE_INSTALL
:
1609 case DPLANE_OP_ROUTE_UPDATE
:
1610 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1612 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1613 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1616 * On an update operation from the same route type
1617 * context retrieval currently has no way to know
1618 * which was the old and which was the new.
1619 * So don't unset our flags that we just set.
1620 * We know redistribution is ok because the
1621 * old_re in this case is used for nothing
1622 * more than knowing whom to contact if necessary.
1624 if (old_re
&& old_re
!= re
) {
1625 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1626 UNSET_FLAG(old_re
->status
,
1627 ROUTE_ENTRY_INSTALLED
);
1630 /* Update zebra route based on the results in
1631 * the context struct.
1635 rib_update_re_from_ctx(re
, rn
, ctx
);
1638 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1639 zlog_debug("%u:%s no fib change for re",
1646 redistribute_update(dest_pfx
, src_pfx
,
1651 * System routes are weird in that they
1652 * allow multiple to be installed that match
1653 * to the same prefix, so after we get the
1654 * result we need to clean them up so that
1655 * we can actually use them.
1657 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1658 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1659 zebra_rib_fixup_system(rn
);
1664 /* Notify route owner */
1665 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1669 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1670 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1672 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1674 zsend_route_notify_owner(re
, dest_pfx
,
1675 ZAPI_ROUTE_FAIL_INSTALL
);
1677 zlog_warn("%u:%s: Route install failed",
1678 dplane_ctx_get_vrf(ctx
),
1679 prefix2str(dest_pfx
,
1680 dest_str
, sizeof(dest_str
)));
1683 case DPLANE_OP_ROUTE_DELETE
:
1685 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1687 * In the delete case, the zebra core datastructs were
1688 * updated (or removed) at the time the delete was issued,
1689 * so we're just notifying the route owner.
1691 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1693 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1694 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1696 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1702 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1703 zsend_route_notify_owner_ctx(ctx
,
1704 ZAPI_ROUTE_REMOVE_FAIL
);
1706 zlog_warn("%u:%s: Route Deletion failure",
1707 dplane_ctx_get_vrf(ctx
),
1708 prefix2str(dest_pfx
,
1709 dest_str
, sizeof(dest_str
)));
1713 * System routes are weird in that they
1714 * allow multiple to be installed that match
1715 * to the same prefix, so after we get the
1716 * result we need to clean them up so that
1717 * we can actually use them.
1719 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1720 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1721 zebra_rib_fixup_system(rn
);
1727 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1728 zebra_rib_evaluate_mpls(rn
);
1732 route_unlock_node(rn
);
1734 /* Return context to dataplane module */
1735 dplane_ctx_fini(&ctx
);
1739 * Handle notification from async dataplane: the dataplane has detected
1740 * some change to a route, and notifies zebra so that the control plane
1741 * can reflect that change.
1743 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1745 struct route_node
*rn
= NULL
;
1746 struct route_entry
*re
= NULL
;
1747 struct nexthop
*nexthop
;
1748 char dest_str
[PREFIX_STRLEN
] = "";
1749 const struct prefix
*dest_pfx
, *src_pfx
;
1751 bool fib_changed
= false;
1752 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1753 int start_count
, end_count
;
1754 dest_pfx
= dplane_ctx_get_dest(ctx
);
1756 /* Note well: only capturing the prefix string if debug is enabled here;
1757 * unconditional log messages will have to generate the string.
1760 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1762 /* Locate rn and re(s) from ctx */
1763 rn
= rib_find_rn_from_ctx(ctx
);
1766 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1767 dplane_ctx_get_vrf(ctx
), dest_str
);
1772 dest
= rib_dest_from_rnode(rn
);
1773 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1776 zlog_debug("%u:%s Processing dplane notif ctx %p",
1777 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1780 * Take a pass through the routes, look for matches with the context
1783 RNODE_FOREACH_RE(rn
, re
) {
1784 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1788 /* No match? Nothing we can do */
1791 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1792 dplane_ctx_get_vrf(ctx
), dest_str
,
1794 dplane_ctx_get_type(ctx
)));
1799 /* Ensure we clear the QUEUED flag */
1800 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1802 /* Is this a notification that ... matters? We mostly care about
1803 * the route that is currently selected for installation; we may also
1804 * get an un-install notification, and handle that too.
1806 if (re
!= dest
->selected_fib
) {
1808 * If we need to, clean up after a delete that was part of
1809 * an update operation.
1812 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1813 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1817 /* If no nexthops or none installed, ensure that this re
1818 * gets its 'installed' flag cleared.
1820 if (end_count
== 0) {
1821 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
1822 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1824 zlog_debug("%u:%s dplane notif, uninstalled type %s route",
1825 dplane_ctx_get_vrf(ctx
), dest_str
,
1827 dplane_ctx_get_type(ctx
)));
1829 /* At least report on the event. */
1831 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1832 dplane_ctx_get_vrf(ctx
), dest_str
,
1834 dplane_ctx_get_type(ctx
)));
1839 /* We'll want to determine whether the installation status of the
1840 * route has changed: we'll check the status before processing,
1841 * and then again if there's been a change.
1845 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)) {
1846 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1847 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1852 /* Update zebra's nexthop FIB flags based on the context struct's
1855 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1859 zlog_debug("%u:%s dplane notification: rib_update returns FALSE",
1860 dplane_ctx_get_vrf(ctx
), dest_str
);
1864 * Perform follow-up work if the actual status of the prefix
1869 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1870 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1874 /* Various fib transitions: changed nexthops; from installed to
1875 * not-installed; or not-installed to installed.
1877 if (start_count
> 0 && end_count
> 0) {
1879 zlog_debug("%u:%s applied nexthop changes from dplane notification",
1880 dplane_ctx_get_vrf(ctx
), dest_str
);
1882 /* Changed nexthops - update kernel/others */
1883 dplane_route_notif_update(rn
, re
,
1884 DPLANE_OP_ROUTE_UPDATE
, ctx
);
1886 } else if (start_count
== 0 && end_count
> 0) {
1888 zlog_debug("%u:%s installed transition from dplane notification",
1889 dplane_ctx_get_vrf(ctx
), dest_str
);
1891 /* We expect this to be the selected route, so we want
1892 * to tell others about this transition.
1894 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1896 /* Changed nexthops - update kernel/others */
1897 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
1899 /* Redistribute, lsp, and nht update */
1900 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
1902 } else if (start_count
> 0 && end_count
== 0) {
1904 zlog_debug("%u:%s un-installed transition from dplane notification",
1905 dplane_ctx_get_vrf(ctx
), dest_str
);
1907 /* Transition from _something_ installed to _nothing_
1910 /* We expect this to be the selected route, so we want
1911 * to tell others about this transistion.
1913 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1915 /* Changed nexthops - update kernel/others */
1916 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
1918 /* Redistribute, lsp, and nht update */
1919 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
1922 /* Make any changes visible for lsp and nexthop-tracking processing */
1923 zebra_rib_evaluate_rn_nexthops(
1924 rn
, zebra_router_get_next_sequence());
1926 zebra_rib_evaluate_mpls(rn
);
1930 route_unlock_node(rn
);
1932 /* Return context to dataplane module */
1933 dplane_ctx_fini(&ctx
);
1936 static void process_subq_nhg(struct listnode
*lnode
)
1938 struct nhg_ctx
*ctx
= NULL
;
1939 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
1941 ctx
= listgetdata(lnode
);
1946 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1947 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
1950 rib_nhg_process(ctx
);
1953 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
1955 struct route_node
*rnode
= NULL
;
1956 rib_dest_t
*dest
= NULL
;
1957 struct zebra_vrf
*zvrf
= NULL
;
1959 rnode
= listgetdata(lnode
);
1960 dest
= rib_dest_from_rnode(rnode
);
1962 zvrf
= rib_dest_vrf(dest
);
1966 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1967 char buf
[SRCDEST2STR_BUFFER
];
1969 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1970 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1971 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1975 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1976 RIB_ROUTE_QUEUED(qindex
));
1981 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1982 __func__
, rnode
, rnode
->lock
);
1983 zlog_backtrace(LOG_DEBUG
);
1986 route_unlock_node(rnode
);
1989 /* Take a list of route_node structs and return 1, if there was a record
1990 * picked from it and processed by rib_process(). Don't process more,
1991 * than one RN record; operate only in the specified sub-queue.
1993 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
1995 struct listnode
*lnode
= listhead(subq
);
2000 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2001 process_subq_nhg(lnode
);
2003 process_subq_route(lnode
, qindex
);
2005 list_delete_node(subq
, lnode
);
2010 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2011 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2013 * is pointed to the meta queue structure.
2015 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2017 struct meta_queue
*mq
= data
;
2019 uint32_t queue_len
, queue_limit
;
2021 /* Ensure there's room for more dataplane updates */
2022 queue_limit
= dplane_get_in_queue_limit();
2023 queue_len
= dplane_get_in_queue_len();
2024 if (queue_len
> queue_limit
) {
2025 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2026 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2027 queue_len
, queue_limit
);
2029 /* Ensure that the meta-queue is actually enqueued */
2030 if (work_queue_empty(zrouter
.ribq
))
2031 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2033 return WQ_QUEUE_BLOCKED
;
2036 for (i
= 0; i
< MQ_SIZE
; i
++)
2037 if (process_subq(mq
->subq
[i
], i
)) {
2041 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2046 * Look into the RN and queue it into the highest priority queue
2047 * at this point in time for processing.
2049 * We will enqueue a route node only once per invocation.
2051 * There are two possibilities here that should be kept in mind.
2052 * If the original invocation has not been pulled off for processing
2053 * yet, A subsuquent invocation can have a route entry with a better
2054 * meta queue index value and we can have a situation where
2055 * we might have the same node enqueued 2 times. Not necessarily
2056 * an optimal situation but it should be ok.
2058 * The other possibility is that the original invocation has not
2059 * been pulled off for processing yet, A subsusquent invocation
2060 * doesn't have a route_entry with a better meta-queue and the
2061 * original metaqueue index value will win and we'll end up with
2062 * the route node enqueued once.
2064 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2066 struct route_node
*rn
= NULL
;
2067 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2068 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2070 rn
= (struct route_node
*)data
;
2072 RNODE_FOREACH_RE (rn
, curr_re
) {
2073 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2075 if (curr_qindex
<= qindex
) {
2077 qindex
= curr_qindex
;
2084 /* Invariant: at this point we always have rn->info set. */
2085 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2086 RIB_ROUTE_QUEUED(qindex
))) {
2087 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2088 rnode_debug(rn
, re
->vrf_id
,
2089 "rn %p is already queued in sub-queue %u",
2090 (void *)rn
, qindex
);
2094 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2095 listnode_add(mq
->subq
[qindex
], rn
);
2096 route_lock_node(rn
);
2099 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2100 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2101 (void *)rn
, qindex
);
2106 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2108 struct nhg_ctx
*ctx
= NULL
;
2109 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2111 ctx
= (struct nhg_ctx
*)data
;
2116 listnode_add(mq
->subq
[qindex
], ctx
);
2119 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2120 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2126 static int mq_add_handler(void *data
,
2127 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2129 if (zrouter
.ribq
== NULL
) {
2130 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2131 "%s: work_queue does not exist!", __func__
);
2136 * The RIB queue should normally be either empty or holding the only
2137 * work_queue_item element. In the latter case this element would
2138 * hold a pointer to the meta queue structure, which must be used to
2139 * actually queue the route nodes to process. So create the MQ
2140 * holder, if necessary, then push the work into it in any case.
2141 * This semantics was introduced after 0.99.9 release.
2143 if (work_queue_empty(zrouter
.ribq
))
2144 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2146 return mq_add_func(zrouter
.mq
, data
);
2149 /* Add route_node to work queue and schedule processing */
2150 int rib_queue_add(struct route_node
*rn
)
2154 /* Pointless to queue a route_node with no RIB entries to add or remove
2156 if (!rnode_to_ribs(rn
)) {
2157 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2158 __func__
, (void *)rn
, rn
->lock
);
2159 zlog_backtrace(LOG_DEBUG
);
2163 return mq_add_handler(rn
, &rib_meta_queue_add
);
2166 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2170 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2173 /* Create new meta queue.
2174 A destructor function doesn't seem to be necessary here.
2176 static struct meta_queue
*meta_queue_new(void)
2178 struct meta_queue
*new;
2181 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2183 for (i
= 0; i
< MQ_SIZE
; i
++) {
2184 new->subq
[i
] = list_new();
2185 assert(new->subq
[i
]);
2191 void meta_queue_free(struct meta_queue
*mq
)
2195 for (i
= 0; i
< MQ_SIZE
; i
++)
2196 list_delete(&mq
->subq
[i
]);
2198 XFREE(MTYPE_WORK_QUEUE
, mq
);
2201 /* initialise zebra rib work queue */
2202 static void rib_queue_init(void)
2204 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2205 "route_node processing"))) {
2206 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2207 "%s: could not initialise work queue!", __func__
);
2211 /* fill in the work queue spec */
2212 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2213 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2214 zrouter
.ribq
->spec
.completion_func
= NULL
;
2215 /* XXX: TODO: These should be runtime configurable via vty */
2216 zrouter
.ribq
->spec
.max_retries
= 3;
2217 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2218 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2220 if (!(zrouter
.mq
= meta_queue_new())) {
2221 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2222 "%s: could not initialise meta queue!", __func__
);
2228 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2232 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2233 rnh_list_init(&dest
->nht
);
2234 route_lock_node(rn
); /* rn route table reference */
2241 /* RIB updates are processed via a queue of pointers to route_nodes.
2243 * The queue length is bounded by the maximal size of the routing table,
2244 * as a route_node will not be requeued, if already queued.
2246 * REs are submitted via rib_addnode or rib_delnode which set minimal
2247 * state, or static_install_route (when an existing RE is updated)
2248 * and then submit route_node to queue for best-path selection later.
2249 * Order of add/delete state changes are preserved for any given RE.
2251 * Deleted REs are reaped during best-path selection.
2254 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2255 * |-------->| | best RE, if required
2257 * static_install->|->rib_addqueue...... -> rib_process
2259 * |-------->| |-> rib_unlink
2260 * |-> set ROUTE_ENTRY_REMOVE |
2261 * rib_delnode (RE freed)
2263 * The 'info' pointer of a route_node points to a rib_dest_t
2264 * ('dest'). Queueing state for a route_node is kept on the dest. The
2265 * dest is created on-demand by rib_link() and is kept around at least
2266 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2268 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2270 * - route_nodes: refcounted by:
2271 * - dest attached to route_node:
2272 * - managed by: rib_link/rib_gc_dest
2273 * - route_node processing queue
2274 * - managed by: rib_addqueue, rib_process.
2278 /* Add RE to head of the route node. */
2279 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2283 const char *rmap_name
;
2287 dest
= rib_dest_from_rnode(rn
);
2289 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2290 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2292 dest
= zebra_rib_create_dest(rn
);
2295 re_list_add_head(&dest
->routes
, re
);
2297 afi
= (rn
->p
.family
== AF_INET
)
2299 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2300 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2301 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2303 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2304 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2309 static void rib_addnode(struct route_node
*rn
,
2310 struct route_entry
*re
, int process
)
2312 /* RE node has been un-removed before route-node is processed.
2313 * route_node must hence already be on the queue for processing..
2315 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2316 if (IS_ZEBRA_DEBUG_RIB
)
2317 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2318 (void *)rn
, (void *)re
);
2320 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2323 rib_link(rn
, re
, process
);
2329 * Detach a rib structure from a route_node.
2331 * Note that a call to rib_unlink() should be followed by a call to
2332 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2333 * longer required to be deleted.
2335 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2338 struct nhg_hash_entry
*nhe
= NULL
;
2342 if (IS_ZEBRA_DEBUG_RIB
)
2343 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2346 dest
= rib_dest_from_rnode(rn
);
2348 re_list_del(&dest
->routes
, re
);
2350 if (dest
->selected_fib
== re
)
2351 dest
->selected_fib
= NULL
;
2354 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2356 zebra_nhg_decrement_ref(nhe
);
2357 } else if (re
->nhe
->nhg
)
2358 nexthop_group_delete(&re
->nhe
->nhg
);
2360 nexthops_free(re
->fib_ng
.nexthop
);
2362 XFREE(MTYPE_RE
, re
);
2365 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2369 if (IS_ZEBRA_DEBUG_RIB
)
2370 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2371 (void *)rn
, (void *)re
);
2372 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2374 afi
= (rn
->p
.family
== AF_INET
)
2376 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2377 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2378 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2380 zebra_del_import_table_entry(zvrf
, rn
, re
);
2381 /* Just clean up if non main table */
2382 if (IS_ZEBRA_DEBUG_RIB
) {
2383 char buf
[SRCDEST2STR_BUFFER
];
2384 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2385 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2386 re
->vrf_id
, buf
, rn
, re
,
2387 zebra_route_string(re
->type
));
2396 /* This function dumps the contents of a given RE entry into
2397 * standard debug log. Calling function name and IP prefix in
2398 * question are passed as 1st and 2nd arguments.
2401 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2402 union prefixconstptr src_pp
,
2403 const struct route_entry
*re
)
2405 const struct prefix
*src_p
= src_pp
.p
;
2406 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2407 char straddr
[PREFIX_STRLEN
];
2408 char srcaddr
[PREFIX_STRLEN
];
2409 char nhname
[PREFIX_STRLEN
];
2410 struct nexthop
*nexthop
;
2412 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2413 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2414 is_srcdst
? " from " : "",
2415 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2418 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2419 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2422 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2423 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2424 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2425 nexthop_group_nexthop_num(re
->nhe
->nhg
),
2426 nexthop_group_active_nexthop_num(re
->nhe
->nhg
));
2428 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
)) {
2429 struct interface
*ifp
;
2430 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2432 switch (nexthop
->type
) {
2433 case NEXTHOP_TYPE_BLACKHOLE
:
2434 sprintf(nhname
, "Blackhole");
2436 case NEXTHOP_TYPE_IFINDEX
:
2437 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2439 sprintf(nhname
, "%s", ifp
? ifp
->name
: "Unknown");
2441 case NEXTHOP_TYPE_IPV4
:
2443 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2444 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
,
2447 case NEXTHOP_TYPE_IPV6
:
2448 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2449 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
,
2453 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s",
2454 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2455 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2457 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2460 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2463 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2466 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2469 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2473 zlog_debug("%s: dump complete", straddr
);
2476 /* This is an exported helper to rtm_read() to dump the strange
2477 * RE entry found by rib_lookup_ipv4_route()
2480 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2482 struct route_table
*table
;
2483 struct route_node
*rn
;
2484 struct route_entry
*re
;
2485 char prefix_buf
[INET_ADDRSTRLEN
];
2488 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2490 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2491 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2496 /* Scan the RIB table for exactly matching RE entry. */
2497 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2499 /* No route for this prefix. */
2501 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2502 prefix2str((struct prefix
*)p
, prefix_buf
,
2503 sizeof(prefix_buf
)));
2508 route_unlock_node(rn
);
2511 RNODE_FOREACH_RE (rn
, re
) {
2512 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2514 (void *)rn
, (void *)re
,
2515 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2518 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2521 route_entry_dump(p
, NULL
, re
);
2525 /* Check if requested address assignment will fail due to another
2526 * route being installed by zebra in FIB already. Take necessary
2527 * actions, if needed: remove such a route from FIB and deSELECT
2528 * corresponding RE entry. Then put affected RN into RIBQ head.
2530 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2532 struct route_table
*table
;
2533 struct route_node
*rn
;
2536 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2537 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2538 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2543 /* No matches would be the simplest case. */
2544 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2548 route_unlock_node(rn
);
2550 dest
= rib_dest_from_rnode(rn
);
2551 /* Check all RE entries. In case any changes have to be done, requeue
2552 * the RN into RIBQ head. If the routing message about the new connected
2553 * route (generated by the IP address we are going to assign very soon)
2554 * comes before the RIBQ is processed, the new RE entry will join
2555 * RIBQ record already on head. This is necessary for proper
2557 * of the rest of the RE.
2559 if (dest
->selected_fib
) {
2560 if (IS_ZEBRA_DEBUG_RIB
) {
2561 char buf
[PREFIX_STRLEN
];
2563 zlog_debug("%u:%s: freeing way for connected prefix",
2564 dest
->selected_fib
->vrf_id
,
2565 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2566 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2568 rib_uninstall(rn
, dest
->selected_fib
);
2573 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2574 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
2575 struct nexthop_group
*ng
)
2577 struct nhg_hash_entry
*nhe
= NULL
;
2578 struct route_table
*table
;
2579 struct route_node
*rn
;
2580 struct route_entry
*same
= NULL
;
2586 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2589 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2593 nexthop_group_delete(&ng
);
2594 XFREE(MTYPE_RE
, re
);
2599 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2603 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2604 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
2606 XFREE(MTYPE_RE
, re
);
2610 nhe
= zebra_nhg_rib_find(0, ng
, afi
);
2613 * The nexthops got copied over into an nhe,
2616 nexthop_group_delete(&ng
);
2619 char buf
[PREFIX_STRLEN
] = "";
2620 char buf2
[PREFIX_STRLEN
] = "";
2623 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2624 "Zebra failed to find or create a nexthop hash entry for %s%s%s",
2625 prefix2str(p
, buf
, sizeof(buf
)),
2626 src_p
? " from " : "",
2627 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
2630 XFREE(MTYPE_RE
, re
);
2636 * Attach the re to the nhe's nexthop group.
2638 * TODO: This will need to change when we start getting IDs from upper
2639 * level protocols, as the refcnt might be wrong, since it checks
2640 * if old_id != new_id.
2642 route_entry_update_nhe(re
, nhe
);
2644 /* Make it sure prefixlen is applied to the prefix. */
2647 apply_mask_ipv6(src_p
);
2649 /* Set default distance by route type. */
2650 if (re
->distance
== 0)
2651 re
->distance
= route_distance(re
->type
);
2653 /* Lookup route node.*/
2654 rn
= srcdest_rnode_get(table
, p
, src_p
);
2657 * If same type of route are installed, treat it as a implicit
2659 * If the user has specified the No route replace semantics
2660 * for the install don't do a route replace.
2662 RNODE_FOREACH_RE (rn
, same
) {
2663 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2666 if (same
->type
!= re
->type
)
2668 if (same
->instance
!= re
->instance
)
2670 if (same
->type
== ZEBRA_ROUTE_KERNEL
2671 && same
->metric
!= re
->metric
)
2674 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2675 same
->distance
!= re
->distance
)
2679 * We should allow duplicate connected routes
2680 * because of IPv6 link-local routes and unnumbered
2681 * interfaces on Linux.
2683 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2687 /* If this route is kernel/connected route, notify the dataplane. */
2688 if (RIB_SYSTEM_ROUTE(re
)) {
2689 /* Notify dataplane */
2690 dplane_sys_route_add(rn
, re
);
2693 /* Link new re to node.*/
2694 if (IS_ZEBRA_DEBUG_RIB
) {
2695 rnode_debug(rn
, re
->vrf_id
,
2696 "Inserting route rn %p, re %p (%s) existing %p",
2697 rn
, re
, zebra_route_string(re
->type
), same
);
2699 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2700 route_entry_dump(p
, src_p
, re
);
2703 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2704 rib_addnode(rn
, re
, 1);
2707 /* Free implicit route.*/
2709 rib_delnode(rn
, same
);
2713 route_unlock_node(rn
);
2717 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2718 unsigned short instance
, int flags
, struct prefix
*p
,
2719 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2720 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
2721 uint8_t distance
, bool fromkernel
)
2723 struct route_table
*table
;
2724 struct route_node
*rn
;
2725 struct route_entry
*re
;
2726 struct route_entry
*fib
= NULL
;
2727 struct route_entry
*same
= NULL
;
2728 struct nexthop
*rtnh
;
2729 char buf2
[INET6_ADDRSTRLEN
];
2732 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2735 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
2743 apply_mask_ipv6(src_p
);
2745 /* Lookup route node. */
2746 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2748 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2750 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2751 if (src_p
&& src_p
->prefixlen
)
2752 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2756 if (IS_ZEBRA_DEBUG_RIB
) {
2757 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2759 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
2760 vrf
->name
, table_id
, dst_buf
,
2761 (src_buf
[0] != '\0') ? " from " : "",
2767 dest
= rib_dest_from_rnode(rn
);
2768 fib
= dest
->selected_fib
;
2770 /* Lookup same type route. */
2771 RNODE_FOREACH_RE (rn
, re
) {
2772 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2775 if (re
->type
!= type
)
2777 if (re
->instance
!= instance
)
2779 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2780 distance
!= re
->distance
)
2783 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2785 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
2786 (rtnh
= re
->nhe
->nhg
->nexthop
)
2787 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2788 if (rtnh
->ifindex
!= nh
->ifindex
)
2794 /* Make sure that the route found has the same gateway. */
2795 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
2804 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, rtnh
)) {
2806 * No guarantee all kernel send nh with labels
2809 if (nexthop_same_no_labels(rtnh
, nh
)) {
2818 /* If same type of route can't be found and this message is from
2822 * In the past(HA!) we could get here because
2823 * we were receiving a route delete from the
2824 * kernel and we're not marking the proto
2825 * as coming from it's appropriate originator.
2826 * Now that we are properly noticing the fact
2827 * that the kernel has deleted our route we
2828 * are not going to get called in this path
2829 * I am going to leave this here because
2830 * this might still work this way on non-linux
2831 * platforms as well as some weird state I have
2832 * not properly thought of yet.
2833 * If we can show that this code path is
2834 * dead then we can remove it.
2836 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2837 if (IS_ZEBRA_DEBUG_RIB
) {
2838 rnode_debug(rn
, vrf_id
,
2839 "rn %p, re %p (%s) was deleted from kernel, adding",
2841 zebra_route_string(fib
->type
));
2844 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2846 for (rtnh
= fib
->nhe
->nhg
->nexthop
; rtnh
;
2848 UNSET_FLAG(rtnh
->flags
,
2852 * This is a non FRR route
2853 * as such we should mark
2856 dest
->selected_fib
= NULL
;
2858 /* This means someone else, other than Zebra,
2860 * a Zebra router from the kernel. We will add
2862 rib_install_kernel(rn
, fib
, NULL
);
2865 if (IS_ZEBRA_DEBUG_RIB
) {
2869 "via %s ifindex %d type %d "
2870 "doesn't exist in rib",
2871 inet_ntop(afi2family(afi
),
2878 "type %d doesn't exist in rib",
2881 route_unlock_node(rn
);
2887 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2889 rib_install_kernel(rn
, same
, NULL
);
2890 route_unlock_node(rn
);
2895 /* Special handling for IPv4 or IPv6 routes sourced from
2896 * EVPN - the nexthop (and associated MAC) need to be
2897 * uninstalled if no more refs.
2899 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2900 struct nexthop
*tmp_nh
;
2902 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, tmp_nh
)) {
2903 struct ipaddr vtep_ip
;
2905 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2906 if (afi
== AFI_IP
) {
2907 vtep_ip
.ipa_type
= IPADDR_V4
;
2908 memcpy(&(vtep_ip
.ipaddr_v4
),
2909 &(tmp_nh
->gate
.ipv4
),
2910 sizeof(struct in_addr
));
2912 vtep_ip
.ipa_type
= IPADDR_V6
;
2913 memcpy(&(vtep_ip
.ipaddr_v6
),
2914 &(tmp_nh
->gate
.ipv6
),
2915 sizeof(struct in6_addr
));
2917 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2922 /* Notify dplane if system route changes */
2923 if (RIB_SYSTEM_ROUTE(re
))
2924 dplane_sys_route_del(rn
, same
);
2926 rib_delnode(rn
, same
);
2929 route_unlock_node(rn
);
2934 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2935 unsigned short instance
, int flags
, struct prefix
*p
,
2936 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2937 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
2938 uint8_t distance
, route_tag_t tag
)
2940 struct route_entry
*re
= NULL
;
2941 struct nexthop
*nexthop
= NULL
;
2942 struct nexthop_group
*ng
= NULL
;
2944 /* Allocate new route_entry structure. */
2945 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2947 re
->instance
= instance
;
2948 re
->distance
= distance
;
2950 re
->metric
= metric
;
2952 re
->table
= table_id
;
2953 re
->vrf_id
= vrf_id
;
2954 re
->uptime
= monotime(NULL
);
2956 re
->nhe_id
= nhe_id
;
2958 /* If the owner of the route supplies a shared nexthop-group id,
2959 * we'll use that. Otherwise, pass the nexthop along directly.
2962 ng
= nexthop_group_new();
2965 nexthop
= nexthop_new();
2967 nexthop_group_add_sorted(ng
, nexthop
);
2970 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
2973 static const char *rib_update_event2str(rib_update_event_t event
)
2975 const char *ret
= "UNKNOWN";
2978 case RIB_UPDATE_KERNEL
:
2979 ret
= "RIB_UPDATE_KERNEL";
2981 case RIB_UPDATE_RMAP_CHANGE
:
2982 ret
= "RIB_UPDATE_RMAP_CHANGE";
2984 case RIB_UPDATE_OTHER
:
2985 ret
= "RIB_UPDATE_OTHER";
2987 case RIB_UPDATE_MAX
:
2995 /* Schedule route nodes to be processed if they match the type */
2996 static void rib_update_route_node(struct route_node
*rn
, int type
)
2998 struct route_entry
*re
, *next
;
2999 bool re_changed
= false;
3001 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3002 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3003 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3012 /* Schedule routes of a particular table (address-family) based on event. */
3013 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3015 struct route_node
*rn
;
3017 if (IS_ZEBRA_DEBUG_EVENT
) {
3018 struct zebra_vrf
*zvrf
;
3021 zvrf
= table
->info
? ((rib_table_info_t
*)table
->info
)->zvrf
3023 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3025 zlog_debug("%s: %s VRF %s Table %u event %s", __func__
,
3026 table
->info
? afi2str(
3027 ((rib_table_info_t
*)table
->info
)->afi
)
3029 vrf
? vrf
->name
: "Unknown",
3030 zvrf
? zvrf
->table_id
: 0,
3031 rib_update_event2str(event
));
3034 /* Walk all routes and queue for processing, if appropriate for
3035 * the trigger event.
3037 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3039 * If we are looking at a route node and the node
3040 * has already been queued we don't
3041 * need to queue it up again
3044 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3045 RIB_ROUTE_ANY_QUEUED
))
3049 case RIB_UPDATE_KERNEL
:
3050 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3052 case RIB_UPDATE_RMAP_CHANGE
:
3053 case RIB_UPDATE_OTHER
:
3054 rib_update_route_node(rn
, ZEBRA_ROUTE_ALL
);
3062 static void rib_update_handle_vrf(vrf_id_t vrf_id
, rib_update_event_t event
)
3064 struct route_table
*table
;
3066 if (IS_ZEBRA_DEBUG_EVENT
)
3067 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3068 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3070 /* Process routes of interested address-families. */
3071 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3073 rib_update_table(table
, event
);
3075 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3077 rib_update_table(table
, event
);
3080 static void rib_update_handle_vrf_all(rib_update_event_t event
)
3082 struct zebra_router_table
*zrt
;
3084 if (IS_ZEBRA_DEBUG_EVENT
)
3085 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3086 rib_update_event2str(event
));
3088 /* Just iterate over all the route tables, rather than vrf lookups */
3089 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3090 rib_update_table(zrt
->table
, event
);
3093 struct rib_update_ctx
{
3094 rib_update_event_t event
;
3099 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3100 rib_update_event_t event
)
3102 struct rib_update_ctx
*ctx
;
3104 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3107 ctx
->vrf_id
= vrf_id
;
3112 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3114 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3117 static int rib_update_handler(struct thread
*thread
)
3119 struct rib_update_ctx
*ctx
;
3121 ctx
= THREAD_ARG(thread
);
3124 rib_update_handle_vrf_all(ctx
->event
);
3126 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
);
3128 rib_update_ctx_fini(&ctx
);
3134 * Thread list to ensure we don't schedule a ton of events
3135 * if interfaces are flapping for instance.
3137 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3139 /* Schedule a RIB update event for specific vrf */
3140 void rib_update_vrf(vrf_id_t vrf_id
, rib_update_event_t event
)
3142 struct rib_update_ctx
*ctx
;
3144 ctx
= rib_update_ctx_init(vrf_id
, event
);
3146 /* Don't worry about making sure multiple rib updates for specific vrf
3147 * are scheduled at once for now. If it becomes a problem, we can use a
3148 * lookup of some sort to keep track of running threads via t_vrf_id
3149 * like how we are doing it in t_rib_update_threads[].
3151 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0, NULL
);
3153 if (IS_ZEBRA_DEBUG_EVENT
)
3154 zlog_debug("%s: Scheduled VRF %s, event %s", __func__
,
3155 vrf_id_to_name(ctx
->vrf_id
),
3156 rib_update_event2str(event
));
3159 /* Schedule a RIB update event for all vrfs */
3160 void rib_update(rib_update_event_t event
)
3162 struct rib_update_ctx
*ctx
;
3164 ctx
= rib_update_ctx_init(0, event
);
3166 ctx
->vrf_all
= true;
3168 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3169 &t_rib_update_threads
[event
]))
3170 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3171 else if (IS_ZEBRA_DEBUG_EVENT
)
3172 zlog_debug("%s: Schedued VRF (ALL), event %s", __func__
,
3173 rib_update_event2str(event
));
3176 /* Delete self installed routes after zebra is relaunched. */
3177 void rib_sweep_table(struct route_table
*table
)
3179 struct route_node
*rn
;
3180 struct route_entry
*re
;
3181 struct route_entry
*next
;
3182 struct nexthop
*nexthop
;
3187 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3188 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3190 if (IS_ZEBRA_DEBUG_RIB
)
3191 route_entry_dump(&rn
->p
, NULL
, re
);
3193 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3196 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3200 * If routes are older than startup_time then
3201 * we know we read them in from the kernel.
3202 * As such we can safely remove them.
3204 if (zrouter
.startup_time
< re
->uptime
)
3208 * So we are starting up and have received
3209 * routes from the kernel that we have installed
3210 * from a previous run of zebra but not cleaned
3211 * up ( say a kill -9 )
3212 * But since we haven't actually installed
3213 * them yet( we received them from the kernel )
3214 * we don't think they are active.
3215 * So let's pretend they are active to actually
3217 * In all honesty I'm not sure if we should
3218 * mark them as active when we receive them
3219 * This is startup only so probably ok.
3221 * If we ever decide to move rib_sweep_table
3222 * to a different spot (ie startup )
3223 * this decision needs to be revisited
3225 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3226 for (ALL_NEXTHOPS_PTR(re
->nhe
->nhg
, nexthop
))
3227 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3229 rib_uninstall_kernel(rn
, re
);
3230 rib_delnode(rn
, re
);
3235 /* Sweep all RIB tables. */
3236 int rib_sweep_route(struct thread
*t
)
3239 struct zebra_vrf
*zvrf
;
3241 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3242 if ((zvrf
= vrf
->info
) == NULL
)
3245 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3246 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3249 zebra_router_sweep_route();
3250 zebra_router_sweep_nhgs();
3255 /* Remove specific by protocol routes from 'table'. */
3256 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3257 struct route_table
*table
)
3259 struct route_node
*rn
;
3260 struct route_entry
*re
;
3261 struct route_entry
*next
;
3262 unsigned long n
= 0;
3265 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3266 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3267 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3269 if (re
->type
== proto
3270 && re
->instance
== instance
) {
3271 rib_delnode(rn
, re
);
3278 /* Remove specific by protocol routes. */
3279 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3282 struct zebra_vrf
*zvrf
;
3283 struct other_route_table
*ort
;
3284 unsigned long cnt
= 0;
3286 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3291 cnt
+= rib_score_proto_table(proto
, instance
,
3292 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3293 + rib_score_proto_table(
3295 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3297 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3298 rib_score_proto_table(proto
, instance
, ort
->table
);
3304 /* Close RIB and clean up kernel routes. */
3305 void rib_close_table(struct route_table
*table
)
3307 struct route_node
*rn
;
3308 rib_table_info_t
*info
;
3314 info
= route_table_get_info(table
);
3316 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3317 dest
= rib_dest_from_rnode(rn
);
3319 if (dest
&& dest
->selected_fib
) {
3320 if (info
->safi
== SAFI_UNICAST
)
3321 hook_call(rib_update
, rn
, NULL
);
3323 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3324 dest
->selected_fib
= NULL
;
3330 * Handler for async dataplane results after a pseudowire installation
3332 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3334 struct zebra_pw
*pw
;
3335 struct zebra_vrf
*vrf
;
3337 /* The pseudowire code assumes success - we act on an error
3338 * result for installation attempts here.
3340 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3343 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3344 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3345 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3347 zebra_pw_install_failure(pw
);
3357 * Handle results from the dataplane system. Dequeue update context
3358 * structs, dispatch to appropriate internal handlers.
3360 static int rib_process_dplane_results(struct thread
*thread
)
3362 struct zebra_dplane_ctx
*ctx
;
3363 struct dplane_ctx_q ctxlist
;
3364 bool shut_p
= false;
3366 /* Dequeue a list of completed updates with one lock/unlock cycle */
3369 TAILQ_INIT(&ctxlist
);
3371 /* Take lock controlling queue of results */
3372 frr_with_mutex(&dplane_mutex
) {
3373 /* Dequeue list of context structs */
3374 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3377 /* Dequeue context block */
3378 ctx
= dplane_ctx_dequeue(&ctxlist
);
3380 /* If we've emptied the results queue, we're done */
3384 /* If zebra is shutting down, avoid processing results,
3385 * just drain the results queue.
3387 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3388 memory_order_relaxed
);
3391 dplane_ctx_fini(&ctx
);
3393 ctx
= dplane_ctx_dequeue(&ctxlist
);
3400 switch (dplane_ctx_get_op(ctx
)) {
3401 case DPLANE_OP_ROUTE_INSTALL
:
3402 case DPLANE_OP_ROUTE_UPDATE
:
3403 case DPLANE_OP_ROUTE_DELETE
:
3405 /* Bit of special case for route updates
3406 * that were generated by async notifications:
3407 * we don't want to continue processing these
3410 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3411 rib_process_result(ctx
);
3413 dplane_ctx_fini(&ctx
);
3417 case DPLANE_OP_ROUTE_NOTIFY
:
3418 rib_process_dplane_notify(ctx
);
3421 case DPLANE_OP_NH_INSTALL
:
3422 case DPLANE_OP_NH_UPDATE
:
3423 case DPLANE_OP_NH_DELETE
:
3424 zebra_nhg_dplane_result(ctx
);
3427 case DPLANE_OP_LSP_INSTALL
:
3428 case DPLANE_OP_LSP_UPDATE
:
3429 case DPLANE_OP_LSP_DELETE
:
3431 /* Bit of special case for LSP updates
3432 * that were generated by async notifications:
3433 * we don't want to continue processing these.
3435 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3436 zebra_mpls_lsp_dplane_result(ctx
);
3438 dplane_ctx_fini(&ctx
);
3442 case DPLANE_OP_LSP_NOTIFY
:
3443 zebra_mpls_process_dplane_notify(ctx
);
3446 case DPLANE_OP_PW_INSTALL
:
3447 case DPLANE_OP_PW_UNINSTALL
:
3448 handle_pw_result(ctx
);
3451 case DPLANE_OP_SYS_ROUTE_ADD
:
3452 case DPLANE_OP_SYS_ROUTE_DELETE
:
3453 /* No further processing in zebra for these. */
3454 dplane_ctx_fini(&ctx
);
3457 case DPLANE_OP_MAC_INSTALL
:
3458 case DPLANE_OP_MAC_DELETE
:
3459 zebra_vxlan_handle_result(ctx
);
3462 /* Some op codes not handled here */
3463 case DPLANE_OP_ADDR_INSTALL
:
3464 case DPLANE_OP_ADDR_UNINSTALL
:
3465 case DPLANE_OP_NEIGH_INSTALL
:
3466 case DPLANE_OP_NEIGH_UPDATE
:
3467 case DPLANE_OP_NEIGH_DELETE
:
3468 case DPLANE_OP_VTEP_ADD
:
3469 case DPLANE_OP_VTEP_DELETE
:
3470 case DPLANE_OP_NONE
:
3471 /* Don't expect this: just return the struct? */
3472 dplane_ctx_fini(&ctx
);
3475 } /* Dispatch by op code */
3477 ctx
= dplane_ctx_dequeue(&ctxlist
);
3486 * Results are returned from the dataplane subsystem, in the context of
3487 * the dataplane pthread. We enqueue the results here for processing by
3488 * the main thread later.
3490 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3492 /* Take lock controlling queue of results */
3493 frr_with_mutex(&dplane_mutex
) {
3494 /* Enqueue context blocks */
3495 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3498 /* Ensure event is signalled to zebra main pthread */
3499 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3506 * Ensure there are no empty slots in the route_info array.
3507 * Every route type in zebra should be present there.
3509 static void check_route_info(void)
3511 int len
= array_size(route_info
);
3514 * ZEBRA_ROUTE_SYSTEM is special cased since
3515 * its key is 0 anyway.
3517 * ZEBRA_ROUTE_ALL is also ignored.
3519 for (int i
= 0; i
< len
; i
++) {
3520 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3522 assert(route_info
[i
].key
);
3523 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3527 /* Routing information base initialize. */
3534 /* Init dataplane, and register for results */
3535 pthread_mutex_init(&dplane_mutex
, NULL
);
3536 TAILQ_INIT(&rib_dplane_q
);
3537 zebra_dplane_init(rib_dplane_results
);
3543 * Get the first vrf id that is greater than the given vrf id if any.
3545 * Returns true if a vrf id was found, false otherwise.
3547 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3551 vrf
= vrf_lookup_by_id(vrf_id
);
3553 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3555 *next_id_p
= vrf
->vrf_id
;
3564 * rib_tables_iter_next
3566 * Returns the next table in the iteration.
3568 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3570 struct route_table
*table
;
3573 * Array that helps us go over all AFI/SAFI combinations via one
3576 static const struct {
3580 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3581 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3582 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3587 switch (iter
->state
) {
3589 case RIB_TABLES_ITER_S_INIT
:
3590 iter
->vrf_id
= VRF_DEFAULT
;
3591 iter
->afi_safi_ix
= -1;
3595 case RIB_TABLES_ITER_S_ITERATING
:
3596 iter
->afi_safi_ix
++;
3599 while (iter
->afi_safi_ix
3600 < (int)array_size(afi_safis
)) {
3601 table
= zebra_vrf_table(
3602 afi_safis
[iter
->afi_safi_ix
].afi
,
3603 afi_safis
[iter
->afi_safi_ix
].safi
,
3608 iter
->afi_safi_ix
++;
3612 * Found another table in this vrf.
3618 * Done with all tables in the current vrf, go to the
3622 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3625 iter
->afi_safi_ix
= 0;
3630 case RIB_TABLES_ITER_S_DONE
:
3635 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3637 iter
->state
= RIB_TABLES_ITER_S_DONE
;