Home | History | Annotate | Line # | Download | only in altq
altq_cbq.c revision 1.16
      1 /*	$NetBSD: altq_cbq.c,v 1.16 2006/05/15 00:05:39 christos Exp $	*/
      2 /*	$KAME: altq_cbq.c,v 1.11 2002/10/04 14:24:09 kjc 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.16 2006/05/15 00:05:39 christos Exp $");
     36 
     37 #if defined(__FreeBSD__) || defined(__NetBSD__)
     38 #include "opt_altq.h"
     39 #if (__FreeBSD__ != 2)
     40 #include "opt_inet.h"
     41 #ifdef __FreeBSD__
     42 #include "opt_inet6.h"
     43 #endif
     44 #endif
     45 #endif /* __FreeBSD__ || __NetBSD__ */
     46 #ifdef ALTQ_CBQ	/* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
     47 
     48 /* #pragma ident "@(#)cbq.c  1.39     98/05/13 SMI" */
     49 
     50 #include <sys/param.h>
     51 #include <sys/malloc.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/uio.h>
     54 #include <sys/socket.h>
     55 #include <sys/systm.h>
     56 #include <sys/proc.h>
     57 #include <sys/errno.h>
     58 #include <sys/time.h>
     59 #include <sys/kernel.h>
     60 #include <sys/kauth.h>
     61 
     62 #include <net/if.h>
     63 #include <net/if_types.h>
     64 #include <netinet/in.h>
     65 
     66 #include <altq/altq.h>
     67 #include <altq/altq_conf.h>
     68 #include <altq/altq_cbq.h>
     69 
     70 /*
     71  * Local Data structures.
     72  */
     73 static cbq_state_t *cbq_list = NULL;
     74 
     75 /*
     76  * Forward Declarations.
     77  */
     78 
     79 static int	cbq_add_class __P((struct cbq_add_class *));
     80 static int	cbq_delete_class __P((struct cbq_delete_class *));
     81 static int	cbq_modify_class __P((struct cbq_modify_class *));
     82 static int 	cbq_class_create __P((cbq_state_t *, struct cbq_add_class *,
     83 				      struct rm_class *, struct rm_class *));
     84 static int	cbq_class_destroy __P((cbq_state_t *, struct rm_class *));
     85 static struct rm_class  *clh_to_clp __P((cbq_state_t *, u_long));
     86 static int	cbq_add_filter __P((struct cbq_add_filter *));
     87 static int	cbq_delete_filter __P((struct cbq_delete_filter *));
     88 
     89 static int	cbq_clear_hierarchy __P((struct cbq_interface *));
     90 static int	cbq_clear_interface __P((cbq_state_t *));
     91 static int	cbq_request __P((struct ifaltq *, int, void *));
     92 static int	cbq_set_enable __P((struct cbq_interface *, int));
     93 static int	cbq_ifattach __P((struct cbq_interface *));
     94 static int	cbq_ifdetach __P((struct cbq_interface *));
     95 static int	cbq_enqueue __P((struct ifaltq *, struct mbuf *,
     96 				 struct altq_pktattr *));
     97 static struct mbuf 	*cbq_dequeue __P((struct ifaltq *, int));
     98 static void	cbqrestart __P((struct ifaltq *));
     99 static void 	get_class_stats __P((class_stats_t *, struct rm_class *));
    100 static int 	cbq_getstats __P((struct cbq_getstats *));
    101 static void	cbq_purge(cbq_state_t *);
    102 
    103 static int
    104 cbq_add_class(acp)
    105 	struct cbq_add_class *acp;
    106 {
    107 	char		*ifacename;
    108 	struct rm_class	*borrow, *parent;
    109 	cbq_state_t	*cbqp;
    110 
    111 	ifacename = acp->cbq_iface.cbq_ifacename;
    112 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    113 		return (EBADF);
    114 
    115 	/* check parameters */
    116 	if (acp->cbq_class.priority >= CBQ_MAXPRI ||
    117 	    acp->cbq_class.maxq > CBQ_MAXQSIZE)
    118 		return (EINVAL);
    119 
    120 	/* Get pointers to parent and borrow classes.  */
    121 	parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
    122 	borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
    123 
    124 	/*
    125 	 * A class must borrow from it's parent or it can not
    126 	 * borrow at all.  Hence, borrow can be null.
    127 	 */
    128 	if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
    129 		printf("cbq_add_class: no parent class!\n");
    130 		return (EINVAL);
    131 	}
    132 
    133 	if ((borrow != parent)  && (borrow != NULL)) {
    134 		printf("cbq_add_class: borrow class != parent\n");
    135 		return (EINVAL);
    136 	}
    137 
    138 	return cbq_class_create(cbqp, acp, parent, borrow);
    139 }
    140 
    141 static int
    142 cbq_delete_class(dcp)
    143 	struct cbq_delete_class *dcp;
    144 {
    145 	char		*ifacename;
    146 	struct rm_class	*cl;
    147 	cbq_state_t	*cbqp;
    148 
    149 	ifacename = dcp->cbq_iface.cbq_ifacename;
    150 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    151 		return (EBADF);
    152 
    153 	if ((cl = clh_to_clp(cbqp, dcp->cbq_class_handle)) == NULL)
    154 		return (EINVAL);
    155 
    156 	/* if we are a parent class, then return an error. */
    157 	if (is_a_parent_class(cl))
    158 		return (EINVAL);
    159 
    160 	/* if a filter has a reference to this class delete the filter */
    161 	acc_discard_filters(&cbqp->cbq_classifier, cl, 0);
    162 
    163 	return cbq_class_destroy(cbqp, cl);
    164 }
    165 
    166 static int
    167 cbq_modify_class(acp)
    168 	struct cbq_modify_class *acp;
    169 {
    170 	char		*ifacename;
    171 	struct rm_class	*cl;
    172 	cbq_state_t	*cbqp;
    173 
    174 	ifacename = acp->cbq_iface.cbq_ifacename;
    175 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    176 		return (EBADF);
    177 
    178 	/* Get pointer to this class */
    179 	if ((cl = clh_to_clp(cbqp, acp->cbq_class_handle)) == NULL)
    180 		return (EINVAL);
    181 
    182 	if (rmc_modclass(cl, acp->cbq_class.nano_sec_per_byte,
    183 			 acp->cbq_class.maxq, acp->cbq_class.maxidle,
    184 			 acp->cbq_class.minidle, acp->cbq_class.offtime,
    185 			 acp->cbq_class.pktsize) < 0)
    186 		return (EINVAL);
    187 	return (0);
    188 }
    189 
    190 /*
    191  * struct rm_class *
    192  * cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
    193  *		u_long handle, struct rm_class *parent,
    194  *		struct rm_class *borrow)
    195  *
    196  * This function create a new traffic class in the CBQ class hierarchy of
    197  * given parameters.  The class that created is either the root, default,
    198  * or a new dynamic class.  If CBQ is not initilaized, the root class will
    199  * be created.
    200  */
    201 static int
    202 cbq_class_create(cbqp, acp, parent, borrow)
    203 	cbq_state_t *cbqp;
    204 	struct cbq_add_class *acp;
    205 	struct rm_class *parent, *borrow;
    206 {
    207 	struct rm_class	*cl;
    208 	cbq_class_spec_t *spec = &acp->cbq_class;
    209 	u_long		chandle;
    210 	int		i;
    211 
    212 	/*
    213 	 * allocate class handle
    214 	 */
    215 	switch (spec->flags & CBQCLF_CLASSMASK) {
    216 	case CBQCLF_ROOTCLASS:
    217 		if (parent != NULL)
    218 			return (EINVAL);
    219 		if (cbqp->ifnp.root_)
    220 			return (EINVAL);
    221 		chandle = ROOT_CLASS_HANDLE;
    222 		break;
    223 	case CBQCLF_DEFCLASS:
    224 		if (cbqp->ifnp.default_)
    225 			return (EINVAL);
    226 		chandle = DEFAULT_CLASS_HANDLE;
    227 		break;
    228 	case CBQCLF_CTLCLASS:
    229 		if (cbqp->ifnp.ctl_)
    230 			return (EINVAL);
    231 		chandle = CTL_CLASS_HANDLE;
    232 		break;
    233 	case 0:
    234 		/* find a free class slot */
    235 		for (i = 0; i < CBQ_MAX_CLASSES; i++)
    236 			if (cbqp->cbq_class_tbl[i] == NULL)
    237 				break;
    238 		if (i == CBQ_MAX_CLASSES)
    239 			return (ENOSPC);
    240 		chandle = (u_long)i;
    241 		break;
    242 	default:
    243 		/* more than two flags bits set */
    244 		return (EINVAL);
    245 	}
    246 
    247 	/*
    248 	 * create a class.  if this is a root class, initialize the
    249 	 * interface.
    250 	 */
    251 	if (chandle == ROOT_CLASS_HANDLE) {
    252 		rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, spec->nano_sec_per_byte,
    253 			 cbqrestart, spec->maxq, RM_MAXQUEUED,
    254 			 spec->maxidle, spec->minidle, spec->offtime,
    255 			 spec->flags);
    256 		cl = cbqp->ifnp.root_;
    257 	} else {
    258 		cl = rmc_newclass(spec->priority,
    259 				  &cbqp->ifnp, spec->nano_sec_per_byte,
    260 				  rmc_delay_action, spec->maxq, parent, borrow,
    261 				  spec->maxidle, spec->minidle, spec->offtime,
    262 				  spec->pktsize, spec->flags);
    263 	}
    264 	if (cl == NULL)
    265 		return (ENOMEM);
    266 
    267 	/* return handle to user space. */
    268 	acp->cbq_class_handle = chandle;
    269 
    270 	cl->stats_.handle = chandle;
    271 	cl->stats_.depth = cl->depth_;
    272 
    273 	/* save the allocated class */
    274 	switch (chandle) {
    275 	case NULL_CLASS_HANDLE:
    276 	case ROOT_CLASS_HANDLE:
    277 		break;
    278 	case DEFAULT_CLASS_HANDLE:
    279 		cbqp->ifnp.default_ = cl;
    280 		break;
    281 	case CTL_CLASS_HANDLE:
    282 		cbqp->ifnp.ctl_ = cl;
    283 		break;
    284 	default:
    285 		cbqp->cbq_class_tbl[chandle] = cl;
    286 		break;
    287 	}
    288 	return (0);
    289 }
    290 
    291 /*
    292  * int
    293  * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
    294  *	function destroys a given traffic class.  Before destorying
    295  *	the class, all traffic for that class is released.
    296  */
    297 static int
    298 cbq_class_destroy(cbqp, cl)
    299 	cbq_state_t *cbqp;
    300 	struct rm_class *cl;
    301 {
    302 	u_long	chandle;
    303 
    304 	chandle = cl->stats_.handle;
    305 
    306 	/* delete the class */
    307 	rmc_delete_class(&cbqp->ifnp, cl);
    308 
    309 	/*
    310 	 * free the class handle
    311 	 */
    312 	switch (chandle) {
    313 	case ROOT_CLASS_HANDLE:
    314 		cbqp->ifnp.root_ = NULL;
    315 		break;
    316 	case DEFAULT_CLASS_HANDLE:
    317 		cbqp->ifnp.default_ = NULL;
    318 		break;
    319 	case CTL_CLASS_HANDLE:
    320 		cbqp->ifnp.ctl_ = NULL;
    321 		break;
    322 	case NULL_CLASS_HANDLE:
    323 		break;
    324 	default:
    325 		if (chandle >= CBQ_MAX_CLASSES)
    326 			break;
    327 		cbqp->cbq_class_tbl[chandle] = NULL;
    328 	}
    329 
    330 	return (0);
    331 }
    332 
    333 /* convert class handle to class pointer */
    334 static struct rm_class *
    335 clh_to_clp(cbqp, chandle)
    336 	cbq_state_t *cbqp;
    337 	u_long chandle;
    338 {
    339 	switch (chandle) {
    340 	case NULL_CLASS_HANDLE:
    341 		return (NULL);
    342 	case ROOT_CLASS_HANDLE:
    343 		return (cbqp->ifnp.root_);
    344 	case DEFAULT_CLASS_HANDLE:
    345 		return (cbqp->ifnp.default_);
    346 	case CTL_CLASS_HANDLE:
    347 		return (cbqp->ifnp.ctl_);
    348 	}
    349 
    350 	if (chandle >= CBQ_MAX_CLASSES)
    351 		return (NULL);
    352 
    353 	return (cbqp->cbq_class_tbl[chandle]);
    354 }
    355 
    356 static int
    357 cbq_add_filter(afp)
    358 	struct cbq_add_filter *afp;
    359 {
    360 	char		*ifacename;
    361 	cbq_state_t	*cbqp;
    362 	struct rm_class	*cl;
    363 
    364 	ifacename = afp->cbq_iface.cbq_ifacename;
    365 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    366 		return (EBADF);
    367 
    368 	/* Get the pointer to class. */
    369 	if ((cl = clh_to_clp(cbqp, afp->cbq_class_handle)) == NULL)
    370 		return (EINVAL);
    371 
    372 	return acc_add_filter(&cbqp->cbq_classifier, &afp->cbq_filter,
    373 			      cl, &afp->cbq_filter_handle);
    374 }
    375 
    376 static int
    377 cbq_delete_filter(dfp)
    378 	struct cbq_delete_filter *dfp;
    379 {
    380 	char		*ifacename;
    381 	cbq_state_t	*cbqp;
    382 
    383 	ifacename = dfp->cbq_iface.cbq_ifacename;
    384 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    385 		return (EBADF);
    386 
    387 	return acc_delete_filter(&cbqp->cbq_classifier,
    388 				 dfp->cbq_filter_handle);
    389 }
    390 
    391 /*
    392  * cbq_clear_hierarchy deletes all classes and their filters on the
    393  * given interface.
    394  */
    395 static int
    396 cbq_clear_hierarchy(ifacep)
    397 	struct cbq_interface *ifacep;
    398 {
    399 	char		*ifacename;
    400 	cbq_state_t	*cbqp;
    401 
    402 	ifacename = ifacep->cbq_ifacename;
    403 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    404 		return (EBADF);
    405 
    406 	return cbq_clear_interface(cbqp);
    407 }
    408 
    409 static int
    410 cbq_clear_interface(cbqp)
    411 	cbq_state_t *cbqp;
    412 {
    413 	int		again, i;
    414 	struct rm_class	*cl;
    415 
    416 	/* free the filters for this interface */
    417 	acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
    418 
    419 	/* clear out the classes now */
    420 	do {
    421 		again = 0;
    422 		for (i = 0; i < CBQ_MAX_CLASSES; i++) {
    423 			if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
    424 				if (is_a_parent_class(cl))
    425 					again++;
    426 				else {
    427 					cbq_class_destroy(cbqp, cl);
    428 					cbqp->cbq_class_tbl[i] = NULL;
    429 				}
    430 			}
    431 		}
    432 		if (cbqp->ifnp.ctl_ != NULL &&
    433 		    !is_a_parent_class(cbqp->ifnp.ctl_)) {
    434 			cbq_class_destroy(cbqp, cbqp->ifnp.ctl_);
    435 			cbqp->ifnp.ctl_ = NULL;
    436 		}
    437 		if (cbqp->ifnp.default_ != NULL &&
    438 		    !is_a_parent_class(cbqp->ifnp.default_)) {
    439 			cbq_class_destroy(cbqp, cbqp->ifnp.default_);
    440 			cbqp->ifnp.default_ = NULL;
    441 		}
    442 		if (cbqp->ifnp.root_ != NULL &&
    443 		    !is_a_parent_class(cbqp->ifnp.root_)) {
    444 			cbq_class_destroy(cbqp, cbqp->ifnp.root_);
    445 			cbqp->ifnp.root_ = NULL;
    446 		}
    447 	} while (again);
    448 
    449 	return (0);
    450 }
    451 
    452 static int
    453 cbq_request(ifq, req, arg)
    454 	struct ifaltq *ifq;
    455 	int req;
    456 	void *arg;
    457 {
    458 	cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
    459 
    460 	switch (req) {
    461 	case ALTRQ_PURGE:
    462 		cbq_purge(cbqp);
    463 		break;
    464 	}
    465 	return (0);
    466 }
    467 
    468 /*
    469  * static int
    470  * cbq_set_enable(struct cbq_enable *ep) - this function processed the
    471  *	ioctl request to enable class based queueing.  It searches the list
    472  *	of interfaces for the specified interface and then enables CBQ on
    473  *	that interface.
    474  *
    475  *	Returns:	0, for no error.
    476  *			EBADF, for specified inteface not found.
    477  */
    478 
    479 static int
    480 cbq_set_enable(ep, enable)
    481 	struct cbq_interface *ep;
    482 	int enable;
    483 {
    484 	int 	error = 0;
    485 	cbq_state_t	*cbqp;
    486 	char 	*ifacename;
    487 
    488 	ifacename = ep->cbq_ifacename;
    489 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    490 		return (EBADF);
    491 
    492 	switch (enable) {
    493 	case ENABLE:
    494 		if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL ||
    495 		    cbqp->ifnp.ctl_ == NULL) {
    496 			if (cbqp->ifnp.root_ == NULL)
    497 				printf("No Root Class for %s\n", ifacename);
    498 			if (cbqp->ifnp.default_ == NULL)
    499 				printf("No Default Class for %s\n", ifacename);
    500 			if (cbqp->ifnp.ctl_ == NULL)
    501 				printf("No Control Class for %s\n", ifacename);
    502 			error = EINVAL;
    503 		} else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
    504 			cbqp->cbq_qlen = 0;
    505 		}
    506 		break;
    507 
    508 	case DISABLE:
    509 		error = altq_disable(cbqp->ifnp.ifq_);
    510 		break;
    511 	}
    512 	return (error);
    513 }
    514 
    515 /* copy the stats info in rm_class to class_states_t */
    516 static void
    517 get_class_stats(statsp, cl)
    518 	class_stats_t	*statsp;
    519 	struct rm_class	*cl;
    520 {
    521 	statsp->xmit_cnt 	= cl->stats_.xmit_cnt;
    522 	statsp->drop_cnt 	= cl->stats_.drop_cnt;
    523 	statsp->over		= cl->stats_.over;
    524 	statsp->borrows 	= cl->stats_.borrows;
    525 	statsp->overactions 	= cl->stats_.overactions;
    526 	statsp->delays 		= cl->stats_.delays;
    527 
    528 	statsp->depth 		= cl->depth_;
    529 	statsp->priority	= cl->pri_;
    530 	statsp->maxidle		= cl->maxidle_;
    531 	statsp->minidle		= cl->minidle_;
    532 	statsp->offtime		= cl->offtime_;
    533 	statsp->qmax		= qlimit(cl->q_);
    534 	statsp->ns_per_byte	= cl->ns_per_byte_;
    535 	statsp->wrr_allot	= cl->w_allotment_;
    536 	statsp->qcnt		= qlen(cl->q_);
    537 	statsp->avgidle		= cl->avgidle_;
    538 
    539 	statsp->qtype		= qtype(cl->q_);
    540 #ifdef ALTQ_RED
    541 	if (q_is_red(cl->q_))
    542 		red_getstats(cl->red_, &statsp->red[0]);
    543 #endif
    544 #ifdef ALTQ_RIO
    545 	if (q_is_rio(cl->q_))
    546 		rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
    547 #endif
    548 }
    549 
    550 static int
    551 cbq_getstats(gsp)
    552 	struct cbq_getstats *gsp;
    553 {
    554 	char		*ifacename;
    555 	int		chandle, n, nclasses;
    556 	cbq_state_t	*cbqp;
    557 	struct rm_class	*cl;
    558 	class_stats_t	stats, *usp;
    559 	int error = 0;
    560 
    561 	ifacename = gsp->iface.cbq_ifacename;
    562 	nclasses = gsp->nclasses;
    563 	usp = gsp->stats;
    564 
    565 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    566 		return (EBADF);
    567 	if (nclasses <= 0)
    568 		return (EINVAL);
    569 
    570 	for (n = 0, chandle = 0; n < nclasses && chandle < CBQ_MAX_CLASSES;
    571 	     n++) {
    572 		switch(n) {
    573 		case 0:
    574 			cl = cbqp->ifnp.root_;
    575 			stats.handle = ROOT_CLASS_HANDLE;
    576 			break;
    577 		case 1:
    578 			cl = cbqp->ifnp.default_;
    579 			stats.handle = DEFAULT_CLASS_HANDLE;
    580 			break;
    581 		case 2:
    582 			cl = cbqp->ifnp.ctl_;
    583 			stats.handle = CTL_CLASS_HANDLE;
    584 			break;
    585 		default:
    586 			while ((cl = cbqp->cbq_class_tbl[chandle]) == NULL)
    587 				if (++chandle >= CBQ_MAX_CLASSES)
    588 					goto out;
    589 			stats.handle = chandle++;
    590 			break;
    591 		}
    592 
    593 		get_class_stats(&stats, cl);
    594 
    595 		if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
    596 				     sizeof(stats))) != 0)
    597 			return (error);
    598 	}
    599 
    600  out:
    601 	gsp->nclasses = n;
    602 	return (error);
    603 }
    604 
    605 static int
    606 cbq_ifattach(ifacep)
    607 	struct cbq_interface *ifacep;
    608 {
    609 	int		error = 0;
    610 	char		*ifacename;
    611 	cbq_state_t	*new_cbqp;
    612 	struct ifnet 	*ifp;
    613 
    614 	ifacename = ifacep->cbq_ifacename;
    615 	if ((ifp = ifunit(ifacename)) == NULL)
    616 		return (ENXIO);
    617 	if (!ALTQ_IS_READY(&ifp->if_snd))
    618 		return (ENXIO);
    619 
    620 	/* allocate and initialize cbq_state_t */
    621 	new_cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    622 	if (new_cbqp == NULL)
    623 		return (ENOMEM);
    624  	CALLOUT_INIT(&new_cbqp->cbq_callout);
    625 	new_cbqp->cbq_class_tbl = malloc(sizeof(struct rm_class *) *
    626 	    CBQ_MAX_CLASSES, M_DEVBUF, M_WAITOK|M_ZERO);
    627 	if (new_cbqp->cbq_class_tbl == NULL) {
    628 		free(new_cbqp, M_DEVBUF);
    629 		return (ENOMEM);
    630 	}
    631 	new_cbqp->cbq_qlen = 0;
    632 	new_cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
    633 
    634 	/*
    635 	 * set CBQ to this ifnet structure.
    636 	 */
    637 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, new_cbqp,
    638 			    cbq_enqueue, cbq_dequeue, cbq_request,
    639 			    &new_cbqp->cbq_classifier, acc_classify);
    640 	if (error) {
    641 		free(new_cbqp->cbq_class_tbl, M_DEVBUF);
    642 		free(new_cbqp, M_DEVBUF);
    643 		return (error);
    644 	}
    645 
    646 	/* prepend to the list of cbq_state_t's. */
    647 	new_cbqp->cbq_next = cbq_list;
    648 	cbq_list = new_cbqp;
    649 
    650 	return (0);
    651 }
    652 
    653 static int
    654 cbq_ifdetach(ifacep)
    655 	struct cbq_interface *ifacep;
    656 {
    657 	char		*ifacename;
    658 	cbq_state_t 	*cbqp;
    659 
    660 	ifacename = ifacep->cbq_ifacename;
    661 	if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
    662 		return (EBADF);
    663 
    664 	(void)cbq_set_enable(ifacep, DISABLE);
    665 
    666 	cbq_clear_interface(cbqp);
    667 
    668 	if (cbqp->ifnp.ctl_)
    669 		cbq_class_destroy(cbqp, cbqp->ifnp.ctl_);
    670 	if (cbqp->ifnp.default_)
    671 		cbq_class_destroy(cbqp, cbqp->ifnp.default_);
    672 	if (cbqp->ifnp.root_)
    673 		cbq_class_destroy(cbqp, cbqp->ifnp.root_);
    674 
    675 	/* remove CBQ from the ifnet structure. */
    676 	(void)altq_detach(cbqp->ifnp.ifq_);
    677 
    678 	/* remove from the list of cbq_state_t's. */
    679 	if (cbq_list == cbqp)
    680 		cbq_list = cbqp->cbq_next;
    681 	else {
    682 		cbq_state_t *cp;
    683 
    684 		for (cp = cbq_list; cp != NULL; cp = cp->cbq_next)
    685 			if (cp->cbq_next == cbqp) {
    686 				cp->cbq_next = cbqp->cbq_next;
    687 				break;
    688 			}
    689 		ASSERT(cp != NULL);
    690 	}
    691 
    692 	/* deallocate cbq_state_t */
    693 	free(cbqp->cbq_class_tbl, M_DEVBUF);
    694 	free(cbqp, M_DEVBUF);
    695 
    696 	return (0);
    697 }
    698 
    699 /*
    700  * int
    701  * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr)
    702  *		- Queue data packets.
    703  *
    704  *	cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
    705  *	layer (e.g. ether_output).  cbq_enqueue queues the given packet
    706  *	to the cbq, then invokes the driver's start routine.
    707  *
    708  *	Assumptions:	called in splnet
    709  *	Returns:	0 if the queueing is successful.
    710  *			ENOBUFS if a packet dropping occurred as a result of
    711  *			the queueing.
    712  */
    713 
    714 static int
    715 cbq_enqueue(ifq, m, pktattr)
    716 	struct ifaltq *ifq;
    717 	struct mbuf *m;
    718 	struct altq_pktattr *pktattr;
    719 {
    720 	cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
    721 	struct rm_class *cl;
    722 	int len;
    723 
    724 	/* grab class set by classifier */
    725 	if (pktattr == NULL || (cl = pktattr->pattr_class) == NULL)
    726 		cl = cbqp->ifnp.default_;
    727 	cl->pktattr_ = pktattr;  /* save proto hdr used by ECN */
    728 
    729 	len = m_pktlen(m);
    730 	if (rmc_queue_packet(cl, m) != 0) {
    731 		/* drop occurred.  some mbuf was freed in rmc_queue_packet. */
    732 		PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
    733 		return (ENOBUFS);
    734 	}
    735 
    736 	/* successfully queued. */
    737 	++cbqp->cbq_qlen;
    738 	IFQ_INC_LEN(ifq);
    739 	return (0);
    740 }
    741 
    742 static struct mbuf *
    743 cbq_dequeue(ifq, op)
    744 	struct ifaltq *ifq;
    745 	int op;
    746 {
    747 	cbq_state_t 	*cbqp = (cbq_state_t *)ifq->altq_disc;
    748 	struct mbuf 	*m;
    749 
    750 	m = rmc_dequeue_next(&cbqp->ifnp, op);
    751 
    752 	if (m && op == ALTDQ_REMOVE) {
    753 		--cbqp->cbq_qlen;  /* decrement # of packets in cbq */
    754 		IFQ_DEC_LEN(ifq);
    755 
    756 		/* Update the class. */
    757 		rmc_update_class_util(&cbqp->ifnp);
    758 	}
    759 	return (m);
    760 }
    761 
    762 /*
    763  * void
    764  * cbqrestart(queue_t *) - Restart sending of data.
    765  * called from rmc_restart in splnet via timeout after waking up
    766  * a suspended class.
    767  *	Returns:	NONE
    768  */
    769 
    770 static void
    771 cbqrestart(ifq)
    772 	struct ifaltq *ifq;
    773 {
    774 	cbq_state_t	*cbqp;
    775 	struct ifnet	*ifp;
    776 
    777 	if (!ALTQ_IS_ENABLED(ifq))
    778 		/* cbq must have been detached */
    779 		return;
    780 	if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
    781 		/* should not happen */
    782 		return;
    783 
    784 	ifp = ifq->altq_ifp;
    785 	if (ifp->if_start &&
    786 	    cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
    787 		(*ifp->if_start)(ifp);
    788 }
    789 
    790 static void cbq_purge(cbqp)
    791 	cbq_state_t *cbqp;
    792 {
    793 	struct rm_class	*cl;
    794 	int 		i;
    795 
    796 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
    797 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
    798 			rmc_dropall(cl);
    799 	if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
    800 		cbqp->ifnp.ifq_->ifq_len = 0;
    801 }
    802 
    803 /*
    804  * cbq device interface
    805  */
    806 
    807 altqdev_decl(cbq);
    808 
    809 int
    810 cbqopen(dev, flag, fmt, l)
    811 	dev_t dev;
    812 	int flag, fmt;
    813 	struct lwp *l;
    814 {
    815 	return (0);
    816 }
    817 
    818 int
    819 cbqclose(dev, flag, fmt, l)
    820 	dev_t dev;
    821 	int flag, fmt;
    822 	struct lwp *l;
    823 {
    824 	struct ifnet *ifp;
    825 	struct cbq_interface iface;
    826 	int err, error = 0;
    827 
    828 	while (cbq_list) {
    829 		ifp = cbq_list->ifnp.ifq_->altq_ifp;
    830 #if defined(__NetBSD__) || defined(__OpenBSD__)
    831 		sprintf(iface.cbq_ifacename, "%s", ifp->if_xname);
    832 #else
    833 		sprintf(iface.cbq_ifacename,
    834 			"%s%d", ifp->if_name, ifp->if_unit);
    835 #endif
    836 		err = cbq_ifdetach(&iface);
    837 		if (err != 0 && error == 0)
    838 			error = err;
    839 	}
    840 
    841 	return (error);
    842 }
    843 
    844 int
    845 cbqioctl(dev, cmd, addr, flag, l)
    846 	dev_t dev;
    847 	ioctlcmd_t cmd;
    848 	caddr_t addr;
    849 	int flag;
    850 	struct lwp *l;
    851 {
    852 	struct proc *p = l->l_proc;
    853 	int	error = 0;
    854 
    855 	/* check cmd for superuser only */
    856 	switch (cmd) {
    857 	case CBQ_GETSTATS:
    858 		/* currently only command that an ordinary user can call */
    859 		break;
    860 	default:
    861 #if (__FreeBSD_version > 400000)
    862 		error = suser(p);
    863 #else
    864 		error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    865 					  &p->p_acflag);
    866 #endif
    867 		if (error)
    868 			return (error);
    869 		break;
    870 	}
    871 
    872 	switch (cmd) {
    873 
    874 	case CBQ_ENABLE:
    875 		error = cbq_set_enable((struct cbq_interface *)addr, ENABLE);
    876 		break;
    877 
    878 	case CBQ_DISABLE:
    879 		error = cbq_set_enable((struct cbq_interface *)addr, DISABLE);
    880 		break;
    881 
    882 	case CBQ_ADD_FILTER:
    883 		error = cbq_add_filter((struct cbq_add_filter *)addr);
    884 		break;
    885 
    886 	case CBQ_DEL_FILTER:
    887 		error = cbq_delete_filter((struct cbq_delete_filter *)addr);
    888 		break;
    889 
    890 	case CBQ_ADD_CLASS:
    891 		error = cbq_add_class((struct cbq_add_class *)addr);
    892 		break;
    893 
    894 	case CBQ_DEL_CLASS:
    895 		error = cbq_delete_class((struct cbq_delete_class *)addr);
    896 		break;
    897 
    898 	case CBQ_MODIFY_CLASS:
    899 		error = cbq_modify_class((struct cbq_modify_class *)addr);
    900 		break;
    901 
    902 	case CBQ_CLEAR_HIERARCHY:
    903 		error = cbq_clear_hierarchy((struct cbq_interface *)addr);
    904 		break;
    905 
    906 	case CBQ_IF_ATTACH:
    907 		error = cbq_ifattach((struct cbq_interface *)addr);
    908 		break;
    909 
    910 	case CBQ_IF_DETACH:
    911 		error = cbq_ifdetach((struct cbq_interface *)addr);
    912 		break;
    913 
    914 	case CBQ_GETSTATS:
    915 		error = cbq_getstats((struct cbq_getstats *)addr);
    916 		break;
    917 
    918 	default:
    919 		error = EINVAL;
    920 		break;
    921 	}
    922 
    923 	return error;
    924 }
    925 
    926 #if 0
    927 /* for debug */
    928 static void cbq_class_dump(int);
    929 
    930 static void cbq_class_dump(i)
    931 	int i;
    932 {
    933 	struct rm_class *cl;
    934 	rm_class_stats_t *s;
    935 	struct _class_queue_ *q;
    936 
    937 	if (cbq_list == NULL) {
    938 		printf("cbq_class_dump: no cbq_state found\n");
    939 		return;
    940 	}
    941 	cl = cbq_list->cbq_class_tbl[i];
    942 
    943 	printf("class %d cl=%p\n", i, cl);
    944 	if (cl != NULL) {
    945 		s = &cl->stats_;
    946 		q = cl->q_;
    947 
    948 		printf("pri=%d, depth=%d, maxrate=%d, allotment=%d\n",
    949 		       cl->pri_, cl->depth_, cl->maxrate_, cl->allotment_);
    950 		printf("w_allotment=%d, bytes_alloc=%d, avgidle=%d, maxidle=%d\n",
    951 		       cl->w_allotment_, cl->bytes_alloc_, cl->avgidle_,
    952 		       cl->maxidle_);
    953 		printf("minidle=%d, offtime=%d, sleeping=%d, leaf=%d\n",
    954 		       cl->minidle_, cl->offtime_, cl->sleeping_, cl->leaf_);
    955 		printf("handle=%d, depth=%d, packets=%d, bytes=%d\n",
    956 		       s->handle, s->depth,
    957 		       (int)s->xmit_cnt.packets, (int)s->xmit_cnt.bytes);
    958 		printf("over=%d\n, borrows=%d, drops=%d, overactions=%d, delays=%d\n",
    959 		       s->over, s->borrows, (int)s->drop_cnt.packets,
    960 		       s->overactions, s->delays);
    961 		printf("tail=%p, head=%p, qlen=%d, qlim=%d, qthresh=%d,qtype=%d\n",
    962 		       q->tail_, q->head_, q->qlen_, q->qlim_,
    963 		       q->qthresh_, q->qtype_);
    964 	}
    965 }
    966 #endif /* 0 */
    967 
    968 #ifdef KLD_MODULE
    969 
    970 static struct altqsw cbq_sw =
    971 	{"cbq", cbqopen, cbqclose, cbqioctl};
    972 
    973 ALTQ_MODULE(altq_cbq, ALTQT_CBQ, &cbq_sw);
    974 
    975 #endif /* KLD_MODULE */
    976 
    977 #endif /* ALTQ_CBQ */
    978