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>
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 int level,
149 struct node_queue_bw *bw, struct node_queue_opt *qopts)
151 if (a->qname[0] != 0) {
152 print_queue(a, level, bw, 1, qopts);
157 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
161 printf("altq on %s ", a->ifname);
163 switch (a->scheduler) {
165 if (!print_cbq_opts(a))
169 if (!print_priq_opts(a))
173 if (!print_hfsc_opts(a, qopts))
178 if (bw != NULL && bw->bw_percent > 0) {
179 if (bw->bw_percent < 100)
180 printf("bandwidth %u%% ", bw->bw_percent);
182 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
184 if (a->qlimit != DEFAULT_QLIMIT)
185 printf("qlimit %u ", a->qlimit);
186 printf("tbrsize %u ", a->tbrsize);
190 print_queue(const struct pf_altq *a, unsigned int level,
191 struct node_queue_bw *bw, int print_interface,
192 struct node_queue_opt *qopts)
197 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
201 for (i = 0; i < level; ++i)
203 printf("%s ", a->qname);
205 printf("on %s ", a->ifname);
206 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC) {
207 if (bw != NULL && bw->bw_percent > 0) {
208 if (bw->bw_percent < 100)
209 printf("bandwidth %u%% ", bw->bw_percent);
211 printf("bandwidth %s ", rate2str((double)a->bandwidth));
213 if (a->priority != DEFAULT_PRIORITY)
214 printf("priority %u ", a->priority);
215 if (a->qlimit != DEFAULT_QLIMIT)
216 printf("qlimit %u ", a->qlimit);
217 switch (a->scheduler) {
225 print_hfsc_opts(a, qopts);
231 * eval_pfaltq computes the discipline parameters.
234 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
235 struct node_queue_opt *opts)
237 u_int rate, size, errors = 0;
239 if (bw->bw_absolute > 0)
240 pa->ifbandwidth = bw->bw_absolute;
243 if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) {
245 if ((rate = getifspeed(pa->ifname)) == 0) {
247 fprintf(stderr, "interface %s does not know its bandwidth, "
248 "please specify an absolute bandwidth\n",
251 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
252 pa->ifbandwidth = rate;
254 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
256 /* if tbrsize is not specified, use heuristics */
257 if (pa->tbrsize == 0) {
258 rate = pa->ifbandwidth;
259 if (rate <= 1 * 1000 * 1000)
261 else if (rate <= 10 * 1000 * 1000)
263 else if (rate <= 200 * 1000 * 1000)
267 size = size * getifmtu(pa->ifname);
276 * check_commit_altq does consistency check for each interface
279 check_commit_altq(int dev, int opts)
281 struct pf_altq *altq;
284 /* call the discipline check for each interface. */
285 TAILQ_FOREACH(altq, &altqs, entries) {
286 if (altq->qname[0] == 0) {
287 switch (altq->scheduler) {
289 error = check_commit_cbq(dev, opts, altq);
292 error = check_commit_priq(dev, opts, altq);
295 error = check_commit_hfsc(dev, opts, altq);
306 * eval_pfqueue computes the queue parameters.
309 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
310 struct node_queue_opt *opts)
312 /* should be merged with expand_queue */
313 struct pf_altq *if_pa, *parent, *altq;
317 /* find the corresponding interface and copy fields used by queues */
318 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
319 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
322 pa->scheduler = if_pa->scheduler;
323 pa->ifbandwidth = if_pa->ifbandwidth;
325 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
326 fprintf(stderr, "queue %s already exists on interface %s\n",
327 pa->qname, pa->ifname);
330 pa->qid = qname_to_qid(pa->qname);
333 if (pa->parent[0] != 0) {
334 parent = qname_to_pfaltq(pa->parent, pa->ifname);
335 if (parent == NULL) {
336 fprintf(stderr, "parent %s not found for %s\n",
337 pa->parent, pa->qname);
340 pa->parent_qid = parent->qid;
343 pa->qlimit = DEFAULT_QLIMIT;
345 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC) {
346 pa->bandwidth = eval_bwspec(bw,
347 parent == NULL ? 0 : parent->bandwidth);
349 if (pa->bandwidth > pa->ifbandwidth) {
350 fprintf(stderr, "bandwidth for %s higher than "
351 "interface\n", pa->qname);
354 /* check the sum of the child bandwidth is under parent's */
355 if (parent != NULL) {
356 if (pa->bandwidth > parent->bandwidth) {
357 warnx("bandwidth for %s higher than parent",
362 TAILQ_FOREACH(altq, &altqs, entries) {
363 if (strncmp(altq->ifname, pa->ifname,
365 altq->qname[0] != 0 &&
366 strncmp(altq->parent, pa->parent,
368 bwsum += altq->bandwidth;
370 bwsum += pa->bandwidth;
371 if (bwsum > parent->bandwidth) {
372 warnx("the sum of the child bandwidth higher"
373 " than parent \"%s\"", parent->qname);
378 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
381 switch (pa->scheduler) {
383 error = eval_pfqueue_cbq(pf, pa);
386 error = eval_pfqueue_priq(pf, pa);
389 error = eval_pfqueue_hfsc(pf, pa);
398 * CBQ support functions
400 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
401 #define RM_NS_PER_SEC (1000000000)
404 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
406 struct cbq_opts *opts;
409 if (pa->priority >= CBQ_MAXPRI) {
410 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
414 ifmtu = getifmtu(pa->ifname);
415 opts = &pa->pq_u.cbq_opts;
417 if (opts->pktsize == 0) { /* use default */
418 opts->pktsize = ifmtu;
419 if (opts->pktsize > MCLBYTES) /* do what TCP does */
420 opts->pktsize &= ~MCLBYTES;
421 } else if (opts->pktsize > ifmtu)
422 opts->pktsize = ifmtu;
423 if (opts->maxpktsize == 0) /* use default */
424 opts->maxpktsize = ifmtu;
425 else if (opts->maxpktsize > ifmtu)
426 opts->pktsize = ifmtu;
428 if (opts->pktsize > opts->maxpktsize)
429 opts->pktsize = opts->maxpktsize;
431 if (pa->parent[0] == 0)
432 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
434 cbq_compute_idletime(pf, pa);
439 * compute ns_per_byte, maxidle, minidle, and offtime
442 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
444 struct cbq_opts *opts;
445 double maxidle_s, maxidle, minidle;
446 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
447 double z, g, f, gton, gtom;
448 u_int minburst, maxburst;
450 opts = &pa->pq_u.cbq_opts;
451 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
452 minburst = opts->minburst;
453 maxburst = opts->maxburst;
455 if (pa->bandwidth == 0)
456 f = 0.0001; /* small enough? */
458 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
460 nsPerByte = ifnsPerByte / f;
461 ptime = (double)opts->pktsize * ifnsPerByte;
462 cptime = ptime * (1.0 - f) / f;
464 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
466 * this causes integer overflow in kernel!
467 * (bandwidth < 6Kbps when max_pkt_size=1500)
469 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
470 warnx("queue bandwidth must be larger than %s",
471 rate2str(ifnsPerByte * (double)opts->maxpktsize /
472 (double)INT_MAX * (double)pa->ifbandwidth));
473 fprintf(stderr, "cbq: queue %s is too slow!\n",
475 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
478 if (maxburst == 0) { /* use default */
479 if (cptime > 10.0 * 1000000)
484 if (minburst == 0) /* use default */
486 if (minburst > maxburst)
489 z = (double)(1 << RM_FILTER_GAIN);
491 gton = pow(g, (double)maxburst);
492 gtom = pow(g, (double)(minburst-1));
493 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
494 maxidle_s = (1.0 - g);
495 if (maxidle > maxidle_s)
496 maxidle = ptime * maxidle;
498 maxidle = ptime * maxidle_s;
499 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
500 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
502 /* scale parameters */
503 maxidle = ((maxidle * 8.0) / nsPerByte) *
504 pow(2.0, (double)RM_FILTER_GAIN);
505 offtime = (offtime * 8.0) / nsPerByte *
506 pow(2.0, (double)RM_FILTER_GAIN);
507 minidle = ((minidle * 8.0) / nsPerByte) *
508 pow(2.0, (double)RM_FILTER_GAIN);
510 maxidle = maxidle / 1000.0;
511 offtime = offtime / 1000.0;
512 minidle = minidle / 1000.0;
514 opts->minburst = minburst;
515 opts->maxburst = maxburst;
516 opts->ns_per_byte = (u_int)nsPerByte;
517 opts->maxidle = (u_int)fabs(maxidle);
518 opts->minidle = (int)minidle;
519 opts->offtime = (u_int)fabs(offtime);
525 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
527 struct pf_altq *altq;
528 int root_class, default_class;
532 * check if cbq has one root queue and one default queue
535 root_class = default_class = 0;
536 TAILQ_FOREACH(altq, &altqs, entries) {
537 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
539 if (altq->qname[0] == 0) /* this is for interface */
541 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
543 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
546 if (root_class != 1) {
547 warnx("should have one root queue on %s", pa->ifname);
550 if (default_class != 1) {
551 warnx("should have one default queue on %s", pa->ifname);
558 print_cbq_opts(const struct pf_altq *a)
560 const struct cbq_opts *opts;
562 opts = &a->pq_u.cbq_opts;
565 if (opts->flags & CBQCLF_RED)
567 if (opts->flags & CBQCLF_ECN)
569 if (opts->flags & CBQCLF_RIO)
571 if (opts->flags & CBQCLF_CLEARDSCP)
572 printf(" cleardscp");
573 if (opts->flags & CBQCLF_FLOWVALVE)
574 printf(" flowvalve");
575 if (opts->flags & CBQCLF_BORROW)
577 if (opts->flags & CBQCLF_WRR)
579 if (opts->flags & CBQCLF_EFFICIENT)
580 printf(" efficient");
581 if (opts->flags & CBQCLF_ROOTCLASS)
583 if (opts->flags & CBQCLF_DEFCLASS)
593 * PRIQ support functions
596 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
598 struct pf_altq *altq;
600 if (pa->priority >= PRIQ_MAXPRI) {
601 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
604 /* the priority should be unique for the interface */
605 TAILQ_FOREACH(altq, &altqs, entries) {
606 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
607 altq->qname[0] != 0 && altq->priority == pa->priority) {
608 warnx("%s and %s have the same priority",
609 altq->qname, pa->qname);
618 check_commit_priq(int dev, int opts, struct pf_altq *pa)
620 struct pf_altq *altq;
625 * check if priq has one default class for this interface
628 TAILQ_FOREACH(altq, &altqs, entries) {
629 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
631 if (altq->qname[0] == 0) /* this is for interface */
633 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
636 if (default_class != 1) {
637 warnx("should have one default queue on %s", pa->ifname);
644 print_priq_opts(const struct pf_altq *a)
646 const struct priq_opts *opts;
648 opts = &a->pq_u.priq_opts;
652 if (opts->flags & PRCF_RED)
654 if (opts->flags & PRCF_ECN)
656 if (opts->flags & PRCF_RIO)
658 if (opts->flags & PRCF_CLEARDSCP)
659 printf(" cleardscp");
660 if (opts->flags & PRCF_DEFAULTCLASS)
670 * HFSC support functions
673 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
675 struct pf_altq *altq, *parent;
676 struct hfsc_opts *opts;
677 struct service_curve sc;
679 opts = &pa->pq_u.hfsc_opts;
681 if (pa->parent[0] == 0) {
683 opts->lssc_m1 = pa->ifbandwidth;
684 opts->lssc_m2 = pa->ifbandwidth;
692 /* if link_share is not specified, use bandwidth */
693 if (opts->lssc_m2 == 0)
694 opts->lssc_m2 = pa->bandwidth;
696 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
697 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
698 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
699 warnx("m2 is zero for %s", pa->qname);
703 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
704 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
705 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
706 warnx("m1 must be zero for convex curve: %s", pa->qname);
712 * for the real-time service curve, the sum of the service curves
713 * should not exceed 80% of the interface bandwidth. 20% is reserved
714 * not to over-commit the actual interface bandwidth.
715 * for the linkshare service curve, the sum of the child service
716 * curve should not exceed the parent service curve.
717 * for the upper-limit service curve, the assigned bandwidth should
718 * be smaller than the interface bandwidth, and the upper-limit should
719 * be larger than the real-time service curve when both are defined.
721 parent = qname_to_pfaltq(pa->parent, pa->ifname);
723 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
725 TAILQ_FOREACH(altq, &altqs, entries) {
726 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
728 if (altq->qname[0] == 0) /* this is for interface */
731 /* if the class has a real-time service curve, add it. */
732 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
733 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
734 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
735 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
736 gsc_add_sc(&rtsc, &sc);
739 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
742 /* if the class has a linkshare service curve, add it. */
743 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
744 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
745 sc.d = altq->pq_u.hfsc_opts.lssc_d;
746 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
747 gsc_add_sc(&lssc, &sc);
751 /* check the real-time service curve. reserve 20% of interface bw */
752 if (opts->rtsc_m2 != 0) {
753 /* add this queue to the sum */
754 sc.m1 = opts->rtsc_m1;
756 sc.m2 = opts->rtsc_m2;
757 gsc_add_sc(&rtsc, &sc);
758 /* compare the sum with 80% of the interface */
761 sc.m2 = pa->ifbandwidth / 100 * 80;
762 if (!is_gsc_under_sc(&rtsc, &sc)) {
763 warnx("real-time sc exceeds 80%% of the interface "
764 "bandwidth (%s)", rate2str((double)sc.m2));
769 /* check the linkshare service curve. */
770 if (opts->lssc_m2 != 0) {
771 /* add this queue to the child sum */
772 sc.m1 = opts->lssc_m1;
774 sc.m2 = opts->lssc_m2;
775 gsc_add_sc(&lssc, &sc);
776 /* compare the sum of the children with parent's sc */
777 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
778 sc.d = parent->pq_u.hfsc_opts.lssc_d;
779 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
780 if (!is_gsc_under_sc(&lssc, &sc)) {
781 warnx("linkshare sc exceeds parent's sc");
786 /* check the upper-limit service curve. */
787 if (opts->ulsc_m2 != 0) {
788 if (opts->ulsc_m1 > pa->ifbandwidth ||
789 opts->ulsc_m2 > pa->ifbandwidth) {
790 warnx("upper-limit larger than interface bandwidth");
793 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
794 warnx("upper-limit sc smaller than real-time sc");
811 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
813 struct pf_altq *altq, *def = NULL;
817 /* check if hfsc has one default queue for this interface */
819 TAILQ_FOREACH(altq, &altqs, entries) {
820 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
822 if (altq->qname[0] == 0) /* this is for interface */
824 if (altq->parent[0] == 0) /* dummy root */
826 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
831 if (default_class != 1) {
832 warnx("should have one default queue on %s", pa->ifname);
835 /* make sure the default queue is a leaf */
836 TAILQ_FOREACH(altq, &altqs, entries) {
837 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
839 if (altq->qname[0] == 0) /* this is for interface */
841 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
842 warnx("default queue is not a leaf");
850 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
852 const struct hfsc_opts *opts;
853 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
855 opts = &a->pq_u.hfsc_opts;
857 rtsc = lssc = ulsc = NULL;
859 rtsc = &qopts->data.hfsc_opts.realtime;
860 lssc = &qopts->data.hfsc_opts.linkshare;
861 ulsc = &qopts->data.hfsc_opts.upperlimit;
864 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
865 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
866 opts->lssc_d != 0))) {
868 if (opts->flags & HFCF_RED)
870 if (opts->flags & HFCF_ECN)
872 if (opts->flags & HFCF_RIO)
874 if (opts->flags & HFCF_CLEARDSCP)
875 printf(" cleardscp");
876 if (opts->flags & HFCF_DEFAULTCLASS)
878 if (opts->rtsc_m2 != 0)
879 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
880 opts->rtsc_m2, rtsc);
881 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
883 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
884 opts->lssc_m2, lssc);
885 if (opts->ulsc_m2 != 0)
886 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
887 opts->ulsc_m2, ulsc);
896 * admission control using generalized service curve
899 /* add a new service curve to a generalized service curve */
901 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
906 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
907 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
911 * check whether all points of a generalized service curve have
912 * their y-coordinates no larger than a given two-piece linear
916 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
918 struct segment *s, *last, *end;
921 if (is_sc_null(sc)) {
924 LIST_FOREACH(s, gsc, _next) {
931 * gsc has a dummy entry at the end with x = INFINITY.
932 * loop through up to this dummy entry.
934 end = gsc_getentry(gsc, INFINITY);
938 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
939 if (s->y > sc_x2y(sc, s->x))
943 /* last now holds the real last segment */
946 if (last->m > sc->m2)
948 if (last->x < sc->d && last->m > sc->m1) {
949 y = last->y + (sc->d - last->x) * last->m;
950 if (y > sc_x2y(sc, sc->d))
957 gsc_destroy(struct gen_sc *gsc)
961 while ((s = LIST_FIRST(gsc)) != NULL) {
962 LIST_REMOVE(s, _next);
968 * return a segment entry starting at x.
969 * if gsc has no entry starting at x, a new entry is created at x.
971 static struct segment *
972 gsc_getentry(struct gen_sc *gsc, double x)
974 struct segment *new, *prev, *s;
977 LIST_FOREACH(s, gsc, _next) {
979 return (s); /* matching entry found */
986 /* we have to create a new entry */
987 if ((new = calloc(1, sizeof(struct segment))) == NULL)
991 if (x == INFINITY || s == NULL)
993 else if (s->x == INFINITY)
998 /* insert the new entry at the head of the list */
1001 LIST_INSERT_HEAD(gsc, new, _next);
1004 * the start point intersects with the segment pointed by
1005 * prev. divide prev into 2 segments
1007 if (x == INFINITY) {
1014 prev->d = x - prev->x;
1015 new->y = prev->d * prev->m + prev->y;
1018 LIST_INSERT_AFTER(prev, new, _next);
1023 /* add a segment to a generalized service curve */
1025 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1027 struct segment *start, *end, *s;
1034 start = gsc_getentry(gsc, x);
1035 end = gsc_getentry(gsc, x2);
1036 if (start == NULL || end == NULL)
1039 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1041 s->y += y + (s->x - x) * m;
1044 end = gsc_getentry(gsc, INFINITY);
1045 for (; s != end; s = LIST_NEXT(s, _next)) {
1052 /* get y-projection of a service curve */
1054 sc_x2y(struct service_curve *sc, double x)
1058 if (x <= (double)sc->d)
1059 /* y belongs to the 1st segment */
1060 y = x * (double)sc->m1;
1062 /* y belongs to the 2nd segment */
1063 y = (double)sc->d * (double)sc->m1
1064 + (x - (double)sc->d) * (double)sc->m2;
1072 #define RATESTR_MAX 16
1075 rate2str(double rate)
1078 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1081 static const char unit[] = " KMG";
1083 buf = r2sbuf[idx++];
1084 if (idx == R2S_BUFS)
1087 for (i = 0; rate >= 1000 && i <= 3; i++)
1090 if ((int)(rate * 100) % 100)
1091 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1093 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1101 * FreeBSD does not have SIOCGIFDATA.
1102 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1105 getifspeed(int pfdev, char *ifname)
1107 struct pf_ifspeed io;
1109 bzero(&io, sizeof io);
1110 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1112 errx(1, "getifspeed: strlcpy");
1113 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1114 err(1, "DIOCGIFSPEED");
1115 return ((u_int32_t)io.baudrate);
1119 getifspeed(char *ifname)
1123 struct if_data ifrdat;
1125 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1127 bzero(&ifr, sizeof(ifr));
1128 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1129 sizeof(ifr.ifr_name))
1130 errx(1, "getifspeed: strlcpy");
1131 ifr.ifr_data = (caddr_t)&ifrdat;
1132 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1133 err(1, "SIOCGIFDATA");
1136 return ((u_int32_t)ifrdat.ifi_baudrate);
1141 getifmtu(char *ifname)
1146 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1148 bzero(&ifr, sizeof(ifr));
1149 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1150 sizeof(ifr.ifr_name))
1151 errx(1, "getifmtu: strlcpy");
1152 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1156 err(1, "SIOCGIFMTU");
1160 if (ifr.ifr_mtu > 0)
1161 return (ifr.ifr_mtu);
1163 warnx("could not get mtu for %s, assuming 1500", ifname);
1169 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1174 switch (pa->scheduler) {
1176 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1179 pa->pq_u.priq_opts = opts->data.priq_opts;
1182 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1183 if (opts->data.hfsc_opts.linkshare.used) {
1184 pa->pq_u.hfsc_opts.lssc_m1 =
1185 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1187 pa->pq_u.hfsc_opts.lssc_m2 =
1188 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1190 pa->pq_u.hfsc_opts.lssc_d =
1191 opts->data.hfsc_opts.linkshare.d;
1193 if (opts->data.hfsc_opts.realtime.used) {
1194 pa->pq_u.hfsc_opts.rtsc_m1 =
1195 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1197 pa->pq_u.hfsc_opts.rtsc_m2 =
1198 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1200 pa->pq_u.hfsc_opts.rtsc_d =
1201 opts->data.hfsc_opts.realtime.d;
1203 if (opts->data.hfsc_opts.upperlimit.used) {
1204 pa->pq_u.hfsc_opts.ulsc_m1 =
1205 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1207 pa->pq_u.hfsc_opts.ulsc_m2 =
1208 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1210 pa->pq_u.hfsc_opts.ulsc_d =
1211 opts->data.hfsc_opts.upperlimit.d;
1215 warnx("eval_queue_opts: unknown scheduler type %u",
1225 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1227 if (bw->bw_absolute > 0)
1228 return (bw->bw_absolute);
1230 if (bw->bw_percent > 0)
1231 return (ref_bw / 100 * bw->bw_percent);
1237 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1238 const struct node_hfsc_sc *sc)
1240 printf(" %s", scname);
1244 if (sc != NULL && sc->m1.bw_percent > 0)
1245 printf("%u%%", sc->m1.bw_percent);
1247 printf("%s", rate2str((double)m1));
1251 if (sc != NULL && sc->m2.bw_percent > 0)
1252 printf(" %u%%", sc->m2.bw_percent);
1254 printf(" %s", rate2str((double)m2));