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