1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _IPP_IPGPC_CLASSIFIER_OBJECTS_H
27 #define	_IPP_IPGPC_CLASSIFIER_OBJECTS_H
28 
29 #include <sys/time.h>
30 #include <ipp/ipp.h>
31 #include <ipp/ipgpc/ipgpc.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /* common objects and defines used by the ipgpc code base */
38 
39 /* default wildcard and unspecified value for selectors */
40 #define	IPGPC_WILDCARD		-1
41 #define	IPGPC_UNSPECIFIED	0
42 
43 /* trie id's */
44 #define	IPGPC_TRIE_SPORTID	0
45 #define	IPGPC_TRIE_DPORTID	1
46 #define	IPGPC_TRIE_SADDRID	2
47 #define	IPGPC_TRIE_DADDRID	3
48 
49 /*
50  * IPv6 trie id's
51  * note: tries for SPORT, DPORT are shared between IPv4 and IPv6 filters
52  */
53 #define	IPGPC_TRIE_SADDRID6	4
54 #define	IPGPC_TRIE_DADDRID6	5
55 
56 /* ba table id's */
57 #define	IPGPC_BA_DSID		6
58 
59 /* table id's */
60 #define	IPGPC_TABLE_PROTOID	7
61 #define	IPGPC_TABLE_UID		8
62 #define	IPGPC_TABLE_PROJID	9
63 #define	IPGPC_TABLE_IF		10
64 #define	IPGPC_TABLE_DIR		11
65 #define	TABLE_ID_OFFSET		IPGPC_TABLE_PROTOID
66 #define	PROTOID_IDX		(IPGPC_TABLE_PROTOID - TABLE_ID_OFFSET)
67 #define	UID_IDX			(IPGPC_TABLE_UID - TABLE_ID_OFFSET)
68 #define	PROJID_IDX		(IPGPC_TABLE_PROJID - TABLE_ID_OFFSET)
69 #define	IF_IDX			(IPGPC_TABLE_IF - TABLE_ID_OFFSET)
70 #define	DIR_IDX			(IPGPC_TABLE_DIR - TABLE_ID_OFFSET)
71 
72 /* Match types for selector searching */
73 #define	NORMAL_MATCH		0
74 #define	NO_MATCHES		1
75 #define	DONTCARE_ONLY_MATCH	2
76 
77 /* match masks */
78 #define	PROTO_MASK	0x01
79 #define	DS_MASK		0x02
80 #define	SPORT_MASK	0x04
81 #define	DPORT_MASK	0x08
82 #define	SADDR_MASK	0x10
83 #define	DADDR_MASK	0x20
84 #define	SADDR6_MASK	SADDR_MASK
85 #define	DADDR6_MASK	DADDR_MASK
86 #define	UID_MASK	0x40
87 #define	PROJID_MASK	0x80
88 #define	IF_MASK		0x100
89 #define	DIR_MASK	0x200
90 #define	ALL_MATCH_MASK	(DS_MASK | PROTO_MASK | SADDR_MASK | DADDR_MASK | \
91 			SPORT_MASK | DPORT_MASK | UID_MASK | PROJID_MASK | \
92 			IF_MASK | DIR_MASK)
93 
94 #define	HASH_SIZE    	11	/* default hash table size */
95 
96 /* used when inserting values into selector structures */
97 #define	NORMAL_VALUE	0	/* a valid value was insert */
98 #define	DONTCARE_VALUE	1	/* a dontcare/wildcard value was inserted */
99 
100 /* filter definition structure */
101 typedef struct ipgpc_filter_s {
102 	char filter_name[MAXNAMELEN]; /* null terminated name of filter */
103 
104 	/* exact match selectors */
105 	uid_t uid;		/* uid key, value = exact or IPGPC_WILDCARD */
106 	projid_t projid;	/* project id, " " */
107 	uint_t if_index;	/* interface index, " " or 0 for wildcard */
108 	/*
109 	 * packet direction
110 	 * value = IPP_LOCAL_IN | IPP_LOCAL_OUT |
111 	 * IPP_FWD_IN | IPP_FWD_OUT | 0 for wildcard
112 	 */
113 	uint32_t direction;
114 	uint8_t proto;		/* protocol key, exact or 0 for wildcard */
115 
116 	/* non-exact match selectors */
117 	uint8_t dsfield;	/* diffserv field key */
118 	uint8_t dsfield_mask;	/* mask for diffserv field key */
119 	/* IP Addresses are represented as IPV6 address structures */
120 	in6_addr_t saddr;	/* source address key */
121 	in6_addr_t saddr_mask;	/* mask for saddr key */
122 	char *saddr_hostname;	/* hostname of source address, optional */
123 	in6_addr_t daddr;	/* destination address key */
124 	in6_addr_t daddr_mask;	/* mask for daddr key */
125 	char *daddr_hostname;	/* hostname of destination address, optional */
126 	uint16_t sport;		/* source port key */
127 	uint16_t sport_mask;	/* mask for sport key */
128 	uint16_t dport;		/* destination port key */
129 	uint16_t dport_mask;	/* mask for dport key */
130 
131 	/* filter ranking variables */
132 	uint32_t precedence;		/* precedence value for filter */
133 	uint32_t priority;		/* filter priority */
134 
135 	/*
136 	 * filter_type accepted values =
137 	 * IPGPC_GENERIC_FLTR | IPGPC_V4_FLTR |
138 	 * IPGPC_V6_FLTR
139 	 */
140 	uint8_t filter_type;
141 	int32_t filter_instance; /* filter instance number, -1 if unused */
142 	uint32_t originator;	/* originator of this config item */
143 	char *filter_comment;	/* optional and unused by ipgpc */
144 } ipgpc_filter_t;
145 
146 typedef struct ipgpc_class_stats_s {
147 	ipp_action_id_t next_action; /* next action id */
148 	hrtime_t last_match;	/* hrtime value of last match to class */
149 	uint64_t nbytes;	/* number of matching bytes */
150 	uint64_t npackets;	/* number of matching packets */
151 } ipgpc_class_stats_t;
152 
153 /* linked list Element node structure */
154 typedef struct element_node_s *linked_list;
155 typedef struct element_node_s *plink;
156 typedef struct element_node_s {
157 	plink next;
158 	void (*element_ref)(struct element_node_s *);
159 	void (*element_unref)(struct element_node_s *);
160 	int id;
161 	uint32_t element_refcnt;
162 } element_node_t;
163 
164 /* trie node structure  */
165 typedef struct node_s *node_p;
166 typedef struct node_s {
167 	linked_list elements;	/* pointer to element list */
168 	node_p zero;		/* left link */
169 	node_p one;		/* right link */
170 	uint32_t val;		/* value of bits covered */
171 	uint32_t mask;		/* mask of bits covered */
172 	uint8_t bits;		/* number of bits covered by this node */
173 	uint8_t pos;		/* starting position of bits covered */
174 	uint16_t isroot;	/* 1 if is root node, 0 otherwise */
175 } node_t;
176 typedef node_p trie;
177 
178 /* hashtable node structure */
179 typedef struct ht_node_s *hash_table;
180 typedef struct ht_node_s *ht_node_p;
181 typedef struct ht_node_s {
182 	ht_node_p next;		/* link to next node in chain */
183 	linked_list elements;	/* elements stored at this node */
184 	int key;		/* key stored at this node */
185 	int info;
186 } ht_node_t;
187 
188 /* behavior aggregate table element structure */
189 typedef struct ba_table_element_s {
190 	linked_list filter_list; /* list of filters */
191 	uint32_t info;
192 } ba_table_element_t;
193 
194 /* behavior aggregate table structure */
195 typedef struct ba_table_s {
196 	linked_list masks;	/* list of loaded masks */
197 	ba_table_element_t masked_values[256]; /* table of masked values */
198 } ba_table_t;
199 
200 /* selector information structure */
201 typedef struct sel_info_s {
202 	uint16_t  mask;		/* mask for marking  */
203 	boolean_t dontcareonly;	/* true if only don't cares are loaded */
204 } sel_info_t;
205 
206 /* selector statistics structure */
207 typedef struct sel_stats_s {
208 	uint32_t num_inserted; /* number of nodes that are not dontcares */
209 	uint32_t num_dontcare;	/* number of nodes that are dontcares */
210 } sel_stats_t;
211 
212 /* identification structure for a trie */
213 typedef struct trie_id_s {
214 	trie   trie;		/* pointer to the trie structure */
215 	krwlock_t rw_lock;	/* lock protecting this trie */
216 	size_t key_len;		/* length (bits) of the key for a lookup */
217 	sel_stats_t stats;	/* selector statistics strucutre */
218 	sel_info_t info;	/* selector info structure */
219 } trie_id_t;
220 
221 /* identification structure for a table */
222 typedef struct table_id_s {
223 	hash_table table;	/* pointer to the hash table structure */
224 	int wildcard;		/* wildcard value for this selector */
225 	sel_stats_t stats;	/* selector statistics strucutre */
226 	sel_info_t info;	/* selector info structure */
227 } table_id_t;
228 
229 /* identification structure for a ba_table */
230 typedef struct ba_table_id_s {
231 	ba_table_t table;
232 	kmutex_t lock;		/* ba table lock */
233 	sel_info_t info;	/* selector info structure */
234 	sel_stats_t stats;	/* selector statistics structure */
235 } ba_table_id_t;
236 
237 /* class definition structure  */
238 typedef struct ipgpc_class_s {
239 	ipp_action_id_t next_action; /* id of action at head of list */
240 	boolean_t gather_stats;	/* are stats desired? B_TRUE or B_FALSE */
241 	uint32_t originator;	/* originator of this config item */
242 	char class_name[MAXNAMELEN]; /* name of classification */
243 } ipgpc_class_t;
244 
245 /* filter id association data structure */
246 typedef struct fid_s {
247 	int info;		/* 0 if unused, -1 if dirty, 1 if used */
248 	int class_id;		/* id of class associated with filter */
249 	uint16_t insert_map;	/* selectors w/ values inserted for this fid */
250 	ipgpc_filter_t filter;	/* filter structure that this fid describes */
251 } fid_t;
252 
253 /* class_id structure */
254 typedef struct cid_s {
255 	linked_list filter_list; /* list of filters associated with class */
256 	int info;		/* 0 if unused, -1 if dirty, 1 if used */
257 	ipgpc_class_t aclass;	/* the class structure this cid describes */
258 	ipp_stat_t *cl_stats;	/* kstats structure */
259 	ipgpc_class_stats_t stats; /* statistics structure for class */
260 } cid_t;
261 
262 /* ipp_stat global stats structure */
263 typedef struct globalstats_s {
264 	ipp_named_t nfilters;
265 	ipp_named_t nclasses;
266 	ipp_named_t nbytes;
267 	ipp_named_t npackets;
268 	ipp_named_t epackets;
269 } globalstats_t;
270 
271 /* ipp_stat class stats structure */
272 typedef struct classstats_s {
273 	ipp_named_t nbytes;
274 	ipp_named_t npackets;
275 	ipp_named_t last_match;
276 } classstats_t;
277 
278 /* matching hash table element */
279 typedef struct ht_match_s *ht_chain;
280 typedef struct ht_match_s {
281 	ht_chain next;		/* link to next node in chain */
282 	int key;		/* key stored at this node in the table */
283 	uint16_t match_map;	/* match map for this id */
284 } ht_match_t;
285 
286 extern kmem_cache_t *ht_node_cache;
287 extern kmem_cache_t *element_node_cache;
288 extern kmem_cache_t *ht_match_cache;
289 extern kmem_cache_t *trie_node_cache;
290 
291 #ifdef	__cplusplus
292 }
293 #endif
294 
295 #endif	/* _IPP_IPGPC_CLASSIFIER_OBJECTS_H */
296