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(LIB
, ROUTE_TABLE
, "Route table")
32 DEFINE_MTYPE(LIB
, ROUTE_NODE
, "Route node")
34 static void route_node_delete(struct route_node
*);
35 static void route_table_free(struct route_table
*);
37 static int route_table_hash_cmp(const void *a
, const void *b
)
39 const struct prefix
*pa
= a
, *pb
= b
;
40 return prefix_cmp(pa
, pb
) == 0;
44 * route_table_init_with_delegate
47 route_table_init_with_delegate(route_table_delegate_t
*delegate
)
49 struct route_table
*rt
;
51 rt
= XCALLOC(MTYPE_ROUTE_TABLE
, sizeof(struct route_table
));
52 rt
->delegate
= delegate
;
53 rt
->hash
= hash_create(prefix_hash_key
, route_table_hash_cmp
,
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
, *inserted
;
75 node
= route_node_new(table
);
77 prefix_copy(&node
->p
, prefix
);
81 inserted
= hash_get(node
->table
->hash
, node
, hash_alloc_intern
);
82 assert(inserted
== node
);
87 /* Free route node. */
88 static void route_node_free(struct route_table
*table
, struct route_node
*node
)
91 table
->cleanup(table
, node
);
92 table
->delegate
->destroy_node(table
->delegate
, table
, node
);
95 /* Free route table. */
96 static void route_table_free(struct route_table
*rt
)
98 struct route_node
*tmp_node
;
99 struct route_node
*node
;
104 hash_clean(rt
->hash
, NULL
);
109 /* Bulk deletion of nodes remaining in this table. This function is not
110 called until workers have completed their dependency on this table.
111 A final route_unlock_node() will not be called for these nodes. */
119 node
= node
->l_right
;
126 tmp_node
->table
->count
--;
127 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
128 route_node_free(rt
, tmp_node
);
131 if (node
->l_left
== tmp_node
)
134 node
->l_right
= NULL
;
140 assert(rt
->count
== 0);
142 XFREE(MTYPE_ROUTE_TABLE
, rt
);
146 /* Utility mask array. */
147 static const u_char maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
148 0xf8, 0xfc, 0xfe, 0xff};
150 /* Common prefix route genaration. */
151 static void route_common(const struct prefix
*n
, const struct prefix
*p
,
158 const u_char
*np
= (const u_char
*)&n
->u
.prefix
;
159 const u_char
*pp
= (const u_char
*)&p
->u
.prefix
;
160 u_char
*newp
= (u_char
*)&new->u
.prefix
;
162 for (i
= 0; i
< p
->prefixlen
/ 8; i
++) {
169 new->prefixlen
= i
* 8;
171 if (new->prefixlen
!= p
->prefixlen
) {
172 diff
= np
[i
] ^ pp
[i
];
174 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
)) {
178 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
182 static void set_link(struct route_node
*node
, struct route_node
*new)
184 unsigned int bit
= prefix_bit(&new->p
.u
.prefix
, node
->p
.prefixlen
);
186 node
->link
[bit
] = new;
191 struct route_node
*route_lock_node(struct route_node
*node
)
198 void route_unlock_node(struct route_node
*node
)
200 assert(node
->lock
> 0);
204 route_node_delete(node
);
207 /* Find matched prefix. */
208 struct route_node
*route_node_match(const struct route_table
*table
,
209 union prefixconstptr pu
)
211 const struct prefix
*p
= pu
.p
;
212 struct route_node
*node
;
213 struct route_node
*matched
;
218 /* Walk down tree. If there is matched route then store it to
220 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
221 && prefix_match(&node
->p
, p
)) {
225 if (node
->p
.prefixlen
== p
->prefixlen
)
228 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
231 /* If matched route found, return it. */
233 return route_lock_node(matched
);
238 struct route_node
*route_node_match_ipv4(const struct route_table
*table
,
239 const struct in_addr
*addr
)
241 struct prefix_ipv4 p
;
243 memset(&p
, 0, sizeof(struct prefix_ipv4
));
245 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
248 return route_node_match(table
, (struct prefix
*)&p
);
251 struct route_node
*route_node_match_ipv6(const struct route_table
*table
,
252 const struct in6_addr
*addr
)
254 struct prefix_ipv6 p
;
256 memset(&p
, 0, sizeof(struct prefix_ipv6
));
258 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
261 return route_node_match(table
, (struct prefix
*)&p
);
264 /* Lookup same prefix node. Return NULL when we can't find route. */
265 struct route_node
*route_node_lookup(const struct route_table
*table
,
266 union prefixconstptr pu
)
269 struct route_node
*node
;
270 prefix_copy(&p
, pu
.p
);
273 node
= hash_get(table
->hash
, (void *)&p
, NULL
);
274 return (node
&& node
->info
) ? route_lock_node(node
) : NULL
;
277 /* Lookup same prefix node. Return NULL when we can't find route. */
278 struct route_node
*route_node_lookup_maynull(const struct route_table
*table
,
279 union prefixconstptr pu
)
282 struct route_node
*node
;
283 prefix_copy(&p
, pu
.p
);
286 node
= hash_get(table
->hash
, (void *)&p
, NULL
);
287 return node
? route_lock_node(node
) : NULL
;
290 /* Add node to routing table. */
291 struct route_node
*route_node_get(struct route_table
*const table
,
292 union prefixconstptr pu
)
294 const struct prefix
*p
= pu
.p
;
295 struct route_node
*new;
296 struct route_node
*node
;
297 struct route_node
*match
;
298 struct route_node
*inserted
;
299 u_char prefixlen
= p
->prefixlen
;
300 const u_char
*prefix
= &p
->u
.prefix
;
302 node
= hash_get(table
->hash
, (void *)p
, NULL
);
303 if (node
&& node
->info
)
304 return route_lock_node(node
);
308 while (node
&& node
->p
.prefixlen
<= prefixlen
309 && prefix_match(&node
->p
, p
)) {
310 if (node
->p
.prefixlen
== prefixlen
)
311 return route_lock_node(node
);
314 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
318 new = route_node_set(table
, p
);
320 set_link(match
, new);
324 new = route_node_new(table
);
325 route_common(&node
->p
, p
, &new->p
);
326 new->p
.family
= p
->family
;
329 inserted
= hash_get(node
->table
->hash
, new, hash_alloc_intern
);
330 assert(inserted
== new);
333 set_link(match
, new);
337 if (new->p
.prefixlen
!= p
->prefixlen
) {
339 new = route_node_set(table
, p
);
340 set_link(match
, new);
345 route_lock_node(new);
350 /* Delete node from the routing table. */
351 static void route_node_delete(struct route_node
*node
)
353 struct route_node
*child
;
354 struct route_node
*parent
;
356 assert(node
->lock
== 0);
357 assert(node
->info
== NULL
);
359 if (node
->l_left
&& node
->l_right
)
363 child
= node
->l_left
;
365 child
= node
->l_right
;
367 parent
= node
->parent
;
370 child
->parent
= parent
;
373 if (parent
->l_left
== node
)
374 parent
->l_left
= child
;
376 parent
->l_right
= child
;
378 node
->table
->top
= child
;
380 node
->table
->count
--;
382 hash_release(node
->table
->hash
, node
);
384 /* WARNING: FRAGILE CODE!
385 * route_node_free may have the side effect of free'ing the entire
387 * this is permitted only if table->count got decremented to zero above,
388 * because in that case parent will also be NULL, so that we won't try
390 * delete a now-stale parent below.
392 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
394 route_node_free(node
->table
, node
);
396 /* If parent node is stub then delete it also. */
397 if (parent
&& parent
->lock
== 0)
398 route_node_delete(parent
);
401 /* Get fist node and lock it. This function is useful when one want
402 to lookup all the node exist in the routing table. */
403 struct route_node
*route_top(struct route_table
*table
)
405 /* If there is no node in the routing table return NULL. */
406 if (table
->top
== NULL
)
409 /* Lock the top node and return it. */
410 route_lock_node(table
->top
);
414 /* Unlock current node and lock next node then return it. */
415 struct route_node
*route_next(struct route_node
*node
)
417 struct route_node
*next
;
418 struct route_node
*start
;
420 /* Node may be deleted from route_unlock_node so we have to preserve
421 next node's pointer. */
425 route_lock_node(next
);
426 route_unlock_node(node
);
430 next
= node
->l_right
;
431 route_lock_node(next
);
432 route_unlock_node(node
);
437 while (node
->parent
) {
438 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
439 next
= node
->parent
->l_right
;
440 route_lock_node(next
);
441 route_unlock_node(start
);
446 route_unlock_node(start
);
450 /* Unlock current node and lock next node until limit. */
451 struct route_node
*route_next_until(struct route_node
*node
,
452 const struct route_node
*limit
)
454 struct route_node
*next
;
455 struct route_node
*start
;
457 /* Node may be deleted from route_unlock_node so we have to preserve
458 next node's pointer. */
462 route_lock_node(next
);
463 route_unlock_node(node
);
467 next
= node
->l_right
;
468 route_lock_node(next
);
469 route_unlock_node(node
);
474 while (node
->parent
&& node
!= limit
) {
475 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
476 next
= node
->parent
->l_right
;
477 route_lock_node(next
);
478 route_unlock_node(start
);
483 route_unlock_node(start
);
487 unsigned long route_table_count(const struct route_table
*table
)
495 * Default function for creating a route node.
497 struct route_node
*route_node_create(route_table_delegate_t
*delegate
,
498 struct route_table
*table
)
500 struct route_node
*node
;
501 node
= XCALLOC(MTYPE_ROUTE_NODE
, sizeof(struct route_node
));
508 * Default function for destroying a route node.
510 void route_node_destroy(route_table_delegate_t
*delegate
,
511 struct route_table
*table
, struct route_node
*node
)
513 XFREE(MTYPE_ROUTE_NODE
, node
);
519 static route_table_delegate_t default_delegate
= {
520 .create_node
= route_node_create
,
521 .destroy_node
= route_node_destroy
};
523 route_table_delegate_t
*route_table_get_default_delegate(void)
525 return &default_delegate
;
531 struct route_table
*route_table_init(void)
533 return route_table_init_with_delegate(&default_delegate
);
537 * route_table_prefix_iter_cmp
539 * Compare two prefixes according to the order in which they appear in
540 * an iteration over a tree.
542 * @return -1 if p1 occurs before p2 (p1 < p2)
543 * 0 if the prefixes are identical (p1 == p2)
544 * +1 if p1 occurs after p2 (p1 > p2)
546 int route_table_prefix_iter_cmp(const struct prefix
*p1
,
547 const struct prefix
*p2
)
549 struct prefix common_space
;
550 struct prefix
*common
= &common_space
;
552 if (p1
->prefixlen
<= p2
->prefixlen
) {
553 if (prefix_match(p1
, p2
)) {
556 * p1 contains p2, or is equal to it.
558 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
563 * Check if p2 contains p1.
565 if (prefix_match(p2
, p1
))
569 route_common(p1
, p2
, common
);
570 assert(common
->prefixlen
< p1
->prefixlen
);
571 assert(common
->prefixlen
< p2
->prefixlen
);
574 * Both prefixes are longer than the common prefix.
576 * We need to check the bit after the common prefixlen to determine
577 * which one comes later.
579 if (prefix_bit(&p1
->u
.prefix
, common
->prefixlen
)) {
582 * We branch to the right to get to p1 from the common prefix.
584 assert(!prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
589 * We branch to the right to get to p2 from the common prefix.
591 assert(prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
596 * route_get_subtree_next
598 * Helper function that returns the first node that follows the nodes
599 * in the sub-tree under 'node' in iteration order.
601 static struct route_node
*route_get_subtree_next(struct route_node
*node
)
603 while (node
->parent
) {
604 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
605 return node
->parent
->l_right
;
614 * route_table_get_next_internal
616 * Helper function to find the node that occurs after the given prefix in
617 * order of iteration.
619 * @see route_table_get_next
621 static struct route_node
*
622 route_table_get_next_internal(const struct route_table
*table
,
623 const struct prefix
*p
)
625 struct route_node
*node
, *tmp_node
;
633 if (node
->p
.prefixlen
< p
->prefixlen
)
634 match
= prefix_match(&node
->p
, p
);
636 match
= prefix_match(p
, &node
->p
);
639 if (node
->p
.prefixlen
== p
->prefixlen
) {
642 * The prefix p exists in the tree, just return
646 route_lock_node(node
);
647 node
= route_next(node
);
649 route_unlock_node(node
);
654 if (node
->p
.prefixlen
> p
->prefixlen
) {
657 * Node is in the subtree of p, and hence
664 * p is in the sub-tree under node.
666 tmp_node
= node
->link
[prefix_bit(&p
->u
.prefix
,
675 * There are no nodes in the direction where p should
677 * node has a right child, then it must be greater than
681 return node
->l_right
;
684 * No more children to follow, go upwards looking for
688 return route_get_subtree_next(node
);
692 * Neither node prefix nor 'p' contains the other.
694 cmp
= route_table_prefix_iter_cmp(&node
->p
, p
);
698 * Node follows p in iteration order. Return it.
706 * Node and the subtree under it come before prefix p in
707 * iteration order. Prefix p and its sub-tree are not present in
708 * the tree. Go upwards and find the first node that follows the
709 * subtree. That node will also succeed p.
711 return route_get_subtree_next(node
);
718 * route_table_get_next
720 * Find the node that occurs after the given prefix in order of
723 struct route_node
*route_table_get_next(const struct route_table
*table
,
724 union prefixconstptr pu
)
726 const struct prefix
*p
= pu
.p
;
727 struct route_node
*node
;
729 node
= route_table_get_next_internal(table
, p
);
731 assert(route_table_prefix_iter_cmp(&node
->p
, p
) > 0);
732 route_lock_node(node
);
738 * route_table_iter_init
740 void route_table_iter_init(route_table_iter_t
*iter
, struct route_table
*table
)
742 memset(iter
, 0, sizeof(*iter
));
743 iter
->state
= RT_ITER_STATE_INIT
;
748 * route_table_iter_pause
750 * Pause an iteration over the table. This allows the iteration to be
751 * resumed point after arbitrary additions/deletions from the table.
752 * An iteration can be resumed by just calling route_table_iter_next()
755 void route_table_iter_pause(route_table_iter_t
*iter
)
757 switch (iter
->state
) {
759 case RT_ITER_STATE_INIT
:
760 case RT_ITER_STATE_PAUSED
:
761 case RT_ITER_STATE_DONE
:
764 case RT_ITER_STATE_ITERATING
:
767 * Save the prefix that we are currently at. The next call to
768 * route_table_iter_next() will return the node after this
772 prefix_copy(&iter
->pause_prefix
, &iter
->current
->p
);
773 route_unlock_node(iter
->current
);
774 iter
->current
= NULL
;
775 iter
->state
= RT_ITER_STATE_PAUSED
;
784 * route_table_iter_cleanup
786 * Release any resources held by the iterator.
788 void route_table_iter_cleanup(route_table_iter_t
*iter
)
790 if (iter
->state
== RT_ITER_STATE_ITERATING
) {
791 route_unlock_node(iter
->current
);
792 iter
->current
= NULL
;
794 assert(!iter
->current
);
797 * Set the state to RT_ITER_STATE_DONE to make any
798 * route_table_iter_next() calls on this iterator return NULL.
800 iter
->state
= RT_ITER_STATE_DONE
;