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