Home | History | Annotate | Line # | Download | only in altq
altq_cbq.c revision 1.35
      1 /*	$NetBSD: altq_cbq.c,v 1.35 2021/07/21 06:33:30 ozaki-r 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.35 2021/07/21 06:33:30 ozaki-r 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 	statsp->xmit_cnt	= cl->stats_.xmit_cnt;
    217 	statsp->drop_cnt	= cl->stats_.drop_cnt;
    218 	statsp->over		= cl->stats_.over;
    219 	statsp->borrows		= cl->stats_.borrows;
    220 	statsp->overactions	= cl->stats_.overactions;
    221 	statsp->delays		= cl->stats_.delays;
    222 
    223 	statsp->depth		= cl->depth_;
    224 	statsp->priority	= cl->pri_;
    225 	statsp->maxidle		= cl->maxidle_;
    226 	statsp->minidle		= cl->minidle_;
    227 	statsp->offtime		= cl->offtime_;
    228 	statsp->qmax		= qlimit(cl->q_);
    229 	statsp->ps_per_byte	= cl->ps_per_byte_;
    230 	statsp->wrr_allot	= cl->w_allotment_;
    231 	statsp->qcnt		= qlen(cl->q_);
    232 	statsp->avgidle		= cl->avgidle_;
    233 
    234 	statsp->qtype		= qtype(cl->q_);
    235 #ifdef ALTQ_RED
    236 	if (q_is_red(cl->q_))
    237 		red_getstats(cl->red_, &statsp->red[0]);
    238 #endif
    239 #ifdef ALTQ_RIO
    240 	if (q_is_rio(cl->q_))
    241 		rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
    242 #endif
    243 }
    244 
    245 #if NPF > 0
    246 int
    247 cbq_pfattach(struct pf_altq *a)
    248 {
    249 	struct ifnet	*ifp;
    250 	int		 s, error;
    251 
    252 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
    253 		return (EINVAL);
    254 	s = splnet();
    255 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc,
    256 	    cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL);
    257 	splx(s);
    258 	return (error);
    259 }
    260 
    261 int
    262 cbq_add_altq(struct pf_altq *a)
    263 {
    264 	cbq_state_t	*cbqp;
    265 	struct ifnet	*ifp;
    266 
    267 	if ((ifp = ifunit(a->ifname)) == NULL)
    268 		return (EINVAL);
    269 	if (!ALTQ_IS_READY(&ifp->if_snd))
    270 		return (ENODEV);
    271 
    272 	/* allocate and initialize cbq_state_t */
    273 	cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    274 	if (cbqp == NULL)
    275 		return (ENOMEM);
    276 	(void)memset(cbqp, 0, sizeof(cbq_state_t));
    277 	CALLOUT_INIT(&cbqp->cbq_callout);
    278 	cbqp->cbq_qlen = 0;
    279 	cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
    280 
    281 	/* keep the state in pf_altq */
    282 	a->altq_disc = cbqp;
    283 
    284 	return (0);
    285 }
    286 
    287 int
    288 cbq_remove_altq(struct pf_altq *a)
    289 {
    290 	cbq_state_t	*cbqp;
    291 
    292 	if ((cbqp = a->altq_disc) == NULL)
    293 		return (EINVAL);
    294 	a->altq_disc = NULL;
    295 
    296 	cbq_clear_interface(cbqp);
    297 
    298 	if (cbqp->ifnp.default_)
    299 		cbq_class_destroy(cbqp, cbqp->ifnp.default_);
    300 	if (cbqp->ifnp.root_)
    301 		cbq_class_destroy(cbqp, cbqp->ifnp.root_);
    302 
    303 	/* deallocate cbq_state_t */
    304 	free(cbqp, M_DEVBUF);
    305 
    306 	return (0);
    307 }
    308 
    309 int
    310 cbq_add_queue(struct pf_altq *a)
    311 {
    312 	struct rm_class	*borrow, *parent;
    313 	cbq_state_t	*cbqp;
    314 	struct rm_class	*cl;
    315 	struct cbq_opts	*opts;
    316 	int		i, error;
    317 
    318 	if ((cbqp = a->altq_disc) == NULL)
    319 		return (EINVAL);
    320 	if (a->qid == 0)
    321 		return (EINVAL);
    322 
    323 	/*
    324 	 * find a free slot in the class table.  if the slot matching
    325 	 * the lower bits of qid is free, use this slot.  otherwise,
    326 	 * use the first free slot.
    327 	 */
    328 	i = a->qid % CBQ_MAX_CLASSES;
    329 	if (cbqp->cbq_class_tbl[i] != NULL) {
    330 		for (i = 0; i < CBQ_MAX_CLASSES; i++)
    331 			if (cbqp->cbq_class_tbl[i] == NULL)
    332 				break;
    333 		if (i == CBQ_MAX_CLASSES)
    334 			return (EINVAL);
    335 	}
    336 
    337 	opts = &a->pq_u.cbq_opts;
    338 	/* check parameters */
    339 	if (a->priority >= CBQ_MAXPRI)
    340 		return (EINVAL);
    341 
    342 	/* Get pointers to parent and borrow classes.  */
    343 	parent = clh_to_clp(cbqp, a->parent_qid);
    344 	if (opts->flags & CBQCLF_BORROW)
    345 		borrow = parent;
    346 	else
    347 		borrow = NULL;
    348 
    349 	/*
    350 	 * A class must borrow from its parent or it can not
    351 	 * borrow at all.  Hence, borrow can be null.
    352 	 */
    353 	if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
    354 		printf("cbq_add_queue: no parent class!\n");
    355 		return (EINVAL);
    356 	}
    357 
    358 	if ((borrow != parent)  && (borrow != NULL)) {
    359 		printf("cbq_add_class: borrow class != parent\n");
    360 		return (EINVAL);
    361 	}
    362 
    363 	/*
    364 	 * check parameters
    365 	 */
    366 	if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
    367 		if (parent != NULL)
    368 			return (EINVAL);
    369 		if (cbqp->ifnp.root_)
    370 			return (EINVAL);
    371 	}
    372 	if ((opts->flags & CBQCLF_DEFCLASS) != 0) {
    373 		if (cbqp->ifnp.default_)
    374 			return (EINVAL);
    375 	}
    376 	if ((opts->flags & CBQCLF_CLASSMASK) == 0) {
    377 		if (a->qid == 0)
    378 			return (EINVAL);
    379 	}
    380 
    381 	/*
    382 	 * create a class.  if this is a root class, initialize the
    383 	 * interface.
    384 	 */
    385 	if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
    386 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
    387 		    opts->ps_per_byte, cbqrestart, a->qlimit, RM_MAXQUEUED,
    388 		    opts->maxidle, opts->minidle, opts->offtime,
    389 		    opts->flags);
    390 		if (error != 0)
    391 			return (error);
    392 		cl = cbqp->ifnp.root_;
    393 	} else {
    394 		cl = rmc_newclass(a->priority,
    395 				  &cbqp->ifnp, opts->ps_per_byte,
    396 				  rmc_delay_action, a->qlimit, parent, borrow,
    397 				  opts->maxidle, opts->minidle, opts->offtime,
    398 				  opts->pktsize, opts->flags);
    399 	}
    400 	if (cl == NULL)
    401 		return (ENOMEM);
    402 
    403 	/* return handle to user space. */
    404 	cl->stats_.handle = a->qid;
    405 	cl->stats_.depth = cl->depth_;
    406 
    407 	/* save the allocated class */
    408 	cbqp->cbq_class_tbl[i] = cl;
    409 
    410 	if ((opts->flags & CBQCLF_DEFCLASS) != 0)
    411 		cbqp->ifnp.default_ = cl;
    412 
    413 	return (0);
    414 }
    415 
    416 int
    417 cbq_remove_queue(struct pf_altq *a)
    418 {
    419 	struct rm_class	*cl;
    420 	cbq_state_t	*cbqp;
    421 	int		i;
    422 
    423 	if ((cbqp = a->altq_disc) == NULL)
    424 		return (EINVAL);
    425 
    426 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
    427 		return (EINVAL);
    428 
    429 	/* if we are a parent class, then return an error. */
    430 	if (is_a_parent_class(cl))
    431 		return (EINVAL);
    432 
    433 	/* delete the class */
    434 	rmc_delete_class(&cbqp->ifnp, cl);
    435 
    436 	/*
    437 	 * free the class handle
    438 	 */
    439 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    440 		if (cbqp->cbq_class_tbl[i] == cl) {
    441 			cbqp->cbq_class_tbl[i] = NULL;
    442 			if (cl == cbqp->ifnp.root_)
    443 				cbqp->ifnp.root_ = NULL;
    444 			if (cl == cbqp->ifnp.default_)
    445 				cbqp->ifnp.default_ = NULL;
    446 			break;
    447 		}
    448 
    449 	return (0);
    450 }
    451 
    452 int
    453 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
    454 {
    455 	cbq_state_t	*cbqp;
    456 	struct rm_class	*cl;
    457 	class_stats_t	 stats;
    458 	int		 error = 0;
    459 
    460 	if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
    461 		return (EBADF);
    462 
    463 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
    464 		return (EINVAL);
    465 
    466 	if (*nbytes < sizeof(stats))
    467 		return (EINVAL);
    468 
    469 	memset(&stats, 0, sizeof(stats));
    470 	get_class_stats(&stats, cl);
    471 
    472 	if ((error = copyout((void *)&stats, ubuf, sizeof(stats))) != 0)
    473 		return (error);
    474 	*nbytes = sizeof(stats);
    475 	return (0);
    476 }
    477 #endif /* NPF > 0 */
    478 
    479 /*
    480  * int
    481  * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
    482  *		- Queue data packets.
    483  *
    484  *	cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
    485  *	layer (e.g. ether_output).  cbq_enqueue queues the given packet
    486  *	to the cbq, then invokes the driver's start routine.
    487  *
    488  *	Assumptions:	called in splnet
    489  *	Returns:	0 if the queueing is successful.
    490  *			ENOBUFS if a packet dropping occurred as a result of
    491  *			the queueing.
    492  */
    493 
    494 static int
    495 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
    496 {
    497 	struct altq_pktattr pktattr;
    498 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
    499 	struct rm_class	*cl;
    500 	struct m_tag	*t;
    501 	int		 len;
    502 
    503 	/* grab class set by classifier */
    504 	if ((m->m_flags & M_PKTHDR) == 0) {
    505 		/* should not happen */
    506 		printf("altq: packet for %s does not have pkthdr\n",
    507 		    ifq->altq_ifp->if_xname);
    508 		m_freem(m);
    509 		return (ENOBUFS);
    510 	}
    511 	cl = NULL;
    512 	if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID)) != NULL)
    513 		cl = clh_to_clp(cbqp, ((struct altq_tag *)(t+1))->qid);
    514 #ifdef ALTQ3_COMPAT
    515 	else if (ifq->altq_flags & ALTQF_CLASSIFY)
    516 		cl = m->m_pkthdr.pattr_class;
    517 #endif
    518 	if (cl == NULL) {
    519 		cl = cbqp->ifnp.default_;
    520 		if (cl == NULL) {
    521 			m_freem(m);
    522 			return (ENOBUFS);
    523 		}
    524 	}
    525 #ifdef ALTQ3_COMPAT
    526 	if (m->m_pkthdr.pattr_af != AF_UNSPEC) {
    527 		pktattr.pattr_class = m->m_pkthdr.pattr_class;
    528 		pktattr.pattr_af = m->m_pkthdr.pattr_af;
    529 		pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr;
    530 
    531 		cl->pktattr_ = &pktattr;  /* save proto hdr used by ECN */
    532 	} else
    533 #endif
    534 		cl->pktattr_ = NULL;
    535 	len = m_pktlen(m);
    536 	if (rmc_queue_packet(cl, m) != 0) {
    537 		/* drop occurred.  some mbuf was freed in rmc_queue_packet. */
    538 		PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
    539 		return (ENOBUFS);
    540 	}
    541 
    542 	/* successfully queued. */
    543 	++cbqp->cbq_qlen;
    544 	IFQ_INC_LEN(ifq);
    545 	return (0);
    546 }
    547 
    548 static struct mbuf *
    549 cbq_dequeue(struct ifaltq *ifq, int op)
    550 {
    551 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
    552 	struct mbuf	*m;
    553 
    554 	m = rmc_dequeue_next(&cbqp->ifnp, op);
    555 
    556 	if (m && op == ALTDQ_REMOVE) {
    557 		--cbqp->cbq_qlen;  /* decrement # of packets in cbq */
    558 		IFQ_DEC_LEN(ifq);
    559 
    560 		/* Update the class. */
    561 		rmc_update_class_util(&cbqp->ifnp);
    562 	}
    563 	return (m);
    564 }
    565 
    566 /*
    567  * void
    568  * cbqrestart(queue_t *) - Restart sending of data.
    569  * called from rmc_restart in splnet via timeout after waking up
    570  * a suspended class.
    571  *	Returns:	NONE
    572  */
    573 
    574 static void
    575 cbqrestart(struct ifaltq *ifq)
    576 {
    577 	cbq_state_t	*cbqp;
    578 	struct ifnet	*ifp;
    579 
    580 	if (!ALTQ_IS_ENABLED(ifq))
    581 		/* cbq must have been detached */
    582 		return;
    583 
    584 	if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
    585 		/* should not happen */
    586 		return;
    587 
    588 	ifp = ifq->altq_ifp;
    589 	if (ifp->if_start &&
    590 	    cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
    591 		if_start_lock(ifp);
    592 }
    593 
    594 static void
    595 cbq_purge(cbq_state_t *cbqp)
    596 {
    597 	struct rm_class	*cl;
    598 	int		 i;
    599 
    600 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    601 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
    602 			rmc_dropall(cl);
    603 	if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
    604 		cbqp->ifnp.ifq_->ifq_len = 0;
    605 }
    606 #ifdef ALTQ3_COMPAT
    607 
    608 static int
    609 cbq_add_class(struct cbq_add_class *acp)
    610 {
    611 	char		*ifacename;
    612 	struct rm_class	*borrow, *parent;
    613 	cbq_state_t	*cbqp;
    614 
    615 	ifacename = acp->cbq_iface.cbq_ifacename;
    616 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    617 		return (EBADF);
    618 
    619 	/* check parameters */
    620 	if (acp->cbq_class.priority >= CBQ_MAXPRI ||
    621 	    acp->cbq_class.maxq > CBQ_MAXQSIZE)
    622 		return (EINVAL);
    623 
    624 	/* Get pointers to parent and borrow classes.  */
    625 	parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
    626 	borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
    627 
    628 	/*
    629 	 * A class must borrow from its parent or it can not
    630 	 * borrow at all.  Hence, borrow can be null.
    631 	 */
    632 	if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
    633 		printf("cbq_add_class: no parent class!\n");
    634 		return (EINVAL);
    635 	}
    636 
    637 	if ((borrow != parent)  && (borrow != NULL)) {
    638 		printf("cbq_add_class: borrow class != parent\n");
    639 		return (EINVAL);
    640 	}
    641 
    642 	return cbq_class_create(cbqp, acp, parent, borrow);
    643 }
    644 
    645 static int
    646 cbq_delete_class(struct cbq_delete_class *dcp)
    647 {
    648 	char		*ifacename;
    649 	struct rm_class	*cl;
    650 	cbq_state_t	*cbqp;
    651 
    652 	ifacename = dcp->cbq_iface.cbq_ifacename;
    653 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    654 		return (EBADF);
    655 
    656 	if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL)
    657 		return (EINVAL);
    658 
    659 	/* if we are a parent class, then return an error. */
    660 	if (is_a_parent_class(cl))
    661 		return (EINVAL);
    662 
    663 	/* if a filter has a reference to this class delete the filter */
    664 	acc_discard_filters(&cbqp->cbq_classifier, cl, 0);
    665 
    666 	return cbq_class_destroy(cbqp, cl);
    667 }
    668 
    669 static int
    670 cbq_modify_class(struct cbq_modify_class *acp)
    671 {
    672 	char		*ifacename;
    673 	struct rm_class	*cl;
    674 	cbq_state_t	*cbqp;
    675 
    676 	ifacename = acp->cbq_iface.cbq_ifacename;
    677 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    678 		return (EBADF);
    679 
    680 	/* Get pointer to this class */
    681 	if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
    682 		return (EINVAL);
    683 
    684 	if (rmc_modclass(cl, acp->cbq_class.pico_sec_per_byte,
    685 			 acp->cbq_class.maxq, acp->cbq_class.maxidle,
    686 			 acp->cbq_class.minidle, acp->cbq_class.offtime,
    687 			 acp->cbq_class.pktsize) < 0)
    688 		return (EINVAL);
    689 	return (0);
    690 }
    691 
    692 /*
    693  * struct rm_class *
    694  * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
    695  *		struct rm_class *parent, struct rm_class *borrow)
    696  *
    697  * This function create a new traffic class in the CBQ class hierarchy of
    698  * given paramters.  The class that created is either the root, default,
    699  * or a new dynamic class.  If CBQ is not initilaized, the root class
    700  * will be created.
    701  */
    702 static int
    703 cbq_class_create(cbq_state_t *cbqp, struct cbq_add_class *acp,
    704     struct rm_class *parent, struct rm_class *borrow)
    705 {
    706 	struct rm_class	*cl;
    707 	cbq_class_spec_t *spec = &acp->cbq_class;
    708 	u_int32_t	chandle;
    709 	int		i, error;
    710 
    711 	/*
    712 	 * allocate class handle
    713 	 */
    714 	for (i = 1; i < CBQ_MAX_CLASSES; i++)
    715 		if (cbqp->cbq_class_tbl[i] == NULL)
    716 			break;
    717 	if (i == CBQ_MAX_CLASSES)
    718 		return (EINVAL);
    719 	chandle = i;	/* use the slot number as class handle */
    720 
    721 	/*
    722 	 * create a class.  if this is a root class, initialize the
    723 	 * interface.
    724 	 */
    725 	if ((spec->flags & CBQCLF_ROOTCLASS) != 0) {
    726 		error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
    727 		    spec->pico_sec_per_byte, cbqrestart, spec->maxq,
    728 		    RM_MAXQUEUED, spec->maxidle, spec->minidle, spec->offtime,
    729 		    spec->flags);
    730 		if (error)
    731 			return (error);
    732 		cl = cbqp->ifnp.root_;
    733 	} else {
    734 		cl = rmc_newclass(spec->priority,
    735 				  &cbqp->ifnp, spec->pico_sec_per_byte,
    736 				  rmc_delay_action, spec->maxq, parent, borrow,
    737 				  spec->maxidle, spec->minidle, spec->offtime,
    738 				  spec->pktsize, spec->flags);
    739 	}
    740 	if (cl == NULL)
    741 		return (ENOMEM);
    742 
    743 	/* return handle to user space. */
    744 	acp->cbq_class_handle = chandle;
    745 
    746 	cl->stats_.handle = chandle;
    747 	cl->stats_.depth = cl->depth_;
    748 
    749 	/* save the allocated class */
    750 	cbqp->cbq_class_tbl[i] = cl;
    751 
    752 	if ((spec->flags & CBQCLF_CLASSMASK) != 0)
    753 		cbqp->ifnp.default_ = cl;
    754 	if ((spec->flags & CBQCLF_CTLCLASS) != 0)
    755 		cbqp->ifnp.ctl_ = cl;
    756 
    757 	return (0);
    758 }
    759 
    760 static int
    761 cbq_add_filter(struct cbq_add_filter *afp)
    762 {
    763 	char		*ifacename;
    764 	cbq_state_t	*cbqp;
    765 	struct rm_class	*cl;
    766 
    767 	ifacename = afp->cbq_iface.cbq_ifacename;
    768 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    769 		return (EBADF);
    770 
    771 	/* Get the pointer to class. */
    772 	if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL)
    773 		return (EINVAL);
    774 
    775 	return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter,
    776 			      cl, &afp->cbq_filter_handle);
    777 }
    778 
    779 static int
    780 cbq_delete_filter(struct cbq_delete_filter *dfp)
    781 {
    782 	char		*ifacename;
    783 	cbq_state_t	*cbqp;
    784 
    785 	ifacename = dfp->cbq_iface.cbq_ifacename;
    786 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    787 		return (EBADF);
    788 
    789 	return acc_delete_filter(&cbqp->cbq_classifier,
    790 				 dfp->cbq_filter_handle);
    791 }
    792 
    793 /*
    794  * cbq_clear_hierarchy deletes all classes and their filters on the
    795  * given interface.
    796  */
    797 static int
    798 cbq_clear_hierarchy(struct cbq_interface *ifacep)
    799 {
    800 	char		*ifacename;
    801 	cbq_state_t	*cbqp;
    802 
    803 	ifacename = ifacep->cbq_ifacename;
    804 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    805 		return (EBADF);
    806 
    807 	return cbq_clear_interface(cbqp);
    808 }
    809 
    810 /*
    811  * static int
    812  * cbq_set_enable(struct cbq_enable *ep) - this function processed the
    813  *	ioctl request to enable class based queueing.  It searches the list
    814  *	of interfaces for the specified interface and then enables CBQ on
    815  *	that interface.
    816  *
    817  *	Returns:	0, for no error.
    818  *			EBADF, for specified inteface not found.
    819  */
    820 
    821 static int
    822 cbq_set_enable(struct cbq_interface *ep, int enable)
    823 {
    824 	int 	error = 0;
    825 	cbq_state_t	*cbqp;
    826 	char 	*ifacename;
    827 
    828 	ifacename = ep->cbq_ifacename;
    829 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    830 		return (EBADF);
    831 
    832 	switch (enable) {
    833 	case ENABLE:
    834 		if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL) {
    835 			if (cbqp->ifnp.root_ == NULL)
    836 				printf("No Root Class for %s\n", ifacename);
    837 			if (cbqp->ifnp.default_ == NULL)
    838 				printf("No Default Class for %s\n", ifacename);
    839 			error = EINVAL;
    840 		} else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
    841 			cbqp->cbq_qlen = 0;
    842 		}
    843 		break;
    844 
    845 	case DISABLE:
    846 		error = altq_disable(cbqp->ifnp.ifq_);
    847 		break;
    848 	}
    849 	return (error);
    850 }
    851 
    852 static int
    853 cbq_getstats(struct cbq_getstats *gsp)
    854 {
    855 	char		*ifacename;
    856 	int		i, n, nclasses;
    857 	cbq_state_t	*cbqp;
    858 	struct rm_class	*cl;
    859 	class_stats_t	stats, *usp;
    860 	int error = 0;
    861 
    862 	ifacename = gsp->iface.cbq_ifacename;
    863 	nclasses = gsp->nclasses;
    864 	usp = gsp->stats;
    865 
    866 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    867 		return (EBADF);
    868 	if (nclasses <= 0)
    869 		return (EINVAL);
    870 
    871 	for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) {
    872 		while ((cl = cbqp->cbq_class_tbl[i]) == NULL)
    873 			if (++i >= CBQ_MAX_CLASSES)
    874 				goto out;
    875 
    876 		memset(&stats, 0, sizeof(stats));
    877 		get_class_stats(&stats, cl);
    878 		stats.handle = cl->stats_.handle;
    879 
    880 		if ((error = copyout((void *)&stats, (void *)usp++,
    881 		    sizeof(stats))) != 0)
    882 			return (error);
    883 	}
    884 
    885  out:
    886 	gsp->nclasses = n;
    887 	return (error);
    888 }
    889 
    890 static int
    891 cbq_ifattach(struct cbq_interface *ifacep)
    892 {
    893 	int		error = 0;
    894 	char		*ifacename;
    895 	cbq_state_t	*new_cbqp;
    896 	struct ifnet 	*ifp;
    897 
    898 	ifacename = ifacep->cbq_ifacename;
    899 	if ((ifp = ifunit(ifacename)) == NULL)
    900 		return (ENXIO);
    901 	if (!ALTQ_IS_READY(&ifp->if_snd))
    902 		return (ENXIO);
    903 
    904 	/* allocate and initialize cbq_state_t */
    905 	new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    906 	if (new_cbqp == NULL)
    907 		return (ENOMEM);
    908  	CALLOUT_INIT(&new_cbqp->cbq_callout);
    909 
    910 	new_cbqp->cbq_qlen = 0;
    911 	new_cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
    912 
    913 	/*
    914 	 * set CBQ to this ifnet structure.
    915 	 */
    916 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp,
    917 			    cbq_enqueue, cbq_dequeue, cbq_request,
    918 			    &new_cbqp->cbq_classifier, acc_classify);
    919 	if (error) {
    920 		free(new_cbqp, M_DEVBUF);
    921 		return (error);
    922 	}
    923 
    924 	/* prepend to the list of cbq_state_t's. */
    925 	new_cbqp->cbq_next = cbq_list;
    926 	cbq_list = new_cbqp;
    927 
    928 	return (0);
    929 }
    930 
    931 static int
    932 cbq_ifdetach(struct cbq_interface *ifacep)
    933 {
    934 	char		*ifacename;
    935 	cbq_state_t 	*cbqp;
    936 
    937 	ifacename = ifacep->cbq_ifacename;
    938 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    939 		return (EBADF);
    940 
    941 	(void)cbq_set_enable(ifacep, DISABLE);
    942 
    943 	cbq_clear_interface(cbqp);
    944 
    945 	/* remove CBQ from the ifnet structure. */
    946 	(void)altq_detach(cbqp->ifnp.ifq_);
    947 
    948 	/* remove from the list of cbq_state_t's. */
    949 	if (cbq_list == cbqp)
    950 		cbq_list = cbqp->cbq_next;
    951 	else {
    952 		cbq_state_t *cp;
    953 
    954 		for (cp = cbq_list; cp != NULL; cp = cp->cbq_next)
    955 			if (cp->cbq_next == cbqp) {
    956 				cp->cbq_next = cbqp->cbq_next;
    957 				break;
    958 			}
    959 		ASSERT(cp != NULL);
    960 	}
    961 
    962 	/* deallocate cbq_state_t */
    963 	free(cbqp, M_DEVBUF);
    964 
    965 	return (0);
    966 }
    967 
    968 /*
    969  * cbq device interface
    970  */
    971 
    972 altqdev_decl(cbq);
    973 
    974 int
    975 cbqopen(dev_t dev, int flag, int fmt,
    976     struct lwp *l)
    977 {
    978 	return (0);
    979 }
    980 
    981 int
    982 cbqclose(dev_t dev, int flag, int fmt,
    983     struct lwp *l)
    984 {
    985 	struct ifnet *ifp;
    986 	struct cbq_interface iface;
    987 	int err, error = 0;
    988 
    989 	while (cbq_list) {
    990 		ifp = cbq_list->ifnp.ifq_->altq_ifp;
    991 		snprintf(iface.cbq_ifacename, sizeof(iface.cbq_ifacename),
    992 		    "%s", ifp->if_xname);
    993 		err = cbq_ifdetach(&iface);
    994 		if (err != 0 && error == 0)
    995 			error = err;
    996 	}
    997 
    998 	return (error);
    999 }
   1000 
   1001 int
   1002 cbqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
   1003     struct lwp *l)
   1004 {
   1005 	int	error = 0;
   1006 
   1007 	/* check cmd for superuser only */
   1008 	switch (cmd) {
   1009 	case CBQ_GETSTATS:
   1010 		/* currently only command that an ordinary user can call */
   1011 		break;
   1012 	default:
   1013 #if (__FreeBSD_version > 400000)
   1014 		error = suser(p);
   1015 #else
   1016 		error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_ALTQ,
   1017 		    KAUTH_REQ_NETWORK_ALTQ_CBQ, NULL, NULL, NULL);
   1018 #endif
   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