No empty .Rs/.Re
[netbsd-mini2440.git] / sys / altq / altq_priq.c
blob5b8d7dd38e7f1638084f4307452188722c56b336
1 /* $NetBSD: altq_priq.c,v 1.20 2008/06/18 09:06:27 yamt Exp $ */
2 /* $KAME: altq_priq.c,v 1.13 2005/04/13 03:44:25 suz Exp $ */
3 /*
4 * Copyright (C) 2000-2003
5 * Sony Computer Science Laboratories Inc. All rights reserved.
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.
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.
30 * priority queue
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: altq_priq.c,v 1.20 2008/06/18 09:06:27 yamt Exp $");
36 #ifdef _KERNEL_OPT
37 #include "opt_altq.h"
38 #include "opt_inet.h"
39 #include "pf.h"
40 #endif
42 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
44 #include <sys/param.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/errno.h>
52 #include <sys/kernel.h>
53 #include <sys/queue.h>
54 #include <sys/kauth.h>
56 #include <net/if.h>
57 #include <netinet/in.h>
59 #if NPF > 0
60 #include <net/pfvar.h>
61 #endif
62 #include <altq/altq.h>
63 #include <altq/altq_conf.h>
64 #include <altq/altq_priq.h>
67 * function prototypes
69 #ifdef ALTQ3_COMPAT
70 static struct priq_if *priq_attach(struct ifaltq *, u_int);
71 static int priq_detach(struct priq_if *);
72 #endif
73 static int priq_clear_interface(struct priq_if *);
74 static int priq_request(struct ifaltq *, int, void *);
75 static void priq_purge(struct priq_if *);
76 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
77 int);
78 static int priq_class_destroy(struct priq_class *);
79 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
80 static struct mbuf *priq_dequeue(struct ifaltq *, int);
82 static int priq_addq(struct priq_class *, struct mbuf *);
83 static struct mbuf *priq_getq(struct priq_class *);
84 static struct mbuf *priq_pollq(struct priq_class *);
85 static void priq_purgeq(struct priq_class *);
87 #ifdef ALTQ3_COMPAT
88 static int priqcmd_if_attach(struct priq_interface *);
89 static int priqcmd_if_detach(struct priq_interface *);
90 static int priqcmd_add_class(struct priq_add_class *);
91 static int priqcmd_delete_class(struct priq_delete_class *);
92 static int priqcmd_modify_class(struct priq_modify_class *);
93 static int priqcmd_add_filter(struct priq_add_filter *);
94 static int priqcmd_delete_filter(struct priq_delete_filter *);
95 static int priqcmd_class_stats(struct priq_class_stats *);
96 #endif /* ALTQ3_COMPAT */
98 static void get_class_stats(struct priq_classstats *, struct priq_class *);
99 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
101 #ifdef ALTQ3_COMPAT
102 altqdev_decl(priq);
104 /* pif_list keeps all priq_if's allocated. */
105 static struct priq_if *pif_list = NULL;
106 #endif /* ALTQ3_COMPAT */
108 #if NPF > 0
110 priq_pfattach(struct pf_altq *a)
112 struct ifnet *ifp;
113 int s, error;
115 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
116 return (EINVAL);
117 s = splnet();
118 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
119 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
120 splx(s);
121 return (error);
125 priq_add_altq(struct pf_altq *a)
127 struct priq_if *pif;
128 struct ifnet *ifp;
130 if ((ifp = ifunit(a->ifname)) == NULL)
131 return (EINVAL);
132 if (!ALTQ_IS_READY(&ifp->if_snd))
133 return (ENODEV);
135 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO);
136 if (pif == NULL)
137 return (ENOMEM);
138 pif->pif_bandwidth = a->ifbandwidth;
139 pif->pif_maxpri = -1;
140 pif->pif_ifq = &ifp->if_snd;
142 /* keep the state in pf_altq */
143 a->altq_disc = pif;
145 return (0);
149 priq_remove_altq(struct pf_altq *a)
151 struct priq_if *pif;
153 if ((pif = a->altq_disc) == NULL)
154 return (EINVAL);
155 a->altq_disc = NULL;
157 (void)priq_clear_interface(pif);
159 free(pif, M_DEVBUF);
160 return (0);
164 priq_add_queue(struct pf_altq *a)
166 struct priq_if *pif;
167 struct priq_class *cl;
169 if ((pif = a->altq_disc) == NULL)
170 return (EINVAL);
172 /* check parameters */
173 if (a->priority >= PRIQ_MAXPRI)
174 return (EINVAL);
175 if (a->qid == 0)
176 return (EINVAL);
177 if (pif->pif_classes[a->priority] != NULL)
178 return (EBUSY);
179 if (clh_to_clp(pif, a->qid) != NULL)
180 return (EBUSY);
182 cl = priq_class_create(pif, a->priority, a->qlimit,
183 a->pq_u.priq_opts.flags, a->qid);
184 if (cl == NULL)
185 return (ENOMEM);
187 return (0);
191 priq_remove_queue(struct pf_altq *a)
193 struct priq_if *pif;
194 struct priq_class *cl;
196 if ((pif = a->altq_disc) == NULL)
197 return (EINVAL);
199 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
200 return (EINVAL);
202 return (priq_class_destroy(cl));
206 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
208 struct priq_if *pif;
209 struct priq_class *cl;
210 struct priq_classstats stats;
211 int error = 0;
213 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
214 return (EBADF);
216 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
217 return (EINVAL);
219 if (*nbytes < sizeof(stats))
220 return (EINVAL);
222 get_class_stats(&stats, cl);
224 if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0)
225 return (error);
226 *nbytes = sizeof(stats);
227 return (0);
229 #endif /* NPF > 0 */
232 * bring the interface back to the initial state by discarding
233 * all the filters and classes.
235 static int
236 priq_clear_interface(struct priq_if *pif)
238 struct priq_class *cl;
239 int pri;
241 #ifdef ALTQ3_CLFIER_COMPAT
242 /* free the filters for this interface */
243 acc_discard_filters(&pif->pif_classifier, NULL, 1);
244 #endif
246 /* clear out the classes */
247 for (pri = 0; pri <= pif->pif_maxpri; pri++)
248 if ((cl = pif->pif_classes[pri]) != NULL)
249 priq_class_destroy(cl);
251 return (0);
254 static int
255 priq_request(struct ifaltq *ifq, int req, void *arg)
257 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
259 switch (req) {
260 case ALTRQ_PURGE:
261 priq_purge(pif);
262 break;
264 return (0);
267 /* discard all the queued packets on the interface */
268 static void
269 priq_purge(struct priq_if *pif)
271 struct priq_class *cl;
272 int pri;
274 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
275 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
276 priq_purgeq(cl);
278 if (ALTQ_IS_ENABLED(pif->pif_ifq))
279 pif->pif_ifq->ifq_len = 0;
282 static struct priq_class *
283 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
285 struct priq_class *cl;
286 int s;
288 #ifndef ALTQ_RED
289 if (flags & PRCF_RED) {
290 #ifdef ALTQ_DEBUG
291 printf("priq_class_create: RED not configured for PRIQ!\n");
292 #endif
293 return (NULL);
295 #endif
297 if ((cl = pif->pif_classes[pri]) != NULL) {
298 /* modify the class instead of creating a new one */
299 s = splnet();
300 if (!qempty(cl->cl_q))
301 priq_purgeq(cl);
302 splx(s);
303 #ifdef ALTQ_RIO
304 if (q_is_rio(cl->cl_q))
305 rio_destroy((rio_t *)cl->cl_red);
306 #endif
307 #ifdef ALTQ_RED
308 if (q_is_red(cl->cl_q))
309 red_destroy(cl->cl_red);
310 #endif
311 } else {
312 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
313 M_WAITOK|M_ZERO);
314 if (cl == NULL)
315 return (NULL);
317 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
318 M_WAITOK|M_ZERO);
319 if (cl->cl_q == NULL)
320 goto err_ret;
323 pif->pif_classes[pri] = cl;
324 if (flags & PRCF_DEFAULTCLASS)
325 pif->pif_default = cl;
326 if (qlimit == 0)
327 qlimit = 50; /* use default */
328 qlimit(cl->cl_q) = qlimit;
329 qtype(cl->cl_q) = Q_DROPTAIL;
330 qlen(cl->cl_q) = 0;
331 cl->cl_flags = flags;
332 cl->cl_pri = pri;
333 if (pri > pif->pif_maxpri)
334 pif->pif_maxpri = pri;
335 cl->cl_pif = pif;
336 cl->cl_handle = qid;
338 #ifdef ALTQ_RED
339 if (flags & (PRCF_RED|PRCF_RIO)) {
340 int red_flags, red_pkttime;
342 red_flags = 0;
343 if (flags & PRCF_ECN)
344 red_flags |= REDF_ECN;
345 #ifdef ALTQ_RIO
346 if (flags & PRCF_CLEARDSCP)
347 red_flags |= RIOF_CLEARDSCP;
348 #endif
349 if (pif->pif_bandwidth < 8)
350 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
351 else
352 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
353 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
354 #ifdef ALTQ_RIO
355 if (flags & PRCF_RIO) {
356 cl->cl_red = (red_t *)rio_alloc(0, NULL,
357 red_flags, red_pkttime);
358 if (cl->cl_red != NULL)
359 qtype(cl->cl_q) = Q_RIO;
360 } else
361 #endif
362 if (flags & PRCF_RED) {
363 cl->cl_red = red_alloc(0, 0,
364 qlimit(cl->cl_q) * 10/100,
365 qlimit(cl->cl_q) * 30/100,
366 red_flags, red_pkttime);
367 if (cl->cl_red != NULL)
368 qtype(cl->cl_q) = Q_RED;
371 #endif /* ALTQ_RED */
373 return (cl);
375 err_ret:
376 if (cl->cl_red != NULL) {
377 #ifdef ALTQ_RIO
378 if (q_is_rio(cl->cl_q))
379 rio_destroy((rio_t *)cl->cl_red);
380 #endif
381 #ifdef ALTQ_RED
382 if (q_is_red(cl->cl_q))
383 red_destroy(cl->cl_red);
384 #endif
386 if (cl->cl_q != NULL)
387 free(cl->cl_q, M_DEVBUF);
388 free(cl, M_DEVBUF);
389 return (NULL);
392 static int
393 priq_class_destroy(struct priq_class *cl)
395 struct priq_if *pif;
396 int s, pri;
398 s = splnet();
400 #ifdef ALTQ3_CLFIER_COMPAT
401 /* delete filters referencing to this class */
402 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
403 #endif
405 if (!qempty(cl->cl_q))
406 priq_purgeq(cl);
408 pif = cl->cl_pif;
409 pif->pif_classes[cl->cl_pri] = NULL;
410 if (pif->pif_maxpri == cl->cl_pri) {
411 for (pri = cl->cl_pri; pri >= 0; pri--)
412 if (pif->pif_classes[pri] != NULL) {
413 pif->pif_maxpri = pri;
414 break;
416 if (pri < 0)
417 pif->pif_maxpri = -1;
419 splx(s);
421 if (cl->cl_red != NULL) {
422 #ifdef ALTQ_RIO
423 if (q_is_rio(cl->cl_q))
424 rio_destroy((rio_t *)cl->cl_red);
425 #endif
426 #ifdef ALTQ_RED
427 if (q_is_red(cl->cl_q))
428 red_destroy(cl->cl_red);
429 #endif
431 free(cl->cl_q, M_DEVBUF);
432 free(cl, M_DEVBUF);
433 return (0);
437 * priq_enqueue is an enqueue function to be registered to
438 * (*altq_enqueue) in struct ifaltq.
440 static int
441 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
443 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
444 struct priq_class *cl;
445 struct m_tag *t;
446 int len;
448 /* grab class set by classifier */
449 if ((m->m_flags & M_PKTHDR) == 0) {
450 /* should not happen */
451 printf("altq: packet for %s does not have pkthdr\n",
452 ifq->altq_ifp->if_xname);
453 m_freem(m);
454 return (ENOBUFS);
456 cl = NULL;
457 if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID, NULL)) != NULL)
458 cl = clh_to_clp(pif, ((struct altq_tag *)(t+1))->qid);
459 #ifdef ALTQ3_COMPAT
460 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
461 cl = pktattr->pattr_class;
462 #endif
463 if (cl == NULL) {
464 cl = pif->pif_default;
465 if (cl == NULL) {
466 m_freem(m);
467 return (ENOBUFS);
470 #ifdef ALTQ3_COMPAT
471 if (pktattr != NULL)
472 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
473 else
474 #endif
475 cl->cl_pktattr = NULL;
476 len = m_pktlen(m);
477 if (priq_addq(cl, m) != 0) {
478 /* drop occurred. mbuf was freed in priq_addq. */
479 PKTCNTR_ADD(&cl->cl_dropcnt, len);
480 return (ENOBUFS);
482 IFQ_INC_LEN(ifq);
484 /* successfully queued. */
485 return (0);
489 * priq_dequeue is a dequeue function to be registered to
490 * (*altq_dequeue) in struct ifaltq.
492 * note: ALTDQ_POLL returns the next packet without removing the packet
493 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
494 * ALTDQ_REMOVE must return the same packet if called immediately
495 * after ALTDQ_POLL.
497 static struct mbuf *
498 priq_dequeue(struct ifaltq *ifq, int op)
500 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
501 struct priq_class *cl;
502 struct mbuf *m;
503 int pri;
505 if (IFQ_IS_EMPTY(ifq))
506 /* no packet in the queue */
507 return (NULL);
509 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
510 if ((cl = pif->pif_classes[pri]) != NULL &&
511 !qempty(cl->cl_q)) {
512 if (op == ALTDQ_POLL)
513 return (priq_pollq(cl));
515 m = priq_getq(cl);
516 if (m != NULL) {
517 IFQ_DEC_LEN(ifq);
518 if (qempty(cl->cl_q))
519 cl->cl_period++;
520 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
522 return (m);
525 return (NULL);
528 static int
529 priq_addq(struct priq_class *cl, struct mbuf *m)
532 #ifdef ALTQ_RIO
533 if (q_is_rio(cl->cl_q))
534 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
535 cl->cl_pktattr);
536 #endif
537 #ifdef ALTQ_RED
538 if (q_is_red(cl->cl_q))
539 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
540 #endif
541 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
542 m_freem(m);
543 return (-1);
546 if (cl->cl_flags & PRCF_CLEARDSCP)
547 write_dsfield(m, cl->cl_pktattr, 0);
549 _addq(cl->cl_q, m);
551 return (0);
554 static struct mbuf *
555 priq_getq(struct priq_class *cl)
557 #ifdef ALTQ_RIO
558 if (q_is_rio(cl->cl_q))
559 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
560 #endif
561 #ifdef ALTQ_RED
562 if (q_is_red(cl->cl_q))
563 return red_getq(cl->cl_red, cl->cl_q);
564 #endif
565 return _getq(cl->cl_q);
568 static struct mbuf *
569 priq_pollq(struct priq_class *cl)
571 return qhead(cl->cl_q);
574 static void
575 priq_purgeq(struct priq_class *cl)
577 struct mbuf *m;
579 if (qempty(cl->cl_q))
580 return;
582 while ((m = _getq(cl->cl_q)) != NULL) {
583 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
584 m_freem(m);
586 ASSERT(qlen(cl->cl_q) == 0);
589 static void
590 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
592 sp->class_handle = cl->cl_handle;
593 sp->qlength = qlen(cl->cl_q);
594 sp->qlimit = qlimit(cl->cl_q);
595 sp->period = cl->cl_period;
596 sp->xmitcnt = cl->cl_xmitcnt;
597 sp->dropcnt = cl->cl_dropcnt;
599 sp->qtype = qtype(cl->cl_q);
600 #ifdef ALTQ_RED
601 if (q_is_red(cl->cl_q))
602 red_getstats(cl->cl_red, &sp->red[0]);
603 #endif
604 #ifdef ALTQ_RIO
605 if (q_is_rio(cl->cl_q))
606 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
607 #endif
611 /* convert a class handle to the corresponding class pointer */
612 static struct priq_class *
613 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
615 struct priq_class *cl;
616 int idx;
618 if (chandle == 0)
619 return (NULL);
621 for (idx = pif->pif_maxpri; idx >= 0; idx--)
622 if ((cl = pif->pif_classes[idx]) != NULL &&
623 cl->cl_handle == chandle)
624 return (cl);
626 return (NULL);
630 #ifdef ALTQ3_COMPAT
632 static struct priq_if *
633 priq_attach(struct ifaltq *ifq, u_int bandwidth)
635 struct priq_if *pif;
637 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO);
638 if (pif == NULL)
639 return (NULL);
640 pif->pif_bandwidth = bandwidth;
641 pif->pif_maxpri = -1;
642 pif->pif_ifq = ifq;
644 /* add this state to the priq list */
645 pif->pif_next = pif_list;
646 pif_list = pif;
648 return (pif);
651 static int
652 priq_detach(struct priq_if *pif)
654 (void)priq_clear_interface(pif);
656 /* remove this interface from the pif list */
657 if (pif_list == pif)
658 pif_list = pif->pif_next;
659 else {
660 struct priq_if *p;
662 for (p = pif_list; p != NULL; p = p->pif_next)
663 if (p->pif_next == pif) {
664 p->pif_next = pif->pif_next;
665 break;
667 ASSERT(p != NULL);
670 free(pif, M_DEVBUF);
671 return (0);
675 * priq device interface
678 priqopen(dev_t dev, int flag, int fmt,
679 struct lwp *l)
681 /* everything will be done when the queueing scheme is attached. */
682 return 0;
686 priqclose(dev_t dev, int flag, int fmt,
687 struct lwp *l)
689 struct priq_if *pif;
690 int err, error = 0;
692 while ((pif = pif_list) != NULL) {
693 /* destroy all */
694 if (ALTQ_IS_ENABLED(pif->pif_ifq))
695 altq_disable(pif->pif_ifq);
697 err = altq_detach(pif->pif_ifq);
698 if (err == 0)
699 err = priq_detach(pif);
700 if (err != 0 && error == 0)
701 error = err;
704 return error;
708 priqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
709 struct lwp *l)
711 struct priq_if *pif;
712 struct priq_interface *ifacep;
713 int error = 0;
715 /* check super-user privilege */
716 switch (cmd) {
717 case PRIQ_GETSTATS:
718 break;
719 default:
720 #if (__FreeBSD_version > 400000)
721 if ((error = suser(p)) != 0)
722 return (error);
723 #else
724 if ((error = kauth_authorize_network(l->l_cred,
725 KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_PRIQ, NULL,
726 NULL, NULL)) != 0)
727 return (error);
728 #endif
729 break;
732 switch (cmd) {
734 case PRIQ_IF_ATTACH:
735 error = priqcmd_if_attach((struct priq_interface *)addr);
736 break;
738 case PRIQ_IF_DETACH:
739 error = priqcmd_if_detach((struct priq_interface *)addr);
740 break;
742 case PRIQ_ENABLE:
743 case PRIQ_DISABLE:
744 case PRIQ_CLEAR:
745 ifacep = (struct priq_interface *)addr;
746 if ((pif = altq_lookup(ifacep->ifname,
747 ALTQT_PRIQ)) == NULL) {
748 error = EBADF;
749 break;
752 switch (cmd) {
753 case PRIQ_ENABLE:
754 if (pif->pif_default == NULL) {
755 #ifdef ALTQ_DEBUG
756 printf("priq: no default class\n");
757 #endif
758 error = EINVAL;
759 break;
761 error = altq_enable(pif->pif_ifq);
762 break;
764 case PRIQ_DISABLE:
765 error = altq_disable(pif->pif_ifq);
766 break;
768 case PRIQ_CLEAR:
769 priq_clear_interface(pif);
770 break;
772 break;
774 case PRIQ_ADD_CLASS:
775 error = priqcmd_add_class((struct priq_add_class *)addr);
776 break;
778 case PRIQ_DEL_CLASS:
779 error = priqcmd_delete_class((struct priq_delete_class *)addr);
780 break;
782 case PRIQ_MOD_CLASS:
783 error = priqcmd_modify_class((struct priq_modify_class *)addr);
784 break;
786 case PRIQ_ADD_FILTER:
787 error = priqcmd_add_filter((struct priq_add_filter *)addr);
788 break;
790 case PRIQ_DEL_FILTER:
791 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
792 break;
794 case PRIQ_GETSTATS:
795 error = priqcmd_class_stats((struct priq_class_stats *)addr);
796 break;
798 default:
799 error = EINVAL;
800 break;
802 return error;
805 static int
806 priqcmd_if_attach(struct priq_interface *ap)
808 struct priq_if *pif;
809 struct ifnet *ifp;
810 int error;
812 if ((ifp = ifunit(ap->ifname)) == NULL)
813 return (ENXIO);
815 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
816 return (ENOMEM);
819 * set PRIQ to this ifnet structure.
821 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
822 priq_enqueue, priq_dequeue, priq_request,
823 &pif->pif_classifier, acc_classify)) != 0)
824 (void)priq_detach(pif);
826 return (error);
829 static int
830 priqcmd_if_detach(struct priq_interface *ap)
832 struct priq_if *pif;
833 int error;
835 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
836 return (EBADF);
838 if (ALTQ_IS_ENABLED(pif->pif_ifq))
839 altq_disable(pif->pif_ifq);
841 if ((error = altq_detach(pif->pif_ifq)))
842 return (error);
844 return priq_detach(pif);
847 static int
848 priqcmd_add_class(struct priq_add_class *ap)
850 struct priq_if *pif;
851 struct priq_class *cl;
852 int qid;
854 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
855 return (EBADF);
857 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
858 return (EINVAL);
859 if (pif->pif_classes[ap->pri] != NULL)
860 return (EBUSY);
862 qid = ap->pri + 1;
863 if ((cl = priq_class_create(pif, ap->pri,
864 ap->qlimit, ap->flags, qid)) == NULL)
865 return (ENOMEM);
867 /* return a class handle to the user */
868 ap->class_handle = cl->cl_handle;
870 return (0);
873 static int
874 priqcmd_delete_class(struct priq_delete_class *ap)
876 struct priq_if *pif;
877 struct priq_class *cl;
879 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
880 return (EBADF);
882 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
883 return (EINVAL);
885 return priq_class_destroy(cl);
888 static int
889 priqcmd_modify_class(struct priq_modify_class *ap)
891 struct priq_if *pif;
892 struct priq_class *cl;
894 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
895 return (EBADF);
897 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
898 return (EINVAL);
900 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
901 return (EINVAL);
904 * if priority is changed, move the class to the new priority
906 if (pif->pif_classes[ap->pri] != cl) {
907 if (pif->pif_classes[ap->pri] != NULL)
908 return (EEXIST);
909 pif->pif_classes[cl->cl_pri] = NULL;
910 pif->pif_classes[ap->pri] = cl;
911 cl->cl_pri = ap->pri;
914 /* call priq_class_create to change class parameters */
915 if ((cl = priq_class_create(pif, ap->pri,
916 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
917 return (ENOMEM);
918 return 0;
921 static int
922 priqcmd_add_filter(struct priq_add_filter *ap)
924 struct priq_if *pif;
925 struct priq_class *cl;
927 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
928 return (EBADF);
930 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
931 return (EINVAL);
933 return acc_add_filter(&pif->pif_classifier, &ap->filter,
934 cl, &ap->filter_handle);
937 static int
938 priqcmd_delete_filter(struct priq_delete_filter *ap)
940 struct priq_if *pif;
942 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
943 return (EBADF);
945 return acc_delete_filter(&pif->pif_classifier,
946 ap->filter_handle);
949 static int
950 priqcmd_class_stats(struct priq_class_stats *ap)
952 struct priq_if *pif;
953 struct priq_class *cl;
954 struct priq_classstats stats, *usp;
955 int pri, error;
957 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
958 return (EBADF);
960 ap->maxpri = pif->pif_maxpri;
962 /* then, read the next N classes in the tree */
963 usp = ap->stats;
964 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
965 cl = pif->pif_classes[pri];
966 if (cl != NULL)
967 get_class_stats(&stats, cl);
968 else
969 memset(&stats, 0, sizeof(stats));
970 if ((error = copyout((void *)&stats, (void *)usp++,
971 sizeof(stats))) != 0)
972 return (error);
974 return (0);
977 #ifdef KLD_MODULE
979 static struct altqsw priq_sw =
980 {"priq", priqopen, priqclose, priqioctl};
982 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
983 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
984 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
986 #endif /* KLD_MODULE */
988 #endif /* ALTQ3_COMPAT */
989 #endif /* ALTQ_PRIQ */