2 /* $KAME: altq_priq.c,v 1.11 2003/09/17 14:23:25 kjc Exp $ */
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
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
32 #if defined(__FreeBSD__) || defined(__NetBSD__)
36 #include "opt_inet6.h"
38 #endif /* __FreeBSD__ || __NetBSD__ */
40 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
42 #include <sys/param.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/systm.h>
49 #include <sys/errno.h>
50 #include <sys/kernel.h>
51 #include <sys/queue.h>
54 #include <netinet/in.h>
56 #include <net/pfvar.h>
57 #include <altq/altq.h>
59 #include <altq/altq_conf.h>
61 #include <altq/altq_priq.h>
67 static struct priq_if *priq_attach(struct ifaltq *, u_int);
68 static int priq_detach(struct priq_if *);
70 static int priq_clear_interface(struct priq_if *);
71 static int priq_request(struct ifaltq *, int, void *);
72 static void priq_purge(struct priq_if *);
73 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
75 static int priq_class_destroy(struct priq_class *);
76 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
77 static struct mbuf *priq_dequeue(struct ifaltq *, int);
79 static int priq_addq(struct priq_class *, struct mbuf *);
80 static struct mbuf *priq_getq(struct priq_class *);
81 static struct mbuf *priq_pollq(struct priq_class *);
82 static void priq_purgeq(struct priq_class *);
85 static int priqcmd_if_attach(struct priq_interface *);
86 static int priqcmd_if_detach(struct priq_interface *);
87 static int priqcmd_add_class(struct priq_add_class *);
88 static int priqcmd_delete_class(struct priq_delete_class *);
89 static int priqcmd_modify_class(struct priq_modify_class *);
90 static int priqcmd_add_filter(struct priq_add_filter *);
91 static int priqcmd_delete_filter(struct priq_delete_filter *);
92 static int priqcmd_class_stats(struct priq_class_stats *);
93 #endif /* ALTQ3_COMPAT */
95 static void get_class_stats(struct priq_classstats *, struct priq_class *);
96 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
101 /* pif_list keeps all priq_if's allocated. */
102 static struct priq_if *pif_list = NULL;
103 #endif /* ALTQ3_COMPAT */
106 priq_pfattach(struct pf_altq *a)
111 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
118 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
119 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
125 priq_add_altq(struct pf_altq *a)
130 if ((ifp = ifunit(a->ifname)) == NULL)
132 if (!ALTQ_IS_READY(&ifp->if_snd))
135 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
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 */
149 priq_remove_altq(struct pf_altq *a)
153 if ((pif = a->altq_disc) == NULL)
157 (void)priq_clear_interface(pif);
164 priq_add_queue(struct pf_altq *a)
167 struct priq_class *cl;
169 if ((pif = a->altq_disc) == NULL)
172 /* check parameters */
173 if (a->priority >= PRIQ_MAXPRI)
177 if (pif->pif_classes[a->priority] != NULL)
179 if (clh_to_clp(pif, a->qid) != NULL)
182 cl = priq_class_create(pif, a->priority, a->qlimit,
183 a->pq_u.priq_opts.flags, a->qid);
191 priq_remove_queue(struct pf_altq *a)
194 struct priq_class *cl;
196 if ((pif = a->altq_disc) == NULL)
199 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
202 return (priq_class_destroy(cl));
206 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
209 struct priq_class *cl;
210 struct priq_classstats stats;
213 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
216 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
219 if (*nbytes < sizeof(stats))
222 get_class_stats(&stats, cl);
224 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
226 *nbytes = sizeof(stats);
231 * bring the interface back to the initial state by discarding
232 * all the filters and classes.
235 priq_clear_interface(struct priq_if *pif)
237 struct priq_class *cl;
240 #ifdef ALTQ3_CLFIER_COMPAT
241 /* free the filters for this interface */
242 acc_discard_filters(&pif->pif_classifier, NULL, 1);
245 /* clear out the classes */
246 for (pri = 0; pri <= pif->pif_maxpri; pri++)
247 if ((cl = pif->pif_classes[pri]) != NULL)
248 priq_class_destroy(cl);
254 priq_request(struct ifaltq *ifq, int req, void *arg)
256 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
258 IFQ_LOCK_ASSERT(ifq);
268 /* discard all the queued packets on the interface */
270 priq_purge(struct priq_if *pif)
272 struct priq_class *cl;
275 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
276 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
279 if (ALTQ_IS_ENABLED(pif->pif_ifq))
280 pif->pif_ifq->ifq_len = 0;
283 static struct priq_class *
284 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
286 struct priq_class *cl;
290 if (flags & PRCF_RED) {
292 printf("priq_class_create: RED not configured for PRIQ!\n");
298 if ((cl = pif->pif_classes[pri]) != NULL) {
299 /* modify the class instead of creating a new one */
305 IFQ_LOCK(cl->cl_pif->pif_ifq);
306 if (!qempty(cl->cl_q))
308 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
311 if (q_is_rio(cl->cl_q))
312 rio_destroy((rio_t *)cl->cl_red);
315 if (q_is_red(cl->cl_q))
316 red_destroy(cl->cl_red);
319 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
324 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
326 if (cl->cl_q == NULL)
330 pif->pif_classes[pri] = cl;
331 if (flags & PRCF_DEFAULTCLASS)
332 pif->pif_default = cl;
334 qlimit = 50; /* use default */
335 qlimit(cl->cl_q) = qlimit;
336 qtype(cl->cl_q) = Q_DROPTAIL;
338 cl->cl_flags = flags;
340 if (pri > pif->pif_maxpri)
341 pif->pif_maxpri = pri;
346 if (flags & (PRCF_RED|PRCF_RIO)) {
347 int red_flags, red_pkttime;
350 if (flags & PRCF_ECN)
351 red_flags |= REDF_ECN;
353 if (flags & PRCF_CLEARDSCP)
354 red_flags |= RIOF_CLEARDSCP;
356 if (pif->pif_bandwidth < 8)
357 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
359 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
360 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
362 if (flags & PRCF_RIO) {
363 cl->cl_red = (red_t *)rio_alloc(0, NULL,
364 red_flags, red_pkttime);
365 if (cl->cl_red == NULL)
367 qtype(cl->cl_q) = Q_RIO;
370 if (flags & PRCF_RED) {
371 cl->cl_red = red_alloc(0, 0,
372 qlimit(cl->cl_q) * 10/100,
373 qlimit(cl->cl_q) * 30/100,
374 red_flags, red_pkttime);
375 if (cl->cl_red == NULL)
377 qtype(cl->cl_q) = Q_RED;
380 #endif /* ALTQ_RED */
385 if (cl->cl_red != NULL) {
387 if (q_is_rio(cl->cl_q))
388 rio_destroy((rio_t *)cl->cl_red);
391 if (q_is_red(cl->cl_q))
392 red_destroy(cl->cl_red);
395 if (cl->cl_q != NULL)
396 free(cl->cl_q, M_DEVBUF);
402 priq_class_destroy(struct priq_class *cl)
412 IFQ_LOCK(cl->cl_pif->pif_ifq);
414 #ifdef ALTQ3_CLFIER_COMPAT
415 /* delete filters referencing to this class */
416 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
419 if (!qempty(cl->cl_q))
423 pif->pif_classes[cl->cl_pri] = NULL;
424 if (pif->pif_maxpri == cl->cl_pri) {
425 for (pri = cl->cl_pri; pri >= 0; pri--)
426 if (pif->pif_classes[pri] != NULL) {
427 pif->pif_maxpri = pri;
431 pif->pif_maxpri = -1;
433 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
436 if (cl->cl_red != NULL) {
438 if (q_is_rio(cl->cl_q))
439 rio_destroy((rio_t *)cl->cl_red);
442 if (q_is_red(cl->cl_q))
443 red_destroy(cl->cl_red);
446 free(cl->cl_q, M_DEVBUF);
452 * priq_enqueue is an enqueue function to be registered to
453 * (*altq_enqueue) in struct ifaltq.
456 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
458 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
459 struct priq_class *cl;
463 IFQ_LOCK_ASSERT(ifq);
465 /* grab class set by classifier */
466 if ((m->m_flags & M_PKTHDR) == 0) {
467 /* should not happen */
468 printf("altq: packet for %s does not have pkthdr\n",
469 ifq->altq_ifp->if_xname);
474 if ((t = pf_find_mtag(m)) != NULL)
475 cl = clh_to_clp(pif, t->qid);
477 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
478 cl = pktattr->pattr_class;
481 cl = pif->pif_default;
489 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
492 cl->cl_pktattr = NULL;
494 if (priq_addq(cl, m) != 0) {
495 /* drop occurred. mbuf was freed in priq_addq. */
496 PKTCNTR_ADD(&cl->cl_dropcnt, len);
501 /* successfully queued. */
506 * priq_dequeue is a dequeue function to be registered to
507 * (*altq_dequeue) in struct ifaltq.
509 * note: ALTDQ_POLL returns the next packet without removing the packet
510 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
511 * ALTDQ_REMOVE must return the same packet if called immediately
515 priq_dequeue(struct ifaltq *ifq, int op)
517 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
518 struct priq_class *cl;
522 IFQ_LOCK_ASSERT(ifq);
524 if (IFQ_IS_EMPTY(ifq))
525 /* no packet in the queue */
528 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
529 if ((cl = pif->pif_classes[pri]) != NULL &&
531 if (op == ALTDQ_POLL)
532 return (priq_pollq(cl));
537 if (qempty(cl->cl_q))
539 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
548 priq_addq(struct priq_class *cl, struct mbuf *m)
552 if (q_is_rio(cl->cl_q))
553 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
557 if (q_is_red(cl->cl_q))
558 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
560 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
565 if (cl->cl_flags & PRCF_CLEARDSCP)
566 write_dsfield(m, cl->cl_pktattr, 0);
574 priq_getq(struct priq_class *cl)
577 if (q_is_rio(cl->cl_q))
578 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
581 if (q_is_red(cl->cl_q))
582 return red_getq(cl->cl_red, cl->cl_q);
584 return _getq(cl->cl_q);
589 struct priq_class *cl;
591 return qhead(cl->cl_q);
595 priq_purgeq(struct priq_class *cl)
599 if (qempty(cl->cl_q))
602 while ((m = _getq(cl->cl_q)) != NULL) {
603 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
606 ASSERT(qlen(cl->cl_q) == 0);
610 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
612 sp->class_handle = cl->cl_handle;
613 sp->qlength = qlen(cl->cl_q);
614 sp->qlimit = qlimit(cl->cl_q);
615 sp->period = cl->cl_period;
616 sp->xmitcnt = cl->cl_xmitcnt;
617 sp->dropcnt = cl->cl_dropcnt;
619 sp->qtype = qtype(cl->cl_q);
621 if (q_is_red(cl->cl_q))
622 red_getstats(cl->cl_red, &sp->red[0]);
625 if (q_is_rio(cl->cl_q))
626 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
631 /* convert a class handle to the corresponding class pointer */
632 static struct priq_class *
633 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
635 struct priq_class *cl;
641 for (idx = pif->pif_maxpri; idx >= 0; idx--)
642 if ((cl = pif->pif_classes[idx]) != NULL &&
643 cl->cl_handle == chandle)
652 static struct priq_if *
653 priq_attach(ifq, bandwidth)
659 pif = malloc(sizeof(struct priq_if),
663 bzero(pif, sizeof(struct priq_if));
664 pif->pif_bandwidth = bandwidth;
665 pif->pif_maxpri = -1;
668 /* add this state to the priq list */
669 pif->pif_next = pif_list;
679 (void)priq_clear_interface(pif);
681 /* remove this interface from the pif list */
683 pif_list = pif->pif_next;
687 for (p = pif_list; p != NULL; p = p->pif_next)
688 if (p->pif_next == pif) {
689 p->pif_next = pif->pif_next;
700 * priq device interface
703 priqopen(dev, flag, fmt, p)
706 #if (__FreeBSD_version > 500000)
712 /* everything will be done when the queueing scheme is attached. */
717 priqclose(dev, flag, fmt, p)
720 #if (__FreeBSD_version > 500000)
729 while ((pif = pif_list) != NULL) {
731 if (ALTQ_IS_ENABLED(pif->pif_ifq))
732 altq_disable(pif->pif_ifq);
734 err = altq_detach(pif->pif_ifq);
736 err = priq_detach(pif);
737 if (err != 0 && error == 0)
745 priqioctl(dev, cmd, addr, flag, p)
750 #if (__FreeBSD_version > 500000)
757 struct priq_interface *ifacep;
760 /* check super-user privilege */
765 #if (__FreeBSD_version > 700000)
766 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
768 #elsif (__FreeBSD_version > 400000)
769 if ((error = suser(p)) != 0)
772 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
781 error = priqcmd_if_attach((struct priq_interface *)addr);
785 error = priqcmd_if_detach((struct priq_interface *)addr);
791 ifacep = (struct priq_interface *)addr;
792 if ((pif = altq_lookup(ifacep->ifname,
793 ALTQT_PRIQ)) == NULL) {
800 if (pif->pif_default == NULL) {
802 printf("priq: no default class\n");
807 error = altq_enable(pif->pif_ifq);
811 error = altq_disable(pif->pif_ifq);
815 priq_clear_interface(pif);
821 error = priqcmd_add_class((struct priq_add_class *)addr);
825 error = priqcmd_delete_class((struct priq_delete_class *)addr);
829 error = priqcmd_modify_class((struct priq_modify_class *)addr);
832 case PRIQ_ADD_FILTER:
833 error = priqcmd_add_filter((struct priq_add_filter *)addr);
836 case PRIQ_DEL_FILTER:
837 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
841 error = priqcmd_class_stats((struct priq_class_stats *)addr);
852 priqcmd_if_attach(ap)
853 struct priq_interface *ap;
859 if ((ifp = ifunit(ap->ifname)) == NULL)
862 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
866 * set PRIQ to this ifnet structure.
868 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
869 priq_enqueue, priq_dequeue, priq_request,
870 &pif->pif_classifier, acc_classify)) != 0)
871 (void)priq_detach(pif);
877 priqcmd_if_detach(ap)
878 struct priq_interface *ap;
883 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
886 if (ALTQ_IS_ENABLED(pif->pif_ifq))
887 altq_disable(pif->pif_ifq);
889 if ((error = altq_detach(pif->pif_ifq)))
892 return priq_detach(pif);
896 priqcmd_add_class(ap)
897 struct priq_add_class *ap;
900 struct priq_class *cl;
903 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
906 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
908 if (pif->pif_classes[ap->pri] != NULL)
912 if ((cl = priq_class_create(pif, ap->pri,
913 ap->qlimit, ap->flags, qid)) == NULL)
916 /* return a class handle to the user */
917 ap->class_handle = cl->cl_handle;
923 priqcmd_delete_class(ap)
924 struct priq_delete_class *ap;
927 struct priq_class *cl;
929 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
932 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
935 return priq_class_destroy(cl);
939 priqcmd_modify_class(ap)
940 struct priq_modify_class *ap;
943 struct priq_class *cl;
945 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
948 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
951 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
955 * if priority is changed, move the class to the new priority
957 if (pif->pif_classes[ap->pri] != cl) {
958 if (pif->pif_classes[ap->pri] != NULL)
960 pif->pif_classes[cl->cl_pri] = NULL;
961 pif->pif_classes[ap->pri] = cl;
962 cl->cl_pri = ap->pri;
965 /* call priq_class_create to change class parameters */
966 if ((cl = priq_class_create(pif, ap->pri,
967 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
973 priqcmd_add_filter(ap)
974 struct priq_add_filter *ap;
977 struct priq_class *cl;
979 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
982 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
985 return acc_add_filter(&pif->pif_classifier, &ap->filter,
986 cl, &ap->filter_handle);
990 priqcmd_delete_filter(ap)
991 struct priq_delete_filter *ap;
995 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
998 return acc_delete_filter(&pif->pif_classifier,
1003 priqcmd_class_stats(ap)
1004 struct priq_class_stats *ap;
1006 struct priq_if *pif;
1007 struct priq_class *cl;
1008 struct priq_classstats stats, *usp;
1011 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1014 ap->maxpri = pif->pif_maxpri;
1016 /* then, read the next N classes in the tree */
1018 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1019 cl = pif->pif_classes[pri];
1021 get_class_stats(&stats, cl);
1023 bzero(&stats, sizeof(stats));
1024 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1025 sizeof(stats))) != 0)
1033 static struct altqsw priq_sw =
1034 {"priq", priqopen, priqclose, priqioctl};
1036 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1037 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1038 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1040 #endif /* KLD_MODULE */
1042 #endif /* ALTQ3_COMPAT */
1043 #endif /* ALTQ_PRIQ */