1 /* $OpenBSD: pfctl_altq.c,v 1.83 2004/03/14 21:51:44 dhartmei Exp $ */
2 /* $DragonFly: src/usr.sbin/pfctl/pfctl_altq.c,v 1.1 2004/09/21 21:25:28 joerg Exp $ */
6 * Sony Computer Science Laboratories Inc.
7 * Copyright (c) 2002, 2003 Henning Brauer <henning@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <net/pf/pfvar.h>
39 #include <altq/altq.h>
40 #include <altq/altq_cbq.h>
41 #include <altq/altq_priq.h>
42 #include <altq/altq_hfsc.h>
44 #include "pfctl_parser.h"
47 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
49 TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
50 LIST_HEAD(gen_sc, segment) rtsc, lssc;
52 struct pf_altq *qname_to_pfaltq(const char *, const char *);
53 u_int32_t qname_to_qid(const char *);
55 static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *);
56 static int cbq_compute_idletime(struct pfctl *, struct pf_altq *);
57 static int check_commit_cbq(int, int, struct pf_altq *);
58 static int print_cbq_opts(const struct pf_altq *);
60 static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *);
61 static int check_commit_priq(int, int, struct pf_altq *);
62 static int print_priq_opts(const struct pf_altq *);
64 static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *);
65 static int check_commit_hfsc(int, int, struct pf_altq *);
66 static int print_hfsc_opts(const struct pf_altq *,
67 const struct node_queue_opt *);
69 static void gsc_add_sc(struct gen_sc *, struct service_curve *);
70 static int is_gsc_under_sc(struct gen_sc *,
71 struct service_curve *);
72 static void gsc_destroy(struct gen_sc *);
73 static struct segment *gsc_getentry(struct gen_sc *, double);
74 static int gsc_add_seg(struct gen_sc *, double, double, double,
76 static double sc_x2y(struct service_curve *, double);
78 u_int32_t getifspeed(char *);
79 u_long getifmtu(char *);
80 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
82 u_int32_t eval_bwspec(struct node_queue_bw *, u_int32_t);
83 void print_hfsc_sc(const char *, u_int, u_int, u_int,
84 const struct node_hfsc_sc *);
87 pfaltq_store(struct pf_altq *a)
91 if ((altq = malloc(sizeof(*altq))) == NULL)
93 memcpy(altq, a, sizeof(struct pf_altq));
94 TAILQ_INSERT_TAIL(&altqs, altq, entries);
98 pfaltq_free(struct pf_altq *a)
100 struct pf_altq *altq;
102 TAILQ_FOREACH(altq, &altqs, entries) {
103 if (strncmp(a->ifname, altq->ifname, IFNAMSIZ) == 0 &&
104 strncmp(a->qname, altq->qname, PF_QNAME_SIZE) == 0) {
105 TAILQ_REMOVE(&altqs, altq, entries);
113 pfaltq_lookup(const char *ifname)
115 struct pf_altq *altq;
117 TAILQ_FOREACH(altq, &altqs, entries) {
118 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
126 qname_to_pfaltq(const char *qname, const char *ifname)
128 struct pf_altq *altq;
130 TAILQ_FOREACH(altq, &altqs, entries) {
131 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
132 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
139 qname_to_qid(const char *qname)
141 struct pf_altq *altq;
144 * We guarantee that same named queues on different interfaces
145 * have the same qid, so we do NOT need to limit matching on
149 TAILQ_FOREACH(altq, &altqs, entries) {
150 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
157 print_altq(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
158 struct node_queue_opt *qopts)
160 if (a->qname[0] != 0) {
161 print_queue(a, level, bw, 0, qopts);
165 printf("altq on %s ", a->ifname);
167 switch (a->scheduler) {
169 if (!print_cbq_opts(a))
173 if (!print_priq_opts(a))
177 if (!print_hfsc_opts(a, qopts))
182 if (bw != NULL && bw->bw_percent > 0) {
183 if (bw->bw_percent < 100)
184 printf("bandwidth %u%% ", bw->bw_percent);
186 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
188 if (a->qlimit != DEFAULT_QLIMIT)
189 printf("qlimit %u ", a->qlimit);
190 printf("tbrsize %u ", a->tbrsize);
194 print_queue(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
195 int print_interface, struct node_queue_opt *qopts)
200 for (i = 0; i < level; ++i)
202 printf("%s ", a->qname);
204 printf("on %s ", a->ifname);
205 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC) {
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);
230 * eval_pfaltq computes the discipline parameters.
233 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
234 struct node_queue_opt *opts)
236 u_int rate, size, errors = 0;
238 if (bw->bw_absolute > 0)
239 pa->ifbandwidth = bw->bw_absolute;
241 if ((rate = getifspeed(pa->ifname)) == 0) {
242 fprintf(stderr, "cannot determine interface bandwidth "
243 "for %s, specify an absolute bandwidth\n",
246 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
247 pa->ifbandwidth = rate;
249 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
251 /* if tbrsize is not specified, use heuristics */
252 if (pa->tbrsize == 0) {
253 rate = pa->ifbandwidth;
254 if (rate <= 1 * 1000 * 1000)
256 else if (rate <= 10 * 1000 * 1000)
258 else if (rate <= 200 * 1000 * 1000)
262 size = size * getifmtu(pa->ifname);
271 * check_commit_altq does consistency check for each interface
274 check_commit_altq(int dev, int opts)
276 struct pf_altq *altq;
279 /* call the discipline check for each interface. */
280 TAILQ_FOREACH(altq, &altqs, entries) {
281 if (altq->qname[0] == 0) {
282 switch (altq->scheduler) {
284 error = check_commit_cbq(dev, opts, altq);
287 error = check_commit_priq(dev, opts, altq);
290 error = check_commit_hfsc(dev, opts, altq);
301 * eval_pfqueue computes the queue parameters.
304 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
305 struct node_queue_opt *opts)
307 /* should be merged with expand_queue */
308 struct pf_altq *if_pa, *parent;
311 /* find the corresponding interface and copy fields used by queues */
312 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
313 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
316 pa->scheduler = if_pa->scheduler;
317 pa->ifbandwidth = if_pa->ifbandwidth;
319 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
320 fprintf(stderr, "queue %s already exists on interface %s\n",
321 pa->qname, pa->ifname);
324 pa->qid = qname_to_qid(pa->qname);
327 if (pa->parent[0] != 0) {
328 parent = qname_to_pfaltq(pa->parent, pa->ifname);
329 if (parent == NULL) {
330 fprintf(stderr, "parent %s not found for %s\n",
331 pa->parent, pa->qname);
334 pa->parent_qid = parent->qid;
337 pa->qlimit = DEFAULT_QLIMIT;
339 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC) {
340 if ((pa->bandwidth = eval_bwspec(bw,
341 parent == NULL ? 0 : parent->bandwidth)) == 0) {
342 fprintf(stderr, "bandwidth for %s invalid (%d / %d)\n",
343 pa->qname, bw->bw_absolute, bw->bw_percent);
347 if (pa->bandwidth > pa->ifbandwidth) {
348 fprintf(stderr, "bandwidth for %s higher than "
349 "interface\n", pa->qname);
352 if (parent != NULL && pa->bandwidth > parent->bandwidth) {
353 fprintf(stderr, "bandwidth for %s higher than parent\n",
359 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
362 switch (pa->scheduler) {
364 error = eval_pfqueue_cbq(pf, pa);
367 error = eval_pfqueue_priq(pf, pa);
370 error = eval_pfqueue_hfsc(pf, pa);
379 * CBQ support functions
381 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
382 #define RM_NS_PER_SEC (1000000000)
385 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
387 struct cbq_opts *opts;
390 if (pa->priority >= CBQ_MAXPRI) {
391 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
395 ifmtu = getifmtu(pa->ifname);
396 opts = &pa->pq_u.cbq_opts;
398 if (opts->pktsize == 0) { /* use default */
399 opts->pktsize = ifmtu;
400 if (opts->pktsize > MCLBYTES) /* do what TCP does */
401 opts->pktsize &= ~MCLBYTES;
402 } else if (opts->pktsize > ifmtu)
403 opts->pktsize = ifmtu;
404 if (opts->maxpktsize == 0) /* use default */
405 opts->maxpktsize = ifmtu;
406 else if (opts->maxpktsize > ifmtu)
407 opts->pktsize = ifmtu;
409 if (opts->pktsize > opts->maxpktsize)
410 opts->pktsize = opts->maxpktsize;
412 if (pa->parent[0] == 0)
413 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
415 cbq_compute_idletime(pf, pa);
420 * compute ns_per_byte, maxidle, minidle, and offtime
423 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
425 struct cbq_opts *opts;
426 double maxidle_s, maxidle, minidle;
427 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
428 double z, g, f, gton, gtom;
429 u_int minburst, maxburst;
431 opts = &pa->pq_u.cbq_opts;
432 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
433 minburst = opts->minburst;
434 maxburst = opts->maxburst;
436 if (pa->bandwidth == 0)
437 f = 0.0001; /* small enough? */
439 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
441 nsPerByte = ifnsPerByte / f;
442 ptime = (double)opts->pktsize * ifnsPerByte;
443 cptime = ptime * (1.0 - f) / f;
445 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
447 * this causes integer overflow in kernel!
448 * (bandwidth < 6Kbps when max_pkt_size=1500)
450 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
451 warnx("queue bandwidth must be larger than %s",
452 rate2str(ifnsPerByte * (double)opts->maxpktsize /
453 (double)INT_MAX * (double)pa->ifbandwidth));
454 fprintf(stderr, "cbq: queue %s is too slow!\n",
456 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
459 if (maxburst == 0) { /* use default */
460 if (cptime > 10.0 * 1000000)
465 if (minburst == 0) /* use default */
467 if (minburst > maxburst)
470 z = (double)(1 << RM_FILTER_GAIN);
472 gton = pow(g, (double)maxburst);
473 gtom = pow(g, (double)(minburst-1));
474 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
475 maxidle_s = (1.0 - g);
476 if (maxidle > maxidle_s)
477 maxidle = ptime * maxidle;
479 maxidle = ptime * maxidle_s;
481 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
484 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
486 /* scale parameters */
487 maxidle = ((maxidle * 8.0) / nsPerByte) *
488 pow(2.0, (double)RM_FILTER_GAIN);
489 offtime = (offtime * 8.0) / nsPerByte *
490 pow(2.0, (double)RM_FILTER_GAIN);
491 minidle = ((minidle * 8.0) / nsPerByte) *
492 pow(2.0, (double)RM_FILTER_GAIN);
494 maxidle = maxidle / 1000.0;
495 offtime = offtime / 1000.0;
496 minidle = minidle / 1000.0;
498 opts->minburst = minburst;
499 opts->maxburst = maxburst;
500 opts->ns_per_byte = (u_int)nsPerByte;
501 opts->maxidle = (u_int)fabs(maxidle);
502 opts->minidle = (int)minidle;
503 opts->offtime = (u_int)fabs(offtime);
509 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
511 struct pf_altq *altq;
512 int root_class, default_class;
516 * check if cbq has one root queue and one default queue
519 root_class = default_class = 0;
520 TAILQ_FOREACH(altq, &altqs, entries) {
521 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
523 if (altq->qname[0] == 0) /* this is for interface */
525 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
527 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
530 if (root_class != 1) {
531 warnx("should have one root queue on %s", pa->ifname);
534 if (default_class != 1) {
535 warnx("should have one default queue on %s", pa->ifname);
542 print_cbq_opts(const struct pf_altq *a)
544 const struct cbq_opts *opts;
546 opts = &a->pq_u.cbq_opts;
549 if (opts->flags & CBQCLF_RED)
551 if (opts->flags & CBQCLF_ECN)
553 if (opts->flags & CBQCLF_RIO)
555 if (opts->flags & CBQCLF_CLEARDSCP)
556 printf(" cleardscp");
557 if (opts->flags & CBQCLF_FLOWVALVE)
558 printf(" flowvalve");
559 if (opts->flags & CBQCLF_BORROW)
561 if (opts->flags & CBQCLF_WRR)
563 if (opts->flags & CBQCLF_EFFICIENT)
564 printf(" efficient");
565 if (opts->flags & CBQCLF_ROOTCLASS)
567 if (opts->flags & CBQCLF_DEFCLASS)
577 * PRIQ support functions
580 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
582 struct pf_altq *altq;
584 if (pa->priority >= PRIQ_MAXPRI) {
585 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
588 /* the priority should be unique for the interface */
589 TAILQ_FOREACH(altq, &altqs, entries) {
590 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
591 altq->qname[0] != 0 && altq->priority == pa->priority) {
592 warnx("%s and %s have the same priority",
593 altq->qname, pa->qname);
602 check_commit_priq(int dev, int opts, struct pf_altq *pa)
604 struct pf_altq *altq;
609 * check if priq has one default class for this interface
612 TAILQ_FOREACH(altq, &altqs, entries) {
613 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
615 if (altq->qname[0] == 0) /* this is for interface */
617 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
620 if (default_class != 1) {
621 warnx("should have one default queue on %s", pa->ifname);
628 print_priq_opts(const struct pf_altq *a)
630 const struct priq_opts *opts;
632 opts = &a->pq_u.priq_opts;
636 if (opts->flags & PRCF_RED)
638 if (opts->flags & PRCF_ECN)
640 if (opts->flags & PRCF_RIO)
642 if (opts->flags & PRCF_CLEARDSCP)
643 printf(" cleardscp");
644 if (opts->flags & PRCF_DEFAULTCLASS)
654 * HFSC support functions
657 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
659 struct pf_altq *altq, *parent;
660 struct hfsc_opts *opts;
661 struct service_curve sc;
663 opts = &pa->pq_u.hfsc_opts;
665 if (pa->parent[0] == 0) {
667 opts->lssc_m1 = pa->ifbandwidth;
668 opts->lssc_m2 = pa->ifbandwidth;
676 /* if link_share is not specified, use bandwidth */
677 if (opts->lssc_m2 == 0)
678 opts->lssc_m2 = pa->bandwidth;
680 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
681 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
682 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
683 warnx("m2 is zero for %s", pa->qname);
687 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
688 (opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
689 (opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0)) {
690 warnx("m1 must be zero for convex curve: %s", pa->qname);
696 * for the real-time service curve, the sum of the service curves
697 * should not exceed 80% of the interface bandwidth. 20% is reserved
698 * not to over-commit the actual interface bandwidth.
699 * for the link-sharing service curve, the sum of the child service
700 * curve should not exceed the parent service curve.
701 * for the upper-limit service curve, the assigned bandwidth should
702 * be smaller than the interface bandwidth, and the upper-limit should
703 * be larger than the real-time service curve when both are defined.
705 parent = qname_to_pfaltq(pa->parent, pa->ifname);
707 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
709 TAILQ_FOREACH(altq, &altqs, entries) {
710 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
712 if (altq->qname[0] == 0) /* this is for interface */
715 /* if the class has a real-time service curve, add it. */
716 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
717 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
718 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
719 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
720 gsc_add_sc(&rtsc, &sc);
723 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
726 /* if the class has a link-sharing service curve, add it. */
727 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
728 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
729 sc.d = altq->pq_u.hfsc_opts.lssc_d;
730 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
731 gsc_add_sc(&lssc, &sc);
735 /* check the real-time service curve. reserve 20% of interface bw */
736 if (opts->rtsc_m2 != 0) {
739 sc.m2 = pa->ifbandwidth / 100 * 80;
740 if (!is_gsc_under_sc(&rtsc, &sc)) {
741 warnx("real-time sc exceeds the interface bandwidth");
746 /* check the link-sharing service curve. */
747 if (opts->lssc_m2 != 0) {
748 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
749 sc.d = parent->pq_u.hfsc_opts.lssc_d;
750 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
751 if (!is_gsc_under_sc(&lssc, &sc)) {
752 warnx("link-sharing sc exceeds parent's sc");
757 /* check the upper-limit service curve. */
758 if (opts->ulsc_m2 != 0) {
759 if (opts->ulsc_m1 > pa->ifbandwidth ||
760 opts->ulsc_m2 > pa->ifbandwidth) {
761 warnx("upper-limit larger than interface bandwidth");
764 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
765 warnx("upper-limit sc smaller than real-time sc");
782 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
784 struct pf_altq *altq, *def = NULL;
788 /* check if hfsc has one default queue for this interface */
790 TAILQ_FOREACH(altq, &altqs, entries) {
791 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
793 if (altq->qname[0] == 0) /* this is for interface */
795 if (altq->parent[0] == 0) /* dummy root */
797 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
802 if (default_class != 1) {
803 warnx("should have one default queue on %s", pa->ifname);
806 /* make sure the default queue is a leaf */
807 TAILQ_FOREACH(altq, &altqs, entries) {
808 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
810 if (altq->qname[0] == 0) /* this is for interface */
812 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
813 warnx("default queue is not a leaf");
821 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
823 const struct hfsc_opts *opts;
824 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
826 opts = &a->pq_u.hfsc_opts;
828 rtsc = lssc = ulsc = NULL;
830 rtsc = &qopts->data.hfsc_opts.realtime;
831 lssc = &qopts->data.hfsc_opts.linkshare;
832 ulsc = &qopts->data.hfsc_opts.upperlimit;
835 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
836 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
837 opts->lssc_d != 0))) {
839 if (opts->flags & HFCF_RED)
841 if (opts->flags & HFCF_ECN)
843 if (opts->flags & HFCF_RIO)
845 if (opts->flags & HFCF_CLEARDSCP)
846 printf(" cleardscp");
847 if (opts->flags & HFCF_DEFAULTCLASS)
849 if (opts->rtsc_m2 != 0)
850 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
851 opts->rtsc_m2, rtsc);
852 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
854 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
855 opts->lssc_m2, lssc);
856 if (opts->ulsc_m2 != 0)
857 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
858 opts->ulsc_m2, ulsc);
867 * admission control using generalized service curve
869 #define INFINITY HUGE_VAL /* positive infinity defined in <math.h> */
871 /* add a new service curve to a generalized service curve */
873 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
878 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
879 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
883 * check whether all points of a generalized service curve have
884 * their y-coordinates no larger than a given two-piece linear
888 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
890 struct segment *s, *last, *end;
893 if (is_sc_null(sc)) {
896 LIST_FOREACH(s, gsc, _next) {
903 * gsc has a dummy entry at the end with x = INFINITY.
904 * loop through up to this dummy entry.
906 end = gsc_getentry(gsc, INFINITY);
910 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
911 if (s->y > sc_x2y(sc, s->x))
915 /* last now holds the real last segment */
918 if (last->m > sc->m2)
920 if (last->x < sc->d && last->m > sc->m1) {
921 y = last->y + (sc->d - last->x) * last->m;
922 if (y > sc_x2y(sc, sc->d))
929 gsc_destroy(struct gen_sc *gsc)
933 while ((s = LIST_FIRST(gsc)) != NULL) {
934 LIST_REMOVE(s, _next);
940 * return a segment entry starting at x.
941 * if gsc has no entry starting at x, a new entry is created at x.
943 static struct segment *
944 gsc_getentry(struct gen_sc *gsc, double x)
946 struct segment *new, *prev, *s;
949 LIST_FOREACH(s, gsc, _next) {
951 return (s); /* matching entry found */
958 /* we have to create a new entry */
959 if ((new = calloc(1, sizeof(struct segment))) == NULL)
963 if (x == INFINITY || s == NULL)
965 else if (s->x == INFINITY)
970 /* insert the new entry at the head of the list */
973 LIST_INSERT_HEAD(gsc, new, _next);
976 * the start point intersects with the segment pointed by
977 * prev. divide prev into 2 segments
986 prev->d = x - prev->x;
987 new->y = prev->d * prev->m + prev->y;
990 LIST_INSERT_AFTER(prev, new, _next);
995 /* add a segment to a generalized service curve */
997 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
999 struct segment *start, *end, *s;
1006 start = gsc_getentry(gsc, x);
1007 end = gsc_getentry(gsc, x2);
1008 if (start == NULL || end == NULL)
1011 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1013 s->y += y + (s->x - x) * m;
1016 end = gsc_getentry(gsc, INFINITY);
1017 for (; s != end; s = LIST_NEXT(s, _next)) {
1024 /* get y-projection of a service curve */
1026 sc_x2y(struct service_curve *sc, double x)
1030 if (x <= (double)sc->d)
1031 /* y belongs to the 1st segment */
1032 y = x * (double)sc->m1;
1034 /* y belongs to the 2nd segment */
1035 y = (double)sc->d * (double)sc->m1
1036 + (x - (double)sc->d) * (double)sc->m2;
1044 #define RATESTR_MAX 16
1047 rate2str(double rate)
1050 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1053 static const char unit[] = " KMG";
1055 buf = r2sbuf[idx++];
1056 if (idx == R2S_BUFS)
1059 for (i = 0; rate >= 1000 && i <= 3; i++)
1062 if ((int)(rate * 100) % 100)
1063 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1065 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1071 getifspeed(char *ifname)
1075 struct if_data ifrdat;
1077 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1079 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1080 sizeof(ifr.ifr_name))
1081 errx(1, "getifspeed: strlcpy");
1082 ifr.ifr_data = (caddr_t)&ifrdat;
1083 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1084 err(1, "SIOCGIFDATA");
1085 if (shutdown(s, SHUT_RDWR) == -1)
1089 return ((u_int32_t)ifrdat.ifi_baudrate);
1093 getifmtu(char *ifname)
1098 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1100 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1101 sizeof(ifr.ifr_name))
1102 errx(1, "getifmtu: strlcpy");
1103 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1104 err(1, "SIOCGIFMTU");
1105 if (shutdown(s, SHUT_RDWR) == -1)
1109 if (ifr.ifr_mtu > 0)
1110 return (ifr.ifr_mtu);
1112 warnx("could not get mtu for %s, assuming 1500", ifname);
1118 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1123 switch (pa->scheduler) {
1125 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1128 pa->pq_u.priq_opts = opts->data.priq_opts;
1131 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1132 if (opts->data.hfsc_opts.linkshare.used) {
1133 pa->pq_u.hfsc_opts.lssc_m1 =
1134 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1136 pa->pq_u.hfsc_opts.lssc_m2 =
1137 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1139 pa->pq_u.hfsc_opts.lssc_d =
1140 opts->data.hfsc_opts.linkshare.d;
1142 if (opts->data.hfsc_opts.realtime.used) {
1143 pa->pq_u.hfsc_opts.rtsc_m1 =
1144 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1146 pa->pq_u.hfsc_opts.rtsc_m2 =
1147 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1149 pa->pq_u.hfsc_opts.rtsc_d =
1150 opts->data.hfsc_opts.realtime.d;
1152 if (opts->data.hfsc_opts.upperlimit.used) {
1153 pa->pq_u.hfsc_opts.ulsc_m1 =
1154 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1156 pa->pq_u.hfsc_opts.ulsc_m2 =
1157 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1159 pa->pq_u.hfsc_opts.ulsc_d =
1160 opts->data.hfsc_opts.upperlimit.d;
1164 warnx("eval_queue_opts: unknown scheduler type %u",
1174 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1176 if (bw->bw_absolute > 0)
1177 return (bw->bw_absolute);
1179 if (bw->bw_percent > 0)
1180 return (ref_bw / 100 * bw->bw_percent);
1186 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1187 const struct node_hfsc_sc *sc)
1189 printf(" %s", scname);
1193 if (sc != NULL && sc->m1.bw_percent > 0)
1194 printf("%u%%", sc->m1.bw_percent);
1196 printf("%s", rate2str((double)m1));
1200 if (sc != NULL && sc->m2.bw_percent > 0)
1201 printf(" %u%%", sc->m2.bw_percent);
1203 printf(" %s", rate2str((double)m2));