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
;
221 zebra_nhg_decrement_ref(re
->nhe
);
226 if (re
->nhe_id
!= new->id
) {
229 route_entry_attach_ref(re
, new);
232 zebra_nhg_decrement_ref(old
);
234 /* This is the first time it's being attached */
235 route_entry_attach_ref(re
, new);
241 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
242 union g_addr
*addr
, struct route_node
**rn_out
)
245 struct route_table
*table
;
246 struct route_node
*rn
;
247 struct route_entry
*match
= NULL
;
250 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
254 memset(&p
, 0, sizeof(struct prefix
));
257 p
.u
.prefix4
= addr
->ipv4
;
258 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
260 p
.u
.prefix6
= addr
->ipv6
;
261 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
264 rn
= route_node_match(table
, (struct prefix
*)&p
);
269 route_unlock_node(rn
);
271 dest
= rib_dest_from_rnode(rn
);
272 if (dest
&& dest
->selected_fib
273 && !CHECK_FLAG(dest
->selected_fib
->status
,
274 ROUTE_ENTRY_REMOVED
))
275 match
= dest
->selected_fib
;
277 /* If there is no selected route or matched route is EGP, go up
282 } while (rn
&& rn
->info
== NULL
);
286 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
287 if (!CHECK_FLAG(match
->status
,
288 ROUTE_ENTRY_INSTALLED
))
300 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
302 struct route_node
**rn_out
)
304 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
305 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
306 union g_addr gaddr
= {.ipv4
= addr
};
308 switch (zrouter
.ipv4_multicast_mode
) {
309 case MCAST_MRIB_ONLY
:
310 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
312 case MCAST_URIB_ONLY
:
313 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
314 case MCAST_NO_CONFIG
:
315 case MCAST_MIX_MRIB_FIRST
:
316 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
319 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
322 case MCAST_MIX_DISTANCE
:
323 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
324 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
326 re
= ure
->distance
< mre
->distance
? ure
: mre
;
332 case MCAST_MIX_PFXLEN
:
333 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
334 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
336 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
345 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
347 if (IS_ZEBRA_DEBUG_RIB
) {
349 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
351 zlog_debug("%s: %s: vrf: %u found %s, using %s",
352 __func__
, buf
, vrf_id
,
353 mre
? (ure
? "MRIB+URIB" : "MRIB")
354 : ure
? "URIB" : "nothing",
355 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
360 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
362 struct route_table
*table
;
363 struct route_node
*rn
;
364 struct route_entry
*match
= NULL
;
368 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
372 rn
= route_node_lookup(table
, (struct prefix
*)p
);
374 /* No route for this prefix. */
379 route_unlock_node(rn
);
380 dest
= rib_dest_from_rnode(rn
);
382 if (dest
&& dest
->selected_fib
383 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
384 match
= dest
->selected_fib
;
389 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
392 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_INSTALLED
))
399 * Is this RIB labeled-unicast? It must be of type BGP and all paths
400 * (nexthops) must have a label.
402 int zebra_rib_labeled_unicast(struct route_entry
*re
)
404 struct nexthop
*nexthop
= NULL
;
406 if (re
->type
!= ZEBRA_ROUTE_BGP
)
409 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
410 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
416 /* Update flag indicates whether this is a "replace" or not. Currently, this
417 * is only used for IPv4.
419 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
420 struct route_entry
*old
)
422 struct nexthop
*nexthop
;
423 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
424 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
425 const struct prefix
*p
, *src_p
;
426 enum zebra_dplane_result ret
;
428 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
430 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
432 if (info
->safi
!= SAFI_UNICAST
) {
433 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
434 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
440 * Install the resolved nexthop object first.
442 zebra_nhg_install_kernel(re
->nhe
);
445 * If this is a replace to a new RE let the originator of the RE
446 * know that they've lost
448 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
449 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
451 /* Update fib selection */
452 dest
->selected_fib
= re
;
455 * Make sure we update the FPM any time we send new information to
458 hook_call(rib_update
, rn
, "installing in kernel");
460 /* Send add or update */
462 ret
= dplane_route_update(rn
, re
, old
);
464 ret
= dplane_route_add(rn
, re
);
467 case ZEBRA_DPLANE_REQUEST_QUEUED
:
468 SET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
471 SET_FLAG(old
->status
, ROUTE_ENTRY_QUEUED
);
473 /* Free old FIB nexthop group */
474 if (old
->fib_ng
.nexthop
) {
475 nexthops_free(old
->fib_ng
.nexthop
);
476 old
->fib_ng
.nexthop
= NULL
;
481 zvrf
->installs_queued
++;
483 case ZEBRA_DPLANE_REQUEST_FAILURE
:
485 char str
[SRCDEST2STR_BUFFER
];
487 srcdest_rnode2str(rn
, str
, sizeof(str
));
488 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
489 "%u:%s: Failed to enqueue dataplane install",
493 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
502 /* Uninstall the route from kernel. */
503 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
505 struct nexthop
*nexthop
;
506 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
507 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
509 if (info
->safi
!= SAFI_UNICAST
) {
510 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
511 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
512 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
517 * Make sure we update the FPM any time we send new information to
520 hook_call(rib_update
, rn
, "uninstalling from kernel");
522 switch (dplane_route_delete(rn
, re
)) {
523 case ZEBRA_DPLANE_REQUEST_QUEUED
:
525 zvrf
->removals_queued
++;
527 case ZEBRA_DPLANE_REQUEST_FAILURE
:
529 char str
[SRCDEST2STR_BUFFER
];
531 srcdest_rnode2str(rn
, str
, sizeof(str
));
532 flog_err(EC_ZEBRA_DP_INSTALL_FAIL
,
533 "%u:%s: Failed to enqueue dataplane uninstall",
537 case ZEBRA_DPLANE_REQUEST_SUCCESS
:
546 /* Uninstall the route from kernel. */
547 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
549 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
550 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
551 struct nexthop
*nexthop
;
553 if (dest
&& dest
->selected_fib
== re
) {
554 if (info
->safi
== SAFI_UNICAST
)
555 hook_call(rib_update
, rn
, "rib_uninstall");
557 /* If labeled-unicast route, uninstall transit LSP. */
558 if (zebra_rib_labeled_unicast(re
))
559 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
561 rib_uninstall_kernel(rn
, re
);
563 dest
->selected_fib
= NULL
;
565 /* Free FIB nexthop group, if present */
566 if (re
->fib_ng
.nexthop
) {
567 nexthops_free(re
->fib_ng
.nexthop
);
568 re
->fib_ng
.nexthop
= NULL
;
571 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
572 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
575 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
576 const struct prefix
*p
, *src_p
;
578 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
580 redistribute_delete(p
, src_p
, re
, NULL
);
581 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
586 * rib_can_delete_dest
588 * Returns true if the given dest can be deleted from the table.
590 static int rib_can_delete_dest(rib_dest_t
*dest
)
592 if (re_list_first(&dest
->routes
)) {
597 * Unresolved rnh's are stored on the default route's list
599 * dest->rnode can also be the source prefix node in an
600 * ipv6 sourcedest table. Fortunately the prefix of a
601 * source prefix node can never be the default prefix.
603 if (is_default_prefix(&dest
->rnode
->p
))
607 * Don't delete the dest if we have to update the FPM about this
610 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
611 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
617 void zebra_rib_evaluate_rn_nexthops(struct route_node
*rn
, uint32_t seq
)
619 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
623 * We are storing the rnh's associated withb
624 * the tracked nexthop as a list of the rn's.
625 * Unresolved rnh's are placed at the top
626 * of the tree list.( 0.0.0.0/0 for v4 and 0::0/0 for v6 )
627 * As such for each rn we need to walk up the tree
628 * and see if any rnh's need to see if they
629 * would match a more specific route
632 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
633 char buf
[PREFIX_STRLEN
];
636 "%s: %s Being examined for Nexthop Tracking Count: %zd",
638 srcdest_rnode2str(rn
, buf
, sizeof(buf
)),
639 dest
? rnh_list_count(&dest
->nht
) : 0);
644 dest
= rib_dest_from_rnode(rn
);
648 * If we have any rnh's stored in the nht list
649 * then we know that this route node was used for
650 * nht resolution and as such we need to call the
651 * nexthop tracking evaluation code
653 frr_each_safe(rnh_list
, &dest
->nht
, rnh
) {
654 struct zebra_vrf
*zvrf
=
655 zebra_vrf_lookup_by_id(rnh
->vrf_id
);
656 struct prefix
*p
= &rnh
->node
->p
;
658 if (IS_ZEBRA_DEBUG_NHT_DETAILED
) {
659 char buf1
[PREFIX_STRLEN
];
660 char buf2
[PREFIX_STRLEN
];
662 zlog_debug("%u:%s has Nexthop(%s) Type: %s depending on it, evaluating %u:%u",
664 srcdest_rnode2str(rn
, buf1
,
666 prefix2str(p
, buf2
, sizeof(buf2
)),
667 rnh_type2str(rnh
->type
),
672 * If we have evaluated this node on this pass
673 * already, due to following the tree up
674 * then we know that we can move onto the next
677 * Additionally we call zebra_evaluate_rnh
678 * when we gc the dest. In this case we know
679 * that there must be no other re's where
680 * we were originally as such we know that
681 * that sequence number is ok to respect.
683 if (rnh
->seqno
== seq
) {
684 if (IS_ZEBRA_DEBUG_NHT_DETAILED
)
686 "\tNode processed and moved already");
691 zebra_evaluate_rnh(zvrf
, family2afi(p
->family
), 0,
697 dest
= rib_dest_from_rnode(rn
);
704 * Garbage collect the rib dest corresponding to the given route node
707 * Returns true if the dest was deleted, false otherwise.
709 int rib_gc_dest(struct route_node
*rn
)
713 dest
= rib_dest_from_rnode(rn
);
717 if (!rib_can_delete_dest(dest
))
720 if (IS_ZEBRA_DEBUG_RIB
) {
721 struct zebra_vrf
*zvrf
;
723 zvrf
= rib_dest_vrf(dest
);
724 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
727 zebra_rib_evaluate_rn_nexthops(rn
, zebra_router_get_next_sequence());
730 rnh_list_fini(&dest
->nht
);
731 XFREE(MTYPE_RIB_DEST
, dest
);
735 * Release the one reference that we keep on the route node.
737 route_unlock_node(rn
);
741 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
742 struct route_entry
*new)
744 hook_call(rib_update
, rn
, "new route selected");
746 /* Update real nexthop. This may actually determine if nexthop is active
748 if (!nexthop_group_active_nexthop_num(&(new->nhe
->nhg
))) {
749 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
753 if (IS_ZEBRA_DEBUG_RIB
) {
754 char buf
[SRCDEST2STR_BUFFER
];
755 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
756 zlog_debug("%u:%s: Adding route rn %p, re %p (%s)",
757 zvrf_id(zvrf
), buf
, rn
, new,
758 zebra_route_string(new->type
));
761 /* If labeled-unicast route, install transit LSP. */
762 if (zebra_rib_labeled_unicast(new))
763 zebra_mpls_lsp_install(zvrf
, rn
, new);
765 rib_install_kernel(rn
, new, NULL
);
767 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
770 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
771 struct route_entry
*old
)
773 hook_call(rib_update
, rn
, "removing existing route");
775 /* Uninstall from kernel. */
776 if (IS_ZEBRA_DEBUG_RIB
) {
777 char buf
[SRCDEST2STR_BUFFER
];
778 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
779 zlog_debug("%u:%s: Deleting route rn %p, re %p (%s)",
780 zvrf_id(zvrf
), buf
, rn
, old
,
781 zebra_route_string(old
->type
));
784 /* If labeled-unicast route, uninstall transit LSP. */
785 if (zebra_rib_labeled_unicast(old
))
786 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
788 rib_uninstall_kernel(rn
, old
);
790 /* Update nexthop for route, reset changed flag. */
791 /* Note: this code also handles the Linux case when an interface goes
792 * down, causing the kernel to delete routes without sending DELROUTE
795 if (RIB_KERNEL_ROUTE(old
))
796 SET_FLAG(old
->status
, ROUTE_ENTRY_REMOVED
);
798 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
801 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
802 struct route_node
*rn
,
803 struct route_entry
*old
,
804 struct route_entry
*new)
809 * We have to install or update if a new route has been selected or
810 * something has changed.
812 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
813 hook_call(rib_update
, rn
, "updating existing route");
815 /* Update the nexthop; we could determine here that nexthop is
817 if (nexthop_group_active_nexthop_num(&(new->nhe
->nhg
)))
820 /* If nexthop is active, install the selected route, if
822 * the install succeeds, cleanup flags for prior route, if
827 if (IS_ZEBRA_DEBUG_RIB
) {
828 char buf
[SRCDEST2STR_BUFFER
];
829 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
832 "%u:%s: Updating route rn %p, re %p (%s) old %p (%s)",
833 zvrf_id(zvrf
), buf
, rn
, new,
834 zebra_route_string(new->type
),
836 zebra_route_string(old
->type
));
839 "%u:%s: Updating route rn %p, re %p (%s)",
840 zvrf_id(zvrf
), buf
, rn
, new,
841 zebra_route_string(new->type
));
844 /* If labeled-unicast route, uninstall transit LSP. */
845 if (zebra_rib_labeled_unicast(old
))
846 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
849 * Non-system route should be installed.
850 * If labeled-unicast route, install transit
853 if (zebra_rib_labeled_unicast(new))
854 zebra_mpls_lsp_install(zvrf
, rn
, new);
856 rib_install_kernel(rn
, new, old
);
860 * If nexthop for selected route is not active or install
862 * may need to uninstall and delete for redistribution.
865 if (IS_ZEBRA_DEBUG_RIB
) {
866 char buf
[SRCDEST2STR_BUFFER
];
867 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
870 "%u:%s: Deleting route rn %p, re %p (%s) old %p (%s) - nexthop inactive",
871 zvrf_id(zvrf
), buf
, rn
, new,
872 zebra_route_string(new->type
),
874 zebra_route_string(old
->type
));
877 "%u:%s: Deleting route rn %p, re %p (%s) - nexthop inactive",
878 zvrf_id(zvrf
), buf
, rn
, new,
879 zebra_route_string(new->type
));
882 /* If labeled-unicast route, uninstall transit LSP. */
883 if (zebra_rib_labeled_unicast(old
))
884 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
886 rib_uninstall_kernel(rn
, old
);
890 * Same route selected; check if in the FIB and if not,
891 * re-install. This is housekeeping code to deal with
892 * race conditions in kernel with linux netlink reporting
893 * interface up before IPv4 or IPv6 protocol is ready
896 if (!CHECK_FLAG(new->status
, ROUTE_ENTRY_INSTALLED
) ||
897 RIB_SYSTEM_ROUTE(new))
898 rib_install_kernel(rn
, new, NULL
);
901 /* Update prior route. */
903 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
905 /* Clear changed flag. */
906 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
909 /* Check if 'alternate' RIB entry is better than 'current'. */
910 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
911 struct route_entry
*alternate
)
916 /* filter route selection in following order:
917 * - connected beats other types
918 * - if both connected, loopback or vrf wins
919 * - lower distance beats higher
920 * - lower metric beats higher for equal distance
921 * - last, hence oldest, route wins tie break.
924 /* Connected routes. Check to see if either are a vrf
925 * or loopback interface. If not, pick the last connected
926 * route of the set of lowest metric connected routes.
928 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
929 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
932 /* both are connected. are either loop or vrf? */
933 struct nexthop
*nexthop
= NULL
;
935 for (ALL_NEXTHOPS(alternate
->nhe
->nhg
, nexthop
)) {
936 struct interface
*ifp
= if_lookup_by_index(
937 nexthop
->ifindex
, alternate
->vrf_id
);
939 if (ifp
&& if_is_loopback_or_vrf(ifp
))
943 for (ALL_NEXTHOPS(current
->nhe
->nhg
, nexthop
)) {
944 struct interface
*ifp
= if_lookup_by_index(
945 nexthop
->ifindex
, current
->vrf_id
);
947 if (ifp
&& if_is_loopback_or_vrf(ifp
))
951 /* Neither are loop or vrf so pick best metric */
952 if (alternate
->metric
<= current
->metric
)
958 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
961 /* higher distance loses */
962 if (alternate
->distance
< current
->distance
)
964 if (current
->distance
< alternate
->distance
)
967 /* metric tie-breaks equal distance */
968 if (alternate
->metric
<= current
->metric
)
974 /* Core function for processing nexthop group contexts's off metaq */
975 static void rib_nhg_process(struct nhg_ctx
*ctx
)
977 nhg_ctx_process(ctx
);
980 /* Core function for processing routing information base. */
981 static void rib_process(struct route_node
*rn
)
983 struct route_entry
*re
;
984 struct route_entry
*next
;
985 struct route_entry
*old_selected
= NULL
;
986 struct route_entry
*new_selected
= NULL
;
987 struct route_entry
*old_fib
= NULL
;
988 struct route_entry
*new_fib
= NULL
;
989 struct route_entry
*best
= NULL
;
990 char buf
[SRCDEST2STR_BUFFER
];
992 struct zebra_vrf
*zvrf
= NULL
;
993 const struct prefix
*p
, *src_p
;
995 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
996 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1000 dest
= rib_dest_from_rnode(rn
);
1002 zvrf
= rib_dest_vrf(dest
);
1003 vrf_id
= zvrf_id(zvrf
);
1006 if (IS_ZEBRA_DEBUG_RIB
)
1007 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1009 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1010 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1013 * we can have rn's that have a NULL info pointer
1014 * (dest). As such let's not let the deref happen
1015 * additionally we know RNODE_FOREACH_RE_SAFE
1016 * will not iterate so we are ok.
1019 old_fib
= dest
->selected_fib
;
1021 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1022 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1024 "%u:%s: Examine re %p (%s) status %x flags %x dist %d metric %d",
1025 vrf_id
, buf
, re
, zebra_route_string(re
->type
),
1026 re
->status
, re
->flags
, re
->distance
,
1029 /* Currently selected re. */
1030 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1031 assert(old_selected
== NULL
);
1035 /* Skip deleted entries from selection */
1036 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1039 /* Skip unreachable nexthop. */
1040 /* This first call to nexthop_active_update is merely to
1041 * determine if there's any change to nexthops associated
1042 * with this RIB entry. Now, rib_process() can be invoked due
1043 * to an external event such as link down or due to
1044 * next-hop-tracking evaluation. In the latter case,
1045 * a decision has already been made that the NHs have changed.
1046 * So, no need to invoke a potentially expensive call again.
1047 * Further, since the change might be in a recursive NH which
1048 * is not caught in the nexthop_active_update() code. Thus, we
1049 * might miss changes to recursive NHs.
1051 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1052 && !nexthop_active_update(rn
, re
)) {
1053 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1054 /* XXX: HERE BE DRAGONS!!!!!
1055 * In all honesty, I have not yet figured out
1056 * what this part does or why the
1057 * ROUTE_ENTRY_CHANGED test above is correct
1058 * or why we need to delete a route here, and
1059 * also not whether this concerns both selected
1060 * and fib route, or only selected
1063 * This entry was denied by the 'ip protocol
1064 * table' route-map, we need to delete it */
1065 if (re
!= old_selected
) {
1066 if (IS_ZEBRA_DEBUG_RIB
)
1068 "%s: %u:%s: imported via import-table but denied "
1069 "by the ip protocol table route-map",
1070 __func__
, vrf_id
, buf
);
1073 SET_FLAG(re
->status
,
1074 ROUTE_ENTRY_REMOVED
);
1080 /* Infinite distance. */
1081 if (re
->distance
== DISTANCE_INFINITY
&&
1082 re
->type
!= ZEBRA_ROUTE_KERNEL
) {
1083 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1087 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1088 best
= rib_choose_best(new_fib
, re
);
1089 if (new_fib
&& best
!= new_fib
)
1090 UNSET_FLAG(new_fib
->status
,
1091 ROUTE_ENTRY_CHANGED
);
1094 best
= rib_choose_best(new_selected
, re
);
1095 if (new_selected
&& best
!= new_selected
)
1096 UNSET_FLAG(new_selected
->status
,
1097 ROUTE_ENTRY_CHANGED
);
1098 new_selected
= best
;
1101 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1102 } /* RNODE_FOREACH_RE */
1104 /* If no FIB override route, use the selected route also for FIB */
1105 if (new_fib
== NULL
)
1106 new_fib
= new_selected
;
1108 /* After the cycle is finished, the following pointers will be set:
1109 * old_selected --- RE entry currently having SELECTED
1110 * new_selected --- RE entry that is newly SELECTED
1111 * old_fib --- RE entry currently in kernel FIB
1112 * new_fib --- RE entry that is newly to be in kernel FIB
1114 * new_selected will get SELECTED flag, and is going to be redistributed
1115 * the zclients. new_fib (which can be new_selected) will be installed
1119 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1121 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1122 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1123 (void *)old_fib
, (void *)new_fib
);
1126 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1127 * fib == selected */
1128 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1129 ROUTE_ENTRY_CHANGED
);
1131 /* Update fib according to selection results */
1132 if (new_fib
&& old_fib
)
1133 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1135 rib_process_add_fib(zvrf
, rn
, new_fib
);
1137 rib_process_del_fib(zvrf
, rn
, old_fib
);
1139 /* Update SELECTED entry */
1140 if (old_selected
!= new_selected
|| selected_changed
) {
1142 if (new_selected
&& new_selected
!= new_fib
)
1143 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1146 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1150 * If we're removing the old entry, we should tell
1151 * redist subscribers about that *if* they aren't
1152 * going to see a redist for the new entry.
1154 if (!new_selected
|| CHECK_FLAG(old_selected
->status
,
1155 ROUTE_ENTRY_REMOVED
))
1156 redistribute_delete(p
, src_p
,
1160 if (old_selected
!= new_selected
)
1161 UNSET_FLAG(old_selected
->flags
,
1162 ZEBRA_FLAG_SELECTED
);
1166 /* Remove all RE entries queued for removal */
1167 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1168 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1169 if (IS_ZEBRA_DEBUG_RIB
) {
1170 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1171 (void *)rn
, (void *)re
);
1178 * Check if the dest can be deleted now.
1183 static void zebra_rib_evaluate_mpls(struct route_node
*rn
)
1185 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1186 struct zebra_vrf
*zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1191 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_LSPS
)) {
1192 if (IS_ZEBRA_DEBUG_MPLS
)
1194 "%u: Scheduling all LSPs upon RIB completion",
1196 zebra_mpls_lsp_schedule(zvrf
);
1197 mpls_unmark_lsps_for_processing(rn
);
1202 * Utility to match route with dplane context data
1204 static bool rib_route_match_ctx(const struct route_entry
*re
,
1205 const struct zebra_dplane_ctx
*ctx
,
1208 bool result
= false;
1212 * In 'update' case, we test info about the 'previous' or
1215 if ((re
->type
== dplane_ctx_get_old_type(ctx
)) &&
1216 (re
->instance
== dplane_ctx_get_old_instance(ctx
))) {
1219 /* TODO -- we're using this extra test, but it's not
1220 * exactly clear why.
1222 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1223 (re
->distance
!= dplane_ctx_get_old_distance(ctx
) ||
1224 re
->tag
!= dplane_ctx_get_old_tag(ctx
))) {
1231 * Ordinary, single-route case using primary context info
1233 if ((dplane_ctx_get_op(ctx
) != DPLANE_OP_ROUTE_DELETE
) &&
1234 CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1235 /* Skip route that's been deleted */
1239 if ((re
->type
== dplane_ctx_get_type(ctx
)) &&
1240 (re
->instance
== dplane_ctx_get_instance(ctx
))) {
1243 /* TODO -- we're using this extra test, but it's not
1244 * exactly clear why.
1246 if (re
->type
== ZEBRA_ROUTE_STATIC
&&
1247 (re
->distance
!= dplane_ctx_get_distance(ctx
) ||
1248 re
->tag
!= dplane_ctx_get_tag(ctx
))) {
1259 static void zebra_rib_fixup_system(struct route_node
*rn
)
1261 struct route_entry
*re
;
1263 RNODE_FOREACH_RE(rn
, re
) {
1264 struct nexthop
*nhop
;
1266 if (!RIB_SYSTEM_ROUTE(re
))
1269 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1272 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1273 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1275 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nhop
)) {
1276 if (CHECK_FLAG(nhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1279 SET_FLAG(nhop
->flags
, NEXTHOP_FLAG_FIB
);
1285 * Update a route from a dplane context. This consolidates common code
1286 * that can be used in processing of results from FIB updates, and in
1287 * async notification processing.
1288 * The return is 'true' if the installed nexthops changed; 'false' otherwise.
1290 static bool rib_update_re_from_ctx(struct route_entry
*re
,
1291 struct route_node
*rn
,
1292 struct zebra_dplane_ctx
*ctx
)
1294 char dest_str
[PREFIX_STRLEN
] = "";
1295 char nh_str
[NEXTHOP_STRLEN
];
1296 struct nexthop
*nexthop
, *ctx_nexthop
;
1298 const struct nexthop_group
*ctxnhg
;
1299 bool is_selected
= false; /* Is 're' currently the selected re? */
1300 bool changed_p
= false; /* Change to nexthops? */
1303 /* Note well: only capturing the prefix string if debug is enabled here;
1304 * unconditional log messages will have to generate the string.
1306 if (IS_ZEBRA_DEBUG_RIB
)
1307 prefix2str(&(rn
->p
), dest_str
, sizeof(dest_str
));
1309 dest
= rib_dest_from_rnode(rn
);
1311 is_selected
= (re
== dest
->selected_fib
);
1313 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1314 zlog_debug("update_from_ctx: %u:%s: %sSELECTED",
1315 re
->vrf_id
, dest_str
, (is_selected
? "" : "NOT "));
1317 /* Update zebra's nexthop FIB flag for each nexthop that was installed.
1318 * If the installed set differs from the set requested by the rib/owner,
1319 * we use the fib-specific nexthop-group to record the actual FIB
1323 /* Check both fib group and notif group for equivalence.
1325 * Let's assume the nexthops are ordered here to save time.
1327 if (nexthop_group_equal(&re
->fib_ng
, dplane_ctx_get_ng(ctx
)) == false) {
1328 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1330 "%u:%s update_from_ctx: notif nh and fib nh mismatch",
1331 re
->vrf_id
, dest_str
);
1338 /* If the new FIB set matches the existing FIB set, we're done. */
1340 if (IS_ZEBRA_DEBUG_RIB
)
1341 zlog_debug("%u:%s update_from_ctx(): existing fib nhg, no change",
1342 re
->vrf_id
, dest_str
);
1345 } else if (re
->fib_ng
.nexthop
) {
1347 * Free stale fib list and move on to check the rib nhg.
1349 if (IS_ZEBRA_DEBUG_RIB
)
1350 zlog_debug("%u:%s update_from_ctx(): replacing fib nhg",
1351 re
->vrf_id
, dest_str
);
1352 nexthops_free(re
->fib_ng
.nexthop
);
1353 re
->fib_ng
.nexthop
= NULL
;
1355 /* Note that the installed nexthops have changed */
1358 if (IS_ZEBRA_DEBUG_RIB
)
1359 zlog_debug("%u:%s update_from_ctx(): no fib nhg",
1360 re
->vrf_id
, dest_str
);
1364 * Compare with the rib nexthop group. The comparison here is different:
1365 * the RIB group may be a superset of the list installed in the FIB. We
1366 * walk the RIB group, looking for the 'installable' candidate
1367 * nexthops, and then check those against the set
1368 * that is actually installed.
1370 * Assume nexthops are ordered here as well.
1374 ctx_nexthop
= dplane_ctx_get_ng(ctx
)->nexthop
;
1376 /* Nothing installed - we can skip some of the checking/comparison
1379 if (ctx_nexthop
== NULL
) {
1384 /* Get the first `installed` one to check against.
1385 * If the dataplane doesn't set these to be what was actually installed,
1386 * it will just be whatever was in re->nhe->nhg?
1388 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
1389 || !CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1390 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1392 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
1394 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1397 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1400 /* Check for a FIB nexthop corresponding to the RIB nexthop */
1401 if (nexthop_same(ctx_nexthop
, nexthop
) == false) {
1402 /* If the FIB doesn't know about the nexthop,
1403 * it's not installed
1405 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1406 nexthop2str(nexthop
, nh_str
, sizeof(nh_str
));
1408 "update_from_ctx: no notif match for rib nh %s",
1413 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1416 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1418 /* Keep checking nexthops */
1422 if (CHECK_FLAG(ctx_nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1423 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1426 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1428 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1431 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1434 ctx_nexthop
= nexthop_next_active_resolved(ctx_nexthop
);
1437 /* If all nexthops were processed, we're done */
1439 if (IS_ZEBRA_DEBUG_RIB
)
1440 zlog_debug("%u:%s update_from_ctx(): rib nhg matched, changed '%s'",
1441 re
->vrf_id
, dest_str
,
1442 (changed_p
? "true" : "false"));
1448 /* FIB nexthop set differs from the RIB set:
1449 * create a fib-specific nexthop-group
1451 if (IS_ZEBRA_DEBUG_RIB
)
1452 zlog_debug("%u:%s update_from_ctx(): changed %s, adding new fib nhg",
1453 re
->vrf_id
, dest_str
,
1454 (changed_p
? "true" : "false"));
1456 ctxnhg
= dplane_ctx_get_ng(ctx
);
1458 if (ctxnhg
->nexthop
)
1459 copy_nexthops(&(re
->fib_ng
.nexthop
), ctxnhg
->nexthop
, NULL
);
1461 /* Bit of a special case when the fib has _no_ installed
1464 nexthop
= nexthop_new();
1465 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
1466 _nexthop_add(&(re
->fib_ng
.nexthop
), nexthop
);
1474 * Helper to locate a zebra route-node from a dplane context. This is used
1475 * when processing dplane results, e.g. Note well: the route-node is returned
1476 * with a ref held - route_unlock_node() must be called eventually.
1478 static struct route_node
*
1479 rib_find_rn_from_ctx(const struct zebra_dplane_ctx
*ctx
)
1481 struct route_table
*table
= NULL
;
1482 struct route_node
*rn
= NULL
;
1483 const struct prefix
*dest_pfx
, *src_pfx
;
1485 /* Locate rn and re(s) from ctx */
1487 table
= zebra_vrf_lookup_table_with_table_id(
1488 dplane_ctx_get_afi(ctx
), dplane_ctx_get_safi(ctx
),
1489 dplane_ctx_get_vrf(ctx
), dplane_ctx_get_table(ctx
));
1490 if (table
== NULL
) {
1491 if (IS_ZEBRA_DEBUG_DPLANE
) {
1492 zlog_debug("Failed to find route for ctx: no table for afi %d, safi %d, vrf %u",
1493 dplane_ctx_get_afi(ctx
),
1494 dplane_ctx_get_safi(ctx
),
1495 dplane_ctx_get_vrf(ctx
));
1500 dest_pfx
= dplane_ctx_get_dest(ctx
);
1501 src_pfx
= dplane_ctx_get_src(ctx
);
1503 rn
= srcdest_rnode_get(table
, dest_pfx
,
1504 src_pfx
? (struct prefix_ipv6
*)src_pfx
: NULL
);
1513 * Route-update results processing after async dataplane update.
1515 static void rib_process_result(struct zebra_dplane_ctx
*ctx
)
1517 struct zebra_vrf
*zvrf
= NULL
;
1518 struct route_node
*rn
= NULL
;
1519 struct route_entry
*re
= NULL
, *old_re
= NULL
, *rib
;
1520 bool is_update
= false;
1521 char dest_str
[PREFIX_STRLEN
] = "";
1522 enum dplane_op_e op
;
1523 enum zebra_dplane_result status
;
1524 const struct prefix
*dest_pfx
, *src_pfx
;
1526 bool fib_changed
= false;
1528 zvrf
= vrf_info_lookup(dplane_ctx_get_vrf(ctx
));
1529 dest_pfx
= dplane_ctx_get_dest(ctx
);
1531 /* Note well: only capturing the prefix string if debug is enabled here;
1532 * unconditional log messages will have to generate the string.
1534 if (IS_ZEBRA_DEBUG_DPLANE
)
1535 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1537 /* Locate rn and re(s) from ctx */
1538 rn
= rib_find_rn_from_ctx(ctx
);
1540 if (IS_ZEBRA_DEBUG_DPLANE
) {
1541 zlog_debug("Failed to process dplane results: no route for %u:%s",
1542 dplane_ctx_get_vrf(ctx
), dest_str
);
1547 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1549 op
= dplane_ctx_get_op(ctx
);
1550 status
= dplane_ctx_get_status(ctx
);
1552 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1553 zlog_debug("%u:%s Processing dplane ctx %p, op %s result %s",
1554 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
,
1555 dplane_op2str(op
), dplane_res2str(status
));
1558 * Update is a bit of a special case, where we may have both old and new
1559 * routes to post-process.
1561 is_update
= dplane_ctx_is_update(ctx
);
1564 * Take a pass through the routes, look for matches with the context
1567 RNODE_FOREACH_RE(rn
, rib
) {
1570 if (rib_route_match_ctx(rib
, ctx
, false))
1574 /* Check for old route match */
1575 if (is_update
&& (old_re
== NULL
)) {
1576 if (rib_route_match_ctx(rib
, ctx
, true /*is_update*/))
1580 /* Have we found the routes we need to work on? */
1581 if (re
&& ((!is_update
|| old_re
)))
1585 seq
= dplane_ctx_get_seq(ctx
);
1588 * Check sequence number(s) to detect stale results before continuing
1591 if (re
->dplane_sequence
!= seq
) {
1592 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1593 zlog_debug("%u:%s Stale dplane result for re %p",
1594 dplane_ctx_get_vrf(ctx
),
1597 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1601 if (old_re
->dplane_sequence
!= dplane_ctx_get_old_seq(ctx
)) {
1602 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1603 zlog_debug("%u:%s Stale dplane result for old_re %p",
1604 dplane_ctx_get_vrf(ctx
),
1607 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_QUEUED
);
1611 case DPLANE_OP_ROUTE_INSTALL
:
1612 case DPLANE_OP_ROUTE_UPDATE
:
1613 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1615 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1616 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1619 * On an update operation from the same route type
1620 * context retrieval currently has no way to know
1621 * which was the old and which was the new.
1622 * So don't unset our flags that we just set.
1623 * We know redistribution is ok because the
1624 * old_re in this case is used for nothing
1625 * more than knowing whom to contact if necessary.
1627 if (old_re
&& old_re
!= re
) {
1628 UNSET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1629 UNSET_FLAG(old_re
->status
,
1630 ROUTE_ENTRY_INSTALLED
);
1633 /* Update zebra route based on the results in
1634 * the context struct.
1638 rib_update_re_from_ctx(re
, rn
, ctx
);
1641 if (IS_ZEBRA_DEBUG_DPLANE_DETAIL
)
1642 zlog_debug("%u:%s no fib change for re",
1649 redistribute_update(dest_pfx
, src_pfx
,
1654 * System routes are weird in that they
1655 * allow multiple to be installed that match
1656 * to the same prefix, so after we get the
1657 * result we need to clean them up so that
1658 * we can actually use them.
1660 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1661 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1662 zebra_rib_fixup_system(rn
);
1667 /* Notify route owner */
1668 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_INSTALLED
);
1672 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1673 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1675 SET_FLAG(old_re
->status
, ROUTE_ENTRY_FAILED
);
1677 zsend_route_notify_owner(re
, dest_pfx
,
1678 ZAPI_ROUTE_FAIL_INSTALL
);
1680 zlog_warn("%u:%s: Route install failed",
1681 dplane_ctx_get_vrf(ctx
),
1682 prefix2str(dest_pfx
,
1683 dest_str
, sizeof(dest_str
)));
1686 case DPLANE_OP_ROUTE_DELETE
:
1688 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1690 * In the delete case, the zebra core datastructs were
1691 * updated (or removed) at the time the delete was issued,
1692 * so we're just notifying the route owner.
1694 if (status
== ZEBRA_DPLANE_REQUEST_SUCCESS
) {
1696 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1697 UNSET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1699 zsend_route_notify_owner_ctx(ctx
, ZAPI_ROUTE_REMOVED
);
1705 SET_FLAG(re
->status
, ROUTE_ENTRY_FAILED
);
1706 zsend_route_notify_owner_ctx(ctx
,
1707 ZAPI_ROUTE_REMOVE_FAIL
);
1709 zlog_warn("%u:%s: Route Deletion failure",
1710 dplane_ctx_get_vrf(ctx
),
1711 prefix2str(dest_pfx
,
1712 dest_str
, sizeof(dest_str
)));
1716 * System routes are weird in that they
1717 * allow multiple to be installed that match
1718 * to the same prefix, so after we get the
1719 * result we need to clean them up so that
1720 * we can actually use them.
1722 if ((re
&& RIB_SYSTEM_ROUTE(re
)) ||
1723 (old_re
&& RIB_SYSTEM_ROUTE(old_re
)))
1724 zebra_rib_fixup_system(rn
);
1730 zebra_rib_evaluate_rn_nexthops(rn
, seq
);
1731 zebra_rib_evaluate_mpls(rn
);
1735 route_unlock_node(rn
);
1737 /* Return context to dataplane module */
1738 dplane_ctx_fini(&ctx
);
1742 * Handle notification from async dataplane: the dataplane has detected
1743 * some change to a route, and notifies zebra so that the control plane
1744 * can reflect that change.
1746 static void rib_process_dplane_notify(struct zebra_dplane_ctx
*ctx
)
1748 struct route_node
*rn
= NULL
;
1749 struct route_entry
*re
= NULL
;
1750 struct nexthop
*nexthop
;
1751 char dest_str
[PREFIX_STRLEN
] = "";
1752 const struct prefix
*dest_pfx
, *src_pfx
;
1754 bool fib_changed
= false;
1755 bool debug_p
= IS_ZEBRA_DEBUG_DPLANE
| IS_ZEBRA_DEBUG_RIB
;
1756 int start_count
, end_count
;
1757 dest_pfx
= dplane_ctx_get_dest(ctx
);
1759 /* Note well: only capturing the prefix string if debug is enabled here;
1760 * unconditional log messages will have to generate the string.
1763 prefix2str(dest_pfx
, dest_str
, sizeof(dest_str
));
1765 /* Locate rn and re(s) from ctx */
1766 rn
= rib_find_rn_from_ctx(ctx
);
1769 zlog_debug("Failed to process dplane notification: no routes for %u:%s",
1770 dplane_ctx_get_vrf(ctx
), dest_str
);
1775 dest
= rib_dest_from_rnode(rn
);
1776 srcdest_rnode_prefixes(rn
, &dest_pfx
, &src_pfx
);
1779 zlog_debug("%u:%s Processing dplane notif ctx %p",
1780 dplane_ctx_get_vrf(ctx
), dest_str
, ctx
);
1783 * Take a pass through the routes, look for matches with the context
1786 RNODE_FOREACH_RE(rn
, re
) {
1787 if (rib_route_match_ctx(re
, ctx
, false /*!update*/))
1791 /* No match? Nothing we can do */
1794 zlog_debug("%u:%s Unable to process dplane notification: no entry for type %s",
1795 dplane_ctx_get_vrf(ctx
), dest_str
,
1797 dplane_ctx_get_type(ctx
)));
1802 /* Ensure we clear the QUEUED flag */
1803 UNSET_FLAG(re
->status
, ROUTE_ENTRY_QUEUED
);
1805 /* Is this a notification that ... matters? We mostly care about
1806 * the route that is currently selected for installation; we may also
1807 * get an un-install notification, and handle that too.
1809 if (re
!= dest
->selected_fib
) {
1811 * If we need to, clean up after a delete that was part of
1812 * an update operation.
1815 for (ALL_NEXTHOPS_PTR(dplane_ctx_get_ng(ctx
), nexthop
)) {
1816 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1820 /* If no nexthops or none installed, ensure that this re
1821 * gets its 'installed' flag cleared.
1823 if (end_count
== 0) {
1824 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))
1825 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1827 zlog_debug("%u:%s dplane notif, uninstalled type %s route",
1828 dplane_ctx_get_vrf(ctx
), dest_str
,
1830 dplane_ctx_get_type(ctx
)));
1832 /* At least report on the event. */
1834 zlog_debug("%u:%s dplane notif, but type %s not selected_fib",
1835 dplane_ctx_get_vrf(ctx
), dest_str
,
1837 dplane_ctx_get_type(ctx
)));
1842 /* We'll want to determine whether the installation status of the
1843 * route has changed: we'll check the status before processing,
1844 * and then again if there's been a change.
1848 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)) {
1849 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1850 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1855 /* Update zebra's nexthop FIB flags based on the context struct's
1858 fib_changed
= rib_update_re_from_ctx(re
, rn
, ctx
);
1862 zlog_debug("%u:%s dplane notification: rib_update returns FALSE",
1863 dplane_ctx_get_vrf(ctx
), dest_str
);
1867 * Perform follow-up work if the actual status of the prefix
1872 for (ALL_NEXTHOPS_PTR(rib_active_nhg(re
), nexthop
)) {
1873 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1877 /* Various fib transitions: changed nexthops; from installed to
1878 * not-installed; or not-installed to installed.
1880 if (start_count
> 0 && end_count
> 0) {
1882 zlog_debug("%u:%s applied nexthop changes from dplane notification",
1883 dplane_ctx_get_vrf(ctx
), dest_str
);
1885 /* Changed nexthops - update kernel/others */
1886 dplane_route_notif_update(rn
, re
,
1887 DPLANE_OP_ROUTE_UPDATE
, ctx
);
1889 } else if (start_count
== 0 && end_count
> 0) {
1891 zlog_debug("%u:%s installed transition from dplane notification",
1892 dplane_ctx_get_vrf(ctx
), dest_str
);
1894 /* We expect this to be the selected route, so we want
1895 * to tell others about this transition.
1897 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1899 /* Changed nexthops - update kernel/others */
1900 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_INSTALL
, ctx
);
1902 /* Redistribute, lsp, and nht update */
1903 redistribute_update(dest_pfx
, src_pfx
, re
, NULL
);
1905 } else if (start_count
> 0 && end_count
== 0) {
1907 zlog_debug("%u:%s un-installed transition from dplane notification",
1908 dplane_ctx_get_vrf(ctx
), dest_str
);
1910 /* Transition from _something_ installed to _nothing_
1913 /* We expect this to be the selected route, so we want
1914 * to tell others about this transistion.
1916 UNSET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
1918 /* Changed nexthops - update kernel/others */
1919 dplane_route_notif_update(rn
, re
, DPLANE_OP_ROUTE_DELETE
, ctx
);
1921 /* Redistribute, lsp, and nht update */
1922 redistribute_delete(dest_pfx
, src_pfx
, re
, NULL
);
1925 /* Make any changes visible for lsp and nexthop-tracking processing */
1926 zebra_rib_evaluate_rn_nexthops(
1927 rn
, zebra_router_get_next_sequence());
1929 zebra_rib_evaluate_mpls(rn
);
1933 route_unlock_node(rn
);
1935 /* Return context to dataplane module */
1936 dplane_ctx_fini(&ctx
);
1939 static void process_subq_nhg(struct listnode
*lnode
)
1941 struct nhg_ctx
*ctx
= NULL
;
1942 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
1944 ctx
= listgetdata(lnode
);
1949 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1950 zlog_debug("NHG Context id=%u dequeued from sub-queue %u",
1953 rib_nhg_process(ctx
);
1956 static void process_subq_route(struct listnode
*lnode
, uint8_t qindex
)
1958 struct route_node
*rnode
= NULL
;
1959 rib_dest_t
*dest
= NULL
;
1960 struct zebra_vrf
*zvrf
= NULL
;
1962 rnode
= listgetdata(lnode
);
1963 dest
= rib_dest_from_rnode(rnode
);
1965 zvrf
= rib_dest_vrf(dest
);
1969 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1970 char buf
[SRCDEST2STR_BUFFER
];
1972 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1973 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1974 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1978 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1979 RIB_ROUTE_QUEUED(qindex
));
1984 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1985 __func__
, rnode
, rnode
->lock
);
1986 zlog_backtrace(LOG_DEBUG
);
1989 route_unlock_node(rnode
);
1992 /* Take a list of route_node structs and return 1, if there was a record
1993 * picked from it and processed by rib_process(). Don't process more,
1994 * than one RN record; operate only in the specified sub-queue.
1996 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
1998 struct listnode
*lnode
= listhead(subq
);
2003 if (qindex
== route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
)
2004 process_subq_nhg(lnode
);
2006 process_subq_route(lnode
, qindex
);
2008 list_delete_node(subq
, lnode
);
2013 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2014 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
2016 * is pointed to the meta queue structure.
2018 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
2020 struct meta_queue
*mq
= data
;
2022 uint32_t queue_len
, queue_limit
;
2024 /* Ensure there's room for more dataplane updates */
2025 queue_limit
= dplane_get_in_queue_limit();
2026 queue_len
= dplane_get_in_queue_len();
2027 if (queue_len
> queue_limit
) {
2028 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2029 zlog_debug("rib queue: dplane queue len %u, limit %u, retrying",
2030 queue_len
, queue_limit
);
2032 /* Ensure that the meta-queue is actually enqueued */
2033 if (work_queue_empty(zrouter
.ribq
))
2034 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2036 return WQ_QUEUE_BLOCKED
;
2039 for (i
= 0; i
< MQ_SIZE
; i
++)
2040 if (process_subq(mq
->subq
[i
], i
)) {
2044 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2049 * Look into the RN and queue it into the highest priority queue
2050 * at this point in time for processing.
2052 * We will enqueue a route node only once per invocation.
2054 * There are two possibilities here that should be kept in mind.
2055 * If the original invocation has not been pulled off for processing
2056 * yet, A subsuquent invocation can have a route entry with a better
2057 * meta queue index value and we can have a situation where
2058 * we might have the same node enqueued 2 times. Not necessarily
2059 * an optimal situation but it should be ok.
2061 * The other possibility is that the original invocation has not
2062 * been pulled off for processing yet, A subsusquent invocation
2063 * doesn't have a route_entry with a better meta-queue and the
2064 * original metaqueue index value will win and we'll end up with
2065 * the route node enqueued once.
2067 static int rib_meta_queue_add(struct meta_queue
*mq
, void *data
)
2069 struct route_node
*rn
= NULL
;
2070 struct route_entry
*re
= NULL
, *curr_re
= NULL
;
2071 uint8_t qindex
= MQ_SIZE
, curr_qindex
= MQ_SIZE
;
2073 rn
= (struct route_node
*)data
;
2075 RNODE_FOREACH_RE (rn
, curr_re
) {
2076 curr_qindex
= route_info
[curr_re
->type
].meta_q_map
;
2078 if (curr_qindex
<= qindex
) {
2080 qindex
= curr_qindex
;
2087 /* Invariant: at this point we always have rn->info set. */
2088 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2089 RIB_ROUTE_QUEUED(qindex
))) {
2090 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2091 rnode_debug(rn
, re
->vrf_id
,
2092 "rn %p is already queued in sub-queue %u",
2093 (void *)rn
, qindex
);
2097 SET_FLAG(rib_dest_from_rnode(rn
)->flags
, RIB_ROUTE_QUEUED(qindex
));
2098 listnode_add(mq
->subq
[qindex
], rn
);
2099 route_lock_node(rn
);
2102 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2103 rnode_debug(rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
2104 (void *)rn
, qindex
);
2109 static int rib_meta_queue_nhg_add(struct meta_queue
*mq
, void *data
)
2111 struct nhg_ctx
*ctx
= NULL
;
2112 uint8_t qindex
= route_info
[ZEBRA_ROUTE_NHG
].meta_q_map
;
2114 ctx
= (struct nhg_ctx
*)data
;
2119 listnode_add(mq
->subq
[qindex
], ctx
);
2122 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2123 zlog_debug("NHG Context id=%u queued into sub-queue %u",
2129 static int mq_add_handler(void *data
,
2130 int (*mq_add_func
)(struct meta_queue
*mq
, void *data
))
2132 if (zrouter
.ribq
== NULL
) {
2133 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2134 "%s: work_queue does not exist!", __func__
);
2139 * The RIB queue should normally be either empty or holding the only
2140 * work_queue_item element. In the latter case this element would
2141 * hold a pointer to the meta queue structure, which must be used to
2142 * actually queue the route nodes to process. So create the MQ
2143 * holder, if necessary, then push the work into it in any case.
2144 * This semantics was introduced after 0.99.9 release.
2146 if (work_queue_empty(zrouter
.ribq
))
2147 work_queue_add(zrouter
.ribq
, zrouter
.mq
);
2149 return mq_add_func(zrouter
.mq
, data
);
2152 /* Add route_node to work queue and schedule processing */
2153 int rib_queue_add(struct route_node
*rn
)
2157 /* Pointless to queue a route_node with no RIB entries to add or remove
2159 if (!rnode_to_ribs(rn
)) {
2160 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2161 __func__
, (void *)rn
, rn
->lock
);
2162 zlog_backtrace(LOG_DEBUG
);
2166 return mq_add_handler(rn
, &rib_meta_queue_add
);
2169 int rib_queue_nhg_add(struct nhg_ctx
*ctx
)
2173 return mq_add_handler(ctx
, &rib_meta_queue_nhg_add
);
2176 /* Create new meta queue.
2177 A destructor function doesn't seem to be necessary here.
2179 static struct meta_queue
*meta_queue_new(void)
2181 struct meta_queue
*new;
2184 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2186 for (i
= 0; i
< MQ_SIZE
; i
++) {
2187 new->subq
[i
] = list_new();
2188 assert(new->subq
[i
]);
2194 void meta_queue_free(struct meta_queue
*mq
)
2198 for (i
= 0; i
< MQ_SIZE
; i
++)
2199 list_delete(&mq
->subq
[i
]);
2201 XFREE(MTYPE_WORK_QUEUE
, mq
);
2204 /* initialise zebra rib work queue */
2205 static void rib_queue_init(void)
2207 if (!(zrouter
.ribq
= work_queue_new(zrouter
.master
,
2208 "route_node processing"))) {
2209 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2210 "%s: could not initialise work queue!", __func__
);
2214 /* fill in the work queue spec */
2215 zrouter
.ribq
->spec
.workfunc
= &meta_queue_process
;
2216 zrouter
.ribq
->spec
.errorfunc
= NULL
;
2217 zrouter
.ribq
->spec
.completion_func
= NULL
;
2218 /* XXX: TODO: These should be runtime configurable via vty */
2219 zrouter
.ribq
->spec
.max_retries
= 3;
2220 zrouter
.ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2221 zrouter
.ribq
->spec
.retry
= ZEBRA_RIB_PROCESS_RETRY_TIME
;
2223 if (!(zrouter
.mq
= meta_queue_new())) {
2224 flog_err(EC_ZEBRA_WQ_NONEXISTENT
,
2225 "%s: could not initialise meta queue!", __func__
);
2231 rib_dest_t
*zebra_rib_create_dest(struct route_node
*rn
)
2235 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2236 rnh_list_init(&dest
->nht
);
2237 route_lock_node(rn
); /* rn route table reference */
2244 /* RIB updates are processed via a queue of pointers to route_nodes.
2246 * The queue length is bounded by the maximal size of the routing table,
2247 * as a route_node will not be requeued, if already queued.
2249 * REs are submitted via rib_addnode or rib_delnode which set minimal
2250 * state, or static_install_route (when an existing RE is updated)
2251 * and then submit route_node to queue for best-path selection later.
2252 * Order of add/delete state changes are preserved for any given RE.
2254 * Deleted REs are reaped during best-path selection.
2257 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2258 * |-------->| | best RE, if required
2260 * static_install->|->rib_addqueue...... -> rib_process
2262 * |-------->| |-> rib_unlink
2263 * |-> set ROUTE_ENTRY_REMOVE |
2264 * rib_delnode (RE freed)
2266 * The 'info' pointer of a route_node points to a rib_dest_t
2267 * ('dest'). Queueing state for a route_node is kept on the dest. The
2268 * dest is created on-demand by rib_link() and is kept around at least
2269 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2271 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2273 * - route_nodes: refcounted by:
2274 * - dest attached to route_node:
2275 * - managed by: rib_link/rib_gc_dest
2276 * - route_node processing queue
2277 * - managed by: rib_addqueue, rib_process.
2281 /* Add RE to head of the route node. */
2282 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2286 const char *rmap_name
;
2290 dest
= rib_dest_from_rnode(rn
);
2292 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2293 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2295 dest
= zebra_rib_create_dest(rn
);
2298 re_list_add_head(&dest
->routes
, re
);
2300 afi
= (rn
->p
.family
== AF_INET
)
2302 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2303 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2304 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2306 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2307 zebra_add_import_table_entry(zvrf
, rn
, re
, rmap_name
);
2312 static void rib_addnode(struct route_node
*rn
,
2313 struct route_entry
*re
, int process
)
2315 /* RE node has been un-removed before route-node is processed.
2316 * route_node must hence already be on the queue for processing..
2318 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2319 if (IS_ZEBRA_DEBUG_RIB
)
2320 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2321 (void *)rn
, (void *)re
);
2323 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2326 rib_link(rn
, re
, process
);
2332 * Detach a rib structure from a route_node.
2334 * Note that a call to rib_unlink() should be followed by a call to
2335 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2336 * longer required to be deleted.
2338 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2341 struct nhg_hash_entry
*nhe
= NULL
;
2345 if (IS_ZEBRA_DEBUG_RIB
)
2346 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2349 dest
= rib_dest_from_rnode(rn
);
2351 re_list_del(&dest
->routes
, re
);
2353 if (dest
->selected_fib
== re
)
2354 dest
->selected_fib
= NULL
;
2357 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2359 zebra_nhg_decrement_ref(nhe
);
2360 } else if (re
->nhe
->nhg
.nexthop
)
2361 nexthops_free(re
->nhe
->nhg
.nexthop
);
2363 nexthops_free(re
->fib_ng
.nexthop
);
2365 XFREE(MTYPE_RE
, re
);
2368 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2372 if (IS_ZEBRA_DEBUG_RIB
)
2373 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2374 (void *)rn
, (void *)re
);
2375 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2377 afi
= (rn
->p
.family
== AF_INET
)
2379 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2380 if (is_zebra_import_table_enabled(afi
, re
->vrf_id
, re
->table
)) {
2381 struct zebra_vrf
*zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
2383 zebra_del_import_table_entry(zvrf
, rn
, re
);
2384 /* Just clean up if non main table */
2385 if (IS_ZEBRA_DEBUG_RIB
) {
2386 char buf
[SRCDEST2STR_BUFFER
];
2387 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2388 zlog_debug("%u:%s: Freeing route rn %p, re %p (%s)",
2389 re
->vrf_id
, buf
, rn
, re
,
2390 zebra_route_string(re
->type
));
2399 /* This function dumps the contents of a given RE entry into
2400 * standard debug log. Calling function name and IP prefix in
2401 * question are passed as 1st and 2nd arguments.
2404 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2405 union prefixconstptr src_pp
,
2406 const struct route_entry
*re
)
2408 const struct prefix
*src_p
= src_pp
.p
;
2409 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2410 char straddr
[PREFIX_STRLEN
];
2411 char srcaddr
[PREFIX_STRLEN
];
2412 char nhname
[PREFIX_STRLEN
];
2413 struct nexthop
*nexthop
;
2414 struct vrf
*vrf
= vrf_lookup_by_id(re
->vrf_id
);
2416 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %s(%u)", func
,
2417 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2418 is_srcdst
? " from " : "",
2419 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2421 VRF_LOGNAME(vrf
), re
->vrf_id
);
2422 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2423 straddr
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2426 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2427 straddr
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2428 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", straddr
,
2429 nexthop_group_nexthop_num(&(re
->nhe
->nhg
)),
2430 nexthop_group_active_nexthop_num(&(re
->nhe
->nhg
)));
2432 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
2433 struct interface
*ifp
;
2434 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2436 switch (nexthop
->type
) {
2437 case NEXTHOP_TYPE_BLACKHOLE
:
2438 sprintf(nhname
, "Blackhole");
2440 case NEXTHOP_TYPE_IFINDEX
:
2441 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2443 sprintf(nhname
, "%s", ifp
? ifp
->name
: "Unknown");
2445 case NEXTHOP_TYPE_IPV4
:
2447 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2448 inet_ntop(AF_INET
, &nexthop
->gate
, nhname
,
2451 case NEXTHOP_TYPE_IPV6
:
2452 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2453 inet_ntop(AF_INET6
, &nexthop
->gate
, nhname
,
2457 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s%s%s",
2458 straddr
, (nexthop
->rparent
? " NH" : "NH"), nhname
,
2459 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2461 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2464 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
)
2467 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2470 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)
2473 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
)
2477 zlog_debug("%s: dump complete", straddr
);
2480 /* This is an exported helper to rtm_read() to dump the strange
2481 * RE entry found by rib_lookup_ipv4_route()
2484 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2486 struct route_table
*table
;
2487 struct route_node
*rn
;
2488 struct route_entry
*re
;
2489 char prefix_buf
[INET_ADDRSTRLEN
];
2492 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2494 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2495 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2500 /* Scan the RIB table for exactly matching RE entry. */
2501 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2503 /* No route for this prefix. */
2505 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2506 prefix2str((struct prefix
*)p
, prefix_buf
,
2507 sizeof(prefix_buf
)));
2512 route_unlock_node(rn
);
2515 RNODE_FOREACH_RE (rn
, re
) {
2516 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2518 (void *)rn
, (void *)re
,
2519 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2522 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2525 route_entry_dump(p
, NULL
, re
);
2529 /* Check if requested address assignment will fail due to another
2530 * route being installed by zebra in FIB already. Take necessary
2531 * actions, if needed: remove such a route from FIB and deSELECT
2532 * corresponding RE entry. Then put affected RN into RIBQ head.
2534 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2536 struct route_table
*table
;
2537 struct route_node
*rn
;
2540 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2541 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2542 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2547 /* No matches would be the simplest case. */
2548 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2552 route_unlock_node(rn
);
2554 dest
= rib_dest_from_rnode(rn
);
2555 /* Check all RE entries. In case any changes have to be done, requeue
2556 * the RN into RIBQ head. If the routing message about the new connected
2557 * route (generated by the IP address we are going to assign very soon)
2558 * comes before the RIBQ is processed, the new RE entry will join
2559 * RIBQ record already on head. This is necessary for proper
2561 * of the rest of the RE.
2563 if (dest
->selected_fib
) {
2564 if (IS_ZEBRA_DEBUG_RIB
) {
2565 char buf
[PREFIX_STRLEN
];
2567 zlog_debug("%u:%s: freeing way for connected prefix",
2568 dest
->selected_fib
->vrf_id
,
2569 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2570 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2572 rib_uninstall(rn
, dest
->selected_fib
);
2577 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2578 struct prefix_ipv6
*src_p
, struct route_entry
*re
,
2579 struct nexthop_group
*ng
)
2581 struct nhg_hash_entry
*nhe
= NULL
;
2582 struct route_table
*table
;
2583 struct route_node
*rn
;
2584 struct route_entry
*same
= NULL
;
2590 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2593 table
= zebra_vrf_get_table_with_table_id(afi
, safi
, re
->vrf_id
,
2597 nexthop_group_delete(&ng
);
2598 XFREE(MTYPE_RE
, re
);
2603 nhe
= zebra_nhg_lookup_id(re
->nhe_id
);
2607 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2608 "Zebra failed to find the nexthop hash entry for id=%u in a route entry",
2610 XFREE(MTYPE_RE
, re
);
2614 nhe
= zebra_nhg_rib_find(0, ng
, afi
);
2617 * The nexthops got copied over into an nhe,
2620 nexthop_group_delete(&ng
);
2623 char buf
[PREFIX_STRLEN
] = "";
2624 char buf2
[PREFIX_STRLEN
] = "";
2627 EC_ZEBRA_TABLE_LOOKUP_FAILED
,
2628 "Zebra failed to find or create a nexthop hash entry for %s%s%s",
2629 prefix2str(p
, buf
, sizeof(buf
)),
2630 src_p
? " from " : "",
2631 src_p
? prefix2str(src_p
, buf2
, sizeof(buf2
))
2634 XFREE(MTYPE_RE
, re
);
2640 * Attach the re to the nhe's nexthop group.
2642 * TODO: This will need to change when we start getting IDs from upper
2643 * level protocols, as the refcnt might be wrong, since it checks
2644 * if old_id != new_id.
2646 route_entry_update_nhe(re
, nhe
);
2648 /* Make it sure prefixlen is applied to the prefix. */
2651 apply_mask_ipv6(src_p
);
2653 /* Set default distance by route type. */
2654 if (re
->distance
== 0)
2655 re
->distance
= route_distance(re
->type
);
2657 /* Lookup route node.*/
2658 rn
= srcdest_rnode_get(table
, p
, src_p
);
2661 * If same type of route are installed, treat it as a implicit
2663 * If the user has specified the No route replace semantics
2664 * for the install don't do a route replace.
2666 RNODE_FOREACH_RE (rn
, same
) {
2667 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2670 if (same
->type
!= re
->type
)
2672 if (same
->instance
!= re
->instance
)
2674 if (same
->type
== ZEBRA_ROUTE_KERNEL
2675 && same
->metric
!= re
->metric
)
2678 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2679 same
->distance
!= re
->distance
)
2683 * We should allow duplicate connected routes
2684 * because of IPv6 link-local routes and unnumbered
2685 * interfaces on Linux.
2687 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2691 /* If this route is kernel/connected route, notify the dataplane. */
2692 if (RIB_SYSTEM_ROUTE(re
)) {
2693 /* Notify dataplane */
2694 dplane_sys_route_add(rn
, re
);
2697 /* Link new re to node.*/
2698 if (IS_ZEBRA_DEBUG_RIB
) {
2699 rnode_debug(rn
, re
->vrf_id
,
2700 "Inserting route rn %p, re %p (%s) existing %p",
2701 rn
, re
, zebra_route_string(re
->type
), same
);
2703 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2704 route_entry_dump(p
, src_p
, re
);
2707 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
2708 rib_addnode(rn
, re
, 1);
2711 /* Free implicit route.*/
2713 rib_delnode(rn
, same
);
2717 route_unlock_node(rn
);
2721 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2722 unsigned short instance
, int flags
, struct prefix
*p
,
2723 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2724 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
,
2725 uint8_t distance
, bool fromkernel
)
2727 struct route_table
*table
;
2728 struct route_node
*rn
;
2729 struct route_entry
*re
;
2730 struct route_entry
*fib
= NULL
;
2731 struct route_entry
*same
= NULL
;
2732 struct nexthop
*rtnh
;
2733 char buf2
[INET6_ADDRSTRLEN
];
2736 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2739 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
2747 apply_mask_ipv6(src_p
);
2749 /* Lookup route node. */
2750 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2752 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2754 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2755 if (src_p
&& src_p
->prefixlen
)
2756 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2760 if (IS_ZEBRA_DEBUG_RIB
) {
2761 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
2763 zlog_debug("%s[%d]:%s%s%s doesn't exist in rib",
2764 vrf
->name
, table_id
, dst_buf
,
2765 (src_buf
[0] != '\0') ? " from " : "",
2771 dest
= rib_dest_from_rnode(rn
);
2772 fib
= dest
->selected_fib
;
2774 /* Lookup same type route. */
2775 RNODE_FOREACH_RE (rn
, re
) {
2776 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2779 if (re
->type
!= type
)
2781 if (re
->instance
!= instance
)
2783 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2784 distance
!= re
->distance
)
2787 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2789 if (re
->type
== ZEBRA_ROUTE_CONNECT
&&
2790 (rtnh
= re
->nhe
->nhg
.nexthop
)
2791 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2792 if (rtnh
->ifindex
!= nh
->ifindex
)
2798 /* Make sure that the route found has the same gateway. */
2799 if (nhe_id
&& re
->nhe_id
== nhe_id
) {
2808 for (ALL_NEXTHOPS(re
->nhe
->nhg
, rtnh
)) {
2810 * No guarantee all kernel send nh with labels
2813 if (nexthop_same_no_labels(rtnh
, nh
)) {
2822 /* If same type of route can't be found and this message is from
2826 * In the past(HA!) we could get here because
2827 * we were receiving a route delete from the
2828 * kernel and we're not marking the proto
2829 * as coming from it's appropriate originator.
2830 * Now that we are properly noticing the fact
2831 * that the kernel has deleted our route we
2832 * are not going to get called in this path
2833 * I am going to leave this here because
2834 * this might still work this way on non-linux
2835 * platforms as well as some weird state I have
2836 * not properly thought of yet.
2837 * If we can show that this code path is
2838 * dead then we can remove it.
2840 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2841 if (IS_ZEBRA_DEBUG_RIB
) {
2842 rnode_debug(rn
, vrf_id
,
2843 "rn %p, re %p (%s) was deleted from kernel, adding",
2845 zebra_route_string(fib
->type
));
2848 UNSET_FLAG(fib
->status
, ROUTE_ENTRY_INSTALLED
);
2850 for (rtnh
= fib
->nhe
->nhg
.nexthop
; rtnh
;
2852 UNSET_FLAG(rtnh
->flags
,
2856 * This is a non FRR route
2857 * as such we should mark
2860 dest
->selected_fib
= NULL
;
2862 /* This means someone else, other than Zebra,
2864 * a Zebra router from the kernel. We will add
2866 rib_install_kernel(rn
, fib
, NULL
);
2869 if (IS_ZEBRA_DEBUG_RIB
) {
2873 "via %s ifindex %d type %d "
2874 "doesn't exist in rib",
2875 inet_ntop(afi2family(afi
),
2882 "type %d doesn't exist in rib",
2885 route_unlock_node(rn
);
2891 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2893 rib_install_kernel(rn
, same
, NULL
);
2894 route_unlock_node(rn
);
2899 /* Special handling for IPv4 or IPv6 routes sourced from
2900 * EVPN - the nexthop (and associated MAC) need to be
2901 * uninstalled if no more refs.
2903 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2904 struct nexthop
*tmp_nh
;
2906 for (ALL_NEXTHOPS(re
->nhe
->nhg
, tmp_nh
)) {
2907 struct ipaddr vtep_ip
;
2909 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2910 if (afi
== AFI_IP
) {
2911 vtep_ip
.ipa_type
= IPADDR_V4
;
2912 memcpy(&(vtep_ip
.ipaddr_v4
),
2913 &(tmp_nh
->gate
.ipv4
),
2914 sizeof(struct in_addr
));
2916 vtep_ip
.ipa_type
= IPADDR_V6
;
2917 memcpy(&(vtep_ip
.ipaddr_v6
),
2918 &(tmp_nh
->gate
.ipv6
),
2919 sizeof(struct in6_addr
));
2921 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2926 /* Notify dplane if system route changes */
2927 if (RIB_SYSTEM_ROUTE(re
))
2928 dplane_sys_route_del(rn
, same
);
2930 rib_delnode(rn
, same
);
2933 route_unlock_node(rn
);
2938 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2939 unsigned short instance
, int flags
, struct prefix
*p
,
2940 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2941 uint32_t nhe_id
, uint32_t table_id
, uint32_t metric
, uint32_t mtu
,
2942 uint8_t distance
, route_tag_t tag
)
2944 struct route_entry
*re
= NULL
;
2945 struct nexthop
*nexthop
= NULL
;
2946 struct nexthop_group
*ng
= NULL
;
2948 /* Allocate new route_entry structure. */
2949 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2951 re
->instance
= instance
;
2952 re
->distance
= distance
;
2954 re
->metric
= metric
;
2956 re
->table
= table_id
;
2957 re
->vrf_id
= vrf_id
;
2958 re
->uptime
= monotime(NULL
);
2960 re
->nhe_id
= nhe_id
;
2962 /* If the owner of the route supplies a shared nexthop-group id,
2963 * we'll use that. Otherwise, pass the nexthop along directly.
2966 ng
= nexthop_group_new();
2969 nexthop
= nexthop_new();
2971 nexthop_group_add_sorted(ng
, nexthop
);
2974 return rib_add_multipath(afi
, safi
, p
, src_p
, re
, ng
);
2977 static const char *rib_update_event2str(rib_update_event_t event
)
2979 const char *ret
= "UNKNOWN";
2982 case RIB_UPDATE_KERNEL
:
2983 ret
= "RIB_UPDATE_KERNEL";
2985 case RIB_UPDATE_RMAP_CHANGE
:
2986 ret
= "RIB_UPDATE_RMAP_CHANGE";
2988 case RIB_UPDATE_OTHER
:
2989 ret
= "RIB_UPDATE_OTHER";
2991 case RIB_UPDATE_MAX
:
2999 /* Schedule route nodes to be processed if they match the type */
3000 static void rib_update_route_node(struct route_node
*rn
, int type
)
3002 struct route_entry
*re
, *next
;
3003 bool re_changed
= false;
3005 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3006 if (type
== ZEBRA_ROUTE_ALL
|| type
== re
->type
) {
3007 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
3016 /* Schedule routes of a particular table (address-family) based on event. */
3017 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
3019 struct route_node
*rn
;
3021 if (IS_ZEBRA_DEBUG_EVENT
) {
3022 struct zebra_vrf
*zvrf
;
3025 zvrf
= table
->info
? ((rib_table_info_t
*)table
->info
)->zvrf
3027 vrf
= zvrf
? zvrf
->vrf
: NULL
;
3029 zlog_debug("%s: %s VRF %s Table %u event %s", __func__
,
3030 table
->info
? afi2str(
3031 ((rib_table_info_t
*)table
->info
)->afi
)
3033 vrf
? vrf
->name
: "Unknown",
3034 zvrf
? zvrf
->table_id
: 0,
3035 rib_update_event2str(event
));
3038 /* Walk all routes and queue for processing, if appropriate for
3039 * the trigger event.
3041 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3043 * If we are looking at a route node and the node
3044 * has already been queued we don't
3045 * need to queue it up again
3048 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
3049 RIB_ROUTE_ANY_QUEUED
))
3053 case RIB_UPDATE_KERNEL
:
3054 rib_update_route_node(rn
, ZEBRA_ROUTE_KERNEL
);
3056 case RIB_UPDATE_RMAP_CHANGE
:
3057 case RIB_UPDATE_OTHER
:
3058 rib_update_route_node(rn
, ZEBRA_ROUTE_ALL
);
3066 static void rib_update_handle_vrf(vrf_id_t vrf_id
, rib_update_event_t event
)
3068 struct route_table
*table
;
3070 if (IS_ZEBRA_DEBUG_EVENT
)
3071 zlog_debug("%s: Handling VRF %s event %s", __func__
,
3072 vrf_id_to_name(vrf_id
), rib_update_event2str(event
));
3074 /* Process routes of interested address-families. */
3075 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
3077 rib_update_table(table
, event
);
3079 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3081 rib_update_table(table
, event
);
3084 static void rib_update_handle_vrf_all(rib_update_event_t event
)
3086 struct zebra_router_table
*zrt
;
3088 if (IS_ZEBRA_DEBUG_EVENT
)
3089 zlog_debug("%s: Handling VRF (ALL) event %s", __func__
,
3090 rib_update_event2str(event
));
3092 /* Just iterate over all the route tables, rather than vrf lookups */
3093 RB_FOREACH (zrt
, zebra_router_table_head
, &zrouter
.tables
)
3094 rib_update_table(zrt
->table
, event
);
3097 struct rib_update_ctx
{
3098 rib_update_event_t event
;
3103 static struct rib_update_ctx
*rib_update_ctx_init(vrf_id_t vrf_id
,
3104 rib_update_event_t event
)
3106 struct rib_update_ctx
*ctx
;
3108 ctx
= XCALLOC(MTYPE_RIB_UPDATE_CTX
, sizeof(struct rib_update_ctx
));
3111 ctx
->vrf_id
= vrf_id
;
3116 static void rib_update_ctx_fini(struct rib_update_ctx
**ctx
)
3118 XFREE(MTYPE_RIB_UPDATE_CTX
, *ctx
);
3121 static int rib_update_handler(struct thread
*thread
)
3123 struct rib_update_ctx
*ctx
;
3125 ctx
= THREAD_ARG(thread
);
3128 rib_update_handle_vrf_all(ctx
->event
);
3130 rib_update_handle_vrf(ctx
->vrf_id
, ctx
->event
);
3132 rib_update_ctx_fini(&ctx
);
3138 * Thread list to ensure we don't schedule a ton of events
3139 * if interfaces are flapping for instance.
3141 static struct thread
*t_rib_update_threads
[RIB_UPDATE_MAX
];
3143 /* Schedule a RIB update event for specific vrf */
3144 void rib_update_vrf(vrf_id_t vrf_id
, rib_update_event_t event
)
3146 struct rib_update_ctx
*ctx
;
3148 ctx
= rib_update_ctx_init(vrf_id
, event
);
3150 /* Don't worry about making sure multiple rib updates for specific vrf
3151 * are scheduled at once for now. If it becomes a problem, we can use a
3152 * lookup of some sort to keep track of running threads via t_vrf_id
3153 * like how we are doing it in t_rib_update_threads[].
3155 thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0, NULL
);
3157 if (IS_ZEBRA_DEBUG_EVENT
)
3158 zlog_debug("%s: Scheduled VRF %s, event %s", __func__
,
3159 vrf_id_to_name(ctx
->vrf_id
),
3160 rib_update_event2str(event
));
3163 /* Schedule a RIB update event for all vrfs */
3164 void rib_update(rib_update_event_t event
)
3166 struct rib_update_ctx
*ctx
;
3168 ctx
= rib_update_ctx_init(0, event
);
3170 ctx
->vrf_all
= true;
3172 if (!thread_add_event(zrouter
.master
, rib_update_handler
, ctx
, 0,
3173 &t_rib_update_threads
[event
]))
3174 rib_update_ctx_fini(&ctx
); /* Already scheduled */
3175 else if (IS_ZEBRA_DEBUG_EVENT
)
3176 zlog_debug("%s: Scheduled VRF (ALL), event %s", __func__
,
3177 rib_update_event2str(event
));
3180 /* Delete self installed routes after zebra is relaunched. */
3181 void rib_sweep_table(struct route_table
*table
)
3183 struct route_node
*rn
;
3184 struct route_entry
*re
;
3185 struct route_entry
*next
;
3186 struct nexthop
*nexthop
;
3191 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3192 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3194 if (IS_ZEBRA_DEBUG_RIB
)
3195 route_entry_dump(&rn
->p
, NULL
, re
);
3197 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3200 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
3204 * If routes are older than startup_time then
3205 * we know we read them in from the kernel.
3206 * As such we can safely remove them.
3208 if (zrouter
.startup_time
< re
->uptime
)
3212 * So we are starting up and have received
3213 * routes from the kernel that we have installed
3214 * from a previous run of zebra but not cleaned
3215 * up ( say a kill -9 )
3216 * But since we haven't actually installed
3217 * them yet( we received them from the kernel )
3218 * we don't think they are active.
3219 * So let's pretend they are active to actually
3221 * In all honesty I'm not sure if we should
3222 * mark them as active when we receive them
3223 * This is startup only so probably ok.
3225 * If we ever decide to move rib_sweep_table
3226 * to a different spot (ie startup )
3227 * this decision needs to be revisited
3229 SET_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
);
3230 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
))
3231 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3233 rib_uninstall_kernel(rn
, re
);
3234 rib_delnode(rn
, re
);
3239 /* Sweep all RIB tables. */
3240 int rib_sweep_route(struct thread
*t
)
3243 struct zebra_vrf
*zvrf
;
3245 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3246 if ((zvrf
= vrf
->info
) == NULL
)
3249 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3250 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3253 zebra_router_sweep_route();
3254 zebra_router_sweep_nhgs();
3259 /* Remove specific by protocol routes from 'table'. */
3260 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
3261 struct route_table
*table
)
3263 struct route_node
*rn
;
3264 struct route_entry
*re
;
3265 struct route_entry
*next
;
3266 unsigned long n
= 0;
3269 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
3270 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
3271 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
3273 if (re
->type
== proto
3274 && re
->instance
== instance
) {
3275 rib_delnode(rn
, re
);
3282 /* Remove specific by protocol routes. */
3283 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
3286 struct zebra_vrf
*zvrf
;
3287 struct other_route_table
*ort
;
3288 unsigned long cnt
= 0;
3290 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
3295 cnt
+= rib_score_proto_table(proto
, instance
,
3296 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3297 + rib_score_proto_table(
3299 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3301 frr_each(otable
, &zvrf
->other_tables
, ort
) cnt
+=
3302 rib_score_proto_table(proto
, instance
, ort
->table
);
3308 /* Close RIB and clean up kernel routes. */
3309 void rib_close_table(struct route_table
*table
)
3311 struct route_node
*rn
;
3312 rib_table_info_t
*info
;
3318 info
= route_table_get_info(table
);
3320 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
3321 dest
= rib_dest_from_rnode(rn
);
3323 if (dest
&& dest
->selected_fib
) {
3324 if (info
->safi
== SAFI_UNICAST
)
3325 hook_call(rib_update
, rn
, NULL
);
3327 rib_uninstall_kernel(rn
, dest
->selected_fib
);
3328 dest
->selected_fib
= NULL
;
3334 * Handler for async dataplane results after a pseudowire installation
3336 static int handle_pw_result(struct zebra_dplane_ctx
*ctx
)
3338 struct zebra_pw
*pw
;
3339 struct zebra_vrf
*vrf
;
3341 /* The pseudowire code assumes success - we act on an error
3342 * result for installation attempts here.
3344 if (dplane_ctx_get_op(ctx
) != DPLANE_OP_PW_INSTALL
)
3347 if (dplane_ctx_get_status(ctx
) != ZEBRA_DPLANE_REQUEST_SUCCESS
) {
3348 vrf
= zebra_vrf_lookup_by_id(dplane_ctx_get_vrf(ctx
));
3349 pw
= zebra_pw_find(vrf
, dplane_ctx_get_ifname(ctx
));
3351 zebra_pw_install_failure(pw
);
3361 * Handle results from the dataplane system. Dequeue update context
3362 * structs, dispatch to appropriate internal handlers.
3364 static int rib_process_dplane_results(struct thread
*thread
)
3366 struct zebra_dplane_ctx
*ctx
;
3367 struct dplane_ctx_q ctxlist
;
3368 bool shut_p
= false;
3370 /* Dequeue a list of completed updates with one lock/unlock cycle */
3373 TAILQ_INIT(&ctxlist
);
3375 /* Take lock controlling queue of results */
3376 frr_with_mutex(&dplane_mutex
) {
3377 /* Dequeue list of context structs */
3378 dplane_ctx_list_append(&ctxlist
, &rib_dplane_q
);
3381 /* Dequeue context block */
3382 ctx
= dplane_ctx_dequeue(&ctxlist
);
3384 /* If we've emptied the results queue, we're done */
3388 /* If zebra is shutting down, avoid processing results,
3389 * just drain the results queue.
3391 shut_p
= atomic_load_explicit(&zrouter
.in_shutdown
,
3392 memory_order_relaxed
);
3395 dplane_ctx_fini(&ctx
);
3397 ctx
= dplane_ctx_dequeue(&ctxlist
);
3404 switch (dplane_ctx_get_op(ctx
)) {
3405 case DPLANE_OP_ROUTE_INSTALL
:
3406 case DPLANE_OP_ROUTE_UPDATE
:
3407 case DPLANE_OP_ROUTE_DELETE
:
3409 /* Bit of special case for route updates
3410 * that were generated by async notifications:
3411 * we don't want to continue processing these
3414 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3415 rib_process_result(ctx
);
3417 dplane_ctx_fini(&ctx
);
3421 case DPLANE_OP_ROUTE_NOTIFY
:
3422 rib_process_dplane_notify(ctx
);
3425 case DPLANE_OP_NH_INSTALL
:
3426 case DPLANE_OP_NH_UPDATE
:
3427 case DPLANE_OP_NH_DELETE
:
3428 zebra_nhg_dplane_result(ctx
);
3431 case DPLANE_OP_LSP_INSTALL
:
3432 case DPLANE_OP_LSP_UPDATE
:
3433 case DPLANE_OP_LSP_DELETE
:
3435 /* Bit of special case for LSP updates
3436 * that were generated by async notifications:
3437 * we don't want to continue processing these.
3439 if (dplane_ctx_get_notif_provider(ctx
) == 0)
3440 zebra_mpls_lsp_dplane_result(ctx
);
3442 dplane_ctx_fini(&ctx
);
3446 case DPLANE_OP_LSP_NOTIFY
:
3447 zebra_mpls_process_dplane_notify(ctx
);
3450 case DPLANE_OP_PW_INSTALL
:
3451 case DPLANE_OP_PW_UNINSTALL
:
3452 handle_pw_result(ctx
);
3455 case DPLANE_OP_SYS_ROUTE_ADD
:
3456 case DPLANE_OP_SYS_ROUTE_DELETE
:
3457 /* No further processing in zebra for these. */
3458 dplane_ctx_fini(&ctx
);
3461 case DPLANE_OP_MAC_INSTALL
:
3462 case DPLANE_OP_MAC_DELETE
:
3463 zebra_vxlan_handle_result(ctx
);
3466 /* Some op codes not handled here */
3467 case DPLANE_OP_ADDR_INSTALL
:
3468 case DPLANE_OP_ADDR_UNINSTALL
:
3469 case DPLANE_OP_NEIGH_INSTALL
:
3470 case DPLANE_OP_NEIGH_UPDATE
:
3471 case DPLANE_OP_NEIGH_DELETE
:
3472 case DPLANE_OP_VTEP_ADD
:
3473 case DPLANE_OP_VTEP_DELETE
:
3474 case DPLANE_OP_NONE
:
3475 /* Don't expect this: just return the struct? */
3476 dplane_ctx_fini(&ctx
);
3479 } /* Dispatch by op code */
3481 ctx
= dplane_ctx_dequeue(&ctxlist
);
3490 * Results are returned from the dataplane subsystem, in the context of
3491 * the dataplane pthread. We enqueue the results here for processing by
3492 * the main thread later.
3494 static int rib_dplane_results(struct dplane_ctx_q
*ctxlist
)
3496 /* Take lock controlling queue of results */
3497 frr_with_mutex(&dplane_mutex
) {
3498 /* Enqueue context blocks */
3499 dplane_ctx_list_append(&rib_dplane_q
, ctxlist
);
3502 /* Ensure event is signalled to zebra main pthread */
3503 thread_add_event(zrouter
.master
, rib_process_dplane_results
, NULL
, 0,
3510 * Ensure there are no empty slots in the route_info array.
3511 * Every route type in zebra should be present there.
3513 static void check_route_info(void)
3515 int len
= array_size(route_info
);
3518 * ZEBRA_ROUTE_SYSTEM is special cased since
3519 * its key is 0 anyway.
3521 * ZEBRA_ROUTE_ALL is also ignored.
3523 for (int i
= 0; i
< len
; i
++) {
3524 if (i
== ZEBRA_ROUTE_SYSTEM
|| i
== ZEBRA_ROUTE_ALL
)
3526 assert(route_info
[i
].key
);
3527 assert(route_info
[i
].meta_q_map
< MQ_SIZE
);
3531 /* Routing information base initialize. */
3538 /* Init dataplane, and register for results */
3539 pthread_mutex_init(&dplane_mutex
, NULL
);
3540 TAILQ_INIT(&rib_dplane_q
);
3541 zebra_dplane_init(rib_dplane_results
);
3547 * Get the first vrf id that is greater than the given vrf id if any.
3549 * Returns true if a vrf id was found, false otherwise.
3551 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3555 vrf
= vrf_lookup_by_id(vrf_id
);
3557 vrf
= RB_NEXT(vrf_id_head
, vrf
);
3559 *next_id_p
= vrf
->vrf_id
;
3568 * rib_tables_iter_next
3570 * Returns the next table in the iteration.
3572 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
3574 struct route_table
*table
;
3577 * Array that helps us go over all AFI/SAFI combinations via one
3580 static const struct {
3584 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
3585 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
3586 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
3591 switch (iter
->state
) {
3593 case RIB_TABLES_ITER_S_INIT
:
3594 iter
->vrf_id
= VRF_DEFAULT
;
3595 iter
->afi_safi_ix
= -1;
3599 case RIB_TABLES_ITER_S_ITERATING
:
3600 iter
->afi_safi_ix
++;
3603 while (iter
->afi_safi_ix
3604 < (int)array_size(afi_safis
)) {
3605 table
= zebra_vrf_table(
3606 afi_safis
[iter
->afi_safi_ix
].afi
,
3607 afi_safis
[iter
->afi_safi_ix
].safi
,
3612 iter
->afi_safi_ix
++;
3616 * Found another table in this vrf.
3622 * Done with all tables in the current vrf, go to the
3626 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3629 iter
->afi_safi_ix
= 0;
3634 case RIB_TABLES_ITER_S_DONE
:
3639 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3641 iter
->state
= RIB_TABLES_ITER_S_DONE
;