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