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 *);
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);
83 priq_pfattach(struct pf_altq *a)
88 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
91 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
92 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
98 priq_add_altq(struct pf_altq *a)
103 if ((ifp = ifunit(a->ifname)) == NULL)
105 if (!ALTQ_IS_READY(&ifp->if_snd))
108 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
111 pif->pif_bandwidth = a->ifbandwidth;
112 pif->pif_maxpri = -1;
113 pif->pif_ifq = &ifp->if_snd;
115 /* keep the state in pf_altq */
122 priq_remove_altq(struct pf_altq *a)
126 if ((pif = a->altq_disc) == NULL)
130 (void)priq_clear_interface(pif);
137 priq_add_queue(struct pf_altq *a)
140 struct priq_class *cl;
142 if ((pif = a->altq_disc) == NULL)
145 /* check parameters */
146 if (a->priority >= PRIQ_MAXPRI)
150 if (pif->pif_classes[a->priority] != NULL)
152 if (clh_to_clp(pif, a->qid) != NULL)
155 cl = priq_class_create(pif, a->priority, a->qlimit,
156 a->pq_u.priq_opts.flags, a->qid);
164 priq_remove_queue(struct pf_altq *a)
167 struct priq_class *cl;
169 if ((pif = a->altq_disc) == NULL)
172 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
175 return (priq_class_destroy(cl));
179 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
182 struct priq_class *cl;
183 struct priq_classstats stats;
186 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
189 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
192 if (*nbytes < sizeof(stats))
195 get_class_stats(&stats, cl);
197 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
199 *nbytes = sizeof(stats);
204 * bring the interface back to the initial state by discarding
205 * all the filters and classes.
208 priq_clear_interface(struct priq_if *pif)
210 struct priq_class *cl;
213 #ifdef ALTQ3_CLFIER_COMPAT
214 /* free the filters for this interface */
215 acc_discard_filters(&pif->pif_classifier, NULL, 1);
218 /* clear out the classes */
219 for (pri = 0; pri <= pif->pif_maxpri; pri++)
220 if ((cl = pif->pif_classes[pri]) != NULL)
221 priq_class_destroy(cl);
227 priq_request(struct ifaltq *ifq, int req, void *arg)
229 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
231 IFQ_LOCK_ASSERT(ifq);
241 /* discard all the queued packets on the interface */
243 priq_purge(struct priq_if *pif)
245 struct priq_class *cl;
248 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
249 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
252 if (ALTQ_IS_ENABLED(pif->pif_ifq))
253 pif->pif_ifq->ifq_len = 0;
256 static struct priq_class *
257 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
259 struct priq_class *cl;
263 if (flags & PRCF_RED) {
265 printf("priq_class_create: RED not configured for PRIQ!\n");
271 if (flags & PRCF_CODEL) {
273 printf("priq_class_create: CODEL not configured for PRIQ!\n");
279 if ((cl = pif->pif_classes[pri]) != NULL) {
280 /* modify the class instead of creating a new one */
282 IFQ_LOCK(cl->cl_pif->pif_ifq);
283 if (!qempty(cl->cl_q))
285 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
288 if (q_is_rio(cl->cl_q))
289 rio_destroy((rio_t *)cl->cl_red);
292 if (q_is_red(cl->cl_q))
293 red_destroy(cl->cl_red);
296 if (q_is_codel(cl->cl_q))
297 codel_destroy(cl->cl_codel);
300 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
305 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
307 if (cl->cl_q == NULL)
311 pif->pif_classes[pri] = cl;
312 if (flags & PRCF_DEFAULTCLASS)
313 pif->pif_default = cl;
315 qlimit = 50; /* use default */
316 qlimit(cl->cl_q) = qlimit;
317 qtype(cl->cl_q) = Q_DROPTAIL;
320 cl->cl_flags = flags;
322 if (pri > pif->pif_maxpri)
323 pif->pif_maxpri = pri;
328 if (flags & (PRCF_RED|PRCF_RIO)) {
329 int red_flags, red_pkttime;
332 if (flags & PRCF_ECN)
333 red_flags |= REDF_ECN;
335 if (flags & PRCF_CLEARDSCP)
336 red_flags |= RIOF_CLEARDSCP;
338 if (pif->pif_bandwidth < 8)
339 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
341 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
342 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
344 if (flags & PRCF_RIO) {
345 cl->cl_red = (red_t *)rio_alloc(0, NULL,
346 red_flags, red_pkttime);
347 if (cl->cl_red == NULL)
349 qtype(cl->cl_q) = Q_RIO;
352 if (flags & PRCF_RED) {
353 cl->cl_red = red_alloc(0, 0,
354 qlimit(cl->cl_q) * 10/100,
355 qlimit(cl->cl_q) * 30/100,
356 red_flags, red_pkttime);
357 if (cl->cl_red == NULL)
359 qtype(cl->cl_q) = Q_RED;
362 #endif /* ALTQ_RED */
364 if (flags & PRCF_CODEL) {
365 cl->cl_codel = codel_alloc(5, 100, 0);
366 if (cl->cl_codel != NULL)
367 qtype(cl->cl_q) = Q_CODEL;
374 if (cl->cl_red != NULL) {
376 if (q_is_rio(cl->cl_q))
377 rio_destroy((rio_t *)cl->cl_red);
380 if (q_is_red(cl->cl_q))
381 red_destroy(cl->cl_red);
384 if (q_is_codel(cl->cl_q))
385 codel_destroy(cl->cl_codel);
388 if (cl->cl_q != NULL)
389 free(cl->cl_q, M_DEVBUF);
395 priq_class_destroy(struct priq_class *cl)
401 IFQ_LOCK(cl->cl_pif->pif_ifq);
403 #ifdef ALTQ3_CLFIER_COMPAT
404 /* delete filters referencing to this class */
405 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
408 if (!qempty(cl->cl_q))
412 pif->pif_classes[cl->cl_pri] = NULL;
413 if (pif->pif_maxpri == cl->cl_pri) {
414 for (pri = cl->cl_pri; pri >= 0; pri--)
415 if (pif->pif_classes[pri] != NULL) {
416 pif->pif_maxpri = pri;
420 pif->pif_maxpri = -1;
422 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
425 if (cl->cl_red != NULL) {
427 if (q_is_rio(cl->cl_q))
428 rio_destroy((rio_t *)cl->cl_red);
431 if (q_is_red(cl->cl_q))
432 red_destroy(cl->cl_red);
435 if (q_is_codel(cl->cl_q))
436 codel_destroy(cl->cl_codel);
439 free(cl->cl_q, M_DEVBUF);
445 * priq_enqueue is an enqueue function to be registered to
446 * (*altq_enqueue) in struct ifaltq.
449 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
451 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
452 struct priq_class *cl;
456 IFQ_LOCK_ASSERT(ifq);
458 /* grab class set by classifier */
459 if ((m->m_flags & M_PKTHDR) == 0) {
460 /* should not happen */
461 printf("altq: packet for %s does not have pkthdr\n",
462 ifq->altq_ifp->if_xname);
467 if ((t = pf_find_mtag(m)) != NULL)
468 cl = clh_to_clp(pif, t->qid);
470 cl = pif->pif_default;
476 cl->cl_pktattr = NULL;
478 if (priq_addq(cl, m) != 0) {
479 /* drop occurred. mbuf was freed in priq_addq. */
480 PKTCNTR_ADD(&cl->cl_dropcnt, len);
485 /* successfully queued. */
490 * priq_dequeue is a dequeue function to be registered to
491 * (*altq_dequeue) in struct ifaltq.
493 * note: ALTDQ_POLL returns the next packet without removing the packet
494 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
495 * ALTDQ_REMOVE must return the same packet if called immediately
499 priq_dequeue(struct ifaltq *ifq, int op)
501 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
502 struct priq_class *cl;
506 IFQ_LOCK_ASSERT(ifq);
508 if (IFQ_IS_EMPTY(ifq))
509 /* no packet in the queue */
512 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
513 if ((cl = pif->pif_classes[pri]) != NULL &&
515 if (op == ALTDQ_POLL)
516 return (priq_pollq(cl));
521 if (qempty(cl->cl_q))
523 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
532 priq_addq(struct priq_class *cl, struct mbuf *m)
536 if (q_is_rio(cl->cl_q))
537 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
541 if (q_is_red(cl->cl_q))
542 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
545 if (q_is_codel(cl->cl_q))
546 return codel_addq(cl->cl_codel, cl->cl_q, m);
548 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
553 if (cl->cl_flags & PRCF_CLEARDSCP)
554 write_dsfield(m, cl->cl_pktattr, 0);
562 priq_getq(struct priq_class *cl)
565 if (q_is_rio(cl->cl_q))
566 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
569 if (q_is_red(cl->cl_q))
570 return red_getq(cl->cl_red, cl->cl_q);
573 if (q_is_codel(cl->cl_q))
574 return codel_getq(cl->cl_codel, cl->cl_q);
576 return _getq(cl->cl_q);
581 struct priq_class *cl;
583 return qhead(cl->cl_q);
587 priq_purgeq(struct priq_class *cl)
591 if (qempty(cl->cl_q))
594 while ((m = _getq(cl->cl_q)) != NULL) {
595 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
598 ASSERT(qlen(cl->cl_q) == 0);
602 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
604 sp->class_handle = cl->cl_handle;
605 sp->qlength = qlen(cl->cl_q);
606 sp->qlimit = qlimit(cl->cl_q);
607 sp->period = cl->cl_period;
608 sp->xmitcnt = cl->cl_xmitcnt;
609 sp->dropcnt = cl->cl_dropcnt;
611 sp->qtype = qtype(cl->cl_q);
613 if (q_is_red(cl->cl_q))
614 red_getstats(cl->cl_red, &sp->red[0]);
617 if (q_is_rio(cl->cl_q))
618 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
621 if (q_is_codel(cl->cl_q))
622 codel_getstats(cl->cl_codel, &sp->codel);
626 /* convert a class handle to the corresponding class pointer */
627 static struct priq_class *
628 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
630 struct priq_class *cl;
636 for (idx = pif->pif_maxpri; idx >= 0; idx--)
637 if ((cl = pif->pif_classes[idx]) != NULL &&
638 cl->cl_handle == chandle)
645 #endif /* ALTQ_PRIQ */