xref: /freebsd/sys/net/altq/altq_priq.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1da8ae05dSGleb Smirnoff /*-
2772e66a6SGleb Smirnoff  * Copyright (C) 2000-2003
3772e66a6SGleb Smirnoff  *	Sony Computer Science Laboratories Inc.  All rights reserved.
4772e66a6SGleb Smirnoff  *
5772e66a6SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6772e66a6SGleb Smirnoff  * modification, are permitted provided that the following conditions
7772e66a6SGleb Smirnoff  * are met:
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  * 2. Redistributions in binary form must reproduce the above copyright
11772e66a6SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12772e66a6SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13772e66a6SGleb Smirnoff  *
14772e66a6SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15772e66a6SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16772e66a6SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17772e66a6SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18772e66a6SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19772e66a6SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20772e66a6SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21772e66a6SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22772e66a6SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23772e66a6SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24772e66a6SGleb Smirnoff  * SUCH DAMAGE.
25da8ae05dSGleb Smirnoff  *
26da8ae05dSGleb Smirnoff  * $KAME: altq_priq.c,v 1.11 2003/09/17 14:23:25 kjc Exp $
27772e66a6SGleb Smirnoff  */
28772e66a6SGleb Smirnoff /*
29772e66a6SGleb Smirnoff  * priority queue
30772e66a6SGleb Smirnoff  */
31772e66a6SGleb Smirnoff 
32772e66a6SGleb Smirnoff #include "opt_altq.h"
33772e66a6SGleb Smirnoff #include "opt_inet.h"
34772e66a6SGleb Smirnoff #include "opt_inet6.h"
35772e66a6SGleb Smirnoff 
36772e66a6SGleb Smirnoff #ifdef ALTQ_PRIQ  /* priq is enabled by ALTQ_PRIQ 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/sockio.h>
43772e66a6SGleb Smirnoff #include <sys/systm.h>
44772e66a6SGleb Smirnoff #include <sys/proc.h>
45772e66a6SGleb Smirnoff #include <sys/errno.h>
46772e66a6SGleb Smirnoff #include <sys/kernel.h>
47772e66a6SGleb Smirnoff #include <sys/queue.h>
48772e66a6SGleb Smirnoff 
49772e66a6SGleb Smirnoff #include <net/if.h>
50772e66a6SGleb Smirnoff #include <net/if_var.h>
512c2b37adSJustin Hibbits #include <net/if_private.h>
52772e66a6SGleb Smirnoff #include <netinet/in.h>
53772e66a6SGleb Smirnoff 
54772e66a6SGleb Smirnoff #include <netpfil/pf/pf.h>
55772e66a6SGleb Smirnoff #include <netpfil/pf/pf_altq.h>
56772e66a6SGleb Smirnoff #include <netpfil/pf/pf_mtag.h>
57772e66a6SGleb Smirnoff #include <net/altq/altq.h>
58772e66a6SGleb Smirnoff #include <net/altq/altq_priq.h>
59772e66a6SGleb Smirnoff 
60772e66a6SGleb Smirnoff /*
61772e66a6SGleb Smirnoff  * function prototypes
62772e66a6SGleb Smirnoff  */
63772e66a6SGleb Smirnoff static int priq_clear_interface(struct priq_if *);
64772e66a6SGleb Smirnoff static int priq_request(struct ifaltq *, int, void *);
65772e66a6SGleb Smirnoff static void priq_purge(struct priq_if *);
66772e66a6SGleb Smirnoff static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
67772e66a6SGleb Smirnoff     int);
68772e66a6SGleb Smirnoff static int priq_class_destroy(struct priq_class *);
69772e66a6SGleb Smirnoff static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
70772e66a6SGleb Smirnoff static struct mbuf *priq_dequeue(struct ifaltq *, int);
71772e66a6SGleb Smirnoff 
72772e66a6SGleb Smirnoff static int priq_addq(struct priq_class *, struct mbuf *);
73772e66a6SGleb Smirnoff static struct mbuf *priq_getq(struct priq_class *);
74772e66a6SGleb Smirnoff static struct mbuf *priq_pollq(struct priq_class *);
75772e66a6SGleb Smirnoff static void priq_purgeq(struct priq_class *);
76772e66a6SGleb Smirnoff 
77772e66a6SGleb Smirnoff static void get_class_stats(struct priq_classstats *, struct priq_class *);
78772e66a6SGleb Smirnoff static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
79772e66a6SGleb Smirnoff 
80772e66a6SGleb Smirnoff int
priq_pfattach(struct pf_altq * a)81772e66a6SGleb Smirnoff priq_pfattach(struct pf_altq *a)
82772e66a6SGleb Smirnoff {
83772e66a6SGleb Smirnoff 	struct ifnet *ifp;
84772e66a6SGleb Smirnoff 	int s, error;
85772e66a6SGleb Smirnoff 
86772e66a6SGleb Smirnoff 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
87772e66a6SGleb Smirnoff 		return (EINVAL);
88772e66a6SGleb Smirnoff 	s = splnet();
89772e66a6SGleb Smirnoff 	error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
9027b2aa49SKristof Provost 	    priq_enqueue, priq_dequeue, priq_request);
91772e66a6SGleb Smirnoff 	splx(s);
92772e66a6SGleb Smirnoff 	return (error);
93772e66a6SGleb Smirnoff }
94772e66a6SGleb Smirnoff 
95772e66a6SGleb Smirnoff int
priq_add_altq(struct ifnet * ifp,struct pf_altq * a)968f2ac656SPatrick Kelsey priq_add_altq(struct ifnet * ifp, struct pf_altq *a)
97772e66a6SGleb Smirnoff {
98772e66a6SGleb Smirnoff 	struct priq_if	*pif;
99772e66a6SGleb Smirnoff 
1008f2ac656SPatrick Kelsey 	if (ifp == NULL)
101772e66a6SGleb Smirnoff 		return (EINVAL);
102772e66a6SGleb Smirnoff 	if (!ALTQ_IS_READY(&ifp->if_snd))
103772e66a6SGleb Smirnoff 		return (ENODEV);
104772e66a6SGleb Smirnoff 
105772e66a6SGleb Smirnoff 	pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
106772e66a6SGleb Smirnoff 	if (pif == NULL)
107772e66a6SGleb Smirnoff 		return (ENOMEM);
108772e66a6SGleb Smirnoff 	pif->pif_bandwidth = a->ifbandwidth;
109772e66a6SGleb Smirnoff 	pif->pif_maxpri = -1;
110772e66a6SGleb Smirnoff 	pif->pif_ifq = &ifp->if_snd;
111772e66a6SGleb Smirnoff 
112772e66a6SGleb Smirnoff 	/* keep the state in pf_altq */
113772e66a6SGleb Smirnoff 	a->altq_disc = pif;
114772e66a6SGleb Smirnoff 
115772e66a6SGleb Smirnoff 	return (0);
116772e66a6SGleb Smirnoff }
117772e66a6SGleb Smirnoff 
118772e66a6SGleb Smirnoff int
priq_remove_altq(struct pf_altq * a)119772e66a6SGleb Smirnoff priq_remove_altq(struct pf_altq *a)
120772e66a6SGleb Smirnoff {
121772e66a6SGleb Smirnoff 	struct priq_if *pif;
122772e66a6SGleb Smirnoff 
123772e66a6SGleb Smirnoff 	if ((pif = a->altq_disc) == NULL)
124772e66a6SGleb Smirnoff 		return (EINVAL);
125772e66a6SGleb Smirnoff 	a->altq_disc = NULL;
126772e66a6SGleb Smirnoff 
127772e66a6SGleb Smirnoff 	(void)priq_clear_interface(pif);
128772e66a6SGleb Smirnoff 
129772e66a6SGleb Smirnoff 	free(pif, M_DEVBUF);
130772e66a6SGleb Smirnoff 	return (0);
131772e66a6SGleb Smirnoff }
132772e66a6SGleb Smirnoff 
133772e66a6SGleb Smirnoff int
priq_add_queue(struct pf_altq * a)134772e66a6SGleb Smirnoff priq_add_queue(struct pf_altq *a)
135772e66a6SGleb Smirnoff {
136772e66a6SGleb Smirnoff 	struct priq_if *pif;
137772e66a6SGleb Smirnoff 	struct priq_class *cl;
138772e66a6SGleb Smirnoff 
139772e66a6SGleb Smirnoff 	if ((pif = a->altq_disc) == NULL)
140772e66a6SGleb Smirnoff 		return (EINVAL);
141772e66a6SGleb Smirnoff 
142772e66a6SGleb Smirnoff 	/* check parameters */
143772e66a6SGleb Smirnoff 	if (a->priority >= PRIQ_MAXPRI)
144772e66a6SGleb Smirnoff 		return (EINVAL);
145772e66a6SGleb Smirnoff 	if (a->qid == 0)
146772e66a6SGleb Smirnoff 		return (EINVAL);
147772e66a6SGleb Smirnoff 	if (pif->pif_classes[a->priority] != NULL)
148772e66a6SGleb Smirnoff 		return (EBUSY);
149772e66a6SGleb Smirnoff 	if (clh_to_clp(pif, a->qid) != NULL)
150772e66a6SGleb Smirnoff 		return (EBUSY);
151772e66a6SGleb Smirnoff 
152772e66a6SGleb Smirnoff 	cl = priq_class_create(pif, a->priority, a->qlimit,
153772e66a6SGleb Smirnoff 	    a->pq_u.priq_opts.flags, a->qid);
154772e66a6SGleb Smirnoff 	if (cl == NULL)
155772e66a6SGleb Smirnoff 		return (ENOMEM);
156772e66a6SGleb Smirnoff 
157772e66a6SGleb Smirnoff 	return (0);
158772e66a6SGleb Smirnoff }
159772e66a6SGleb Smirnoff 
160772e66a6SGleb Smirnoff int
priq_remove_queue(struct pf_altq * a)161772e66a6SGleb Smirnoff priq_remove_queue(struct pf_altq *a)
162772e66a6SGleb Smirnoff {
163772e66a6SGleb Smirnoff 	struct priq_if *pif;
164772e66a6SGleb Smirnoff 	struct priq_class *cl;
165772e66a6SGleb Smirnoff 
166772e66a6SGleb Smirnoff 	if ((pif = a->altq_disc) == NULL)
167772e66a6SGleb Smirnoff 		return (EINVAL);
168772e66a6SGleb Smirnoff 
169772e66a6SGleb Smirnoff 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
170772e66a6SGleb Smirnoff 		return (EINVAL);
171772e66a6SGleb Smirnoff 
172772e66a6SGleb Smirnoff 	return (priq_class_destroy(cl));
173772e66a6SGleb Smirnoff }
174772e66a6SGleb Smirnoff 
175772e66a6SGleb Smirnoff int
priq_getqstats(struct pf_altq * a,void * ubuf,int * nbytes,int version)176249cc75fSPatrick Kelsey priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
177772e66a6SGleb Smirnoff {
178772e66a6SGleb Smirnoff 	struct priq_if *pif;
179772e66a6SGleb Smirnoff 	struct priq_class *cl;
180772e66a6SGleb Smirnoff 	struct priq_classstats stats;
181772e66a6SGleb Smirnoff 	int error = 0;
182772e66a6SGleb Smirnoff 
183772e66a6SGleb Smirnoff 	if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
184772e66a6SGleb Smirnoff 		return (EBADF);
185772e66a6SGleb Smirnoff 
186772e66a6SGleb Smirnoff 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
187772e66a6SGleb Smirnoff 		return (EINVAL);
188772e66a6SGleb Smirnoff 
189772e66a6SGleb Smirnoff 	if (*nbytes < sizeof(stats))
190772e66a6SGleb Smirnoff 		return (EINVAL);
191772e66a6SGleb Smirnoff 
192772e66a6SGleb Smirnoff 	get_class_stats(&stats, cl);
193772e66a6SGleb Smirnoff 
194772e66a6SGleb Smirnoff 	if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
195772e66a6SGleb Smirnoff 		return (error);
196772e66a6SGleb Smirnoff 	*nbytes = sizeof(stats);
197772e66a6SGleb Smirnoff 	return (0);
198772e66a6SGleb Smirnoff }
199772e66a6SGleb Smirnoff 
200772e66a6SGleb Smirnoff /*
201772e66a6SGleb Smirnoff  * bring the interface back to the initial state by discarding
202772e66a6SGleb Smirnoff  * all the filters and classes.
203772e66a6SGleb Smirnoff  */
204772e66a6SGleb Smirnoff static int
priq_clear_interface(struct priq_if * pif)205772e66a6SGleb Smirnoff priq_clear_interface(struct priq_if *pif)
206772e66a6SGleb Smirnoff {
207772e66a6SGleb Smirnoff 	struct priq_class	*cl;
208772e66a6SGleb Smirnoff 	int pri;
209772e66a6SGleb Smirnoff 
210772e66a6SGleb Smirnoff #ifdef ALTQ3_CLFIER_COMPAT
211772e66a6SGleb Smirnoff 	/* free the filters for this interface */
212772e66a6SGleb Smirnoff 	acc_discard_filters(&pif->pif_classifier, NULL, 1);
213772e66a6SGleb Smirnoff #endif
214772e66a6SGleb Smirnoff 
215772e66a6SGleb Smirnoff 	/* clear out the classes */
216772e66a6SGleb Smirnoff 	for (pri = 0; pri <= pif->pif_maxpri; pri++)
217772e66a6SGleb Smirnoff 		if ((cl = pif->pif_classes[pri]) != NULL)
218772e66a6SGleb Smirnoff 			priq_class_destroy(cl);
219772e66a6SGleb Smirnoff 
220772e66a6SGleb Smirnoff 	return (0);
221772e66a6SGleb Smirnoff }
222772e66a6SGleb Smirnoff 
223772e66a6SGleb Smirnoff static int
priq_request(struct ifaltq * ifq,int req,void * arg)224772e66a6SGleb Smirnoff priq_request(struct ifaltq *ifq, int req, void *arg)
225772e66a6SGleb Smirnoff {
226772e66a6SGleb Smirnoff 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
227772e66a6SGleb Smirnoff 
228772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
229772e66a6SGleb Smirnoff 
230772e66a6SGleb Smirnoff 	switch (req) {
231772e66a6SGleb Smirnoff 	case ALTRQ_PURGE:
232772e66a6SGleb Smirnoff 		priq_purge(pif);
233772e66a6SGleb Smirnoff 		break;
234772e66a6SGleb Smirnoff 	}
235772e66a6SGleb Smirnoff 	return (0);
236772e66a6SGleb Smirnoff }
237772e66a6SGleb Smirnoff 
238772e66a6SGleb Smirnoff /* discard all the queued packets on the interface */
239772e66a6SGleb Smirnoff static void
priq_purge(struct priq_if * pif)240772e66a6SGleb Smirnoff priq_purge(struct priq_if *pif)
241772e66a6SGleb Smirnoff {
242772e66a6SGleb Smirnoff 	struct priq_class *cl;
243772e66a6SGleb Smirnoff 	int pri;
244772e66a6SGleb Smirnoff 
245772e66a6SGleb Smirnoff 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
246772e66a6SGleb Smirnoff 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
247772e66a6SGleb Smirnoff 			priq_purgeq(cl);
248772e66a6SGleb Smirnoff 	}
249772e66a6SGleb Smirnoff 	if (ALTQ_IS_ENABLED(pif->pif_ifq))
250772e66a6SGleb Smirnoff 		pif->pif_ifq->ifq_len = 0;
251772e66a6SGleb Smirnoff }
252772e66a6SGleb Smirnoff 
253772e66a6SGleb Smirnoff static struct priq_class *
priq_class_create(struct priq_if * pif,int pri,int qlimit,int flags,int qid)254772e66a6SGleb Smirnoff priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
255772e66a6SGleb Smirnoff {
256772e66a6SGleb Smirnoff 	struct priq_class *cl;
257772e66a6SGleb Smirnoff 	int s;
258772e66a6SGleb Smirnoff 
259772e66a6SGleb Smirnoff #ifndef ALTQ_RED
260772e66a6SGleb Smirnoff 	if (flags & PRCF_RED) {
261772e66a6SGleb Smirnoff #ifdef ALTQ_DEBUG
262772e66a6SGleb Smirnoff 		printf("priq_class_create: RED not configured for PRIQ!\n");
263772e66a6SGleb Smirnoff #endif
264772e66a6SGleb Smirnoff 		return (NULL);
265772e66a6SGleb Smirnoff 	}
266772e66a6SGleb Smirnoff #endif
2670a70aaf8SLuiz Otavio O Souza #ifndef ALTQ_CODEL
2680a70aaf8SLuiz Otavio O Souza 	if (flags & PRCF_CODEL) {
2690a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_DEBUG
2700a70aaf8SLuiz Otavio O Souza 		printf("priq_class_create: CODEL not configured for PRIQ!\n");
2710a70aaf8SLuiz Otavio O Souza #endif
2720a70aaf8SLuiz Otavio O Souza 		return (NULL);
2730a70aaf8SLuiz Otavio O Souza 	}
2740a70aaf8SLuiz Otavio O Souza #endif
275772e66a6SGleb Smirnoff 
276772e66a6SGleb Smirnoff 	if ((cl = pif->pif_classes[pri]) != NULL) {
277772e66a6SGleb Smirnoff 		/* modify the class instead of creating a new one */
278772e66a6SGleb Smirnoff 		s = splnet();
279772e66a6SGleb Smirnoff 		IFQ_LOCK(cl->cl_pif->pif_ifq);
280772e66a6SGleb Smirnoff 		if (!qempty(cl->cl_q))
281772e66a6SGleb Smirnoff 			priq_purgeq(cl);
282772e66a6SGleb Smirnoff 		IFQ_UNLOCK(cl->cl_pif->pif_ifq);
283772e66a6SGleb Smirnoff 		splx(s);
284772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
285772e66a6SGleb Smirnoff 		if (q_is_rio(cl->cl_q))
286772e66a6SGleb Smirnoff 			rio_destroy((rio_t *)cl->cl_red);
287772e66a6SGleb Smirnoff #endif
288772e66a6SGleb Smirnoff #ifdef ALTQ_RED
289772e66a6SGleb Smirnoff 		if (q_is_red(cl->cl_q))
290772e66a6SGleb Smirnoff 			red_destroy(cl->cl_red);
291772e66a6SGleb Smirnoff #endif
2920a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
2930a70aaf8SLuiz Otavio O Souza 		if (q_is_codel(cl->cl_q))
2940a70aaf8SLuiz Otavio O Souza 			codel_destroy(cl->cl_codel);
2950a70aaf8SLuiz Otavio O Souza #endif
296772e66a6SGleb Smirnoff 	} else {
297772e66a6SGleb Smirnoff 		cl = malloc(sizeof(struct priq_class), M_DEVBUF,
298772e66a6SGleb Smirnoff 		    M_NOWAIT | M_ZERO);
299772e66a6SGleb Smirnoff 		if (cl == NULL)
300772e66a6SGleb Smirnoff 			return (NULL);
301772e66a6SGleb Smirnoff 
302772e66a6SGleb Smirnoff 		cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
303772e66a6SGleb Smirnoff 		    M_NOWAIT | M_ZERO);
304772e66a6SGleb Smirnoff 		if (cl->cl_q == NULL)
305772e66a6SGleb Smirnoff 			goto err_ret;
306772e66a6SGleb Smirnoff 	}
307772e66a6SGleb Smirnoff 
308772e66a6SGleb Smirnoff 	pif->pif_classes[pri] = cl;
309772e66a6SGleb Smirnoff 	if (flags & PRCF_DEFAULTCLASS)
310772e66a6SGleb Smirnoff 		pif->pif_default = cl;
311772e66a6SGleb Smirnoff 	if (qlimit == 0)
312772e66a6SGleb Smirnoff 		qlimit = 50;  /* use default */
313772e66a6SGleb Smirnoff 	qlimit(cl->cl_q) = qlimit;
314772e66a6SGleb Smirnoff 	qtype(cl->cl_q) = Q_DROPTAIL;
315772e66a6SGleb Smirnoff 	qlen(cl->cl_q) = 0;
3160a70aaf8SLuiz Otavio O Souza 	qsize(cl->cl_q) = 0;
317772e66a6SGleb Smirnoff 	cl->cl_flags = flags;
318772e66a6SGleb Smirnoff 	cl->cl_pri = pri;
319772e66a6SGleb Smirnoff 	if (pri > pif->pif_maxpri)
320772e66a6SGleb Smirnoff 		pif->pif_maxpri = pri;
321772e66a6SGleb Smirnoff 	cl->cl_pif = pif;
322772e66a6SGleb Smirnoff 	cl->cl_handle = qid;
323772e66a6SGleb Smirnoff 
324772e66a6SGleb Smirnoff #ifdef ALTQ_RED
325772e66a6SGleb Smirnoff 	if (flags & (PRCF_RED|PRCF_RIO)) {
326772e66a6SGleb Smirnoff 		int red_flags, red_pkttime;
327772e66a6SGleb Smirnoff 
328772e66a6SGleb Smirnoff 		red_flags = 0;
329772e66a6SGleb Smirnoff 		if (flags & PRCF_ECN)
330772e66a6SGleb Smirnoff 			red_flags |= REDF_ECN;
331772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
332772e66a6SGleb Smirnoff 		if (flags & PRCF_CLEARDSCP)
333772e66a6SGleb Smirnoff 			red_flags |= RIOF_CLEARDSCP;
334772e66a6SGleb Smirnoff #endif
335772e66a6SGleb Smirnoff 		if (pif->pif_bandwidth < 8)
336772e66a6SGleb Smirnoff 			red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
337772e66a6SGleb Smirnoff 		else
338772e66a6SGleb Smirnoff 			red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
339772e66a6SGleb Smirnoff 			  * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
340772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
341772e66a6SGleb Smirnoff 		if (flags & PRCF_RIO) {
342772e66a6SGleb Smirnoff 			cl->cl_red = (red_t *)rio_alloc(0, NULL,
343772e66a6SGleb Smirnoff 						red_flags, red_pkttime);
344772e66a6SGleb Smirnoff 			if (cl->cl_red == NULL)
345772e66a6SGleb Smirnoff 				goto err_ret;
346772e66a6SGleb Smirnoff 			qtype(cl->cl_q) = Q_RIO;
347772e66a6SGleb Smirnoff 		} else
348772e66a6SGleb Smirnoff #endif
349772e66a6SGleb Smirnoff 		if (flags & PRCF_RED) {
350772e66a6SGleb Smirnoff 			cl->cl_red = red_alloc(0, 0,
351772e66a6SGleb Smirnoff 			    qlimit(cl->cl_q) * 10/100,
352772e66a6SGleb Smirnoff 			    qlimit(cl->cl_q) * 30/100,
353772e66a6SGleb Smirnoff 			    red_flags, red_pkttime);
354772e66a6SGleb Smirnoff 			if (cl->cl_red == NULL)
355772e66a6SGleb Smirnoff 				goto err_ret;
356772e66a6SGleb Smirnoff 			qtype(cl->cl_q) = Q_RED;
357772e66a6SGleb Smirnoff 		}
358772e66a6SGleb Smirnoff 	}
359772e66a6SGleb Smirnoff #endif /* ALTQ_RED */
3600a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
3610a70aaf8SLuiz Otavio O Souza 	if (flags & PRCF_CODEL) {
3620a70aaf8SLuiz Otavio O Souza 		cl->cl_codel = codel_alloc(5, 100, 0);
3630a70aaf8SLuiz Otavio O Souza 		if (cl->cl_codel != NULL)
3640a70aaf8SLuiz Otavio O Souza 			qtype(cl->cl_q) = Q_CODEL;
3650a70aaf8SLuiz Otavio O Souza 	}
3660a70aaf8SLuiz Otavio O Souza #endif
367772e66a6SGleb Smirnoff 
368772e66a6SGleb Smirnoff 	return (cl);
369772e66a6SGleb Smirnoff 
370772e66a6SGleb Smirnoff  err_ret:
371772e66a6SGleb Smirnoff 	if (cl->cl_red != NULL) {
372772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
373772e66a6SGleb Smirnoff 		if (q_is_rio(cl->cl_q))
374772e66a6SGleb Smirnoff 			rio_destroy((rio_t *)cl->cl_red);
375772e66a6SGleb Smirnoff #endif
376772e66a6SGleb Smirnoff #ifdef ALTQ_RED
377772e66a6SGleb Smirnoff 		if (q_is_red(cl->cl_q))
378772e66a6SGleb Smirnoff 			red_destroy(cl->cl_red);
379772e66a6SGleb Smirnoff #endif
3800a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
3810a70aaf8SLuiz Otavio O Souza 		if (q_is_codel(cl->cl_q))
3820a70aaf8SLuiz Otavio O Souza 			codel_destroy(cl->cl_codel);
3830a70aaf8SLuiz Otavio O Souza #endif
384772e66a6SGleb Smirnoff 	}
385772e66a6SGleb Smirnoff 	if (cl->cl_q != NULL)
386772e66a6SGleb Smirnoff 		free(cl->cl_q, M_DEVBUF);
387772e66a6SGleb Smirnoff 	free(cl, M_DEVBUF);
388772e66a6SGleb Smirnoff 	return (NULL);
389772e66a6SGleb Smirnoff }
390772e66a6SGleb Smirnoff 
391772e66a6SGleb Smirnoff static int
priq_class_destroy(struct priq_class * cl)392772e66a6SGleb Smirnoff priq_class_destroy(struct priq_class *cl)
393772e66a6SGleb Smirnoff {
394772e66a6SGleb Smirnoff 	struct priq_if *pif;
395772e66a6SGleb Smirnoff 	int s, pri;
396772e66a6SGleb Smirnoff 
397772e66a6SGleb Smirnoff 	s = splnet();
398772e66a6SGleb Smirnoff 	IFQ_LOCK(cl->cl_pif->pif_ifq);
399772e66a6SGleb Smirnoff 
400772e66a6SGleb Smirnoff #ifdef ALTQ3_CLFIER_COMPAT
401772e66a6SGleb Smirnoff 	/* delete filters referencing to this class */
402772e66a6SGleb Smirnoff 	acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
403772e66a6SGleb Smirnoff #endif
404772e66a6SGleb Smirnoff 
405772e66a6SGleb Smirnoff 	if (!qempty(cl->cl_q))
406772e66a6SGleb Smirnoff 		priq_purgeq(cl);
407772e66a6SGleb Smirnoff 
408772e66a6SGleb Smirnoff 	pif = cl->cl_pif;
409772e66a6SGleb Smirnoff 	pif->pif_classes[cl->cl_pri] = NULL;
410772e66a6SGleb Smirnoff 	if (pif->pif_maxpri == cl->cl_pri) {
411772e66a6SGleb Smirnoff 		for (pri = cl->cl_pri; pri >= 0; pri--)
412772e66a6SGleb Smirnoff 			if (pif->pif_classes[pri] != NULL) {
413772e66a6SGleb Smirnoff 				pif->pif_maxpri = pri;
414772e66a6SGleb Smirnoff 				break;
415772e66a6SGleb Smirnoff 			}
416772e66a6SGleb Smirnoff 		if (pri < 0)
417772e66a6SGleb Smirnoff 			pif->pif_maxpri = -1;
418772e66a6SGleb Smirnoff 	}
419772e66a6SGleb Smirnoff 	IFQ_UNLOCK(cl->cl_pif->pif_ifq);
420772e66a6SGleb Smirnoff 	splx(s);
421772e66a6SGleb Smirnoff 
422772e66a6SGleb Smirnoff 	if (cl->cl_red != NULL) {
423772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
424772e66a6SGleb Smirnoff 		if (q_is_rio(cl->cl_q))
425772e66a6SGleb Smirnoff 			rio_destroy((rio_t *)cl->cl_red);
426772e66a6SGleb Smirnoff #endif
427772e66a6SGleb Smirnoff #ifdef ALTQ_RED
428772e66a6SGleb Smirnoff 		if (q_is_red(cl->cl_q))
429772e66a6SGleb Smirnoff 			red_destroy(cl->cl_red);
430772e66a6SGleb Smirnoff #endif
4310a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
4320a70aaf8SLuiz Otavio O Souza 		if (q_is_codel(cl->cl_q))
4330a70aaf8SLuiz Otavio O Souza 			codel_destroy(cl->cl_codel);
4340a70aaf8SLuiz Otavio O Souza #endif
435772e66a6SGleb Smirnoff 	}
436772e66a6SGleb Smirnoff 	free(cl->cl_q, M_DEVBUF);
437772e66a6SGleb Smirnoff 	free(cl, M_DEVBUF);
438772e66a6SGleb Smirnoff 	return (0);
439772e66a6SGleb Smirnoff }
440772e66a6SGleb Smirnoff 
441772e66a6SGleb Smirnoff /*
442772e66a6SGleb Smirnoff  * priq_enqueue is an enqueue function to be registered to
443772e66a6SGleb Smirnoff  * (*altq_enqueue) in struct ifaltq.
444772e66a6SGleb Smirnoff  */
445772e66a6SGleb Smirnoff static int
priq_enqueue(struct ifaltq * ifq,struct mbuf * m,struct altq_pktattr * pktattr)446772e66a6SGleb Smirnoff priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
447772e66a6SGleb Smirnoff {
448772e66a6SGleb Smirnoff 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
449772e66a6SGleb Smirnoff 	struct priq_class *cl;
450772e66a6SGleb Smirnoff 	struct pf_mtag *t;
451772e66a6SGleb Smirnoff 	int len;
452772e66a6SGleb Smirnoff 
453772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
454772e66a6SGleb Smirnoff 
455772e66a6SGleb Smirnoff 	/* grab class set by classifier */
456772e66a6SGleb Smirnoff 	if ((m->m_flags & M_PKTHDR) == 0) {
457772e66a6SGleb Smirnoff 		/* should not happen */
458772e66a6SGleb Smirnoff 		printf("altq: packet for %s does not have pkthdr\n",
459772e66a6SGleb Smirnoff 		    ifq->altq_ifp->if_xname);
460772e66a6SGleb Smirnoff 		m_freem(m);
461772e66a6SGleb Smirnoff 		return (ENOBUFS);
462772e66a6SGleb Smirnoff 	}
463772e66a6SGleb Smirnoff 	cl = NULL;
464772e66a6SGleb Smirnoff 	if ((t = pf_find_mtag(m)) != NULL)
465772e66a6SGleb Smirnoff 		cl = clh_to_clp(pif, t->qid);
466772e66a6SGleb Smirnoff 	if (cl == NULL) {
467772e66a6SGleb Smirnoff 		cl = pif->pif_default;
468772e66a6SGleb Smirnoff 		if (cl == NULL) {
469772e66a6SGleb Smirnoff 			m_freem(m);
470772e66a6SGleb Smirnoff 			return (ENOBUFS);
471772e66a6SGleb Smirnoff 		}
472772e66a6SGleb Smirnoff 	}
473772e66a6SGleb Smirnoff 	cl->cl_pktattr = NULL;
474772e66a6SGleb Smirnoff 	len = m_pktlen(m);
475772e66a6SGleb Smirnoff 	if (priq_addq(cl, m) != 0) {
476772e66a6SGleb Smirnoff 		/* drop occurred.  mbuf was freed in priq_addq. */
477772e66a6SGleb Smirnoff 		PKTCNTR_ADD(&cl->cl_dropcnt, len);
478772e66a6SGleb Smirnoff 		return (ENOBUFS);
479772e66a6SGleb Smirnoff 	}
480772e66a6SGleb Smirnoff 	IFQ_INC_LEN(ifq);
481772e66a6SGleb Smirnoff 
482772e66a6SGleb Smirnoff 	/* successfully queued. */
483772e66a6SGleb Smirnoff 	return (0);
484772e66a6SGleb Smirnoff }
485772e66a6SGleb Smirnoff 
486772e66a6SGleb Smirnoff /*
487772e66a6SGleb Smirnoff  * priq_dequeue is a dequeue function to be registered to
488772e66a6SGleb Smirnoff  * (*altq_dequeue) in struct ifaltq.
489772e66a6SGleb Smirnoff  *
490772e66a6SGleb Smirnoff  * note: ALTDQ_POLL returns the next packet without removing the packet
491772e66a6SGleb Smirnoff  *	from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
492772e66a6SGleb Smirnoff  *	ALTDQ_REMOVE must return the same packet if called immediately
493772e66a6SGleb Smirnoff  *	after ALTDQ_POLL.
494772e66a6SGleb Smirnoff  */
495772e66a6SGleb Smirnoff static struct mbuf *
priq_dequeue(struct ifaltq * ifq,int op)496772e66a6SGleb Smirnoff priq_dequeue(struct ifaltq *ifq, int op)
497772e66a6SGleb Smirnoff {
498772e66a6SGleb Smirnoff 	struct priq_if	*pif = (struct priq_if *)ifq->altq_disc;
499772e66a6SGleb Smirnoff 	struct priq_class *cl;
500772e66a6SGleb Smirnoff 	struct mbuf *m;
501772e66a6SGleb Smirnoff 	int pri;
502772e66a6SGleb Smirnoff 
503772e66a6SGleb Smirnoff 	IFQ_LOCK_ASSERT(ifq);
504772e66a6SGleb Smirnoff 
505772e66a6SGleb Smirnoff 	if (IFQ_IS_EMPTY(ifq))
506772e66a6SGleb Smirnoff 		/* no packet in the queue */
507772e66a6SGleb Smirnoff 		return (NULL);
508772e66a6SGleb Smirnoff 
509772e66a6SGleb Smirnoff 	for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
510772e66a6SGleb Smirnoff 		if ((cl = pif->pif_classes[pri]) != NULL &&
511772e66a6SGleb Smirnoff 		    !qempty(cl->cl_q)) {
512772e66a6SGleb Smirnoff 			if (op == ALTDQ_POLL)
513772e66a6SGleb Smirnoff 				return (priq_pollq(cl));
514772e66a6SGleb Smirnoff 
515772e66a6SGleb Smirnoff 			m = priq_getq(cl);
516772e66a6SGleb Smirnoff 			if (m != NULL) {
517772e66a6SGleb Smirnoff 				IFQ_DEC_LEN(ifq);
518772e66a6SGleb Smirnoff 				if (qempty(cl->cl_q))
519772e66a6SGleb Smirnoff 					cl->cl_period++;
520772e66a6SGleb Smirnoff 				PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
521772e66a6SGleb Smirnoff 			}
522772e66a6SGleb Smirnoff 			return (m);
523772e66a6SGleb Smirnoff 		}
524772e66a6SGleb Smirnoff 	}
525772e66a6SGleb Smirnoff 	return (NULL);
526772e66a6SGleb Smirnoff }
527772e66a6SGleb Smirnoff 
528772e66a6SGleb Smirnoff static int
priq_addq(struct priq_class * cl,struct mbuf * m)529772e66a6SGleb Smirnoff priq_addq(struct priq_class *cl, struct mbuf *m)
530772e66a6SGleb Smirnoff {
531772e66a6SGleb Smirnoff 
532772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
533772e66a6SGleb Smirnoff 	if (q_is_rio(cl->cl_q))
534772e66a6SGleb Smirnoff 		return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
535772e66a6SGleb Smirnoff 				cl->cl_pktattr);
536772e66a6SGleb Smirnoff #endif
537772e66a6SGleb Smirnoff #ifdef ALTQ_RED
538772e66a6SGleb Smirnoff 	if (q_is_red(cl->cl_q))
539772e66a6SGleb Smirnoff 		return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
540772e66a6SGleb Smirnoff #endif
5410a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
5420a70aaf8SLuiz Otavio O Souza 	if (q_is_codel(cl->cl_q))
5430a70aaf8SLuiz Otavio O Souza 		return codel_addq(cl->cl_codel, cl->cl_q, m);
5440a70aaf8SLuiz Otavio O Souza #endif
545772e66a6SGleb Smirnoff 	if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
546772e66a6SGleb Smirnoff 		m_freem(m);
547772e66a6SGleb Smirnoff 		return (-1);
548772e66a6SGleb Smirnoff 	}
549772e66a6SGleb Smirnoff 
550772e66a6SGleb Smirnoff 	if (cl->cl_flags & PRCF_CLEARDSCP)
551772e66a6SGleb Smirnoff 		write_dsfield(m, cl->cl_pktattr, 0);
552772e66a6SGleb Smirnoff 
553772e66a6SGleb Smirnoff 	_addq(cl->cl_q, m);
554772e66a6SGleb Smirnoff 
555772e66a6SGleb Smirnoff 	return (0);
556772e66a6SGleb Smirnoff }
557772e66a6SGleb Smirnoff 
558772e66a6SGleb Smirnoff static struct mbuf *
priq_getq(struct priq_class * cl)559772e66a6SGleb Smirnoff priq_getq(struct priq_class *cl)
560772e66a6SGleb Smirnoff {
561772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
562772e66a6SGleb Smirnoff 	if (q_is_rio(cl->cl_q))
563772e66a6SGleb Smirnoff 		return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
564772e66a6SGleb Smirnoff #endif
565772e66a6SGleb Smirnoff #ifdef ALTQ_RED
566772e66a6SGleb Smirnoff 	if (q_is_red(cl->cl_q))
567772e66a6SGleb Smirnoff 		return red_getq(cl->cl_red, cl->cl_q);
568772e66a6SGleb Smirnoff #endif
5690a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
5700a70aaf8SLuiz Otavio O Souza 	if (q_is_codel(cl->cl_q))
5710a70aaf8SLuiz Otavio O Souza 		return codel_getq(cl->cl_codel, cl->cl_q);
5720a70aaf8SLuiz Otavio O Souza #endif
573772e66a6SGleb Smirnoff 	return _getq(cl->cl_q);
574772e66a6SGleb Smirnoff }
575772e66a6SGleb Smirnoff 
576772e66a6SGleb Smirnoff static struct mbuf *
priq_pollq(struct priq_class * cl)577*c492eb60SMateusz Guzik priq_pollq(struct priq_class *cl)
578772e66a6SGleb Smirnoff {
579772e66a6SGleb Smirnoff 	return qhead(cl->cl_q);
580772e66a6SGleb Smirnoff }
581772e66a6SGleb Smirnoff 
582772e66a6SGleb Smirnoff static void
priq_purgeq(struct priq_class * cl)583772e66a6SGleb Smirnoff priq_purgeq(struct priq_class *cl)
584772e66a6SGleb Smirnoff {
585772e66a6SGleb Smirnoff 	struct mbuf *m;
586772e66a6SGleb Smirnoff 
587772e66a6SGleb Smirnoff 	if (qempty(cl->cl_q))
588772e66a6SGleb Smirnoff 		return;
589772e66a6SGleb Smirnoff 
590772e66a6SGleb Smirnoff 	while ((m = _getq(cl->cl_q)) != NULL) {
591772e66a6SGleb Smirnoff 		PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
592772e66a6SGleb Smirnoff 		m_freem(m);
593772e66a6SGleb Smirnoff 	}
594772e66a6SGleb Smirnoff 	ASSERT(qlen(cl->cl_q) == 0);
595772e66a6SGleb Smirnoff }
596772e66a6SGleb Smirnoff 
597772e66a6SGleb Smirnoff static void
get_class_stats(struct priq_classstats * sp,struct priq_class * cl)598772e66a6SGleb Smirnoff get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
599772e66a6SGleb Smirnoff {
600772e66a6SGleb Smirnoff 	sp->class_handle = cl->cl_handle;
601772e66a6SGleb Smirnoff 	sp->qlength = qlen(cl->cl_q);
602772e66a6SGleb Smirnoff 	sp->qlimit = qlimit(cl->cl_q);
603772e66a6SGleb Smirnoff 	sp->period = cl->cl_period;
604772e66a6SGleb Smirnoff 	sp->xmitcnt = cl->cl_xmitcnt;
605772e66a6SGleb Smirnoff 	sp->dropcnt = cl->cl_dropcnt;
606772e66a6SGleb Smirnoff 
607772e66a6SGleb Smirnoff 	sp->qtype = qtype(cl->cl_q);
608772e66a6SGleb Smirnoff #ifdef ALTQ_RED
609772e66a6SGleb Smirnoff 	if (q_is_red(cl->cl_q))
610772e66a6SGleb Smirnoff 		red_getstats(cl->cl_red, &sp->red[0]);
611772e66a6SGleb Smirnoff #endif
612772e66a6SGleb Smirnoff #ifdef ALTQ_RIO
613772e66a6SGleb Smirnoff 	if (q_is_rio(cl->cl_q))
614772e66a6SGleb Smirnoff 		rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
615772e66a6SGleb Smirnoff #endif
6160a70aaf8SLuiz Otavio O Souza #ifdef ALTQ_CODEL
6170a70aaf8SLuiz Otavio O Souza 	if (q_is_codel(cl->cl_q))
6180a70aaf8SLuiz Otavio O Souza 		codel_getstats(cl->cl_codel, &sp->codel);
6190a70aaf8SLuiz Otavio O Souza #endif
620772e66a6SGleb Smirnoff }
621772e66a6SGleb Smirnoff 
622772e66a6SGleb Smirnoff /* convert a class handle to the corresponding class pointer */
623772e66a6SGleb Smirnoff static struct priq_class *
clh_to_clp(struct priq_if * pif,u_int32_t chandle)624772e66a6SGleb Smirnoff clh_to_clp(struct priq_if *pif, u_int32_t chandle)
625772e66a6SGleb Smirnoff {
626772e66a6SGleb Smirnoff 	struct priq_class *cl;
627772e66a6SGleb Smirnoff 	int idx;
628772e66a6SGleb Smirnoff 
629772e66a6SGleb Smirnoff 	if (chandle == 0)
630772e66a6SGleb Smirnoff 		return (NULL);
631772e66a6SGleb Smirnoff 
632772e66a6SGleb Smirnoff 	for (idx = pif->pif_maxpri; idx >= 0; idx--)
633772e66a6SGleb Smirnoff 		if ((cl = pif->pif_classes[idx]) != NULL &&
634772e66a6SGleb Smirnoff 		    cl->cl_handle == chandle)
635772e66a6SGleb Smirnoff 			return (cl);
636772e66a6SGleb Smirnoff 
637772e66a6SGleb Smirnoff 	return (NULL);
638772e66a6SGleb Smirnoff }
639772e66a6SGleb Smirnoff 
640772e66a6SGleb Smirnoff #endif /* ALTQ_PRIQ */
641