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