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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "zebra_memory.h"
32 #include "sockunion.h"
35 #include "workqueue.h"
42 #include "zebra/rib.h"
44 #include "zebra/zebra_ns.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/debug.h"
50 #include "zebra/zebra_fpm.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/interface.h"
53 #include "zebra/connected.h"
55 /* Should we allow non Quagga processes to delete our routes */
56 extern int allow_delete
;
58 /* Hold time for RIB process, should be very minimal.
59 * it is useful to able to set it otherwise for testing, hence exported
60 * as global here for test-rig code.
62 int rib_process_hold_time
= 10;
64 /* Each route type's string and default distance value. */
69 } route_info
[ZEBRA_ROUTE_MAX
] =
71 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
72 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
73 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
74 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
75 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
76 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
77 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
78 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
79 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
80 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
81 /* no entry/default: 150 */
84 /* RPF lookup behaviour */
85 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
88 static void __attribute__((format (printf
, 5, 6)))
89 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
90 const char *msgfmt
, ...)
92 char buf
[PREFIX_STRLEN
+ 8];
97 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
102 rib_table_info_t
*info
= rn
->table
->info
;
104 prefix2str(&rn
->p
, buf
, sizeof(buf
));
105 if (info
->safi
== SAFI_MULTICAST
)
106 strcat(buf
, " (MRIB)");
110 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
113 zlog (NULL
, priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
116 #define rnode_debug(node, vrf_id, ...) \
117 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
118 #define rnode_info(node, ...) \
119 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
122 route_distance (int type
)
126 if ((unsigned)type
>= array_size(route_info
))
129 distance
= route_info
[type
].distance
;
135 is_zebra_valid_kernel_table(u_int32_t table_id
)
137 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
141 if ((table_id
== RT_TABLE_UNSPEC
) ||
142 (table_id
== RT_TABLE_LOCAL
) ||
143 (table_id
== RT_TABLE_COMPAT
))
151 is_zebra_main_routing_table(u_int32_t table_id
)
153 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
159 zebra_check_addr (struct prefix
*p
)
161 if (p
->family
== AF_INET
)
165 addr
= p
->u
.prefix4
.s_addr
;
168 if (IPV4_NET127 (addr
)
170 || IPV4_LINKLOCAL(addr
))
173 if (p
->family
== AF_INET6
)
175 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
177 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
183 /* Add nexthop to the end of a rib node's nexthop list */
185 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
187 nexthop_add(&rib
->nexthop
, nexthop
);
194 * copy_nexthop - copy a nexthop to the rib structure.
197 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
199 struct nexthop
*nexthop
;
201 nexthop
= nexthop_new();
202 nexthop
->flags
= nh
->flags
;
203 nexthop
->type
= nh
->type
;
204 nexthop
->ifindex
= nh
->ifindex
;
205 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
206 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
207 rib_nexthop_add(rib
, nexthop
);
208 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
209 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
212 /* Delete specified nexthop from the list. */
214 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
217 nexthop
->next
->prev
= nexthop
->prev
;
219 nexthop
->prev
->next
= nexthop
->next
;
221 rib
->nexthop
= nexthop
->next
;
228 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
230 struct nexthop
*nexthop
;
232 nexthop
= nexthop_new();
233 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
234 nexthop
->ifindex
= ifindex
;
236 rib_nexthop_add (rib
, nexthop
);
242 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
244 struct nexthop
*nexthop
;
246 nexthop
= nexthop_new();
247 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
248 nexthop
->gate
.ipv4
= *ipv4
;
250 nexthop
->src
.ipv4
= *src
;
252 rib_nexthop_add (rib
, nexthop
);
258 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
259 struct in_addr
*src
, ifindex_t ifindex
)
261 struct nexthop
*nexthop
;
262 struct interface
*ifp
;
264 nexthop
= nexthop_new();
265 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
266 nexthop
->gate
.ipv4
= *ipv4
;
268 nexthop
->src
.ipv4
= *src
;
269 nexthop
->ifindex
= ifindex
;
270 ifp
= if_lookup_by_index (nexthop
->ifindex
);
271 /*Pending: need to think if null ifp here is ok during bootup?
272 There was a crash because ifp here was coming to be NULL */
274 if (connected_is_unnumbered(ifp
)) {
275 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
278 rib_nexthop_add (rib
, nexthop
);
284 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
286 struct nexthop
*nexthop
;
288 nexthop
= nexthop_new();
289 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
290 nexthop
->gate
.ipv6
= *ipv6
;
292 rib_nexthop_add (rib
, nexthop
);
298 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
301 struct nexthop
*nexthop
;
303 nexthop
= nexthop_new();
304 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
305 nexthop
->gate
.ipv6
= *ipv6
;
306 nexthop
->ifindex
= ifindex
;
308 rib_nexthop_add (rib
, nexthop
);
314 rib_nexthop_blackhole_add (struct rib
*rib
)
316 struct nexthop
*nexthop
;
318 nexthop
= nexthop_new();
319 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
320 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
322 rib_nexthop_add (rib
, nexthop
);
327 /* This method checks whether a recursive nexthop has at
328 * least one resolved nexthop in the fib.
331 nexthop_has_fib_child(struct nexthop
*nexthop
)
335 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
338 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
339 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
345 /* If force flag is not set, do not modify falgs at all for uninstall
346 the route from FIB. */
348 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
349 struct route_node
*top
)
351 struct prefix_ipv4 p
;
352 struct route_table
*table
;
353 struct route_node
*rn
;
356 struct nexthop
*newhop
, *tnewhop
;
357 struct nexthop
*resolved_hop
;
359 struct interface
*ifp
;
361 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
362 nexthop
->ifindex
= 0;
366 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
367 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
368 nexthops_free(nexthop
->resolved
);
369 nexthop
->resolved
= NULL
;
370 rib
->nexthop_mtu
= 0;
373 /* Skip nexthops that have been filtered out due to route-map */
374 /* The nexthops are specific to this route and so the same */
375 /* nexthop for a different route may not have this flag set */
376 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
380 * Check to see if we should trust the passed in information
381 * for UNNUMBERED interfaces as that we won't find the GW
382 * address in the routing table.
384 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
386 ifp
= if_lookup_by_index (nexthop
->ifindex
);
387 if (ifp
&& connected_is_unnumbered(ifp
))
389 if (if_is_operative(ifp
))
398 /* Make lookup prefix. */
399 memset (&p
, 0, sizeof (struct prefix_ipv4
));
401 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
402 p
.prefix
= nexthop
->gate
.ipv4
;
405 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, rib
->vrf_id
);
409 rn
= route_node_match (table
, (struct prefix
*) &p
);
412 route_unlock_node (rn
);
414 /* If lookup self prefix return immediately. */
418 /* Pick up selected route. */
419 /* However, do not resolve over default route unless explicitly allowed. */
420 if (is_default_prefix (&rn
->p
) &&
421 !nh_resolve_via_default (p
.family
))
424 RNODE_FOREACH_RIB (rn
, match
)
426 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
429 /* if the next hop is imported from another table, skip it */
430 if (match
->type
== ZEBRA_ROUTE_TABLE
)
432 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
436 /* If there is no selected route or matched route is EGP, go up
442 } while (rn
&& rn
->info
== NULL
);
444 route_lock_node (rn
);
448 /* If the longest prefix match for the nexthop yields
449 * a blackhole, mark it as inactive. */
450 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
451 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
454 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
456 /* Directly point connected route. */
457 newhop
= match
->nexthop
;
458 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
459 nexthop
->ifindex
= newhop
->ifindex
;
463 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
466 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
467 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
468 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
472 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
473 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
475 resolved_hop
= nexthop_new();
476 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
477 /* If the resolving route specifies a gateway, use it */
478 if (newhop
->type
== NEXTHOP_TYPE_IPV4
479 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
481 resolved_hop
->type
= newhop
->type
;
482 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
486 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
487 resolved_hop
->ifindex
= newhop
->ifindex
;
488 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
489 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
493 /* If the resolving route is an interface route,
494 * it means the gateway we are looking up is connected
495 * to that interface. (The actual network is _not_ onlink).
496 * Therefore, the resolved route should have the original
497 * gateway as nexthop as it is directly connected.
499 * On Linux, we have to set the onlink netlink flag because
500 * otherwise, the kernel won't accept the route. */
501 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
503 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
504 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
505 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
506 resolved_hop
->ifindex
= newhop
->ifindex
;
509 nexthop_add(&nexthop
->resolved
, resolved_hop
);
515 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
518 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
519 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
523 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
525 resolved_hop
= nexthop_new();
526 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
527 /* If the resolving route specifies a gateway, use it */
528 if (newhop
->type
== NEXTHOP_TYPE_IPV4
529 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
531 resolved_hop
->type
= newhop
->type
;
532 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
536 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
537 resolved_hop
->ifindex
= newhop
->ifindex
;
538 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
539 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
543 /* If the resolving route is an interface route,
544 * it means the gateway we are looking up is connected
545 * to that interface. (The actual network is _not_ onlink).
546 * Therefore, the resolved route should have the original
547 * gateway as nexthop as it is directly connected.
549 * On Linux, we have to set the onlink netlink flag because
550 * otherwise, the kernel won't accept the route.
552 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
554 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
555 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
556 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
557 resolved_hop
->ifindex
= newhop
->ifindex
;
560 nexthop_add(&nexthop
->resolved
, resolved_hop
);
565 rib
->nexthop_mtu
= match
->mtu
;
577 /* If force flag is not set, do not modify falgs at all for uninstall
578 the route from FIB. */
580 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
581 struct route_node
*top
)
583 struct prefix_ipv6 p
;
584 struct route_table
*table
;
585 struct route_node
*rn
;
588 struct nexthop
*newhop
, *tnewhop
;
590 struct nexthop
*resolved_hop
;
592 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
593 nexthop
->ifindex
= 0;
597 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
598 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, nexthop
->resolved
, top
);
599 nexthops_free(nexthop
->resolved
);
600 nexthop
->resolved
= NULL
;
603 /* Skip nexthops that have been filtered out due to route-map */
604 /* The nexthops are specific to this route and so the same */
605 /* nexthop for a different route may not have this flag set */
606 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
609 /* Make lookup prefix. */
610 memset (&p
, 0, sizeof (struct prefix_ipv6
));
612 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
613 p
.prefix
= nexthop
->gate
.ipv6
;
616 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, rib
->vrf_id
);
620 rn
= route_node_match (table
, (struct prefix
*) &p
);
623 route_unlock_node (rn
);
625 /* If lookup self prefix return immediately. */
629 /* Pick up selected route. */
630 /* However, do not resolve over default route unless explicitly allowed. */
631 if (is_default_prefix (&rn
->p
) &&
632 !nh_resolve_via_default (p
.family
))
635 RNODE_FOREACH_RIB (rn
, match
)
637 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
639 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
643 /* If there is no selected route or matched route is EGP, go up
649 } while (rn
&& rn
->info
== NULL
);
651 route_lock_node (rn
);
655 /* If the longest prefix match for the nexthop yields
656 * a blackhole, mark it as inactive. */
657 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
658 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
661 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
663 /* Directly point connected route. */
664 newhop
= match
->nexthop
;
666 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
667 nexthop
->ifindex
= newhop
->ifindex
;
671 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
674 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
675 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
676 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
680 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
681 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
683 resolved_hop
= nexthop_new();
684 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
685 /* See nexthop_active_ipv4 for a description how the
686 * resolved nexthop is constructed. */
687 if (newhop
->type
== NEXTHOP_TYPE_IPV6
688 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
690 resolved_hop
->type
= newhop
->type
;
691 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
695 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
696 resolved_hop
->ifindex
= newhop
->ifindex
;
700 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
702 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
703 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
704 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
705 resolved_hop
->ifindex
= newhop
->ifindex
;
708 nexthop_add(&nexthop
->resolved
, resolved_hop
);
714 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
717 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
718 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
722 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
724 resolved_hop
= nexthop_new();
725 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
726 /* See nexthop_active_ipv4 for a description how the
727 * resolved nexthop is constructed. */
728 if (newhop
->type
== NEXTHOP_TYPE_IPV6
729 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
731 resolved_hop
->type
= newhop
->type
;
732 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
736 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
737 resolved_hop
->ifindex
= newhop
->ifindex
;
741 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
743 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
744 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
745 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
746 resolved_hop
->ifindex
= newhop
->ifindex
;
749 nexthop_add(&nexthop
->resolved
, resolved_hop
);
765 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
766 union g_addr
*addr
, struct route_node
**rn_out
)
769 struct route_table
*table
;
770 struct route_node
*rn
;
772 struct nexthop
*newhop
, *tnewhop
;
776 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
780 memset (&p
, 0, sizeof (struct prefix
));
784 p
.u
.prefix4
= addr
->ipv4
;
785 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
789 p
.u
.prefix6
= addr
->ipv6
;
790 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
793 rn
= route_node_match (table
, (struct prefix
*) &p
);
797 route_unlock_node (rn
);
799 /* Pick up selected route. */
800 RNODE_FOREACH_RIB (rn
, match
)
802 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
804 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
808 /* If there is no selected route or matched route is EGP, go up
814 } while (rn
&& rn
->info
== NULL
);
816 route_lock_node (rn
);
820 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
823 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
824 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
842 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
844 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
845 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
846 union g_addr gaddr
= { .ipv4
= addr
};
848 switch (ipv4_multicast_mode
)
850 case MCAST_MRIB_ONLY
:
851 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
852 case MCAST_URIB_ONLY
:
853 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
854 case MCAST_NO_CONFIG
:
855 case MCAST_MIX_MRIB_FIRST
:
856 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
858 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
860 case MCAST_MIX_DISTANCE
:
861 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
862 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
864 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
870 case MCAST_MIX_PFXLEN
:
871 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
872 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
874 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
883 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
885 if (IS_ZEBRA_DEBUG_RIB
)
888 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
890 zlog_debug("%s: %s: found %s, using %s",
892 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
893 urib
? "URIB" : "nothing",
894 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
900 multicast_mode_ipv4_set (enum multicast_mode mode
)
902 if (IS_ZEBRA_DEBUG_RIB
)
903 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
904 ipv4_multicast_mode
= mode
;
908 multicast_mode_ipv4_get (void)
910 return ipv4_multicast_mode
;
914 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
916 struct route_table
*table
;
917 struct route_node
*rn
;
919 struct nexthop
*nexthop
, *tnexthop
;
923 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
927 rn
= route_node_lookup (table
, (struct prefix
*) p
);
929 /* No route for this prefix. */
934 route_unlock_node (rn
);
936 RNODE_FOREACH_RIB (rn
, match
)
938 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
940 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
947 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
950 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
951 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
958 * This clone function, unlike its original rib_lookup_ipv4(), checks
959 * if specified IPv4 route record (prefix/mask -> gate) exists in
960 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
964 * 0: exact match found
965 * 1: a match was found with a different gate
966 * 2: connected route found
967 * 3: no matches found
970 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
973 struct route_table
*table
;
974 struct route_node
*rn
;
976 struct nexthop
*nexthop
, *tnexthop
;
981 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
983 return ZEBRA_RIB_LOOKUP_ERROR
;
985 /* Scan the RIB table for exactly matching RIB entry. */
986 rn
= route_node_lookup (table
, (struct prefix
*) p
);
988 /* No route for this prefix. */
990 return ZEBRA_RIB_NOTFOUND
;
993 route_unlock_node (rn
);
995 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
996 RNODE_FOREACH_RIB (rn
, match
)
998 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1000 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
1004 /* None such found :( */
1006 return ZEBRA_RIB_NOTFOUND
;
1008 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1009 return ZEBRA_RIB_FOUND_CONNECTED
;
1011 /* Ok, we have a cood candidate, let's check it's nexthop list... */
1012 nexthops_active
= 0;
1013 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
1014 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1016 nexthops_active
= 1;
1017 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
1018 return ZEBRA_RIB_FOUND_EXACT
;
1019 if (IS_ZEBRA_DEBUG_RIB
)
1021 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
1022 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
1023 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
1024 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
1025 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
1029 if (nexthops_active
)
1030 return ZEBRA_RIB_FOUND_NOGATE
;
1032 return ZEBRA_RIB_NOTFOUND
;
1035 #define RIB_SYSTEM_ROUTE(R) \
1036 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1038 /* This function verifies reachability of one given nexthop, which can be
1039 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1040 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1041 * nexthop->ifindex will be updated appropriately as well.
1042 * An existing route map can turn (otherwise active) nexthop into inactive, but
1045 * The return value is the final value of 'ACTIVE' flag.
1049 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
1050 struct nexthop
*nexthop
, int set
)
1052 rib_table_info_t
*info
= rn
->table
->info
;
1053 struct interface
*ifp
;
1054 route_map_result_t ret
= RMAP_MATCH
;
1056 char buf
[INET6_ADDRSTRLEN
+1];
1058 if (rn
->p
.family
== AF_INET
)
1060 else if (rn
->p
.family
== AF_INET6
)
1064 switch (nexthop
->type
)
1066 case NEXTHOP_TYPE_IFINDEX
:
1067 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1068 if (ifp
&& if_is_operative(ifp
))
1069 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1071 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1073 case NEXTHOP_TYPE_IPV4
:
1074 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1076 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
1077 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1079 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1081 case NEXTHOP_TYPE_IPV6
:
1083 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1084 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1086 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1088 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1089 /* RFC 5549, v4 prefix with v6 NH */
1090 if (rn
->p
.family
!= AF_INET
)
1092 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1094 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1095 if (ifp
&& if_is_operative(ifp
))
1096 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1098 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1102 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1103 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1105 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1108 case NEXTHOP_TYPE_BLACKHOLE
:
1109 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1114 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1117 /* XXX: What exactly do those checks do? Do we support
1118 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1119 if (RIB_SYSTEM_ROUTE(rib
) ||
1120 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1121 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1122 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1124 /* The original code didn't determine the family correctly
1125 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1126 * from the rib_table_info in those cases.
1127 * Possibly it may be better to use only the rib_table_info
1133 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1135 /* It'll get set if required inside */
1136 ret
= zebra_route_map_check(family
, rib
->type
, &rn
->p
, nexthop
, rib
->vrf_id
,
1138 if (ret
== RMAP_DENYMATCH
)
1140 if (IS_ZEBRA_DEBUG_RIB
)
1142 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1143 zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map",
1144 rib
->vrf_id
, buf
, rn
->p
.prefixlen
,
1145 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1147 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1149 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1152 /* Iterate over all nexthops of the given RIB entry and refresh their
1153 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1154 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1155 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1156 * transparently passed to nexthop_active_check().
1158 * Return value is the new number of active nexthops.
1162 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1164 struct nexthop
*nexthop
;
1165 union g_addr prev_src
;
1166 unsigned int prev_active
, new_active
, old_num_nh
;
1167 ifindex_t prev_index
;
1168 old_num_nh
= rib
->nexthop_active_num
;
1170 rib
->nexthop_active_num
= 0;
1171 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1173 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1175 /* No protocol daemon provides src and so we're skipping tracking it */
1176 prev_src
= nexthop
->rmap_src
;
1177 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1178 prev_index
= nexthop
->ifindex
;
1179 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1180 rib
->nexthop_active_num
++;
1181 /* Don't allow src setting on IPv6 addr for now */
1182 if (prev_active
!= new_active
||
1183 prev_index
!= nexthop
->ifindex
||
1184 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1185 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1186 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1187 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1188 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1189 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1191 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1192 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1196 if (old_num_nh
!= rib
->nexthop_active_num
)
1197 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1199 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1201 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1204 return rib
->nexthop_active_num
;
1209 /* Update flag indicates whether this is a "replace" or not. Currently, this
1210 * is only used for IPv4.
1213 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, int update
)
1216 struct nexthop
*nexthop
, *tnexthop
;
1217 rib_table_info_t
*info
= rn
->table
->info
;
1220 if (info
->safi
!= SAFI_UNICAST
)
1222 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1223 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1228 * Make sure we update the FPM any time we send new information to
1231 zfpm_trigger_update (rn
, "installing in kernel");
1232 switch (PREFIX_FAMILY (&rn
->p
))
1236 ret
= kernel_update_ipv4 (&rn
->p
, rib
);
1238 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
1242 ret
= kernel_update_ipv6 (&rn
->p
, rib
);
1244 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
1248 /* If install succeeds, update FIB flag for nexthops. */
1251 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1253 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1256 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1257 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1259 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1266 /* Uninstall the route from kernel. */
1268 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1271 struct nexthop
*nexthop
, *tnexthop
;
1272 rib_table_info_t
*info
= rn
->table
->info
;
1275 if (info
->safi
!= SAFI_UNICAST
)
1277 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1278 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1283 * Make sure we update the FPM any time we send new information to
1286 zfpm_trigger_update (rn
, "uninstalling from kernel");
1288 switch (PREFIX_FAMILY (&rn
->p
))
1291 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
1294 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
1298 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1299 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1304 /* Uninstall the route from kernel. */
1306 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1308 rib_table_info_t
*info
= rn
->table
->info
;
1310 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1312 if (info
->safi
== SAFI_UNICAST
)
1313 zfpm_trigger_update (rn
, "rib_uninstall");
1315 redistribute_delete (&rn
->p
, rib
);
1316 if (! RIB_SYSTEM_ROUTE (rib
))
1317 rib_uninstall_kernel (rn
, rib
);
1318 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1322 static void rib_unlink (struct route_node
*, struct rib
*);
1325 * rib_can_delete_dest
1327 * Returns TRUE if the given dest can be deleted from the table.
1330 rib_can_delete_dest (rib_dest_t
*dest
)
1338 * Don't delete the dest if we have to update the FPM about this
1341 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1342 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1351 * Garbage collect the rib dest corresponding to the given route node
1354 * Returns TRUE if the dest was deleted, FALSE otherwise.
1357 rib_gc_dest (struct route_node
*rn
)
1360 struct zebra_vrf
*zvrf
;
1362 dest
= rib_dest_from_rnode (rn
);
1366 if (!rib_can_delete_dest (dest
))
1369 zvrf
= rib_dest_vrf (dest
);
1370 if (IS_ZEBRA_DEBUG_RIB
)
1371 rnode_debug (rn
, zvrf
->vrf_id
, "removing dest from table");
1374 XFREE (MTYPE_RIB_DEST
, dest
);
1378 * Release the one reference that we keep on the route node.
1380 route_unlock_node (rn
);
1385 rib_process_add_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1388 char buf
[INET6_ADDRSTRLEN
];
1391 zfpm_trigger_update (rn
, "new route selected");
1393 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1394 if (!nexthop_active_update (rn
, select
, 1))
1396 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1400 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1401 if (IS_ZEBRA_DEBUG_RIB
)
1403 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1404 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1405 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1408 if (!RIB_SYSTEM_ROUTE (select
))
1410 if (rib_install_kernel (rn
, select
, 0))
1413 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1414 zlog_warn ("%u:%s/%d: Route install failed",
1415 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1419 /* Update for redistribution. */
1421 redistribute_update (&rn
->p
, select
, NULL
);
1422 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1426 rib_process_del_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1429 char buf
[INET6_ADDRSTRLEN
];
1431 zfpm_trigger_update (rn
, "removing existing route");
1433 /* Withdraw redistribute and uninstall from kernel. */
1434 if (IS_ZEBRA_DEBUG_RIB
)
1436 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1437 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1438 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1441 redistribute_delete(&rn
->p
, fib
);
1442 if (!RIB_SYSTEM_ROUTE (fib
))
1443 rib_uninstall_kernel (rn
, fib
);
1445 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1447 /* Update nexthop for route, reset changed flag. */
1448 nexthop_active_update (rn
, fib
, 1);
1449 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1453 rib_process_update_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1454 struct rib
*select
, struct rib
*fib
)
1456 char buf
[INET6_ADDRSTRLEN
];
1457 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1462 if (IS_ZEBRA_DEBUG_RIB
)
1463 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1466 * We have to install or update if a new route has been selected or
1467 * something has changed.
1469 if (select
!= fib
||
1470 CHECK_FLAG (select
->status
, RIB_ENTRY_CHANGED
))
1472 zfpm_trigger_update (rn
, "updating existing route");
1474 /* Update the nexthop; we could determine here that nexthop is inactive. */
1475 if (nexthop_active_update (rn
, select
, 1))
1478 /* If nexthop is active, install the selected route, if appropriate. If
1479 * the install succeeds, cleanup flags for prior route, if different from
1484 if (IS_ZEBRA_DEBUG_RIB
)
1487 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1488 "old %p (type %d)", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1489 rn
, select
, select
->type
, fib
, fib
->type
);
1491 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1492 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1494 /* Non-system route should be installed. */
1495 if (!RIB_SYSTEM_ROUTE (select
))
1497 if (rib_install_kernel (rn
, select
, 1))
1500 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1501 zlog_warn ("%u:%s/%d: Route install failed",
1502 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1506 /* If install succeeded or system route, cleanup flags for prior route. */
1507 if (installed
&& select
!= fib
)
1509 if (RIB_SYSTEM_ROUTE(select
))
1511 if (!RIB_SYSTEM_ROUTE (fib
))
1512 rib_uninstall_kernel (rn
, fib
);
1516 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1517 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1521 /* Update for redistribution. */
1524 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1525 redistribute_update (&rn
->p
, select
, (select
== fib
) ? NULL
: fib
);
1530 * If nexthop for selected route is not active or install failed, we
1531 * may need to uninstall and delete for redistribution.
1533 if (!nh_active
|| !installed
)
1537 if (IS_ZEBRA_DEBUG_RIB
)
1540 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1541 "old %p (type %d) - %s", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1542 rn
, select
, select
->type
, fib
, fib
->type
,
1543 nh_active
? "install failed" : "nexthop inactive");
1545 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) - %s",
1546 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
,
1547 nh_active
? "install failed" : "nexthop inactive");
1550 del
= (select
== fib
) ? select
: fib
;
1552 redistribute_delete(&rn
->p
, del
);
1554 if (!RIB_SYSTEM_ROUTE (del
))
1555 rib_uninstall_kernel (rn
, del
);
1556 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1562 * Same route selected; check if in the FIB and if not, re-install. This
1563 * is housekeeping code to deal with race conditions in kernel with linux
1564 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1567 if (!RIB_SYSTEM_ROUTE (select
))
1571 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1572 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1578 rib_install_kernel (rn
, select
, 0);
1582 /* Update prior route. */
1585 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1587 /* Set real nexthop. */
1588 nexthop_active_update (rn
, fib
, 1);
1589 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1592 /* Clear changed flag. */
1593 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1596 /* Check if 'alternate' RIB entry is better than 'current'. */
1598 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1600 if (current
== NULL
)
1603 /* filter route selection in following order:
1604 * - connected beats other types
1605 * - lower distance beats higher
1606 * - lower metric beats higher for equal distance
1607 * - last, hence oldest, route wins tie break.
1610 /* Connected routes. Pick the last connected
1611 * route of the set of lowest metric connected routes.
1613 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1615 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1616 || alternate
->metric
<= current
->metric
)
1622 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1625 /* higher distance loses */
1626 if (alternate
->distance
< current
->distance
)
1628 if (current
->distance
< alternate
->distance
)
1631 /* metric tie-breaks equal distance */
1632 if (alternate
->metric
<= current
->metric
)
1638 /* Core function for processing routing information base. */
1640 rib_process (struct route_node
*rn
)
1644 struct rib
*fib
= NULL
;
1645 struct rib
*select
= NULL
;
1646 struct rib
*del
= NULL
;
1647 struct rib
*best
= NULL
;
1648 char buf
[INET6_ADDRSTRLEN
];
1650 struct zebra_vrf
*zvrf
= NULL
;
1651 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1652 rib_table_info_t
*info
;
1655 info
= rn
->table
->info
;
1657 dest
= rib_dest_from_rnode (rn
);
1660 zvrf
= rib_dest_vrf (dest
);
1661 vrf_id
= zvrf
->vrf_id
;
1664 if (IS_ZEBRA_DEBUG_RIB
)
1665 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1667 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1668 zlog_debug ("%u:%s/%d: Processing rn %p", vrf_id
, buf
, rn
->p
.prefixlen
, rn
);
1670 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1672 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1673 zlog_debug ("%u:%s/%d: Examine rib %p (type %d) status %x flags %x "
1674 "dist %d metric %d",
1675 vrf_id
, buf
, rn
->p
.prefixlen
, rib
, rib
->type
, rib
->status
,
1676 rib
->flags
, rib
->distance
, rib
->metric
);
1678 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1680 /* Currently installed rib. */
1681 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1683 assert (fib
== NULL
);
1687 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1688 * which we need to do do further work with below.
1690 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1694 if (IS_ZEBRA_DEBUG_RIB
)
1695 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1696 (void *)rn
, (void *)rib
);
1697 rib_unlink (rn
, rib
);
1705 /* Skip unreachable nexthop. */
1706 /* This first call to nexthop_active_update is merely to determine if
1707 * there's any change to nexthops associated with this RIB entry. Now,
1708 * rib_process() can be invoked due to an external event such as link
1709 * down or due to next-hop-tracking evaluation. In the latter case,
1710 * a decision has already been made that the NHs have changed. So, no
1711 * need to invoke a potentially expensive call again. Further, since
1712 * the change might be in a recursive NH which is not caught in
1713 * the nexthop_active_update() code. Thus, we might miss changes to
1716 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1717 ! nexthop_active_update (rn
, rib
, 0))
1719 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1721 /* This entry was denied by the 'ip protocol table' route-map, we
1722 * need to delete it */
1725 if (IS_ZEBRA_DEBUG_RIB
)
1726 zlog_debug ("%s: %s/%d: imported via import-table but denied "
1727 "by the ip protocol table route-map",
1728 __func__
, buf
, rn
->p
.prefixlen
);
1729 rib_unlink (rn
, rib
);
1738 if (info
->safi
== SAFI_MULTICAST
)
1741 /* Infinite distance. */
1742 if (rib
->distance
== DISTANCE_INFINITY
)
1744 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1748 best
= rib_choose_best(select
, rib
);
1749 if (select
&& best
!= select
)
1750 UNSET_FLAG (select
->status
, RIB_ENTRY_CHANGED
);
1752 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1754 } /* RNODE_FOREACH_RIB_SAFE */
1756 /* After the cycle is finished, the following pointers will be set:
1757 * select --- the winner RIB entry, if any was found, otherwise NULL
1758 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1759 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1762 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1763 zlog_debug ("%u:%s/%d: After processing: select %p fib %p del %p",
1764 vrf_id
, buf
, rn
->p
.prefixlen
, select
, fib
, del
);
1766 /* Same RIB entry is selected. Update FIB and finish. */
1767 if (select
&& select
== fib
)
1768 rib_process_update_route (zvrf
, rn
, select
, select
);
1769 else if (select
&& fib
)
1770 rib_process_update_route (zvrf
, rn
, select
, fib
);
1772 rib_process_add_route (zvrf
, rn
, select
);
1774 rib_process_del_route (zvrf
, rn
, fib
);
1777 if (select
&& select
== fib
)
1779 if (IS_ZEBRA_DEBUG_RIB
)
1780 rnode_debug (rn
, vrf_id
, "Updating existing route, select %p, fib %p",
1781 (void *)select
, (void *)fib
);
1782 if (CHECK_FLAG (select
->status
, RIB_ENTRY_CHANGED
))
1784 if (info
->safi
== SAFI_UNICAST
)
1785 zfpm_trigger_update (rn
, "updating existing route");
1787 /* Set real nexthop. */
1788 /* Need to check if any NHs are active to clear the
1791 if (nexthop_active_update (rn
, select
, 1))
1793 if (IS_ZEBRA_DEBUG_RIB
)
1794 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1795 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1796 if (! RIB_SYSTEM_ROUTE (select
))
1798 /* Clear FIB flag if performing a replace, will get set again
1799 * as part of install.
1801 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1802 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1803 rib_install_kernel (rn
, select
, 1);
1806 /* assuming that the receiver knows how to dedup */
1807 redistribute_update (&rn
->p
, select
, NULL
);
1811 if (IS_ZEBRA_DEBUG_RIB
)
1812 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1813 "- nexthop inactive",
1814 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1816 /* Withdraw unreachable redistribute route */
1817 redistribute_delete(&rn
->p
, select
);
1819 /* Do the uninstall here, if not done earlier. */
1820 if (! RIB_SYSTEM_ROUTE (select
))
1821 rib_uninstall_kernel (rn
, select
);
1822 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1824 UNSET_FLAG (select
->status
, RIB_ENTRY_CHANGED
);
1826 else if (! RIB_SYSTEM_ROUTE (select
))
1828 /* Housekeeping code to deal with
1829 race conditions in kernel with linux
1830 netlink reporting interface up before IPv4 or IPv6 protocol
1831 is ready to add routes.
1832 This makes sure the routes are IN the kernel.
1835 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1836 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1842 rib_install_kernel (rn
, select
, 0);
1847 /* At this point we either haven't found the best RIB entry or it is
1848 * different from what we currently intend to flag with SELECTED. In both
1849 * cases, if a RIB block is present in FIB, it should be withdrawn.
1853 if (IS_ZEBRA_DEBUG_RIB
)
1854 rnode_debug (rn
, vrf_id
, "Removing existing route, fib %p", (void *)fib
);
1856 if (info
->safi
== SAFI_UNICAST
)
1857 zfpm_trigger_update (rn
, "removing existing route");
1859 /* If there's no route to replace this with, withdraw redistribute and
1860 * uninstall from kernel.
1864 if (IS_ZEBRA_DEBUG_RIB
)
1865 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1866 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1868 redistribute_delete(&rn
->p
, fib
);
1869 if (! RIB_SYSTEM_ROUTE (fib
))
1870 rib_uninstall_kernel (rn
, fib
);
1873 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1875 /* Set real nexthop. */
1876 nexthop_active_update (rn
, fib
, 1);
1877 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1880 /* Regardless of some RIB entry being SELECTED or not before, now we can
1881 * tell, that if a new winner exists, FIB is still not updated with this
1882 * data, but ready to be.
1886 if (IS_ZEBRA_DEBUG_RIB
)
1887 rnode_debug (rn
, "Adding route, select %p", (void *)select
);
1889 if (info
->safi
== SAFI_UNICAST
)
1890 zfpm_trigger_update (rn
, "new route selected");
1892 /* Set real nexthop. */
1893 if (nexthop_active_update (rn
, select
, 1))
1895 if (IS_ZEBRA_DEBUG_RIB
)
1898 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1899 "old %p (type %d)", vrf_id
, buf
, rn
->p
.prefixlen
, rn
,
1900 select
, select
->type
, fib
, fib
->type
);
1902 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1903 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1906 if (! RIB_SYSTEM_ROUTE (select
))
1908 /* Clear FIB flag if performing a replace, will get set again
1909 * as part of install.
1913 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1914 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1916 rib_install_kernel (rn
, select
, fib
? 1 : 0);
1920 /* Uninstall prior route here, if needed. */
1921 if (fib
&& !RIB_SYSTEM_ROUTE (fib
))
1922 rib_uninstall_kernel (rn
, fib
);
1925 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1926 /* Unconditionally announce, this part is exercised by new routes */
1927 /* If we cannot add, for example route added is learnt by the */
1928 /* protocol we're trying to redistribute to, delete the redist */
1929 /* This is notified by setting the is_update to 1 */
1930 redistribute_update (&rn
->p
, select
, fib
);
1934 /* Uninstall prior route here and do redist delete, if needed. */
1937 if (IS_ZEBRA_DEBUG_RIB
)
1938 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1939 "- nexthop inactive",
1940 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1942 if (!RIB_SYSTEM_ROUTE (fib
))
1943 rib_uninstall_kernel (rn
, fib
);
1944 redistribute_delete(&rn
->p
, fib
);
1947 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1951 /* FIB route was removed, should be deleted */
1954 if (IS_ZEBRA_DEBUG_RIB
)
1955 rnode_debug (rn
, vrf_id
, "Deleting fib %p, rn %p", (void *)del
, (void *)rn
);
1956 rib_unlink (rn
, del
);
1960 * Check if the dest can be deleted now.
1965 /* Take a list of route_node structs and return 1, if there was a record
1966 * picked from it and processed by rib_process(). Don't process more,
1967 * than one RN record; operate only in the specified sub-queue.
1970 process_subq (struct list
* subq
, u_char qindex
)
1972 struct listnode
*lnode
= listhead (subq
);
1973 struct route_node
*rnode
;
1974 char buf
[INET6_ADDRSTRLEN
];
1976 struct zebra_vrf
*zvrf
= NULL
;
1981 rnode
= listgetdata (lnode
);
1982 dest
= rib_dest_from_rnode (rnode
);
1984 zvrf
= rib_dest_vrf (dest
);
1986 rib_process (rnode
);
1988 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1990 inet_ntop (rnode
->p
.family
, &rnode
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1991 zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u",
1992 zvrf
? zvrf
->vrf_id
: 0, buf
, rnode
->p
.prefixlen
, rnode
, qindex
);
1996 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2001 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2002 __func__
, rnode
, rnode
->lock
);
2003 zlog_backtrace(LOG_DEBUG
);
2006 route_unlock_node (rnode
);
2007 list_delete_node (subq
, lnode
);
2012 * All meta queues have been processed. Trigger next-hop evaluation.
2015 meta_queue_process_complete (struct work_queue
*dummy
)
2018 struct zebra_vrf
*zvrf
;
2020 /* Evaluate nexthops for those VRFs which underwent route processing. This
2021 * should limit the evaluation to the necessary VRFs in most common
2024 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
2026 if (((zvrf
= vrf_iter2info (iter
)) != NULL
) &&
2027 (zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2029 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2030 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2031 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2032 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2033 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2038 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2039 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
2040 * is pointed to the meta queue structure.
2042 static wq_item_status
2043 meta_queue_process (struct work_queue
*dummy
, void *data
)
2045 struct meta_queue
* mq
= data
;
2048 for (i
= 0; i
< MQ_SIZE
; i
++)
2049 if (process_subq (mq
->subq
[i
], i
))
2054 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2058 * Map from rib types to queue type (priority) in meta queue
2060 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
2061 [ZEBRA_ROUTE_SYSTEM
] = 4,
2062 [ZEBRA_ROUTE_KERNEL
] = 0,
2063 [ZEBRA_ROUTE_CONNECT
] = 0,
2064 [ZEBRA_ROUTE_STATIC
] = 1,
2065 [ZEBRA_ROUTE_RIP
] = 2,
2066 [ZEBRA_ROUTE_RIPNG
] = 2,
2067 [ZEBRA_ROUTE_OSPF
] = 2,
2068 [ZEBRA_ROUTE_OSPF6
] = 2,
2069 [ZEBRA_ROUTE_ISIS
] = 2,
2070 [ZEBRA_ROUTE_BGP
] = 3,
2071 [ZEBRA_ROUTE_HSLS
] = 4,
2072 [ZEBRA_ROUTE_TABLE
] = 1,
2075 /* Look into the RN and queue it into one or more priority queues,
2076 * increasing the size for each data push done.
2079 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
2083 RNODE_FOREACH_RIB (rn
, rib
)
2085 u_char qindex
= meta_queue_map
[rib
->type
];
2086 struct zebra_vrf
*zvrf
;
2088 /* Invariant: at this point we always have rn->info set. */
2089 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
2090 RIB_ROUTE_QUEUED (qindex
)))
2092 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2093 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
2094 (void *)rn
, qindex
);
2098 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2099 listnode_add (mq
->subq
[qindex
], rn
);
2100 route_lock_node (rn
);
2103 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2104 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
2105 (void *)rn
, qindex
);
2107 zvrf
= zebra_vrf_lookup (rib
->vrf_id
);
2109 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2113 /* Add route_node to work queue and schedule processing */
2115 rib_queue_add (struct route_node
*rn
)
2119 /* Pointless to queue a route_node with no RIB entries to add or remove */
2120 if (!rnode_to_ribs (rn
))
2122 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2123 __func__
, (void *)rn
, rn
->lock
);
2124 zlog_backtrace(LOG_DEBUG
);
2128 if (zebrad
.ribq
== NULL
)
2130 zlog_err ("%s: work_queue does not exist!", __func__
);
2135 * The RIB queue should normally be either empty or holding the only
2136 * work_queue_item element. In the latter case this element would
2137 * hold a pointer to the meta queue structure, which must be used to
2138 * actually queue the route nodes to process. So create the MQ
2139 * holder, if necessary, then push the work into it in any case.
2140 * This semantics was introduced after 0.99.9 release.
2142 if (!zebrad
.ribq
->items
->count
)
2143 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
2145 rib_meta_queue_add (zebrad
.mq
, rn
);
2150 /* Create new meta queue.
2151 A destructor function doesn't seem to be necessary here.
2153 static struct meta_queue
*
2154 meta_queue_new (void)
2156 struct meta_queue
*new;
2159 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
2162 for (i
= 0; i
< MQ_SIZE
; i
++)
2164 new->subq
[i
] = list_new ();
2165 assert(new->subq
[i
]);
2171 /* initialise zebra rib work queue */
2173 rib_queue_init (struct zebra_t
*zebra
)
2177 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
2178 "route_node processing")))
2180 zlog_err ("%s: could not initialise work queue!", __func__
);
2184 /* fill in the work queue spec */
2185 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2186 zebra
->ribq
->spec
.errorfunc
= NULL
;
2187 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2188 /* XXX: TODO: These should be runtime configurable via vty */
2189 zebra
->ribq
->spec
.max_retries
= 3;
2190 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
2192 if (!(zebra
->mq
= meta_queue_new ()))
2194 zlog_err ("%s: could not initialise meta queue!", __func__
);
2200 /* RIB updates are processed via a queue of pointers to route_nodes.
2202 * The queue length is bounded by the maximal size of the routing table,
2203 * as a route_node will not be requeued, if already queued.
2205 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2206 * state, or static_install_route (when an existing RIB is updated)
2207 * and then submit route_node to queue for best-path selection later.
2208 * Order of add/delete state changes are preserved for any given RIB.
2210 * Deleted RIBs are reaped during best-path selection.
2213 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2214 * |-------->| | best RIB, if required
2216 * static_install->|->rib_addqueue...... -> rib_process
2218 * |-------->| |-> rib_unlink
2219 * |-> set RIB_ENTRY_REMOVE |
2220 * rib_delnode (RIB freed)
2222 * The 'info' pointer of a route_node points to a rib_dest_t
2223 * ('dest'). Queueing state for a route_node is kept on the dest. The
2224 * dest is created on-demand by rib_link() and is kept around at least
2225 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2227 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2229 * - route_nodes: refcounted by:
2230 * - dest attached to route_node:
2231 * - managed by: rib_link/rib_gc_dest
2232 * - route_node processing queue
2233 * - managed by: rib_addqueue, rib_process.
2237 /* Add RIB to head of the route node. */
2239 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2244 const char *rmap_name
;
2248 dest
= rib_dest_from_rnode (rn
);
2251 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2252 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2254 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2255 route_lock_node (rn
); /* rn route table reference */
2260 head
= dest
->routes
;
2268 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2269 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2270 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2272 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2273 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2281 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2283 /* RIB node has been un-removed before route-node is processed.
2284 * route_node must hence already be on the queue for processing..
2286 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2288 if (IS_ZEBRA_DEBUG_RIB
)
2289 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2291 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2294 rib_link (rn
, rib
, process
);
2300 * Detach a rib structure from a route_node.
2302 * Note that a call to rib_unlink() should be followed by a call to
2303 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2304 * longer required to be deleted.
2307 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2313 if (IS_ZEBRA_DEBUG_RIB
)
2314 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2316 dest
= rib_dest_from_rnode (rn
);
2319 rib
->next
->prev
= rib
->prev
;
2322 rib
->prev
->next
= rib
->next
;
2325 dest
->routes
= rib
->next
;
2328 /* free RIB and nexthops */
2329 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2330 nexthops_free(rib
->nexthop
);
2331 XFREE (MTYPE_RIB
, rib
);
2336 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2340 if (IS_ZEBRA_DEBUG_RIB
)
2341 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2342 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2344 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2345 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2346 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2348 zebra_del_import_table_entry(rn
, rib
);
2349 /* Just clean up if non main table */
2350 if (IS_ZEBRA_DEBUG_RIB
)
2352 char buf
[INET6_ADDRSTRLEN
];
2353 if (IS_ZEBRA_DEBUG_RIB
)
2355 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2356 zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)",
2357 rib
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, rib
, rib
->type
);
2361 rib_unlink(rn
, rib
);
2369 /* This function dumps the contents of a given RIB entry into
2370 * standard debug log. Calling function name and IP prefix in
2371 * question are passed as 1st and 2nd arguments.
2374 void _rib_dump (const char * func
,
2375 union prefix46constptr pp
, const struct rib
* rib
)
2377 const struct prefix
*p
= pp
.p
;
2378 char straddr
[PREFIX_STRLEN
];
2379 struct nexthop
*nexthop
, *tnexthop
;
2382 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func
, (const void *)rib
,
2383 prefix2str(pp
, straddr
, sizeof(straddr
)), rib
->vrf_id
);
2386 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2389 (unsigned long) rib
->uptime
,
2396 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2406 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2409 rib
->nexthop_active_num
,
2410 rib
->nexthop_fib_num
2413 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2415 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2418 "%s: %s %s with flags %s%s%s",
2420 (recursing
? " NH" : "NH"),
2422 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2423 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2424 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2427 zlog_debug ("%s: dump complete", func
);
2430 /* This is an exported helper to rtm_read() to dump the strange
2431 * RIB entry found by rib_lookup_ipv4_route()
2434 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2436 struct route_table
*table
;
2437 struct route_node
*rn
;
2439 char prefix_buf
[INET_ADDRSTRLEN
];
2442 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2445 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2449 /* Scan the RIB table for exactly matching RIB entry. */
2450 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2452 /* No route for this prefix. */
2455 zlog_debug ("%s: lookup failed for %s", __func__
,
2456 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2461 route_unlock_node (rn
);
2464 RNODE_FOREACH_RIB (rn
, rib
)
2468 "%s: rn %p, rib %p: %s, %s",
2472 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2473 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2479 /* Check if requested address assignment will fail due to another
2480 * route being installed by zebra in FIB already. Take necessary
2481 * actions, if needed: remove such a route from FIB and deSELECT
2482 * corresponding RIB entry. Then put affected RN into RIBQ head.
2484 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2486 struct route_table
*table
;
2487 struct route_node
*rn
;
2489 unsigned changed
= 0;
2491 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2493 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2497 /* No matches would be the simplest case. */
2498 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2502 route_unlock_node (rn
);
2504 /* Check all RIB entries. In case any changes have to be done, requeue
2505 * the RN into RIBQ head. If the routing message about the new connected
2506 * route (generated by the IP address we are going to assign very soon)
2507 * comes before the RIBQ is processed, the new RIB entry will join
2508 * RIBQ record already on head. This is necessary for proper revalidation
2509 * of the rest of the RIB.
2511 RNODE_FOREACH_RIB (rn
, rib
)
2513 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
2514 ! RIB_SYSTEM_ROUTE (rib
))
2517 if (IS_ZEBRA_DEBUG_RIB
)
2519 char buf
[PREFIX_STRLEN
];
2520 zlog_debug ("%u:%s: freeing way for connected prefix",
2521 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2522 rib_dump (&rn
->p
, rib
);
2524 rib_uninstall (rn
, rib
);
2532 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2535 struct route_table
*table
;
2536 struct route_node
*rn
;
2538 struct nexthop
*nexthop
;
2545 if (p
->family
== AF_INET
)
2551 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2555 /* Make it sure prefixlen is applied to the prefix. */
2558 /* Set default distance by route type. */
2559 if (rib
->distance
== 0)
2561 rib
->distance
= route_info
[rib
->type
].distance
;
2563 /* iBGP distance is 200. */
2564 if (rib
->type
== ZEBRA_ROUTE_BGP
2565 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2566 rib
->distance
= 200;
2569 /* Lookup route node.*/
2570 rn
= route_node_get (table
, p
);
2572 /* If same type of route are installed, treat it as a implicit
2574 RNODE_FOREACH_RIB (rn
, same
)
2576 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2579 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2580 && same
->table
== rib
->table
2581 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2585 /* If this route is kernel route, set FIB flag to the route. */
2586 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2587 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2588 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2590 /* Link new rib to node.*/
2591 if (IS_ZEBRA_DEBUG_RIB
)
2593 char buf
[INET6_ADDRSTRLEN
];
2594 if (IS_ZEBRA_DEBUG_RIB
)
2596 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2597 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2599 rib
->vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2600 (void *)rib
, rib
->type
, (void *)same
);
2603 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2604 rib_dump ((struct prefix
*)p
, rib
);
2606 rib_addnode (rn
, rib
, 1);
2609 /* Free implicit route.*/
2612 rib_delnode (rn
, same
);
2616 route_unlock_node (rn
);
2621 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2622 int flags
, struct prefix
*p
, union g_addr
*gate
, ifindex_t ifindex
,
2625 struct route_table
*table
;
2626 struct route_node
*rn
;
2628 struct rib
*fib
= NULL
;
2629 struct rib
*same
= NULL
;
2630 struct nexthop
*nexthop
, *tnexthop
;
2632 char buf1
[PREFIX_STRLEN
];
2633 char buf2
[INET6_ADDRSTRLEN
];
2636 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2643 /* Lookup route node. */
2644 rn
= route_node_lookup (table
, p
);
2647 if (IS_ZEBRA_DEBUG_RIB
)
2648 zlog_debug ("%u:%s: doesn't exist in rib",
2649 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)));
2650 return ZEBRA_ERR_RTNOEXIST
;
2653 /* Lookup same type route. */
2654 RNODE_FOREACH_RIB (rn
, rib
)
2656 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2659 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2662 if (rib
->type
!= type
)
2664 if (rib
->instance
!= instance
)
2666 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2667 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2669 if (nexthop
->ifindex
!= ifindex
)
2674 route_unlock_node (rn
);
2675 route_unlock_node (rn
);
2681 /* Make sure that the route found has the same gateway. */
2689 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2690 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2691 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2700 /* If same type of route can't be found and this message is from
2704 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2705 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2707 if (IS_ZEBRA_DEBUG_RIB
)
2709 zlog_debug ("%u:%s: rn %p, rib %p (type %d) was deleted "
2710 "from kernel, adding",
2711 vrf_id
, prefix2str(p
, buf1
, INET6_ADDRSTRLEN
),
2712 rn
, fib
, fib
->type
);
2717 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2718 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2720 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2724 /* This means someone else, other than Zebra, has deleted
2725 * a Zebra router from the kernel. We will add it back */
2726 rib_install_kernel(rn
, fib
, 0);
2731 if (IS_ZEBRA_DEBUG_RIB
)
2734 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2735 "doesn't exist in rib",
2736 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2737 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
),
2741 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2742 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2746 route_unlock_node (rn
);
2747 return ZEBRA_ERR_RTNOEXIST
;
2752 rib_delnode (rn
, same
);
2754 route_unlock_node (rn
);
2761 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2762 u_short instance
, int flags
, struct prefix
*p
,
2763 union g_addr
*gate
, union g_addr
*src
, ifindex_t ifindex
,
2764 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2768 struct rib
*same
= NULL
;
2769 struct route_table
*table
;
2770 struct route_node
*rn
;
2771 struct nexthop
*nexthop
;
2774 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2778 /* Make sure mask is applied. */
2781 /* Set default distance by route type. */
2784 if ((unsigned)type
>= array_size(route_info
))
2787 distance
= route_info
[type
].distance
;
2789 /* iBGP distance is 200. */
2790 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2794 /* Lookup route node.*/
2795 rn
= route_node_get (table
, p
);
2797 /* If same type of route are installed, treat it as a implicit
2799 RNODE_FOREACH_RIB (rn
, rib
)
2801 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2804 if (rib
->type
!= type
)
2806 if (rib
->instance
!= instance
)
2808 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2813 /* Duplicate connected route comes in. */
2814 else if ((nexthop
= rib
->nexthop
) &&
2815 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2816 nexthop
->ifindex
== ifindex
&&
2817 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2824 /* Allocate new rib structure. */
2825 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2828 rib
->instance
= instance
;
2829 rib
->distance
= distance
;
2831 rib
->metric
= metric
;
2833 rib
->table
= table_id
;
2834 rib
->vrf_id
= vrf_id
;
2835 rib
->nexthop_num
= 0;
2836 rib
->uptime
= time (NULL
);
2838 /* Nexthop settings. */
2844 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2846 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2851 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2853 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2857 rib_nexthop_ifindex_add (rib
, ifindex
);
2859 /* If this route is kernel route, set FIB flag to the route. */
2860 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2861 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2862 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2864 /* Link new rib to node.*/
2865 if (IS_ZEBRA_DEBUG_RIB
)
2867 char buf
[INET6_ADDRSTRLEN
];
2868 if (IS_ZEBRA_DEBUG_RIB
)
2870 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2871 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2873 vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2874 (void *)rib
, rib
->type
, (void *)same
);
2877 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2880 rib_addnode (rn
, rib
, 1);
2882 /* Free implicit route.*/
2884 rib_delnode (rn
, same
);
2886 route_unlock_node (rn
);
2890 /* Schedule routes of a particular table (address-family) based on event. */
2892 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2894 struct route_node
*rn
;
2895 struct rib
*rib
, *next
;
2897 /* Walk all routes and queue for processing, if appropriate for
2898 * the trigger event.
2900 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2904 case RIB_UPDATE_IF_CHANGE
:
2905 /* Examine all routes that won't get processed by the protocol or
2906 * triggered by nexthop evaluation (NHT). This would be system,
2907 * kernel and certain static routes. Note that NHT will get
2908 * triggered upon an interface event as connected routes always
2909 * get queued for processing.
2911 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2913 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2914 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2915 rib
->type
== ZEBRA_ROUTE_BGP
)
2916 continue; /* protocol will handle. */
2917 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2920 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2921 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2922 nh
->type
== NEXTHOP_TYPE_IPV6
))
2925 /* If we only have nexthops to a gateway, NHT will
2936 case RIB_UPDATE_RMAP_CHANGE
:
2937 case RIB_UPDATE_OTHER
:
2938 /* Right now, examine all routes. Can restrict to a protocol in
2939 * some cases (TODO).
2941 if (rnode_to_ribs (rn
))
2951 /* RIB update function. */
2953 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
2955 struct route_table
*table
;
2957 /* Process routes of interested address-families. */
2958 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2960 rib_update_table (table
, event
);
2962 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2964 rib_update_table (table
, event
);
2967 /* Remove all routes which comes from non main table. */
2969 rib_weed_table (struct route_table
*table
)
2971 struct route_node
*rn
;
2976 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2977 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2979 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2982 if (rib
->table
!= zebrad
.rtm_table_default
&&
2983 rib
->table
!= RT_TABLE_MAIN
)
2984 rib_delnode (rn
, rib
);
2988 /* Delete all routes from non main table. */
2990 rib_weed_tables (void)
2993 struct zebra_vrf
*zvrf
;
2995 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
2996 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
2998 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2999 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3003 /* Delete self installed routes after zebra is relaunched. */
3005 rib_sweep_table (struct route_table
*table
)
3007 struct route_node
*rn
;
3013 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3014 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3016 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3019 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3020 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3022 ret
= rib_uninstall_kernel (rn
, rib
);
3024 rib_delnode (rn
, rib
);
3029 /* Sweep all RIB tables. */
3031 rib_sweep_route (void)
3034 struct zebra_vrf
*zvrf
;
3036 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3037 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3039 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3040 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3044 /* Remove specific by protocol routes from 'table'. */
3045 static unsigned long
3046 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
3048 struct route_node
*rn
;
3051 unsigned long n
= 0;
3054 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3055 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3057 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3059 if (rib
->type
== proto
&& rib
->instance
== instance
)
3061 rib_delnode (rn
, rib
);
3069 /* Remove specific by protocol routes. */
3071 rib_score_proto (u_char proto
, u_short instance
)
3074 struct zebra_vrf
*zvrf
;
3075 unsigned long cnt
= 0;
3077 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3078 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3079 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3080 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3085 /* Close RIB and clean up kernel routes. */
3087 rib_close_table (struct route_table
*table
)
3089 struct route_node
*rn
;
3090 rib_table_info_t
*info
= table
->info
;
3094 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3095 RNODE_FOREACH_RIB (rn
, rib
)
3097 if (!CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3100 if (info
->safi
== SAFI_UNICAST
)
3101 zfpm_trigger_update (rn
, NULL
);
3103 if (! RIB_SYSTEM_ROUTE (rib
))
3104 rib_uninstall_kernel (rn
, rib
);
3108 /* Close all RIB tables. */
3113 struct zebra_vrf
*zvrf
;
3114 struct listnode
*node
;
3115 struct interface
*ifp
;
3118 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3120 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3122 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3123 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3125 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter
), node
, ifp
))
3126 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
3129 /* If we do multiple tables per vrf, need to move this to loop above */
3130 zvrf
= vrf_info_lookup (VRF_DEFAULT
);
3132 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
3134 if (zvrf
->other_table
[AFI_IP
][table_id
])
3135 rib_close_table (zvrf
->other_table
[AFI_IP
][table_id
]);
3137 if (zvrf
->other_table
[AFI_IP6
][table_id
])
3138 rib_close_table (zvrf
->other_table
[AFI_IP6
][table_id
]);
3141 zebra_mpls_close_tables(zvrf
);
3145 /* Routing information base initialize. */
3149 rib_queue_init (&zebrad
);
3155 * Get the first vrf id that is greater than the given vrf id if any.
3157 * Returns TRUE if a vrf id was found, FALSE otherwise.
3160 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3162 vrf_iter_t iter
= vrf_iterator (vrf_id
);
3163 struct zebra_vrf
*zvrf
= vrf_iter2info (iter
);
3165 /* The same one ? Then find out the next. */
3166 if (zvrf
&& (zvrf
->vrf_id
== vrf_id
))
3167 zvrf
= vrf_iter2info (vrf_next (iter
));
3171 *next_id_p
= zvrf
->vrf_id
;
3179 * rib_tables_iter_next
3181 * Returns the next table in the iteration.
3183 struct route_table
*
3184 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3186 struct route_table
*table
;
3189 * Array that helps us go over all AFI/SAFI combinations via one
3196 { AFI_IP
, SAFI_UNICAST
},
3197 { AFI_IP
, SAFI_MULTICAST
},
3198 { AFI_IP6
, SAFI_UNICAST
},
3199 { AFI_IP6
, SAFI_MULTICAST
},
3204 switch (iter
->state
)
3207 case RIB_TABLES_ITER_S_INIT
:
3208 iter
->vrf_id
= VRF_DEFAULT
;
3209 iter
->afi_safi_ix
= -1;
3213 case RIB_TABLES_ITER_S_ITERATING
:
3214 iter
->afi_safi_ix
++;
3218 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3220 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3221 afi_safis
[iter
->afi_safi_ix
].safi
,
3226 iter
->afi_safi_ix
++;
3230 * Found another table in this vrf.
3236 * Done with all tables in the current vrf, go to the next
3239 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3242 iter
->afi_safi_ix
= 0;
3247 case RIB_TABLES_ITER_S_DONE
:
3252 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3254 iter
->state
= RIB_TABLES_ITER_S_DONE
;