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 u_char 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
,
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
++) {
167 new->prefixlen
= i
* 8;
169 if (new->prefixlen
!= p
->prefixlen
) {
170 diff
= np
[i
] ^ pp
[i
];
172 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
)) {
176 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
180 static void set_link(struct route_node
*node
, struct route_node
*new)
182 unsigned int bit
= prefix_bit(&new->p
.u
.prefix
, node
->p
.prefixlen
);
184 node
->link
[bit
] = new;
188 /* Find matched prefix. */
189 struct route_node
*route_node_match(const struct route_table
*table
,
190 union prefixconstptr pu
)
192 const struct prefix
*p
= pu
.p
;
193 struct route_node
*node
;
194 struct route_node
*matched
;
199 /* Walk down tree. If there is matched route then store it to
201 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
202 && prefix_match(&node
->p
, p
)) {
206 if (node
->p
.prefixlen
== p
->prefixlen
)
209 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
212 /* If matched route found, return it. */
214 return route_lock_node(matched
);
219 struct route_node
*route_node_match_ipv4(const struct route_table
*table
,
220 const struct in_addr
*addr
)
222 struct prefix_ipv4 p
;
224 memset(&p
, 0, sizeof(struct prefix_ipv4
));
226 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
229 return route_node_match(table
, (struct prefix
*)&p
);
232 struct route_node
*route_node_match_ipv6(const struct route_table
*table
,
233 const struct in6_addr
*addr
)
235 struct prefix_ipv6 p
;
237 memset(&p
, 0, sizeof(struct prefix_ipv6
));
239 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
242 return route_node_match(table
, (struct prefix
*)&p
);
245 /* Lookup same prefix node. Return NULL when we can't find route. */
246 struct route_node
*route_node_lookup(const struct route_table
*table
,
247 union prefixconstptr pu
)
250 struct route_node
*node
;
251 prefix_copy(&p
, pu
.p
);
254 node
= hash_get(table
->hash
, (void *)&p
, NULL
);
255 return (node
&& node
->info
) ? route_lock_node(node
) : NULL
;
258 /* Lookup same prefix node. Return NULL when we can't find route. */
259 struct route_node
*route_node_lookup_maynull(const struct route_table
*table
,
260 union prefixconstptr pu
)
263 struct route_node
*node
;
264 prefix_copy(&p
, pu
.p
);
267 node
= hash_get(table
->hash
, (void *)&p
, NULL
);
268 return node
? route_lock_node(node
) : NULL
;
271 /* Add node to routing table. */
272 struct route_node
*route_node_get(struct route_table
*const table
,
273 union prefixconstptr pu
)
275 const struct prefix
*p
= pu
.p
;
276 struct route_node
*new;
277 struct route_node
*node
;
278 struct route_node
*match
;
279 struct route_node
*inserted
;
280 u_char prefixlen
= p
->prefixlen
;
281 const u_char
*prefix
= &p
->u
.prefix
;
283 apply_mask((struct prefix
*)p
);
284 node
= hash_get(table
->hash
, (void *)p
, NULL
);
285 if (node
&& node
->info
)
286 return route_lock_node(node
);
290 while (node
&& node
->p
.prefixlen
<= prefixlen
291 && prefix_match(&node
->p
, p
)) {
292 if (node
->p
.prefixlen
== prefixlen
)
293 return route_lock_node(node
);
296 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
300 new = route_node_set(table
, p
);
302 set_link(match
, new);
306 new = route_node_new(table
);
307 route_common(&node
->p
, p
, &new->p
);
308 new->p
.family
= p
->family
;
311 inserted
= hash_get(node
->table
->hash
, new, hash_alloc_intern
);
312 assert(inserted
== new);
315 set_link(match
, new);
319 if (new->p
.prefixlen
!= p
->prefixlen
) {
321 new = route_node_set(table
, p
);
322 set_link(match
, new);
327 route_lock_node(new);
332 /* Delete node from the routing table. */
333 void route_node_delete(struct route_node
*node
)
335 struct route_node
*child
;
336 struct route_node
*parent
;
338 assert(node
->lock
== 0);
339 assert(node
->info
== NULL
);
341 if (node
->l_left
&& node
->l_right
)
345 child
= node
->l_left
;
347 child
= node
->l_right
;
349 parent
= node
->parent
;
352 child
->parent
= parent
;
355 if (parent
->l_left
== node
)
356 parent
->l_left
= child
;
358 parent
->l_right
= child
;
360 node
->table
->top
= child
;
362 node
->table
->count
--;
364 hash_release(node
->table
->hash
, node
);
366 /* WARNING: FRAGILE CODE!
367 * route_node_free may have the side effect of free'ing the entire
369 * this is permitted only if table->count got decremented to zero above,
370 * because in that case parent will also be NULL, so that we won't try
372 * delete a now-stale parent below.
374 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
376 route_node_free(node
->table
, node
);
378 /* If parent node is stub then delete it also. */
379 if (parent
&& parent
->lock
== 0)
380 route_node_delete(parent
);
383 /* Get fist node and lock it. This function is useful when one want
384 to lookup all the node exist in the routing table. */
385 struct route_node
*route_top(struct route_table
*table
)
387 /* If there is no node in the routing table return NULL. */
388 if (table
->top
== NULL
)
391 /* Lock the top node and return it. */
392 route_lock_node(table
->top
);
396 /* Unlock current node and lock next node then return it. */
397 struct route_node
*route_next(struct route_node
*node
)
399 struct route_node
*next
;
400 struct route_node
*start
;
402 /* Node may be deleted from route_unlock_node so we have to preserve
403 next node's pointer. */
407 route_lock_node(next
);
408 route_unlock_node(node
);
412 next
= node
->l_right
;
413 route_lock_node(next
);
414 route_unlock_node(node
);
419 while (node
->parent
) {
420 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
421 next
= node
->parent
->l_right
;
422 route_lock_node(next
);
423 route_unlock_node(start
);
428 route_unlock_node(start
);
432 /* Unlock current node and lock next node until limit. */
433 struct route_node
*route_next_until(struct route_node
*node
,
434 const struct route_node
*limit
)
436 struct route_node
*next
;
437 struct route_node
*start
;
439 /* Node may be deleted from route_unlock_node so we have to preserve
440 next node's pointer. */
444 route_lock_node(next
);
445 route_unlock_node(node
);
449 next
= node
->l_right
;
450 route_lock_node(next
);
451 route_unlock_node(node
);
456 while (node
->parent
&& node
!= limit
) {
457 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
458 next
= node
->parent
->l_right
;
459 route_lock_node(next
);
460 route_unlock_node(start
);
465 route_unlock_node(start
);
469 unsigned long route_table_count(const struct route_table
*table
)
477 * Default function for creating a route node.
479 struct route_node
*route_node_create(route_table_delegate_t
*delegate
,
480 struct route_table
*table
)
482 struct route_node
*node
;
483 node
= XCALLOC(MTYPE_ROUTE_NODE
, sizeof(struct route_node
));
490 * Default function for destroying a route node.
492 void route_node_destroy(route_table_delegate_t
*delegate
,
493 struct route_table
*table
, struct route_node
*node
)
495 XFREE(MTYPE_ROUTE_NODE
, node
);
501 static route_table_delegate_t default_delegate
= {
502 .create_node
= route_node_create
,
503 .destroy_node
= route_node_destroy
};
505 route_table_delegate_t
*route_table_get_default_delegate(void)
507 return &default_delegate
;
513 struct route_table
*route_table_init(void)
515 return route_table_init_with_delegate(&default_delegate
);
519 * route_table_prefix_iter_cmp
521 * Compare two prefixes according to the order in which they appear in
522 * an iteration over a tree.
524 * @return -1 if p1 occurs before p2 (p1 < p2)
525 * 0 if the prefixes are identical (p1 == p2)
526 * +1 if p1 occurs after p2 (p1 > p2)
528 int route_table_prefix_iter_cmp(const struct prefix
*p1
,
529 const struct prefix
*p2
)
531 struct prefix common_space
;
532 struct prefix
*common
= &common_space
;
534 if (p1
->prefixlen
<= p2
->prefixlen
) {
535 if (prefix_match(p1
, p2
)) {
538 * p1 contains p2, or is equal to it.
540 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
545 * Check if p2 contains p1.
547 if (prefix_match(p2
, p1
))
551 route_common(p1
, p2
, common
);
552 assert(common
->prefixlen
< p1
->prefixlen
);
553 assert(common
->prefixlen
< p2
->prefixlen
);
556 * Both prefixes are longer than the common prefix.
558 * We need to check the bit after the common prefixlen to determine
559 * which one comes later.
561 if (prefix_bit(&p1
->u
.prefix
, common
->prefixlen
)) {
564 * We branch to the right to get to p1 from the common prefix.
566 assert(!prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
571 * We branch to the right to get to p2 from the common prefix.
573 assert(prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
578 * route_get_subtree_next
580 * Helper function that returns the first node that follows the nodes
581 * in the sub-tree under 'node' in iteration order.
583 static struct route_node
*route_get_subtree_next(struct route_node
*node
)
585 while (node
->parent
) {
586 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
587 return node
->parent
->l_right
;
596 * route_table_get_next_internal
598 * Helper function to find the node that occurs after the given prefix in
599 * order of iteration.
601 * @see route_table_get_next
603 static struct route_node
*
604 route_table_get_next_internal(const struct route_table
*table
,
605 const struct prefix
*p
)
607 struct route_node
*node
, *tmp_node
;
615 if (node
->p
.prefixlen
< p
->prefixlen
)
616 match
= prefix_match(&node
->p
, p
);
618 match
= prefix_match(p
, &node
->p
);
621 if (node
->p
.prefixlen
== p
->prefixlen
) {
624 * The prefix p exists in the tree, just return
628 route_lock_node(node
);
629 node
= route_next(node
);
631 route_unlock_node(node
);
636 if (node
->p
.prefixlen
> p
->prefixlen
) {
639 * Node is in the subtree of p, and hence
646 * p is in the sub-tree under node.
648 tmp_node
= node
->link
[prefix_bit(&p
->u
.prefix
,
657 * There are no nodes in the direction where p should
659 * node has a right child, then it must be greater than
663 return node
->l_right
;
666 * No more children to follow, go upwards looking for
670 return route_get_subtree_next(node
);
674 * Neither node prefix nor 'p' contains the other.
676 cmp
= route_table_prefix_iter_cmp(&node
->p
, p
);
680 * Node follows p in iteration order. Return it.
688 * Node and the subtree under it come before prefix p in
689 * iteration order. Prefix p and its sub-tree are not present in
690 * the tree. Go upwards and find the first node that follows the
691 * subtree. That node will also succeed p.
693 return route_get_subtree_next(node
);
700 * route_table_get_next
702 * Find the node that occurs after the given prefix in order of
705 struct route_node
*route_table_get_next(const struct route_table
*table
,
706 union prefixconstptr pu
)
708 const struct prefix
*p
= pu
.p
;
709 struct route_node
*node
;
711 node
= route_table_get_next_internal(table
, p
);
713 assert(route_table_prefix_iter_cmp(&node
->p
, p
) > 0);
714 route_lock_node(node
);
720 * route_table_iter_init
722 void route_table_iter_init(route_table_iter_t
*iter
, struct route_table
*table
)
724 memset(iter
, 0, sizeof(*iter
));
725 iter
->state
= RT_ITER_STATE_INIT
;
730 * route_table_iter_pause
732 * Pause an iteration over the table. This allows the iteration to be
733 * resumed point after arbitrary additions/deletions from the table.
734 * An iteration can be resumed by just calling route_table_iter_next()
737 void route_table_iter_pause(route_table_iter_t
*iter
)
739 switch (iter
->state
) {
741 case RT_ITER_STATE_INIT
:
742 case RT_ITER_STATE_PAUSED
:
743 case RT_ITER_STATE_DONE
:
746 case RT_ITER_STATE_ITERATING
:
749 * Save the prefix that we are currently at. The next call to
750 * route_table_iter_next() will return the node after this
754 prefix_copy(&iter
->pause_prefix
, &iter
->current
->p
);
755 route_unlock_node(iter
->current
);
756 iter
->current
= NULL
;
757 iter
->state
= RT_ITER_STATE_PAUSED
;
766 * route_table_iter_cleanup
768 * Release any resources held by the iterator.
770 void route_table_iter_cleanup(route_table_iter_t
*iter
)
772 if (iter
->state
== RT_ITER_STATE_ITERATING
) {
773 route_unlock_node(iter
->current
);
774 iter
->current
= NULL
;
776 assert(!iter
->current
);
779 * Set the state to RT_ITER_STATE_DONE to make any
780 * route_table_iter_next() calls on this iterator return NULL.
782 iter
->state
= RT_ITER_STATE_DONE
;