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 <net/if_var.h>
55 #include <netinet/in.h>
57 #include <netpfil/pf/pf.h>
58 #include <netpfil/pf/pf_altq.h>
59 #include <netpfil/pf/pf_mtag.h>
60 #include <altq/altq.h>
62 #include <altq/altq_conf.h>
64 #include <altq/altq_priq.h>
70 static struct priq_if *priq_attach(struct ifaltq *, u_int);
71 static int priq_detach(struct priq_if *);
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,
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 *);
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);
104 /* pif_list keeps all priq_if's allocated. */
105 static struct priq_if *pif_list = NULL;
106 #endif /* ALTQ3_COMPAT */
109 priq_pfattach(struct pf_altq *a)
114 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
121 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
122 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
128 priq_add_altq(struct pf_altq *a)
133 if ((ifp = ifunit(a->ifname)) == NULL)
135 if (!ALTQ_IS_READY(&ifp->if_snd))
138 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
141 pif->pif_bandwidth = a->ifbandwidth;
142 pif->pif_maxpri = -1;
143 pif->pif_ifq = &ifp->if_snd;
145 /* keep the state in pf_altq */
152 priq_remove_altq(struct pf_altq *a)
156 if ((pif = a->altq_disc) == NULL)
160 (void)priq_clear_interface(pif);
167 priq_add_queue(struct pf_altq *a)
170 struct priq_class *cl;
172 if ((pif = a->altq_disc) == NULL)
175 /* check parameters */
176 if (a->priority >= PRIQ_MAXPRI)
180 if (pif->pif_classes[a->priority] != NULL)
182 if (clh_to_clp(pif, a->qid) != NULL)
185 cl = priq_class_create(pif, a->priority, a->qlimit,
186 a->pq_u.priq_opts.flags, a->qid);
194 priq_remove_queue(struct pf_altq *a)
197 struct priq_class *cl;
199 if ((pif = a->altq_disc) == NULL)
202 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
205 return (priq_class_destroy(cl));
209 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
212 struct priq_class *cl;
213 struct priq_classstats stats;
216 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
219 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
222 if (*nbytes < sizeof(stats))
225 get_class_stats(&stats, cl);
227 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
229 *nbytes = sizeof(stats);
234 * bring the interface back to the initial state by discarding
235 * all the filters and classes.
238 priq_clear_interface(struct priq_if *pif)
240 struct priq_class *cl;
243 #ifdef ALTQ3_CLFIER_COMPAT
244 /* free the filters for this interface */
245 acc_discard_filters(&pif->pif_classifier, NULL, 1);
248 /* clear out the classes */
249 for (pri = 0; pri <= pif->pif_maxpri; pri++)
250 if ((cl = pif->pif_classes[pri]) != NULL)
251 priq_class_destroy(cl);
257 priq_request(struct ifaltq *ifq, int req, void *arg)
259 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
261 IFQ_LOCK_ASSERT(ifq);
271 /* discard all the queued packets on the interface */
273 priq_purge(struct priq_if *pif)
275 struct priq_class *cl;
278 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
279 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
282 if (ALTQ_IS_ENABLED(pif->pif_ifq))
283 pif->pif_ifq->ifq_len = 0;
286 static struct priq_class *
287 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
289 struct priq_class *cl;
293 if (flags & PRCF_RED) {
295 printf("priq_class_create: RED not configured for PRIQ!\n");
301 if ((cl = pif->pif_classes[pri]) != NULL) {
302 /* modify the class instead of creating a new one */
308 IFQ_LOCK(cl->cl_pif->pif_ifq);
309 if (!qempty(cl->cl_q))
311 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
314 if (q_is_rio(cl->cl_q))
315 rio_destroy((rio_t *)cl->cl_red);
318 if (q_is_red(cl->cl_q))
319 red_destroy(cl->cl_red);
322 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
327 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
329 if (cl->cl_q == NULL)
333 pif->pif_classes[pri] = cl;
334 if (flags & PRCF_DEFAULTCLASS)
335 pif->pif_default = cl;
337 qlimit = 50; /* use default */
338 qlimit(cl->cl_q) = qlimit;
339 qtype(cl->cl_q) = Q_DROPTAIL;
341 cl->cl_flags = flags;
343 if (pri > pif->pif_maxpri)
344 pif->pif_maxpri = pri;
349 if (flags & (PRCF_RED|PRCF_RIO)) {
350 int red_flags, red_pkttime;
353 if (flags & PRCF_ECN)
354 red_flags |= REDF_ECN;
356 if (flags & PRCF_CLEARDSCP)
357 red_flags |= RIOF_CLEARDSCP;
359 if (pif->pif_bandwidth < 8)
360 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
362 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
363 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
365 if (flags & PRCF_RIO) {
366 cl->cl_red = (red_t *)rio_alloc(0, NULL,
367 red_flags, red_pkttime);
368 if (cl->cl_red == NULL)
370 qtype(cl->cl_q) = Q_RIO;
373 if (flags & PRCF_RED) {
374 cl->cl_red = red_alloc(0, 0,
375 qlimit(cl->cl_q) * 10/100,
376 qlimit(cl->cl_q) * 30/100,
377 red_flags, red_pkttime);
378 if (cl->cl_red == NULL)
380 qtype(cl->cl_q) = Q_RED;
383 #endif /* ALTQ_RED */
388 if (cl->cl_red != NULL) {
390 if (q_is_rio(cl->cl_q))
391 rio_destroy((rio_t *)cl->cl_red);
394 if (q_is_red(cl->cl_q))
395 red_destroy(cl->cl_red);
398 if (cl->cl_q != NULL)
399 free(cl->cl_q, M_DEVBUF);
405 priq_class_destroy(struct priq_class *cl)
415 IFQ_LOCK(cl->cl_pif->pif_ifq);
417 #ifdef ALTQ3_CLFIER_COMPAT
418 /* delete filters referencing to this class */
419 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
422 if (!qempty(cl->cl_q))
426 pif->pif_classes[cl->cl_pri] = NULL;
427 if (pif->pif_maxpri == cl->cl_pri) {
428 for (pri = cl->cl_pri; pri >= 0; pri--)
429 if (pif->pif_classes[pri] != NULL) {
430 pif->pif_maxpri = pri;
434 pif->pif_maxpri = -1;
436 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
439 if (cl->cl_red != NULL) {
441 if (q_is_rio(cl->cl_q))
442 rio_destroy((rio_t *)cl->cl_red);
445 if (q_is_red(cl->cl_q))
446 red_destroy(cl->cl_red);
449 free(cl->cl_q, M_DEVBUF);
455 * priq_enqueue is an enqueue function to be registered to
456 * (*altq_enqueue) in struct ifaltq.
459 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
461 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
462 struct priq_class *cl;
466 IFQ_LOCK_ASSERT(ifq);
468 /* grab class set by classifier */
469 if ((m->m_flags & M_PKTHDR) == 0) {
470 /* should not happen */
471 printf("altq: packet for %s does not have pkthdr\n",
472 ifq->altq_ifp->if_xname);
477 if ((t = pf_find_mtag(m)) != NULL)
478 cl = clh_to_clp(pif, t->qid);
480 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
481 cl = pktattr->pattr_class;
484 cl = pif->pif_default;
492 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
495 cl->cl_pktattr = NULL;
497 if (priq_addq(cl, m) != 0) {
498 /* drop occurred. mbuf was freed in priq_addq. */
499 PKTCNTR_ADD(&cl->cl_dropcnt, len);
504 /* successfully queued. */
509 * priq_dequeue is a dequeue function to be registered to
510 * (*altq_dequeue) in struct ifaltq.
512 * note: ALTDQ_POLL returns the next packet without removing the packet
513 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
514 * ALTDQ_REMOVE must return the same packet if called immediately
518 priq_dequeue(struct ifaltq *ifq, int op)
520 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
521 struct priq_class *cl;
525 IFQ_LOCK_ASSERT(ifq);
527 if (IFQ_IS_EMPTY(ifq))
528 /* no packet in the queue */
531 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
532 if ((cl = pif->pif_classes[pri]) != NULL &&
534 if (op == ALTDQ_POLL)
535 return (priq_pollq(cl));
540 if (qempty(cl->cl_q))
542 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
551 priq_addq(struct priq_class *cl, struct mbuf *m)
555 if (q_is_rio(cl->cl_q))
556 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
560 if (q_is_red(cl->cl_q))
561 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
563 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
568 if (cl->cl_flags & PRCF_CLEARDSCP)
569 write_dsfield(m, cl->cl_pktattr, 0);
577 priq_getq(struct priq_class *cl)
580 if (q_is_rio(cl->cl_q))
581 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
584 if (q_is_red(cl->cl_q))
585 return red_getq(cl->cl_red, cl->cl_q);
587 return _getq(cl->cl_q);
592 struct priq_class *cl;
594 return qhead(cl->cl_q);
598 priq_purgeq(struct priq_class *cl)
602 if (qempty(cl->cl_q))
605 while ((m = _getq(cl->cl_q)) != NULL) {
606 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
609 ASSERT(qlen(cl->cl_q) == 0);
613 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
615 sp->class_handle = cl->cl_handle;
616 sp->qlength = qlen(cl->cl_q);
617 sp->qlimit = qlimit(cl->cl_q);
618 sp->period = cl->cl_period;
619 sp->xmitcnt = cl->cl_xmitcnt;
620 sp->dropcnt = cl->cl_dropcnt;
622 sp->qtype = qtype(cl->cl_q);
624 if (q_is_red(cl->cl_q))
625 red_getstats(cl->cl_red, &sp->red[0]);
628 if (q_is_rio(cl->cl_q))
629 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
634 /* convert a class handle to the corresponding class pointer */
635 static struct priq_class *
636 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
638 struct priq_class *cl;
644 for (idx = pif->pif_maxpri; idx >= 0; idx--)
645 if ((cl = pif->pif_classes[idx]) != NULL &&
646 cl->cl_handle == chandle)
655 static struct priq_if *
656 priq_attach(ifq, bandwidth)
662 pif = malloc(sizeof(struct priq_if),
666 bzero(pif, sizeof(struct priq_if));
667 pif->pif_bandwidth = bandwidth;
668 pif->pif_maxpri = -1;
671 /* add this state to the priq list */
672 pif->pif_next = pif_list;
682 (void)priq_clear_interface(pif);
684 /* remove this interface from the pif list */
686 pif_list = pif->pif_next;
690 for (p = pif_list; p != NULL; p = p->pif_next)
691 if (p->pif_next == pif) {
692 p->pif_next = pif->pif_next;
703 * priq device interface
706 priqopen(dev, flag, fmt, p)
709 #if (__FreeBSD_version > 500000)
715 /* everything will be done when the queueing scheme is attached. */
720 priqclose(dev, flag, fmt, p)
723 #if (__FreeBSD_version > 500000)
732 while ((pif = pif_list) != NULL) {
734 if (ALTQ_IS_ENABLED(pif->pif_ifq))
735 altq_disable(pif->pif_ifq);
737 err = altq_detach(pif->pif_ifq);
739 err = priq_detach(pif);
740 if (err != 0 && error == 0)
748 priqioctl(dev, cmd, addr, flag, p)
753 #if (__FreeBSD_version > 500000)
760 struct priq_interface *ifacep;
763 /* check super-user privilege */
768 #if (__FreeBSD_version > 700000)
769 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
771 #elsif (__FreeBSD_version > 400000)
772 if ((error = suser(p)) != 0)
775 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
784 error = priqcmd_if_attach((struct priq_interface *)addr);
788 error = priqcmd_if_detach((struct priq_interface *)addr);
794 ifacep = (struct priq_interface *)addr;
795 if ((pif = altq_lookup(ifacep->ifname,
796 ALTQT_PRIQ)) == NULL) {
803 if (pif->pif_default == NULL) {
805 printf("priq: no default class\n");
810 error = altq_enable(pif->pif_ifq);
814 error = altq_disable(pif->pif_ifq);
818 priq_clear_interface(pif);
824 error = priqcmd_add_class((struct priq_add_class *)addr);
828 error = priqcmd_delete_class((struct priq_delete_class *)addr);
832 error = priqcmd_modify_class((struct priq_modify_class *)addr);
835 case PRIQ_ADD_FILTER:
836 error = priqcmd_add_filter((struct priq_add_filter *)addr);
839 case PRIQ_DEL_FILTER:
840 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
844 error = priqcmd_class_stats((struct priq_class_stats *)addr);
855 priqcmd_if_attach(ap)
856 struct priq_interface *ap;
862 if ((ifp = ifunit(ap->ifname)) == NULL)
865 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
869 * set PRIQ to this ifnet structure.
871 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
872 priq_enqueue, priq_dequeue, priq_request,
873 &pif->pif_classifier, acc_classify)) != 0)
874 (void)priq_detach(pif);
880 priqcmd_if_detach(ap)
881 struct priq_interface *ap;
886 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
889 if (ALTQ_IS_ENABLED(pif->pif_ifq))
890 altq_disable(pif->pif_ifq);
892 if ((error = altq_detach(pif->pif_ifq)))
895 return priq_detach(pif);
899 priqcmd_add_class(ap)
900 struct priq_add_class *ap;
903 struct priq_class *cl;
906 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
909 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
911 if (pif->pif_classes[ap->pri] != NULL)
915 if ((cl = priq_class_create(pif, ap->pri,
916 ap->qlimit, ap->flags, qid)) == NULL)
919 /* return a class handle to the user */
920 ap->class_handle = cl->cl_handle;
926 priqcmd_delete_class(ap)
927 struct priq_delete_class *ap;
930 struct priq_class *cl;
932 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
935 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
938 return priq_class_destroy(cl);
942 priqcmd_modify_class(ap)
943 struct priq_modify_class *ap;
946 struct priq_class *cl;
948 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
951 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
954 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
958 * if priority is changed, move the class to the new priority
960 if (pif->pif_classes[ap->pri] != cl) {
961 if (pif->pif_classes[ap->pri] != NULL)
963 pif->pif_classes[cl->cl_pri] = NULL;
964 pif->pif_classes[ap->pri] = cl;
965 cl->cl_pri = ap->pri;
968 /* call priq_class_create to change class parameters */
969 if ((cl = priq_class_create(pif, ap->pri,
970 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
976 priqcmd_add_filter(ap)
977 struct priq_add_filter *ap;
980 struct priq_class *cl;
982 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
985 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
988 return acc_add_filter(&pif->pif_classifier, &ap->filter,
989 cl, &ap->filter_handle);
993 priqcmd_delete_filter(ap)
994 struct priq_delete_filter *ap;
998 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1001 return acc_delete_filter(&pif->pif_classifier,
1006 priqcmd_class_stats(ap)
1007 struct priq_class_stats *ap;
1009 struct priq_if *pif;
1010 struct priq_class *cl;
1011 struct priq_classstats stats, *usp;
1014 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1017 ap->maxpri = pif->pif_maxpri;
1019 /* then, read the next N classes in the tree */
1021 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1022 cl = pif->pif_classes[pri];
1024 get_class_stats(&stats, cl);
1026 bzero(&stats, sizeof(stats));
1027 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1028 sizeof(stats))) != 0)
1036 static struct altqsw priq_sw =
1037 {"priq", priqopen, priqclose, priqioctl};
1039 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1040 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1041 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1043 #endif /* KLD_MODULE */
1045 #endif /* ALTQ3_COMPAT */
1046 #endif /* ALTQ_PRIQ */