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
34 #include "sockunion.h"
35 #include "srcdest_table.h"
39 #include "workqueue.h"
41 #include "zebra/connected.h"
42 #include "zebra/debug.h"
43 #include "zebra/interface.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/rib.h"
47 #include "zebra/zapi_msg.h"
48 #include "zebra/zebra_errors.h"
49 #include "zebra/zebra_memory.h"
50 #include "zebra/zebra_ns.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/zebra_routemap.h"
53 #include "zebra/zebra_vrf.h"
54 #include "zebra/zebra_vxlan.h"
56 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
59 /* Should we allow non Quagga processes to delete our routes */
60 extern int allow_delete
;
62 /* Each route type's string and default distance value. */
66 } route_info
[ZEBRA_ROUTE_MAX
] = {
67 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
68 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
69 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
70 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
71 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
72 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
73 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
74 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
75 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
76 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
77 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
78 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
79 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
80 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
81 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
82 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
83 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
84 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
85 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
86 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
87 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
88 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
89 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
90 [ZEBRA_ROUTE_SHARP
] = {ZEBRA_ROUTE_SHARP
, 150},
92 /* no entry/default: 150 */
95 /* RPF lookup behaviour */
96 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
99 static void __attribute__((format(printf
, 5, 6)))
100 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
101 int priority
, const char *msgfmt
, ...)
103 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
107 va_start(ap
, msgfmt
);
108 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
112 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
113 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
115 if (info
->safi
== SAFI_MULTICAST
)
116 strcat(buf
, " (MRIB)");
118 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
121 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
124 #define rnode_debug(node, vrf_id, ...) \
125 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
126 #define rnode_info(node, ...) \
127 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
129 uint8_t route_distance(int type
)
133 if ((unsigned)type
>= array_size(route_info
))
136 distance
= route_info
[type
].distance
;
141 int is_zebra_valid_kernel_table(uint32_t table_id
)
144 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
145 || (table_id
== RT_TABLE_COMPAT
))
152 int is_zebra_main_routing_table(uint32_t table_id
)
154 if ((table_id
== RT_TABLE_MAIN
)
155 || (table_id
== zebrad
.rtm_table_default
))
160 int zebra_check_addr(const struct prefix
*p
)
162 if (p
->family
== AF_INET
) {
165 addr
= p
->u
.prefix4
.s_addr
;
168 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
169 || IPV4_LINKLOCAL(addr
))
172 if (p
->family
== AF_INET6
) {
173 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
175 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
181 /* Add nexthop to the end of a rib node's nexthop list */
182 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
184 nexthop_add(&re
->ng
.nexthop
, nexthop
);
190 * copy_nexthop - copy a nexthop to the rib structure.
192 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
194 assert(!re
->ng
.nexthop
);
195 copy_nexthops(&re
->ng
.nexthop
, nh
, NULL
);
196 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
200 /* Delete specified nexthop from the list. */
201 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
204 nexthop
->next
->prev
= nexthop
->prev
;
206 nexthop
->prev
->next
= nexthop
->next
;
208 re
->ng
.nexthop
= nexthop
->next
;
213 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
217 struct nexthop
*nexthop
;
219 nexthop
= nexthop_new();
220 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
221 nexthop
->ifindex
= ifindex
;
222 nexthop
->vrf_id
= nh_vrf_id
;
224 route_entry_nexthop_add(re
, nexthop
);
229 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
230 struct in_addr
*ipv4
,
234 struct nexthop
*nexthop
;
236 nexthop
= nexthop_new();
237 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
238 nexthop
->vrf_id
= nh_vrf_id
;
239 nexthop
->gate
.ipv4
= *ipv4
;
241 nexthop
->src
.ipv4
= *src
;
243 route_entry_nexthop_add(re
, nexthop
);
248 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
249 struct in_addr
*ipv4
,
254 struct nexthop
*nexthop
;
255 struct interface
*ifp
;
257 nexthop
= nexthop_new();
258 nexthop
->vrf_id
= nh_vrf_id
;
259 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
260 nexthop
->gate
.ipv4
= *ipv4
;
262 nexthop
->src
.ipv4
= *src
;
263 nexthop
->ifindex
= ifindex
;
264 ifp
= if_lookup_by_index(nexthop
->ifindex
, nh_vrf_id
);
265 /*Pending: need to think if null ifp here is ok during bootup?
266 There was a crash because ifp here was coming to be NULL */
268 if (connected_is_unnumbered(ifp
)
269 || CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
270 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
273 route_entry_nexthop_add(re
, nexthop
);
278 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
279 struct in6_addr
*ipv6
,
282 struct nexthop
*nexthop
;
284 nexthop
= nexthop_new();
285 nexthop
->vrf_id
= nh_vrf_id
;
286 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
287 nexthop
->gate
.ipv6
= *ipv6
;
289 route_entry_nexthop_add(re
, nexthop
);
294 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
295 struct in6_addr
*ipv6
,
299 struct nexthop
*nexthop
;
301 nexthop
= nexthop_new();
302 nexthop
->vrf_id
= nh_vrf_id
;
303 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
304 nexthop
->gate
.ipv6
= *ipv6
;
305 nexthop
->ifindex
= ifindex
;
306 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
307 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
309 route_entry_nexthop_add(re
, nexthop
);
314 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
315 enum blackhole_type bh_type
)
317 struct nexthop
*nexthop
;
319 nexthop
= nexthop_new();
320 nexthop
->vrf_id
= VRF_DEFAULT
;
321 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
322 nexthop
->bh_type
= bh_type
;
324 route_entry_nexthop_add(re
, nexthop
);
329 static void nexthop_set_resolved(afi_t afi
, const struct nexthop
*newhop
,
330 struct nexthop
*nexthop
)
332 struct nexthop
*resolved_hop
;
334 resolved_hop
= nexthop_new();
335 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
337 resolved_hop
->vrf_id
= nexthop
->vrf_id
;
338 switch (newhop
->type
) {
339 case NEXTHOP_TYPE_IPV4
:
340 case NEXTHOP_TYPE_IPV4_IFINDEX
:
341 /* If the resolving route specifies a gateway, use it */
342 resolved_hop
->type
= newhop
->type
;
343 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
345 if (newhop
->ifindex
) {
346 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
347 resolved_hop
->ifindex
= newhop
->ifindex
;
348 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
349 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
352 case NEXTHOP_TYPE_IPV6
:
353 case NEXTHOP_TYPE_IPV6_IFINDEX
:
354 resolved_hop
->type
= newhop
->type
;
355 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
357 if (newhop
->ifindex
) {
358 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
359 resolved_hop
->ifindex
= newhop
->ifindex
;
362 case NEXTHOP_TYPE_IFINDEX
:
363 /* If the resolving route is an interface route,
364 * it means the gateway we are looking up is connected
365 * to that interface. (The actual network is _not_ onlink).
366 * Therefore, the resolved route should have the original
367 * gateway as nexthop as it is directly connected.
369 * On Linux, we have to set the onlink netlink flag because
370 * otherwise, the kernel won't accept the route.
372 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
374 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
375 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
376 } else if (afi
== AFI_IP6
) {
377 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
378 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
380 resolved_hop
->ifindex
= newhop
->ifindex
;
382 case NEXTHOP_TYPE_BLACKHOLE
:
383 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
384 resolved_hop
->bh_type
= nexthop
->bh_type
;
388 /* Copy labels of the resolved route */
389 if (newhop
->nh_label
)
390 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
391 newhop
->nh_label
->num_labels
,
392 &newhop
->nh_label
->label
[0]);
394 resolved_hop
->rparent
= nexthop
;
395 nexthop_add(&nexthop
->resolved
, resolved_hop
);
398 /* If force flag is not set, do not modify falgs at all for uninstall
399 the route from FIB. */
400 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
401 struct nexthop
*nexthop
, int set
,
402 struct route_node
*top
)
405 struct route_table
*table
;
406 struct route_node
*rn
;
407 struct route_entry
*match
= NULL
;
409 struct nexthop
*newhop
;
410 struct interface
*ifp
;
413 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
414 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
415 nexthop
->ifindex
= 0;
418 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
419 nexthops_free(nexthop
->resolved
);
420 nexthop
->resolved
= NULL
;
424 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
425 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
428 /* Skip nexthops that have been filtered out due to route-map */
429 /* The nexthops are specific to this route and so the same */
430 /* nexthop for a different route may not have this flag set */
431 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
435 * Check to see if we should trust the passed in information
436 * for UNNUMBERED interfaces as that we won't find the GW
437 * address in the routing table.
439 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
440 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
441 if (ifp
&& connected_is_unnumbered(ifp
)) {
442 if (if_is_operative(ifp
))
450 /* Make lookup prefix. */
451 memset(&p
, 0, sizeof(struct prefix
));
455 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
456 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
460 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
461 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
464 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
468 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, nexthop
->vrf_id
);
472 rn
= route_node_match(table
, (struct prefix
*)&p
);
474 route_unlock_node(rn
);
476 /* Lookup should halt if we've matched against ourselves ('top',
477 * if specified) - i.e., we cannot have a nexthop NH1 is
478 * resolved by a route NH1. The exception is if the route is a
481 if (top
&& rn
== top
)
482 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32))
483 || ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128)))
486 /* Pick up selected route. */
487 /* However, do not resolve over default route unless explicitly
489 if (is_default_prefix(&rn
->p
)
490 && !rnh_resolve_via_default(p
.family
))
493 dest
= rib_dest_from_rnode(rn
);
494 if (dest
&& dest
->selected_fib
495 && !CHECK_FLAG(dest
->selected_fib
->status
,
497 && dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
498 match
= dest
->selected_fib
;
500 /* If there is no selected route or matched route is EGP, go up
505 } while (rn
&& rn
->info
== NULL
);
512 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
513 /* Directly point connected route. */
514 newhop
= match
->ng
.nexthop
;
516 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
517 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
518 nexthop
->ifindex
= newhop
->ifindex
;
521 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_ALLOW_RECURSION
)) {
523 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
524 if (!CHECK_FLAG(newhop
->flags
,
527 if (CHECK_FLAG(newhop
->flags
,
528 NEXTHOP_FLAG_RECURSIVE
))
532 SET_FLAG(nexthop
->flags
,
533 NEXTHOP_FLAG_RECURSIVE
);
535 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
536 nexthop_set_resolved(afi
, newhop
,
542 re
->nexthop_mtu
= match
->mtu
;
544 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
546 for (ALL_NEXTHOPS(match
->ng
, newhop
)) {
547 if (!CHECK_FLAG(newhop
->flags
,
552 SET_FLAG(nexthop
->flags
,
553 NEXTHOP_FLAG_RECURSIVE
);
554 nexthop_set_resolved(afi
, newhop
,
560 re
->nexthop_mtu
= match
->mtu
;
569 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
570 union g_addr
*addr
, struct route_node
**rn_out
)
573 struct route_table
*table
;
574 struct route_node
*rn
;
575 struct route_entry
*match
= NULL
;
576 struct nexthop
*newhop
;
579 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
583 memset(&p
, 0, sizeof(struct prefix
));
586 p
.u
.prefix4
= addr
->ipv4
;
587 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
589 p
.u
.prefix6
= addr
->ipv6
;
590 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
593 rn
= route_node_match(table
, (struct prefix
*)&p
);
598 route_unlock_node(rn
);
600 dest
= rib_dest_from_rnode(rn
);
601 if (dest
&& dest
->selected_fib
602 && !CHECK_FLAG(dest
->selected_fib
->status
,
603 ROUTE_ENTRY_REMOVED
))
604 match
= dest
->selected_fib
;
606 /* If there is no selected route or matched route is EGP, go up
611 } while (rn
&& rn
->info
== NULL
);
615 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
617 for (ALL_NEXTHOPS(match
->ng
, newhop
))
618 if (CHECK_FLAG(newhop
->flags
,
635 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
637 struct route_node
**rn_out
)
639 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
640 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
641 union g_addr gaddr
= {.ipv4
= addr
};
643 switch (ipv4_multicast_mode
) {
644 case MCAST_MRIB_ONLY
:
645 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
647 case MCAST_URIB_ONLY
:
648 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
649 case MCAST_NO_CONFIG
:
650 case MCAST_MIX_MRIB_FIRST
:
651 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
654 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
657 case MCAST_MIX_DISTANCE
:
658 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
659 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
661 re
= ure
->distance
< mre
->distance
? ure
: mre
;
667 case MCAST_MIX_PFXLEN
:
668 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
669 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
671 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
680 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
682 if (IS_ZEBRA_DEBUG_RIB
) {
684 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
686 zlog_debug("%s: %s: vrf: %u found %s, using %s",
687 __func__
, buf
, vrf_id
,
688 mre
? (ure
? "MRIB+URIB" : "MRIB")
689 : ure
? "URIB" : "nothing",
690 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
695 void multicast_mode_ipv4_set(enum multicast_mode mode
)
697 if (IS_ZEBRA_DEBUG_RIB
)
698 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
700 ipv4_multicast_mode
= mode
;
703 enum multicast_mode
multicast_mode_ipv4_get(void)
705 return ipv4_multicast_mode
;
708 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
710 struct route_table
*table
;
711 struct route_node
*rn
;
712 struct route_entry
*match
= NULL
;
713 struct nexthop
*nexthop
;
717 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
721 rn
= route_node_lookup(table
, (struct prefix
*)p
);
723 /* No route for this prefix. */
728 route_unlock_node(rn
);
729 dest
= rib_dest_from_rnode(rn
);
731 if (dest
&& dest
->selected_fib
732 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
733 match
= dest
->selected_fib
;
738 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
741 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
742 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
749 * This clone function, unlike its original rib_lookup_ipv4(), checks
750 * if specified IPv4 route record (prefix/mask -> gate) exists in
751 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
755 * 0: exact match found
756 * 1: a match was found with a different gate
757 * 2: connected route found
758 * 3: no matches found
760 int rib_lookup_ipv4_route(struct prefix_ipv4
*p
, union sockunion
*qgate
,
763 struct route_table
*table
;
764 struct route_node
*rn
;
765 struct route_entry
*match
= NULL
;
766 struct nexthop
*nexthop
;
771 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
773 return ZEBRA_RIB_LOOKUP_ERROR
;
775 /* Scan the RIB table for exactly matching RIB entry. */
776 rn
= route_node_lookup(table
, (struct prefix
*)p
);
778 /* No route for this prefix. */
780 return ZEBRA_RIB_NOTFOUND
;
783 route_unlock_node(rn
);
784 dest
= rib_dest_from_rnode(rn
);
786 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
788 if (dest
&& dest
->selected_fib
789 && !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
790 match
= dest
->selected_fib
;
792 /* None such found :( */
794 return ZEBRA_RIB_NOTFOUND
;
796 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
797 return ZEBRA_RIB_FOUND_CONNECTED
;
799 /* Ok, we have a cood candidate, let's check it's nexthop list... */
801 for (ALL_NEXTHOPS(match
->ng
, nexthop
))
802 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
804 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip(qgate
))
805 return ZEBRA_RIB_FOUND_EXACT
;
806 if (IS_ZEBRA_DEBUG_RIB
) {
807 char gate_buf
[INET_ADDRSTRLEN
],
808 qgate_buf
[INET_ADDRSTRLEN
];
809 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
.s_addr
,
810 gate_buf
, INET_ADDRSTRLEN
);
811 inet_ntop(AF_INET
, &sockunion2ip(qgate
),
812 qgate_buf
, INET_ADDRSTRLEN
);
813 zlog_debug("%s: qgate == %s, %s == %s",
815 nexthop
->rparent
? "rgate" : "gate",
821 return ZEBRA_RIB_FOUND_NOGATE
;
823 return ZEBRA_RIB_NOTFOUND
;
826 #define RIB_SYSTEM_ROUTE(R) \
827 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
829 /* This function verifies reachability of one given nexthop, which can be
830 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
831 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
832 * nexthop->ifindex will be updated appropriately as well.
833 * An existing route map can turn (otherwise active) nexthop into inactive, but
836 * The return value is the final value of 'ACTIVE' flag.
839 static unsigned nexthop_active_check(struct route_node
*rn
,
840 struct route_entry
*re
,
841 struct nexthop
*nexthop
, int set
)
843 struct interface
*ifp
;
844 route_map_result_t ret
= RMAP_MATCH
;
846 char buf
[SRCDEST2STR_BUFFER
];
847 const struct prefix
*p
, *src_p
;
848 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
850 if (rn
->p
.family
== AF_INET
)
852 else if (rn
->p
.family
== AF_INET6
)
856 switch (nexthop
->type
) {
857 case NEXTHOP_TYPE_IFINDEX
:
858 ifp
= if_lookup_by_index(nexthop
->ifindex
, nexthop
->vrf_id
);
859 if (ifp
&& if_is_operative(ifp
))
860 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
862 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
864 case NEXTHOP_TYPE_IPV4
:
865 case NEXTHOP_TYPE_IPV4_IFINDEX
:
867 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
868 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
872 case NEXTHOP_TYPE_IPV6
:
874 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
875 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
877 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
879 case NEXTHOP_TYPE_IPV6_IFINDEX
:
880 /* RFC 5549, v4 prefix with v6 NH */
881 if (rn
->p
.family
!= AF_INET
)
883 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
884 ifp
= if_lookup_by_index(nexthop
->ifindex
,
886 if (ifp
&& if_is_operative(ifp
))
887 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
889 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
891 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
892 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
894 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
897 case NEXTHOP_TYPE_BLACKHOLE
:
898 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
903 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
906 /* XXX: What exactly do those checks do? Do we support
907 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
908 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
909 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
910 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
912 /* The original code didn't determine the family correctly
913 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
914 * from the rib_table_info in those cases.
915 * Possibly it may be better to use only the rib_table_info
919 rib_table_info_t
*info
;
921 info
= srcdest_rnode_table_info(rn
);
925 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
927 /* It'll get set if required inside */
928 ret
= zebra_route_map_check(family
, re
->type
, re
->instance
, p
, nexthop
,
929 nexthop
->vrf_id
, re
->tag
);
930 if (ret
== RMAP_DENYMATCH
) {
931 if (IS_ZEBRA_DEBUG_RIB
) {
932 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
934 "%u:%s: Filtering out with NH out %s due to route map",
936 ifindex2ifname(nexthop
->ifindex
,
939 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
941 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
944 /* Iterate over all nexthops of the given RIB entry and refresh their
945 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
946 * nexthop is found to toggle the ACTIVE flag, the whole re structure
947 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
948 * transparently passed to nexthop_active_check().
950 * Return value is the new number of active nexthops.
953 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
956 struct nexthop
*nexthop
;
957 union g_addr prev_src
;
958 unsigned int prev_active
, new_active
, old_num_nh
;
959 ifindex_t prev_index
;
960 old_num_nh
= re
->nexthop_active_num
;
962 re
->nexthop_active_num
= 0;
963 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
965 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
) {
966 /* No protocol daemon provides src and so we're skipping
968 prev_src
= nexthop
->rmap_src
;
969 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
970 prev_index
= nexthop
->ifindex
;
971 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
972 re
->nexthop_active_num
++;
973 /* Don't allow src setting on IPv6 addr for now */
974 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
975 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
976 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
977 && prev_src
.ipv4
.s_addr
978 != nexthop
->rmap_src
.ipv4
.s_addr
)
979 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
980 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
981 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
982 &nexthop
->rmap_src
.ipv6
)))) {
983 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
984 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
988 if (old_num_nh
!= re
->nexthop_active_num
)
989 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
991 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
992 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
995 return re
->nexthop_active_num
;
999 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1000 * (nexthops) must have a label.
1002 int zebra_rib_labeled_unicast(struct route_entry
*re
)
1004 struct nexthop
*nexthop
= NULL
;
1006 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1009 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1010 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1016 void kernel_route_rib_pass_fail(struct route_node
*rn
, const struct prefix
*p
,
1017 struct route_entry
*re
,
1018 enum dp_results res
)
1020 struct nexthop
*nexthop
;
1021 char buf
[PREFIX_STRLEN
];
1024 dest
= rib_dest_from_rnode(rn
);
1027 case DP_INSTALL_SUCCESS
:
1028 dest
->selected_fib
= re
;
1029 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1030 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1033 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1034 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1036 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1038 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_INSTALLED
);
1040 case DP_INSTALL_FAILURE
:
1042 * I am not sure this is the right thing to do here
1043 * but the code always set selected_fib before
1044 * this assignment was moved here.
1046 dest
->selected_fib
= re
;
1048 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_FAIL_INSTALL
);
1049 flog_err(ZEBRA_ERR_DP_INSTALL_FAIL
,
1050 "%u:%s: Route install failed", re
->vrf_id
,
1051 prefix2str(p
, buf
, sizeof(buf
)));
1053 case DP_DELETE_SUCCESS
:
1055 * The case where selected_fib is not re is
1056 * when we have received a system route
1057 * that is overriding our installed route
1058 * as such we should leave the selected_fib
1061 if (dest
->selected_fib
== re
)
1062 dest
->selected_fib
= NULL
;
1063 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1064 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1066 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_REMOVED
);
1068 case DP_DELETE_FAILURE
:
1070 * Should we set this to NULL if the
1073 dest
->selected_fib
= NULL
;
1074 flog_err(ZEBRA_ERR_DP_DELETE_FAIL
,
1075 "%u:%s: Route Deletion failure", re
->vrf_id
,
1076 prefix2str(p
, buf
, sizeof(buf
)));
1078 zsend_route_notify_owner(re
, p
, ZAPI_ROUTE_REMOVE_FAIL
);
1083 /* Update flag indicates whether this is a "replace" or not. Currently, this
1084 * is only used for IPv4.
1086 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1087 struct route_entry
*old
)
1089 struct nexthop
*nexthop
;
1090 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1091 const struct prefix
*p
, *src_p
;
1092 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1094 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1096 if (info
->safi
!= SAFI_UNICAST
) {
1097 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1098 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1101 struct nexthop
*prev
;
1103 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
1104 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1105 for (ALL_NEXTHOPS(re
->ng
, prev
)) {
1106 if (prev
== nexthop
)
1108 if (nexthop_same_firsthop(nexthop
, prev
)) {
1109 SET_FLAG(nexthop
->flags
,
1110 NEXTHOP_FLAG_DUPLICATE
);
1118 * If this is a replace to a new RE let the originator of the RE
1119 * know that they've lost
1121 if (old
&& (old
!= re
) && (old
->type
!= re
->type
))
1122 zsend_route_notify_owner(old
, p
, ZAPI_ROUTE_BETTER_ADMIN_WON
);
1125 * Make sure we update the FPM any time we send new information to
1128 hook_call(rib_update
, rn
, "installing in kernel");
1129 switch (kernel_route_rib(rn
, p
, src_p
, old
, re
)) {
1130 case DP_REQUEST_QUEUED
:
1132 ZEBRA_ERR_DP_INVALID_RC
,
1133 "No current known DataPlane interfaces can return this, please fix");
1135 case DP_REQUEST_FAILURE
:
1137 ZEBRA_ERR_DP_INSTALL_FAIL
,
1138 "No current known Rib Install Failure cases, please fix");
1140 case DP_REQUEST_SUCCESS
:
1148 /* Uninstall the route from kernel. */
1149 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1151 struct nexthop
*nexthop
;
1152 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1153 const struct prefix
*p
, *src_p
;
1154 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1156 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1158 if (info
->safi
!= SAFI_UNICAST
) {
1159 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
1160 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1165 * Make sure we update the FPM any time we send new information to
1168 hook_call(rib_update
, rn
, "uninstalling from kernel");
1169 switch (kernel_route_rib(rn
, p
, src_p
, re
, NULL
)) {
1170 case DP_REQUEST_QUEUED
:
1172 ZEBRA_ERR_DP_INVALID_RC
,
1173 "No current known DataPlane interfaces can return this, please fix");
1175 case DP_REQUEST_FAILURE
:
1177 ZEBRA_ERR_DP_INSTALL_FAIL
,
1178 "No current known RIB Install Failure cases, please fix");
1180 case DP_REQUEST_SUCCESS
:
1189 /* Uninstall the route from kernel. */
1190 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1192 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1193 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1195 if (dest
&& dest
->selected_fib
== re
) {
1196 if (info
->safi
== SAFI_UNICAST
)
1197 hook_call(rib_update
, rn
, "rib_uninstall");
1199 if (!RIB_SYSTEM_ROUTE(re
))
1200 rib_uninstall_kernel(rn
, re
);
1202 /* If labeled-unicast route, uninstall transit LSP. */
1203 if (zebra_rib_labeled_unicast(re
))
1204 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1207 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1208 const struct prefix
*p
, *src_p
;
1210 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1212 redistribute_delete(p
, src_p
, re
);
1213 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1218 * rib_can_delete_dest
1220 * Returns TRUE if the given dest can be deleted from the table.
1222 static int rib_can_delete_dest(rib_dest_t
*dest
)
1229 * Don't delete the dest if we have to update the FPM about this
1232 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1233 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1242 * Garbage collect the rib dest corresponding to the given route node
1245 * Returns TRUE if the dest was deleted, FALSE otherwise.
1247 int rib_gc_dest(struct route_node
*rn
)
1251 dest
= rib_dest_from_rnode(rn
);
1255 if (!rib_can_delete_dest(dest
))
1258 if (IS_ZEBRA_DEBUG_RIB
) {
1259 struct zebra_vrf
*zvrf
;
1261 zvrf
= rib_dest_vrf(dest
);
1262 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1266 XFREE(MTYPE_RIB_DEST
, dest
);
1270 * Release the one reference that we keep on the route node.
1272 route_unlock_node(rn
);
1276 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1277 struct route_entry
*new)
1279 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1281 hook_call(rib_update
, rn
, "new route selected");
1283 /* Update real nexthop. This may actually determine if nexthop is active
1285 if (!nexthop_active_update(rn
, new, 1)) {
1286 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1290 if (IS_ZEBRA_DEBUG_RIB
) {
1291 char buf
[SRCDEST2STR_BUFFER
];
1292 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1293 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1294 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1297 /* If labeled-unicast route, install transit LSP. */
1298 if (zebra_rib_labeled_unicast(new))
1299 zebra_mpls_lsp_install(zvrf
, rn
, new);
1301 if (!RIB_SYSTEM_ROUTE(new))
1302 rib_install_kernel(rn
, new, NULL
);
1304 dest
->selected_fib
= new;
1306 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1309 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1310 struct route_entry
*old
)
1312 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1313 hook_call(rib_update
, rn
, "removing existing route");
1315 /* Uninstall from kernel. */
1316 if (IS_ZEBRA_DEBUG_RIB
) {
1317 char buf
[SRCDEST2STR_BUFFER
];
1318 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1319 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1320 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1323 /* If labeled-unicast route, uninstall transit LSP. */
1324 if (zebra_rib_labeled_unicast(old
))
1325 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1327 if (!RIB_SYSTEM_ROUTE(old
))
1328 rib_uninstall_kernel(rn
, old
);
1331 * We are setting this to NULL here
1332 * because that is what we traditionally
1333 * have been doing. I am not positive
1334 * that this is the right thing to do
1335 * but let's leave the code alone
1336 * for the RIB_SYSTEM_ROUTE case
1338 dest
->selected_fib
= NULL
;
1341 /* Update nexthop for route, reset changed flag. */
1342 nexthop_active_update(rn
, old
, 1);
1343 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1346 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1347 struct route_node
*rn
,
1348 struct route_entry
*old
,
1349 struct route_entry
*new)
1351 struct nexthop
*nexthop
= NULL
;
1353 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1356 * We have to install or update if a new route has been selected or
1357 * something has changed.
1359 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1360 hook_call(rib_update
, rn
, "updating existing route");
1362 /* Update the nexthop; we could determine here that nexthop is
1364 if (nexthop_active_update(rn
, new, 1))
1367 /* If nexthop is active, install the selected route, if
1369 * the install succeeds, cleanup flags for prior route, if
1374 if (IS_ZEBRA_DEBUG_RIB
) {
1375 char buf
[SRCDEST2STR_BUFFER
];
1376 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1379 "%u:%s: Updating route rn %p, re %p (type %d) "
1381 zvrf_id(zvrf
), buf
, rn
, new,
1382 new->type
, old
, old
->type
);
1385 "%u:%s: Updating route rn %p, re %p (type %d)",
1386 zvrf_id(zvrf
), buf
, rn
, new,
1390 /* If labeled-unicast route, uninstall transit LSP. */
1391 if (zebra_rib_labeled_unicast(old
))
1392 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1394 /* Non-system route should be installed. */
1395 if (!RIB_SYSTEM_ROUTE(new)) {
1396 /* If labeled-unicast route, install transit
1398 if (zebra_rib_labeled_unicast(new))
1399 zebra_mpls_lsp_install(zvrf
, rn
, new);
1401 rib_install_kernel(rn
, new, old
);
1404 * We do not need to install the
1405 * selected route because it
1406 * is already isntalled by
1407 * the system( ie not us )
1408 * so just mark it as winning
1409 * we do need to ensure that
1410 * if we uninstall a route
1411 * from ourselves we don't
1412 * over write this pointer
1414 dest
->selected_fib
= NULL
;
1416 /* If install succeeded or system route, cleanup flags
1417 * for prior route. */
1419 if (RIB_SYSTEM_ROUTE(new)) {
1420 if (!RIB_SYSTEM_ROUTE(old
))
1421 rib_uninstall_kernel(rn
, old
);
1423 for (nexthop
= old
->ng
.nexthop
; nexthop
;
1424 nexthop
= nexthop
->next
)
1425 UNSET_FLAG(nexthop
->flags
,
1432 * If nexthop for selected route is not active or install
1434 * may need to uninstall and delete for redistribution.
1437 if (IS_ZEBRA_DEBUG_RIB
) {
1438 char buf
[SRCDEST2STR_BUFFER
];
1439 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1442 "%u:%s: Deleting route rn %p, re %p (type %d) "
1443 "old %p (type %d) - nexthop inactive",
1444 zvrf_id(zvrf
), buf
, rn
, new,
1445 new->type
, old
, old
->type
);
1448 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1449 zvrf_id(zvrf
), buf
, rn
, new,
1453 /* If labeled-unicast route, uninstall transit LSP. */
1454 if (zebra_rib_labeled_unicast(old
))
1455 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1457 if (!RIB_SYSTEM_ROUTE(old
))
1458 rib_uninstall_kernel(rn
, old
);
1460 dest
->selected_fib
= NULL
;
1464 * Same route selected; check if in the FIB and if not,
1466 * is housekeeping code to deal with race conditions in kernel
1468 * netlink reporting interface up before IPv4 or IPv6 protocol
1472 if (!RIB_SYSTEM_ROUTE(new)) {
1473 bool in_fib
= false;
1475 for (ALL_NEXTHOPS(new->ng
, nexthop
))
1476 if (CHECK_FLAG(nexthop
->flags
,
1477 NEXTHOP_FLAG_FIB
)) {
1482 rib_install_kernel(rn
, new, NULL
);
1486 /* Update prior route. */
1488 /* Set real nexthop. */
1489 nexthop_active_update(rn
, old
, 1);
1490 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1493 /* Clear changed flag. */
1494 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1497 /* Check if 'alternate' RIB entry is better than 'current'. */
1498 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1499 struct route_entry
*alternate
)
1501 if (current
== NULL
)
1504 /* filter route selection in following order:
1505 * - connected beats other types
1506 * - if both connected, loopback or vrf wins
1507 * - lower distance beats higher
1508 * - lower metric beats higher for equal distance
1509 * - last, hence oldest, route wins tie break.
1512 /* Connected routes. Check to see if either are a vrf
1513 * or loopback interface. If not, pick the last connected
1514 * route of the set of lowest metric connected routes.
1516 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1517 if (current
->type
!= ZEBRA_ROUTE_CONNECT
)
1520 /* both are connected. are either loop or vrf? */
1521 struct nexthop
*nexthop
= NULL
;
1523 for (ALL_NEXTHOPS(alternate
->ng
, nexthop
)) {
1524 if (if_is_loopback_or_vrf(if_lookup_by_index(
1525 nexthop
->ifindex
, alternate
->vrf_id
)))
1529 for (ALL_NEXTHOPS(current
->ng
, nexthop
)) {
1530 if (if_is_loopback_or_vrf(if_lookup_by_index(
1531 nexthop
->ifindex
, current
->vrf_id
)))
1535 /* Neither are loop or vrf so pick best metric */
1536 if (alternate
->metric
<= current
->metric
)
1542 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1545 /* higher distance loses */
1546 if (alternate
->distance
< current
->distance
)
1548 if (current
->distance
< alternate
->distance
)
1551 /* metric tie-breaks equal distance */
1552 if (alternate
->metric
<= current
->metric
)
1558 /* Core function for processing routing information base. */
1559 static void rib_process(struct route_node
*rn
)
1561 struct route_entry
*re
;
1562 struct route_entry
*next
;
1563 struct route_entry
*old_selected
= NULL
;
1564 struct route_entry
*new_selected
= NULL
;
1565 struct route_entry
*old_fib
= NULL
;
1566 struct route_entry
*new_fib
= NULL
;
1567 struct route_entry
*best
= NULL
;
1568 char buf
[SRCDEST2STR_BUFFER
];
1570 struct zebra_vrf
*zvrf
= NULL
;
1571 const struct prefix
*p
, *src_p
;
1573 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1574 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1578 dest
= rib_dest_from_rnode(rn
);
1580 zvrf
= rib_dest_vrf(dest
);
1581 vrf_id
= zvrf_id(zvrf
);
1584 if (IS_ZEBRA_DEBUG_RIB
)
1585 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1587 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1588 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1591 * we can have rn's that have a NULL info pointer
1592 * (dest). As such let's not let the deref happen
1593 * additionally we know RNODE_FOREACH_RE_SAFE
1594 * will not iterate so we are ok.
1597 old_fib
= dest
->selected_fib
;
1599 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1600 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1602 "%u:%s: Examine re %p (type %d) status %x flags %x "
1603 "dist %d metric %d",
1604 vrf_id
, buf
, re
, re
->type
, re
->status
,
1605 re
->flags
, re
->distance
, re
->metric
);
1607 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1609 /* Currently selected re. */
1610 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1611 assert(old_selected
== NULL
);
1615 /* Skip deleted entries from selection */
1616 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1619 /* Skip unreachable nexthop. */
1620 /* This first call to nexthop_active_update is merely to
1622 * there's any change to nexthops associated with this RIB
1624 * rib_process() can be invoked due to an external event such as
1626 * down or due to next-hop-tracking evaluation. In the latter
1628 * a decision has already been made that the NHs have changed.
1630 * need to invoke a potentially expensive call again. Further,
1632 * the change might be in a recursive NH which is not caught in
1633 * the nexthop_active_update() code. Thus, we might miss changes
1637 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1638 && !nexthop_active_update(rn
, re
, 0)) {
1639 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1640 /* XXX: HERE BE DRAGONS!!!!!
1641 * In all honesty, I have not yet figured out
1643 * does or why the ROUTE_ENTRY_CHANGED test
1645 * or why we need to delete a route here, and
1647 * this concerns both selected and fib route, or
1650 /* This entry was denied by the 'ip protocol
1651 * table' route-map, we
1652 * need to delete it */
1653 if (re
!= old_selected
) {
1654 if (IS_ZEBRA_DEBUG_RIB
)
1656 "%s: %u:%s: imported via import-table but denied "
1657 "by the ip protocol table route-map",
1658 __func__
, vrf_id
, buf
);
1661 SET_FLAG(re
->status
,
1662 ROUTE_ENTRY_REMOVED
);
1668 /* Infinite distance. */
1669 if (re
->distance
== DISTANCE_INFINITY
) {
1670 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1674 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1675 best
= rib_choose_best(new_fib
, re
);
1676 if (new_fib
&& best
!= new_fib
)
1677 UNSET_FLAG(new_fib
->status
,
1678 ROUTE_ENTRY_CHANGED
);
1681 best
= rib_choose_best(new_selected
, re
);
1682 if (new_selected
&& best
!= new_selected
)
1683 UNSET_FLAG(new_selected
->status
,
1684 ROUTE_ENTRY_CHANGED
);
1685 new_selected
= best
;
1688 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1689 } /* RNODE_FOREACH_RE */
1691 /* If no FIB override route, use the selected route also for FIB */
1692 if (new_fib
== NULL
)
1693 new_fib
= new_selected
;
1695 /* After the cycle is finished, the following pointers will be set:
1696 * old_selected --- RE entry currently having SELECTED
1697 * new_selected --- RE entry that is newly SELECTED
1698 * old_fib --- RE entry currently in kernel FIB
1699 * new_fib --- RE entry that is newly to be in kernel FIB
1701 * new_selected will get SELECTED flag, and is going to be redistributed
1702 * the zclients. new_fib (which can be new_selected) will be installed
1706 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1708 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1709 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1710 (void *)old_fib
, (void *)new_fib
);
1713 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1714 * fib == selected */
1715 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1716 ROUTE_ENTRY_CHANGED
);
1718 /* Update fib according to selection results */
1719 if (new_fib
&& old_fib
)
1720 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1722 rib_process_add_fib(zvrf
, rn
, new_fib
);
1724 rib_process_del_fib(zvrf
, rn
, old_fib
);
1726 /* Redistribute SELECTED entry */
1727 if (old_selected
!= new_selected
|| selected_changed
) {
1728 struct nexthop
*nexthop
= NULL
;
1730 /* Check if we have a FIB route for the destination, otherwise,
1731 * don't redistribute it */
1733 for (ALL_NEXTHOPS(new_fib
->ng
, nexthop
)) {
1734 if (CHECK_FLAG(nexthop
->flags
,
1735 NEXTHOP_FLAG_FIB
)) {
1741 new_selected
= NULL
;
1743 if (new_selected
&& new_selected
!= new_fib
) {
1744 nexthop_active_update(rn
, new_selected
, 1);
1745 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1750 redistribute_delete(p
, src_p
, old_selected
);
1751 if (old_selected
!= new_selected
)
1752 UNSET_FLAG(old_selected
->flags
,
1753 ZEBRA_FLAG_SELECTED
);
1757 /* Install new or replace existing redistributed entry
1759 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1760 redistribute_update(p
, src_p
, new_selected
,
1765 /* Remove all RE entries queued for removal */
1766 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1767 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1768 if (IS_ZEBRA_DEBUG_RIB
) {
1769 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1770 (void *)rn
, (void *)re
);
1777 * Check if the dest can be deleted now.
1782 /* Take a list of route_node structs and return 1, if there was a record
1783 * picked from it and processed by rib_process(). Don't process more,
1784 * than one RN record; operate only in the specified sub-queue.
1786 static unsigned int process_subq(struct list
*subq
, uint8_t qindex
)
1788 struct listnode
*lnode
= listhead(subq
);
1789 struct route_node
*rnode
;
1791 struct zebra_vrf
*zvrf
= NULL
;
1796 rnode
= listgetdata(lnode
);
1797 dest
= rib_dest_from_rnode(rnode
);
1799 zvrf
= rib_dest_vrf(dest
);
1803 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1804 char buf
[SRCDEST2STR_BUFFER
];
1805 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1806 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1807 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1811 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1812 RIB_ROUTE_QUEUED(qindex
));
1817 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1818 __func__
, rnode
, rnode
->lock
);
1819 zlog_backtrace(LOG_DEBUG
);
1822 route_unlock_node(rnode
);
1823 list_delete_node(subq
, lnode
);
1828 * All meta queues have been processed. Trigger next-hop evaluation.
1830 static void meta_queue_process_complete(struct work_queue
*dummy
)
1833 struct zebra_vrf
*zvrf
;
1835 /* Evaluate nexthops for those VRFs which underwent route processing.
1837 * should limit the evaluation to the necessary VRFs in most common
1840 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1842 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1845 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1846 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
,
1848 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0,
1849 RNH_IMPORT_CHECK_TYPE
, NULL
);
1850 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
,
1852 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0,
1853 RNH_IMPORT_CHECK_TYPE
, NULL
);
1856 /* Schedule LSPs for processing, if needed. */
1857 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1858 if (mpls_should_lsps_be_processed(zvrf
)) {
1859 if (IS_ZEBRA_DEBUG_MPLS
)
1861 "%u: Scheduling all LSPs upon RIB completion",
1863 zebra_mpls_lsp_schedule(zvrf
);
1864 mpls_unmark_lsps_for_processing(zvrf
);
1868 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1869 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1871 * is pointed to the meta queue structure.
1873 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
1875 struct meta_queue
*mq
= data
;
1878 for (i
= 0; i
< MQ_SIZE
; i
++)
1879 if (process_subq(mq
->subq
[i
], i
)) {
1883 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1887 * Map from rib types to queue type (priority) in meta queue
1889 static const uint8_t meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1890 [ZEBRA_ROUTE_SYSTEM
] = 4,
1891 [ZEBRA_ROUTE_KERNEL
] = 0,
1892 [ZEBRA_ROUTE_CONNECT
] = 0,
1893 [ZEBRA_ROUTE_STATIC
] = 1,
1894 [ZEBRA_ROUTE_RIP
] = 2,
1895 [ZEBRA_ROUTE_RIPNG
] = 2,
1896 [ZEBRA_ROUTE_OSPF
] = 2,
1897 [ZEBRA_ROUTE_OSPF6
] = 2,
1898 [ZEBRA_ROUTE_ISIS
] = 2,
1899 [ZEBRA_ROUTE_BGP
] = 3,
1900 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1901 [ZEBRA_ROUTE_EIGRP
] = 2,
1902 [ZEBRA_ROUTE_NHRP
] = 2,
1903 [ZEBRA_ROUTE_HSLS
] = 4,
1904 [ZEBRA_ROUTE_OLSR
] = 4,
1905 [ZEBRA_ROUTE_TABLE
] = 1,
1906 [ZEBRA_ROUTE_LDP
] = 4,
1907 [ZEBRA_ROUTE_VNC
] = 3,
1908 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1909 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1910 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1911 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1912 [ZEBRA_ROUTE_BABEL
] = 2,
1913 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1916 /* Look into the RN and queue it into one or more priority queues,
1917 * increasing the size for each data push done.
1919 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
1921 struct route_entry
*re
;
1923 RNODE_FOREACH_RE (rn
, re
) {
1924 uint8_t qindex
= meta_queue_map
[re
->type
];
1925 struct zebra_vrf
*zvrf
;
1927 /* Invariant: at this point we always have rn->info set. */
1928 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
1929 RIB_ROUTE_QUEUED(qindex
))) {
1930 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1933 "rn %p is already queued in sub-queue %u",
1934 (void *)rn
, qindex
);
1938 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
1939 RIB_ROUTE_QUEUED(qindex
));
1940 listnode_add(mq
->subq
[qindex
], rn
);
1941 route_lock_node(rn
);
1944 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1945 rnode_debug(rn
, re
->vrf_id
,
1946 "queued rn %p into sub-queue %u",
1947 (void *)rn
, qindex
);
1949 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
1951 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1955 /* Add route_node to work queue and schedule processing */
1956 void rib_queue_add(struct route_node
*rn
)
1960 /* Pointless to queue a route_node with no RIB entries to add or remove
1962 if (!rnode_to_ribs(rn
)) {
1963 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
1964 __func__
, (void *)rn
, rn
->lock
);
1965 zlog_backtrace(LOG_DEBUG
);
1969 if (zebrad
.ribq
== NULL
) {
1970 flog_err(ZEBRA_ERR_WQ_NONEXISTENT
,
1971 "%s: work_queue does not exist!", __func__
);
1976 * The RIB queue should normally be either empty or holding the only
1977 * work_queue_item element. In the latter case this element would
1978 * hold a pointer to the meta queue structure, which must be used to
1979 * actually queue the route nodes to process. So create the MQ
1980 * holder, if necessary, then push the work into it in any case.
1981 * This semantics was introduced after 0.99.9 release.
1983 if (work_queue_empty(zebrad
.ribq
))
1984 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
1986 rib_meta_queue_add(zebrad
.mq
, rn
);
1991 /* Create new meta queue.
1992 A destructor function doesn't seem to be necessary here.
1994 static struct meta_queue
*meta_queue_new(void)
1996 struct meta_queue
*new;
1999 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
2001 for (i
= 0; i
< MQ_SIZE
; i
++) {
2002 new->subq
[i
] = list_new();
2003 assert(new->subq
[i
]);
2009 void meta_queue_free(struct meta_queue
*mq
)
2013 for (i
= 0; i
< MQ_SIZE
; i
++)
2014 list_delete_and_null(&mq
->subq
[i
]);
2016 XFREE(MTYPE_WORK_QUEUE
, mq
);
2019 /* initialise zebra rib work queue */
2020 static void rib_queue_init(struct zebra_t
*zebra
)
2025 work_queue_new(zebra
->master
, "route_node processing"))) {
2026 flog_err(ZEBRA_ERR_WQ_NONEXISTENT
,
2027 "%s: could not initialise work queue!", __func__
);
2031 /* fill in the work queue spec */
2032 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2033 zebra
->ribq
->spec
.errorfunc
= NULL
;
2034 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2035 /* XXX: TODO: These should be runtime configurable via vty */
2036 zebra
->ribq
->spec
.max_retries
= 3;
2037 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
2039 if (!(zebra
->mq
= meta_queue_new())) {
2040 flog_err(ZEBRA_ERR_WQ_NONEXISTENT
,
2041 "%s: could not initialise meta queue!", __func__
);
2047 /* RIB updates are processed via a queue of pointers to route_nodes.
2049 * The queue length is bounded by the maximal size of the routing table,
2050 * as a route_node will not be requeued, if already queued.
2052 * REs are submitted via rib_addnode or rib_delnode which set minimal
2053 * state, or static_install_route (when an existing RE is updated)
2054 * and then submit route_node to queue for best-path selection later.
2055 * Order of add/delete state changes are preserved for any given RE.
2057 * Deleted REs are reaped during best-path selection.
2060 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2061 * |-------->| | best RE, if required
2063 * static_install->|->rib_addqueue...... -> rib_process
2065 * |-------->| |-> rib_unlink
2066 * |-> set ROUTE_ENTRY_REMOVE |
2067 * rib_delnode (RE freed)
2069 * The 'info' pointer of a route_node points to a rib_dest_t
2070 * ('dest'). Queueing state for a route_node is kept on the dest. The
2071 * dest is created on-demand by rib_link() and is kept around at least
2072 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2074 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2076 * - route_nodes: refcounted by:
2077 * - dest attached to route_node:
2078 * - managed by: rib_link/rib_gc_dest
2079 * - route_node processing queue
2080 * - managed by: rib_addqueue, rib_process.
2084 /* Add RE to head of the route node. */
2085 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
2087 struct route_entry
*head
;
2090 const char *rmap_name
;
2094 dest
= rib_dest_from_rnode(rn
);
2096 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2097 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2099 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
2100 route_lock_node(rn
); /* rn route table reference */
2105 head
= dest
->routes
;
2112 afi
= (rn
->p
.family
== AF_INET
)
2114 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2115 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2116 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2117 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2122 static void rib_addnode(struct route_node
*rn
,
2123 struct route_entry
*re
, int process
)
2125 /* RE node has been un-removed before route-node is processed.
2126 * route_node must hence already be on the queue for processing..
2128 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2129 if (IS_ZEBRA_DEBUG_RIB
)
2130 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2131 (void *)rn
, (void *)re
);
2133 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2136 rib_link(rn
, re
, process
);
2142 * Detach a rib structure from a route_node.
2144 * Note that a call to rib_unlink() should be followed by a call to
2145 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2146 * longer required to be deleted.
2148 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2154 if (IS_ZEBRA_DEBUG_RIB
)
2155 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2158 dest
= rib_dest_from_rnode(rn
);
2161 re
->next
->prev
= re
->prev
;
2164 re
->prev
->next
= re
->next
;
2166 dest
->routes
= re
->next
;
2169 if (dest
->selected_fib
== re
)
2170 dest
->selected_fib
= NULL
;
2172 nexthops_free(re
->ng
.nexthop
);
2173 XFREE(MTYPE_RE
, re
);
2176 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2180 if (IS_ZEBRA_DEBUG_RIB
)
2181 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2182 (void *)rn
, (void *)re
);
2183 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2185 afi
= (rn
->p
.family
== AF_INET
)
2187 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2188 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2189 zebra_del_import_table_entry(rn
, re
);
2190 /* Just clean up if non main table */
2191 if (IS_ZEBRA_DEBUG_RIB
) {
2192 char buf
[SRCDEST2STR_BUFFER
];
2193 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2195 "%u:%s: Freeing route rn %p, re %p (type %d)",
2196 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2205 /* This function dumps the contents of a given RE entry into
2206 * standard debug log. Calling function name and IP prefix in
2207 * question are passed as 1st and 2nd arguments.
2210 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2211 union prefixconstptr src_pp
,
2212 const struct route_entry
*re
)
2214 const struct prefix
*src_p
= src_pp
.p
;
2215 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2216 char straddr
[PREFIX_STRLEN
];
2217 char srcaddr
[PREFIX_STRLEN
];
2218 struct nexthop
*nexthop
;
2220 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2221 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2222 is_srcdst
? " from " : "",
2223 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2226 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2227 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2230 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2231 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2232 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2233 re
->nexthop_num
, re
->nexthop_active_num
);
2235 for (ALL_NEXTHOPS(re
->ng
, nexthop
)) {
2236 struct interface
*ifp
;
2237 struct vrf
*vrf
= vrf_lookup_by_id(nexthop
->vrf_id
);
2239 switch (nexthop
->type
) {
2240 case NEXTHOP_TYPE_BLACKHOLE
:
2241 sprintf(straddr
, "Blackhole");
2243 case NEXTHOP_TYPE_IFINDEX
:
2244 ifp
= if_lookup_by_index(nexthop
->ifindex
,
2246 sprintf(straddr
, "%s", ifp
? ifp
->name
: "Unknown");
2248 case NEXTHOP_TYPE_IPV4
:
2250 case NEXTHOP_TYPE_IPV4_IFINDEX
:
2251 inet_ntop(AF_INET
, &nexthop
->gate
, straddr
,
2254 case NEXTHOP_TYPE_IPV6
:
2255 case NEXTHOP_TYPE_IPV6_IFINDEX
:
2256 inet_ntop(AF_INET6
, &nexthop
->gate
, straddr
,
2260 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func
,
2261 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2262 nexthop
->ifindex
, vrf
? vrf
->name
: "Unknown",
2264 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2267 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2270 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2274 zlog_debug("%s: dump complete", func
);
2277 /* This is an exported helper to rtm_read() to dump the strange
2278 * RE entry found by rib_lookup_ipv4_route()
2281 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2283 struct route_table
*table
;
2284 struct route_node
*rn
;
2285 struct route_entry
*re
;
2286 char prefix_buf
[INET_ADDRSTRLEN
];
2289 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2291 flog_err(ZEBRA_ERR_TABLE_LOOKUP_FAILED
,
2292 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2297 /* Scan the RIB table for exactly matching RE entry. */
2298 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2300 /* No route for this prefix. */
2302 zlog_debug("%s:%u lookup failed for %s", __func__
, vrf_id
,
2303 prefix2str((struct prefix
*)p
, prefix_buf
,
2304 sizeof(prefix_buf
)));
2309 route_unlock_node(rn
);
2312 RNODE_FOREACH_RE (rn
, re
) {
2313 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2315 (void *)rn
, (void *)re
,
2316 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2319 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2322 route_entry_dump(p
, NULL
, re
);
2326 /* Check if requested address assignment will fail due to another
2327 * route being installed by zebra in FIB already. Take necessary
2328 * actions, if needed: remove such a route from FIB and deSELECT
2329 * corresponding RE entry. Then put affected RN into RIBQ head.
2331 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2333 struct route_table
*table
;
2334 struct route_node
*rn
;
2335 unsigned changed
= 0;
2338 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2339 flog_err(ZEBRA_ERR_TABLE_LOOKUP_FAILED
,
2340 "%s:%u zebra_vrf_table() returned NULL", __func__
,
2345 /* No matches would be the simplest case. */
2346 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2350 route_unlock_node(rn
);
2352 dest
= rib_dest_from_rnode(rn
);
2353 /* Check all RE entries. In case any changes have to be done, requeue
2354 * the RN into RIBQ head. If the routing message about the new connected
2355 * route (generated by the IP address we are going to assign very soon)
2356 * comes before the RIBQ is processed, the new RE entry will join
2357 * RIBQ record already on head. This is necessary for proper
2359 * of the rest of the RE.
2361 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2363 if (IS_ZEBRA_DEBUG_RIB
) {
2364 char buf
[PREFIX_STRLEN
];
2366 zlog_debug("%u:%s: freeing way for connected prefix",
2367 dest
->selected_fib
->vrf_id
,
2368 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2369 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2371 rib_uninstall(rn
, dest
->selected_fib
);
2377 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2378 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2380 struct route_table
*table
;
2381 struct route_node
*rn
;
2382 struct route_entry
*same
= NULL
;
2383 struct nexthop
*nexthop
;
2389 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2392 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2394 XFREE(MTYPE_RE
, re
);
2398 /* Make it sure prefixlen is applied to the prefix. */
2401 apply_mask_ipv6(src_p
);
2403 /* Set default distance by route type. */
2404 if (re
->distance
== 0) {
2405 re
->distance
= route_distance(re
->type
);
2407 /* iBGP distance is 200. */
2408 if (re
->type
== ZEBRA_ROUTE_BGP
2409 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2413 /* Lookup route node.*/
2414 rn
= srcdest_rnode_get(table
, p
, src_p
);
2417 * If same type of route are installed, treat it as a implicit
2419 * If the user has specified the No route replace semantics
2420 * for the install don't do a route replace.
2422 RNODE_FOREACH_RE (rn
, same
) {
2423 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2426 if (same
->type
!= re
->type
)
2428 if (same
->instance
!= re
->instance
)
2430 if (same
->type
== ZEBRA_ROUTE_KERNEL
2431 && same
->metric
!= re
->metric
)
2434 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2435 same
->distance
!= re
->distance
)
2439 * We should allow duplicate connected routes
2440 * because of IPv6 link-local routes and unnumbered
2441 * interfaces on Linux.
2443 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2447 /* If this route is kernel route, set FIB flag to the route. */
2448 if (RIB_SYSTEM_ROUTE(re
))
2449 for (nexthop
= re
->ng
.nexthop
; nexthop
; nexthop
= nexthop
->next
)
2450 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2452 /* Link new re to node.*/
2453 if (IS_ZEBRA_DEBUG_RIB
) {
2456 "Inserting route rn %p, re %p (type %d) existing %p",
2457 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2459 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2460 route_entry_dump(p
, src_p
, re
);
2462 rib_addnode(rn
, re
, 1);
2465 /* Free implicit route.*/
2467 rib_delnode(rn
, same
);
2471 route_unlock_node(rn
);
2475 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2476 unsigned short instance
, int flags
, struct prefix
*p
,
2477 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2478 uint32_t table_id
, uint32_t metric
, uint8_t distance
,
2481 struct route_table
*table
;
2482 struct route_node
*rn
;
2483 struct route_entry
*re
;
2484 struct route_entry
*fib
= NULL
;
2485 struct route_entry
*same
= NULL
;
2486 struct nexthop
*rtnh
;
2487 char buf2
[INET6_ADDRSTRLEN
];
2490 assert(!src_p
|| !src_p
->prefixlen
|| afi
== AFI_IP6
);
2493 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2500 apply_mask_ipv6(src_p
);
2502 /* Lookup route node. */
2503 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2505 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2507 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2508 if (src_p
&& src_p
->prefixlen
)
2509 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2513 if (IS_ZEBRA_DEBUG_RIB
)
2514 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2516 (src_buf
[0] != '\0') ? " from " : "",
2521 dest
= rib_dest_from_rnode(rn
);
2522 fib
= dest
->selected_fib
;
2524 /* Lookup same type route. */
2525 RNODE_FOREACH_RE (rn
, re
) {
2526 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2529 if (re
->type
!= type
)
2531 if (re
->instance
!= instance
)
2533 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_RR_USE_DISTANCE
) &&
2534 distance
!= re
->distance
)
2537 if (re
->type
== ZEBRA_ROUTE_KERNEL
&& re
->metric
!= metric
)
2539 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->ng
.nexthop
)
2540 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2541 if (rtnh
->ifindex
!= nh
->ifindex
)
2546 /* Make sure that the route found has the same gateway. */
2552 for (ALL_NEXTHOPS(re
->ng
, rtnh
))
2553 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2561 /* If same type of route can't be found and this message is from
2565 * In the past(HA!) we could get here because
2566 * we were receiving a route delete from the
2567 * kernel and we're not marking the proto
2568 * as coming from it's appropriate originator.
2569 * Now that we are properly noticing the fact
2570 * that the kernel has deleted our route we
2571 * are not going to get called in this path
2572 * I am going to leave this here because
2573 * this might still work this way on non-linux
2574 * platforms as well as some weird state I have
2575 * not properly thought of yet.
2576 * If we can show that this code path is
2577 * dead then we can remove it.
2579 if (fib
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2580 if (IS_ZEBRA_DEBUG_RIB
) {
2583 "rn %p, re %p (type %d) was deleted from kernel, adding",
2584 rn
, fib
, fib
->type
);
2588 for (rtnh
= fib
->ng
.nexthop
; rtnh
;
2590 UNSET_FLAG(rtnh
->flags
,
2594 * This is a non FRR route
2595 * as such we should mark
2598 dest
->selected_fib
= NULL
;
2600 /* This means someone else, other than Zebra,
2602 * a Zebra router from the kernel. We will add
2604 rib_install_kernel(rn
, fib
, NULL
);
2607 if (IS_ZEBRA_DEBUG_RIB
) {
2611 "via %s ifindex %d type %d "
2612 "doesn't exist in rib",
2613 inet_ntop(afi2family(afi
),
2620 "type %d doesn't exist in rib",
2623 route_unlock_node(rn
);
2629 if (fromkernel
&& CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)
2631 rib_install_kernel(rn
, same
, NULL
);
2632 route_unlock_node(rn
);
2637 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2638 struct nexthop
*tmp_nh
;
2640 for (ALL_NEXTHOPS(re
->ng
, tmp_nh
)) {
2641 struct ipaddr vtep_ip
;
2643 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2644 if (afi
== AFI_IP
) {
2645 vtep_ip
.ipa_type
= IPADDR_V4
;
2646 memcpy(&(vtep_ip
.ipaddr_v4
),
2647 &(tmp_nh
->gate
.ipv4
),
2648 sizeof(struct in_addr
));
2650 vtep_ip
.ipa_type
= IPADDR_V6
;
2651 memcpy(&(vtep_ip
.ipaddr_v6
),
2652 &(tmp_nh
->gate
.ipv6
),
2653 sizeof(struct in6_addr
));
2655 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
,
2659 rib_delnode(rn
, same
);
2662 route_unlock_node(rn
);
2667 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2668 unsigned short instance
, int flags
, struct prefix
*p
,
2669 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2670 uint32_t table_id
, uint32_t metric
, uint32_t mtu
, uint8_t distance
,
2673 struct route_entry
*re
;
2674 struct nexthop
*nexthop
;
2676 /* Allocate new route_entry structure. */
2677 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2679 re
->instance
= instance
;
2680 re
->distance
= distance
;
2682 re
->metric
= metric
;
2684 re
->table
= table_id
;
2685 re
->vrf_id
= vrf_id
;
2686 re
->nexthop_num
= 0;
2687 re
->uptime
= time(NULL
);
2691 nexthop
= nexthop_new();
2693 route_entry_nexthop_add(re
, nexthop
);
2695 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2698 /* Schedule routes of a particular table (address-family) based on event. */
2699 void rib_update_table(struct route_table
*table
, rib_update_event_t event
)
2701 struct route_node
*rn
;
2702 struct route_entry
*re
, *next
;
2704 /* Walk all routes and queue for processing, if appropriate for
2705 * the trigger event.
2707 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2709 * If we are looking at a route node and the node
2710 * has already been queued we don't
2711 * need to queue it up again
2713 if (rn
->info
&& CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2714 RIB_ROUTE_ANY_QUEUED
))
2717 case RIB_UPDATE_IF_CHANGE
:
2718 /* Examine all routes that won't get processed by the
2720 * triggered by nexthop evaluation (NHT). This would be
2722 * kernel and certain static routes. Note that NHT will
2724 * triggered upon an interface event as connected routes
2726 * get queued for processing.
2728 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2731 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
2732 && re
->type
!= ZEBRA_ROUTE_KERNEL
2733 && re
->type
!= ZEBRA_ROUTE_CONNECT
2734 && re
->type
!= ZEBRA_ROUTE_STATIC
)
2737 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2742 for (nh
= re
->ng
.nexthop
; nh
; nh
= nh
->next
)
2743 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2744 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2747 /* If we only have nexthops to a
2756 case RIB_UPDATE_RMAP_CHANGE
:
2757 case RIB_UPDATE_OTHER
:
2758 /* Right now, examine all routes. Can restrict to a
2760 * some cases (TODO).
2762 if (rnode_to_ribs(rn
))
2772 /* RIB update function. */
2773 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2775 struct route_table
*table
;
2777 /* Process routes of interested address-families. */
2778 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2780 if (IS_ZEBRA_DEBUG_EVENT
)
2781 zlog_debug("%s : AFI_IP event %d", __func__
, event
);
2782 rib_update_table(table
, event
);
2785 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2787 if (IS_ZEBRA_DEBUG_EVENT
)
2788 zlog_debug("%s : AFI_IP6 event %d", __func__
, event
);
2789 rib_update_table(table
, event
);
2793 /* Delete self installed routes after zebra is relaunched. */
2794 void rib_sweep_table(struct route_table
*table
)
2796 struct route_node
*rn
;
2797 struct route_entry
*re
;
2798 struct route_entry
*next
;
2799 struct nexthop
*nexthop
;
2804 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2805 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2806 if (IS_ZEBRA_DEBUG_RIB
)
2807 route_entry_dump(&rn
->p
, NULL
, re
);
2809 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2812 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2816 * So we are starting up and have received
2817 * routes from the kernel that we have installed
2818 * from a previous run of zebra but not cleaned
2819 * up ( say a kill -9 )
2820 * But since we haven't actually installed
2821 * them yet( we received them from the kernel )
2822 * we don't think they are active.
2823 * So let's pretend they are active to actually
2825 * In all honesty I'm not sure if we should
2826 * mark them as active when we receive them
2827 * This is startup only so probably ok.
2829 * If we ever decide to move rib_sweep_table
2830 * to a different spot (ie startup )
2831 * this decision needs to be revisited
2833 for (ALL_NEXTHOPS(re
->ng
, nexthop
))
2834 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2836 rib_uninstall_kernel(rn
, re
);
2837 rib_delnode(rn
, re
);
2842 /* Sweep all RIB tables. */
2843 void rib_sweep_route(void)
2846 struct zebra_vrf
*zvrf
;
2848 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2849 if ((zvrf
= vrf
->info
) == NULL
)
2852 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2853 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2856 zebra_ns_sweep_route();
2859 /* Remove specific by protocol routes from 'table'. */
2860 unsigned long rib_score_proto_table(uint8_t proto
, unsigned short instance
,
2861 struct route_table
*table
)
2863 struct route_node
*rn
;
2864 struct route_entry
*re
;
2865 struct route_entry
*next
;
2866 unsigned long n
= 0;
2869 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2870 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2871 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2873 if (re
->type
== proto
2874 && re
->instance
== instance
) {
2875 rib_delnode(rn
, re
);
2882 /* Remove specific by protocol routes. */
2883 unsigned long rib_score_proto(uint8_t proto
, unsigned short instance
)
2886 struct zebra_vrf
*zvrf
;
2887 unsigned long cnt
= 0;
2889 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2890 if ((zvrf
= vrf
->info
) != NULL
)
2891 cnt
+= rib_score_proto_table(
2893 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2894 + rib_score_proto_table(
2896 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2898 cnt
+= zebra_ns_score_proto(proto
, instance
);
2903 /* Close RIB and clean up kernel routes. */
2904 void rib_close_table(struct route_table
*table
)
2906 struct route_node
*rn
;
2907 rib_table_info_t
*info
;
2915 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2916 dest
= rib_dest_from_rnode(rn
);
2918 if (dest
&& dest
->selected_fib
) {
2919 if (info
->safi
== SAFI_UNICAST
)
2920 hook_call(rib_update
, rn
, NULL
);
2922 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
))
2923 rib_uninstall_kernel(rn
, dest
->selected_fib
);
2928 /* Routing information base initialize. */
2931 rib_queue_init(&zebrad
);
2937 * Get the first vrf id that is greater than the given vrf id if any.
2939 * Returns TRUE if a vrf id was found, FALSE otherwise.
2941 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2945 vrf
= vrf_lookup_by_id(vrf_id
);
2947 vrf
= RB_NEXT(vrf_id_head
, vrf
);
2949 *next_id_p
= vrf
->vrf_id
;
2958 * rib_tables_iter_next
2960 * Returns the next table in the iteration.
2962 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
2964 struct route_table
*table
;
2967 * Array that helps us go over all AFI/SAFI combinations via one
2974 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
2975 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
2976 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
2981 switch (iter
->state
) {
2983 case RIB_TABLES_ITER_S_INIT
:
2984 iter
->vrf_id
= VRF_DEFAULT
;
2985 iter
->afi_safi_ix
= -1;
2989 case RIB_TABLES_ITER_S_ITERATING
:
2990 iter
->afi_safi_ix
++;
2993 while (iter
->afi_safi_ix
2994 < (int)ZEBRA_NUM_OF(afi_safis
)) {
2995 table
= zebra_vrf_table(
2996 afi_safis
[iter
->afi_safi_ix
].afi
,
2997 afi_safis
[iter
->afi_safi_ix
].safi
,
3002 iter
->afi_safi_ix
++;
3006 * Found another table in this vrf.
3012 * Done with all tables in the current vrf, go to the
3016 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
3019 iter
->afi_safi_ix
= 0;
3024 case RIB_TABLES_ITER_S_DONE
:
3029 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3031 iter
->state
= RIB_TABLES_ITER_S_DONE
;