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 <altq/altq.h>
42 #include <altq/altq_cbq.h>
43 #include <altq/altq_codel.h>
44 #include <altq/altq_priq.h>
45 #include <altq/altq_hfsc.h>
46 #include <altq/altq_fairq.h>
48 #include "pfctl_parser.h"
51 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
53 TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
54 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",
506 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
509 if (maxburst == 0) { /* use default */
510 if (cptime > 10.0 * 1000000)
515 if (minburst == 0) /* use default */
517 if (minburst > maxburst)
520 z = (double)(1 << RM_FILTER_GAIN);
522 gton = pow(g, (double)maxburst);
523 gtom = pow(g, (double)(minburst-1));
524 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
525 maxidle_s = (1.0 - g);
526 if (maxidle > maxidle_s)
527 maxidle = ptime * maxidle;
529 maxidle = ptime * maxidle_s;
530 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
531 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
533 /* scale parameters */
534 maxidle = ((maxidle * 8.0) / nsPerByte) *
535 pow(2.0, (double)RM_FILTER_GAIN);
536 offtime = (offtime * 8.0) / nsPerByte *
537 pow(2.0, (double)RM_FILTER_GAIN);
538 minidle = ((minidle * 8.0) / nsPerByte) *
539 pow(2.0, (double)RM_FILTER_GAIN);
541 maxidle = maxidle / 1000.0;
542 offtime = offtime / 1000.0;
543 minidle = minidle / 1000.0;
545 opts->minburst = minburst;
546 opts->maxburst = maxburst;
547 opts->ns_per_byte = (u_int)nsPerByte;
548 opts->maxidle = (u_int)fabs(maxidle);
549 opts->minidle = (int)minidle;
550 opts->offtime = (u_int)fabs(offtime);
556 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
558 struct pf_altq *altq;
559 int root_class, default_class;
563 * check if cbq has one root queue and one default queue
566 root_class = default_class = 0;
567 TAILQ_FOREACH(altq, &altqs, entries) {
568 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
570 if (altq->qname[0] == 0) /* this is for interface */
572 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
574 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
577 if (root_class != 1) {
578 warnx("should have one root queue on %s", pa->ifname);
581 if (default_class != 1) {
582 warnx("should have one default queue on %s", pa->ifname);
589 print_cbq_opts(const struct pf_altq *a)
591 const struct cbq_opts *opts;
593 opts = &a->pq_u.cbq_opts;
596 if (opts->flags & CBQCLF_RED)
598 if (opts->flags & CBQCLF_ECN)
600 if (opts->flags & CBQCLF_RIO)
602 if (opts->flags & CBQCLF_CODEL)
604 if (opts->flags & CBQCLF_CLEARDSCP)
605 printf(" cleardscp");
606 if (opts->flags & CBQCLF_FLOWVALVE)
607 printf(" flowvalve");
608 if (opts->flags & CBQCLF_BORROW)
610 if (opts->flags & CBQCLF_WRR)
612 if (opts->flags & CBQCLF_EFFICIENT)
613 printf(" efficient");
614 if (opts->flags & CBQCLF_ROOTCLASS)
616 if (opts->flags & CBQCLF_DEFCLASS)
626 * PRIQ support functions
629 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
631 struct pf_altq *altq;
633 if (pa->priority >= PRIQ_MAXPRI) {
634 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
637 /* the priority should be unique for the interface */
638 TAILQ_FOREACH(altq, &altqs, entries) {
639 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
640 altq->qname[0] != 0 && altq->priority == pa->priority) {
641 warnx("%s and %s have the same priority",
642 altq->qname, pa->qname);
651 check_commit_priq(int dev, int opts, struct pf_altq *pa)
653 struct pf_altq *altq;
658 * check if priq has one default class for this interface
661 TAILQ_FOREACH(altq, &altqs, entries) {
662 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
664 if (altq->qname[0] == 0) /* this is for interface */
666 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
669 if (default_class != 1) {
670 warnx("should have one default queue on %s", pa->ifname);
677 print_priq_opts(const struct pf_altq *a)
679 const struct priq_opts *opts;
681 opts = &a->pq_u.priq_opts;
685 if (opts->flags & PRCF_RED)
687 if (opts->flags & PRCF_ECN)
689 if (opts->flags & PRCF_RIO)
691 if (opts->flags & PRCF_CODEL)
693 if (opts->flags & PRCF_CLEARDSCP)
694 printf(" cleardscp");
695 if (opts->flags & PRCF_DEFAULTCLASS)
705 * HFSC support functions
708 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
710 struct pf_altq *altq, *parent;
711 struct hfsc_opts *opts;
712 struct service_curve sc;
714 opts = &pa->pq_u.hfsc_opts;
716 if (pa->parent[0] == 0) {
718 opts->lssc_m1 = pa->ifbandwidth;
719 opts->lssc_m2 = pa->ifbandwidth;
727 /* if link_share is not specified, use bandwidth */
728 if (opts->lssc_m2 == 0)
729 opts->lssc_m2 = pa->bandwidth;
731 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
732 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
733 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
734 warnx("m2 is zero for %s", pa->qname);
738 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
739 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
740 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
741 warnx("m1 must be zero for convex curve: %s", pa->qname);
747 * for the real-time service curve, the sum of the service curves
748 * should not exceed 80% of the interface bandwidth. 20% is reserved
749 * not to over-commit the actual interface bandwidth.
750 * for the linkshare service curve, the sum of the child service
751 * curve should not exceed the parent service curve.
752 * for the upper-limit service curve, the assigned bandwidth should
753 * be smaller than the interface bandwidth, and the upper-limit should
754 * be larger than the real-time service curve when both are defined.
756 parent = qname_to_pfaltq(pa->parent, pa->ifname);
758 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
760 TAILQ_FOREACH(altq, &altqs, entries) {
761 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
763 if (altq->qname[0] == 0) /* this is for interface */
766 /* if the class has a real-time service curve, add it. */
767 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
768 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
769 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
770 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
771 gsc_add_sc(&rtsc, &sc);
774 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
777 /* if the class has a linkshare service curve, add it. */
778 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
779 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
780 sc.d = altq->pq_u.hfsc_opts.lssc_d;
781 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
782 gsc_add_sc(&lssc, &sc);
786 /* check the real-time service curve. reserve 20% of interface bw */
787 if (opts->rtsc_m2 != 0) {
788 /* add this queue to the sum */
789 sc.m1 = opts->rtsc_m1;
791 sc.m2 = opts->rtsc_m2;
792 gsc_add_sc(&rtsc, &sc);
793 /* compare the sum with 80% of the interface */
796 sc.m2 = pa->ifbandwidth / 100 * 80;
797 if (!is_gsc_under_sc(&rtsc, &sc)) {
798 warnx("real-time sc exceeds 80%% of the interface "
799 "bandwidth (%s)", rate2str((double)sc.m2));
804 /* check the linkshare service curve. */
805 if (opts->lssc_m2 != 0) {
806 /* add this queue to the child sum */
807 sc.m1 = opts->lssc_m1;
809 sc.m2 = opts->lssc_m2;
810 gsc_add_sc(&lssc, &sc);
811 /* compare the sum of the children with parent's sc */
812 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
813 sc.d = parent->pq_u.hfsc_opts.lssc_d;
814 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
815 if (!is_gsc_under_sc(&lssc, &sc)) {
816 warnx("linkshare sc exceeds parent's sc");
821 /* check the upper-limit service curve. */
822 if (opts->ulsc_m2 != 0) {
823 if (opts->ulsc_m1 > pa->ifbandwidth ||
824 opts->ulsc_m2 > pa->ifbandwidth) {
825 warnx("upper-limit larger than interface bandwidth");
828 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
829 warnx("upper-limit sc smaller than real-time sc");
846 * FAIRQ support functions
849 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
851 struct pf_altq *altq, *parent;
852 struct fairq_opts *opts;
853 struct service_curve sc;
855 opts = &pa->pq_u.fairq_opts;
857 if (pa->parent[0] == 0) {
859 opts->lssc_m1 = pa->ifbandwidth;
860 opts->lssc_m2 = pa->ifbandwidth;
867 /* if link_share is not specified, use bandwidth */
868 if (opts->lssc_m2 == 0)
869 opts->lssc_m2 = pa->bandwidth;
873 * for the real-time service curve, the sum of the service curves
874 * should not exceed 80% of the interface bandwidth. 20% is reserved
875 * not to over-commit the actual interface bandwidth.
876 * for the link-sharing service curve, the sum of the child service
877 * curve should not exceed the parent service curve.
878 * for the upper-limit service curve, the assigned bandwidth should
879 * be smaller than the interface bandwidth, and the upper-limit should
880 * be larger than the real-time service curve when both are defined.
882 parent = qname_to_pfaltq(pa->parent, pa->ifname);
884 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
886 TAILQ_FOREACH(altq, &altqs, entries) {
887 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
889 if (altq->qname[0] == 0) /* this is for interface */
892 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
895 /* if the class has a link-sharing service curve, add it. */
896 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
897 sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
898 sc.d = altq->pq_u.fairq_opts.lssc_d;
899 sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
900 gsc_add_sc(&lssc, &sc);
904 /* check the link-sharing service curve. */
905 if (opts->lssc_m2 != 0) {
906 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
907 sc.d = parent->pq_u.fairq_opts.lssc_d;
908 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
909 if (!is_gsc_under_sc(&lssc, &sc)) {
910 warnx("link-sharing sc exceeds parent's sc");
925 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
927 struct pf_altq *altq, *def = NULL;
931 /* check if hfsc has one default queue for this interface */
933 TAILQ_FOREACH(altq, &altqs, entries) {
934 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
936 if (altq->qname[0] == 0) /* this is for interface */
938 if (altq->parent[0] == 0) /* dummy root */
940 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
945 if (default_class != 1) {
946 warnx("should have one default queue on %s", pa->ifname);
949 /* make sure the default queue is a leaf */
950 TAILQ_FOREACH(altq, &altqs, entries) {
951 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
953 if (altq->qname[0] == 0) /* this is for interface */
955 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
956 warnx("default queue is not a leaf");
964 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
966 struct pf_altq *altq, *def = NULL;
970 /* check if fairq has one default queue for this interface */
972 TAILQ_FOREACH(altq, &altqs, entries) {
973 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
975 if (altq->qname[0] == 0) /* this is for interface */
977 if (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
982 if (default_class != 1) {
983 warnx("should have one default queue on %s", pa->ifname);
986 /* make sure the default queue is a leaf */
987 TAILQ_FOREACH(altq, &altqs, entries) {
988 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
990 if (altq->qname[0] == 0) /* this is for interface */
992 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
993 warnx("default queue is not a leaf");
1001 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1003 const struct hfsc_opts *opts;
1004 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
1006 opts = &a->pq_u.hfsc_opts;
1008 rtsc = lssc = ulsc = NULL;
1010 rtsc = &qopts->data.hfsc_opts.realtime;
1011 lssc = &qopts->data.hfsc_opts.linkshare;
1012 ulsc = &qopts->data.hfsc_opts.upperlimit;
1015 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
1016 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1017 opts->lssc_d != 0))) {
1019 if (opts->flags & HFCF_RED)
1021 if (opts->flags & HFCF_ECN)
1023 if (opts->flags & HFCF_RIO)
1025 if (opts->flags & HFCF_CODEL)
1027 if (opts->flags & HFCF_CLEARDSCP)
1028 printf(" cleardscp");
1029 if (opts->flags & HFCF_DEFAULTCLASS)
1031 if (opts->rtsc_m2 != 0)
1032 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
1033 opts->rtsc_m2, rtsc);
1034 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1036 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1037 opts->lssc_m2, lssc);
1038 if (opts->ulsc_m2 != 0)
1039 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1040 opts->ulsc_m2, ulsc);
1049 print_codel_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1051 const struct codel_opts *opts;
1053 opts = &a->pq_u.codel_opts;
1054 if (opts->target || opts->interval || opts->ecn) {
1057 printf(" target %d", opts->target);
1059 printf(" interval %d", opts->interval);
1071 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1073 const struct fairq_opts *opts;
1074 const struct node_fairq_sc *loc_lssc;
1076 opts = &a->pq_u.fairq_opts;
1080 loc_lssc = &qopts->data.fairq_opts.linkshare;
1083 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1084 opts->lssc_d != 0))) {
1086 if (opts->flags & FARF_RED)
1088 if (opts->flags & FARF_ECN)
1090 if (opts->flags & FARF_RIO)
1092 if (opts->flags & FARF_CODEL)
1094 if (opts->flags & FARF_CLEARDSCP)
1095 printf(" cleardscp");
1096 if (opts->flags & FARF_DEFAULTCLASS)
1098 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1100 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1101 opts->lssc_m2, loc_lssc);
1110 * admission control using generalized service curve
1113 /* add a new service curve to a generalized service curve */
1115 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1120 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1121 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1125 * check whether all points of a generalized service curve have
1126 * their y-coordinates no larger than a given two-piece linear
1130 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1132 struct segment *s, *last, *end;
1135 if (is_sc_null(sc)) {
1136 if (LIST_EMPTY(gsc))
1138 LIST_FOREACH(s, gsc, _next) {
1145 * gsc has a dummy entry at the end with x = INFINITY.
1146 * loop through up to this dummy entry.
1148 end = gsc_getentry(gsc, INFINITY);
1152 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1153 if (s->y > sc_x2y(sc, s->x))
1157 /* last now holds the real last segment */
1160 if (last->m > sc->m2)
1162 if (last->x < sc->d && last->m > sc->m1) {
1163 y = last->y + (sc->d - last->x) * last->m;
1164 if (y > sc_x2y(sc, sc->d))
1171 gsc_destroy(struct gen_sc *gsc)
1175 while ((s = LIST_FIRST(gsc)) != NULL) {
1176 LIST_REMOVE(s, _next);
1182 * return a segment entry starting at x.
1183 * if gsc has no entry starting at x, a new entry is created at x.
1185 static struct segment *
1186 gsc_getentry(struct gen_sc *gsc, double x)
1188 struct segment *new, *prev, *s;
1191 LIST_FOREACH(s, gsc, _next) {
1193 return (s); /* matching entry found */
1200 /* we have to create a new entry */
1201 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1205 if (x == INFINITY || s == NULL)
1207 else if (s->x == INFINITY)
1212 /* insert the new entry at the head of the list */
1215 LIST_INSERT_HEAD(gsc, new, _next);
1218 * the start point intersects with the segment pointed by
1219 * prev. divide prev into 2 segments
1221 if (x == INFINITY) {
1228 prev->d = x - prev->x;
1229 new->y = prev->d * prev->m + prev->y;
1232 LIST_INSERT_AFTER(prev, new, _next);
1237 /* add a segment to a generalized service curve */
1239 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1241 struct segment *start, *end, *s;
1248 start = gsc_getentry(gsc, x);
1249 end = gsc_getentry(gsc, x2);
1250 if (start == NULL || end == NULL)
1253 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1255 s->y += y + (s->x - x) * m;
1258 end = gsc_getentry(gsc, INFINITY);
1259 for (; s != end; s = LIST_NEXT(s, _next)) {
1266 /* get y-projection of a service curve */
1268 sc_x2y(struct service_curve *sc, double x)
1272 if (x <= (double)sc->d)
1273 /* y belongs to the 1st segment */
1274 y = x * (double)sc->m1;
1276 /* y belongs to the 2nd segment */
1277 y = (double)sc->d * (double)sc->m1
1278 + (x - (double)sc->d) * (double)sc->m2;
1286 #define RATESTR_MAX 16
1289 rate2str(double rate)
1292 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1295 static const char unit[] = " KMG";
1297 buf = r2sbuf[idx++];
1298 if (idx == R2S_BUFS)
1301 for (i = 0; rate >= 1000 && i <= 3; i++)
1304 if ((int)(rate * 100) % 100)
1305 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1307 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1315 * FreeBSD does not have SIOCGIFDATA.
1316 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1319 getifspeed(int pfdev, char *ifname)
1321 struct pf_ifspeed io;
1323 bzero(&io, sizeof io);
1324 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1326 errx(1, "getifspeed: strlcpy");
1327 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1328 err(1, "DIOCGIFSPEED");
1329 return ((u_int32_t)io.baudrate);
1333 getifspeed(char *ifname)
1337 struct if_data ifrdat;
1339 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1341 bzero(&ifr, sizeof(ifr));
1342 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1343 sizeof(ifr.ifr_name))
1344 errx(1, "getifspeed: strlcpy");
1345 ifr.ifr_data = (caddr_t)&ifrdat;
1346 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1347 err(1, "SIOCGIFDATA");
1350 return ((u_int32_t)ifrdat.ifi_baudrate);
1355 getifmtu(char *ifname)
1360 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1362 bzero(&ifr, sizeof(ifr));
1363 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1364 sizeof(ifr.ifr_name))
1365 errx(1, "getifmtu: strlcpy");
1366 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1370 err(1, "SIOCGIFMTU");
1374 if (ifr.ifr_mtu > 0)
1375 return (ifr.ifr_mtu);
1377 warnx("could not get mtu for %s, assuming 1500", ifname);
1383 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1388 switch (pa->scheduler) {
1390 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1393 pa->pq_u.priq_opts = opts->data.priq_opts;
1396 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1397 if (opts->data.hfsc_opts.linkshare.used) {
1398 pa->pq_u.hfsc_opts.lssc_m1 =
1399 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1401 pa->pq_u.hfsc_opts.lssc_m2 =
1402 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1404 pa->pq_u.hfsc_opts.lssc_d =
1405 opts->data.hfsc_opts.linkshare.d;
1407 if (opts->data.hfsc_opts.realtime.used) {
1408 pa->pq_u.hfsc_opts.rtsc_m1 =
1409 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1411 pa->pq_u.hfsc_opts.rtsc_m2 =
1412 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1414 pa->pq_u.hfsc_opts.rtsc_d =
1415 opts->data.hfsc_opts.realtime.d;
1417 if (opts->data.hfsc_opts.upperlimit.used) {
1418 pa->pq_u.hfsc_opts.ulsc_m1 =
1419 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1421 pa->pq_u.hfsc_opts.ulsc_m2 =
1422 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1424 pa->pq_u.hfsc_opts.ulsc_d =
1425 opts->data.hfsc_opts.upperlimit.d;
1429 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1430 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1431 pa->pq_u.fairq_opts.hogs_m1 =
1432 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1434 if (opts->data.fairq_opts.linkshare.used) {
1435 pa->pq_u.fairq_opts.lssc_m1 =
1436 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1438 pa->pq_u.fairq_opts.lssc_m2 =
1439 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1441 pa->pq_u.fairq_opts.lssc_d =
1442 opts->data.fairq_opts.linkshare.d;
1446 pa->pq_u.codel_opts.target = opts->data.codel_opts.target;
1447 pa->pq_u.codel_opts.interval = opts->data.codel_opts.interval;
1448 pa->pq_u.codel_opts.ecn = opts->data.codel_opts.ecn;
1451 warnx("eval_queue_opts: unknown scheduler type %u",
1461 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1463 if (bw->bw_absolute > 0)
1464 return (bw->bw_absolute);
1466 if (bw->bw_percent > 0)
1467 return (ref_bw / 100 * bw->bw_percent);
1473 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1474 const struct node_hfsc_sc *sc)
1476 printf(" %s", scname);
1480 if (sc != NULL && sc->m1.bw_percent > 0)
1481 printf("%u%%", sc->m1.bw_percent);
1483 printf("%s", rate2str((double)m1));
1487 if (sc != NULL && sc->m2.bw_percent > 0)
1488 printf(" %u%%", sc->m2.bw_percent);
1490 printf(" %s", rate2str((double)m2));
1497 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1498 const struct node_fairq_sc *sc)
1500 printf(" %s", scname);
1504 if (sc != NULL && sc->m1.bw_percent > 0)
1505 printf("%u%%", sc->m1.bw_percent);
1507 printf("%s", rate2str((double)m1));
1511 if (sc != NULL && sc->m2.bw_percent > 0)
1512 printf(" %u%%", sc->m2.bw_percent);
1514 printf(" %s", rate2str((double)m2));