1 /* $OpenBSD: pfctl_altq.c,v 1.93 2007/10/15 02:16:35 deraadt Exp $ */
5 * Sony Computer Science Laboratories Inc.
6 * Copyright (c) 2002, 2003 Henning Brauer <henning@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <net/pfvar.h>
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_codel.h>
44 #include <net/altq/altq_priq.h>
45 #include <net/altq/altq_hfsc.h>
46 #include <net/altq/altq_fairq.h>
48 #include "pfctl_parser.h"
51 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
53 static TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
54 static LIST_HEAD(gen_sc, segment) rtsc, lssc;
56 struct pf_altq *qname_to_pfaltq(const char *, const char *);
57 u_int32_t qname_to_qid(const char *);
59 static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *);
60 static int cbq_compute_idletime(struct pfctl *, struct pf_altq *);
61 static int check_commit_cbq(int, int, struct pf_altq *);
62 static int print_cbq_opts(const struct pf_altq *);
64 static int print_codel_opts(const struct pf_altq *,
65 const struct node_queue_opt *);
67 static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *);
68 static int check_commit_priq(int, int, struct pf_altq *);
69 static int print_priq_opts(const struct pf_altq *);
71 static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *);
72 static int check_commit_hfsc(int, int, struct pf_altq *);
73 static int print_hfsc_opts(const struct pf_altq *,
74 const struct node_queue_opt *);
76 static int eval_pfqueue_fairq(struct pfctl *, struct pf_altq *);
77 static int print_fairq_opts(const struct pf_altq *,
78 const struct node_queue_opt *);
79 static int check_commit_fairq(int, int, struct pf_altq *);
81 static void gsc_add_sc(struct gen_sc *, struct service_curve *);
82 static int is_gsc_under_sc(struct gen_sc *,
83 struct service_curve *);
84 static void gsc_destroy(struct gen_sc *);
85 static struct segment *gsc_getentry(struct gen_sc *, double);
86 static int gsc_add_seg(struct gen_sc *, double, double, double,
88 static double sc_x2y(struct service_curve *, double);
91 u_int32_t getifspeed(int, char *);
93 u_int32_t getifspeed(char *);
95 u_long getifmtu(char *);
96 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
98 u_int32_t eval_bwspec(struct node_queue_bw *, u_int32_t);
99 void print_hfsc_sc(const char *, u_int, u_int, u_int,
100 const struct node_hfsc_sc *);
101 void print_fairq_sc(const char *, u_int, u_int, u_int,
102 const struct node_fairq_sc *);
105 pfaltq_store(struct pf_altq *a)
107 struct pf_altq *altq;
109 if ((altq = malloc(sizeof(*altq))) == NULL)
111 memcpy(altq, a, sizeof(struct pf_altq));
112 TAILQ_INSERT_TAIL(&altqs, altq, entries);
116 pfaltq_lookup(const char *ifname)
118 struct pf_altq *altq;
120 TAILQ_FOREACH(altq, &altqs, entries) {
121 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
129 qname_to_pfaltq(const char *qname, const char *ifname)
131 struct pf_altq *altq;
133 TAILQ_FOREACH(altq, &altqs, entries) {
134 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
135 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
142 qname_to_qid(const char *qname)
144 struct pf_altq *altq;
147 * We guarantee that same named queues on different interfaces
148 * have the same qid, so we do NOT need to limit matching on
152 TAILQ_FOREACH(altq, &altqs, entries) {
153 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
160 print_altq(const struct pf_altq *a, unsigned int level,
161 struct node_queue_bw *bw, struct node_queue_opt *qopts)
163 if (a->qname[0] != 0) {
164 print_queue(a, level, bw, 1, qopts);
169 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
173 printf("altq on %s ", a->ifname);
175 switch (a->scheduler) {
177 if (!print_cbq_opts(a))
181 if (!print_priq_opts(a))
185 if (!print_hfsc_opts(a, qopts))
189 if (!print_fairq_opts(a, qopts))
193 if (!print_codel_opts(a, qopts))
198 if (bw != NULL && bw->bw_percent > 0) {
199 if (bw->bw_percent < 100)
200 printf("bandwidth %u%% ", bw->bw_percent);
202 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
204 if (a->qlimit != DEFAULT_QLIMIT)
205 printf("qlimit %u ", a->qlimit);
206 printf("tbrsize %u ", a->tbrsize);
210 print_queue(const struct pf_altq *a, unsigned int level,
211 struct node_queue_bw *bw, int print_interface,
212 struct node_queue_opt *qopts)
217 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
221 for (i = 0; i < level; ++i)
223 printf("%s ", a->qname);
225 printf("on %s ", a->ifname);
226 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC ||
227 a->scheduler == ALTQT_FAIRQ) {
228 if (bw != NULL && bw->bw_percent > 0) {
229 if (bw->bw_percent < 100)
230 printf("bandwidth %u%% ", bw->bw_percent);
232 printf("bandwidth %s ", rate2str((double)a->bandwidth));
234 if (a->priority != DEFAULT_PRIORITY)
235 printf("priority %u ", a->priority);
236 if (a->qlimit != DEFAULT_QLIMIT)
237 printf("qlimit %u ", a->qlimit);
238 switch (a->scheduler) {
246 print_hfsc_opts(a, qopts);
249 print_fairq_opts(a, qopts);
255 * eval_pfaltq computes the discipline parameters.
258 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
259 struct node_queue_opt *opts)
261 u_int rate, size, errors = 0;
263 if (bw->bw_absolute > 0)
264 pa->ifbandwidth = bw->bw_absolute;
267 if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) {
269 if ((rate = getifspeed(pa->ifname)) == 0) {
271 fprintf(stderr, "interface %s does not know its bandwidth, "
272 "please specify an absolute bandwidth\n",
275 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
276 pa->ifbandwidth = rate;
278 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
280 /* if tbrsize is not specified, use heuristics */
281 if (pa->tbrsize == 0) {
282 rate = pa->ifbandwidth;
283 if (rate <= 1 * 1000 * 1000)
285 else if (rate <= 10 * 1000 * 1000)
287 else if (rate <= 200 * 1000 * 1000)
291 size = size * getifmtu(pa->ifname);
300 * check_commit_altq does consistency check for each interface
303 check_commit_altq(int dev, int opts)
305 struct pf_altq *altq;
308 /* call the discipline check for each interface. */
309 TAILQ_FOREACH(altq, &altqs, entries) {
310 if (altq->qname[0] == 0) {
311 switch (altq->scheduler) {
313 error = check_commit_cbq(dev, opts, altq);
316 error = check_commit_priq(dev, opts, altq);
319 error = check_commit_hfsc(dev, opts, altq);
322 error = check_commit_fairq(dev, opts, altq);
333 * eval_pfqueue computes the queue parameters.
336 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
337 struct node_queue_opt *opts)
339 /* should be merged with expand_queue */
340 struct pf_altq *if_pa, *parent, *altq;
344 /* find the corresponding interface and copy fields used by queues */
345 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
346 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
349 pa->scheduler = if_pa->scheduler;
350 pa->ifbandwidth = if_pa->ifbandwidth;
352 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
353 fprintf(stderr, "queue %s already exists on interface %s\n",
354 pa->qname, pa->ifname);
357 pa->qid = qname_to_qid(pa->qname);
360 if (pa->parent[0] != 0) {
361 parent = qname_to_pfaltq(pa->parent, pa->ifname);
362 if (parent == NULL) {
363 fprintf(stderr, "parent %s not found for %s\n",
364 pa->parent, pa->qname);
367 pa->parent_qid = parent->qid;
370 pa->qlimit = DEFAULT_QLIMIT;
372 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC ||
373 pa->scheduler == ALTQT_FAIRQ) {
374 pa->bandwidth = eval_bwspec(bw,
375 parent == NULL ? 0 : parent->bandwidth);
377 if (pa->bandwidth > pa->ifbandwidth) {
378 fprintf(stderr, "bandwidth for %s higher than "
379 "interface\n", pa->qname);
382 /* check the sum of the child bandwidth is under parent's */
383 if (parent != NULL) {
384 if (pa->bandwidth > parent->bandwidth) {
385 warnx("bandwidth for %s higher than parent",
390 TAILQ_FOREACH(altq, &altqs, entries) {
391 if (strncmp(altq->ifname, pa->ifname,
393 altq->qname[0] != 0 &&
394 strncmp(altq->parent, pa->parent,
396 bwsum += altq->bandwidth;
398 bwsum += pa->bandwidth;
399 if (bwsum > parent->bandwidth) {
400 warnx("the sum of the child bandwidth higher"
401 " than parent \"%s\"", parent->qname);
406 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
409 switch (pa->scheduler) {
411 error = eval_pfqueue_cbq(pf, pa);
414 error = eval_pfqueue_priq(pf, pa);
417 error = eval_pfqueue_hfsc(pf, pa);
420 error = eval_pfqueue_fairq(pf, pa);
429 * CBQ support functions
431 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
432 #define RM_NS_PER_SEC (1000000000)
435 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
437 struct cbq_opts *opts;
440 if (pa->priority >= CBQ_MAXPRI) {
441 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
445 ifmtu = getifmtu(pa->ifname);
446 opts = &pa->pq_u.cbq_opts;
448 if (opts->pktsize == 0) { /* use default */
449 opts->pktsize = ifmtu;
450 if (opts->pktsize > MCLBYTES) /* do what TCP does */
451 opts->pktsize &= ~MCLBYTES;
452 } else if (opts->pktsize > ifmtu)
453 opts->pktsize = ifmtu;
454 if (opts->maxpktsize == 0) /* use default */
455 opts->maxpktsize = ifmtu;
456 else if (opts->maxpktsize > ifmtu)
457 opts->pktsize = ifmtu;
459 if (opts->pktsize > opts->maxpktsize)
460 opts->pktsize = opts->maxpktsize;
462 if (pa->parent[0] == 0)
463 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
465 cbq_compute_idletime(pf, pa);
470 * compute ns_per_byte, maxidle, minidle, and offtime
473 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
475 struct cbq_opts *opts;
476 double maxidle_s, maxidle, minidle;
477 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
478 double z, g, f, gton, gtom;
479 u_int minburst, maxburst;
481 opts = &pa->pq_u.cbq_opts;
482 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
483 minburst = opts->minburst;
484 maxburst = opts->maxburst;
486 if (pa->bandwidth == 0)
487 f = 0.0001; /* small enough? */
489 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
491 nsPerByte = ifnsPerByte / f;
492 ptime = (double)opts->pktsize * ifnsPerByte;
493 cptime = ptime * (1.0 - f) / f;
495 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
497 * this causes integer overflow in kernel!
498 * (bandwidth < 6Kbps when max_pkt_size=1500)
500 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0) {
501 warnx("queue bandwidth must be larger than %s",
502 rate2str(ifnsPerByte * (double)opts->maxpktsize /
503 (double)INT_MAX * (double)pa->ifbandwidth));
504 fprintf(stderr, "cbq: queue %s is too slow!\n",
507 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
510 if (maxburst == 0) { /* use default */
511 if (cptime > 10.0 * 1000000)
516 if (minburst == 0) /* use default */
518 if (minburst > maxburst)
521 z = (double)(1 << RM_FILTER_GAIN);
523 gton = pow(g, (double)maxburst);
524 gtom = pow(g, (double)(minburst-1));
525 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
526 maxidle_s = (1.0 - g);
527 if (maxidle > maxidle_s)
528 maxidle = ptime * maxidle;
530 maxidle = ptime * maxidle_s;
531 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
532 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
534 /* scale parameters */
535 maxidle = ((maxidle * 8.0) / nsPerByte) *
536 pow(2.0, (double)RM_FILTER_GAIN);
537 offtime = (offtime * 8.0) / nsPerByte *
538 pow(2.0, (double)RM_FILTER_GAIN);
539 minidle = ((minidle * 8.0) / nsPerByte) *
540 pow(2.0, (double)RM_FILTER_GAIN);
542 maxidle = maxidle / 1000.0;
543 offtime = offtime / 1000.0;
544 minidle = minidle / 1000.0;
546 opts->minburst = minburst;
547 opts->maxburst = maxburst;
548 opts->ns_per_byte = (u_int)nsPerByte;
549 opts->maxidle = (u_int)fabs(maxidle);
550 opts->minidle = (int)minidle;
551 opts->offtime = (u_int)fabs(offtime);
557 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
559 struct pf_altq *altq;
560 int root_class, default_class;
564 * check if cbq has one root queue and one default queue
567 root_class = default_class = 0;
568 TAILQ_FOREACH(altq, &altqs, entries) {
569 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
571 if (altq->qname[0] == 0) /* this is for interface */
573 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
575 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
578 if (root_class != 1) {
579 warnx("should have one root queue on %s", pa->ifname);
582 if (default_class != 1) {
583 warnx("should have one default queue on %s", pa->ifname);
590 print_cbq_opts(const struct pf_altq *a)
592 const struct cbq_opts *opts;
594 opts = &a->pq_u.cbq_opts;
597 if (opts->flags & CBQCLF_RED)
599 if (opts->flags & CBQCLF_ECN)
601 if (opts->flags & CBQCLF_RIO)
603 if (opts->flags & CBQCLF_CODEL)
605 if (opts->flags & CBQCLF_CLEARDSCP)
606 printf(" cleardscp");
607 if (opts->flags & CBQCLF_FLOWVALVE)
608 printf(" flowvalve");
609 if (opts->flags & CBQCLF_BORROW)
611 if (opts->flags & CBQCLF_WRR)
613 if (opts->flags & CBQCLF_EFFICIENT)
614 printf(" efficient");
615 if (opts->flags & CBQCLF_ROOTCLASS)
617 if (opts->flags & CBQCLF_DEFCLASS)
627 * PRIQ support functions
630 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
632 struct pf_altq *altq;
634 if (pa->priority >= PRIQ_MAXPRI) {
635 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
638 /* the priority should be unique for the interface */
639 TAILQ_FOREACH(altq, &altqs, entries) {
640 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
641 altq->qname[0] != 0 && altq->priority == pa->priority) {
642 warnx("%s and %s have the same priority",
643 altq->qname, pa->qname);
652 check_commit_priq(int dev, int opts, struct pf_altq *pa)
654 struct pf_altq *altq;
659 * check if priq has one default class for this interface
662 TAILQ_FOREACH(altq, &altqs, entries) {
663 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
665 if (altq->qname[0] == 0) /* this is for interface */
667 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
670 if (default_class != 1) {
671 warnx("should have one default queue on %s", pa->ifname);
678 print_priq_opts(const struct pf_altq *a)
680 const struct priq_opts *opts;
682 opts = &a->pq_u.priq_opts;
686 if (opts->flags & PRCF_RED)
688 if (opts->flags & PRCF_ECN)
690 if (opts->flags & PRCF_RIO)
692 if (opts->flags & PRCF_CODEL)
694 if (opts->flags & PRCF_CLEARDSCP)
695 printf(" cleardscp");
696 if (opts->flags & PRCF_DEFAULTCLASS)
706 * HFSC support functions
709 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
711 struct pf_altq *altq, *parent;
712 struct hfsc_opts *opts;
713 struct service_curve sc;
715 opts = &pa->pq_u.hfsc_opts;
717 if (pa->parent[0] == 0) {
719 opts->lssc_m1 = pa->ifbandwidth;
720 opts->lssc_m2 = pa->ifbandwidth;
728 /* if link_share is not specified, use bandwidth */
729 if (opts->lssc_m2 == 0)
730 opts->lssc_m2 = pa->bandwidth;
732 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
733 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
734 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
735 warnx("m2 is zero for %s", pa->qname);
739 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
740 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
741 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
742 warnx("m1 must be zero for convex curve: %s", pa->qname);
748 * for the real-time service curve, the sum of the service curves
749 * should not exceed 80% of the interface bandwidth. 20% is reserved
750 * not to over-commit the actual interface bandwidth.
751 * for the linkshare service curve, the sum of the child service
752 * curve should not exceed the parent service curve.
753 * for the upper-limit service curve, the assigned bandwidth should
754 * be smaller than the interface bandwidth, and the upper-limit should
755 * be larger than the real-time service curve when both are defined.
757 parent = qname_to_pfaltq(pa->parent, pa->ifname);
759 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
761 TAILQ_FOREACH(altq, &altqs, entries) {
762 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
764 if (altq->qname[0] == 0) /* this is for interface */
767 /* if the class has a real-time service curve, add it. */
768 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
769 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
770 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
771 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
772 gsc_add_sc(&rtsc, &sc);
775 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
778 /* if the class has a linkshare service curve, add it. */
779 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
780 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
781 sc.d = altq->pq_u.hfsc_opts.lssc_d;
782 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
783 gsc_add_sc(&lssc, &sc);
787 /* check the real-time service curve. reserve 20% of interface bw */
788 if (opts->rtsc_m2 != 0) {
789 /* add this queue to the sum */
790 sc.m1 = opts->rtsc_m1;
792 sc.m2 = opts->rtsc_m2;
793 gsc_add_sc(&rtsc, &sc);
794 /* compare the sum with 80% of the interface */
797 sc.m2 = pa->ifbandwidth / 100 * 80;
798 if (!is_gsc_under_sc(&rtsc, &sc)) {
799 warnx("real-time sc exceeds 80%% of the interface "
800 "bandwidth (%s)", rate2str((double)sc.m2));
805 /* check the linkshare service curve. */
806 if (opts->lssc_m2 != 0) {
807 /* add this queue to the child sum */
808 sc.m1 = opts->lssc_m1;
810 sc.m2 = opts->lssc_m2;
811 gsc_add_sc(&lssc, &sc);
812 /* compare the sum of the children with parent's sc */
813 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
814 sc.d = parent->pq_u.hfsc_opts.lssc_d;
815 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
816 if (!is_gsc_under_sc(&lssc, &sc)) {
817 warnx("linkshare sc exceeds parent's sc");
822 /* check the upper-limit service curve. */
823 if (opts->ulsc_m2 != 0) {
824 if (opts->ulsc_m1 > pa->ifbandwidth ||
825 opts->ulsc_m2 > pa->ifbandwidth) {
826 warnx("upper-limit larger than interface bandwidth");
829 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
830 warnx("upper-limit sc smaller than real-time sc");
847 * FAIRQ support functions
850 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
852 struct pf_altq *altq, *parent;
853 struct fairq_opts *opts;
854 struct service_curve sc;
856 opts = &pa->pq_u.fairq_opts;
858 if (pa->parent[0] == 0) {
860 opts->lssc_m1 = pa->ifbandwidth;
861 opts->lssc_m2 = pa->ifbandwidth;
868 /* if link_share is not specified, use bandwidth */
869 if (opts->lssc_m2 == 0)
870 opts->lssc_m2 = pa->bandwidth;
874 * for the real-time service curve, the sum of the service curves
875 * should not exceed 80% of the interface bandwidth. 20% is reserved
876 * not to over-commit the actual interface bandwidth.
877 * for the link-sharing service curve, the sum of the child service
878 * curve should not exceed the parent service curve.
879 * for the upper-limit service curve, the assigned bandwidth should
880 * be smaller than the interface bandwidth, and the upper-limit should
881 * be larger than the real-time service curve when both are defined.
883 parent = qname_to_pfaltq(pa->parent, pa->ifname);
885 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
887 TAILQ_FOREACH(altq, &altqs, entries) {
888 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
890 if (altq->qname[0] == 0) /* this is for interface */
893 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
896 /* if the class has a link-sharing service curve, add it. */
897 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
898 sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
899 sc.d = altq->pq_u.fairq_opts.lssc_d;
900 sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
901 gsc_add_sc(&lssc, &sc);
905 /* check the link-sharing service curve. */
906 if (opts->lssc_m2 != 0) {
907 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
908 sc.d = parent->pq_u.fairq_opts.lssc_d;
909 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
910 if (!is_gsc_under_sc(&lssc, &sc)) {
911 warnx("link-sharing sc exceeds parent's sc");
926 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
928 struct pf_altq *altq, *def = NULL;
932 /* check if hfsc has one default queue for this interface */
934 TAILQ_FOREACH(altq, &altqs, entries) {
935 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
937 if (altq->qname[0] == 0) /* this is for interface */
939 if (altq->parent[0] == 0) /* dummy root */
941 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
946 if (default_class != 1) {
947 warnx("should have one default queue on %s", pa->ifname);
950 /* make sure the default queue is a leaf */
951 TAILQ_FOREACH(altq, &altqs, entries) {
952 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
954 if (altq->qname[0] == 0) /* this is for interface */
956 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
957 warnx("default queue is not a leaf");
965 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
967 struct pf_altq *altq, *def = NULL;
971 /* check if fairq has one default queue for this interface */
973 TAILQ_FOREACH(altq, &altqs, entries) {
974 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
976 if (altq->qname[0] == 0) /* this is for interface */
978 if (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
983 if (default_class != 1) {
984 warnx("should have one default queue on %s", pa->ifname);
987 /* make sure the default queue is a leaf */
988 TAILQ_FOREACH(altq, &altqs, entries) {
989 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
991 if (altq->qname[0] == 0) /* this is for interface */
993 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
994 warnx("default queue is not a leaf");
1002 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1004 const struct hfsc_opts *opts;
1005 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
1007 opts = &a->pq_u.hfsc_opts;
1009 rtsc = lssc = ulsc = NULL;
1011 rtsc = &qopts->data.hfsc_opts.realtime;
1012 lssc = &qopts->data.hfsc_opts.linkshare;
1013 ulsc = &qopts->data.hfsc_opts.upperlimit;
1016 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
1017 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1018 opts->lssc_d != 0))) {
1020 if (opts->flags & HFCF_RED)
1022 if (opts->flags & HFCF_ECN)
1024 if (opts->flags & HFCF_RIO)
1026 if (opts->flags & HFCF_CODEL)
1028 if (opts->flags & HFCF_CLEARDSCP)
1029 printf(" cleardscp");
1030 if (opts->flags & HFCF_DEFAULTCLASS)
1032 if (opts->rtsc_m2 != 0)
1033 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
1034 opts->rtsc_m2, rtsc);
1035 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1037 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1038 opts->lssc_m2, lssc);
1039 if (opts->ulsc_m2 != 0)
1040 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1041 opts->ulsc_m2, ulsc);
1050 print_codel_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1052 const struct codel_opts *opts;
1054 opts = &a->pq_u.codel_opts;
1055 if (opts->target || opts->interval || opts->ecn) {
1058 printf(" target %d", opts->target);
1060 printf(" interval %d", opts->interval);
1072 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1074 const struct fairq_opts *opts;
1075 const struct node_fairq_sc *loc_lssc;
1077 opts = &a->pq_u.fairq_opts;
1081 loc_lssc = &qopts->data.fairq_opts.linkshare;
1084 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1085 opts->lssc_d != 0))) {
1087 if (opts->flags & FARF_RED)
1089 if (opts->flags & FARF_ECN)
1091 if (opts->flags & FARF_RIO)
1093 if (opts->flags & FARF_CODEL)
1095 if (opts->flags & FARF_CLEARDSCP)
1096 printf(" cleardscp");
1097 if (opts->flags & FARF_DEFAULTCLASS)
1099 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1101 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1102 opts->lssc_m2, loc_lssc);
1111 * admission control using generalized service curve
1114 /* add a new service curve to a generalized service curve */
1116 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1121 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1122 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1126 * check whether all points of a generalized service curve have
1127 * their y-coordinates no larger than a given two-piece linear
1131 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1133 struct segment *s, *last, *end;
1136 if (is_sc_null(sc)) {
1137 if (LIST_EMPTY(gsc))
1139 LIST_FOREACH(s, gsc, _next) {
1146 * gsc has a dummy entry at the end with x = INFINITY.
1147 * loop through up to this dummy entry.
1149 end = gsc_getentry(gsc, INFINITY);
1153 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1154 if (s->y > sc_x2y(sc, s->x))
1158 /* last now holds the real last segment */
1161 if (last->m > sc->m2)
1163 if (last->x < sc->d && last->m > sc->m1) {
1164 y = last->y + (sc->d - last->x) * last->m;
1165 if (y > sc_x2y(sc, sc->d))
1172 gsc_destroy(struct gen_sc *gsc)
1176 while ((s = LIST_FIRST(gsc)) != NULL) {
1177 LIST_REMOVE(s, _next);
1183 * return a segment entry starting at x.
1184 * if gsc has no entry starting at x, a new entry is created at x.
1186 static struct segment *
1187 gsc_getentry(struct gen_sc *gsc, double x)
1189 struct segment *new, *prev, *s;
1192 LIST_FOREACH(s, gsc, _next) {
1194 return (s); /* matching entry found */
1201 /* we have to create a new entry */
1202 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1206 if (x == INFINITY || s == NULL)
1208 else if (s->x == INFINITY)
1213 /* insert the new entry at the head of the list */
1216 LIST_INSERT_HEAD(gsc, new, _next);
1219 * the start point intersects with the segment pointed by
1220 * prev. divide prev into 2 segments
1222 if (x == INFINITY) {
1229 prev->d = x - prev->x;
1230 new->y = prev->d * prev->m + prev->y;
1233 LIST_INSERT_AFTER(prev, new, _next);
1238 /* add a segment to a generalized service curve */
1240 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1242 struct segment *start, *end, *s;
1249 start = gsc_getentry(gsc, x);
1250 end = gsc_getentry(gsc, x2);
1251 if (start == NULL || end == NULL)
1254 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1256 s->y += y + (s->x - x) * m;
1259 end = gsc_getentry(gsc, INFINITY);
1260 for (; s != end; s = LIST_NEXT(s, _next)) {
1267 /* get y-projection of a service curve */
1269 sc_x2y(struct service_curve *sc, double x)
1273 if (x <= (double)sc->d)
1274 /* y belongs to the 1st segment */
1275 y = x * (double)sc->m1;
1277 /* y belongs to the 2nd segment */
1278 y = (double)sc->d * (double)sc->m1
1279 + (x - (double)sc->d) * (double)sc->m2;
1287 #define RATESTR_MAX 16
1290 rate2str(double rate)
1293 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1296 static const char unit[] = " KMG";
1298 buf = r2sbuf[idx++];
1299 if (idx == R2S_BUFS)
1302 for (i = 0; rate >= 1000 && i <= 3; i++)
1305 if ((int)(rate * 100) % 100)
1306 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1308 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1316 * FreeBSD does not have SIOCGIFDATA.
1317 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1320 getifspeed(int pfdev, char *ifname)
1322 struct pf_ifspeed io;
1324 bzero(&io, sizeof io);
1325 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1327 errx(1, "getifspeed: strlcpy");
1328 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1329 err(1, "DIOCGIFSPEED");
1330 return ((u_int32_t)io.baudrate);
1334 getifspeed(char *ifname)
1338 struct if_data ifrdat;
1340 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1342 bzero(&ifr, sizeof(ifr));
1343 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1344 sizeof(ifr.ifr_name))
1345 errx(1, "getifspeed: strlcpy");
1346 ifr.ifr_data = (caddr_t)&ifrdat;
1347 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1348 err(1, "SIOCGIFDATA");
1351 return ((u_int32_t)ifrdat.ifi_baudrate);
1356 getifmtu(char *ifname)
1361 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1363 bzero(&ifr, sizeof(ifr));
1364 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1365 sizeof(ifr.ifr_name))
1366 errx(1, "getifmtu: strlcpy");
1367 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1371 err(1, "SIOCGIFMTU");
1375 if (ifr.ifr_mtu > 0)
1376 return (ifr.ifr_mtu);
1378 warnx("could not get mtu for %s, assuming 1500", ifname);
1384 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1389 switch (pa->scheduler) {
1391 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1394 pa->pq_u.priq_opts = opts->data.priq_opts;
1397 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1398 if (opts->data.hfsc_opts.linkshare.used) {
1399 pa->pq_u.hfsc_opts.lssc_m1 =
1400 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1402 pa->pq_u.hfsc_opts.lssc_m2 =
1403 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1405 pa->pq_u.hfsc_opts.lssc_d =
1406 opts->data.hfsc_opts.linkshare.d;
1408 if (opts->data.hfsc_opts.realtime.used) {
1409 pa->pq_u.hfsc_opts.rtsc_m1 =
1410 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1412 pa->pq_u.hfsc_opts.rtsc_m2 =
1413 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1415 pa->pq_u.hfsc_opts.rtsc_d =
1416 opts->data.hfsc_opts.realtime.d;
1418 if (opts->data.hfsc_opts.upperlimit.used) {
1419 pa->pq_u.hfsc_opts.ulsc_m1 =
1420 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1422 pa->pq_u.hfsc_opts.ulsc_m2 =
1423 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1425 pa->pq_u.hfsc_opts.ulsc_d =
1426 opts->data.hfsc_opts.upperlimit.d;
1430 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1431 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1432 pa->pq_u.fairq_opts.hogs_m1 =
1433 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1435 if (opts->data.fairq_opts.linkshare.used) {
1436 pa->pq_u.fairq_opts.lssc_m1 =
1437 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1439 pa->pq_u.fairq_opts.lssc_m2 =
1440 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1442 pa->pq_u.fairq_opts.lssc_d =
1443 opts->data.fairq_opts.linkshare.d;
1447 pa->pq_u.codel_opts.target = opts->data.codel_opts.target;
1448 pa->pq_u.codel_opts.interval = opts->data.codel_opts.interval;
1449 pa->pq_u.codel_opts.ecn = opts->data.codel_opts.ecn;
1452 warnx("eval_queue_opts: unknown scheduler type %u",
1462 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1464 if (bw->bw_absolute > 0)
1465 return (bw->bw_absolute);
1467 if (bw->bw_percent > 0)
1468 return (ref_bw / 100 * bw->bw_percent);
1474 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1475 const struct node_hfsc_sc *sc)
1477 printf(" %s", scname);
1481 if (sc != NULL && sc->m1.bw_percent > 0)
1482 printf("%u%%", sc->m1.bw_percent);
1484 printf("%s", rate2str((double)m1));
1488 if (sc != NULL && sc->m2.bw_percent > 0)
1489 printf(" %u%%", sc->m2.bw_percent);
1491 printf(" %s", rate2str((double)m2));
1498 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1499 const struct node_fairq_sc *sc)
1501 printf(" %s", scname);
1505 if (sc != NULL && sc->m1.bw_percent > 0)
1506 printf("%u%%", sc->m1.bw_percent);
1508 printf("%s", rate2str((double)m1));
1512 if (sc != NULL && sc->m2.bw_percent > 0)
1513 printf(" %u%%", sc->m2.bw_percent);
1515 printf(" %s", rate2str((double)m2));