Home | History | Annotate | Line # | Download | only in altq
      1 /*	$NetBSD: altq_cbq.c,v 1.43 2026/01/11 16:31:27 christos Exp $	*/
      2 /*	$KAME: altq_cbq.c,v 1.21 2005/04/13 03:44:24 suz Exp $	*/
      3 
      4 /*
      5  * Copyright (c) Sun Microsystems, Inc. 1993-1998 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  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  *
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the SMCC Technology
     21  *      Development Group at Sun Microsystems, Inc.
     22  *
     23  * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
     24  *      promote products derived from this software without specific prior
     25  *      written permission.
     26  *
     27  * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
     28  * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is
     29  * provided "as is" without express or implied warranty of any kind.
     30  *
     31  * These notices must be retained in any copies of any part of this software.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: altq_cbq.c,v 1.43 2026/01/11 16:31:27 christos Exp $");
     36 
     37 #ifdef _KERNEL_OPT
     38 #include "opt_altq.h"
     39 #include "opt_inet.h"
     40 #include "pf.h"
     41 #endif
     42 
     43 #ifdef ALTQ_CBQ	/* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
     44 
     45 #include <sys/param.h>
     46 #include <sys/malloc.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/socket.h>
     49 #include <sys/systm.h>
     50 #include <sys/proc.h>
     51 #include <sys/errno.h>
     52 #include <sys/time.h>
     53 #ifdef ALTQ3_COMPAT
     54 #include <sys/uio.h>
     55 #include <sys/kernel.h>
     56 #endif
     57 #include <sys/kauth.h>
     58 
     59 #include <net/if.h>
     60 #include <netinet/in.h>
     61 
     62 #if NPF > 0
     63 #include <net/pfvar.h>
     64 #endif
     65 #include <altq/altq.h>
     66 #include <altq/altq_cbq.h>
     67 #ifdef ALTQ3_COMPAT
     68 #include <altq/altq_conf.h>
     69 #endif
     70 
     71 #ifdef ALTQ3_COMPAT
     72 /*
     73  * Local Data structures.
     74  */
     75 static cbq_state_t *cbq_list = NULL;
     76 #endif
     77 
     78 /*
     79  * Forward Declarations.
     80  */
     81 static int		 cbq_class_destroy(cbq_state_t *, struct rm_class *);
     82 static struct rm_class  *clh_to_clp(cbq_state_t *, u_int32_t);
     83 static int		 cbq_clear_interface(cbq_state_t *);
     84 static int		 cbq_request(struct ifaltq *, int, void *);
     85 static int		 cbq_enqueue(struct ifaltq *, struct mbuf *);
     86 static struct mbuf	*cbq_dequeue(struct ifaltq *, int);
     87 static void		 cbqrestart(struct ifaltq *);
     88 static void		 get_class_stats(class_stats_t *, struct rm_class *);
     89 static void		 cbq_purge(cbq_state_t *);
     90 #ifdef ALTQ3_COMPAT
     91 static int	cbq_add_class(struct cbq_add_class *);
     92 static int	cbq_delete_class(struct cbq_delete_class *);
     93 static int	cbq_modify_class(struct cbq_modify_class *);
     94 static int 	cbq_class_create(cbq_state_t *, struct cbq_add_class *,
     95 				 struct rm_class *, struct rm_class *);
     96 static int	cbq_clear_hierarchy(struct cbq_interface *);
     97 static int	cbq_set_enable(struct cbq_interface *, int);
     98 static int	cbq_ifattach(struct cbq_interface *);
     99 static int	cbq_ifdetach(struct cbq_interface *);
    100 static int 	cbq_getstats(struct cbq_getstats *);
    101 
    102 static int	cbq_add_filter(struct cbq_add_filter *);
    103 static int	cbq_delete_filter(struct cbq_delete_filter *);
    104 #endif /* ALTQ3_COMPAT */
    105 
    106 /*
    107  * int
    108  * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
    109  *	function destroys a given traffic class.  Before destroying
    110  *	the class, all traffic for that class is released.
    111  */
    112 static int
    113 cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl)
    114 {
    115 	int	i;
    116 
    117 	/* delete the class */
    118 	rmc_delete_class(&cbqp->ifnp, cl);
    119 
    120 	/*
    121 	 * free the class handle
    122 	 */
    123 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    124 		if (cbqp->cbq_class_tbl[i] == cl)
    125 			cbqp->cbq_class_tbl[i] = NULL;
    126 
    127 	if (cl == cbqp->ifnp.root_)
    128 		cbqp->ifnp.root_ = NULL;
    129 	if (cl == cbqp->ifnp.default_)
    130 		cbqp->ifnp.default_ = NULL;
    131 #ifdef ALTQ3_COMPAT
    132 	if (cl == cbqp->ifnp.ctl_)
    133 		cbqp->ifnp.ctl_ = NULL;
    134 #endif
    135 	return 0;
    136 }
    137 
    138 /* convert class handle to class pointer */
    139 static struct rm_class *
    140 clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle)
    141 {
    142 	int i;
    143 	struct rm_class *cl;
    144 
    145 	if (chandle == 0)
    146 		return NULL;
    147 	/*
    148 	 * first, try optimistically the slot matching the lower bits of
    149 	 * the handle.  if it fails, do the linear table search.
    150 	 */
    151 	i = chandle % CBQ_MAX_CLASSES;
    152 	if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
    153 	    cl->stats_.handle == chandle)
    154 		return (cl);
    155 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    156 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
    157 		    cl->stats_.handle == chandle)
    158 			return cl;
    159 	return NULL;
    160 }
    161 
    162 static int
    163 cbq_clear_interface(cbq_state_t *cbqp)
    164 {
    165 	int		 again, i;
    166 	struct rm_class	*cl;
    167 
    168 #ifdef ALTQ3_CLFIER_COMPAT
    169 	/* free the filters for this interface */
    170 	acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
    171 #endif
    172 
    173 	/* clear out the classes now */
    174 	do {
    175 		again = 0;
    176 		for (i = 0; i < CBQ_MAX_CLASSES; i++) {
    177 			if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
    178 				if (is_a_parent_class(cl))
    179 					again++;
    180 				else {
    181 					cbq_class_destroy(cbqp, cl);
    182 					cbqp->cbq_class_tbl[i] = NULL;
    183 					if (cl == cbqp->ifnp.root_)
    184 						cbqp->ifnp.root_ = NULL;
    185 					if (cl == cbqp->ifnp.default_)
    186 						cbqp->ifnp.default_ = NULL;
    187 #ifdef ALTQ3_COMPAT
    188 					if (cl == cbqp->ifnp.ctl_)
    189 						cbqp->ifnp.ctl_ = NULL;
    190 #endif
    191 				}
    192 			}
    193 		}
    194 	} while (again);
    195 
    196 	return 0;
    197 }
    198 
    199 static int
    200 cbq_request(struct ifaltq *ifq, int req, void *arg)
    201 {
    202 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
    203 
    204 	switch (req) {
    205 	case ALTRQ_PURGE:
    206 		cbq_purge(cbqp);
    207 		break;
    208 	}
    209 	return 0;
    210 }
    211 
    212 /* copy the stats info in rm_class to class_states_t */
    213 static void
    214 get_class_stats(class_stats_t *statsp, struct rm_class *cl)
    215 {
    216 	memset(statsp, 0, sizeof(*statsp));
    217 
    218 	statsp->xmit_cnt	= cl->stats_.xmit_cnt;
    219 	statsp->drop_cnt	= cl->stats_.drop_cnt;
    220 	statsp->over		= cl->stats_.over;
    221 	statsp->borrows		= cl->stats_.borrows;
    222 	statsp->overactions	= cl->stats_.overactions;
    223 	statsp->delays		= cl->stats_.delays;
    224 
    225 	statsp->depth		= cl->depth_;
    226 	statsp->priority	= cl->pri_;
    227 	statsp->maxidle		= cl->maxidle_;
    228 	statsp->minidle		= cl->minidle_;
    229 	statsp->offtime		= cl->offtime_;
    230 	statsp->qmax		= qlimit(cl->q_);
    231 	statsp->ps_per_byte	= cl->ps_per_byte_;
    232 	statsp->wrr_allot	= cl->w_allotment_;
    233 	statsp->qcnt		= qlen(cl->q_);
    234 	statsp->avgidle		= cl->avgidle_;
    235 
    236 	statsp->qtype		= qtype(cl->q_);
    237 #ifdef ALTQ_RED
    238 	if (q_is_red(cl->q_))
    239 		red_getstats(cl->red_, &statsp->red[0]);
    240 #endif
    241 #ifdef ALTQ_RIO
    242 	if (q_is_rio(cl->q_))
    243 		rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
    244 #endif
    245 }
    246 
    247 #if NPF > 0
    248 int
    249 cbq_pfattach(struct pf_altq *a)
    250 {
    251 	struct ifnet	*ifp;
    252 	int		 s, error;
    253 
    254 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
    255 		return (EINVAL);
    256 	s = splnet();
    257 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc,
    258 	    cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL);
    259 	splx(s);
    260 	return error;
    261 }
    262 
    263 int
    264 cbq_add_altq(struct pf_altq *a)
    265 {
    266 	cbq_state_t	*cbqp;
    267 	struct ifnet	*ifp;
    268 
    269 	if ((ifp = ifunit(a->ifname)) == NULL)
    270 		return EINVAL;
    271 	if (!ALTQ_IS_READY(&ifp->if_snd))
    272 		return ENODEV;
    273 
    274 	/* allocate and initialize cbq_state_t */
    275 	cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    276 	if (cbqp == NULL)
    277 		return ENOMEM;
    278 	(void)memset(cbqp, 0, sizeof(cbq_state_t));
    279 	CALLOUT_INIT(&cbqp->cbq_callout);
    280 	cbqp->cbq_qlen = 0;
    281 	cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
    282 
    283 	/* keep the state in pf_altq */
    284 	a->altq_disc = cbqp;
    285 
    286 	return 0;
    287 }
    288 
    289 int
    290 cbq_remove_altq(struct pf_altq *a)
    291 {
    292 	cbq_state_t	*cbqp;
    293 
    294 	if ((cbqp = a->altq_disc) == NULL)
    295 		return EINVAL;
    296 	a->altq_disc = NULL;
    297 
    298 	cbq_clear_interface(cbqp);
    299 
    300 	if (cbqp->ifnp.default_)
    301 		cbq_class_destroy(cbqp, cbqp->ifnp.default_);
    302 	if (cbqp->ifnp.root_)
    303 		cbq_class_destroy(cbqp, cbqp->ifnp.root_);
    304 
    305 	/* deallocate cbq_state_t */
    306 	free(cbqp, M_DEVBUF);
    307 
    308 	return 0;
    309 }
    310 
    311 #define NSEC_TO_PSEC(s)	((uint64_t)(s) * 1000)
    312 int
    313 cbq_add_queue(struct pf_altq *a)
    314 {
    315 	struct rm_class	*borrow, *parent;
    316 	cbq_state_t	*cbqp;
    317 	struct rm_class	*cl;
    318 	struct cbq_opts	*opts;
    319 	int		i, error;
    320 
    321 	if ((cbqp = a->altq_disc) == NULL)
    322 		return EINVAL;
    323 	if (a->qid == 0)
    324 		return EINVAL;
    325 
    326 	/*
    327 	 * find a free slot in the class table.  if the slot matching
    328 	 * the lower bits of qid is free, use this slot.  otherwise,
    329 	 * use the first free slot.
    330 	 */
    331 	i = a->qid % CBQ_MAX_CLASSES;
    332 	if (cbqp->cbq_class_tbl[i] != NULL) {
    333 		for (i = 0; i < CBQ_MAX_CLASSES; i++)
    334 			if (cbqp->cbq_class_tbl[i] == NULL)
    335 				break;
    336 		if (i == CBQ_MAX_CLASSES)
    337 			return (EINVAL);
    338 	}
    339 
    340 	opts = &a->pq_u.cbq_opts;
    341 	/* check parameters */
    342 	if (a->priority >= CBQ_MAXPRI)
    343 		return EINVAL;
    344 
    345 	/* Get pointers to parent and borrow classes.  */
    346 	parent = clh_to_clp(cbqp, a->parent_qid);
    347 	if (opts->flags & CBQCLF_BORROW)
    348 		borrow = parent;
    349 	else
    350 		borrow = NULL;
    351 
    352 	/*
    353 	 * A class must borrow from its parent or it can not
    354 	 * borrow at all.  Hence, borrow can be null.
    355 	 */
    356 	if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
    357 		printf("cbq_add_queue: no parent class!\n");
    358 		return EINVAL;
    359 	}
    360 
    361 	if ((borrow != parent)  && (borrow != NULL)) {
    362 		printf("cbq_add_class: borrow class != parent\n");
    363 		return EINVAL;
    364 	}
    365 
    366 	/*
    367 	 * check parameters
    368 	 */
    369 	if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
    370 		if (parent != NULL)
    371 			return EINVAL;
    372 		if (cbqp->ifnp.root_)
    373 			return EINVAL;
    374 	}
    375 	if ((opts->flags & CBQCLF_DEFCLASS) != 0) {
    376 		if (cbqp->ifnp.default_)
    377 			return EINVAL;
    378 	}
    379 	if ((opts->flags & CBQCLF_CLASSMASK) == 0) {
    380 		if (a->qid == 0)
    381 			return EINVAL;
    382 	}
    383 
    384 	/*
    385 	 * create a class.  if this is a root class, initialize the
    386 	 * interface.
    387 	 */
    388 	if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
    389 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
    390 		    NSEC_TO_PSEC(opts->ns_per_byte), cbqrestart, a->qlimit, RM_MAXQUEUED,
    391 		    opts->maxidle, opts->minidle, opts->offtime,
    392 		    opts->flags);
    393 		if (error != 0)
    394 			return error;
    395 		cl = cbqp->ifnp.root_;
    396 	} else {
    397 		cl = rmc_newclass(a->priority,
    398 				  &cbqp->ifnp, NSEC_TO_PSEC(opts->ns_per_byte),
    399 				  rmc_delay_action, a->qlimit, parent, borrow,
    400 				  opts->maxidle, opts->minidle, opts->offtime,
    401 				  opts->pktsize, opts->flags);
    402 	}
    403 	if (cl == NULL)
    404 		return ENOMEM;
    405 
    406 	/* return handle to user space. */
    407 	cl->stats_.handle = a->qid;
    408 	cl->stats_.depth = cl->depth_;
    409 
    410 	/* save the allocated class */
    411 	cbqp->cbq_class_tbl[i] = cl;
    412 
    413 	if ((opts->flags & CBQCLF_DEFCLASS) != 0)
    414 		cbqp->ifnp.default_ = cl;
    415 
    416 	return 0;
    417 }
    418 
    419 int
    420 cbq_remove_queue(struct pf_altq *a)
    421 {
    422 	struct rm_class	*cl;
    423 	cbq_state_t	*cbqp;
    424 	int		i;
    425 
    426 	if ((cbqp = a->altq_disc) == NULL)
    427 		return EINVAL;
    428 
    429 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
    430 		return EINVAL;
    431 
    432 	/* if we are a parent class, then return an error. */
    433 	if (is_a_parent_class(cl))
    434 		return EINVAL;
    435 
    436 	/* delete the class */
    437 	rmc_delete_class(&cbqp->ifnp, cl);
    438 
    439 	/*
    440 	 * free the class handle
    441 	 */
    442 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    443 		if (cbqp->cbq_class_tbl[i] == cl) {
    444 			cbqp->cbq_class_tbl[i] = NULL;
    445 			if (cl == cbqp->ifnp.root_)
    446 				cbqp->ifnp.root_ = NULL;
    447 			if (cl == cbqp->ifnp.default_)
    448 				cbqp->ifnp.default_ = NULL;
    449 			break;
    450 		}
    451 
    452 	return 0;
    453 }
    454 
    455 int
    456 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
    457 {
    458 	cbq_state_t	*cbqp;
    459 	struct rm_class	*cl;
    460 	class_stats_t	 stats;
    461 	int		 error = 0;
    462 
    463 	if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
    464 		return EBADF;
    465 
    466 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
    467 		return EINVAL;
    468 
    469 	if (*nbytes < sizeof(stats))
    470 		return EINVAL;
    471 
    472 	memset(&stats, 0, sizeof(stats));
    473 	get_class_stats(&stats, cl);
    474 
    475 	if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0)
    476 		return error;
    477 	*nbytes = sizeof(stats);
    478 	return (0);
    479 }
    480 #endif /* NPF > 0 */
    481 
    482 /*
    483  * int
    484  * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
    485  *		- Queue data packets.
    486  *
    487  *	cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
    488  *	layer (e.g. ether_output).  cbq_enqueue queues the given packet
    489  *	to the cbq, then invokes the driver's start routine.
    490  *
    491  *	Assumptions:	called in splnet
    492  *	Returns:	0 if the queueing is successful.
    493  *			ENOBUFS if a packet dropping occurred as a result of
    494  *			the queueing.
    495  */
    496 
    497 static int
    498 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
    499 {
    500 	struct altq_pktattr pktattr;
    501 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
    502 	struct rm_class	*cl;
    503 	struct m_tag	*t;
    504 	int		 len;
    505 
    506 	/* grab class set by classifier */
    507 	if ((m->m_flags & M_PKTHDR) == 0) {
    508 		/* should not happen */
    509 		printf("altq: packet for %s does not have pkthdr\n",
    510 		    ifq->altq_ifp->if_xname);
    511 		m_freem(m);
    512 		return ENOBUFS;
    513 	}
    514 	cl = NULL;
    515 	if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID)) != NULL)
    516 		cl = clh_to_clp(cbqp, ((struct altq_tag *)(t+1))->qid);
    517 #ifdef ALTQ3_COMPAT
    518 	else if (ifq->altq_flags & ALTQF_CLASSIFY)
    519 		cl = m->m_pkthdr.pattr_class;
    520 #endif
    521 	if (cl == NULL) {
    522 		cl = cbqp->ifnp.default_;
    523 		if (cl == NULL) {
    524 			m_freem(m);
    525 			return ENOBUFS;
    526 		}
    527 	}
    528 #ifdef ALTQ3_COMPAT
    529 	if (m->m_pkthdr.pattr_af != AF_UNSPEC) {
    530 		pktattr.pattr_class = m->m_pkthdr.pattr_class;
    531 		pktattr.pattr_af = m->m_pkthdr.pattr_af;
    532 		pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr;
    533 
    534 		cl->pktattr_ = &pktattr;  /* save proto hdr used by ECN */
    535 	} else
    536 #endif
    537 		cl->pktattr_ = NULL;
    538 	len = m_pktlen(m);
    539 	if (rmc_queue_packet(cl, m) != 0) {
    540 		/* drop occurred.  some mbuf was freed in rmc_queue_packet. */
    541 		PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
    542 		return ENOBUFS;
    543 	}
    544 
    545 	/* successfully queued. */
    546 	++cbqp->cbq_qlen;
    547 	IFQ_INC_LEN(ifq);
    548 	return 0;
    549 }
    550 
    551 static struct mbuf *
    552 cbq_dequeue(struct ifaltq *ifq, int op)
    553 {
    554 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
    555 	struct mbuf	*m;
    556 
    557 	m = rmc_dequeue_next(&cbqp->ifnp, op);
    558 
    559 	if (m && op == ALTDQ_REMOVE) {
    560 		--cbqp->cbq_qlen;  /* decrement # of packets in cbq */
    561 		IFQ_DEC_LEN(ifq);
    562 
    563 		/* Update the class. */
    564 		rmc_update_class_util(&cbqp->ifnp);
    565 	}
    566 	return m;
    567 }
    568 
    569 /*
    570  * void
    571  * cbqrestart(queue_t *) - Restart sending of data.
    572  * called from rmc_restart in splnet via timeout after waking up
    573  * a suspended class.
    574  *	Returns:	NONE
    575  */
    576 
    577 static void
    578 cbqrestart(struct ifaltq *ifq)
    579 {
    580 	cbq_state_t	*cbqp;
    581 	struct ifnet	*ifp;
    582 
    583 	if (!ALTQ_IS_ENABLED(ifq))
    584 		/* cbq must have been detached */
    585 		return;
    586 
    587 	if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
    588 		/* should not happen */
    589 		return;
    590 
    591 	ifp = ifq->altq_ifp;
    592 	if (ifp->if_start &&
    593 	    cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
    594 		if_start_lock(ifp);
    595 }
    596 
    597 static void
    598 cbq_purge(cbq_state_t *cbqp)
    599 {
    600 	struct rm_class	*cl;
    601 	int		 i;
    602 
    603 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    604 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
    605 			rmc_dropall(cl);
    606 	if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
    607 		cbqp->ifnp.ifq_->ifq_len = 0;
    608 }
    609 #ifdef ALTQ3_COMPAT
    610 
    611 static int
    612 cbq_add_class(struct cbq_add_class *acp)
    613 {
    614 	char		*ifacename;
    615 	struct rm_class	*borrow, *parent;
    616 	cbq_state_t	*cbqp;
    617 
    618 	ifacename = acp->cbq_iface.cbq_ifacename;
    619 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    620 		return EBADF;
    621 
    622 	/* check parameters */
    623 	if (acp->cbq_class.priority >= CBQ_MAXPRI ||
    624 	    acp->cbq_class.maxq > CBQ_MAXQSIZE)
    625 		return EINVAL;
    626 
    627 	/* Get pointers to parent and borrow classes.  */
    628 	parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
    629 	borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
    630 
    631 	/*
    632 	 * A class must borrow from its parent or it can not
    633 	 * borrow at all.  Hence, borrow can be null.
    634 	 */
    635 	if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
    636 		printf("cbq_add_class: no parent class!\n");
    637 		return EINVAL;
    638 	}
    639 
    640 	if ((borrow != parent)  && (borrow != NULL)) {
    641 		printf("cbq_add_class: borrow class != parent\n");
    642 		return EINVAL;
    643 	}
    644 
    645 	return cbq_class_create(cbqp, acp, parent, borrow);
    646 }
    647 
    648 static int
    649 cbq_delete_class(struct cbq_delete_class *dcp)
    650 {
    651 	char		*ifacename;
    652 	struct rm_class	*cl;
    653 	cbq_state_t	*cbqp;
    654 
    655 	ifacename = dcp->cbq_iface.cbq_ifacename;
    656 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    657 		return EBADF;
    658 
    659 	if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL)
    660 		return EINVAL;
    661 
    662 	/* if we are a parent class, then return an error. */
    663 	if (is_a_parent_class(cl))
    664 		return EINVAL;
    665 
    666 	/* if a filter has a reference to this class delete the filter */
    667 	acc_discard_filters(&cbqp->cbq_classifier, cl, 0);
    668 
    669 	return cbq_class_destroy(cbqp, cl);
    670 }
    671 
    672 static int
    673 cbq_modify_class(struct cbq_modify_class *acp)
    674 {
    675 	char		*ifacename;
    676 	struct rm_class	*cl;
    677 	cbq_state_t	*cbqp;
    678 
    679 	ifacename = acp->cbq_iface.cbq_ifacename;
    680 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    681 		return EBADF;
    682 
    683 	/* Get pointer to this class */
    684 	if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
    685 		return EINVAL;
    686 
    687 	if (rmc_modclass(cl, acp->cbq_class.pico_sec_per_byte,
    688 			 acp->cbq_class.maxq, acp->cbq_class.maxidle,
    689 			 acp->cbq_class.minidle, acp->cbq_class.offtime,
    690 			 acp->cbq_class.pktsize) < 0)
    691 		return EINVAL;
    692 	return 0;
    693 }
    694 
    695 /*
    696  * struct rm_class *
    697  * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
    698  *		struct rm_class *parent, struct rm_class *borrow)
    699  *
    700  * This function create a new traffic class in the CBQ class hierarchy of
    701  * given parameters.  The class that created is either the root, default,
    702  * or a new dynamic class.  If CBQ is not initialized, the root class
    703  * will be created.
    704  */
    705 static int
    706 cbq_class_create(cbq_state_t *cbqp, struct cbq_add_class *acp,
    707     struct rm_class *parent, struct rm_class *borrow)
    708 {
    709 	struct rm_class	*cl;
    710 	cbq_class_spec_t *spec = &acp->cbq_class;
    711 	u_int32_t	chandle;
    712 	int		i, error;
    713 
    714 	/*
    715 	 * allocate class handle
    716 	 */
    717 	for (i = 1; i < CBQ_MAX_CLASSES; i++)
    718 		if (cbqp->cbq_class_tbl[i] == NULL)
    719 			break;
    720 	if (i == CBQ_MAX_CLASSES)
    721 		return EINVAL;
    722 	chandle = i;	/* use the slot number as class handle */
    723 
    724 	/*
    725 	 * create a class.  if this is a root class, initialize the
    726 	 * interface.
    727 	 */
    728 	if ((spec->flags & CBQCLF_ROOTCLASS) != 0) {
    729 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
    730 		    spec->pico_sec_per_byte, cbqrestart, spec->maxq,
    731 		    RM_MAXQUEUED, spec->maxidle, spec->minidle, spec->offtime,
    732 		    spec->flags);
    733 		if (error)
    734 			return error;
    735 		cl = cbqp->ifnp.root_;
    736 	} else {
    737 		cl = rmc_newclass(spec->priority,
    738 				  &cbqp->ifnp, spec->pico_sec_per_byte,
    739 				  rmc_delay_action, spec->maxq, parent, borrow,
    740 				  spec->maxidle, spec->minidle, spec->offtime,
    741 				  spec->pktsize, spec->flags);
    742 	}
    743 	if (cl == NULL)
    744 		return ENOMEM;
    745 
    746 	/* return handle to user space. */
    747 	acp->cbq_class_handle = chandle;
    748 
    749 	cl->stats_.handle = chandle;
    750 	cl->stats_.depth = cl->depth_;
    751 
    752 	/* save the allocated class */
    753 	cbqp->cbq_class_tbl[i] = cl;
    754 
    755 	if ((spec->flags & CBQCLF_DEFCLASS) != 0)
    756 		cbqp->ifnp.default_ = cl;
    757 	if ((spec->flags & CBQCLF_CTLCLASS) != 0)
    758 		cbqp->ifnp.ctl_ = cl;
    759 
    760 	return 0;
    761 }
    762 
    763 static int
    764 cbq_add_filter(struct cbq_add_filter *afp)
    765 {
    766 	char		*ifacename;
    767 	cbq_state_t	*cbqp;
    768 	struct rm_class	*cl;
    769 
    770 	ifacename = afp->cbq_iface.cbq_ifacename;
    771 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    772 		return EBADF;
    773 
    774 	/* Get the pointer to class. */
    775 	if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL)
    776 		return EINVAL;
    777 
    778 	return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter,
    779 			      cl, &afp->cbq_filter_handle);
    780 }
    781 
    782 static int
    783 cbq_delete_filter(struct cbq_delete_filter *dfp)
    784 {
    785 	char		*ifacename;
    786 	cbq_state_t	*cbqp;
    787 
    788 	ifacename = dfp->cbq_iface.cbq_ifacename;
    789 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    790 		return EBADF;
    791 
    792 	return acc_delete_filter(&cbqp->cbq_classifier,
    793 				 dfp->cbq_filter_handle);
    794 }
    795 
    796 /*
    797  * cbq_clear_hierarchy deletes all classes and their filters on the
    798  * given interface.
    799  */
    800 static int
    801 cbq_clear_hierarchy(struct cbq_interface *ifacep)
    802 {
    803 	char		*ifacename;
    804 	cbq_state_t	*cbqp;
    805 
    806 	ifacename = ifacep->cbq_ifacename;
    807 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    808 		return EBADF;
    809 
    810 	return cbq_clear_interface(cbqp);
    811 }
    812 
    813 /*
    814  * static int
    815  * cbq_set_enable(struct cbq_enable *ep) - this function processed the
    816  *	ioctl request to enable class based queueing.  It searches the list
    817  *	of interfaces for the specified interface and then enables CBQ on
    818  *	that interface.
    819  *
    820  *	Returns:	0, for no error.
    821  *			EBADF, for specified interface not found.
    822  */
    823 
    824 static int
    825 cbq_set_enable(struct cbq_interface *ep, int enable)
    826 {
    827 	int 	error = 0;
    828 	cbq_state_t	*cbqp;
    829 	char 	*ifacename;
    830 
    831 	ifacename = ep->cbq_ifacename;
    832 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    833 		return EBADF;
    834 
    835 	switch (enable) {
    836 	case ENABLE:
    837 		if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL) {
    838 			if (cbqp->ifnp.root_ == NULL)
    839 				printf("No Root Class for %s\n", ifacename);
    840 			if (cbqp->ifnp.default_ == NULL)
    841 				printf("No Default Class for %s\n", ifacename);
    842 			error = EINVAL;
    843 		} else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
    844 			cbqp->cbq_qlen = 0;
    845 		}
    846 		break;
    847 
    848 	case DISABLE:
    849 		error = altq_disable(cbqp->ifnp.ifq_);
    850 		break;
    851 	}
    852 	return error;
    853 }
    854 
    855 static int
    856 cbq_getstats(struct cbq_getstats *gsp)
    857 {
    858 	char		*ifacename;
    859 	int		i, n, nclasses;
    860 	cbq_state_t	*cbqp;
    861 	struct rm_class	*cl;
    862 	class_stats_t	stats, *usp;
    863 	int error = 0;
    864 
    865 	ifacename = gsp->iface.cbq_ifacename;
    866 	nclasses = gsp->nclasses;
    867 	usp = gsp->stats;
    868 
    869 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    870 		return EBADF;
    871 	if (nclasses <= 0)
    872 		return EINVAL;
    873 
    874 	for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) {
    875 		while ((cl = cbqp->cbq_class_tbl[i]) == NULL)
    876 			if (++i >= CBQ_MAX_CLASSES)
    877 				goto out;
    878 
    879 		memset(&stats, 0, sizeof(stats));
    880 		get_class_stats(&stats, cl);
    881 		stats.handle = cl->stats_.handle;
    882 
    883 		if ((error = copyout((void *)&stats, (void *)usp++,
    884 		    sizeof(stats))) != 0)
    885 			return error;
    886 	}
    887 
    888  out:
    889 	gsp->nclasses = n;
    890 	return error;
    891 }
    892 
    893 static int
    894 cbq_ifattach(struct cbq_interface *ifacep)
    895 {
    896 	int		error = 0;
    897 	char		*ifacename;
    898 	cbq_state_t	*new_cbqp;
    899 	struct ifnet 	*ifp;
    900 
    901 	ifacename = ifacep->cbq_ifacename;
    902 	if ((ifp = ifunit(ifacename)) == NULL)
    903 		return ENXIO;
    904 	if (!ALTQ_IS_READY(&ifp->if_snd))
    905 		return ENXIO;
    906 
    907 	/* allocate and initialize cbq_state_t */
    908 	new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    909 	if (new_cbqp == NULL)
    910 		return ENOMEM;
    911  	CALLOUT_INIT(&new_cbqp->cbq_callout);
    912 
    913 	new_cbqp->cbq_qlen = 0;
    914 	new_cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
    915 
    916 	/*
    917 	 * set CBQ to this ifnet structure.
    918 	 */
    919 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp,
    920 			    cbq_enqueue, cbq_dequeue, cbq_request,
    921 			    &new_cbqp->cbq_classifier, acc_classify);
    922 	if (error) {
    923 		free(new_cbqp, M_DEVBUF);
    924 		return error;
    925 	}
    926 
    927 	/* prepend to the list of cbq_state_t's. */
    928 	new_cbqp->cbq_next = cbq_list;
    929 	cbq_list = new_cbqp;
    930 
    931 	return 0;
    932 }
    933 
    934 static int
    935 cbq_ifdetach(struct cbq_interface *ifacep)
    936 {
    937 	char		*ifacename;
    938 	cbq_state_t 	*cbqp;
    939 
    940 	ifacename = ifacep->cbq_ifacename;
    941 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    942 		return (EBADF);
    943 
    944 	(void)cbq_set_enable(ifacep, DISABLE);
    945 
    946 	cbq_clear_interface(cbqp);
    947 
    948 	/* remove CBQ from the ifnet structure. */
    949 	(void)altq_detach(cbqp->ifnp.ifq_);
    950 
    951 	/* remove from the list of cbq_state_t's. */
    952 	if (cbq_list == cbqp)
    953 		cbq_list = cbqp->cbq_next;
    954 	else {
    955 		cbq_state_t *cp;
    956 
    957 		for (cp = cbq_list; cp != NULL; cp = cp->cbq_next)
    958 			if (cp->cbq_next == cbqp) {
    959 				cp->cbq_next = cbqp->cbq_next;
    960 				break;
    961 			}
    962 		ASSERT(cp != NULL);
    963 	}
    964 
    965 	/* deallocate cbq_state_t */
    966 	free(cbqp, M_DEVBUF);
    967 
    968 	return 0;
    969 }
    970 
    971 /*
    972  * cbq device interface
    973  */
    974 
    975 altqdev_decl(cbq);
    976 
    977 int
    978 cbqopen(dev_t dev, int flag, int fmt,
    979     struct lwp *l)
    980 {
    981 	return 0;
    982 }
    983 
    984 int
    985 cbqclose(dev_t dev, int flag, int fmt,
    986     struct lwp *l)
    987 {
    988 	struct ifnet *ifp;
    989 	struct cbq_interface iface;
    990 	int err, error = 0;
    991 
    992 	while (cbq_list) {
    993 		ifp = cbq_list->ifnp.ifq_->altq_ifp;
    994 		snprintf(iface.cbq_ifacename, sizeof(iface.cbq_ifacename),
    995 		    "%s", ifp->if_xname);
    996 		err = cbq_ifdetach(&iface);
    997 		if (err != 0 && error == 0)
    998 			error = err;
    999 	}
   1000 
   1001 	return error;
   1002 }
   1003 
   1004 int
   1005 cbqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
   1006     struct lwp *l)
   1007 {
   1008 	int	error = 0;
   1009 
   1010 	/* check cmd for superuser only */
   1011 	switch (cmd) {
   1012 	case CBQ_GETSTATS:
   1013 		/* currently only command that an ordinary user can call */
   1014 		break;
   1015 	default:
   1016 		error = kauth_authorize_network(l->l_cred,
   1017 		    KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_CBQ, NULL, NULL,
   1018 		    NULL);
   1019 		if (error)
   1020 			return error;
   1021 		break;
   1022 	}
   1023 
   1024 	switch (cmd) {
   1025 
   1026 	case CBQ_ENABLE:
   1027 		error = cbq_set_enable((struct cbq_interface *)addr, ENABLE);
   1028 		break;
   1029 
   1030 	case CBQ_DISABLE:
   1031 		error = cbq_set_enable((struct cbq_interface *)addr, DISABLE);
   1032 		break;
   1033 
   1034 	case CBQ_ADD_FILTER:
   1035 		error = cbq_add_filter((struct cbq_add_filter *)addr);
   1036 		break;
   1037 
   1038 	case CBQ_DEL_FILTER:
   1039 		error = cbq_delete_filter((struct cbq_delete_filter *)addr);
   1040 		break;
   1041 
   1042 	case CBQ_ADD_CLASS:
   1043 		error = cbq_add_class((struct cbq_add_class *)addr);
   1044 		break;
   1045 
   1046 	case CBQ_DEL_CLASS:
   1047 		error = cbq_delete_class((struct cbq_delete_class *)addr);
   1048 		break;
   1049 
   1050 	case CBQ_MODIFY_CLASS:
   1051 		error = cbq_modify_class((struct cbq_modify_class *)addr);
   1052 		break;
   1053 
   1054 	case CBQ_CLEAR_HIERARCHY:
   1055 		error = cbq_clear_hierarchy((struct cbq_interface *)addr);
   1056 		break;
   1057 
   1058 	case CBQ_IF_ATTACH:
   1059 		error = cbq_ifattach((struct cbq_interface *)addr);
   1060 		break;
   1061 
   1062 	case CBQ_IF_DETACH:
   1063 		error = cbq_ifdetach((struct cbq_interface *)addr);
   1064 		break;
   1065 
   1066 	case CBQ_GETSTATS:
   1067 		error = cbq_getstats((struct cbq_getstats *)addr);
   1068 		break;
   1069 
   1070 	default:
   1071 		error = EINVAL;
   1072 		break;
   1073 	}
   1074 
   1075 	return error;
   1076 }
   1077 
   1078 #if 0
   1079 /* for debug */
   1080 static void cbq_class_dump(int);
   1081 
   1082 static void
   1083 cbq_class_dump(int i)
   1084 {
   1085 	struct rm_class *cl;
   1086 	rm_class_stats_t *s;
   1087 	struct _class_queue_ *q;
   1088 
   1089 	if (cbq_list == NULL) {
   1090 		printf("cbq_class_dump: no cbq_state found\n");
   1091 		return;
   1092 	}
   1093 	cl = cbq_list->cbq_class_tbl[i];
   1094 
   1095 	printf("class %d cl=%p\n", i, cl);
   1096 	if (cl != NULL) {
   1097 		s = &cl->stats_;
   1098 		q = cl->q_;
   1099 
   1100 		printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n",
   1101 		       cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_);
   1102 		printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n",
   1103 		       cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_,
   1104 		       cl->maxidle_);
   1105 		printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n",
   1106 		       cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_);
   1107 		printf("handle=%d, depth=%d, packets=%d, bytes=%d\n",
   1108 		       s->handle, s->depth,
   1109 		       (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes);
   1110 		printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n",
   1111 		       s->over, s->borrows, (int)s->drop_cnt.packets,
   1112 		       s->overactions, s->delays);
   1113 		printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n",
   1114 		       q->tail_, q->head_, q->qlen_, q->qlim_,
   1115 		       q->qthresh_, q->qtype_);
   1116 	}
   1117 }
   1118 #endif /* 0 */
   1119 
   1120 #ifdef KLD_MODULE
   1121 
   1122 static struct altqsw cbq_sw =
   1123 	{"cbq", cbqopen, cbqclose, cbqioctl};
   1124 
   1125 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw);
   1126 MODULE_DEPEND(altq_cbq, altq_red, 1, 1, 1);
   1127 MODULE_DEPEND(altq_cbq, altq_rio, 1, 1, 1);
   1128 
   1129 #endif /* KLD_MODULE */
   1130 #endif /* ALTQ3_COMPAT */
   1131 
   1132 #endif /* ALTQ_CBQ */
   1133