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
);
42 DECLARE_HASH(rn_hash_node
, struct route_node
, nodehash
, route_table_hash_cmp
,
45 * route_table_init_with_delegate
48 route_table_init_with_delegate(route_table_delegate_t
*delegate
)
50 struct route_table
*rt
;
52 rt
= XCALLOC(MTYPE_ROUTE_TABLE
, sizeof(struct route_table
));
53 rt
->delegate
= delegate
;
54 rn_hash_node_init(&rt
->hash
);
58 void route_table_finish(struct route_table
*rt
)
63 /* Allocate new route node. */
64 static struct route_node
*route_node_new(struct route_table
*table
)
66 return table
->delegate
->create_node(table
->delegate
, table
);
69 /* Allocate new route node with prefix set. */
70 static struct route_node
*route_node_set(struct route_table
*table
,
71 const struct prefix
*prefix
)
73 struct route_node
*node
;
75 node
= route_node_new(table
);
77 prefix_copy(&node
->p
, prefix
);
80 rn_hash_node_add(&node
->table
->hash
, 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
;
104 /* Bulk deletion of nodes remaining in this table. This function is not
105 called until workers have completed their dependency on this table.
106 A final route_unlock_node() will not be called for these nodes. */
114 node
= node
->l_right
;
121 tmp_node
->table
->count
--;
122 tmp_node
->lock
= 0; /* to cause assert if unlocked after this */
123 rn_hash_node_del(&rt
->hash
, tmp_node
);
124 route_node_free(rt
, tmp_node
);
127 if (node
->l_left
== tmp_node
)
130 node
->l_right
= NULL
;
136 assert(rt
->count
== 0);
138 rn_hash_node_fini(&rt
->hash
);
139 XFREE(MTYPE_ROUTE_TABLE
, rt
);
143 /* Utility mask array. */
144 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
145 0xf8, 0xfc, 0xfe, 0xff};
147 /* Common prefix route genaration. */
148 static void route_common(const struct prefix
*n
, const struct prefix
*p
,
158 if (n
->family
== AF_FLOWSPEC
)
159 return prefix_copy(new, p
);
160 np
= (const uint8_t *)&n
->u
.prefix
;
161 pp
= (const uint8_t *)&p
->u
.prefix
;
163 newp
= (uint8_t *)&new->u
.prefix
;
165 for (i
= 0; i
< p
->prefixlen
/ 8; i
++) {
172 new->prefixlen
= i
* 8;
174 if (new->prefixlen
!= p
->prefixlen
) {
175 diff
= np
[i
] ^ pp
[i
];
177 while (new->prefixlen
< p
->prefixlen
&& !(mask
& diff
)) {
181 newp
[i
] = np
[i
] & maskbit
[new->prefixlen
% 8];
185 static void set_link(struct route_node
*node
, struct route_node
*new)
187 unsigned int bit
= prefix_bit(&new->p
.u
.prefix
, node
->p
.prefixlen
);
189 node
->link
[bit
] = new;
193 /* Find matched prefix. */
194 struct route_node
*route_node_match(const struct route_table
*table
,
195 union prefixconstptr pu
)
197 const struct prefix
*p
= pu
.p
;
198 struct route_node
*node
;
199 struct route_node
*matched
;
204 /* Walk down tree. If there is matched route then store it to
206 while (node
&& node
->p
.prefixlen
<= p
->prefixlen
207 && prefix_match(&node
->p
, p
)) {
211 if (node
->p
.prefixlen
== p
->prefixlen
)
214 node
= node
->link
[prefix_bit(&p
->u
.prefix
, node
->p
.prefixlen
)];
217 /* If matched route found, return it. */
219 return route_lock_node(matched
);
224 struct route_node
*route_node_match_ipv4(const struct route_table
*table
,
225 const struct in_addr
*addr
)
227 struct prefix_ipv4 p
;
229 memset(&p
, 0, sizeof(struct prefix_ipv4
));
231 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
234 return route_node_match(table
, (struct prefix
*)&p
);
237 struct route_node
*route_node_match_ipv6(const struct route_table
*table
,
238 const struct in6_addr
*addr
)
240 struct prefix_ipv6 p
;
242 memset(&p
, 0, sizeof(struct prefix_ipv6
));
244 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
247 return route_node_match(table
, (struct prefix
*)&p
);
250 /* Lookup same prefix node. Return NULL when we can't find route. */
251 struct route_node
*route_node_lookup(const struct route_table
*table
,
252 union prefixconstptr pu
)
255 struct route_node
*node
;
256 prefix_copy(&p
, pu
.p
);
259 node
= rn_hash_node_find(&table
->hash
, (void *)&p
);
260 return (node
&& node
->info
) ? route_lock_node(node
) : NULL
;
263 /* Lookup same prefix node. Return NULL when we can't find route. */
264 struct route_node
*route_node_lookup_maynull(const struct route_table
*table
,
265 union prefixconstptr pu
)
268 struct route_node
*node
;
269 prefix_copy(&p
, pu
.p
);
272 node
= rn_hash_node_find(&table
->hash
, (void *)&p
);
273 return node
? route_lock_node(node
) : NULL
;
276 /* Add node to routing table. */
277 struct route_node
*route_node_get(struct route_table
*const table
,
278 union prefixconstptr pu
)
280 const struct prefix
*p
= pu
.p
;
281 struct route_node
*new;
282 struct route_node
*node
;
283 struct route_node
*match
;
284 uint16_t prefixlen
= p
->prefixlen
;
285 const uint8_t *prefix
= &p
->u
.prefix
;
287 apply_mask((struct prefix
*)p
);
288 node
= rn_hash_node_find(&table
->hash
, (void *)p
);
289 if (node
&& node
->info
)
290 return route_lock_node(node
);
294 while (node
&& node
->p
.prefixlen
<= prefixlen
295 && prefix_match(&node
->p
, p
)) {
296 if (node
->p
.prefixlen
== prefixlen
)
297 return route_lock_node(node
);
300 node
= node
->link
[prefix_bit(prefix
, node
->p
.prefixlen
)];
304 new = route_node_set(table
, p
);
306 set_link(match
, new);
310 new = route_node_new(table
);
311 route_common(&node
->p
, p
, &new->p
);
312 new->p
.family
= p
->family
;
315 rn_hash_node_add(&table
->hash
, new);
318 set_link(match
, new);
322 if (new->p
.prefixlen
!= p
->prefixlen
) {
324 new = route_node_set(table
, p
);
325 set_link(match
, new);
330 route_lock_node(new);
335 /* Delete node from the routing table. */
336 void route_node_delete(struct route_node
*node
)
338 struct route_node
*child
;
339 struct route_node
*parent
;
341 assert(node
->lock
== 0);
342 assert(node
->info
== NULL
);
344 if (node
->l_left
&& node
->l_right
)
348 child
= node
->l_left
;
350 child
= node
->l_right
;
352 parent
= node
->parent
;
355 child
->parent
= parent
;
358 if (parent
->l_left
== node
)
359 parent
->l_left
= child
;
361 parent
->l_right
= child
;
363 node
->table
->top
= child
;
365 node
->table
->count
--;
367 rn_hash_node_del(&node
->table
->hash
, node
);
369 /* WARNING: FRAGILE CODE!
370 * route_node_free may have the side effect of free'ing the entire
372 * this is permitted only if table->count got decremented to zero above,
373 * because in that case parent will also be NULL, so that we won't try
375 * delete a now-stale parent below.
377 * cf. srcdest_srcnode_destroy() in zebra/zebra_rib.c */
379 route_node_free(node
->table
, node
);
381 /* If parent node is stub then delete it also. */
382 if (parent
&& parent
->lock
== 0)
383 route_node_delete(parent
);
386 /* Get fist node and lock it. This function is useful when one want
387 to lookup all the node exist in the routing table. */
388 struct route_node
*route_top(struct route_table
*table
)
390 /* If there is no node in the routing table return NULL. */
391 if (table
->top
== NULL
)
394 /* Lock the top node and return it. */
395 route_lock_node(table
->top
);
399 /* Unlock current node and lock next node then return it. */
400 struct route_node
*route_next(struct route_node
*node
)
402 struct route_node
*next
;
403 struct route_node
*start
;
405 /* Node may be deleted from route_unlock_node so we have to preserve
406 next node's pointer. */
410 route_lock_node(next
);
411 route_unlock_node(node
);
415 next
= node
->l_right
;
416 route_lock_node(next
);
417 route_unlock_node(node
);
422 while (node
->parent
) {
423 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
424 next
= node
->parent
->l_right
;
425 route_lock_node(next
);
426 route_unlock_node(start
);
431 route_unlock_node(start
);
435 /* Unlock current node and lock next node until limit. */
436 struct route_node
*route_next_until(struct route_node
*node
,
437 const struct route_node
*limit
)
439 struct route_node
*next
;
440 struct route_node
*start
;
442 /* Node may be deleted from route_unlock_node so we have to preserve
443 next node's pointer. */
447 route_lock_node(next
);
448 route_unlock_node(node
);
452 next
= node
->l_right
;
453 route_lock_node(next
);
454 route_unlock_node(node
);
459 while (node
->parent
&& node
!= limit
) {
460 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
) {
461 next
= node
->parent
->l_right
;
462 route_lock_node(next
);
463 route_unlock_node(start
);
468 route_unlock_node(start
);
472 unsigned long route_table_count(const struct route_table
*table
)
480 * Default function for creating a route node.
482 struct route_node
*route_node_create(route_table_delegate_t
*delegate
,
483 struct route_table
*table
)
485 struct route_node
*node
;
486 node
= XCALLOC(MTYPE_ROUTE_NODE
, sizeof(struct route_node
));
493 * Default function for destroying a route node.
495 void route_node_destroy(route_table_delegate_t
*delegate
,
496 struct route_table
*table
, struct route_node
*node
)
498 XFREE(MTYPE_ROUTE_NODE
, node
);
504 static route_table_delegate_t default_delegate
= {
505 .create_node
= route_node_create
,
506 .destroy_node
= route_node_destroy
};
508 route_table_delegate_t
*route_table_get_default_delegate(void)
510 return &default_delegate
;
516 struct route_table
*route_table_init(void)
518 return route_table_init_with_delegate(&default_delegate
);
522 * route_table_prefix_iter_cmp
524 * Compare two prefixes according to the order in which they appear in
525 * an iteration over a tree.
527 * @return -1 if p1 occurs before p2 (p1 < p2)
528 * 0 if the prefixes are identical (p1 == p2)
529 * +1 if p1 occurs after p2 (p1 > p2)
531 int route_table_prefix_iter_cmp(const struct prefix
*p1
,
532 const struct prefix
*p2
)
534 struct prefix common_space
;
535 struct prefix
*common
= &common_space
;
537 if (p1
->prefixlen
<= p2
->prefixlen
) {
538 if (prefix_match(p1
, p2
)) {
541 * p1 contains p2, or is equal to it.
543 return (p1
->prefixlen
== p2
->prefixlen
) ? 0 : -1;
548 * Check if p2 contains p1.
550 if (prefix_match(p2
, p1
))
554 route_common(p1
, p2
, common
);
555 assert(common
->prefixlen
< p1
->prefixlen
);
556 assert(common
->prefixlen
< p2
->prefixlen
);
559 * Both prefixes are longer than the common prefix.
561 * We need to check the bit after the common prefixlen to determine
562 * which one comes later.
564 if (prefix_bit(&p1
->u
.prefix
, common
->prefixlen
)) {
567 * We branch to the right to get to p1 from the common prefix.
569 assert(!prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
574 * We branch to the right to get to p2 from the common prefix.
576 assert(prefix_bit(&p2
->u
.prefix
, common
->prefixlen
));
581 * route_get_subtree_next
583 * Helper function that returns the first node that follows the nodes
584 * in the sub-tree under 'node' in iteration order.
586 static struct route_node
*route_get_subtree_next(struct route_node
*node
)
588 while (node
->parent
) {
589 if (node
->parent
->l_left
== node
&& node
->parent
->l_right
)
590 return node
->parent
->l_right
;
599 * route_table_get_next_internal
601 * Helper function to find the node that occurs after the given prefix in
602 * order of iteration.
604 * @see route_table_get_next
606 static struct route_node
*
607 route_table_get_next_internal(const struct route_table
*table
,
608 const struct prefix
*p
)
610 struct route_node
*node
, *tmp_node
;
618 if (node
->p
.prefixlen
< p
->prefixlen
)
619 match
= prefix_match(&node
->p
, p
);
621 match
= prefix_match(p
, &node
->p
);
624 if (node
->p
.prefixlen
== p
->prefixlen
) {
627 * The prefix p exists in the tree, just return
631 route_lock_node(node
);
632 node
= route_next(node
);
634 route_unlock_node(node
);
639 if (node
->p
.prefixlen
> p
->prefixlen
) {
642 * Node is in the subtree of p, and hence
649 * p is in the sub-tree under node.
651 tmp_node
= node
->link
[prefix_bit(&p
->u
.prefix
,
660 * There are no nodes in the direction where p should
662 * node has a right child, then it must be greater than
666 return node
->l_right
;
669 * No more children to follow, go upwards looking for
673 return route_get_subtree_next(node
);
677 * Neither node prefix nor 'p' contains the other.
679 cmp
= route_table_prefix_iter_cmp(&node
->p
, p
);
683 * Node follows p in iteration order. Return it.
691 * Node and the subtree under it come before prefix p in
692 * iteration order. Prefix p and its sub-tree are not present in
693 * the tree. Go upwards and find the first node that follows the
694 * subtree. That node will also succeed p.
696 return route_get_subtree_next(node
);
703 * route_table_get_next
705 * Find the node that occurs after the given prefix in order of
708 struct route_node
*route_table_get_next(const struct route_table
*table
,
709 union prefixconstptr pu
)
711 const struct prefix
*p
= pu
.p
;
712 struct route_node
*node
;
714 node
= route_table_get_next_internal(table
, p
);
716 assert(route_table_prefix_iter_cmp(&node
->p
, p
) > 0);
717 route_lock_node(node
);
723 * route_table_iter_init
725 void route_table_iter_init(route_table_iter_t
*iter
, struct route_table
*table
)
727 memset(iter
, 0, sizeof(*iter
));
728 iter
->state
= RT_ITER_STATE_INIT
;
733 * route_table_iter_pause
735 * Pause an iteration over the table. This allows the iteration to be
736 * resumed point after arbitrary additions/deletions from the table.
737 * An iteration can be resumed by just calling route_table_iter_next()
740 void route_table_iter_pause(route_table_iter_t
*iter
)
742 switch (iter
->state
) {
744 case RT_ITER_STATE_INIT
:
745 case RT_ITER_STATE_PAUSED
:
746 case RT_ITER_STATE_DONE
:
749 case RT_ITER_STATE_ITERATING
:
752 * Save the prefix that we are currently at. The next call to
753 * route_table_iter_next() will return the node after this
757 prefix_copy(&iter
->pause_prefix
, &iter
->current
->p
);
758 route_unlock_node(iter
->current
);
759 iter
->current
= NULL
;
760 iter
->state
= RT_ITER_STATE_PAUSED
;
769 * route_table_iter_cleanup
771 * Release any resources held by the iterator.
773 void route_table_iter_cleanup(route_table_iter_t
*iter
)
775 if (iter
->state
== RT_ITER_STATE_ITERATING
) {
776 route_unlock_node(iter
->current
);
777 iter
->current
= NULL
;
779 assert(!iter
->current
);
782 * Set the state to RT_ITER_STATE_DONE to make any
783 * route_table_iter_next() calls on this iterator return NULL.
785 iter
->state
= RT_ITER_STATE_DONE
;