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__)
34 #if (__FreeBSD__ != 2)
37 #include "opt_inet6.h"
40 #endif /* __FreeBSD__ || __NetBSD__ */
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>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/systm.h>
51 #include <sys/errno.h>
52 #include <sys/kernel.h>
53 #include <sys/queue.h>
56 #include <netinet/in.h>
58 #include <net/pfvar.h>
59 #include <altq/altq.h>
61 #include <altq/altq_conf.h>
63 #include <altq/altq_priq.h>
69 static struct priq_if *priq_attach(struct ifaltq *, u_int);
70 static int priq_detach(struct priq_if *);
72 static int priq_clear_interface(struct priq_if *);
73 static int priq_request(struct ifaltq *, int, void *);
74 static void priq_purge(struct priq_if *);
75 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
77 static int priq_class_destroy(struct priq_class *);
78 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
79 static struct mbuf *priq_dequeue(struct ifaltq *, int);
81 static int priq_addq(struct priq_class *, struct mbuf *);
82 static struct mbuf *priq_getq(struct priq_class *);
83 static struct mbuf *priq_pollq(struct priq_class *);
84 static void priq_purgeq(struct priq_class *);
87 static int priqcmd_if_attach(struct priq_interface *);
88 static int priqcmd_if_detach(struct priq_interface *);
89 static int priqcmd_add_class(struct priq_add_class *);
90 static int priqcmd_delete_class(struct priq_delete_class *);
91 static int priqcmd_modify_class(struct priq_modify_class *);
92 static int priqcmd_add_filter(struct priq_add_filter *);
93 static int priqcmd_delete_filter(struct priq_delete_filter *);
94 static int priqcmd_class_stats(struct priq_class_stats *);
95 #endif /* ALTQ3_COMPAT */
97 static void get_class_stats(struct priq_classstats *, struct priq_class *);
98 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
103 /* pif_list keeps all priq_if's allocated. */
104 static struct priq_if *pif_list = NULL;
105 #endif /* ALTQ3_COMPAT */
108 priq_pfattach(struct pf_altq *a)
113 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
120 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
121 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
127 priq_add_altq(struct pf_altq *a)
132 if ((ifp = ifunit(a->ifname)) == NULL)
134 if (!ALTQ_IS_READY(&ifp->if_snd))
137 MALLOC(pif, struct priq_if *, sizeof(struct priq_if),
141 bzero(pif, sizeof(struct priq_if));
142 pif->pif_bandwidth = a->ifbandwidth;
143 pif->pif_maxpri = -1;
144 pif->pif_ifq = &ifp->if_snd;
146 /* keep the state in pf_altq */
153 priq_remove_altq(struct pf_altq *a)
157 if ((pif = a->altq_disc) == NULL)
161 (void)priq_clear_interface(pif);
168 priq_add_queue(struct pf_altq *a)
171 struct priq_class *cl;
173 if ((pif = a->altq_disc) == NULL)
176 /* check parameters */
177 if (a->priority >= PRIQ_MAXPRI)
181 if (pif->pif_classes[a->priority] != NULL)
183 if (clh_to_clp(pif, a->qid) != NULL)
186 cl = priq_class_create(pif, a->priority, a->qlimit,
187 a->pq_u.priq_opts.flags, a->qid);
195 priq_remove_queue(struct pf_altq *a)
198 struct priq_class *cl;
200 if ((pif = a->altq_disc) == NULL)
203 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
206 return (priq_class_destroy(cl));
210 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
213 struct priq_class *cl;
214 struct priq_classstats stats;
217 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
220 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
223 if (*nbytes < sizeof(stats))
226 get_class_stats(&stats, cl);
228 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
230 *nbytes = sizeof(stats);
235 * bring the interface back to the initial state by discarding
236 * all the filters and classes.
239 priq_clear_interface(struct priq_if *pif)
241 struct priq_class *cl;
244 #ifdef ALTQ3_CLFIER_COMPAT
245 /* free the filters for this interface */
246 acc_discard_filters(&pif->pif_classifier, NULL, 1);
249 /* clear out the classes */
250 for (pri = 0; pri <= pif->pif_maxpri; pri++)
251 if ((cl = pif->pif_classes[pri]) != NULL)
252 priq_class_destroy(cl);
258 priq_request(struct ifaltq *ifq, int req, void *arg)
260 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
262 IFQ_LOCK_ASSERT(ifq);
272 /* discard all the queued packets on the interface */
274 priq_purge(struct priq_if *pif)
276 struct priq_class *cl;
279 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
280 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
283 if (ALTQ_IS_ENABLED(pif->pif_ifq))
284 pif->pif_ifq->ifq_len = 0;
287 static struct priq_class *
288 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
290 struct priq_class *cl;
294 if (flags & PRCF_RED) {
296 printf("priq_class_create: RED not configured for PRIQ!\n");
302 if ((cl = pif->pif_classes[pri]) != NULL) {
303 /* modify the class instead of creating a new one */
309 IFQ_LOCK(cl->cl_pif->pif_ifq);
310 if (!qempty(cl->cl_q))
312 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
315 if (q_is_rio(cl->cl_q))
316 rio_destroy((rio_t *)cl->cl_red);
319 if (q_is_red(cl->cl_q))
320 red_destroy(cl->cl_red);
323 MALLOC(cl, struct priq_class *, sizeof(struct priq_class),
327 bzero(cl, sizeof(struct priq_class));
329 MALLOC(cl->cl_q, class_queue_t *, sizeof(class_queue_t),
331 if (cl->cl_q == NULL)
333 bzero(cl->cl_q, sizeof(class_queue_t));
336 pif->pif_classes[pri] = cl;
337 if (flags & PRCF_DEFAULTCLASS)
338 pif->pif_default = cl;
340 qlimit = 50; /* use default */
341 qlimit(cl->cl_q) = qlimit;
342 qtype(cl->cl_q) = Q_DROPTAIL;
344 cl->cl_flags = flags;
346 if (pri > pif->pif_maxpri)
347 pif->pif_maxpri = pri;
352 if (flags & (PRCF_RED|PRCF_RIO)) {
353 int red_flags, red_pkttime;
356 if (flags & PRCF_ECN)
357 red_flags |= REDF_ECN;
359 if (flags & PRCF_CLEARDSCP)
360 red_flags |= RIOF_CLEARDSCP;
362 if (pif->pif_bandwidth < 8)
363 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
365 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
366 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
368 if (flags & PRCF_RIO) {
369 cl->cl_red = (red_t *)rio_alloc(0, NULL,
370 red_flags, red_pkttime);
371 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)
381 qtype(cl->cl_q) = Q_RED;
384 #endif /* ALTQ_RED */
389 if (cl->cl_red != NULL) {
391 if (q_is_rio(cl->cl_q))
392 rio_destroy((rio_t *)cl->cl_red);
395 if (q_is_red(cl->cl_q))
396 red_destroy(cl->cl_red);
399 if (cl->cl_q != NULL)
400 FREE(cl->cl_q, M_DEVBUF);
406 priq_class_destroy(struct priq_class *cl)
416 IFQ_LOCK(cl->cl_pif->pif_ifq);
418 #ifdef ALTQ3_CLFIER_COMPAT
419 /* delete filters referencing to this class */
420 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
423 if (!qempty(cl->cl_q))
427 pif->pif_classes[cl->cl_pri] = NULL;
428 if (pif->pif_maxpri == cl->cl_pri) {
429 for (pri = cl->cl_pri; pri >= 0; pri--)
430 if (pif->pif_classes[pri] != NULL) {
431 pif->pif_maxpri = pri;
435 pif->pif_maxpri = -1;
437 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
440 if (cl->cl_red != NULL) {
442 if (q_is_rio(cl->cl_q))
443 rio_destroy((rio_t *)cl->cl_red);
446 if (q_is_red(cl->cl_q))
447 red_destroy(cl->cl_red);
450 FREE(cl->cl_q, M_DEVBUF);
456 * priq_enqueue is an enqueue function to be registered to
457 * (*altq_enqueue) in struct ifaltq.
460 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
462 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
463 struct priq_class *cl;
467 IFQ_LOCK_ASSERT(ifq);
469 /* grab class set by classifier */
470 if ((m->m_flags & M_PKTHDR) == 0) {
471 /* should not happen */
472 #if defined(__NetBSD__) || defined(__OpenBSD__)\
473 || (defined(__FreeBSD__) && __FreeBSD_version >= 501113)
474 printf("altq: packet for %s does not have pkthdr\n",
475 ifq->altq_ifp->if_xname);
477 printf("altq: packet for %s%d does not have pkthdr\n",
478 ifq->altq_ifp->if_name, ifq->altq_ifp->if_unit);
484 if ((t = pf_find_mtag(m)) != NULL)
485 cl = clh_to_clp(pif, t->qid);
487 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
488 cl = pktattr->pattr_class;
491 cl = pif->pif_default;
499 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
502 cl->cl_pktattr = NULL;
504 if (priq_addq(cl, m) != 0) {
505 /* drop occurred. mbuf was freed in priq_addq. */
506 PKTCNTR_ADD(&cl->cl_dropcnt, len);
511 /* successfully queued. */
516 * priq_dequeue is a dequeue function to be registered to
517 * (*altq_dequeue) in struct ifaltq.
519 * note: ALTDQ_POLL returns the next packet without removing the packet
520 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
521 * ALTDQ_REMOVE must return the same packet if called immediately
525 priq_dequeue(struct ifaltq *ifq, int op)
527 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
528 struct priq_class *cl;
532 IFQ_LOCK_ASSERT(ifq);
534 if (IFQ_IS_EMPTY(ifq))
535 /* no packet in the queue */
538 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
539 if ((cl = pif->pif_classes[pri]) != NULL &&
541 if (op == ALTDQ_POLL)
542 return (priq_pollq(cl));
547 if (qempty(cl->cl_q))
549 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
558 priq_addq(struct priq_class *cl, struct mbuf *m)
562 if (q_is_rio(cl->cl_q))
563 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
567 if (q_is_red(cl->cl_q))
568 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
570 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
575 if (cl->cl_flags & PRCF_CLEARDSCP)
576 write_dsfield(m, cl->cl_pktattr, 0);
584 priq_getq(struct priq_class *cl)
587 if (q_is_rio(cl->cl_q))
588 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
591 if (q_is_red(cl->cl_q))
592 return red_getq(cl->cl_red, cl->cl_q);
594 return _getq(cl->cl_q);
599 struct priq_class *cl;
601 return qhead(cl->cl_q);
605 priq_purgeq(struct priq_class *cl)
609 if (qempty(cl->cl_q))
612 while ((m = _getq(cl->cl_q)) != NULL) {
613 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
616 ASSERT(qlen(cl->cl_q) == 0);
620 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
622 sp->class_handle = cl->cl_handle;
623 sp->qlength = qlen(cl->cl_q);
624 sp->qlimit = qlimit(cl->cl_q);
625 sp->period = cl->cl_period;
626 sp->xmitcnt = cl->cl_xmitcnt;
627 sp->dropcnt = cl->cl_dropcnt;
629 sp->qtype = qtype(cl->cl_q);
631 if (q_is_red(cl->cl_q))
632 red_getstats(cl->cl_red, &sp->red[0]);
635 if (q_is_rio(cl->cl_q))
636 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
641 /* convert a class handle to the corresponding class pointer */
642 static struct priq_class *
643 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
645 struct priq_class *cl;
651 for (idx = pif->pif_maxpri; idx >= 0; idx--)
652 if ((cl = pif->pif_classes[idx]) != NULL &&
653 cl->cl_handle == chandle)
662 static struct priq_if *
663 priq_attach(ifq, bandwidth)
669 MALLOC(pif, struct priq_if *, sizeof(struct priq_if),
673 bzero(pif, sizeof(struct priq_if));
674 pif->pif_bandwidth = bandwidth;
675 pif->pif_maxpri = -1;
678 /* add this state to the priq list */
679 pif->pif_next = pif_list;
689 (void)priq_clear_interface(pif);
691 /* remove this interface from the pif list */
693 pif_list = pif->pif_next;
697 for (p = pif_list; p != NULL; p = p->pif_next)
698 if (p->pif_next == pif) {
699 p->pif_next = pif->pif_next;
710 * priq device interface
713 priqopen(dev, flag, fmt, p)
716 #if (__FreeBSD_version > 500000)
722 /* everything will be done when the queueing scheme is attached. */
727 priqclose(dev, flag, fmt, p)
730 #if (__FreeBSD_version > 500000)
739 while ((pif = pif_list) != NULL) {
741 if (ALTQ_IS_ENABLED(pif->pif_ifq))
742 altq_disable(pif->pif_ifq);
744 err = altq_detach(pif->pif_ifq);
746 err = priq_detach(pif);
747 if (err != 0 && error == 0)
755 priqioctl(dev, cmd, addr, flag, p)
760 #if (__FreeBSD_version > 500000)
767 struct priq_interface *ifacep;
770 /* check super-user privilege */
775 #if (__FreeBSD_version > 700000)
776 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
778 #elsif (__FreeBSD_version > 400000)
779 if ((error = suser(p)) != 0)
782 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
791 error = priqcmd_if_attach((struct priq_interface *)addr);
795 error = priqcmd_if_detach((struct priq_interface *)addr);
801 ifacep = (struct priq_interface *)addr;
802 if ((pif = altq_lookup(ifacep->ifname,
803 ALTQT_PRIQ)) == NULL) {
810 if (pif->pif_default == NULL) {
812 printf("priq: no default class\n");
817 error = altq_enable(pif->pif_ifq);
821 error = altq_disable(pif->pif_ifq);
825 priq_clear_interface(pif);
831 error = priqcmd_add_class((struct priq_add_class *)addr);
835 error = priqcmd_delete_class((struct priq_delete_class *)addr);
839 error = priqcmd_modify_class((struct priq_modify_class *)addr);
842 case PRIQ_ADD_FILTER:
843 error = priqcmd_add_filter((struct priq_add_filter *)addr);
846 case PRIQ_DEL_FILTER:
847 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
851 error = priqcmd_class_stats((struct priq_class_stats *)addr);
862 priqcmd_if_attach(ap)
863 struct priq_interface *ap;
869 if ((ifp = ifunit(ap->ifname)) == NULL)
872 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
876 * set PRIQ to this ifnet structure.
878 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
879 priq_enqueue, priq_dequeue, priq_request,
880 &pif->pif_classifier, acc_classify)) != 0)
881 (void)priq_detach(pif);
887 priqcmd_if_detach(ap)
888 struct priq_interface *ap;
893 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
896 if (ALTQ_IS_ENABLED(pif->pif_ifq))
897 altq_disable(pif->pif_ifq);
899 if ((error = altq_detach(pif->pif_ifq)))
902 return priq_detach(pif);
906 priqcmd_add_class(ap)
907 struct priq_add_class *ap;
910 struct priq_class *cl;
913 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
916 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
918 if (pif->pif_classes[ap->pri] != NULL)
922 if ((cl = priq_class_create(pif, ap->pri,
923 ap->qlimit, ap->flags, qid)) == NULL)
926 /* return a class handle to the user */
927 ap->class_handle = cl->cl_handle;
933 priqcmd_delete_class(ap)
934 struct priq_delete_class *ap;
937 struct priq_class *cl;
939 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
942 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
945 return priq_class_destroy(cl);
949 priqcmd_modify_class(ap)
950 struct priq_modify_class *ap;
953 struct priq_class *cl;
955 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
958 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
961 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
965 * if priority is changed, move the class to the new priority
967 if (pif->pif_classes[ap->pri] != cl) {
968 if (pif->pif_classes[ap->pri] != NULL)
970 pif->pif_classes[cl->cl_pri] = NULL;
971 pif->pif_classes[ap->pri] = cl;
972 cl->cl_pri = ap->pri;
975 /* call priq_class_create to change class parameters */
976 if ((cl = priq_class_create(pif, ap->pri,
977 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
983 priqcmd_add_filter(ap)
984 struct priq_add_filter *ap;
987 struct priq_class *cl;
989 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
992 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
995 return acc_add_filter(&pif->pif_classifier, &ap->filter,
996 cl, &ap->filter_handle);
1000 priqcmd_delete_filter(ap)
1001 struct priq_delete_filter *ap;
1003 struct priq_if *pif;
1005 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1008 return acc_delete_filter(&pif->pif_classifier,
1013 priqcmd_class_stats(ap)
1014 struct priq_class_stats *ap;
1016 struct priq_if *pif;
1017 struct priq_class *cl;
1018 struct priq_classstats stats, *usp;
1021 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1024 ap->maxpri = pif->pif_maxpri;
1026 /* then, read the next N classes in the tree */
1028 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1029 cl = pif->pif_classes[pri];
1031 get_class_stats(&stats, cl);
1033 bzero(&stats, sizeof(stats));
1034 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1035 sizeof(stats))) != 0)
1043 static struct altqsw priq_sw =
1044 {"priq", priqopen, priqclose, priqioctl};
1046 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1047 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1048 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1050 #endif /* KLD_MODULE */
1052 #endif /* ALTQ3_COMPAT */
1053 #endif /* ALTQ_PRIQ */