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_table_free(struct route_table
*);
36 static int route_table_hash_cmp(const void *a
, const void *b
)
38 const struct prefix
*pa
= a
, *pb
= b
;
39 return prefix_cmp(pa
, pb
) == 0;
43 * route_table_init_with_delegate
46 route_table_init_with_delegate(route_table_delegate_t
*delegate
)
48 struct route_table
*rt
;
50 rt
= XCALLOC(MTYPE_ROUTE_TABLE
, sizeof(struct route_table
));
51 rt
->delegate
= delegate
;
52 rt
->hash
= hash_create(prefix_hash_key
, route_table_hash_cmp
,
57 void route_table_finish(struct route_table
*rt
)
62 /* Allocate new route node. */
63 static struct route_node
*route_node_new(struct route_table
*table
)
65 return table
->delegate
->create_node(table
->delegate
, table
);
68 /* Allocate new route node with prefix set. */
69 static struct route_node
*route_node_set(struct route_table
*table
,
70 const struct prefix
*prefix
)
72 struct route_node
*node
, *inserted
;
74 node
= route_node_new(table
);
76 prefix_copy(&node
->p
, prefix
);
79 inserted
= hash_get(node
->table
->hash
, node
, hash_alloc_intern
);
80 assert(inserted
== 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
;
102 hash_clean(rt
->hash
, NULL
);
107 /* Bulk deletion of nodes remaining in this table. This function is not
108 called until workers have completed their dependency on this table.
109 A final route_unlock_node() will not be called for these nodes. */
117 node
= node
->l_right
;
124 tmp_node
->table
->count
--;
125 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
126 route_node_free(rt
, tmp_node
);
129 if (node
->l_left
== tmp_node
)
132 node
->l_right
= NULL
;
138 assert(rt
->count
== 0);
140 XFREE(MTYPE_ROUTE_TABLE
, rt
);
144 /* Utility mask array. */
145 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
146 0xf8, 0xfc, 0xfe, 0xff};
148 /* Common prefix route genaration. */
149 static void route_common(const struct prefix
*n
, const struct prefix
*p
,
159 if (n
->family
== AF_FLOWSPEC
)
160 return prefix_copy(new, p
);
161 np
= (const uint8_t *)&n
->u
.prefix
;
162 pp
= (const uint8_t *)&p
->u
.prefix
;
164 newp
= (uint8_t *)&new->u
.prefix
;
166 for (i
= 0; i
< p
->prefixlen
/ 8; i
++) {
173 new->prefixlen
= i
* 8;
175 if (new->prefixlen
!= p
->prefixlen
) {
176 diff
= np
[i
] ^ pp
[i
];
178 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
)) {
182 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
186 static void set_link(struct route_node
*node
, struct route_node
*new)
188 unsigned int bit
= prefix_bit(&new->p
.u
.prefix
, node
->p
.prefixlen
);
190 node
->link
[bit
] = new;
194 /* Find matched prefix. */
195 struct route_node
*route_node_match(const struct route_table
*table
,
196 union prefixconstptr pu
)
198 const struct prefix
*p
= pu
.p
;
199 struct route_node
*node
;
200 struct route_node
*matched
;
205 /* Walk down tree. If there is matched route then store it to
207 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
208 && prefix_match(&node
->p
, p
)) {
212 if (node
->p
.prefixlen
== p
->prefixlen
)
215 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
218 /* If matched route found, return it. */
220 return route_lock_node(matched
);
225 struct route_node
*route_node_match_ipv4(const struct route_table
*table
,
226 const struct in_addr
*addr
)
228 struct prefix_ipv4 p
;
230 memset(&p
, 0, sizeof(struct prefix_ipv4
));
232 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
235 return route_node_match(table
, (struct prefix
*)&p
);
238 struct route_node
*route_node_match_ipv6(const struct route_table
*table
,
239 const struct in6_addr
*addr
)
241 struct prefix_ipv6 p
;
243 memset(&p
, 0, sizeof(struct prefix_ipv6
));
245 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
248 return route_node_match(table
, (struct prefix
*)&p
);
251 /* Lookup same prefix node. Return NULL when we can't find route. */
252 struct route_node
*route_node_lookup(const struct route_table
*table
,
253 union prefixconstptr pu
)
256 struct route_node
*node
;
257 prefix_copy(&p
, pu
.p
);
260 node
= hash_get(table
->hash
, (void *)&p
, NULL
);
261 return (node
&& node
->info
) ? route_lock_node(node
) : NULL
;
264 /* Lookup same prefix node. Return NULL when we can't find route. */
265 struct route_node
*route_node_lookup_maynull(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
? route_lock_node(node
) : NULL
;
277 /* Add node to routing table. */
278 struct route_node
*route_node_get(struct route_table
*const table
,
279 union prefixconstptr pu
)
281 const struct prefix
*p
= pu
.p
;
282 struct route_node
*new;
283 struct route_node
*node
;
284 struct route_node
*match
;
285 struct route_node
*inserted
;
286 uint8_t prefixlen
= p
->prefixlen
;
287 const uint8_t *prefix
= &p
->u
.prefix
;
289 apply_mask((struct prefix
*)p
);
290 node
= hash_get(table
->hash
, (void *)p
, NULL
);
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 inserted
= hash_get(node
->table
->hash
, new, hash_alloc_intern
);
318 assert(inserted
== new);
321 set_link(match
, new);
325 if (new->p
.prefixlen
!= p
->prefixlen
) {
327 new = route_node_set(table
, p
);
328 set_link(match
, new);
333 route_lock_node(new);
338 /* Delete node from the routing table. */
339 void route_node_delete(struct route_node
*node
)
341 struct route_node
*child
;
342 struct route_node
*parent
;
344 assert(node
->lock
== 0);
345 assert(node
->info
== NULL
);
347 if (node
->l_left
&& node
->l_right
)
351 child
= node
->l_left
;
353 child
= node
->l_right
;
355 parent
= node
->parent
;
358 child
->parent
= parent
;
361 if (parent
->l_left
== node
)
362 parent
->l_left
= child
;
364 parent
->l_right
= child
;
366 node
->table
->top
= child
;
368 node
->table
->count
--;
370 hash_release(node
->table
->hash
, node
);
372 /* WARNING: FRAGILE CODE!
373 * route_node_free may have the side effect of free'ing the entire
375 * this is permitted only if table->count got decremented to zero above,
376 * because in that case parent will also be NULL, so that we won't try
378 * delete a now-stale parent below.
380 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
382 route_node_free(node
->table
, node
);
384 /* If parent node is stub then delete it also. */
385 if (parent
&& parent
->lock
== 0)
386 route_node_delete(parent
);
389 /* Get fist node and lock it. This function is useful when one want
390 to lookup all the node exist in the routing table. */
391 struct route_node
*route_top(struct route_table
*table
)
393 /* If there is no node in the routing table return NULL. */
394 if (table
->top
== NULL
)
397 /* Lock the top node and return it. */
398 route_lock_node(table
->top
);
402 /* Unlock current node and lock next node then return it. */
403 struct route_node
*route_next(struct route_node
*node
)
405 struct route_node
*next
;
406 struct route_node
*start
;
408 /* Node may be deleted from route_unlock_node so we have to preserve
409 next node's pointer. */
413 route_lock_node(next
);
414 route_unlock_node(node
);
418 next
= node
->l_right
;
419 route_lock_node(next
);
420 route_unlock_node(node
);
425 while (node
->parent
) {
426 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
427 next
= node
->parent
->l_right
;
428 route_lock_node(next
);
429 route_unlock_node(start
);
434 route_unlock_node(start
);
438 /* Unlock current node and lock next node until limit. */
439 struct route_node
*route_next_until(struct route_node
*node
,
440 const struct route_node
*limit
)
442 struct route_node
*next
;
443 struct route_node
*start
;
445 /* Node may be deleted from route_unlock_node so we have to preserve
446 next node's pointer. */
450 route_lock_node(next
);
451 route_unlock_node(node
);
455 next
= node
->l_right
;
456 route_lock_node(next
);
457 route_unlock_node(node
);
462 while (node
->parent
&& node
!= limit
) {
463 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
464 next
= node
->parent
->l_right
;
465 route_lock_node(next
);
466 route_unlock_node(start
);
471 route_unlock_node(start
);
475 unsigned long route_table_count(const struct route_table
*table
)
483 * Default function for creating a route node.
485 struct route_node
*route_node_create(route_table_delegate_t
*delegate
,
486 struct route_table
*table
)
488 struct route_node
*node
;
489 node
= XCALLOC(MTYPE_ROUTE_NODE
, sizeof(struct route_node
));
496 * Default function for destroying a route node.
498 void route_node_destroy(route_table_delegate_t
*delegate
,
499 struct route_table
*table
, struct route_node
*node
)
501 XFREE(MTYPE_ROUTE_NODE
, node
);
507 static route_table_delegate_t default_delegate
= {
508 .create_node
= route_node_create
,
509 .destroy_node
= route_node_destroy
};
511 route_table_delegate_t
*route_table_get_default_delegate(void)
513 return &default_delegate
;
519 struct route_table
*route_table_init(void)
521 return route_table_init_with_delegate(&default_delegate
);
525 * route_table_prefix_iter_cmp
527 * Compare two prefixes according to the order in which they appear in
528 * an iteration over a tree.
530 * @return -1 if p1 occurs before p2 (p1 < p2)
531 * 0 if the prefixes are identical (p1 == p2)
532 * +1 if p1 occurs after p2 (p1 > p2)
534 int route_table_prefix_iter_cmp(const struct prefix
*p1
,
535 const struct prefix
*p2
)
537 struct prefix common_space
;
538 struct prefix
*common
= &common_space
;
540 if (p1
->prefixlen
<= p2
->prefixlen
) {
541 if (prefix_match(p1
, p2
)) {
544 * p1 contains p2, or is equal to it.
546 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
551 * Check if p2 contains p1.
553 if (prefix_match(p2
, p1
))
557 route_common(p1
, p2
, common
);
558 assert(common
->prefixlen
< p1
->prefixlen
);
559 assert(common
->prefixlen
< p2
->prefixlen
);
562 * Both prefixes are longer than the common prefix.
564 * We need to check the bit after the common prefixlen to determine
565 * which one comes later.
567 if (prefix_bit(&p1
->u
.prefix
, common
->prefixlen
)) {
570 * We branch to the right to get to p1 from the common prefix.
572 assert(!prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
577 * We branch to the right to get to p2 from the common prefix.
579 assert(prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
584 * route_get_subtree_next
586 * Helper function that returns the first node that follows the nodes
587 * in the sub-tree under 'node' in iteration order.
589 static struct route_node
*route_get_subtree_next(struct route_node
*node
)
591 while (node
->parent
) {
592 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
593 return node
->parent
->l_right
;
602 * route_table_get_next_internal
604 * Helper function to find the node that occurs after the given prefix in
605 * order of iteration.
607 * @see route_table_get_next
609 static struct route_node
*
610 route_table_get_next_internal(const struct route_table
*table
,
611 const struct prefix
*p
)
613 struct route_node
*node
, *tmp_node
;
621 if (node
->p
.prefixlen
< p
->prefixlen
)
622 match
= prefix_match(&node
->p
, p
);
624 match
= prefix_match(p
, &node
->p
);
627 if (node
->p
.prefixlen
== p
->prefixlen
) {
630 * The prefix p exists in the tree, just return
634 route_lock_node(node
);
635 node
= route_next(node
);
637 route_unlock_node(node
);
642 if (node
->p
.prefixlen
> p
->prefixlen
) {
645 * Node is in the subtree of p, and hence
652 * p is in the sub-tree under node.
654 tmp_node
= node
->link
[prefix_bit(&p
->u
.prefix
,
663 * There are no nodes in the direction where p should
665 * node has a right child, then it must be greater than
669 return node
->l_right
;
672 * No more children to follow, go upwards looking for
676 return route_get_subtree_next(node
);
680 * Neither node prefix nor 'p' contains the other.
682 cmp
= route_table_prefix_iter_cmp(&node
->p
, p
);
686 * Node follows p in iteration order. Return it.
694 * Node and the subtree under it come before prefix p in
695 * iteration order. Prefix p and its sub-tree are not present in
696 * the tree. Go upwards and find the first node that follows the
697 * subtree. That node will also succeed p.
699 return route_get_subtree_next(node
);
706 * route_table_get_next
708 * Find the node that occurs after the given prefix in order of
711 struct route_node
*route_table_get_next(const struct route_table
*table
,
712 union prefixconstptr pu
)
714 const struct prefix
*p
= pu
.p
;
715 struct route_node
*node
;
717 node
= route_table_get_next_internal(table
, p
);
719 assert(route_table_prefix_iter_cmp(&node
->p
, p
) > 0);
720 route_lock_node(node
);
726 * route_table_iter_init
728 void route_table_iter_init(route_table_iter_t
*iter
, struct route_table
*table
)
730 memset(iter
, 0, sizeof(*iter
));
731 iter
->state
= RT_ITER_STATE_INIT
;
736 * route_table_iter_pause
738 * Pause an iteration over the table. This allows the iteration to be
739 * resumed point after arbitrary additions/deletions from the table.
740 * An iteration can be resumed by just calling route_table_iter_next()
743 void route_table_iter_pause(route_table_iter_t
*iter
)
745 switch (iter
->state
) {
747 case RT_ITER_STATE_INIT
:
748 case RT_ITER_STATE_PAUSED
:
749 case RT_ITER_STATE_DONE
:
752 case RT_ITER_STATE_ITERATING
:
755 * Save the prefix that we are currently at. The next call to
756 * route_table_iter_next() will return the node after this
760 prefix_copy(&iter
->pause_prefix
, &iter
->current
->p
);
761 route_unlock_node(iter
->current
);
762 iter
->current
= NULL
;
763 iter
->state
= RT_ITER_STATE_PAUSED
;
772 * route_table_iter_cleanup
774 * Release any resources held by the iterator.
776 void route_table_iter_cleanup(route_table_iter_t
*iter
)
778 if (iter
->state
== RT_ITER_STATE_ITERATING
) {
779 route_unlock_node(iter
->current
);
780 iter
->current
= NULL
;
782 assert(!iter
->current
);
785 * Set the state to RT_ITER_STATE_DONE to make any
786 * route_table_iter_next() calls on this iterator return NULL.
788 iter
->state
= RT_ITER_STATE_DONE
;