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 ifnet * ifp, struct pf_altq *a)
104 if (!ALTQ_IS_READY(&ifp->if_snd))
107 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
110 pif->pif_bandwidth = a->ifbandwidth;
111 pif->pif_maxpri = -1;
112 pif->pif_ifq = &ifp->if_snd;
114 /* keep the state in pf_altq */
121 priq_remove_altq(struct pf_altq *a)
125 if ((pif = a->altq_disc) == NULL)
129 (void)priq_clear_interface(pif);
136 priq_add_queue(struct pf_altq *a)
139 struct priq_class *cl;
141 if ((pif = a->altq_disc) == NULL)
144 /* check parameters */
145 if (a->priority >= PRIQ_MAXPRI)
149 if (pif->pif_classes[a->priority] != NULL)
151 if (clh_to_clp(pif, a->qid) != NULL)
154 cl = priq_class_create(pif, a->priority, a->qlimit,
155 a->pq_u.priq_opts.flags, a->qid);
163 priq_remove_queue(struct pf_altq *a)
166 struct priq_class *cl;
168 if ((pif = a->altq_disc) == NULL)
171 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
174 return (priq_class_destroy(cl));
178 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
181 struct priq_class *cl;
182 struct priq_classstats stats;
185 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
188 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
191 if (*nbytes < sizeof(stats))
194 get_class_stats(&stats, cl);
196 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
198 *nbytes = sizeof(stats);
203 * bring the interface back to the initial state by discarding
204 * all the filters and classes.
207 priq_clear_interface(struct priq_if *pif)
209 struct priq_class *cl;
212 #ifdef ALTQ3_CLFIER_COMPAT
213 /* free the filters for this interface */
214 acc_discard_filters(&pif->pif_classifier, NULL, 1);
217 /* clear out the classes */
218 for (pri = 0; pri <= pif->pif_maxpri; pri++)
219 if ((cl = pif->pif_classes[pri]) != NULL)
220 priq_class_destroy(cl);
226 priq_request(struct ifaltq *ifq, int req, void *arg)
228 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
230 IFQ_LOCK_ASSERT(ifq);
240 /* discard all the queued packets on the interface */
242 priq_purge(struct priq_if *pif)
244 struct priq_class *cl;
247 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
248 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
251 if (ALTQ_IS_ENABLED(pif->pif_ifq))
252 pif->pif_ifq->ifq_len = 0;
255 static struct priq_class *
256 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
258 struct priq_class *cl;
262 if (flags & PRCF_RED) {
264 printf("priq_class_create: RED not configured for PRIQ!\n");
270 if (flags & PRCF_CODEL) {
272 printf("priq_class_create: CODEL not configured for PRIQ!\n");
278 if ((cl = pif->pif_classes[pri]) != NULL) {
279 /* modify the class instead of creating a new one */
281 IFQ_LOCK(cl->cl_pif->pif_ifq);
282 if (!qempty(cl->cl_q))
284 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
287 if (q_is_rio(cl->cl_q))
288 rio_destroy((rio_t *)cl->cl_red);
291 if (q_is_red(cl->cl_q))
292 red_destroy(cl->cl_red);
295 if (q_is_codel(cl->cl_q))
296 codel_destroy(cl->cl_codel);
299 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
304 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
306 if (cl->cl_q == NULL)
310 pif->pif_classes[pri] = cl;
311 if (flags & PRCF_DEFAULTCLASS)
312 pif->pif_default = cl;
314 qlimit = 50; /* use default */
315 qlimit(cl->cl_q) = qlimit;
316 qtype(cl->cl_q) = Q_DROPTAIL;
319 cl->cl_flags = flags;
321 if (pri > pif->pif_maxpri)
322 pif->pif_maxpri = pri;
327 if (flags & (PRCF_RED|PRCF_RIO)) {
328 int red_flags, red_pkttime;
331 if (flags & PRCF_ECN)
332 red_flags |= REDF_ECN;
334 if (flags & PRCF_CLEARDSCP)
335 red_flags |= RIOF_CLEARDSCP;
337 if (pif->pif_bandwidth < 8)
338 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
340 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
341 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
343 if (flags & PRCF_RIO) {
344 cl->cl_red = (red_t *)rio_alloc(0, NULL,
345 red_flags, red_pkttime);
346 if (cl->cl_red == NULL)
348 qtype(cl->cl_q) = Q_RIO;
351 if (flags & PRCF_RED) {
352 cl->cl_red = red_alloc(0, 0,
353 qlimit(cl->cl_q) * 10/100,
354 qlimit(cl->cl_q) * 30/100,
355 red_flags, red_pkttime);
356 if (cl->cl_red == NULL)
358 qtype(cl->cl_q) = Q_RED;
361 #endif /* ALTQ_RED */
363 if (flags & PRCF_CODEL) {
364 cl->cl_codel = codel_alloc(5, 100, 0);
365 if (cl->cl_codel != NULL)
366 qtype(cl->cl_q) = Q_CODEL;
373 if (cl->cl_red != NULL) {
375 if (q_is_rio(cl->cl_q))
376 rio_destroy((rio_t *)cl->cl_red);
379 if (q_is_red(cl->cl_q))
380 red_destroy(cl->cl_red);
383 if (q_is_codel(cl->cl_q))
384 codel_destroy(cl->cl_codel);
387 if (cl->cl_q != NULL)
388 free(cl->cl_q, M_DEVBUF);
394 priq_class_destroy(struct priq_class *cl)
400 IFQ_LOCK(cl->cl_pif->pif_ifq);
402 #ifdef ALTQ3_CLFIER_COMPAT
403 /* delete filters referencing to this class */
404 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
407 if (!qempty(cl->cl_q))
411 pif->pif_classes[cl->cl_pri] = NULL;
412 if (pif->pif_maxpri == cl->cl_pri) {
413 for (pri = cl->cl_pri; pri >= 0; pri--)
414 if (pif->pif_classes[pri] != NULL) {
415 pif->pif_maxpri = pri;
419 pif->pif_maxpri = -1;
421 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
424 if (cl->cl_red != NULL) {
426 if (q_is_rio(cl->cl_q))
427 rio_destroy((rio_t *)cl->cl_red);
430 if (q_is_red(cl->cl_q))
431 red_destroy(cl->cl_red);
434 if (q_is_codel(cl->cl_q))
435 codel_destroy(cl->cl_codel);
438 free(cl->cl_q, M_DEVBUF);
444 * priq_enqueue is an enqueue function to be registered to
445 * (*altq_enqueue) in struct ifaltq.
448 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
450 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
451 struct priq_class *cl;
455 IFQ_LOCK_ASSERT(ifq);
457 /* grab class set by classifier */
458 if ((m->m_flags & M_PKTHDR) == 0) {
459 /* should not happen */
460 printf("altq: packet for %s does not have pkthdr\n",
461 ifq->altq_ifp->if_xname);
466 if ((t = pf_find_mtag(m)) != NULL)
467 cl = clh_to_clp(pif, t->qid);
469 cl = pif->pif_default;
475 cl->cl_pktattr = NULL;
477 if (priq_addq(cl, m) != 0) {
478 /* drop occurred. mbuf was freed in priq_addq. */
479 PKTCNTR_ADD(&cl->cl_dropcnt, len);
484 /* successfully queued. */
489 * priq_dequeue is a dequeue function to be registered to
490 * (*altq_dequeue) in struct ifaltq.
492 * note: ALTDQ_POLL returns the next packet without removing the packet
493 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
494 * ALTDQ_REMOVE must return the same packet if called immediately
498 priq_dequeue(struct ifaltq *ifq, int op)
500 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
501 struct priq_class *cl;
505 IFQ_LOCK_ASSERT(ifq);
507 if (IFQ_IS_EMPTY(ifq))
508 /* no packet in the queue */
511 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
512 if ((cl = pif->pif_classes[pri]) != NULL &&
514 if (op == ALTDQ_POLL)
515 return (priq_pollq(cl));
520 if (qempty(cl->cl_q))
522 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
531 priq_addq(struct priq_class *cl, struct mbuf *m)
535 if (q_is_rio(cl->cl_q))
536 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
540 if (q_is_red(cl->cl_q))
541 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
544 if (q_is_codel(cl->cl_q))
545 return codel_addq(cl->cl_codel, cl->cl_q, m);
547 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
552 if (cl->cl_flags & PRCF_CLEARDSCP)
553 write_dsfield(m, cl->cl_pktattr, 0);
561 priq_getq(struct priq_class *cl)
564 if (q_is_rio(cl->cl_q))
565 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
568 if (q_is_red(cl->cl_q))
569 return red_getq(cl->cl_red, cl->cl_q);
572 if (q_is_codel(cl->cl_q))
573 return codel_getq(cl->cl_codel, cl->cl_q);
575 return _getq(cl->cl_q);
580 struct priq_class *cl;
582 return qhead(cl->cl_q);
586 priq_purgeq(struct priq_class *cl)
590 if (qempty(cl->cl_q))
593 while ((m = _getq(cl->cl_q)) != NULL) {
594 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
597 ASSERT(qlen(cl->cl_q) == 0);
601 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
603 sp->class_handle = cl->cl_handle;
604 sp->qlength = qlen(cl->cl_q);
605 sp->qlimit = qlimit(cl->cl_q);
606 sp->period = cl->cl_period;
607 sp->xmitcnt = cl->cl_xmitcnt;
608 sp->dropcnt = cl->cl_dropcnt;
610 sp->qtype = qtype(cl->cl_q);
612 if (q_is_red(cl->cl_q))
613 red_getstats(cl->cl_red, &sp->red[0]);
616 if (q_is_rio(cl->cl_q))
617 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
620 if (q_is_codel(cl->cl_q))
621 codel_getstats(cl->cl_codel, &sp->codel);
625 /* convert a class handle to the corresponding class pointer */
626 static struct priq_class *
627 clh_to_clp(struct priq_if *pif, u_int32_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)
644 #endif /* ALTQ_PRIQ */