xref: /onnv-gate/usr/src/uts/common/ipp/ipgpc/trie.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <ipp/ipgpc/trie.h>
30*0Sstevel@tonic-gate #include <ipp/ipgpc/filters.h>
31*0Sstevel@tonic-gate #include <ipp/ipgpc/classifier.h>
32*0Sstevel@tonic-gate #include <inet/ip6.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /* trie data structure used for classifying IP addresses and TCP/UDP ports */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #define	ZERO	0
37*0Sstevel@tonic-gate #define	ONE	1
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /* Statics */
41*0Sstevel@tonic-gate static void t_split(node_t **, uint8_t, uint8_t);
42*0Sstevel@tonic-gate static boolean_t t_traverse_delete(node_t **, uint8_t, key_t, uint32_t,
43*0Sstevel@tonic-gate     uint32_t, trie_id_t **);
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * create_node(flag)
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  * generates a pointer to a new trie node
49*0Sstevel@tonic-gate  * flag is passed to kmem_alloc
50*0Sstevel@tonic-gate  * returns NULL to signify memory error
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate node_t *
create_node(int flag)53*0Sstevel@tonic-gate create_node(int flag)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	node_t *buf = kmem_cache_alloc(trie_node_cache, flag);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (buf == NULL) {
58*0Sstevel@tonic-gate 		return (NULL);
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate 	buf->elements = NULL;
61*0Sstevel@tonic-gate 	buf->zero = NULL;
62*0Sstevel@tonic-gate 	buf->one = NULL;
63*0Sstevel@tonic-gate 	buf->pos = 0;
64*0Sstevel@tonic-gate 	buf->bits = 0;
65*0Sstevel@tonic-gate 	buf->val = 0;
66*0Sstevel@tonic-gate 	buf->mask = 0;
67*0Sstevel@tonic-gate 	buf->isroot = 0;
68*0Sstevel@tonic-gate 	return (buf);
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * t_split(c_node, pos, key_len)
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  * performs a split on c_node for the following three cases:
76*0Sstevel@tonic-gate  * 1 a mismatch occured between the insert key and the value at the node
77*0Sstevel@tonic-gate  * 2 the insert key specifies a shorter key than the one at the node
78*0Sstevel@tonic-gate  * 3 the insert key specifies a longer key than the one at the node
79*0Sstevel@tonic-gate  * cases 1 and 2 are handled in the same way
80*0Sstevel@tonic-gate  * when t_split returns, c_node->one and c_node->zero must != NULL
81*0Sstevel@tonic-gate  *
82*0Sstevel@tonic-gate  * (note: we assume a key_len = n (where in the real world n = 16 | 32),
83*0Sstevel@tonic-gate  *  and a "key" in this example is actaully some value of key_len n that
84*0Sstevel@tonic-gate  *  has its high order bits masked.
85*0Sstevel@tonic-gate  *  For example: key = 1011 with key_len = 8, would actaully be the key:mask
86*0Sstevel@tonic-gate  *  combo 1011xxxx:11110000.  I am using short keys for ease of example)
87*0Sstevel@tonic-gate  * Case 1 and 2:
88*0Sstevel@tonic-gate  *
89*0Sstevel@tonic-gate  * assume 8 bit keys for all examples
90*0Sstevel@tonic-gate  *
91*0Sstevel@tonic-gate  * trie A contains keys 111011, 0, 10
92*0Sstevel@tonic-gate  *       *
93*0Sstevel@tonic-gate  *      / \
94*0Sstevel@tonic-gate  *         *
95*0Sstevel@tonic-gate  *        / \
96*0Sstevel@tonic-gate  *        *  * bits = 4 pos = 5 val = 1011 mask = 00111100
97*0Sstevel@tonic-gate  * inserting 111100 would result in the following split
98*0Sstevel@tonic-gate  *                       *
99*0Sstevel@tonic-gate  *                      / \
100*0Sstevel@tonic-gate  *                         *
101*0Sstevel@tonic-gate  *                        / \
102*0Sstevel@tonic-gate  *                           *  bits = 1 pos = 5 val = 1 mask = 00100000
103*0Sstevel@tonic-gate  *                          / \
104*0Sstevel@tonic-gate  *  bits = 2 pos = 3 val=11*   * (to be inserted: (bits = 2 pos = 3 val = 00
105*0Sstevel@tonic-gate  *  mask = 00001100                               mask = 00001100))
106*0Sstevel@tonic-gate  *
107*0Sstevel@tonic-gate  * Case 3:
108*0Sstevel@tonic-gate  *
109*0Sstevel@tonic-gate  * trie A same as above, before insert
110*0Sstevel@tonic-gate  * inserting key 11101111 would results in the following split
111*0Sstevel@tonic-gate  *       *
112*0Sstevel@tonic-gate  *      / \
113*0Sstevel@tonic-gate  *         *
114*0Sstevel@tonic-gate  *        / \
115*0Sstevel@tonic-gate  *        *  * bits = 4 pos = 5 val = 1011 mask = 00111100
116*0Sstevel@tonic-gate  *          / \
117*0Sstevel@tonic-gate  *         *   *  (to be inserted: bits = 1 pos = 0 val = 1 mask = 00000001)
118*0Sstevel@tonic-gate  */
119*0Sstevel@tonic-gate /* ARGSUSED */
120*0Sstevel@tonic-gate static void
t_split(node_t ** c_node,uint8_t pos,uint8_t key_len)121*0Sstevel@tonic-gate t_split(node_t **c_node, uint8_t pos, uint8_t key_len)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	uint8_t old_bits = 0;
124*0Sstevel@tonic-gate 	uint8_t i;
125*0Sstevel@tonic-gate 	int bit;
126*0Sstevel@tonic-gate 	node_t *nodep = *c_node;
127*0Sstevel@tonic-gate 	node_t *tnodep = NULL;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	/* check if case is that the mask is longer */
130*0Sstevel@tonic-gate 	if (pos == (nodep->pos - nodep->bits)) {
131*0Sstevel@tonic-gate 		/* pos is past the last bit covered at this node */
132*0Sstevel@tonic-gate 		ASSERT(nodep->one == NULL);
133*0Sstevel@tonic-gate 		ASSERT(nodep->zero == NULL);
134*0Sstevel@tonic-gate 		nodep->one = create_node(KM_SLEEP);
135*0Sstevel@tonic-gate 		nodep->zero = create_node(KM_SLEEP);
136*0Sstevel@tonic-gate 	} else {		/* pos > (nodep->pos - nodep->bits) */
137*0Sstevel@tonic-gate 		old_bits = nodep->bits; /* save old bits entry */
138*0Sstevel@tonic-gate 		/* nodep->pos will remain the same */
139*0Sstevel@tonic-gate 		nodep->bits = nodep->pos - pos;
140*0Sstevel@tonic-gate 		/* find the mismatch bit */
141*0Sstevel@tonic-gate 		bit = EXTRACTBIT(nodep->val, pos, key_len);
142*0Sstevel@tonic-gate 		if (bit == ZERO) {
143*0Sstevel@tonic-gate 			if ((nodep->one == NULL) && (nodep->zero == NULL)) {
144*0Sstevel@tonic-gate 				nodep->one = create_node(KM_SLEEP);
145*0Sstevel@tonic-gate 				nodep->zero = create_node(KM_SLEEP);
146*0Sstevel@tonic-gate 			} else {
147*0Sstevel@tonic-gate 				tnodep = create_node(KM_SLEEP);
148*0Sstevel@tonic-gate 				tnodep->one = nodep->one;
149*0Sstevel@tonic-gate 				tnodep->zero = nodep->zero;
150*0Sstevel@tonic-gate 				nodep->zero = tnodep;
151*0Sstevel@tonic-gate 				nodep->one = create_node(KM_SLEEP);
152*0Sstevel@tonic-gate 			}
153*0Sstevel@tonic-gate 			/* pos is before the last bit covered at this node */
154*0Sstevel@tonic-gate 			nodep->zero->pos = pos - 1; /* link is one bit */
155*0Sstevel@tonic-gate 			/* bits gets remaining bits minus the link */
156*0Sstevel@tonic-gate 			nodep->zero->bits = (old_bits - nodep->bits) - 1;
157*0Sstevel@tonic-gate 			/* set bits that are covered by this node */
158*0Sstevel@tonic-gate 			for (i = 0; i < nodep->zero->bits; ++i) {
159*0Sstevel@tonic-gate 				SETBIT(nodep->zero->val,
160*0Sstevel@tonic-gate 				    (nodep->zero->pos - i),
161*0Sstevel@tonic-gate 				    EXTRACTBIT(nodep->val,
162*0Sstevel@tonic-gate 					(nodep->zero->pos - i), key_len),
163*0Sstevel@tonic-gate 				    key_len);
164*0Sstevel@tonic-gate 				SETBIT(nodep->zero->mask,
165*0Sstevel@tonic-gate 				    (nodep->zero->pos - i), 1, key_len);
166*0Sstevel@tonic-gate 			}
167*0Sstevel@tonic-gate 			nodep->zero->elements = nodep->elements;
168*0Sstevel@tonic-gate 			nodep->elements = NULL;
169*0Sstevel@tonic-gate 		} else {	/* bit == ONE */
170*0Sstevel@tonic-gate 			if ((nodep->one == NULL) && (nodep->zero == NULL)) {
171*0Sstevel@tonic-gate 				nodep->one = create_node(KM_SLEEP);
172*0Sstevel@tonic-gate 				nodep->zero = create_node(KM_SLEEP);
173*0Sstevel@tonic-gate 			} else {
174*0Sstevel@tonic-gate 				tnodep = create_node(KM_SLEEP);
175*0Sstevel@tonic-gate 				tnodep->one = nodep->one;
176*0Sstevel@tonic-gate 				tnodep->zero = nodep->zero;
177*0Sstevel@tonic-gate 				nodep->one = tnodep;
178*0Sstevel@tonic-gate 				nodep->zero = create_node(KM_SLEEP);
179*0Sstevel@tonic-gate 			}
180*0Sstevel@tonic-gate 			/* pos is before the last bit covered at this node */
181*0Sstevel@tonic-gate 			nodep->one->pos = pos - 1; /* link is one bit */
182*0Sstevel@tonic-gate 			/* bits gets remaining bits minus the link */
183*0Sstevel@tonic-gate 			nodep->one->bits = (old_bits - nodep->bits) - 1;
184*0Sstevel@tonic-gate 			/* set bits that are covered by this node */
185*0Sstevel@tonic-gate 			for (i = 0; i < nodep->one->bits; ++i) {
186*0Sstevel@tonic-gate 				SETBIT(nodep->one->val, (nodep->one->pos - i),
187*0Sstevel@tonic-gate 				    EXTRACTBIT(nodep->val,
188*0Sstevel@tonic-gate 					(nodep->one->pos - i), key_len),
189*0Sstevel@tonic-gate 				    key_len);
190*0Sstevel@tonic-gate 				SETBIT(nodep->one->mask,
191*0Sstevel@tonic-gate 				    (nodep->one->pos - i), 1, key_len);
192*0Sstevel@tonic-gate 			}
193*0Sstevel@tonic-gate 			nodep->one->elements = nodep->elements;
194*0Sstevel@tonic-gate 			nodep->elements = NULL;
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 		/* clear bits no longer covered by this node, from pos=>0 */
198*0Sstevel@tonic-gate 		for (i = 0; i <= pos; ++i) {
199*0Sstevel@tonic-gate 			UNSETBIT(nodep->val, i, key_len);
200*0Sstevel@tonic-gate 			UNSETBIT(nodep->mask, i, key_len);
201*0Sstevel@tonic-gate 		}
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /*
206*0Sstevel@tonic-gate  * t_insert(tid, id, key, mask)
207*0Sstevel@tonic-gate  *
208*0Sstevel@tonic-gate  * inserts a new value, id, into the trie, tid->trie with the input key
209*0Sstevel@tonic-gate  * - if node exists, id is appended to element list at the node, if id does
210*0Sstevel@tonic-gate  *   not already exist.
211*0Sstevel@tonic-gate  * - if node does not exist, a new node is created and id is the head of a new
212*0Sstevel@tonic-gate  *   element list
213*0Sstevel@tonic-gate  * return DONTCARE_VALUE if mask == 0, otherwise NORMAL_VALUE
214*0Sstevel@tonic-gate  */
215*0Sstevel@tonic-gate int
t_insert(trie_id_t * tid,key_t id,uint32_t key,uint32_t mask)216*0Sstevel@tonic-gate t_insert(trie_id_t *tid, key_t id, uint32_t key, uint32_t mask)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	node_t *c_node;
219*0Sstevel@tonic-gate 	int bit;
220*0Sstevel@tonic-gate 	uint8_t pos;
221*0Sstevel@tonic-gate 	uint8_t key_len = (uint8_t)tid->key_len;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/* don't insert if don't care */
224*0Sstevel@tonic-gate 	if (mask == 0) {
225*0Sstevel@tonic-gate 		++tid->stats.num_dontcare;
226*0Sstevel@tonic-gate 		return (DONTCARE_VALUE);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	rw_enter(&tid->rw_lock, RW_WRITER);
230*0Sstevel@tonic-gate 	c_node = tid->trie;	/* point at trie root */
231*0Sstevel@tonic-gate 	key &= mask;		/* apply mask */
232*0Sstevel@tonic-gate 	/* traverse trie to the correct position */
233*0Sstevel@tonic-gate 	for (pos = key_len; pos > 0; --pos) {
234*0Sstevel@tonic-gate 		/* check if bit is significant */
235*0Sstevel@tonic-gate 		/* bit in key is significant if it is covered by the mask */
236*0Sstevel@tonic-gate 		if (EXTRACTBIT(mask, (pos - 1), key_len) != 1) {
237*0Sstevel@tonic-gate 			/* check if this is a path compressed internal node */
238*0Sstevel@tonic-gate 			if (c_node->bits > 0) {
239*0Sstevel@tonic-gate 				/* check if split is needed */
240*0Sstevel@tonic-gate 				if ((pos - 1) > (c_node->pos - c_node->bits)) {
241*0Sstevel@tonic-gate 					t_split(&c_node, (pos - 1), key_len);
242*0Sstevel@tonic-gate 					ASSERT(c_node->one != NULL);
243*0Sstevel@tonic-gate 					ASSERT(c_node->zero != NULL);
244*0Sstevel@tonic-gate 				}
245*0Sstevel@tonic-gate 			}
246*0Sstevel@tonic-gate 			break;
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 		/* extra bit at current position */
249*0Sstevel@tonic-gate 		bit = EXTRACTBIT(key, (pos - 1), key_len);
250*0Sstevel@tonic-gate 		/* check if this is a path compressed internal node */
251*0Sstevel@tonic-gate 		if (c_node->bits > 0) { /* path compressed node */
252*0Sstevel@tonic-gate 			/* check if split is needed */
253*0Sstevel@tonic-gate 			if ((pos - 1) > (c_node->pos - c_node->bits)) {
254*0Sstevel@tonic-gate 				/* testing for mismatch */
255*0Sstevel@tonic-gate 				if (bit != EXTRACTBIT(c_node->val, (pos - 1),
256*0Sstevel@tonic-gate 				    key_len)) {
257*0Sstevel@tonic-gate 					t_split(&c_node, (pos - 1), key_len);
258*0Sstevel@tonic-gate 					ASSERT(c_node->one != NULL);
259*0Sstevel@tonic-gate 					ASSERT(c_node->zero != NULL);
260*0Sstevel@tonic-gate 				} else {
261*0Sstevel@tonic-gate 					continue; /* bits match, so go on */
262*0Sstevel@tonic-gate 				}
263*0Sstevel@tonic-gate 			} else if ((pos - 1) == (c_node->pos - c_node->bits)) {
264*0Sstevel@tonic-gate 				/* check if at a leaf node with elements */
265*0Sstevel@tonic-gate 				if ((c_node->one == NULL) &&
266*0Sstevel@tonic-gate 				    (c_node->zero == NULL) &&
267*0Sstevel@tonic-gate 				    (c_node->elements != NULL)) {
268*0Sstevel@tonic-gate 					/*
269*0Sstevel@tonic-gate 					 * this case occurs when mask for key
270*0Sstevel@tonic-gate 					 * is longer than mask for key at
271*0Sstevel@tonic-gate 					 * current node
272*0Sstevel@tonic-gate 					 */
273*0Sstevel@tonic-gate 					t_split(&c_node, (pos - 1), key_len);
274*0Sstevel@tonic-gate 					ASSERT(c_node->one != NULL);
275*0Sstevel@tonic-gate 					ASSERT(c_node->zero != NULL);
276*0Sstevel@tonic-gate 				}
277*0Sstevel@tonic-gate 			} /* else continue onto child */
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 		if (bit == ZERO) {
280*0Sstevel@tonic-gate 			if (c_node->zero == NULL) { /* leaf node */
281*0Sstevel@tonic-gate 				if (c_node->bits == 0) {
282*0Sstevel@tonic-gate 					c_node->pos = (pos - 1);
283*0Sstevel@tonic-gate 				}
284*0Sstevel@tonic-gate 				c_node->bits++;
285*0Sstevel@tonic-gate 				/* bit at pos for node value should be 0 */
286*0Sstevel@tonic-gate 				UNSETBIT(c_node->val, (pos - 1), key_len);
287*0Sstevel@tonic-gate 				SETBIT(c_node->mask, (pos - 1), 1, key_len);
288*0Sstevel@tonic-gate 			} else {
289*0Sstevel@tonic-gate 				/* assert that trie is path compressed */
290*0Sstevel@tonic-gate 				ASSERT(c_node->one != NULL);
291*0Sstevel@tonic-gate 				c_node = c_node->zero; /* internal node */
292*0Sstevel@tonic-gate 			}
293*0Sstevel@tonic-gate 		} else {	/* ONE bit */
294*0Sstevel@tonic-gate 			if (c_node->one == NULL) { /* leaf node */
295*0Sstevel@tonic-gate 				if (c_node->bits == 0) {
296*0Sstevel@tonic-gate 					c_node->pos = (pos - 1);
297*0Sstevel@tonic-gate 				}
298*0Sstevel@tonic-gate 				c_node->bits++;
299*0Sstevel@tonic-gate 				/* bit at pos for node value should be 1 */
300*0Sstevel@tonic-gate 				SETBIT(c_node->val, (pos - 1), 1, key_len);
301*0Sstevel@tonic-gate 				SETBIT(c_node->mask, (pos - 1), 1, key_len);
302*0Sstevel@tonic-gate 			} else {
303*0Sstevel@tonic-gate 				/* assert that trie is path compressed */
304*0Sstevel@tonic-gate 				ASSERT(c_node->zero != NULL);
305*0Sstevel@tonic-gate 				c_node = c_node->one; /* internal node */
306*0Sstevel@tonic-gate 			}
307*0Sstevel@tonic-gate 		}
308*0Sstevel@tonic-gate 	}
309*0Sstevel@tonic-gate 	/* insert at node */
310*0Sstevel@tonic-gate 	(void) ipgpc_list_insert(&c_node->elements, id);
311*0Sstevel@tonic-gate 	/* update stats */
312*0Sstevel@tonic-gate 	++tid->stats.num_inserted;
313*0Sstevel@tonic-gate 	/*
314*0Sstevel@tonic-gate 	 * check if this is the first key to be inserted that is not a
315*0Sstevel@tonic-gate 	 * don't care (*)
316*0Sstevel@tonic-gate 	 */
317*0Sstevel@tonic-gate 	if (tid->info.dontcareonly == B_TRUE) {
318*0Sstevel@tonic-gate 		tid->info.dontcareonly = B_FALSE;
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 	rw_exit(&tid->rw_lock);
321*0Sstevel@tonic-gate 	return (NORMAL_VALUE);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate /*
325*0Sstevel@tonic-gate  * t_insert6(tid, id, key, mask)
326*0Sstevel@tonic-gate  *
327*0Sstevel@tonic-gate  * specific to inserting keys of 128 bits in length
328*0Sstevel@tonic-gate  */
329*0Sstevel@tonic-gate int
t_insert6(trie_id_t * tid,key_t id,in6_addr_t key,in6_addr_t mask)330*0Sstevel@tonic-gate t_insert6(trie_id_t *tid, key_t id, in6_addr_t key, in6_addr_t mask)
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	node_t *c_node;
333*0Sstevel@tonic-gate 	int bit, i;
334*0Sstevel@tonic-gate 	uint8_t pos;
335*0Sstevel@tonic-gate 	uint8_t type_len = IP_ABITS;
336*0Sstevel@tonic-gate 	in6_addr_t zero_addr = IN6ADDR_ANY_INIT;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	/* don't insert if don't care */
339*0Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(&mask, &zero_addr)) {
340*0Sstevel@tonic-gate 		++tid->stats.num_dontcare;
341*0Sstevel@tonic-gate 		return (DONTCARE_VALUE);
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	rw_enter(&tid->rw_lock, RW_WRITER);
345*0Sstevel@tonic-gate 	c_node = tid->trie;	/* point at root of trie */
346*0Sstevel@tonic-gate 	V6_MASK_COPY(key, mask, key); /* apply mask to key */
347*0Sstevel@tonic-gate 	/*
348*0Sstevel@tonic-gate 	 * A IPv6 address is structured as an array of four uint32_t
349*0Sstevel@tonic-gate 	 * values.  The highest order of the bits are located in array[0]
350*0Sstevel@tonic-gate 	 */
351*0Sstevel@tonic-gate 	for (i = 0; i < 4; ++i) {
352*0Sstevel@tonic-gate 		/* traverse trie to the correct position */
353*0Sstevel@tonic-gate 		for (pos = type_len; pos > 0; --pos) {
354*0Sstevel@tonic-gate 			/* check if bit is significant */
355*0Sstevel@tonic-gate 			if (EXTRACTBIT(mask.s6_addr32[i], (pos - 1), type_len)
356*0Sstevel@tonic-gate 			    != ONE) {
357*0Sstevel@tonic-gate 				break;
358*0Sstevel@tonic-gate 			}
359*0Sstevel@tonic-gate 			bit = EXTRACTBIT(key.s6_addr32[i], (pos - 1), type_len);
360*0Sstevel@tonic-gate 			if (bit == ZERO) {
361*0Sstevel@tonic-gate 				if (c_node->zero == NULL) {
362*0Sstevel@tonic-gate 					c_node->zero = create_node(KM_SLEEP);
363*0Sstevel@tonic-gate 				}
364*0Sstevel@tonic-gate 				c_node = c_node->zero;
365*0Sstevel@tonic-gate 			} else {	/* ONE bit */
366*0Sstevel@tonic-gate 				if (c_node->one == NULL) {
367*0Sstevel@tonic-gate 					c_node->one = create_node(KM_SLEEP);
368*0Sstevel@tonic-gate 				}
369*0Sstevel@tonic-gate 				c_node = c_node->one;
370*0Sstevel@tonic-gate 			}
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 		}
373*0Sstevel@tonic-gate 	}
374*0Sstevel@tonic-gate 	/* insert at node */
375*0Sstevel@tonic-gate 	(void) ipgpc_list_insert(&c_node->elements, id);
376*0Sstevel@tonic-gate 	/* update stats */
377*0Sstevel@tonic-gate 	++tid->stats.num_inserted;
378*0Sstevel@tonic-gate 	/*
379*0Sstevel@tonic-gate 	 * check if this is the first key to be inserted that is not a
380*0Sstevel@tonic-gate 	 * don't care (*)
381*0Sstevel@tonic-gate 	 */
382*0Sstevel@tonic-gate 	if (tid->info.dontcareonly == B_TRUE) {
383*0Sstevel@tonic-gate 		tid->info.dontcareonly = B_FALSE;
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	rw_exit(&tid->rw_lock);
386*0Sstevel@tonic-gate 	return (NORMAL_VALUE);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate /*
390*0Sstevel@tonic-gate  * t_traverse_delete(in_node, pos, id, key, mask, tid)
391*0Sstevel@tonic-gate  *
392*0Sstevel@tonic-gate  * used to traverse to the node containing id, as found under key
393*0Sstevel@tonic-gate  * once id is found, it is removed from the trie.
394*0Sstevel@tonic-gate  * Upon removing the id from a given node in the trie, path compression
395*0Sstevel@tonic-gate  * will be applied to nodes that are no longer compressed.
396*0Sstevel@tonic-gate  * If the id is successfully removed, tid->stats are updated
397*0Sstevel@tonic-gate  */
398*0Sstevel@tonic-gate static boolean_t
t_traverse_delete(node_t ** in_node,uint8_t pos,key_t id,uint32_t key,uint32_t mask,trie_id_t ** tid)399*0Sstevel@tonic-gate t_traverse_delete(node_t **in_node, uint8_t pos, key_t id, uint32_t key,
400*0Sstevel@tonic-gate     uint32_t mask, trie_id_t **tid)
401*0Sstevel@tonic-gate {
402*0Sstevel@tonic-gate 	node_t *c_node = *in_node;
403*0Sstevel@tonic-gate 	node_t *t_node;
404*0Sstevel@tonic-gate 	int bit;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	if (c_node == NULL) {
407*0Sstevel@tonic-gate 		return (B_FALSE); /* base failure case */
408*0Sstevel@tonic-gate 	}
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	/* we've found the node the id is probably at */
411*0Sstevel@tonic-gate 	if ((pos == 0) ||
412*0Sstevel@tonic-gate 	    (EXTRACTBIT(mask, (pos - 1), (uint8_t)(*tid)->key_len) != 1)) {
413*0Sstevel@tonic-gate 		if (ipgpc_list_remove(&c_node->elements, id) == B_FALSE) {
414*0Sstevel@tonic-gate 			ipgpc0dbg(("t_traverse_delete: id %d does not " \
415*0Sstevel@tonic-gate 			    "exist in trie\n", id));
416*0Sstevel@tonic-gate 			return (B_FALSE); /* key does not exist at node */
417*0Sstevel@tonic-gate 		} else {
418*0Sstevel@tonic-gate 			/* update stats */
419*0Sstevel@tonic-gate 			--(*tid)->stats.num_inserted;
420*0Sstevel@tonic-gate 			/* check if 0 values are inserted in this trie */
421*0Sstevel@tonic-gate 			if ((*tid)->stats.num_inserted == 0) {
422*0Sstevel@tonic-gate 				/* update dontcareonly boolean */
423*0Sstevel@tonic-gate 				(*tid)->info.dontcareonly = B_TRUE;
424*0Sstevel@tonic-gate 			}
425*0Sstevel@tonic-gate 		}
426*0Sstevel@tonic-gate 		/* check if node has zero elements, is a LEAF node */
427*0Sstevel@tonic-gate 		if ((c_node->elements == NULL) &&
428*0Sstevel@tonic-gate 		    ((c_node->one == NULL) && (c_node->zero == NULL))) {
429*0Sstevel@tonic-gate 			/* make sure we don't delete the root */
430*0Sstevel@tonic-gate 			if (c_node->isroot != 1) {
431*0Sstevel@tonic-gate 				kmem_cache_free(trie_node_cache, c_node);
432*0Sstevel@tonic-gate 				return (B_TRUE);
433*0Sstevel@tonic-gate 			} else {
434*0Sstevel@tonic-gate 				/* this is the root, just zero out the info */
435*0Sstevel@tonic-gate 				c_node->pos = 0;
436*0Sstevel@tonic-gate 				c_node->bits = 0;
437*0Sstevel@tonic-gate 				c_node->val = 0;
438*0Sstevel@tonic-gate 				c_node->mask = 0;
439*0Sstevel@tonic-gate 			}
440*0Sstevel@tonic-gate 		}
441*0Sstevel@tonic-gate 		return (B_FALSE);
442*0Sstevel@tonic-gate 	}
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	/* check to see if node describes bits to skip */
445*0Sstevel@tonic-gate 	if (c_node->bits > 0) {
446*0Sstevel@tonic-gate 		if ((key & c_node->mask) != c_node->val) {
447*0Sstevel@tonic-gate 			ipgpc0dbg(("t_traverse_delete: id %d does not " \
448*0Sstevel@tonic-gate 			    "exist in trie\n", id));
449*0Sstevel@tonic-gate 			return (B_FALSE); /* key does not exist at node */
450*0Sstevel@tonic-gate 		}
451*0Sstevel@tonic-gate 		pos = (c_node->pos - c_node->bits) + 1;
452*0Sstevel@tonic-gate 		/* search should continue if mask and pos are valid */
453*0Sstevel@tonic-gate 		if ((pos == 0) ||
454*0Sstevel@tonic-gate 		    (EXTRACTBIT(mask, (pos - 1), (uint8_t)(*tid)->key_len)
455*0Sstevel@tonic-gate 			!= 1)) {
456*0Sstevel@tonic-gate 			/* this node probably contains the id */
457*0Sstevel@tonic-gate 			if (ipgpc_list_remove(&c_node->elements,
458*0Sstevel@tonic-gate 			    id) == B_FALSE) {
459*0Sstevel@tonic-gate 				ipgpc0dbg(("t_traverse_delete: id %d does" \
460*0Sstevel@tonic-gate 				    "not exist in trie\n", id));
461*0Sstevel@tonic-gate 				return (B_FALSE);
462*0Sstevel@tonic-gate 			} else {
463*0Sstevel@tonic-gate 				/* update stats */
464*0Sstevel@tonic-gate 				--(*tid)->stats.num_inserted;
465*0Sstevel@tonic-gate 				/* check if 0 values are inserted */
466*0Sstevel@tonic-gate 				if ((*tid)->stats.num_inserted == 0) {
467*0Sstevel@tonic-gate 					/* update dontcare boolean */
468*0Sstevel@tonic-gate 					(*tid)->info.dontcareonly = B_TRUE;
469*0Sstevel@tonic-gate 				}
470*0Sstevel@tonic-gate 			}
471*0Sstevel@tonic-gate 			/* check if node has zero elements & is a LEAF node */
472*0Sstevel@tonic-gate 			if ((c_node->elements == NULL) &&
473*0Sstevel@tonic-gate 			    ((c_node->one == NULL) &&
474*0Sstevel@tonic-gate 				(c_node->zero == NULL))) {
475*0Sstevel@tonic-gate 				/* make sure we don't delete the root */
476*0Sstevel@tonic-gate 				if (c_node->isroot != 1) {
477*0Sstevel@tonic-gate 					kmem_cache_free(trie_node_cache,
478*0Sstevel@tonic-gate 					    c_node);
479*0Sstevel@tonic-gate 					return (B_TRUE);
480*0Sstevel@tonic-gate 				} else {
481*0Sstevel@tonic-gate 					/* this is the root, zero out info */
482*0Sstevel@tonic-gate 					c_node->pos = 0;
483*0Sstevel@tonic-gate 					c_node->bits = 0;
484*0Sstevel@tonic-gate 					c_node->val = 0;
485*0Sstevel@tonic-gate 					c_node->mask = 0;
486*0Sstevel@tonic-gate 				}
487*0Sstevel@tonic-gate 			}
488*0Sstevel@tonic-gate 			return (B_FALSE);
489*0Sstevel@tonic-gate 		}
490*0Sstevel@tonic-gate 	}
491*0Sstevel@tonic-gate 	/* extract next bit and test */
492*0Sstevel@tonic-gate 	bit = EXTRACTBIT(key, (pos - 1), (uint8_t)(*tid)->key_len);
493*0Sstevel@tonic-gate 	if (bit == ZERO) {
494*0Sstevel@tonic-gate 		if (t_traverse_delete(&c_node->zero, (pos - 1), id, key, mask,
495*0Sstevel@tonic-gate 		    tid) == B_TRUE) {
496*0Sstevel@tonic-gate 			c_node->zero = NULL;
497*0Sstevel@tonic-gate 		}
498*0Sstevel@tonic-gate 	} else {	/* ONE bit */
499*0Sstevel@tonic-gate 		if (t_traverse_delete(&c_node->one, (pos - 1), id, key, mask,
500*0Sstevel@tonic-gate 		    tid) == B_TRUE) {
501*0Sstevel@tonic-gate 			c_node->one = NULL;
502*0Sstevel@tonic-gate 		}
503*0Sstevel@tonic-gate 	}
504*0Sstevel@tonic-gate 	/*
505*0Sstevel@tonic-gate 	 * non path-compressed nodes will contain one child and no elements
506*0Sstevel@tonic-gate 	 * what occurs here:
507*0Sstevel@tonic-gate 	 *	  *
508*0Sstevel@tonic-gate 	 *	 / \
509*0Sstevel@tonic-gate 	 *	*   *  <-- p_node->elements == NULL
510*0Sstevel@tonic-gate 	 *	   /
511*0Sstevel@tonic-gate 	 *	  *  <-- c_node->elements = foo
512*0Sstevel@tonic-gate 	 *	 / \
513*0Sstevel@tonic-gate 	 *	*   *  <-- children of c_node
514*0Sstevel@tonic-gate 	 * after:
515*0Sstevel@tonic-gate 	 *	  *
516*0Sstevel@tonic-gate 	 *	 / \
517*0Sstevel@tonic-gate 	 *	*   *   <-- p_node->elements = foo
518*0Sstevel@tonic-gate 	 *	   / \
519*0Sstevel@tonic-gate 	 *	  *   *  <-- p_node adopts children of c_node
520*0Sstevel@tonic-gate 	 */
521*0Sstevel@tonic-gate 	if ((c_node->one == NULL) && (c_node->zero != NULL)) {
522*0Sstevel@tonic-gate 		if (c_node->elements == NULL) {
523*0Sstevel@tonic-gate 			/* move child elements to parent */
524*0Sstevel@tonic-gate 			c_node->elements = c_node->zero->elements;
525*0Sstevel@tonic-gate 			/* be sure to include the link in the bits */
526*0Sstevel@tonic-gate 			c_node->bits += c_node->zero->bits + 1;
527*0Sstevel@tonic-gate 			/* c_node->pos will remain the same */
528*0Sstevel@tonic-gate 			c_node->mask |= c_node->zero->mask;
529*0Sstevel@tonic-gate 			/* don't forget to mark the link */
530*0Sstevel@tonic-gate 			SETBIT(c_node->mask, (pos - 1), 1,
531*0Sstevel@tonic-gate 			    (uint8_t)(*tid)->key_len);
532*0Sstevel@tonic-gate 			c_node->val |= c_node->zero->val;
533*0Sstevel@tonic-gate 			/* don't forget to mark the link  */
534*0Sstevel@tonic-gate 			UNSETBIT(c_node->val, (pos - 1),
535*0Sstevel@tonic-gate 			    (uint8_t)(*tid)->key_len);
536*0Sstevel@tonic-gate 			/* adopt children */
537*0Sstevel@tonic-gate 			t_node = c_node->zero;
538*0Sstevel@tonic-gate 			c_node->one = c_node->zero->one;
539*0Sstevel@tonic-gate 			c_node->zero = c_node->zero->zero;
540*0Sstevel@tonic-gate 			kmem_cache_free(trie_node_cache, t_node);
541*0Sstevel@tonic-gate 		} else {
542*0Sstevel@tonic-gate 			ASSERT(c_node->zero->one == NULL);
543*0Sstevel@tonic-gate 			ASSERT(c_node->zero->zero == NULL);
544*0Sstevel@tonic-gate 			kmem_cache_free(trie_node_cache, c_node->zero);
545*0Sstevel@tonic-gate 			c_node->zero = NULL;
546*0Sstevel@tonic-gate 		}
547*0Sstevel@tonic-gate 	} else if ((c_node->one != NULL) && (c_node->zero == NULL)) {
548*0Sstevel@tonic-gate 		if (c_node->elements == NULL) {
549*0Sstevel@tonic-gate 			/* move child elements to parent */
550*0Sstevel@tonic-gate 			c_node->elements = c_node->one->elements;
551*0Sstevel@tonic-gate 			/* be sure to include the link in the bits */
552*0Sstevel@tonic-gate 			c_node->bits += c_node->one->bits + 1;
553*0Sstevel@tonic-gate 			/* c_node->pos will remain the same */
554*0Sstevel@tonic-gate 			c_node->mask |= c_node->one->mask;
555*0Sstevel@tonic-gate 			/* don't forget to mark the link */
556*0Sstevel@tonic-gate 			SETBIT(c_node->mask, (pos - 1), 1,
557*0Sstevel@tonic-gate 			    (uint8_t)(*tid)->key_len);
558*0Sstevel@tonic-gate 			c_node->val |= c_node->one->val;
559*0Sstevel@tonic-gate 			/* don't forget to mark the link  */
560*0Sstevel@tonic-gate 			SETBIT(c_node->val, (pos - 1), 1,
561*0Sstevel@tonic-gate 			    (uint8_t)(*tid)->key_len);
562*0Sstevel@tonic-gate 			/* adopt children */
563*0Sstevel@tonic-gate 			t_node = c_node->one;
564*0Sstevel@tonic-gate 			c_node->zero = c_node->one->zero;
565*0Sstevel@tonic-gate 			c_node->one = c_node->one->one;
566*0Sstevel@tonic-gate 			kmem_cache_free(trie_node_cache, t_node);
567*0Sstevel@tonic-gate 		} else {
568*0Sstevel@tonic-gate 			ASSERT(c_node->one->one == NULL);
569*0Sstevel@tonic-gate 			ASSERT(c_node->one->zero == NULL);
570*0Sstevel@tonic-gate 			kmem_cache_free(trie_node_cache, c_node->one);
571*0Sstevel@tonic-gate 			c_node->one = NULL;
572*0Sstevel@tonic-gate 		}
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 	/* check if node has zero elements, is a LEAF node */
575*0Sstevel@tonic-gate 	if ((c_node->elements == NULL) &&
576*0Sstevel@tonic-gate 	    ((c_node->one == NULL) && (c_node->zero == NULL))) {
577*0Sstevel@tonic-gate 		/* make sure we don't delete the root */
578*0Sstevel@tonic-gate 		if (c_node->isroot != 1) {
579*0Sstevel@tonic-gate 			kmem_cache_free(trie_node_cache, c_node);
580*0Sstevel@tonic-gate 			return (B_TRUE);
581*0Sstevel@tonic-gate 		} else {
582*0Sstevel@tonic-gate 			/* this is the root, just zero out the info */
583*0Sstevel@tonic-gate 			c_node->pos = 0;
584*0Sstevel@tonic-gate 			c_node->bits = 0;
585*0Sstevel@tonic-gate 			c_node->val = 0;
586*0Sstevel@tonic-gate 			c_node->mask = 0;
587*0Sstevel@tonic-gate 		}
588*0Sstevel@tonic-gate 	}
589*0Sstevel@tonic-gate 	return (B_FALSE);
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate /*
595*0Sstevel@tonic-gate  * t_remove(tid, id, key, mask)
596*0Sstevel@tonic-gate  *
597*0Sstevel@tonic-gate  * removes a value associated with an id from the trie
598*0Sstevel@tonic-gate  * - if the item does not exist, nothing is removed
599*0Sstevel@tonic-gate  * - if more than one id share the same key, only the id specified is removed
600*0Sstevel@tonic-gate  */
601*0Sstevel@tonic-gate void
t_remove(trie_id_t * tid,key_t id,uint32_t key,uint32_t mask)602*0Sstevel@tonic-gate t_remove(trie_id_t *tid, key_t id, uint32_t key, uint32_t mask)
603*0Sstevel@tonic-gate {
604*0Sstevel@tonic-gate 	node_t *c_node;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	/* don't cares are not inserted */
607*0Sstevel@tonic-gate 	if (mask == 0) {
608*0Sstevel@tonic-gate 		--tid->stats.num_dontcare;
609*0Sstevel@tonic-gate 		return;
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	key &= mask;		/* apply mask */
613*0Sstevel@tonic-gate 	/* traverse to node containing id and remove the id from the trie */
614*0Sstevel@tonic-gate 	rw_enter(&tid->rw_lock, RW_WRITER);
615*0Sstevel@tonic-gate 	c_node = tid->trie;
616*0Sstevel@tonic-gate 	(void) t_traverse_delete(&c_node, (uint8_t)tid->key_len, id, key, mask,
617*0Sstevel@tonic-gate 	    &tid);
618*0Sstevel@tonic-gate 	rw_exit(&tid->rw_lock);
619*0Sstevel@tonic-gate }
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate /*
622*0Sstevel@tonic-gate  * t_remove6(tid, id, key, mask)
623*0Sstevel@tonic-gate  *
624*0Sstevel@tonic-gate  * specific to removing key of 128 bits in length
625*0Sstevel@tonic-gate  */
626*0Sstevel@tonic-gate void
t_remove6(trie_id_t * tid,key_t id,in6_addr_t key,in6_addr_t mask)627*0Sstevel@tonic-gate t_remove6(trie_id_t *tid, key_t id, in6_addr_t key, in6_addr_t mask)
628*0Sstevel@tonic-gate {
629*0Sstevel@tonic-gate 	node_t *c_node;
630*0Sstevel@tonic-gate 	int bit, i;
631*0Sstevel@tonic-gate 	uint8_t pos;
632*0Sstevel@tonic-gate 	uint8_t type_len = IP_ABITS;
633*0Sstevel@tonic-gate 	in6_addr_t zero_addr = IN6ADDR_ANY_INIT;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	/* don't cares are not inserted */
636*0Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(&mask, &zero_addr)) {
637*0Sstevel@tonic-gate 		--tid->stats.num_dontcare;
638*0Sstevel@tonic-gate 		return;
639*0Sstevel@tonic-gate 	}
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	rw_enter(&tid->rw_lock, RW_WRITER);
642*0Sstevel@tonic-gate 	c_node = tid->trie;	/* point at root of trie */
643*0Sstevel@tonic-gate 	V6_MASK_COPY(key, mask, key);
644*0Sstevel@tonic-gate 	/*
645*0Sstevel@tonic-gate 	 * A IPv6 address is structured as an array of four uint32_t
646*0Sstevel@tonic-gate 	 * values.  The higest order of the bits are located in array[0]
647*0Sstevel@tonic-gate 	 */
648*0Sstevel@tonic-gate 	for (i = 0; i < 4; ++i) {
649*0Sstevel@tonic-gate 		/* traverse trie to the correct position */
650*0Sstevel@tonic-gate 		for (pos = type_len; pos > 0; --pos) {
651*0Sstevel@tonic-gate 			/* check if bit is significant */
652*0Sstevel@tonic-gate 			if (EXTRACTBIT(mask.s6_addr32[i], (pos - 1), type_len)
653*0Sstevel@tonic-gate 			    != ONE) {
654*0Sstevel@tonic-gate 				break;
655*0Sstevel@tonic-gate 			}
656*0Sstevel@tonic-gate 			bit = EXTRACTBIT(key.s6_addr32[i], (pos - 1), type_len);
657*0Sstevel@tonic-gate 			if (bit == ZERO) {
658*0Sstevel@tonic-gate 				if (c_node->zero == NULL) {
659*0Sstevel@tonic-gate 					break;
660*0Sstevel@tonic-gate 				}
661*0Sstevel@tonic-gate 				c_node = c_node->zero;
662*0Sstevel@tonic-gate 			} else {	/* ONE bit */
663*0Sstevel@tonic-gate 				if (c_node->one == NULL) {
664*0Sstevel@tonic-gate 					break;
665*0Sstevel@tonic-gate 				}
666*0Sstevel@tonic-gate 				c_node = c_node->one;
667*0Sstevel@tonic-gate 			}
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 		}
670*0Sstevel@tonic-gate 	}
671*0Sstevel@tonic-gate 	if (c_node != NULL) {
672*0Sstevel@tonic-gate 		if (ipgpc_list_remove(&c_node->elements, id)) {
673*0Sstevel@tonic-gate 			/* update stats */
674*0Sstevel@tonic-gate 			--tid->stats.num_inserted;
675*0Sstevel@tonic-gate 			/*
676*0Sstevel@tonic-gate 			 * check to see if only dontcare's are inserted
677*0Sstevel@tonic-gate 			 */
678*0Sstevel@tonic-gate 			if (tid->stats.num_inserted <= 0) {
679*0Sstevel@tonic-gate 				tid->info.dontcareonly = B_TRUE;
680*0Sstevel@tonic-gate 			}
681*0Sstevel@tonic-gate 		}
682*0Sstevel@tonic-gate 	}
683*0Sstevel@tonic-gate 	rw_exit(&tid->rw_lock);
684*0Sstevel@tonic-gate }
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate /*
688*0Sstevel@tonic-gate  * t_retrieve(tid, key, fid_table)
689*0Sstevel@tonic-gate  *
690*0Sstevel@tonic-gate  * returns the number of found filters that match the input key
691*0Sstevel@tonic-gate  * - each value that matches either a partial or exact match on the key
692*0Sstevel@tonic-gate  *   is inserted into the fid_table
693*0Sstevel@tonic-gate  * - some nodes may contain multiple id's, all items will be inserted
694*0Sstevel@tonic-gate  *   into the fid_table
695*0Sstevel@tonic-gate  * - the find stops when an edge node is reached, the left and right nodes
696*0Sstevel@tonic-gate  *   for the current node are null
697*0Sstevel@tonic-gate  * - 0 is returned if no matches are found, otherwise the number of matches
698*0Sstevel@tonic-gate  *   is returned
699*0Sstevel@tonic-gate  * - (-1) is returned if a memory error occurred
700*0Sstevel@tonic-gate  */
701*0Sstevel@tonic-gate int
t_retrieve(trie_id_t * tid,uint32_t key,ht_match_t * fid_table)702*0Sstevel@tonic-gate t_retrieve(trie_id_t *tid, uint32_t key, ht_match_t *fid_table)
703*0Sstevel@tonic-gate {
704*0Sstevel@tonic-gate 	int bit;
705*0Sstevel@tonic-gate 	uint8_t pos;
706*0Sstevel@tonic-gate 	int num_found = 0;
707*0Sstevel@tonic-gate 	int ret;
708*0Sstevel@tonic-gate 	node_t *c_node;
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	rw_enter(&tid->rw_lock, RW_READER);
711*0Sstevel@tonic-gate 	c_node = tid->trie;	/* point at root of trie */
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	/* ensure trie structure is allocated */
714*0Sstevel@tonic-gate 	if (c_node == NULL) {
715*0Sstevel@tonic-gate 		rw_exit(&tid->rw_lock);
716*0Sstevel@tonic-gate 		return (num_found);
717*0Sstevel@tonic-gate 	}
718*0Sstevel@tonic-gate 	/*
719*0Sstevel@tonic-gate 	 * foreach node encountered in the search, collect elements and append
720*0Sstevel@tonic-gate 	 * to a list to be returned
721*0Sstevel@tonic-gate 	 */
722*0Sstevel@tonic-gate 	for (pos = (uint8_t)tid->key_len; pos > 0; --pos) {
723*0Sstevel@tonic-gate 		/* check node for bits to check */
724*0Sstevel@tonic-gate 		if (c_node->bits > 0) {
725*0Sstevel@tonic-gate 			if ((key & c_node->mask) != c_node->val) {
726*0Sstevel@tonic-gate 				rw_exit(&tid->rw_lock);
727*0Sstevel@tonic-gate 				return (num_found); /* search is done */
728*0Sstevel@tonic-gate 			}
729*0Sstevel@tonic-gate 			/* pos is set to next bit not covered by node */
730*0Sstevel@tonic-gate 			if ((pos = (c_node->pos - c_node->bits) + 1) == 0) {
731*0Sstevel@tonic-gate 				/* if node covers rest of bits in key */
732*0Sstevel@tonic-gate 				break;
733*0Sstevel@tonic-gate 			}
734*0Sstevel@tonic-gate 		}
735*0Sstevel@tonic-gate 		/* check node for elements */
736*0Sstevel@tonic-gate 		if (c_node->elements != NULL) {
737*0Sstevel@tonic-gate 			if ((ret = ipgpc_mark_found(tid->info.mask,
738*0Sstevel@tonic-gate 			    c_node->elements, fid_table)) == -1) {
739*0Sstevel@tonic-gate 				/* signifies a memory error */
740*0Sstevel@tonic-gate 				rw_exit(&tid->rw_lock);
741*0Sstevel@tonic-gate 				return (-1);
742*0Sstevel@tonic-gate 			}
743*0Sstevel@tonic-gate 			num_found += ret; /* increment num_found */
744*0Sstevel@tonic-gate 		}
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 		bit = EXTRACTBIT(key, (pos - 1), (uint8_t)tid->key_len);
747*0Sstevel@tonic-gate 		if (bit == ZERO) { /* choose leaf */
748*0Sstevel@tonic-gate 			c_node = c_node->zero;
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 		} else {	/* bit == ONE */
751*0Sstevel@tonic-gate 			c_node = c_node->one;
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 		}
754*0Sstevel@tonic-gate 		if (c_node == NULL) {
755*0Sstevel@tonic-gate 			/* search is finished, edge node reached */
756*0Sstevel@tonic-gate 			rw_exit(&tid->rw_lock);
757*0Sstevel@tonic-gate 			return (num_found);
758*0Sstevel@tonic-gate 		}
759*0Sstevel@tonic-gate 	}
760*0Sstevel@tonic-gate 	/* see if current node contains elements */
761*0Sstevel@tonic-gate 	if (c_node->elements != NULL) {
762*0Sstevel@tonic-gate 		if ((ret = ipgpc_mark_found(tid->info.mask, c_node->elements,
763*0Sstevel@tonic-gate 		    fid_table)) == -1) {
764*0Sstevel@tonic-gate 			rw_exit(&tid->rw_lock);
765*0Sstevel@tonic-gate 			return (-1); /* signifies a memory error */
766*0Sstevel@tonic-gate 		}
767*0Sstevel@tonic-gate 		num_found += ret; /* increment num_found */
768*0Sstevel@tonic-gate 	}
769*0Sstevel@tonic-gate 	rw_exit(&tid->rw_lock);
770*0Sstevel@tonic-gate 	return (num_found);
771*0Sstevel@tonic-gate }
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate /*
774*0Sstevel@tonic-gate  * t_retrieve6(tid, key, fid_table)
775*0Sstevel@tonic-gate  *
776*0Sstevel@tonic-gate  * specific to retrieving keys of 128 bits in length
777*0Sstevel@tonic-gate  */
778*0Sstevel@tonic-gate int
t_retrieve6(trie_id_t * tid,in6_addr_t key,ht_match_t * fid_table)779*0Sstevel@tonic-gate t_retrieve6(trie_id_t *tid, in6_addr_t key, ht_match_t *fid_table)
780*0Sstevel@tonic-gate {
781*0Sstevel@tonic-gate 	int bit, i;
782*0Sstevel@tonic-gate 	uint8_t pos;
783*0Sstevel@tonic-gate 	int num_found = 0;
784*0Sstevel@tonic-gate 	int ret;
785*0Sstevel@tonic-gate 	node_t *c_node;
786*0Sstevel@tonic-gate 	uint8_t type_len = IP_ABITS;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	rw_enter(&tid->rw_lock, RW_READER);
789*0Sstevel@tonic-gate 	c_node = tid->trie;
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	/* ensure trie structure is allocated */
792*0Sstevel@tonic-gate 	if (c_node == NULL) {
793*0Sstevel@tonic-gate 		rw_exit(&tid->rw_lock);
794*0Sstevel@tonic-gate 		return (num_found);
795*0Sstevel@tonic-gate 	}
796*0Sstevel@tonic-gate 	/*
797*0Sstevel@tonic-gate 	 * A IPv6 address is structured as an array of four uint32_t
798*0Sstevel@tonic-gate 	 * values.  The higest order of the bits are located in array[0]
799*0Sstevel@tonic-gate 	 */
800*0Sstevel@tonic-gate 	for (i = 0; i < 4; ++i) {
801*0Sstevel@tonic-gate 		/*
802*0Sstevel@tonic-gate 		 * foreach node encountered in the search, collect elements
803*0Sstevel@tonic-gate 		 * and append to a list to be returned
804*0Sstevel@tonic-gate 		 */
805*0Sstevel@tonic-gate 		for (pos = type_len; pos > 0; --pos) {
806*0Sstevel@tonic-gate 			/* extract bit at pos */
807*0Sstevel@tonic-gate 			bit =
808*0Sstevel@tonic-gate 			    EXTRACTBIT(key.s6_addr32[i], (pos - 1), type_len);
809*0Sstevel@tonic-gate 			if (bit == ZERO) { /* choose leaf */
810*0Sstevel@tonic-gate 				c_node = c_node->zero;
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 			} else {
813*0Sstevel@tonic-gate 				c_node = c_node->one;
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 			}
816*0Sstevel@tonic-gate 			if (c_node == NULL) {
817*0Sstevel@tonic-gate 				/* search is finished, edge node reached */
818*0Sstevel@tonic-gate 				rw_exit(&tid->rw_lock);
819*0Sstevel@tonic-gate 				return (num_found);
820*0Sstevel@tonic-gate 			}
821*0Sstevel@tonic-gate 			/* see if current node contains elements */
822*0Sstevel@tonic-gate 			if (c_node->elements != NULL) {
823*0Sstevel@tonic-gate 				if ((ret = ipgpc_mark_found(tid->info.mask,
824*0Sstevel@tonic-gate 				    c_node->elements, fid_table)) == -1) {
825*0Sstevel@tonic-gate 					/* signifies a memory error */
826*0Sstevel@tonic-gate 					rw_exit(&tid->rw_lock);
827*0Sstevel@tonic-gate 					return (-1);
828*0Sstevel@tonic-gate 				}
829*0Sstevel@tonic-gate 				num_found += ret; /* increment num_found */
830*0Sstevel@tonic-gate 			}
831*0Sstevel@tonic-gate 		}
832*0Sstevel@tonic-gate 	}
833*0Sstevel@tonic-gate 	rw_exit(&tid->rw_lock);
834*0Sstevel@tonic-gate 	return (num_found);
835*0Sstevel@tonic-gate }
836