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