2 * Routing Table functions.
3 * Copyright (C) 1998 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #define FRR_COMPILING_TABLE_C
29 #include "sockunion.h"
31 DEFINE_MTYPE_STATIC(LIB
, ROUTE_TABLE
, "Route table")
32 DEFINE_MTYPE(LIB
, ROUTE_NODE
, "Route node")
34 static void route_table_free(struct route_table
*);
36 static int route_table_hash_cmp(const struct route_node
*a
,
37 const struct route_node
*b
)
39 return prefix_cmp(&a
->p
, &b
->p
);
42 DECLARE_HASH(rn_hash_node
, struct route_node
, nodehash
, route_table_hash_cmp
,
45 * route_table_init_with_delegate
48 route_table_init_with_delegate(route_table_delegate_t
*delegate
)
50 struct route_table
*rt
;
52 rt
= XCALLOC(MTYPE_ROUTE_TABLE
, sizeof(struct route_table
));
53 rt
->delegate
= delegate
;
54 rn_hash_node_init(&rt
->hash
);
58 void route_table_finish(struct route_table
*rt
)
63 /* Allocate new route node. */
64 static struct route_node
*route_node_new(struct route_table
*table
)
66 return table
->delegate
->create_node(table
->delegate
, table
);
69 /* Allocate new route node with prefix set. */
70 static struct route_node
*route_node_set(struct route_table
*table
,
71 const struct prefix
*prefix
)
73 struct route_node
*node
;
75 node
= route_node_new(table
);
77 prefix_copy(&node
->p
, prefix
);
80 rn_hash_node_add(&node
->table
->hash
, node
);
85 /* Free route node. */
86 static void route_node_free(struct route_table
*table
, struct route_node
*node
)
89 table
->cleanup(table
, node
);
90 table
->delegate
->destroy_node(table
->delegate
, table
, node
);
93 /* Free route table. */
94 static void route_table_free(struct route_table
*rt
)
96 struct route_node
*tmp_node
;
97 struct route_node
*node
;
104 /* Bulk deletion of nodes remaining in this table. This function is not
105 called until workers have completed their dependency on this table.
106 A final route_unlock_node() will not be called for these nodes. */
114 node
= node
->l_right
;
121 tmp_node
->table
->count
--;
122 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
123 rn_hash_node_del(&rt
->hash
, tmp_node
);
124 route_node_free(rt
, tmp_node
);
127 if (node
->l_left
== tmp_node
)
130 node
->l_right
= NULL
;
136 assert(rt
->count
== 0);
138 rn_hash_node_fini(&rt
->hash
);
139 XFREE(MTYPE_ROUTE_TABLE
, rt
);
143 /* Utility mask array. */
144 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
145 0xf8, 0xfc, 0xfe, 0xff};
147 /* Common prefix route genaration. */
148 static void route_common(const struct prefix
*n
, const struct prefix
*p
,
158 if (n
->family
== AF_FLOWSPEC
)
159 return prefix_copy(new, p
);
160 np
= (const uint8_t *)&n
->u
.prefix
;
161 pp
= (const uint8_t *)&p
->u
.prefix
;
163 newp
= (uint8_t *)&new->u
.prefix
;
165 for (i
= 0; i
< p
->prefixlen
/ 8; i
++) {
172 new->prefixlen
= i
* 8;
174 if (new->prefixlen
!= p
->prefixlen
) {
175 diff
= np
[i
] ^ pp
[i
];
177 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
)) {
181 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
185 static void set_link(struct route_node
*node
, struct route_node
*new)
187 unsigned int bit
= prefix_bit(&new->p
.u
.prefix
, node
->p
.prefixlen
);
189 node
->link
[bit
] = new;
193 /* Find matched prefix. */
194 struct route_node
*route_node_match(struct route_table
*table
,
195 union prefixconstptr pu
)
197 const struct prefix
*p
= pu
.p
;
198 struct route_node
*node
;
199 struct route_node
*matched
;
204 /* Walk down tree. If there is matched route then store it to
206 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
207 && prefix_match(&node
->p
, p
)) {
211 if (node
->p
.prefixlen
== p
->prefixlen
)
214 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
217 /* If matched route found, return it. */
219 return route_lock_node(matched
);
224 struct route_node
*route_node_match_ipv4(struct route_table
*table
,
225 const struct in_addr
*addr
)
227 struct prefix_ipv4 p
;
229 memset(&p
, 0, sizeof(struct prefix_ipv4
));
231 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
234 return route_node_match(table
, (struct prefix
*)&p
);
237 struct route_node
*route_node_match_ipv6(struct route_table
*table
,
238 const struct in6_addr
*addr
)
240 struct prefix_ipv6 p
;
242 memset(&p
, 0, sizeof(struct prefix_ipv6
));
244 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
247 return route_node_match(table
, &p
);
250 /* Lookup same prefix node. Return NULL when we can't find route. */
251 struct route_node
*route_node_lookup(struct route_table
*table
,
252 union prefixconstptr pu
)
254 struct route_node rn
, *node
;
255 prefix_copy(&rn
.p
, pu
.p
);
258 node
= rn_hash_node_find(&table
->hash
, &rn
);
259 return (node
&& node
->info
) ? route_lock_node(node
) : NULL
;
262 /* Lookup same prefix node. Return NULL when we can't find route. */
263 struct route_node
*route_node_lookup_maynull(struct route_table
*table
,
264 union prefixconstptr pu
)
266 struct route_node rn
, *node
;
267 prefix_copy(&rn
.p
, pu
.p
);
270 node
= rn_hash_node_find(&table
->hash
, &rn
);
271 return node
? route_lock_node(node
) : NULL
;
274 /* Add node to routing table. */
275 struct route_node
*route_node_get(struct route_table
*table
,
276 union prefixconstptr pu
)
278 struct route_node search
;
279 struct prefix
*p
= &search
.p
;
281 prefix_copy(p
, pu
.p
);
284 struct route_node
*new;
285 struct route_node
*node
;
286 struct route_node
*match
;
287 uint16_t prefixlen
= p
->prefixlen
;
288 const uint8_t *prefix
= &p
->u
.prefix
;
290 node
= rn_hash_node_find(&table
->hash
, &search
);
291 if (node
&& node
->info
)
292 return route_lock_node(node
);
296 while (node
&& node
->p
.prefixlen
<= prefixlen
297 && prefix_match(&node
->p
, p
)) {
298 if (node
->p
.prefixlen
== prefixlen
)
299 return route_lock_node(node
);
302 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
306 new = route_node_set(table
, p
);
308 set_link(match
, new);
312 new = route_node_new(table
);
313 route_common(&node
->p
, p
, &new->p
);
314 new->p
.family
= p
->family
;
317 rn_hash_node_add(&table
->hash
, new);
320 set_link(match
, new);
324 if (new->p
.prefixlen
!= p
->prefixlen
) {
326 new = route_node_set(table
, p
);
327 set_link(match
, new);
332 route_lock_node(new);
337 /* Delete node from the routing table. */
338 void route_node_delete(struct route_node
*node
)
340 struct route_node
*child
;
341 struct route_node
*parent
;
343 assert(node
->lock
== 0);
344 assert(node
->info
== NULL
);
346 if (node
->l_left
&& node
->l_right
)
350 child
= node
->l_left
;
352 child
= node
->l_right
;
354 parent
= node
->parent
;
357 child
->parent
= parent
;
360 if (parent
->l_left
== node
)
361 parent
->l_left
= child
;
363 parent
->l_right
= child
;
365 node
->table
->top
= child
;
367 node
->table
->count
--;
369 rn_hash_node_del(&node
->table
->hash
, node
);
371 /* WARNING: FRAGILE CODE!
372 * route_node_free may have the side effect of free'ing the entire
374 * this is permitted only if table->count got decremented to zero above,
375 * because in that case parent will also be NULL, so that we won't try
377 * delete a now-stale parent below.
379 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
381 route_node_free(node
->table
, node
);
383 /* If parent node is stub then delete it also. */
384 if (parent
&& parent
->lock
== 0)
385 route_node_delete(parent
);
388 /* Get fist node and lock it. This function is useful when one want
389 to lookup all the node exist in the routing table. */
390 struct route_node
*route_top(struct route_table
*table
)
392 /* If there is no node in the routing table return NULL. */
393 if (table
->top
== NULL
)
396 /* Lock the top node and return it. */
397 route_lock_node(table
->top
);
401 /* Unlock current node and lock next node then return it. */
402 struct route_node
*route_next(struct route_node
*node
)
404 struct route_node
*next
;
405 struct route_node
*start
;
407 /* Node may be deleted from route_unlock_node so we have to preserve
408 next node's pointer. */
412 route_lock_node(next
);
413 route_unlock_node(node
);
417 next
= node
->l_right
;
418 route_lock_node(next
);
419 route_unlock_node(node
);
424 while (node
->parent
) {
425 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
426 next
= node
->parent
->l_right
;
427 route_lock_node(next
);
428 route_unlock_node(start
);
433 route_unlock_node(start
);
437 /* Unlock current node and lock next node until limit. */
438 struct route_node
*route_next_until(struct route_node
*node
,
439 const struct route_node
*limit
)
441 struct route_node
*next
;
442 struct route_node
*start
;
444 /* Node may be deleted from route_unlock_node so we have to preserve
445 next node's pointer. */
449 route_lock_node(next
);
450 route_unlock_node(node
);
454 next
= node
->l_right
;
455 route_lock_node(next
);
456 route_unlock_node(node
);
461 while (node
->parent
&& node
!= limit
) {
462 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
463 next
= node
->parent
->l_right
;
464 route_lock_node(next
);
465 route_unlock_node(start
);
470 route_unlock_node(start
);
474 unsigned long route_table_count(struct route_table
*table
)
482 * Default function for creating a route node.
484 struct route_node
*route_node_create(route_table_delegate_t
*delegate
,
485 struct route_table
*table
)
487 struct route_node
*node
;
488 node
= XCALLOC(MTYPE_ROUTE_NODE
, sizeof(struct route_node
));
495 * Default function for destroying a route node.
497 void route_node_destroy(route_table_delegate_t
*delegate
,
498 struct route_table
*table
, struct route_node
*node
)
500 XFREE(MTYPE_ROUTE_NODE
, node
);
506 static route_table_delegate_t default_delegate
= {
507 .create_node
= route_node_create
,
508 .destroy_node
= route_node_destroy
};
510 route_table_delegate_t
*route_table_get_default_delegate(void)
512 return &default_delegate
;
518 struct route_table
*route_table_init(void)
520 return route_table_init_with_delegate(&default_delegate
);
524 * route_table_prefix_iter_cmp
526 * Compare two prefixes according to the order in which they appear in
527 * an iteration over a tree.
529 * @return -1 if p1 occurs before p2 (p1 < p2)
530 * 0 if the prefixes are identical (p1 == p2)
531 * +1 if p1 occurs after p2 (p1 > p2)
533 int route_table_prefix_iter_cmp(const struct prefix
*p1
,
534 const struct prefix
*p2
)
536 struct prefix common_space
;
537 struct prefix
*common
= &common_space
;
539 if (p1
->prefixlen
<= p2
->prefixlen
) {
540 if (prefix_match(p1
, p2
)) {
543 * p1 contains p2, or is equal to it.
545 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
550 * Check if p2 contains p1.
552 if (prefix_match(p2
, p1
))
556 route_common(p1
, p2
, common
);
557 assert(common
->prefixlen
< p1
->prefixlen
);
558 assert(common
->prefixlen
< p2
->prefixlen
);
561 * Both prefixes are longer than the common prefix.
563 * We need to check the bit after the common prefixlen to determine
564 * which one comes later.
566 if (prefix_bit(&p1
->u
.prefix
, common
->prefixlen
)) {
569 * We branch to the right to get to p1 from the common prefix.
571 assert(!prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
576 * We branch to the right to get to p2 from the common prefix.
578 assert(prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
583 * route_get_subtree_next
585 * Helper function that returns the first node that follows the nodes
586 * in the sub-tree under 'node' in iteration order.
588 static struct route_node
*route_get_subtree_next(struct route_node
*node
)
590 while (node
->parent
) {
591 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
592 return node
->parent
->l_right
;
601 * route_table_get_next_internal
603 * Helper function to find the node that occurs after the given prefix in
604 * order of iteration.
606 * @see route_table_get_next
608 static struct route_node
*
609 route_table_get_next_internal(struct route_table
*table
,
610 const struct prefix
*p
)
612 struct route_node
*node
, *tmp_node
;
620 if (node
->p
.prefixlen
< p
->prefixlen
)
621 match
= prefix_match(&node
->p
, p
);
623 match
= prefix_match(p
, &node
->p
);
626 if (node
->p
.prefixlen
== p
->prefixlen
) {
629 * The prefix p exists in the tree, just return
633 route_lock_node(node
);
634 node
= route_next(node
);
636 route_unlock_node(node
);
641 if (node
->p
.prefixlen
> p
->prefixlen
) {
644 * Node is in the subtree of p, and hence
651 * p is in the sub-tree under node.
653 tmp_node
= node
->link
[prefix_bit(&p
->u
.prefix
,
662 * There are no nodes in the direction where p should
664 * node has a right child, then it must be greater than
668 return node
->l_right
;
671 * No more children to follow, go upwards looking for
675 return route_get_subtree_next(node
);
679 * Neither node prefix nor 'p' contains the other.
681 cmp
= route_table_prefix_iter_cmp(&node
->p
, p
);
685 * Node follows p in iteration order. Return it.
693 * Node and the subtree under it come before prefix p in
694 * iteration order. Prefix p and its sub-tree are not present in
695 * the tree. Go upwards and find the first node that follows the
696 * subtree. That node will also succeed p.
698 return route_get_subtree_next(node
);
705 * route_table_get_next
707 * Find the node that occurs after the given prefix in order of
710 struct route_node
*route_table_get_next(struct route_table
*table
,
711 union prefixconstptr pu
)
713 const struct prefix
*p
= pu
.p
;
714 struct route_node
*node
;
716 node
= route_table_get_next_internal(table
, p
);
718 assert(route_table_prefix_iter_cmp(&node
->p
, p
) > 0);
719 route_lock_node(node
);
725 * route_table_iter_init
727 void route_table_iter_init(route_table_iter_t
*iter
, struct route_table
*table
)
729 memset(iter
, 0, sizeof(*iter
));
730 iter
->state
= RT_ITER_STATE_INIT
;
735 * route_table_iter_pause
737 * Pause an iteration over the table. This allows the iteration to be
738 * resumed point after arbitrary additions/deletions from the table.
739 * An iteration can be resumed by just calling route_table_iter_next()
742 void route_table_iter_pause(route_table_iter_t
*iter
)
744 switch (iter
->state
) {
746 case RT_ITER_STATE_INIT
:
747 case RT_ITER_STATE_PAUSED
:
748 case RT_ITER_STATE_DONE
:
751 case RT_ITER_STATE_ITERATING
:
754 * Save the prefix that we are currently at. The next call to
755 * route_table_iter_next() will return the node after this
759 prefix_copy(&iter
->pause_prefix
, &iter
->current
->p
);
760 route_unlock_node(iter
->current
);
761 iter
->current
= NULL
;
762 iter
->state
= RT_ITER_STATE_PAUSED
;
771 * route_table_iter_cleanup
773 * Release any resources held by the iterator.
775 void route_table_iter_cleanup(route_table_iter_t
*iter
)
777 if (iter
->state
== RT_ITER_STATE_ITERATING
) {
778 route_unlock_node(iter
->current
);
779 iter
->current
= NULL
;
781 assert(!iter
->current
);
784 * Set the state to RT_ITER_STATE_DONE to make any
785 * route_table_iter_next() calls on this iterator return NULL.
787 iter
->state
= RT_ITER_STATE_DONE
;