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