1 /* $OpenBSD: pfctl_altq.c,v 1.94 2008/07/25 17:43:44 martynas 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/param.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <sys/sysctl.h>
27 #include <net/if_mib.h>
28 #include <netinet/in.h>
29 #include <net/pf/pfvar.h>
40 #include <net/altq/altq.h>
41 #include <net/altq/altq_cbq.h>
42 #include <net/altq/altq_priq.h>
43 #include <net/altq/altq_hfsc.h>
44 #include <net/altq/altq_fairq.h>
46 #include "pfctl_parser.h"
49 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
51 TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
52 LIST_HEAD(gen_sc, segment) rtsc, lssc;
54 struct pf_altq *qname_to_pfaltq(const char *, const char *);
55 u_int32_t qname_to_qid(const char *);
57 static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *);
58 static int cbq_compute_idletime(struct pfctl *, struct pf_altq *);
59 static int check_commit_cbq(int, int, struct pf_altq *);
60 static int print_cbq_opts(const struct pf_altq *);
62 static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *);
63 static int check_commit_priq(int, int, struct pf_altq *);
64 static int print_priq_opts(const struct pf_altq *);
66 static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *);
67 static int check_commit_hfsc(int, int, struct pf_altq *);
68 static int print_hfsc_opts(const struct pf_altq *,
69 const struct node_queue_opt *);
71 static int eval_pfqueue_fairq(struct pfctl *, struct pf_altq *);
72 static int print_fairq_opts(const struct pf_altq *,
73 const struct node_queue_opt *);
74 static int check_commit_fairq(int, int, struct pf_altq *);
76 static void gsc_add_sc(struct gen_sc *, struct service_curve *);
77 static int is_gsc_under_sc(struct gen_sc *,
78 struct service_curve *);
79 static void gsc_destroy(struct gen_sc *);
80 static struct segment *gsc_getentry(struct gen_sc *, double);
81 static int gsc_add_seg(struct gen_sc *, double, double, double,
83 static double sc_x2y(struct service_curve *, double);
85 u_int32_t getifspeed(const char *);
86 u_long getifmtu(char *);
87 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
89 u_int32_t eval_bwspec(struct node_queue_bw *, u_int32_t);
90 void print_hfsc_sc(const char *, u_int, u_int, u_int,
91 const struct node_hfsc_sc *);
92 void print_fairq_sc(const char *, u_int, u_int, u_int,
93 const struct node_fairq_sc *);
96 pfaltq_store(struct pf_altq *a)
100 if ((altq = malloc(sizeof(*altq))) == NULL)
102 memcpy(altq, a, sizeof(struct pf_altq));
103 TAILQ_INSERT_TAIL(&altqs, altq, entries);
107 pfaltq_lookup(const char *ifname)
109 struct pf_altq *altq;
111 TAILQ_FOREACH(altq, &altqs, entries) {
112 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
120 qname_to_pfaltq(const char *qname, const char *ifname)
122 struct pf_altq *altq;
124 TAILQ_FOREACH(altq, &altqs, entries) {
125 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
126 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
133 qname_to_qid(const char *qname)
135 struct pf_altq *altq;
138 * We guarantee that same named queues on different interfaces
139 * have the same qid, so we do NOT need to limit matching on
143 TAILQ_FOREACH(altq, &altqs, entries) {
144 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
151 print_altq(const struct pf_altq *a, unsigned int level,
152 struct node_queue_bw *bw, struct node_queue_opt *qopts)
154 if (a->qname[0] != 0) {
155 print_queue(a, level, bw, 1, qopts);
159 printf("altq on %s ", a->ifname);
161 switch (a->scheduler) {
163 if (!print_cbq_opts(a))
167 if (!print_priq_opts(a))
171 if (!print_hfsc_opts(a, qopts))
175 if (!print_fairq_opts(a, qopts))
180 if (bw != NULL && bw->bw_percent > 0) {
181 if (bw->bw_percent < 100)
182 printf("bandwidth %u%% ", bw->bw_percent);
184 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
186 if (a->qlimit != DEFAULT_QLIMIT)
187 printf("qlimit %u ", a->qlimit);
188 printf("tbrsize %u ", a->tbrsize);
192 print_queue(const struct pf_altq *a, unsigned int level,
193 struct node_queue_bw *bw, int print_interface,
194 struct node_queue_opt *qopts)
199 for (i = 0; i < level; ++i)
201 printf("%s ", a->qname);
203 printf("on %s ", a->ifname);
204 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC ||
205 a->scheduler == ALTQT_FAIRQ) {
206 if (bw != NULL && bw->bw_percent > 0) {
207 if (bw->bw_percent < 100)
208 printf("bandwidth %u%% ", bw->bw_percent);
210 printf("bandwidth %s ", rate2str((double)a->bandwidth));
212 if (a->priority != DEFAULT_PRIORITY)
213 printf("priority %u ", a->priority);
214 if (a->qlimit != DEFAULT_QLIMIT)
215 printf("qlimit %u ", a->qlimit);
216 switch (a->scheduler) {
224 print_hfsc_opts(a, qopts);
227 print_fairq_opts(a, qopts);
233 * eval_pfaltq computes the discipline parameters.
236 eval_pfaltq(struct pfctl *pf __unused, struct pf_altq *pa, struct node_queue_bw *bw,
237 struct node_queue_opt *opts)
239 u_int rate, size, errors = 0;
241 if (bw->bw_absolute > 0)
242 pa->ifbandwidth = bw->bw_absolute;
244 if ((rate = getifspeed(pa->ifname)) == 0) {
245 fprintf(stderr, "interface %s does not know its bandwidth, "
246 "please specify an absolute bandwidth\n",
249 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
250 pa->ifbandwidth = rate;
252 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
254 /* if tbrsize is not specified, use heuristics */
255 if (pa->tbrsize == 0) {
256 rate = pa->ifbandwidth;
257 if (rate <= 1 * 1000 * 1000)
259 else if (rate <= 10 * 1000 * 1000)
261 else if (rate <= 200 * 1000 * 1000)
265 size = size * getifmtu(pa->ifname);
274 * check_commit_altq does consistency check for each interface
277 check_commit_altq(int dev, int opts)
279 struct pf_altq *altq;
282 /* call the discipline check for each interface. */
283 TAILQ_FOREACH(altq, &altqs, entries) {
284 if (altq->qname[0] == 0) {
285 switch (altq->scheduler) {
287 error = check_commit_cbq(dev, opts, altq);
290 error = check_commit_priq(dev, opts, altq);
293 error = check_commit_hfsc(dev, opts, altq);
296 error = check_commit_fairq(dev, opts, altq);
307 * eval_pfqueue computes the queue parameters.
310 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
311 struct node_queue_opt *opts)
313 /* should be merged with expand_queue */
314 struct pf_altq *if_pa, *parent, *altq;
318 /* find the corresponding interface and copy fields used by queues */
319 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
320 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
323 pa->scheduler = if_pa->scheduler;
324 pa->ifbandwidth = if_pa->ifbandwidth;
326 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
327 fprintf(stderr, "queue %s already exists on interface %s\n",
328 pa->qname, pa->ifname);
331 pa->qid = qname_to_qid(pa->qname);
334 if (pa->parent[0] != 0) {
335 parent = qname_to_pfaltq(pa->parent, pa->ifname);
336 if (parent == NULL) {
337 fprintf(stderr, "parent %s not found for %s\n",
338 pa->parent, pa->qname);
341 pa->parent_qid = parent->qid;
344 pa->qlimit = DEFAULT_QLIMIT;
346 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC ||
347 pa->scheduler == ALTQT_FAIRQ) {
348 pa->bandwidth = eval_bwspec(bw,
349 parent == NULL ? 0 : parent->bandwidth);
351 if (pa->bandwidth > pa->ifbandwidth) {
352 fprintf(stderr, "bandwidth for %s higher than "
353 "interface\n", pa->qname);
356 /* check the sum of the child bandwidth is under parent's */
357 if (parent != NULL) {
358 if (pa->bandwidth > parent->bandwidth) {
359 warnx("bandwidth for %s higher than parent",
364 TAILQ_FOREACH(altq, &altqs, entries) {
365 if (strncmp(altq->ifname, pa->ifname,
367 altq->qname[0] != 0 &&
368 strncmp(altq->parent, pa->parent,
370 bwsum += altq->bandwidth;
372 bwsum += pa->bandwidth;
373 if (bwsum > parent->bandwidth) {
374 warnx("the sum of the child bandwidth higher"
375 " than parent \"%s\"", parent->qname);
380 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
383 switch (pa->scheduler) {
385 error = eval_pfqueue_cbq(pf, pa);
388 error = eval_pfqueue_priq(pf, pa);
391 error = eval_pfqueue_hfsc(pf, pa);
394 error = eval_pfqueue_fairq(pf, pa);
403 * CBQ support functions
405 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
406 #define RM_NS_PER_SEC (1000000000)
409 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
411 struct cbq_opts *opts;
414 if (pa->priority >= CBQ_MAXPRI) {
415 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
419 ifmtu = getifmtu(pa->ifname);
420 opts = &pa->pq_u.cbq_opts;
422 if (opts->pktsize == 0) { /* use default */
423 opts->pktsize = ifmtu;
424 if (opts->pktsize > MCLBYTES) /* do what TCP does */
425 opts->pktsize &= ~MCLBYTES;
426 } else if (opts->pktsize > ifmtu)
427 opts->pktsize = ifmtu;
428 if (opts->maxpktsize == 0) /* use default */
429 opts->maxpktsize = ifmtu;
430 else if (opts->maxpktsize > ifmtu)
431 opts->pktsize = ifmtu;
433 if (opts->pktsize > opts->maxpktsize)
434 opts->pktsize = opts->maxpktsize;
436 if (pa->parent[0] == 0)
437 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
439 cbq_compute_idletime(pf, pa);
444 * compute ns_per_byte, maxidle, minidle, and offtime
447 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
449 struct cbq_opts *opts;
450 double maxidle_s, maxidle, minidle;
451 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
452 double z, g, f, gton, gtom;
453 u_int minburst, maxburst;
455 opts = &pa->pq_u.cbq_opts;
456 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
457 minburst = opts->minburst;
458 maxburst = opts->maxburst;
460 if (pa->bandwidth == 0)
461 f = 0.0001; /* small enough? */
463 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
465 nsPerByte = ifnsPerByte / f;
466 ptime = (double)opts->pktsize * ifnsPerByte;
467 cptime = ptime * (1.0 - f) / f;
469 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
471 * this causes integer overflow in kernel!
472 * (bandwidth < 6Kbps when max_pkt_size=1500)
474 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
475 warnx("queue bandwidth must be larger than %s",
476 rate2str(ifnsPerByte * (double)opts->maxpktsize /
477 (double)INT_MAX * (double)pa->ifbandwidth));
478 fprintf(stderr, "cbq: queue %s is too slow!\n",
480 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
483 if (maxburst == 0) { /* use default */
484 if (cptime > 10.0 * 1000000)
489 if (minburst == 0) /* use default */
491 if (minburst > maxburst)
494 z = (double)(1 << RM_FILTER_GAIN);
496 gton = pow(g, (double)maxburst);
497 gtom = pow(g, (double)(minburst-1));
498 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
499 maxidle_s = (1.0 - g);
500 if (maxidle > maxidle_s)
501 maxidle = ptime * maxidle;
503 maxidle = ptime * maxidle_s;
504 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
505 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
507 /* scale parameters */
508 maxidle = ((maxidle * 8.0) / nsPerByte) *
509 pow(2.0, (double)RM_FILTER_GAIN);
510 offtime = (offtime * 8.0) / nsPerByte *
511 pow(2.0, (double)RM_FILTER_GAIN);
512 minidle = ((minidle * 8.0) / nsPerByte) *
513 pow(2.0, (double)RM_FILTER_GAIN);
515 maxidle = maxidle / 1000.0;
516 offtime = offtime / 1000.0;
517 minidle = minidle / 1000.0;
519 opts->minburst = minburst;
520 opts->maxburst = maxburst;
521 opts->ns_per_byte = (u_int)nsPerByte;
522 opts->maxidle = (u_int)fabs(maxidle);
523 opts->minidle = (int)minidle;
524 opts->offtime = (u_int)fabs(offtime);
530 check_commit_cbq(int dev __unused, int opts __unused, struct pf_altq *pa)
532 struct pf_altq *altq;
533 int root_class, default_class;
537 * check if cbq has one root queue and one default queue
540 root_class = default_class = 0;
541 TAILQ_FOREACH(altq, &altqs, entries) {
542 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
544 if (altq->qname[0] == 0) /* this is for interface */
546 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
548 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
551 if (root_class != 1) {
552 warnx("should have one root queue on %s", pa->ifname);
555 if (default_class != 1) {
556 warnx("should have one default queue on %s", pa->ifname);
563 print_cbq_opts(const struct pf_altq *a)
565 const struct cbq_opts *opts;
567 opts = &a->pq_u.cbq_opts;
570 if (opts->flags & CBQCLF_RED)
572 if (opts->flags & CBQCLF_ECN)
574 if (opts->flags & CBQCLF_RIO)
576 if (opts->flags & CBQCLF_CLEARDSCP)
577 printf(" cleardscp");
578 if (opts->flags & CBQCLF_BORROW)
580 if (opts->flags & CBQCLF_WRR)
582 if (opts->flags & CBQCLF_EFFICIENT)
583 printf(" efficient");
584 if (opts->flags & CBQCLF_ROOTCLASS)
586 if (opts->flags & CBQCLF_DEFCLASS)
596 * PRIQ support functions
599 eval_pfqueue_priq(struct pfctl *pf __unused, struct pf_altq *pa)
601 struct pf_altq *altq;
603 if (pa->priority >= PRIQ_MAXPRI) {
604 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
607 /* the priority should be unique for the interface */
608 TAILQ_FOREACH(altq, &altqs, entries) {
609 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
610 altq->qname[0] != 0 && altq->priority == pa->priority) {
611 warnx("%s and %s have the same priority",
612 altq->qname, pa->qname);
621 check_commit_priq(int dev __unused, int opts __unused, struct pf_altq *pa)
623 struct pf_altq *altq;
628 * check if priq has one default class for this interface
631 TAILQ_FOREACH(altq, &altqs, entries) {
632 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
634 if (altq->qname[0] == 0) /* this is for interface */
636 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
639 if (default_class != 1) {
640 warnx("should have one default queue on %s", pa->ifname);
647 print_priq_opts(const struct pf_altq *a)
649 const struct priq_opts *opts;
651 opts = &a->pq_u.priq_opts;
655 if (opts->flags & PRCF_RED)
657 if (opts->flags & PRCF_ECN)
659 if (opts->flags & PRCF_RIO)
661 if (opts->flags & PRCF_CLEARDSCP)
662 printf(" cleardscp");
663 if (opts->flags & PRCF_DEFAULTCLASS)
673 * HFSC support functions
676 eval_pfqueue_hfsc(struct pfctl *pf __unused, struct pf_altq *pa)
678 struct pf_altq *altq, *parent;
679 struct hfsc_opts *opts;
680 struct service_curve sc;
682 opts = &pa->pq_u.hfsc_opts;
684 if (pa->parent[0] == 0) {
686 opts->lssc_m1 = pa->ifbandwidth;
687 opts->lssc_m2 = pa->ifbandwidth;
695 /* if link_share is not specified, use bandwidth */
696 if (opts->lssc_m2 == 0)
697 opts->lssc_m2 = pa->bandwidth;
699 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
700 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
701 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
702 warnx("m2 is zero for %s", pa->qname);
706 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
707 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
708 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
709 warnx("m1 must be zero for convex curve: %s", pa->qname);
715 * for the real-time service curve, the sum of the service curves
716 * should not exceed 80% of the interface bandwidth. 20% is reserved
717 * not to over-commit the actual interface bandwidth.
718 * for the linkshare service curve, the sum of the child service
719 * curve should not exceed the parent service curve.
720 * for the upper-limit service curve, the assigned bandwidth should
721 * be smaller than the interface bandwidth, and the upper-limit should
722 * be larger than the real-time service curve when both are defined.
724 parent = qname_to_pfaltq(pa->parent, pa->ifname);
726 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
728 TAILQ_FOREACH(altq, &altqs, entries) {
729 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
731 if (altq->qname[0] == 0) /* this is for interface */
734 /* if the class has a real-time service curve, add it. */
735 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
736 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
737 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
738 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
739 gsc_add_sc(&rtsc, &sc);
742 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
745 /* if the class has a linkshare service curve, add it. */
746 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
747 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
748 sc.d = altq->pq_u.hfsc_opts.lssc_d;
749 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
750 gsc_add_sc(&lssc, &sc);
754 /* check the real-time service curve. reserve 20% of interface bw */
755 if (opts->rtsc_m2 != 0) {
756 /* add this queue to the sum */
757 sc.m1 = opts->rtsc_m1;
759 sc.m2 = opts->rtsc_m2;
760 gsc_add_sc(&rtsc, &sc);
761 /* compare the sum with 80% of the interface */
764 sc.m2 = pa->ifbandwidth / 100 * 80;
765 if (!is_gsc_under_sc(&rtsc, &sc)) {
766 warnx("real-time sc exceeds 80%% of the interface "
767 "bandwidth (%s)", rate2str((double)sc.m2));
772 /* check the linkshare service curve. */
773 if (opts->lssc_m2 != 0) {
774 /* add this queue to the child sum */
775 sc.m1 = opts->lssc_m1;
777 sc.m2 = opts->lssc_m2;
778 gsc_add_sc(&lssc, &sc);
779 /* compare the sum of the children with parent's sc */
780 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
781 sc.d = parent->pq_u.hfsc_opts.lssc_d;
782 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
783 if (!is_gsc_under_sc(&lssc, &sc)) {
784 warnx("linkshare sc exceeds parent's sc");
789 /* check the upper-limit service curve. */
790 if (opts->ulsc_m2 != 0) {
791 if (opts->ulsc_m1 > pa->ifbandwidth ||
792 opts->ulsc_m2 > pa->ifbandwidth) {
793 warnx("upper-limit larger than interface bandwidth");
796 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
797 warnx("upper-limit sc smaller than real-time sc");
814 * FAIRQ support functions
817 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
819 struct pf_altq *altq, *parent;
820 struct fairq_opts *opts;
821 struct service_curve sc;
823 opts = &pa->pq_u.fairq_opts;
825 if (pa->parent[0] == 0) {
827 opts->lssc_m1 = pa->ifbandwidth;
828 opts->lssc_m2 = pa->ifbandwidth;
835 /* if link_share is not specified, use bandwidth */
836 if (opts->lssc_m2 == 0)
837 opts->lssc_m2 = pa->bandwidth;
841 * for the real-time service curve, the sum of the service curves
842 * should not exceed 80% of the interface bandwidth. 20% is reserved
843 * not to over-commit the actual interface bandwidth.
844 * for the link-sharing service curve, the sum of the child service
845 * curve should not exceed the parent service curve.
846 * for the upper-limit service curve, the assigned bandwidth should
847 * be smaller than the interface bandwidth, and the upper-limit should
848 * be larger than the real-time service curve when both are defined.
850 parent = qname_to_pfaltq(pa->parent, pa->ifname);
852 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
854 TAILQ_FOREACH(altq, &altqs, entries) {
855 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
857 if (altq->qname[0] == 0) /* this is for interface */
860 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
863 /* if the class has a link-sharing service curve, add it. */
864 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
865 sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
866 sc.d = altq->pq_u.fairq_opts.lssc_d;
867 sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
868 gsc_add_sc(&lssc, &sc);
872 /* check the link-sharing service curve. */
873 if (opts->lssc_m2 != 0) {
874 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
875 sc.d = parent->pq_u.fairq_opts.lssc_d;
876 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
877 if (!is_gsc_under_sc(&lssc, &sc)) {
878 warnx("link-sharing sc exceeds parent's sc");
893 check_commit_hfsc(int dev __unused, int opts __unused, struct pf_altq *pa)
895 struct pf_altq *altq, *def = NULL;
899 /* check if hfsc has one default queue for this interface */
901 TAILQ_FOREACH(altq, &altqs, entries) {
902 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
904 if (altq->qname[0] == 0) /* this is for interface */
906 if (altq->parent[0] == 0) /* dummy root */
908 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
913 if (default_class != 1) {
914 warnx("should have one default queue on %s", pa->ifname);
917 /* make sure the default queue is a leaf */
918 TAILQ_FOREACH(altq, &altqs, entries) {
919 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
921 if (altq->qname[0] == 0) /* this is for interface */
923 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
924 warnx("default queue is not a leaf");
932 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
934 struct pf_altq *altq, *def = NULL;
938 /* check if fairq has one default queue for this interface */
940 TAILQ_FOREACH(altq, &altqs, entries) {
941 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
943 if (altq->qname[0] == 0) /* this is for interface */
945 if (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
950 if (default_class != 1) {
951 warnx("should have one default queue on %s", pa->ifname);
954 /* make sure the default queue is a leaf */
955 TAILQ_FOREACH(altq, &altqs, entries) {
956 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
958 if (altq->qname[0] == 0) /* this is for interface */
960 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
961 warnx("default queue is not a leaf");
969 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
971 const struct hfsc_opts *opts;
972 const struct node_hfsc_sc *loc_rtsc, *loc_lssc, *ulsc;
974 opts = &a->pq_u.hfsc_opts;
976 loc_rtsc = loc_lssc = ulsc = NULL;
978 loc_rtsc = &qopts->data.hfsc_opts.realtime;
979 loc_lssc = &qopts->data.hfsc_opts.linkshare;
980 ulsc = &qopts->data.hfsc_opts.upperlimit;
983 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
984 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
985 opts->lssc_d != 0))) {
987 if (opts->flags & HFCF_RED)
989 if (opts->flags & HFCF_ECN)
991 if (opts->flags & HFCF_RIO)
993 if (opts->flags & HFCF_CLEARDSCP)
994 printf(" cleardscp");
995 if (opts->flags & HFCF_DEFAULTCLASS)
997 if (opts->rtsc_m2 != 0)
998 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
999 opts->rtsc_m2, loc_rtsc);
1000 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1002 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1003 opts->lssc_m2, loc_lssc);
1004 if (opts->ulsc_m2 != 0)
1005 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1006 opts->ulsc_m2, ulsc);
1015 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1017 const struct fairq_opts *opts;
1018 const struct node_fairq_sc *loc_lssc;
1020 opts = &a->pq_u.fairq_opts;
1024 loc_lssc = &qopts->data.fairq_opts.linkshare;
1027 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1028 opts->lssc_d != 0))) {
1030 if (opts->flags & FARF_RED)
1032 if (opts->flags & FARF_ECN)
1034 if (opts->flags & FARF_RIO)
1036 if (opts->flags & FARF_CLEARDSCP)
1037 printf(" cleardscp");
1038 if (opts->flags & FARF_DEFAULTCLASS)
1040 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1042 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1043 opts->lssc_m2, loc_lssc);
1052 * admission control using generalized service curve
1055 /* add a new service curve to a generalized service curve */
1057 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1062 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1063 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1067 * check whether all points of a generalized service curve have
1068 * their y-coordinates no larger than a given two-piece linear
1072 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1074 struct segment *s, *last, *end;
1077 if (is_sc_null(sc)) {
1078 if (LIST_EMPTY(gsc))
1080 LIST_FOREACH(s, gsc, _next) {
1087 * gsc has a dummy entry at the end with x = INFINITY.
1088 * loop through up to this dummy entry.
1090 end = gsc_getentry(gsc, INFINITY);
1094 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1095 if (s->y > sc_x2y(sc, s->x))
1099 /* last now holds the real last segment */
1102 if (last->m > sc->m2)
1104 if (last->x < sc->d && last->m > sc->m1) {
1105 y = last->y + (sc->d - last->x) * last->m;
1106 if (y > sc_x2y(sc, sc->d))
1113 gsc_destroy(struct gen_sc *gsc)
1117 while ((s = LIST_FIRST(gsc)) != NULL) {
1118 LIST_REMOVE(s, _next);
1124 * return a segment entry starting at x.
1125 * if gsc has no entry starting at x, a new entry is created at x.
1127 static struct segment *
1128 gsc_getentry(struct gen_sc *gsc, double x)
1130 struct segment *new, *prev, *s;
1133 LIST_FOREACH(s, gsc, _next) {
1135 return (s); /* matching entry found */
1142 /* we have to create a new entry */
1143 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1147 if (x == INFINITY || s == NULL)
1149 else if (s->x == INFINITY)
1154 /* insert the new entry at the head of the list */
1157 LIST_INSERT_HEAD(gsc, new, _next);
1160 * the start point intersects with the segment pointed by
1161 * prev. divide prev into 2 segments
1163 if (x == INFINITY) {
1170 prev->d = x - prev->x;
1171 new->y = prev->d * prev->m + prev->y;
1174 LIST_INSERT_AFTER(prev, new, _next);
1179 /* add a segment to a generalized service curve */
1181 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1183 struct segment *start, *end, *s;
1190 start = gsc_getentry(gsc, x);
1191 end = gsc_getentry(gsc, x2);
1192 if (start == NULL || end == NULL)
1195 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1197 s->y += y + (s->x - x) * m;
1200 end = gsc_getentry(gsc, INFINITY);
1201 for (; s != end; s = LIST_NEXT(s, _next)) {
1208 /* get y-projection of a service curve */
1210 sc_x2y(struct service_curve *sc, double x)
1214 if (x <= (double)sc->d)
1215 /* y belongs to the 1st segment */
1216 y = x * (double)sc->m1;
1218 /* y belongs to the 2nd segment */
1219 y = (double)sc->d * (double)sc->m1
1220 + (x - (double)sc->d) * (double)sc->m2;
1228 #define RATESTR_MAX 16
1231 rate2str(double rate)
1234 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1237 static const char unit[] = " KMG";
1239 buf = r2sbuf[idx++];
1240 if (idx == R2S_BUFS)
1243 for (i = 0; rate >= 1000 && i <= 3; i++)
1246 if ((int)(rate * 100) % 100)
1247 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1249 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1255 getifspeed(const char *ifname)
1259 struct ifmibdata data;
1269 if ((idx = (int)if_nametoindex(ifname)) == 0)
1270 err(1, "getifspeed: if_nametoindex");
1273 datalen = sizeof(data);
1274 if (sysctl(name, 6, &data, &datalen, NULL, 0))
1275 err(1, "getifspeed: sysctl");
1277 return(data.ifmd_data.ifi_baudrate);
1281 getifmtu(char *ifname)
1286 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1288 bzero(&ifr, sizeof(ifr));
1289 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1290 sizeof(ifr.ifr_name))
1291 errx(1, "getifmtu: strlcpy");
1292 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1293 err(1, "SIOCGIFMTU");
1296 if (ifr.ifr_mtu > 0)
1297 return (ifr.ifr_mtu);
1299 warnx("could not get mtu for %s, assuming 1500", ifname);
1305 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1310 switch (pa->scheduler) {
1312 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1315 pa->pq_u.priq_opts = opts->data.priq_opts;
1318 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1319 if (opts->data.hfsc_opts.linkshare.used) {
1320 pa->pq_u.hfsc_opts.lssc_m1 =
1321 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1323 pa->pq_u.hfsc_opts.lssc_m2 =
1324 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1326 pa->pq_u.hfsc_opts.lssc_d =
1327 opts->data.hfsc_opts.linkshare.d;
1329 if (opts->data.hfsc_opts.realtime.used) {
1330 pa->pq_u.hfsc_opts.rtsc_m1 =
1331 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1333 pa->pq_u.hfsc_opts.rtsc_m2 =
1334 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1336 pa->pq_u.hfsc_opts.rtsc_d =
1337 opts->data.hfsc_opts.realtime.d;
1339 if (opts->data.hfsc_opts.upperlimit.used) {
1340 pa->pq_u.hfsc_opts.ulsc_m1 =
1341 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1343 pa->pq_u.hfsc_opts.ulsc_m2 =
1344 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1346 pa->pq_u.hfsc_opts.ulsc_d =
1347 opts->data.hfsc_opts.upperlimit.d;
1351 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1352 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1353 pa->pq_u.fairq_opts.hogs_m1 =
1354 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1356 if (opts->data.fairq_opts.linkshare.used) {
1357 pa->pq_u.fairq_opts.lssc_m1 =
1358 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1360 pa->pq_u.fairq_opts.lssc_m2 =
1361 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1363 pa->pq_u.fairq_opts.lssc_d =
1364 opts->data.fairq_opts.linkshare.d;
1368 warnx("eval_queue_opts: unknown scheduler type %u",
1378 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1380 if (bw->bw_absolute > 0)
1381 return (bw->bw_absolute);
1383 if (bw->bw_percent > 0)
1384 return (ref_bw / 100 * bw->bw_percent);
1390 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1391 const struct node_hfsc_sc *sc)
1393 printf(" %s", scname);
1397 if (sc != NULL && sc->m1.bw_percent > 0)
1398 printf("%u%%", sc->m1.bw_percent);
1400 printf("%s", rate2str((double)m1));
1404 if (sc != NULL && sc->m2.bw_percent > 0)
1405 printf(" %u%%", sc->m2.bw_percent);
1407 printf(" %s", rate2str((double)m2));
1414 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1415 const struct node_fairq_sc *sc)
1417 printf(" %s", scname);
1421 if (sc != NULL && sc->m1.bw_percent > 0)
1422 printf("%u%%", sc->m1.bw_percent);
1424 printf("%s", rate2str((double)m1));
1428 if (sc != NULL && sc->m2.bw_percent > 0)
1429 printf(" %u%%", sc->m2.bw_percent);
1431 printf(" %s", rate2str((double)m2));