altq_priq.c revision 1.9 1 1.9 christos /* $NetBSD: altq_priq.c,v 1.9 2005/12/11 12:16:03 christos Exp $ */
2 1.4 itojun /* $KAME: altq_priq.c,v 1.2 2001/10/26 04:56:11 kjc Exp $ */
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (C) 2000
5 1.1 thorpej * Sony Computer Science Laboratories Inc. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej *
16 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 thorpej * SUCH DAMAGE.
27 1.1 thorpej */
28 1.1 thorpej /*
29 1.1 thorpej * priority queue
30 1.1 thorpej */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.9 christos __KERNEL_RCSID(0, "$NetBSD: altq_priq.c,v 1.9 2005/12/11 12:16:03 christos Exp $");
34 1.1 thorpej
35 1.1 thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
36 1.1 thorpej #include "opt_altq.h"
37 1.1 thorpej #if (__FreeBSD__ != 2)
38 1.1 thorpej #include "opt_inet.h"
39 1.1 thorpej #ifdef __FreeBSD__
40 1.1 thorpej #include "opt_inet6.h"
41 1.1 thorpej #endif
42 1.1 thorpej #endif
43 1.1 thorpej #endif /* __FreeBSD__ || __NetBSD__ */
44 1.1 thorpej
45 1.1 thorpej #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
46 1.1 thorpej
47 1.1 thorpej #include <sys/param.h>
48 1.1 thorpej #include <sys/malloc.h>
49 1.1 thorpej #include <sys/mbuf.h>
50 1.1 thorpej #include <sys/socket.h>
51 1.1 thorpej #include <sys/sockio.h>
52 1.1 thorpej #include <sys/systm.h>
53 1.1 thorpej #include <sys/proc.h>
54 1.1 thorpej #include <sys/errno.h>
55 1.1 thorpej #include <sys/kernel.h>
56 1.1 thorpej #include <sys/queue.h>
57 1.1 thorpej
58 1.1 thorpej #include <net/if.h>
59 1.1 thorpej #include <net/if_types.h>
60 1.1 thorpej
61 1.1 thorpej #include <altq/altq.h>
62 1.1 thorpej #include <altq/altq_conf.h>
63 1.1 thorpej #include <altq/altq_priq.h>
64 1.1 thorpej
65 1.1 thorpej /*
66 1.1 thorpej * function prototypes
67 1.1 thorpej */
68 1.1 thorpej static struct priq_if *priq_attach __P((struct ifaltq *, u_int));
69 1.1 thorpej static int priq_detach __P((struct priq_if *));
70 1.1 thorpej static int priq_clear_interface __P((struct priq_if *));
71 1.1 thorpej static int priq_request __P((struct ifaltq *, int, void *));
72 1.1 thorpej static void priq_purge __P((struct priq_if *));
73 1.1 thorpej static struct priq_class *priq_class_create __P((struct priq_if *,
74 1.1 thorpej int, int, int));
75 1.1 thorpej static int priq_class_destroy __P((struct priq_class *));
76 1.1 thorpej static int priq_enqueue __P((struct ifaltq *, struct mbuf *,
77 1.1 thorpej struct altq_pktattr *));
78 1.1 thorpej static struct mbuf *priq_dequeue __P((struct ifaltq *, int));
79 1.1 thorpej
80 1.1 thorpej static int priq_addq __P((struct priq_class *, struct mbuf *));
81 1.1 thorpej static struct mbuf *priq_getq __P((struct priq_class *));
82 1.1 thorpej static struct mbuf *priq_pollq __P((struct priq_class *));
83 1.1 thorpej static void priq_purgeq __P((struct priq_class *));
84 1.1 thorpej
85 1.9 christos int priqopen __P((dev_t, int, int, struct lwp *));
86 1.9 christos int priqclose __P((dev_t, int, int, struct lwp *));
87 1.9 christos int priqioctl __P((dev_t, ioctlcmd_t, caddr_t, int, struct lwp *));
88 1.1 thorpej static int priqcmd_if_attach __P((struct priq_interface *));
89 1.1 thorpej static int priqcmd_if_detach __P((struct priq_interface *));
90 1.1 thorpej static int priqcmd_add_class __P((struct priq_add_class *));
91 1.1 thorpej static int priqcmd_delete_class __P((struct priq_delete_class *));
92 1.1 thorpej static int priqcmd_modify_class __P((struct priq_modify_class *));
93 1.1 thorpej static int priqcmd_add_filter __P((struct priq_add_filter *));
94 1.1 thorpej static int priqcmd_delete_filter __P((struct priq_delete_filter *));
95 1.1 thorpej static int priqcmd_class_stats __P((struct priq_class_stats *));
96 1.6 christos static void get_class_stats __P((struct priq_basic_class_stats *,
97 1.6 christos struct priq_class *));
98 1.1 thorpej static struct priq_class *clh_to_clp __P((struct priq_if *, u_long));
99 1.1 thorpej static u_long clp_to_clh __P((struct priq_class *));
100 1.1 thorpej
101 1.1 thorpej /* pif_list keeps all priq_if's allocated. */
102 1.1 thorpej static struct priq_if *pif_list = NULL;
103 1.1 thorpej
104 1.1 thorpej static struct priq_if *
105 1.1 thorpej priq_attach(ifq, bandwidth)
106 1.1 thorpej struct ifaltq *ifq;
107 1.1 thorpej u_int bandwidth;
108 1.1 thorpej {
109 1.1 thorpej struct priq_if *pif;
110 1.1 thorpej
111 1.1 thorpej MALLOC(pif, struct priq_if *, sizeof(struct priq_if),
112 1.1 thorpej M_DEVBUF, M_WAITOK);
113 1.1 thorpej if (pif == NULL)
114 1.1 thorpej return (NULL);
115 1.7 christos (void)memset(pif, 0, sizeof(struct priq_if));
116 1.1 thorpej pif->pif_bandwidth = bandwidth;
117 1.1 thorpej pif->pif_maxpri = -1;
118 1.1 thorpej pif->pif_ifq = ifq;
119 1.1 thorpej
120 1.1 thorpej /* add this state to the priq list */
121 1.1 thorpej pif->pif_next = pif_list;
122 1.1 thorpej pif_list = pif;
123 1.1 thorpej
124 1.1 thorpej return (pif);
125 1.1 thorpej }
126 1.1 thorpej
127 1.1 thorpej static int
128 1.1 thorpej priq_detach(pif)
129 1.1 thorpej struct priq_if *pif;
130 1.1 thorpej {
131 1.1 thorpej (void)priq_clear_interface(pif);
132 1.1 thorpej
133 1.1 thorpej /* remove this interface from the pif list */
134 1.1 thorpej if (pif_list == pif)
135 1.1 thorpej pif_list = pif->pif_next;
136 1.1 thorpej else {
137 1.1 thorpej struct priq_if *p;
138 1.8 perry
139 1.1 thorpej for (p = pif_list; p != NULL; p = p->pif_next)
140 1.1 thorpej if (p->pif_next == pif) {
141 1.1 thorpej p->pif_next = pif->pif_next;
142 1.1 thorpej break;
143 1.1 thorpej }
144 1.1 thorpej ASSERT(p != NULL);
145 1.1 thorpej }
146 1.1 thorpej
147 1.1 thorpej FREE(pif, M_DEVBUF);
148 1.1 thorpej return (0);
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej /*
152 1.1 thorpej * bring the interface back to the initial state by discarding
153 1.1 thorpej * all the filters and classes.
154 1.1 thorpej */
155 1.1 thorpej static int
156 1.1 thorpej priq_clear_interface(pif)
157 1.1 thorpej struct priq_if *pif;
158 1.1 thorpej {
159 1.1 thorpej struct priq_class *cl;
160 1.1 thorpej int pri;
161 1.1 thorpej
162 1.1 thorpej /* free the filters for this interface */
163 1.1 thorpej acc_discard_filters(&pif->pif_classifier, NULL, 1);
164 1.1 thorpej
165 1.1 thorpej /* clear out the classes */
166 1.1 thorpej for (pri = 0; pri <= pif->pif_maxpri; pri++)
167 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL)
168 1.1 thorpej priq_class_destroy(cl);
169 1.1 thorpej
170 1.1 thorpej return (0);
171 1.1 thorpej }
172 1.1 thorpej
173 1.1 thorpej static int
174 1.1 thorpej priq_request(ifq, req, arg)
175 1.1 thorpej struct ifaltq *ifq;
176 1.1 thorpej int req;
177 1.1 thorpej void *arg;
178 1.1 thorpej {
179 1.1 thorpej struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
180 1.1 thorpej
181 1.1 thorpej switch (req) {
182 1.1 thorpej case ALTRQ_PURGE:
183 1.1 thorpej priq_purge(pif);
184 1.1 thorpej break;
185 1.1 thorpej }
186 1.1 thorpej return (0);
187 1.1 thorpej }
188 1.1 thorpej
189 1.1 thorpej /* discard all the queued packets on the interface */
190 1.1 thorpej static void
191 1.1 thorpej priq_purge(pif)
192 1.1 thorpej struct priq_if *pif;
193 1.1 thorpej {
194 1.1 thorpej struct priq_class *cl;
195 1.1 thorpej int pri;
196 1.1 thorpej
197 1.1 thorpej for (pri = 0; pri <= pif->pif_maxpri; pri++) {
198 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
199 1.1 thorpej priq_purgeq(cl);
200 1.1 thorpej }
201 1.1 thorpej if (ALTQ_IS_ENABLED(pif->pif_ifq))
202 1.1 thorpej pif->pif_ifq->ifq_len = 0;
203 1.1 thorpej }
204 1.1 thorpej
205 1.1 thorpej static struct priq_class *
206 1.1 thorpej priq_class_create(pif, pri, qlimit, flags)
207 1.1 thorpej struct priq_if *pif;
208 1.1 thorpej int pri, qlimit, flags;
209 1.1 thorpej {
210 1.1 thorpej struct priq_class *cl;
211 1.1 thorpej int s;
212 1.1 thorpej
213 1.1 thorpej #ifndef ALTQ_RED
214 1.1 thorpej if (flags & PRCF_RED) {
215 1.1 thorpej printf("priq_class_create: RED not configured for PRIQ!\n");
216 1.1 thorpej return (NULL);
217 1.1 thorpej }
218 1.1 thorpej #endif
219 1.1 thorpej
220 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL) {
221 1.1 thorpej /* modify the class instead of creating a new one */
222 1.3 thorpej s = splnet();
223 1.1 thorpej if (!qempty(cl->cl_q))
224 1.1 thorpej priq_purgeq(cl);
225 1.1 thorpej splx(s);
226 1.1 thorpej #ifdef ALTQ_RIO
227 1.1 thorpej if (q_is_rio(cl->cl_q))
228 1.1 thorpej rio_destroy((rio_t *)cl->cl_red);
229 1.1 thorpej #endif
230 1.1 thorpej #ifdef ALTQ_RED
231 1.1 thorpej if (q_is_red(cl->cl_q))
232 1.1 thorpej red_destroy(cl->cl_red);
233 1.1 thorpej #endif
234 1.1 thorpej } else {
235 1.1 thorpej MALLOC(cl, struct priq_class *, sizeof(struct priq_class),
236 1.1 thorpej M_DEVBUF, M_WAITOK);
237 1.1 thorpej if (cl == NULL)
238 1.1 thorpej return (NULL);
239 1.7 christos (void)memset(cl, 0, sizeof(struct priq_class));
240 1.1 thorpej
241 1.1 thorpej MALLOC(cl->cl_q, class_queue_t *, sizeof(class_queue_t),
242 1.1 thorpej M_DEVBUF, M_WAITOK);
243 1.1 thorpej if (cl->cl_q == NULL)
244 1.1 thorpej goto err_ret;
245 1.7 christos (void)memset(cl->cl_q, 0, sizeof(class_queue_t));
246 1.1 thorpej }
247 1.1 thorpej
248 1.1 thorpej pif->pif_classes[pri] = cl;
249 1.1 thorpej if (flags & PRCF_DEFAULTCLASS)
250 1.1 thorpej pif->pif_default = cl;
251 1.1 thorpej if (qlimit == 0)
252 1.1 thorpej qlimit = 50; /* use default */
253 1.1 thorpej qlimit(cl->cl_q) = qlimit;
254 1.1 thorpej qtype(cl->cl_q) = Q_DROPTAIL;
255 1.1 thorpej qlen(cl->cl_q) = 0;
256 1.1 thorpej cl->cl_flags = flags;
257 1.1 thorpej cl->cl_pri = pri;
258 1.1 thorpej if (pri > pif->pif_maxpri)
259 1.1 thorpej pif->pif_maxpri = pri;
260 1.1 thorpej cl->cl_pif = pif;
261 1.1 thorpej cl->cl_handle = (u_long)cl; /* XXX: just a pointer to this class */
262 1.1 thorpej
263 1.1 thorpej #ifdef ALTQ_RED
264 1.1 thorpej if (flags & (PRCF_RED|PRCF_RIO)) {
265 1.1 thorpej int red_flags, red_pkttime;
266 1.1 thorpej
267 1.1 thorpej red_flags = 0;
268 1.1 thorpej if (flags & PRCF_ECN)
269 1.1 thorpej red_flags |= REDF_ECN;
270 1.1 thorpej #ifdef ALTQ_RIO
271 1.1 thorpej if (flags & PRCF_CLEARDSCP)
272 1.1 thorpej red_flags |= RIOF_CLEARDSCP;
273 1.1 thorpej #endif
274 1.4 itojun if (pif->pif_bandwidth < 8)
275 1.1 thorpej red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
276 1.1 thorpej else
277 1.1 thorpej red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
278 1.1 thorpej * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
279 1.1 thorpej #ifdef ALTQ_RIO
280 1.1 thorpej if (flags & PRCF_RIO) {
281 1.1 thorpej cl->cl_red = (red_t *)rio_alloc(0, NULL,
282 1.1 thorpej red_flags, red_pkttime);
283 1.1 thorpej if (cl->cl_red != NULL)
284 1.1 thorpej qtype(cl->cl_q) = Q_RIO;
285 1.8 perry } else
286 1.1 thorpej #endif
287 1.1 thorpej if (flags & PRCF_RED) {
288 1.1 thorpej cl->cl_red = red_alloc(0, 0, 0, 0,
289 1.1 thorpej red_flags, red_pkttime);
290 1.1 thorpej if (cl->cl_red != NULL)
291 1.1 thorpej qtype(cl->cl_q) = Q_RED;
292 1.1 thorpej }
293 1.1 thorpej }
294 1.1 thorpej #endif /* ALTQ_RED */
295 1.1 thorpej
296 1.1 thorpej return (cl);
297 1.1 thorpej
298 1.1 thorpej err_ret:
299 1.1 thorpej if (cl->cl_red != NULL) {
300 1.1 thorpej #ifdef ALTQ_RIO
301 1.1 thorpej if (q_is_rio(cl->cl_q))
302 1.1 thorpej rio_destroy((rio_t *)cl->cl_red);
303 1.1 thorpej #endif
304 1.1 thorpej #ifdef ALTQ_RED
305 1.1 thorpej if (q_is_red(cl->cl_q))
306 1.1 thorpej red_destroy(cl->cl_red);
307 1.1 thorpej #endif
308 1.1 thorpej }
309 1.1 thorpej if (cl->cl_q != NULL)
310 1.1 thorpej FREE(cl->cl_q, M_DEVBUF);
311 1.1 thorpej FREE(cl, M_DEVBUF);
312 1.1 thorpej return (NULL);
313 1.1 thorpej }
314 1.1 thorpej
315 1.1 thorpej static int
316 1.1 thorpej priq_class_destroy(cl)
317 1.1 thorpej struct priq_class *cl;
318 1.1 thorpej {
319 1.1 thorpej struct priq_if *pif;
320 1.1 thorpej int s, pri;
321 1.1 thorpej
322 1.3 thorpej s = splnet();
323 1.1 thorpej
324 1.1 thorpej /* delete filters referencing to this class */
325 1.1 thorpej acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
326 1.1 thorpej
327 1.1 thorpej if (!qempty(cl->cl_q))
328 1.1 thorpej priq_purgeq(cl);
329 1.1 thorpej
330 1.1 thorpej pif = cl->cl_pif;
331 1.1 thorpej pif->pif_classes[cl->cl_pri] = NULL;
332 1.1 thorpej if (pif->pif_maxpri == cl->cl_pri) {
333 1.1 thorpej for (pri = cl->cl_pri; pri >= 0; pri--)
334 1.1 thorpej if (pif->pif_classes[pri] != NULL) {
335 1.1 thorpej pif->pif_maxpri = pri;
336 1.1 thorpej break;
337 1.1 thorpej }
338 1.1 thorpej if (pri < 0)
339 1.1 thorpej pif->pif_maxpri = -1;
340 1.1 thorpej }
341 1.1 thorpej splx(s);
342 1.1 thorpej
343 1.1 thorpej if (cl->cl_red != NULL) {
344 1.1 thorpej #ifdef ALTQ_RIO
345 1.1 thorpej if (q_is_rio(cl->cl_q))
346 1.1 thorpej rio_destroy((rio_t *)cl->cl_red);
347 1.1 thorpej #endif
348 1.1 thorpej #ifdef ALTQ_RED
349 1.1 thorpej if (q_is_red(cl->cl_q))
350 1.1 thorpej red_destroy(cl->cl_red);
351 1.1 thorpej #endif
352 1.1 thorpej }
353 1.1 thorpej FREE(cl->cl_q, M_DEVBUF);
354 1.1 thorpej FREE(cl, M_DEVBUF);
355 1.1 thorpej return (0);
356 1.1 thorpej }
357 1.1 thorpej
358 1.1 thorpej /*
359 1.1 thorpej * priq_enqueue is an enqueue function to be registered to
360 1.1 thorpej * (*altq_enqueue) in struct ifaltq.
361 1.1 thorpej */
362 1.8 perry static int
363 1.1 thorpej priq_enqueue(ifq, m, pktattr)
364 1.1 thorpej struct ifaltq *ifq;
365 1.1 thorpej struct mbuf *m;
366 1.1 thorpej struct altq_pktattr *pktattr;
367 1.1 thorpej {
368 1.1 thorpej struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
369 1.1 thorpej struct priq_class *cl;
370 1.1 thorpej int len;
371 1.1 thorpej
372 1.1 thorpej /* grab class set by classifier */
373 1.1 thorpej if (pktattr == NULL || (cl = pktattr->pattr_class) == NULL)
374 1.1 thorpej cl = pif->pif_default;
375 1.1 thorpej cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
376 1.1 thorpej
377 1.1 thorpej len = m_pktlen(m);
378 1.1 thorpej if (priq_addq(cl, m) != 0) {
379 1.1 thorpej /* drop occurred. mbuf was freed in priq_addq. */
380 1.1 thorpej PKTCNTR_ADD(&cl->cl_dropcnt, len);
381 1.1 thorpej return (ENOBUFS);
382 1.1 thorpej }
383 1.1 thorpej IFQ_INC_LEN(ifq);
384 1.1 thorpej
385 1.1 thorpej /* successfully queued. */
386 1.1 thorpej return (0);
387 1.1 thorpej }
388 1.1 thorpej
389 1.1 thorpej /*
390 1.1 thorpej * priq_dequeue is a dequeue function to be registered to
391 1.1 thorpej * (*altq_dequeue) in struct ifaltq.
392 1.1 thorpej *
393 1.1 thorpej * note: ALTDQ_POLL returns the next packet without removing the packet
394 1.1 thorpej * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
395 1.1 thorpej * ALTDQ_REMOVE must return the same packet if called immediately
396 1.1 thorpej * after ALTDQ_POLL.
397 1.1 thorpej */
398 1.1 thorpej static struct mbuf *
399 1.1 thorpej priq_dequeue(ifq, op)
400 1.1 thorpej struct ifaltq *ifq;
401 1.1 thorpej int op;
402 1.1 thorpej {
403 1.1 thorpej struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
404 1.1 thorpej struct priq_class *cl;
405 1.1 thorpej struct mbuf *m;
406 1.1 thorpej int pri;
407 1.1 thorpej
408 1.1 thorpej if (IFQ_IS_EMPTY(ifq))
409 1.1 thorpej /* no packet in the queue */
410 1.1 thorpej return (NULL);
411 1.1 thorpej
412 1.1 thorpej for (pri = pif->pif_maxpri; pri >= 0; pri--) {
413 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL &&
414 1.1 thorpej !qempty(cl->cl_q)) {
415 1.1 thorpej if (op == ALTDQ_POLL)
416 1.1 thorpej return (priq_pollq(cl));
417 1.1 thorpej
418 1.1 thorpej m = priq_getq(cl);
419 1.1 thorpej if (m != NULL) {
420 1.1 thorpej IFQ_DEC_LEN(ifq);
421 1.1 thorpej if (qempty(cl->cl_q))
422 1.1 thorpej cl->cl_period++;
423 1.1 thorpej PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
424 1.1 thorpej }
425 1.1 thorpej return (m);
426 1.1 thorpej }
427 1.1 thorpej }
428 1.1 thorpej return (NULL);
429 1.1 thorpej }
430 1.1 thorpej
431 1.1 thorpej static int
432 1.1 thorpej priq_addq(cl, m)
433 1.1 thorpej struct priq_class *cl;
434 1.1 thorpej struct mbuf *m;
435 1.1 thorpej {
436 1.1 thorpej
437 1.1 thorpej #ifdef ALTQ_RIO
438 1.1 thorpej if (q_is_rio(cl->cl_q))
439 1.1 thorpej return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
440 1.1 thorpej cl->cl_pktattr);
441 1.1 thorpej #endif
442 1.1 thorpej #ifdef ALTQ_RED
443 1.1 thorpej if (q_is_red(cl->cl_q))
444 1.1 thorpej return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
445 1.1 thorpej #endif
446 1.1 thorpej if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
447 1.1 thorpej m_freem(m);
448 1.1 thorpej return (-1);
449 1.1 thorpej }
450 1.8 perry
451 1.1 thorpej if (cl->cl_flags & PRCF_CLEARDSCP)
452 1.1 thorpej write_dsfield(m, cl->cl_pktattr, 0);
453 1.1 thorpej
454 1.1 thorpej _addq(cl->cl_q, m);
455 1.1 thorpej
456 1.1 thorpej return (0);
457 1.1 thorpej }
458 1.1 thorpej
459 1.1 thorpej static struct mbuf *
460 1.1 thorpej priq_getq(cl)
461 1.1 thorpej struct priq_class *cl;
462 1.1 thorpej {
463 1.1 thorpej #ifdef ALTQ_RIO
464 1.1 thorpej if (q_is_rio(cl->cl_q))
465 1.1 thorpej return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
466 1.1 thorpej #endif
467 1.1 thorpej #ifdef ALTQ_RED
468 1.1 thorpej if (q_is_red(cl->cl_q))
469 1.1 thorpej return red_getq(cl->cl_red, cl->cl_q);
470 1.1 thorpej #endif
471 1.1 thorpej return _getq(cl->cl_q);
472 1.1 thorpej }
473 1.1 thorpej
474 1.1 thorpej static struct mbuf *
475 1.1 thorpej priq_pollq(cl)
476 1.1 thorpej struct priq_class *cl;
477 1.1 thorpej {
478 1.1 thorpej return qhead(cl->cl_q);
479 1.1 thorpej }
480 1.1 thorpej
481 1.1 thorpej static void
482 1.1 thorpej priq_purgeq(cl)
483 1.1 thorpej struct priq_class *cl;
484 1.1 thorpej {
485 1.1 thorpej struct mbuf *m;
486 1.1 thorpej
487 1.1 thorpej if (qempty(cl->cl_q))
488 1.1 thorpej return;
489 1.1 thorpej
490 1.1 thorpej while ((m = _getq(cl->cl_q)) != NULL) {
491 1.1 thorpej PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
492 1.1 thorpej m_freem(m);
493 1.1 thorpej }
494 1.1 thorpej ASSERT(qlen(cl->cl_q) == 0);
495 1.1 thorpej }
496 1.1 thorpej
497 1.1 thorpej /*
498 1.1 thorpej * priq device interface
499 1.1 thorpej */
500 1.1 thorpej int
501 1.9 christos priqopen(dev, flag, fmt, l)
502 1.1 thorpej dev_t dev;
503 1.1 thorpej int flag, fmt;
504 1.9 christos struct lwp *l;
505 1.1 thorpej {
506 1.1 thorpej /* everything will be done when the queueing scheme is attached. */
507 1.1 thorpej return 0;
508 1.1 thorpej }
509 1.1 thorpej
510 1.1 thorpej int
511 1.9 christos priqclose(dev, flag, fmt, l)
512 1.1 thorpej dev_t dev;
513 1.1 thorpej int flag, fmt;
514 1.9 christos struct lwp *l;
515 1.1 thorpej {
516 1.1 thorpej struct priq_if *pif;
517 1.1 thorpej int err, error = 0;
518 1.1 thorpej
519 1.1 thorpej while ((pif = pif_list) != NULL) {
520 1.1 thorpej /* destroy all */
521 1.1 thorpej if (ALTQ_IS_ENABLED(pif->pif_ifq))
522 1.1 thorpej altq_disable(pif->pif_ifq);
523 1.1 thorpej
524 1.1 thorpej err = altq_detach(pif->pif_ifq);
525 1.1 thorpej if (err == 0)
526 1.1 thorpej err = priq_detach(pif);
527 1.1 thorpej if (err != 0 && error == 0)
528 1.1 thorpej error = err;
529 1.1 thorpej }
530 1.1 thorpej
531 1.1 thorpej return error;
532 1.1 thorpej }
533 1.1 thorpej
534 1.1 thorpej int
535 1.9 christos priqioctl(dev, cmd, addr, flag, l)
536 1.1 thorpej dev_t dev;
537 1.1 thorpej ioctlcmd_t cmd;
538 1.1 thorpej caddr_t addr;
539 1.1 thorpej int flag;
540 1.9 christos struct lwp *l;
541 1.1 thorpej {
542 1.1 thorpej struct priq_if *pif;
543 1.1 thorpej struct priq_interface *ifacep;
544 1.9 christos struct proc *p = l->l_proc;
545 1.1 thorpej int error = 0;
546 1.1 thorpej
547 1.1 thorpej /* check super-user privilege */
548 1.1 thorpej switch (cmd) {
549 1.1 thorpej case PRIQ_GETSTATS:
550 1.1 thorpej break;
551 1.1 thorpej default:
552 1.1 thorpej #if (__FreeBSD_version > 400000)
553 1.1 thorpej if ((error = suser(p)) != 0)
554 1.1 thorpej return (error);
555 1.1 thorpej #else
556 1.1 thorpej if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
557 1.1 thorpej return (error);
558 1.1 thorpej #endif
559 1.1 thorpej break;
560 1.1 thorpej }
561 1.8 perry
562 1.1 thorpej switch (cmd) {
563 1.1 thorpej
564 1.1 thorpej case PRIQ_IF_ATTACH:
565 1.1 thorpej error = priqcmd_if_attach((struct priq_interface *)addr);
566 1.1 thorpej break;
567 1.1 thorpej
568 1.1 thorpej case PRIQ_IF_DETACH:
569 1.1 thorpej error = priqcmd_if_detach((struct priq_interface *)addr);
570 1.1 thorpej break;
571 1.1 thorpej
572 1.1 thorpej case PRIQ_ENABLE:
573 1.1 thorpej case PRIQ_DISABLE:
574 1.1 thorpej case PRIQ_CLEAR:
575 1.1 thorpej ifacep = (struct priq_interface *)addr;
576 1.1 thorpej if ((pif = altq_lookup(ifacep->ifname,
577 1.1 thorpej ALTQT_PRIQ)) == NULL) {
578 1.1 thorpej error = EBADF;
579 1.1 thorpej break;
580 1.1 thorpej }
581 1.1 thorpej
582 1.1 thorpej switch (cmd) {
583 1.1 thorpej case PRIQ_ENABLE:
584 1.1 thorpej if (pif->pif_default == NULL) {
585 1.1 thorpej #if 1
586 1.1 thorpej printf("priq: no default class\n");
587 1.1 thorpej #endif
588 1.1 thorpej error = EINVAL;
589 1.1 thorpej break;
590 1.1 thorpej }
591 1.1 thorpej error = altq_enable(pif->pif_ifq);
592 1.1 thorpej break;
593 1.1 thorpej
594 1.1 thorpej case PRIQ_DISABLE:
595 1.1 thorpej error = altq_disable(pif->pif_ifq);
596 1.1 thorpej break;
597 1.1 thorpej
598 1.1 thorpej case PRIQ_CLEAR:
599 1.1 thorpej priq_clear_interface(pif);
600 1.1 thorpej break;
601 1.1 thorpej }
602 1.1 thorpej break;
603 1.1 thorpej
604 1.1 thorpej case PRIQ_ADD_CLASS:
605 1.1 thorpej error = priqcmd_add_class((struct priq_add_class *)addr);
606 1.1 thorpej break;
607 1.1 thorpej
608 1.1 thorpej case PRIQ_DEL_CLASS:
609 1.1 thorpej error = priqcmd_delete_class((struct priq_delete_class *)addr);
610 1.1 thorpej break;
611 1.1 thorpej
612 1.1 thorpej case PRIQ_MOD_CLASS:
613 1.1 thorpej error = priqcmd_modify_class((struct priq_modify_class *)addr);
614 1.1 thorpej break;
615 1.1 thorpej
616 1.1 thorpej case PRIQ_ADD_FILTER:
617 1.1 thorpej error = priqcmd_add_filter((struct priq_add_filter *)addr);
618 1.1 thorpej break;
619 1.1 thorpej
620 1.1 thorpej case PRIQ_DEL_FILTER:
621 1.1 thorpej error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
622 1.1 thorpej break;
623 1.1 thorpej
624 1.1 thorpej case PRIQ_GETSTATS:
625 1.1 thorpej error = priqcmd_class_stats((struct priq_class_stats *)addr);
626 1.1 thorpej break;
627 1.1 thorpej
628 1.1 thorpej default:
629 1.1 thorpej error = EINVAL;
630 1.1 thorpej break;
631 1.1 thorpej }
632 1.1 thorpej return error;
633 1.1 thorpej }
634 1.1 thorpej
635 1.1 thorpej static int
636 1.1 thorpej priqcmd_if_attach(ap)
637 1.1 thorpej struct priq_interface *ap;
638 1.1 thorpej {
639 1.1 thorpej struct priq_if *pif;
640 1.1 thorpej struct ifnet *ifp;
641 1.1 thorpej int error;
642 1.8 perry
643 1.1 thorpej if ((ifp = ifunit(ap->ifname)) == NULL)
644 1.1 thorpej return (ENXIO);
645 1.1 thorpej
646 1.1 thorpej if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
647 1.1 thorpej return (ENOMEM);
648 1.8 perry
649 1.1 thorpej /*
650 1.1 thorpej * set PRIQ to this ifnet structure.
651 1.1 thorpej */
652 1.1 thorpej if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
653 1.1 thorpej priq_enqueue, priq_dequeue, priq_request,
654 1.1 thorpej &pif->pif_classifier, acc_classify)) != 0)
655 1.1 thorpej (void)priq_detach(pif);
656 1.1 thorpej
657 1.1 thorpej return (error);
658 1.1 thorpej }
659 1.1 thorpej
660 1.1 thorpej static int
661 1.1 thorpej priqcmd_if_detach(ap)
662 1.1 thorpej struct priq_interface *ap;
663 1.1 thorpej {
664 1.1 thorpej struct priq_if *pif;
665 1.1 thorpej int error;
666 1.1 thorpej
667 1.1 thorpej if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
668 1.1 thorpej return (EBADF);
669 1.8 perry
670 1.1 thorpej if (ALTQ_IS_ENABLED(pif->pif_ifq))
671 1.1 thorpej altq_disable(pif->pif_ifq);
672 1.1 thorpej
673 1.1 thorpej if ((error = altq_detach(pif->pif_ifq)))
674 1.1 thorpej return (error);
675 1.1 thorpej
676 1.1 thorpej return priq_detach(pif);
677 1.1 thorpej }
678 1.1 thorpej
679 1.1 thorpej static int
680 1.1 thorpej priqcmd_add_class(ap)
681 1.1 thorpej struct priq_add_class *ap;
682 1.1 thorpej {
683 1.1 thorpej struct priq_if *pif;
684 1.1 thorpej struct priq_class *cl;
685 1.1 thorpej
686 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
687 1.1 thorpej return (EBADF);
688 1.1 thorpej
689 1.1 thorpej if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
690 1.1 thorpej return (EINVAL);
691 1.1 thorpej
692 1.1 thorpej if ((cl = priq_class_create(pif, ap->pri,
693 1.1 thorpej ap->qlimit, ap->flags)) == NULL)
694 1.1 thorpej return (ENOMEM);
695 1.8 perry
696 1.1 thorpej /* return a class handle to the user */
697 1.1 thorpej ap->class_handle = clp_to_clh(cl);
698 1.1 thorpej return (0);
699 1.1 thorpej }
700 1.1 thorpej
701 1.1 thorpej static int
702 1.1 thorpej priqcmd_delete_class(ap)
703 1.1 thorpej struct priq_delete_class *ap;
704 1.1 thorpej {
705 1.1 thorpej struct priq_if *pif;
706 1.1 thorpej struct priq_class *cl;
707 1.1 thorpej
708 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
709 1.1 thorpej return (EBADF);
710 1.1 thorpej
711 1.1 thorpej if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
712 1.1 thorpej return (EINVAL);
713 1.8 perry
714 1.1 thorpej return priq_class_destroy(cl);
715 1.1 thorpej }
716 1.1 thorpej
717 1.1 thorpej static int
718 1.1 thorpej priqcmd_modify_class(ap)
719 1.1 thorpej struct priq_modify_class *ap;
720 1.1 thorpej {
721 1.1 thorpej struct priq_if *pif;
722 1.1 thorpej struct priq_class *cl;
723 1.1 thorpej
724 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
725 1.1 thorpej return (EBADF);
726 1.1 thorpej
727 1.1 thorpej if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
728 1.1 thorpej return (EINVAL);
729 1.1 thorpej
730 1.1 thorpej if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
731 1.1 thorpej return (EINVAL);
732 1.1 thorpej
733 1.1 thorpej /*
734 1.1 thorpej * if priority is changed, move the class to the new priority
735 1.1 thorpej */
736 1.1 thorpej if (pif->pif_classes[ap->pri] != cl) {
737 1.1 thorpej if (pif->pif_classes[ap->pri] != NULL)
738 1.1 thorpej return (EEXIST);
739 1.1 thorpej pif->pif_classes[cl->cl_pri] = NULL;
740 1.1 thorpej pif->pif_classes[ap->pri] = cl;
741 1.1 thorpej cl->cl_pri = ap->pri;
742 1.1 thorpej }
743 1.1 thorpej
744 1.1 thorpej /* call priq_class_create to change class parameters */
745 1.1 thorpej if ((cl = priq_class_create(pif, ap->pri,
746 1.1 thorpej ap->qlimit, ap->flags)) == NULL)
747 1.1 thorpej return (ENOMEM);
748 1.1 thorpej return 0;
749 1.1 thorpej }
750 1.1 thorpej
751 1.1 thorpej static int
752 1.1 thorpej priqcmd_add_filter(ap)
753 1.1 thorpej struct priq_add_filter *ap;
754 1.1 thorpej {
755 1.1 thorpej struct priq_if *pif;
756 1.1 thorpej struct priq_class *cl;
757 1.1 thorpej
758 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
759 1.1 thorpej return (EBADF);
760 1.1 thorpej
761 1.1 thorpej if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
762 1.1 thorpej return (EINVAL);
763 1.1 thorpej
764 1.1 thorpej return acc_add_filter(&pif->pif_classifier, &ap->filter,
765 1.1 thorpej cl, &ap->filter_handle);
766 1.1 thorpej }
767 1.1 thorpej
768 1.1 thorpej static int
769 1.1 thorpej priqcmd_delete_filter(ap)
770 1.1 thorpej struct priq_delete_filter *ap;
771 1.1 thorpej {
772 1.1 thorpej struct priq_if *pif;
773 1.1 thorpej
774 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
775 1.1 thorpej return (EBADF);
776 1.1 thorpej
777 1.1 thorpej return acc_delete_filter(&pif->pif_classifier,
778 1.1 thorpej ap->filter_handle);
779 1.1 thorpej }
780 1.1 thorpej
781 1.1 thorpej static int
782 1.1 thorpej priqcmd_class_stats(ap)
783 1.1 thorpej struct priq_class_stats *ap;
784 1.1 thorpej {
785 1.1 thorpej struct priq_if *pif;
786 1.1 thorpej struct priq_class *cl;
787 1.6 christos struct priq_basic_class_stats stats, *usp;
788 1.1 thorpej int pri, error;
789 1.8 perry
790 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
791 1.1 thorpej return (EBADF);
792 1.1 thorpej
793 1.1 thorpej ap->maxpri = pif->pif_maxpri;
794 1.1 thorpej
795 1.1 thorpej /* then, read the next N classes in the tree */
796 1.1 thorpej usp = ap->stats;
797 1.1 thorpej for (pri = 0; pri <= pif->pif_maxpri; pri++) {
798 1.1 thorpej cl = pif->pif_classes[pri];
799 1.1 thorpej if (cl != NULL)
800 1.1 thorpej get_class_stats(&stats, cl);
801 1.1 thorpej else
802 1.7 christos (void)memset(&stats, 0, sizeof(stats));
803 1.1 thorpej if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
804 1.1 thorpej sizeof(stats))) != 0)
805 1.1 thorpej return (error);
806 1.1 thorpej }
807 1.1 thorpej return (0);
808 1.1 thorpej }
809 1.1 thorpej
810 1.1 thorpej static void get_class_stats(sp, cl)
811 1.6 christos struct priq_basic_class_stats *sp;
812 1.1 thorpej struct priq_class *cl;
813 1.1 thorpej {
814 1.1 thorpej sp->class_handle = clp_to_clh(cl);
815 1.1 thorpej
816 1.1 thorpej sp->qlength = qlen(cl->cl_q);
817 1.1 thorpej sp->period = cl->cl_period;
818 1.1 thorpej sp->xmitcnt = cl->cl_xmitcnt;
819 1.1 thorpej sp->dropcnt = cl->cl_dropcnt;
820 1.1 thorpej
821 1.1 thorpej sp->qtype = qtype(cl->cl_q);
822 1.1 thorpej #ifdef ALTQ_RED
823 1.1 thorpej if (q_is_red(cl->cl_q))
824 1.1 thorpej red_getstats(cl->cl_red, &sp->red[0]);
825 1.1 thorpej #endif
826 1.1 thorpej #ifdef ALTQ_RIO
827 1.1 thorpej if (q_is_rio(cl->cl_q))
828 1.1 thorpej rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
829 1.1 thorpej #endif
830 1.1 thorpej
831 1.1 thorpej }
832 1.1 thorpej
833 1.1 thorpej /* convert a class handle to the corresponding class pointer */
834 1.1 thorpej static struct priq_class *
835 1.1 thorpej clh_to_clp(pif, chandle)
836 1.1 thorpej struct priq_if *pif;
837 1.1 thorpej u_long chandle;
838 1.1 thorpej {
839 1.1 thorpej struct priq_class *cl;
840 1.1 thorpej
841 1.1 thorpej cl = (struct priq_class *)chandle;
842 1.1 thorpej if (chandle != ALIGN(cl)) {
843 1.1 thorpej #if 1
844 1.1 thorpej printf("clh_to_cl: unaligned pointer %p\n", cl);
845 1.1 thorpej #endif
846 1.1 thorpej return (NULL);
847 1.1 thorpej }
848 1.1 thorpej
849 1.1 thorpej if (cl == NULL || cl->cl_handle != chandle || cl->cl_pif != pif)
850 1.1 thorpej return (NULL);
851 1.1 thorpej return (cl);
852 1.1 thorpej }
853 1.1 thorpej
854 1.1 thorpej /* convert a class pointer to the corresponding class handle */
855 1.1 thorpej static u_long
856 1.1 thorpej clp_to_clh(cl)
857 1.1 thorpej struct priq_class *cl;
858 1.1 thorpej {
859 1.1 thorpej return (cl->cl_handle);
860 1.1 thorpej }
861 1.1 thorpej
862 1.1 thorpej #ifdef KLD_MODULE
863 1.1 thorpej
864 1.1 thorpej static struct altqsw priq_sw =
865 1.1 thorpej {"priq", priqopen, priqclose, priqioctl};
866 1.1 thorpej
867 1.1 thorpej ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
868 1.1 thorpej
869 1.1 thorpej #endif /* KLD_MODULE */
870 1.1 thorpej
871 1.1 thorpej #endif /* ALTQ_PRIQ */
872