1 /* $NetBSD: radix.c,v 1.42 2009/03/15 20:30:05 cegger Exp $ */
4 * Copyright (c) 1988, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)radix.c 8.6 (Berkeley) 10/17/95
35 * Routines to build and maintain radix trees for routing lookups.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: radix.c,v 1.42 2009/03/15 20:30:05 cegger Exp $");
42 #include <sys/param.h>
43 #include <sys/queue.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #define M_DONTWAIT M_NOWAIT
51 #include <sys/domain.h>
55 #include <machine/stdarg.h>
56 #include <sys/syslog.h>
57 #include <net/radix.h>
60 typedef void (*rn_printer_t
)(void *, const char *fmt
, ...);
63 struct radix_mask
*rn_mkfreelist
;
64 struct radix_node_head
*mask_rnhead
;
65 static char *addmask_key
;
66 static const char normal_chars
[] =
67 {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
68 static char *rn_zeros
, *rn_ones
;
70 #define rn_masktop (mask_rnhead->rnh_treetop)
72 static int rn_satisfies_leaf(const char *, struct radix_node
*, int);
73 static int rn_lexobetter(const void *, const void *);
74 static struct radix_mask
*rn_new_radix_mask(struct radix_node
*,
76 static struct radix_node
*rn_walknext(struct radix_node
*, rn_printer_t
,
78 static struct radix_node
*rn_walkfirst(struct radix_node
*, rn_printer_t
,
80 static void rn_nodeprint(struct radix_node
*, rn_printer_t
, void *,
83 #define SUBTREE_OPEN "[ "
84 #define SUBTREE_CLOSE " ]"
87 static void rn_treeprint(struct radix_node_head
*, rn_printer_t
, void *);
91 * The data structure for the keys is a radix tree with one way
92 * branching removed. The index rn_b at an internal node n represents a bit
93 * position to be tested. The tree is arranged so that all descendants
94 * of a node n have keys whose bits all agree up to position rn_b - 1.
95 * (We say the index of n is rn_b.)
97 * There is at least one descendant which has a one bit at position rn_b,
98 * and at least one with a zero there.
100 * A route is determined by a pair of key and mask. We require that the
101 * bit-wise logical and of the key and mask to be the key.
102 * We define the index of a route to associated with the mask to be
103 * the first bit number in the mask where 0 occurs (with bit number 0
104 * representing the highest order bit).
106 * We say a mask is normal if every bit is 0, past the index of the mask.
107 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
108 * and m is a normal mask, then the route applies to every descendant of n.
109 * If the index(m) < rn_b, this implies the trailing last few bits of k
110 * before bit b are all 0, (and hence consequently true of every descendant
111 * of n), so the route applies to all descendants of the node as well.
113 * Similar logic shows that a non-normal mask m such that
114 * index(m) <= index(n) could potentially apply to many children of n.
115 * Thus, for each non-host route, we attach its mask to a list at an internal
116 * node as high in the tree as we can go.
118 * The present version of the code makes use of normal routes in short-
119 * circuiting an explicit mask and compare operation when testing whether
120 * a key satisfies a normal route, and also in remembering the unique leaf
121 * that governs a subtree.
127 struct radix_node
*head
)
129 const u_char
* const v
= v_arg
;
130 struct radix_node
*x
;
132 for (x
= head
; x
->rn_b
>= 0;) {
133 if (x
->rn_bmask
& v
[x
->rn_off
])
144 struct radix_node
*head
,
147 struct radix_node
*x
;
148 const u_char
* const v
= v_arg
;
149 const u_char
* const m
= m_arg
;
151 for (x
= head
; x
->rn_b
>= 0;) {
152 if ((x
->rn_bmask
& m
[x
->rn_off
]) &&
153 (x
->rn_bmask
& v
[x
->rn_off
]))
166 const char *m
= m_arg
;
167 const char *n
= n_arg
;
168 const char *lim
= n
+ *(const u_char
*)n
;
169 const char *lim2
= lim
;
170 int longer
= (*(const u_char
*)n
++) - (int)(*(const u_char
*)m
++);
171 int masks_are_equal
= 1;
184 if (masks_are_equal
&& (longer
< 0))
185 for (lim2
= m
- longer
; m
< lim2
; )
188 return !masks_are_equal
;
195 struct radix_node_head
*head
)
197 struct radix_node
*x
;
198 const char *netmask
= NULL
;
201 if ((x
= rn_addmask(m_arg
, 1, head
->rnh_treetop
->rn_off
)) == 0)
205 x
= rn_match(v_arg
, head
);
206 if (x
!= NULL
&& netmask
!= NULL
) {
207 while (x
!= NULL
&& x
->rn_mask
!= netmask
)
216 struct radix_node
*leaf
,
219 const char *cp
= trial
;
220 const char *cp2
= leaf
->rn_key
;
221 const char *cp3
= leaf
->rn_mask
;
223 int length
= min(*(const u_char
*)cp
, *(const u_char
*)cp2
);
228 length
= min(length
, *(const u_char
*)cp3
);
229 cplim
= cp
+ length
; cp3
+= skip
; cp2
+= skip
;
230 for (cp
+= skip
; cp
< cplim
; cp
++, cp2
++, cp3
++)
231 if ((*cp
^ *cp2
) & *cp3
)
239 struct radix_node_head
*head
)
241 const char * const v
= v_arg
;
242 struct radix_node
*t
= head
->rnh_treetop
;
243 struct radix_node
*top
= t
;
244 struct radix_node
*x
;
245 struct radix_node
*saved_t
;
250 int vlen
= *(const u_char
*)cp
;
255 * Open code rn_search(v, top) to avoid overhead of extra
258 for (; t
->rn_b
>= 0; ) {
259 if (t
->rn_bmask
& cp
[t
->rn_off
])
265 * See if we match exactly as a host destination
266 * or at least learn how many bits match, for normal mask finesse.
268 * It doesn't hurt us to limit how many bytes to check
269 * to the length of the mask, since if it matches we had a genuine
270 * match and the leaf we have is the most specific one anyway;
271 * if it didn't match with a shorter length it would fail
272 * with a long one. This wins big for class B&C netmasks which
273 * are probably the most common case...
276 vlen
= *(const u_char
*)t
->rn_mask
;
277 cp
+= off
; cp2
= t
->rn_key
+ off
; cplim
= v
+ vlen
;
278 for (; cp
< cplim
; cp
++, cp2
++)
282 * This extra grot is in case we are explicitly asked
283 * to look up the default. Ugh!
285 if ((t
->rn_flags
& RNF_ROOT
) && t
->rn_dupedkey
)
289 test
= (*cp
^ *cp2
) & 0xff; /* find first bit that differs */
290 for (b
= 7; (test
>>= 1) > 0;)
292 matched_off
= cp
- v
;
293 b
+= matched_off
<< 3;
296 * If there is a host route in a duped-key chain, it will be first.
298 if ((saved_t
= t
)->rn_mask
== 0)
300 for (; t
; t
= t
->rn_dupedkey
)
302 * Even if we don't match exactly as a host,
303 * we may match if the leaf we wound up at is
306 if (t
->rn_flags
& RNF_NORMAL
) {
309 } else if (rn_satisfies_leaf(v
, t
, matched_off
))
312 /* start searching up the tree */
314 struct radix_mask
*m
;
319 * If non-contiguous masks ever become important
320 * we can restore the masking and open coding of
321 * the search and satisfaction test and put the
322 * calculation of "off" back before the "do".
325 if (m
->rm_flags
& RNF_NORMAL
) {
329 off
= min(t
->rn_off
, matched_off
);
330 x
= rn_search_m(v
, t
, m
->rm_mask
);
331 while (x
&& x
->rn_mask
!= m
->rm_mask
)
333 if (x
&& rn_satisfies_leaf(v
, x
, off
))
344 rn_nodeprint(struct radix_node
*rn
, rn_printer_t printer
, void *arg
,
347 (*printer
)(arg
, "%s(%s%p: p<%p> l<%p> r<%p>)",
348 delim
, ((void *)rn
== arg
) ? "*" : "", rn
, rn
->rn_p
,
356 rn_dbg_print(void *arg
, const char *fmt
, ...)
361 vlog(LOG_DEBUG
, fmt
, ap
);
366 rn_treeprint(struct radix_node_head
*h
, rn_printer_t printer
, void *arg
)
368 struct radix_node
*dup
, *rn
;
374 rn
= rn_walkfirst(h
->rnh_treetop
, printer
, arg
);
378 for (dup
= rn
; dup
!= NULL
; dup
= dup
->rn_dupedkey
) {
379 if ((dup
->rn_flags
& RNF_ROOT
) != 0)
381 rn_nodeprint(dup
, printer
, arg
, delim
);
384 rn
= rn_walknext(rn
, printer
, arg
);
385 if (rn
->rn_flags
& RNF_ROOT
)
391 #define traverse(__head, __rn) rn_treeprint((__head), rn_dbg_print, (__rn))
392 #endif /* RN_DEBUG */
398 struct radix_node nodes
[2])
400 struct radix_node
*tt
= nodes
;
401 struct radix_node
*t
= tt
+ 1;
402 t
->rn_b
= b
; t
->rn_bmask
= 0x80 >> (b
& 7);
403 t
->rn_l
= tt
; t
->rn_off
= b
>> 3;
404 tt
->rn_b
= -1; tt
->rn_key
= v
; tt
->rn_p
= t
;
405 tt
->rn_flags
= t
->rn_flags
= RNF_ACTIVE
;
412 struct radix_node_head
*head
,
414 struct radix_node nodes
[2])
416 struct radix_node
*top
= head
->rnh_treetop
;
417 struct radix_node
*t
= rn_search(v_arg
, top
);
418 struct radix_node
*tt
;
419 const char *v
= v_arg
;
420 int head_off
= top
->rn_off
;
421 int vlen
= *((const u_char
*)v
);
422 const char *cp
= v
+ head_off
;
425 * Find first bit at which v and t->rn_key differ
428 const char *cp2
= t
->rn_key
+ head_off
;
429 const char *cplim
= v
+ vlen
;
439 cmp_res
= (cp
[-1] ^ cp2
[-1]) & 0xff;
440 for (b
= (cp
- v
) << 3; cmp_res
; b
--)
444 struct radix_node
*p
, *x
= top
;
448 if (cp
[x
->rn_off
] & x
->rn_bmask
)
451 } while (b
> (unsigned) x
->rn_b
); /* x->rn_b < b && x->rn_b >= 0 */
454 log(LOG_DEBUG
, "%s: Going In:\n", __func__
), traverse(head
, p
);
456 t
= rn_newpair(v_arg
, b
, nodes
); tt
= t
->rn_l
;
457 if ((cp
[p
->rn_off
] & p
->rn_bmask
) == 0)
461 x
->rn_p
= t
; t
->rn_p
= p
; /* frees x, p as temp vars below */
462 if ((cp
[t
->rn_off
] & t
->rn_bmask
) == 0) {
465 t
->rn_r
= tt
; t
->rn_l
= x
;
469 log(LOG_DEBUG
, "%s: Coming Out:\n", __func__
),
472 #endif /* RN_DEBUG */
483 const char *netmask
= n_arg
;
486 struct radix_node
*x
;
487 struct radix_node
*saved_x
;
489 int maskduplicated
, m0
, isnormal
;
490 static int last_zeroed
= 0;
492 if ((mlen
= *(const u_char
*)netmask
) > max_keylen
)
497 return mask_rnhead
->rnh_nodes
;
499 memmove(addmask_key
+ 1, rn_ones
+ 1, skip
- 1);
500 if ((m0
= mlen
) > skip
)
501 memmove(addmask_key
+ skip
, netmask
+ skip
, mlen
- skip
);
503 * Trim trailing zeroes.
505 for (cp
= addmask_key
+ mlen
; (cp
> addmask_key
) && cp
[-1] == 0;)
507 mlen
= cp
- addmask_key
;
509 if (m0
>= last_zeroed
)
511 return mask_rnhead
->rnh_nodes
;
513 if (m0
< last_zeroed
)
514 memset(addmask_key
+ m0
, 0, last_zeroed
- m0
);
515 *addmask_key
= last_zeroed
= mlen
;
516 x
= rn_search(addmask_key
, rn_masktop
);
517 if (memcmp(addmask_key
, x
->rn_key
, mlen
) != 0)
521 R_Malloc(x
, struct radix_node
*, max_keylen
+ 2 * sizeof (*x
));
522 if ((saved_x
= x
) == NULL
)
524 memset(x
, 0, max_keylen
+ 2 * sizeof (*x
));
525 cp
= netmask
= (void *)(x
+ 2);
526 memmove(x
+ 2, addmask_key
, mlen
);
527 x
= rn_insert(cp
, mask_rnhead
, &maskduplicated
, x
);
528 if (maskduplicated
) {
529 log(LOG_ERR
, "rn_addmask: mask impossibly already in tree\n");
534 * Calculate index of mask, and check for normalcy.
536 cplim
= netmask
+ mlen
; isnormal
= 1;
537 for (cp
= netmask
+ skip
; (cp
< cplim
) && *(const u_char
*)cp
== 0xff;)
540 for (j
= 0x80; (j
& *cp
) != 0; j
>>= 1)
542 if (*cp
!= normal_chars
[b
] || cp
!= (cplim
- 1))
545 b
+= (cp
- netmask
) << 3;
548 x
->rn_flags
|= RNF_NORMAL
;
552 static int /* XXX: arbitrary ordering for non-contiguous masks */
557 const u_char
*mp
= m_arg
;
558 const u_char
*np
= n_arg
;
562 return 1; /* not really, but need to check longer one first */
564 for (lim
= mp
+ *mp
; mp
< lim
;)
570 static struct radix_mask
*
572 struct radix_node
*tt
,
573 struct radix_mask
*next
)
575 struct radix_mask
*m
;
579 log(LOG_ERR
, "Mask for route not entered\n");
582 memset(m
, 0, sizeof(*m
));
584 m
->rm_flags
= tt
->rn_flags
;
585 if (tt
->rn_flags
& RNF_NORMAL
)
588 m
->rm_mask
= tt
->rn_mask
;
598 struct radix_node_head
*head
,
599 struct radix_node treenodes
[2])
601 const char *v
= v_arg
, *netmask
= n_arg
;
602 struct radix_node
*t
, *x
= NULL
, *tt
;
603 struct radix_node
*saved_tt
, *top
= head
->rnh_treetop
;
604 short b
= 0, b_leaf
= 0;
607 struct radix_mask
*m
, **mp
;
610 * In dealing with non-contiguous masks, there may be
611 * many different routes which have the same mask.
612 * We will find it useful to have a unique pointer to
613 * the mask to speed avoiding duplicate references at
614 * nodes and possibly save time in calculating indices.
616 if (netmask
!= NULL
) {
617 if ((x
= rn_addmask(netmask
, 0, top
->rn_off
)) == NULL
)
624 * Deal with duplicated keys: attach node to previous instance
626 saved_tt
= tt
= rn_insert(v
, head
, &keyduplicated
, treenodes
);
628 for (t
= tt
; tt
!= NULL
; t
= tt
, tt
= tt
->rn_dupedkey
) {
629 if (tt
->rn_mask
== netmask
)
631 if (netmask
== NULL
||
632 (tt
->rn_mask
!= NULL
&&
633 (b_leaf
< tt
->rn_b
|| /* index(netmask) > node */
634 rn_refines(netmask
, tt
->rn_mask
) ||
635 rn_lexobetter(netmask
, tt
->rn_mask
))))
639 * If the mask is not duplicated, we wouldn't
640 * find it among possible duplicate key entries
641 * anyway, so the above test doesn't hurt.
643 * We sort the masks for a duplicated key the same way as
644 * in a masklist -- most specific to least specific.
645 * This may require the unfortunate nuisance of relocating
646 * the head of the list.
648 * We also reverse, or doubly link the list through the
651 if (tt
== saved_tt
) {
652 struct radix_node
*xx
= x
;
653 /* link in at head of list */
654 (tt
= treenodes
)->rn_dupedkey
= t
;
655 tt
->rn_flags
= t
->rn_flags
;
656 tt
->rn_p
= x
= t
->rn_p
;
665 (tt
= treenodes
)->rn_dupedkey
= t
->rn_dupedkey
;
669 tt
->rn_dupedkey
->rn_p
= tt
;
673 tt
->rn_flags
= RNF_ACTIVE
;
678 if (netmask
!= NULL
) {
679 tt
->rn_mask
= netmask
;
681 tt
->rn_flags
|= x
->rn_flags
& RNF_NORMAL
;
686 b_leaf
= -1 - t
->rn_b
;
687 if (t
->rn_r
== saved_tt
)
691 /* Promote general routes from below */
693 for (mp
= &t
->rn_mklist
; x
!= NULL
; x
= x
->rn_dupedkey
) {
694 if (x
->rn_mask
!= NULL
&& x
->rn_b
>= b_leaf
&&
695 x
->rn_mklist
== NULL
) {
696 *mp
= m
= rn_new_radix_mask(x
, NULL
);
701 } else if (x
->rn_mklist
!= NULL
) {
703 * Skip over masks whose index is > that of new node
705 for (mp
= &x
->rn_mklist
; (m
= *mp
) != NULL
; mp
= &m
->rm_mklist
)
706 if (m
->rm_b
>= b_leaf
)
712 /* Add new route to highest possible ancestor's list */
713 if (netmask
== NULL
|| b
> t
->rn_b
)
714 return tt
; /* can't lift at all */
719 } while (b
<= t
->rn_b
&& x
!= top
);
721 * Search through routes associated with node to
722 * insert new route according to index.
723 * Need same criteria as when sorting dupedkeys to avoid
724 * double loop on deletion.
726 for (mp
= &x
->rn_mklist
; (m
= *mp
) != NULL
; mp
= &m
->rm_mklist
) {
727 if (m
->rm_b
< b_leaf
)
729 if (m
->rm_b
> b_leaf
)
731 if (m
->rm_flags
& RNF_NORMAL
) {
732 mmask
= m
->rm_leaf
->rn_mask
;
733 if (tt
->rn_flags
& RNF_NORMAL
) {
734 log(LOG_ERR
, "Non-unique normal route,"
735 " mask not entered\n");
740 if (mmask
== netmask
) {
745 if (rn_refines(netmask
, mmask
) || rn_lexobetter(netmask
, mmask
))
748 *mp
= rn_new_radix_mask(tt
, *mp
);
755 const void *netmask_arg
,
756 struct radix_node_head
*head
,
757 struct radix_node
*rn
)
759 struct radix_node
*t
, *p
, *x
, *tt
;
760 struct radix_mask
*m
, *saved_m
, **mp
;
761 struct radix_node
*dupedkey
, *saved_tt
, *top
;
762 const char *v
, *netmask
;
763 int b
, head_off
, vlen
;
766 netmask
= netmask_arg
;
767 x
= head
->rnh_treetop
;
768 tt
= rn_search(v
, x
);
769 head_off
= x
->rn_off
;
770 vlen
= *(const u_char
*)v
;
774 memcmp(v
+ head_off
, tt
->rn_key
+ head_off
, vlen
- head_off
) != 0)
777 * Delete our route from mask lists.
779 if (netmask
!= NULL
) {
780 if ((x
= rn_addmask(netmask
, 1, head_off
)) == NULL
)
783 while (tt
->rn_mask
!= netmask
)
784 if ((tt
= tt
->rn_dupedkey
) == NULL
)
787 if (tt
->rn_mask
== NULL
|| (saved_m
= m
= tt
->rn_mklist
) == NULL
)
789 if (tt
->rn_flags
& RNF_NORMAL
) {
790 if (m
->rm_leaf
!= tt
|| m
->rm_refs
> 0) {
791 log(LOG_ERR
, "rn_delete: inconsistent annotation\n");
792 return NULL
; /* dangling ref could cause disaster */
795 if (m
->rm_mask
!= tt
->rn_mask
) {
796 log(LOG_ERR
, "rn_delete: inconsistent annotation\n");
799 if (--m
->rm_refs
>= 0)
805 goto on1
; /* Wasn't lifted at all */
809 } while (b
<= t
->rn_b
&& x
!= top
);
810 for (mp
= &x
->rn_mklist
; (m
= *mp
) != NULL
; mp
= &m
->rm_mklist
) {
818 log(LOG_ERR
, "rn_delete: couldn't find our annotation\n");
819 if (tt
->rn_flags
& RNF_NORMAL
)
820 return NULL
; /* Dangling ref to us */
824 * Eliminate us from tree
826 if (tt
->rn_flags
& RNF_ROOT
)
830 log(LOG_DEBUG
, "%s: Going In:\n", __func__
), traverse(head
, tt
);
833 dupedkey
= saved_tt
->rn_dupedkey
;
834 if (dupedkey
!= NULL
) {
836 * Here, tt is the deletion target, and
837 * saved_tt is the head of the dupedkey chain.
839 if (tt
== saved_tt
) {
847 /* find node in front of tt on the chain */
848 for (x
= p
= saved_tt
;
849 p
!= NULL
&& p
->rn_dupedkey
!= tt
;)
852 p
->rn_dupedkey
= tt
->rn_dupedkey
;
853 if (tt
->rn_dupedkey
!= NULL
)
854 tt
->rn_dupedkey
->rn_p
= p
;
856 log(LOG_ERR
, "rn_delete: couldn't find us\n");
859 if (t
->rn_flags
& RNF_ACTIVE
) {
882 * Demote routes attached to us.
884 if (t
->rn_mklist
== NULL
)
886 else if (x
->rn_b
>= 0) {
887 for (mp
= &x
->rn_mklist
; (m
= *mp
) != NULL
; mp
= &m
->rm_mklist
)
891 /* If there are any key,mask pairs in a sibling
892 duped-key chain, some subset will appear sorted
893 in the same order attached to our mklist */
894 for (m
= t
->rn_mklist
;
895 m
!= NULL
&& x
!= NULL
;
896 x
= x
->rn_dupedkey
) {
897 if (m
== x
->rn_mklist
) {
898 struct radix_mask
*mm
= m
->rm_mklist
;
900 if (--(m
->rm_refs
) < 0)
906 log(LOG_ERR
, "rn_delete: Orphaned Mask %p at %p\n",
911 * We may be holding an active internal node in the tree.
927 log(LOG_DEBUG
, "%s: Coming Out:\n", __func__
),
930 #endif /* RN_DEBUG */
931 tt
->rn_flags
&= ~RNF_ACTIVE
;
932 tt
[1].rn_flags
&= ~RNF_ACTIVE
;
939 const void *netmask_arg
,
940 struct radix_node_head
*head
)
942 return rn_delete1(v_arg
, netmask_arg
, head
, NULL
);
945 static struct radix_node
*
946 rn_walknext(struct radix_node
*rn
, rn_printer_t printer
, void *arg
)
948 /* If at right child go back up, otherwise, go right */
949 while (rn
->rn_p
->rn_r
== rn
&& (rn
->rn_flags
& RNF_ROOT
) == 0) {
951 (*printer
)(arg
, SUBTREE_CLOSE
);
955 rn_nodeprint(rn
->rn_p
, printer
, arg
, "");
956 /* Find the next *leaf* since next node might vanish, too */
957 for (rn
= rn
->rn_p
->rn_r
; rn
->rn_b
>= 0;) {
959 (*printer
)(arg
, SUBTREE_OPEN
);
965 static struct radix_node
*
966 rn_walkfirst(struct radix_node
*rn
, rn_printer_t printer
, void *arg
)
968 /* First time through node, go left */
969 while (rn
->rn_b
>= 0) {
971 (*printer
)(arg
, SUBTREE_OPEN
);
979 struct radix_node_head
*h
,
980 int (*f
)(struct radix_node
*, void *),
984 struct radix_node
*base
, *next
, *rn
;
986 * This gets complicated because we may delete the node
987 * while applying the function f to it, so we need to calculate
988 * the successor node in advance.
990 rn
= rn_walkfirst(h
->rnh_treetop
, NULL
, NULL
);
993 next
= rn_walknext(rn
, NULL
, NULL
);
995 while ((rn
= base
) != NULL
) {
996 base
= rn
->rn_dupedkey
;
997 if (!(rn
->rn_flags
& RNF_ROOT
) && (error
= (*f
)(rn
, w
)))
1001 if (rn
->rn_flags
& RNF_ROOT
)
1010 SLIST_ENTRY(delayinit
) entries
;
1012 static SLIST_HEAD(, delayinit
) delayinits
= SLIST_HEAD_INITIALIZER(delayheads
);
1013 static int radix_initialized
;
1016 * Initialize a radix tree once radix is initialized. Only for bootstrap.
1017 * Assume that no concurrency protection is necessary at this stage.
1020 rn_delayedinit(void **head
, int off
)
1022 struct delayinit
*di
;
1024 KASSERT(radix_initialized
== 0);
1026 di
= kmem_alloc(sizeof(*di
), KM_SLEEP
);
1029 SLIST_INSERT_HEAD(&delayinits
, di
, entries
);
1033 rn_inithead(void **head
, int off
)
1035 struct radix_node_head
*rnh
;
1039 R_Malloc(rnh
, struct radix_node_head
*, sizeof (*rnh
));
1043 return rn_inithead0(rnh
, off
);
1047 rn_inithead0(struct radix_node_head
*rnh
, int off
)
1049 struct radix_node
*t
;
1050 struct radix_node
*tt
;
1051 struct radix_node
*ttt
;
1053 memset(rnh
, 0, sizeof(*rnh
));
1054 t
= rn_newpair(rn_zeros
, off
, rnh
->rnh_nodes
);
1055 ttt
= rnh
->rnh_nodes
+ 2;
1059 tt
->rn_flags
= t
->rn_flags
= RNF_ROOT
| RNF_ACTIVE
;
1060 tt
->rn_b
= -1 - off
;
1062 ttt
->rn_key
= rn_ones
;
1063 rnh
->rnh_addaddr
= rn_addroute
;
1064 rnh
->rnh_deladdr
= rn_delete
;
1065 rnh
->rnh_matchaddr
= rn_match
;
1066 rnh
->rnh_lookup
= rn_lookup
;
1067 rnh
->rnh_treetop
= t
;
1075 struct delayinit
*di
;
1079 if (radix_initialized
)
1080 panic("radix already initialized");
1081 radix_initialized
= 1;
1083 DOMAIN_FOREACH(dp
) {
1084 if (dp
->dom_maxrtkey
> max_keylen
)
1085 max_keylen
= dp
->dom_maxrtkey
;
1088 if (max_keylen
== 0) {
1090 "rn_init: radix functions require max_keylen be set\n");
1094 R_Malloc(rn_zeros
, char *, 3 * max_keylen
);
1095 if (rn_zeros
== NULL
)
1097 memset(rn_zeros
, 0, 3 * max_keylen
);
1098 rn_ones
= cp
= rn_zeros
+ max_keylen
;
1099 addmask_key
= cplim
= rn_ones
+ max_keylen
;
1102 if (rn_inithead((void *)&mask_rnhead
, 0) == 0)
1105 while ((di
= SLIST_FIRST(&delayinits
)) != NULL
) {
1106 if (!rn_inithead(di
->head
, di
->off
))
1107 panic("delayed rn_inithead failed");
1108 SLIST_REMOVE_HEAD(&delayinits
, entries
);
1109 kmem_free(di
, sizeof(*di
));