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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _IPP_FLOWACCT_FLOWACCT_IMPL_H
28 #define	_IPP_FLOWACCT_FLOWACCT_IMPL_H
29 
30 #include <sys/types.h>
31 #include <sys/cmn_err.h>
32 #include <ipp/ipp.h>
33 #include <inet/ipp_common.h>
34 #include <ipp/flowacct/flowacct.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /* Header file for implementation of flowacct */
41 
42 #ifdef	_KERNEL
43 
44 #define	_FLOWACCT_DEBUG
45 
46 #ifdef _FLOWACCT_DEBUG
47 #include <sys/debug.h>
48 #define	flowacct0dbg(a)		printf a
49 #define	flowacct1dbg(a)		if (flowacct_debug > 2) printf a
50 #define	flowacct2dbg(a)		if (flowacct_debug > 3) printf a
51 #else
52 #define	flowacct0dbg(a)		/*  */
53 #define	flowacct1dbg(a)		/*  */
54 #define	flowacct2dbg(a)		/*  */
55 #endif /* _FLOWACCT_DEBUG */
56 
57 #define	FLOWACCT_PURGE_FLOW	0x01
58 #define	FLOWACCT_FLOW_TIMER	0x02
59 #define	FLOWACCT_JUST_ONE	0x03
60 
61 /* Flow Table Size */
62 #define	FLOW_TBL_COUNT	((uint_t)256)
63 
64 /* To identify objects in the list - could be a flow or an item */
65 #define	FLOWACCT_FLOW		0x01
66 #define	FLOWACCT_ITEM		0x02
67 
68 /* Whether an object has to be physically removed from the table */
69 #define	FLOWACCT_DEL_OBJ		0x01
70 
71 /* Utility macros to convert from msec to usec/nsec */
72 #define	FLOWACCT_MSEC_TO_USEC		(1000)
73 #define	FLOWACCT_MSEC_TO_NSEC		(1000000)
74 
75 /*
76  * Default values for timer and timeout - taken from SBM
77  * timer 15 secs (15000 msec) and timeout 60 secs (60000 msec).
78  */
79 #define	FLOWACCT_DEF_TIMER		(15000)
80 #define	FLOWACCT_DEF_TIMEOUT		(60000)
81 
82 /* List holding an obj - flow or item */
83 typedef struct	list_hdr_s {
84 	struct	list_hdr_s	*next;
85 	struct	list_hdr_s	*prev;
86 	struct	list_hdr_s	*timeout_next;
87 	struct	list_hdr_s	*timeout_prev;
88 	timespec_t		last_seen;
89 	void			*objp;
90 } list_hdr_t;
91 
92 /* List of list of flows */
93 typedef struct list_head_s {
94 	list_hdr_t	*head;
95 	list_hdr_t	*tail;
96 	uint_t		nbr_items;
97 	uint_t		max_items;
98 	kmutex_t	lock;
99 } list_head_t;
100 
101 /* Global stats for flowacct */
102 typedef struct flowacct_stat_s {
103 	ipp_named_t npackets;		/* no. of pkts seen by this instance */
104 	ipp_named_t nbytes;		/* no. of bytes seen by this instance */
105 	ipp_named_t nflows;		/* no. of flow items in the table */
106 	ipp_named_t tbytes;		/* no. of bytes in the flow table */
107 	ipp_named_t usedmem;		/* memory used by the flow table */
108 	ipp_named_t epackets;		/* no. of pkts. in error */
109 } flowacct_stat_t;
110 
111 #define	FLOWACCT_STATS_COUNT	6
112 #define	FLOWACCT_STATS_STRING	"Flowacct statistics"
113 
114 /* Item common to a flow (identified by 5-tuple) */
115 typedef struct flow_item_s {
116 	uint_t		type;
117 	list_hdr_t	*hdr;
118 	timespec_t	creation_time;
119 	uint64_t	npackets;
120 	uint64_t	nbytes;
121 	uint8_t		dsfield;
122 	projid_t	projid;
123 	uid_t		uid;
124 } flow_item_t;
125 
126 /* Flow attributes */
127 typedef struct flow_s {
128 	uint_t		type;
129 	list_hdr_t	*hdr;
130 	in6_addr_t	saddr;
131 	in6_addr_t	daddr;
132 	uint8_t		proto;
133 	uint16_t	sport;
134 	uint16_t	dport;
135 	list_head_t	items;
136 	list_head_t	*back_ptr;
137 	boolean_t	isv4;
138 	/*
139 	 * to indicate to the flow timer not to delete this flow
140 	 */
141 	boolean_t	inuse;
142 } flow_t;
143 
144 /* From the IP header */
145 typedef struct header {
146 	uint_t		dir;
147 	uint_t		len;
148 	in6_addr_t	saddr;
149 	in6_addr_t	daddr;
150 	uint16_t	sport;
151 	uint16_t	dport;
152 	uint16_t	ident;
153 	uint8_t		proto;
154 	uint8_t		dsfield;
155 	projid_t	projid;
156 	uid_t		uid;
157 	boolean_t	isv4;
158 	uint32_t	pktlen;
159 } header_t;
160 
161 
162 typedef struct flowacct_data_s {
163 	ipp_action_id_t next_action; 		/* action id of next action */
164 	char		*act_name;		/* action name of next action */
165 	uint64_t 	timer;			/* flow timer */
166 	uint64_t 	timeout;		/* flow timeout */
167 	uint32_t	max_limit;		/* max flow entries */
168 	uint32_t 	nflows;			/* no. of flows */
169 	kmutex_t	lock;			/* for nflows */
170 
171 	/* TRhe flow table. We'll use the last bucket for timeout purposes */
172 	list_head_t flows_tbl[FLOW_TBL_COUNT+1];
173 	boolean_t 	global_stats;		/* global stats */
174 
175 	uint64_t 	tbytes;			/* no. of bytes in flow tbl. */
176 	uint64_t 	nbytes;			/* no. of bytes seen */
177 	uint64_t 	npackets;		/* no. of pkts seen */
178 	uint64_t 	usedmem;		/* mem used by flow table */
179 	uint64_t 	epackets;		/* packets in error */
180 	ipp_stat_t 	*stats;
181 	timeout_id_t	flow_tid;
182 
183 } flowacct_data_t;
184 
185 #define	FLOWACCT_DATA_SZ	sizeof (flowacct_data_t)
186 #define	FLOWACCT_HDR_SZ		sizeof (list_hdr_t)
187 #define	FLOWACCT_HEAD_SZ	sizeof (list_head_t)
188 #define	FLOWACCT_FLOW_SZ	sizeof (flow_t)
189 #define	FLOWACCT_ITEM_SZ	sizeof (flow_item_t)
190 #define	FLOWACCT_HEADER_SZ	sizeof (header_t)
191 #define	FLOWACCT_FLOW_RECORD_SZ (FLOWACCT_HDR_SZ + FLOWACCT_FLOW_SZ)
192 #define	FLOWACCT_ITEM_RECORD_SZ (FLOWACCT_HDR_SZ + FLOWACCT_ITEM_SZ)
193 
194 extern int flowacct_process(mblk_t **, flowacct_data_t *);
195 extern void flowacct_timer(int, flowacct_data_t *);
196 extern void flowacct_timeout_flows(void *);
197 
198 #endif /* _KERNEL */
199 
200 #ifdef	__cplusplus
201 }
202 #endif
203 
204 #endif /* _IPP_FLOWACCT_FLOWACCT_IMPL_H */
205