1 /* $OpenBSD: pfctl_altq.c,v 1.93 2007/10/15 02:16:35 deraadt Exp $ */
5 * Sony Computer Science Laboratories Inc.
6 * Copyright (c) 2002, 2003 Henning Brauer <henning@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <net/pfvar.h>
41 #include <altq/altq.h>
42 #include <altq/altq_cbq.h>
43 #include <altq/altq_priq.h>
44 #include <altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
47 #include "pfctl_parser.h"
50 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
52 TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
53 LIST_HEAD(gen_sc, segment) rtsc, lssc;
55 struct pf_altq *qname_to_pfaltq(const char *, const char *);
56 u_int32_t qname_to_qid(const char *);
58 static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *);
59 static int cbq_compute_idletime(struct pfctl *, struct pf_altq *);
60 static int check_commit_cbq(int, int, struct pf_altq *);
61 static int print_cbq_opts(const struct pf_altq *);
63 static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *);
64 static int check_commit_priq(int, int, struct pf_altq *);
65 static int print_priq_opts(const struct pf_altq *);
67 static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *);
68 static int check_commit_hfsc(int, int, struct pf_altq *);
69 static int print_hfsc_opts(const struct pf_altq *,
70 const struct node_queue_opt *);
72 static int eval_pfqueue_fairq(struct pfctl *, struct pf_altq *);
73 static int print_fairq_opts(const struct pf_altq *,
74 const struct node_queue_opt *);
75 static int check_commit_fairq(int, int, struct pf_altq *);
77 static void gsc_add_sc(struct gen_sc *, struct service_curve *);
78 static int is_gsc_under_sc(struct gen_sc *,
79 struct service_curve *);
80 static void gsc_destroy(struct gen_sc *);
81 static struct segment *gsc_getentry(struct gen_sc *, double);
82 static int gsc_add_seg(struct gen_sc *, double, double, double,
84 static double sc_x2y(struct service_curve *, double);
87 u_int32_t getifspeed(int, char *);
89 u_int32_t getifspeed(char *);
91 u_long getifmtu(char *);
92 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
94 u_int32_t eval_bwspec(struct node_queue_bw *, u_int32_t);
95 void print_hfsc_sc(const char *, u_int, u_int, u_int,
96 const struct node_hfsc_sc *);
97 void print_fairq_sc(const char *, u_int, u_int, u_int,
98 const struct node_fairq_sc *);
101 pfaltq_store(struct pf_altq *a)
103 struct pf_altq *altq;
105 if ((altq = malloc(sizeof(*altq))) == NULL)
107 memcpy(altq, a, sizeof(struct pf_altq));
108 TAILQ_INSERT_TAIL(&altqs, altq, entries);
112 pfaltq_lookup(const char *ifname)
114 struct pf_altq *altq;
116 TAILQ_FOREACH(altq, &altqs, entries) {
117 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
125 qname_to_pfaltq(const char *qname, const char *ifname)
127 struct pf_altq *altq;
129 TAILQ_FOREACH(altq, &altqs, entries) {
130 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
131 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
138 qname_to_qid(const char *qname)
140 struct pf_altq *altq;
143 * We guarantee that same named queues on different interfaces
144 * have the same qid, so we do NOT need to limit matching on
148 TAILQ_FOREACH(altq, &altqs, entries) {
149 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
156 print_altq(const struct pf_altq *a, unsigned int level,
157 struct node_queue_bw *bw, struct node_queue_opt *qopts)
159 if (a->qname[0] != 0) {
160 print_queue(a, level, bw, 1, qopts);
165 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
169 printf("altq on %s ", a->ifname);
171 switch (a->scheduler) {
173 if (!print_cbq_opts(a))
177 if (!print_priq_opts(a))
181 if (!print_hfsc_opts(a, qopts))
185 if (!print_fairq_opts(a, qopts))
190 if (bw != NULL && bw->bw_percent > 0) {
191 if (bw->bw_percent < 100)
192 printf("bandwidth %u%% ", bw->bw_percent);
194 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
196 if (a->qlimit != DEFAULT_QLIMIT)
197 printf("qlimit %u ", a->qlimit);
198 printf("tbrsize %u ", a->tbrsize);
202 print_queue(const struct pf_altq *a, unsigned int level,
203 struct node_queue_bw *bw, int print_interface,
204 struct node_queue_opt *qopts)
209 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
213 for (i = 0; i < level; ++i)
215 printf("%s ", a->qname);
217 printf("on %s ", a->ifname);
218 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC ||
219 a->scheduler == ALTQT_FAIRQ) {
220 if (bw != NULL && bw->bw_percent > 0) {
221 if (bw->bw_percent < 100)
222 printf("bandwidth %u%% ", bw->bw_percent);
224 printf("bandwidth %s ", rate2str((double)a->bandwidth));
226 if (a->priority != DEFAULT_PRIORITY)
227 printf("priority %u ", a->priority);
228 if (a->qlimit != DEFAULT_QLIMIT)
229 printf("qlimit %u ", a->qlimit);
230 switch (a->scheduler) {
238 print_hfsc_opts(a, qopts);
241 print_fairq_opts(a, qopts);
247 * eval_pfaltq computes the discipline parameters.
250 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
251 struct node_queue_opt *opts)
253 u_int rate, size, errors = 0;
255 if (bw->bw_absolute > 0)
256 pa->ifbandwidth = bw->bw_absolute;
259 if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) {
261 if ((rate = getifspeed(pa->ifname)) == 0) {
263 fprintf(stderr, "interface %s does not know its bandwidth, "
264 "please specify an absolute bandwidth\n",
267 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
268 pa->ifbandwidth = rate;
270 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
272 /* if tbrsize is not specified, use heuristics */
273 if (pa->tbrsize == 0) {
274 rate = pa->ifbandwidth;
275 if (rate <= 1 * 1000 * 1000)
277 else if (rate <= 10 * 1000 * 1000)
279 else if (rate <= 200 * 1000 * 1000)
283 size = size * getifmtu(pa->ifname);
292 * check_commit_altq does consistency check for each interface
295 check_commit_altq(int dev, int opts)
297 struct pf_altq *altq;
300 /* call the discipline check for each interface. */
301 TAILQ_FOREACH(altq, &altqs, entries) {
302 if (altq->qname[0] == 0) {
303 switch (altq->scheduler) {
305 error = check_commit_cbq(dev, opts, altq);
308 error = check_commit_priq(dev, opts, altq);
311 error = check_commit_hfsc(dev, opts, altq);
314 error = check_commit_fairq(dev, opts, altq);
325 * eval_pfqueue computes the queue parameters.
328 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
329 struct node_queue_opt *opts)
331 /* should be merged with expand_queue */
332 struct pf_altq *if_pa, *parent, *altq;
336 /* find the corresponding interface and copy fields used by queues */
337 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
338 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
341 pa->scheduler = if_pa->scheduler;
342 pa->ifbandwidth = if_pa->ifbandwidth;
344 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
345 fprintf(stderr, "queue %s already exists on interface %s\n",
346 pa->qname, pa->ifname);
349 pa->qid = qname_to_qid(pa->qname);
352 if (pa->parent[0] != 0) {
353 parent = qname_to_pfaltq(pa->parent, pa->ifname);
354 if (parent == NULL) {
355 fprintf(stderr, "parent %s not found for %s\n",
356 pa->parent, pa->qname);
359 pa->parent_qid = parent->qid;
362 pa->qlimit = DEFAULT_QLIMIT;
364 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC ||
365 pa->scheduler == ALTQT_FAIRQ) {
366 pa->bandwidth = eval_bwspec(bw,
367 parent == NULL ? 0 : parent->bandwidth);
369 if (pa->bandwidth > pa->ifbandwidth) {
370 fprintf(stderr, "bandwidth for %s higher than "
371 "interface\n", pa->qname);
374 /* check the sum of the child bandwidth is under parent's */
375 if (parent != NULL) {
376 if (pa->bandwidth > parent->bandwidth) {
377 warnx("bandwidth for %s higher than parent",
382 TAILQ_FOREACH(altq, &altqs, entries) {
383 if (strncmp(altq->ifname, pa->ifname,
385 altq->qname[0] != 0 &&
386 strncmp(altq->parent, pa->parent,
388 bwsum += altq->bandwidth;
390 bwsum += pa->bandwidth;
391 if (bwsum > parent->bandwidth) {
392 warnx("the sum of the child bandwidth higher"
393 " than parent \"%s\"", parent->qname);
398 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
401 switch (pa->scheduler) {
403 error = eval_pfqueue_cbq(pf, pa);
406 error = eval_pfqueue_priq(pf, pa);
409 error = eval_pfqueue_hfsc(pf, pa);
412 error = eval_pfqueue_fairq(pf, pa);
421 * CBQ support functions
423 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
424 #define RM_NS_PER_SEC (1000000000)
427 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
429 struct cbq_opts *opts;
432 if (pa->priority >= CBQ_MAXPRI) {
433 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
437 ifmtu = getifmtu(pa->ifname);
438 opts = &pa->pq_u.cbq_opts;
440 if (opts->pktsize == 0) { /* use default */
441 opts->pktsize = ifmtu;
442 if (opts->pktsize > MCLBYTES) /* do what TCP does */
443 opts->pktsize &= ~MCLBYTES;
444 } else if (opts->pktsize > ifmtu)
445 opts->pktsize = ifmtu;
446 if (opts->maxpktsize == 0) /* use default */
447 opts->maxpktsize = ifmtu;
448 else if (opts->maxpktsize > ifmtu)
449 opts->pktsize = ifmtu;
451 if (opts->pktsize > opts->maxpktsize)
452 opts->pktsize = opts->maxpktsize;
454 if (pa->parent[0] == 0)
455 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
457 cbq_compute_idletime(pf, pa);
462 * compute ns_per_byte, maxidle, minidle, and offtime
465 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
467 struct cbq_opts *opts;
468 double maxidle_s, maxidle, minidle;
469 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
470 double z, g, f, gton, gtom;
471 u_int minburst, maxburst;
473 opts = &pa->pq_u.cbq_opts;
474 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
475 minburst = opts->minburst;
476 maxburst = opts->maxburst;
478 if (pa->bandwidth == 0)
479 f = 0.0001; /* small enough? */
481 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
483 nsPerByte = ifnsPerByte / f;
484 ptime = (double)opts->pktsize * ifnsPerByte;
485 cptime = ptime * (1.0 - f) / f;
487 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
489 * this causes integer overflow in kernel!
490 * (bandwidth < 6Kbps when max_pkt_size=1500)
492 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
493 warnx("queue bandwidth must be larger than %s",
494 rate2str(ifnsPerByte * (double)opts->maxpktsize /
495 (double)INT_MAX * (double)pa->ifbandwidth));
496 fprintf(stderr, "cbq: queue %s is too slow!\n",
498 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
501 if (maxburst == 0) { /* use default */
502 if (cptime > 10.0 * 1000000)
507 if (minburst == 0) /* use default */
509 if (minburst > maxburst)
512 z = (double)(1 << RM_FILTER_GAIN);
514 gton = pow(g, (double)maxburst);
515 gtom = pow(g, (double)(minburst-1));
516 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
517 maxidle_s = (1.0 - g);
518 if (maxidle > maxidle_s)
519 maxidle = ptime * maxidle;
521 maxidle = ptime * maxidle_s;
522 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
523 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
525 /* scale parameters */
526 maxidle = ((maxidle * 8.0) / nsPerByte) *
527 pow(2.0, (double)RM_FILTER_GAIN);
528 offtime = (offtime * 8.0) / nsPerByte *
529 pow(2.0, (double)RM_FILTER_GAIN);
530 minidle = ((minidle * 8.0) / nsPerByte) *
531 pow(2.0, (double)RM_FILTER_GAIN);
533 maxidle = maxidle / 1000.0;
534 offtime = offtime / 1000.0;
535 minidle = minidle / 1000.0;
537 opts->minburst = minburst;
538 opts->maxburst = maxburst;
539 opts->ns_per_byte = (u_int)nsPerByte;
540 opts->maxidle = (u_int)fabs(maxidle);
541 opts->minidle = (int)minidle;
542 opts->offtime = (u_int)fabs(offtime);
548 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
550 struct pf_altq *altq;
551 int root_class, default_class;
555 * check if cbq has one root queue and one default queue
558 root_class = default_class = 0;
559 TAILQ_FOREACH(altq, &altqs, entries) {
560 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
562 if (altq->qname[0] == 0) /* this is for interface */
564 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
566 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
569 if (root_class != 1) {
570 warnx("should have one root queue on %s", pa->ifname);
573 if (default_class != 1) {
574 warnx("should have one default queue on %s", pa->ifname);
581 print_cbq_opts(const struct pf_altq *a)
583 const struct cbq_opts *opts;
585 opts = &a->pq_u.cbq_opts;
588 if (opts->flags & CBQCLF_RED)
590 if (opts->flags & CBQCLF_ECN)
592 if (opts->flags & CBQCLF_RIO)
594 if (opts->flags & CBQCLF_CLEARDSCP)
595 printf(" cleardscp");
596 if (opts->flags & CBQCLF_FLOWVALVE)
597 printf(" flowvalve");
598 if (opts->flags & CBQCLF_BORROW)
600 if (opts->flags & CBQCLF_WRR)
602 if (opts->flags & CBQCLF_EFFICIENT)
603 printf(" efficient");
604 if (opts->flags & CBQCLF_ROOTCLASS)
606 if (opts->flags & CBQCLF_DEFCLASS)
616 * PRIQ support functions
619 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
621 struct pf_altq *altq;
623 if (pa->priority >= PRIQ_MAXPRI) {
624 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
627 /* the priority should be unique for the interface */
628 TAILQ_FOREACH(altq, &altqs, entries) {
629 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
630 altq->qname[0] != 0 && altq->priority == pa->priority) {
631 warnx("%s and %s have the same priority",
632 altq->qname, pa->qname);
641 check_commit_priq(int dev, int opts, struct pf_altq *pa)
643 struct pf_altq *altq;
648 * check if priq has one default class for this interface
651 TAILQ_FOREACH(altq, &altqs, entries) {
652 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
654 if (altq->qname[0] == 0) /* this is for interface */
656 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
659 if (default_class != 1) {
660 warnx("should have one default queue on %s", pa->ifname);
667 print_priq_opts(const struct pf_altq *a)
669 const struct priq_opts *opts;
671 opts = &a->pq_u.priq_opts;
675 if (opts->flags & PRCF_RED)
677 if (opts->flags & PRCF_ECN)
679 if (opts->flags & PRCF_RIO)
681 if (opts->flags & PRCF_CLEARDSCP)
682 printf(" cleardscp");
683 if (opts->flags & PRCF_DEFAULTCLASS)
693 * HFSC support functions
696 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
698 struct pf_altq *altq, *parent;
699 struct hfsc_opts *opts;
700 struct service_curve sc;
702 opts = &pa->pq_u.hfsc_opts;
704 if (pa->parent[0] == 0) {
706 opts->lssc_m1 = pa->ifbandwidth;
707 opts->lssc_m2 = pa->ifbandwidth;
715 /* if link_share is not specified, use bandwidth */
716 if (opts->lssc_m2 == 0)
717 opts->lssc_m2 = pa->bandwidth;
719 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
720 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
721 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
722 warnx("m2 is zero for %s", pa->qname);
726 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
727 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
728 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
729 warnx("m1 must be zero for convex curve: %s", pa->qname);
735 * for the real-time service curve, the sum of the service curves
736 * should not exceed 80% of the interface bandwidth. 20% is reserved
737 * not to over-commit the actual interface bandwidth.
738 * for the linkshare service curve, the sum of the child service
739 * curve should not exceed the parent service curve.
740 * for the upper-limit service curve, the assigned bandwidth should
741 * be smaller than the interface bandwidth, and the upper-limit should
742 * be larger than the real-time service curve when both are defined.
744 parent = qname_to_pfaltq(pa->parent, pa->ifname);
746 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
748 TAILQ_FOREACH(altq, &altqs, entries) {
749 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
751 if (altq->qname[0] == 0) /* this is for interface */
754 /* if the class has a real-time service curve, add it. */
755 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
756 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
757 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
758 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
759 gsc_add_sc(&rtsc, &sc);
762 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
765 /* if the class has a linkshare service curve, add it. */
766 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
767 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
768 sc.d = altq->pq_u.hfsc_opts.lssc_d;
769 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
770 gsc_add_sc(&lssc, &sc);
774 /* check the real-time service curve. reserve 20% of interface bw */
775 if (opts->rtsc_m2 != 0) {
776 /* add this queue to the sum */
777 sc.m1 = opts->rtsc_m1;
779 sc.m2 = opts->rtsc_m2;
780 gsc_add_sc(&rtsc, &sc);
781 /* compare the sum with 80% of the interface */
784 sc.m2 = pa->ifbandwidth / 100 * 80;
785 if (!is_gsc_under_sc(&rtsc, &sc)) {
786 warnx("real-time sc exceeds 80%% of the interface "
787 "bandwidth (%s)", rate2str((double)sc.m2));
792 /* check the linkshare service curve. */
793 if (opts->lssc_m2 != 0) {
794 /* add this queue to the child sum */
795 sc.m1 = opts->lssc_m1;
797 sc.m2 = opts->lssc_m2;
798 gsc_add_sc(&lssc, &sc);
799 /* compare the sum of the children with parent's sc */
800 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
801 sc.d = parent->pq_u.hfsc_opts.lssc_d;
802 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
803 if (!is_gsc_under_sc(&lssc, &sc)) {
804 warnx("linkshare sc exceeds parent's sc");
809 /* check the upper-limit service curve. */
810 if (opts->ulsc_m2 != 0) {
811 if (opts->ulsc_m1 > pa->ifbandwidth ||
812 opts->ulsc_m2 > pa->ifbandwidth) {
813 warnx("upper-limit larger than interface bandwidth");
816 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
817 warnx("upper-limit sc smaller than real-time sc");
834 * FAIRQ support functions
837 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
839 struct pf_altq *altq, *parent;
840 struct fairq_opts *opts;
841 struct service_curve sc;
843 opts = &pa->pq_u.fairq_opts;
845 if (pa->parent[0] == 0) {
847 opts->lssc_m1 = pa->ifbandwidth;
848 opts->lssc_m2 = pa->ifbandwidth;
855 /* if link_share is not specified, use bandwidth */
856 if (opts->lssc_m2 == 0)
857 opts->lssc_m2 = pa->bandwidth;
861 * for the real-time service curve, the sum of the service curves
862 * should not exceed 80% of the interface bandwidth. 20% is reserved
863 * not to over-commit the actual interface bandwidth.
864 * for the link-sharing service curve, the sum of the child service
865 * curve should not exceed the parent service curve.
866 * for the upper-limit service curve, the assigned bandwidth should
867 * be smaller than the interface bandwidth, and the upper-limit should
868 * be larger than the real-time service curve when both are defined.
870 parent = qname_to_pfaltq(pa->parent, pa->ifname);
872 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
874 TAILQ_FOREACH(altq, &altqs, entries) {
875 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
877 if (altq->qname[0] == 0) /* this is for interface */
880 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
883 /* if the class has a link-sharing service curve, add it. */
884 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
885 sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
886 sc.d = altq->pq_u.fairq_opts.lssc_d;
887 sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
888 gsc_add_sc(&lssc, &sc);
892 /* check the link-sharing service curve. */
893 if (opts->lssc_m2 != 0) {
894 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
895 sc.d = parent->pq_u.fairq_opts.lssc_d;
896 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
897 if (!is_gsc_under_sc(&lssc, &sc)) {
898 warnx("link-sharing sc exceeds parent's sc");
913 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
915 struct pf_altq *altq, *def = NULL;
919 /* check if hfsc has one default queue for this interface */
921 TAILQ_FOREACH(altq, &altqs, entries) {
922 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
924 if (altq->qname[0] == 0) /* this is for interface */
926 if (altq->parent[0] == 0) /* dummy root */
928 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
933 if (default_class != 1) {
934 warnx("should have one default queue on %s", pa->ifname);
937 /* make sure the default queue is a leaf */
938 TAILQ_FOREACH(altq, &altqs, entries) {
939 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
941 if (altq->qname[0] == 0) /* this is for interface */
943 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
944 warnx("default queue is not a leaf");
952 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
954 struct pf_altq *altq, *def = NULL;
958 /* check if fairq has one default queue for this interface */
960 TAILQ_FOREACH(altq, &altqs, entries) {
961 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
963 if (altq->qname[0] == 0) /* this is for interface */
965 if (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
970 if (default_class != 1) {
971 warnx("should have one default queue on %s", pa->ifname);
974 /* make sure the default queue is a leaf */
975 TAILQ_FOREACH(altq, &altqs, entries) {
976 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
978 if (altq->qname[0] == 0) /* this is for interface */
980 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
981 warnx("default queue is not a leaf");
989 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
991 const struct hfsc_opts *opts;
992 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
994 opts = &a->pq_u.hfsc_opts;
996 rtsc = lssc = ulsc = NULL;
998 rtsc = &qopts->data.hfsc_opts.realtime;
999 lssc = &qopts->data.hfsc_opts.linkshare;
1000 ulsc = &qopts->data.hfsc_opts.upperlimit;
1003 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
1004 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1005 opts->lssc_d != 0))) {
1007 if (opts->flags & HFCF_RED)
1009 if (opts->flags & HFCF_ECN)
1011 if (opts->flags & HFCF_RIO)
1013 if (opts->flags & HFCF_CLEARDSCP)
1014 printf(" cleardscp");
1015 if (opts->flags & HFCF_DEFAULTCLASS)
1017 if (opts->rtsc_m2 != 0)
1018 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
1019 opts->rtsc_m2, rtsc);
1020 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1022 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1023 opts->lssc_m2, lssc);
1024 if (opts->ulsc_m2 != 0)
1025 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1026 opts->ulsc_m2, ulsc);
1035 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1037 const struct fairq_opts *opts;
1038 const struct node_fairq_sc *loc_lssc;
1040 opts = &a->pq_u.fairq_opts;
1044 loc_lssc = &qopts->data.fairq_opts.linkshare;
1047 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1048 opts->lssc_d != 0))) {
1050 if (opts->flags & FARF_RED)
1052 if (opts->flags & FARF_ECN)
1054 if (opts->flags & FARF_RIO)
1056 if (opts->flags & FARF_CLEARDSCP)
1057 printf(" cleardscp");
1058 if (opts->flags & FARF_DEFAULTCLASS)
1060 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1062 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1063 opts->lssc_m2, loc_lssc);
1072 * admission control using generalized service curve
1075 /* add a new service curve to a generalized service curve */
1077 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1082 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1083 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1087 * check whether all points of a generalized service curve have
1088 * their y-coordinates no larger than a given two-piece linear
1092 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1094 struct segment *s, *last, *end;
1097 if (is_sc_null(sc)) {
1098 if (LIST_EMPTY(gsc))
1100 LIST_FOREACH(s, gsc, _next) {
1107 * gsc has a dummy entry at the end with x = INFINITY.
1108 * loop through up to this dummy entry.
1110 end = gsc_getentry(gsc, INFINITY);
1114 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1115 if (s->y > sc_x2y(sc, s->x))
1119 /* last now holds the real last segment */
1122 if (last->m > sc->m2)
1124 if (last->x < sc->d && last->m > sc->m1) {
1125 y = last->y + (sc->d - last->x) * last->m;
1126 if (y > sc_x2y(sc, sc->d))
1133 gsc_destroy(struct gen_sc *gsc)
1137 while ((s = LIST_FIRST(gsc)) != NULL) {
1138 LIST_REMOVE(s, _next);
1144 * return a segment entry starting at x.
1145 * if gsc has no entry starting at x, a new entry is created at x.
1147 static struct segment *
1148 gsc_getentry(struct gen_sc *gsc, double x)
1150 struct segment *new, *prev, *s;
1153 LIST_FOREACH(s, gsc, _next) {
1155 return (s); /* matching entry found */
1162 /* we have to create a new entry */
1163 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1167 if (x == INFINITY || s == NULL)
1169 else if (s->x == INFINITY)
1174 /* insert the new entry at the head of the list */
1177 LIST_INSERT_HEAD(gsc, new, _next);
1180 * the start point intersects with the segment pointed by
1181 * prev. divide prev into 2 segments
1183 if (x == INFINITY) {
1190 prev->d = x - prev->x;
1191 new->y = prev->d * prev->m + prev->y;
1194 LIST_INSERT_AFTER(prev, new, _next);
1199 /* add a segment to a generalized service curve */
1201 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1203 struct segment *start, *end, *s;
1210 start = gsc_getentry(gsc, x);
1211 end = gsc_getentry(gsc, x2);
1212 if (start == NULL || end == NULL)
1215 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1217 s->y += y + (s->x - x) * m;
1220 end = gsc_getentry(gsc, INFINITY);
1221 for (; s != end; s = LIST_NEXT(s, _next)) {
1228 /* get y-projection of a service curve */
1230 sc_x2y(struct service_curve *sc, double x)
1234 if (x <= (double)sc->d)
1235 /* y belongs to the 1st segment */
1236 y = x * (double)sc->m1;
1238 /* y belongs to the 2nd segment */
1239 y = (double)sc->d * (double)sc->m1
1240 + (x - (double)sc->d) * (double)sc->m2;
1248 #define RATESTR_MAX 16
1251 rate2str(double rate)
1254 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1257 static const char unit[] = " KMG";
1259 buf = r2sbuf[idx++];
1260 if (idx == R2S_BUFS)
1263 for (i = 0; rate >= 1000 && i <= 3; i++)
1266 if ((int)(rate * 100) % 100)
1267 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1269 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1277 * FreeBSD does not have SIOCGIFDATA.
1278 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1281 getifspeed(int pfdev, char *ifname)
1283 struct pf_ifspeed io;
1285 bzero(&io, sizeof io);
1286 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1288 errx(1, "getifspeed: strlcpy");
1289 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1290 err(1, "DIOCGIFSPEED");
1291 return ((u_int32_t)io.baudrate);
1295 getifspeed(char *ifname)
1299 struct if_data ifrdat;
1301 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1303 bzero(&ifr, sizeof(ifr));
1304 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1305 sizeof(ifr.ifr_name))
1306 errx(1, "getifspeed: strlcpy");
1307 ifr.ifr_data = (caddr_t)&ifrdat;
1308 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1309 err(1, "SIOCGIFDATA");
1312 return ((u_int32_t)ifrdat.ifi_baudrate);
1317 getifmtu(char *ifname)
1322 if ((s = socket(get_socket_domain(), SOCK_DGRAM, 0)) < 0)
1324 bzero(&ifr, sizeof(ifr));
1325 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1326 sizeof(ifr.ifr_name))
1327 errx(1, "getifmtu: strlcpy");
1328 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1332 err(1, "SIOCGIFMTU");
1336 if (ifr.ifr_mtu > 0)
1337 return (ifr.ifr_mtu);
1339 warnx("could not get mtu for %s, assuming 1500", ifname);
1345 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1350 switch (pa->scheduler) {
1352 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1355 pa->pq_u.priq_opts = opts->data.priq_opts;
1358 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1359 if (opts->data.hfsc_opts.linkshare.used) {
1360 pa->pq_u.hfsc_opts.lssc_m1 =
1361 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1363 pa->pq_u.hfsc_opts.lssc_m2 =
1364 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1366 pa->pq_u.hfsc_opts.lssc_d =
1367 opts->data.hfsc_opts.linkshare.d;
1369 if (opts->data.hfsc_opts.realtime.used) {
1370 pa->pq_u.hfsc_opts.rtsc_m1 =
1371 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1373 pa->pq_u.hfsc_opts.rtsc_m2 =
1374 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1376 pa->pq_u.hfsc_opts.rtsc_d =
1377 opts->data.hfsc_opts.realtime.d;
1379 if (opts->data.hfsc_opts.upperlimit.used) {
1380 pa->pq_u.hfsc_opts.ulsc_m1 =
1381 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1383 pa->pq_u.hfsc_opts.ulsc_m2 =
1384 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1386 pa->pq_u.hfsc_opts.ulsc_d =
1387 opts->data.hfsc_opts.upperlimit.d;
1391 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1392 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1393 pa->pq_u.fairq_opts.hogs_m1 =
1394 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1396 if (opts->data.fairq_opts.linkshare.used) {
1397 pa->pq_u.fairq_opts.lssc_m1 =
1398 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1400 pa->pq_u.fairq_opts.lssc_m2 =
1401 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1403 pa->pq_u.fairq_opts.lssc_d =
1404 opts->data.fairq_opts.linkshare.d;
1408 warnx("eval_queue_opts: unknown scheduler type %u",
1418 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1420 if (bw->bw_absolute > 0)
1421 return (bw->bw_absolute);
1423 if (bw->bw_percent > 0)
1424 return (ref_bw / 100 * bw->bw_percent);
1430 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1431 const struct node_hfsc_sc *sc)
1433 printf(" %s", scname);
1437 if (sc != NULL && sc->m1.bw_percent > 0)
1438 printf("%u%%", sc->m1.bw_percent);
1440 printf("%s", rate2str((double)m1));
1444 if (sc != NULL && sc->m2.bw_percent > 0)
1445 printf(" %u%%", sc->m2.bw_percent);
1447 printf(" %s", rate2str((double)m2));
1454 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1455 const struct node_fairq_sc *sc)
1457 printf(" %s", scname);
1461 if (sc != NULL && sc->m1.bw_percent > 0)
1462 printf("%u%%", sc->m1.bw_percent);
1464 printf("%s", rate2str((double)m1));
1468 if (sc != NULL && sc->m2.bw_percent > 0)
1469 printf(" %u%%", sc->m2.bw_percent);
1471 printf(" %s", rate2str((double)m2));