altq_wfq.c revision 1.2.2.3 1 1.2.2.3 bouyer /* $NetBSD: altq_wfq.c,v 1.2.2.3 2001/04/21 17:46:12 bouyer Exp $ */
2 1.2.2.2 bouyer /* $KAME: altq_wfq.c,v 1.7 2000/12/14 08:12:46 thorpej Exp $ */
3 1.2.2.2 bouyer
4 1.2.2.2 bouyer /*
5 1.2.2.2 bouyer * Copyright (C) 1997-2000
6 1.2.2.2 bouyer * Sony Computer Science Laboratories Inc. All rights reserved.
7 1.2.2.2 bouyer *
8 1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
9 1.2.2.2 bouyer * modification, are permitted provided that the following conditions
10 1.2.2.2 bouyer * are met:
11 1.2.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
12 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer.
13 1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.2.2.2 bouyer * documentation and/or other materials provided with the distribution.
16 1.2.2.2 bouyer *
17 1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 1.2.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.2.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.2.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 1.2.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.2.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.2.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.2.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.2.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.2.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.2.2.2 bouyer * SUCH DAMAGE.
28 1.2.2.2 bouyer */
29 1.2.2.2 bouyer /*
30 1.2.2.2 bouyer * March 27, 1997. Written by Hiroshi Kyusojin of Keio University
31 1.2.2.2 bouyer * (kyu (at) mt.cs.keio.ac.jp).
32 1.2.2.2 bouyer */
33 1.2.2.2 bouyer
34 1.2.2.2 bouyer #if defined(__FreeBSD__) || defined(__NetBSD__)
35 1.2.2.2 bouyer #include "opt_altq.h"
36 1.2.2.2 bouyer #if (__FreeBSD__ != 2)
37 1.2.2.2 bouyer #include "opt_inet.h"
38 1.2.2.2 bouyer #ifdef __FreeBSD__
39 1.2.2.2 bouyer #include "opt_inet6.h"
40 1.2.2.2 bouyer #endif
41 1.2.2.2 bouyer #endif
42 1.2.2.2 bouyer #endif /* __FreeBSD__ || __NetBSD__ */
43 1.2.2.2 bouyer #ifdef ALTQ_WFQ
44 1.2.2.2 bouyer
45 1.2.2.2 bouyer #include <sys/types.h>
46 1.2.2.2 bouyer #include <sys/param.h>
47 1.2.2.2 bouyer #include <sys/malloc.h>
48 1.2.2.2 bouyer #include <sys/mbuf.h>
49 1.2.2.2 bouyer #include <sys/uio.h>
50 1.2.2.2 bouyer #include <sys/socket.h>
51 1.2.2.2 bouyer #include <sys/systm.h>
52 1.2.2.2 bouyer #include <sys/proc.h>
53 1.2.2.2 bouyer #include <sys/errno.h>
54 1.2.2.2 bouyer #include <sys/time.h>
55 1.2.2.2 bouyer #include <sys/kernel.h>
56 1.2.2.2 bouyer
57 1.2.2.2 bouyer #include <net/if.h>
58 1.2.2.2 bouyer #include <net/if_types.h>
59 1.2.2.2 bouyer #include <netinet/in.h>
60 1.2.2.2 bouyer
61 1.2.2.2 bouyer #include <altq/altq.h>
62 1.2.2.2 bouyer #include <altq/altq_conf.h>
63 1.2.2.2 bouyer #include <altq/altq_wfq.h>
64 1.2.2.2 bouyer
65 1.2.2.2 bouyer /*
66 1.2.2.2 bouyer #define WFQ_DEBUG
67 1.2.2.2 bouyer */
68 1.2.2.2 bouyer
69 1.2.2.2 bouyer static int wfq_setenable(struct wfq_interface *, int);
70 1.2.2.2 bouyer static int wfq_ifattach(struct wfq_interface *);
71 1.2.2.2 bouyer static int wfq_ifdetach(struct wfq_interface *);
72 1.2.2.2 bouyer static int wfq_ifenqueue(struct ifaltq *, struct mbuf *,
73 1.2.2.2 bouyer struct altq_pktattr *);
74 1.2.2.2 bouyer static u_long wfq_hash(struct flowinfo *, int);
75 1.2.2.2 bouyer static __inline u_long wfq_hashbydstaddr(struct flowinfo *, int);
76 1.2.2.2 bouyer static __inline u_long wfq_hashbysrcport(struct flowinfo *, int);
77 1.2.2.2 bouyer static wfq *wfq_maxqueue(wfq_state_t *);
78 1.2.2.2 bouyer static struct mbuf *wfq_ifdequeue(struct ifaltq *, int);
79 1.2.2.2 bouyer static int wfq_getqid(struct wfq_getqid *);
80 1.2.2.2 bouyer static int wfq_setweight(struct wfq_setweight *);
81 1.2.2.2 bouyer static int wfq_getstats(struct wfq_getstats *);
82 1.2.2.2 bouyer static int wfq_config(struct wfq_conf *);
83 1.2.2.2 bouyer static int wfq_request __P((struct ifaltq *, int, void *));
84 1.2.2.2 bouyer static int wfq_flush(struct ifaltq *);
85 1.2.2.2 bouyer static void *wfq_classify(void *, struct mbuf *, int);
86 1.2.2.2 bouyer
87 1.2.2.2 bouyer /* global value : pointer to wfq queue list */
88 1.2.2.2 bouyer static wfq_state_t *wfq_list = NULL;
89 1.2.2.2 bouyer
90 1.2.2.2 bouyer static int
91 1.2.2.2 bouyer wfq_setenable(ifacep, flag)
92 1.2.2.2 bouyer struct wfq_interface *ifacep;
93 1.2.2.2 bouyer int flag;
94 1.2.2.2 bouyer {
95 1.2.2.2 bouyer wfq_state_t *wfqp;
96 1.2.2.2 bouyer int error = 0;
97 1.2.2.2 bouyer
98 1.2.2.2 bouyer if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
99 1.2.2.2 bouyer return (EBADF);
100 1.2.2.2 bouyer
101 1.2.2.2 bouyer switch(flag){
102 1.2.2.2 bouyer case ENABLE:
103 1.2.2.2 bouyer error = altq_enable(wfqp->ifq);
104 1.2.2.2 bouyer break;
105 1.2.2.2 bouyer case DISABLE:
106 1.2.2.2 bouyer error = altq_disable(wfqp->ifq);
107 1.2.2.2 bouyer break;
108 1.2.2.2 bouyer }
109 1.2.2.2 bouyer return error;
110 1.2.2.2 bouyer }
111 1.2.2.2 bouyer
112 1.2.2.2 bouyer
113 1.2.2.2 bouyer static int
114 1.2.2.2 bouyer wfq_ifattach(ifacep)
115 1.2.2.2 bouyer struct wfq_interface *ifacep;
116 1.2.2.2 bouyer {
117 1.2.2.2 bouyer int error = 0, i;
118 1.2.2.2 bouyer struct ifnet *ifp;
119 1.2.2.2 bouyer wfq_state_t *new_wfqp;
120 1.2.2.2 bouyer wfq *queue;
121 1.2.2.2 bouyer
122 1.2.2.2 bouyer if ((ifp = ifunit(ifacep->wfq_ifacename)) == NULL) {
123 1.2.2.2 bouyer #ifdef WFQ_DEBUG
124 1.2.2.2 bouyer printf("wfq_ifattach()...no ifp found\n");
125 1.2.2.2 bouyer #endif
126 1.2.2.2 bouyer return (ENXIO);
127 1.2.2.2 bouyer }
128 1.2.2.2 bouyer
129 1.2.2.2 bouyer if (!ALTQ_IS_READY(&ifp->if_snd)) {
130 1.2.2.2 bouyer #ifdef WFQ_DEBUG
131 1.2.2.2 bouyer printf("wfq_ifattach()...altq is not ready\n");
132 1.2.2.2 bouyer #endif
133 1.2.2.2 bouyer return (ENXIO);
134 1.2.2.2 bouyer }
135 1.2.2.2 bouyer
136 1.2.2.2 bouyer /* allocate and initialize wfq_state_t */
137 1.2.2.2 bouyer MALLOC(new_wfqp, wfq_state_t *, sizeof(wfq_state_t),
138 1.2.2.2 bouyer M_DEVBUF, M_WAITOK);
139 1.2.2.2 bouyer if (new_wfqp == NULL)
140 1.2.2.2 bouyer return (ENOMEM);
141 1.2.2.2 bouyer bzero(new_wfqp, sizeof(wfq_state_t));
142 1.2.2.2 bouyer MALLOC(queue, wfq *, sizeof(wfq) * DEFAULT_QSIZE,
143 1.2.2.2 bouyer M_DEVBUF, M_WAITOK);
144 1.2.2.2 bouyer if (queue == NULL) {
145 1.2.2.2 bouyer FREE(new_wfqp, M_DEVBUF);
146 1.2.2.2 bouyer return (ENOMEM);
147 1.2.2.2 bouyer }
148 1.2.2.2 bouyer bzero(queue, sizeof(wfq) * DEFAULT_QSIZE);
149 1.2.2.2 bouyer
150 1.2.2.2 bouyer /* keep the ifq */
151 1.2.2.2 bouyer new_wfqp->ifq = &ifp->if_snd;
152 1.2.2.2 bouyer new_wfqp->nums = DEFAULT_QSIZE;
153 1.2.2.2 bouyer new_wfqp->hwm = HWM;
154 1.2.2.2 bouyer new_wfqp->bytes = 0;
155 1.2.2.2 bouyer new_wfqp->rrp = NULL;
156 1.2.2.2 bouyer new_wfqp->queue = queue;
157 1.2.2.2 bouyer new_wfqp->hash_func = wfq_hashbydstaddr;
158 1.2.2.2 bouyer new_wfqp->fbmask = FIMB4_DADDR;
159 1.2.2.2 bouyer
160 1.2.2.2 bouyer for (i = 0; i < new_wfqp->nums; i++, queue++) {
161 1.2.2.2 bouyer queue->next = queue->prev = NULL;
162 1.2.2.2 bouyer queue->head = queue->tail = NULL;
163 1.2.2.2 bouyer queue->bytes = queue->quota = 0;
164 1.2.2.2 bouyer queue->weight = 100;
165 1.2.2.2 bouyer }
166 1.2.2.2 bouyer
167 1.2.2.2 bouyer /*
168 1.2.2.2 bouyer * set WFQ to this ifnet structure.
169 1.2.2.2 bouyer */
170 1.2.2.2 bouyer if ((error = altq_attach(&ifp->if_snd, ALTQT_WFQ, new_wfqp,
171 1.2.2.2 bouyer wfq_ifenqueue, wfq_ifdequeue, wfq_request,
172 1.2.2.2 bouyer new_wfqp, wfq_classify)) != 0) {
173 1.2.2.2 bouyer FREE(queue, M_DEVBUF);
174 1.2.2.2 bouyer FREE(new_wfqp, M_DEVBUF);
175 1.2.2.2 bouyer return (error);
176 1.2.2.2 bouyer }
177 1.2.2.2 bouyer
178 1.2.2.2 bouyer new_wfqp->next = wfq_list;
179 1.2.2.2 bouyer wfq_list = new_wfqp;
180 1.2.2.2 bouyer
181 1.2.2.2 bouyer return (error);
182 1.2.2.2 bouyer }
183 1.2.2.2 bouyer
184 1.2.2.2 bouyer
185 1.2.2.2 bouyer static int
186 1.2.2.2 bouyer wfq_ifdetach(ifacep)
187 1.2.2.2 bouyer struct wfq_interface *ifacep;
188 1.2.2.2 bouyer {
189 1.2.2.2 bouyer int error = 0;
190 1.2.2.2 bouyer wfq_state_t *wfqp;
191 1.2.2.2 bouyer
192 1.2.2.2 bouyer if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
193 1.2.2.2 bouyer return (EBADF);
194 1.2.2.2 bouyer
195 1.2.2.2 bouyer /* free queued mbuf */
196 1.2.2.2 bouyer wfq_flush(wfqp->ifq);
197 1.2.2.2 bouyer
198 1.2.2.2 bouyer /* remove WFQ from the ifnet structure. */
199 1.2.2.2 bouyer (void)altq_disable(wfqp->ifq);
200 1.2.2.2 bouyer (void)altq_detach(wfqp->ifq);
201 1.2.2.2 bouyer
202 1.2.2.2 bouyer /* remove from the wfqstate list */
203 1.2.2.2 bouyer if (wfq_list == wfqp)
204 1.2.2.2 bouyer wfq_list = wfqp->next;
205 1.2.2.2 bouyer else {
206 1.2.2.2 bouyer wfq_state_t *wp = wfq_list;
207 1.2.2.2 bouyer do {
208 1.2.2.2 bouyer if (wp->next == wfqp) {
209 1.2.2.2 bouyer wp->next = wfqp->next;
210 1.2.2.2 bouyer break;
211 1.2.2.2 bouyer }
212 1.2.2.2 bouyer } while ((wp = wp->next) != NULL);
213 1.2.2.2 bouyer }
214 1.2.2.2 bouyer
215 1.2.2.2 bouyer /* deallocate wfq_state_t */
216 1.2.2.2 bouyer FREE(wfqp->queue, M_DEVBUF);
217 1.2.2.2 bouyer FREE(wfqp, M_DEVBUF);
218 1.2.2.2 bouyer return (error);
219 1.2.2.2 bouyer }
220 1.2.2.2 bouyer
221 1.2.2.2 bouyer static int
222 1.2.2.2 bouyer wfq_request(ifq, req, arg)
223 1.2.2.2 bouyer struct ifaltq *ifq;
224 1.2.2.2 bouyer int req;
225 1.2.2.2 bouyer void *arg;
226 1.2.2.2 bouyer {
227 1.2.2.2 bouyer wfq_state_t *wfqp = (wfq_state_t *)ifq->altq_disc;
228 1.2.2.2 bouyer
229 1.2.2.2 bouyer switch (req) {
230 1.2.2.2 bouyer case ALTRQ_PURGE:
231 1.2.2.2 bouyer wfq_flush(wfqp->ifq);
232 1.2.2.2 bouyer break;
233 1.2.2.2 bouyer }
234 1.2.2.2 bouyer return (0);
235 1.2.2.2 bouyer }
236 1.2.2.2 bouyer
237 1.2.2.2 bouyer
238 1.2.2.2 bouyer static int
239 1.2.2.2 bouyer wfq_flush(ifq)
240 1.2.2.2 bouyer struct ifaltq *ifq;
241 1.2.2.2 bouyer {
242 1.2.2.2 bouyer struct mbuf *mp;
243 1.2.2.2 bouyer
244 1.2.2.2 bouyer while ((mp = wfq_ifdequeue(ifq, ALTDQ_REMOVE)) != NULL)
245 1.2.2.2 bouyer m_freem(mp);
246 1.2.2.2 bouyer if (ALTQ_IS_ENABLED(ifq))
247 1.2.2.2 bouyer ifq->ifq_len = 0;
248 1.2.2.2 bouyer return 0;
249 1.2.2.2 bouyer }
250 1.2.2.2 bouyer
251 1.2.2.2 bouyer static void *
252 1.2.2.2 bouyer wfq_classify(clfier, m, af)
253 1.2.2.2 bouyer void *clfier;
254 1.2.2.2 bouyer struct mbuf *m;
255 1.2.2.2 bouyer int af;
256 1.2.2.2 bouyer {
257 1.2.2.2 bouyer wfq_state_t *wfqp = (wfq_state_t *)clfier;
258 1.2.2.2 bouyer struct flowinfo flow;
259 1.2.2.2 bouyer
260 1.2.2.2 bouyer altq_extractflow(m, af, &flow, wfqp->fbmask);
261 1.2.2.2 bouyer return (&wfqp->queue[(*wfqp->hash_func)(&flow, wfqp->nums)]);
262 1.2.2.2 bouyer }
263 1.2.2.2 bouyer
264 1.2.2.2 bouyer static int
265 1.2.2.2 bouyer wfq_ifenqueue(ifq, mp, pktattr)
266 1.2.2.2 bouyer struct ifaltq *ifq;
267 1.2.2.2 bouyer struct mbuf *mp;
268 1.2.2.2 bouyer struct altq_pktattr *pktattr;
269 1.2.2.2 bouyer {
270 1.2.2.2 bouyer wfq_state_t *wfqp;
271 1.2.2.2 bouyer wfq *queue;
272 1.2.2.2 bouyer int byte, error = 0;
273 1.2.2.2 bouyer
274 1.2.2.2 bouyer wfqp = (wfq_state_t *)ifq->altq_disc;
275 1.2.2.2 bouyer mp->m_nextpkt = NULL;
276 1.2.2.2 bouyer
277 1.2.2.2 bouyer /* grab a queue selected by classifier */
278 1.2.2.2 bouyer if (pktattr == NULL || (queue = pktattr->pattr_class) == NULL)
279 1.2.2.2 bouyer queue = &wfqp->queue[0];
280 1.2.2.2 bouyer
281 1.2.2.2 bouyer if (queue->tail == NULL)
282 1.2.2.2 bouyer queue->head = mp;
283 1.2.2.2 bouyer else
284 1.2.2.2 bouyer queue->tail->m_nextpkt = mp;
285 1.2.2.2 bouyer queue->tail = mp;
286 1.2.2.2 bouyer byte = mp->m_pkthdr.len;
287 1.2.2.2 bouyer queue->bytes += byte;
288 1.2.2.2 bouyer wfqp->bytes += byte;
289 1.2.2.2 bouyer ifq->ifq_len++;
290 1.2.2.2 bouyer
291 1.2.2.2 bouyer if (queue->next == NULL) {
292 1.2.2.2 bouyer /* this queue gets active. add the queue to the active list */
293 1.2.2.2 bouyer if (wfqp->rrp == NULL){
294 1.2.2.2 bouyer /* no queue in the active list */
295 1.2.2.2 bouyer queue->next = queue->prev = queue;
296 1.2.2.2 bouyer wfqp->rrp = queue;
297 1.2.2.2 bouyer WFQ_ADDQUOTA(queue);
298 1.2.2.2 bouyer } else {
299 1.2.2.2 bouyer /* insert the queue at the tail of the active list */
300 1.2.2.2 bouyer queue->prev = wfqp->rrp->prev;
301 1.2.2.2 bouyer wfqp->rrp->prev->next = queue;
302 1.2.2.2 bouyer wfqp->rrp->prev = queue;
303 1.2.2.2 bouyer queue->next = wfqp->rrp;
304 1.2.2.2 bouyer queue->quota = 0;
305 1.2.2.2 bouyer }
306 1.2.2.2 bouyer }
307 1.2.2.2 bouyer
308 1.2.2.2 bouyer /* check overflow. if the total size exceeds the high water mark,
309 1.2.2.2 bouyer drop packets from the longest queue. */
310 1.2.2.2 bouyer while (wfqp->bytes > wfqp->hwm) {
311 1.2.2.2 bouyer wfq *drop_queue = wfq_maxqueue(wfqp);
312 1.2.2.2 bouyer
313 1.2.2.2 bouyer /* drop the packet at the head. */
314 1.2.2.2 bouyer mp = drop_queue->head;
315 1.2.2.2 bouyer if ((drop_queue->head = mp->m_nextpkt) == NULL)
316 1.2.2.2 bouyer drop_queue->tail = NULL;
317 1.2.2.2 bouyer mp->m_nextpkt = NULL;
318 1.2.2.2 bouyer byte = mp->m_pkthdr.len;
319 1.2.2.2 bouyer drop_queue->bytes -= byte;
320 1.2.2.2 bouyer PKTCNTR_ADD(&drop_queue->drop_cnt, byte);
321 1.2.2.2 bouyer wfqp->bytes -= byte;
322 1.2.2.2 bouyer m_freem(mp);
323 1.2.2.2 bouyer ifq->ifq_len--;
324 1.2.2.2 bouyer if(drop_queue == queue)
325 1.2.2.2 bouyer /* the queue for this flow is selected to drop */
326 1.2.2.2 bouyer error = ENOBUFS;
327 1.2.2.2 bouyer }
328 1.2.2.2 bouyer return error;
329 1.2.2.2 bouyer }
330 1.2.2.2 bouyer
331 1.2.2.2 bouyer
332 1.2.2.2 bouyer static u_long wfq_hash(flow, n)
333 1.2.2.2 bouyer struct flowinfo *flow;
334 1.2.2.2 bouyer int n;
335 1.2.2.2 bouyer {
336 1.2.2.2 bouyer u_long val = 0;
337 1.2.2.2 bouyer
338 1.2.2.2 bouyer if (flow != NULL) {
339 1.2.2.2 bouyer if (flow->fi_family == AF_INET) {
340 1.2.2.2 bouyer struct flowinfo_in *fp = (struct flowinfo_in *)flow;
341 1.2.2.2 bouyer u_long val2;
342 1.2.2.2 bouyer
343 1.2.2.2 bouyer val = fp->fi_dst.s_addr ^ fp->fi_src.s_addr;
344 1.2.2.2 bouyer val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
345 1.2.2.2 bouyer val2 = fp->fi_dport ^ fp->fi_sport ^ fp->fi_proto;
346 1.2.2.2 bouyer val2 = val2 ^ (val2 >> 8);
347 1.2.2.2 bouyer val = val ^ val2;
348 1.2.2.2 bouyer }
349 1.2.2.2 bouyer #ifdef INET6
350 1.2.2.2 bouyer else if (flow->fi_family == AF_INET6) {
351 1.2.2.2 bouyer struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
352 1.2.2.2 bouyer
353 1.2.2.2 bouyer val = ntohl(fp6->fi6_flowlabel);
354 1.2.2.2 bouyer }
355 1.2.2.2 bouyer #endif
356 1.2.2.2 bouyer }
357 1.2.2.2 bouyer
358 1.2.2.2 bouyer return (val % n);
359 1.2.2.2 bouyer }
360 1.2.2.2 bouyer
361 1.2.2.2 bouyer
362 1.2.2.2 bouyer static __inline u_long wfq_hashbydstaddr(flow, n)
363 1.2.2.2 bouyer struct flowinfo *flow;
364 1.2.2.2 bouyer int n;
365 1.2.2.2 bouyer {
366 1.2.2.2 bouyer u_long val = 0;
367 1.2.2.2 bouyer
368 1.2.2.2 bouyer if (flow != NULL) {
369 1.2.2.2 bouyer if (flow->fi_family == AF_INET) {
370 1.2.2.2 bouyer struct flowinfo_in *fp = (struct flowinfo_in *)flow;
371 1.2.2.2 bouyer
372 1.2.2.2 bouyer val = fp->fi_dst.s_addr;
373 1.2.2.2 bouyer val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
374 1.2.2.2 bouyer }
375 1.2.2.2 bouyer #ifdef INET6
376 1.2.2.2 bouyer else if (flow->fi_family == AF_INET6) {
377 1.2.2.2 bouyer struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
378 1.2.2.2 bouyer
379 1.2.2.2 bouyer val = ntohl(fp6->fi6_flowlabel);
380 1.2.2.2 bouyer }
381 1.2.2.2 bouyer #endif
382 1.2.2.2 bouyer }
383 1.2.2.2 bouyer
384 1.2.2.2 bouyer return (val % n);
385 1.2.2.2 bouyer }
386 1.2.2.2 bouyer
387 1.2.2.2 bouyer static __inline u_long wfq_hashbysrcport(flow, n)
388 1.2.2.2 bouyer struct flowinfo *flow;
389 1.2.2.2 bouyer int n;
390 1.2.2.2 bouyer {
391 1.2.2.2 bouyer u_long val = 0;
392 1.2.2.2 bouyer
393 1.2.2.2 bouyer if (flow != NULL) {
394 1.2.2.2 bouyer if (flow->fi_family == AF_INET) {
395 1.2.2.2 bouyer struct flowinfo_in *fp = (struct flowinfo_in *)flow;
396 1.2.2.2 bouyer
397 1.2.2.2 bouyer val = fp->fi_sport;
398 1.2.2.2 bouyer }
399 1.2.2.2 bouyer #ifdef INET6
400 1.2.2.2 bouyer else if (flow->fi_family == AF_INET6) {
401 1.2.2.2 bouyer struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
402 1.2.2.2 bouyer
403 1.2.2.2 bouyer val = fp6->fi6_sport;
404 1.2.2.2 bouyer }
405 1.2.2.2 bouyer #endif
406 1.2.2.2 bouyer }
407 1.2.2.2 bouyer val = val ^ (val >> 8);
408 1.2.2.2 bouyer
409 1.2.2.2 bouyer return (val % n);
410 1.2.2.2 bouyer }
411 1.2.2.2 bouyer
412 1.2.2.2 bouyer static wfq *wfq_maxqueue(wfqp)
413 1.2.2.2 bouyer wfq_state_t *wfqp;
414 1.2.2.2 bouyer {
415 1.2.2.2 bouyer int byte, max_byte = 0;
416 1.2.2.2 bouyer wfq *queue, *max_queue = NULL;
417 1.2.2.2 bouyer
418 1.2.2.2 bouyer if((queue = wfqp->rrp) == NULL)
419 1.2.2.2 bouyer /* never happens */
420 1.2.2.2 bouyer return NULL;
421 1.2.2.2 bouyer do{
422 1.2.2.2 bouyer if ((byte = queue->bytes * 100 / queue->weight) > max_byte) {
423 1.2.2.2 bouyer max_queue = queue;
424 1.2.2.2 bouyer max_byte = byte;
425 1.2.2.2 bouyer }
426 1.2.2.2 bouyer } while ((queue = queue->next) != wfqp->rrp);
427 1.2.2.2 bouyer
428 1.2.2.2 bouyer return max_queue;
429 1.2.2.2 bouyer }
430 1.2.2.2 bouyer
431 1.2.2.2 bouyer
432 1.2.2.2 bouyer static struct mbuf *
433 1.2.2.2 bouyer wfq_ifdequeue(ifq, op)
434 1.2.2.2 bouyer struct ifaltq *ifq;
435 1.2.2.2 bouyer int op;
436 1.2.2.2 bouyer {
437 1.2.2.2 bouyer wfq_state_t *wfqp;
438 1.2.2.2 bouyer wfq *queue;
439 1.2.2.2 bouyer struct mbuf *mp;
440 1.2.2.2 bouyer int byte;
441 1.2.2.2 bouyer
442 1.2.2.2 bouyer wfqp = (wfq_state_t *)ifq->altq_disc;
443 1.2.2.2 bouyer
444 1.2.2.2 bouyer if ((wfqp->bytes == 0) || ((queue = wfqp->rrp) == NULL))
445 1.2.2.2 bouyer /* no packet in the queues */
446 1.2.2.2 bouyer return NULL;
447 1.2.2.2 bouyer
448 1.2.2.2 bouyer while (1) {
449 1.2.2.2 bouyer if (queue->quota > 0) {
450 1.2.2.2 bouyer if (queue->bytes <= 0) {
451 1.2.2.2 bouyer /* this queue no longer has packet.
452 1.2.2.2 bouyer remove the queue from the active list. */
453 1.2.2.2 bouyer if (queue->next == queue){
454 1.2.2.2 bouyer /* no other active queue
455 1.2.2.2 bouyer -- this case never happens in
456 1.2.2.2 bouyer this algorithm. */
457 1.2.2.2 bouyer queue->next = queue->prev = NULL;
458 1.2.2.2 bouyer wfqp->rrp = NULL;
459 1.2.2.2 bouyer return NULL;
460 1.2.2.2 bouyer } else {
461 1.2.2.2 bouyer queue->prev->next = queue->next;
462 1.2.2.2 bouyer queue->next->prev = queue->prev;
463 1.2.2.2 bouyer /* the round-robin pointer points
464 1.2.2.2 bouyer to this queue, advance the rrp */
465 1.2.2.2 bouyer wfqp->rrp = queue->next;
466 1.2.2.2 bouyer queue->next = queue->prev = NULL;
467 1.2.2.2 bouyer queue = wfqp->rrp;
468 1.2.2.2 bouyer WFQ_ADDQUOTA(queue);
469 1.2.2.2 bouyer continue;
470 1.2.2.2 bouyer }
471 1.2.2.2 bouyer }
472 1.2.2.2 bouyer
473 1.2.2.2 bouyer /* dequeue a packet from this queue */
474 1.2.2.2 bouyer mp = queue->head;
475 1.2.2.2 bouyer if (op == ALTDQ_REMOVE) {
476 1.2.2.2 bouyer if((queue->head = mp->m_nextpkt) == NULL)
477 1.2.2.2 bouyer queue->tail = NULL;
478 1.2.2.2 bouyer byte = mp->m_pkthdr.len;
479 1.2.2.2 bouyer mp->m_nextpkt = NULL;
480 1.2.2.2 bouyer queue->quota -= byte;
481 1.2.2.2 bouyer queue->bytes -= byte;
482 1.2.2.2 bouyer PKTCNTR_ADD(&queue->xmit_cnt, byte);
483 1.2.2.2 bouyer wfqp->bytes -= byte;
484 1.2.2.2 bouyer if (ALTQ_IS_ENABLED(ifq))
485 1.2.2.2 bouyer ifq->ifq_len--;
486 1.2.2.2 bouyer }
487 1.2.2.2 bouyer return mp;
488 1.2.2.2 bouyer
489 1.2.2.2 bouyer /* if the queue gets empty by this dequeueing,
490 1.2.2.2 bouyer the queue will be removed from the active list
491 1.2.2.2 bouyer at the next round */
492 1.2.2.2 bouyer }
493 1.2.2.2 bouyer
494 1.2.2.2 bouyer /* advance the round-robin pointer */
495 1.2.2.2 bouyer queue = wfqp->rrp = queue->next;
496 1.2.2.2 bouyer WFQ_ADDQUOTA(queue);
497 1.2.2.2 bouyer }
498 1.2.2.2 bouyer }
499 1.2.2.2 bouyer
500 1.2.2.2 bouyer static int
501 1.2.2.2 bouyer wfq_getqid(gqidp)
502 1.2.2.2 bouyer struct wfq_getqid *gqidp;
503 1.2.2.2 bouyer {
504 1.2.2.2 bouyer wfq_state_t *wfqp;
505 1.2.2.2 bouyer
506 1.2.2.2 bouyer if ((wfqp = altq_lookup(gqidp->iface.wfq_ifacename, ALTQT_WFQ))
507 1.2.2.2 bouyer == NULL)
508 1.2.2.2 bouyer return (EBADF);
509 1.2.2.2 bouyer
510 1.2.2.2 bouyer gqidp->qid = (*wfqp->hash_func)(&gqidp->flow, wfqp->nums);
511 1.2.2.2 bouyer return 0;
512 1.2.2.2 bouyer }
513 1.2.2.2 bouyer
514 1.2.2.2 bouyer static int
515 1.2.2.2 bouyer wfq_setweight(swp)
516 1.2.2.2 bouyer struct wfq_setweight *swp;
517 1.2.2.2 bouyer {
518 1.2.2.2 bouyer wfq_state_t *wfqp;
519 1.2.2.2 bouyer wfq *queue;
520 1.2.2.2 bouyer int old;
521 1.2.2.2 bouyer
522 1.2.2.2 bouyer if (swp->weight < 0) {
523 1.2.2.2 bouyer printf("set weight in natural number\n");
524 1.2.2.2 bouyer return (EINVAL);
525 1.2.2.2 bouyer }
526 1.2.2.2 bouyer
527 1.2.2.2 bouyer if ((wfqp = altq_lookup(swp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
528 1.2.2.2 bouyer return (EBADF);
529 1.2.2.2 bouyer
530 1.2.2.2 bouyer queue = &wfqp->queue[swp->qid];
531 1.2.2.2 bouyer old = queue->weight;
532 1.2.2.2 bouyer queue->weight = swp->weight;
533 1.2.2.2 bouyer swp->weight = old;
534 1.2.2.2 bouyer return 0;
535 1.2.2.2 bouyer }
536 1.2.2.2 bouyer
537 1.2.2.2 bouyer
538 1.2.2.2 bouyer static int
539 1.2.2.2 bouyer wfq_getstats(gsp)
540 1.2.2.2 bouyer struct wfq_getstats *gsp;
541 1.2.2.2 bouyer {
542 1.2.2.2 bouyer wfq_state_t *wfqp;
543 1.2.2.2 bouyer wfq *queue;
544 1.2.2.2 bouyer queue_stats *stats;
545 1.2.2.2 bouyer
546 1.2.2.2 bouyer if ((wfqp = altq_lookup(gsp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
547 1.2.2.2 bouyer return (EBADF);
548 1.2.2.2 bouyer
549 1.2.2.2 bouyer if (gsp->qid >= wfqp->nums)
550 1.2.2.2 bouyer return (EINVAL);
551 1.2.2.2 bouyer
552 1.2.2.2 bouyer queue = &wfqp->queue[gsp->qid];
553 1.2.2.2 bouyer stats = &gsp->stats;
554 1.2.2.2 bouyer
555 1.2.2.2 bouyer stats->bytes = queue->bytes;
556 1.2.2.2 bouyer stats->weight = queue->weight;
557 1.2.2.2 bouyer stats->xmit_cnt = queue->xmit_cnt;
558 1.2.2.2 bouyer stats->drop_cnt = queue->drop_cnt;
559 1.2.2.2 bouyer
560 1.2.2.2 bouyer return 0;
561 1.2.2.2 bouyer }
562 1.2.2.2 bouyer
563 1.2.2.2 bouyer
564 1.2.2.2 bouyer static int
565 1.2.2.2 bouyer wfq_config(cf)
566 1.2.2.2 bouyer struct wfq_conf *cf;
567 1.2.2.2 bouyer {
568 1.2.2.2 bouyer wfq_state_t *wfqp;
569 1.2.2.2 bouyer wfq *queue;
570 1.2.2.2 bouyer int i, error = 0;
571 1.2.2.2 bouyer
572 1.2.2.2 bouyer if ((wfqp = altq_lookup(cf->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
573 1.2.2.2 bouyer return (EBADF);
574 1.2.2.2 bouyer
575 1.2.2.2 bouyer if(cf->nqueues <= 0 || MAX_QSIZE < cf->nqueues)
576 1.2.2.2 bouyer cf->nqueues = DEFAULT_QSIZE;
577 1.2.2.2 bouyer
578 1.2.2.2 bouyer if (cf->nqueues != wfqp->nums) {
579 1.2.2.2 bouyer /* free queued mbuf */
580 1.2.2.2 bouyer wfq_flush(wfqp->ifq);
581 1.2.2.2 bouyer FREE(wfqp->queue, M_DEVBUF);
582 1.2.2.2 bouyer
583 1.2.2.2 bouyer MALLOC(queue, wfq *, sizeof(wfq) * cf->nqueues,
584 1.2.2.2 bouyer M_DEVBUF, M_WAITOK);
585 1.2.2.2 bouyer if (queue == NULL)
586 1.2.2.2 bouyer return (ENOMEM);
587 1.2.2.2 bouyer bzero(queue, sizeof(wfq) * cf->nqueues);
588 1.2.2.2 bouyer
589 1.2.2.2 bouyer wfqp->nums = cf->nqueues;
590 1.2.2.2 bouyer wfqp->bytes = 0;
591 1.2.2.2 bouyer wfqp->rrp = NULL;
592 1.2.2.2 bouyer wfqp->queue = queue;
593 1.2.2.2 bouyer for (i = 0; i < wfqp->nums; i++, queue++) {
594 1.2.2.2 bouyer queue->next = queue->prev = NULL;
595 1.2.2.2 bouyer queue->head = queue->tail = NULL;
596 1.2.2.2 bouyer queue->bytes = queue->quota = 0;
597 1.2.2.2 bouyer queue->weight = 100;
598 1.2.2.2 bouyer }
599 1.2.2.2 bouyer }
600 1.2.2.2 bouyer
601 1.2.2.2 bouyer if (cf->qlimit != 0)
602 1.2.2.2 bouyer wfqp->hwm = cf->qlimit;
603 1.2.2.2 bouyer
604 1.2.2.2 bouyer switch (cf->hash_policy) {
605 1.2.2.2 bouyer case WFQ_HASH_DSTADDR:
606 1.2.2.2 bouyer wfqp->hash_func = wfq_hashbydstaddr;
607 1.2.2.2 bouyer wfqp->fbmask = FIMB4_DADDR;
608 1.2.2.2 bouyer #ifdef INET6
609 1.2.2.2 bouyer wfqp->fbmask |= FIMB6_FLABEL; /* use flowlabel for ipv6 */
610 1.2.2.2 bouyer #endif
611 1.2.2.2 bouyer break;
612 1.2.2.2 bouyer case WFQ_HASH_SRCPORT:
613 1.2.2.2 bouyer wfqp->hash_func = wfq_hashbysrcport;
614 1.2.2.2 bouyer wfqp->fbmask = FIMB4_SPORT;
615 1.2.2.2 bouyer #ifdef INET6
616 1.2.2.2 bouyer wfqp->fbmask |= FIMB6_SPORT;
617 1.2.2.2 bouyer #endif
618 1.2.2.2 bouyer break;
619 1.2.2.2 bouyer case WFQ_HASH_FULL:
620 1.2.2.2 bouyer wfqp->hash_func = wfq_hash;
621 1.2.2.2 bouyer wfqp->fbmask = FIMB4_ALL;
622 1.2.2.2 bouyer #ifdef INET6
623 1.2.2.2 bouyer wfqp->fbmask |= FIMB6_FLABEL; /* use flowlabel for ipv6 */
624 1.2.2.2 bouyer #endif
625 1.2.2.2 bouyer break;
626 1.2.2.2 bouyer default:
627 1.2.2.2 bouyer error = EINVAL;
628 1.2.2.2 bouyer break;
629 1.2.2.2 bouyer }
630 1.2.2.2 bouyer return error;
631 1.2.2.2 bouyer }
632 1.2.2.2 bouyer
633 1.2.2.2 bouyer /*
634 1.2.2.2 bouyer * wfq device interface
635 1.2.2.2 bouyer */
636 1.2.2.2 bouyer
637 1.2.2.2 bouyer altqdev_decl(wfq);
638 1.2.2.2 bouyer
639 1.2.2.2 bouyer int
640 1.2.2.2 bouyer wfqopen(dev, flag, fmt, p)
641 1.2.2.2 bouyer dev_t dev;
642 1.2.2.2 bouyer int flag, fmt;
643 1.2.2.2 bouyer struct proc *p;
644 1.2.2.2 bouyer {
645 1.2.2.2 bouyer return 0;
646 1.2.2.2 bouyer }
647 1.2.2.2 bouyer
648 1.2.2.2 bouyer int
649 1.2.2.2 bouyer wfqclose(dev, flag, fmt, p)
650 1.2.2.2 bouyer dev_t dev;
651 1.2.2.2 bouyer int flag, fmt;
652 1.2.2.2 bouyer struct proc *p;
653 1.2.2.2 bouyer {
654 1.2.2.2 bouyer struct ifnet *ifp;
655 1.2.2.2 bouyer struct wfq_interface iface;
656 1.2.2.2 bouyer wfq_state_t *wfqp;
657 1.2.2.2 bouyer int s;
658 1.2.2.2 bouyer
659 1.2.2.3 bouyer s = splnet();
660 1.2.2.2 bouyer while ((wfqp = wfq_list) != NULL) {
661 1.2.2.2 bouyer ifp = wfqp->ifq->altq_ifp;
662 1.2.2.2 bouyer #if defined(__NetBSD__) || defined(__OpenBSD__)
663 1.2.2.2 bouyer sprintf(iface.wfq_ifacename, "%s", ifp->if_xname);
664 1.2.2.2 bouyer #else
665 1.2.2.2 bouyer sprintf(iface.wfq_ifacename, "%s%d",
666 1.2.2.2 bouyer ifp->if_name, ifp->if_unit);
667 1.2.2.2 bouyer #endif
668 1.2.2.2 bouyer wfq_ifdetach(&iface);
669 1.2.2.2 bouyer }
670 1.2.2.2 bouyer splx(s);
671 1.2.2.2 bouyer return 0;
672 1.2.2.2 bouyer }
673 1.2.2.2 bouyer
674 1.2.2.2 bouyer int
675 1.2.2.2 bouyer wfqioctl(dev, cmd, addr, flag, p)
676 1.2.2.2 bouyer dev_t dev;
677 1.2.2.2 bouyer ioctlcmd_t cmd;
678 1.2.2.2 bouyer caddr_t addr;
679 1.2.2.2 bouyer int flag;
680 1.2.2.2 bouyer struct proc *p;
681 1.2.2.2 bouyer {
682 1.2.2.2 bouyer int error = 0;
683 1.2.2.2 bouyer int s;
684 1.2.2.2 bouyer
685 1.2.2.2 bouyer /* check cmd for superuser only */
686 1.2.2.2 bouyer switch (cmd) {
687 1.2.2.2 bouyer case WFQ_GET_QID:
688 1.2.2.2 bouyer case WFQ_GET_STATS:
689 1.2.2.2 bouyer break;
690 1.2.2.2 bouyer default:
691 1.2.2.2 bouyer #if (__FreeBSD_version > 400000)
692 1.2.2.2 bouyer if ((error = suser(p)) != 0)
693 1.2.2.2 bouyer #else
694 1.2.2.2 bouyer if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
695 1.2.2.2 bouyer #endif
696 1.2.2.2 bouyer return (error);
697 1.2.2.2 bouyer break;
698 1.2.2.2 bouyer }
699 1.2.2.2 bouyer
700 1.2.2.3 bouyer s = splnet();
701 1.2.2.2 bouyer switch (cmd) {
702 1.2.2.2 bouyer
703 1.2.2.2 bouyer case WFQ_ENABLE:
704 1.2.2.2 bouyer error = wfq_setenable((struct wfq_interface *)addr, ENABLE);
705 1.2.2.2 bouyer break;
706 1.2.2.2 bouyer
707 1.2.2.2 bouyer case WFQ_DISABLE:
708 1.2.2.2 bouyer error = wfq_setenable((struct wfq_interface *)addr, DISABLE);
709 1.2.2.2 bouyer break;
710 1.2.2.2 bouyer
711 1.2.2.2 bouyer case WFQ_IF_ATTACH:
712 1.2.2.2 bouyer error = wfq_ifattach((struct wfq_interface *)addr);
713 1.2.2.2 bouyer break;
714 1.2.2.2 bouyer
715 1.2.2.2 bouyer case WFQ_IF_DETACH:
716 1.2.2.2 bouyer error = wfq_ifdetach((struct wfq_interface *)addr);
717 1.2.2.2 bouyer break;
718 1.2.2.2 bouyer
719 1.2.2.2 bouyer case WFQ_GET_QID:
720 1.2.2.2 bouyer error = wfq_getqid((struct wfq_getqid *)addr);
721 1.2.2.2 bouyer break;
722 1.2.2.2 bouyer
723 1.2.2.2 bouyer case WFQ_SET_WEIGHT:
724 1.2.2.2 bouyer error = wfq_setweight((struct wfq_setweight *)addr);
725 1.2.2.2 bouyer break;
726 1.2.2.2 bouyer
727 1.2.2.2 bouyer case WFQ_GET_STATS:
728 1.2.2.2 bouyer error = wfq_getstats((struct wfq_getstats *)addr);
729 1.2.2.2 bouyer break;
730 1.2.2.2 bouyer
731 1.2.2.2 bouyer case WFQ_CONFIG:
732 1.2.2.2 bouyer error = wfq_config((struct wfq_conf *)addr);
733 1.2.2.2 bouyer break;
734 1.2.2.2 bouyer
735 1.2.2.2 bouyer default:
736 1.2.2.2 bouyer error = EINVAL;
737 1.2.2.2 bouyer break;
738 1.2.2.2 bouyer }
739 1.2.2.2 bouyer splx(s);
740 1.2.2.2 bouyer return error;
741 1.2.2.2 bouyer }
742 1.2.2.2 bouyer
743 1.2.2.2 bouyer #ifdef KLD_MODULE
744 1.2.2.2 bouyer
745 1.2.2.2 bouyer static struct altqsw wfq_sw =
746 1.2.2.2 bouyer {"wfq", wfqopen, wfqclose, wfqioctl};
747 1.2.2.2 bouyer
748 1.2.2.2 bouyer ALTQ_MODULE(altq_wfq, ALTQT_WFQ, &wfq_sw);
749 1.2.2.2 bouyer
750 1.2.2.2 bouyer #endif /* KLD_MODULE */
751 1.2.2.2 bouyer
752 1.2.2.2 bouyer #endif /* ALTQ_WFQ */
753