1 /* $OpenBSD: pfctl_altq.c,v 1.91 2006/11/28 00:08:50 henning 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/param.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>
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 void gsc_add_sc(struct gen_sc *, struct service_curve *);
72 static int is_gsc_under_sc(struct gen_sc *,
73 struct service_curve *);
74 static void gsc_destroy(struct gen_sc *);
75 static struct segment *gsc_getentry(struct gen_sc *, double);
76 static int gsc_add_seg(struct gen_sc *, double, double, double,
78 static double sc_x2y(struct service_curve *, double);
81 u_int32_t getifspeed(int, char *);
83 u_int32_t getifspeed(char *);
85 u_long getifmtu(char *);
86 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
88 u_int32_t eval_bwspec(struct node_queue_bw *, u_int32_t);
89 void print_hfsc_sc(const char *, u_int, u_int, u_int,
90 const struct node_hfsc_sc *);
93 pfaltq_store(struct pf_altq *a)
97 if ((altq = malloc(sizeof(*altq))) == NULL)
99 memcpy(altq, a, sizeof(struct pf_altq));
100 TAILQ_INSERT_TAIL(&altqs, altq, entries);
104 pfaltq_lookup(const char *ifname)
106 struct pf_altq *altq;
108 TAILQ_FOREACH(altq, &altqs, entries) {
109 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
117 qname_to_pfaltq(const char *qname, const char *ifname)
119 struct pf_altq *altq;
121 TAILQ_FOREACH(altq, &altqs, entries) {
122 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
123 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
130 qname_to_qid(const char *qname)
132 struct pf_altq *altq;
135 * We guarantee that same named queues on different interfaces
136 * have the same qid, so we do NOT need to limit matching on
140 TAILQ_FOREACH(altq, &altqs, entries) {
141 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
148 print_altq(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
149 struct node_queue_opt *qopts)
151 if (a->qname[0] != 0) {
152 print_queue(a, level, bw, 1, qopts);
156 printf("altq on %s ", a->ifname);
158 switch (a->scheduler) {
160 if (!print_cbq_opts(a))
164 if (!print_priq_opts(a))
168 if (!print_hfsc_opts(a, qopts))
173 if (bw != NULL && bw->bw_percent > 0) {
174 if (bw->bw_percent < 100)
175 printf("bandwidth %u%% ", bw->bw_percent);
177 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
179 if (a->qlimit != DEFAULT_QLIMIT)
180 printf("qlimit %u ", a->qlimit);
181 printf("tbrsize %u ", a->tbrsize);
185 print_queue(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
186 int print_interface, struct node_queue_opt *qopts)
191 for (i = 0; i < level; ++i)
193 printf("%s ", a->qname);
195 printf("on %s ", a->ifname);
196 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC) {
197 if (bw != NULL && bw->bw_percent > 0) {
198 if (bw->bw_percent < 100)
199 printf("bandwidth %u%% ", bw->bw_percent);
201 printf("bandwidth %s ", rate2str((double)a->bandwidth));
203 if (a->priority != DEFAULT_PRIORITY)
204 printf("priority %u ", a->priority);
205 if (a->qlimit != DEFAULT_QLIMIT)
206 printf("qlimit %u ", a->qlimit);
207 switch (a->scheduler) {
215 print_hfsc_opts(a, qopts);
221 * eval_pfaltq computes the discipline parameters.
224 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
225 struct node_queue_opt *opts)
227 u_int rate, size, errors = 0;
229 if (bw->bw_absolute > 0)
230 pa->ifbandwidth = bw->bw_absolute;
233 if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) {
235 if ((rate = getifspeed(pa->ifname)) == 0) {
237 fprintf(stderr, "interface %s does not know its bandwidth, "
238 "please specify an absolute bandwidth\n",
241 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
242 pa->ifbandwidth = rate;
244 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
246 /* if tbrsize is not specified, use heuristics */
247 if (pa->tbrsize == 0) {
248 rate = pa->ifbandwidth;
249 if (rate <= 1 * 1000 * 1000)
251 else if (rate <= 10 * 1000 * 1000)
253 else if (rate <= 200 * 1000 * 1000)
257 size = size * getifmtu(pa->ifname);
266 * check_commit_altq does consistency check for each interface
269 check_commit_altq(int dev, int opts)
271 struct pf_altq *altq;
274 /* call the discipline check for each interface. */
275 TAILQ_FOREACH(altq, &altqs, entries) {
276 if (altq->qname[0] == 0) {
277 switch (altq->scheduler) {
279 error = check_commit_cbq(dev, opts, altq);
282 error = check_commit_priq(dev, opts, altq);
285 error = check_commit_hfsc(dev, opts, altq);
296 * eval_pfqueue computes the queue parameters.
299 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
300 struct node_queue_opt *opts)
302 /* should be merged with expand_queue */
303 struct pf_altq *if_pa, *parent, *altq;
307 /* find the corresponding interface and copy fields used by queues */
308 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
309 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
312 pa->scheduler = if_pa->scheduler;
313 pa->ifbandwidth = if_pa->ifbandwidth;
315 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
316 fprintf(stderr, "queue %s already exists on interface %s\n",
317 pa->qname, pa->ifname);
320 pa->qid = qname_to_qid(pa->qname);
323 if (pa->parent[0] != 0) {
324 parent = qname_to_pfaltq(pa->parent, pa->ifname);
325 if (parent == NULL) {
326 fprintf(stderr, "parent %s not found for %s\n",
327 pa->parent, pa->qname);
330 pa->parent_qid = parent->qid;
333 pa->qlimit = DEFAULT_QLIMIT;
335 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC) {
336 pa->bandwidth = eval_bwspec(bw,
337 parent == NULL ? 0 : parent->bandwidth);
339 if (pa->bandwidth > pa->ifbandwidth) {
340 fprintf(stderr, "bandwidth for %s higher than "
341 "interface\n", pa->qname);
344 /* check the sum of the child bandwidth is under parent's */
345 if (parent != NULL) {
346 if (pa->bandwidth > parent->bandwidth) {
347 warnx("bandwidth for %s higher than parent",
352 TAILQ_FOREACH(altq, &altqs, entries) {
353 if (strncmp(altq->ifname, pa->ifname,
355 altq->qname[0] != 0 &&
356 strncmp(altq->parent, pa->parent,
358 bwsum += altq->bandwidth;
360 bwsum += pa->bandwidth;
361 if (bwsum > parent->bandwidth) {
362 warnx("the sum of the child bandwidth higher"
363 " than parent \"%s\"", parent->qname);
368 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
371 switch (pa->scheduler) {
373 error = eval_pfqueue_cbq(pf, pa);
376 error = eval_pfqueue_priq(pf, pa);
379 error = eval_pfqueue_hfsc(pf, pa);
388 * CBQ support functions
390 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
391 #define RM_NS_PER_SEC (1000000000)
394 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
396 struct cbq_opts *opts;
399 if (pa->priority >= CBQ_MAXPRI) {
400 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
404 ifmtu = getifmtu(pa->ifname);
405 opts = &pa->pq_u.cbq_opts;
407 if (opts->pktsize == 0) { /* use default */
408 opts->pktsize = ifmtu;
409 if (opts->pktsize > MCLBYTES) /* do what TCP does */
410 opts->pktsize &= ~MCLBYTES;
411 } else if (opts->pktsize > ifmtu)
412 opts->pktsize = ifmtu;
413 if (opts->maxpktsize == 0) /* use default */
414 opts->maxpktsize = ifmtu;
415 else if (opts->maxpktsize > ifmtu)
416 opts->pktsize = ifmtu;
418 if (opts->pktsize > opts->maxpktsize)
419 opts->pktsize = opts->maxpktsize;
421 if (pa->parent[0] == 0)
422 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
424 cbq_compute_idletime(pf, pa);
429 * compute ns_per_byte, maxidle, minidle, and offtime
432 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
434 struct cbq_opts *opts;
435 double maxidle_s, maxidle, minidle;
436 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
437 double z, g, f, gton, gtom;
438 u_int minburst, maxburst;
440 opts = &pa->pq_u.cbq_opts;
441 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
442 minburst = opts->minburst;
443 maxburst = opts->maxburst;
445 if (pa->bandwidth == 0)
446 f = 0.0001; /* small enough? */
448 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
450 nsPerByte = ifnsPerByte / f;
451 ptime = (double)opts->pktsize * ifnsPerByte;
452 cptime = ptime * (1.0 - f) / f;
454 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
456 * this causes integer overflow in kernel!
457 * (bandwidth < 6Kbps when max_pkt_size=1500)
459 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
460 warnx("queue bandwidth must be larger than %s",
461 rate2str(ifnsPerByte * (double)opts->maxpktsize /
462 (double)INT_MAX * (double)pa->ifbandwidth));
463 fprintf(stderr, "cbq: queue %s is too slow!\n",
465 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
468 if (maxburst == 0) { /* use default */
469 if (cptime > 10.0 * 1000000)
474 if (minburst == 0) /* use default */
476 if (minburst > maxburst)
479 z = (double)(1 << RM_FILTER_GAIN);
481 gton = pow(g, (double)maxburst);
482 gtom = pow(g, (double)(minburst-1));
483 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
484 maxidle_s = (1.0 - g);
485 if (maxidle > maxidle_s)
486 maxidle = ptime * maxidle;
488 maxidle = ptime * maxidle_s;
489 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
490 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
492 /* scale parameters */
493 maxidle = ((maxidle * 8.0) / nsPerByte) *
494 pow(2.0, (double)RM_FILTER_GAIN);
495 offtime = (offtime * 8.0) / nsPerByte *
496 pow(2.0, (double)RM_FILTER_GAIN);
497 minidle = ((minidle * 8.0) / nsPerByte) *
498 pow(2.0, (double)RM_FILTER_GAIN);
500 maxidle = maxidle / 1000.0;
501 offtime = offtime / 1000.0;
502 minidle = minidle / 1000.0;
504 opts->minburst = minburst;
505 opts->maxburst = maxburst;
506 opts->ns_per_byte = (u_int)nsPerByte;
507 opts->maxidle = (u_int)fabs(maxidle);
508 opts->minidle = (int)minidle;
509 opts->offtime = (u_int)fabs(offtime);
515 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
517 struct pf_altq *altq;
518 int root_class, default_class;
522 * check if cbq has one root queue and one default queue
525 root_class = default_class = 0;
526 TAILQ_FOREACH(altq, &altqs, entries) {
527 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
529 if (altq->qname[0] == 0) /* this is for interface */
531 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
533 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
536 if (root_class != 1) {
537 warnx("should have one root queue on %s", pa->ifname);
540 if (default_class != 1) {
541 warnx("should have one default queue on %s", pa->ifname);
548 print_cbq_opts(const struct pf_altq *a)
550 const struct cbq_opts *opts;
552 opts = &a->pq_u.cbq_opts;
555 if (opts->flags & CBQCLF_RED)
557 if (opts->flags & CBQCLF_ECN)
559 if (opts->flags & CBQCLF_RIO)
561 if (opts->flags & CBQCLF_CLEARDSCP)
562 printf(" cleardscp");
563 if (opts->flags & CBQCLF_FLOWVALVE)
564 printf(" flowvalve");
565 if (opts->flags & CBQCLF_BORROW)
567 if (opts->flags & CBQCLF_WRR)
569 if (opts->flags & CBQCLF_EFFICIENT)
570 printf(" efficient");
571 if (opts->flags & CBQCLF_ROOTCLASS)
573 if (opts->flags & CBQCLF_DEFCLASS)
583 * PRIQ support functions
586 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
588 struct pf_altq *altq;
590 if (pa->priority >= PRIQ_MAXPRI) {
591 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
594 /* the priority should be unique for the interface */
595 TAILQ_FOREACH(altq, &altqs, entries) {
596 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
597 altq->qname[0] != 0 && altq->priority == pa->priority) {
598 warnx("%s and %s have the same priority",
599 altq->qname, pa->qname);
608 check_commit_priq(int dev, int opts, struct pf_altq *pa)
610 struct pf_altq *altq;
615 * check if priq has one default class for this interface
618 TAILQ_FOREACH(altq, &altqs, entries) {
619 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
621 if (altq->qname[0] == 0) /* this is for interface */
623 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
626 if (default_class != 1) {
627 warnx("should have one default queue on %s", pa->ifname);
634 print_priq_opts(const struct pf_altq *a)
636 const struct priq_opts *opts;
638 opts = &a->pq_u.priq_opts;
642 if (opts->flags & PRCF_RED)
644 if (opts->flags & PRCF_ECN)
646 if (opts->flags & PRCF_RIO)
648 if (opts->flags & PRCF_CLEARDSCP)
649 printf(" cleardscp");
650 if (opts->flags & PRCF_DEFAULTCLASS)
660 * HFSC support functions
663 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
665 struct pf_altq *altq, *parent;
666 struct hfsc_opts *opts;
667 struct service_curve sc;
669 opts = &pa->pq_u.hfsc_opts;
671 if (pa->parent[0] == 0) {
673 opts->lssc_m1 = pa->ifbandwidth;
674 opts->lssc_m2 = pa->ifbandwidth;
682 /* if link_share is not specified, use bandwidth */
683 if (opts->lssc_m2 == 0)
684 opts->lssc_m2 = pa->bandwidth;
686 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
687 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
688 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
689 warnx("m2 is zero for %s", pa->qname);
693 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
694 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
695 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
696 warnx("m1 must be zero for convex curve: %s", pa->qname);
702 * for the real-time service curve, the sum of the service curves
703 * should not exceed 80% of the interface bandwidth. 20% is reserved
704 * not to over-commit the actual interface bandwidth.
705 * for the linkshare service curve, the sum of the child service
706 * curve should not exceed the parent service curve.
707 * for the upper-limit service curve, the assigned bandwidth should
708 * be smaller than the interface bandwidth, and the upper-limit should
709 * be larger than the real-time service curve when both are defined.
711 parent = qname_to_pfaltq(pa->parent, pa->ifname);
713 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
715 TAILQ_FOREACH(altq, &altqs, entries) {
716 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
718 if (altq->qname[0] == 0) /* this is for interface */
721 /* if the class has a real-time service curve, add it. */
722 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
723 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
724 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
725 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
726 gsc_add_sc(&rtsc, &sc);
729 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
732 /* if the class has a linkshare service curve, add it. */
733 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
734 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
735 sc.d = altq->pq_u.hfsc_opts.lssc_d;
736 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
737 gsc_add_sc(&lssc, &sc);
741 /* check the real-time service curve. reserve 20% of interface bw */
742 if (opts->rtsc_m2 != 0) {
743 /* add this queue to the sum */
744 sc.m1 = opts->rtsc_m1;
746 sc.m2 = opts->rtsc_m2;
747 gsc_add_sc(&rtsc, &sc);
748 /* compare the sum with 80% of the interface */
751 sc.m2 = pa->ifbandwidth / 100 * 80;
752 if (!is_gsc_under_sc(&rtsc, &sc)) {
753 warnx("real-time sc exceeds 80%% of the interface "
754 "bandwidth (%s)", rate2str((double)sc.m2));
759 /* check the linkshare service curve. */
760 if (opts->lssc_m2 != 0) {
761 /* add this queue to the child sum */
762 sc.m1 = opts->lssc_m1;
764 sc.m2 = opts->lssc_m2;
765 gsc_add_sc(&lssc, &sc);
766 /* compare the sum of the children with parent's sc */
767 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
768 sc.d = parent->pq_u.hfsc_opts.lssc_d;
769 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
770 if (!is_gsc_under_sc(&lssc, &sc)) {
771 warnx("linkshare sc exceeds parent's sc");
776 /* check the upper-limit service curve. */
777 if (opts->ulsc_m2 != 0) {
778 if (opts->ulsc_m1 > pa->ifbandwidth ||
779 opts->ulsc_m2 > pa->ifbandwidth) {
780 warnx("upper-limit larger than interface bandwidth");
783 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
784 warnx("upper-limit sc smaller than real-time sc");
801 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
803 struct pf_altq *altq, *def = NULL;
807 /* check if hfsc has one default queue for this interface */
809 TAILQ_FOREACH(altq, &altqs, entries) {
810 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
812 if (altq->qname[0] == 0) /* this is for interface */
814 if (altq->parent[0] == 0) /* dummy root */
816 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
821 if (default_class != 1) {
822 warnx("should have one default queue on %s", pa->ifname);
825 /* make sure the default queue is a leaf */
826 TAILQ_FOREACH(altq, &altqs, entries) {
827 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
829 if (altq->qname[0] == 0) /* this is for interface */
831 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
832 warnx("default queue is not a leaf");
840 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
842 const struct hfsc_opts *opts;
843 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
845 opts = &a->pq_u.hfsc_opts;
847 rtsc = lssc = ulsc = NULL;
849 rtsc = &qopts->data.hfsc_opts.realtime;
850 lssc = &qopts->data.hfsc_opts.linkshare;
851 ulsc = &qopts->data.hfsc_opts.upperlimit;
854 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
855 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
856 opts->lssc_d != 0))) {
858 if (opts->flags & HFCF_RED)
860 if (opts->flags & HFCF_ECN)
862 if (opts->flags & HFCF_RIO)
864 if (opts->flags & HFCF_CLEARDSCP)
865 printf(" cleardscp");
866 if (opts->flags & HFCF_DEFAULTCLASS)
868 if (opts->rtsc_m2 != 0)
869 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
870 opts->rtsc_m2, rtsc);
871 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
873 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
874 opts->lssc_m2, lssc);
875 if (opts->ulsc_m2 != 0)
876 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
877 opts->ulsc_m2, ulsc);
886 * admission control using generalized service curve
889 #define INFINITY HUGE_VAL /* positive infinity defined in <math.h> */
892 /* add a new service curve to a generalized service curve */
894 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
899 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
900 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
904 * check whether all points of a generalized service curve have
905 * their y-coordinates no larger than a given two-piece linear
909 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
911 struct segment *s, *last, *end;
914 if (is_sc_null(sc)) {
917 LIST_FOREACH(s, gsc, _next) {
924 * gsc has a dummy entry at the end with x = INFINITY.
925 * loop through up to this dummy entry.
927 end = gsc_getentry(gsc, INFINITY);
931 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
932 if (s->y > sc_x2y(sc, s->x))
936 /* last now holds the real last segment */
939 if (last->m > sc->m2)
941 if (last->x < sc->d && last->m > sc->m1) {
942 y = last->y + (sc->d - last->x) * last->m;
943 if (y > sc_x2y(sc, sc->d))
950 gsc_destroy(struct gen_sc *gsc)
954 while ((s = LIST_FIRST(gsc)) != NULL) {
955 LIST_REMOVE(s, _next);
961 * return a segment entry starting at x.
962 * if gsc has no entry starting at x, a new entry is created at x.
964 static struct segment *
965 gsc_getentry(struct gen_sc *gsc, double x)
967 struct segment *new, *prev, *s;
970 LIST_FOREACH(s, gsc, _next) {
972 return (s); /* matching entry found */
979 /* we have to create a new entry */
980 if ((new = calloc(1, sizeof(struct segment))) == NULL)
984 if (x == INFINITY || s == NULL)
986 else if (s->x == INFINITY)
991 /* insert the new entry at the head of the list */
994 LIST_INSERT_HEAD(gsc, new, _next);
997 * the start point intersects with the segment pointed by
998 * prev. divide prev into 2 segments
1000 if (x == INFINITY) {
1007 prev->d = x - prev->x;
1008 new->y = prev->d * prev->m + prev->y;
1011 LIST_INSERT_AFTER(prev, new, _next);
1016 /* add a segment to a generalized service curve */
1018 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1020 struct segment *start, *end, *s;
1027 start = gsc_getentry(gsc, x);
1028 end = gsc_getentry(gsc, x2);
1029 if (start == NULL || end == NULL)
1032 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1034 s->y += y + (s->x - x) * m;
1037 end = gsc_getentry(gsc, INFINITY);
1038 for (; s != end; s = LIST_NEXT(s, _next)) {
1045 /* get y-projection of a service curve */
1047 sc_x2y(struct service_curve *sc, double x)
1051 if (x <= (double)sc->d)
1052 /* y belongs to the 1st segment */
1053 y = x * (double)sc->m1;
1055 /* y belongs to the 2nd segment */
1056 y = (double)sc->d * (double)sc->m1
1057 + (x - (double)sc->d) * (double)sc->m2;
1065 #define RATESTR_MAX 16
1068 rate2str(double rate)
1071 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1074 static const char unit[] = " KMG";
1076 buf = r2sbuf[idx++];
1077 if (idx == R2S_BUFS)
1080 for (i = 0; rate >= 1000 && i <= 3; i++)
1083 if ((int)(rate * 100) % 100)
1084 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1086 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1094 * FreeBSD does not have SIOCGIFDATA.
1095 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1098 getifspeed(int pfdev, char *ifname)
1100 struct pf_ifspeed io;
1102 bzero(&io, sizeof io);
1103 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1105 errx(1, "getifspeed: strlcpy");
1106 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1107 err(1, "DIOCGIFSPEED");
1108 return ((u_int32_t)io.baudrate);
1112 getifspeed(char *ifname)
1116 struct if_data ifrdat;
1118 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1120 bzero(&ifr, sizeof(ifr));
1121 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1122 sizeof(ifr.ifr_name))
1123 errx(1, "getifspeed: strlcpy");
1124 ifr.ifr_data = (caddr_t)&ifrdat;
1125 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1126 err(1, "SIOCGIFDATA");
1127 if (shutdown(s, SHUT_RDWR) == -1)
1131 return ((u_int32_t)ifrdat.ifi_baudrate);
1136 getifmtu(char *ifname)
1141 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1143 bzero(&ifr, sizeof(ifr));
1144 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1145 sizeof(ifr.ifr_name))
1146 errx(1, "getifmtu: strlcpy");
1147 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1148 err(1, "SIOCGIFMTU");
1149 if (shutdown(s, SHUT_RDWR) == -1)
1153 if (ifr.ifr_mtu > 0)
1154 return (ifr.ifr_mtu);
1156 warnx("could not get mtu for %s, assuming 1500", ifname);
1162 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1167 switch (pa->scheduler) {
1169 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1172 pa->pq_u.priq_opts = opts->data.priq_opts;
1175 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1176 if (opts->data.hfsc_opts.linkshare.used) {
1177 pa->pq_u.hfsc_opts.lssc_m1 =
1178 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1180 pa->pq_u.hfsc_opts.lssc_m2 =
1181 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1183 pa->pq_u.hfsc_opts.lssc_d =
1184 opts->data.hfsc_opts.linkshare.d;
1186 if (opts->data.hfsc_opts.realtime.used) {
1187 pa->pq_u.hfsc_opts.rtsc_m1 =
1188 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1190 pa->pq_u.hfsc_opts.rtsc_m2 =
1191 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1193 pa->pq_u.hfsc_opts.rtsc_d =
1194 opts->data.hfsc_opts.realtime.d;
1196 if (opts->data.hfsc_opts.upperlimit.used) {
1197 pa->pq_u.hfsc_opts.ulsc_m1 =
1198 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1200 pa->pq_u.hfsc_opts.ulsc_m2 =
1201 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1203 pa->pq_u.hfsc_opts.ulsc_d =
1204 opts->data.hfsc_opts.upperlimit.d;
1208 warnx("eval_queue_opts: unknown scheduler type %u",
1218 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1220 if (bw->bw_absolute > 0)
1221 return (bw->bw_absolute);
1223 if (bw->bw_percent > 0)
1224 return (ref_bw / 100 * bw->bw_percent);
1230 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1231 const struct node_hfsc_sc *sc)
1233 printf(" %s", scname);
1237 if (sc != NULL && sc->m1.bw_percent > 0)
1238 printf("%u%%", sc->m1.bw_percent);
1240 printf("%s", rate2str((double)m1));
1244 if (sc != NULL && sc->m2.bw_percent > 0)
1245 printf(" %u%%", sc->m2.bw_percent);
1247 printf(" %s", rate2str((double)m2));