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