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
27 #include "sockunion.h"
29 DEFINE_MTYPE( LIB
, ROUTE_TABLE
, "Route table")
30 DEFINE_MTYPE( LIB
, ROUTE_NODE
, "Route node")
32 static void route_node_delete (struct route_node
*);
33 static void route_table_free (struct route_table
*);
37 * route_table_init_with_delegate
40 route_table_init_with_delegate (route_table_delegate_t
*delegate
)
42 struct route_table
*rt
;
44 rt
= XCALLOC (MTYPE_ROUTE_TABLE
, sizeof (struct route_table
));
45 rt
->delegate
= delegate
;
50 route_table_finish (struct route_table
*rt
)
52 route_table_free (rt
);
55 /* Allocate new route node. */
56 static struct route_node
*
57 route_node_new (struct route_table
*table
)
59 return table
->delegate
->create_node (table
->delegate
, table
);
62 /* Allocate new route node with prefix set. */
63 static struct route_node
*
64 route_node_set (struct route_table
*table
, const struct prefix
*prefix
)
66 struct route_node
*node
;
68 node
= route_node_new (table
);
70 prefix_copy (&node
->p
, prefix
);
76 /* Free route node. */
78 route_node_free (struct route_table
*table
, struct route_node
*node
)
81 table
->cleanup(table
, node
);
82 table
->delegate
->destroy_node (table
->delegate
, table
, node
);
85 /* Free route table. */
87 route_table_free (struct route_table
*rt
)
89 struct route_node
*tmp_node
;
90 struct route_node
*node
;
97 /* Bulk deletion of nodes remaining in this table. This function is not
98 called until workers have completed their dependency on this table.
99 A final route_unlock_node() will not be called for these nodes. */
110 node
= node
->l_right
;
117 tmp_node
->table
->count
--;
118 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
119 route_node_free (rt
, tmp_node
);
123 if (node
->l_left
== tmp_node
)
126 node
->l_right
= NULL
;
134 assert (rt
->count
== 0);
136 XFREE (MTYPE_ROUTE_TABLE
, rt
);
140 /* Utility mask array. */
141 static const u_char maskbit
[] =
143 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
146 /* Common prefix route genaration. */
148 route_common (const struct prefix
*n
, const struct prefix
*p
, struct prefix
*new)
154 const u_char
*np
= (const u_char
*)&n
->u
.prefix
;
155 const u_char
*pp
= (const u_char
*)&p
->u
.prefix
;
156 u_char
*newp
= (u_char
*)&new->u
.prefix
;
158 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
166 new->prefixlen
= i
* 8;
168 if (new->prefixlen
!= p
->prefixlen
)
170 diff
= np
[i
] ^ pp
[i
];
172 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
177 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
182 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;
192 route_lock_node (struct route_node
*node
)
200 route_unlock_node (struct route_node
*node
)
202 assert (node
->lock
> 0);
206 route_node_delete (node
);
209 /* Find matched prefix. */
211 route_node_match (const struct route_table
*table
, const struct prefix
*p
)
213 struct route_node
*node
;
214 struct route_node
*matched
;
219 /* Walk down tree. If there is matched route then store it to
221 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
222 prefix_match (&node
->p
, p
))
227 if (node
->p
.prefixlen
== p
->prefixlen
)
230 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
233 /* If matched route found, return it. */
235 return route_lock_node (matched
);
241 route_node_match_ipv4 (const struct route_table
*table
,
242 const struct in_addr
*addr
)
244 struct prefix_ipv4 p
;
246 memset (&p
, 0, sizeof (struct prefix_ipv4
));
248 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
251 return route_node_match (table
, (struct prefix
*) &p
);
255 route_node_match_ipv6 (const struct route_table
*table
,
256 const struct in6_addr
*addr
)
258 struct prefix_ipv6 p
;
260 memset (&p
, 0, sizeof (struct prefix_ipv6
));
262 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
265 return route_node_match (table
, (struct prefix
*) &p
);
268 /* Lookup same prefix node. Return NULL when we can't find route. */
270 route_node_lookup (const struct route_table
*table
, const struct prefix
*p
)
272 struct route_node
*node
;
273 u_char prefixlen
= p
->prefixlen
;
274 const u_char
*prefix
= &p
->u
.prefix
;
278 while (node
&& node
->p
.prefixlen
<= prefixlen
&&
279 prefix_match (&node
->p
, p
))
281 if (node
->p
.prefixlen
== prefixlen
)
282 return node
->info
? route_lock_node (node
) : NULL
;
284 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
290 /* Lookup same prefix node. Return NULL when we can't find route. */
292 route_node_lookup_maynull (const struct route_table
*table
, const struct prefix
*p
)
294 struct route_node
*node
;
295 u_char prefixlen
= p
->prefixlen
;
296 const u_char
*prefix
= &p
->u
.prefix
;
300 while (node
&& node
->p
.prefixlen
<= prefixlen
&&
301 prefix_match (&node
->p
, p
))
303 if (node
->p
.prefixlen
== prefixlen
)
304 return route_lock_node (node
);
306 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
312 /* Add node to routing table. */
314 route_node_get (struct route_table
*const table
, const struct prefix
*p
)
316 struct route_node
*new;
317 struct route_node
*node
;
318 struct route_node
*match
;
319 u_char prefixlen
= p
->prefixlen
;
320 const u_char
*prefix
= &p
->u
.prefix
;
324 while (node
&& node
->p
.prefixlen
<= prefixlen
&&
325 prefix_match (&node
->p
, p
))
327 if (node
->p
.prefixlen
== prefixlen
)
328 return route_lock_node (node
);
331 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
336 new = route_node_set (table
, p
);
338 set_link (match
, new);
344 new = route_node_new (table
);
345 route_common (&node
->p
, p
, &new->p
);
346 new->p
.family
= p
->family
;
348 set_link (new, node
);
351 set_link (match
, new);
355 if (new->p
.prefixlen
!= p
->prefixlen
)
358 new = route_node_set (table
, p
);
359 set_link (match
, new);
364 route_lock_node (new);
369 /* Delete node from the routing table. */
371 route_node_delete (struct route_node
*node
)
373 struct route_node
*child
;
374 struct route_node
*parent
;
376 assert (node
->lock
== 0);
377 assert (node
->info
== NULL
);
379 if (node
->l_left
&& node
->l_right
)
383 child
= node
->l_left
;
385 child
= node
->l_right
;
387 parent
= node
->parent
;
390 child
->parent
= parent
;
394 if (parent
->l_left
== node
)
395 parent
->l_left
= child
;
397 parent
->l_right
= child
;
400 node
->table
->top
= child
;
402 node
->table
->count
--;
404 /* WARNING: FRAGILE CODE!
405 * route_node_free may have the side effect of free'ing the entire table.
406 * this is permitted only if table->count got decremented to zero above,
407 * because in that case parent will also be NULL, so that we won't try to
408 * delete a now-stale parent below.
410 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
412 route_node_free (node
->table
, node
);
414 /* If parent node is stub then delete it also. */
415 if (parent
&& parent
->lock
== 0)
416 route_node_delete (parent
);
419 /* Get fist node and lock it. This function is useful when one want
420 to lookup all the node exist in the routing table. */
422 route_top (struct route_table
*table
)
424 /* If there is no node in the routing table return NULL. */
425 if (table
->top
== NULL
)
428 /* Lock the top node and return it. */
429 route_lock_node (table
->top
);
433 /* Unlock current node and lock next node then return it. */
435 route_next (struct route_node
*node
)
437 struct route_node
*next
;
438 struct route_node
*start
;
440 /* Node may be deleted from route_unlock_node so we have to preserve
441 next node's pointer. */
446 route_lock_node (next
);
447 route_unlock_node (node
);
452 next
= node
->l_right
;
453 route_lock_node (next
);
454 route_unlock_node (node
);
461 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 /* Unlock current node and lock next node until limit. */
476 route_next_until (struct route_node
*node
, struct route_node
*limit
)
478 struct route_node
*next
;
479 struct route_node
*start
;
481 /* Node may be deleted from route_unlock_node so we have to preserve
482 next node's pointer. */
487 route_lock_node (next
);
488 route_unlock_node (node
);
493 next
= node
->l_right
;
494 route_lock_node (next
);
495 route_unlock_node (node
);
500 while (node
->parent
&& node
!= limit
)
502 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
504 next
= node
->parent
->l_right
;
505 route_lock_node (next
);
506 route_unlock_node (start
);
511 route_unlock_node (start
);
516 route_table_count (const struct route_table
*table
)
524 * Default function for creating a route node.
527 route_node_create (route_table_delegate_t
*delegate
,
528 struct route_table
*table
)
530 struct route_node
*node
;
531 node
= XCALLOC (MTYPE_ROUTE_NODE
, sizeof (struct route_node
));
538 * Default function for destroying a route node.
541 route_node_destroy (route_table_delegate_t
*delegate
,
542 struct route_table
*table
, struct route_node
*node
)
544 XFREE (MTYPE_ROUTE_NODE
, node
);
550 static route_table_delegate_t default_delegate
= {
551 .create_node
= route_node_create
,
552 .destroy_node
= route_node_destroy
555 route_table_delegate_t
*
556 route_table_get_default_delegate(void)
558 return &default_delegate
;
565 route_table_init (void)
567 return route_table_init_with_delegate (&default_delegate
);
571 * route_table_prefix_iter_cmp
573 * Compare two prefixes according to the order in which they appear in
574 * an iteration over a tree.
576 * @return -1 if p1 occurs before p2 (p1 < p2)
577 * 0 if the prefixes are identical (p1 == p2)
578 * +1 if p1 occurs after p2 (p1 > p2)
581 route_table_prefix_iter_cmp (struct prefix
*p1
, struct prefix
*p2
)
583 struct prefix common_space
;
584 struct prefix
*common
= &common_space
;
586 if (p1
->prefixlen
<= p2
->prefixlen
)
588 if (prefix_match (p1
, p2
))
592 * p1 contains p2, or is equal to it.
594 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
601 * Check if p2 contains p1.
603 if (prefix_match (p2
, p1
))
607 route_common (p1
, p2
, common
);
608 assert (common
->prefixlen
< p1
->prefixlen
);
609 assert (common
->prefixlen
< p2
->prefixlen
);
612 * Both prefixes are longer than the common prefix.
614 * We need to check the bit after the common prefixlen to determine
615 * which one comes later.
617 if (prefix_bit (&p1
->u
.prefix
, common
->prefixlen
))
621 * We branch to the right to get to p1 from the common prefix.
623 assert (!prefix_bit (&p2
->u
.prefix
, common
->prefixlen
));
628 * We branch to the right to get to p2 from the common prefix.
630 assert (prefix_bit (&p2
->u
.prefix
, common
->prefixlen
));
635 * route_get_subtree_next
637 * Helper function that returns the first node that follows the nodes
638 * in the sub-tree under 'node' in iteration order.
640 static struct route_node
*
641 route_get_subtree_next (struct route_node
*node
)
645 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
646 return node
->parent
->l_right
;
655 * route_table_get_next_internal
657 * Helper function to find the node that occurs after the given prefix in
658 * order of iteration.
660 * @see route_table_get_next
662 static struct route_node
*
663 route_table_get_next_internal (const struct route_table
*table
,
666 struct route_node
*node
, *tmp_node
;
675 if (node
->p
.prefixlen
< p
->prefixlen
)
676 match
= prefix_match (&node
->p
, p
);
678 match
= prefix_match (p
, &node
->p
);
682 if (node
->p
.prefixlen
== p
->prefixlen
)
686 * The prefix p exists in the tree, just return the next
689 route_lock_node (node
);
690 node
= route_next (node
);
692 route_unlock_node (node
);
697 if (node
->p
.prefixlen
> p
->prefixlen
)
701 * Node is in the subtree of p, and hence greater than p.
707 * p is in the sub-tree under node.
709 tmp_node
= node
->link
[prefix_bit (&p
->u
.prefix
, node
->p
.prefixlen
)];
718 * There are no nodes in the direction where p should be. If
719 * node has a right child, then it must be greater than p.
722 return node
->l_right
;
725 * No more children to follow, go upwards looking for the next
728 return route_get_subtree_next (node
);
732 * Neither node prefix nor 'p' contains the other.
734 cmp
= route_table_prefix_iter_cmp (&node
->p
, p
);
739 * Node follows p in iteration order. Return it.
747 * Node and the subtree under it come before prefix p in
748 * iteration order. Prefix p and its sub-tree are not present in
749 * the tree. Go upwards and find the first node that follows the
750 * subtree. That node will also succeed p.
752 return route_get_subtree_next (node
);
759 * route_table_get_next
761 * Find the node that occurs after the given prefix in order of
765 route_table_get_next (const struct route_table
*table
, struct prefix
*p
)
767 struct route_node
*node
;
769 node
= route_table_get_next_internal (table
, p
);
772 assert (route_table_prefix_iter_cmp (&node
->p
, p
) > 0);
773 route_lock_node (node
);
779 * route_table_iter_init
782 route_table_iter_init (route_table_iter_t
* iter
, struct route_table
*table
)
784 memset (iter
, 0, sizeof (*iter
));
785 iter
->state
= RT_ITER_STATE_INIT
;
790 * route_table_iter_pause
792 * Pause an iteration over the table. This allows the iteration to be
793 * resumed point after arbitrary additions/deletions from the table.
794 * An iteration can be resumed by just calling route_table_iter_next()
798 route_table_iter_pause (route_table_iter_t
* iter
)
803 case RT_ITER_STATE_INIT
:
804 case RT_ITER_STATE_PAUSED
:
805 case RT_ITER_STATE_DONE
:
808 case RT_ITER_STATE_ITERATING
:
811 * Save the prefix that we are currently at. The next call to
812 * route_table_iter_next() will return the node after this prefix
815 prefix_copy (&iter
->pause_prefix
, &iter
->current
->p
);
816 route_unlock_node (iter
->current
);
817 iter
->current
= NULL
;
818 iter
->state
= RT_ITER_STATE_PAUSED
;
828 * route_table_iter_cleanup
830 * Release any resources held by the iterator.
833 route_table_iter_cleanup (route_table_iter_t
* iter
)
835 if (iter
->state
== RT_ITER_STATE_ITERATING
)
837 route_unlock_node (iter
->current
);
838 iter
->current
= NULL
;
840 assert (!iter
->current
);
843 * Set the state to RT_ITER_STATE_DONE to make any
844 * route_table_iter_next() calls on this iterator return NULL.
846 iter
->state
= RT_ITER_STATE_DONE
;