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