xref: /dragonfly/sys/net/altq/altq_priq.c (revision 0ca59c34)
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.9 2008/05/14 11:59:23 sephe 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 #include <sys/thread.h>
50 
51 #include <net/if.h>
52 #include <net/ifq_var.h>
53 #include <netinet/in.h>
54 
55 #include <net/pf/pfvar.h>
56 #include <net/altq/altq.h>
57 #include <net/altq/altq_priq.h>
58 
59 #include <sys/thread2.h>
60 
61 #define PRIQ_SUBQ_INDEX		ALTQ_SUBQ_INDEX_DEFAULT
62 #define PRIQ_LOCK(ifq) \
63     ALTQ_SQ_LOCK(&(ifq)->altq_subq[PRIQ_SUBQ_INDEX])
64 #define PRIQ_UNLOCK(ifq) \
65     ALTQ_SQ_UNLOCK(&(ifq)->altq_subq[PRIQ_SUBQ_INDEX])
66 
67 /*
68  * function prototypes
69  */
70 static int	priq_clear_interface(struct priq_if *);
71 static int	priq_request(struct ifaltq_subque *, int, void *);
72 static void	priq_purge(struct priq_if *);
73 static struct priq_class *priq_class_create(struct priq_if *, int, int, int, int);
74 static int	priq_class_destroy(struct priq_class *);
75 static int	priq_enqueue(struct ifaltq_subque *, struct mbuf *,
76 		    struct altq_pktattr *);
77 static struct mbuf *priq_dequeue(struct ifaltq_subque *, int);
78 
79 static int	priq_addq(struct priq_class *, struct mbuf *);
80 static struct mbuf *priq_getq(struct priq_class *);
81 static struct mbuf *priq_pollq(struct priq_class *);
82 static void	priq_purgeq(struct priq_class *);
83 
84 static void	get_class_stats(struct priq_classstats *, struct priq_class *);
85 static struct priq_class *clh_to_clp(struct priq_if *, uint32_t);
86 
87 int
88 priq_pfattach(struct pf_altq *a, struct ifaltq *ifq)
89 {
90 	return altq_attach(ifq, ALTQT_PRIQ, a->altq_disc, ifq_mapsubq_default,
91 	    priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
92 }
93 
94 int
95 priq_add_altq(struct pf_altq *a)
96 {
97 	struct priq_if *pif;
98 	struct ifnet *ifp;
99 
100 	ifnet_lock();
101 
102 	if ((ifp = ifunit(a->ifname)) == NULL) {
103 		ifnet_unlock();
104 		return (EINVAL);
105 	}
106 	if (!ifq_is_ready(&ifp->if_snd)) {
107 		ifnet_unlock();
108 		return (ENODEV);
109 	}
110 
111 	pif = kmalloc(sizeof(*pif), M_ALTQ, M_WAITOK | M_ZERO);
112 	pif->pif_bandwidth = a->ifbandwidth;
113 	pif->pif_maxpri = -1;
114 	pif->pif_ifq = &ifp->if_snd;
115 	ifq_purge_all(&ifp->if_snd);
116 
117 	ifnet_unlock();
118 
119 	/* keep the state in pf_altq */
120 	a->altq_disc = pif;
121 
122 	return (0);
123 }
124 
125 int
126 priq_remove_altq(struct pf_altq *a)
127 {
128 	struct priq_if *pif;
129 
130 	if ((pif = a->altq_disc) == NULL)
131 		return (EINVAL);
132 	a->altq_disc = NULL;
133 
134 	priq_clear_interface(pif);
135 
136 	kfree(pif, M_ALTQ);
137 	return (0);
138 }
139 
140 static int
141 priq_add_queue_locked(struct pf_altq *a, struct priq_if *pif)
142 {
143 	struct priq_class *cl;
144 
145 	KKASSERT(a->priority < PRIQ_MAXPRI);
146 	KKASSERT(a->qid != 0);
147 
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_add_queue(struct pf_altq *a)
163 {
164 	struct priq_if *pif;
165 	struct ifaltq *ifq;
166 	int error;
167 
168 	/* check parameters */
169 	if (a->priority >= PRIQ_MAXPRI)
170 		return (EINVAL);
171 	if (a->qid == 0)
172 		return (EINVAL);
173 
174 	/* XXX not MP safe */
175 	if ((pif = a->altq_disc) == NULL)
176 		return (EINVAL);
177 	ifq = pif->pif_ifq;
178 
179 	PRIQ_LOCK(ifq);
180 	error = priq_add_queue_locked(a, pif);
181 	PRIQ_UNLOCK(ifq);
182 
183 	return error;
184 }
185 
186 static int
187 priq_remove_queue_locked(struct pf_altq *a, struct priq_if *pif)
188 {
189 	struct priq_class *cl;
190 
191 	if ((cl = clh_to_clp(pif, a->qid)) == NULL)
192 		return (EINVAL);
193 
194 	return (priq_class_destroy(cl));
195 }
196 
197 int
198 priq_remove_queue(struct pf_altq *a)
199 {
200 	struct priq_if *pif;
201 	struct ifaltq *ifq;
202 	int error;
203 
204 	/* XXX not MF safe */
205 	if ((pif = a->altq_disc) == NULL)
206 		return (EINVAL);
207 	ifq = pif->pif_ifq;
208 
209 	PRIQ_LOCK(ifq);
210 	error = priq_remove_queue_locked(a, pif);
211 	PRIQ_UNLOCK(ifq);
212 
213 	return error;
214 }
215 
216 int
217 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
218 {
219 	struct priq_if *pif;
220 	struct priq_class *cl;
221 	struct priq_classstats stats;
222 	struct ifaltq *ifq;
223 	int error = 0;
224 
225 	if (*nbytes < sizeof(stats))
226 		return (EINVAL);
227 
228 	ifnet_lock();
229 
230 	/* XXX not MP safe */
231 	if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL) {
232 		ifnet_unlock();
233 		return (EBADF);
234 	}
235 	ifq = pif->pif_ifq;
236 
237 	PRIQ_LOCK(ifq);
238 
239 	if ((cl = clh_to_clp(pif, a->qid)) == NULL) {
240 		PRIQ_UNLOCK(ifq);
241 		ifnet_unlock();
242 		return (EINVAL);
243 	}
244 
245 	get_class_stats(&stats, cl);
246 
247 	PRIQ_UNLOCK(ifq);
248 
249 	ifnet_unlock();
250 
251 	if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
252 		return (error);
253 	*nbytes = sizeof(stats);
254 	return (0);
255 }
256 
257 /*
258  * bring the interface back to the initial state by discarding
259  * all the filters and classes.
260  */
261 static int
262 priq_clear_interface(struct priq_if *pif)
263 {
264 	struct priq_class *cl;
265 	int pri;
266 
267 	/* clear out the classes */
268 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
269 		if ((cl = pif->pif_classes[pri]) != NULL)
270 			priq_class_destroy(cl);
271 	}
272 
273 	return (0);
274 }
275 
276 static int
277 priq_request(struct ifaltq_subque *ifsq, int req, void *arg)
278 {
279 	struct ifaltq *ifq = ifsq->ifsq_altq;
280 	struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
281 
282 	crit_enter();
283 	switch (req) {
284 	case ALTRQ_PURGE:
285 		if (ifsq_get_index(ifsq) == PRIQ_SUBQ_INDEX) {
286 			priq_purge(pif);
287 		} else {
288 			/*
289 			 * Race happened, the unrelated subqueue was
290 			 * picked during the packet scheduler transition.
291 			 */
292 			ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
293 		}
294 		break;
295 	}
296 	crit_exit();
297 	return (0);
298 }
299 
300 /* discard all the queued packets on the interface */
301 static void
302 priq_purge(struct priq_if *pif)
303 {
304 	struct priq_class *cl;
305 	int pri;
306 
307 	for (pri = 0; pri <= pif->pif_maxpri; pri++) {
308 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
309 			priq_purgeq(cl);
310 	}
311 	if (ifq_is_enabled(pif->pif_ifq))
312 		ALTQ_SQ_CNTR_RESET(&pif->pif_ifq->altq_subq[PRIQ_SUBQ_INDEX]);
313 }
314 
315 static struct priq_class *
316 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
317 {
318 	struct priq_class *cl;
319 
320 #ifndef ALTQ_RED
321 	if (flags & PRCF_RED) {
322 #ifdef ALTQ_DEBUG
323 		kprintf("priq_class_create: RED not configured for PRIQ!\n");
324 #endif
325 		return (NULL);
326 	}
327 #endif
328 
329 	if ((cl = pif->pif_classes[pri]) != NULL) {
330 		/* modify the class instead of creating a new one */
331 		crit_enter();
332 		if (!qempty(cl->cl_q))
333 			priq_purgeq(cl);
334 		crit_exit();
335 #ifdef ALTQ_RIO
336 		if (q_is_rio(cl->cl_q))
337 			rio_destroy((rio_t *)cl->cl_red);
338 #endif
339 #ifdef ALTQ_RED
340 		if (q_is_red(cl->cl_q))
341 			red_destroy(cl->cl_red);
342 #endif
343 	} else {
344 		cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
345 		cl->cl_q = kmalloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO);
346 	}
347 
348 	pif->pif_classes[pri] = cl;
349 	if (flags & PRCF_DEFAULTCLASS)
350 		pif->pif_default = cl;
351 	if (qlimit == 0)
352 		qlimit = 50;  /* use default */
353 	qlimit(cl->cl_q) = qlimit;
354 	qtype(cl->cl_q) = Q_DROPTAIL;
355 	qlen(cl->cl_q) = 0;
356 	cl->cl_flags = flags;
357 	cl->cl_pri = pri;
358 	if (pri > pif->pif_maxpri)
359 		pif->pif_maxpri = pri;
360 	cl->cl_pif = pif;
361 	cl->cl_handle = qid;
362 
363 #ifdef ALTQ_RED
364 	if (flags & (PRCF_RED|PRCF_RIO)) {
365 		int red_flags, red_pkttime;
366 
367 		red_flags = 0;
368 		if (flags & PRCF_ECN)
369 			red_flags |= REDF_ECN;
370 #ifdef ALTQ_RIO
371 		if (flags & PRCF_CLEARDSCP)
372 			red_flags |= RIOF_CLEARDSCP;
373 #endif
374 		if (pif->pif_bandwidth < 8)
375 			red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
376 		else
377 			red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
378 			  * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
379 #ifdef ALTQ_RIO
380 		if (flags & PRCF_RIO) {
381 			cl->cl_red = (red_t *)rio_alloc(0, NULL,
382 						red_flags, red_pkttime);
383 			if (cl->cl_red != NULL)
384 				qtype(cl->cl_q) = Q_RIO;
385 		} else
386 #endif
387 		if (flags & PRCF_RED) {
388 			cl->cl_red = red_alloc(0, 0,
389 			    qlimit(cl->cl_q) * 10/100,
390 			    qlimit(cl->cl_q) * 30/100,
391 			    red_flags, red_pkttime);
392 			if (cl->cl_red != NULL)
393 				qtype(cl->cl_q) = Q_RED;
394 		}
395 	}
396 #endif /* ALTQ_RED */
397 
398 	return (cl);
399 }
400 
401 static int
402 priq_class_destroy(struct priq_class *cl)
403 {
404 	struct priq_if *pif;
405 	int pri;
406 
407 	crit_enter();
408 
409 	if (!qempty(cl->cl_q))
410 		priq_purgeq(cl);
411 
412 	pif = cl->cl_pif;
413 	pif->pif_classes[cl->cl_pri] = NULL;
414 	if (pif->pif_maxpri == cl->cl_pri) {
415 		for (pri = cl->cl_pri; pri >= 0; pri--)
416 			if (pif->pif_classes[pri] != NULL) {
417 				pif->pif_maxpri = pri;
418 				break;
419 			}
420 		if (pri < 0)
421 			pif->pif_maxpri = -1;
422 	}
423 	crit_exit();
424 
425 	if (cl->cl_red != NULL) {
426 #ifdef ALTQ_RIO
427 		if (q_is_rio(cl->cl_q))
428 			rio_destroy((rio_t *)cl->cl_red);
429 #endif
430 #ifdef ALTQ_RED
431 		if (q_is_red(cl->cl_q))
432 			red_destroy(cl->cl_red);
433 #endif
434 	}
435 	kfree(cl->cl_q, M_ALTQ);
436 	kfree(cl, M_ALTQ);
437 	return (0);
438 }
439 
440 /*
441  * priq_enqueue is an enqueue function to be registered to
442  * (*ifsq_enqueue) in struct ifaltq_subque.
443  */
444 static int
445 priq_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
446     struct altq_pktattr *pktattr)
447 {
448 	struct ifaltq *ifq = ifsq->ifsq_altq;
449 	struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
450 	struct priq_class *cl;
451 	int error;
452 	int len;
453 
454 	if (ifsq_get_index(ifsq) != PRIQ_SUBQ_INDEX) {
455 		/*
456 		 * Race happened, the unrelated subqueue was
457 		 * picked during the packet scheduler transition.
458 		 */
459 		ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
460 		m_freem(m);
461 		return ENOBUFS;
462 	}
463 
464 	crit_enter();
465 
466 	/* grab class set by classifier */
467 	M_ASSERTPKTHDR(m);
468 	if (m->m_pkthdr.fw_flags & PF_MBUF_STRUCTURE)
469 		cl = clh_to_clp(pif, m->m_pkthdr.pf.qid);
470 	else
471 		cl = NULL;
472 	if (cl == NULL) {
473 		cl = pif->pif_default;
474 		if (cl == NULL) {
475 			m_freem(m);
476 			error = ENOBUFS;
477 			goto done;
478 		}
479 	}
480 	cl->cl_pktattr = NULL;
481 	len = m_pktlen(m);
482 	if (priq_addq(cl, m) != 0) {
483 		/* drop occurred.  mbuf was freed in priq_addq. */
484 		PKTCNTR_ADD(&cl->cl_dropcnt, len);
485 		error = ENOBUFS;
486 		goto done;
487 	}
488 	ALTQ_SQ_PKTCNT_INC(ifsq);
489 	error = 0;
490 done:
491 	crit_exit();
492 	return (error);
493 }
494 
495 /*
496  * priq_dequeue is a dequeue function to be registered to
497  * (*ifsq_dequeue) in struct ifaltq_subque.
498  *
499  * note: ALTDQ_POLL returns the next packet without removing the packet
500  *	from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
501  */
502 static struct mbuf *
503 priq_dequeue(struct ifaltq_subque *ifsq, int op)
504 {
505 	struct ifaltq *ifq = ifsq->ifsq_altq;
506 	struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
507 	struct priq_class *cl;
508 	struct mbuf *m;
509 	int pri;
510 
511 	if (ifsq_get_index(ifsq) != PRIQ_SUBQ_INDEX) {
512 		/*
513 		 * Race happened, the unrelated subqueue was
514 		 * picked during the packet scheduler transition.
515 		 */
516 		ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
517 		return NULL;
518 	}
519 
520 	if (ifsq_is_empty(ifsq)) {
521 		/* no packet in the queue */
522 		return (NULL);
523 	}
524 
525 	crit_enter();
526 	m = NULL;
527 	for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
528 		if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) {
529 			if (op == ALTDQ_POLL) {
530 				m = priq_pollq(cl);
531 				break;
532 			}
533 
534 			m = priq_getq(cl);
535 			if (m != NULL) {
536 				ALTQ_SQ_PKTCNT_DEC(ifsq);
537 				if (qempty(cl->cl_q))
538 					cl->cl_period++;
539 				PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
540 			}
541 			break;
542 		}
543 	}
544 	crit_exit();
545 	return (m);
546 }
547 
548 static int
549 priq_addq(struct priq_class *cl, struct mbuf *m)
550 {
551 #ifdef ALTQ_RIO
552 	if (q_is_rio(cl->cl_q))
553 		return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
554 				cl->cl_pktattr);
555 #endif
556 #ifdef ALTQ_RED
557 	if (q_is_red(cl->cl_q))
558 		return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
559 #endif
560 	if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
561 		m_freem(m);
562 		return (-1);
563 	}
564 
565 	if (cl->cl_flags & PRCF_CLEARDSCP)
566 		write_dsfield(m, cl->cl_pktattr, 0);
567 
568 	_addq(cl->cl_q, m);
569 
570 	return (0);
571 }
572 
573 static struct mbuf *
574 priq_getq(struct priq_class *cl)
575 {
576 #ifdef ALTQ_RIO
577 	if (q_is_rio(cl->cl_q))
578 		return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
579 #endif
580 #ifdef ALTQ_RED
581 	if (q_is_red(cl->cl_q))
582 		return red_getq(cl->cl_red, cl->cl_q);
583 #endif
584 	return _getq(cl->cl_q);
585 }
586 
587 static struct mbuf *
588 priq_pollq(struct priq_class *cl)
589 {
590 	return qhead(cl->cl_q);
591 }
592 
593 static void
594 priq_purgeq(struct priq_class *cl)
595 {
596 	struct mbuf *m;
597 
598 	if (qempty(cl->cl_q))
599 		return;
600 
601 	while ((m = _getq(cl->cl_q)) != NULL) {
602 		PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
603 		m_freem(m);
604 	}
605 	KKASSERT(qlen(cl->cl_q) == 0);
606 }
607 
608 static void
609 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
610 {
611 	sp->class_handle = cl->cl_handle;
612 	sp->qlength = qlen(cl->cl_q);
613 	sp->qlimit = qlimit(cl->cl_q);
614 	sp->period = cl->cl_period;
615 	sp->xmitcnt = cl->cl_xmitcnt;
616 	sp->dropcnt = cl->cl_dropcnt;
617 
618 	sp->qtype = qtype(cl->cl_q);
619 #ifdef ALTQ_RED
620 	if (q_is_red(cl->cl_q))
621 		red_getstats(cl->cl_red, &sp->red[0]);
622 #endif
623 #ifdef ALTQ_RIO
624 	if (q_is_rio(cl->cl_q))
625 		rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
626 #endif
627 }
628 
629 /* convert a class handle to the corresponding class pointer */
630 static struct priq_class *
631 clh_to_clp(struct priq_if *pif, uint32_t chandle)
632 {
633 	struct priq_class *cl;
634 	int idx;
635 
636 	if (chandle == 0)
637 		return (NULL);
638 
639 	for (idx = pif->pif_maxpri; idx >= 0; idx--)
640 		if ((cl = pif->pif_classes[idx]) != NULL &&
641 		    cl->cl_handle == chandle)
642 			return (cl);
643 
644 	return (NULL);
645 }
646 
647 #endif /* ALTQ_PRIQ */
648