Home | History | Annotate | Line # | Download | only in altq
altq_priq.c revision 1.15
      1  1.15     peter /*	$NetBSD: altq_priq.c,v 1.15 2006/10/12 19:59:08 peter Exp $	*/
      2  1.15     peter /*	$KAME: altq_priq.c,v 1.13 2005/04/13 03:44:25 suz Exp $	*/
      3   1.1   thorpej /*
      4  1.15     peter  * Copyright (C) 2000-2003
      5   1.1   thorpej  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  *
     16   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     17   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     20   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1   thorpej  * SUCH DAMAGE.
     27   1.1   thorpej  */
     28  1.15     peter 
     29   1.1   thorpej /*
     30   1.1   thorpej  * priority queue
     31   1.1   thorpej  */
     32   1.5     lukem 
     33   1.5     lukem #include <sys/cdefs.h>
     34  1.15     peter __KERNEL_RCSID(0, "$NetBSD: altq_priq.c,v 1.15 2006/10/12 19:59:08 peter Exp $");
     35   1.1   thorpej 
     36  1.15     peter #ifdef _KERNEL_OPT
     37   1.1   thorpej #include "opt_altq.h"
     38   1.1   thorpej #include "opt_inet.h"
     39   1.1   thorpej #endif
     40   1.1   thorpej 
     41   1.1   thorpej #ifdef ALTQ_PRIQ  /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
     42   1.1   thorpej 
     43   1.1   thorpej #include <sys/param.h>
     44   1.1   thorpej #include <sys/malloc.h>
     45   1.1   thorpej #include <sys/mbuf.h>
     46   1.1   thorpej #include <sys/socket.h>
     47   1.1   thorpej #include <sys/sockio.h>
     48   1.1   thorpej #include <sys/systm.h>
     49   1.1   thorpej #include <sys/proc.h>
     50   1.1   thorpej #include <sys/errno.h>
     51   1.1   thorpej #include <sys/kernel.h>
     52   1.1   thorpej #include <sys/queue.h>
     53  1.12  christos #include <sys/kauth.h>
     54   1.1   thorpej 
     55   1.1   thorpej #include <net/if.h>
     56  1.15     peter #include <netinet/in.h>
     57   1.1   thorpej 
     58  1.15     peter #include <net/pfvar.h>
     59   1.1   thorpej #include <altq/altq.h>
     60   1.1   thorpej #include <altq/altq_conf.h>
     61   1.1   thorpej #include <altq/altq_priq.h>
     62   1.1   thorpej 
     63   1.1   thorpej /*
     64   1.1   thorpej  * function prototypes
     65   1.1   thorpej  */
     66  1.15     peter #ifdef ALTQ3_COMPAT
     67  1.15     peter static struct priq_if *priq_attach(struct ifaltq *, u_int);
     68  1.15     peter static int priq_detach(struct priq_if *);
     69  1.15     peter #endif
     70  1.15     peter static int priq_clear_interface(struct priq_if *);
     71  1.15     peter static int priq_request(struct ifaltq *, int, void *);
     72  1.15     peter static void priq_purge(struct priq_if *);
     73  1.15     peter static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
     74  1.15     peter     int);
     75  1.15     peter static int priq_class_destroy(struct priq_class *);
     76  1.15     peter static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
     77  1.15     peter static struct mbuf *priq_dequeue(struct ifaltq *, int);
     78  1.15     peter 
     79  1.15     peter static int priq_addq(struct priq_class *, struct mbuf *);
     80  1.15     peter static struct mbuf *priq_getq(struct priq_class *);
     81  1.15     peter static struct mbuf *priq_pollq(struct priq_class *);
     82  1.15     peter static void priq_purgeq(struct priq_class *);
     83  1.15     peter 
     84  1.15     peter #ifdef ALTQ3_COMPAT
     85  1.15     peter static int priqcmd_if_attach(struct priq_interface *);
     86  1.15     peter static int priqcmd_if_detach(struct priq_interface *);
     87  1.15     peter static int priqcmd_add_class(struct priq_add_class *);
     88  1.15     peter static int priqcmd_delete_class(struct priq_delete_class *);
     89  1.15     peter static int priqcmd_modify_class(struct priq_modify_class *);
     90  1.15     peter static int priqcmd_add_filter(struct priq_add_filter *);
     91  1.15     peter static int priqcmd_delete_filter(struct priq_delete_filter *);
     92  1.15     peter static int priqcmd_class_stats(struct priq_class_stats *);
     93  1.15     peter #endif /* ALTQ3_COMPAT */
     94  1.15     peter 
     95  1.15     peter static void get_class_stats(struct priq_classstats *, struct priq_class *);
     96  1.15     peter static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
     97  1.15     peter 
     98  1.15     peter #ifdef ALTQ3_COMPAT
     99  1.15     peter altqdev_decl(priq);
    100   1.1   thorpej 
    101   1.1   thorpej /* pif_list keeps all priq_if's allocated. */
    102   1.1   thorpej static struct priq_if *pif_list = NULL;
    103  1.15     peter #endif /* ALTQ3_COMPAT */
    104  1.15     peter 
    105  1.15     peter int
    106  1.15     peter priq_pfattach(struct pf_altq *a)
    107  1.15     peter {
    108  1.15     peter 	struct ifnet *ifp;
    109  1.15     peter 	int s, error;
    110  1.15     peter 
    111  1.15     peter 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
    112  1.15     peter 		return (EINVAL);
    113  1.15     peter 	s = splnet();
    114  1.15     peter 	error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
    115  1.15     peter 	    priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
    116  1.15     peter 	splx(s);
    117  1.15     peter 	return (error);
    118  1.15     peter }
    119   1.1   thorpej 
    120  1.15     peter int
    121  1.15     peter priq_add_altq(struct pf_altq *a)
    122   1.1   thorpej {
    123  1.15     peter 	struct priq_if	*pif;
    124  1.15     peter 	struct ifnet	*ifp;
    125  1.15     peter 
    126  1.15     peter 	if ((ifp = ifunit(a->ifname)) == NULL)
    127  1.15     peter 		return (EINVAL);
    128  1.15     peter 	if (!ALTQ_IS_READY(&ifp->if_snd))
    129  1.15     peter 		return (ENODEV);
    130   1.1   thorpej 
    131  1.10  christos 	pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO);
    132   1.1   thorpej 	if (pif == NULL)
    133  1.15     peter 		return (ENOMEM);
    134  1.15     peter 	pif->pif_bandwidth = a->ifbandwidth;
    135   1.1   thorpej 	pif->pif_maxpri = -1;
    136  1.15     peter 	pif->pif_ifq = &ifp->if_snd;
    137  1.15     peter 
    138  1.15     peter 	/* keep the state in pf_altq */
    139  1.15     peter 	a->altq_disc = pif;
    140  1.15     peter 
    141  1.15     peter 	return (0);
    142  1.15     peter }
    143  1.15     peter 
    144  1.15     peter int
    145  1.15     peter priq_remove_altq(struct pf_altq *a)
    146  1.15     peter {
    147  1.15     peter 	struct priq_if *pif;
    148  1.15     peter 
    149  1.15     peter 	if ((pif = a->altq_disc) == NULL)
    150  1.15     peter 		return (EINVAL);
    151  1.15     peter 	a->altq_disc = NULL;
    152  1.15     peter 
    153  1.15     peter 	(void)priq_clear_interface(pif);
    154  1.15     peter 
    155  1.15     peter 	free(pif, M_DEVBUF);
    156  1.15     peter 	return (0);
    157  1.15     peter }
    158  1.15     peter 
    159  1.15     peter int
    160  1.15     peter priq_add_queue(struct pf_altq *a)
    161  1.15     peter {
    162  1.15     peter 	struct priq_if *pif;
    163  1.15     peter 	struct priq_class *cl;
    164  1.15     peter 
    165  1.15     peter 	if ((pif = a->altq_disc) == NULL)
    166  1.15     peter 		return (EINVAL);
    167   1.1   thorpej 
    168  1.15     peter 	/* check parameters */
    169  1.15     peter 	if (a->priority >= PRIQ_MAXPRI)
    170  1.15     peter 		return (EINVAL);
    171  1.15     peter 	if (a->qid == 0)
    172  1.15     peter 		return (EINVAL);
    173  1.15     peter 	if (pif->pif_classes[a->priority] != NULL)
    174  1.15     peter 		return (EBUSY);
    175  1.15     peter 	if (clh_to_clp(pif, a->qid) != NULL)
    176  1.15     peter 		return (EBUSY);
    177  1.15     peter 
    178  1.15     peter 	cl = priq_class_create(pif, a->priority, a->qlimit,
    179  1.15     peter 	    a->pq_u.priq_opts.flags, a->qid);
    180  1.15     peter 	if (cl == NULL)
    181  1.15     peter 		return (ENOMEM);
    182   1.1   thorpej 
    183  1.15     peter 	return (0);
    184   1.1   thorpej }
    185   1.1   thorpej 
    186  1.15     peter int
    187  1.15     peter priq_remove_queue(struct pf_altq *a)
    188  1.15     peter {
    189   1.1   thorpej 	struct priq_if *pif;
    190  1.15     peter 	struct priq_class *cl;
    191  1.15     peter 
    192  1.15     peter 	if ((pif = a->altq_disc) == NULL)
    193  1.15     peter 		return (EINVAL);
    194  1.15     peter 
    195  1.15     peter 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
    196  1.15     peter 		return (EINVAL);
    197  1.15     peter 
    198  1.15     peter 	return (priq_class_destroy(cl));
    199  1.15     peter }
    200  1.15     peter 
    201  1.15     peter int
    202  1.15     peter priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
    203   1.1   thorpej {
    204  1.15     peter 	struct priq_if *pif;
    205  1.15     peter 	struct priq_class *cl;
    206  1.15     peter 	struct priq_classstats stats;
    207  1.15     peter 	int error = 0;
    208  1.15     peter 
    209  1.15     peter 	if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
    210  1.15     peter 		return (EBADF);
    211  1.15     peter 
    212  1.15     peter 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
    213  1.15     peter 		return (EINVAL);
    214   1.1   thorpej 
    215  1.15     peter 	if (*nbytes < sizeof(stats))
    216  1.15     peter 		return (EINVAL);
    217   1.8     perry 
    218  1.15     peter 	get_class_stats(&stats, cl);
    219   1.1   thorpej 
    220  1.15     peter 	if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
    221  1.15     peter 		return (error);
    222  1.15     peter 	*nbytes = sizeof(stats);
    223   1.1   thorpej 	return (0);
    224   1.1   thorpej }
    225   1.1   thorpej 
    226   1.1   thorpej /*
    227   1.1   thorpej  * bring the interface back to the initial state by discarding
    228   1.1   thorpej  * all the filters and classes.
    229   1.1   thorpej  */
    230   1.1   thorpej static int
    231  1.15     peter priq_clear_interface(struct priq_if *pif)
    232   1.1   thorpej {
    233   1.1   thorpej 	struct priq_class	*cl;
    234   1.1   thorpej 	int pri;
    235   1.1   thorpej 
    236  1.15     peter #ifdef ALTQ3_CLFIER_COMPAT
    237   1.1   thorpej 	/* free the filters for this interface */
    238   1.1   thorpej 	acc_discard_filters(&pif->pif_classifier, NULL, 1);
    239  1.15     peter #endif
    240   1.1   thorpej 
    241   1.1   thorpej 	/* clear out the classes */
    242   1.1   thorpej 	for (pri = 0; pri <= pif->pif_maxpri; pri++)
    243   1.1   thorpej 		if ((cl = pif->pif_classes[pri]) != NULL)
    244   1.1   thorpej 			priq_class_destroy(cl);
    245   1.1   thorpej 
    246   1.1   thorpej 	return (0);
    247   1.1   thorpej }
    248   1.1   thorpej 
    249   1.1   thorpej static int
    250  1.14  christos priq_request(struct ifaltq *ifq, int req, void *arg __unused)
    251   1.1   thorpej {
    252   1.1   thorpej 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
    253   1.1   thorpej 
    254   1.1   thorpej 	switch (req) {
    255   1.1   thorpej 	case ALTRQ_PURGE:
    256   1.1   thorpej 		priq_purge(pif);
    257   1.1   thorpej 		break;
    258   1.1   thorpej 	}
    259   1.1   thorpej 	return (0);
    260   1.1   thorpej }
    261   1.1   thorpej 
    262   1.1   thorpej /* discard all the queued packets on the interface */
    263   1.1   thorpej static void
    264  1.15     peter priq_purge(struct priq_if *pif)
    265   1.1   thorpej {
    266   1.1   thorpej 	struct priq_class *cl;
    267   1.1   thorpej 	int pri;
    268   1.1   thorpej 
    269   1.1   thorpej 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
    270   1.1   thorpej 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
    271   1.1   thorpej 			priq_purgeq(cl);
    272   1.1   thorpej 	}
    273   1.1   thorpej 	if (ALTQ_IS_ENABLED(pif->pif_ifq))
    274   1.1   thorpej 		pif->pif_ifq->ifq_len = 0;
    275   1.1   thorpej }
    276   1.1   thorpej 
    277   1.1   thorpej static struct priq_class *
    278  1.15     peter priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
    279   1.1   thorpej {
    280   1.1   thorpej 	struct priq_class *cl;
    281   1.1   thorpej 	int s;
    282   1.1   thorpej 
    283   1.1   thorpej #ifndef ALTQ_RED
    284   1.1   thorpej 	if (flags & PRCF_RED) {
    285  1.15     peter #ifdef ALTQ_DEBUG
    286   1.1   thorpej 		printf("priq_class_create: RED not configured for PRIQ!\n");
    287  1.15     peter #endif
    288   1.1   thorpej 		return (NULL);
    289   1.1   thorpej 	}
    290   1.1   thorpej #endif
    291   1.1   thorpej 
    292   1.1   thorpej 	if ((cl = pif->pif_classes[pri]) != NULL) {
    293   1.1   thorpej 		/* modify the class instead of creating a new one */
    294   1.3   thorpej 		s = splnet();
    295   1.1   thorpej 		if (!qempty(cl->cl_q))
    296   1.1   thorpej 			priq_purgeq(cl);
    297   1.1   thorpej 		splx(s);
    298   1.1   thorpej #ifdef ALTQ_RIO
    299   1.1   thorpej 		if (q_is_rio(cl->cl_q))
    300   1.1   thorpej 			rio_destroy((rio_t *)cl->cl_red);
    301   1.1   thorpej #endif
    302   1.1   thorpej #ifdef ALTQ_RED
    303   1.1   thorpej 		if (q_is_red(cl->cl_q))
    304   1.1   thorpej 			red_destroy(cl->cl_red);
    305   1.1   thorpej #endif
    306   1.1   thorpej 	} else {
    307  1.10  christos 		cl = malloc(sizeof(struct priq_class), M_DEVBUF,
    308  1.10  christos 		    M_WAITOK|M_ZERO);
    309   1.1   thorpej 		if (cl == NULL)
    310   1.1   thorpej 			return (NULL);
    311   1.1   thorpej 
    312  1.10  christos 		cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
    313  1.10  christos 		    M_WAITOK|M_ZERO);
    314   1.1   thorpej 		if (cl->cl_q == NULL)
    315   1.1   thorpej 			goto err_ret;
    316   1.1   thorpej 	}
    317   1.1   thorpej 
    318   1.1   thorpej 	pif->pif_classes[pri] = cl;
    319   1.1   thorpej 	if (flags & PRCF_DEFAULTCLASS)
    320   1.1   thorpej 		pif->pif_default = cl;
    321   1.1   thorpej 	if (qlimit == 0)
    322   1.1   thorpej 		qlimit = 50;  /* use default */
    323   1.1   thorpej 	qlimit(cl->cl_q) = qlimit;
    324   1.1   thorpej 	qtype(cl->cl_q) = Q_DROPTAIL;
    325   1.1   thorpej 	qlen(cl->cl_q) = 0;
    326   1.1   thorpej 	cl->cl_flags = flags;
    327   1.1   thorpej 	cl->cl_pri = pri;
    328   1.1   thorpej 	if (pri > pif->pif_maxpri)
    329   1.1   thorpej 		pif->pif_maxpri = pri;
    330   1.1   thorpej 	cl->cl_pif = pif;
    331  1.15     peter 	cl->cl_handle = qid;
    332   1.1   thorpej 
    333   1.1   thorpej #ifdef ALTQ_RED
    334   1.1   thorpej 	if (flags & (PRCF_RED|PRCF_RIO)) {
    335   1.1   thorpej 		int red_flags, red_pkttime;
    336   1.1   thorpej 
    337   1.1   thorpej 		red_flags = 0;
    338   1.1   thorpej 		if (flags & PRCF_ECN)
    339   1.1   thorpej 			red_flags |= REDF_ECN;
    340   1.1   thorpej #ifdef ALTQ_RIO
    341   1.1   thorpej 		if (flags & PRCF_CLEARDSCP)
    342   1.1   thorpej 			red_flags |= RIOF_CLEARDSCP;
    343   1.1   thorpej #endif
    344   1.4    itojun 		if (pif->pif_bandwidth < 8)
    345   1.1   thorpej 			red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
    346   1.1   thorpej 		else
    347   1.1   thorpej 			red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
    348   1.1   thorpej 			  * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
    349   1.1   thorpej #ifdef ALTQ_RIO
    350   1.1   thorpej 		if (flags & PRCF_RIO) {
    351   1.1   thorpej 			cl->cl_red = (red_t *)rio_alloc(0, NULL,
    352   1.1   thorpej 						red_flags, red_pkttime);
    353   1.1   thorpej 			if (cl->cl_red != NULL)
    354   1.1   thorpej 				qtype(cl->cl_q) = Q_RIO;
    355   1.8     perry 		} else
    356   1.1   thorpej #endif
    357   1.1   thorpej 		if (flags & PRCF_RED) {
    358  1.15     peter 			cl->cl_red = red_alloc(0, 0,
    359  1.15     peter 			    qlimit(cl->cl_q) * 10/100,
    360  1.15     peter 			    qlimit(cl->cl_q) * 30/100,
    361  1.15     peter 			    red_flags, red_pkttime);
    362   1.1   thorpej 			if (cl->cl_red != NULL)
    363   1.1   thorpej 				qtype(cl->cl_q) = Q_RED;
    364   1.1   thorpej 		}
    365   1.1   thorpej 	}
    366   1.1   thorpej #endif /* ALTQ_RED */
    367   1.1   thorpej 
    368   1.1   thorpej 	return (cl);
    369   1.1   thorpej 
    370   1.1   thorpej  err_ret:
    371   1.1   thorpej 	if (cl->cl_red != NULL) {
    372   1.1   thorpej #ifdef ALTQ_RIO
    373   1.1   thorpej 		if (q_is_rio(cl->cl_q))
    374   1.1   thorpej 			rio_destroy((rio_t *)cl->cl_red);
    375   1.1   thorpej #endif
    376   1.1   thorpej #ifdef ALTQ_RED
    377   1.1   thorpej 		if (q_is_red(cl->cl_q))
    378   1.1   thorpej 			red_destroy(cl->cl_red);
    379   1.1   thorpej #endif
    380   1.1   thorpej 	}
    381   1.1   thorpej 	if (cl->cl_q != NULL)
    382  1.10  christos 		free(cl->cl_q, M_DEVBUF);
    383  1.10  christos 	free(cl, M_DEVBUF);
    384   1.1   thorpej 	return (NULL);
    385   1.1   thorpej }
    386   1.1   thorpej 
    387   1.1   thorpej static int
    388  1.15     peter priq_class_destroy(struct priq_class *cl)
    389   1.1   thorpej {
    390   1.1   thorpej 	struct priq_if *pif;
    391   1.1   thorpej 	int s, pri;
    392   1.1   thorpej 
    393   1.3   thorpej 	s = splnet();
    394   1.1   thorpej 
    395  1.15     peter #ifdef ALTQ3_CLFIER_COMPAT
    396   1.1   thorpej 	/* delete filters referencing to this class */
    397   1.1   thorpej 	acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
    398  1.15     peter #endif
    399   1.1   thorpej 
    400   1.1   thorpej 	if (!qempty(cl->cl_q))
    401   1.1   thorpej 		priq_purgeq(cl);
    402   1.1   thorpej 
    403   1.1   thorpej 	pif = cl->cl_pif;
    404   1.1   thorpej 	pif->pif_classes[cl->cl_pri] = NULL;
    405   1.1   thorpej 	if (pif->pif_maxpri == cl->cl_pri) {
    406   1.1   thorpej 		for (pri = cl->cl_pri; pri >= 0; pri--)
    407   1.1   thorpej 			if (pif->pif_classes[pri] != NULL) {
    408   1.1   thorpej 				pif->pif_maxpri = pri;
    409   1.1   thorpej 				break;
    410   1.1   thorpej 			}
    411   1.1   thorpej 		if (pri < 0)
    412   1.1   thorpej 			pif->pif_maxpri = -1;
    413   1.1   thorpej 	}
    414   1.1   thorpej 	splx(s);
    415   1.1   thorpej 
    416   1.1   thorpej 	if (cl->cl_red != NULL) {
    417   1.1   thorpej #ifdef ALTQ_RIO
    418   1.1   thorpej 		if (q_is_rio(cl->cl_q))
    419   1.1   thorpej 			rio_destroy((rio_t *)cl->cl_red);
    420   1.1   thorpej #endif
    421   1.1   thorpej #ifdef ALTQ_RED
    422   1.1   thorpej 		if (q_is_red(cl->cl_q))
    423   1.1   thorpej 			red_destroy(cl->cl_red);
    424   1.1   thorpej #endif
    425   1.1   thorpej 	}
    426  1.10  christos 	free(cl->cl_q, M_DEVBUF);
    427  1.10  christos 	free(cl, M_DEVBUF);
    428   1.1   thorpej 	return (0);
    429   1.1   thorpej }
    430   1.1   thorpej 
    431   1.1   thorpej /*
    432   1.1   thorpej  * priq_enqueue is an enqueue function to be registered to
    433   1.1   thorpej  * (*altq_enqueue) in struct ifaltq.
    434   1.1   thorpej  */
    435   1.8     perry static int
    436  1.15     peter priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
    437   1.1   thorpej {
    438   1.1   thorpej 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
    439   1.1   thorpej 	struct priq_class *cl;
    440  1.15     peter 	struct m_tag *t;
    441   1.1   thorpej 	int len;
    442   1.1   thorpej 
    443   1.1   thorpej 	/* grab class set by classifier */
    444  1.15     peter 	if ((m->m_flags & M_PKTHDR) == 0) {
    445  1.15     peter 		/* should not happen */
    446  1.15     peter 		printf("altq: packet for %s does not have pkthdr\n",
    447  1.15     peter 		    ifq->altq_ifp->if_xname);
    448  1.15     peter 		m_freem(m);
    449  1.15     peter 		return (ENOBUFS);
    450  1.15     peter 	}
    451  1.15     peter 	cl = NULL;
    452  1.15     peter 	if ((t = m_tag_find(m, PACKET_TAG_PF_QID, NULL)) != NULL)
    453  1.15     peter 		cl = clh_to_clp(pif, ((struct altq_tag *)(t+1))->qid);
    454  1.15     peter #ifdef ALTQ3_COMPAT
    455  1.15     peter 	else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
    456  1.15     peter 		cl = pktattr->pattr_class;
    457  1.15     peter #endif
    458  1.15     peter 	if (cl == NULL) {
    459   1.1   thorpej 		cl = pif->pif_default;
    460  1.15     peter 		if (cl == NULL) {
    461  1.15     peter 			m_freem(m);
    462  1.15     peter 			return (ENOBUFS);
    463  1.15     peter 		}
    464  1.15     peter 	}
    465  1.15     peter #ifdef ALTQ3_COMPAT
    466  1.15     peter 	if (pktattr != NULL)
    467  1.15     peter 		cl->cl_pktattr = pktattr;  /* save proto hdr used by ECN */
    468  1.15     peter 	else
    469  1.15     peter #endif
    470  1.15     peter 		cl->cl_pktattr = NULL;
    471   1.1   thorpej 	len = m_pktlen(m);
    472   1.1   thorpej 	if (priq_addq(cl, m) != 0) {
    473   1.1   thorpej 		/* drop occurred.  mbuf was freed in priq_addq. */
    474   1.1   thorpej 		PKTCNTR_ADD(&cl->cl_dropcnt, len);
    475   1.1   thorpej 		return (ENOBUFS);
    476   1.1   thorpej 	}
    477   1.1   thorpej 	IFQ_INC_LEN(ifq);
    478   1.1   thorpej 
    479   1.1   thorpej 	/* successfully queued. */
    480   1.1   thorpej 	return (0);
    481   1.1   thorpej }
    482   1.1   thorpej 
    483   1.1   thorpej /*
    484   1.1   thorpej  * priq_dequeue is a dequeue function to be registered to
    485   1.1   thorpej  * (*altq_dequeue) in struct ifaltq.
    486   1.1   thorpej  *
    487   1.1   thorpej  * note: ALTDQ_POLL returns the next packet without removing the packet
    488   1.1   thorpej  *	from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
    489   1.1   thorpej  *	ALTDQ_REMOVE must return the same packet if called immediately
    490   1.1   thorpej  *	after ALTDQ_POLL.
    491   1.1   thorpej  */
    492   1.1   thorpej static struct mbuf *
    493  1.15     peter priq_dequeue(struct ifaltq *ifq, int op)
    494   1.1   thorpej {
    495   1.1   thorpej 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
    496   1.1   thorpej 	struct priq_class *cl;
    497   1.1   thorpej 	struct mbuf *m;
    498   1.1   thorpej 	int pri;
    499   1.1   thorpej 
    500   1.1   thorpej 	if (IFQ_IS_EMPTY(ifq))
    501   1.1   thorpej 		/* no packet in the queue */
    502   1.1   thorpej 		return (NULL);
    503   1.1   thorpej 
    504   1.1   thorpej 	for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
    505   1.1   thorpej 		if ((cl = pif->pif_classes[pri]) != NULL &&
    506   1.1   thorpej 		    !qempty(cl->cl_q)) {
    507   1.1   thorpej 			if (op == ALTDQ_POLL)
    508   1.1   thorpej 				return (priq_pollq(cl));
    509   1.1   thorpej 
    510   1.1   thorpej 			m = priq_getq(cl);
    511   1.1   thorpej 			if (m != NULL) {
    512   1.1   thorpej 				IFQ_DEC_LEN(ifq);
    513   1.1   thorpej 				if (qempty(cl->cl_q))
    514   1.1   thorpej 					cl->cl_period++;
    515   1.1   thorpej 				PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
    516   1.1   thorpej 			}
    517   1.1   thorpej 			return (m);
    518   1.1   thorpej 		}
    519   1.1   thorpej 	}
    520   1.1   thorpej 	return (NULL);
    521   1.1   thorpej }
    522   1.1   thorpej 
    523   1.1   thorpej static int
    524  1.15     peter priq_addq(struct priq_class *cl, struct mbuf *m)
    525   1.1   thorpej {
    526   1.1   thorpej 
    527   1.1   thorpej #ifdef ALTQ_RIO
    528   1.1   thorpej 	if (q_is_rio(cl->cl_q))
    529   1.1   thorpej 		return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
    530   1.1   thorpej 				cl->cl_pktattr);
    531   1.1   thorpej #endif
    532   1.1   thorpej #ifdef ALTQ_RED
    533   1.1   thorpej 	if (q_is_red(cl->cl_q))
    534   1.1   thorpej 		return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
    535   1.1   thorpej #endif
    536   1.1   thorpej 	if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
    537   1.1   thorpej 		m_freem(m);
    538   1.1   thorpej 		return (-1);
    539   1.1   thorpej 	}
    540   1.8     perry 
    541   1.1   thorpej 	if (cl->cl_flags & PRCF_CLEARDSCP)
    542   1.1   thorpej 		write_dsfield(m, cl->cl_pktattr, 0);
    543   1.1   thorpej 
    544   1.1   thorpej 	_addq(cl->cl_q, m);
    545   1.1   thorpej 
    546   1.1   thorpej 	return (0);
    547   1.1   thorpej }
    548   1.1   thorpej 
    549   1.1   thorpej static struct mbuf *
    550  1.15     peter priq_getq(struct priq_class *cl)
    551   1.1   thorpej {
    552   1.1   thorpej #ifdef ALTQ_RIO
    553   1.1   thorpej 	if (q_is_rio(cl->cl_q))
    554   1.1   thorpej 		return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
    555   1.1   thorpej #endif
    556   1.1   thorpej #ifdef ALTQ_RED
    557   1.1   thorpej 	if (q_is_red(cl->cl_q))
    558   1.1   thorpej 		return red_getq(cl->cl_red, cl->cl_q);
    559   1.1   thorpej #endif
    560   1.1   thorpej 	return _getq(cl->cl_q);
    561   1.1   thorpej }
    562   1.1   thorpej 
    563   1.1   thorpej static struct mbuf *
    564   1.1   thorpej priq_pollq(cl)
    565   1.1   thorpej 	struct priq_class *cl;
    566   1.1   thorpej {
    567   1.1   thorpej 	return qhead(cl->cl_q);
    568   1.1   thorpej }
    569   1.1   thorpej 
    570   1.1   thorpej static void
    571  1.15     peter priq_purgeq(struct priq_class *cl)
    572   1.1   thorpej {
    573   1.1   thorpej 	struct mbuf *m;
    574   1.1   thorpej 
    575   1.1   thorpej 	if (qempty(cl->cl_q))
    576   1.1   thorpej 		return;
    577   1.1   thorpej 
    578   1.1   thorpej 	while ((m = _getq(cl->cl_q)) != NULL) {
    579   1.1   thorpej 		PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
    580   1.1   thorpej 		m_freem(m);
    581   1.1   thorpej 	}
    582   1.1   thorpej 	ASSERT(qlen(cl->cl_q) == 0);
    583   1.1   thorpej }
    584   1.1   thorpej 
    585  1.15     peter static void
    586  1.15     peter get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
    587  1.15     peter {
    588  1.15     peter 	sp->class_handle = cl->cl_handle;
    589  1.15     peter 	sp->qlength = qlen(cl->cl_q);
    590  1.15     peter 	sp->qlimit = qlimit(cl->cl_q);
    591  1.15     peter 	sp->period = cl->cl_period;
    592  1.15     peter 	sp->xmitcnt = cl->cl_xmitcnt;
    593  1.15     peter 	sp->dropcnt = cl->cl_dropcnt;
    594  1.15     peter 
    595  1.15     peter 	sp->qtype = qtype(cl->cl_q);
    596  1.15     peter #ifdef ALTQ_RED
    597  1.15     peter 	if (q_is_red(cl->cl_q))
    598  1.15     peter 		red_getstats(cl->cl_red, &sp->red[0]);
    599  1.15     peter #endif
    600  1.15     peter #ifdef ALTQ_RIO
    601  1.15     peter 	if (q_is_rio(cl->cl_q))
    602  1.15     peter 		rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
    603  1.15     peter #endif
    604  1.15     peter 
    605  1.15     peter }
    606  1.15     peter 
    607  1.15     peter /* convert a class handle to the corresponding class pointer */
    608  1.15     peter static struct priq_class *
    609  1.15     peter clh_to_clp(struct priq_if *pif, u_int32_t chandle)
    610  1.15     peter {
    611  1.15     peter 	struct priq_class *cl;
    612  1.15     peter 	int idx;
    613  1.15     peter 
    614  1.15     peter 	if (chandle == 0)
    615  1.15     peter 		return (NULL);
    616  1.15     peter 
    617  1.15     peter 	for (idx = pif->pif_maxpri; idx >= 0; idx--)
    618  1.15     peter 		if ((cl = pif->pif_classes[idx]) != NULL &&
    619  1.15     peter 		    cl->cl_handle == chandle)
    620  1.15     peter 			return (cl);
    621  1.15     peter 
    622  1.15     peter 	return (NULL);
    623  1.15     peter }
    624  1.15     peter 
    625  1.15     peter 
    626  1.15     peter #ifdef ALTQ3_COMPAT
    627  1.15     peter 
    628  1.15     peter static struct priq_if *
    629  1.15     peter priq_attach(struct ifaltq *ifq, u_int bandwidth)
    630  1.15     peter {
    631  1.15     peter 	struct priq_if *pif;
    632  1.15     peter 
    633  1.15     peter 	pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_WAITOK|M_ZERO);
    634  1.15     peter 	if (pif == NULL)
    635  1.15     peter 		return (NULL);
    636  1.15     peter 	pif->pif_bandwidth = bandwidth;
    637  1.15     peter 	pif->pif_maxpri = -1;
    638  1.15     peter 	pif->pif_ifq = ifq;
    639  1.15     peter 
    640  1.15     peter 	/* add this state to the priq list */
    641  1.15     peter 	pif->pif_next = pif_list;
    642  1.15     peter 	pif_list = pif;
    643  1.15     peter 
    644  1.15     peter 	return (pif);
    645  1.15     peter }
    646  1.15     peter 
    647  1.15     peter static int
    648  1.15     peter priq_detach(struct priq_if *pif)
    649  1.15     peter {
    650  1.15     peter 	(void)priq_clear_interface(pif);
    651  1.15     peter 
    652  1.15     peter 	/* remove this interface from the pif list */
    653  1.15     peter 	if (pif_list == pif)
    654  1.15     peter 		pif_list = pif->pif_next;
    655  1.15     peter 	else {
    656  1.15     peter 		struct priq_if *p;
    657  1.15     peter 
    658  1.15     peter 		for (p = pif_list; p != NULL; p = p->pif_next)
    659  1.15     peter 			if (p->pif_next == pif) {
    660  1.15     peter 				p->pif_next = pif->pif_next;
    661  1.15     peter 				break;
    662  1.15     peter 			}
    663  1.15     peter 		ASSERT(p != NULL);
    664  1.15     peter 	}
    665  1.15     peter 
    666  1.15     peter 	free(pif, M_DEVBUF);
    667  1.15     peter 	return (0);
    668  1.15     peter }
    669  1.15     peter 
    670   1.1   thorpej /*
    671   1.1   thorpej  * priq device interface
    672   1.1   thorpej  */
    673   1.1   thorpej int
    674  1.14  christos priqopen(dev_t dev __unused, int flag __unused, int fmt __unused,
    675  1.14  christos     struct lwp *l __unused)
    676   1.1   thorpej {
    677   1.1   thorpej 	/* everything will be done when the queueing scheme is attached. */
    678   1.1   thorpej 	return 0;
    679   1.1   thorpej }
    680   1.1   thorpej 
    681   1.1   thorpej int
    682  1.14  christos priqclose(dev_t dev __unused, int flag __unused, int fmt __unused,
    683  1.14  christos     struct lwp *l __unused)
    684   1.1   thorpej {
    685   1.1   thorpej 	struct priq_if *pif;
    686   1.1   thorpej 	int err, error = 0;
    687   1.1   thorpej 
    688   1.1   thorpej 	while ((pif = pif_list) != NULL) {
    689   1.1   thorpej 		/* destroy all */
    690   1.1   thorpej 		if (ALTQ_IS_ENABLED(pif->pif_ifq))
    691   1.1   thorpej 			altq_disable(pif->pif_ifq);
    692   1.1   thorpej 
    693   1.1   thorpej 		err = altq_detach(pif->pif_ifq);
    694   1.1   thorpej 		if (err == 0)
    695   1.1   thorpej 			err = priq_detach(pif);
    696   1.1   thorpej 		if (err != 0 && error == 0)
    697   1.1   thorpej 			error = err;
    698   1.1   thorpej 	}
    699   1.1   thorpej 
    700   1.1   thorpej 	return error;
    701   1.1   thorpej }
    702   1.1   thorpej 
    703   1.1   thorpej int
    704  1.14  christos priqioctl(dev_t dev __unused, ioctlcmd_t cmd, caddr_t addr, int flag __unused,
    705  1.14  christos     struct lwp *l)
    706   1.1   thorpej {
    707   1.1   thorpej 	struct priq_if *pif;
    708   1.1   thorpej 	struct priq_interface *ifacep;
    709   1.1   thorpej 	int	error = 0;
    710   1.1   thorpej 
    711   1.1   thorpej 	/* check super-user privilege */
    712   1.1   thorpej 	switch (cmd) {
    713   1.1   thorpej 	case PRIQ_GETSTATS:
    714   1.1   thorpej 		break;
    715   1.1   thorpej 	default:
    716   1.1   thorpej #if (__FreeBSD_version > 400000)
    717   1.1   thorpej 		if ((error = suser(p)) != 0)
    718   1.1   thorpej 			return (error);
    719   1.1   thorpej #else
    720  1.13        ad 		if ((error = kauth_authorize_generic(l->l_cred,
    721  1.13        ad 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
    722   1.1   thorpej 			return (error);
    723   1.1   thorpej #endif
    724   1.1   thorpej 		break;
    725   1.1   thorpej 	}
    726   1.8     perry 
    727   1.1   thorpej 	switch (cmd) {
    728   1.1   thorpej 
    729   1.1   thorpej 	case PRIQ_IF_ATTACH:
    730   1.1   thorpej 		error = priqcmd_if_attach((struct priq_interface *)addr);
    731   1.1   thorpej 		break;
    732   1.1   thorpej 
    733   1.1   thorpej 	case PRIQ_IF_DETACH:
    734   1.1   thorpej 		error = priqcmd_if_detach((struct priq_interface *)addr);
    735   1.1   thorpej 		break;
    736   1.1   thorpej 
    737   1.1   thorpej 	case PRIQ_ENABLE:
    738   1.1   thorpej 	case PRIQ_DISABLE:
    739   1.1   thorpej 	case PRIQ_CLEAR:
    740   1.1   thorpej 		ifacep = (struct priq_interface *)addr;
    741   1.1   thorpej 		if ((pif = altq_lookup(ifacep->ifname,
    742   1.1   thorpej 				       ALTQT_PRIQ)) == NULL) {
    743   1.1   thorpej 			error = EBADF;
    744   1.1   thorpej 			break;
    745   1.1   thorpej 		}
    746   1.1   thorpej 
    747   1.1   thorpej 		switch (cmd) {
    748   1.1   thorpej 		case PRIQ_ENABLE:
    749   1.1   thorpej 			if (pif->pif_default == NULL) {
    750  1.15     peter #ifdef ALTQ_DEBUG
    751   1.1   thorpej 				printf("priq: no default class\n");
    752   1.1   thorpej #endif
    753   1.1   thorpej 				error = EINVAL;
    754   1.1   thorpej 				break;
    755   1.1   thorpej 			}
    756   1.1   thorpej 			error = altq_enable(pif->pif_ifq);
    757   1.1   thorpej 			break;
    758   1.1   thorpej 
    759   1.1   thorpej 		case PRIQ_DISABLE:
    760   1.1   thorpej 			error = altq_disable(pif->pif_ifq);
    761   1.1   thorpej 			break;
    762   1.1   thorpej 
    763   1.1   thorpej 		case PRIQ_CLEAR:
    764   1.1   thorpej 			priq_clear_interface(pif);
    765   1.1   thorpej 			break;
    766   1.1   thorpej 		}
    767   1.1   thorpej 		break;
    768   1.1   thorpej 
    769   1.1   thorpej 	case PRIQ_ADD_CLASS:
    770   1.1   thorpej 		error = priqcmd_add_class((struct priq_add_class *)addr);
    771   1.1   thorpej 		break;
    772   1.1   thorpej 
    773   1.1   thorpej 	case PRIQ_DEL_CLASS:
    774   1.1   thorpej 		error = priqcmd_delete_class((struct priq_delete_class *)addr);
    775   1.1   thorpej 		break;
    776   1.1   thorpej 
    777   1.1   thorpej 	case PRIQ_MOD_CLASS:
    778   1.1   thorpej 		error = priqcmd_modify_class((struct priq_modify_class *)addr);
    779   1.1   thorpej 		break;
    780   1.1   thorpej 
    781   1.1   thorpej 	case PRIQ_ADD_FILTER:
    782   1.1   thorpej 		error = priqcmd_add_filter((struct priq_add_filter *)addr);
    783   1.1   thorpej 		break;
    784   1.1   thorpej 
    785   1.1   thorpej 	case PRIQ_DEL_FILTER:
    786   1.1   thorpej 		error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
    787   1.1   thorpej 		break;
    788   1.1   thorpej 
    789   1.1   thorpej 	case PRIQ_GETSTATS:
    790   1.1   thorpej 		error = priqcmd_class_stats((struct priq_class_stats *)addr);
    791   1.1   thorpej 		break;
    792   1.1   thorpej 
    793   1.1   thorpej 	default:
    794   1.1   thorpej 		error = EINVAL;
    795   1.1   thorpej 		break;
    796   1.1   thorpej 	}
    797   1.1   thorpej 	return error;
    798   1.1   thorpej }
    799   1.1   thorpej 
    800   1.1   thorpej static int
    801  1.15     peter priqcmd_if_attach(struct priq_interface *ap)
    802   1.1   thorpej {
    803   1.1   thorpej 	struct priq_if *pif;
    804   1.1   thorpej 	struct ifnet *ifp;
    805   1.1   thorpej 	int error;
    806   1.8     perry 
    807   1.1   thorpej 	if ((ifp = ifunit(ap->ifname)) == NULL)
    808   1.1   thorpej 		return (ENXIO);
    809   1.1   thorpej 
    810   1.1   thorpej 	if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
    811   1.1   thorpej 		return (ENOMEM);
    812   1.8     perry 
    813   1.1   thorpej 	/*
    814   1.1   thorpej 	 * set PRIQ to this ifnet structure.
    815   1.1   thorpej 	 */
    816   1.1   thorpej 	if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
    817   1.1   thorpej 				 priq_enqueue, priq_dequeue, priq_request,
    818   1.1   thorpej 				 &pif->pif_classifier, acc_classify)) != 0)
    819   1.1   thorpej 		(void)priq_detach(pif);
    820   1.1   thorpej 
    821   1.1   thorpej 	return (error);
    822   1.1   thorpej }
    823   1.1   thorpej 
    824   1.1   thorpej static int
    825  1.15     peter priqcmd_if_detach(struct priq_interface *ap)
    826   1.1   thorpej {
    827   1.1   thorpej 	struct priq_if *pif;
    828   1.1   thorpej 	int error;
    829   1.1   thorpej 
    830   1.1   thorpej 	if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
    831   1.1   thorpej 		return (EBADF);
    832   1.8     perry 
    833   1.1   thorpej 	if (ALTQ_IS_ENABLED(pif->pif_ifq))
    834   1.1   thorpej 		altq_disable(pif->pif_ifq);
    835   1.1   thorpej 
    836   1.1   thorpej 	if ((error = altq_detach(pif->pif_ifq)))
    837   1.1   thorpej 		return (error);
    838   1.1   thorpej 
    839   1.1   thorpej 	return priq_detach(pif);
    840   1.1   thorpej }
    841   1.1   thorpej 
    842   1.1   thorpej static int
    843  1.15     peter priqcmd_add_class(struct priq_add_class *ap)
    844   1.1   thorpej {
    845   1.1   thorpej 	struct priq_if *pif;
    846   1.1   thorpej 	struct priq_class *cl;
    847  1.15     peter 	int qid;
    848   1.1   thorpej 
    849   1.1   thorpej 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
    850   1.1   thorpej 		return (EBADF);
    851   1.1   thorpej 
    852   1.1   thorpej 	if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
    853   1.1   thorpej 		return (EINVAL);
    854  1.15     peter 	if (pif->pif_classes[ap->pri] != NULL)
    855  1.15     peter 		return (EBUSY);
    856   1.1   thorpej 
    857  1.15     peter 	qid = ap->pri + 1;
    858   1.1   thorpej 	if ((cl = priq_class_create(pif, ap->pri,
    859  1.15     peter 	    ap->qlimit, ap->flags, qid)) == NULL)
    860   1.1   thorpej 		return (ENOMEM);
    861   1.8     perry 
    862   1.1   thorpej 	/* return a class handle to the user */
    863  1.15     peter 	ap->class_handle = cl->cl_handle;
    864  1.15     peter 
    865   1.1   thorpej 	return (0);
    866   1.1   thorpej }
    867   1.1   thorpej 
    868   1.1   thorpej static int
    869  1.15     peter priqcmd_delete_class(struct priq_delete_class *ap)
    870   1.1   thorpej {
    871   1.1   thorpej 	struct priq_if *pif;
    872   1.1   thorpej 	struct priq_class *cl;
    873   1.1   thorpej 
    874   1.1   thorpej 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
    875   1.1   thorpej 		return (EBADF);
    876   1.1   thorpej 
    877   1.1   thorpej 	if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
    878   1.1   thorpej 		return (EINVAL);
    879   1.8     perry 
    880   1.1   thorpej 	return priq_class_destroy(cl);
    881   1.1   thorpej }
    882   1.1   thorpej 
    883   1.1   thorpej static int
    884  1.15     peter priqcmd_modify_class(struct priq_modify_class *ap)
    885   1.1   thorpej {
    886   1.1   thorpej 	struct priq_if *pif;
    887   1.1   thorpej 	struct priq_class *cl;
    888   1.1   thorpej 
    889   1.1   thorpej 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
    890   1.1   thorpej 		return (EBADF);
    891   1.1   thorpej 
    892   1.1   thorpej 	if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
    893   1.1   thorpej 		return (EINVAL);
    894   1.1   thorpej 
    895   1.1   thorpej 	if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
    896   1.1   thorpej 		return (EINVAL);
    897   1.1   thorpej 
    898   1.1   thorpej 	/*
    899   1.1   thorpej 	 * if priority is changed, move the class to the new priority
    900   1.1   thorpej 	 */
    901   1.1   thorpej 	if (pif->pif_classes[ap->pri] != cl) {
    902   1.1   thorpej 		if (pif->pif_classes[ap->pri] != NULL)
    903   1.1   thorpej 			return (EEXIST);
    904   1.1   thorpej 		pif->pif_classes[cl->cl_pri] = NULL;
    905   1.1   thorpej 		pif->pif_classes[ap->pri] = cl;
    906   1.1   thorpej 		cl->cl_pri = ap->pri;
    907   1.1   thorpej 	}
    908   1.1   thorpej 
    909   1.1   thorpej 	/* call priq_class_create to change class parameters */
    910   1.1   thorpej 	if ((cl = priq_class_create(pif, ap->pri,
    911  1.15     peter 	    ap->qlimit, ap->flags, ap->class_handle)) == NULL)
    912   1.1   thorpej 		return (ENOMEM);
    913   1.1   thorpej 	return 0;
    914   1.1   thorpej }
    915   1.1   thorpej 
    916   1.1   thorpej static int
    917  1.15     peter priqcmd_add_filter(struct priq_add_filter *ap)
    918   1.1   thorpej {
    919   1.1   thorpej 	struct priq_if *pif;
    920   1.1   thorpej 	struct priq_class *cl;
    921   1.1   thorpej 
    922   1.1   thorpej 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
    923   1.1   thorpej 		return (EBADF);
    924   1.1   thorpej 
    925   1.1   thorpej 	if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
    926   1.1   thorpej 		return (EINVAL);
    927   1.1   thorpej 
    928   1.1   thorpej 	return acc_add_filter(&pif->pif_classifier, &ap->filter,
    929   1.1   thorpej 			      cl, &ap->filter_handle);
    930   1.1   thorpej }
    931   1.1   thorpej 
    932   1.1   thorpej static int
    933  1.15     peter priqcmd_delete_filter(struct priq_delete_filter *ap)
    934   1.1   thorpej {
    935   1.1   thorpej 	struct priq_if *pif;
    936   1.1   thorpej 
    937   1.1   thorpej 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
    938   1.1   thorpej 		return (EBADF);
    939   1.1   thorpej 
    940   1.1   thorpej 	return acc_delete_filter(&pif->pif_classifier,
    941   1.1   thorpej 				 ap->filter_handle);
    942   1.1   thorpej }
    943   1.1   thorpej 
    944   1.1   thorpej static int
    945  1.15     peter priqcmd_class_stats(struct priq_class_stats *ap)
    946   1.1   thorpej {
    947   1.1   thorpej 	struct priq_if *pif;
    948   1.1   thorpej 	struct priq_class *cl;
    949  1.15     peter 	struct priq_classstats stats, *usp;
    950   1.1   thorpej 	int	pri, error;
    951   1.8     perry 
    952   1.1   thorpej 	if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
    953   1.1   thorpej 		return (EBADF);
    954   1.1   thorpej 
    955   1.1   thorpej 	ap->maxpri = pif->pif_maxpri;
    956   1.1   thorpej 
    957   1.1   thorpej 	/* then, read the next N classes in the tree */
    958   1.1   thorpej 	usp = ap->stats;
    959   1.1   thorpej 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
    960   1.1   thorpej 		cl = pif->pif_classes[pri];
    961   1.1   thorpej 		if (cl != NULL)
    962   1.1   thorpej 			get_class_stats(&stats, cl);
    963   1.1   thorpej 		else
    964  1.15     peter 			memset(&stats, 0, sizeof(stats));
    965   1.1   thorpej 		if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
    966   1.1   thorpej 				     sizeof(stats))) != 0)
    967   1.1   thorpej 			return (error);
    968   1.1   thorpej 	}
    969   1.1   thorpej 	return (0);
    970   1.1   thorpej }
    971   1.1   thorpej 
    972   1.1   thorpej #ifdef KLD_MODULE
    973   1.1   thorpej 
    974   1.1   thorpej static struct altqsw priq_sw =
    975   1.1   thorpej 	{"priq", priqopen, priqclose, priqioctl};
    976   1.1   thorpej 
    977   1.1   thorpej ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
    978  1.15     peter MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
    979  1.15     peter MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
    980   1.1   thorpej 
    981   1.1   thorpej #endif /* KLD_MODULE */
    982   1.1   thorpej 
    983  1.15     peter #endif /* ALTQ3_COMPAT */
    984   1.1   thorpej #endif /* ALTQ_PRIQ */
    985