2 * Copyright (C) 2000-2003
3 * Sony Computer Science Laboratories Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $KAME: altq_priq.c,v 1.11 2003/09/17 14:23:25 kjc Exp $
35 #include "opt_inet6.h"
37 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
39 #include <sys/param.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
54 #include <netpfil/pf/pf.h>
55 #include <netpfil/pf/pf_altq.h>
56 #include <netpfil/pf/pf_mtag.h>
57 #include <net/altq/altq.h>
59 #include <net/altq/altq_conf.h>
61 #include <net/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)
114 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
115 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
121 priq_add_altq(struct pf_altq *a)
126 if ((ifp = ifunit(a->ifname)) == NULL)
128 if (!ALTQ_IS_READY(&ifp->if_snd))
131 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
134 pif->pif_bandwidth = a->ifbandwidth;
135 pif->pif_maxpri = -1;
136 pif->pif_ifq = &ifp->if_snd;
138 /* keep the state in pf_altq */
145 priq_remove_altq(struct pf_altq *a)
149 if ((pif = a->altq_disc) == NULL)
153 (void)priq_clear_interface(pif);
160 priq_add_queue(struct pf_altq *a)
163 struct priq_class *cl;
165 if ((pif = a->altq_disc) == NULL)
168 /* check parameters */
169 if (a->priority >= PRIQ_MAXPRI)
173 if (pif->pif_classes[a->priority] != NULL)
175 if (clh_to_clp(pif, a->qid) != NULL)
178 cl = priq_class_create(pif, a->priority, a->qlimit,
179 a->pq_u.priq_opts.flags, a->qid);
187 priq_remove_queue(struct pf_altq *a)
190 struct priq_class *cl;
192 if ((pif = a->altq_disc) == NULL)
195 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
198 return (priq_class_destroy(cl));
202 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
205 struct priq_class *cl;
206 struct priq_classstats stats;
209 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
212 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
215 if (*nbytes < sizeof(stats))
218 get_class_stats(&stats, cl);
220 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
222 *nbytes = sizeof(stats);
227 * bring the interface back to the initial state by discarding
228 * all the filters and classes.
231 priq_clear_interface(struct priq_if *pif)
233 struct priq_class *cl;
236 #ifdef ALTQ3_CLFIER_COMPAT
237 /* free the filters for this interface */
238 acc_discard_filters(&pif->pif_classifier, NULL, 1);
241 /* clear out the classes */
242 for (pri = 0; pri <= pif->pif_maxpri; pri++)
243 if ((cl = pif->pif_classes[pri]) != NULL)
244 priq_class_destroy(cl);
250 priq_request(struct ifaltq *ifq, int req, void *arg)
252 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
254 IFQ_LOCK_ASSERT(ifq);
264 /* discard all the queued packets on the interface */
266 priq_purge(struct priq_if *pif)
268 struct priq_class *cl;
271 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
272 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
275 if (ALTQ_IS_ENABLED(pif->pif_ifq))
276 pif->pif_ifq->ifq_len = 0;
279 static struct priq_class *
280 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
282 struct priq_class *cl;
286 if (flags & PRCF_RED) {
288 printf("priq_class_create: RED not configured for PRIQ!\n");
294 if (flags & PRCF_CODEL) {
296 printf("priq_class_create: CODEL not configured for PRIQ!\n");
302 if ((cl = pif->pif_classes[pri]) != NULL) {
303 /* 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 if (q_is_codel(cl->cl_q))
320 codel_destroy(cl->cl_codel);
323 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
328 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
330 if (cl->cl_q == NULL)
334 pif->pif_classes[pri] = cl;
335 if (flags & PRCF_DEFAULTCLASS)
336 pif->pif_default = cl;
338 qlimit = 50; /* use default */
339 qlimit(cl->cl_q) = qlimit;
340 qtype(cl->cl_q) = Q_DROPTAIL;
343 cl->cl_flags = flags;
345 if (pri > pif->pif_maxpri)
346 pif->pif_maxpri = pri;
351 if (flags & (PRCF_RED|PRCF_RIO)) {
352 int red_flags, red_pkttime;
355 if (flags & PRCF_ECN)
356 red_flags |= REDF_ECN;
358 if (flags & PRCF_CLEARDSCP)
359 red_flags |= RIOF_CLEARDSCP;
361 if (pif->pif_bandwidth < 8)
362 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
364 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
365 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
367 if (flags & PRCF_RIO) {
368 cl->cl_red = (red_t *)rio_alloc(0, NULL,
369 red_flags, red_pkttime);
370 if (cl->cl_red == NULL)
372 qtype(cl->cl_q) = Q_RIO;
375 if (flags & PRCF_RED) {
376 cl->cl_red = red_alloc(0, 0,
377 qlimit(cl->cl_q) * 10/100,
378 qlimit(cl->cl_q) * 30/100,
379 red_flags, red_pkttime);
380 if (cl->cl_red == NULL)
382 qtype(cl->cl_q) = Q_RED;
385 #endif /* ALTQ_RED */
387 if (flags & PRCF_CODEL) {
388 cl->cl_codel = codel_alloc(5, 100, 0);
389 if (cl->cl_codel != NULL)
390 qtype(cl->cl_q) = Q_CODEL;
397 if (cl->cl_red != NULL) {
399 if (q_is_rio(cl->cl_q))
400 rio_destroy((rio_t *)cl->cl_red);
403 if (q_is_red(cl->cl_q))
404 red_destroy(cl->cl_red);
407 if (q_is_codel(cl->cl_q))
408 codel_destroy(cl->cl_codel);
411 if (cl->cl_q != NULL)
412 free(cl->cl_q, M_DEVBUF);
418 priq_class_destroy(struct priq_class *cl)
424 IFQ_LOCK(cl->cl_pif->pif_ifq);
426 #ifdef ALTQ3_CLFIER_COMPAT
427 /* delete filters referencing to this class */
428 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
431 if (!qempty(cl->cl_q))
435 pif->pif_classes[cl->cl_pri] = NULL;
436 if (pif->pif_maxpri == cl->cl_pri) {
437 for (pri = cl->cl_pri; pri >= 0; pri--)
438 if (pif->pif_classes[pri] != NULL) {
439 pif->pif_maxpri = pri;
443 pif->pif_maxpri = -1;
445 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
448 if (cl->cl_red != NULL) {
450 if (q_is_rio(cl->cl_q))
451 rio_destroy((rio_t *)cl->cl_red);
454 if (q_is_red(cl->cl_q))
455 red_destroy(cl->cl_red);
458 if (q_is_codel(cl->cl_q))
459 codel_destroy(cl->cl_codel);
462 free(cl->cl_q, M_DEVBUF);
468 * priq_enqueue is an enqueue function to be registered to
469 * (*altq_enqueue) in struct ifaltq.
472 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
474 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
475 struct priq_class *cl;
479 IFQ_LOCK_ASSERT(ifq);
481 /* grab class set by classifier */
482 if ((m->m_flags & M_PKTHDR) == 0) {
483 /* should not happen */
484 printf("altq: packet for %s does not have pkthdr\n",
485 ifq->altq_ifp->if_xname);
490 if ((t = pf_find_mtag(m)) != NULL)
491 cl = clh_to_clp(pif, t->qid);
493 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
494 cl = pktattr->pattr_class;
497 cl = pif->pif_default;
505 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
508 cl->cl_pktattr = NULL;
510 if (priq_addq(cl, m) != 0) {
511 /* drop occurred. mbuf was freed in priq_addq. */
512 PKTCNTR_ADD(&cl->cl_dropcnt, len);
517 /* successfully queued. */
522 * priq_dequeue is a dequeue function to be registered to
523 * (*altq_dequeue) in struct ifaltq.
525 * note: ALTDQ_POLL returns the next packet without removing the packet
526 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
527 * ALTDQ_REMOVE must return the same packet if called immediately
531 priq_dequeue(struct ifaltq *ifq, int op)
533 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
534 struct priq_class *cl;
538 IFQ_LOCK_ASSERT(ifq);
540 if (IFQ_IS_EMPTY(ifq))
541 /* no packet in the queue */
544 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
545 if ((cl = pif->pif_classes[pri]) != NULL &&
547 if (op == ALTDQ_POLL)
548 return (priq_pollq(cl));
553 if (qempty(cl->cl_q))
555 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
564 priq_addq(struct priq_class *cl, struct mbuf *m)
568 if (q_is_rio(cl->cl_q))
569 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
573 if (q_is_red(cl->cl_q))
574 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
577 if (q_is_codel(cl->cl_q))
578 return codel_addq(cl->cl_codel, cl->cl_q, m);
580 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
585 if (cl->cl_flags & PRCF_CLEARDSCP)
586 write_dsfield(m, cl->cl_pktattr, 0);
594 priq_getq(struct priq_class *cl)
597 if (q_is_rio(cl->cl_q))
598 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
601 if (q_is_red(cl->cl_q))
602 return red_getq(cl->cl_red, cl->cl_q);
605 if (q_is_codel(cl->cl_q))
606 return codel_getq(cl->cl_codel, cl->cl_q);
608 return _getq(cl->cl_q);
613 struct priq_class *cl;
615 return qhead(cl->cl_q);
619 priq_purgeq(struct priq_class *cl)
623 if (qempty(cl->cl_q))
626 while ((m = _getq(cl->cl_q)) != NULL) {
627 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
630 ASSERT(qlen(cl->cl_q) == 0);
634 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
636 sp->class_handle = cl->cl_handle;
637 sp->qlength = qlen(cl->cl_q);
638 sp->qlimit = qlimit(cl->cl_q);
639 sp->period = cl->cl_period;
640 sp->xmitcnt = cl->cl_xmitcnt;
641 sp->dropcnt = cl->cl_dropcnt;
643 sp->qtype = qtype(cl->cl_q);
645 if (q_is_red(cl->cl_q))
646 red_getstats(cl->cl_red, &sp->red[0]);
649 if (q_is_rio(cl->cl_q))
650 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
653 if (q_is_codel(cl->cl_q))
654 codel_getstats(cl->cl_codel, &sp->codel);
658 /* convert a class handle to the corresponding class pointer */
659 static struct priq_class *
660 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
662 struct priq_class *cl;
668 for (idx = pif->pif_maxpri; idx >= 0; idx--)
669 if ((cl = pif->pif_classes[idx]) != NULL &&
670 cl->cl_handle == chandle)
679 static struct priq_if *
680 priq_attach(ifq, bandwidth)
686 pif = malloc(sizeof(struct priq_if),
690 bzero(pif, sizeof(struct priq_if));
691 pif->pif_bandwidth = bandwidth;
692 pif->pif_maxpri = -1;
695 /* add this state to the priq list */
696 pif->pif_next = pif_list;
706 (void)priq_clear_interface(pif);
708 /* remove this interface from the pif list */
710 pif_list = pif->pif_next;
714 for (p = pif_list; p != NULL; p = p->pif_next)
715 if (p->pif_next == pif) {
716 p->pif_next = pif->pif_next;
727 * priq device interface
730 priqopen(dev, flag, fmt, p)
733 #if (__FreeBSD_version > 500000)
739 /* everything will be done when the queueing scheme is attached. */
744 priqclose(dev, flag, fmt, p)
747 #if (__FreeBSD_version > 500000)
756 while ((pif = pif_list) != NULL) {
758 if (ALTQ_IS_ENABLED(pif->pif_ifq))
759 altq_disable(pif->pif_ifq);
761 err = altq_detach(pif->pif_ifq);
763 err = priq_detach(pif);
764 if (err != 0 && error == 0)
772 priqioctl(dev, cmd, addr, flag, p)
777 #if (__FreeBSD_version > 500000)
784 struct priq_interface *ifacep;
787 /* check super-user privilege */
792 #if (__FreeBSD_version > 700000)
793 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
795 #elsif (__FreeBSD_version > 400000)
796 if ((error = suser(p)) != 0)
799 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
808 error = priqcmd_if_attach((struct priq_interface *)addr);
812 error = priqcmd_if_detach((struct priq_interface *)addr);
818 ifacep = (struct priq_interface *)addr;
819 if ((pif = altq_lookup(ifacep->ifname,
820 ALTQT_PRIQ)) == NULL) {
827 if (pif->pif_default == NULL) {
829 printf("priq: no default class\n");
834 error = altq_enable(pif->pif_ifq);
838 error = altq_disable(pif->pif_ifq);
842 priq_clear_interface(pif);
848 error = priqcmd_add_class((struct priq_add_class *)addr);
852 error = priqcmd_delete_class((struct priq_delete_class *)addr);
856 error = priqcmd_modify_class((struct priq_modify_class *)addr);
859 case PRIQ_ADD_FILTER:
860 error = priqcmd_add_filter((struct priq_add_filter *)addr);
863 case PRIQ_DEL_FILTER:
864 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
868 error = priqcmd_class_stats((struct priq_class_stats *)addr);
879 priqcmd_if_attach(ap)
880 struct priq_interface *ap;
886 if ((ifp = ifunit(ap->ifname)) == NULL)
889 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
893 * set PRIQ to this ifnet structure.
895 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
896 priq_enqueue, priq_dequeue, priq_request,
897 &pif->pif_classifier, acc_classify)) != 0)
898 (void)priq_detach(pif);
904 priqcmd_if_detach(ap)
905 struct priq_interface *ap;
910 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
913 if (ALTQ_IS_ENABLED(pif->pif_ifq))
914 altq_disable(pif->pif_ifq);
916 if ((error = altq_detach(pif->pif_ifq)))
919 return priq_detach(pif);
923 priqcmd_add_class(ap)
924 struct priq_add_class *ap;
927 struct priq_class *cl;
930 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
933 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
935 if (pif->pif_classes[ap->pri] != NULL)
939 if ((cl = priq_class_create(pif, ap->pri,
940 ap->qlimit, ap->flags, qid)) == NULL)
943 /* return a class handle to the user */
944 ap->class_handle = cl->cl_handle;
950 priqcmd_delete_class(ap)
951 struct priq_delete_class *ap;
954 struct priq_class *cl;
956 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
959 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
962 return priq_class_destroy(cl);
966 priqcmd_modify_class(ap)
967 struct priq_modify_class *ap;
970 struct priq_class *cl;
972 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
975 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
978 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
982 * if priority is changed, move the class to the new priority
984 if (pif->pif_classes[ap->pri] != cl) {
985 if (pif->pif_classes[ap->pri] != NULL)
987 pif->pif_classes[cl->cl_pri] = NULL;
988 pif->pif_classes[ap->pri] = cl;
989 cl->cl_pri = ap->pri;
992 /* call priq_class_create to change class parameters */
993 if ((cl = priq_class_create(pif, ap->pri,
994 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
1000 priqcmd_add_filter(ap)
1001 struct priq_add_filter *ap;
1003 struct priq_if *pif;
1004 struct priq_class *cl;
1006 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1009 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
1012 return acc_add_filter(&pif->pif_classifier, &ap->filter,
1013 cl, &ap->filter_handle);
1017 priqcmd_delete_filter(ap)
1018 struct priq_delete_filter *ap;
1020 struct priq_if *pif;
1022 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1025 return acc_delete_filter(&pif->pif_classifier,
1030 priqcmd_class_stats(ap)
1031 struct priq_class_stats *ap;
1033 struct priq_if *pif;
1034 struct priq_class *cl;
1035 struct priq_classstats stats, *usp;
1038 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1041 ap->maxpri = pif->pif_maxpri;
1043 /* then, read the next N classes in the tree */
1045 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1046 cl = pif->pif_classes[pri];
1048 get_class_stats(&stats, cl);
1050 bzero(&stats, sizeof(stats));
1051 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1052 sizeof(stats))) != 0)
1060 static struct altqsw priq_sw =
1061 {"priq", priqopen, priqclose, priqioctl};
1063 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1064 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1065 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1067 #endif /* KLD_MODULE */
1069 #endif /* ALTQ3_COMPAT */
1070 #endif /* ALTQ_PRIQ */