1 /*        $NetBSD: altq_var.h,v 1.13 2024/12/07 07:45:43 andvar Exp $ */
2 /*        $KAME: altq_var.h,v 1.18 2005/04/13 03:44:25 suz Exp $      */
3 
4 /*
5  * Copyright (C) 1998-2003
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 #ifdef ALTQ3_CLFIER_COMPAT
39 /*
40  * filter structure for altq common classifier
41  */
42 struct acc_filter {
43           LIST_ENTRY(acc_filter)        f_chain;
44           void                          *f_class; /* pointer to the class */
45           u_long                        f_handle; /* filter id */
46           u_int32_t           f_fbmask; /* filter bitmask */
47           struct flow_filter  f_filter; /* filter value */
48 };
49 
50 /*
51  * XXX ACC_FILTER_TABLESIZE can't be larger than 2048 unless we fix
52  * the handle assignment.
53  */
54 #define   ACC_FILTER_TABLESIZE          (256+1)
55 #define   ACC_FILTER_MASK               (ACC_FILTER_TABLESIZE - 2)
56 #define   ACC_WILDCARD_INDEX  (ACC_FILTER_TABLESIZE - 1)
57 #ifdef __GNUC__
58 #define   ACC_GET_HASH_INDEX(addr) \
59           ({int x = (addr) + ((addr) >> 16); (x + (x >> 8)) & ACC_FILTER_MASK;})
60 #else
61 #define   ACC_GET_HASH_INDEX(addr) \
62           (((addr) + ((addr) >> 8) + ((addr) >> 16) + ((addr) >> 24)) \
63           & ACC_FILTER_MASK)
64 #endif
65 #define   ACC_GET_HINDEX(handle) ((handle) >> 20)
66 
67 #if (__FreeBSD_version > 500000)
68 #define ACC_LOCK_INIT(ac)     mtx_init(&(ac)->acc_mtx, "classifier", MTX_DEF)
69 #define ACC_LOCK_DESTROY(ac)  mtx_destroy(&(ac)->acc_mtx)
70 #define ACC_LOCK(ac)                    mtx_lock(&(ac)->acc_mtx)
71 #define ACC_UNLOCK(ac)                  mtx_unlock(&(ac)->acc_mtx)
72 #else
73 #define ACC_LOCK_INIT(ac)
74 #define ACC_LOCK_DESTROY(ac)
75 #define ACC_LOCK(ac)
76 #define ACC_UNLOCK(ac)
77 #endif
78 
79 struct acc_classifier {
80           u_int32_t                     acc_fbmask;
81           LIST_HEAD(filt, acc_filter)   acc_filters[ACC_FILTER_TABLESIZE];
82 
83 #if (__FreeBSD_version > 500000)
84           struct    mtx acc_mtx;
85 #endif
86 };
87 
88 /*
89  * flowinfo mask bits used by classifier
90  */
91 /* for ipv4 */
92 #define   FIMB4_PROTO         0x0001
93 #define   FIMB4_TOS 0x0002
94 #define   FIMB4_DADDR         0x0004
95 #define   FIMB4_SADDR         0x0008
96 #define   FIMB4_DPORT         0x0010
97 #define   FIMB4_SPORT         0x0020
98 #define   FIMB4_GPI 0x0040
99 #define   FIMB4_ALL 0x007f
100 /* for ipv6 */
101 #define   FIMB6_PROTO         0x0100
102 #define   FIMB6_TCLASS        0x0200
103 #define   FIMB6_DADDR         0x0400
104 #define   FIMB6_SADDR         0x0800
105 #define   FIMB6_DPORT         0x1000
106 #define   FIMB6_SPORT         0x2000
107 #define   FIMB6_GPI 0x4000
108 #define   FIMB6_FLABEL        0x8000
109 #define   FIMB6_ALL 0xff00
110 
111 #define   FIMB_ALL  (FIMB4_ALL|FIMB6_ALL)
112 
113 #define   FIMB4_PORTS         (FIMB4_DPORT|FIMB4_SPORT|FIMB4_GPI)
114 #define   FIMB6_PORTS         (FIMB6_DPORT|FIMB6_SPORT|FIMB6_GPI)
115 #endif /* ALTQ3_CLFIER_COMPAT */
116 
117 /*
118  * machine dependent clock
119  * a 64bit high resolution time counter.
120  */
121 extern u_int32_t machclk_freq;
122 extern u_int32_t machclk_per_tick;
123 extern void init_machclk(void);
124 extern 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 typedef u_long ioctlcmd_t;
144 
145 /*
146  * queue macros:
147  * the interface of TAILQ_LAST macro changed after the introduction
148  * of softupdate. redefine it here to make it work with pre-2.2.7.
149  */
150 #undef TAILQ_LAST
151 #define   TAILQ_LAST(head, headname) \
152           (*(((struct headname *)((head)->tqh_last))->tqh_last))
153 
154 #ifndef TAILQ_EMPTY
155 #define   TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
156 #endif
157 #ifndef TAILQ_FOREACH
158 #define TAILQ_FOREACH(var, head, field)                                         \
159           for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
160 #endif
161 
162 /* macro for timeout/untimeout */
163 #if (__FreeBSD_version > 300000) || defined(__NetBSD__)
164 /* use callout */
165 #include <sys/callout.h>
166 
167 #if (__FreeBSD_version > 500000) || defined(__NetBSD__)
168 #define   CALLOUT_INIT(c)               callout_init((c), 0)
169 #else
170 #define   CALLOUT_INIT(c)               callout_init((c))
171 #endif
172 #define   CALLOUT_RESET(c,t,f,a)        callout_reset((c),(t),(f),(a))
173 #define   CALLOUT_STOP(c)               callout_stop((c))
174 #ifndef CALLOUT_INITIALIZER
175 #define   CALLOUT_INITIALIZER { { { NULL } }, 0, NULL, NULL, 0 }
176 #endif
177 #elif defined(__OpenBSD__)
178 #include <sys/timeout.h>
179 /* callout structure as a wrapper of struct timeout */
180 struct callout {
181           struct timeout      c_to;
182 };
183 #define   CALLOUT_INIT(c)               do { (void)memset((c), 0, sizeof(*(c))); } while (/*CONSTCOND*/ 0)
184 #define   CALLOUT_RESET(c,t,f,a)        do { if (!timeout_initialized(&(c)->c_to))  \
185                                                    timeout_set(&(c)->c_to, (f), (a)); \
186                                              timeout_add(&(c)->c_to, (t)); } while (/*CONSTCOND*/ 0)
187 #define   CALLOUT_STOP(c)               timeout_del(&(c)->c_to)
188 #define   CALLOUT_INITIALIZER { { { NULL }, NULL, NULL, 0, 0 } }
189 #else
190 /* use old-style timeout/untimeout */
191 /* dummy callout structure */
192 struct callout {
193           void                *c_arg;                       /* function argument */
194           void                (*c_func)(void *);  /* function to call */
195 };
196 #define   CALLOUT_INIT(c)               do { (void)memset((c), 0, sizeof(*(c))); } while (/*CONSTCOND*/ 0)
197 #define   CALLOUT_RESET(c,t,f,a)        do {      (c)->c_arg = (a);   \
198                                                   (c)->c_func = (f);  \
199                                                   timeout((f),(a),(t)); } while (/*CONSTCOND*/ 0)
200 #define   CALLOUT_STOP(c)               untimeout((c)->c_func,(c)->c_arg)
201 #define   CALLOUT_INITIALIZER { NULL, NULL }
202 #endif
203 #if !defined(__FreeBSD__)
204 typedef void (timeout_t)(void *);
205 #endif
206 
207 #define   m_pktlen(m)                   ((m)->m_pkthdr.len)
208 
209 struct ifnet; struct mbuf;
210 struct pf_altq;
211 #ifdef ALTQ3_CLFIER_COMPAT
212 struct flowinfo;
213 #endif
214 
215 void      *altq_lookup(char *, int);
216 #ifdef ALTQ3_CLFIER_COMPAT
217 int       altq_extractflow(struct mbuf *, int, struct flowinfo *, u_int32_t);
218 int       acc_add_filter(struct acc_classifier *, struct flow_filter *,
219               void *, u_long *);
220 int       acc_delete_filter(struct acc_classifier *, u_long);
221 int       acc_discard_filters(struct acc_classifier *, void *, int);
222 void      *acc_classify(void *, struct mbuf *, int);
223 #endif
224 u_int8_t read_dsfield(struct mbuf *, struct altq_pktattr *);
225 void      write_dsfield(struct mbuf *, struct altq_pktattr *, u_int8_t);
226 void      altq_assert(const char *, int, const char *);
227 int       tbr_set(struct ifaltq *, struct tb_profile *);
228 int       tbr_get(struct ifaltq *, struct tb_profile *);
229 
230 int       altq_pfattach(struct pf_altq *);
231 int       altq_pfdetach(struct pf_altq *);
232 int       altq_add(struct pf_altq *);
233 int       altq_remove(struct pf_altq *);
234 int       altq_add_queue(struct pf_altq *);
235 int       altq_remove_queue(struct pf_altq *);
236 int       altq_getqstats(struct pf_altq *, void *, int *);
237 
238 int       cbq_pfattach(struct pf_altq *);
239 int       cbq_add_altq(struct pf_altq *);
240 int       cbq_remove_altq(struct pf_altq *);
241 int       cbq_add_queue(struct pf_altq *);
242 int       cbq_remove_queue(struct pf_altq *);
243 int       cbq_getqstats(struct pf_altq *, void *, int *);
244 
245 int       priq_pfattach(struct pf_altq *);
246 int       priq_add_altq(struct pf_altq *);
247 int       priq_remove_altq(struct pf_altq *);
248 int       priq_add_queue(struct pf_altq *);
249 int       priq_remove_queue(struct pf_altq *);
250 int       priq_getqstats(struct pf_altq *, void *, int *);
251 
252 int       hfsc_pfattach(struct pf_altq *);
253 int       hfsc_add_altq(struct pf_altq *);
254 int       hfsc_remove_altq(struct pf_altq *);
255 int       hfsc_add_queue(struct pf_altq *);
256 int       hfsc_remove_queue(struct pf_altq *);
257 int       hfsc_getqstats(struct pf_altq *, void *, int *);
258 
259 #endif /* _KERNEL */
260 #endif /* _ALTQ_ALTQ_VAR_H_ */
261