xref: /dragonfly/sys/net/altq/altq_priq.c (revision 222a27c4)
1 /*	$KAME: altq_priq.c,v 1.12 2004/04/17 10:54:48 kjc Exp $	*/
2 /*	$DragonFly: src/sys/net/altq/altq_priq.c,v 1.1 2005/02/11 22:25:57 joerg Exp $ */
3 
4 /*
5  * Copyright (C) 2000-2003
6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*
30  * priority queue
31  */
32 
33 #include "opt_altq.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 
37 #ifdef ALTQ_PRIQ  /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
38 
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 
50 #include <net/if.h>
51 #include <net/ifq_var.h>
52 #include <netinet/in.h>
53 
54 #include <net/pf/pfvar.h>
55 #include <net/altq/altq.h>
56 #include <net/altq/altq_priq.h>
57 
58 /*
59  * function prototypes
60  */
61 static int	priq_clear_interface(struct priq_if *);
62 static int	priq_request(struct ifaltq *, int, void *);
63 static void	priq_purge(struct priq_if *);
64 static struct priq_class *priq_class_create(struct priq_if *, int, int, int, int);
65 static int	priq_class_destroy(struct priq_class *);
66 static int	priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
67 static struct mbuf *priq_dequeue(struct ifaltq *, int);
68 
69 static int	priq_addq(struct priq_class *, struct mbuf *);
70 static struct mbuf *priq_getq(struct priq_class *);
71 static struct mbuf *priq_pollq(struct priq_class *);
72 static void	priq_purgeq(struct priq_class *);
73 
74 static void	get_class_stats(struct priq_classstats *, struct priq_class *);
75 static struct priq_class *clh_to_clp(struct priq_if *, uint32_t);
76 
77 int
78 priq_pfattach(struct pf_altq *a)
79 {
80 	struct ifnet *ifp;
81 	int s, error;
82 
83 	if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
84 		return (EINVAL);
85 	s = splimp();
86 	error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
87 	    priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
88 	splx(s);
89 	return (error);
90 }
91 
92 int
93 priq_add_altq(struct pf_altq *a)
94 {
95 	struct priq_if *pif;
96 	struct ifnet *ifp;
97 
98 	if ((ifp = ifunit(a->ifname)) == NULL)
99 		return (EINVAL);
100 	if (!ifq_is_ready(&ifp->if_snd))
101 		return (ENODEV);
102 
103 	pif = malloc(sizeof(*pif), M_ALTQ, M_WAITOK | M_ZERO);
104 	pif->pif_bandwidth = a->ifbandwidth;
105 	pif->pif_maxpri = -1;
106 	pif->pif_ifq = &ifp->if_snd;
107 
108 	/* keep the state in pf_altq */
109 	a->altq_disc = pif;
110 
111 	return (0);
112 }
113 
114 int
115 priq_remove_altq(struct pf_altq *a)
116 {
117 	struct priq_if *pif;
118 
119 	if ((pif = a->altq_disc) == NULL)
120 		return (EINVAL);
121 	a->altq_disc = NULL;
122 
123 	priq_clear_interface(pif);
124 
125 	free(pif, M_ALTQ);
126 	return (0);
127 }
128 
129 int
130 priq_add_queue(struct pf_altq *a)
131 {
132 	struct priq_if *pif;
133 	struct priq_class *cl;
134 
135 	if ((pif = a->altq_disc) == NULL)
136 		return (EINVAL);
137 
138 	/* check parameters */
139 	if (a->priority >= PRIQ_MAXPRI)
140 		return (EINVAL);
141 	if (a->qid == 0)
142 		return (EINVAL);
143 	if (pif->pif_classes[a->priority] != NULL)
144 		return (EBUSY);
145 	if (clh_to_clp(pif, a->qid) != NULL)
146 		return (EBUSY);
147 
148 	cl = priq_class_create(pif, a->priority, a->qlimit,
149 			       a->pq_u.priq_opts.flags, a->qid);
150 	if (cl == NULL)
151 		return (ENOMEM);
152 
153 	return (0);
154 }
155 
156 int
157 priq_remove_queue(struct pf_altq *a)
158 {
159 	struct priq_if *pif;
160 	struct priq_class *cl;
161 
162 	if ((pif = a->altq_disc) == NULL)
163 		return (EINVAL);
164 
165 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
166 		return (EINVAL);
167 
168 	return (priq_class_destroy(cl));
169 }
170 
171 int
172 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
173 {
174 	struct priq_if *pif;
175 	struct priq_class *cl;
176 	struct priq_classstats stats;
177 	int error = 0;
178 
179 	if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
180 		return (EBADF);
181 
182 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
183 		return (EINVAL);
184 
185 	if (*nbytes < sizeof(stats))
186 		return (EINVAL);
187 
188 	get_class_stats(&stats, cl);
189 
190 	if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
191 		return (error);
192 	*nbytes = sizeof(stats);
193 	return (0);
194 }
195 
196 /*
197  * bring the interface back to the initial state by discarding
198  * all the filters and classes.
199  */
200 static int
201 priq_clear_interface(struct priq_if *pif)
202 {
203 	struct priq_class *cl;
204 	int pri;
205 
206 	/* clear out the classes */
207 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
208 		if ((cl = pif->pif_classes[pri]) != NULL)
209 			priq_class_destroy(cl);
210 	}
211 
212 	return (0);
213 }
214 
215 static int
216 priq_request(struct ifaltq *ifq, int req, void *arg)
217 {
218 	struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
219 
220 	switch (req) {
221 	case ALTRQ_PURGE:
222 		priq_purge(pif);
223 		break;
224 	}
225 	return (0);
226 }
227 
228 /* discard all the queued packets on the interface */
229 static void
230 priq_purge(struct priq_if *pif)
231 {
232 	struct priq_class *cl;
233 	int pri;
234 
235 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
236 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
237 			priq_purgeq(cl);
238 	}
239 	if (ifq_is_enabled(pif->pif_ifq))
240 		pif->pif_ifq->ifq_len = 0;
241 }
242 
243 static struct priq_class *
244 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
245 {
246 	struct priq_class *cl;
247 	int s;
248 
249 #ifndef ALTQ_RED
250 	if (flags & PRCF_RED) {
251 #ifdef ALTQ_DEBUG
252 		printf("priq_class_create: RED not configured for PRIQ!\n");
253 #endif
254 		return (NULL);
255 	}
256 #endif
257 
258 	if ((cl = pif->pif_classes[pri]) != NULL) {
259 		/* modify the class instead of creating a new one */
260 		s = splimp();
261 		if (!qempty(cl->cl_q))
262 			priq_purgeq(cl);
263 		splx(s);
264 #ifdef ALTQ_RIO
265 		if (q_is_rio(cl->cl_q))
266 			rio_destroy((rio_t *)cl->cl_red);
267 #endif
268 #ifdef ALTQ_RED
269 		if (q_is_red(cl->cl_q))
270 			red_destroy(cl->cl_red);
271 #endif
272 	} else {
273 		cl = malloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
274 		cl->cl_q = malloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO);
275 	}
276 
277 	pif->pif_classes[pri] = cl;
278 	if (flags & PRCF_DEFAULTCLASS)
279 		pif->pif_default = cl;
280 	if (qlimit == 0)
281 		qlimit = 50;  /* use default */
282 	qlimit(cl->cl_q) = qlimit;
283 	qtype(cl->cl_q) = Q_DROPTAIL;
284 	qlen(cl->cl_q) = 0;
285 	cl->cl_flags = flags;
286 	cl->cl_pri = pri;
287 	if (pri > pif->pif_maxpri)
288 		pif->pif_maxpri = pri;
289 	cl->cl_pif = pif;
290 	cl->cl_handle = qid;
291 
292 #ifdef ALTQ_RED
293 	if (flags & (PRCF_RED|PRCF_RIO)) {
294 		int red_flags, red_pkttime;
295 
296 		red_flags = 0;
297 		if (flags & PRCF_ECN)
298 			red_flags |= REDF_ECN;
299 #ifdef ALTQ_RIO
300 		if (flags & PRCF_CLEARDSCP)
301 			red_flags |= RIOF_CLEARDSCP;
302 #endif
303 		if (pif->pif_bandwidth < 8)
304 			red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
305 		else
306 			red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
307 			  * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
308 #ifdef ALTQ_RIO
309 		if (flags & PRCF_RIO) {
310 			cl->cl_red = (red_t *)rio_alloc(0, NULL,
311 						red_flags, red_pkttime);
312 			if (cl->cl_red != NULL)
313 				qtype(cl->cl_q) = Q_RIO;
314 		} else
315 #endif
316 		if (flags & PRCF_RED) {
317 			cl->cl_red = red_alloc(0, 0,
318 			    qlimit(cl->cl_q) * 10/100,
319 			    qlimit(cl->cl_q) * 30/100,
320 			    red_flags, red_pkttime);
321 			if (cl->cl_red != NULL)
322 				qtype(cl->cl_q) = Q_RED;
323 		}
324 	}
325 #endif /* ALTQ_RED */
326 
327 	return (cl);
328 }
329 
330 static int
331 priq_class_destroy(struct priq_class *cl)
332 {
333 	struct priq_if *pif;
334 	int s, pri;
335 
336 	s = splimp();
337 
338 	if (!qempty(cl->cl_q))
339 		priq_purgeq(cl);
340 
341 	pif = cl->cl_pif;
342 	pif->pif_classes[cl->cl_pri] = NULL;
343 	if (pif->pif_maxpri == cl->cl_pri) {
344 		for (pri = cl->cl_pri; pri >= 0; pri--)
345 			if (pif->pif_classes[pri] != NULL) {
346 				pif->pif_maxpri = pri;
347 				break;
348 			}
349 		if (pri < 0)
350 			pif->pif_maxpri = -1;
351 	}
352 	splx(s);
353 
354 	if (cl->cl_red != NULL) {
355 #ifdef ALTQ_RIO
356 		if (q_is_rio(cl->cl_q))
357 			rio_destroy((rio_t *)cl->cl_red);
358 #endif
359 #ifdef ALTQ_RED
360 		if (q_is_red(cl->cl_q))
361 			red_destroy(cl->cl_red);
362 #endif
363 	}
364 	free(cl->cl_q, M_ALTQ);
365 	free(cl, M_ALTQ);
366 	return (0);
367 }
368 
369 /*
370  * priq_enqueue is an enqueue function to be registered to
371  * (*altq_enqueue) in struct ifaltq.
372  */
373 static int
374 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
375 {
376 	struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
377 	struct priq_class *cl;
378 	int len;
379 
380 	/* grab class set by classifier */
381 	if ((m->m_flags & M_PKTHDR) == 0) {
382 		/* should not happen */
383 		if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n");
384 		m_freem(m);
385 		return (ENOBUFS);
386 	}
387 	if (m->m_pkthdr.fw_flags & ALTQ_MBUF_TAGGED)
388 		cl = clh_to_clp(pif, m->m_pkthdr.altq_qid);
389 	else
390 		cl = NULL;
391 	if (cl == NULL) {
392 		cl = pif->pif_default;
393 		if (cl == NULL) {
394 			m_freem(m);
395 			return (ENOBUFS);
396 		}
397 	}
398 	cl->cl_pktattr = NULL;
399 	len = m_pktlen(m);
400 	if (priq_addq(cl, m) != 0) {
401 		/* drop occurred.  mbuf was freed in priq_addq. */
402 		PKTCNTR_ADD(&cl->cl_dropcnt, len);
403 		return (ENOBUFS);
404 	}
405 	ifq->ifq_len++;
406 
407 	/* successfully queued. */
408 	return (0);
409 }
410 
411 /*
412  * priq_dequeue is a dequeue function to be registered to
413  * (*altq_dequeue) in struct ifaltq.
414  *
415  * note: ALTDQ_POLL returns the next packet without removing the packet
416  *	from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
417  *	ALTDQ_REMOVE must return the same packet if called immediately
418  *	after ALTDQ_POLL.
419  */
420 static struct mbuf *
421 priq_dequeue(struct ifaltq *ifq, int op)
422 {
423 	struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
424 	struct priq_class *cl;
425 	struct mbuf *m;
426 	int pri;
427 
428 	if (ifq_is_empty(ifq)) {
429 		/* no packet in the queue */
430 		return (NULL);
431 	}
432 
433 	for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
434 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) {
435 			if (op == ALTDQ_POLL)
436 				return (priq_pollq(cl));
437 
438 			m = priq_getq(cl);
439 			if (m != NULL) {
440 				ifq->ifq_len--;
441 				if (qempty(cl->cl_q))
442 					cl->cl_period++;
443 				PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
444 			}
445 			return (m);
446 		}
447 	}
448 	return (NULL);
449 }
450 
451 static int
452 priq_addq(struct priq_class *cl, struct mbuf *m)
453 {
454 #ifdef ALTQ_RIO
455 	if (q_is_rio(cl->cl_q))
456 		return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
457 				cl->cl_pktattr);
458 #endif
459 #ifdef ALTQ_RED
460 	if (q_is_red(cl->cl_q))
461 		return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
462 #endif
463 	if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
464 		m_freem(m);
465 		return (-1);
466 	}
467 
468 	if (cl->cl_flags & PRCF_CLEARDSCP)
469 		write_dsfield(m, cl->cl_pktattr, 0);
470 
471 	_addq(cl->cl_q, m);
472 
473 	return (0);
474 }
475 
476 static struct mbuf *
477 priq_getq(struct priq_class *cl)
478 {
479 #ifdef ALTQ_RIO
480 	if (q_is_rio(cl->cl_q))
481 		return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
482 #endif
483 #ifdef ALTQ_RED
484 	if (q_is_red(cl->cl_q))
485 		return red_getq(cl->cl_red, cl->cl_q);
486 #endif
487 	return _getq(cl->cl_q);
488 }
489 
490 static struct mbuf *
491 priq_pollq(struct priq_class *cl)
492 {
493 	return qhead(cl->cl_q);
494 }
495 
496 static void
497 priq_purgeq(struct priq_class *cl)
498 {
499 	struct mbuf *m;
500 
501 	if (qempty(cl->cl_q))
502 		return;
503 
504 	while ((m = _getq(cl->cl_q)) != NULL) {
505 		PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
506 		m_freem(m);
507 	}
508 	KKASSERT(qlen(cl->cl_q) == 0);
509 }
510 
511 static void
512 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
513 {
514 	sp->class_handle = cl->cl_handle;
515 	sp->qlength = qlen(cl->cl_q);
516 	sp->qlimit = qlimit(cl->cl_q);
517 	sp->period = cl->cl_period;
518 	sp->xmitcnt = cl->cl_xmitcnt;
519 	sp->dropcnt = cl->cl_dropcnt;
520 
521 	sp->qtype = qtype(cl->cl_q);
522 #ifdef ALTQ_RED
523 	if (q_is_red(cl->cl_q))
524 		red_getstats(cl->cl_red, &sp->red[0]);
525 #endif
526 #ifdef ALTQ_RIO
527 	if (q_is_rio(cl->cl_q))
528 		rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
529 #endif
530 }
531 
532 /* convert a class handle to the corresponding class pointer */
533 static struct priq_class *
534 clh_to_clp(struct priq_if *pif, uint32_t chandle)
535 {
536 	struct priq_class *cl;
537 	int idx;
538 
539 	if (chandle == 0)
540 		return (NULL);
541 
542 	for (idx = pif->pif_maxpri; idx >= 0; idx--)
543 		if ((cl = pif->pif_classes[idx]) != NULL &&
544 		    cl->cl_handle == chandle)
545 			return (cl);
546 
547 	return (NULL);
548 }
549 
550 #endif /* ALTQ_PRIQ */
551