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",
481 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
484 if (maxburst == 0) { /* use default */
485 if (cptime > 10.0 * 1000000)
490 if (minburst == 0) /* use default */
492 if (minburst > maxburst)
495 z = (double)(1 << RM_FILTER_GAIN);
497 gton = pow(g, (double)maxburst);
498 gtom = pow(g, (double)(minburst-1));
499 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
500 maxidle_s = (1.0 - g);
501 if (maxidle > maxidle_s)
502 maxidle = ptime * maxidle;
504 maxidle = ptime * maxidle_s;
505 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
506 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
508 /* scale parameters */
509 maxidle = ((maxidle * 8.0) / nsPerByte) *
510 pow(2.0, (double)RM_FILTER_GAIN);
511 offtime = (offtime * 8.0) / nsPerByte *
512 pow(2.0, (double)RM_FILTER_GAIN);
513 minidle = ((minidle * 8.0) / nsPerByte) *
514 pow(2.0, (double)RM_FILTER_GAIN);
516 maxidle = maxidle / 1000.0;
517 offtime = offtime / 1000.0;
518 minidle = minidle / 1000.0;
520 opts->minburst = minburst;
521 opts->maxburst = maxburst;
522 opts->ns_per_byte = (u_int)nsPerByte;
523 opts->maxidle = (u_int)fabs(maxidle);
524 opts->minidle = (int)minidle;
525 opts->offtime = (u_int)fabs(offtime);
531 check_commit_cbq(int dev __unused, int opts __unused, struct pf_altq *pa)
533 struct pf_altq *altq;
534 int root_class, default_class;
538 * check if cbq has one root queue and one default queue
541 root_class = default_class = 0;
542 TAILQ_FOREACH(altq, &altqs, entries) {
543 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
545 if (altq->qname[0] == 0) /* this is for interface */
547 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
549 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
552 if (root_class != 1) {
553 warnx("should have one root queue on %s", pa->ifname);
556 if (default_class != 1) {
557 warnx("should have one default queue on %s", pa->ifname);
564 print_cbq_opts(const struct pf_altq *a)
566 const struct cbq_opts *opts;
568 opts = &a->pq_u.cbq_opts;
571 if (opts->flags & CBQCLF_RED)
573 if (opts->flags & CBQCLF_ECN)
575 if (opts->flags & CBQCLF_RIO)
577 if (opts->flags & CBQCLF_CLEARDSCP)
578 printf(" cleardscp");
579 if (opts->flags & CBQCLF_BORROW)
581 if (opts->flags & CBQCLF_WRR)
583 if (opts->flags & CBQCLF_EFFICIENT)
584 printf(" efficient");
585 if (opts->flags & CBQCLF_ROOTCLASS)
587 if (opts->flags & CBQCLF_DEFCLASS)
597 * PRIQ support functions
600 eval_pfqueue_priq(struct pfctl *pf __unused, struct pf_altq *pa)
602 struct pf_altq *altq;
604 if (pa->priority >= PRIQ_MAXPRI) {
605 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
608 /* the priority should be unique for the interface */
609 TAILQ_FOREACH(altq, &altqs, entries) {
610 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
611 altq->qname[0] != 0 && altq->priority == pa->priority) {
612 warnx("%s and %s have the same priority",
613 altq->qname, pa->qname);
622 check_commit_priq(int dev __unused, int opts __unused, struct pf_altq *pa)
624 struct pf_altq *altq;
629 * check if priq has one default class for this interface
632 TAILQ_FOREACH(altq, &altqs, entries) {
633 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
635 if (altq->qname[0] == 0) /* this is for interface */
637 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
640 if (default_class != 1) {
641 warnx("should have one default queue on %s", pa->ifname);
648 print_priq_opts(const struct pf_altq *a)
650 const struct priq_opts *opts;
652 opts = &a->pq_u.priq_opts;
656 if (opts->flags & PRCF_RED)
658 if (opts->flags & PRCF_ECN)
660 if (opts->flags & PRCF_RIO)
662 if (opts->flags & PRCF_CLEARDSCP)
663 printf(" cleardscp");
664 if (opts->flags & PRCF_DEFAULTCLASS)
674 * HFSC support functions
677 eval_pfqueue_hfsc(struct pfctl *pf __unused, struct pf_altq *pa)
679 struct pf_altq *altq, *parent;
680 struct hfsc_opts *opts;
681 struct service_curve sc;
683 opts = &pa->pq_u.hfsc_opts;
685 if (pa->parent[0] == 0) {
687 opts->lssc_m1 = pa->ifbandwidth;
688 opts->lssc_m2 = pa->ifbandwidth;
696 /* if link_share is not specified, use bandwidth */
697 if (opts->lssc_m2 == 0)
698 opts->lssc_m2 = pa->bandwidth;
700 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
701 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
702 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
703 warnx("m2 is zero for %s", pa->qname);
707 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
708 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
709 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
710 warnx("m1 must be zero for convex curve: %s", pa->qname);
716 * for the real-time service curve, the sum of the service curves
717 * should not exceed 80% of the interface bandwidth. 20% is reserved
718 * not to over-commit the actual interface bandwidth.
719 * for the linkshare service curve, the sum of the child service
720 * curve should not exceed the parent service curve.
721 * for the upper-limit service curve, the assigned bandwidth should
722 * be smaller than the interface bandwidth, and the upper-limit should
723 * be larger than the real-time service curve when both are defined.
725 parent = qname_to_pfaltq(pa->parent, pa->ifname);
727 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
729 TAILQ_FOREACH(altq, &altqs, entries) {
730 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
732 if (altq->qname[0] == 0) /* this is for interface */
735 /* if the class has a real-time service curve, add it. */
736 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
737 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
738 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
739 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
740 gsc_add_sc(&rtsc, &sc);
743 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
746 /* if the class has a linkshare service curve, add it. */
747 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
748 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
749 sc.d = altq->pq_u.hfsc_opts.lssc_d;
750 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
751 gsc_add_sc(&lssc, &sc);
755 /* check the real-time service curve. reserve 20% of interface bw */
756 if (opts->rtsc_m2 != 0) {
757 /* add this queue to the sum */
758 sc.m1 = opts->rtsc_m1;
760 sc.m2 = opts->rtsc_m2;
761 gsc_add_sc(&rtsc, &sc);
762 /* compare the sum with 80% of the interface */
765 sc.m2 = pa->ifbandwidth / 100 * 80;
766 if (!is_gsc_under_sc(&rtsc, &sc)) {
767 warnx("real-time sc exceeds 80%% of the interface "
768 "bandwidth (%s)", rate2str((double)sc.m2));
773 /* check the linkshare service curve. */
774 if (opts->lssc_m2 != 0) {
775 /* add this queue to the child sum */
776 sc.m1 = opts->lssc_m1;
778 sc.m2 = opts->lssc_m2;
779 gsc_add_sc(&lssc, &sc);
780 /* compare the sum of the children with parent's sc */
781 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
782 sc.d = parent->pq_u.hfsc_opts.lssc_d;
783 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
784 if (!is_gsc_under_sc(&lssc, &sc)) {
785 warnx("linkshare sc exceeds parent's sc");
790 /* check the upper-limit service curve. */
791 if (opts->ulsc_m2 != 0) {
792 if (opts->ulsc_m1 > pa->ifbandwidth ||
793 opts->ulsc_m2 > pa->ifbandwidth) {
794 warnx("upper-limit larger than interface bandwidth");
797 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
798 warnx("upper-limit sc smaller than real-time sc");
815 * FAIRQ support functions
818 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
820 struct pf_altq *altq, *parent;
821 struct fairq_opts *opts;
822 struct service_curve sc;
824 opts = &pa->pq_u.fairq_opts;
826 if (pa->parent[0] == 0) {
828 opts->lssc_m1 = pa->ifbandwidth;
829 opts->lssc_m2 = pa->ifbandwidth;
836 /* if link_share is not specified, use bandwidth */
837 if (opts->lssc_m2 == 0)
838 opts->lssc_m2 = pa->bandwidth;
842 * for the real-time service curve, the sum of the service curves
843 * should not exceed 80% of the interface bandwidth. 20% is reserved
844 * not to over-commit the actual interface bandwidth.
845 * for the link-sharing service curve, the sum of the child service
846 * curve should not exceed the parent service curve.
847 * for the upper-limit service curve, the assigned bandwidth should
848 * be smaller than the interface bandwidth, and the upper-limit should
849 * be larger than the real-time service curve when both are defined.
851 parent = qname_to_pfaltq(pa->parent, pa->ifname);
853 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
855 TAILQ_FOREACH(altq, &altqs, entries) {
856 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
858 if (altq->qname[0] == 0) /* this is for interface */
861 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
864 /* if the class has a link-sharing service curve, add it. */
865 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
866 sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
867 sc.d = altq->pq_u.fairq_opts.lssc_d;
868 sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
869 gsc_add_sc(&lssc, &sc);
873 /* check the link-sharing service curve. */
874 if (opts->lssc_m2 != 0) {
875 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
876 sc.d = parent->pq_u.fairq_opts.lssc_d;
877 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
878 if (!is_gsc_under_sc(&lssc, &sc)) {
879 warnx("link-sharing sc exceeds parent's sc");
894 check_commit_hfsc(int dev __unused, int opts __unused, struct pf_altq *pa)
896 struct pf_altq *altq, *def = NULL;
900 /* check if hfsc has one default queue for this interface */
902 TAILQ_FOREACH(altq, &altqs, entries) {
903 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
905 if (altq->qname[0] == 0) /* this is for interface */
907 if (altq->parent[0] == 0) /* dummy root */
909 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
914 if (default_class != 1) {
915 warnx("should have one default queue on %s", pa->ifname);
918 /* make sure the default queue is a leaf */
919 TAILQ_FOREACH(altq, &altqs, entries) {
920 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
922 if (altq->qname[0] == 0) /* this is for interface */
924 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
925 warnx("default queue is not a leaf");
933 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
935 struct pf_altq *altq, *def = NULL;
939 /* check if fairq has one default queue for this interface */
941 TAILQ_FOREACH(altq, &altqs, entries) {
942 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
944 if (altq->qname[0] == 0) /* this is for interface */
946 if (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
951 if (default_class != 1) {
952 warnx("should have one default queue on %s", pa->ifname);
955 /* make sure the default queue is a leaf */
956 TAILQ_FOREACH(altq, &altqs, entries) {
957 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
959 if (altq->qname[0] == 0) /* this is for interface */
961 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
962 warnx("default queue is not a leaf");
970 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
972 const struct hfsc_opts *opts;
973 const struct node_hfsc_sc *loc_rtsc, *loc_lssc, *ulsc;
975 opts = &a->pq_u.hfsc_opts;
977 loc_rtsc = loc_lssc = ulsc = NULL;
979 loc_rtsc = &qopts->data.hfsc_opts.realtime;
980 loc_lssc = &qopts->data.hfsc_opts.linkshare;
981 ulsc = &qopts->data.hfsc_opts.upperlimit;
984 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
985 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
986 opts->lssc_d != 0))) {
988 if (opts->flags & HFCF_RED)
990 if (opts->flags & HFCF_ECN)
992 if (opts->flags & HFCF_RIO)
994 if (opts->flags & HFCF_CLEARDSCP)
995 printf(" cleardscp");
996 if (opts->flags & HFCF_DEFAULTCLASS)
998 if (opts->rtsc_m2 != 0)
999 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
1000 opts->rtsc_m2, loc_rtsc);
1001 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1003 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1004 opts->lssc_m2, loc_lssc);
1005 if (opts->ulsc_m2 != 0)
1006 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1007 opts->ulsc_m2, ulsc);
1016 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1018 const struct fairq_opts *opts;
1019 const struct node_fairq_sc *loc_lssc;
1021 opts = &a->pq_u.fairq_opts;
1025 loc_lssc = &qopts->data.fairq_opts.linkshare;
1028 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1029 opts->lssc_d != 0))) {
1031 if (opts->flags & FARF_RED)
1033 if (opts->flags & FARF_ECN)
1035 if (opts->flags & FARF_RIO)
1037 if (opts->flags & FARF_CLEARDSCP)
1038 printf(" cleardscp");
1039 if (opts->flags & FARF_DEFAULTCLASS)
1041 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1043 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1044 opts->lssc_m2, loc_lssc);
1053 * admission control using generalized service curve
1056 /* add a new service curve to a generalized service curve */
1058 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1063 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1064 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1068 * check whether all points of a generalized service curve have
1069 * their y-coordinates no larger than a given two-piece linear
1073 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1075 struct segment *s, *last, *end;
1078 if (is_sc_null(sc)) {
1079 if (LIST_EMPTY(gsc))
1081 LIST_FOREACH(s, gsc, _next) {
1088 * gsc has a dummy entry at the end with x = INFINITY.
1089 * loop through up to this dummy entry.
1091 end = gsc_getentry(gsc, INFINITY);
1095 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1096 if (s->y > sc_x2y(sc, s->x))
1100 /* last now holds the real last segment */
1103 if (last->m > sc->m2)
1105 if (last->x < sc->d && last->m > sc->m1) {
1106 y = last->y + (sc->d - last->x) * last->m;
1107 if (y > sc_x2y(sc, sc->d))
1114 gsc_destroy(struct gen_sc *gsc)
1118 while ((s = LIST_FIRST(gsc)) != NULL) {
1119 LIST_REMOVE(s, _next);
1125 * return a segment entry starting at x.
1126 * if gsc has no entry starting at x, a new entry is created at x.
1128 static struct segment *
1129 gsc_getentry(struct gen_sc *gsc, double x)
1131 struct segment *new, *prev, *s;
1134 LIST_FOREACH(s, gsc, _next) {
1136 return (s); /* matching entry found */
1143 /* we have to create a new entry */
1144 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1148 if (x == INFINITY || s == NULL)
1150 else if (s->x == INFINITY)
1155 /* insert the new entry at the head of the list */
1158 LIST_INSERT_HEAD(gsc, new, _next);
1161 * the start point intersects with the segment pointed by
1162 * prev. divide prev into 2 segments
1164 if (x == INFINITY) {
1171 prev->d = x - prev->x;
1172 new->y = prev->d * prev->m + prev->y;
1175 LIST_INSERT_AFTER(prev, new, _next);
1180 /* add a segment to a generalized service curve */
1182 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1184 struct segment *start, *end, *s;
1191 start = gsc_getentry(gsc, x);
1192 end = gsc_getentry(gsc, x2);
1193 if (start == NULL || end == NULL)
1196 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1198 s->y += y + (s->x - x) * m;
1201 end = gsc_getentry(gsc, INFINITY);
1202 for (; s != end; s = LIST_NEXT(s, _next)) {
1209 /* get y-projection of a service curve */
1211 sc_x2y(struct service_curve *sc, double x)
1215 if (x <= (double)sc->d)
1216 /* y belongs to the 1st segment */
1217 y = x * (double)sc->m1;
1219 /* y belongs to the 2nd segment */
1220 y = (double)sc->d * (double)sc->m1
1221 + (x - (double)sc->d) * (double)sc->m2;
1229 #define RATESTR_MAX 16
1232 rate2str(double rate)
1235 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1238 static const char unit[] = " KMG";
1240 buf = r2sbuf[idx++];
1241 if (idx == R2S_BUFS)
1244 for (i = 0; rate >= 1000 && i <= 3; i++)
1247 if ((int)(rate * 100) % 100)
1248 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1250 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1256 getifspeed(const char *ifname)
1260 struct ifmibdata data;
1270 if ((idx = (int)if_nametoindex(ifname)) == 0)
1271 err(1, "getifspeed: if_nametoindex");
1274 datalen = sizeof(data);
1275 if (sysctl(name, 6, &data, &datalen, NULL, 0))
1276 err(1, "getifspeed: sysctl");
1278 return(data.ifmd_data.ifi_baudrate);
1282 getifmtu(char *ifname)
1287 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1289 bzero(&ifr, sizeof(ifr));
1290 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1291 sizeof(ifr.ifr_name))
1292 errx(1, "getifmtu: strlcpy");
1293 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1294 err(1, "SIOCGIFMTU");
1297 if (ifr.ifr_mtu > 0)
1298 return (ifr.ifr_mtu);
1300 warnx("could not get mtu for %s, assuming 1500", ifname);
1306 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1311 switch (pa->scheduler) {
1313 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1316 pa->pq_u.priq_opts = opts->data.priq_opts;
1319 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1320 if (opts->data.hfsc_opts.linkshare.used) {
1321 pa->pq_u.hfsc_opts.lssc_m1 =
1322 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1324 pa->pq_u.hfsc_opts.lssc_m2 =
1325 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1327 pa->pq_u.hfsc_opts.lssc_d =
1328 opts->data.hfsc_opts.linkshare.d;
1330 if (opts->data.hfsc_opts.realtime.used) {
1331 pa->pq_u.hfsc_opts.rtsc_m1 =
1332 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1334 pa->pq_u.hfsc_opts.rtsc_m2 =
1335 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1337 pa->pq_u.hfsc_opts.rtsc_d =
1338 opts->data.hfsc_opts.realtime.d;
1340 if (opts->data.hfsc_opts.upperlimit.used) {
1341 pa->pq_u.hfsc_opts.ulsc_m1 =
1342 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1344 pa->pq_u.hfsc_opts.ulsc_m2 =
1345 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1347 pa->pq_u.hfsc_opts.ulsc_d =
1348 opts->data.hfsc_opts.upperlimit.d;
1352 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1353 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1354 pa->pq_u.fairq_opts.hogs_m1 =
1355 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1357 if (opts->data.fairq_opts.linkshare.used) {
1358 pa->pq_u.fairq_opts.lssc_m1 =
1359 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1361 pa->pq_u.fairq_opts.lssc_m2 =
1362 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1364 pa->pq_u.fairq_opts.lssc_d =
1365 opts->data.fairq_opts.linkshare.d;
1369 warnx("eval_queue_opts: unknown scheduler type %u",
1379 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1381 if (bw->bw_absolute > 0)
1382 return (bw->bw_absolute);
1384 if (bw->bw_percent > 0)
1385 return (ref_bw / 100 * bw->bw_percent);
1391 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1392 const struct node_hfsc_sc *sc)
1394 printf(" %s", scname);
1398 if (sc != NULL && sc->m1.bw_percent > 0)
1399 printf("%u%%", sc->m1.bw_percent);
1401 printf("%s", rate2str((double)m1));
1405 if (sc != NULL && sc->m2.bw_percent > 0)
1406 printf(" %u%%", sc->m2.bw_percent);
1408 printf(" %s", rate2str((double)m2));
1415 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1416 const struct node_fairq_sc *sc)
1418 printf(" %s", scname);
1422 if (sc != NULL && sc->m1.bw_percent > 0)
1423 printf("%u%%", sc->m1.bw_percent);
1425 printf("%s", rate2str((double)m1));
1429 if (sc != NULL && sc->m2.bw_percent > 0)
1430 printf(" %u%%", sc->m2.bw_percent);
1432 printf(" %s", rate2str((double)m2));