xref: /netbsd/sys/altq/altq_var.h (revision bf9ec67e)
1 /*	$NetBSD: altq_var.h,v 1.2 2000/12/14 08:49:51 thorpej Exp $	*/
2 /*	$KAME: altq_var.h,v 1.7 2000/12/14 08:12:46 thorpej Exp $	*/
3 
4 /*
5  * Copyright (C) 1998-2000
6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #ifndef _ALTQ_ALTQ_VAR_H_
30 #define	_ALTQ_ALTQ_VAR_H_
31 
32 #ifdef _KERNEL
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/queue.h>
37 
38 /*
39  * filter structure for altq common classifier
40  */
41 struct acc_filter {
42 	LIST_ENTRY(acc_filter)	f_chain;
43 	void			*f_class;	/* pointer to the class */
44 	u_long			f_handle;	/* filter id */
45 	u_int32_t		f_fbmask;	/* filter bitmask */
46 	struct flow_filter	f_filter;	/* filter value */
47 };
48 
49 /*
50  * XXX ACC_FILTER_TABLESIZE can't be larger than 2048 unless we fix
51  * the handle assignment.
52  */
53 #define	ACC_FILTER_TABLESIZE	(256+1)
54 #define	ACC_FILTER_MASK		(ACC_FILTER_TABLESIZE - 2)
55 #define	ACC_WILDCARD_INDEX	(ACC_FILTER_TABLESIZE - 1)
56 #ifdef __GNUC__
57 #define	ACC_GET_HASH_INDEX(addr) \
58 	({int x = (addr) + ((addr) >> 16); (x + (x >> 8)) & ACC_FILTER_MASK;})
59 #else
60 #define	ACC_GET_HASH_INDEX(addr) \
61 	(((addr) + ((addr) >> 8) + ((addr) >> 16) + ((addr) >> 24)) \
62 	& ACC_FILTER_MASK)
63 #endif
64 #define	ACC_GET_HINDEX(handle) ((handle) >> 20)
65 
66 struct acc_classifier {
67 	u_int32_t			acc_fbmask;
68 	LIST_HEAD(filt, acc_filter)	acc_filters[ACC_FILTER_TABLESIZE];
69 };
70 
71 /*
72  * flowinfo mask bits used by classifier
73  */
74 /* for ipv4 */
75 #define	FIMB4_PROTO	0x0001
76 #define	FIMB4_TOS	0x0002
77 #define	FIMB4_DADDR	0x0004
78 #define	FIMB4_SADDR	0x0008
79 #define	FIMB4_DPORT	0x0010
80 #define	FIMB4_SPORT	0x0020
81 #define	FIMB4_GPI	0x0040
82 #define	FIMB4_ALL	0x007f
83 /* for ipv6 */
84 #define	FIMB6_PROTO	0x0100
85 #define	FIMB6_TCLASS	0x0200
86 #define	FIMB6_DADDR	0x0400
87 #define	FIMB6_SADDR	0x0800
88 #define	FIMB6_DPORT	0x1000
89 #define	FIMB6_SPORT	0x2000
90 #define	FIMB6_GPI	0x4000
91 #define	FIMB6_FLABEL	0x8000
92 #define	FIMB6_ALL	0xff00
93 
94 #define	FIMB_ALL	(FIMB4_ALL|FIMB6_ALL)
95 
96 #define	FIMB4_PORTS	(FIMB4_DPORT|FIMB4_SPORT|FIMB4_GPI)
97 #define	FIMB6_PORTS	(FIMB6_DPORT|FIMB6_SPORT|FIMB6_GPI)
98 
99 /*
100  * machine dependent clock
101  * a 64bit high resolution time counter.
102  */
103 extern u_int32_t machclk_freq;
104 extern u_int32_t machclk_per_tick;
105 extern void init_machclk(void);
106 
107 #if defined(__i386__) && !defined(ALTQ_NOPCC)
108 /* for pentium tsc */
109 #include <machine/cpufunc.h>
110 
111 #define	read_machclk()		rdtsc()
112 #ifdef __OpenBSD__
113 static __inline u_int64_t
114 rdtsc(void)
115 {
116 	u_int64_t rv;
117 	__asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
118 	return (rv);
119 }
120 #endif /* __OpenBSD__ */
121 
122 #elif defined(__alpha__) && !defined(ALTQ_NOPCC)
123 /* for alpha rpcc */
124 extern u_int64_t read_machclk(void);
125 
126 #else /* !i386 && !alpha */
127 /* emulate 256MHz using microtime() */
128 #define	MACHCLK_SHIFT	8
129 static __inline u_int64_t
130 read_machclk(void)
131 {
132 	struct timeval tv;
133 	microtime(&tv);
134 	return (((u_int64_t)(tv.tv_sec - boottime.tv_sec) * 1000000
135 		 + tv.tv_usec) << MACHCLK_SHIFT);
136 }
137 #endif /* !i386 && !alpha */
138 
139 /*
140  * debug support
141  */
142 #ifdef ALTQ_DEBUG
143 #ifdef __STDC__
144 #define	ASSERT(e)	((e) ? (void)0 : altq_assert(__FILE__, __LINE__, #e))
145 #else	/* PCC */
146 #define	ASSERT(e)	((e) ? (void)0 : altq_assert(__FILE__, __LINE__, "e"))
147 #endif
148 #else
149 #define	ASSERT(e)	((void)0)
150 #endif
151 
152 /*
153  * misc stuff for compatibility
154  */
155 /* ioctl cmd type */
156 #if defined(__FreeBSD__) && (__FreeBSD__ < 3)
157 typedef int ioctlcmd_t;
158 #else
159 typedef u_long ioctlcmd_t;
160 #endif
161 
162 /*
163  * queue macros:
164  * the interface of TAILQ_LAST macro changed after the introduction
165  * of softupdate. redefine it here to make it work with pre-2.2.7.
166  */
167 #undef TAILQ_LAST
168 #define	TAILQ_LAST(head, headname) \
169 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
170 
171 #ifndef TAILQ_EMPTY
172 #define	TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
173 #endif
174 #ifndef TAILQ_FOREACH
175 #define TAILQ_FOREACH(var, head, field)					\
176 	for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
177 #endif
178 
179 /* macro for timeout/untimeout */
180 #if (__FreeBSD_version > 300000) || defined(__NetBSD__)
181 /* use callout */
182 #include <sys/callout.h>
183 
184 #define	CALLOUT_INIT(c)		callout_init((c))
185 #define	CALLOUT_RESET(c,t,f,a)	callout_reset((c),(t),(f),(a))
186 #define	CALLOUT_STOP(c)		callout_stop((c))
187 #ifndef CALLOUT_INITIALIZER
188 #define	CALLOUT_INITIALIZER	{ { { NULL } }, 0, NULL, NULL, 0 }
189 #endif
190 #else
191 /* use old-style timeout/untimeout */
192 /* dummy callout structure */
193 struct callout {
194 	void		*c_arg;			/* function argument */
195 	void		(*c_func) __P((void *));/* functiuon to call */
196 };
197 #define	CALLOUT_INIT(c)		do { bzero((c), sizeof(*(c))); } while (0)
198 #define	CALLOUT_RESET(c,t,f,a)	do {	(c)->c_arg = (a);	\
199 					(c)->c_func = (f);	\
200 					timeout((f),(a),(t)); } while (0)
201 #define	CALLOUT_STOP(c)		untimeout((c)->c_func,(c)->c_arg)
202 #define	CALLOUT_INITIALIZER	{ NULL, NULL }
203 #endif
204 #if !defined(__FreeBSD__)
205 typedef void (timeout_t)(void *);
206 #endif
207 
208 #define	m_pktlen(m)		((m)->m_pkthdr.len)
209 
210 struct ifnet; struct mbuf; struct flowinfo;
211 
212 void *altq_lookup __P((char *, int));
213 int altq_extractflow __P((struct mbuf *, int, struct flowinfo *, u_int32_t));
214 int acc_add_filter __P((struct acc_classifier *, struct flow_filter *,
215 			   void *, u_long *));
216 int acc_delete_filter __P((struct acc_classifier *, u_long));
217 int acc_discard_filters __P((struct acc_classifier *, void *, int));
218 void *acc_classify __P((void *, struct mbuf *, int));
219 u_int8_t read_dsfield __P((struct mbuf *, struct altq_pktattr *));
220 void write_dsfield __P((struct mbuf *, struct altq_pktattr *, u_int8_t));
221 void altq_assert __P((const char *, int, const char *));
222 int tbr_set __P((struct ifaltq *, struct tb_profile *));
223 int tbr_get __P((struct ifaltq *, struct tb_profile *));
224 
225 #endif /* _KERNEL */
226 #endif /* _ALTQ_ALTQ_VAR_H_ */
227