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