xref: /freebsd/sys/net/altq/altq_cbq.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1da8ae05dSGleb Smirnoff /*-
2772e66a6SGleb Smirnoff  * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
3772e66a6SGleb Smirnoff  *
4772e66a6SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
5772e66a6SGleb Smirnoff  * modification, are permitted provided that the following conditions
6772e66a6SGleb Smirnoff  * are met:
7772e66a6SGleb Smirnoff  *
8772e66a6SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10772e66a6SGleb Smirnoff  *
11772e66a6SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
12772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
13772e66a6SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
14772e66a6SGleb Smirnoff  *
15772e66a6SGleb Smirnoff  * 3. All advertising materials mentioning features or use of this software
16772e66a6SGleb Smirnoff  *    must display the following acknowledgement:
17772e66a6SGleb Smirnoff  *      This product includes software developed by the SMCC Technology
18772e66a6SGleb Smirnoff  *      Development Group at Sun Microsystems, Inc.
19772e66a6SGleb Smirnoff  *
20772e66a6SGleb Smirnoff  * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
21772e66a6SGleb Smirnoff  *      promote products derived from this software without specific prior
22772e66a6SGleb Smirnoff  *      written permission.
23772e66a6SGleb Smirnoff  *
24772e66a6SGleb Smirnoff  * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
25772e66a6SGleb Smirnoff  * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is
26772e66a6SGleb Smirnoff  * provided "as is" without express or implied warranty of any kind.
27772e66a6SGleb Smirnoff  *
28772e66a6SGleb Smirnoff  * These notices must be retained in any copies of any part of this software.
29da8ae05dSGleb Smirnoff  *
30da8ae05dSGleb Smirnoff  * $KAME: altq_cbq.c,v 1.19 2003/09/17 14:23:25 kjc Exp $
31772e66a6SGleb Smirnoff  */
32772e66a6SGleb Smirnoff 
33772e66a6SGleb Smirnoff #include "opt_altq.h"
34772e66a6SGleb Smirnoff #include "opt_inet.h"
35772e66a6SGleb Smirnoff #include "opt_inet6.h"
36772e66a6SGleb Smirnoff #ifdef ALTQ_CBQ	/* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
37772e66a6SGleb Smirnoff 
38772e66a6SGleb Smirnoff #include <sys/param.h>
39772e66a6SGleb Smirnoff #include <sys/malloc.h>
40772e66a6SGleb Smirnoff #include <sys/mbuf.h>
41772e66a6SGleb Smirnoff #include <sys/socket.h>
42772e66a6SGleb Smirnoff #include <sys/systm.h>
43772e66a6SGleb Smirnoff #include <sys/proc.h>
44772e66a6SGleb Smirnoff #include <sys/errno.h>
45772e66a6SGleb Smirnoff #include <sys/time.h>
46772e66a6SGleb Smirnoff 
47772e66a6SGleb Smirnoff #include <net/if.h>
48772e66a6SGleb Smirnoff #include <net/if_var.h>
49*2c2b37adSJustin Hibbits #include <net/if_private.h>
50772e66a6SGleb Smirnoff #include <netinet/in.h>
51772e66a6SGleb Smirnoff 
52772e66a6SGleb Smirnoff #include <netpfil/pf/pf.h>
53772e66a6SGleb Smirnoff #include <netpfil/pf/pf_altq.h>
54772e66a6SGleb Smirnoff #include <netpfil/pf/pf_mtag.h>
55772e66a6SGleb Smirnoff #include <net/altq/altq.h>
56772e66a6SGleb Smirnoff #include <net/altq/altq_cbq.h>
57772e66a6SGleb Smirnoff 
58772e66a6SGleb Smirnoff /*
59772e66a6SGleb Smirnoff  * Forward Declarations.
60772e66a6SGleb Smirnoff  */
61772e66a6SGleb Smirnoff static int		 cbq_class_destroy(cbq_state_t *, struct rm_class *);
62772e66a6SGleb Smirnoff static struct rm_class  *clh_to_clp(cbq_state_t *, u_int32_t);
63772e66a6SGleb Smirnoff static int		 cbq_clear_interface(cbq_state_t *);
64772e66a6SGleb Smirnoff static int		 cbq_request(struct ifaltq *, int, void *);
65772e66a6SGleb Smirnoff static int		 cbq_enqueue(struct ifaltq *, struct mbuf *,
66772e66a6SGleb Smirnoff 			     struct altq_pktattr *);
67772e66a6SGleb Smirnoff static struct mbuf	*cbq_dequeue(struct ifaltq *, int);
68772e66a6SGleb Smirnoff static void		 cbqrestart(struct ifaltq *);
69772e66a6SGleb Smirnoff static void		 get_class_stats(class_stats_t *, struct rm_class *);
70772e66a6SGleb Smirnoff static void		 cbq_purge(cbq_state_t *);
71772e66a6SGleb Smirnoff 
72772e66a6SGleb Smirnoff /*
73772e66a6SGleb Smirnoff  * int
74772e66a6SGleb Smirnoff  * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
75772e66a6SGleb Smirnoff  *	function destroys a given traffic class.  Before destroying
76772e66a6SGleb Smirnoff  *	the class, all traffic for that class is released.
77772e66a6SGleb Smirnoff  */
78772e66a6SGleb Smirnoff static int
cbq_class_destroy(cbq_state_t * cbqp,struct rm_class * cl)79772e66a6SGleb Smirnoff cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl)
80772e66a6SGleb Smirnoff {
81772e66a6SGleb Smirnoff 	int	i;
82772e66a6SGleb Smirnoff 
83772e66a6SGleb Smirnoff 	/* delete the class */
84772e66a6SGleb Smirnoff 	rmc_delete_class(&cbqp->ifnp, cl);
85772e66a6SGleb Smirnoff 
86772e66a6SGleb Smirnoff 	/*
87772e66a6SGleb Smirnoff 	 * free the class handle
88772e66a6SGleb Smirnoff 	 */
89772e66a6SGleb Smirnoff 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
90772e66a6SGleb Smirnoff 		if (cbqp->cbq_class_tbl[i] == cl)
91772e66a6SGleb Smirnoff 			cbqp->cbq_class_tbl[i] = NULL;
92772e66a6SGleb Smirnoff 
93772e66a6SGleb Smirnoff 	if (cl == cbqp->ifnp.root_)
94772e66a6SGleb Smirnoff 		cbqp->ifnp.root_ = NULL;
95772e66a6SGleb Smirnoff 	if (cl == cbqp->ifnp.default_)
96772e66a6SGleb Smirnoff 		cbqp->ifnp.default_ = NULL;
97772e66a6SGleb Smirnoff 	return (0);
98772e66a6SGleb Smirnoff }
99772e66a6SGleb Smirnoff 
100772e66a6SGleb Smirnoff /* convert class handle to class pointer */
101772e66a6SGleb Smirnoff static struct rm_class *
clh_to_clp(cbq_state_t * cbqp,u_int32_t chandle)102772e66a6SGleb Smirnoff clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle)
103772e66a6SGleb Smirnoff {
104772e66a6SGleb Smirnoff 	int i;
105772e66a6SGleb Smirnoff 	struct rm_class *cl;
106772e66a6SGleb Smirnoff 
107772e66a6SGleb Smirnoff 	if (chandle == 0)
108772e66a6SGleb Smirnoff 		return (NULL);
109772e66a6SGleb Smirnoff 	/*
110772e66a6SGleb Smirnoff 	 * first, try optimistically the slot matching the lower bits of
111772e66a6SGleb Smirnoff 	 * the handle.  if it fails, do the linear table search.
112772e66a6SGleb Smirnoff 	 */
113772e66a6SGleb Smirnoff 	i = chandle % CBQ_MAX_CLASSES;
114772e66a6SGleb Smirnoff 	if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
115772e66a6SGleb Smirnoff 	    cl->stats_.handle == chandle)
116772e66a6SGleb Smirnoff 		return (cl);
117772e66a6SGleb Smirnoff 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
118772e66a6SGleb Smirnoff 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
119772e66a6SGleb Smirnoff 		    cl->stats_.handle == chandle)
120772e66a6SGleb Smirnoff 			return (cl);
121772e66a6SGleb Smirnoff 	return (NULL);
122772e66a6SGleb Smirnoff }
123772e66a6SGleb Smirnoff 
124772e66a6SGleb Smirnoff static int
cbq_clear_interface(cbq_state_t * cbqp)125772e66a6SGleb Smirnoff cbq_clear_interface(cbq_state_t *cbqp)
126772e66a6SGleb Smirnoff {
127772e66a6SGleb Smirnoff 	int		 again, i;
128772e66a6SGleb Smirnoff 	struct rm_class	*cl;
129772e66a6SGleb Smirnoff 
130772e66a6SGleb Smirnoff #ifdef ALTQ3_CLFIER_COMPAT
131772e66a6SGleb Smirnoff 	/* free the filters for this interface */
132772e66a6SGleb Smirnoff 	acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
133772e66a6SGleb Smirnoff #endif
134772e66a6SGleb Smirnoff 
135772e66a6SGleb Smirnoff 	/* clear out the classes now */
136772e66a6SGleb Smirnoff 	do {
137772e66a6SGleb Smirnoff 		again = 0;
138772e66a6SGleb Smirnoff 		for (i = 0; i < CBQ_MAX_CLASSES; i++) {
139772e66a6SGleb Smirnoff 			if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
140772e66a6SGleb Smirnoff 				if (is_a_parent_class(cl))
141772e66a6SGleb Smirnoff 					again++;
142772e66a6SGleb Smirnoff 				else {
143772e66a6SGleb Smirnoff 					cbq_class_destroy(cbqp, cl);
144772e66a6SGleb Smirnoff 					cbqp->cbq_class_tbl[i] = NULL;
145772e66a6SGleb Smirnoff 					if (cl == cbqp->ifnp.root_)
146772e66a6SGleb Smirnoff 						cbqp->ifnp.root_ = NULL;
147772e66a6SGleb Smirnoff 					if (cl == cbqp->ifnp.default_)
148772e66a6SGleb Smirnoff 						cbqp->ifnp.default_ = NULL;
149772e66a6SGleb Smirnoff 				}
150772e66a6SGleb Smirnoff 			}
151772e66a6SGleb Smirnoff 		}
152772e66a6SGleb Smirnoff 	} while (again);
153772e66a6SGleb Smirnoff 
154772e66a6SGleb Smirnoff 	return (0);
155772e66a6SGleb Smirnoff }
156772e66a6SGleb Smirnoff 
157772e66a6SGleb Smirnoff static int
cbq_request(struct ifaltq * ifq,int req,void * arg)158772e66a6SGleb Smirnoff cbq_request(struct ifaltq *ifq, int req, void *arg)
159772e66a6SGleb Smirnoff {
160772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
161772e66a6SGleb Smirnoff 
162772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
163772e66a6SGleb Smirnoff 
164772e66a6SGleb Smirnoff 	switch (req) {
165772e66a6SGleb Smirnoff 	case ALTRQ_PURGE:
166772e66a6SGleb Smirnoff 		cbq_purge(cbqp);
167772e66a6SGleb Smirnoff 		break;
168772e66a6SGleb Smirnoff 	}
169772e66a6SGleb Smirnoff 	return (0);
170772e66a6SGleb Smirnoff }
171772e66a6SGleb Smirnoff 
172772e66a6SGleb Smirnoff /* copy the stats info in rm_class to class_states_t */
173772e66a6SGleb Smirnoff static void
get_class_stats(class_stats_t * statsp,struct rm_class * cl)174772e66a6SGleb Smirnoff get_class_stats(class_stats_t *statsp, struct rm_class *cl)
175772e66a6SGleb Smirnoff {
176772e66a6SGleb Smirnoff 	statsp->xmit_cnt	= cl->stats_.xmit_cnt;
177772e66a6SGleb Smirnoff 	statsp->drop_cnt	= cl->stats_.drop_cnt;
178772e66a6SGleb Smirnoff 	statsp->over		= cl->stats_.over;
179772e66a6SGleb Smirnoff 	statsp->borrows		= cl->stats_.borrows;
180772e66a6SGleb Smirnoff 	statsp->overactions	= cl->stats_.overactions;
181772e66a6SGleb Smirnoff 	statsp->delays		= cl->stats_.delays;
182772e66a6SGleb Smirnoff 
183772e66a6SGleb Smirnoff 	statsp->depth		= cl->depth_;
184772e66a6SGleb Smirnoff 	statsp->priority	= cl->pri_;
185772e66a6SGleb Smirnoff 	statsp->maxidle		= cl->maxidle_;
186772e66a6SGleb Smirnoff 	statsp->minidle		= cl->minidle_;
187772e66a6SGleb Smirnoff 	statsp->offtime		= cl->offtime_;
188772e66a6SGleb Smirnoff 	statsp->qmax		= qlimit(cl->q_);
189772e66a6SGleb Smirnoff 	statsp->ns_per_byte	= cl->ns_per_byte_;
190772e66a6SGleb Smirnoff 	statsp->wrr_allot	= cl->w_allotment_;
191772e66a6SGleb Smirnoff 	statsp->qcnt		= qlen(cl->q_);
192772e66a6SGleb Smirnoff 	statsp->avgidle		= cl->avgidle_;
193772e66a6SGleb Smirnoff 
194772e66a6SGleb Smirnoff 	statsp->qtype		= qtype(cl->q_);
195772e66a6SGleb Smirnoff #ifdef ALTQ_RED
196772e66a6SGleb Smirnoff 	if (q_is_red(cl->q_))
197772e66a6SGleb Smirnoff 		red_getstats(cl->red_, &statsp->red[0]);
198772e66a6SGleb Smirnoff #endif
199772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
200772e66a6SGleb Smirnoff 	if (q_is_rio(cl->q_))
201772e66a6SGleb Smirnoff 		rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
202772e66a6SGleb Smirnoff #endif
2030a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
2040a70aaf8SLuiz Otavio O Souza 	if (q_is_codel(cl->q_))
2050a70aaf8SLuiz Otavio O Souza 		codel_getstats(cl->codel_, &statsp->codel);
2060a70aaf8SLuiz Otavio O Souza #endif
207772e66a6SGleb Smirnoff }
208772e66a6SGleb Smirnoff 
209772e66a6SGleb Smirnoff int
cbq_pfattach(struct pf_altq * a)210772e66a6SGleb Smirnoff cbq_pfattach(struct pf_altq *a)
211772e66a6SGleb Smirnoff {
212772e66a6SGleb Smirnoff 	struct ifnet	*ifp;
213772e66a6SGleb Smirnoff 	int		 s, error;
214772e66a6SGleb Smirnoff 
215772e66a6SGleb Smirnoff 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
216772e66a6SGleb Smirnoff 		return (EINVAL);
217772e66a6SGleb Smirnoff 	s = splnet();
218772e66a6SGleb Smirnoff 	error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc,
21927b2aa49SKristof Provost 	    cbq_enqueue, cbq_dequeue, cbq_request);
220772e66a6SGleb Smirnoff 	splx(s);
221772e66a6SGleb Smirnoff 	return (error);
222772e66a6SGleb Smirnoff }
223772e66a6SGleb Smirnoff 
224772e66a6SGleb Smirnoff int
cbq_add_altq(struct ifnet * ifp,struct pf_altq * a)2258f2ac656SPatrick Kelsey cbq_add_altq(struct ifnet *ifp, struct pf_altq *a)
226772e66a6SGleb Smirnoff {
227772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp;
228772e66a6SGleb Smirnoff 
2298f2ac656SPatrick Kelsey 	if (ifp == NULL)
230772e66a6SGleb Smirnoff 		return (EINVAL);
231772e66a6SGleb Smirnoff 	if (!ALTQ_IS_READY(&ifp->if_snd))
232772e66a6SGleb Smirnoff 		return (ENODEV);
233772e66a6SGleb Smirnoff 
234772e66a6SGleb Smirnoff 	/* allocate and initialize cbq_state_t */
235772e66a6SGleb Smirnoff 	cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_NOWAIT | M_ZERO);
236772e66a6SGleb Smirnoff 	if (cbqp == NULL)
237772e66a6SGleb Smirnoff 		return (ENOMEM);
238772e66a6SGleb Smirnoff 	CALLOUT_INIT(&cbqp->cbq_callout);
239772e66a6SGleb Smirnoff 	cbqp->cbq_qlen = 0;
240772e66a6SGleb Smirnoff 	cbqp->ifnp.ifq_ = &ifp->if_snd;	    /* keep the ifq */
241772e66a6SGleb Smirnoff 
242772e66a6SGleb Smirnoff 	/* keep the state in pf_altq */
243772e66a6SGleb Smirnoff 	a->altq_disc = cbqp;
244772e66a6SGleb Smirnoff 
245772e66a6SGleb Smirnoff 	return (0);
246772e66a6SGleb Smirnoff }
247772e66a6SGleb Smirnoff 
248772e66a6SGleb Smirnoff int
cbq_remove_altq(struct pf_altq * a)249772e66a6SGleb Smirnoff cbq_remove_altq(struct pf_altq *a)
250772e66a6SGleb Smirnoff {
251772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp;
252772e66a6SGleb Smirnoff 
253772e66a6SGleb Smirnoff 	if ((cbqp = a->altq_disc) == NULL)
254772e66a6SGleb Smirnoff 		return (EINVAL);
255772e66a6SGleb Smirnoff 	a->altq_disc = NULL;
256772e66a6SGleb Smirnoff 
257772e66a6SGleb Smirnoff 	cbq_clear_interface(cbqp);
258772e66a6SGleb Smirnoff 
259772e66a6SGleb Smirnoff 	if (cbqp->ifnp.default_)
260772e66a6SGleb Smirnoff 		cbq_class_destroy(cbqp, cbqp->ifnp.default_);
261772e66a6SGleb Smirnoff 	if (cbqp->ifnp.root_)
262772e66a6SGleb Smirnoff 		cbq_class_destroy(cbqp, cbqp->ifnp.root_);
263772e66a6SGleb Smirnoff 
264772e66a6SGleb Smirnoff 	/* deallocate cbq_state_t */
265772e66a6SGleb Smirnoff 	free(cbqp, M_DEVBUF);
266772e66a6SGleb Smirnoff 
267772e66a6SGleb Smirnoff 	return (0);
268772e66a6SGleb Smirnoff }
269772e66a6SGleb Smirnoff 
270772e66a6SGleb Smirnoff int
cbq_add_queue(struct pf_altq * a)271772e66a6SGleb Smirnoff cbq_add_queue(struct pf_altq *a)
272772e66a6SGleb Smirnoff {
273772e66a6SGleb Smirnoff 	struct rm_class	*borrow, *parent;
274772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp;
275772e66a6SGleb Smirnoff 	struct rm_class	*cl;
276772e66a6SGleb Smirnoff 	struct cbq_opts	*opts;
277772e66a6SGleb Smirnoff 	int		i;
278772e66a6SGleb Smirnoff 
279772e66a6SGleb Smirnoff 	if ((cbqp = a->altq_disc) == NULL)
280772e66a6SGleb Smirnoff 		return (EINVAL);
281772e66a6SGleb Smirnoff 	if (a->qid == 0)
282772e66a6SGleb Smirnoff 		return (EINVAL);
283772e66a6SGleb Smirnoff 
284772e66a6SGleb Smirnoff 	/*
285772e66a6SGleb Smirnoff 	 * find a free slot in the class table.  if the slot matching
286772e66a6SGleb Smirnoff 	 * the lower bits of qid is free, use this slot.  otherwise,
287772e66a6SGleb Smirnoff 	 * use the first free slot.
288772e66a6SGleb Smirnoff 	 */
289772e66a6SGleb Smirnoff 	i = a->qid % CBQ_MAX_CLASSES;
290772e66a6SGleb Smirnoff 	if (cbqp->cbq_class_tbl[i] != NULL) {
291772e66a6SGleb Smirnoff 		for (i = 0; i < CBQ_MAX_CLASSES; i++)
292772e66a6SGleb Smirnoff 			if (cbqp->cbq_class_tbl[i] == NULL)
293772e66a6SGleb Smirnoff 				break;
294772e66a6SGleb Smirnoff 		if (i == CBQ_MAX_CLASSES)
295772e66a6SGleb Smirnoff 			return (EINVAL);
296772e66a6SGleb Smirnoff 	}
297772e66a6SGleb Smirnoff 
298772e66a6SGleb Smirnoff 	opts = &a->pq_u.cbq_opts;
299772e66a6SGleb Smirnoff 	/* check parameters */
300772e66a6SGleb Smirnoff 	if (a->priority >= CBQ_MAXPRI)
301772e66a6SGleb Smirnoff 		return (EINVAL);
302772e66a6SGleb Smirnoff 
303772e66a6SGleb Smirnoff 	/* Get pointers to parent and borrow classes.  */
304772e66a6SGleb Smirnoff 	parent = clh_to_clp(cbqp, a->parent_qid);
305772e66a6SGleb Smirnoff 	if (opts->flags & CBQCLF_BORROW)
306772e66a6SGleb Smirnoff 		borrow = parent;
307772e66a6SGleb Smirnoff 	else
308772e66a6SGleb Smirnoff 		borrow = NULL;
309772e66a6SGleb Smirnoff 
310772e66a6SGleb Smirnoff 	/*
311772e66a6SGleb Smirnoff 	 * A class must borrow from it's parent or it can not
312772e66a6SGleb Smirnoff 	 * borrow at all.  Hence, borrow can be null.
313772e66a6SGleb Smirnoff 	 */
314772e66a6SGleb Smirnoff 	if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
315772e66a6SGleb Smirnoff 		printf("cbq_add_queue: no parent class!\n");
316772e66a6SGleb Smirnoff 		return (EINVAL);
317772e66a6SGleb Smirnoff 	}
318772e66a6SGleb Smirnoff 
319772e66a6SGleb Smirnoff 	if ((borrow != parent)  && (borrow != NULL)) {
320772e66a6SGleb Smirnoff 		printf("cbq_add_class: borrow class != parent\n");
321772e66a6SGleb Smirnoff 		return (EINVAL);
322772e66a6SGleb Smirnoff 	}
323772e66a6SGleb Smirnoff 
324772e66a6SGleb Smirnoff 	/*
325772e66a6SGleb Smirnoff 	 * check parameters
326772e66a6SGleb Smirnoff 	 */
327772e66a6SGleb Smirnoff 	switch (opts->flags & CBQCLF_CLASSMASK) {
328772e66a6SGleb Smirnoff 	case CBQCLF_ROOTCLASS:
329772e66a6SGleb Smirnoff 		if (parent != NULL)
330772e66a6SGleb Smirnoff 			return (EINVAL);
331772e66a6SGleb Smirnoff 		if (cbqp->ifnp.root_)
332772e66a6SGleb Smirnoff 			return (EINVAL);
333772e66a6SGleb Smirnoff 		break;
334772e66a6SGleb Smirnoff 	case CBQCLF_DEFCLASS:
335772e66a6SGleb Smirnoff 		if (cbqp->ifnp.default_)
336772e66a6SGleb Smirnoff 			return (EINVAL);
337772e66a6SGleb Smirnoff 		break;
338772e66a6SGleb Smirnoff 	case 0:
339772e66a6SGleb Smirnoff 		if (a->qid == 0)
340772e66a6SGleb Smirnoff 			return (EINVAL);
341772e66a6SGleb Smirnoff 		break;
342772e66a6SGleb Smirnoff 	default:
343772e66a6SGleb Smirnoff 		/* more than two flags bits set */
344772e66a6SGleb Smirnoff 		return (EINVAL);
345772e66a6SGleb Smirnoff 	}
346772e66a6SGleb Smirnoff 
347772e66a6SGleb Smirnoff 	/*
348772e66a6SGleb Smirnoff 	 * create a class.  if this is a root class, initialize the
349772e66a6SGleb Smirnoff 	 * interface.
350772e66a6SGleb Smirnoff 	 */
351772e66a6SGleb Smirnoff 	if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) {
352772e66a6SGleb Smirnoff 		rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, opts->ns_per_byte,
353772e66a6SGleb Smirnoff 		    cbqrestart, a->qlimit, RM_MAXQUEUED,
354772e66a6SGleb Smirnoff 		    opts->maxidle, opts->minidle, opts->offtime,
355772e66a6SGleb Smirnoff 		    opts->flags);
356772e66a6SGleb Smirnoff 		cl = cbqp->ifnp.root_;
357772e66a6SGleb Smirnoff 	} else {
358772e66a6SGleb Smirnoff 		cl = rmc_newclass(a->priority,
359772e66a6SGleb Smirnoff 				  &cbqp->ifnp, opts->ns_per_byte,
360772e66a6SGleb Smirnoff 				  rmc_delay_action, a->qlimit, parent, borrow,
361772e66a6SGleb Smirnoff 				  opts->maxidle, opts->minidle, opts->offtime,
362772e66a6SGleb Smirnoff 				  opts->pktsize, opts->flags);
363772e66a6SGleb Smirnoff 	}
364772e66a6SGleb Smirnoff 	if (cl == NULL)
365772e66a6SGleb Smirnoff 		return (ENOMEM);
366772e66a6SGleb Smirnoff 
367772e66a6SGleb Smirnoff 	/* return handle to user space. */
368772e66a6SGleb Smirnoff 	cl->stats_.handle = a->qid;
369772e66a6SGleb Smirnoff 	cl->stats_.depth = cl->depth_;
370772e66a6SGleb Smirnoff 
371772e66a6SGleb Smirnoff 	/* save the allocated class */
372772e66a6SGleb Smirnoff 	cbqp->cbq_class_tbl[i] = cl;
373772e66a6SGleb Smirnoff 
374772e66a6SGleb Smirnoff 	if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS)
375772e66a6SGleb Smirnoff 		cbqp->ifnp.default_ = cl;
376772e66a6SGleb Smirnoff 
377772e66a6SGleb Smirnoff 	return (0);
378772e66a6SGleb Smirnoff }
379772e66a6SGleb Smirnoff 
380772e66a6SGleb Smirnoff int
cbq_remove_queue(struct pf_altq * a)381772e66a6SGleb Smirnoff cbq_remove_queue(struct pf_altq *a)
382772e66a6SGleb Smirnoff {
383772e66a6SGleb Smirnoff 	struct rm_class	*cl;
384772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp;
385772e66a6SGleb Smirnoff 	int		i;
386772e66a6SGleb Smirnoff 
387772e66a6SGleb Smirnoff 	if ((cbqp = a->altq_disc) == NULL)
388772e66a6SGleb Smirnoff 		return (EINVAL);
389772e66a6SGleb Smirnoff 
390772e66a6SGleb Smirnoff 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
391772e66a6SGleb Smirnoff 		return (EINVAL);
392772e66a6SGleb Smirnoff 
393772e66a6SGleb Smirnoff 	/* if we are a parent class, then return an error. */
394772e66a6SGleb Smirnoff 	if (is_a_parent_class(cl))
395772e66a6SGleb Smirnoff 		return (EINVAL);
396772e66a6SGleb Smirnoff 
397772e66a6SGleb Smirnoff 	/* delete the class */
398772e66a6SGleb Smirnoff 	rmc_delete_class(&cbqp->ifnp, cl);
399772e66a6SGleb Smirnoff 
400772e66a6SGleb Smirnoff 	/*
401772e66a6SGleb Smirnoff 	 * free the class handle
402772e66a6SGleb Smirnoff 	 */
403772e66a6SGleb Smirnoff 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
404772e66a6SGleb Smirnoff 		if (cbqp->cbq_class_tbl[i] == cl) {
405772e66a6SGleb Smirnoff 			cbqp->cbq_class_tbl[i] = NULL;
406772e66a6SGleb Smirnoff 			if (cl == cbqp->ifnp.root_)
407772e66a6SGleb Smirnoff 				cbqp->ifnp.root_ = NULL;
408772e66a6SGleb Smirnoff 			if (cl == cbqp->ifnp.default_)
409772e66a6SGleb Smirnoff 				cbqp->ifnp.default_ = NULL;
410772e66a6SGleb Smirnoff 			break;
411772e66a6SGleb Smirnoff 		}
412772e66a6SGleb Smirnoff 
413772e66a6SGleb Smirnoff 	return (0);
414772e66a6SGleb Smirnoff }
415772e66a6SGleb Smirnoff 
416772e66a6SGleb Smirnoff int
cbq_getqstats(struct pf_altq * a,void * ubuf,int * nbytes,int version)417249cc75fSPatrick Kelsey cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
418772e66a6SGleb Smirnoff {
419772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp;
420772e66a6SGleb Smirnoff 	struct rm_class	*cl;
421772e66a6SGleb Smirnoff 	class_stats_t	 stats;
422772e66a6SGleb Smirnoff 	int		 error = 0;
423772e66a6SGleb Smirnoff 
424772e66a6SGleb Smirnoff 	if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
425772e66a6SGleb Smirnoff 		return (EBADF);
426772e66a6SGleb Smirnoff 
427772e66a6SGleb Smirnoff 	if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
428772e66a6SGleb Smirnoff 		return (EINVAL);
429772e66a6SGleb Smirnoff 
430772e66a6SGleb Smirnoff 	if (*nbytes < sizeof(stats))
431772e66a6SGleb Smirnoff 		return (EINVAL);
432772e66a6SGleb Smirnoff 
433772e66a6SGleb Smirnoff 	get_class_stats(&stats, cl);
434772e66a6SGleb Smirnoff 
435772e66a6SGleb Smirnoff 	if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
436772e66a6SGleb Smirnoff 		return (error);
437772e66a6SGleb Smirnoff 	*nbytes = sizeof(stats);
438772e66a6SGleb Smirnoff 	return (0);
439772e66a6SGleb Smirnoff }
440772e66a6SGleb Smirnoff 
441772e66a6SGleb Smirnoff /*
442772e66a6SGleb Smirnoff  * int
443772e66a6SGleb Smirnoff  * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr)
444772e66a6SGleb Smirnoff  *		- Queue data packets.
445772e66a6SGleb Smirnoff  *
446772e66a6SGleb Smirnoff  *	cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
447772e66a6SGleb Smirnoff  *	layer (e.g. ether_output).  cbq_enqueue queues the given packet
448772e66a6SGleb Smirnoff  *	to the cbq, then invokes the driver's start routine.
449772e66a6SGleb Smirnoff  *
450772e66a6SGleb Smirnoff  *	Assumptions:	called in splimp
451772e66a6SGleb Smirnoff  *	Returns:	0 if the queueing is successful.
452772e66a6SGleb Smirnoff  *			ENOBUFS if a packet dropping occurred as a result of
453772e66a6SGleb Smirnoff  *			the queueing.
454772e66a6SGleb Smirnoff  */
455772e66a6SGleb Smirnoff 
456772e66a6SGleb Smirnoff static int
cbq_enqueue(struct ifaltq * ifq,struct mbuf * m,struct altq_pktattr * pktattr)457772e66a6SGleb Smirnoff cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
458772e66a6SGleb Smirnoff {
459772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
460772e66a6SGleb Smirnoff 	struct rm_class	*cl;
461772e66a6SGleb Smirnoff 	struct pf_mtag	*t;
462772e66a6SGleb Smirnoff 	int		 len;
463772e66a6SGleb Smirnoff 
464772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
465772e66a6SGleb Smirnoff 
466772e66a6SGleb Smirnoff 	/* grab class set by classifier */
467772e66a6SGleb Smirnoff 	if ((m->m_flags & M_PKTHDR) == 0) {
468772e66a6SGleb Smirnoff 		/* should not happen */
469772e66a6SGleb Smirnoff 		printf("altq: packet for %s does not have pkthdr\n",
470772e66a6SGleb Smirnoff 		    ifq->altq_ifp->if_xname);
471772e66a6SGleb Smirnoff 		m_freem(m);
472772e66a6SGleb Smirnoff 		return (ENOBUFS);
473772e66a6SGleb Smirnoff 	}
474772e66a6SGleb Smirnoff 	cl = NULL;
475772e66a6SGleb Smirnoff 	if ((t = pf_find_mtag(m)) != NULL)
476772e66a6SGleb Smirnoff 		cl = clh_to_clp(cbqp, t->qid);
477772e66a6SGleb Smirnoff 	if (cl == NULL) {
478772e66a6SGleb Smirnoff 		cl = cbqp->ifnp.default_;
479772e66a6SGleb Smirnoff 		if (cl == NULL) {
480772e66a6SGleb Smirnoff 			m_freem(m);
481772e66a6SGleb Smirnoff 			return (ENOBUFS);
482772e66a6SGleb Smirnoff 		}
483772e66a6SGleb Smirnoff 	}
484772e66a6SGleb Smirnoff 	cl->pktattr_ = NULL;
485772e66a6SGleb Smirnoff 	len = m_pktlen(m);
486772e66a6SGleb Smirnoff 	if (rmc_queue_packet(cl, m) != 0) {
487772e66a6SGleb Smirnoff 		/* drop occurred.  some mbuf was freed in rmc_queue_packet. */
488772e66a6SGleb Smirnoff 		PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
489772e66a6SGleb Smirnoff 		return (ENOBUFS);
490772e66a6SGleb Smirnoff 	}
491772e66a6SGleb Smirnoff 
492772e66a6SGleb Smirnoff 	/* successfully queued. */
493772e66a6SGleb Smirnoff 	++cbqp->cbq_qlen;
494772e66a6SGleb Smirnoff 	IFQ_INC_LEN(ifq);
495772e66a6SGleb Smirnoff 	return (0);
496772e66a6SGleb Smirnoff }
497772e66a6SGleb Smirnoff 
498772e66a6SGleb Smirnoff static struct mbuf *
cbq_dequeue(struct ifaltq * ifq,int op)499772e66a6SGleb Smirnoff cbq_dequeue(struct ifaltq *ifq, int op)
500772e66a6SGleb Smirnoff {
501772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp = (cbq_state_t *)ifq->altq_disc;
502772e66a6SGleb Smirnoff 	struct mbuf	*m;
503772e66a6SGleb Smirnoff 
504772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
505772e66a6SGleb Smirnoff 
506772e66a6SGleb Smirnoff 	m = rmc_dequeue_next(&cbqp->ifnp, op);
507772e66a6SGleb Smirnoff 
508772e66a6SGleb Smirnoff 	if (m && op == ALTDQ_REMOVE) {
509772e66a6SGleb Smirnoff 		--cbqp->cbq_qlen;  /* decrement # of packets in cbq */
510772e66a6SGleb Smirnoff 		IFQ_DEC_LEN(ifq);
511772e66a6SGleb Smirnoff 
512772e66a6SGleb Smirnoff 		/* Update the class. */
513772e66a6SGleb Smirnoff 		rmc_update_class_util(&cbqp->ifnp);
514772e66a6SGleb Smirnoff 	}
515772e66a6SGleb Smirnoff 	return (m);
516772e66a6SGleb Smirnoff }
517772e66a6SGleb Smirnoff 
518772e66a6SGleb Smirnoff /*
519772e66a6SGleb Smirnoff  * void
520772e66a6SGleb Smirnoff  * cbqrestart(queue_t *) - Restart sending of data.
521772e66a6SGleb Smirnoff  * called from rmc_restart in splimp via timeout after waking up
522772e66a6SGleb Smirnoff  * a suspended class.
523772e66a6SGleb Smirnoff  *	Returns:	NONE
524772e66a6SGleb Smirnoff  */
525772e66a6SGleb Smirnoff 
526772e66a6SGleb Smirnoff static void
cbqrestart(struct ifaltq * ifq)527772e66a6SGleb Smirnoff cbqrestart(struct ifaltq *ifq)
528772e66a6SGleb Smirnoff {
529772e66a6SGleb Smirnoff 	cbq_state_t	*cbqp;
530772e66a6SGleb Smirnoff 	struct ifnet	*ifp;
531772e66a6SGleb Smirnoff 
532772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
533772e66a6SGleb Smirnoff 
534772e66a6SGleb Smirnoff 	if (!ALTQ_IS_ENABLED(ifq))
535772e66a6SGleb Smirnoff 		/* cbq must have been detached */
536772e66a6SGleb Smirnoff 		return;
537772e66a6SGleb Smirnoff 
538772e66a6SGleb Smirnoff 	if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
539772e66a6SGleb Smirnoff 		/* should not happen */
540772e66a6SGleb Smirnoff 		return;
541772e66a6SGleb Smirnoff 
542772e66a6SGleb Smirnoff 	ifp = ifq->altq_ifp;
543772e66a6SGleb Smirnoff 	if (ifp->if_start &&
544772e66a6SGleb Smirnoff 	    cbqp->cbq_qlen > 0 && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
545772e66a6SGleb Smirnoff 	    	IFQ_UNLOCK(ifq);
546772e66a6SGleb Smirnoff 		(*ifp->if_start)(ifp);
547772e66a6SGleb Smirnoff 		IFQ_LOCK(ifq);
548772e66a6SGleb Smirnoff 	}
549772e66a6SGleb Smirnoff }
550772e66a6SGleb Smirnoff 
cbq_purge(cbq_state_t * cbqp)551772e66a6SGleb Smirnoff static void cbq_purge(cbq_state_t *cbqp)
552772e66a6SGleb Smirnoff {
553772e66a6SGleb Smirnoff 	struct rm_class	*cl;
554772e66a6SGleb Smirnoff 	int		 i;
555772e66a6SGleb Smirnoff 
556772e66a6SGleb Smirnoff 	for (i = 0; i < CBQ_MAX_CLASSES; i++)
557772e66a6SGleb Smirnoff 		if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
558772e66a6SGleb Smirnoff 			rmc_dropall(cl);
559772e66a6SGleb Smirnoff 	if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
560772e66a6SGleb Smirnoff 		cbqp->ifnp.ifq_->ifq_len = 0;
561772e66a6SGleb Smirnoff }
562772e66a6SGleb Smirnoff 
563772e66a6SGleb Smirnoff #endif /* ALTQ_CBQ */
564