1142178Smlaier/*	$FreeBSD$	*/
2130365Smlaier/*	$KAME: altq_var.h,v 1.16 2003/10/03 05:05:15 kjc Exp $	*/
3130365Smlaier
4130365Smlaier/*
5130365Smlaier * Copyright (C) 1998-2003
6130365Smlaier *	Sony Computer Science Laboratories Inc.  All rights reserved.
7130365Smlaier *
8130365Smlaier * Redistribution and use in source and binary forms, with or without
9130365Smlaier * modification, are permitted provided that the following conditions
10130365Smlaier * are met:
11130365Smlaier * 1. Redistributions of source code must retain the above copyright
12130365Smlaier *    notice, this list of conditions and the following disclaimer.
13130365Smlaier * 2. Redistributions in binary form must reproduce the above copyright
14130365Smlaier *    notice, this list of conditions and the following disclaimer in the
15130365Smlaier *    documentation and/or other materials provided with the distribution.
16130365Smlaier *
17130365Smlaier * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18130365Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19130365Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20130365Smlaier * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21130365Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22130365Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23130365Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24130365Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25130365Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26130365Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27130365Smlaier * SUCH DAMAGE.
28130365Smlaier */
29130365Smlaier#ifndef _ALTQ_ALTQ_VAR_H_
30130365Smlaier#define	_ALTQ_ALTQ_VAR_H_
31130365Smlaier
32130365Smlaier#ifdef _KERNEL
33130365Smlaier
34130365Smlaier#include <sys/param.h>
35130365Smlaier#include <sys/kernel.h>
36130365Smlaier#include <sys/queue.h>
37130365Smlaier
38130365Smlaier#ifdef ALTQ3_CLFIER_COMPAT
39130365Smlaier/*
40130365Smlaier * filter structure for altq common classifier
41130365Smlaier */
42130365Smlaierstruct acc_filter {
43130365Smlaier	LIST_ENTRY(acc_filter)	f_chain;
44130365Smlaier	void			*f_class;	/* pointer to the class */
45130365Smlaier	u_long			f_handle;	/* filter id */
46130365Smlaier	u_int32_t		f_fbmask;	/* filter bitmask */
47130365Smlaier	struct flow_filter	f_filter;	/* filter value */
48130365Smlaier};
49130365Smlaier
50130365Smlaier/*
51130365Smlaier * XXX ACC_FILTER_TABLESIZE can't be larger than 2048 unless we fix
52130365Smlaier * the handle assignment.
53130365Smlaier */
54130365Smlaier#define	ACC_FILTER_TABLESIZE	(256+1)
55130365Smlaier#define	ACC_FILTER_MASK		(ACC_FILTER_TABLESIZE - 2)
56130365Smlaier#define	ACC_WILDCARD_INDEX	(ACC_FILTER_TABLESIZE - 1)
57130365Smlaier#ifdef __GNUC__
58130365Smlaier#define	ACC_GET_HASH_INDEX(addr) \
59130365Smlaier	({int x = (addr) + ((addr) >> 16); (x + (x >> 8)) & ACC_FILTER_MASK;})
60130365Smlaier#else
61130365Smlaier#define	ACC_GET_HASH_INDEX(addr) \
62130365Smlaier	(((addr) + ((addr) >> 8) + ((addr) >> 16) + ((addr) >> 24)) \
63130365Smlaier	& ACC_FILTER_MASK)
64130365Smlaier#endif
65130365Smlaier#define	ACC_GET_HINDEX(handle) ((handle) >> 20)
66130365Smlaier
67130365Smlaier#if (__FreeBSD_version > 500000)
68130365Smlaier#define ACC_LOCK_INIT(ac)	mtx_init(&(ac)->acc_mtx, "classifier", MTX_DEF)
69130365Smlaier#define ACC_LOCK_DESTROY(ac)	mtx_destroy(&(ac)->acc_mtx)
70130365Smlaier#define ACC_LOCK(ac)		mtx_lock(&(ac)->acc_mtx)
71130365Smlaier#define ACC_UNLOCK(ac)		mtx_unlock(&(ac)->acc_mtx)
72130365Smlaier#else
73130365Smlaier#define ACC_LOCK_INIT(ac)
74130365Smlaier#define ACC_LOCK_DESTROY(ac)
75130365Smlaier#define ACC_LOCK(ac)
76130365Smlaier#define ACC_UNLOCK(ac)
77130365Smlaier#endif
78130365Smlaier
79130365Smlaierstruct acc_classifier {
80130365Smlaier	u_int32_t			acc_fbmask;
81130365Smlaier	LIST_HEAD(filt, acc_filter)	acc_filters[ACC_FILTER_TABLESIZE];
82130365Smlaier
83130365Smlaier#if (__FreeBSD_version > 500000)
84130365Smlaier	struct	mtx acc_mtx;
85130365Smlaier#endif
86130365Smlaier};
87130365Smlaier
88130365Smlaier/*
89130365Smlaier * flowinfo mask bits used by classifier
90130365Smlaier */
91130365Smlaier/* for ipv4 */
92130365Smlaier#define	FIMB4_PROTO	0x0001
93130365Smlaier#define	FIMB4_TOS	0x0002
94130365Smlaier#define	FIMB4_DADDR	0x0004
95130365Smlaier#define	FIMB4_SADDR	0x0008
96130365Smlaier#define	FIMB4_DPORT	0x0010
97130365Smlaier#define	FIMB4_SPORT	0x0020
98130365Smlaier#define	FIMB4_GPI	0x0040
99130365Smlaier#define	FIMB4_ALL	0x007f
100130365Smlaier/* for ipv6 */
101130365Smlaier#define	FIMB6_PROTO	0x0100
102130365Smlaier#define	FIMB6_TCLASS	0x0200
103130365Smlaier#define	FIMB6_DADDR	0x0400
104130365Smlaier#define	FIMB6_SADDR	0x0800
105130365Smlaier#define	FIMB6_DPORT	0x1000
106130365Smlaier#define	FIMB6_SPORT	0x2000
107130365Smlaier#define	FIMB6_GPI	0x4000
108130365Smlaier#define	FIMB6_FLABEL	0x8000
109130365Smlaier#define	FIMB6_ALL	0xff00
110130365Smlaier
111130365Smlaier#define	FIMB_ALL	(FIMB4_ALL|FIMB6_ALL)
112130365Smlaier
113130365Smlaier#define	FIMB4_PORTS	(FIMB4_DPORT|FIMB4_SPORT|FIMB4_GPI)
114130365Smlaier#define	FIMB6_PORTS	(FIMB6_DPORT|FIMB6_SPORT|FIMB6_GPI)
115130365Smlaier#endif /* ALTQ3_CLFIER_COMPAT */
116130365Smlaier
117130365Smlaier/*
118130365Smlaier * machine dependent clock
119130365Smlaier * a 64bit high resolution time counter.
120130365Smlaier */
121130365Smlaierextern int machclk_usepcc;
122130365Smlaierextern u_int32_t machclk_freq;
123130365Smlaierextern u_int32_t machclk_per_tick;
124130365Smlaierextern void init_machclk(void);
125130365Smlaierextern u_int64_t read_machclk(void);
126130365Smlaier
127130365Smlaier/*
128130365Smlaier * debug support
129130365Smlaier */
130130365Smlaier#ifdef ALTQ_DEBUG
131130365Smlaier#ifdef __STDC__
132130365Smlaier#define	ASSERT(e)	((e) ? (void)0 : altq_assert(__FILE__, __LINE__, #e))
133130365Smlaier#else	/* PCC */
134130365Smlaier#define	ASSERT(e)	((e) ? (void)0 : altq_assert(__FILE__, __LINE__, "e"))
135130365Smlaier#endif
136130365Smlaier#else
137130365Smlaier#define	ASSERT(e)	((void)0)
138130365Smlaier#endif
139130365Smlaier
140130365Smlaier/*
141130365Smlaier * misc stuff for compatibility
142130365Smlaier */
143130365Smlaier/* ioctl cmd type */
144130365Smlaiertypedef u_long ioctlcmd_t;
145130365Smlaier
146130365Smlaier/*
147130365Smlaier * queue macros:
148130365Smlaier * the interface of TAILQ_LAST macro changed after the introduction
149130365Smlaier * of softupdate. redefine it here to make it work with pre-2.2.7.
150130365Smlaier */
151130365Smlaier#undef TAILQ_LAST
152130365Smlaier#define	TAILQ_LAST(head, headname) \
153130365Smlaier	(*(((struct headname *)((head)->tqh_last))->tqh_last))
154130365Smlaier
155130365Smlaier#ifndef TAILQ_EMPTY
156130365Smlaier#define	TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
157130365Smlaier#endif
158130365Smlaier#ifndef TAILQ_FOREACH
159130365Smlaier#define TAILQ_FOREACH(var, head, field)					\
160130365Smlaier	for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
161130365Smlaier#endif
162130365Smlaier
163130365Smlaier/* macro for timeout/untimeout */
164130365Smlaier#if (__FreeBSD_version > 300000) || defined(__NetBSD__)
165130365Smlaier/* use callout */
166130365Smlaier#include <sys/callout.h>
167130365Smlaier
168130365Smlaier#if (__FreeBSD_version > 500000)
169130365Smlaier#define	CALLOUT_INIT(c)		callout_init((c), 0)
170130365Smlaier#else
171130365Smlaier#define	CALLOUT_INIT(c)		callout_init((c))
172130365Smlaier#endif
173130365Smlaier#define	CALLOUT_RESET(c,t,f,a)	callout_reset((c),(t),(f),(a))
174130365Smlaier#define	CALLOUT_STOP(c)		callout_stop((c))
175142178Smlaier#if !defined(CALLOUT_INITIALIZER) && (__FreeBSD_version < 600000)
176130365Smlaier#define	CALLOUT_INITIALIZER	{ { { NULL } }, 0, NULL, NULL, 0 }
177130365Smlaier#endif
178130365Smlaier#elif defined(__OpenBSD__)
179130365Smlaier#include <sys/timeout.h>
180130365Smlaier/* callout structure as a wrapper of struct timeout */
181130365Smlaierstruct callout {
182130365Smlaier	struct timeout	c_to;
183130365Smlaier};
184130365Smlaier#define	CALLOUT_INIT(c)		do { bzero((c), sizeof(*(c))); } while (/*CONSTCOND*/ 0)
185130365Smlaier#define	CALLOUT_RESET(c,t,f,a)	do { if (!timeout_initialized(&(c)->c_to))  \
186130365Smlaier					 timeout_set(&(c)->c_to, (f), (a)); \
187130365Smlaier				     timeout_add(&(c)->c_to, (t)); } while (/*CONSTCOND*/ 0)
188130365Smlaier#define	CALLOUT_STOP(c)		timeout_del(&(c)->c_to)
189130365Smlaier#define	CALLOUT_INITIALIZER	{ { { NULL }, NULL, NULL, 0, 0 } }
190130365Smlaier#else
191130365Smlaier/* use old-style timeout/untimeout */
192130365Smlaier/* dummy callout structure */
193130365Smlaierstruct callout {
194130365Smlaier	void		*c_arg;			/* function argument */
195130365Smlaier	void		(*c_func)(void *);	/* functiuon to call */
196130365Smlaier};
197130365Smlaier#define	CALLOUT_INIT(c)		do { bzero((c), sizeof(*(c))); } while (/*CONSTCOND*/ 0)
198130365Smlaier#define	CALLOUT_RESET(c,t,f,a)	do {	(c)->c_arg = (a);	\
199130365Smlaier					(c)->c_func = (f);	\
200130365Smlaier					timeout((f),(a),(t)); } while (/*CONSTCOND*/ 0)
201130365Smlaier#define	CALLOUT_STOP(c)		untimeout((c)->c_func,(c)->c_arg)
202130365Smlaier#define	CALLOUT_INITIALIZER	{ NULL, NULL }
203130365Smlaier#endif
204130365Smlaier#if !defined(__FreeBSD__)
205130365Smlaiertypedef void (timeout_t)(void *);
206130365Smlaier#endif
207130365Smlaier
208130365Smlaier#define	m_pktlen(m)		((m)->m_pkthdr.len)
209130365Smlaier
210130365Smlaierstruct ifnet; struct mbuf;
211130365Smlaierstruct pf_altq;
212130365Smlaier#ifdef ALTQ3_CLFIER_COMPAT
213130365Smlaierstruct flowinfo;
214130365Smlaier#endif
215130365Smlaier
216130365Smlaiervoid	*altq_lookup(char *, int);
217130365Smlaier#ifdef ALTQ3_CLFIER_COMPAT
218130365Smlaierint	altq_extractflow(struct mbuf *, int, struct flowinfo *, u_int32_t);
219130365Smlaierint	acc_add_filter(struct acc_classifier *, struct flow_filter *,
220130365Smlaier	    void *, u_long *);
221130365Smlaierint	acc_delete_filter(struct acc_classifier *, u_long);
222130365Smlaierint	acc_discard_filters(struct acc_classifier *, void *, int);
223130365Smlaiervoid	*acc_classify(void *, struct mbuf *, int);
224130365Smlaier#endif
225130365Smlaieru_int8_t read_dsfield(struct mbuf *, struct altq_pktattr *);
226130365Smlaiervoid	write_dsfield(struct mbuf *, struct altq_pktattr *, u_int8_t);
227130365Smlaiervoid	altq_assert(const char *, int, const char *);
228130365Smlaierint	tbr_set(struct ifaltq *, struct tb_profile *);
229130365Smlaierint	tbr_get(struct ifaltq *, struct tb_profile *);
230130365Smlaier
231130365Smlaierint	altq_pfattach(struct pf_altq *);
232130365Smlaierint	altq_pfdetach(struct pf_altq *);
233130365Smlaierint	altq_add(struct pf_altq *);
234130365Smlaierint	altq_remove(struct pf_altq *);
235130365Smlaierint	altq_add_queue(struct pf_altq *);
236130365Smlaierint	altq_remove_queue(struct pf_altq *);
237130365Smlaierint	altq_getqstats(struct pf_altq *, void *, int *);
238130365Smlaier
239130365Smlaierint	cbq_pfattach(struct pf_altq *);
240130365Smlaierint	cbq_add_altq(struct pf_altq *);
241130365Smlaierint	cbq_remove_altq(struct pf_altq *);
242130365Smlaierint	cbq_add_queue(struct pf_altq *);
243130365Smlaierint	cbq_remove_queue(struct pf_altq *);
244130365Smlaierint	cbq_getqstats(struct pf_altq *, void *, int *);
245130365Smlaier
246298133Sloosint	codel_pfattach(struct pf_altq *);
247298133Sloosint	codel_add_altq(struct pf_altq *);
248298133Sloosint	codel_remove_altq(struct pf_altq *);
249298133Sloosint	codel_getqstats(struct pf_altq *, void *, int *);
250298133Sloos
251130365Smlaierint	priq_pfattach(struct pf_altq *);
252130365Smlaierint	priq_add_altq(struct pf_altq *);
253130365Smlaierint	priq_remove_altq(struct pf_altq *);
254130365Smlaierint	priq_add_queue(struct pf_altq *);
255130365Smlaierint	priq_remove_queue(struct pf_altq *);
256130365Smlaierint	priq_getqstats(struct pf_altq *, void *, int *);
257130365Smlaier
258130365Smlaierint	hfsc_pfattach(struct pf_altq *);
259130365Smlaierint	hfsc_add_altq(struct pf_altq *);
260130365Smlaierint	hfsc_remove_altq(struct pf_altq *);
261130365Smlaierint	hfsc_add_queue(struct pf_altq *);
262130365Smlaierint	hfsc_remove_queue(struct pf_altq *);
263130365Smlaierint	hfsc_getqstats(struct pf_altq *, void *, int *);
264130365Smlaier
265298091Sloosint	fairq_pfattach(struct pf_altq *);
266298091Sloosint	fairq_add_altq(struct pf_altq *);
267298091Sloosint	fairq_remove_altq(struct pf_altq *);
268298091Sloosint	fairq_add_queue(struct pf_altq *);
269298091Sloosint	fairq_remove_queue(struct pf_altq *);
270298091Sloosint	fairq_getqstats(struct pf_altq *, void *, int *);
271298091Sloos
272130365Smlaier#endif /* _KERNEL */
273130365Smlaier#endif /* _ALTQ_ALTQ_VAR_H_ */
274