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"
31 #include "sockunion.h"
34 #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 route_distance (int type
)
125 if ((unsigned)type
>= array_size(route_info
))
128 distance
= route_info
[type
].distance
;
134 is_zebra_valid_kernel_table(u_int32_t table_id
)
136 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
140 if ((table_id
== RT_TABLE_UNSPEC
) ||
141 (table_id
== RT_TABLE_LOCAL
) ||
142 (table_id
== RT_TABLE_COMPAT
))
150 is_zebra_main_routing_table(u_int32_t table_id
)
152 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
158 zebra_check_addr (struct prefix
*p
)
160 if (p
->family
== AF_INET
)
164 addr
= p
->u
.prefix4
.s_addr
;
167 if (IPV4_NET127 (addr
)
169 || IPV4_LINKLOCAL(addr
))
172 if (p
->family
== AF_INET6
)
174 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
176 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
182 /* Add nexthop to the end of a rib node's nexthop list */
184 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
186 nexthop_add(&rib
->nexthop
, nexthop
);
193 * copy_nexthop - copy a nexthop to the rib structure.
196 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
198 struct nexthop
*nexthop
;
200 nexthop
= nexthop_new();
201 nexthop
->flags
= nh
->flags
;
202 nexthop
->type
= nh
->type
;
203 nexthop
->ifindex
= nh
->ifindex
;
204 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
205 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
207 nexthop_add_labels (nexthop
, nh
->nh_label_type
, nh
->nh_label
->num_labels
,
208 &nh
->nh_label
->label
[0]);
209 rib_nexthop_add(rib
, nexthop
);
210 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
211 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
214 /* Delete specified nexthop from the list. */
216 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
219 nexthop
->next
->prev
= nexthop
->prev
;
221 nexthop
->prev
->next
= nexthop
->next
;
223 rib
->nexthop
= nexthop
->next
;
230 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
232 struct nexthop
*nexthop
;
234 nexthop
= nexthop_new();
235 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
236 nexthop
->ifindex
= ifindex
;
238 rib_nexthop_add (rib
, nexthop
);
244 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
246 struct nexthop
*nexthop
;
248 nexthop
= nexthop_new();
249 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
250 nexthop
->gate
.ipv4
= *ipv4
;
252 nexthop
->src
.ipv4
= *src
;
254 rib_nexthop_add (rib
, nexthop
);
260 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
261 struct in_addr
*src
, ifindex_t ifindex
)
263 struct nexthop
*nexthop
;
264 struct interface
*ifp
;
266 nexthop
= nexthop_new();
267 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
268 nexthop
->gate
.ipv4
= *ipv4
;
270 nexthop
->src
.ipv4
= *src
;
271 nexthop
->ifindex
= ifindex
;
272 ifp
= if_lookup_by_index (nexthop
->ifindex
);
273 /*Pending: need to think if null ifp here is ok during bootup?
274 There was a crash because ifp here was coming to be NULL */
276 if (connected_is_unnumbered(ifp
)) {
277 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
280 rib_nexthop_add (rib
, nexthop
);
286 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
288 struct nexthop
*nexthop
;
290 nexthop
= nexthop_new();
291 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
292 nexthop
->gate
.ipv6
= *ipv6
;
294 rib_nexthop_add (rib
, nexthop
);
300 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
303 struct nexthop
*nexthop
;
305 nexthop
= nexthop_new();
306 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
307 nexthop
->gate
.ipv6
= *ipv6
;
308 nexthop
->ifindex
= ifindex
;
310 rib_nexthop_add (rib
, nexthop
);
316 rib_nexthop_blackhole_add (struct rib
*rib
)
318 struct nexthop
*nexthop
;
320 nexthop
= nexthop_new();
321 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
322 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
324 rib_nexthop_add (rib
, nexthop
);
329 /* This method checks whether a recursive nexthop has at
330 * least one resolved nexthop in the fib.
333 nexthop_has_fib_child(struct nexthop
*nexthop
)
337 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
340 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
341 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
347 /* If force flag is not set, do not modify falgs at all for uninstall
348 the route from FIB. */
350 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
351 struct route_node
*top
)
353 struct prefix_ipv4 p
;
354 struct route_table
*table
;
355 struct route_node
*rn
;
358 struct nexthop
*newhop
, *tnewhop
;
359 struct nexthop
*resolved_hop
;
361 struct interface
*ifp
;
363 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
364 nexthop
->ifindex
= 0;
368 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
369 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
370 nexthops_free(nexthop
->resolved
);
371 nexthop
->resolved
= NULL
;
372 rib
->nexthop_mtu
= 0;
375 /* Skip nexthops that have been filtered out due to route-map */
376 /* The nexthops are specific to this route and so the same */
377 /* nexthop for a different route may not have this flag set */
378 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
382 * Check to see if we should trust the passed in information
383 * for UNNUMBERED interfaces as that we won't find the GW
384 * address in the routing table.
386 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
388 ifp
= if_lookup_by_index (nexthop
->ifindex
);
389 if (ifp
&& connected_is_unnumbered(ifp
))
391 if (if_is_operative(ifp
))
400 /* Make lookup prefix. */
401 memset (&p
, 0, sizeof (struct prefix_ipv4
));
403 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
404 p
.prefix
= nexthop
->gate
.ipv4
;
407 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, rib
->vrf_id
);
411 rn
= route_node_match (table
, (struct prefix
*) &p
);
414 route_unlock_node (rn
);
416 /* If lookup self prefix return immediately. */
420 /* Pick up selected route. */
421 /* However, do not resolve over default route unless explicitly allowed. */
422 if (is_default_prefix (&rn
->p
) &&
423 !nh_resolve_via_default (p
.family
))
426 RNODE_FOREACH_RIB (rn
, match
)
428 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
431 /* if the next hop is imported from another table, skip it */
432 if (match
->type
== ZEBRA_ROUTE_TABLE
)
434 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
438 /* If there is no selected route or matched route is EGP, go up
444 } while (rn
&& rn
->info
== NULL
);
446 route_lock_node (rn
);
450 /* If the longest prefix match for the nexthop yields
451 * a blackhole, mark it as inactive. */
452 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
453 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
456 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
458 /* Directly point connected route. */
459 newhop
= match
->nexthop
;
460 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
461 nexthop
->ifindex
= newhop
->ifindex
;
465 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
468 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
469 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
470 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
474 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
475 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
477 resolved_hop
= nexthop_new();
478 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
479 /* If the resolving route specifies a gateway, use it */
480 if (newhop
->type
== NEXTHOP_TYPE_IPV4
481 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
483 resolved_hop
->type
= newhop
->type
;
484 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
488 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
489 resolved_hop
->ifindex
= newhop
->ifindex
;
490 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
491 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
495 /* If the resolving route is an interface route,
496 * it means the gateway we are looking up is connected
497 * to that interface. (The actual network is _not_ onlink).
498 * Therefore, the resolved route should have the original
499 * gateway as nexthop as it is directly connected.
501 * On Linux, we have to set the onlink netlink flag because
502 * otherwise, the kernel won't accept the route. */
503 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
505 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
506 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
507 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
508 resolved_hop
->ifindex
= newhop
->ifindex
;
511 nexthop_add(&nexthop
->resolved
, resolved_hop
);
517 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
520 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
521 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
525 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
527 resolved_hop
= nexthop_new();
528 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
529 /* If the resolving route specifies a gateway, use it */
530 if (newhop
->type
== NEXTHOP_TYPE_IPV4
531 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
533 resolved_hop
->type
= newhop
->type
;
534 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
538 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
539 resolved_hop
->ifindex
= newhop
->ifindex
;
540 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
541 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
545 /* If the resolving route is an interface route,
546 * it means the gateway we are looking up is connected
547 * to that interface. (The actual network is _not_ onlink).
548 * Therefore, the resolved route should have the original
549 * gateway as nexthop as it is directly connected.
551 * On Linux, we have to set the onlink netlink flag because
552 * otherwise, the kernel won't accept the route.
554 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
556 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
557 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
558 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
559 resolved_hop
->ifindex
= newhop
->ifindex
;
562 nexthop_add(&nexthop
->resolved
, resolved_hop
);
567 rib
->nexthop_mtu
= match
->mtu
;
579 /* If force flag is not set, do not modify falgs at all for uninstall
580 the route from FIB. */
582 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
583 struct route_node
*top
)
585 struct prefix_ipv6 p
;
586 struct route_table
*table
;
587 struct route_node
*rn
;
590 struct nexthop
*newhop
, *tnewhop
;
592 struct nexthop
*resolved_hop
;
594 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
595 nexthop
->ifindex
= 0;
599 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
600 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, nexthop
->resolved
, top
);
601 nexthops_free(nexthop
->resolved
);
602 nexthop
->resolved
= NULL
;
605 /* Skip nexthops that have been filtered out due to route-map */
606 /* The nexthops are specific to this route and so the same */
607 /* nexthop for a different route may not have this flag set */
608 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
611 /* Make lookup prefix. */
612 memset (&p
, 0, sizeof (struct prefix_ipv6
));
614 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
615 p
.prefix
= nexthop
->gate
.ipv6
;
618 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, rib
->vrf_id
);
622 rn
= route_node_match (table
, (struct prefix
*) &p
);
625 route_unlock_node (rn
);
627 /* If lookup self prefix return immediately. */
631 /* Pick up selected route. */
632 /* However, do not resolve over default route unless explicitly allowed. */
633 if (is_default_prefix (&rn
->p
) &&
634 !nh_resolve_via_default (p
.family
))
637 RNODE_FOREACH_RIB (rn
, match
)
639 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
641 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
645 /* If there is no selected route or matched route is EGP, go up
651 } while (rn
&& rn
->info
== NULL
);
653 route_lock_node (rn
);
657 /* If the longest prefix match for the nexthop yields
658 * a blackhole, mark it as inactive. */
659 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
660 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
663 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
665 /* Directly point connected route. */
666 newhop
= match
->nexthop
;
668 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
669 nexthop
->ifindex
= newhop
->ifindex
;
673 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
676 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
677 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
678 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
682 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
683 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
685 resolved_hop
= nexthop_new();
686 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
687 /* See nexthop_active_ipv4 for a description how the
688 * resolved nexthop is constructed. */
689 if (newhop
->type
== NEXTHOP_TYPE_IPV6
690 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
692 resolved_hop
->type
= newhop
->type
;
693 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
697 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
698 resolved_hop
->ifindex
= newhop
->ifindex
;
702 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
704 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
705 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
706 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
707 resolved_hop
->ifindex
= newhop
->ifindex
;
710 nexthop_add(&nexthop
->resolved
, resolved_hop
);
716 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
719 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
720 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
724 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
726 resolved_hop
= nexthop_new();
727 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
728 /* See nexthop_active_ipv4 for a description how the
729 * resolved nexthop is constructed. */
730 if (newhop
->type
== NEXTHOP_TYPE_IPV6
731 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
733 resolved_hop
->type
= newhop
->type
;
734 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
738 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
739 resolved_hop
->ifindex
= newhop
->ifindex
;
743 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
745 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
746 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
747 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
748 resolved_hop
->ifindex
= newhop
->ifindex
;
751 nexthop_add(&nexthop
->resolved
, resolved_hop
);
767 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
768 union g_addr
*addr
, struct route_node
**rn_out
)
771 struct route_table
*table
;
772 struct route_node
*rn
;
774 struct nexthop
*newhop
, *tnewhop
;
778 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
782 memset (&p
, 0, sizeof (struct prefix
));
786 p
.u
.prefix4
= addr
->ipv4
;
787 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
791 p
.u
.prefix6
= addr
->ipv6
;
792 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
795 rn
= route_node_match (table
, (struct prefix
*) &p
);
799 route_unlock_node (rn
);
801 /* Pick up selected route. */
802 RNODE_FOREACH_RIB (rn
, match
)
804 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
806 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
810 /* If there is no selected route or matched route is EGP, go up
816 } while (rn
&& rn
->info
== NULL
);
818 route_lock_node (rn
);
822 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
825 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
826 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
844 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
846 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
847 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
848 union g_addr gaddr
= { .ipv4
= addr
};
850 switch (ipv4_multicast_mode
)
852 case MCAST_MRIB_ONLY
:
853 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
854 case MCAST_URIB_ONLY
:
855 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
856 case MCAST_NO_CONFIG
:
857 case MCAST_MIX_MRIB_FIRST
:
858 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
860 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
862 case MCAST_MIX_DISTANCE
:
863 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
864 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
866 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
872 case MCAST_MIX_PFXLEN
:
873 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
874 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
876 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
885 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
887 if (IS_ZEBRA_DEBUG_RIB
)
890 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
892 zlog_debug("%s: %s: found %s, using %s",
894 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
895 urib
? "URIB" : "nothing",
896 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
902 multicast_mode_ipv4_set (enum multicast_mode mode
)
904 if (IS_ZEBRA_DEBUG_RIB
)
905 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
906 ipv4_multicast_mode
= mode
;
910 multicast_mode_ipv4_get (void)
912 return ipv4_multicast_mode
;
916 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
918 struct route_table
*table
;
919 struct route_node
*rn
;
921 struct nexthop
*nexthop
, *tnexthop
;
925 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
929 rn
= route_node_lookup (table
, (struct prefix
*) p
);
931 /* No route for this prefix. */
936 route_unlock_node (rn
);
938 RNODE_FOREACH_RIB (rn
, match
)
940 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
942 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
949 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
952 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
953 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
960 * This clone function, unlike its original rib_lookup_ipv4(), checks
961 * if specified IPv4 route record (prefix/mask -> gate) exists in
962 * the whole RIB and has RIB_ENTRY_SELECTED_FIB set.
966 * 0: exact match found
967 * 1: a match was found with a different gate
968 * 2: connected route found
969 * 3: no matches found
972 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
975 struct route_table
*table
;
976 struct route_node
*rn
;
978 struct nexthop
*nexthop
, *tnexthop
;
983 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
985 return ZEBRA_RIB_LOOKUP_ERROR
;
987 /* Scan the RIB table for exactly matching RIB entry. */
988 rn
= route_node_lookup (table
, (struct prefix
*) p
);
990 /* No route for this prefix. */
992 return ZEBRA_RIB_NOTFOUND
;
995 route_unlock_node (rn
);
997 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
998 RNODE_FOREACH_RIB (rn
, match
)
1000 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1002 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
1006 /* None such found :( */
1008 return ZEBRA_RIB_NOTFOUND
;
1010 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1011 return ZEBRA_RIB_FOUND_CONNECTED
;
1013 /* Ok, we have a cood candidate, let's check it's nexthop list... */
1014 nexthops_active
= 0;
1015 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
1016 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1018 nexthops_active
= 1;
1019 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
1020 return ZEBRA_RIB_FOUND_EXACT
;
1021 if (IS_ZEBRA_DEBUG_RIB
)
1023 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
1024 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
1025 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
1026 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
1027 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
1031 if (nexthops_active
)
1032 return ZEBRA_RIB_FOUND_NOGATE
;
1034 return ZEBRA_RIB_NOTFOUND
;
1037 #define RIB_SYSTEM_ROUTE(R) \
1038 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1040 /* This function verifies reachability of one given nexthop, which can be
1041 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1042 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1043 * nexthop->ifindex will be updated appropriately as well.
1044 * An existing route map can turn (otherwise active) nexthop into inactive, but
1047 * The return value is the final value of 'ACTIVE' flag.
1051 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
1052 struct nexthop
*nexthop
, int set
)
1054 rib_table_info_t
*info
= rn
->table
->info
;
1055 struct interface
*ifp
;
1056 route_map_result_t ret
= RMAP_MATCH
;
1058 char buf
[INET6_ADDRSTRLEN
+1];
1060 if (rn
->p
.family
== AF_INET
)
1062 else if (rn
->p
.family
== AF_INET6
)
1066 switch (nexthop
->type
)
1068 case NEXTHOP_TYPE_IFINDEX
:
1069 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1070 if (ifp
&& if_is_operative(ifp
))
1071 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1073 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1075 case NEXTHOP_TYPE_IPV4
:
1076 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1078 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
1079 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1081 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1083 case NEXTHOP_TYPE_IPV6
:
1085 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1086 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1088 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1090 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1091 /* RFC 5549, v4 prefix with v6 NH */
1092 if (rn
->p
.family
!= AF_INET
)
1094 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1096 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1097 if (ifp
&& if_is_operative(ifp
))
1098 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1100 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1104 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1105 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1107 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1110 case NEXTHOP_TYPE_BLACKHOLE
:
1111 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1116 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1119 /* XXX: What exactly do those checks do? Do we support
1120 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1121 if (RIB_SYSTEM_ROUTE(rib
) ||
1122 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1123 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1124 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1126 /* The original code didn't determine the family correctly
1127 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1128 * from the rib_table_info in those cases.
1129 * Possibly it may be better to use only the rib_table_info
1135 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1137 /* It'll get set if required inside */
1138 ret
= zebra_route_map_check(family
, rib
->type
, &rn
->p
, nexthop
, rib
->vrf_id
,
1140 if (ret
== RMAP_DENYMATCH
)
1142 if (IS_ZEBRA_DEBUG_RIB
)
1144 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1145 zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map",
1146 rib
->vrf_id
, buf
, rn
->p
.prefixlen
,
1147 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1149 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1151 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1154 /* Iterate over all nexthops of the given RIB entry and refresh their
1155 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1156 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1157 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1158 * transparently passed to nexthop_active_check().
1160 * Return value is the new number of active nexthops.
1164 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1166 struct nexthop
*nexthop
;
1167 union g_addr prev_src
;
1168 unsigned int prev_active
, new_active
, old_num_nh
;
1169 ifindex_t prev_index
;
1170 old_num_nh
= rib
->nexthop_active_num
;
1172 rib
->nexthop_active_num
= 0;
1173 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1175 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1177 /* No protocol daemon provides src and so we're skipping tracking it */
1178 prev_src
= nexthop
->rmap_src
;
1179 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1180 prev_index
= nexthop
->ifindex
;
1181 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1182 rib
->nexthop_active_num
++;
1183 /* Don't allow src setting on IPv6 addr for now */
1184 if (prev_active
!= new_active
||
1185 prev_index
!= nexthop
->ifindex
||
1186 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1187 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1188 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1189 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1190 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1191 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1193 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1194 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1198 if (old_num_nh
!= rib
->nexthop_active_num
)
1199 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1201 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1203 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1206 return rib
->nexthop_active_num
;
1211 /* Update flag indicates whether this is a "replace" or not. Currently, this
1212 * is only used for IPv4.
1215 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, struct rib
*old
)
1218 struct nexthop
*nexthop
, *tnexthop
;
1219 rib_table_info_t
*info
= rn
->table
->info
;
1222 if (info
->safi
!= SAFI_UNICAST
)
1224 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1225 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1230 * Make sure we update the FPM any time we send new information to
1233 zfpm_trigger_update (rn
, "installing in kernel");
1234 ret
= kernel_route_rib (&rn
->p
, old
, rib
);
1236 /* If install succeeds, update FIB flag for nexthops. */
1239 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1241 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1244 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1245 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1247 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1254 /* Uninstall the route from kernel. */
1256 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1259 struct nexthop
*nexthop
, *tnexthop
;
1260 rib_table_info_t
*info
= rn
->table
->info
;
1263 if (info
->safi
!= SAFI_UNICAST
)
1265 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1266 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1271 * Make sure we update the FPM any time we send new information to
1274 zfpm_trigger_update (rn
, "uninstalling from kernel");
1275 ret
= kernel_route_rib (&rn
->p
, rib
, NULL
);
1277 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1278 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1283 /* Uninstall the route from kernel. */
1285 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1287 rib_table_info_t
*info
= rn
->table
->info
;
1289 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1291 if (info
->safi
== SAFI_UNICAST
)
1292 zfpm_trigger_update (rn
, "rib_uninstall");
1294 if (! RIB_SYSTEM_ROUTE (rib
))
1295 rib_uninstall_kernel (rn
, rib
);
1296 UNSET_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
);
1299 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1301 redistribute_delete (&rn
->p
, rib
);
1302 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1307 * rib_can_delete_dest
1309 * Returns TRUE if the given dest can be deleted from the table.
1312 rib_can_delete_dest (rib_dest_t
*dest
)
1320 * Don't delete the dest if we have to update the FPM about this
1323 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1324 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1333 * Garbage collect the rib dest corresponding to the given route node
1336 * Returns TRUE if the dest was deleted, FALSE otherwise.
1339 rib_gc_dest (struct route_node
*rn
)
1342 struct zebra_vrf
*zvrf
;
1344 dest
= rib_dest_from_rnode (rn
);
1348 if (!rib_can_delete_dest (dest
))
1351 zvrf
= rib_dest_vrf (dest
);
1352 if (IS_ZEBRA_DEBUG_RIB
)
1353 rnode_debug (rn
, zvrf_id (zvrf
), "removing dest from table");
1356 XFREE (MTYPE_RIB_DEST
, dest
);
1360 * Release the one reference that we keep on the route node.
1362 route_unlock_node (rn
);
1367 rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1370 char buf
[INET6_ADDRSTRLEN
];
1372 zfpm_trigger_update (rn
, "new route selected");
1374 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1375 if (!nexthop_active_update (rn
, new, 1))
1377 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1381 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1382 if (IS_ZEBRA_DEBUG_RIB
)
1384 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1385 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1386 zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
, rn
, new, new->type
);
1389 if (!RIB_SYSTEM_ROUTE (new))
1391 if (rib_install_kernel (rn
, new, NULL
))
1393 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1394 zlog_warn ("%u:%s/%d: Route install failed",
1395 zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
);
1399 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1403 rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1406 char buf
[INET6_ADDRSTRLEN
];
1408 zfpm_trigger_update (rn
, "removing existing route");
1410 /* Uninstall from kernel. */
1411 if (IS_ZEBRA_DEBUG_RIB
)
1413 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1414 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1415 zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
, rn
, old
, old
->type
);
1418 if (!RIB_SYSTEM_ROUTE (old
))
1419 rib_uninstall_kernel (rn
, old
);
1421 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1423 /* Update nexthop for route, reset changed flag. */
1424 nexthop_active_update (rn
, old
, 1);
1425 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1429 rib_process_update_fib (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1430 struct rib
*old
, struct rib
*new)
1432 char buf
[INET6_ADDRSTRLEN
];
1433 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1438 if (IS_ZEBRA_DEBUG_RIB
)
1439 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1442 * We have to install or update if a new route has been selected or
1443 * something has changed.
1446 CHECK_FLAG (new->status
, RIB_ENTRY_CHANGED
))
1448 zfpm_trigger_update (rn
, "updating existing route");
1450 /* Update the nexthop; we could determine here that nexthop is inactive. */
1451 if (nexthop_active_update (rn
, new, 1))
1454 /* If nexthop is active, install the selected route, if appropriate. If
1455 * the install succeeds, cleanup flags for prior route, if different from
1460 if (IS_ZEBRA_DEBUG_RIB
)
1463 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1464 "old %p (type %d)", zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
,
1465 rn
, new, new->type
, old
, old
->type
);
1467 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1468 zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
, rn
, new, new->type
);
1470 /* Non-system route should be installed. */
1471 if (!RIB_SYSTEM_ROUTE (new))
1473 if (rib_install_kernel (rn
, new, old
))
1476 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1477 zlog_warn ("%u:%s/%d: Route install failed",
1478 zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
);
1482 /* If install succeeded or system route, cleanup flags for prior route. */
1483 if (installed
&& new != old
)
1485 if (RIB_SYSTEM_ROUTE(new))
1487 if (!RIB_SYSTEM_ROUTE (old
))
1488 rib_uninstall_kernel (rn
, old
);
1492 for (nexthop
= old
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1493 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1497 /* Update for redistribution. */
1499 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1503 * If nexthop for selected route is not active or install failed, we
1504 * may need to uninstall and delete for redistribution.
1506 if (!nh_active
|| !installed
)
1508 if (IS_ZEBRA_DEBUG_RIB
)
1511 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1512 "old %p (type %d) - %s", zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
,
1513 rn
, new, new->type
, old
, old
->type
,
1514 nh_active
? "install failed" : "nexthop inactive");
1516 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) - %s",
1517 zvrf_id (zvrf
), buf
, rn
->p
.prefixlen
, rn
, new, new->type
,
1518 nh_active
? "install failed" : "nexthop inactive");
1521 if (!RIB_SYSTEM_ROUTE (old
))
1522 rib_uninstall_kernel (rn
, old
);
1523 UNSET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1529 * Same route selected; check if in the FIB and if not, re-install. This
1530 * is housekeeping code to deal with race conditions in kernel with linux
1531 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1534 if (!RIB_SYSTEM_ROUTE (new))
1538 for (ALL_NEXTHOPS_RO(new->nexthop
, nexthop
, tnexthop
, recursing
))
1539 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1545 rib_install_kernel (rn
, new, NULL
);
1549 /* Update prior route. */
1552 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1554 /* Set real nexthop. */
1555 nexthop_active_update (rn
, old
, 1);
1556 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1559 /* Clear changed flag. */
1560 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1563 /* Check if 'alternate' RIB entry is better than 'current'. */
1565 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1567 if (current
== NULL
)
1570 /* filter route selection in following order:
1571 * - connected beats other types
1572 * - lower distance beats higher
1573 * - lower metric beats higher for equal distance
1574 * - last, hence oldest, route wins tie break.
1577 /* Connected routes. Pick the last connected
1578 * route of the set of lowest metric connected routes.
1580 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1582 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1583 || alternate
->metric
<= current
->metric
)
1589 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1592 /* higher distance loses */
1593 if (alternate
->distance
< current
->distance
)
1595 if (current
->distance
< alternate
->distance
)
1598 /* metric tie-breaks equal distance */
1599 if (alternate
->metric
<= current
->metric
)
1605 /* Core function for processing routing information base. */
1607 rib_process (struct route_node
*rn
)
1611 struct rib
*old_selected
= NULL
;
1612 struct rib
*new_selected
= NULL
;
1613 struct rib
*old_fib
= NULL
;
1614 struct rib
*new_fib
= NULL
;
1615 struct rib
*best
= NULL
;
1616 char buf
[INET6_ADDRSTRLEN
];
1618 struct zebra_vrf
*zvrf
= NULL
;
1619 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1623 dest
= rib_dest_from_rnode (rn
);
1626 zvrf
= rib_dest_vrf (dest
);
1627 vrf_id
= zvrf_id (zvrf
);
1630 if (IS_ZEBRA_DEBUG_RIB
)
1631 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1633 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1634 zlog_debug ("%u:%s/%d: Processing rn %p", vrf_id
, buf
, rn
->p
.prefixlen
, rn
);
1636 RNODE_FOREACH_RIB (rn
, rib
)
1638 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1639 zlog_debug ("%u:%s/%d: Examine rib %p (type %d) status %x flags %x "
1640 "dist %d metric %d",
1641 vrf_id
, buf
, rn
->p
.prefixlen
, rib
, rib
->type
, rib
->status
,
1642 rib
->flags
, rib
->distance
, rib
->metric
);
1644 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1646 /* Currently selected rib. */
1647 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1649 assert (old_selected
== NULL
);
1652 /* Currently in fib */
1653 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1655 assert (old_fib
== NULL
);
1659 /* Skip deleted entries from selection */
1660 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1663 /* Skip unreachable nexthop. */
1664 /* This first call to nexthop_active_update is merely to determine if
1665 * there's any change to nexthops associated with this RIB entry. Now,
1666 * rib_process() can be invoked due to an external event such as link
1667 * down or due to next-hop-tracking evaluation. In the latter case,
1668 * a decision has already been made that the NHs have changed. So, no
1669 * need to invoke a potentially expensive call again. Further, since
1670 * the change might be in a recursive NH which is not caught in
1671 * the nexthop_active_update() code. Thus, we might miss changes to
1674 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1675 ! nexthop_active_update (rn
, rib
, 0))
1677 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1679 /* XXX: HERE BE DRAGONS!!!!!
1680 * In all honesty, I have not yet figured out what this part
1681 * does or why the RIB_ENTRY_CHANGED test above is correct
1682 * or why we need to delete a route here, and also not whether
1683 * this concerns both selected and fib route, or only selected
1685 /* This entry was denied by the 'ip protocol table' route-map, we
1686 * need to delete it */
1687 if (rib
!= old_selected
)
1689 if (IS_ZEBRA_DEBUG_RIB
)
1690 zlog_debug ("%s: %s/%d: imported via import-table but denied "
1691 "by the ip protocol table route-map",
1692 __func__
, buf
, rn
->p
.prefixlen
);
1693 rib_unlink (rn
, rib
);
1696 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1702 /* Infinite distance. */
1703 if (rib
->distance
== DISTANCE_INFINITY
)
1705 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1709 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
))
1711 best
= rib_choose_best(new_fib
, rib
);
1712 if (new_fib
&& best
!= new_fib
)
1713 UNSET_FLAG (new_fib
->status
, RIB_ENTRY_CHANGED
);
1718 best
= rib_choose_best(new_selected
, rib
);
1719 if (new_selected
&& best
!= new_selected
)
1720 UNSET_FLAG (new_selected
->status
, RIB_ENTRY_CHANGED
);
1721 new_selected
= best
;
1724 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1725 } /* RNODE_FOREACH_RIB */
1727 /* If no FIB override route, use the selected route also for FIB */
1728 if (new_fib
== NULL
)
1729 new_fib
= new_selected
;
1731 /* After the cycle is finished, the following pointers will be set:
1732 * old_selected --- RIB entry currently having SELECTED
1733 * new_selected --- RIB entry that is newly SELECTED
1734 * old_fib --- RIB entry currently in kernel FIB
1735 * new_fib --- RIB entry that is newly to be in kernel FIB
1737 * new_selected will get SELECTED flag, and is going to be redistributed
1738 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1741 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1743 zlog_debug ("%u:%s/%d: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1744 vrf_id
, buf
, rn
->p
.prefixlen
,
1745 (void *)old_selected
,
1746 (void *)new_selected
,
1751 /* Buffer RIB_ENTRY_CHANGED here, because it will get cleared if
1752 * fib == selected */
1753 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1756 /* Update fib according to selection results */
1757 if (new_fib
&& old_fib
)
1758 rib_process_update_fib (zvrf
, rn
, old_fib
, new_fib
);
1760 rib_process_add_fib (zvrf
, rn
, new_fib
);
1762 rib_process_del_fib (zvrf
, rn
, old_fib
);
1764 /* Redistribute SELECTED entry */
1765 if (old_selected
!= new_selected
|| selected_changed
)
1767 struct nexthop
*nexthop
, *tnexthop
;
1770 /* Check if we have a FIB route for the destination, otherwise,
1771 * don't redistribute it */
1772 for (ALL_NEXTHOPS_RO(new_fib
? new_fib
->nexthop
: NULL
, nexthop
,
1773 tnexthop
, recursing
))
1775 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1781 new_selected
= NULL
;
1783 if (new_selected
&& new_selected
!= new_fib
)
1785 nexthop_active_update(rn
, new_selected
, 1);
1786 UNSET_FLAG(new_selected
->status
, RIB_ENTRY_CHANGED
);
1792 redistribute_delete(&rn
->p
, old_selected
);
1793 if (old_selected
!= new_selected
)
1794 UNSET_FLAG (old_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1799 /* Install new or replace existing redistributed entry */
1800 SET_FLAG (new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1801 redistribute_update (&rn
->p
, new_selected
, old_selected
);
1806 if (select
&& select
== fib
)
1808 if (IS_ZEBRA_DEBUG_RIB
)
1809 rnode_debug (rn
, vrf_id
, "Updating existing route, select %p, fib %p",
1810 (void *)select
, (void *)fib
);
1811 if (CHECK_FLAG (select
->status
, RIB_ENTRY_CHANGED
))
1813 if (info
->safi
== SAFI_UNICAST
)
1814 zfpm_trigger_update (rn
, "updating existing route");
1816 /* Set real nexthop. */
1817 /* Need to check if any NHs are active to clear the
1820 if (nexthop_active_update (rn
, select
, 1))
1822 if (IS_ZEBRA_DEBUG_RIB
)
1823 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1824 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1825 if (! RIB_SYSTEM_ROUTE (select
))
1827 /* Clear FIB flag if performing a replace, will get set again
1828 * as part of install.
1830 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1831 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1832 rib_install_kernel (rn
, select
, 1);
1835 /* assuming that the receiver knows how to dedup */
1836 redistribute_update (&rn
->p
, select
, NULL
);
1840 if (IS_ZEBRA_DEBUG_RIB
)
1841 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1842 "- nexthop inactive",
1843 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1845 /* Withdraw unreachable redistribute route */
1846 redistribute_delete(&rn
->p
, select
);
1848 /* Do the uninstall here, if not done earlier. */
1849 if (! RIB_SYSTEM_ROUTE (select
))
1850 rib_uninstall_kernel (rn
, select
);
1851 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1853 UNSET_FLAG (select
->status
, RIB_ENTRY_CHANGED
);
1855 else if (! RIB_SYSTEM_ROUTE (select
))
1857 /* Housekeeping code to deal with
1858 race conditions in kernel with linux
1859 netlink reporting interface up before IPv4 or IPv6 protocol
1860 is ready to add routes.
1861 This makes sure the routes are IN the kernel.
1864 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1865 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1871 rib_install_kernel (rn
, select
, 0);
1876 /* At this point we either haven't found the best RIB entry or it is
1877 * different from what we currently intend to flag with SELECTED. In both
1878 * cases, if a RIB block is present in FIB, it should be withdrawn.
1882 if (IS_ZEBRA_DEBUG_RIB
)
1883 rnode_debug (rn
, vrf_id
, "Removing existing route, fib %p", (void *)fib
);
1885 if (info
->safi
== SAFI_UNICAST
)
1886 zfpm_trigger_update (rn
, "removing existing route");
1888 /* If there's no route to replace this with, withdraw redistribute and
1889 * uninstall from kernel.
1893 if (IS_ZEBRA_DEBUG_RIB
)
1894 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1895 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1897 redistribute_delete(&rn
->p
, fib
);
1898 if (! RIB_SYSTEM_ROUTE (fib
))
1899 rib_uninstall_kernel (rn
, fib
);
1902 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1904 /* Set real nexthop. */
1905 nexthop_active_update (rn
, fib
, 1);
1906 UNSET_FLAG(fib
->status
, RIB_ENTRY_CHANGED
);
1909 /* Regardless of some RIB entry being SELECTED or not before, now we can
1910 * tell, that if a new winner exists, FIB is still not updated with this
1911 * data, but ready to be.
1915 if (IS_ZEBRA_DEBUG_RIB
)
1916 rnode_debug (rn
, "Adding route, select %p", (void *)select
);
1918 if (info
->safi
== SAFI_UNICAST
)
1919 zfpm_trigger_update (rn
, "new route selected");
1921 /* Set real nexthop. */
1922 if (nexthop_active_update (rn
, select
, 1))
1924 if (IS_ZEBRA_DEBUG_RIB
)
1927 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1928 "old %p (type %d)", vrf_id
, buf
, rn
->p
.prefixlen
, rn
,
1929 select
, select
->type
, fib
, fib
->type
);
1931 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1932 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1935 if (! RIB_SYSTEM_ROUTE (select
))
1937 /* Clear FIB flag if performing a replace, will get set again
1938 * as part of install.
1942 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1943 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1945 rib_install_kernel (rn
, select
, fib
? 1 : 0);
1949 /* Uninstall prior route here, if needed. */
1950 if (fib
&& !RIB_SYSTEM_ROUTE (fib
))
1951 rib_uninstall_kernel (rn
, fib
);
1954 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1955 /* Unconditionally announce, this part is exercised by new routes */
1956 /* If we cannot add, for example route added is learnt by the */
1957 /* protocol we're trying to redistribute to, delete the redist */
1958 /* This is notified by setting the is_update to 1 */
1959 redistribute_update (&rn
->p
, select
, fib
);
1963 /* Uninstall prior route here and do redist delete, if needed. */
1966 if (IS_ZEBRA_DEBUG_RIB
)
1967 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1968 "- nexthop inactive",
1969 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1971 if (!RIB_SYSTEM_ROUTE (fib
))
1972 rib_uninstall_kernel (rn
, fib
);
1973 redistribute_delete(&rn
->p
, fib
);
1976 UNSET_FLAG(select
->status
, RIB_ENTRY_CHANGED
);
1980 /* Remove all RIB entries queued for removal */
1981 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1983 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1985 if (IS_ZEBRA_DEBUG_RIB
)
1987 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1988 (void *)rn
, (void *)rib
);
1990 rib_unlink(rn
, rib
);
1995 * Check if the dest can be deleted now.
2000 /* Take a list of route_node structs and return 1, if there was a record
2001 * picked from it and processed by rib_process(). Don't process more,
2002 * than one RN record; operate only in the specified sub-queue.
2005 process_subq (struct list
* subq
, u_char qindex
)
2007 struct listnode
*lnode
= listhead (subq
);
2008 struct route_node
*rnode
;
2009 char buf
[INET6_ADDRSTRLEN
];
2011 struct zebra_vrf
*zvrf
= NULL
;
2016 rnode
= listgetdata (lnode
);
2017 dest
= rib_dest_from_rnode (rnode
);
2019 zvrf
= rib_dest_vrf (dest
);
2021 rib_process (rnode
);
2023 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2025 inet_ntop (rnode
->p
.family
, &rnode
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2026 zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u",
2027 zvrf
? zvrf_id (zvrf
) : 0, buf
, rnode
->p
.prefixlen
, rnode
, qindex
);
2031 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2036 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2037 __func__
, rnode
, rnode
->lock
);
2038 zlog_backtrace(LOG_DEBUG
);
2041 route_unlock_node (rnode
);
2042 list_delete_node (subq
, lnode
);
2047 * All meta queues have been processed. Trigger next-hop evaluation.
2050 meta_queue_process_complete (struct work_queue
*dummy
)
2053 struct zebra_vrf
*zvrf
;
2055 /* Evaluate nexthops for those VRFs which underwent route processing. This
2056 * should limit the evaluation to the necessary VRFs in most common
2059 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2062 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2065 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2066 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2067 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2068 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2069 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2072 /* Schedule LSPs for processing, if needed. */
2073 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
2074 if (mpls_should_lsps_be_processed(zvrf
))
2076 if (IS_ZEBRA_DEBUG_MPLS
)
2077 zlog_debug ("%u: Scheduling all LSPs upon RIB completion", zvrf_id (zvrf
));
2078 zebra_mpls_lsp_schedule (zvrf
);
2079 mpls_unmark_lsps_for_processing(zvrf
);
2083 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2084 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
2085 * is pointed to the meta queue structure.
2087 static wq_item_status
2088 meta_queue_process (struct work_queue
*dummy
, void *data
)
2090 struct meta_queue
* mq
= data
;
2093 for (i
= 0; i
< MQ_SIZE
; i
++)
2094 if (process_subq (mq
->subq
[i
], i
))
2099 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2103 * Map from rib types to queue type (priority) in meta queue
2105 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
2106 [ZEBRA_ROUTE_SYSTEM
] = 4,
2107 [ZEBRA_ROUTE_KERNEL
] = 0,
2108 [ZEBRA_ROUTE_CONNECT
] = 0,
2109 [ZEBRA_ROUTE_STATIC
] = 1,
2110 [ZEBRA_ROUTE_RIP
] = 2,
2111 [ZEBRA_ROUTE_RIPNG
] = 2,
2112 [ZEBRA_ROUTE_OSPF
] = 2,
2113 [ZEBRA_ROUTE_OSPF6
] = 2,
2114 [ZEBRA_ROUTE_ISIS
] = 2,
2115 [ZEBRA_ROUTE_BGP
] = 3,
2116 [ZEBRA_ROUTE_HSLS
] = 4,
2117 [ZEBRA_ROUTE_TABLE
] = 1,
2120 /* Look into the RN and queue it into one or more priority queues,
2121 * increasing the size for each data push done.
2124 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
2128 RNODE_FOREACH_RIB (rn
, rib
)
2130 u_char qindex
= meta_queue_map
[rib
->type
];
2131 struct zebra_vrf
*zvrf
;
2133 /* Invariant: at this point we always have rn->info set. */
2134 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
2135 RIB_ROUTE_QUEUED (qindex
)))
2137 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2138 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
2139 (void *)rn
, qindex
);
2143 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2144 listnode_add (mq
->subq
[qindex
], rn
);
2145 route_lock_node (rn
);
2148 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2149 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
2150 (void *)rn
, qindex
);
2152 zvrf
= zebra_vrf_lookup_by_id (rib
->vrf_id
);
2154 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2158 /* Add route_node to work queue and schedule processing */
2160 rib_queue_add (struct route_node
*rn
)
2164 /* Pointless to queue a route_node with no RIB entries to add or remove */
2165 if (!rnode_to_ribs (rn
))
2167 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2168 __func__
, (void *)rn
, rn
->lock
);
2169 zlog_backtrace(LOG_DEBUG
);
2173 if (zebrad
.ribq
== NULL
)
2175 zlog_err ("%s: work_queue does not exist!", __func__
);
2180 * The RIB queue should normally be either empty or holding the only
2181 * work_queue_item element. In the latter case this element would
2182 * hold a pointer to the meta queue structure, which must be used to
2183 * actually queue the route nodes to process. So create the MQ
2184 * holder, if necessary, then push the work into it in any case.
2185 * This semantics was introduced after 0.99.9 release.
2187 if (!zebrad
.ribq
->items
->count
)
2188 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
2190 rib_meta_queue_add (zebrad
.mq
, rn
);
2195 /* Create new meta queue.
2196 A destructor function doesn't seem to be necessary here.
2198 static struct meta_queue
*
2199 meta_queue_new (void)
2201 struct meta_queue
*new;
2204 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
2207 for (i
= 0; i
< MQ_SIZE
; i
++)
2209 new->subq
[i
] = list_new ();
2210 assert(new->subq
[i
]);
2217 meta_queue_free (struct meta_queue
*mq
)
2221 for (i
= 0; i
< MQ_SIZE
; i
++)
2222 list_delete (mq
->subq
[i
]);
2224 XFREE (MTYPE_WORK_QUEUE
, mq
);
2227 /* initialise zebra rib work queue */
2229 rib_queue_init (struct zebra_t
*zebra
)
2233 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
2234 "route_node processing")))
2236 zlog_err ("%s: could not initialise work queue!", __func__
);
2240 /* fill in the work queue spec */
2241 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2242 zebra
->ribq
->spec
.errorfunc
= NULL
;
2243 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2244 /* XXX: TODO: These should be runtime configurable via vty */
2245 zebra
->ribq
->spec
.max_retries
= 3;
2246 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
2248 if (!(zebra
->mq
= meta_queue_new ()))
2250 zlog_err ("%s: could not initialise meta queue!", __func__
);
2256 /* RIB updates are processed via a queue of pointers to route_nodes.
2258 * The queue length is bounded by the maximal size of the routing table,
2259 * as a route_node will not be requeued, if already queued.
2261 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2262 * state, or static_install_route (when an existing RIB is updated)
2263 * and then submit route_node to queue for best-path selection later.
2264 * Order of add/delete state changes are preserved for any given RIB.
2266 * Deleted RIBs are reaped during best-path selection.
2269 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2270 * |-------->| | best RIB, if required
2272 * static_install->|->rib_addqueue...... -> rib_process
2274 * |-------->| |-> rib_unlink
2275 * |-> set RIB_ENTRY_REMOVE |
2276 * rib_delnode (RIB freed)
2278 * The 'info' pointer of a route_node points to a rib_dest_t
2279 * ('dest'). Queueing state for a route_node is kept on the dest. The
2280 * dest is created on-demand by rib_link() and is kept around at least
2281 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2283 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2285 * - route_nodes: refcounted by:
2286 * - dest attached to route_node:
2287 * - managed by: rib_link/rib_gc_dest
2288 * - route_node processing queue
2289 * - managed by: rib_addqueue, rib_process.
2293 /* Add RIB to head of the route node. */
2295 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2300 const char *rmap_name
;
2304 dest
= rib_dest_from_rnode (rn
);
2307 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2308 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2310 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2311 route_lock_node (rn
); /* rn route table reference */
2316 head
= dest
->routes
;
2324 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2325 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2326 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2328 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2329 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2337 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2339 /* RIB node has been un-removed before route-node is processed.
2340 * route_node must hence already be on the queue for processing..
2342 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2344 if (IS_ZEBRA_DEBUG_RIB
)
2345 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2347 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2350 rib_link (rn
, rib
, process
);
2356 * Detach a rib structure from a route_node.
2358 * Note that a call to rib_unlink() should be followed by a call to
2359 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2360 * longer required to be deleted.
2363 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2369 if (IS_ZEBRA_DEBUG_RIB
)
2370 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2372 dest
= rib_dest_from_rnode (rn
);
2375 rib
->next
->prev
= rib
->prev
;
2378 rib
->prev
->next
= rib
->next
;
2381 dest
->routes
= rib
->next
;
2384 /* free RIB and nexthops */
2385 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2386 nexthops_free(rib
->nexthop
);
2387 XFREE (MTYPE_RIB
, rib
);
2392 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2396 if (IS_ZEBRA_DEBUG_RIB
)
2397 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2398 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2400 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2401 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2402 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2404 zebra_del_import_table_entry(rn
, rib
);
2405 /* Just clean up if non main table */
2406 if (IS_ZEBRA_DEBUG_RIB
)
2408 char buf
[INET6_ADDRSTRLEN
];
2409 if (IS_ZEBRA_DEBUG_RIB
)
2411 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2412 zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)",
2413 rib
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, rib
, rib
->type
);
2417 rib_unlink(rn
, rib
);
2425 /* This function dumps the contents of a given RIB entry into
2426 * standard debug log. Calling function name and IP prefix in
2427 * question are passed as 1st and 2nd arguments.
2430 void _rib_dump (const char * func
,
2431 union prefix46constptr pp
, const struct rib
* rib
)
2433 const struct prefix
*p
= pp
.p
;
2434 char straddr
[PREFIX_STRLEN
];
2435 struct nexthop
*nexthop
, *tnexthop
;
2438 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func
, (const void *)rib
,
2439 prefix2str(pp
, straddr
, sizeof(straddr
)), rib
->vrf_id
);
2442 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2445 (unsigned long) rib
->uptime
,
2452 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2462 "%s: nexthop_num == %u, nexthop_active_num == %u",
2465 rib
->nexthop_active_num
2468 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2470 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2473 "%s: %s %s with flags %s%s%s",
2475 (recursing
? " NH" : "NH"),
2477 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2478 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2479 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2482 zlog_debug ("%s: dump complete", func
);
2485 /* This is an exported helper to rtm_read() to dump the strange
2486 * RIB entry found by rib_lookup_ipv4_route()
2489 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2491 struct route_table
*table
;
2492 struct route_node
*rn
;
2494 char prefix_buf
[INET_ADDRSTRLEN
];
2497 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2500 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2504 /* Scan the RIB table for exactly matching RIB entry. */
2505 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2507 /* No route for this prefix. */
2510 zlog_debug ("%s: lookup failed for %s", __func__
,
2511 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2516 route_unlock_node (rn
);
2519 RNODE_FOREACH_RIB (rn
, rib
)
2523 "%s: rn %p, rib %p: %s, %s",
2527 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2528 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2534 /* Check if requested address assignment will fail due to another
2535 * route being installed by zebra in FIB already. Take necessary
2536 * actions, if needed: remove such a route from FIB and deSELECT
2537 * corresponding RIB entry. Then put affected RN into RIBQ head.
2539 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2541 struct route_table
*table
;
2542 struct route_node
*rn
;
2544 unsigned changed
= 0;
2546 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2548 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2552 /* No matches would be the simplest case. */
2553 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2557 route_unlock_node (rn
);
2559 /* Check all RIB entries. In case any changes have to be done, requeue
2560 * the RN into RIBQ head. If the routing message about the new connected
2561 * route (generated by the IP address we are going to assign very soon)
2562 * comes before the RIBQ is processed, the new RIB entry will join
2563 * RIBQ record already on head. This is necessary for proper revalidation
2564 * of the rest of the RIB.
2566 RNODE_FOREACH_RIB (rn
, rib
)
2568 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
) &&
2569 ! RIB_SYSTEM_ROUTE (rib
))
2572 if (IS_ZEBRA_DEBUG_RIB
)
2574 char buf
[PREFIX_STRLEN
];
2575 zlog_debug ("%u:%s: freeing way for connected prefix",
2576 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2577 rib_dump (&rn
->p
, rib
);
2579 rib_uninstall (rn
, rib
);
2587 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2590 struct route_table
*table
;
2591 struct route_node
*rn
;
2593 struct nexthop
*nexthop
;
2600 if (p
->family
== AF_INET
)
2606 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2610 /* Make it sure prefixlen is applied to the prefix. */
2613 /* Set default distance by route type. */
2614 if (rib
->distance
== 0)
2616 rib
->distance
= route_info
[rib
->type
].distance
;
2618 /* iBGP distance is 200. */
2619 if (rib
->type
== ZEBRA_ROUTE_BGP
2620 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2621 rib
->distance
= 200;
2624 /* Lookup route node.*/
2625 rn
= route_node_get (table
, p
);
2627 /* If same type of route are installed, treat it as a implicit
2629 RNODE_FOREACH_RIB (rn
, same
)
2631 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2634 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2635 && same
->table
== rib
->table
2636 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2640 /* If this route is kernel route, set FIB flag to the route. */
2641 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2642 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2643 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2645 /* Link new rib to node.*/
2646 if (IS_ZEBRA_DEBUG_RIB
)
2648 char buf
[INET6_ADDRSTRLEN
];
2649 if (IS_ZEBRA_DEBUG_RIB
)
2651 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2652 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2654 rib
->vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2655 (void *)rib
, rib
->type
, (void *)same
);
2658 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2659 rib_dump ((struct prefix
*)p
, rib
);
2661 rib_addnode (rn
, rib
, 1);
2664 /* Free implicit route.*/
2667 rib_delnode (rn
, same
);
2671 route_unlock_node (rn
);
2676 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2677 int flags
, struct prefix
*p
, union g_addr
*gate
, ifindex_t ifindex
,
2680 struct route_table
*table
;
2681 struct route_node
*rn
;
2683 struct rib
*fib
= NULL
;
2684 struct rib
*same
= NULL
;
2685 struct nexthop
*nexthop
, *tnexthop
;
2687 char buf1
[PREFIX_STRLEN
];
2688 char buf2
[INET6_ADDRSTRLEN
];
2691 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2698 /* Lookup route node. */
2699 rn
= route_node_lookup (table
, p
);
2702 if (IS_ZEBRA_DEBUG_RIB
)
2703 zlog_debug ("%u:%s: doesn't exist in rib",
2704 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)));
2705 return ZEBRA_ERR_RTNOEXIST
;
2708 /* Lookup same type route. */
2709 RNODE_FOREACH_RIB (rn
, rib
)
2711 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2714 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2717 if (rib
->type
!= type
)
2719 if (rib
->instance
!= instance
)
2721 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2722 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2724 if (nexthop
->ifindex
!= ifindex
)
2729 route_unlock_node (rn
);
2730 route_unlock_node (rn
);
2736 /* Make sure that the route found has the same gateway. */
2744 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2745 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2746 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2755 /* If same type of route can't be found and this message is from
2759 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2760 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2762 if (IS_ZEBRA_DEBUG_RIB
)
2764 zlog_debug ("%u:%s: rn %p, rib %p (type %d) was deleted "
2765 "from kernel, adding",
2766 vrf_id
, prefix2str(p
, buf1
, INET6_ADDRSTRLEN
),
2767 rn
, fib
, fib
->type
);
2772 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2773 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2775 UNSET_FLAG (fib
->status
, RIB_ENTRY_SELECTED_FIB
);
2779 /* This means someone else, other than Zebra, has deleted
2780 * a Zebra router from the kernel. We will add it back */
2781 rib_install_kernel(rn
, fib
, NULL
);
2786 if (IS_ZEBRA_DEBUG_RIB
)
2789 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2790 "doesn't exist in rib",
2791 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2792 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
),
2796 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2797 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2801 route_unlock_node (rn
);
2802 return ZEBRA_ERR_RTNOEXIST
;
2807 rib_delnode (rn
, same
);
2809 route_unlock_node (rn
);
2816 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2817 u_short instance
, int flags
, struct prefix
*p
,
2818 union g_addr
*gate
, union g_addr
*src
, ifindex_t ifindex
,
2819 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2823 struct rib
*same
= NULL
;
2824 struct route_table
*table
;
2825 struct route_node
*rn
;
2826 struct nexthop
*nexthop
;
2829 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2833 /* Make sure mask is applied. */
2836 /* Set default distance by route type. */
2839 if ((unsigned)type
>= array_size(route_info
))
2842 distance
= route_info
[type
].distance
;
2844 /* iBGP distance is 200. */
2845 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2849 /* Lookup route node.*/
2850 rn
= route_node_get (table
, p
);
2852 /* If same type of route are installed, treat it as a implicit
2854 RNODE_FOREACH_RIB (rn
, rib
)
2856 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2859 if (rib
->type
!= type
)
2861 if (rib
->instance
!= instance
)
2863 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2868 /* Duplicate connected route comes in. */
2869 else if ((nexthop
= rib
->nexthop
) &&
2870 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2871 nexthop
->ifindex
== ifindex
&&
2872 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2879 /* Allocate new rib structure. */
2880 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2883 rib
->instance
= instance
;
2884 rib
->distance
= distance
;
2886 rib
->metric
= metric
;
2888 rib
->table
= table_id
;
2889 rib
->vrf_id
= vrf_id
;
2890 rib
->nexthop_num
= 0;
2891 rib
->uptime
= time (NULL
);
2893 /* Nexthop settings. */
2899 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2901 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2906 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2908 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2912 rib_nexthop_ifindex_add (rib
, ifindex
);
2914 /* If this route is kernel route, set FIB flag to the route. */
2915 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2916 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2917 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2919 /* Link new rib to node.*/
2920 if (IS_ZEBRA_DEBUG_RIB
)
2922 char buf
[INET6_ADDRSTRLEN
];
2923 if (IS_ZEBRA_DEBUG_RIB
)
2925 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2926 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2928 vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2929 (void *)rib
, rib
->type
, (void *)same
);
2932 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2935 rib_addnode (rn
, rib
, 1);
2937 /* Free implicit route.*/
2939 rib_delnode (rn
, same
);
2941 route_unlock_node (rn
);
2945 /* Schedule routes of a particular table (address-family) based on event. */
2947 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2949 struct route_node
*rn
;
2950 struct rib
*rib
, *next
;
2952 /* Walk all routes and queue for processing, if appropriate for
2953 * the trigger event.
2955 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2959 case RIB_UPDATE_IF_CHANGE
:
2960 /* Examine all routes that won't get processed by the protocol or
2961 * triggered by nexthop evaluation (NHT). This would be system,
2962 * kernel and certain static routes. Note that NHT will get
2963 * triggered upon an interface event as connected routes always
2964 * get queued for processing.
2966 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2968 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2969 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2970 rib
->type
== ZEBRA_ROUTE_BGP
)
2971 continue; /* protocol will handle. */
2972 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2975 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2976 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2977 nh
->type
== NEXTHOP_TYPE_IPV6
))
2980 /* If we only have nexthops to a gateway, NHT will
2991 case RIB_UPDATE_RMAP_CHANGE
:
2992 case RIB_UPDATE_OTHER
:
2993 /* Right now, examine all routes. Can restrict to a protocol in
2994 * some cases (TODO).
2996 if (rnode_to_ribs (rn
))
3006 /* RIB update function. */
3008 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
3010 struct route_table
*table
;
3012 /* Process routes of interested address-families. */
3013 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
3015 rib_update_table (table
, event
);
3017 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3019 rib_update_table (table
, event
);
3022 /* Remove all routes which comes from non main table. */
3024 rib_weed_table (struct route_table
*table
)
3026 struct route_node
*rn
;
3031 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3032 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3034 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3037 if (rib
->table
!= zebrad
.rtm_table_default
&&
3038 rib
->table
!= RT_TABLE_MAIN
)
3039 rib_delnode (rn
, rib
);
3043 /* Delete all routes from non main table. */
3045 rib_weed_tables (void)
3048 struct zebra_vrf
*zvrf
;
3050 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3051 if ((zvrf
= vrf
->info
) != NULL
)
3053 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3054 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3058 /* Delete self installed routes after zebra is relaunched. */
3060 rib_sweep_table (struct route_table
*table
)
3062 struct route_node
*rn
;
3068 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3069 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3071 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3074 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3075 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3077 ret
= rib_uninstall_kernel (rn
, rib
);
3079 rib_delnode (rn
, rib
);
3084 /* Sweep all RIB tables. */
3086 rib_sweep_route (void)
3089 struct zebra_vrf
*zvrf
;
3091 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3092 if ((zvrf
= vrf
->info
) != NULL
)
3094 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3095 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3099 /* Remove specific by protocol routes from 'table'. */
3100 static unsigned long
3101 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
3103 struct route_node
*rn
;
3106 unsigned long n
= 0;
3109 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3110 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3112 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3114 if (rib
->type
== proto
&& rib
->instance
== instance
)
3116 rib_delnode (rn
, rib
);
3124 /* Remove specific by protocol routes. */
3126 rib_score_proto (u_char proto
, u_short instance
)
3129 struct zebra_vrf
*zvrf
;
3130 unsigned long cnt
= 0;
3132 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
3133 if ((zvrf
= vrf
->info
) != NULL
)
3134 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
3135 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3140 /* Close RIB and clean up kernel routes. */
3142 rib_close_table (struct route_table
*table
)
3144 struct route_node
*rn
;
3145 rib_table_info_t
*info
= table
->info
;
3149 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3150 RNODE_FOREACH_RIB (rn
, rib
)
3152 if (!CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
3155 if (info
->safi
== SAFI_UNICAST
)
3156 zfpm_trigger_update (rn
, NULL
);
3158 if (! RIB_SYSTEM_ROUTE (rib
))
3159 rib_uninstall_kernel (rn
, rib
);
3163 /* Routing information base initialize. */
3167 rib_queue_init (&zebrad
);
3173 * Get the first vrf id that is greater than the given vrf id if any.
3175 * Returns TRUE if a vrf id was found, FALSE otherwise.
3178 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
3182 vrf
= vrf_lookup_by_id (vrf_id
);
3185 vrf
= RB_NEXT (vrf_id_head
, &vrfs_by_id
, vrf
);
3187 *next_id_p
= vrf
->vrf_id
;
3196 * rib_tables_iter_next
3198 * Returns the next table in the iteration.
3200 struct route_table
*
3201 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3203 struct route_table
*table
;
3206 * Array that helps us go over all AFI/SAFI combinations via one
3213 { AFI_IP
, SAFI_UNICAST
},
3214 { AFI_IP
, SAFI_MULTICAST
},
3215 { AFI_IP6
, SAFI_UNICAST
},
3216 { AFI_IP6
, SAFI_MULTICAST
},
3221 switch (iter
->state
)
3224 case RIB_TABLES_ITER_S_INIT
:
3225 iter
->vrf_id
= VRF_DEFAULT
;
3226 iter
->afi_safi_ix
= -1;
3230 case RIB_TABLES_ITER_S_ITERATING
:
3231 iter
->afi_safi_ix
++;
3235 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3237 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3238 afi_safis
[iter
->afi_safi_ix
].safi
,
3243 iter
->afi_safi_ix
++;
3247 * Found another table in this vrf.
3253 * Done with all tables in the current vrf, go to the next
3256 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3259 iter
->afi_safi_ix
= 0;
3264 case RIB_TABLES_ITER_S_DONE
:
3269 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3271 iter
->state
= RIB_TABLES_ITER_S_DONE
;