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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002 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 } flow_t;
141 
142 /* From the IP header */
143 typedef struct header {
144 	uint_t		dir;
145 	uint_t		len;
146 	in6_addr_t	saddr;
147 	in6_addr_t	daddr;
148 	uint16_t	sport;
149 	uint16_t	dport;
150 	uint16_t	ident;
151 	uint8_t		proto;
152 	uint8_t		dsfield;
153 	projid_t	projid;
154 	uid_t		uid;
155 	boolean_t	isv4;
156 	uint32_t	pktlen;
157 } header_t;
158 
159 
160 typedef struct flowacct_data_s {
161 	ipp_action_id_t next_action; 		/* action id of next action */
162 	char		*act_name;		/* action name of next action */
163 	uint64_t 	timer;			/* flow timer */
164 	uint64_t 	timeout;		/* flow timeout */
165 	uint32_t	max_limit;		/* max flow entries */
166 	uint32_t 	nflows;			/* no. of flows */
167 	kmutex_t	lock;			/* for nflows */
168 
169 	/* TRhe flow table. We'll use the last bucket for timeout purposes */
170 	list_head_t flows_tbl[FLOW_TBL_COUNT+1];
171 	boolean_t 	global_stats;		/* global stats */
172 
173 	uint64_t 	tbytes;			/* no. of bytes in flow tbl. */
174 	uint64_t 	nbytes;			/* no. of bytes seen */
175 	uint64_t 	npackets;		/* no. of pkts seen */
176 	uint64_t 	usedmem;		/* mem used by flow table */
177 	uint64_t 	epackets;		/* packets in error */
178 	ipp_stat_t 	*stats;
179 	timeout_id_t	flow_tid;
180 
181 } flowacct_data_t;
182 
183 #define	FLOWACCT_DATA_SZ	sizeof (flowacct_data_t)
184 #define	FLOWACCT_HDR_SZ		sizeof (list_hdr_t)
185 #define	FLOWACCT_HEAD_SZ	sizeof (list_head_t)
186 #define	FLOWACCT_FLOW_SZ	sizeof (flow_t)
187 #define	FLOWACCT_ITEM_SZ	sizeof (flow_item_t)
188 #define	FLOWACCT_HEADER_SZ	sizeof (header_t)
189 #define	FLOWACCT_FLOW_RECORD_SZ (FLOWACCT_HDR_SZ + FLOWACCT_FLOW_SZ)
190 #define	FLOWACCT_ITEM_RECORD_SZ (FLOWACCT_HDR_SZ + FLOWACCT_ITEM_SZ)
191 
192 extern int flowacct_process(mblk_t **, flowacct_data_t *);
193 extern void flowacct_timer(int, flowacct_data_t *);
194 extern void flowacct_timeout_flows(void *);
195 
196 #endif /* _KERNEL */
197 
198 #ifdef	__cplusplus
199 }
200 #endif
201 
202 #endif /* _IPP_FLOWACCT_FLOWACCT_IMPL_H */
203