2 * Copyright (C) 2000-2003
3 * Sony Computer Science Laboratories Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $KAME: altq_priq.c,v 1.11 2003/09/17 14:23:25 kjc Exp $
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>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
54 #include <netpfil/pf/pf.h>
55 #include <netpfil/pf/pf_altq.h>
56 #include <netpfil/pf/pf_mtag.h>
57 #include <net/altq/altq.h>
58 #include <net/altq/altq_priq.h>
63 static int priq_clear_interface(struct priq_if *);
64 static int priq_request(struct ifaltq *, int, void *);
65 static void priq_purge(struct priq_if *);
66 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
68 static int priq_class_destroy(struct priq_class *);
69 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
70 static struct mbuf *priq_dequeue(struct ifaltq *, int);
72 static int priq_addq(struct priq_class *, struct mbuf *);
73 static struct mbuf *priq_getq(struct priq_class *);
74 static struct mbuf *priq_pollq(struct priq_class *);
75 static void priq_purgeq(struct priq_class *);
77 static void get_class_stats(struct priq_classstats *, struct priq_class *);
78 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
81 priq_pfattach(struct pf_altq *a)
86 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
89 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
90 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
96 priq_add_altq(struct ifnet * ifp, struct pf_altq *a)
102 if (!ALTQ_IS_READY(&ifp->if_snd))
105 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
108 pif->pif_bandwidth = a->ifbandwidth;
109 pif->pif_maxpri = -1;
110 pif->pif_ifq = &ifp->if_snd;
112 /* keep the state in pf_altq */
119 priq_remove_altq(struct pf_altq *a)
123 if ((pif = a->altq_disc) == NULL)
127 (void)priq_clear_interface(pif);
134 priq_add_queue(struct pf_altq *a)
137 struct priq_class *cl;
139 if ((pif = a->altq_disc) == NULL)
142 /* check parameters */
143 if (a->priority >= PRIQ_MAXPRI)
147 if (pif->pif_classes[a->priority] != NULL)
149 if (clh_to_clp(pif, a->qid) != NULL)
152 cl = priq_class_create(pif, a->priority, a->qlimit,
153 a->pq_u.priq_opts.flags, a->qid);
161 priq_remove_queue(struct pf_altq *a)
164 struct priq_class *cl;
166 if ((pif = a->altq_disc) == NULL)
169 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
172 return (priq_class_destroy(cl));
176 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
179 struct priq_class *cl;
180 struct priq_classstats stats;
183 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
186 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
189 if (*nbytes < sizeof(stats))
192 get_class_stats(&stats, cl);
194 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
196 *nbytes = sizeof(stats);
201 * bring the interface back to the initial state by discarding
202 * all the filters and classes.
205 priq_clear_interface(struct priq_if *pif)
207 struct priq_class *cl;
210 #ifdef ALTQ3_CLFIER_COMPAT
211 /* free the filters for this interface */
212 acc_discard_filters(&pif->pif_classifier, NULL, 1);
215 /* clear out the classes */
216 for (pri = 0; pri <= pif->pif_maxpri; pri++)
217 if ((cl = pif->pif_classes[pri]) != NULL)
218 priq_class_destroy(cl);
224 priq_request(struct ifaltq *ifq, int req, void *arg)
226 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
228 IFQ_LOCK_ASSERT(ifq);
238 /* discard all the queued packets on the interface */
240 priq_purge(struct priq_if *pif)
242 struct priq_class *cl;
245 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
246 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
249 if (ALTQ_IS_ENABLED(pif->pif_ifq))
250 pif->pif_ifq->ifq_len = 0;
253 static struct priq_class *
254 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
256 struct priq_class *cl;
260 if (flags & PRCF_RED) {
262 printf("priq_class_create: RED not configured for PRIQ!\n");
268 if (flags & PRCF_CODEL) {
270 printf("priq_class_create: CODEL not configured for PRIQ!\n");
276 if ((cl = pif->pif_classes[pri]) != NULL) {
277 /* modify the class instead of creating a new one */
279 IFQ_LOCK(cl->cl_pif->pif_ifq);
280 if (!qempty(cl->cl_q))
282 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
285 if (q_is_rio(cl->cl_q))
286 rio_destroy((rio_t *)cl->cl_red);
289 if (q_is_red(cl->cl_q))
290 red_destroy(cl->cl_red);
293 if (q_is_codel(cl->cl_q))
294 codel_destroy(cl->cl_codel);
297 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
302 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
304 if (cl->cl_q == NULL)
308 pif->pif_classes[pri] = cl;
309 if (flags & PRCF_DEFAULTCLASS)
310 pif->pif_default = cl;
312 qlimit = 50; /* use default */
313 qlimit(cl->cl_q) = qlimit;
314 qtype(cl->cl_q) = Q_DROPTAIL;
317 cl->cl_flags = flags;
319 if (pri > pif->pif_maxpri)
320 pif->pif_maxpri = pri;
325 if (flags & (PRCF_RED|PRCF_RIO)) {
326 int red_flags, red_pkttime;
329 if (flags & PRCF_ECN)
330 red_flags |= REDF_ECN;
332 if (flags & PRCF_CLEARDSCP)
333 red_flags |= RIOF_CLEARDSCP;
335 if (pif->pif_bandwidth < 8)
336 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
338 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
339 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
341 if (flags & PRCF_RIO) {
342 cl->cl_red = (red_t *)rio_alloc(0, NULL,
343 red_flags, red_pkttime);
344 if (cl->cl_red == NULL)
346 qtype(cl->cl_q) = Q_RIO;
349 if (flags & PRCF_RED) {
350 cl->cl_red = red_alloc(0, 0,
351 qlimit(cl->cl_q) * 10/100,
352 qlimit(cl->cl_q) * 30/100,
353 red_flags, red_pkttime);
354 if (cl->cl_red == NULL)
356 qtype(cl->cl_q) = Q_RED;
359 #endif /* ALTQ_RED */
361 if (flags & PRCF_CODEL) {
362 cl->cl_codel = codel_alloc(5, 100, 0);
363 if (cl->cl_codel != NULL)
364 qtype(cl->cl_q) = Q_CODEL;
371 if (cl->cl_red != NULL) {
373 if (q_is_rio(cl->cl_q))
374 rio_destroy((rio_t *)cl->cl_red);
377 if (q_is_red(cl->cl_q))
378 red_destroy(cl->cl_red);
381 if (q_is_codel(cl->cl_q))
382 codel_destroy(cl->cl_codel);
385 if (cl->cl_q != NULL)
386 free(cl->cl_q, M_DEVBUF);
392 priq_class_destroy(struct priq_class *cl)
398 IFQ_LOCK(cl->cl_pif->pif_ifq);
400 #ifdef ALTQ3_CLFIER_COMPAT
401 /* delete filters referencing to this class */
402 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
405 if (!qempty(cl->cl_q))
409 pif->pif_classes[cl->cl_pri] = NULL;
410 if (pif->pif_maxpri == cl->cl_pri) {
411 for (pri = cl->cl_pri; pri >= 0; pri--)
412 if (pif->pif_classes[pri] != NULL) {
413 pif->pif_maxpri = pri;
417 pif->pif_maxpri = -1;
419 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
422 if (cl->cl_red != NULL) {
424 if (q_is_rio(cl->cl_q))
425 rio_destroy((rio_t *)cl->cl_red);
428 if (q_is_red(cl->cl_q))
429 red_destroy(cl->cl_red);
432 if (q_is_codel(cl->cl_q))
433 codel_destroy(cl->cl_codel);
436 free(cl->cl_q, M_DEVBUF);
442 * priq_enqueue is an enqueue function to be registered to
443 * (*altq_enqueue) in struct ifaltq.
446 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
448 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
449 struct priq_class *cl;
453 IFQ_LOCK_ASSERT(ifq);
455 /* grab class set by classifier */
456 if ((m->m_flags & M_PKTHDR) == 0) {
457 /* should not happen */
458 printf("altq: packet for %s does not have pkthdr\n",
459 ifq->altq_ifp->if_xname);
464 if ((t = pf_find_mtag(m)) != NULL)
465 cl = clh_to_clp(pif, t->qid);
467 cl = pif->pif_default;
473 cl->cl_pktattr = NULL;
475 if (priq_addq(cl, m) != 0) {
476 /* drop occurred. mbuf was freed in priq_addq. */
477 PKTCNTR_ADD(&cl->cl_dropcnt, len);
482 /* successfully queued. */
487 * priq_dequeue is a dequeue function to be registered to
488 * (*altq_dequeue) in struct ifaltq.
490 * note: ALTDQ_POLL returns the next packet without removing the packet
491 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
492 * ALTDQ_REMOVE must return the same packet if called immediately
496 priq_dequeue(struct ifaltq *ifq, int op)
498 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
499 struct priq_class *cl;
503 IFQ_LOCK_ASSERT(ifq);
505 if (IFQ_IS_EMPTY(ifq))
506 /* no packet in the queue */
509 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
510 if ((cl = pif->pif_classes[pri]) != NULL &&
512 if (op == ALTDQ_POLL)
513 return (priq_pollq(cl));
518 if (qempty(cl->cl_q))
520 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
529 priq_addq(struct priq_class *cl, struct mbuf *m)
533 if (q_is_rio(cl->cl_q))
534 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
538 if (q_is_red(cl->cl_q))
539 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
542 if (q_is_codel(cl->cl_q))
543 return codel_addq(cl->cl_codel, cl->cl_q, m);
545 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
550 if (cl->cl_flags & PRCF_CLEARDSCP)
551 write_dsfield(m, cl->cl_pktattr, 0);
559 priq_getq(struct priq_class *cl)
562 if (q_is_rio(cl->cl_q))
563 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
566 if (q_is_red(cl->cl_q))
567 return red_getq(cl->cl_red, cl->cl_q);
570 if (q_is_codel(cl->cl_q))
571 return codel_getq(cl->cl_codel, cl->cl_q);
573 return _getq(cl->cl_q);
578 struct priq_class *cl;
580 return qhead(cl->cl_q);
584 priq_purgeq(struct priq_class *cl)
588 if (qempty(cl->cl_q))
591 while ((m = _getq(cl->cl_q)) != NULL) {
592 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
595 ASSERT(qlen(cl->cl_q) == 0);
599 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
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;
608 sp->qtype = qtype(cl->cl_q);
610 if (q_is_red(cl->cl_q))
611 red_getstats(cl->cl_red, &sp->red[0]);
614 if (q_is_rio(cl->cl_q))
615 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
618 if (q_is_codel(cl->cl_q))
619 codel_getstats(cl->cl_codel, &sp->codel);
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)
627 struct priq_class *cl;
633 for (idx = pif->pif_maxpri; idx >= 0; idx--)
634 if ((cl = pif->pif_classes[idx]) != NULL &&
635 cl->cl_handle == chandle)
641 #endif /* ALTQ_PRIQ */