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
*);
39 * route_table_init_with_delegate
42 route_table_init_with_delegate (route_table_delegate_t
*delegate
)
44 struct route_table
*rt
;
46 rt
= XCALLOC (MTYPE_ROUTE_TABLE
, sizeof (struct route_table
));
47 rt
->delegate
= delegate
;
52 route_table_finish (struct route_table
*rt
)
54 route_table_free (rt
);
57 /* Allocate new route node. */
58 static struct route_node
*
59 route_node_new (struct route_table
*table
)
61 return table
->delegate
->create_node (table
->delegate
, table
);
64 /* Allocate new route node with prefix set. */
65 static struct route_node
*
66 route_node_set (struct route_table
*table
, const struct prefix
*prefix
)
68 struct route_node
*node
;
70 node
= route_node_new (table
);
72 prefix_copy (&node
->p
, prefix
);
78 /* Free route node. */
80 route_node_free (struct route_table
*table
, struct route_node
*node
)
83 table
->cleanup(table
, node
);
84 table
->delegate
->destroy_node (table
->delegate
, table
, node
);
87 /* Free route table. */
89 route_table_free (struct route_table
*rt
)
91 struct route_node
*tmp_node
;
92 struct route_node
*node
;
99 /* Bulk deletion of nodes remaining in this table. This function is not
100 called until workers have completed their dependency on this table.
101 A final route_unlock_node() will not be called for these nodes. */
112 node
= node
->l_right
;
119 tmp_node
->table
->count
--;
120 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
121 route_node_free (rt
, tmp_node
);
125 if (node
->l_left
== tmp_node
)
128 node
->l_right
= NULL
;
136 assert (rt
->count
== 0);
138 XFREE (MTYPE_ROUTE_TABLE
, rt
);
142 /* Utility mask array. */
143 static const u_char maskbit
[] =
145 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
148 /* Common prefix route genaration. */
150 route_common (const struct prefix
*n
, const struct prefix
*p
, struct prefix
*new)
156 const u_char
*np
= (const u_char
*)&n
->u
.prefix
;
157 const u_char
*pp
= (const u_char
*)&p
->u
.prefix
;
158 u_char
*newp
= (u_char
*)&new->u
.prefix
;
160 for (i
= 0; i
< p
->prefixlen
/ 8; i
++)
168 new->prefixlen
= i
* 8;
170 if (new->prefixlen
!= p
->prefixlen
)
172 diff
= np
[i
] ^ pp
[i
];
174 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
))
179 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
184 set_link (struct route_node
*node
, struct route_node
*new)
186 unsigned int bit
= prefix_bit (&new->p
.u
.prefix
, node
->p
.prefixlen
);
188 node
->link
[bit
] = new;
194 route_lock_node (struct route_node
*node
)
202 route_unlock_node (struct route_node
*node
)
204 assert (node
->lock
> 0);
208 route_node_delete (node
);
211 /* Find matched prefix. */
213 route_node_match (const struct route_table
*table
, union prefixconstptr pu
)
215 const struct prefix
*p
= pu
.p
;
216 struct route_node
*node
;
217 struct route_node
*matched
;
222 /* Walk down tree. If there is matched route then store it to
224 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
&&
225 prefix_match (&node
->p
, p
))
230 if (node
->p
.prefixlen
== p
->prefixlen
)
233 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
236 /* If matched route found, return it. */
238 return route_lock_node (matched
);
244 route_node_match_ipv4 (const struct route_table
*table
,
245 const struct in_addr
*addr
)
247 struct prefix_ipv4 p
;
249 memset (&p
, 0, sizeof (struct prefix_ipv4
));
251 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
254 return route_node_match (table
, (struct prefix
*) &p
);
258 route_node_match_ipv6 (const struct route_table
*table
,
259 const struct in6_addr
*addr
)
261 struct prefix_ipv6 p
;
263 memset (&p
, 0, sizeof (struct prefix_ipv6
));
265 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
268 return route_node_match (table
, (struct prefix
*) &p
);
271 /* Lookup same prefix node. Return NULL when we can't find route. */
273 route_node_lookup (const struct route_table
*table
, union prefixconstptr pu
)
275 const struct prefix
*p
= pu
.p
;
276 struct route_node
*node
;
277 u_char prefixlen
= p
->prefixlen
;
278 const u_char
*prefix
= &p
->u
.prefix
;
282 while (node
&& node
->p
.prefixlen
<= prefixlen
&&
283 prefix_match (&node
->p
, p
))
285 if (node
->p
.prefixlen
== prefixlen
)
286 return node
->info
? route_lock_node (node
) : NULL
;
288 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
294 /* Lookup same prefix node. Return NULL when we can't find route. */
296 route_node_lookup_maynull (const struct route_table
*table
, union prefixconstptr pu
)
298 const struct prefix
*p
= pu
.p
;
299 struct route_node
*node
;
300 u_char prefixlen
= p
->prefixlen
;
301 const u_char
*prefix
= &p
->u
.prefix
;
305 while (node
&& node
->p
.prefixlen
<= prefixlen
&&
306 prefix_match (&node
->p
, p
))
308 if (node
->p
.prefixlen
== prefixlen
)
309 return route_lock_node (node
);
311 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
317 /* Add node to routing table. */
319 route_node_get (struct route_table
*const table
, union prefixconstptr pu
)
321 const struct prefix
*p
= pu
.p
;
322 struct route_node
*new;
323 struct route_node
*node
;
324 struct route_node
*match
;
325 u_char prefixlen
= p
->prefixlen
;
326 const u_char
*prefix
= &p
->u
.prefix
;
330 while (node
&& node
->p
.prefixlen
<= prefixlen
&&
331 prefix_match (&node
->p
, p
))
333 if (node
->p
.prefixlen
== prefixlen
)
334 return route_lock_node (node
);
337 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
342 new = route_node_set (table
, p
);
344 set_link (match
, new);
350 new = route_node_new (table
);
351 route_common (&node
->p
, p
, &new->p
);
352 new->p
.family
= p
->family
;
354 set_link (new, node
);
357 set_link (match
, new);
361 if (new->p
.prefixlen
!= p
->prefixlen
)
364 new = route_node_set (table
, p
);
365 set_link (match
, new);
370 route_lock_node (new);
375 /* Delete node from the routing table. */
377 route_node_delete (struct route_node
*node
)
379 struct route_node
*child
;
380 struct route_node
*parent
;
382 assert (node
->lock
== 0);
383 assert (node
->info
== NULL
);
385 if (node
->l_left
&& node
->l_right
)
389 child
= node
->l_left
;
391 child
= node
->l_right
;
393 parent
= node
->parent
;
396 child
->parent
= parent
;
400 if (parent
->l_left
== node
)
401 parent
->l_left
= child
;
403 parent
->l_right
= child
;
406 node
->table
->top
= child
;
408 node
->table
->count
--;
410 /* WARNING: FRAGILE CODE!
411 * route_node_free may have the side effect of free'ing the entire table.
412 * this is permitted only if table->count got decremented to zero above,
413 * because in that case parent will also be NULL, so that we won't try to
414 * delete a now-stale parent below.
416 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
418 route_node_free (node
->table
, node
);
420 /* If parent node is stub then delete it also. */
421 if (parent
&& parent
->lock
== 0)
422 route_node_delete (parent
);
425 /* Get fist node and lock it. This function is useful when one want
426 to lookup all the node exist in the routing table. */
428 route_top (struct route_table
*table
)
430 /* If there is no node in the routing table return NULL. */
431 if (table
->top
== NULL
)
434 /* Lock the top node and return it. */
435 route_lock_node (table
->top
);
439 /* Unlock current node and lock next node then return it. */
441 route_next (struct route_node
*node
)
443 struct route_node
*next
;
444 struct route_node
*start
;
446 /* Node may be deleted from route_unlock_node so we have to preserve
447 next node's pointer. */
452 route_lock_node (next
);
453 route_unlock_node (node
);
458 next
= node
->l_right
;
459 route_lock_node (next
);
460 route_unlock_node (node
);
467 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
469 next
= node
->parent
->l_right
;
470 route_lock_node (next
);
471 route_unlock_node (start
);
476 route_unlock_node (start
);
480 /* Unlock current node and lock next node until limit. */
482 route_next_until (struct route_node
*node
, const struct route_node
*limit
)
484 struct route_node
*next
;
485 struct route_node
*start
;
487 /* Node may be deleted from route_unlock_node so we have to preserve
488 next node's pointer. */
493 route_lock_node (next
);
494 route_unlock_node (node
);
499 next
= node
->l_right
;
500 route_lock_node (next
);
501 route_unlock_node (node
);
506 while (node
->parent
&& node
!= limit
)
508 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
510 next
= node
->parent
->l_right
;
511 route_lock_node (next
);
512 route_unlock_node (start
);
517 route_unlock_node (start
);
522 route_table_count (const struct route_table
*table
)
530 * Default function for creating a route node.
533 route_node_create (route_table_delegate_t
*delegate
,
534 struct route_table
*table
)
536 struct route_node
*node
;
537 node
= XCALLOC (MTYPE_ROUTE_NODE
, sizeof (struct route_node
));
544 * Default function for destroying a route node.
547 route_node_destroy (route_table_delegate_t
*delegate
,
548 struct route_table
*table
, struct route_node
*node
)
550 XFREE (MTYPE_ROUTE_NODE
, node
);
556 static route_table_delegate_t default_delegate
= {
557 .create_node
= route_node_create
,
558 .destroy_node
= route_node_destroy
561 route_table_delegate_t
*
562 route_table_get_default_delegate(void)
564 return &default_delegate
;
571 route_table_init (void)
573 return route_table_init_with_delegate (&default_delegate
);
577 * route_table_prefix_iter_cmp
579 * Compare two prefixes according to the order in which they appear in
580 * an iteration over a tree.
582 * @return -1 if p1 occurs before p2 (p1 < p2)
583 * 0 if the prefixes are identical (p1 == p2)
584 * +1 if p1 occurs after p2 (p1 > p2)
587 route_table_prefix_iter_cmp (const struct prefix
*p1
, const struct prefix
*p2
)
589 struct prefix common_space
;
590 struct prefix
*common
= &common_space
;
592 if (p1
->prefixlen
<= p2
->prefixlen
)
594 if (prefix_match (p1
, p2
))
598 * p1 contains p2, or is equal to it.
600 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
607 * Check if p2 contains p1.
609 if (prefix_match (p2
, p1
))
613 route_common (p1
, p2
, common
);
614 assert (common
->prefixlen
< p1
->prefixlen
);
615 assert (common
->prefixlen
< p2
->prefixlen
);
618 * Both prefixes are longer than the common prefix.
620 * We need to check the bit after the common prefixlen to determine
621 * which one comes later.
623 if (prefix_bit (&p1
->u
.prefix
, common
->prefixlen
))
627 * We branch to the right to get to p1 from the common prefix.
629 assert (!prefix_bit (&p2
->u
.prefix
, common
->prefixlen
));
634 * We branch to the right to get to p2 from the common prefix.
636 assert (prefix_bit (&p2
->u
.prefix
, common
->prefixlen
));
641 * route_get_subtree_next
643 * Helper function that returns the first node that follows the nodes
644 * in the sub-tree under 'node' in iteration order.
646 static struct route_node
*
647 route_get_subtree_next (struct route_node
*node
)
651 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
652 return node
->parent
->l_right
;
661 * route_table_get_next_internal
663 * Helper function to find the node that occurs after the given prefix in
664 * order of iteration.
666 * @see route_table_get_next
668 static struct route_node
*
669 route_table_get_next_internal (const struct route_table
*table
,
670 const struct prefix
*p
)
672 struct route_node
*node
, *tmp_node
;
681 if (node
->p
.prefixlen
< p
->prefixlen
)
682 match
= prefix_match (&node
->p
, p
);
684 match
= prefix_match (p
, &node
->p
);
688 if (node
->p
.prefixlen
== p
->prefixlen
)
692 * The prefix p exists in the tree, just return the next
695 route_lock_node (node
);
696 node
= route_next (node
);
698 route_unlock_node (node
);
703 if (node
->p
.prefixlen
> p
->prefixlen
)
707 * Node is in the subtree of p, and hence greater than p.
713 * p is in the sub-tree under node.
715 tmp_node
= node
->link
[prefix_bit (&p
->u
.prefix
, node
->p
.prefixlen
)];
724 * There are no nodes in the direction where p should be. If
725 * node has a right child, then it must be greater than p.
728 return node
->l_right
;
731 * No more children to follow, go upwards looking for the next
734 return route_get_subtree_next (node
);
738 * Neither node prefix nor 'p' contains the other.
740 cmp
= route_table_prefix_iter_cmp (&node
->p
, p
);
745 * Node follows p in iteration order. Return it.
753 * Node and the subtree under it come before prefix p in
754 * iteration order. Prefix p and its sub-tree are not present in
755 * the tree. Go upwards and find the first node that follows the
756 * subtree. That node will also succeed p.
758 return route_get_subtree_next (node
);
765 * route_table_get_next
767 * Find the node that occurs after the given prefix in order of
771 route_table_get_next (const struct route_table
*table
, union prefixconstptr pu
)
773 const struct prefix
*p
= pu
.p
;
774 struct route_node
*node
;
776 node
= route_table_get_next_internal (table
, p
);
779 assert (route_table_prefix_iter_cmp (&node
->p
, p
) > 0);
780 route_lock_node (node
);
786 * route_table_iter_init
789 route_table_iter_init (route_table_iter_t
* iter
, struct route_table
*table
)
791 memset (iter
, 0, sizeof (*iter
));
792 iter
->state
= RT_ITER_STATE_INIT
;
797 * route_table_iter_pause
799 * Pause an iteration over the table. This allows the iteration to be
800 * resumed point after arbitrary additions/deletions from the table.
801 * An iteration can be resumed by just calling route_table_iter_next()
805 route_table_iter_pause (route_table_iter_t
* iter
)
810 case RT_ITER_STATE_INIT
:
811 case RT_ITER_STATE_PAUSED
:
812 case RT_ITER_STATE_DONE
:
815 case RT_ITER_STATE_ITERATING
:
818 * Save the prefix that we are currently at. The next call to
819 * route_table_iter_next() will return the node after this prefix
822 prefix_copy (&iter
->pause_prefix
, &iter
->current
->p
);
823 route_unlock_node (iter
->current
);
824 iter
->current
= NULL
;
825 iter
->state
= RT_ITER_STATE_PAUSED
;
835 * route_table_iter_cleanup
837 * Release any resources held by the iterator.
840 route_table_iter_cleanup (route_table_iter_t
* iter
)
842 if (iter
->state
== RT_ITER_STATE_ITERATING
)
844 route_unlock_node (iter
->current
);
845 iter
->current
= NULL
;
847 assert (!iter
->current
);
850 * Set the state to RT_ITER_STATE_DONE to make any
851 * route_table_iter_next() calls on this iterator return NULL.
853 iter
->state
= RT_ITER_STATE_DONE
;