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