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 #define PFIOC_USE_LATEST
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <net/pfvar.h>
44 #include <net/altq/altq.h>
45 #include <net/altq/altq_cbq.h>
46 #include <net/altq/altq_codel.h>
47 #include <net/altq/altq_priq.h>
48 #include <net/altq/altq_hfsc.h>
49 #include <net/altq/altq_fairq.h>
51 #include "pfctl_parser.h"
54 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
56 static TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
57 static LIST_HEAD(gen_sc, segment) rtsc, lssc;
59 struct pf_altq *qname_to_pfaltq(const char *, const char *);
60 u_int32_t qname_to_qid(const char *);
62 static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *);
63 static int cbq_compute_idletime(struct pfctl *, struct pf_altq *);
64 static int check_commit_cbq(int, int, struct pf_altq *);
65 static int print_cbq_opts(const struct pf_altq *);
67 static int print_codel_opts(const struct pf_altq *,
68 const struct node_queue_opt *);
70 static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *);
71 static int check_commit_priq(int, int, struct pf_altq *);
72 static int print_priq_opts(const struct pf_altq *);
74 static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *);
75 static int check_commit_hfsc(int, int, struct pf_altq *);
76 static int print_hfsc_opts(const struct pf_altq *,
77 const struct node_queue_opt *);
79 static int eval_pfqueue_fairq(struct pfctl *, struct pf_altq *);
80 static int print_fairq_opts(const struct pf_altq *,
81 const struct node_queue_opt *);
82 static int check_commit_fairq(int, int, struct pf_altq *);
84 static void gsc_add_sc(struct gen_sc *, struct service_curve *);
85 static int is_gsc_under_sc(struct gen_sc *,
86 struct service_curve *);
87 static void gsc_destroy(struct gen_sc *);
88 static struct segment *gsc_getentry(struct gen_sc *, double);
89 static int gsc_add_seg(struct gen_sc *, double, double, double,
91 static double sc_x2y(struct service_curve *, double);
94 u_int64_t getifspeed(int, char *);
96 u_int32_t getifspeed(char *);
98 u_long getifmtu(char *);
99 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
101 u_int64_t eval_bwspec(struct node_queue_bw *, u_int64_t);
102 void print_hfsc_sc(const char *, u_int, u_int, u_int,
103 const struct node_hfsc_sc *);
104 void print_fairq_sc(const char *, u_int, u_int, u_int,
105 const struct node_fairq_sc *);
108 pfaltq_store(struct pf_altq *a)
110 struct pf_altq *altq;
112 if ((altq = malloc(sizeof(*altq))) == NULL)
114 memcpy(altq, a, sizeof(struct pf_altq));
115 TAILQ_INSERT_TAIL(&altqs, altq, entries);
119 pfaltq_lookup(const char *ifname)
121 struct pf_altq *altq;
123 TAILQ_FOREACH(altq, &altqs, entries) {
124 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
132 qname_to_pfaltq(const char *qname, const char *ifname)
134 struct pf_altq *altq;
136 TAILQ_FOREACH(altq, &altqs, entries) {
137 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
138 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
145 qname_to_qid(const char *qname)
147 struct pf_altq *altq;
150 * We guarantee that same named queues on different interfaces
151 * have the same qid, so we do NOT need to limit matching on
155 TAILQ_FOREACH(altq, &altqs, entries) {
156 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
163 print_altq(const struct pf_altq *a, unsigned int level,
164 struct node_queue_bw *bw, struct node_queue_opt *qopts)
166 if (a->qname[0] != 0) {
167 print_queue(a, level, bw, 1, qopts);
172 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
176 printf("altq on %s ", a->ifname);
178 switch (a->scheduler) {
180 if (!print_cbq_opts(a))
184 if (!print_priq_opts(a))
188 if (!print_hfsc_opts(a, qopts))
192 if (!print_fairq_opts(a, qopts))
196 if (!print_codel_opts(a, qopts))
201 if (bw != NULL && bw->bw_percent > 0) {
202 if (bw->bw_percent < 100)
203 printf("bandwidth %u%% ", bw->bw_percent);
205 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
207 if (a->qlimit != DEFAULT_QLIMIT)
208 printf("qlimit %u ", a->qlimit);
209 printf("tbrsize %u ", a->tbrsize);
213 print_queue(const struct pf_altq *a, unsigned int level,
214 struct node_queue_bw *bw, int print_interface,
215 struct node_queue_opt *qopts)
220 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
224 for (i = 0; i < level; ++i)
226 printf("%s ", a->qname);
228 printf("on %s ", a->ifname);
229 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC ||
230 a->scheduler == ALTQT_FAIRQ) {
231 if (bw != NULL && bw->bw_percent > 0) {
232 if (bw->bw_percent < 100)
233 printf("bandwidth %u%% ", bw->bw_percent);
235 printf("bandwidth %s ", rate2str((double)a->bandwidth));
237 if (a->priority != DEFAULT_PRIORITY)
238 printf("priority %u ", a->priority);
239 if (a->qlimit != DEFAULT_QLIMIT)
240 printf("qlimit %u ", a->qlimit);
241 switch (a->scheduler) {
249 print_hfsc_opts(a, qopts);
252 print_fairq_opts(a, qopts);
258 * eval_pfaltq computes the discipline parameters.
261 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
262 struct node_queue_opt *opts)
265 u_int size, errors = 0;
267 if (bw->bw_absolute > 0)
268 pa->ifbandwidth = bw->bw_absolute;
271 if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) {
273 if ((rate = getifspeed(pa->ifname)) == 0) {
275 fprintf(stderr, "interface %s does not know its bandwidth, "
276 "please specify an absolute bandwidth\n",
279 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
280 pa->ifbandwidth = rate;
283 * Limit bandwidth to UINT_MAX for schedulers that aren't 64-bit ready.
285 if ((pa->scheduler != ALTQT_HFSC) && (pa->ifbandwidth > UINT_MAX)) {
286 pa->ifbandwidth = UINT_MAX;
287 warnx("interface %s bandwidth limited to %" PRIu64 " bps "
288 "because selected scheduler is 32-bit limited\n", pa->ifname,
291 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
293 /* if tbrsize is not specified, use heuristics */
294 if (pa->tbrsize == 0) {
295 rate = pa->ifbandwidth;
296 if (rate <= 1 * 1000 * 1000)
298 else if (rate <= 10 * 1000 * 1000)
300 else if (rate <= 200 * 1000 * 1000)
302 else if (rate <= 2500 * 1000 * 1000)
306 size = size * getifmtu(pa->ifname);
313 * check_commit_altq does consistency check for each interface
316 check_commit_altq(int dev, int opts)
318 struct pf_altq *altq;
321 /* call the discipline check for each interface. */
322 TAILQ_FOREACH(altq, &altqs, entries) {
323 if (altq->qname[0] == 0) {
324 switch (altq->scheduler) {
326 error = check_commit_cbq(dev, opts, altq);
329 error = check_commit_priq(dev, opts, altq);
332 error = check_commit_hfsc(dev, opts, altq);
335 error = check_commit_fairq(dev, opts, altq);
346 * eval_pfqueue computes the queue parameters.
349 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
350 struct node_queue_opt *opts)
352 /* should be merged with expand_queue */
353 struct pf_altq *if_pa, *parent, *altq;
357 /* find the corresponding interface and copy fields used by queues */
358 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
359 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
362 pa->scheduler = if_pa->scheduler;
363 pa->ifbandwidth = if_pa->ifbandwidth;
365 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
366 fprintf(stderr, "queue %s already exists on interface %s\n",
367 pa->qname, pa->ifname);
370 pa->qid = qname_to_qid(pa->qname);
373 if (pa->parent[0] != 0) {
374 parent = qname_to_pfaltq(pa->parent, pa->ifname);
375 if (parent == NULL) {
376 fprintf(stderr, "parent %s not found for %s\n",
377 pa->parent, pa->qname);
380 pa->parent_qid = parent->qid;
383 pa->qlimit = DEFAULT_QLIMIT;
385 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC ||
386 pa->scheduler == ALTQT_FAIRQ) {
387 pa->bandwidth = eval_bwspec(bw,
388 parent == NULL ? pa->ifbandwidth : parent->bandwidth);
390 if (pa->bandwidth > pa->ifbandwidth) {
391 fprintf(stderr, "bandwidth for %s higher than "
392 "interface\n", pa->qname);
395 /* check the sum of the child bandwidth is under parent's */
396 if (parent != NULL) {
397 if (pa->bandwidth > parent->bandwidth) {
398 warnx("bandwidth for %s higher than parent",
403 TAILQ_FOREACH(altq, &altqs, entries) {
404 if (strncmp(altq->ifname, pa->ifname,
406 altq->qname[0] != 0 &&
407 strncmp(altq->parent, pa->parent,
409 bwsum += altq->bandwidth;
411 bwsum += pa->bandwidth;
412 if (bwsum > parent->bandwidth) {
413 warnx("the sum of the child bandwidth higher"
414 " than parent \"%s\"", parent->qname);
419 if (eval_queue_opts(pa, opts,
420 parent == NULL ? pa->ifbandwidth : parent->bandwidth))
423 switch (pa->scheduler) {
425 error = eval_pfqueue_cbq(pf, pa);
428 error = eval_pfqueue_priq(pf, pa);
431 error = eval_pfqueue_hfsc(pf, pa);
434 error = eval_pfqueue_fairq(pf, pa);
443 * CBQ support functions
445 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
446 #define RM_NS_PER_SEC (1000000000)
449 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
451 struct cbq_opts *opts;
454 if (pa->priority >= CBQ_MAXPRI) {
455 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
459 ifmtu = getifmtu(pa->ifname);
460 opts = &pa->pq_u.cbq_opts;
462 if (opts->pktsize == 0) { /* use default */
463 opts->pktsize = ifmtu;
464 if (opts->pktsize > MCLBYTES) /* do what TCP does */
465 opts->pktsize &= ~MCLBYTES;
466 } else if (opts->pktsize > ifmtu)
467 opts->pktsize = ifmtu;
468 if (opts->maxpktsize == 0) /* use default */
469 opts->maxpktsize = ifmtu;
470 else if (opts->maxpktsize > ifmtu)
471 opts->pktsize = ifmtu;
473 if (opts->pktsize > opts->maxpktsize)
474 opts->pktsize = opts->maxpktsize;
476 if (pa->parent[0] == 0)
477 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
479 cbq_compute_idletime(pf, pa);
484 * compute ns_per_byte, maxidle, minidle, and offtime
487 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
489 struct cbq_opts *opts;
490 double maxidle_s, maxidle, minidle;
491 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
492 double z, g, f, gton, gtom;
493 u_int minburst, maxburst;
495 opts = &pa->pq_u.cbq_opts;
496 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
497 minburst = opts->minburst;
498 maxburst = opts->maxburst;
500 if (pa->bandwidth == 0)
501 f = 0.0001; /* small enough? */
503 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
505 nsPerByte = ifnsPerByte / f;
506 ptime = (double)opts->pktsize * ifnsPerByte;
507 cptime = ptime * (1.0 - f) / f;
509 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
511 * this causes integer overflow in kernel!
512 * (bandwidth < 6Kbps when max_pkt_size=1500)
514 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0) {
515 warnx("queue bandwidth must be larger than %s",
516 rate2str(ifnsPerByte * (double)opts->maxpktsize /
517 (double)INT_MAX * (double)pa->ifbandwidth));
518 fprintf(stderr, "cbq: queue %s is too slow!\n",
521 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
524 if (maxburst == 0) { /* use default */
525 if (cptime > 10.0 * 1000000)
530 if (minburst == 0) /* use default */
532 if (minburst > maxburst)
535 z = (double)(1 << RM_FILTER_GAIN);
537 gton = pow(g, (double)maxburst);
538 gtom = pow(g, (double)(minburst-1));
539 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
540 maxidle_s = (1.0 - g);
541 if (maxidle > maxidle_s)
542 maxidle = ptime * maxidle;
544 maxidle = ptime * maxidle_s;
545 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
546 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
548 /* scale parameters */
549 maxidle = ((maxidle * 8.0) / nsPerByte) *
550 pow(2.0, (double)RM_FILTER_GAIN);
551 offtime = (offtime * 8.0) / nsPerByte *
552 pow(2.0, (double)RM_FILTER_GAIN);
553 minidle = ((minidle * 8.0) / nsPerByte) *
554 pow(2.0, (double)RM_FILTER_GAIN);
556 maxidle = maxidle / 1000.0;
557 offtime = offtime / 1000.0;
558 minidle = minidle / 1000.0;
560 opts->minburst = minburst;
561 opts->maxburst = maxburst;
562 opts->ns_per_byte = (u_int)nsPerByte;
563 opts->maxidle = (u_int)fabs(maxidle);
564 opts->minidle = (int)minidle;
565 opts->offtime = (u_int)fabs(offtime);
571 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
573 struct pf_altq *altq;
574 int root_class, default_class;
578 * check if cbq has one root queue and one default queue
581 root_class = default_class = 0;
582 TAILQ_FOREACH(altq, &altqs, entries) {
583 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
585 if (altq->qname[0] == 0) /* this is for interface */
587 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
589 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
592 if (root_class != 1) {
593 warnx("should have one root queue on %s", pa->ifname);
596 if (default_class != 1) {
597 warnx("should have one default queue on %s", pa->ifname);
604 print_cbq_opts(const struct pf_altq *a)
606 const struct cbq_opts *opts;
608 opts = &a->pq_u.cbq_opts;
611 if (opts->flags & CBQCLF_RED)
613 if (opts->flags & CBQCLF_ECN)
615 if (opts->flags & CBQCLF_RIO)
617 if (opts->flags & CBQCLF_CODEL)
619 if (opts->flags & CBQCLF_CLEARDSCP)
620 printf(" cleardscp");
621 if (opts->flags & CBQCLF_FLOWVALVE)
622 printf(" flowvalve");
623 if (opts->flags & CBQCLF_BORROW)
625 if (opts->flags & CBQCLF_WRR)
627 if (opts->flags & CBQCLF_EFFICIENT)
628 printf(" efficient");
629 if (opts->flags & CBQCLF_ROOTCLASS)
631 if (opts->flags & CBQCLF_DEFCLASS)
641 * PRIQ support functions
644 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
646 struct pf_altq *altq;
648 if (pa->priority >= PRIQ_MAXPRI) {
649 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
652 /* the priority should be unique for the interface */
653 TAILQ_FOREACH(altq, &altqs, entries) {
654 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
655 altq->qname[0] != 0 && altq->priority == pa->priority) {
656 warnx("%s and %s have the same priority",
657 altq->qname, pa->qname);
666 check_commit_priq(int dev, int opts, struct pf_altq *pa)
668 struct pf_altq *altq;
673 * check if priq has one default class for this interface
676 TAILQ_FOREACH(altq, &altqs, entries) {
677 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
679 if (altq->qname[0] == 0) /* this is for interface */
681 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
684 if (default_class != 1) {
685 warnx("should have one default queue on %s", pa->ifname);
692 print_priq_opts(const struct pf_altq *a)
694 const struct priq_opts *opts;
696 opts = &a->pq_u.priq_opts;
700 if (opts->flags & PRCF_RED)
702 if (opts->flags & PRCF_ECN)
704 if (opts->flags & PRCF_RIO)
706 if (opts->flags & PRCF_CODEL)
708 if (opts->flags & PRCF_CLEARDSCP)
709 printf(" cleardscp");
710 if (opts->flags & PRCF_DEFAULTCLASS)
720 * HFSC support functions
723 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
725 struct pf_altq *altq, *parent;
726 struct hfsc_opts_v1 *opts;
727 struct service_curve sc;
729 opts = &pa->pq_u.hfsc_opts;
731 if (pa->parent[0] == 0) {
733 opts->lssc_m1 = pa->ifbandwidth;
734 opts->lssc_m2 = pa->ifbandwidth;
742 /* if link_share is not specified, use bandwidth */
743 if (opts->lssc_m2 == 0)
744 opts->lssc_m2 = pa->bandwidth;
746 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
747 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
748 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
749 warnx("m2 is zero for %s", pa->qname);
753 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
754 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
755 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
756 warnx("m1 must be zero for convex curve: %s", pa->qname);
762 * for the real-time service curve, the sum of the service curves
763 * should not exceed 80% of the interface bandwidth. 20% is reserved
764 * not to over-commit the actual interface bandwidth.
765 * for the linkshare service curve, the sum of the child service
766 * curve should not exceed the parent service curve.
767 * for the upper-limit service curve, the assigned bandwidth should
768 * be smaller than the interface bandwidth, and the upper-limit should
769 * be larger than the real-time service curve when both are defined.
771 parent = qname_to_pfaltq(pa->parent, pa->ifname);
773 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
775 TAILQ_FOREACH(altq, &altqs, entries) {
776 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
778 if (altq->qname[0] == 0) /* this is for interface */
781 /* if the class has a real-time service curve, add it. */
782 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
783 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
784 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
785 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
786 gsc_add_sc(&rtsc, &sc);
789 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
792 /* if the class has a linkshare service curve, add it. */
793 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
794 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
795 sc.d = altq->pq_u.hfsc_opts.lssc_d;
796 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
797 gsc_add_sc(&lssc, &sc);
801 /* check the real-time service curve. reserve 20% of interface bw */
802 if (opts->rtsc_m2 != 0) {
803 /* add this queue to the sum */
804 sc.m1 = opts->rtsc_m1;
806 sc.m2 = opts->rtsc_m2;
807 gsc_add_sc(&rtsc, &sc);
808 /* compare the sum with 80% of the interface */
811 sc.m2 = pa->ifbandwidth / 100 * 80;
812 if (!is_gsc_under_sc(&rtsc, &sc)) {
813 warnx("real-time sc exceeds 80%% of the interface "
814 "bandwidth (%s)", rate2str((double)sc.m2));
819 /* check the linkshare service curve. */
820 if (opts->lssc_m2 != 0) {
821 /* add this queue to the child sum */
822 sc.m1 = opts->lssc_m1;
824 sc.m2 = opts->lssc_m2;
825 gsc_add_sc(&lssc, &sc);
826 /* compare the sum of the children with parent's sc */
827 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
828 sc.d = parent->pq_u.hfsc_opts.lssc_d;
829 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
830 if (!is_gsc_under_sc(&lssc, &sc)) {
831 warnx("linkshare sc exceeds parent's sc");
836 /* check the upper-limit service curve. */
837 if (opts->ulsc_m2 != 0) {
838 if (opts->ulsc_m1 > pa->ifbandwidth ||
839 opts->ulsc_m2 > pa->ifbandwidth) {
840 warnx("upper-limit larger than interface bandwidth");
843 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
844 warnx("upper-limit sc smaller than real-time sc");
861 * FAIRQ support functions
864 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
866 struct pf_altq *altq, *parent;
867 struct fairq_opts *opts;
868 struct service_curve sc;
870 opts = &pa->pq_u.fairq_opts;
872 if (pa->parent[0] == 0) {
874 opts->lssc_m1 = pa->ifbandwidth;
875 opts->lssc_m2 = pa->ifbandwidth;
882 /* if link_share is not specified, use bandwidth */
883 if (opts->lssc_m2 == 0)
884 opts->lssc_m2 = pa->bandwidth;
888 * for the real-time service curve, the sum of the service curves
889 * should not exceed 80% of the interface bandwidth. 20% is reserved
890 * not to over-commit the actual interface bandwidth.
891 * for the link-sharing service curve, the sum of the child service
892 * curve should not exceed the parent service curve.
893 * for the upper-limit service curve, the assigned bandwidth should
894 * be smaller than the interface bandwidth, and the upper-limit should
895 * be larger than the real-time service curve when both are defined.
897 parent = qname_to_pfaltq(pa->parent, pa->ifname);
899 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
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 */
907 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
910 /* if the class has a link-sharing service curve, add it. */
911 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
912 sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
913 sc.d = altq->pq_u.fairq_opts.lssc_d;
914 sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
915 gsc_add_sc(&lssc, &sc);
919 /* check the link-sharing service curve. */
920 if (opts->lssc_m2 != 0) {
921 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
922 sc.d = parent->pq_u.fairq_opts.lssc_d;
923 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
924 if (!is_gsc_under_sc(&lssc, &sc)) {
925 warnx("link-sharing sc exceeds parent's sc");
940 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
942 struct pf_altq *altq, *def = NULL;
946 /* check if hfsc has one default queue for this interface */
948 TAILQ_FOREACH(altq, &altqs, entries) {
949 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
951 if (altq->qname[0] == 0) /* this is for interface */
953 if (altq->parent[0] == 0) /* dummy root */
955 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
960 if (default_class != 1) {
961 warnx("should have one default queue on %s", pa->ifname);
964 /* make sure the default queue is a leaf */
965 TAILQ_FOREACH(altq, &altqs, entries) {
966 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
968 if (altq->qname[0] == 0) /* this is for interface */
970 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
971 warnx("default queue is not a leaf");
979 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
981 struct pf_altq *altq, *def = NULL;
985 /* check if fairq has one default queue for this interface */
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 (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
997 if (default_class != 1) {
998 warnx("should have one default queue on %s", pa->ifname);
1001 /* make sure the default queue is a leaf */
1002 TAILQ_FOREACH(altq, &altqs, entries) {
1003 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
1005 if (altq->qname[0] == 0) /* this is for interface */
1007 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
1008 warnx("default queue is not a leaf");
1016 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1018 const struct hfsc_opts_v1 *opts;
1019 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
1021 opts = &a->pq_u.hfsc_opts;
1023 rtsc = lssc = ulsc = NULL;
1025 rtsc = &qopts->data.hfsc_opts.realtime;
1026 lssc = &qopts->data.hfsc_opts.linkshare;
1027 ulsc = &qopts->data.hfsc_opts.upperlimit;
1030 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
1031 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1032 opts->lssc_d != 0))) {
1034 if (opts->flags & HFCF_RED)
1036 if (opts->flags & HFCF_ECN)
1038 if (opts->flags & HFCF_RIO)
1040 if (opts->flags & HFCF_CODEL)
1042 if (opts->flags & HFCF_CLEARDSCP)
1043 printf(" cleardscp");
1044 if (opts->flags & HFCF_DEFAULTCLASS)
1046 if (opts->rtsc_m2 != 0)
1047 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
1048 opts->rtsc_m2, rtsc);
1049 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1051 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1052 opts->lssc_m2, lssc);
1053 if (opts->ulsc_m2 != 0)
1054 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1055 opts->ulsc_m2, ulsc);
1064 print_codel_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1066 const struct codel_opts *opts;
1068 opts = &a->pq_u.codel_opts;
1069 if (opts->target || opts->interval || opts->ecn) {
1072 printf(" target %d", opts->target);
1074 printf(" interval %d", opts->interval);
1086 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1088 const struct fairq_opts *opts;
1089 const struct node_fairq_sc *loc_lssc;
1091 opts = &a->pq_u.fairq_opts;
1095 loc_lssc = &qopts->data.fairq_opts.linkshare;
1098 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1099 opts->lssc_d != 0))) {
1101 if (opts->flags & FARF_RED)
1103 if (opts->flags & FARF_ECN)
1105 if (opts->flags & FARF_RIO)
1107 if (opts->flags & FARF_CODEL)
1109 if (opts->flags & FARF_CLEARDSCP)
1110 printf(" cleardscp");
1111 if (opts->flags & FARF_DEFAULTCLASS)
1113 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1115 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1116 opts->lssc_m2, loc_lssc);
1125 * admission control using generalized service curve
1128 /* add a new service curve to a generalized service curve */
1130 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1135 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1136 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1140 * check whether all points of a generalized service curve have
1141 * their y-coordinates no larger than a given two-piece linear
1145 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1147 struct segment *s, *last, *end;
1150 if (is_sc_null(sc)) {
1151 if (LIST_EMPTY(gsc))
1153 LIST_FOREACH(s, gsc, _next) {
1160 * gsc has a dummy entry at the end with x = INFINITY.
1161 * loop through up to this dummy entry.
1163 end = gsc_getentry(gsc, INFINITY);
1167 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1168 if (s->y > sc_x2y(sc, s->x))
1172 /* last now holds the real last segment */
1175 if (last->m > sc->m2)
1177 if (last->x < sc->d && last->m > sc->m1) {
1178 y = last->y + (sc->d - last->x) * last->m;
1179 if (y > sc_x2y(sc, sc->d))
1186 gsc_destroy(struct gen_sc *gsc)
1190 while ((s = LIST_FIRST(gsc)) != NULL) {
1191 LIST_REMOVE(s, _next);
1197 * return a segment entry starting at x.
1198 * if gsc has no entry starting at x, a new entry is created at x.
1200 static struct segment *
1201 gsc_getentry(struct gen_sc *gsc, double x)
1203 struct segment *new, *prev, *s;
1206 LIST_FOREACH(s, gsc, _next) {
1208 return (s); /* matching entry found */
1215 /* we have to create a new entry */
1216 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1220 if (x == INFINITY || s == NULL)
1222 else if (s->x == INFINITY)
1227 /* insert the new entry at the head of the list */
1230 LIST_INSERT_HEAD(gsc, new, _next);
1233 * the start point intersects with the segment pointed by
1234 * prev. divide prev into 2 segments
1236 if (x == INFINITY) {
1243 prev->d = x - prev->x;
1244 new->y = prev->d * prev->m + prev->y;
1247 LIST_INSERT_AFTER(prev, new, _next);
1252 /* add a segment to a generalized service curve */
1254 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1256 struct segment *start, *end, *s;
1263 start = gsc_getentry(gsc, x);
1264 end = gsc_getentry(gsc, x2);
1265 if (start == NULL || end == NULL)
1268 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1270 s->y += y + (s->x - x) * m;
1273 end = gsc_getentry(gsc, INFINITY);
1274 for (; s != end; s = LIST_NEXT(s, _next)) {
1281 /* get y-projection of a service curve */
1283 sc_x2y(struct service_curve *sc, double x)
1287 if (x <= (double)sc->d)
1288 /* y belongs to the 1st segment */
1289 y = x * (double)sc->m1;
1291 /* y belongs to the 2nd segment */
1292 y = (double)sc->d * (double)sc->m1
1293 + (x - (double)sc->d) * (double)sc->m2;
1301 #define RATESTR_MAX 16
1304 rate2str(double rate)
1307 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1310 static const char unit[] = " KMG";
1312 buf = r2sbuf[idx++];
1313 if (idx == R2S_BUFS)
1316 for (i = 0; rate >= 1000 && i <= 3; i++)
1319 if ((int)(rate * 100) % 100)
1320 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1322 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1330 * FreeBSD does not have SIOCGIFDATA.
1331 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1334 getifspeed(int pfdev, char *ifname)
1336 struct pf_ifspeed io;
1338 bzero(&io, sizeof io);
1339 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1341 errx(1, "getifspeed: strlcpy");
1342 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1343 err(1, "DIOCGIFSPEED");
1344 return (io.baudrate);
1348 getifspeed(char *ifname)
1352 struct if_data ifrdat;
1354 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1356 bzero(&ifr, sizeof(ifr));
1357 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1358 sizeof(ifr.ifr_name))
1359 errx(1, "getifspeed: strlcpy");
1360 ifr.ifr_data = (caddr_t)&ifrdat;
1361 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1362 err(1, "SIOCGIFDATA");
1365 return ((u_int32_t)ifrdat.ifi_baudrate);
1370 getifmtu(char *ifname)
1375 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1377 bzero(&ifr, sizeof(ifr));
1378 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1379 sizeof(ifr.ifr_name))
1380 errx(1, "getifmtu: strlcpy");
1381 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1385 err(1, "SIOCGIFMTU");
1389 if (ifr.ifr_mtu > 0)
1390 return (ifr.ifr_mtu);
1392 warnx("could not get mtu for %s, assuming 1500", ifname);
1398 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1403 switch (pa->scheduler) {
1405 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1408 pa->pq_u.priq_opts = opts->data.priq_opts;
1411 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1412 if (opts->data.hfsc_opts.linkshare.used) {
1413 pa->pq_u.hfsc_opts.lssc_m1 =
1414 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1416 pa->pq_u.hfsc_opts.lssc_m2 =
1417 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1419 pa->pq_u.hfsc_opts.lssc_d =
1420 opts->data.hfsc_opts.linkshare.d;
1422 if (opts->data.hfsc_opts.realtime.used) {
1423 pa->pq_u.hfsc_opts.rtsc_m1 =
1424 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1426 pa->pq_u.hfsc_opts.rtsc_m2 =
1427 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1429 pa->pq_u.hfsc_opts.rtsc_d =
1430 opts->data.hfsc_opts.realtime.d;
1432 if (opts->data.hfsc_opts.upperlimit.used) {
1433 pa->pq_u.hfsc_opts.ulsc_m1 =
1434 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1436 pa->pq_u.hfsc_opts.ulsc_m2 =
1437 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1439 pa->pq_u.hfsc_opts.ulsc_d =
1440 opts->data.hfsc_opts.upperlimit.d;
1444 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1445 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1446 pa->pq_u.fairq_opts.hogs_m1 =
1447 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1449 if (opts->data.fairq_opts.linkshare.used) {
1450 pa->pq_u.fairq_opts.lssc_m1 =
1451 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1453 pa->pq_u.fairq_opts.lssc_m2 =
1454 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1456 pa->pq_u.fairq_opts.lssc_d =
1457 opts->data.fairq_opts.linkshare.d;
1461 pa->pq_u.codel_opts.target = opts->data.codel_opts.target;
1462 pa->pq_u.codel_opts.interval = opts->data.codel_opts.interval;
1463 pa->pq_u.codel_opts.ecn = opts->data.codel_opts.ecn;
1466 warnx("eval_queue_opts: unknown scheduler type %u",
1476 * If absolute bandwidth if set, return the lesser of that value and the
1477 * reference bandwidth. Limiting to the reference bandwidth allows simple
1478 * limiting of configured bandwidth parameters for schedulers that are
1479 * 32-bit limited, as the root/interface bandwidth (top-level reference
1480 * bandwidth) will be properly limited in that case.
1482 * Otherwise, if the absolute bandwidth is not set, return given percentage
1483 * of reference bandwidth.
1486 eval_bwspec(struct node_queue_bw *bw, u_int64_t ref_bw)
1488 if (bw->bw_absolute > 0)
1489 return (MIN(bw->bw_absolute, ref_bw));
1491 if (bw->bw_percent > 0)
1492 return (ref_bw / 100 * bw->bw_percent);
1498 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1499 const struct node_hfsc_sc *sc)
1501 printf(" %s", scname);
1505 if (sc != NULL && sc->m1.bw_percent > 0)
1506 printf("%u%%", sc->m1.bw_percent);
1508 printf("%s", rate2str((double)m1));
1512 if (sc != NULL && sc->m2.bw_percent > 0)
1513 printf(" %u%%", sc->m2.bw_percent);
1515 printf(" %s", rate2str((double)m2));
1522 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1523 const struct node_fairq_sc *sc)
1525 printf(" %s", scname);
1529 if (sc != NULL && sc->m1.bw_percent > 0)
1530 printf("%u%%", sc->m1.bw_percent);
1532 printf("%s", rate2str((double)m1));
1536 if (sc != NULL && sc->m2.bw_percent > 0)
1537 printf(" %u%%", sc->m2.bw_percent);
1539 printf(" %s", rate2str((double)m2));