altq_var.h revision 130366
1162852Sdes/*	$KAME: altq_var.h,v 1.16 2003/10/03 05:05:15 kjc Exp $	*/
2162852Sdes
3162852Sdes/*
4162852Sdes * Copyright (C) 1998-2003
5162852Sdes *	Sony Computer Science Laboratories Inc.  All rights reserved.
6162852Sdes *
7162852Sdes * Redistribution and use in source and binary forms, with or without
8162852Sdes * modification, are permitted provided that the following conditions
9162852Sdes * are met:
10162852Sdes * 1. Redistributions of source code must retain the above copyright
11162852Sdes *    notice, this list of conditions and the following disclaimer.
12162852Sdes * 2. Redistributions in binary form must reproduce the above copyright
13162852Sdes *    notice, this list of conditions and the following disclaimer in the
14162852Sdes *    documentation and/or other materials provided with the distribution.
15162852Sdes *
16162852Sdes * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17162852Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18162852Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19162852Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20162852Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21162852Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22180744Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23162852Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24162852Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25162852Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26180740Sdes * SUCH DAMAGE.
27180740Sdes */
28180740Sdes#ifndef _ALTQ_ALTQ_VAR_H_
29162852Sdes#define	_ALTQ_ALTQ_VAR_H_
30162852Sdes
31162852Sdes#ifdef _KERNEL
32162852Sdes
33162852Sdes#include <sys/param.h>
34162852Sdes#include <sys/kernel.h>
35162852Sdes#include <sys/queue.h>
36162852Sdes
37162852Sdes#ifdef ALTQ3_CLFIER_COMPAT
38162852Sdes/*
39162852Sdes * filter structure for altq common classifier
40162852Sdes */
41162852Sdesstruct acc_filter {
42162852Sdes	LIST_ENTRY(acc_filter)	f_chain;
43162852Sdes	void			*f_class;	/* pointer to the class */
44162852Sdes	u_long			f_handle;	/* filter id */
45162852Sdes	u_int32_t		f_fbmask;	/* filter bitmask */
46162852Sdes	struct flow_filter	f_filter;	/* filter value */
47162852Sdes};
48162852Sdes
49162852Sdes/*
50162852Sdes * XXX ACC_FILTER_TABLESIZE can't be larger than 2048 unless we fix
51162852Sdes * the handle assignment.
52162852Sdes */
53162852Sdes#define	ACC_FILTER_TABLESIZE	(256+1)
54162852Sdes#define	ACC_FILTER_MASK		(ACC_FILTER_TABLESIZE - 2)
55162852Sdes#define	ACC_WILDCARD_INDEX	(ACC_FILTER_TABLESIZE - 1)
56162852Sdes#ifdef __GNUC__
57162852Sdes#define	ACC_GET_HASH_INDEX(addr) \
58162852Sdes	({int x = (addr) + ((addr) >> 16); (x + (x >> 8)) & ACC_FILTER_MASK;})
59180744Sdes#else
60162852Sdes#define	ACC_GET_HASH_INDEX(addr) \
61162852Sdes	(((addr) + ((addr) >> 8) + ((addr) >> 16) + ((addr) >> 24)) \
62162852Sdes	& ACC_FILTER_MASK)
63162852Sdes#endif
64162852Sdes#define	ACC_GET_HINDEX(handle) ((handle) >> 20)
65162852Sdes
66162852Sdes#if (__FreeBSD_version > 500000)
67162852Sdes#define ACC_LOCK_INIT(ac)	mtx_init(&(ac)->acc_mtx, "classifier", MTX_DEF)
68162852Sdes#define ACC_LOCK_DESTROY(ac)	mtx_destroy(&(ac)->acc_mtx)
69162852Sdes#define ACC_LOCK(ac)		mtx_lock(&(ac)->acc_mtx)
70162852Sdes#define ACC_UNLOCK(ac)		mtx_unlock(&(ac)->acc_mtx)
71162852Sdes#else
72162852Sdes#define ACC_LOCK_INIT(ac)
73162852Sdes#define ACC_LOCK_DESTROY(ac)
74162852Sdes#define ACC_LOCK(ac)
75162852Sdes#define ACC_UNLOCK(ac)
76162852Sdes#endif
77162852Sdes
78162852Sdesstruct acc_classifier {
79162852Sdes	u_int32_t			acc_fbmask;
80162852Sdes	LIST_HEAD(filt, acc_filter)	acc_filters[ACC_FILTER_TABLESIZE];
81162852Sdes
82162852Sdes#if (__FreeBSD_version > 500000)
83162852Sdes	struct	mtx acc_mtx;
84162852Sdes#endif
85162852Sdes};
86162852Sdes
87162852Sdes/*
88162852Sdes * flowinfo mask bits used by classifier
89162852Sdes */
90162852Sdes/* for ipv4 */
91#define	FIMB4_PROTO	0x0001
92#define	FIMB4_TOS	0x0002
93#define	FIMB4_DADDR	0x0004
94#define	FIMB4_SADDR	0x0008
95#define	FIMB4_DPORT	0x0010
96#define	FIMB4_SPORT	0x0020
97#define	FIMB4_GPI	0x0040
98#define	FIMB4_ALL	0x007f
99/* for ipv6 */
100#define	FIMB6_PROTO	0x0100
101#define	FIMB6_TCLASS	0x0200
102#define	FIMB6_DADDR	0x0400
103#define	FIMB6_SADDR	0x0800
104#define	FIMB6_DPORT	0x1000
105#define	FIMB6_SPORT	0x2000
106#define	FIMB6_GPI	0x4000
107#define	FIMB6_FLABEL	0x8000
108#define	FIMB6_ALL	0xff00
109
110#define	FIMB_ALL	(FIMB4_ALL|FIMB6_ALL)
111
112#define	FIMB4_PORTS	(FIMB4_DPORT|FIMB4_SPORT|FIMB4_GPI)
113#define	FIMB6_PORTS	(FIMB6_DPORT|FIMB6_SPORT|FIMB6_GPI)
114#endif /* ALTQ3_CLFIER_COMPAT */
115
116/*
117 * machine dependent clock
118 * a 64bit high resolution time counter.
119 */
120extern int machclk_usepcc;
121extern u_int32_t machclk_freq;
122extern u_int32_t machclk_per_tick;
123extern void init_machclk(void);
124extern u_int64_t read_machclk(void);
125
126/*
127 * debug support
128 */
129#ifdef ALTQ_DEBUG
130#ifdef __STDC__
131#define	ASSERT(e)	((e) ? (void)0 : altq_assert(__FILE__, __LINE__, #e))
132#else	/* PCC */
133#define	ASSERT(e)	((e) ? (void)0 : altq_assert(__FILE__, __LINE__, "e"))
134#endif
135#else
136#define	ASSERT(e)	((void)0)
137#endif
138
139/*
140 * misc stuff for compatibility
141 */
142/* ioctl cmd type */
143#if defined(__FreeBSD__) && (__FreeBSD__ < 3)
144typedef int ioctlcmd_t;
145#else
146typedef u_long ioctlcmd_t;
147#endif
148
149/*
150 * queue macros:
151 * the interface of TAILQ_LAST macro changed after the introduction
152 * of softupdate. redefine it here to make it work with pre-2.2.7.
153 */
154#undef TAILQ_LAST
155#define	TAILQ_LAST(head, headname) \
156	(*(((struct headname *)((head)->tqh_last))->tqh_last))
157
158#ifndef TAILQ_EMPTY
159#define	TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
160#endif
161#ifndef TAILQ_FOREACH
162#define TAILQ_FOREACH(var, head, field)					\
163	for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
164#endif
165
166/* macro for timeout/untimeout */
167#if (__FreeBSD_version > 300000) || defined(__NetBSD__)
168/* use callout */
169#include <sys/callout.h>
170
171#if (__FreeBSD_version > 500000)
172#define	CALLOUT_INIT(c)		callout_init((c), 0)
173#else
174#define	CALLOUT_INIT(c)		callout_init((c))
175#endif
176#define	CALLOUT_RESET(c,t,f,a)	callout_reset((c),(t),(f),(a))
177#define	CALLOUT_STOP(c)		callout_stop((c))
178#ifndef CALLOUT_INITIALIZER
179#define	CALLOUT_INITIALIZER	{ { { NULL } }, 0, NULL, NULL, 0 }
180#endif
181#elif defined(__OpenBSD__)
182#include <sys/timeout.h>
183/* callout structure as a wrapper of struct timeout */
184struct callout {
185	struct timeout	c_to;
186};
187#define	CALLOUT_INIT(c)		do { bzero((c), sizeof(*(c))); } while (/*CONSTCOND*/ 0)
188#define	CALLOUT_RESET(c,t,f,a)	do { if (!timeout_initialized(&(c)->c_to))  \
189					 timeout_set(&(c)->c_to, (f), (a)); \
190				     timeout_add(&(c)->c_to, (t)); } while (/*CONSTCOND*/ 0)
191#define	CALLOUT_STOP(c)		timeout_del(&(c)->c_to)
192#define	CALLOUT_INITIALIZER	{ { { NULL }, NULL, NULL, 0, 0 } }
193#else
194/* use old-style timeout/untimeout */
195/* dummy callout structure */
196struct callout {
197	void		*c_arg;			/* function argument */
198	void		(*c_func)(void *);	/* functiuon to call */
199};
200#define	CALLOUT_INIT(c)		do { bzero((c), sizeof(*(c))); } while (/*CONSTCOND*/ 0)
201#define	CALLOUT_RESET(c,t,f,a)	do {	(c)->c_arg = (a);	\
202					(c)->c_func = (f);	\
203					timeout((f),(a),(t)); } while (/*CONSTCOND*/ 0)
204#define	CALLOUT_STOP(c)		untimeout((c)->c_func,(c)->c_arg)
205#define	CALLOUT_INITIALIZER	{ NULL, NULL }
206#endif
207#if !defined(__FreeBSD__)
208typedef void (timeout_t)(void *);
209#endif
210
211#define	m_pktlen(m)		((m)->m_pkthdr.len)
212
213struct ifnet; struct mbuf;
214struct pf_altq;
215#ifdef ALTQ3_CLFIER_COMPAT
216struct flowinfo;
217#endif
218
219void	*altq_lookup(char *, int);
220#ifdef ALTQ3_CLFIER_COMPAT
221int	altq_extractflow(struct mbuf *, int, struct flowinfo *, u_int32_t);
222int	acc_add_filter(struct acc_classifier *, struct flow_filter *,
223	    void *, u_long *);
224int	acc_delete_filter(struct acc_classifier *, u_long);
225int	acc_discard_filters(struct acc_classifier *, void *, int);
226void	*acc_classify(void *, struct mbuf *, int);
227#endif
228u_int8_t read_dsfield(struct mbuf *, struct altq_pktattr *);
229void	write_dsfield(struct mbuf *, struct altq_pktattr *, u_int8_t);
230void	altq_assert(const char *, int, const char *);
231int	tbr_set(struct ifaltq *, struct tb_profile *);
232int	tbr_get(struct ifaltq *, struct tb_profile *);
233
234int	altq_pfattach(struct pf_altq *);
235int	altq_pfdetach(struct pf_altq *);
236int	altq_add(struct pf_altq *);
237int	altq_remove(struct pf_altq *);
238int	altq_add_queue(struct pf_altq *);
239int	altq_remove_queue(struct pf_altq *);
240int	altq_getqstats(struct pf_altq *, void *, int *);
241
242int	cbq_pfattach(struct pf_altq *);
243int	cbq_add_altq(struct pf_altq *);
244int	cbq_remove_altq(struct pf_altq *);
245int	cbq_add_queue(struct pf_altq *);
246int	cbq_remove_queue(struct pf_altq *);
247int	cbq_getqstats(struct pf_altq *, void *, int *);
248
249int	priq_pfattach(struct pf_altq *);
250int	priq_add_altq(struct pf_altq *);
251int	priq_remove_altq(struct pf_altq *);
252int	priq_add_queue(struct pf_altq *);
253int	priq_remove_queue(struct pf_altq *);
254int	priq_getqstats(struct pf_altq *, void *, int *);
255
256int	hfsc_pfattach(struct pf_altq *);
257int	hfsc_add_altq(struct pf_altq *);
258int	hfsc_remove_altq(struct pf_altq *);
259int	hfsc_add_queue(struct pf_altq *);
260int	hfsc_remove_queue(struct pf_altq *);
261int	hfsc_getqstats(struct pf_altq *, void *, int *);
262
263#endif /* _KERNEL */
264#endif /* _ALTQ_ALTQ_VAR_H_ */
265