altq_priq.c revision 1.24 1 1.24 riastrad /* $NetBSD: altq_priq.c,v 1.24 2017/07/28 13:53:17 riastradh Exp $ */
2 1.15 peter /* $KAME: altq_priq.c,v 1.13 2005/04/13 03:44:25 suz Exp $ */
3 1.1 thorpej /*
4 1.15 peter * Copyright (C) 2000-2003
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.15 peter
29 1.1 thorpej /*
30 1.1 thorpej * priority queue
31 1.1 thorpej */
32 1.5 lukem
33 1.5 lukem #include <sys/cdefs.h>
34 1.24 riastrad __KERNEL_RCSID(0, "$NetBSD: altq_priq.c,v 1.24 2017/07/28 13:53:17 riastradh Exp $");
35 1.1 thorpej
36 1.15 peter #ifdef _KERNEL_OPT
37 1.1 thorpej #include "opt_altq.h"
38 1.1 thorpej #include "opt_inet.h"
39 1.16 peter #include "pf.h"
40 1.1 thorpej #endif
41 1.1 thorpej
42 1.1 thorpej #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
43 1.1 thorpej
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/malloc.h>
46 1.1 thorpej #include <sys/mbuf.h>
47 1.1 thorpej #include <sys/socket.h>
48 1.1 thorpej #include <sys/sockio.h>
49 1.1 thorpej #include <sys/systm.h>
50 1.1 thorpej #include <sys/proc.h>
51 1.1 thorpej #include <sys/errno.h>
52 1.1 thorpej #include <sys/kernel.h>
53 1.1 thorpej #include <sys/queue.h>
54 1.12 christos #include <sys/kauth.h>
55 1.1 thorpej
56 1.1 thorpej #include <net/if.h>
57 1.15 peter #include <netinet/in.h>
58 1.1 thorpej
59 1.16 peter #if NPF > 0
60 1.15 peter #include <net/pfvar.h>
61 1.16 peter #endif
62 1.1 thorpej #include <altq/altq.h>
63 1.1 thorpej #include <altq/altq_conf.h>
64 1.1 thorpej #include <altq/altq_priq.h>
65 1.1 thorpej
66 1.1 thorpej /*
67 1.1 thorpej * function prototypes
68 1.1 thorpej */
69 1.15 peter #ifdef ALTQ3_COMPAT
70 1.15 peter static struct priq_if *priq_attach(struct ifaltq *, u_int);
71 1.22 christos static void priq_detach(struct priq_if *);
72 1.15 peter #endif
73 1.15 peter static int priq_clear_interface(struct priq_if *);
74 1.15 peter static int priq_request(struct ifaltq *, int, void *);
75 1.15 peter static void priq_purge(struct priq_if *);
76 1.15 peter static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
77 1.15 peter int);
78 1.15 peter static int priq_class_destroy(struct priq_class *);
79 1.23 knakahar static int priq_enqueue(struct ifaltq *, struct mbuf *);
80 1.15 peter static struct mbuf *priq_dequeue(struct ifaltq *, int);
81 1.15 peter
82 1.15 peter static int priq_addq(struct priq_class *, struct mbuf *);
83 1.15 peter static struct mbuf *priq_getq(struct priq_class *);
84 1.15 peter static struct mbuf *priq_pollq(struct priq_class *);
85 1.15 peter static void priq_purgeq(struct priq_class *);
86 1.15 peter
87 1.15 peter #ifdef ALTQ3_COMPAT
88 1.15 peter static int priqcmd_if_attach(struct priq_interface *);
89 1.15 peter static int priqcmd_if_detach(struct priq_interface *);
90 1.15 peter static int priqcmd_add_class(struct priq_add_class *);
91 1.15 peter static int priqcmd_delete_class(struct priq_delete_class *);
92 1.15 peter static int priqcmd_modify_class(struct priq_modify_class *);
93 1.15 peter static int priqcmd_add_filter(struct priq_add_filter *);
94 1.15 peter static int priqcmd_delete_filter(struct priq_delete_filter *);
95 1.15 peter static int priqcmd_class_stats(struct priq_class_stats *);
96 1.15 peter #endif /* ALTQ3_COMPAT */
97 1.15 peter
98 1.15 peter static void get_class_stats(struct priq_classstats *, struct priq_class *);
99 1.15 peter static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
100 1.15 peter
101 1.15 peter #ifdef ALTQ3_COMPAT
102 1.15 peter altqdev_decl(priq);
103 1.1 thorpej
104 1.1 thorpej /* pif_list keeps all priq_if's allocated. */
105 1.1 thorpej static struct priq_if *pif_list = NULL;
106 1.15 peter #endif /* ALTQ3_COMPAT */
107 1.15 peter
108 1.16 peter #if NPF > 0
109 1.15 peter int
110 1.15 peter priq_pfattach(struct pf_altq *a)
111 1.15 peter {
112 1.15 peter struct ifnet *ifp;
113 1.15 peter int s, error;
114 1.15 peter
115 1.15 peter if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
116 1.15 peter return (EINVAL);
117 1.15 peter s = splnet();
118 1.15 peter error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
119 1.15 peter priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
120 1.15 peter splx(s);
121 1.15 peter return (error);
122 1.15 peter }
123 1.1 thorpej
124 1.15 peter int
125 1.15 peter priq_add_altq(struct pf_altq *a)
126 1.1 thorpej {
127 1.15 peter struct priq_if *pif;
128 1.15 peter struct ifnet *ifp;
129 1.15 peter
130 1.15 peter if ((ifp = ifunit(a->ifname)) == NULL)
131 1.15 peter return (EINVAL);
132 1.15 peter if (!ALTQ_IS_READY(&ifp->if_snd))
133 1.15 peter return (ENODEV);
134 1.1 thorpej
135 1.10 christos pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO);
136 1.1 thorpej if (pif == NULL)
137 1.15 peter return (ENOMEM);
138 1.15 peter pif->pif_bandwidth = a->ifbandwidth;
139 1.1 thorpej pif->pif_maxpri = -1;
140 1.15 peter pif->pif_ifq = &ifp->if_snd;
141 1.15 peter
142 1.15 peter /* keep the state in pf_altq */
143 1.15 peter a->altq_disc = pif;
144 1.15 peter
145 1.15 peter return (0);
146 1.15 peter }
147 1.15 peter
148 1.15 peter int
149 1.15 peter priq_remove_altq(struct pf_altq *a)
150 1.15 peter {
151 1.15 peter struct priq_if *pif;
152 1.15 peter
153 1.15 peter if ((pif = a->altq_disc) == NULL)
154 1.15 peter return (EINVAL);
155 1.15 peter a->altq_disc = NULL;
156 1.15 peter
157 1.15 peter (void)priq_clear_interface(pif);
158 1.15 peter
159 1.15 peter free(pif, M_DEVBUF);
160 1.15 peter return (0);
161 1.15 peter }
162 1.15 peter
163 1.15 peter int
164 1.15 peter priq_add_queue(struct pf_altq *a)
165 1.15 peter {
166 1.15 peter struct priq_if *pif;
167 1.15 peter struct priq_class *cl;
168 1.15 peter
169 1.15 peter if ((pif = a->altq_disc) == NULL)
170 1.15 peter return (EINVAL);
171 1.1 thorpej
172 1.15 peter /* check parameters */
173 1.15 peter if (a->priority >= PRIQ_MAXPRI)
174 1.15 peter return (EINVAL);
175 1.15 peter if (a->qid == 0)
176 1.15 peter return (EINVAL);
177 1.15 peter if (pif->pif_classes[a->priority] != NULL)
178 1.15 peter return (EBUSY);
179 1.15 peter if (clh_to_clp(pif, a->qid) != NULL)
180 1.15 peter return (EBUSY);
181 1.15 peter
182 1.15 peter cl = priq_class_create(pif, a->priority, a->qlimit,
183 1.15 peter a->pq_u.priq_opts.flags, a->qid);
184 1.15 peter if (cl == NULL)
185 1.15 peter return (ENOMEM);
186 1.1 thorpej
187 1.15 peter return (0);
188 1.1 thorpej }
189 1.1 thorpej
190 1.15 peter int
191 1.15 peter priq_remove_queue(struct pf_altq *a)
192 1.15 peter {
193 1.1 thorpej struct priq_if *pif;
194 1.15 peter struct priq_class *cl;
195 1.15 peter
196 1.15 peter if ((pif = a->altq_disc) == NULL)
197 1.15 peter return (EINVAL);
198 1.15 peter
199 1.15 peter if ((cl = clh_to_clp(pif, a->qid)) == NULL)
200 1.15 peter return (EINVAL);
201 1.15 peter
202 1.15 peter return (priq_class_destroy(cl));
203 1.15 peter }
204 1.15 peter
205 1.15 peter int
206 1.15 peter priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
207 1.1 thorpej {
208 1.15 peter struct priq_if *pif;
209 1.15 peter struct priq_class *cl;
210 1.15 peter struct priq_classstats stats;
211 1.15 peter int error = 0;
212 1.15 peter
213 1.15 peter if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
214 1.15 peter return (EBADF);
215 1.15 peter
216 1.15 peter if ((cl = clh_to_clp(pif, a->qid)) == NULL)
217 1.15 peter return (EINVAL);
218 1.1 thorpej
219 1.15 peter if (*nbytes < sizeof(stats))
220 1.15 peter return (EINVAL);
221 1.8 perry
222 1.24 riastrad memset(&stats, 0, sizeof(stats));
223 1.15 peter get_class_stats(&stats, cl);
224 1.1 thorpej
225 1.19 christos if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0)
226 1.15 peter return (error);
227 1.15 peter *nbytes = sizeof(stats);
228 1.1 thorpej return (0);
229 1.1 thorpej }
230 1.16 peter #endif /* NPF > 0 */
231 1.1 thorpej
232 1.1 thorpej /*
233 1.1 thorpej * bring the interface back to the initial state by discarding
234 1.1 thorpej * all the filters and classes.
235 1.1 thorpej */
236 1.1 thorpej static int
237 1.15 peter priq_clear_interface(struct priq_if *pif)
238 1.1 thorpej {
239 1.1 thorpej struct priq_class *cl;
240 1.1 thorpej int pri;
241 1.1 thorpej
242 1.15 peter #ifdef ALTQ3_CLFIER_COMPAT
243 1.1 thorpej /* free the filters for this interface */
244 1.1 thorpej acc_discard_filters(&pif->pif_classifier, NULL, 1);
245 1.15 peter #endif
246 1.1 thorpej
247 1.1 thorpej /* clear out the classes */
248 1.1 thorpej for (pri = 0; pri <= pif->pif_maxpri; pri++)
249 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL)
250 1.1 thorpej priq_class_destroy(cl);
251 1.1 thorpej
252 1.1 thorpej return (0);
253 1.1 thorpej }
254 1.1 thorpej
255 1.1 thorpej static int
256 1.18 christos priq_request(struct ifaltq *ifq, int req, void *arg)
257 1.1 thorpej {
258 1.1 thorpej struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
259 1.1 thorpej
260 1.1 thorpej switch (req) {
261 1.1 thorpej case ALTRQ_PURGE:
262 1.1 thorpej priq_purge(pif);
263 1.1 thorpej break;
264 1.1 thorpej }
265 1.1 thorpej return (0);
266 1.1 thorpej }
267 1.1 thorpej
268 1.1 thorpej /* discard all the queued packets on the interface */
269 1.1 thorpej static void
270 1.15 peter priq_purge(struct priq_if *pif)
271 1.1 thorpej {
272 1.1 thorpej struct priq_class *cl;
273 1.1 thorpej int pri;
274 1.1 thorpej
275 1.1 thorpej for (pri = 0; pri <= pif->pif_maxpri; pri++) {
276 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
277 1.1 thorpej priq_purgeq(cl);
278 1.1 thorpej }
279 1.1 thorpej if (ALTQ_IS_ENABLED(pif->pif_ifq))
280 1.1 thorpej pif->pif_ifq->ifq_len = 0;
281 1.1 thorpej }
282 1.1 thorpej
283 1.1 thorpej static struct priq_class *
284 1.15 peter priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
285 1.1 thorpej {
286 1.1 thorpej struct priq_class *cl;
287 1.1 thorpej int s;
288 1.1 thorpej
289 1.1 thorpej #ifndef ALTQ_RED
290 1.1 thorpej if (flags & PRCF_RED) {
291 1.15 peter #ifdef ALTQ_DEBUG
292 1.1 thorpej printf("priq_class_create: RED not configured for PRIQ!\n");
293 1.15 peter #endif
294 1.1 thorpej return (NULL);
295 1.1 thorpej }
296 1.1 thorpej #endif
297 1.1 thorpej
298 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL) {
299 1.1 thorpej /* modify the class instead of creating a new one */
300 1.3 thorpej s = splnet();
301 1.1 thorpej if (!qempty(cl->cl_q))
302 1.1 thorpej priq_purgeq(cl);
303 1.1 thorpej splx(s);
304 1.1 thorpej #ifdef ALTQ_RIO
305 1.1 thorpej if (q_is_rio(cl->cl_q))
306 1.1 thorpej rio_destroy((rio_t *)cl->cl_red);
307 1.1 thorpej #endif
308 1.1 thorpej #ifdef ALTQ_RED
309 1.1 thorpej if (q_is_red(cl->cl_q))
310 1.1 thorpej red_destroy(cl->cl_red);
311 1.1 thorpej #endif
312 1.1 thorpej } else {
313 1.10 christos cl = malloc(sizeof(struct priq_class), M_DEVBUF,
314 1.10 christos M_WAITOK|M_ZERO);
315 1.1 thorpej if (cl == NULL)
316 1.1 thorpej return (NULL);
317 1.1 thorpej
318 1.10 christos cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
319 1.10 christos M_WAITOK|M_ZERO);
320 1.1 thorpej if (cl->cl_q == NULL)
321 1.1 thorpej goto err_ret;
322 1.1 thorpej }
323 1.1 thorpej
324 1.1 thorpej pif->pif_classes[pri] = cl;
325 1.1 thorpej if (flags & PRCF_DEFAULTCLASS)
326 1.1 thorpej pif->pif_default = cl;
327 1.1 thorpej if (qlimit == 0)
328 1.1 thorpej qlimit = 50; /* use default */
329 1.1 thorpej qlimit(cl->cl_q) = qlimit;
330 1.1 thorpej qtype(cl->cl_q) = Q_DROPTAIL;
331 1.1 thorpej qlen(cl->cl_q) = 0;
332 1.1 thorpej cl->cl_flags = flags;
333 1.1 thorpej cl->cl_pri = pri;
334 1.1 thorpej if (pri > pif->pif_maxpri)
335 1.1 thorpej pif->pif_maxpri = pri;
336 1.1 thorpej cl->cl_pif = pif;
337 1.15 peter cl->cl_handle = qid;
338 1.1 thorpej
339 1.1 thorpej #ifdef ALTQ_RED
340 1.1 thorpej if (flags & (PRCF_RED|PRCF_RIO)) {
341 1.1 thorpej int red_flags, red_pkttime;
342 1.1 thorpej
343 1.1 thorpej red_flags = 0;
344 1.1 thorpej if (flags & PRCF_ECN)
345 1.1 thorpej red_flags |= REDF_ECN;
346 1.1 thorpej #ifdef ALTQ_RIO
347 1.1 thorpej if (flags & PRCF_CLEARDSCP)
348 1.1 thorpej red_flags |= RIOF_CLEARDSCP;
349 1.1 thorpej #endif
350 1.4 itojun if (pif->pif_bandwidth < 8)
351 1.1 thorpej red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
352 1.1 thorpej else
353 1.1 thorpej red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
354 1.1 thorpej * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
355 1.1 thorpej #ifdef ALTQ_RIO
356 1.1 thorpej if (flags & PRCF_RIO) {
357 1.1 thorpej cl->cl_red = (red_t *)rio_alloc(0, NULL,
358 1.1 thorpej red_flags, red_pkttime);
359 1.1 thorpej if (cl->cl_red != NULL)
360 1.1 thorpej qtype(cl->cl_q) = Q_RIO;
361 1.8 perry } else
362 1.1 thorpej #endif
363 1.1 thorpej if (flags & PRCF_RED) {
364 1.15 peter cl->cl_red = red_alloc(0, 0,
365 1.15 peter qlimit(cl->cl_q) * 10/100,
366 1.15 peter qlimit(cl->cl_q) * 30/100,
367 1.15 peter red_flags, red_pkttime);
368 1.1 thorpej if (cl->cl_red != NULL)
369 1.1 thorpej qtype(cl->cl_q) = Q_RED;
370 1.1 thorpej }
371 1.1 thorpej }
372 1.1 thorpej #endif /* ALTQ_RED */
373 1.1 thorpej
374 1.1 thorpej return (cl);
375 1.1 thorpej
376 1.1 thorpej err_ret:
377 1.1 thorpej if (cl->cl_red != NULL) {
378 1.1 thorpej #ifdef ALTQ_RIO
379 1.1 thorpej if (q_is_rio(cl->cl_q))
380 1.1 thorpej rio_destroy((rio_t *)cl->cl_red);
381 1.1 thorpej #endif
382 1.1 thorpej #ifdef ALTQ_RED
383 1.1 thorpej if (q_is_red(cl->cl_q))
384 1.1 thorpej red_destroy(cl->cl_red);
385 1.1 thorpej #endif
386 1.1 thorpej }
387 1.1 thorpej if (cl->cl_q != NULL)
388 1.10 christos free(cl->cl_q, M_DEVBUF);
389 1.10 christos free(cl, M_DEVBUF);
390 1.1 thorpej return (NULL);
391 1.1 thorpej }
392 1.1 thorpej
393 1.1 thorpej static int
394 1.15 peter priq_class_destroy(struct priq_class *cl)
395 1.1 thorpej {
396 1.1 thorpej struct priq_if *pif;
397 1.1 thorpej int s, pri;
398 1.1 thorpej
399 1.3 thorpej s = splnet();
400 1.1 thorpej
401 1.15 peter #ifdef ALTQ3_CLFIER_COMPAT
402 1.1 thorpej /* delete filters referencing to this class */
403 1.1 thorpej acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
404 1.15 peter #endif
405 1.1 thorpej
406 1.1 thorpej if (!qempty(cl->cl_q))
407 1.1 thorpej priq_purgeq(cl);
408 1.1 thorpej
409 1.1 thorpej pif = cl->cl_pif;
410 1.1 thorpej pif->pif_classes[cl->cl_pri] = NULL;
411 1.1 thorpej if (pif->pif_maxpri == cl->cl_pri) {
412 1.1 thorpej for (pri = cl->cl_pri; pri >= 0; pri--)
413 1.1 thorpej if (pif->pif_classes[pri] != NULL) {
414 1.1 thorpej pif->pif_maxpri = pri;
415 1.1 thorpej break;
416 1.1 thorpej }
417 1.1 thorpej if (pri < 0)
418 1.1 thorpej pif->pif_maxpri = -1;
419 1.1 thorpej }
420 1.1 thorpej splx(s);
421 1.1 thorpej
422 1.1 thorpej if (cl->cl_red != NULL) {
423 1.1 thorpej #ifdef ALTQ_RIO
424 1.1 thorpej if (q_is_rio(cl->cl_q))
425 1.1 thorpej rio_destroy((rio_t *)cl->cl_red);
426 1.1 thorpej #endif
427 1.1 thorpej #ifdef ALTQ_RED
428 1.1 thorpej if (q_is_red(cl->cl_q))
429 1.1 thorpej red_destroy(cl->cl_red);
430 1.1 thorpej #endif
431 1.1 thorpej }
432 1.10 christos free(cl->cl_q, M_DEVBUF);
433 1.10 christos free(cl, M_DEVBUF);
434 1.1 thorpej return (0);
435 1.1 thorpej }
436 1.1 thorpej
437 1.1 thorpej /*
438 1.1 thorpej * priq_enqueue is an enqueue function to be registered to
439 1.1 thorpej * (*altq_enqueue) in struct ifaltq.
440 1.1 thorpej */
441 1.8 perry static int
442 1.23 knakahar priq_enqueue(struct ifaltq *ifq, struct mbuf *m)
443 1.1 thorpej {
444 1.23 knakahar struct altq_pktattr pktattr;
445 1.1 thorpej struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
446 1.1 thorpej struct priq_class *cl;
447 1.15 peter struct m_tag *t;
448 1.1 thorpej int len;
449 1.1 thorpej
450 1.1 thorpej /* grab class set by classifier */
451 1.15 peter if ((m->m_flags & M_PKTHDR) == 0) {
452 1.15 peter /* should not happen */
453 1.15 peter printf("altq: packet for %s does not have pkthdr\n",
454 1.15 peter ifq->altq_ifp->if_xname);
455 1.15 peter m_freem(m);
456 1.15 peter return (ENOBUFS);
457 1.15 peter }
458 1.15 peter cl = NULL;
459 1.20 yamt if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID, NULL)) != NULL)
460 1.15 peter cl = clh_to_clp(pif, ((struct altq_tag *)(t+1))->qid);
461 1.15 peter #ifdef ALTQ3_COMPAT
462 1.23 knakahar else if (ifq->altq_flags & ALTQF_CLASSIFY)
463 1.23 knakahar cl = m->m_pkthdr.pattr_class;
464 1.15 peter #endif
465 1.15 peter if (cl == NULL) {
466 1.1 thorpej cl = pif->pif_default;
467 1.15 peter if (cl == NULL) {
468 1.15 peter m_freem(m);
469 1.15 peter return (ENOBUFS);
470 1.15 peter }
471 1.15 peter }
472 1.15 peter #ifdef ALTQ3_COMPAT
473 1.23 knakahar if (m->m_pkthdr.pattr_af != AF_UNSPEC) {
474 1.23 knakahar pktattr.pattr_class = m->m_pkthdr.pattr_class;
475 1.23 knakahar pktattr.pattr_af = m->m_pkthdr.pattr_af;
476 1.23 knakahar pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr;
477 1.23 knakahar
478 1.23 knakahar cl->cl_pktattr = &pktattr; /* save proto hdr used by ECN */
479 1.23 knakahar } else
480 1.15 peter #endif
481 1.15 peter cl->cl_pktattr = NULL;
482 1.1 thorpej len = m_pktlen(m);
483 1.1 thorpej if (priq_addq(cl, m) != 0) {
484 1.1 thorpej /* drop occurred. mbuf was freed in priq_addq. */
485 1.1 thorpej PKTCNTR_ADD(&cl->cl_dropcnt, len);
486 1.1 thorpej return (ENOBUFS);
487 1.1 thorpej }
488 1.1 thorpej IFQ_INC_LEN(ifq);
489 1.1 thorpej
490 1.1 thorpej /* successfully queued. */
491 1.1 thorpej return (0);
492 1.1 thorpej }
493 1.1 thorpej
494 1.1 thorpej /*
495 1.1 thorpej * priq_dequeue is a dequeue function to be registered to
496 1.1 thorpej * (*altq_dequeue) in struct ifaltq.
497 1.1 thorpej *
498 1.1 thorpej * note: ALTDQ_POLL returns the next packet without removing the packet
499 1.1 thorpej * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
500 1.1 thorpej * ALTDQ_REMOVE must return the same packet if called immediately
501 1.1 thorpej * after ALTDQ_POLL.
502 1.1 thorpej */
503 1.1 thorpej static struct mbuf *
504 1.15 peter priq_dequeue(struct ifaltq *ifq, int op)
505 1.1 thorpej {
506 1.1 thorpej struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
507 1.1 thorpej struct priq_class *cl;
508 1.1 thorpej struct mbuf *m;
509 1.1 thorpej int pri;
510 1.1 thorpej
511 1.1 thorpej if (IFQ_IS_EMPTY(ifq))
512 1.1 thorpej /* no packet in the queue */
513 1.1 thorpej return (NULL);
514 1.1 thorpej
515 1.1 thorpej for (pri = pif->pif_maxpri; pri >= 0; pri--) {
516 1.1 thorpej if ((cl = pif->pif_classes[pri]) != NULL &&
517 1.1 thorpej !qempty(cl->cl_q)) {
518 1.1 thorpej if (op == ALTDQ_POLL)
519 1.1 thorpej return (priq_pollq(cl));
520 1.1 thorpej
521 1.1 thorpej m = priq_getq(cl);
522 1.1 thorpej if (m != NULL) {
523 1.1 thorpej IFQ_DEC_LEN(ifq);
524 1.1 thorpej if (qempty(cl->cl_q))
525 1.1 thorpej cl->cl_period++;
526 1.1 thorpej PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
527 1.1 thorpej }
528 1.1 thorpej return (m);
529 1.1 thorpej }
530 1.1 thorpej }
531 1.1 thorpej return (NULL);
532 1.1 thorpej }
533 1.1 thorpej
534 1.1 thorpej static int
535 1.15 peter priq_addq(struct priq_class *cl, struct mbuf *m)
536 1.1 thorpej {
537 1.1 thorpej
538 1.1 thorpej #ifdef ALTQ_RIO
539 1.1 thorpej if (q_is_rio(cl->cl_q))
540 1.1 thorpej return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
541 1.1 thorpej cl->cl_pktattr);
542 1.1 thorpej #endif
543 1.1 thorpej #ifdef ALTQ_RED
544 1.1 thorpej if (q_is_red(cl->cl_q))
545 1.1 thorpej return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
546 1.1 thorpej #endif
547 1.1 thorpej if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
548 1.1 thorpej m_freem(m);
549 1.1 thorpej return (-1);
550 1.1 thorpej }
551 1.8 perry
552 1.1 thorpej if (cl->cl_flags & PRCF_CLEARDSCP)
553 1.1 thorpej write_dsfield(m, cl->cl_pktattr, 0);
554 1.1 thorpej
555 1.1 thorpej _addq(cl->cl_q, m);
556 1.1 thorpej
557 1.1 thorpej return (0);
558 1.1 thorpej }
559 1.1 thorpej
560 1.1 thorpej static struct mbuf *
561 1.15 peter priq_getq(struct priq_class *cl)
562 1.1 thorpej {
563 1.1 thorpej #ifdef ALTQ_RIO
564 1.1 thorpej if (q_is_rio(cl->cl_q))
565 1.1 thorpej return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
566 1.1 thorpej #endif
567 1.1 thorpej #ifdef ALTQ_RED
568 1.1 thorpej if (q_is_red(cl->cl_q))
569 1.1 thorpej return red_getq(cl->cl_red, cl->cl_q);
570 1.1 thorpej #endif
571 1.1 thorpej return _getq(cl->cl_q);
572 1.1 thorpej }
573 1.1 thorpej
574 1.1 thorpej static struct mbuf *
575 1.21 dsl priq_pollq(struct priq_class *cl)
576 1.1 thorpej {
577 1.1 thorpej return qhead(cl->cl_q);
578 1.1 thorpej }
579 1.1 thorpej
580 1.1 thorpej static void
581 1.15 peter priq_purgeq(struct priq_class *cl)
582 1.1 thorpej {
583 1.1 thorpej struct mbuf *m;
584 1.1 thorpej
585 1.1 thorpej if (qempty(cl->cl_q))
586 1.1 thorpej return;
587 1.1 thorpej
588 1.1 thorpej while ((m = _getq(cl->cl_q)) != NULL) {
589 1.1 thorpej PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
590 1.1 thorpej m_freem(m);
591 1.1 thorpej }
592 1.1 thorpej ASSERT(qlen(cl->cl_q) == 0);
593 1.1 thorpej }
594 1.1 thorpej
595 1.15 peter static void
596 1.15 peter get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
597 1.15 peter {
598 1.15 peter sp->class_handle = cl->cl_handle;
599 1.15 peter sp->qlength = qlen(cl->cl_q);
600 1.15 peter sp->qlimit = qlimit(cl->cl_q);
601 1.15 peter sp->period = cl->cl_period;
602 1.15 peter sp->xmitcnt = cl->cl_xmitcnt;
603 1.15 peter sp->dropcnt = cl->cl_dropcnt;
604 1.15 peter
605 1.15 peter sp->qtype = qtype(cl->cl_q);
606 1.15 peter #ifdef ALTQ_RED
607 1.15 peter if (q_is_red(cl->cl_q))
608 1.15 peter red_getstats(cl->cl_red, &sp->red[0]);
609 1.15 peter #endif
610 1.15 peter #ifdef ALTQ_RIO
611 1.15 peter if (q_is_rio(cl->cl_q))
612 1.15 peter rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
613 1.15 peter #endif
614 1.15 peter
615 1.15 peter }
616 1.15 peter
617 1.15 peter /* convert a class handle to the corresponding class pointer */
618 1.15 peter static struct priq_class *
619 1.15 peter clh_to_clp(struct priq_if *pif, u_int32_t chandle)
620 1.15 peter {
621 1.15 peter struct priq_class *cl;
622 1.15 peter int idx;
623 1.15 peter
624 1.15 peter if (chandle == 0)
625 1.15 peter return (NULL);
626 1.15 peter
627 1.15 peter for (idx = pif->pif_maxpri; idx >= 0; idx--)
628 1.15 peter if ((cl = pif->pif_classes[idx]) != NULL &&
629 1.15 peter cl->cl_handle == chandle)
630 1.15 peter return (cl);
631 1.15 peter
632 1.15 peter return (NULL);
633 1.15 peter }
634 1.15 peter
635 1.15 peter
636 1.15 peter #ifdef ALTQ3_COMPAT
637 1.15 peter
638 1.15 peter static struct priq_if *
639 1.15 peter priq_attach(struct ifaltq *ifq, u_int bandwidth)
640 1.15 peter {
641 1.15 peter struct priq_if *pif;
642 1.15 peter
643 1.15 peter pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO);
644 1.15 peter if (pif == NULL)
645 1.15 peter return (NULL);
646 1.15 peter pif->pif_bandwidth = bandwidth;
647 1.15 peter pif->pif_maxpri = -1;
648 1.15 peter pif->pif_ifq = ifq;
649 1.15 peter
650 1.15 peter /* add this state to the priq list */
651 1.15 peter pif->pif_next = pif_list;
652 1.15 peter pif_list = pif;
653 1.15 peter
654 1.15 peter return (pif);
655 1.15 peter }
656 1.15 peter
657 1.22 christos static void
658 1.15 peter priq_detach(struct priq_if *pif)
659 1.15 peter {
660 1.15 peter (void)priq_clear_interface(pif);
661 1.15 peter
662 1.15 peter /* remove this interface from the pif list */
663 1.15 peter if (pif_list == pif)
664 1.15 peter pif_list = pif->pif_next;
665 1.15 peter else {
666 1.15 peter struct priq_if *p;
667 1.15 peter
668 1.15 peter for (p = pif_list; p != NULL; p = p->pif_next)
669 1.15 peter if (p->pif_next == pif) {
670 1.15 peter p->pif_next = pif->pif_next;
671 1.15 peter break;
672 1.15 peter }
673 1.15 peter ASSERT(p != NULL);
674 1.15 peter }
675 1.15 peter
676 1.15 peter free(pif, M_DEVBUF);
677 1.15 peter }
678 1.15 peter
679 1.1 thorpej /*
680 1.1 thorpej * priq device interface
681 1.1 thorpej */
682 1.1 thorpej int
683 1.18 christos priqopen(dev_t dev, int flag, int fmt,
684 1.18 christos struct lwp *l)
685 1.1 thorpej {
686 1.1 thorpej /* everything will be done when the queueing scheme is attached. */
687 1.1 thorpej return 0;
688 1.1 thorpej }
689 1.1 thorpej
690 1.1 thorpej int
691 1.18 christos priqclose(dev_t dev, int flag, int fmt,
692 1.18 christos struct lwp *l)
693 1.1 thorpej {
694 1.1 thorpej struct priq_if *pif;
695 1.1 thorpej
696 1.1 thorpej while ((pif = pif_list) != NULL) {
697 1.1 thorpej /* destroy all */
698 1.1 thorpej if (ALTQ_IS_ENABLED(pif->pif_ifq))
699 1.1 thorpej altq_disable(pif->pif_ifq);
700 1.1 thorpej
701 1.22 christos int error = altq_detach(pif->pif_ifq);
702 1.22 christos switch (error) {
703 1.22 christos case 0:
704 1.22 christos case ENXIO: /* already disabled */
705 1.22 christos break;
706 1.22 christos default:
707 1.22 christos return error;
708 1.22 christos }
709 1.22 christos priq_detach(pif);
710 1.1 thorpej }
711 1.1 thorpej
712 1.22 christos return 0;
713 1.1 thorpej }
714 1.1 thorpej
715 1.1 thorpej int
716 1.19 christos priqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
717 1.14 christos struct lwp *l)
718 1.1 thorpej {
719 1.1 thorpej struct priq_if *pif;
720 1.1 thorpej struct priq_interface *ifacep;
721 1.1 thorpej int error = 0;
722 1.1 thorpej
723 1.1 thorpej /* check super-user privilege */
724 1.1 thorpej switch (cmd) {
725 1.1 thorpej case PRIQ_GETSTATS:
726 1.1 thorpej break;
727 1.1 thorpej default:
728 1.1 thorpej #if (__FreeBSD_version > 400000)
729 1.1 thorpej if ((error = suser(p)) != 0)
730 1.1 thorpej return (error);
731 1.1 thorpej #else
732 1.17 elad if ((error = kauth_authorize_network(l->l_cred,
733 1.17 elad KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_PRIQ, NULL,
734 1.17 elad NULL, NULL)) != 0)
735 1.1 thorpej return (error);
736 1.1 thorpej #endif
737 1.1 thorpej break;
738 1.1 thorpej }
739 1.8 perry
740 1.1 thorpej switch (cmd) {
741 1.1 thorpej
742 1.1 thorpej case PRIQ_IF_ATTACH:
743 1.1 thorpej error = priqcmd_if_attach((struct priq_interface *)addr);
744 1.1 thorpej break;
745 1.1 thorpej
746 1.1 thorpej case PRIQ_IF_DETACH:
747 1.1 thorpej error = priqcmd_if_detach((struct priq_interface *)addr);
748 1.1 thorpej break;
749 1.1 thorpej
750 1.1 thorpej case PRIQ_ENABLE:
751 1.1 thorpej case PRIQ_DISABLE:
752 1.1 thorpej case PRIQ_CLEAR:
753 1.1 thorpej ifacep = (struct priq_interface *)addr;
754 1.1 thorpej if ((pif = altq_lookup(ifacep->ifname,
755 1.1 thorpej ALTQT_PRIQ)) == NULL) {
756 1.1 thorpej error = EBADF;
757 1.1 thorpej break;
758 1.1 thorpej }
759 1.1 thorpej
760 1.1 thorpej switch (cmd) {
761 1.1 thorpej case PRIQ_ENABLE:
762 1.1 thorpej if (pif->pif_default == NULL) {
763 1.15 peter #ifdef ALTQ_DEBUG
764 1.1 thorpej printf("priq: no default class\n");
765 1.1 thorpej #endif
766 1.1 thorpej error = EINVAL;
767 1.1 thorpej break;
768 1.1 thorpej }
769 1.1 thorpej error = altq_enable(pif->pif_ifq);
770 1.1 thorpej break;
771 1.1 thorpej
772 1.1 thorpej case PRIQ_DISABLE:
773 1.1 thorpej error = altq_disable(pif->pif_ifq);
774 1.1 thorpej break;
775 1.1 thorpej
776 1.1 thorpej case PRIQ_CLEAR:
777 1.1 thorpej priq_clear_interface(pif);
778 1.1 thorpej break;
779 1.1 thorpej }
780 1.1 thorpej break;
781 1.1 thorpej
782 1.1 thorpej case PRIQ_ADD_CLASS:
783 1.1 thorpej error = priqcmd_add_class((struct priq_add_class *)addr);
784 1.1 thorpej break;
785 1.1 thorpej
786 1.1 thorpej case PRIQ_DEL_CLASS:
787 1.1 thorpej error = priqcmd_delete_class((struct priq_delete_class *)addr);
788 1.1 thorpej break;
789 1.1 thorpej
790 1.1 thorpej case PRIQ_MOD_CLASS:
791 1.1 thorpej error = priqcmd_modify_class((struct priq_modify_class *)addr);
792 1.1 thorpej break;
793 1.1 thorpej
794 1.1 thorpej case PRIQ_ADD_FILTER:
795 1.1 thorpej error = priqcmd_add_filter((struct priq_add_filter *)addr);
796 1.1 thorpej break;
797 1.1 thorpej
798 1.1 thorpej case PRIQ_DEL_FILTER:
799 1.1 thorpej error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
800 1.1 thorpej break;
801 1.1 thorpej
802 1.1 thorpej case PRIQ_GETSTATS:
803 1.1 thorpej error = priqcmd_class_stats((struct priq_class_stats *)addr);
804 1.1 thorpej break;
805 1.1 thorpej
806 1.1 thorpej default:
807 1.1 thorpej error = EINVAL;
808 1.1 thorpej break;
809 1.1 thorpej }
810 1.1 thorpej return error;
811 1.1 thorpej }
812 1.1 thorpej
813 1.1 thorpej static int
814 1.15 peter priqcmd_if_attach(struct priq_interface *ap)
815 1.1 thorpej {
816 1.1 thorpej struct priq_if *pif;
817 1.1 thorpej struct ifnet *ifp;
818 1.1 thorpej int error;
819 1.8 perry
820 1.1 thorpej if ((ifp = ifunit(ap->ifname)) == NULL)
821 1.1 thorpej return (ENXIO);
822 1.1 thorpej
823 1.1 thorpej if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
824 1.1 thorpej return (ENOMEM);
825 1.8 perry
826 1.1 thorpej /*
827 1.1 thorpej * set PRIQ to this ifnet structure.
828 1.1 thorpej */
829 1.1 thorpej if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
830 1.1 thorpej priq_enqueue, priq_dequeue, priq_request,
831 1.1 thorpej &pif->pif_classifier, acc_classify)) != 0)
832 1.22 christos priq_detach(pif);
833 1.1 thorpej
834 1.1 thorpej return (error);
835 1.1 thorpej }
836 1.1 thorpej
837 1.1 thorpej static int
838 1.15 peter priqcmd_if_detach(struct priq_interface *ap)
839 1.1 thorpej {
840 1.1 thorpej struct priq_if *pif;
841 1.1 thorpej int error;
842 1.1 thorpej
843 1.1 thorpej if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
844 1.1 thorpej return (EBADF);
845 1.8 perry
846 1.1 thorpej if (ALTQ_IS_ENABLED(pif->pif_ifq))
847 1.1 thorpej altq_disable(pif->pif_ifq);
848 1.1 thorpej
849 1.1 thorpej if ((error = altq_detach(pif->pif_ifq)))
850 1.1 thorpej return (error);
851 1.1 thorpej
852 1.22 christos priq_detach(pif);
853 1.22 christos return 0;
854 1.1 thorpej }
855 1.1 thorpej
856 1.1 thorpej static int
857 1.15 peter priqcmd_add_class(struct priq_add_class *ap)
858 1.1 thorpej {
859 1.1 thorpej struct priq_if *pif;
860 1.1 thorpej struct priq_class *cl;
861 1.15 peter int qid;
862 1.1 thorpej
863 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
864 1.1 thorpej return (EBADF);
865 1.1 thorpej
866 1.1 thorpej if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
867 1.1 thorpej return (EINVAL);
868 1.15 peter if (pif->pif_classes[ap->pri] != NULL)
869 1.15 peter return (EBUSY);
870 1.1 thorpej
871 1.15 peter qid = ap->pri + 1;
872 1.1 thorpej if ((cl = priq_class_create(pif, ap->pri,
873 1.15 peter ap->qlimit, ap->flags, qid)) == NULL)
874 1.1 thorpej return (ENOMEM);
875 1.8 perry
876 1.1 thorpej /* return a class handle to the user */
877 1.15 peter ap->class_handle = cl->cl_handle;
878 1.15 peter
879 1.1 thorpej return (0);
880 1.1 thorpej }
881 1.1 thorpej
882 1.1 thorpej static int
883 1.15 peter priqcmd_delete_class(struct priq_delete_class *ap)
884 1.1 thorpej {
885 1.1 thorpej struct priq_if *pif;
886 1.1 thorpej struct priq_class *cl;
887 1.1 thorpej
888 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
889 1.1 thorpej return (EBADF);
890 1.1 thorpej
891 1.1 thorpej if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
892 1.1 thorpej return (EINVAL);
893 1.8 perry
894 1.1 thorpej return priq_class_destroy(cl);
895 1.1 thorpej }
896 1.1 thorpej
897 1.1 thorpej static int
898 1.15 peter priqcmd_modify_class(struct priq_modify_class *ap)
899 1.1 thorpej {
900 1.1 thorpej struct priq_if *pif;
901 1.1 thorpej struct priq_class *cl;
902 1.1 thorpej
903 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
904 1.1 thorpej return (EBADF);
905 1.1 thorpej
906 1.1 thorpej if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
907 1.1 thorpej return (EINVAL);
908 1.1 thorpej
909 1.1 thorpej if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
910 1.1 thorpej return (EINVAL);
911 1.1 thorpej
912 1.1 thorpej /*
913 1.1 thorpej * if priority is changed, move the class to the new priority
914 1.1 thorpej */
915 1.1 thorpej if (pif->pif_classes[ap->pri] != cl) {
916 1.1 thorpej if (pif->pif_classes[ap->pri] != NULL)
917 1.1 thorpej return (EEXIST);
918 1.1 thorpej pif->pif_classes[cl->cl_pri] = NULL;
919 1.1 thorpej pif->pif_classes[ap->pri] = cl;
920 1.1 thorpej cl->cl_pri = ap->pri;
921 1.1 thorpej }
922 1.1 thorpej
923 1.1 thorpej /* call priq_class_create to change class parameters */
924 1.1 thorpej if ((cl = priq_class_create(pif, ap->pri,
925 1.15 peter ap->qlimit, ap->flags, ap->class_handle)) == NULL)
926 1.1 thorpej return (ENOMEM);
927 1.1 thorpej return 0;
928 1.1 thorpej }
929 1.1 thorpej
930 1.1 thorpej static int
931 1.15 peter priqcmd_add_filter(struct priq_add_filter *ap)
932 1.1 thorpej {
933 1.1 thorpej struct priq_if *pif;
934 1.1 thorpej struct priq_class *cl;
935 1.1 thorpej
936 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
937 1.1 thorpej return (EBADF);
938 1.1 thorpej
939 1.1 thorpej if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
940 1.1 thorpej return (EINVAL);
941 1.1 thorpej
942 1.1 thorpej return acc_add_filter(&pif->pif_classifier, &ap->filter,
943 1.1 thorpej cl, &ap->filter_handle);
944 1.1 thorpej }
945 1.1 thorpej
946 1.1 thorpej static int
947 1.15 peter priqcmd_delete_filter(struct priq_delete_filter *ap)
948 1.1 thorpej {
949 1.1 thorpej struct priq_if *pif;
950 1.1 thorpej
951 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
952 1.1 thorpej return (EBADF);
953 1.1 thorpej
954 1.1 thorpej return acc_delete_filter(&pif->pif_classifier,
955 1.1 thorpej ap->filter_handle);
956 1.1 thorpej }
957 1.1 thorpej
958 1.1 thorpej static int
959 1.15 peter priqcmd_class_stats(struct priq_class_stats *ap)
960 1.1 thorpej {
961 1.1 thorpej struct priq_if *pif;
962 1.1 thorpej struct priq_class *cl;
963 1.15 peter struct priq_classstats stats, *usp;
964 1.1 thorpej int pri, error;
965 1.8 perry
966 1.1 thorpej if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
967 1.1 thorpej return (EBADF);
968 1.1 thorpej
969 1.1 thorpej ap->maxpri = pif->pif_maxpri;
970 1.1 thorpej
971 1.1 thorpej /* then, read the next N classes in the tree */
972 1.1 thorpej usp = ap->stats;
973 1.1 thorpej for (pri = 0; pri <= pif->pif_maxpri; pri++) {
974 1.1 thorpej cl = pif->pif_classes[pri];
975 1.1 thorpej if (cl != NULL)
976 1.1 thorpej get_class_stats(&stats, cl);
977 1.1 thorpej else
978 1.15 peter memset(&stats, 0, sizeof(stats));
979 1.19 christos if ((error = copyout((void *)&stats, (void *)usp++,
980 1.1 thorpej sizeof(stats))) != 0)
981 1.1 thorpej return (error);
982 1.1 thorpej }
983 1.1 thorpej return (0);
984 1.1 thorpej }
985 1.1 thorpej
986 1.1 thorpej #ifdef KLD_MODULE
987 1.1 thorpej
988 1.1 thorpej static struct altqsw priq_sw =
989 1.1 thorpej {"priq", priqopen, priqclose, priqioctl};
990 1.1 thorpej
991 1.1 thorpej ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
992 1.15 peter MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
993 1.15 peter MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
994 1.1 thorpej
995 1.1 thorpej #endif /* KLD_MODULE */
996 1.1 thorpej
997 1.15 peter #endif /* ALTQ3_COMPAT */
998 1.1 thorpej #endif /* ALTQ_PRIQ */
999