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),
139 bzero(pif, sizeof(struct priq_if));
140 pif->pif_bandwidth = a->ifbandwidth;
141 pif->pif_maxpri = -1;
142 pif->pif_ifq = &ifp->if_snd;
144 /* keep the state in pf_altq */
151 priq_remove_altq(struct pf_altq *a)
155 if ((pif = a->altq_disc) == NULL)
159 (void)priq_clear_interface(pif);
166 priq_add_queue(struct pf_altq *a)
169 struct priq_class *cl;
171 if ((pif = a->altq_disc) == NULL)
174 /* check parameters */
175 if (a->priority >= PRIQ_MAXPRI)
179 if (pif->pif_classes[a->priority] != NULL)
181 if (clh_to_clp(pif, a->qid) != NULL)
184 cl = priq_class_create(pif, a->priority, a->qlimit,
185 a->pq_u.priq_opts.flags, a->qid);
193 priq_remove_queue(struct pf_altq *a)
196 struct priq_class *cl;
198 if ((pif = a->altq_disc) == NULL)
201 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
204 return (priq_class_destroy(cl));
208 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
211 struct priq_class *cl;
212 struct priq_classstats stats;
215 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
218 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
221 if (*nbytes < sizeof(stats))
224 get_class_stats(&stats, cl);
226 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
228 *nbytes = sizeof(stats);
233 * bring the interface back to the initial state by discarding
234 * all the filters and classes.
237 priq_clear_interface(struct priq_if *pif)
239 struct priq_class *cl;
242 #ifdef ALTQ3_CLFIER_COMPAT
243 /* free the filters for this interface */
244 acc_discard_filters(&pif->pif_classifier, NULL, 1);
247 /* clear out the classes */
248 for (pri = 0; pri <= pif->pif_maxpri; pri++)
249 if ((cl = pif->pif_classes[pri]) != NULL)
250 priq_class_destroy(cl);
256 priq_request(struct ifaltq *ifq, int req, void *arg)
258 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
260 IFQ_LOCK_ASSERT(ifq);
270 /* discard all the queued packets on the interface */
272 priq_purge(struct priq_if *pif)
274 struct priq_class *cl;
277 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
278 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
281 if (ALTQ_IS_ENABLED(pif->pif_ifq))
282 pif->pif_ifq->ifq_len = 0;
285 static struct priq_class *
286 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
288 struct priq_class *cl;
292 if (flags & PRCF_RED) {
294 printf("priq_class_create: RED not configured for PRIQ!\n");
300 if ((cl = pif->pif_classes[pri]) != NULL) {
301 /* modify the class instead of creating a new one */
307 IFQ_LOCK(cl->cl_pif->pif_ifq);
308 if (!qempty(cl->cl_q))
310 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
313 if (q_is_rio(cl->cl_q))
314 rio_destroy((rio_t *)cl->cl_red);
317 if (q_is_red(cl->cl_q))
318 red_destroy(cl->cl_red);
321 cl = malloc(sizeof(struct priq_class),
325 bzero(cl, sizeof(struct priq_class));
327 cl->cl_q = malloc(sizeof(class_queue_t),
329 if (cl->cl_q == NULL)
331 bzero(cl->cl_q, sizeof(class_queue_t));
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;
342 cl->cl_flags = flags;
344 if (pri > pif->pif_maxpri)
345 pif->pif_maxpri = pri;
350 if (flags & (PRCF_RED|PRCF_RIO)) {
351 int red_flags, red_pkttime;
354 if (flags & PRCF_ECN)
355 red_flags |= REDF_ECN;
357 if (flags & PRCF_CLEARDSCP)
358 red_flags |= RIOF_CLEARDSCP;
360 if (pif->pif_bandwidth < 8)
361 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
363 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
364 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
366 if (flags & PRCF_RIO) {
367 cl->cl_red = (red_t *)rio_alloc(0, NULL,
368 red_flags, red_pkttime);
369 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)
379 qtype(cl->cl_q) = Q_RED;
382 #endif /* ALTQ_RED */
387 if (cl->cl_red != NULL) {
389 if (q_is_rio(cl->cl_q))
390 rio_destroy((rio_t *)cl->cl_red);
393 if (q_is_red(cl->cl_q))
394 red_destroy(cl->cl_red);
397 if (cl->cl_q != NULL)
398 free(cl->cl_q, M_DEVBUF);
404 priq_class_destroy(struct priq_class *cl)
414 IFQ_LOCK(cl->cl_pif->pif_ifq);
416 #ifdef ALTQ3_CLFIER_COMPAT
417 /* delete filters referencing to this class */
418 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
421 if (!qempty(cl->cl_q))
425 pif->pif_classes[cl->cl_pri] = NULL;
426 if (pif->pif_maxpri == cl->cl_pri) {
427 for (pri = cl->cl_pri; pri >= 0; pri--)
428 if (pif->pif_classes[pri] != NULL) {
429 pif->pif_maxpri = pri;
433 pif->pif_maxpri = -1;
435 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
438 if (cl->cl_red != NULL) {
440 if (q_is_rio(cl->cl_q))
441 rio_destroy((rio_t *)cl->cl_red);
444 if (q_is_red(cl->cl_q))
445 red_destroy(cl->cl_red);
448 free(cl->cl_q, M_DEVBUF);
454 * priq_enqueue is an enqueue function to be registered to
455 * (*altq_enqueue) in struct ifaltq.
458 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
460 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
461 struct priq_class *cl;
465 IFQ_LOCK_ASSERT(ifq);
467 /* grab class set by classifier */
468 if ((m->m_flags & M_PKTHDR) == 0) {
469 /* should not happen */
470 printf("altq: packet for %s does not have pkthdr\n",
471 ifq->altq_ifp->if_xname);
476 if ((t = pf_find_mtag(m)) != NULL)
477 cl = clh_to_clp(pif, t->qid);
479 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
480 cl = pktattr->pattr_class;
483 cl = pif->pif_default;
491 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
494 cl->cl_pktattr = NULL;
496 if (priq_addq(cl, m) != 0) {
497 /* drop occurred. mbuf was freed in priq_addq. */
498 PKTCNTR_ADD(&cl->cl_dropcnt, len);
503 /* successfully queued. */
508 * priq_dequeue is a dequeue function to be registered to
509 * (*altq_dequeue) in struct ifaltq.
511 * note: ALTDQ_POLL returns the next packet without removing the packet
512 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
513 * ALTDQ_REMOVE must return the same packet if called immediately
517 priq_dequeue(struct ifaltq *ifq, int op)
519 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
520 struct priq_class *cl;
524 IFQ_LOCK_ASSERT(ifq);
526 if (IFQ_IS_EMPTY(ifq))
527 /* no packet in the queue */
530 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
531 if ((cl = pif->pif_classes[pri]) != NULL &&
533 if (op == ALTDQ_POLL)
534 return (priq_pollq(cl));
539 if (qempty(cl->cl_q))
541 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
550 priq_addq(struct priq_class *cl, struct mbuf *m)
554 if (q_is_rio(cl->cl_q))
555 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
559 if (q_is_red(cl->cl_q))
560 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
562 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
567 if (cl->cl_flags & PRCF_CLEARDSCP)
568 write_dsfield(m, cl->cl_pktattr, 0);
576 priq_getq(struct priq_class *cl)
579 if (q_is_rio(cl->cl_q))
580 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
583 if (q_is_red(cl->cl_q))
584 return red_getq(cl->cl_red, cl->cl_q);
586 return _getq(cl->cl_q);
591 struct priq_class *cl;
593 return qhead(cl->cl_q);
597 priq_purgeq(struct priq_class *cl)
601 if (qempty(cl->cl_q))
604 while ((m = _getq(cl->cl_q)) != NULL) {
605 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
608 ASSERT(qlen(cl->cl_q) == 0);
612 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
614 sp->class_handle = cl->cl_handle;
615 sp->qlength = qlen(cl->cl_q);
616 sp->qlimit = qlimit(cl->cl_q);
617 sp->period = cl->cl_period;
618 sp->xmitcnt = cl->cl_xmitcnt;
619 sp->dropcnt = cl->cl_dropcnt;
621 sp->qtype = qtype(cl->cl_q);
623 if (q_is_red(cl->cl_q))
624 red_getstats(cl->cl_red, &sp->red[0]);
627 if (q_is_rio(cl->cl_q))
628 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
633 /* convert a class handle to the corresponding class pointer */
634 static struct priq_class *
635 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
637 struct priq_class *cl;
643 for (idx = pif->pif_maxpri; idx >= 0; idx--)
644 if ((cl = pif->pif_classes[idx]) != NULL &&
645 cl->cl_handle == chandle)
654 static struct priq_if *
655 priq_attach(ifq, bandwidth)
661 pif = malloc(sizeof(struct priq_if),
665 bzero(pif, sizeof(struct priq_if));
666 pif->pif_bandwidth = bandwidth;
667 pif->pif_maxpri = -1;
670 /* add this state to the priq list */
671 pif->pif_next = pif_list;
681 (void)priq_clear_interface(pif);
683 /* remove this interface from the pif list */
685 pif_list = pif->pif_next;
689 for (p = pif_list; p != NULL; p = p->pif_next)
690 if (p->pif_next == pif) {
691 p->pif_next = pif->pif_next;
702 * priq device interface
705 priqopen(dev, flag, fmt, p)
708 #if (__FreeBSD_version > 500000)
714 /* everything will be done when the queueing scheme is attached. */
719 priqclose(dev, flag, fmt, p)
722 #if (__FreeBSD_version > 500000)
731 while ((pif = pif_list) != NULL) {
733 if (ALTQ_IS_ENABLED(pif->pif_ifq))
734 altq_disable(pif->pif_ifq);
736 err = altq_detach(pif->pif_ifq);
738 err = priq_detach(pif);
739 if (err != 0 && error == 0)
747 priqioctl(dev, cmd, addr, flag, p)
752 #if (__FreeBSD_version > 500000)
759 struct priq_interface *ifacep;
762 /* check super-user privilege */
767 #if (__FreeBSD_version > 700000)
768 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
770 #elsif (__FreeBSD_version > 400000)
771 if ((error = suser(p)) != 0)
774 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
783 error = priqcmd_if_attach((struct priq_interface *)addr);
787 error = priqcmd_if_detach((struct priq_interface *)addr);
793 ifacep = (struct priq_interface *)addr;
794 if ((pif = altq_lookup(ifacep->ifname,
795 ALTQT_PRIQ)) == NULL) {
802 if (pif->pif_default == NULL) {
804 printf("priq: no default class\n");
809 error = altq_enable(pif->pif_ifq);
813 error = altq_disable(pif->pif_ifq);
817 priq_clear_interface(pif);
823 error = priqcmd_add_class((struct priq_add_class *)addr);
827 error = priqcmd_delete_class((struct priq_delete_class *)addr);
831 error = priqcmd_modify_class((struct priq_modify_class *)addr);
834 case PRIQ_ADD_FILTER:
835 error = priqcmd_add_filter((struct priq_add_filter *)addr);
838 case PRIQ_DEL_FILTER:
839 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
843 error = priqcmd_class_stats((struct priq_class_stats *)addr);
854 priqcmd_if_attach(ap)
855 struct priq_interface *ap;
861 if ((ifp = ifunit(ap->ifname)) == NULL)
864 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
868 * set PRIQ to this ifnet structure.
870 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
871 priq_enqueue, priq_dequeue, priq_request,
872 &pif->pif_classifier, acc_classify)) != 0)
873 (void)priq_detach(pif);
879 priqcmd_if_detach(ap)
880 struct priq_interface *ap;
885 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
888 if (ALTQ_IS_ENABLED(pif->pif_ifq))
889 altq_disable(pif->pif_ifq);
891 if ((error = altq_detach(pif->pif_ifq)))
894 return priq_detach(pif);
898 priqcmd_add_class(ap)
899 struct priq_add_class *ap;
902 struct priq_class *cl;
905 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
908 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
910 if (pif->pif_classes[ap->pri] != NULL)
914 if ((cl = priq_class_create(pif, ap->pri,
915 ap->qlimit, ap->flags, qid)) == NULL)
918 /* return a class handle to the user */
919 ap->class_handle = cl->cl_handle;
925 priqcmd_delete_class(ap)
926 struct priq_delete_class *ap;
929 struct priq_class *cl;
931 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
934 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
937 return priq_class_destroy(cl);
941 priqcmd_modify_class(ap)
942 struct priq_modify_class *ap;
945 struct priq_class *cl;
947 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
950 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
953 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
957 * if priority is changed, move the class to the new priority
959 if (pif->pif_classes[ap->pri] != cl) {
960 if (pif->pif_classes[ap->pri] != NULL)
962 pif->pif_classes[cl->cl_pri] = NULL;
963 pif->pif_classes[ap->pri] = cl;
964 cl->cl_pri = ap->pri;
967 /* call priq_class_create to change class parameters */
968 if ((cl = priq_class_create(pif, ap->pri,
969 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
975 priqcmd_add_filter(ap)
976 struct priq_add_filter *ap;
979 struct priq_class *cl;
981 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
984 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
987 return acc_add_filter(&pif->pif_classifier, &ap->filter,
988 cl, &ap->filter_handle);
992 priqcmd_delete_filter(ap)
993 struct priq_delete_filter *ap;
997 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1000 return acc_delete_filter(&pif->pif_classifier,
1005 priqcmd_class_stats(ap)
1006 struct priq_class_stats *ap;
1008 struct priq_if *pif;
1009 struct priq_class *cl;
1010 struct priq_classstats stats, *usp;
1013 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1016 ap->maxpri = pif->pif_maxpri;
1018 /* then, read the next N classes in the tree */
1020 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1021 cl = pif->pif_classes[pri];
1023 get_class_stats(&stats, cl);
1025 bzero(&stats, sizeof(stats));
1026 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1027 sizeof(stats))) != 0)
1035 static struct altqsw priq_sw =
1036 {"priq", priqopen, priqclose, priqioctl};
1038 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1039 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1040 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1042 #endif /* KLD_MODULE */
1044 #endif /* ALTQ3_COMPAT */
1045 #endif /* ALTQ_PRIQ */