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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "sockunion.h"
30 static void route_node_delete (struct route_node *);
31 static void route_table_free (struct route_table *);
35 * route_table_init_with_delegate
38 route_table_init_with_delegate (route_table_delegate_t *delegate)
40 struct route_table *rt;
42 rt = XCALLOC (MTYPE_ROUTE_TABLE, sizeof (struct route_table));
43 rt->delegate = delegate;
48 route_table_finish (struct route_table *rt)
50 route_table_free (rt);
53 /* Allocate new route node. */
54 static struct route_node *
55 route_node_new (struct route_table *table)
57 return table->delegate->create_node (table->delegate, table);
60 /* Allocate new route node with prefix set. */
61 static struct route_node *
62 route_node_set (struct route_table *table, const struct prefix *prefix)
64 struct route_node *node;
66 node = route_node_new (table);
68 prefix_copy (&node->p, prefix);
74 /* Free route node. */
76 route_node_free (struct route_table *table, struct route_node *node)
78 table->delegate->destroy_node (table->delegate, table, node);
81 /* Free route table. */
83 route_table_free (struct route_table *rt)
85 struct route_node *tmp_node;
86 struct route_node *node;
93 /* Bulk deletion of nodes remaining in this table. This function is not
94 called until workers have completed their dependency on this table.
95 A final route_unlock_node() will not be called for these nodes. */
106 node = node->l_right;
113 tmp_node->table->count--;
114 tmp_node->lock = 0; /* to cause assert if unlocked after this */
115 route_node_free (rt, tmp_node);
119 if (node->l_left == tmp_node)
122 node->l_right = NULL;
130 assert (rt->count == 0);
132 XFREE (MTYPE_ROUTE_TABLE, rt);
136 /* Utility mask array. */
137 static const u_char maskbit[] =
139 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
142 /* Common prefix route genaration. */
144 route_common (const struct prefix *n, const struct prefix *p, struct prefix *new)
150 const u_char *np = (const u_char *)&n->u.prefix;
151 const u_char *pp = (const u_char *)&p->u.prefix;
152 u_char *newp = (u_char *)&new->u.prefix;
154 for (i = 0; i < p->prefixlen / 8; i++)
162 new->prefixlen = i * 8;
164 if (new->prefixlen != p->prefixlen)
166 diff = np[i] ^ pp[i];
168 while (new->prefixlen < p->prefixlen && !(mask & diff))
173 newp[i] = np[i] & maskbit[new->prefixlen % 8];
178 set_link (struct route_node *node, struct route_node *new)
180 unsigned int bit = prefix_bit (&new->p.u.prefix, node->p.prefixlen);
182 node->link[bit] = new;
188 route_lock_node (struct route_node *node)
196 route_unlock_node (struct route_node *node)
198 assert (node->lock > 0);
202 route_node_delete (node);
205 /* Find matched prefix. */
207 route_node_match (const struct route_table *table, const struct prefix *p)
209 struct route_node *node;
210 struct route_node *matched;
215 /* Walk down tree. If there is matched route then store it to
217 while (node && node->p.prefixlen <= p->prefixlen &&
218 prefix_match (&node->p, p))
223 if (node->p.prefixlen == p->prefixlen)
226 node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)];
229 /* If matched route found, return it. */
231 return route_lock_node (matched);
237 route_node_match_ipv4 (const struct route_table *table,
238 const struct in_addr *addr)
240 struct prefix_ipv4 p;
242 memset (&p, 0, sizeof (struct prefix_ipv4));
244 p.prefixlen = IPV4_MAX_PREFIXLEN;
247 return route_node_match (table, (struct prefix *) &p);
252 route_node_match_ipv6 (const struct route_table *table,
253 const struct in6_addr *addr)
255 struct prefix_ipv6 p;
257 memset (&p, 0, sizeof (struct prefix_ipv6));
259 p.prefixlen = IPV6_MAX_PREFIXLEN;
262 return route_node_match (table, (struct prefix *) &p);
264 #endif /* HAVE_IPV6 */
266 /* Lookup same prefix node. Return NULL when we can't find route. */
268 route_node_lookup (const struct route_table *table, const struct prefix *p)
270 struct route_node *node;
271 u_char prefixlen = p->prefixlen;
272 const u_char *prefix = &p->u.prefix;
276 while (node && node->p.prefixlen <= prefixlen &&
277 prefix_match (&node->p, p))
279 if (node->p.prefixlen == prefixlen)
280 return node->info ? route_lock_node (node) : NULL;
282 node = node->link[prefix_bit(prefix, node->p.prefixlen)];
288 /* Add node to routing table. */
290 route_node_get (struct route_table *const table, const struct prefix *p)
292 struct route_node *new;
293 struct route_node *node;
294 struct route_node *match;
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);
307 node = node->link[prefix_bit(prefix, node->p.prefixlen)];
312 new = route_node_set (table, p);
314 set_link (match, new);
320 new = route_node_new (table);
321 route_common (&node->p, p, &new->p);
322 new->p.family = p->family;
324 set_link (new, node);
327 set_link (match, new);
331 if (new->p.prefixlen != p->prefixlen)
334 new = route_node_set (table, p);
335 set_link (match, new);
340 route_lock_node (new);
345 /* Delete node from the routing table. */
347 route_node_delete (struct route_node *node)
349 struct route_node *child;
350 struct route_node *parent;
352 assert (node->lock == 0);
353 assert (node->info == NULL);
355 if (node->l_left && node->l_right)
359 child = node->l_left;
361 child = node->l_right;
363 parent = node->parent;
366 child->parent = parent;
370 if (parent->l_left == node)
371 parent->l_left = child;
373 parent->l_right = child;
376 node->table->top = child;
378 node->table->count--;
380 route_node_free (node->table, node);
382 /* If parent node is stub then delete it also. */
383 if (parent && parent->lock == 0)
384 route_node_delete (parent);
387 /* Get fist node and lock it. This function is useful when one want
388 to lookup all the node exist in the routing table. */
390 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. */
403 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. */
414 route_lock_node (next);
415 route_unlock_node (node);
420 next = node->l_right;
421 route_lock_node (next);
422 route_unlock_node (node);
429 if (node->parent->l_left == node && node->parent->l_right)
431 next = node->parent->l_right;
432 route_lock_node (next);
433 route_unlock_node (start);
438 route_unlock_node (start);
442 /* Unlock current node and lock next node until limit. */
444 route_next_until (struct route_node *node, struct route_node *limit)
446 struct route_node *next;
447 struct route_node *start;
449 /* Node may be deleted from route_unlock_node so we have to preserve
450 next node's pointer. */
455 route_lock_node (next);
456 route_unlock_node (node);
461 next = node->l_right;
462 route_lock_node (next);
463 route_unlock_node (node);
468 while (node->parent && node != limit)
470 if (node->parent->l_left == node && node->parent->l_right)
472 next = node->parent->l_right;
473 route_lock_node (next);
474 route_unlock_node (start);
479 route_unlock_node (start);
484 route_table_count (const struct route_table *table)
492 * Default function for creating a route node.
494 static struct route_node *
495 route_node_create (route_table_delegate_t *delegate,
496 struct route_table *table)
498 struct route_node *node;
499 node = XCALLOC (MTYPE_ROUTE_NODE, sizeof (struct route_node));
506 * Default function for destroying a route node.
509 route_node_destroy (route_table_delegate_t *delegate,
510 struct route_table *table, struct route_node *node)
512 XFREE (MTYPE_ROUTE_NODE, node);
518 static route_table_delegate_t default_delegate = {
519 .create_node = route_node_create,
520 .destroy_node = route_node_destroy
527 route_table_init (void)
529 return route_table_init_with_delegate (&default_delegate);
533 * route_table_prefix_iter_cmp
535 * Compare two prefixes according to the order in which they appear in
536 * an iteration over a tree.
538 * @return -1 if p1 occurs before p2 (p1 < p2)
539 * 0 if the prefixes are identical (p1 == p2)
540 * +1 if p1 occurs after p2 (p1 > p2)
543 route_table_prefix_iter_cmp (struct prefix *p1, struct prefix *p2)
545 struct prefix common_space;
546 struct prefix *common = &common_space;
548 if (p1->prefixlen <= p2->prefixlen)
550 if (prefix_match (p1, p2))
554 * p1 contains p2, or is equal to it.
556 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))
583 * We branch to the right to get to p1 from the common prefix.
585 assert (!prefix_bit (&p2->u.prefix, common->prefixlen));
590 * We branch to the right to get to p2 from the common prefix.
592 assert (prefix_bit (&p2->u.prefix, common->prefixlen));
597 * route_get_subtree_next
599 * Helper function that returns the first node that follows the nodes
600 * in the sub-tree under 'node' in iteration order.
602 static struct route_node *
603 route_get_subtree_next (struct route_node *node)
607 if (node->parent->l_left == node && node->parent->l_right)
608 return node->parent->l_right;
617 * route_table_get_next_internal
619 * Helper function to find the node that occurs after the given prefix in
620 * order of iteration.
622 * @see route_table_get_next
624 static struct route_node *
625 route_table_get_next_internal (const struct route_table *table,
628 struct route_node *node, *tmp_node;
637 if (node->p.prefixlen < p->prefixlen)
638 match = prefix_match (&node->p, p);
640 match = prefix_match (p, &node->p);
644 if (node->p.prefixlen == p->prefixlen)
648 * The prefix p exists in the tree, just return the next
651 route_lock_node (node);
652 node = route_next (node);
654 route_unlock_node (node);
659 if (node->p.prefixlen > p->prefixlen)
663 * Node is in the subtree of p, and hence greater than p.
669 * p is in the sub-tree under node.
671 tmp_node = node->link[prefix_bit (&p->u.prefix, node->p.prefixlen)];
680 * There are no nodes in the direction where p should be. If
681 * node has a right child, then it must be greater than p.
684 return node->l_right;
687 * No more children to follow, go upwards looking for the next
690 return route_get_subtree_next (node);
694 * Neither node prefix nor 'p' contains the other.
696 cmp = route_table_prefix_iter_cmp (&node->p, p);
701 * Node follows p in iteration order. Return it.
709 * Node and the subtree under it come before prefix p in
710 * iteration order. Prefix p and its sub-tree are not present in
711 * the tree. Go upwards and find the first node that follows the
712 * subtree. That node will also succeed p.
714 return route_get_subtree_next (node);
721 * route_table_get_next
723 * Find the node that occurs after the given prefix in order of
727 route_table_get_next (const struct route_table *table, struct prefix *p)
729 struct route_node *node;
731 node = route_table_get_next_internal (table, p);
734 assert (route_table_prefix_iter_cmp (&node->p, p) > 0);
735 route_lock_node (node);
741 * route_table_iter_init
744 route_table_iter_init (route_table_iter_t * iter, struct route_table *table)
746 memset (iter, 0, sizeof (*iter));
747 iter->state = RT_ITER_STATE_INIT;
752 * route_table_iter_pause
754 * Pause an iteration over the table. This allows the iteration to be
755 * resumed point after arbitrary additions/deletions from the table.
756 * An iteration can be resumed by just calling route_table_iter_next()
760 route_table_iter_pause (route_table_iter_t * iter)
765 case RT_ITER_STATE_INIT:
766 case RT_ITER_STATE_PAUSED:
767 case RT_ITER_STATE_DONE:
770 case RT_ITER_STATE_ITERATING:
773 * Save the prefix that we are currently at. The next call to
774 * route_table_iter_next() will return the node after this prefix
777 prefix_copy (&iter->pause_prefix, &iter->current->p);
778 route_unlock_node (iter->current);
779 iter->current = NULL;
780 iter->state = RT_ITER_STATE_PAUSED;
790 * route_table_iter_cleanup
792 * Release any resources held by the iterator.
795 route_table_iter_cleanup (route_table_iter_t * iter)
797 if (iter->state == RT_ITER_STATE_ITERATING)
799 route_unlock_node (iter->current);
800 iter->current = NULL;
802 assert (!iter->current);
805 * Set the state to RT_ITER_STATE_DONE to make any
806 * route_table_iter_next() calls on this iterator return NULL.
808 iter->state = RT_ITER_STATE_DONE;