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 $ */
5 * Copyright (C) 2000-2003
6 * Sony Computer Science Laboratories Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 #include "opt_inet6.h"
37 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
39 #include <sys/param.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 #include <sys/thread.h>
52 #include <net/ifq_var.h>
53 #include <netinet/in.h>
55 #include <net/pf/pfvar.h>
56 #include <net/altq/altq.h>
57 #include <net/altq/altq_priq.h>
59 #include <sys/thread2.h>
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])
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 *, struct mbuf *, int);
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 *);
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);
88 priq_pfattach(struct pf_altq *a, struct ifaltq *ifq)
90 return altq_attach(ifq, ALTQT_PRIQ, a->altq_disc, ifq_mapsubq_default,
91 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
95 priq_add_altq(struct pf_altq *a)
100 if ((ifp = ifunit(a->ifname)) == NULL)
102 if (!ifq_is_ready(&ifp->if_snd))
105 pif = kmalloc(sizeof(*pif), M_ALTQ, M_WAITOK | M_ZERO);
106 pif->pif_bandwidth = a->ifbandwidth;
107 pif->pif_maxpri = -1;
108 pif->pif_ifq = &ifp->if_snd;
109 ifq_purge_all(&ifp->if_snd);
111 /* keep the state in pf_altq */
118 priq_remove_altq(struct pf_altq *a)
122 if ((pif = a->altq_disc) == NULL)
126 priq_clear_interface(pif);
133 priq_add_queue_locked(struct pf_altq *a, struct priq_if *pif)
135 struct priq_class *cl;
137 KKASSERT(a->priority < PRIQ_MAXPRI);
138 KKASSERT(a->qid != 0);
140 if (pif->pif_classes[a->priority] != NULL)
142 if (clh_to_clp(pif, a->qid) != NULL)
145 cl = priq_class_create(pif, a->priority, a->qlimit,
146 a->pq_u.priq_opts.flags, a->qid);
154 priq_add_queue(struct pf_altq *a)
160 /* check parameters */
161 if (a->priority >= PRIQ_MAXPRI)
166 /* XXX not MP safe */
167 if ((pif = a->altq_disc) == NULL)
172 error = priq_add_queue_locked(a, pif);
179 priq_remove_queue_locked(struct pf_altq *a, struct priq_if *pif)
181 struct priq_class *cl;
183 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
186 return (priq_class_destroy(cl));
190 priq_remove_queue(struct pf_altq *a)
196 /* XXX not MF safe */
197 if ((pif = a->altq_disc) == NULL)
202 error = priq_remove_queue_locked(a, pif);
209 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
212 struct priq_class *cl;
213 struct priq_classstats stats;
217 if (*nbytes < sizeof(stats))
220 /* XXX not MP safe */
221 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
227 if ((cl = clh_to_clp(pif, a->qid)) == NULL) {
232 get_class_stats(&stats, cl);
236 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
238 *nbytes = sizeof(stats);
243 * bring the interface back to the initial state by discarding
244 * all the filters and classes.
247 priq_clear_interface(struct priq_if *pif)
249 struct priq_class *cl;
252 /* clear out the classes */
253 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
254 if ((cl = pif->pif_classes[pri]) != NULL)
255 priq_class_destroy(cl);
262 priq_request(struct ifaltq_subque *ifsq, int req, void *arg)
264 struct ifaltq *ifq = ifsq->ifsq_altq;
265 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
270 if (ifsq_get_index(ifsq) == PRIQ_SUBQ_INDEX) {
274 * Race happened, the unrelated subqueue was
275 * picked during the packet scheduler transition.
277 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
285 /* discard all the queued packets on the interface */
287 priq_purge(struct priq_if *pif)
289 struct priq_class *cl;
292 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
293 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
296 if (ifq_is_enabled(pif->pif_ifq))
297 pif->pif_ifq->altq_subq[PRIQ_SUBQ_INDEX].ifq_len = 0;
300 static struct priq_class *
301 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
303 struct priq_class *cl;
306 if (flags & PRCF_RED) {
308 kprintf("priq_class_create: RED not configured for PRIQ!\n");
314 if ((cl = pif->pif_classes[pri]) != NULL) {
315 /* modify the class instead of creating a new one */
317 if (!qempty(cl->cl_q))
321 if (q_is_rio(cl->cl_q))
322 rio_destroy((rio_t *)cl->cl_red);
325 if (q_is_red(cl->cl_q))
326 red_destroy(cl->cl_red);
329 cl = kmalloc(sizeof(*cl), M_ALTQ, M_WAITOK | M_ZERO);
330 cl->cl_q = kmalloc(sizeof(*cl->cl_q), M_ALTQ, M_WAITOK | M_ZERO);
333 pif->pif_classes[pri] = cl;
334 if (flags & PRCF_DEFAULTCLASS)
335 pif->pif_default = cl;
337 qlimit = 50; /* use default */
338 qlimit(cl->cl_q) = qlimit;
339 qtype(cl->cl_q) = Q_DROPTAIL;
341 cl->cl_flags = flags;
343 if (pri > pif->pif_maxpri)
344 pif->pif_maxpri = pri;
349 if (flags & (PRCF_RED|PRCF_RIO)) {
350 int red_flags, red_pkttime;
353 if (flags & PRCF_ECN)
354 red_flags |= REDF_ECN;
356 if (flags & PRCF_CLEARDSCP)
357 red_flags |= RIOF_CLEARDSCP;
359 if (pif->pif_bandwidth < 8)
360 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
362 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
363 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
365 if (flags & PRCF_RIO) {
366 cl->cl_red = (red_t *)rio_alloc(0, NULL,
367 red_flags, red_pkttime);
368 if (cl->cl_red != NULL)
369 qtype(cl->cl_q) = Q_RIO;
372 if (flags & PRCF_RED) {
373 cl->cl_red = red_alloc(0, 0,
374 qlimit(cl->cl_q) * 10/100,
375 qlimit(cl->cl_q) * 30/100,
376 red_flags, red_pkttime);
377 if (cl->cl_red != NULL)
378 qtype(cl->cl_q) = Q_RED;
381 #endif /* ALTQ_RED */
387 priq_class_destroy(struct priq_class *cl)
394 if (!qempty(cl->cl_q))
398 pif->pif_classes[cl->cl_pri] = NULL;
399 if (pif->pif_maxpri == cl->cl_pri) {
400 for (pri = cl->cl_pri; pri >= 0; pri--)
401 if (pif->pif_classes[pri] != NULL) {
402 pif->pif_maxpri = pri;
406 pif->pif_maxpri = -1;
410 if (cl->cl_red != NULL) {
412 if (q_is_rio(cl->cl_q))
413 rio_destroy((rio_t *)cl->cl_red);
416 if (q_is_red(cl->cl_q))
417 red_destroy(cl->cl_red);
420 kfree(cl->cl_q, M_ALTQ);
426 * priq_enqueue is an enqueue function to be registered to
427 * (*altq_enqueue) in struct ifaltq.
430 priq_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
431 struct altq_pktattr *pktattr)
433 struct ifaltq *ifq = ifsq->ifsq_altq;
434 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
435 struct priq_class *cl;
439 if (ifsq_get_index(ifsq) != PRIQ_SUBQ_INDEX) {
441 * Race happened, the unrelated subqueue was
442 * picked during the packet scheduler transition.
444 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
451 /* grab class set by classifier */
452 if ((m->m_flags & M_PKTHDR) == 0) {
453 /* should not happen */
454 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n");
460 if (m->m_pkthdr.fw_flags & PF_MBUF_STRUCTURE)
461 cl = clh_to_clp(pif, m->m_pkthdr.pf.qid);
465 cl = pif->pif_default;
472 cl->cl_pktattr = NULL;
474 if (priq_addq(cl, m) != 0) {
475 /* drop occurred. mbuf was freed in priq_addq. */
476 PKTCNTR_ADD(&cl->cl_dropcnt, len);
488 * priq_dequeue is a dequeue function to be registered to
489 * (*altq_dequeue) in struct ifaltq.
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
497 priq_dequeue(struct ifaltq_subque *ifsq, struct mbuf *mpolled, int op)
499 struct ifaltq *ifq = ifsq->ifsq_altq;
500 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
501 struct priq_class *cl;
505 if (ifsq_get_index(ifsq) != PRIQ_SUBQ_INDEX) {
507 * Race happened, the unrelated subqueue was
508 * picked during the packet scheduler transition.
510 ifsq_classic_request(ifsq, ALTRQ_PURGE, NULL);
514 if (ifsq_is_empty(ifsq)) {
515 /* no packet in the queue */
516 KKASSERT(mpolled == NULL);
522 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
523 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q)) {
524 if (op == ALTDQ_POLL) {
532 if (qempty(cl->cl_q))
534 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
540 KKASSERT(mpolled == NULL || mpolled == m);
545 priq_addq(struct priq_class *cl, struct mbuf *m)
548 if (q_is_rio(cl->cl_q))
549 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
553 if (q_is_red(cl->cl_q))
554 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
556 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
561 if (cl->cl_flags & PRCF_CLEARDSCP)
562 write_dsfield(m, cl->cl_pktattr, 0);
570 priq_getq(struct priq_class *cl)
573 if (q_is_rio(cl->cl_q))
574 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
577 if (q_is_red(cl->cl_q))
578 return red_getq(cl->cl_red, cl->cl_q);
580 return _getq(cl->cl_q);
584 priq_pollq(struct priq_class *cl)
586 return qhead(cl->cl_q);
590 priq_purgeq(struct priq_class *cl)
594 if (qempty(cl->cl_q))
597 while ((m = _getq(cl->cl_q)) != NULL) {
598 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
601 KKASSERT(qlen(cl->cl_q) == 0);
605 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
607 sp->class_handle = cl->cl_handle;
608 sp->qlength = qlen(cl->cl_q);
609 sp->qlimit = qlimit(cl->cl_q);
610 sp->period = cl->cl_period;
611 sp->xmitcnt = cl->cl_xmitcnt;
612 sp->dropcnt = cl->cl_dropcnt;
614 sp->qtype = qtype(cl->cl_q);
616 if (q_is_red(cl->cl_q))
617 red_getstats(cl->cl_red, &sp->red[0]);
620 if (q_is_rio(cl->cl_q))
621 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
625 /* convert a class handle to the corresponding class pointer */
626 static struct priq_class *
627 clh_to_clp(struct priq_if *pif, uint32_t chandle)
629 struct priq_class *cl;
635 for (idx = pif->pif_maxpri; idx >= 0; idx--)
636 if ((cl = pif->pif_classes[idx]) != NULL &&
637 cl->cl_handle == chandle)
643 #endif /* ALTQ_PRIQ */