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