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