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