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 <sys/errno.h>
30*0Sstevel@tonic-gate #include <ipp/ipgpc/classifier.h>
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate /* Implementation file for behavior aggregate (BA) lookup table */
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate * ba_insert(bataid, filter_id, val, mask)
36*0Sstevel@tonic-gate *
37*0Sstevel@tonic-gate * inserts filter_id into element list of bataid->table->masked_values
38*0Sstevel@tonic-gate * at position val& mask, mask is inserted into the mask list.
39*0Sstevel@tonic-gate * filter_id shouldn't already exist in element list at
40*0Sstevel@tonic-gate * bataid->table->masked_values[val], an error message is printed if this
41*0Sstevel@tonic-gate * occurs.
42*0Sstevel@tonic-gate * return DONTCARE_VALUE if mask == 0, NORMAL_VALUE otherwise
43*0Sstevel@tonic-gate */
44*0Sstevel@tonic-gate int
ba_insert(ba_table_id_t * bataid,int filter_id,uint8_t val,uint8_t mask)45*0Sstevel@tonic-gate ba_insert(ba_table_id_t *bataid, int filter_id, uint8_t val, uint8_t mask)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate uint8_t mskd_val = val & mask;
48*0Sstevel@tonic-gate ba_table_t *table = &bataid->table;
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate /* dontcares are not inserted */
51*0Sstevel@tonic-gate if (mask == 0) {
52*0Sstevel@tonic-gate ++bataid->stats.num_dontcare;
53*0Sstevel@tonic-gate return (DONTCARE_VALUE);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate if (bataid->info.dontcareonly == B_TRUE) {
57*0Sstevel@tonic-gate bataid->info.dontcareonly = B_FALSE;
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate if (ipgpc_list_insert(&table->masked_values[mskd_val].filter_list,
61*0Sstevel@tonic-gate filter_id) == EEXIST) {
62*0Sstevel@tonic-gate ipgpc0dbg(("ba_insert():filter_id %d EEXIST in ba_table",
63*0Sstevel@tonic-gate filter_id));
64*0Sstevel@tonic-gate } else {
65*0Sstevel@tonic-gate /* insert mask */
66*0Sstevel@tonic-gate (void) ipgpc_list_insert(&table->masks, mask);
67*0Sstevel@tonic-gate /* update stats */
68*0Sstevel@tonic-gate ++table->masked_values[mskd_val].info;
69*0Sstevel@tonic-gate ++bataid->stats.num_inserted;
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate return (NORMAL_VALUE);
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate /*
75*0Sstevel@tonic-gate * ba_retrieve(bataid, value, fid_table)
76*0Sstevel@tonic-gate *
77*0Sstevel@tonic-gate * searches for all filters matching value in bataid->table
78*0Sstevel@tonic-gate * search is performed by appling each mask in bataid->table->masks list
79*0Sstevel@tonic-gate * to value and then looking value up in bataid->table->masked_values.
80*0Sstevel@tonic-gate * Each filter id that is matched, is inserted into fid_table
81*0Sstevel@tonic-gate * returns number of matched filters or (-1) if memory error
82*0Sstevel@tonic-gate */
83*0Sstevel@tonic-gate int
ba_retrieve(ba_table_id_t * bataid,uint8_t value,ht_match_t * fid_table)84*0Sstevel@tonic-gate ba_retrieve(ba_table_id_t *bataid, uint8_t value, ht_match_t *fid_table)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate element_node_t *p;
87*0Sstevel@tonic-gate element_node_t *filter_list;
88*0Sstevel@tonic-gate int num_found = 0;
89*0Sstevel@tonic-gate int ret;
90*0Sstevel@tonic-gate int masked_value = 0;
91*0Sstevel@tonic-gate ba_table_t *table = &bataid->table;
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate /* special case, if value == 0, no need to apply masks */
94*0Sstevel@tonic-gate if (value == 0) {
95*0Sstevel@tonic-gate /* masked value will always be 0 for this case */
96*0Sstevel@tonic-gate filter_list =
97*0Sstevel@tonic-gate table->masked_values[0].filter_list;
98*0Sstevel@tonic-gate if ((num_found = ipgpc_mark_found(bataid->info.mask,
99*0Sstevel@tonic-gate filter_list, fid_table)) == -1) {
100*0Sstevel@tonic-gate return (-1); /* signifies a memory error */
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate return (num_found);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate /* apply each mask to the value and do the look up in the ba table */
106*0Sstevel@tonic-gate for (p = table->masks; p != NULL; p = p->next) {
107*0Sstevel@tonic-gate masked_value = (uint8_t)(p->id) & value;
108*0Sstevel@tonic-gate if (bataid->table.masked_values[masked_value].info == 0) {
109*0Sstevel@tonic-gate /* masked_value has 0 filters associated with it */
110*0Sstevel@tonic-gate continue;
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate filter_list =
113*0Sstevel@tonic-gate table->masked_values[masked_value].filter_list;
114*0Sstevel@tonic-gate if ((ret = ipgpc_mark_found(bataid->info.mask, filter_list,
115*0Sstevel@tonic-gate fid_table)) == -1) {
116*0Sstevel@tonic-gate return (-1); /* signifies a memory error */
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate num_found += ret; /* increment num_found */
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate return (num_found);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate * ba_remove(bataid, filter_id, value, mask)
126*0Sstevel@tonic-gate *
127*0Sstevel@tonic-gate * removes filter_id from bataid->table->masked_values[mask & value]
128*0Sstevel@tonic-gate * mask is removed from bataid->table->masks if refcnt == 0 for that list
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate void
ba_remove(ba_table_id_t * bataid,int filter_id,uint8_t value,uint8_t mask)131*0Sstevel@tonic-gate ba_remove(ba_table_id_t *bataid, int filter_id, uint8_t value, uint8_t mask)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate uint8_t masked_value = value & mask;
134*0Sstevel@tonic-gate ba_table_t *table = &bataid->table;
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate /* dontcares are not inserted */
137*0Sstevel@tonic-gate if (mask == 0) {
138*0Sstevel@tonic-gate --bataid->stats.num_dontcare;
139*0Sstevel@tonic-gate return;
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate if (ipgpc_list_remove(&table->masked_values[masked_value].filter_list,
143*0Sstevel@tonic-gate filter_id) == B_TRUE) {
144*0Sstevel@tonic-gate /* update stats */
145*0Sstevel@tonic-gate --table->masked_values[masked_value].info;
146*0Sstevel@tonic-gate --bataid->stats.num_inserted;
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate * check to see if removing this entry will result in
149*0Sstevel@tonic-gate * don't cares only inserted in the table
150*0Sstevel@tonic-gate */
151*0Sstevel@tonic-gate if (bataid->stats.num_inserted <= 0) {
152*0Sstevel@tonic-gate bataid->info.dontcareonly = B_TRUE;
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate /* remove mask if refcnt == 0 */
155*0Sstevel@tonic-gate (void) ipgpc_list_remove(&table->masks, mask);
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate }
158