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