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
));
208 nexthop_add_labels (nexthop
, nh
->nh_label_type
, nh
->nh_label
->num_labels
,
209 &nh
->nh_label
->label
[0]);
210 rib_nexthop_add(rib
, nexthop
);
211 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
212 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
215 /* Delete specified nexthop from the list. */
217 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
220 nexthop
->next
->prev
= nexthop
->prev
;
222 nexthop
->prev
->next
= nexthop
->next
;
224 rib
->nexthop
= nexthop
->next
;
231 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
233 struct nexthop
*nexthop
;
235 nexthop
= nexthop_new();
236 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
237 nexthop
->ifindex
= ifindex
;
239 rib_nexthop_add (rib
, nexthop
);
245 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
247 struct nexthop
*nexthop
;
249 nexthop
= nexthop_new();
250 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
251 nexthop
->gate
.ipv4
= *ipv4
;
253 nexthop
->src
.ipv4
= *src
;
255 rib_nexthop_add (rib
, nexthop
);
261 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
262 struct in_addr
*src
, ifindex_t ifindex
)
264 struct nexthop
*nexthop
;
265 struct interface
*ifp
;
267 nexthop
= nexthop_new();
268 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
269 nexthop
->gate
.ipv4
= *ipv4
;
271 nexthop
->src
.ipv4
= *src
;
272 nexthop
->ifindex
= ifindex
;
273 ifp
= if_lookup_by_index (nexthop
->ifindex
);
274 /*Pending: need to think if null ifp here is ok during bootup?
275 There was a crash because ifp here was coming to be NULL */
277 if (connected_is_unnumbered(ifp
)) {
278 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
281 rib_nexthop_add (rib
, nexthop
);
287 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
289 struct nexthop
*nexthop
;
291 nexthop
= nexthop_new();
292 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
293 nexthop
->gate
.ipv6
= *ipv6
;
295 rib_nexthop_add (rib
, nexthop
);
301 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
304 struct nexthop
*nexthop
;
306 nexthop
= nexthop_new();
307 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
308 nexthop
->gate
.ipv6
= *ipv6
;
309 nexthop
->ifindex
= ifindex
;
311 rib_nexthop_add (rib
, nexthop
);
317 rib_nexthop_blackhole_add (struct rib
*rib
)
319 struct nexthop
*nexthop
;
321 nexthop
= nexthop_new();
322 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
323 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
325 rib_nexthop_add (rib
, nexthop
);
330 /* This method checks whether a recursive nexthop has at
331 * least one resolved nexthop in the fib.
334 nexthop_has_fib_child(struct nexthop
*nexthop
)
338 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
341 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
342 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
348 /* If force flag is not set, do not modify falgs at all for uninstall
349 the route from FIB. */
351 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
352 struct route_node
*top
)
354 struct prefix_ipv4 p
;
355 struct route_table
*table
;
356 struct route_node
*rn
;
359 struct nexthop
*newhop
, *tnewhop
;
360 struct nexthop
*resolved_hop
;
362 struct interface
*ifp
;
364 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
365 nexthop
->ifindex
= 0;
369 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
370 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
371 nexthops_free(nexthop
->resolved
);
372 nexthop
->resolved
= NULL
;
373 rib
->nexthop_mtu
= 0;
376 /* Skip nexthops that have been filtered out due to route-map */
377 /* The nexthops are specific to this route and so the same */
378 /* nexthop for a different route may not have this flag set */
379 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
383 * Check to see if we should trust the passed in information
384 * for UNNUMBERED interfaces as that we won't find the GW
385 * address in the routing table.
387 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
389 ifp
= if_lookup_by_index (nexthop
->ifindex
);
390 if (ifp
&& connected_is_unnumbered(ifp
))
392 if (if_is_operative(ifp
))
401 /* Make lookup prefix. */
402 memset (&p
, 0, sizeof (struct prefix_ipv4
));
404 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
405 p
.prefix
= nexthop
->gate
.ipv4
;
408 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, rib
->vrf_id
);
412 rn
= route_node_match (table
, (struct prefix
*) &p
);
415 route_unlock_node (rn
);
417 /* If lookup self prefix return immediately. */
421 /* Pick up selected route. */
422 /* However, do not resolve over default route unless explicitly allowed. */
423 if (is_default_prefix (&rn
->p
) &&
424 !nh_resolve_via_default (p
.family
))
427 RNODE_FOREACH_RIB (rn
, match
)
429 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
432 /* if the next hop is imported from another table, skip it */
433 if (match
->type
== ZEBRA_ROUTE_TABLE
)
435 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
439 /* If there is no selected route or matched route is EGP, go up
445 } while (rn
&& rn
->info
== NULL
);
447 route_lock_node (rn
);
451 /* If the longest prefix match for the nexthop yields
452 * a blackhole, mark it as inactive. */
453 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
454 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
457 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
459 /* Directly point connected route. */
460 newhop
= match
->nexthop
;
461 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
462 nexthop
->ifindex
= newhop
->ifindex
;
466 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
469 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
470 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
471 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
475 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
476 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
478 resolved_hop
= nexthop_new();
479 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
480 /* If the resolving route specifies a gateway, use it */
481 if (newhop
->type
== NEXTHOP_TYPE_IPV4
482 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
484 resolved_hop
->type
= newhop
->type
;
485 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
489 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
490 resolved_hop
->ifindex
= newhop
->ifindex
;
491 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
492 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
496 /* If the resolving route is an interface route,
497 * it means the gateway we are looking up is connected
498 * to that interface. (The actual network is _not_ onlink).
499 * Therefore, the resolved route should have the original
500 * gateway as nexthop as it is directly connected.
502 * On Linux, we have to set the onlink netlink flag because
503 * otherwise, the kernel won't accept the route. */
504 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
506 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
507 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
508 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
509 resolved_hop
->ifindex
= newhop
->ifindex
;
512 nexthop_add(&nexthop
->resolved
, resolved_hop
);
518 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
521 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
522 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
526 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
528 resolved_hop
= nexthop_new();
529 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
530 /* If the resolving route specifies a gateway, use it */
531 if (newhop
->type
== NEXTHOP_TYPE_IPV4
532 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
534 resolved_hop
->type
= newhop
->type
;
535 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
539 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
540 resolved_hop
->ifindex
= newhop
->ifindex
;
541 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
542 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
546 /* If the resolving route is an interface route,
547 * it means the gateway we are looking up is connected
548 * to that interface. (The actual network is _not_ onlink).
549 * Therefore, the resolved route should have the original
550 * gateway as nexthop as it is directly connected.
552 * On Linux, we have to set the onlink netlink flag because
553 * otherwise, the kernel won't accept the route.
555 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
557 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
558 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
559 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
560 resolved_hop
->ifindex
= newhop
->ifindex
;
563 nexthop_add(&nexthop
->resolved
, resolved_hop
);
568 rib
->nexthop_mtu
= match
->mtu
;
580 /* If force flag is not set, do not modify falgs at all for uninstall
581 the route from FIB. */
583 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
584 struct route_node
*top
)
586 struct prefix_ipv6 p
;
587 struct route_table
*table
;
588 struct route_node
*rn
;
591 struct nexthop
*newhop
, *tnewhop
;
593 struct nexthop
*resolved_hop
;
595 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
596 nexthop
->ifindex
= 0;
600 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
601 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, nexthop
->resolved
, top
);
602 nexthops_free(nexthop
->resolved
);
603 nexthop
->resolved
= NULL
;
606 /* Skip nexthops that have been filtered out due to route-map */
607 /* The nexthops are specific to this route and so the same */
608 /* nexthop for a different route may not have this flag set */
609 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
612 /* Make lookup prefix. */
613 memset (&p
, 0, sizeof (struct prefix_ipv6
));
615 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
616 p
.prefix
= nexthop
->gate
.ipv6
;
619 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, rib
->vrf_id
);
623 rn
= route_node_match (table
, (struct prefix
*) &p
);
626 route_unlock_node (rn
);
628 /* If lookup self prefix return immediately. */
632 /* Pick up selected route. */
633 /* However, do not resolve over default route unless explicitly allowed. */
634 if (is_default_prefix (&rn
->p
) &&
635 !nh_resolve_via_default (p
.family
))
638 RNODE_FOREACH_RIB (rn
, match
)
640 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
642 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
646 /* If there is no selected route or matched route is EGP, go up
652 } while (rn
&& rn
->info
== NULL
);
654 route_lock_node (rn
);
658 /* If the longest prefix match for the nexthop yields
659 * a blackhole, mark it as inactive. */
660 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
661 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
664 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
666 /* Directly point connected route. */
667 newhop
= match
->nexthop
;
669 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
670 nexthop
->ifindex
= newhop
->ifindex
;
674 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
677 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
678 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
679 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
683 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
684 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
686 resolved_hop
= nexthop_new();
687 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
688 /* See nexthop_active_ipv4 for a description how the
689 * resolved nexthop is constructed. */
690 if (newhop
->type
== NEXTHOP_TYPE_IPV6
691 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
693 resolved_hop
->type
= newhop
->type
;
694 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
698 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
699 resolved_hop
->ifindex
= newhop
->ifindex
;
703 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
705 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
706 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
707 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
708 resolved_hop
->ifindex
= newhop
->ifindex
;
711 nexthop_add(&nexthop
->resolved
, resolved_hop
);
717 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
720 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
721 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
725 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
727 resolved_hop
= nexthop_new();
728 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
729 /* See nexthop_active_ipv4 for a description how the
730 * resolved nexthop is constructed. */
731 if (newhop
->type
== NEXTHOP_TYPE_IPV6
732 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
734 resolved_hop
->type
= newhop
->type
;
735 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
739 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
740 resolved_hop
->ifindex
= newhop
->ifindex
;
744 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
746 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
747 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
748 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
749 resolved_hop
->ifindex
= newhop
->ifindex
;
752 nexthop_add(&nexthop
->resolved
, resolved_hop
);
768 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
769 union g_addr
*addr
, struct route_node
**rn_out
)
772 struct route_table
*table
;
773 struct route_node
*rn
;
775 struct nexthop
*newhop
, *tnewhop
;
779 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
783 memset (&p
, 0, sizeof (struct prefix
));
787 p
.u
.prefix4
= addr
->ipv4
;
788 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
792 p
.u
.prefix6
= addr
->ipv6
;
793 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
796 rn
= route_node_match (table
, (struct prefix
*) &p
);
800 route_unlock_node (rn
);
802 /* Pick up selected route. */
803 RNODE_FOREACH_RIB (rn
, match
)
805 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
807 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
811 /* If there is no selected route or matched route is EGP, go up
817 } while (rn
&& rn
->info
== NULL
);
819 route_lock_node (rn
);
823 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
826 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
827 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
845 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
847 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
848 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
849 union g_addr gaddr
= { .ipv4
= addr
};
851 switch (ipv4_multicast_mode
)
853 case MCAST_MRIB_ONLY
:
854 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
855 case MCAST_URIB_ONLY
:
856 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
857 case MCAST_NO_CONFIG
:
858 case MCAST_MIX_MRIB_FIRST
:
859 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
861 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
863 case MCAST_MIX_DISTANCE
:
864 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
865 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
867 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
873 case MCAST_MIX_PFXLEN
:
874 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
875 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
877 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
886 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
888 if (IS_ZEBRA_DEBUG_RIB
)
891 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
893 zlog_debug("%s: %s: found %s, using %s",
895 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
896 urib
? "URIB" : "nothing",
897 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
903 multicast_mode_ipv4_set (enum multicast_mode mode
)
905 if (IS_ZEBRA_DEBUG_RIB
)
906 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
907 ipv4_multicast_mode
= mode
;
911 multicast_mode_ipv4_get (void)
913 return ipv4_multicast_mode
;
917 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
919 struct route_table
*table
;
920 struct route_node
*rn
;
922 struct nexthop
*nexthop
, *tnexthop
;
926 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
930 rn
= route_node_lookup (table
, (struct prefix
*) p
);
932 /* No route for this prefix. */
937 route_unlock_node (rn
);
939 RNODE_FOREACH_RIB (rn
, match
)
941 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
943 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
950 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
953 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
954 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
961 * This clone function, unlike its original rib_lookup_ipv4(), checks
962 * if specified IPv4 route record (prefix/mask -> gate) exists in
963 * the whole RIB and has RIB_ENTRY_SELECTED_FIB set.
967 * 0: exact match found
968 * 1: a match was found with a different gate
969 * 2: connected route found
970 * 3: no matches found
973 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
976 struct route_table
*table
;
977 struct route_node
*rn
;
979 struct nexthop
*nexthop
, *tnexthop
;
984 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
986 return ZEBRA_RIB_LOOKUP_ERROR
;
988 /* Scan the RIB table for exactly matching RIB entry. */
989 rn
= route_node_lookup (table
, (struct prefix
*) p
);
991 /* No route for this prefix. */
993 return ZEBRA_RIB_NOTFOUND
;
996 route_unlock_node (rn
);
998 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
999 RNODE_FOREACH_RIB (rn
, match
)
1001 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1003 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
1007 /* None such found :( */
1009 return ZEBRA_RIB_NOTFOUND
;
1011 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1012 return ZEBRA_RIB_FOUND_CONNECTED
;
1014 /* Ok, we have a cood candidate, let's check it's nexthop list... */
1015 nexthops_active
= 0;
1016 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
1017 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1019 nexthops_active
= 1;
1020 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
1021 return ZEBRA_RIB_FOUND_EXACT
;
1022 if (IS_ZEBRA_DEBUG_RIB
)
1024 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
1025 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
1026 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
1027 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
1028 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
1032 if (nexthops_active
)
1033 return ZEBRA_RIB_FOUND_NOGATE
;
1035 return ZEBRA_RIB_NOTFOUND
;
1038 #define RIB_SYSTEM_ROUTE(R) \
1039 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1041 /* This function verifies reachability of one given nexthop, which can be
1042 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1043 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1044 * nexthop->ifindex will be updated appropriately as well.
1045 * An existing route map can turn (otherwise active) nexthop into inactive, but
1048 * The return value is the final value of 'ACTIVE' flag.
1052 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
1053 struct nexthop
*nexthop
, int set
)
1055 rib_table_info_t
*info
= rn
->table
->info
;
1056 struct interface
*ifp
;
1057 route_map_result_t ret
= RMAP_MATCH
;
1059 char buf
[INET6_ADDRSTRLEN
+1];
1061 if (rn
->p
.family
== AF_INET
)
1063 else if (rn
->p
.family
== AF_INET6
)
1067 switch (nexthop
->type
)
1069 case NEXTHOP_TYPE_IFINDEX
:
1070 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1071 if (ifp
&& if_is_operative(ifp
))
1072 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1074 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1076 case NEXTHOP_TYPE_IPV4
:
1077 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1079 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
1080 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1082 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1084 case NEXTHOP_TYPE_IPV6
:
1086 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1087 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1089 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1091 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1092 /* RFC 5549, v4 prefix with v6 NH */
1093 if (rn
->p
.family
!= AF_INET
)
1095 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1097 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1098 if (ifp
&& if_is_operative(ifp
))
1099 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1101 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1105 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1106 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1108 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1111 case NEXTHOP_TYPE_BLACKHOLE
:
1112 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1117 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1120 /* XXX: What exactly do those checks do? Do we support
1121 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1122 if (RIB_SYSTEM_ROUTE(rib
) ||
1123 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1124 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1125 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1127 /* The original code didn't determine the family correctly
1128 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1129 * from the rib_table_info in those cases.
1130 * Possibly it may be better to use only the rib_table_info
1136 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1138 /* It'll get set if required inside */
1139 ret
= zebra_route_map_check(family
, rib
->type
, &rn
->p
, nexthop
, rib
->vrf_id
,
1141 if (ret
== RMAP_DENYMATCH
)
1143 if (IS_ZEBRA_DEBUG_RIB
)
1145 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1146 zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map",
1147 rib
->vrf_id
, buf
, rn
->p
.prefixlen
,
1148 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1150 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1152 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1155 /* Iterate over all nexthops of the given RIB entry and refresh their
1156 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1157 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1158 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1159 * transparently passed to nexthop_active_check().
1161 * Return value is the new number of active nexthops.
1165 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1167 struct nexthop
*nexthop
;
1168 union g_addr prev_src
;
1169 unsigned int prev_active
, new_active
, old_num_nh
;
1170 ifindex_t prev_index
;
1171 old_num_nh
= rib
->nexthop_active_num
;
1173 rib
->nexthop_active_num
= 0;
1174 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1176 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1178 /* No protocol daemon provides src and so we're skipping tracking it */
1179 prev_src
= nexthop
->rmap_src
;
1180 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1181 prev_index
= nexthop
->ifindex
;
1182 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1183 rib
->nexthop_active_num
++;
1184 /* Don't allow src setting on IPv6 addr for now */
1185 if (prev_active
!= new_active
||
1186 prev_index
!= nexthop
->ifindex
||
1187 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1188 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1189 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1190 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1191 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1192 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1194 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1195 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1199 if (old_num_nh
!= rib
->nexthop_active_num
)
1200 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1202 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1204 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1207 return rib
->nexthop_active_num
;
1212 /* Update flag indicates whether this is a "replace" or not. Currently, this
1213 * is only used for IPv4.
1216 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, int update
)
1219 struct nexthop
*nexthop
, *tnexthop
;
1220 rib_table_info_t
*info
= rn
->table
->info
;
1223 if (info
->safi
!= SAFI_UNICAST
)
1225 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1226 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1231 * Make sure we update the FPM any time we send new information to
1234 zfpm_trigger_update (rn
, "installing in kernel");
1235 switch (PREFIX_FAMILY (&rn
->p
))
1239 ret
= kernel_update_ipv4 (&rn
->p
, rib
);
1241 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
1245 ret
= kernel_update_ipv6 (&rn
->p
, rib
);
1247 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
1251 /* If install succeeds, update FIB flag for nexthops. */
1254 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1256 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1259 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1260 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1262 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1269 /* Uninstall the route from kernel. */
1271 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1274 struct nexthop
*nexthop
, *tnexthop
;
1275 rib_table_info_t
*info
= rn
->table
->info
;
1278 if (info
->safi
!= SAFI_UNICAST
)
1280 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1281 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1286 * Make sure we update the FPM any time we send new information to
1289 zfpm_trigger_update (rn
, "uninstalling from kernel");
1291 switch (PREFIX_FAMILY (&rn
->p
))
1294 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
1297 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
1301 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1302 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1307 /* Uninstall the route from kernel. */
1309 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1311 rib_table_info_t
*info
= rn
->table
->info
;
1313 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1315 if (info
->safi
== SAFI_UNICAST
)
1316 zfpm_trigger_update (rn
, "rib_uninstall");
1318 if (! RIB_SYSTEM_ROUTE (rib
))
1319 rib_uninstall_kernel (rn
, rib
);
1320 UNSET_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
);
1323 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1325 redistribute_delete (&rn
->p
, rib
);
1326 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1330 static void rib_unlink (struct route_node
*, struct rib
*);
1333 * rib_can_delete_dest
1335 * Returns TRUE if the given dest can be deleted from the table.
1338 rib_can_delete_dest (rib_dest_t
*dest
)
1346 * Don't delete the dest if we have to update the FPM about this
1349 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1350 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1359 * Garbage collect the rib dest corresponding to the given route node
1362 * Returns TRUE if the dest was deleted, FALSE otherwise.
1365 rib_gc_dest (struct route_node
*rn
)
1368 struct zebra_vrf
*zvrf
;
1370 dest
= rib_dest_from_rnode (rn
);
1374 if (!rib_can_delete_dest (dest
))
1377 zvrf
= rib_dest_vrf (dest
);
1378 if (IS_ZEBRA_DEBUG_RIB
)
1379 rnode_debug (rn
, zvrf
->vrf_id
, "removing dest from table");
1382 XFREE (MTYPE_RIB_DEST
, dest
);
1386 * Release the one reference that we keep on the route node.
1388 route_unlock_node (rn
);
1393 rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1396 char buf
[INET6_ADDRSTRLEN
];
1398 zfpm_trigger_update (rn
, "new route selected");
1400 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1401 if (!nexthop_active_update (rn
, new, 1))
1403 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1407 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1408 if (IS_ZEBRA_DEBUG_RIB
)
1410 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1411 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1412 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, new, new->type
);
1415 if (!RIB_SYSTEM_ROUTE (new))
1417 if (rib_install_kernel (rn
, new, 0))
1419 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1420 zlog_warn ("%u:%s/%d: Route install failed",
1421 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1425 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1429 rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1432 char buf
[INET6_ADDRSTRLEN
];
1434 zfpm_trigger_update (rn
, "removing existing route");
1436 /* Uninstall from kernel. */
1437 if (IS_ZEBRA_DEBUG_RIB
)
1439 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1440 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1441 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, old
, old
->type
);
1444 if (!RIB_SYSTEM_ROUTE (old
))
1445 rib_uninstall_kernel (rn
, old
);
1447 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1449 /* Update nexthop for route, reset changed flag. */
1450 nexthop_active_update (rn
, old
, 1);
1451 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1455 rib_process_update_fib (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1456 struct rib
*old
, struct rib
*new)
1458 char buf
[INET6_ADDRSTRLEN
];
1459 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1464 if (IS_ZEBRA_DEBUG_RIB
)
1465 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1468 * We have to install or update if a new route has been selected or
1469 * something has changed.
1472 CHECK_FLAG (new->status
, RIB_ENTRY_CHANGED
))
1474 zfpm_trigger_update (rn
, "updating existing route");
1476 /* Update the nexthop; we could determine here that nexthop is inactive. */
1477 if (nexthop_active_update (rn
, new, 1))
1480 /* If nexthop is active, install the selected route, if appropriate. If
1481 * the install succeeds, cleanup flags for prior route, if different from
1486 if (IS_ZEBRA_DEBUG_RIB
)
1489 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1490 "old %p (type %d)", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1491 rn
, new, new->type
, old
, old
->type
);
1493 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1494 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, new, new->type
);
1496 /* Non-system route should be installed. */
1497 if (!RIB_SYSTEM_ROUTE (new))
1499 if (rib_install_kernel (rn
, new, 1))
1502 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1503 zlog_warn ("%u:%s/%d: Route install failed",
1504 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1508 /* If install succeeded or system route, cleanup flags for prior route. */
1509 if (installed
&& new != old
)
1511 if (RIB_SYSTEM_ROUTE(new))
1513 if (!RIB_SYSTEM_ROUTE (old
))
1514 rib_uninstall_kernel (rn
, old
);
1518 for (nexthop
= old
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1519 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1523 /* Update for redistribution. */
1525 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1529 * If nexthop for selected route is not active or install failed, we
1530 * may need to uninstall and delete for redistribution.
1532 if (!nh_active
|| !installed
)
1534 if (IS_ZEBRA_DEBUG_RIB
)
1537 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1538 "old %p (type %d) - %s", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1539 rn
, new, new->type
, old
, old
->type
,
1540 nh_active
? "install failed" : "nexthop inactive");
1542 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) - %s",
1543 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, new, new->type
,
1544 nh_active
? "install failed" : "nexthop inactive");
1547 if (!RIB_SYSTEM_ROUTE (old
))
1548 rib_uninstall_kernel (rn
, old
);
1549 UNSET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1555 * Same route selected; check if in the FIB and if not, re-install. This
1556 * is housekeeping code to deal with race conditions in kernel with linux
1557 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1560 if (!RIB_SYSTEM_ROUTE (new))
1564 for (ALL_NEXTHOPS_RO(new->nexthop
, nexthop
, tnexthop
, recursing
))
1565 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1571 rib_install_kernel (rn
, new, 0);
1575 /* Update prior route. */
1578 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1580 /* Set real nexthop. */
1581 nexthop_active_update (rn
, old
, 1);
1582 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1585 /* Clear changed flag. */
1586 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1589 /* Check if 'alternate' RIB entry is better than 'current'. */
1591 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1593 if (current
== NULL
)
1596 /* filter route selection in following order:
1597 * - connected beats other types
1598 * - lower distance beats higher
1599 * - lower metric beats higher for equal distance
1600 * - last, hence oldest, route wins tie break.
1603 /* Connected routes. Pick the last connected
1604 * route of the set of lowest metric connected routes.
1606 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1608 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1609 || alternate
->metric
<= current
->metric
)
1615 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1618 /* higher distance loses */
1619 if (alternate
->distance
< current
->distance
)
1621 if (current
->distance
< alternate
->distance
)
1624 /* metric tie-breaks equal distance */
1625 if (alternate
->metric
<= current
->metric
)
1631 /* Core function for processing routing information base. */
1633 rib_process (struct route_node
*rn
)
1637 struct rib
*old_selected
= NULL
;
1638 struct rib
*new_selected
= NULL
;
1639 struct rib
*old_fib
= NULL
;
1640 struct rib
*new_fib
= NULL
;
1641 struct rib
*best
= NULL
;
1642 char buf
[INET6_ADDRSTRLEN
];
1644 struct zebra_vrf
*zvrf
= NULL
;
1645 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1649 dest
= rib_dest_from_rnode (rn
);
1652 zvrf
= rib_dest_vrf (dest
);
1653 vrf_id
= zvrf
->vrf_id
;
1656 if (IS_ZEBRA_DEBUG_RIB
)
1657 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1659 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1660 zlog_debug ("%u:%s/%d: Processing rn %p", vrf_id
, buf
, rn
->p
.prefixlen
, rn
);
1662 RNODE_FOREACH_RIB (rn
, rib
)
1664 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1665 zlog_debug ("%u:%s/%d: Examine rib %p (type %d) status %x flags %x "
1666 "dist %d metric %d",
1667 vrf_id
, buf
, rn
->p
.prefixlen
, rib
, rib
->type
, rib
->status
,
1668 rib
->flags
, rib
->distance
, rib
->metric
);
1670 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1672 /* Currently selected rib. */
1673 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1675 assert (old_selected
== NULL
);
1678 /* Currently in fib */
1679 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1681 assert (old_fib
== NULL
);
1685 /* Skip deleted entries from selection */
1686 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1689 /* Skip unreachable nexthop. */
1690 /* This first call to nexthop_active_update is merely to determine if
1691 * there's any change to nexthops associated with this RIB entry. Now,
1692 * rib_process() can be invoked due to an external event such as link
1693 * down or due to next-hop-tracking evaluation. In the latter case,
1694 * a decision has already been made that the NHs have changed. So, no
1695 * need to invoke a potentially expensive call again. Further, since
1696 * the change might be in a recursive NH which is not caught in
1697 * the nexthop_active_update() code. Thus, we might miss changes to
1700 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1701 ! nexthop_active_update (rn
, rib
, 0))
1703 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1705 /* XXX: HERE BE DRAGONS!!!!!
1706 * In all honesty, I have not yet figured out what this part
1707 * does or why the RIB_ENTRY_CHANGED test above is correct
1708 * or why we need to delete a route here, and also not whether
1709 * this concerns both selected and fib route, or only selected
1711 /* This entry was denied by the 'ip protocol table' route-map, we
1712 * need to delete it */
1713 if (rib
!= old_selected
)
1715 if (IS_ZEBRA_DEBUG_RIB
)
1716 zlog_debug ("%s: %s/%d: imported via import-table but denied "
1717 "by the ip protocol table route-map",
1718 __func__
, buf
, rn
->p
.prefixlen
);
1719 rib_unlink (rn
, rib
);
1722 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1728 /* Infinite distance. */
1729 if (rib
->distance
== DISTANCE_INFINITY
)
1731 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1735 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
))
1737 best
= rib_choose_best(new_fib
, rib
);
1738 if (new_fib
&& best
!= new_fib
)
1739 UNSET_FLAG (new_fib
->status
, RIB_ENTRY_CHANGED
);
1744 best
= rib_choose_best(new_selected
, rib
);
1745 if (new_selected
&& best
!= new_selected
)
1746 UNSET_FLAG (new_selected
->status
, RIB_ENTRY_CHANGED
);
1747 new_selected
= best
;
1750 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1751 } /* RNODE_FOREACH_RIB */
1753 /* If no FIB override route, use the selected route also for FIB */
1754 if (new_fib
== NULL
)
1755 new_fib
= new_selected
;
1757 /* After the cycle is finished, the following pointers will be set:
1758 * old_selected --- RIB entry currently having SELECTED
1759 * new_selected --- RIB entry that is newly SELECTED
1760 * old_fib --- RIB entry currently in kernel FIB
1761 * new_fib --- RIB entry that is newly to be in kernel FIB
1763 * new_selected will get SELECTED flag, and is going to be redistributed
1764 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1767 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1769 zlog_debug ("%u:%s/%d: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1770 vrf_id
, buf
, rn
->p
.prefixlen
,
1771 (void *)old_selected
,
1772 (void *)new_selected
,
1777 /* Buffer RIB_ENTRY_CHANGED here, because it will get cleared if
1778 * fib == selected */
1779 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1782 /* Update fib according to selection results */
1783 if (new_fib
&& old_fib
)
1784 rib_process_update_fib (zvrf
, rn
, old_fib
, new_fib
);
1786 rib_process_add_fib (zvrf
, rn
, new_fib
);
1788 rib_process_del_fib (zvrf
, rn
, old_fib
);
1790 /* Redistribute SELECTED entry */
1791 if (old_selected
!= new_selected
|| selected_changed
)
1793 struct nexthop
*nexthop
, *tnexthop
;
1796 /* Check if we have a FIB route for the destination, otherwise,
1797 * don't redistribute it */
1798 for (ALL_NEXTHOPS_RO(new_fib
? new_fib
->nexthop
: NULL
, nexthop
,
1799 tnexthop
, recursing
))
1801 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1807 new_selected
= NULL
;
1809 if (new_selected
&& new_selected
!= new_fib
)
1811 nexthop_active_update(rn
, new_selected
, 1);
1812 UNSET_FLAG(new_selected
->status
, RIB_ENTRY_CHANGED
);
1818 redistribute_delete(&rn
->p
, old_selected
);
1819 if (old_selected
!= new_selected
)
1820 UNSET_FLAG (old_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1825 /* Install new or replace existing redistributed entry */
1826 SET_FLAG (new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1827 redistribute_update (&rn
->p
, new_selected
, old_selected
);
1832 if (select
&& select
== fib
)
1834 if (IS_ZEBRA_DEBUG_RIB
)
1835 rnode_debug (rn
, vrf_id
, "Updating existing route, select %p, fib %p",
1836 (void *)select
, (void *)fib
);
1837 if (CHECK_FLAG (select
->status
, RIB_ENTRY_CHANGED
))
1839 if (info
->safi
== SAFI_UNICAST
)
1840 zfpm_trigger_update (rn
, "updating existing route");
1842 /* Set real nexthop. */
1843 /* Need to check if any NHs are active to clear the
1846 if (nexthop_active_update (rn
, select
, 1))
1848 if (IS_ZEBRA_DEBUG_RIB
)
1849 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1850 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1851 if (! RIB_SYSTEM_ROUTE (select
))
1853 /* Clear FIB flag if performing a replace, will get set again
1854 * as part of install.
1856 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1857 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1858 rib_install_kernel (rn
, select
, 1);
1861 /* assuming that the receiver knows how to dedup */
1862 redistribute_update (&rn
->p
, select
, NULL
);
1866 if (IS_ZEBRA_DEBUG_RIB
)
1867 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1868 "- nexthop inactive",
1869 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1871 /* Withdraw unreachable redistribute route */
1872 redistribute_delete(&rn
->p
, select
);
1874 /* Do the uninstall here, if not done earlier. */
1875 if (! RIB_SYSTEM_ROUTE (select
))
1876 rib_uninstall_kernel (rn
, select
);
1877 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1879 UNSET_FLAG (select
->status
, RIB_ENTRY_CHANGED
);
1881 else if (! RIB_SYSTEM_ROUTE (select
))
1883 /* Housekeeping code to deal with
1884 race conditions in kernel with linux
1885 netlink reporting interface up before IPv4 or IPv6 protocol
1886 is ready to add routes.
1887 This makes sure the routes are IN the kernel.
1890 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1891 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1897 rib_install_kernel (rn
, select
, 0);
1902 /* At this point we either haven't found the best RIB entry or it is
1903 * different from what we currently intend to flag with SELECTED. In both
1904 * cases, if a RIB block is present in FIB, it should be withdrawn.
1908 if (IS_ZEBRA_DEBUG_RIB
)
1909 rnode_debug (rn
, vrf_id
, "Removing existing route, fib %p", (void *)fib
);
1911 if (info
->safi
== SAFI_UNICAST
)
1912 zfpm_trigger_update (rn
, "removing existing route");
1914 /* If there's no route to replace this with, withdraw redistribute and
1915 * uninstall from kernel.
1919 if (IS_ZEBRA_DEBUG_RIB
)
1920 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1921 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1923 redistribute_delete(&rn
->p
, fib
);
1924 if (! RIB_SYSTEM_ROUTE (fib
))
1925 rib_uninstall_kernel (rn
, fib
);
1928 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1930 /* Set real nexthop. */
1931 nexthop_active_update (rn
, fib
, 1);
1932 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1935 /* Regardless of some RIB entry being SELECTED or not before, now we can
1936 * tell, that if a new winner exists, FIB is still not updated with this
1937 * data, but ready to be.
1941 if (IS_ZEBRA_DEBUG_RIB
)
1942 rnode_debug (rn
, "Adding route, select %p", (void *)select
);
1944 if (info
->safi
== SAFI_UNICAST
)
1945 zfpm_trigger_update (rn
, "new route selected");
1947 /* Set real nexthop. */
1948 if (nexthop_active_update (rn
, select
, 1))
1950 if (IS_ZEBRA_DEBUG_RIB
)
1953 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1954 "old %p (type %d)", vrf_id
, buf
, rn
->p
.prefixlen
, rn
,
1955 select
, select
->type
, fib
, fib
->type
);
1957 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1958 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1961 if (! RIB_SYSTEM_ROUTE (select
))
1963 /* Clear FIB flag if performing a replace, will get set again
1964 * as part of install.
1968 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1969 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1971 rib_install_kernel (rn
, select
, fib
? 1 : 0);
1975 /* Uninstall prior route here, if needed. */
1976 if (fib
&& !RIB_SYSTEM_ROUTE (fib
))
1977 rib_uninstall_kernel (rn
, fib
);
1980 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1981 /* Unconditionally announce, this part is exercised by new routes */
1982 /* If we cannot add, for example route added is learnt by the */
1983 /* protocol we're trying to redistribute to, delete the redist */
1984 /* This is notified by setting the is_update to 1 */
1985 redistribute_update (&rn
->p
, select
, fib
);
1989 /* Uninstall prior route here and do redist delete, if needed. */
1992 if (IS_ZEBRA_DEBUG_RIB
)
1993 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1994 "- nexthop inactive",
1995 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1997 if (!RIB_SYSTEM_ROUTE (fib
))
1998 rib_uninstall_kernel (rn
, fib
);
1999 redistribute_delete(&rn
->p
, fib
);
2002 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
2006 /* Remove all RIB entries queued for removal */
2007 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2009 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2011 if (IS_ZEBRA_DEBUG_RIB
)
2013 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
2014 (void *)rn
, (void *)rib
);
2016 rib_unlink(rn
, rib
);
2021 * Check if the dest can be deleted now.
2026 /* Take a list of route_node structs and return 1, if there was a record
2027 * picked from it and processed by rib_process(). Don't process more,
2028 * than one RN record; operate only in the specified sub-queue.
2031 process_subq (struct list
* subq
, u_char qindex
)
2033 struct listnode
*lnode
= listhead (subq
);
2034 struct route_node
*rnode
;
2035 char buf
[INET6_ADDRSTRLEN
];
2037 struct zebra_vrf
*zvrf
= NULL
;
2042 rnode
= listgetdata (lnode
);
2043 dest
= rib_dest_from_rnode (rnode
);
2045 zvrf
= rib_dest_vrf (dest
);
2047 rib_process (rnode
);
2049 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2051 inet_ntop (rnode
->p
.family
, &rnode
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2052 zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u",
2053 zvrf
? zvrf
->vrf_id
: 0, buf
, rnode
->p
.prefixlen
, rnode
, qindex
);
2057 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2062 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2063 __func__
, rnode
, rnode
->lock
);
2064 zlog_backtrace(LOG_DEBUG
);
2067 route_unlock_node (rnode
);
2068 list_delete_node (subq
, lnode
);
2073 * All meta queues have been processed. Trigger next-hop evaluation.
2076 meta_queue_process_complete (struct work_queue
*dummy
)
2079 struct zebra_vrf
*zvrf
;
2081 /* Evaluate nexthops for those VRFs which underwent route processing. This
2082 * should limit the evaluation to the necessary VRFs in most common
2085 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
2087 if (((zvrf
= vrf_iter2info (iter
)) != NULL
) &&
2088 (zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2090 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2091 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2092 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2093 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2094 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2098 /* Schedule LSPs for processing, if needed. */
2099 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2100 if (mpls_should_lsps_be_processed(zvrf
))
2102 if (IS_ZEBRA_DEBUG_MPLS
)
2103 zlog_debug ("%u: Scheduling all LSPs upon RIB completion", zvrf
->vrf_id
);
2104 zebra_mpls_lsp_schedule (zvrf
);
2105 mpls_unmark_lsps_for_processing(zvrf
);
2109 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2110 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
2111 * is pointed to the meta queue structure.
2113 static wq_item_status
2114 meta_queue_process (struct work_queue
*dummy
, void *data
)
2116 struct meta_queue
* mq
= data
;
2119 for (i
= 0; i
< MQ_SIZE
; i
++)
2120 if (process_subq (mq
->subq
[i
], i
))
2125 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2129 * Map from rib types to queue type (priority) in meta queue
2131 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
2132 [ZEBRA_ROUTE_SYSTEM
] = 4,
2133 [ZEBRA_ROUTE_KERNEL
] = 0,
2134 [ZEBRA_ROUTE_CONNECT
] = 0,
2135 [ZEBRA_ROUTE_STATIC
] = 1,
2136 [ZEBRA_ROUTE_RIP
] = 2,
2137 [ZEBRA_ROUTE_RIPNG
] = 2,
2138 [ZEBRA_ROUTE_OSPF
] = 2,
2139 [ZEBRA_ROUTE_OSPF6
] = 2,
2140 [ZEBRA_ROUTE_ISIS
] = 2,
2141 [ZEBRA_ROUTE_BGP
] = 3,
2142 [ZEBRA_ROUTE_HSLS
] = 4,
2143 [ZEBRA_ROUTE_TABLE
] = 1,
2146 /* Look into the RN and queue it into one or more priority queues,
2147 * increasing the size for each data push done.
2150 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
2154 RNODE_FOREACH_RIB (rn
, rib
)
2156 u_char qindex
= meta_queue_map
[rib
->type
];
2157 struct zebra_vrf
*zvrf
;
2159 /* Invariant: at this point we always have rn->info set. */
2160 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
2161 RIB_ROUTE_QUEUED (qindex
)))
2163 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2164 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
2165 (void *)rn
, qindex
);
2169 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2170 listnode_add (mq
->subq
[qindex
], rn
);
2171 route_lock_node (rn
);
2174 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2175 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
2176 (void *)rn
, qindex
);
2178 zvrf
= zebra_vrf_lookup (rib
->vrf_id
);
2180 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2184 /* Add route_node to work queue and schedule processing */
2186 rib_queue_add (struct route_node
*rn
)
2190 /* Pointless to queue a route_node with no RIB entries to add or remove */
2191 if (!rnode_to_ribs (rn
))
2193 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2194 __func__
, (void *)rn
, rn
->lock
);
2195 zlog_backtrace(LOG_DEBUG
);
2199 if (zebrad
.ribq
== NULL
)
2201 zlog_err ("%s: work_queue does not exist!", __func__
);
2206 * The RIB queue should normally be either empty or holding the only
2207 * work_queue_item element. In the latter case this element would
2208 * hold a pointer to the meta queue structure, which must be used to
2209 * actually queue the route nodes to process. So create the MQ
2210 * holder, if necessary, then push the work into it in any case.
2211 * This semantics was introduced after 0.99.9 release.
2213 if (!zebrad
.ribq
->items
->count
)
2214 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
2216 rib_meta_queue_add (zebrad
.mq
, rn
);
2221 /* Create new meta queue.
2222 A destructor function doesn't seem to be necessary here.
2224 static struct meta_queue
*
2225 meta_queue_new (void)
2227 struct meta_queue
*new;
2230 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
2233 for (i
= 0; i
< MQ_SIZE
; i
++)
2235 new->subq
[i
] = list_new ();
2236 assert(new->subq
[i
]);
2242 /* initialise zebra rib work queue */
2244 rib_queue_init (struct zebra_t
*zebra
)
2248 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
2249 "route_node processing")))
2251 zlog_err ("%s: could not initialise work queue!", __func__
);
2255 /* fill in the work queue spec */
2256 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2257 zebra
->ribq
->spec
.errorfunc
= NULL
;
2258 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2259 /* XXX: TODO: These should be runtime configurable via vty */
2260 zebra
->ribq
->spec
.max_retries
= 3;
2261 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
2263 if (!(zebra
->mq
= meta_queue_new ()))
2265 zlog_err ("%s: could not initialise meta queue!", __func__
);
2271 /* RIB updates are processed via a queue of pointers to route_nodes.
2273 * The queue length is bounded by the maximal size of the routing table,
2274 * as a route_node will not be requeued, if already queued.
2276 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2277 * state, or static_install_route (when an existing RIB is updated)
2278 * and then submit route_node to queue for best-path selection later.
2279 * Order of add/delete state changes are preserved for any given RIB.
2281 * Deleted RIBs are reaped during best-path selection.
2284 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2285 * |-------->| | best RIB, if required
2287 * static_install->|->rib_addqueue...... -> rib_process
2289 * |-------->| |-> rib_unlink
2290 * |-> set RIB_ENTRY_REMOVE |
2291 * rib_delnode (RIB freed)
2293 * The 'info' pointer of a route_node points to a rib_dest_t
2294 * ('dest'). Queueing state for a route_node is kept on the dest. The
2295 * dest is created on-demand by rib_link() and is kept around at least
2296 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2298 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2300 * - route_nodes: refcounted by:
2301 * - dest attached to route_node:
2302 * - managed by: rib_link/rib_gc_dest
2303 * - route_node processing queue
2304 * - managed by: rib_addqueue, rib_process.
2308 /* Add RIB to head of the route node. */
2310 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2315 const char *rmap_name
;
2319 dest
= rib_dest_from_rnode (rn
);
2322 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2323 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2325 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2326 route_lock_node (rn
); /* rn route table reference */
2331 head
= dest
->routes
;
2339 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2340 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2341 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2343 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2344 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2352 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2354 /* RIB node has been un-removed before route-node is processed.
2355 * route_node must hence already be on the queue for processing..
2357 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2359 if (IS_ZEBRA_DEBUG_RIB
)
2360 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2362 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2365 rib_link (rn
, rib
, process
);
2371 * Detach a rib structure from a route_node.
2373 * Note that a call to rib_unlink() should be followed by a call to
2374 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2375 * longer required to be deleted.
2378 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2384 if (IS_ZEBRA_DEBUG_RIB
)
2385 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2387 dest
= rib_dest_from_rnode (rn
);
2390 rib
->next
->prev
= rib
->prev
;
2393 rib
->prev
->next
= rib
->next
;
2396 dest
->routes
= rib
->next
;
2399 /* free RIB and nexthops */
2400 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2401 nexthops_free(rib
->nexthop
);
2402 XFREE (MTYPE_RIB
, rib
);
2407 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2411 if (IS_ZEBRA_DEBUG_RIB
)
2412 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2413 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2415 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2416 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2417 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2419 zebra_del_import_table_entry(rn
, rib
);
2420 /* Just clean up if non main table */
2421 if (IS_ZEBRA_DEBUG_RIB
)
2423 char buf
[INET6_ADDRSTRLEN
];
2424 if (IS_ZEBRA_DEBUG_RIB
)
2426 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2427 zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)",
2428 rib
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, rib
, rib
->type
);
2432 rib_unlink(rn
, rib
);
2440 /* This function dumps the contents of a given RIB entry into
2441 * standard debug log. Calling function name and IP prefix in
2442 * question are passed as 1st and 2nd arguments.
2445 void _rib_dump (const char * func
,
2446 union prefix46constptr pp
, const struct rib
* rib
)
2448 const struct prefix
*p
= pp
.p
;
2449 char straddr
[PREFIX_STRLEN
];
2450 struct nexthop
*nexthop
, *tnexthop
;
2453 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func
, (const void *)rib
,
2454 prefix2str(pp
, straddr
, sizeof(straddr
)), rib
->vrf_id
);
2457 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2460 (unsigned long) rib
->uptime
,
2467 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2477 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2480 rib
->nexthop_active_num
,
2481 rib
->nexthop_fib_num
2484 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2486 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2489 "%s: %s %s with flags %s%s%s",
2491 (recursing
? " NH" : "NH"),
2493 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2494 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2495 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2498 zlog_debug ("%s: dump complete", func
);
2501 /* This is an exported helper to rtm_read() to dump the strange
2502 * RIB entry found by rib_lookup_ipv4_route()
2505 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2507 struct route_table
*table
;
2508 struct route_node
*rn
;
2510 char prefix_buf
[INET_ADDRSTRLEN
];
2513 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2516 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2520 /* Scan the RIB table for exactly matching RIB entry. */
2521 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2523 /* No route for this prefix. */
2526 zlog_debug ("%s: lookup failed for %s", __func__
,
2527 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2532 route_unlock_node (rn
);
2535 RNODE_FOREACH_RIB (rn
, rib
)
2539 "%s: rn %p, rib %p: %s, %s",
2543 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2544 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2550 /* Check if requested address assignment will fail due to another
2551 * route being installed by zebra in FIB already. Take necessary
2552 * actions, if needed: remove such a route from FIB and deSELECT
2553 * corresponding RIB entry. Then put affected RN into RIBQ head.
2555 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2557 struct route_table
*table
;
2558 struct route_node
*rn
;
2560 unsigned changed
= 0;
2562 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2564 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2568 /* No matches would be the simplest case. */
2569 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2573 route_unlock_node (rn
);
2575 /* Check all RIB entries. In case any changes have to be done, requeue
2576 * the RN into RIBQ head. If the routing message about the new connected
2577 * route (generated by the IP address we are going to assign very soon)
2578 * comes before the RIBQ is processed, the new RIB entry will join
2579 * RIBQ record already on head. This is necessary for proper revalidation
2580 * of the rest of the RIB.
2582 RNODE_FOREACH_RIB (rn
, rib
)
2584 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
) &&
2585 ! RIB_SYSTEM_ROUTE (rib
))
2588 if (IS_ZEBRA_DEBUG_RIB
)
2590 char buf
[PREFIX_STRLEN
];
2591 zlog_debug ("%u:%s: freeing way for connected prefix",
2592 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2593 rib_dump (&rn
->p
, rib
);
2595 rib_uninstall (rn
, rib
);
2603 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2606 struct route_table
*table
;
2607 struct route_node
*rn
;
2609 struct nexthop
*nexthop
;
2616 if (p
->family
== AF_INET
)
2622 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2626 /* Make it sure prefixlen is applied to the prefix. */
2629 /* Set default distance by route type. */
2630 if (rib
->distance
== 0)
2632 rib
->distance
= route_info
[rib
->type
].distance
;
2634 /* iBGP distance is 200. */
2635 if (rib
->type
== ZEBRA_ROUTE_BGP
2636 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2637 rib
->distance
= 200;
2640 /* Lookup route node.*/
2641 rn
= route_node_get (table
, p
);
2643 /* If same type of route are installed, treat it as a implicit
2645 RNODE_FOREACH_RIB (rn
, same
)
2647 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2650 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2651 && same
->table
== rib
->table
2652 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2656 /* If this route is kernel route, set FIB flag to the route. */
2657 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2658 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2659 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2661 /* Link new rib to node.*/
2662 if (IS_ZEBRA_DEBUG_RIB
)
2664 char buf
[INET6_ADDRSTRLEN
];
2665 if (IS_ZEBRA_DEBUG_RIB
)
2667 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2668 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2670 rib
->vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2671 (void *)rib
, rib
->type
, (void *)same
);
2674 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2675 rib_dump ((struct prefix
*)p
, rib
);
2677 rib_addnode (rn
, rib
, 1);
2680 /* Free implicit route.*/
2683 rib_delnode (rn
, same
);
2687 route_unlock_node (rn
);
2692 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2693 int flags
, struct prefix
*p
, union g_addr
*gate
, ifindex_t ifindex
,
2696 struct route_table
*table
;
2697 struct route_node
*rn
;
2699 struct rib
*fib
= NULL
;
2700 struct rib
*same
= NULL
;
2701 struct nexthop
*nexthop
, *tnexthop
;
2703 char buf1
[PREFIX_STRLEN
];
2704 char buf2
[INET6_ADDRSTRLEN
];
2707 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2714 /* Lookup route node. */
2715 rn
= route_node_lookup (table
, p
);
2718 if (IS_ZEBRA_DEBUG_RIB
)
2719 zlog_debug ("%u:%s: doesn't exist in rib",
2720 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)));
2721 return ZEBRA_ERR_RTNOEXIST
;
2724 /* Lookup same type route. */
2725 RNODE_FOREACH_RIB (rn
, rib
)
2727 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2730 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2733 if (rib
->type
!= type
)
2735 if (rib
->instance
!= instance
)
2737 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2738 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2740 if (nexthop
->ifindex
!= ifindex
)
2745 route_unlock_node (rn
);
2746 route_unlock_node (rn
);
2752 /* Make sure that the route found has the same gateway. */
2760 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2761 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2762 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2771 /* If same type of route can't be found and this message is from
2775 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2776 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2778 if (IS_ZEBRA_DEBUG_RIB
)
2780 zlog_debug ("%u:%s: rn %p, rib %p (type %d) was deleted "
2781 "from kernel, adding",
2782 vrf_id
, prefix2str(p
, buf1
, INET6_ADDRSTRLEN
),
2783 rn
, fib
, fib
->type
);
2788 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2789 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2791 UNSET_FLAG (fib
->status
, RIB_ENTRY_SELECTED_FIB
);
2795 /* This means someone else, other than Zebra, has deleted
2796 * a Zebra router from the kernel. We will add it back */
2797 rib_install_kernel(rn
, fib
, 0);
2802 if (IS_ZEBRA_DEBUG_RIB
)
2805 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2806 "doesn't exist in rib",
2807 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2808 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
),
2812 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2813 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2817 route_unlock_node (rn
);
2818 return ZEBRA_ERR_RTNOEXIST
;
2823 rib_delnode (rn
, same
);
2825 route_unlock_node (rn
);
2832 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2833 u_short instance
, int flags
, struct prefix
*p
,
2834 union g_addr
*gate
, union g_addr
*src
, ifindex_t ifindex
,
2835 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2839 struct rib
*same
= NULL
;
2840 struct route_table
*table
;
2841 struct route_node
*rn
;
2842 struct nexthop
*nexthop
;
2845 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2849 /* Make sure mask is applied. */
2852 /* Set default distance by route type. */
2855 if ((unsigned)type
>= array_size(route_info
))
2858 distance
= route_info
[type
].distance
;
2860 /* iBGP distance is 200. */
2861 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2865 /* Lookup route node.*/
2866 rn
= route_node_get (table
, p
);
2868 /* If same type of route are installed, treat it as a implicit
2870 RNODE_FOREACH_RIB (rn
, rib
)
2872 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2875 if (rib
->type
!= type
)
2877 if (rib
->instance
!= instance
)
2879 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2884 /* Duplicate connected route comes in. */
2885 else if ((nexthop
= rib
->nexthop
) &&
2886 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2887 nexthop
->ifindex
== ifindex
&&
2888 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2895 /* Allocate new rib structure. */
2896 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2899 rib
->instance
= instance
;
2900 rib
->distance
= distance
;
2902 rib
->metric
= metric
;
2904 rib
->table
= table_id
;
2905 rib
->vrf_id
= vrf_id
;
2906 rib
->nexthop_num
= 0;
2907 rib
->uptime
= time (NULL
);
2909 /* Nexthop settings. */
2915 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2917 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2922 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2924 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2928 rib_nexthop_ifindex_add (rib
, ifindex
);
2930 /* If this route is kernel route, set FIB flag to the route. */
2931 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2932 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2933 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2935 /* Link new rib to node.*/
2936 if (IS_ZEBRA_DEBUG_RIB
)
2938 char buf
[INET6_ADDRSTRLEN
];
2939 if (IS_ZEBRA_DEBUG_RIB
)
2941 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2942 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2944 vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2945 (void *)rib
, rib
->type
, (void *)same
);
2948 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2951 rib_addnode (rn
, rib
, 1);
2953 /* Free implicit route.*/
2955 rib_delnode (rn
, same
);
2957 route_unlock_node (rn
);
2961 /* Schedule routes of a particular table (address-family) based on event. */
2963 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2965 struct route_node
*rn
;
2966 struct rib
*rib
, *next
;
2968 /* Walk all routes and queue for processing, if appropriate for
2969 * the trigger event.
2971 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2975 case RIB_UPDATE_IF_CHANGE
:
2976 /* Examine all routes that won't get processed by the protocol or
2977 * triggered by nexthop evaluation (NHT). This would be system,
2978 * kernel and certain static routes. Note that NHT will get
2979 * triggered upon an interface event as connected routes always
2980 * get queued for processing.
2982 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2984 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2985 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2986 rib
->type
== ZEBRA_ROUTE_BGP
)
2987 continue; /* protocol will handle. */
2988 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2991 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2992 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2993 nh
->type
== NEXTHOP_TYPE_IPV6
))
2996 /* If we only have nexthops to a gateway, NHT will
3007 case RIB_UPDATE_RMAP_CHANGE
:
3008 case RIB_UPDATE_OTHER
:
3009 /* Right now, examine all routes. Can restrict to a protocol in
3010 * some cases (TODO).
3012 if (rnode_to_ribs (rn
))
3022 /* RIB update function. */
3024 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
3026 struct route_table
*table
;
3028 /* Process routes of interested address-families. */
3029 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
3031 rib_update_table (table
, event
);
3033 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3035 rib_update_table (table
, event
);
3038 /* Remove all routes which comes from non main table. */
3040 rib_weed_table (struct route_table
*table
)
3042 struct route_node
*rn
;
3047 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3048 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3050 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3053 if (rib
->table
!= zebrad
.rtm_table_default
&&
3054 rib
->table
!= RT_TABLE_MAIN
)
3055 rib_delnode (rn
, rib
);
3059 /* Delete all routes from non main table. */
3061 rib_weed_tables (void)
3064 struct zebra_vrf
*zvrf
;
3066 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3067 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3069 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3070 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3074 /* Delete self installed routes after zebra is relaunched. */
3076 rib_sweep_table (struct route_table
*table
)
3078 struct route_node
*rn
;
3084 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3085 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3087 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3090 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3091 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3093 ret
= rib_uninstall_kernel (rn
, rib
);
3095 rib_delnode (rn
, rib
);
3100 /* Sweep all RIB tables. */
3102 rib_sweep_route (void)
3105 struct zebra_vrf
*zvrf
;
3107 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3108 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3110 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3111 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3115 /* Remove specific by protocol routes from 'table'. */
3116 static unsigned long
3117 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
3119 struct route_node
*rn
;
3122 unsigned long n
= 0;
3125 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3126 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3128 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3130 if (rib
->type
== proto
&& rib
->instance
== instance
)
3132 rib_delnode (rn
, rib
);
3140 /* Remove specific by protocol routes. */
3142 rib_score_proto (u_char proto
, u_short instance
)
3145 struct zebra_vrf
*zvrf
;
3146 unsigned long cnt
= 0;
3148 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3149 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3150 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3151 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3156 /* Close RIB and clean up kernel routes. */
3158 rib_close_table (struct route_table
*table
)
3160 struct route_node
*rn
;
3161 rib_table_info_t
*info
= table
->info
;
3165 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3166 RNODE_FOREACH_RIB (rn
, rib
)
3168 if (!CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
3171 if (info
->safi
== SAFI_UNICAST
)
3172 zfpm_trigger_update (rn
, NULL
);
3174 if (! RIB_SYSTEM_ROUTE (rib
))
3175 rib_uninstall_kernel (rn
, rib
);
3179 /* Close all RIB tables. */
3184 struct zebra_vrf
*zvrf
;
3185 struct listnode
*node
;
3186 struct interface
*ifp
;
3189 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3191 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3193 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3194 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3196 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter
), node
, ifp
))
3197 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
3200 /* If we do multiple tables per vrf, need to move this to loop above */
3201 zvrf
= vrf_info_lookup (VRF_DEFAULT
);
3203 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
3205 if (zvrf
->other_table
[AFI_IP
][table_id
])
3206 rib_close_table (zvrf
->other_table
[AFI_IP
][table_id
]);
3208 if (zvrf
->other_table
[AFI_IP6
][table_id
])
3209 rib_close_table (zvrf
->other_table
[AFI_IP6
][table_id
]);
3212 zebra_mpls_close_tables(zvrf
);
3216 /* Routing information base initialize. */
3220 rib_queue_init (&zebrad
);
3226 * Get the first vrf id that is greater than the given vrf id if any.
3228 * Returns TRUE if a vrf id was found, FALSE otherwise.
3231 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3233 vrf_iter_t iter
= vrf_iterator (vrf_id
);
3234 struct zebra_vrf
*zvrf
= vrf_iter2info (iter
);
3236 /* The same one ? Then find out the next. */
3237 if (zvrf
&& (zvrf
->vrf_id
== vrf_id
))
3238 zvrf
= vrf_iter2info (vrf_next (iter
));
3242 *next_id_p
= zvrf
->vrf_id
;
3250 * rib_tables_iter_next
3252 * Returns the next table in the iteration.
3254 struct route_table
*
3255 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3257 struct route_table
*table
;
3260 * Array that helps us go over all AFI/SAFI combinations via one
3267 { AFI_IP
, SAFI_UNICAST
},
3268 { AFI_IP
, SAFI_MULTICAST
},
3269 { AFI_IP6
, SAFI_UNICAST
},
3270 { AFI_IP6
, SAFI_MULTICAST
},
3275 switch (iter
->state
)
3278 case RIB_TABLES_ITER_S_INIT
:
3279 iter
->vrf_id
= VRF_DEFAULT
;
3280 iter
->afi_safi_ix
= -1;
3284 case RIB_TABLES_ITER_S_ITERATING
:
3285 iter
->afi_safi_ix
++;
3289 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3291 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3292 afi_safis
[iter
->afi_safi_ix
].safi
,
3297 iter
->afi_safi_ix
++;
3301 * Found another table in this vrf.
3307 * Done with all tables in the current vrf, go to the next
3310 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3313 iter
->afi_safi_ix
= 0;
3318 case RIB_TABLES_ITER_S_DONE
:
3323 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3325 iter
->state
= RIB_TABLES_ITER_S_DONE
;