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