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"
41 #include "zebra/rib.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zserv.h"
45 #include "zebra/zebra_vrf.h"
46 #include "zebra/redistribute.h"
47 #include "zebra/zebra_routemap.h"
48 #include "zebra/debug.h"
49 #include "zebra/zebra_fpm.h"
50 #include "zebra/zebra_rnh.h"
51 #include "zebra/interface.h"
52 #include "zebra/connected.h"
54 /* Should we allow non Quagga processes to delete our routes */
55 extern int allow_delete
;
57 /* Hold time for RIB process, should be very minimal.
58 * it is useful to able to set it otherwise for testing, hence exported
59 * as global here for test-rig code.
61 int rib_process_hold_time
= 10;
63 /* Each route type's string and default distance value. */
68 } route_info
[ZEBRA_ROUTE_MAX
] =
70 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
71 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
72 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
73 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
74 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
75 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
76 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
77 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
78 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
79 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
80 /* no entry/default: 150 */
83 /* RPF lookup behaviour */
84 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
87 static void __attribute__((format (printf
, 5, 6)))
88 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
89 const char *msgfmt
, ...)
91 char buf
[PREFIX_STRLEN
+ 8];
96 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
101 rib_table_info_t
*info
= rn
->table
->info
;
103 prefix2str(&rn
->p
, buf
, sizeof(buf
));
104 if (info
->safi
== SAFI_MULTICAST
)
105 strcat(buf
, " (MRIB)");
109 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
112 zlog (NULL
, priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
115 #define rnode_debug(node, vrf_id, ...) \
116 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
117 #define rnode_info(node, ...) \
118 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
121 is_zebra_valid_kernel_table(u_int32_t table_id
)
123 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
127 if ((table_id
== RT_TABLE_UNSPEC
) ||
128 (table_id
== RT_TABLE_LOCAL
) ||
129 (table_id
== RT_TABLE_COMPAT
))
137 is_zebra_main_routing_table(u_int32_t table_id
)
139 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
145 zebra_check_addr (struct prefix
*p
)
147 if (p
->family
== AF_INET
)
151 addr
= p
->u
.prefix4
.s_addr
;
154 if (IPV4_NET127 (addr
)
156 || IPV4_LINKLOCAL(addr
))
159 if (p
->family
== AF_INET6
)
161 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
163 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
169 /* Add nexthop to the end of a rib node's nexthop list */
171 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
173 nexthop_add(&rib
->nexthop
, nexthop
);
180 * copy_nexthop - copy a nexthop to the rib structure.
183 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
185 struct nexthop
*nexthop
;
187 nexthop
= nexthop_new();
188 nexthop
->flags
= nh
->flags
;
189 nexthop
->type
= nh
->type
;
190 nexthop
->ifindex
= nh
->ifindex
;
191 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
192 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
193 rib_nexthop_add(rib
, nexthop
);
194 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
195 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
198 /* Delete specified nexthop from the list. */
200 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
203 nexthop
->next
->prev
= nexthop
->prev
;
205 nexthop
->prev
->next
= nexthop
->next
;
207 rib
->nexthop
= nexthop
->next
;
214 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
216 struct nexthop
*nexthop
;
218 nexthop
= nexthop_new();
219 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
220 nexthop
->ifindex
= ifindex
;
222 rib_nexthop_add (rib
, nexthop
);
228 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
230 struct nexthop
*nexthop
;
232 nexthop
= nexthop_new();
233 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
234 nexthop
->gate
.ipv4
= *ipv4
;
236 nexthop
->src
.ipv4
= *src
;
238 rib_nexthop_add (rib
, nexthop
);
244 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
245 struct in_addr
*src
, ifindex_t ifindex
)
247 struct nexthop
*nexthop
;
248 struct interface
*ifp
;
250 nexthop
= nexthop_new();
251 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
252 nexthop
->gate
.ipv4
= *ipv4
;
254 nexthop
->src
.ipv4
= *src
;
255 nexthop
->ifindex
= ifindex
;
256 ifp
= if_lookup_by_index (nexthop
->ifindex
);
257 /*Pending: need to think if null ifp here is ok during bootup?
258 There was a crash because ifp here was coming to be NULL */
260 if (connected_is_unnumbered(ifp
)) {
261 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
264 rib_nexthop_add (rib
, nexthop
);
270 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
272 struct nexthop
*nexthop
;
274 nexthop
= nexthop_new();
275 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
276 nexthop
->gate
.ipv6
= *ipv6
;
278 rib_nexthop_add (rib
, nexthop
);
284 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
287 struct nexthop
*nexthop
;
289 nexthop
= nexthop_new();
290 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
291 nexthop
->gate
.ipv6
= *ipv6
;
292 nexthop
->ifindex
= ifindex
;
294 rib_nexthop_add (rib
, nexthop
);
300 rib_nexthop_blackhole_add (struct rib
*rib
)
302 struct nexthop
*nexthop
;
304 nexthop
= nexthop_new();
305 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
306 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
308 rib_nexthop_add (rib
, nexthop
);
313 /* This method checks whether a recursive nexthop has at
314 * least one resolved nexthop in the fib.
317 nexthop_has_fib_child(struct nexthop
*nexthop
)
321 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
324 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
325 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
331 /* If force flag is not set, do not modify falgs at all for uninstall
332 the route from FIB. */
334 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
335 struct route_node
*top
)
337 struct prefix_ipv4 p
;
338 struct route_table
*table
;
339 struct route_node
*rn
;
342 struct nexthop
*newhop
, *tnewhop
;
343 struct nexthop
*resolved_hop
;
345 struct interface
*ifp
;
347 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
348 nexthop
->ifindex
= 0;
352 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
353 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
354 nexthops_free(nexthop
->resolved
);
355 nexthop
->resolved
= NULL
;
356 rib
->nexthop_mtu
= 0;
359 /* Skip nexthops that have been filtered out due to route-map */
360 /* The nexthops are specific to this route and so the same */
361 /* nexthop for a different route may not have this flag set */
362 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
366 * Check to see if we should trust the passed in information
367 * for UNNUMBERED interfaces as that we won't find the GW
368 * address in the routing table.
370 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
372 ifp
= if_lookup_by_index (nexthop
->ifindex
);
373 if (ifp
&& connected_is_unnumbered(ifp
))
375 if (if_is_operative(ifp
))
384 /* Make lookup prefix. */
385 memset (&p
, 0, sizeof (struct prefix_ipv4
));
387 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
388 p
.prefix
= nexthop
->gate
.ipv4
;
391 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, rib
->vrf_id
);
395 rn
= route_node_match (table
, (struct prefix
*) &p
);
398 route_unlock_node (rn
);
400 /* If lookup self prefix return immediately. */
404 /* Pick up selected route. */
405 /* However, do not resolve over default route unless explicitly allowed. */
406 if (is_default_prefix (&rn
->p
) &&
407 !nh_resolve_via_default (p
.family
))
410 RNODE_FOREACH_RIB (rn
, match
)
412 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
415 /* if the next hop is imported from another table, skip it */
416 if (match
->type
== ZEBRA_ROUTE_TABLE
)
418 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
422 /* If there is no selected route or matched route is EGP, go up
428 } while (rn
&& rn
->info
== NULL
);
430 route_lock_node (rn
);
434 /* If the longest prefix match for the nexthop yields
435 * a blackhole, mark it as inactive. */
436 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
437 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
440 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
442 /* Directly point connected route. */
443 newhop
= match
->nexthop
;
444 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
445 nexthop
->ifindex
= newhop
->ifindex
;
449 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
452 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
453 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
454 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
458 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
459 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
461 resolved_hop
= nexthop_new();
462 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
463 /* If the resolving route specifies a gateway, use it */
464 if (newhop
->type
== NEXTHOP_TYPE_IPV4
465 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
467 resolved_hop
->type
= newhop
->type
;
468 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
472 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
473 resolved_hop
->ifindex
= newhop
->ifindex
;
474 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
475 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
479 /* If the resolving route is an interface route,
480 * it means the gateway we are looking up is connected
481 * to that interface. (The actual network is _not_ onlink).
482 * Therefore, the resolved route should have the original
483 * gateway as nexthop as it is directly connected.
485 * On Linux, we have to set the onlink netlink flag because
486 * otherwise, the kernel won't accept the route. */
487 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
489 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
490 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
491 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
492 resolved_hop
->ifindex
= newhop
->ifindex
;
495 nexthop_add(&nexthop
->resolved
, resolved_hop
);
501 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
504 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
505 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
509 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
511 resolved_hop
= nexthop_new();
512 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
513 /* If the resolving route specifies a gateway, use it */
514 if (newhop
->type
== NEXTHOP_TYPE_IPV4
515 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
517 resolved_hop
->type
= newhop
->type
;
518 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
522 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
523 resolved_hop
->ifindex
= newhop
->ifindex
;
524 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
525 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
529 /* If the resolving route is an interface route,
530 * it means the gateway we are looking up is connected
531 * to that interface. (The actual network is _not_ onlink).
532 * Therefore, the resolved route should have the original
533 * gateway as nexthop as it is directly connected.
535 * On Linux, we have to set the onlink netlink flag because
536 * otherwise, the kernel won't accept the route.
538 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
540 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
541 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
542 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
543 resolved_hop
->ifindex
= newhop
->ifindex
;
546 nexthop_add(&nexthop
->resolved
, resolved_hop
);
551 rib
->nexthop_mtu
= match
->mtu
;
563 /* If force flag is not set, do not modify falgs at all for uninstall
564 the route from FIB. */
566 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
567 struct route_node
*top
)
569 struct prefix_ipv6 p
;
570 struct route_table
*table
;
571 struct route_node
*rn
;
574 struct nexthop
*newhop
, *tnewhop
;
576 struct nexthop
*resolved_hop
;
578 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
579 nexthop
->ifindex
= 0;
583 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
584 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, nexthop
->resolved
, top
);
585 nexthops_free(nexthop
->resolved
);
586 nexthop
->resolved
= NULL
;
589 /* Skip nexthops that have been filtered out due to route-map */
590 /* The nexthops are specific to this route and so the same */
591 /* nexthop for a different route may not have this flag set */
592 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
595 /* Make lookup prefix. */
596 memset (&p
, 0, sizeof (struct prefix_ipv6
));
598 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
599 p
.prefix
= nexthop
->gate
.ipv6
;
602 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, rib
->vrf_id
);
606 rn
= route_node_match (table
, (struct prefix
*) &p
);
609 route_unlock_node (rn
);
611 /* If lookup self prefix return immediately. */
615 /* Pick up selected route. */
616 /* However, do not resolve over default route unless explicitly allowed. */
617 if (is_default_prefix (&rn
->p
) &&
618 !nh_resolve_via_default (p
.family
))
621 RNODE_FOREACH_RIB (rn
, match
)
623 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
625 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
629 /* If there is no selected route or matched route is EGP, go up
635 } while (rn
&& rn
->info
== NULL
);
637 route_lock_node (rn
);
641 /* If the longest prefix match for the nexthop yields
642 * a blackhole, mark it as inactive. */
643 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
644 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
647 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
649 /* Directly point connected route. */
650 newhop
= match
->nexthop
;
652 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
653 nexthop
->ifindex
= newhop
->ifindex
;
657 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
660 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
661 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
662 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
666 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
667 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
669 resolved_hop
= nexthop_new();
670 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
671 /* See nexthop_active_ipv4 for a description how the
672 * resolved nexthop is constructed. */
673 if (newhop
->type
== NEXTHOP_TYPE_IPV6
674 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
676 resolved_hop
->type
= newhop
->type
;
677 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
681 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
682 resolved_hop
->ifindex
= newhop
->ifindex
;
686 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
688 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
689 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
690 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
691 resolved_hop
->ifindex
= newhop
->ifindex
;
694 nexthop_add(&nexthop
->resolved
, resolved_hop
);
700 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
703 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
704 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
708 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
710 resolved_hop
= nexthop_new();
711 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
712 /* See nexthop_active_ipv4 for a description how the
713 * resolved nexthop is constructed. */
714 if (newhop
->type
== NEXTHOP_TYPE_IPV6
715 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
717 resolved_hop
->type
= newhop
->type
;
718 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
722 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
723 resolved_hop
->ifindex
= newhop
->ifindex
;
727 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
729 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
730 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
731 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
732 resolved_hop
->ifindex
= newhop
->ifindex
;
735 nexthop_add(&nexthop
->resolved
, resolved_hop
);
751 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
752 union g_addr
*addr
, struct route_node
**rn_out
)
755 struct route_table
*table
;
756 struct route_node
*rn
;
758 struct nexthop
*newhop
, *tnewhop
;
762 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
766 memset (&p
, 0, sizeof (struct prefix
));
770 p
.u
.prefix4
= addr
->ipv4
;
771 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
775 p
.u
.prefix6
= addr
->ipv6
;
776 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
779 rn
= route_node_match (table
, (struct prefix
*) &p
);
783 route_unlock_node (rn
);
785 /* Pick up selected route. */
786 RNODE_FOREACH_RIB (rn
, match
)
788 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
790 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
794 /* If there is no selected route or matched route is EGP, go up
800 } while (rn
&& rn
->info
== NULL
);
802 route_lock_node (rn
);
806 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
809 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
810 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
828 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
830 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
831 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
832 union g_addr gaddr
= { .ipv4
= addr
};
834 switch (ipv4_multicast_mode
)
836 case MCAST_MRIB_ONLY
:
837 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
838 case MCAST_URIB_ONLY
:
839 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
840 case MCAST_NO_CONFIG
:
841 case MCAST_MIX_MRIB_FIRST
:
842 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
844 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
846 case MCAST_MIX_DISTANCE
:
847 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
848 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
850 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
856 case MCAST_MIX_PFXLEN
:
857 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
858 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
860 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
869 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
871 if (IS_ZEBRA_DEBUG_RIB
)
874 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
876 zlog_debug("%s: %s: found %s, using %s",
878 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
879 urib
? "URIB" : "nothing",
880 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
886 multicast_mode_ipv4_set (enum multicast_mode mode
)
888 if (IS_ZEBRA_DEBUG_RIB
)
889 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
890 ipv4_multicast_mode
= mode
;
894 multicast_mode_ipv4_get (void)
896 return ipv4_multicast_mode
;
900 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
902 struct route_table
*table
;
903 struct route_node
*rn
;
905 struct nexthop
*nexthop
, *tnexthop
;
909 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
913 rn
= route_node_lookup (table
, (struct prefix
*) p
);
915 /* No route for this prefix. */
920 route_unlock_node (rn
);
922 RNODE_FOREACH_RIB (rn
, match
)
924 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
926 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
933 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
936 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
937 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
944 * This clone function, unlike its original rib_lookup_ipv4(), checks
945 * if specified IPv4 route record (prefix/mask -> gate) exists in
946 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
950 * 0: exact match found
951 * 1: a match was found with a different gate
952 * 2: connected route found
953 * 3: no matches found
956 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
959 struct route_table
*table
;
960 struct route_node
*rn
;
962 struct nexthop
*nexthop
, *tnexthop
;
967 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
969 return ZEBRA_RIB_LOOKUP_ERROR
;
971 /* Scan the RIB table for exactly matching RIB entry. */
972 rn
= route_node_lookup (table
, (struct prefix
*) p
);
974 /* No route for this prefix. */
976 return ZEBRA_RIB_NOTFOUND
;
979 route_unlock_node (rn
);
981 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
982 RNODE_FOREACH_RIB (rn
, match
)
984 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
986 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
990 /* None such found :( */
992 return ZEBRA_RIB_NOTFOUND
;
994 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
995 return ZEBRA_RIB_FOUND_CONNECTED
;
997 /* Ok, we have a cood candidate, let's check it's nexthop list... */
999 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
1000 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1002 nexthops_active
= 1;
1003 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
1004 return ZEBRA_RIB_FOUND_EXACT
;
1005 if (IS_ZEBRA_DEBUG_RIB
)
1007 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
1008 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
1009 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
1010 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
1011 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
1015 if (nexthops_active
)
1016 return ZEBRA_RIB_FOUND_NOGATE
;
1018 return ZEBRA_RIB_NOTFOUND
;
1021 #define RIB_SYSTEM_ROUTE(R) \
1022 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1024 /* This function verifies reachability of one given nexthop, which can be
1025 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1026 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1027 * nexthop->ifindex will be updated appropriately as well.
1028 * An existing route map can turn (otherwise active) nexthop into inactive, but
1031 * The return value is the final value of 'ACTIVE' flag.
1035 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
1036 struct nexthop
*nexthop
, int set
)
1038 rib_table_info_t
*info
= rn
->table
->info
;
1039 struct interface
*ifp
;
1040 route_map_result_t ret
= RMAP_MATCH
;
1042 char buf
[INET6_ADDRSTRLEN
+1];
1044 if (rn
->p
.family
== AF_INET
)
1046 else if (rn
->p
.family
== AF_INET6
)
1050 switch (nexthop
->type
)
1052 case NEXTHOP_TYPE_IFINDEX
:
1053 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1054 if (ifp
&& if_is_operative(ifp
))
1055 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1057 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1059 case NEXTHOP_TYPE_IPV4
:
1060 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1062 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
1063 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1065 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1067 case NEXTHOP_TYPE_IPV6
:
1069 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1070 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1072 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1074 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1075 /* RFC 5549, v4 prefix with v6 NH */
1076 if (rn
->p
.family
!= AF_INET
)
1078 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1080 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1081 if (ifp
&& if_is_operative(ifp
))
1082 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1084 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1088 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1089 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1091 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1094 case NEXTHOP_TYPE_BLACKHOLE
:
1095 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1100 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1103 /* XXX: What exactly do those checks do? Do we support
1104 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1105 if (RIB_SYSTEM_ROUTE(rib
) ||
1106 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1107 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1108 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1110 /* The original code didn't determine the family correctly
1111 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1112 * from the rib_table_info in those cases.
1113 * Possibly it may be better to use only the rib_table_info
1119 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1121 /* It'll get set if required inside */
1122 ret
= zebra_route_map_check(family
, rib
->type
, &rn
->p
, nexthop
, rib
->vrf_id
,
1124 if (ret
== RMAP_DENYMATCH
)
1126 if (IS_ZEBRA_DEBUG_RIB
)
1128 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1129 zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map",
1130 rib
->vrf_id
, buf
, rn
->p
.prefixlen
,
1131 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1133 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1135 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1138 /* Iterate over all nexthops of the given RIB entry and refresh their
1139 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1140 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1141 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1142 * transparently passed to nexthop_active_check().
1144 * Return value is the new number of active nexthops.
1148 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1150 struct nexthop
*nexthop
;
1151 union g_addr prev_src
;
1152 unsigned int prev_active
, new_active
, old_num_nh
;
1153 ifindex_t prev_index
;
1154 old_num_nh
= rib
->nexthop_active_num
;
1156 rib
->nexthop_active_num
= 0;
1157 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1159 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1161 /* No protocol daemon provides src and so we're skipping tracking it */
1162 prev_src
= nexthop
->rmap_src
;
1163 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1164 prev_index
= nexthop
->ifindex
;
1165 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1166 rib
->nexthop_active_num
++;
1167 /* Don't allow src setting on IPv6 addr for now */
1168 if (prev_active
!= new_active
||
1169 prev_index
!= nexthop
->ifindex
||
1170 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1171 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1172 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1173 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1174 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1175 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1177 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1178 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1182 if (old_num_nh
!= rib
->nexthop_active_num
)
1183 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1185 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1187 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1190 return rib
->nexthop_active_num
;
1195 /* Update flag indicates whether this is a "replace" or not. Currently, this
1196 * is only used for IPv4.
1199 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, int update
)
1202 struct nexthop
*nexthop
, *tnexthop
;
1203 rib_table_info_t
*info
= rn
->table
->info
;
1206 if (info
->safi
!= SAFI_UNICAST
)
1208 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1209 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1214 * Make sure we update the FPM any time we send new information to
1217 zfpm_trigger_update (rn
, "installing in kernel");
1218 switch (PREFIX_FAMILY (&rn
->p
))
1222 ret
= kernel_update_ipv4 (&rn
->p
, rib
);
1224 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
1228 ret
= kernel_update_ipv6 (&rn
->p
, rib
);
1230 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
1234 /* If install succeeds, update FIB flag for nexthops. */
1237 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1239 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1242 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1243 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1245 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1252 /* Uninstall the route from kernel. */
1254 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1257 struct nexthop
*nexthop
, *tnexthop
;
1258 rib_table_info_t
*info
= rn
->table
->info
;
1261 if (info
->safi
!= SAFI_UNICAST
)
1263 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1264 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1269 * Make sure we update the FPM any time we send new information to
1272 zfpm_trigger_update (rn
, "uninstalling from kernel");
1274 switch (PREFIX_FAMILY (&rn
->p
))
1277 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
1280 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
1284 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1285 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1290 /* Uninstall the route from kernel. */
1292 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1294 rib_table_info_t
*info
= rn
->table
->info
;
1296 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1298 if (info
->safi
== SAFI_UNICAST
)
1299 zfpm_trigger_update (rn
, "rib_uninstall");
1301 redistribute_delete (&rn
->p
, rib
);
1302 if (! RIB_SYSTEM_ROUTE (rib
))
1303 rib_uninstall_kernel (rn
, rib
);
1304 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1308 static void rib_unlink (struct route_node
*, struct rib
*);
1311 * rib_can_delete_dest
1313 * Returns TRUE if the given dest can be deleted from the table.
1316 rib_can_delete_dest (rib_dest_t
*dest
)
1324 * Don't delete the dest if we have to update the FPM about this
1327 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1328 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1337 * Garbage collect the rib dest corresponding to the given route node
1340 * Returns TRUE if the dest was deleted, FALSE otherwise.
1343 rib_gc_dest (struct route_node
*rn
)
1346 struct zebra_vrf
*zvrf
;
1348 dest
= rib_dest_from_rnode (rn
);
1352 if (!rib_can_delete_dest (dest
))
1355 zvrf
= rib_dest_vrf (dest
);
1356 if (IS_ZEBRA_DEBUG_RIB
)
1357 rnode_debug (rn
, zvrf
->vrf_id
, "removing dest from table");
1360 XFREE (MTYPE_RIB_DEST
, dest
);
1364 * Release the one reference that we keep on the route node.
1366 route_unlock_node (rn
);
1371 rib_process_add_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1374 char buf
[INET6_ADDRSTRLEN
];
1377 zfpm_trigger_update (rn
, "new route selected");
1379 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1380 if (!nexthop_active_update (rn
, select
, 1))
1382 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1386 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1387 if (IS_ZEBRA_DEBUG_RIB
)
1389 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1390 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1391 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1394 if (!RIB_SYSTEM_ROUTE (select
))
1396 if (rib_install_kernel (rn
, select
, 0))
1399 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1400 zlog_warn ("%u:%s/%d: Route install failed",
1401 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1405 /* Update for redistribution. */
1407 redistribute_update (&rn
->p
, select
, NULL
);
1408 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1412 rib_process_del_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1415 char buf
[INET6_ADDRSTRLEN
];
1417 zfpm_trigger_update (rn
, "removing existing route");
1419 /* Withdraw redistribute and uninstall from kernel. */
1420 if (IS_ZEBRA_DEBUG_RIB
)
1422 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1423 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1424 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1427 redistribute_delete(&rn
->p
, fib
);
1428 if (!RIB_SYSTEM_ROUTE (fib
))
1429 rib_uninstall_kernel (rn
, fib
);
1431 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1433 /* Update nexthop for route, reset changed flag. */
1434 nexthop_active_update (rn
, fib
, 1);
1435 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1439 rib_process_update_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1440 struct rib
*select
, struct rib
*fib
)
1442 char buf
[INET6_ADDRSTRLEN
];
1443 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1448 if (IS_ZEBRA_DEBUG_RIB
)
1449 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1452 * We have to install or update if a new route has been selected or
1453 * something has changed.
1455 if (select
!= fib
||
1456 CHECK_FLAG (select
->status
, RIB_ENTRY_CHANGED
))
1458 zfpm_trigger_update (rn
, "updating existing route");
1460 /* Update the nexthop; we could determine here that nexthop is inactive. */
1461 if (nexthop_active_update (rn
, select
, 1))
1464 /* If nexthop is active, install the selected route, if appropriate. If
1465 * the install succeeds, cleanup flags for prior route, if different from
1470 if (IS_ZEBRA_DEBUG_RIB
)
1473 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1474 "old %p (type %d)", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1475 rn
, select
, select
->type
, fib
, fib
->type
);
1477 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1478 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1480 /* Non-system route should be installed. */
1481 if (!RIB_SYSTEM_ROUTE (select
))
1483 if (rib_install_kernel (rn
, select
, 1))
1486 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1487 zlog_warn ("%u:%s/%d: Route install failed",
1488 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1492 /* If install succeeded or system route, cleanup flags for prior route. */
1493 if (installed
&& select
!= fib
)
1495 if (RIB_SYSTEM_ROUTE(select
))
1497 if (!RIB_SYSTEM_ROUTE (fib
))
1498 rib_uninstall_kernel (rn
, fib
);
1502 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1503 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1507 /* Update for redistribution. */
1510 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1511 redistribute_update (&rn
->p
, select
, (select
== fib
) ? NULL
: fib
);
1516 * If nexthop for selected route is not active or install failed, we
1517 * may need to uninstall and delete for redistribution.
1519 if (!nh_active
|| !installed
)
1523 if (IS_ZEBRA_DEBUG_RIB
)
1526 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1527 "old %p (type %d) - %s", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1528 rn
, select
, select
->type
, fib
, fib
->type
,
1529 nh_active
? "install failed" : "nexthop inactive");
1531 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) - %s",
1532 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
,
1533 nh_active
? "install failed" : "nexthop inactive");
1536 del
= (select
== fib
) ? select
: fib
;
1538 redistribute_delete(&rn
->p
, del
);
1540 if (!RIB_SYSTEM_ROUTE (del
))
1541 rib_uninstall_kernel (rn
, del
);
1542 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1548 * Same route selected; check if in the FIB and if not, re-install. This
1549 * is housekeeping code to deal with race conditions in kernel with linux
1550 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1553 if (!RIB_SYSTEM_ROUTE (select
))
1557 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1558 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1564 rib_install_kernel (rn
, select
, 0);
1568 /* Update prior route. */
1571 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1573 /* Set real nexthop. */
1574 nexthop_active_update (rn
, fib
, 1);
1575 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1578 /* Clear changed flag. */
1579 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1582 /* Check if 'alternate' RIB entry is better than 'current'. */
1584 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1586 if (current
== NULL
)
1589 /* filter route selection in following order:
1590 * - connected beats other types
1591 * - lower distance beats higher
1592 * - lower metric beats higher for equal distance
1593 * - last, hence oldest, route wins tie break.
1596 /* Connected routes. Pick the last connected
1597 * route of the set of lowest metric connected routes.
1599 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1601 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1602 || alternate
->metric
<= current
->metric
)
1608 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1611 /* higher distance loses */
1612 if (alternate
->distance
< current
->distance
)
1614 if (current
->distance
< alternate
->distance
)
1617 /* metric tie-breaks equal distance */
1618 if (alternate
->metric
<= current
->metric
)
1624 /* Core function for processing routing information base. */
1626 rib_process (struct route_node
*rn
)
1630 struct rib
*fib
= NULL
;
1631 struct rib
*select
= NULL
;
1632 struct rib
*del
= NULL
;
1633 struct rib
*best
= NULL
;
1634 char buf
[INET6_ADDRSTRLEN
];
1636 struct zebra_vrf
*zvrf
= NULL
;
1637 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1638 rib_table_info_t
*info
;
1641 info
= rn
->table
->info
;
1643 dest
= rib_dest_from_rnode (rn
);
1646 zvrf
= rib_dest_vrf (dest
);
1647 vrf_id
= zvrf
->vrf_id
;
1650 if (IS_ZEBRA_DEBUG_RIB
)
1651 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1653 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1654 zlog_debug ("%u:%s/%d: Processing rn %p", vrf_id
, buf
, rn
->p
.prefixlen
, rn
);
1656 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1658 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1659 zlog_debug ("%u:%s/%d: Examine rib %p (type %d) status %x flags %x "
1660 "dist %d metric %d",
1661 vrf_id
, buf
, rn
->p
.prefixlen
, rib
, rib
->type
, rib
->status
,
1662 rib
->flags
, rib
->distance
, rib
->metric
);
1664 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1666 /* Currently installed rib. */
1667 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1669 assert (fib
== NULL
);
1673 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1674 * which we need to do do further work with below.
1676 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1680 if (IS_ZEBRA_DEBUG_RIB
)
1681 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1682 (void *)rn
, (void *)rib
);
1683 rib_unlink (rn
, rib
);
1691 /* Skip unreachable nexthop. */
1692 /* This first call to nexthop_active_update is merely to determine if
1693 * there's any change to nexthops associated with this RIB entry. Now,
1694 * rib_process() can be invoked due to an external event such as link
1695 * down or due to next-hop-tracking evaluation. In the latter case,
1696 * a decision has already been made that the NHs have changed. So, no
1697 * need to invoke a potentially expensive call again. Further, since
1698 * the change might be in a recursive NH which is not caught in
1699 * the nexthop_active_update() code. Thus, we might miss changes to
1702 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1703 ! nexthop_active_update (rn
, rib
, 0))
1705 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1707 /* This entry was denied by the 'ip protocol table' route-map, we
1708 * need to delete it */
1711 if (IS_ZEBRA_DEBUG_RIB
)
1712 zlog_debug ("%s: %s/%d: imported via import-table but denied "
1713 "by the ip protocol table route-map",
1714 __func__
, buf
, rn
->p
.prefixlen
);
1715 rib_unlink (rn
, rib
);
1724 if (info
->safi
== SAFI_MULTICAST
)
1727 /* Infinite distance. */
1728 if (rib
->distance
== DISTANCE_INFINITY
)
1730 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1734 best
= rib_choose_best(select
, rib
);
1735 if (select
&& best
!= select
)
1736 UNSET_FLAG (select
->status
, RIB_ENTRY_CHANGED
);
1738 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1740 } /* RNODE_FOREACH_RIB_SAFE */
1742 /* After the cycle is finished, the following pointers will be set:
1743 * select --- the winner RIB entry, if any was found, otherwise NULL
1744 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1745 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1748 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1749 zlog_debug ("%u:%s/%d: After processing: select %p fib %p del %p",
1750 vrf_id
, buf
, rn
->p
.prefixlen
, select
, fib
, del
);
1752 /* Same RIB entry is selected. Update FIB and finish. */
1753 if (select
&& select
== fib
)
1754 rib_process_update_route (zvrf
, rn
, select
, select
);
1755 else if (select
&& fib
)
1756 rib_process_update_route (zvrf
, rn
, select
, fib
);
1758 rib_process_add_route (zvrf
, rn
, select
);
1760 rib_process_del_route (zvrf
, rn
, fib
);
1763 if (select
&& select
== fib
)
1765 if (IS_ZEBRA_DEBUG_RIB
)
1766 rnode_debug (rn
, vrf_id
, "Updating existing route, select %p, fib %p",
1767 (void *)select
, (void *)fib
);
1768 if (CHECK_FLAG (select
->status
, RIB_ENTRY_CHANGED
))
1770 if (info
->safi
== SAFI_UNICAST
)
1771 zfpm_trigger_update (rn
, "updating existing route");
1773 /* Set real nexthop. */
1774 /* Need to check if any NHs are active to clear the
1777 if (nexthop_active_update (rn
, select
, 1))
1779 if (IS_ZEBRA_DEBUG_RIB
)
1780 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1781 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1782 if (! RIB_SYSTEM_ROUTE (select
))
1784 /* Clear FIB flag if performing a replace, will get set again
1785 * as part of install.
1787 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1788 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1789 rib_install_kernel (rn
, select
, 1);
1792 /* assuming that the receiver knows how to dedup */
1793 redistribute_update (&rn
->p
, select
, NULL
);
1797 if (IS_ZEBRA_DEBUG_RIB
)
1798 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1799 "- nexthop inactive",
1800 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1802 /* Withdraw unreachable redistribute route */
1803 redistribute_delete(&rn
->p
, select
);
1805 /* Do the uninstall here, if not done earlier. */
1806 if (! RIB_SYSTEM_ROUTE (select
))
1807 rib_uninstall_kernel (rn
, select
);
1808 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1810 UNSET_FLAG (select
->status
, RIB_ENTRY_CHANGED
);
1812 else if (! RIB_SYSTEM_ROUTE (select
))
1814 /* Housekeeping code to deal with
1815 race conditions in kernel with linux
1816 netlink reporting interface up before IPv4 or IPv6 protocol
1817 is ready to add routes.
1818 This makes sure the routes are IN the kernel.
1821 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1822 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1828 rib_install_kernel (rn
, select
, 0);
1833 /* At this point we either haven't found the best RIB entry or it is
1834 * different from what we currently intend to flag with SELECTED. In both
1835 * cases, if a RIB block is present in FIB, it should be withdrawn.
1839 if (IS_ZEBRA_DEBUG_RIB
)
1840 rnode_debug (rn
, vrf_id
, "Removing existing route, fib %p", (void *)fib
);
1842 if (info
->safi
== SAFI_UNICAST
)
1843 zfpm_trigger_update (rn
, "removing existing route");
1845 /* If there's no route to replace this with, withdraw redistribute and
1846 * uninstall from kernel.
1850 if (IS_ZEBRA_DEBUG_RIB
)
1851 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1852 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1854 redistribute_delete(&rn
->p
, fib
);
1855 if (! RIB_SYSTEM_ROUTE (fib
))
1856 rib_uninstall_kernel (rn
, fib
);
1859 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1861 /* Set real nexthop. */
1862 nexthop_active_update (rn
, fib
, 1);
1863 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1866 /* Regardless of some RIB entry being SELECTED or not before, now we can
1867 * tell, that if a new winner exists, FIB is still not updated with this
1868 * data, but ready to be.
1872 if (IS_ZEBRA_DEBUG_RIB
)
1873 rnode_debug (rn
, "Adding route, select %p", (void *)select
);
1875 if (info
->safi
== SAFI_UNICAST
)
1876 zfpm_trigger_update (rn
, "new route selected");
1878 /* Set real nexthop. */
1879 if (nexthop_active_update (rn
, select
, 1))
1881 if (IS_ZEBRA_DEBUG_RIB
)
1884 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1885 "old %p (type %d)", vrf_id
, buf
, rn
->p
.prefixlen
, rn
,
1886 select
, select
->type
, fib
, fib
->type
);
1888 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1889 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1892 if (! RIB_SYSTEM_ROUTE (select
))
1894 /* Clear FIB flag if performing a replace, will get set again
1895 * as part of install.
1899 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1900 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1902 rib_install_kernel (rn
, select
, fib
? 1 : 0);
1906 /* Uninstall prior route here, if needed. */
1907 if (fib
&& !RIB_SYSTEM_ROUTE (fib
))
1908 rib_uninstall_kernel (rn
, fib
);
1911 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1912 /* Unconditionally announce, this part is exercised by new routes */
1913 /* If we cannot add, for example route added is learnt by the */
1914 /* protocol we're trying to redistribute to, delete the redist */
1915 /* This is notified by setting the is_update to 1 */
1916 redistribute_update (&rn
->p
, select
, fib
);
1920 /* Uninstall prior route here and do redist delete, if needed. */
1923 if (IS_ZEBRA_DEBUG_RIB
)
1924 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1925 "- nexthop inactive",
1926 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1928 if (!RIB_SYSTEM_ROUTE (fib
))
1929 rib_uninstall_kernel (rn
, fib
);
1930 redistribute_delete(&rn
->p
, fib
);
1933 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1937 /* FIB route was removed, should be deleted */
1940 if (IS_ZEBRA_DEBUG_RIB
)
1941 rnode_debug (rn
, vrf_id
, "Deleting fib %p, rn %p", (void *)del
, (void *)rn
);
1942 rib_unlink (rn
, del
);
1946 * Check if the dest can be deleted now.
1951 /* Take a list of route_node structs and return 1, if there was a record
1952 * picked from it and processed by rib_process(). Don't process more,
1953 * than one RN record; operate only in the specified sub-queue.
1956 process_subq (struct list
* subq
, u_char qindex
)
1958 struct listnode
*lnode
= listhead (subq
);
1959 struct route_node
*rnode
;
1960 char buf
[INET6_ADDRSTRLEN
];
1962 struct zebra_vrf
*zvrf
= NULL
;
1967 rnode
= listgetdata (lnode
);
1968 dest
= rib_dest_from_rnode (rnode
);
1970 zvrf
= rib_dest_vrf (dest
);
1972 rib_process (rnode
);
1974 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1976 inet_ntop (rnode
->p
.family
, &rnode
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1977 zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u",
1978 zvrf
? zvrf
->vrf_id
: 0, buf
, rnode
->p
.prefixlen
, rnode
, qindex
);
1982 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1987 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1988 __func__
, rnode
, rnode
->lock
);
1989 zlog_backtrace(LOG_DEBUG
);
1992 route_unlock_node (rnode
);
1993 list_delete_node (subq
, lnode
);
1998 * All meta queues have been processed. Trigger next-hop evaluation.
2001 meta_queue_process_complete (struct work_queue
*dummy
)
2004 struct zebra_vrf
*zvrf
;
2006 /* Evaluate nexthops for those VRFs which underwent route processing. This
2007 * should limit the evaluation to the necessary VRFs in most common
2010 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
2012 if (((zvrf
= vrf_iter2info (iter
)) != NULL
) &&
2013 (zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2015 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2016 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2017 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2018 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2019 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2024 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2025 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
2026 * is pointed to the meta queue structure.
2028 static wq_item_status
2029 meta_queue_process (struct work_queue
*dummy
, void *data
)
2031 struct meta_queue
* mq
= data
;
2034 for (i
= 0; i
< MQ_SIZE
; i
++)
2035 if (process_subq (mq
->subq
[i
], i
))
2040 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2044 * Map from rib types to queue type (priority) in meta queue
2046 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
2047 [ZEBRA_ROUTE_SYSTEM
] = 4,
2048 [ZEBRA_ROUTE_KERNEL
] = 0,
2049 [ZEBRA_ROUTE_CONNECT
] = 0,
2050 [ZEBRA_ROUTE_STATIC
] = 1,
2051 [ZEBRA_ROUTE_RIP
] = 2,
2052 [ZEBRA_ROUTE_RIPNG
] = 2,
2053 [ZEBRA_ROUTE_OSPF
] = 2,
2054 [ZEBRA_ROUTE_OSPF6
] = 2,
2055 [ZEBRA_ROUTE_ISIS
] = 2,
2056 [ZEBRA_ROUTE_BGP
] = 3,
2057 [ZEBRA_ROUTE_HSLS
] = 4,
2058 [ZEBRA_ROUTE_TABLE
] = 1,
2061 /* Look into the RN and queue it into one or more priority queues,
2062 * increasing the size for each data push done.
2065 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
2069 RNODE_FOREACH_RIB (rn
, rib
)
2071 u_char qindex
= meta_queue_map
[rib
->type
];
2072 struct zebra_vrf
*zvrf
;
2074 /* Invariant: at this point we always have rn->info set. */
2075 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
2076 RIB_ROUTE_QUEUED (qindex
)))
2078 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2079 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
2080 (void *)rn
, qindex
);
2084 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2085 listnode_add (mq
->subq
[qindex
], rn
);
2086 route_lock_node (rn
);
2089 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2090 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
2091 (void *)rn
, qindex
);
2093 zvrf
= zebra_vrf_lookup (rib
->vrf_id
);
2095 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2099 /* Add route_node to work queue and schedule processing */
2101 rib_queue_add (struct route_node
*rn
)
2105 /* Pointless to queue a route_node with no RIB entries to add or remove */
2106 if (!rnode_to_ribs (rn
))
2108 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2109 __func__
, (void *)rn
, rn
->lock
);
2110 zlog_backtrace(LOG_DEBUG
);
2114 if (zebrad
.ribq
== NULL
)
2116 zlog_err ("%s: work_queue does not exist!", __func__
);
2121 * The RIB queue should normally be either empty or holding the only
2122 * work_queue_item element. In the latter case this element would
2123 * hold a pointer to the meta queue structure, which must be used to
2124 * actually queue the route nodes to process. So create the MQ
2125 * holder, if necessary, then push the work into it in any case.
2126 * This semantics was introduced after 0.99.9 release.
2128 if (!zebrad
.ribq
->items
->count
)
2129 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
2131 rib_meta_queue_add (zebrad
.mq
, rn
);
2136 /* Create new meta queue.
2137 A destructor function doesn't seem to be necessary here.
2139 static struct meta_queue
*
2140 meta_queue_new (void)
2142 struct meta_queue
*new;
2145 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
2148 for (i
= 0; i
< MQ_SIZE
; i
++)
2150 new->subq
[i
] = list_new ();
2151 assert(new->subq
[i
]);
2157 /* initialise zebra rib work queue */
2159 rib_queue_init (struct zebra_t
*zebra
)
2163 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
2164 "route_node processing")))
2166 zlog_err ("%s: could not initialise work queue!", __func__
);
2170 /* fill in the work queue spec */
2171 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2172 zebra
->ribq
->spec
.errorfunc
= NULL
;
2173 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2174 /* XXX: TODO: These should be runtime configurable via vty */
2175 zebra
->ribq
->spec
.max_retries
= 3;
2176 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
2178 if (!(zebra
->mq
= meta_queue_new ()))
2180 zlog_err ("%s: could not initialise meta queue!", __func__
);
2186 /* RIB updates are processed via a queue of pointers to route_nodes.
2188 * The queue length is bounded by the maximal size of the routing table,
2189 * as a route_node will not be requeued, if already queued.
2191 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2192 * state, or static_install_route (when an existing RIB is updated)
2193 * and then submit route_node to queue for best-path selection later.
2194 * Order of add/delete state changes are preserved for any given RIB.
2196 * Deleted RIBs are reaped during best-path selection.
2199 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2200 * |-------->| | best RIB, if required
2202 * static_install->|->rib_addqueue...... -> rib_process
2204 * |-------->| |-> rib_unlink
2205 * |-> set RIB_ENTRY_REMOVE |
2206 * rib_delnode (RIB freed)
2208 * The 'info' pointer of a route_node points to a rib_dest_t
2209 * ('dest'). Queueing state for a route_node is kept on the dest. The
2210 * dest is created on-demand by rib_link() and is kept around at least
2211 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2213 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2215 * - route_nodes: refcounted by:
2216 * - dest attached to route_node:
2217 * - managed by: rib_link/rib_gc_dest
2218 * - route_node processing queue
2219 * - managed by: rib_addqueue, rib_process.
2223 /* Add RIB to head of the route node. */
2225 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2230 const char *rmap_name
;
2234 dest
= rib_dest_from_rnode (rn
);
2237 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2238 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2240 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2241 route_lock_node (rn
); /* rn route table reference */
2246 head
= dest
->routes
;
2254 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2255 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2256 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2258 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2259 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2267 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2269 /* RIB node has been un-removed before route-node is processed.
2270 * route_node must hence already be on the queue for processing..
2272 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2274 if (IS_ZEBRA_DEBUG_RIB
)
2275 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2277 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2280 rib_link (rn
, rib
, process
);
2286 * Detach a rib structure from a route_node.
2288 * Note that a call to rib_unlink() should be followed by a call to
2289 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2290 * longer required to be deleted.
2293 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2299 if (IS_ZEBRA_DEBUG_RIB
)
2300 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2302 dest
= rib_dest_from_rnode (rn
);
2305 rib
->next
->prev
= rib
->prev
;
2308 rib
->prev
->next
= rib
->next
;
2311 dest
->routes
= rib
->next
;
2314 /* free RIB and nexthops */
2315 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2316 nexthops_free(rib
->nexthop
);
2317 XFREE (MTYPE_RIB
, rib
);
2322 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2326 if (IS_ZEBRA_DEBUG_RIB
)
2327 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2328 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2330 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2331 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2332 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2334 zebra_del_import_table_entry(rn
, rib
);
2335 /* Just clean up if non main table */
2336 if (IS_ZEBRA_DEBUG_RIB
)
2338 char buf
[INET6_ADDRSTRLEN
];
2339 if (IS_ZEBRA_DEBUG_RIB
)
2341 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2342 zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)",
2343 rib
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, rib
, rib
->type
);
2347 rib_unlink(rn
, rib
);
2355 /* This function dumps the contents of a given RIB entry into
2356 * standard debug log. Calling function name and IP prefix in
2357 * question are passed as 1st and 2nd arguments.
2360 void _rib_dump (const char * func
,
2361 union prefix46constptr pp
, const struct rib
* rib
)
2363 const struct prefix
*p
= pp
.p
;
2364 char straddr
[PREFIX_STRLEN
];
2365 struct nexthop
*nexthop
, *tnexthop
;
2368 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func
, (const void *)rib
,
2369 prefix2str(pp
, straddr
, sizeof(straddr
)), rib
->vrf_id
);
2372 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2375 (unsigned long) rib
->uptime
,
2382 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2392 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2395 rib
->nexthop_active_num
,
2396 rib
->nexthop_fib_num
2399 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2401 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2404 "%s: %s %s with flags %s%s%s",
2406 (recursing
? " NH" : "NH"),
2408 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2409 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2410 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2413 zlog_debug ("%s: dump complete", func
);
2416 /* This is an exported helper to rtm_read() to dump the strange
2417 * RIB entry found by rib_lookup_ipv4_route()
2420 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2422 struct route_table
*table
;
2423 struct route_node
*rn
;
2425 char prefix_buf
[INET_ADDRSTRLEN
];
2428 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2431 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2435 /* Scan the RIB table for exactly matching RIB entry. */
2436 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2438 /* No route for this prefix. */
2441 zlog_debug ("%s: lookup failed for %s", __func__
,
2442 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2447 route_unlock_node (rn
);
2450 RNODE_FOREACH_RIB (rn
, rib
)
2454 "%s: rn %p, rib %p: %s, %s",
2458 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2459 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2465 /* Check if requested address assignment will fail due to another
2466 * route being installed by zebra in FIB already. Take necessary
2467 * actions, if needed: remove such a route from FIB and deSELECT
2468 * corresponding RIB entry. Then put affected RN into RIBQ head.
2470 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2472 struct route_table
*table
;
2473 struct route_node
*rn
;
2475 unsigned changed
= 0;
2477 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2479 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2483 /* No matches would be the simplest case. */
2484 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2488 route_unlock_node (rn
);
2490 /* Check all RIB entries. In case any changes have to be done, requeue
2491 * the RN into RIBQ head. If the routing message about the new connected
2492 * route (generated by the IP address we are going to assign very soon)
2493 * comes before the RIBQ is processed, the new RIB entry will join
2494 * RIBQ record already on head. This is necessary for proper revalidation
2495 * of the rest of the RIB.
2497 RNODE_FOREACH_RIB (rn
, rib
)
2499 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
2500 ! RIB_SYSTEM_ROUTE (rib
))
2503 if (IS_ZEBRA_DEBUG_RIB
)
2505 char buf
[PREFIX_STRLEN
];
2506 zlog_debug ("%u:%s: freeing way for connected prefix",
2507 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2508 rib_dump (&rn
->p
, rib
);
2510 rib_uninstall (rn
, rib
);
2518 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2521 struct route_table
*table
;
2522 struct route_node
*rn
;
2524 struct nexthop
*nexthop
;
2531 if (p
->family
== AF_INET
)
2537 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2541 /* Make it sure prefixlen is applied to the prefix. */
2544 /* Set default distance by route type. */
2545 if (rib
->distance
== 0)
2547 rib
->distance
= route_info
[rib
->type
].distance
;
2549 /* iBGP distance is 200. */
2550 if (rib
->type
== ZEBRA_ROUTE_BGP
2551 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2552 rib
->distance
= 200;
2555 /* Lookup route node.*/
2556 rn
= route_node_get (table
, p
);
2558 /* If same type of route are installed, treat it as a implicit
2560 RNODE_FOREACH_RIB (rn
, same
)
2562 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2565 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2566 && same
->table
== rib
->table
2567 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2571 /* If this route is kernel route, set FIB flag to the route. */
2572 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2573 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2574 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2576 /* Link new rib to node.*/
2577 if (IS_ZEBRA_DEBUG_RIB
)
2579 char buf
[INET6_ADDRSTRLEN
];
2580 if (IS_ZEBRA_DEBUG_RIB
)
2582 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2583 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2585 rib
->vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2586 (void *)rib
, rib
->type
, (void *)same
);
2589 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2590 rib_dump ((struct prefix
*)p
, rib
);
2592 rib_addnode (rn
, rib
, 1);
2595 /* Free implicit route.*/
2598 rib_delnode (rn
, same
);
2602 route_unlock_node (rn
);
2607 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2608 int flags
, struct prefix
*p
, union g_addr
*gate
, ifindex_t ifindex
,
2611 struct route_table
*table
;
2612 struct route_node
*rn
;
2614 struct rib
*fib
= NULL
;
2615 struct rib
*same
= NULL
;
2616 struct nexthop
*nexthop
, *tnexthop
;
2618 char buf1
[PREFIX_STRLEN
];
2619 char buf2
[INET6_ADDRSTRLEN
];
2622 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2629 /* Lookup route node. */
2630 rn
= route_node_lookup (table
, p
);
2633 if (IS_ZEBRA_DEBUG_RIB
)
2634 zlog_debug ("%u:%s: doesn't exist in rib",
2635 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)));
2636 return ZEBRA_ERR_RTNOEXIST
;
2639 /* Lookup same type route. */
2640 RNODE_FOREACH_RIB (rn
, rib
)
2642 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2645 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2648 if (rib
->type
!= type
)
2650 if (rib
->instance
!= instance
)
2652 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2653 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2655 if (nexthop
->ifindex
!= ifindex
)
2660 route_unlock_node (rn
);
2661 route_unlock_node (rn
);
2667 /* Make sure that the route found has the same gateway. */
2675 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2676 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2677 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2686 /* If same type of route can't be found and this message is from
2690 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2691 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2693 if (IS_ZEBRA_DEBUG_RIB
)
2695 zlog_debug ("%u:%s: rn %p, rib %p (type %d) was deleted "
2696 "from kernel, adding",
2697 vrf_id
, prefix2str(p
, buf1
, INET6_ADDRSTRLEN
),
2698 rn
, fib
, fib
->type
);
2703 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2704 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2706 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2710 /* This means someone else, other than Zebra, has deleted
2711 * a Zebra router from the kernel. We will add it back */
2712 rib_install_kernel(rn
, fib
, 0);
2717 if (IS_ZEBRA_DEBUG_RIB
)
2720 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2721 "doesn't exist in rib",
2722 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2723 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
),
2727 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2728 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2732 route_unlock_node (rn
);
2733 return ZEBRA_ERR_RTNOEXIST
;
2738 rib_delnode (rn
, same
);
2740 route_unlock_node (rn
);
2747 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2748 u_short instance
, int flags
, struct prefix
*p
,
2749 union g_addr
*gate
, union g_addr
*src
, ifindex_t ifindex
,
2750 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2754 struct rib
*same
= NULL
;
2755 struct route_table
*table
;
2756 struct route_node
*rn
;
2757 struct nexthop
*nexthop
;
2760 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2764 /* Make sure mask is applied. */
2767 /* Set default distance by route type. */
2770 if ((unsigned)type
>= array_size(route_info
))
2773 distance
= route_info
[type
].distance
;
2775 /* iBGP distance is 200. */
2776 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2780 /* Lookup route node.*/
2781 rn
= route_node_get (table
, p
);
2783 /* If same type of route are installed, treat it as a implicit
2785 RNODE_FOREACH_RIB (rn
, rib
)
2787 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2790 if (rib
->type
!= type
)
2792 if (rib
->instance
!= instance
)
2794 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2799 /* Duplicate connected route comes in. */
2800 else if ((nexthop
= rib
->nexthop
) &&
2801 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2802 nexthop
->ifindex
== ifindex
&&
2803 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2810 /* Allocate new rib structure. */
2811 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2814 rib
->instance
= instance
;
2815 rib
->distance
= distance
;
2817 rib
->metric
= metric
;
2819 rib
->table
= table_id
;
2820 rib
->vrf_id
= vrf_id
;
2821 rib
->nexthop_num
= 0;
2822 rib
->uptime
= time (NULL
);
2824 /* Nexthop settings. */
2830 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2832 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2837 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2839 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2843 rib_nexthop_ifindex_add (rib
, ifindex
);
2845 /* If this route is kernel route, set FIB flag to the route. */
2846 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2847 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2848 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2850 /* Link new rib to node.*/
2851 if (IS_ZEBRA_DEBUG_RIB
)
2853 char buf
[INET6_ADDRSTRLEN
];
2854 if (IS_ZEBRA_DEBUG_RIB
)
2856 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2857 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2859 vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2860 (void *)rib
, rib
->type
, (void *)same
);
2863 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2866 rib_addnode (rn
, rib
, 1);
2868 /* Free implicit route.*/
2870 rib_delnode (rn
, same
);
2872 route_unlock_node (rn
);
2876 /* Schedule routes of a particular table (address-family) based on event. */
2878 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2880 struct route_node
*rn
;
2881 struct rib
*rib
, *next
;
2883 /* Walk all routes and queue for processing, if appropriate for
2884 * the trigger event.
2886 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2890 case RIB_UPDATE_IF_CHANGE
:
2891 /* Examine all routes that won't get processed by the protocol or
2892 * triggered by nexthop evaluation (NHT). This would be system,
2893 * kernel and certain static routes. Note that NHT will get
2894 * triggered upon an interface event as connected routes always
2895 * get queued for processing.
2897 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2899 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2900 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2901 rib
->type
== ZEBRA_ROUTE_BGP
)
2902 continue; /* protocol will handle. */
2903 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2906 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2907 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2908 nh
->type
== NEXTHOP_TYPE_IPV6
))
2911 /* If we only have nexthops to a gateway, NHT will
2922 case RIB_UPDATE_RMAP_CHANGE
:
2923 case RIB_UPDATE_OTHER
:
2924 /* Right now, examine all routes. Can restrict to a protocol in
2925 * some cases (TODO).
2927 if (rnode_to_ribs (rn
))
2937 /* RIB update function. */
2939 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
2941 struct route_table
*table
;
2943 /* Process routes of interested address-families. */
2944 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2946 rib_update_table (table
, event
);
2948 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2950 rib_update_table (table
, event
);
2953 /* Remove all routes which comes from non main table. */
2955 rib_weed_table (struct route_table
*table
)
2957 struct route_node
*rn
;
2962 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2963 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2965 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2968 if (rib
->table
!= zebrad
.rtm_table_default
&&
2969 rib
->table
!= RT_TABLE_MAIN
)
2970 rib_delnode (rn
, rib
);
2974 /* Delete all routes from non main table. */
2976 rib_weed_tables (void)
2979 struct zebra_vrf
*zvrf
;
2981 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
2982 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
2984 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2985 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2989 /* Delete self installed routes after zebra is relaunched. */
2991 rib_sweep_table (struct route_table
*table
)
2993 struct route_node
*rn
;
2999 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3000 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3002 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3005 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3006 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3008 ret
= rib_uninstall_kernel (rn
, rib
);
3010 rib_delnode (rn
, rib
);
3015 /* Sweep all RIB tables. */
3017 rib_sweep_route (void)
3020 struct zebra_vrf
*zvrf
;
3022 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3023 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3025 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3026 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3030 /* Remove specific by protocol routes from 'table'. */
3031 static unsigned long
3032 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
3034 struct route_node
*rn
;
3037 unsigned long n
= 0;
3040 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3041 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3043 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3045 if (rib
->type
== proto
&& rib
->instance
== instance
)
3047 rib_delnode (rn
, rib
);
3055 /* Remove specific by protocol routes. */
3057 rib_score_proto (u_char proto
, u_short instance
)
3060 struct zebra_vrf
*zvrf
;
3061 unsigned long cnt
= 0;
3063 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3064 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3065 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3066 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3071 /* Close RIB and clean up kernel routes. */
3073 rib_close_table (struct route_table
*table
)
3075 struct route_node
*rn
;
3076 rib_table_info_t
*info
= table
->info
;
3080 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3081 RNODE_FOREACH_RIB (rn
, rib
)
3083 if (!CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3086 if (info
->safi
== SAFI_UNICAST
)
3087 zfpm_trigger_update (rn
, NULL
);
3089 if (! RIB_SYSTEM_ROUTE (rib
))
3090 rib_uninstall_kernel (rn
, rib
);
3094 /* Close all RIB tables. */
3099 struct zebra_vrf
*zvrf
;
3100 struct listnode
*node
;
3101 struct interface
*ifp
;
3104 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3106 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3108 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3109 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3111 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter
), node
, ifp
))
3112 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
3115 /* If we do multiple tables per vrf, need to move this to loop above */
3116 zvrf
= vrf_info_lookup (VRF_DEFAULT
);
3118 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
3120 if (zvrf
->other_table
[AFI_IP
][table_id
])
3121 rib_close_table (zvrf
->other_table
[AFI_IP
][table_id
]);
3123 if (zvrf
->other_table
[AFI_IP6
][table_id
])
3124 rib_close_table (zvrf
->other_table
[AFI_IP6
][table_id
]);
3128 /* Routing information base initialize. */
3132 rib_queue_init (&zebrad
);
3138 * Get the first vrf id that is greater than the given vrf id if any.
3140 * Returns TRUE if a vrf id was found, FALSE otherwise.
3143 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3145 vrf_iter_t iter
= vrf_iterator (vrf_id
);
3146 struct zebra_vrf
*zvrf
= vrf_iter2info (iter
);
3148 /* The same one ? Then find out the next. */
3149 if (zvrf
&& (zvrf
->vrf_id
== vrf_id
))
3150 zvrf
= vrf_iter2info (vrf_next (iter
));
3154 *next_id_p
= zvrf
->vrf_id
;
3162 * rib_tables_iter_next
3164 * Returns the next table in the iteration.
3166 struct route_table
*
3167 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3169 struct route_table
*table
;
3172 * Array that helps us go over all AFI/SAFI combinations via one
3179 { AFI_IP
, SAFI_UNICAST
},
3180 { AFI_IP
, SAFI_MULTICAST
},
3181 { AFI_IP6
, SAFI_UNICAST
},
3182 { AFI_IP6
, SAFI_MULTICAST
},
3187 switch (iter
->state
)
3190 case RIB_TABLES_ITER_S_INIT
:
3191 iter
->vrf_id
= VRF_DEFAULT
;
3192 iter
->afi_safi_ix
= -1;
3196 case RIB_TABLES_ITER_S_ITERATING
:
3197 iter
->afi_safi_ix
++;
3201 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3203 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3204 afi_safis
[iter
->afi_safi_ix
].safi
,
3209 iter
->afi_safi_ix
++;
3213 * Found another table in this vrf.
3219 * Done with all tables in the current vrf, go to the next
3222 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3225 iter
->afi_safi_ix
= 0;
3230 case RIB_TABLES_ITER_S_DONE
:
3235 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3237 iter
->state
= RIB_TABLES_ITER_S_DONE
;