17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6d730a61Svi  * Common Development and Distribution License (the "License").
6*6d730a61Svi  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*6d730a61Svi 
227c478bd9Sstevel@tonic-gate /*
23*6d730a61Svi  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _IPP_FLOWACCT_FLOWACCT_IMPL_H
287c478bd9Sstevel@tonic-gate #define	_IPP_FLOWACCT_FLOWACCT_IMPL_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
327c478bd9Sstevel@tonic-gate #include <ipp/ipp.h>
337c478bd9Sstevel@tonic-gate #include <inet/ipp_common.h>
347c478bd9Sstevel@tonic-gate #include <ipp/flowacct/flowacct.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
377c478bd9Sstevel@tonic-gate extern "C" {
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* Header file for implementation of flowacct */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	_FLOWACCT_DEBUG
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifdef _FLOWACCT_DEBUG
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #define	flowacct0dbg(a)		printf a
497c478bd9Sstevel@tonic-gate #define	flowacct1dbg(a)		if (flowacct_debug > 2) printf a
507c478bd9Sstevel@tonic-gate #define	flowacct2dbg(a)		if (flowacct_debug > 3) printf a
517c478bd9Sstevel@tonic-gate #else
527c478bd9Sstevel@tonic-gate #define	flowacct0dbg(a)		/*  */
537c478bd9Sstevel@tonic-gate #define	flowacct1dbg(a)		/*  */
547c478bd9Sstevel@tonic-gate #define	flowacct2dbg(a)		/*  */
557c478bd9Sstevel@tonic-gate #endif /* _FLOWACCT_DEBUG */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	FLOWACCT_PURGE_FLOW	0x01
587c478bd9Sstevel@tonic-gate #define	FLOWACCT_FLOW_TIMER	0x02
597c478bd9Sstevel@tonic-gate #define	FLOWACCT_JUST_ONE	0x03
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* Flow Table Size */
627c478bd9Sstevel@tonic-gate #define	FLOW_TBL_COUNT	((uint_t)256)
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* To identify objects in the list - could be a flow or an item */
657c478bd9Sstevel@tonic-gate #define	FLOWACCT_FLOW		0x01
667c478bd9Sstevel@tonic-gate #define	FLOWACCT_ITEM		0x02
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* Whether an object has to be physically removed from the table */
697c478bd9Sstevel@tonic-gate #define	FLOWACCT_DEL_OBJ		0x01
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* Utility macros to convert from msec to usec/nsec */
727c478bd9Sstevel@tonic-gate #define	FLOWACCT_MSEC_TO_USEC		(1000)
737c478bd9Sstevel@tonic-gate #define	FLOWACCT_MSEC_TO_NSEC		(1000000)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Default values for timer and timeout - taken from SBM
777c478bd9Sstevel@tonic-gate  * timer 15 secs (15000 msec) and timeout 60 secs (60000 msec).
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #define	FLOWACCT_DEF_TIMER		(15000)
807c478bd9Sstevel@tonic-gate #define	FLOWACCT_DEF_TIMEOUT		(60000)
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* List holding an obj - flow or item */
837c478bd9Sstevel@tonic-gate typedef struct	list_hdr_s {
847c478bd9Sstevel@tonic-gate 	struct	list_hdr_s	*next;
857c478bd9Sstevel@tonic-gate 	struct	list_hdr_s	*prev;
867c478bd9Sstevel@tonic-gate 	struct	list_hdr_s	*timeout_next;
877c478bd9Sstevel@tonic-gate 	struct	list_hdr_s	*timeout_prev;
887c478bd9Sstevel@tonic-gate 	timespec_t		last_seen;
897c478bd9Sstevel@tonic-gate 	void			*objp;
907c478bd9Sstevel@tonic-gate } list_hdr_t;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* List of list of flows */
937c478bd9Sstevel@tonic-gate typedef struct list_head_s {
947c478bd9Sstevel@tonic-gate 	list_hdr_t	*head;
957c478bd9Sstevel@tonic-gate 	list_hdr_t	*tail;
967c478bd9Sstevel@tonic-gate 	uint_t		nbr_items;
977c478bd9Sstevel@tonic-gate 	uint_t		max_items;
987c478bd9Sstevel@tonic-gate 	kmutex_t	lock;
997c478bd9Sstevel@tonic-gate } list_head_t;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /* Global stats for flowacct */
1027c478bd9Sstevel@tonic-gate typedef struct flowacct_stat_s {
1037c478bd9Sstevel@tonic-gate 	ipp_named_t npackets;		/* no. of pkts seen by this instance */
1047c478bd9Sstevel@tonic-gate 	ipp_named_t nbytes;		/* no. of bytes seen by this instance */
1057c478bd9Sstevel@tonic-gate 	ipp_named_t nflows;		/* no. of flow items in the table */
1067c478bd9Sstevel@tonic-gate 	ipp_named_t tbytes;		/* no. of bytes in the flow table */
1077c478bd9Sstevel@tonic-gate 	ipp_named_t usedmem;		/* memory used by the flow table */
1087c478bd9Sstevel@tonic-gate 	ipp_named_t epackets;		/* no. of pkts. in error */
1097c478bd9Sstevel@tonic-gate } flowacct_stat_t;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	FLOWACCT_STATS_COUNT	6
1127c478bd9Sstevel@tonic-gate #define	FLOWACCT_STATS_STRING	"Flowacct statistics"
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Item common to a flow (identified by 5-tuple) */
1157c478bd9Sstevel@tonic-gate typedef struct flow_item_s {
1167c478bd9Sstevel@tonic-gate 	uint_t		type;
1177c478bd9Sstevel@tonic-gate 	list_hdr_t	*hdr;
1187c478bd9Sstevel@tonic-gate 	timespec_t	creation_time;
1197c478bd9Sstevel@tonic-gate 	uint64_t	npackets;
1207c478bd9Sstevel@tonic-gate 	uint64_t	nbytes;
1217c478bd9Sstevel@tonic-gate 	uint8_t		dsfield;
1227c478bd9Sstevel@tonic-gate 	projid_t	projid;
1237c478bd9Sstevel@tonic-gate 	uid_t		uid;
1247c478bd9Sstevel@tonic-gate } flow_item_t;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* Flow attributes */
1277c478bd9Sstevel@tonic-gate typedef struct flow_s {
1287c478bd9Sstevel@tonic-gate 	uint_t		type;
1297c478bd9Sstevel@tonic-gate 	list_hdr_t	*hdr;
1307c478bd9Sstevel@tonic-gate 	in6_addr_t	saddr;
1317c478bd9Sstevel@tonic-gate 	in6_addr_t	daddr;
1327c478bd9Sstevel@tonic-gate 	uint8_t		proto;
1337c478bd9Sstevel@tonic-gate 	uint16_t	sport;
1347c478bd9Sstevel@tonic-gate 	uint16_t	dport;
1357c478bd9Sstevel@tonic-gate 	list_head_t	items;
1367c478bd9Sstevel@tonic-gate 	list_head_t	*back_ptr;
1377c478bd9Sstevel@tonic-gate 	boolean_t	isv4;
138*6d730a61Svi 	/*
139*6d730a61Svi 	 * to indicate to the flow timer not to delete this flow
140*6d730a61Svi 	 */
141*6d730a61Svi 	boolean_t	inuse;
1427c478bd9Sstevel@tonic-gate } flow_t;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* From the IP header */
1457c478bd9Sstevel@tonic-gate typedef struct header {
1467c478bd9Sstevel@tonic-gate 	uint_t		dir;
1477c478bd9Sstevel@tonic-gate 	uint_t		len;
1487c478bd9Sstevel@tonic-gate 	in6_addr_t	saddr;
1497c478bd9Sstevel@tonic-gate 	in6_addr_t	daddr;
1507c478bd9Sstevel@tonic-gate 	uint16_t	sport;
1517c478bd9Sstevel@tonic-gate 	uint16_t	dport;
1527c478bd9Sstevel@tonic-gate 	uint16_t	ident;
1537c478bd9Sstevel@tonic-gate 	uint8_t		proto;
1547c478bd9Sstevel@tonic-gate 	uint8_t		dsfield;
1557c478bd9Sstevel@tonic-gate 	projid_t	projid;
1567c478bd9Sstevel@tonic-gate 	uid_t		uid;
1577c478bd9Sstevel@tonic-gate 	boolean_t	isv4;
1587c478bd9Sstevel@tonic-gate 	uint32_t	pktlen;
1597c478bd9Sstevel@tonic-gate } header_t;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate typedef struct flowacct_data_s {
1637c478bd9Sstevel@tonic-gate 	ipp_action_id_t next_action; 		/* action id of next action */
1647c478bd9Sstevel@tonic-gate 	char		*act_name;		/* action name of next action */
1657c478bd9Sstevel@tonic-gate 	uint64_t 	timer;			/* flow timer */
1667c478bd9Sstevel@tonic-gate 	uint64_t 	timeout;		/* flow timeout */
1677c478bd9Sstevel@tonic-gate 	uint32_t	max_limit;		/* max flow entries */
1687c478bd9Sstevel@tonic-gate 	uint32_t 	nflows;			/* no. of flows */
1697c478bd9Sstevel@tonic-gate 	kmutex_t	lock;			/* for nflows */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* TRhe flow table. We'll use the last bucket for timeout purposes */
1727c478bd9Sstevel@tonic-gate 	list_head_t flows_tbl[FLOW_TBL_COUNT+1];
1737c478bd9Sstevel@tonic-gate 	boolean_t 	global_stats;		/* global stats */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	uint64_t 	tbytes;			/* no. of bytes in flow tbl. */
1767c478bd9Sstevel@tonic-gate 	uint64_t 	nbytes;			/* no. of bytes seen */
1777c478bd9Sstevel@tonic-gate 	uint64_t 	npackets;		/* no. of pkts seen */
1787c478bd9Sstevel@tonic-gate 	uint64_t 	usedmem;		/* mem used by flow table */
1797c478bd9Sstevel@tonic-gate 	uint64_t 	epackets;		/* packets in error */
1807c478bd9Sstevel@tonic-gate 	ipp_stat_t 	*stats;
1817c478bd9Sstevel@tonic-gate 	timeout_id_t	flow_tid;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate } flowacct_data_t;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #define	FLOWACCT_DATA_SZ	sizeof (flowacct_data_t)
1867c478bd9Sstevel@tonic-gate #define	FLOWACCT_HDR_SZ		sizeof (list_hdr_t)
1877c478bd9Sstevel@tonic-gate #define	FLOWACCT_HEAD_SZ	sizeof (list_head_t)
1887c478bd9Sstevel@tonic-gate #define	FLOWACCT_FLOW_SZ	sizeof (flow_t)
1897c478bd9Sstevel@tonic-gate #define	FLOWACCT_ITEM_SZ	sizeof (flow_item_t)
1907c478bd9Sstevel@tonic-gate #define	FLOWACCT_HEADER_SZ	sizeof (header_t)
1917c478bd9Sstevel@tonic-gate #define	FLOWACCT_FLOW_RECORD_SZ (FLOWACCT_HDR_SZ + FLOWACCT_FLOW_SZ)
1927c478bd9Sstevel@tonic-gate #define	FLOWACCT_ITEM_RECORD_SZ (FLOWACCT_HDR_SZ + FLOWACCT_ITEM_SZ)
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate extern int flowacct_process(mblk_t **, flowacct_data_t *);
1957c478bd9Sstevel@tonic-gate extern void flowacct_timer(int, flowacct_data_t *);
1967c478bd9Sstevel@tonic-gate extern void flowacct_timeout_flows(void *);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate #endif
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate #endif /* _IPP_FLOWACCT_FLOWACCT_IMPL_H */
205