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);
157 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
160 printf("altq on %s ", a->ifname);
162 switch (a->scheduler) {
164 if (!print_cbq_opts(a))
168 if (!print_priq_opts(a))
172 if (!print_hfsc_opts(a, qopts))
177 if (bw != NULL && bw->bw_percent > 0) {
178 if (bw->bw_percent < 100)
179 printf("bandwidth %u%% ", bw->bw_percent);
181 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
183 if (a->qlimit != DEFAULT_QLIMIT)
184 printf("qlimit %u ", a->qlimit);
185 printf("tbrsize %u ", a->tbrsize);
189 print_queue(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
190 int print_interface, struct node_queue_opt *qopts)
195 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
199 for (i = 0; i < level; ++i)
201 printf("%s ", a->qname);
203 printf("on %s ", a->ifname);
204 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC) {
205 if (bw != NULL && bw->bw_percent > 0) {
206 if (bw->bw_percent < 100)
207 printf("bandwidth %u%% ", bw->bw_percent);
209 printf("bandwidth %s ", rate2str((double)a->bandwidth));
211 if (a->priority != DEFAULT_PRIORITY)
212 printf("priority %u ", a->priority);
213 if (a->qlimit != DEFAULT_QLIMIT)
214 printf("qlimit %u ", a->qlimit);
215 switch (a->scheduler) {
223 print_hfsc_opts(a, qopts);
229 * eval_pfaltq computes the discipline parameters.
232 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
233 struct node_queue_opt *opts)
235 u_int rate, size, errors = 0;
237 if (bw->bw_absolute > 0)
238 pa->ifbandwidth = bw->bw_absolute;
241 if ((rate = getifspeed(pf->dev, pa->ifname)) == 0) {
243 if ((rate = getifspeed(pa->ifname)) == 0) {
245 fprintf(stderr, "interface %s does not know its bandwidth, "
246 "please specify an absolute bandwidth\n",
249 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
250 pa->ifbandwidth = rate;
252 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
254 /* if tbrsize is not specified, use heuristics */
255 if (pa->tbrsize == 0) {
256 rate = pa->ifbandwidth;
257 if (rate <= 1 * 1000 * 1000)
259 else if (rate <= 10 * 1000 * 1000)
261 else if (rate <= 200 * 1000 * 1000)
265 size = size * getifmtu(pa->ifname);
274 * check_commit_altq does consistency check for each interface
277 check_commit_altq(int dev, int opts)
279 struct pf_altq *altq;
282 /* call the discipline check for each interface. */
283 TAILQ_FOREACH(altq, &altqs, entries) {
284 if (altq->qname[0] == 0) {
285 switch (altq->scheduler) {
287 error = check_commit_cbq(dev, opts, altq);
290 error = check_commit_priq(dev, opts, altq);
293 error = check_commit_hfsc(dev, opts, altq);
304 * eval_pfqueue computes the queue parameters.
307 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
308 struct node_queue_opt *opts)
310 /* should be merged with expand_queue */
311 struct pf_altq *if_pa, *parent, *altq;
315 /* find the corresponding interface and copy fields used by queues */
316 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
317 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
320 pa->scheduler = if_pa->scheduler;
321 pa->ifbandwidth = if_pa->ifbandwidth;
323 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
324 fprintf(stderr, "queue %s already exists on interface %s\n",
325 pa->qname, pa->ifname);
328 pa->qid = qname_to_qid(pa->qname);
331 if (pa->parent[0] != 0) {
332 parent = qname_to_pfaltq(pa->parent, pa->ifname);
333 if (parent == NULL) {
334 fprintf(stderr, "parent %s not found for %s\n",
335 pa->parent, pa->qname);
338 pa->parent_qid = parent->qid;
341 pa->qlimit = DEFAULT_QLIMIT;
343 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC) {
344 pa->bandwidth = eval_bwspec(bw,
345 parent == NULL ? 0 : parent->bandwidth);
347 if (pa->bandwidth > pa->ifbandwidth) {
348 fprintf(stderr, "bandwidth for %s higher than "
349 "interface\n", pa->qname);
352 /* check the sum of the child bandwidth is under parent's */
353 if (parent != NULL) {
354 if (pa->bandwidth > parent->bandwidth) {
355 warnx("bandwidth for %s higher than parent",
360 TAILQ_FOREACH(altq, &altqs, entries) {
361 if (strncmp(altq->ifname, pa->ifname,
363 altq->qname[0] != 0 &&
364 strncmp(altq->parent, pa->parent,
366 bwsum += altq->bandwidth;
368 bwsum += pa->bandwidth;
369 if (bwsum > parent->bandwidth) {
370 warnx("the sum of the child bandwidth higher"
371 " than parent \"%s\"", parent->qname);
376 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
379 switch (pa->scheduler) {
381 error = eval_pfqueue_cbq(pf, pa);
384 error = eval_pfqueue_priq(pf, pa);
387 error = eval_pfqueue_hfsc(pf, pa);
396 * CBQ support functions
398 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
399 #define RM_NS_PER_SEC (1000000000)
402 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
404 struct cbq_opts *opts;
407 if (pa->priority >= CBQ_MAXPRI) {
408 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
412 ifmtu = getifmtu(pa->ifname);
413 opts = &pa->pq_u.cbq_opts;
415 if (opts->pktsize == 0) { /* use default */
416 opts->pktsize = ifmtu;
417 if (opts->pktsize > MCLBYTES) /* do what TCP does */
418 opts->pktsize &= ~MCLBYTES;
419 } else if (opts->pktsize > ifmtu)
420 opts->pktsize = ifmtu;
421 if (opts->maxpktsize == 0) /* use default */
422 opts->maxpktsize = ifmtu;
423 else if (opts->maxpktsize > ifmtu)
424 opts->pktsize = ifmtu;
426 if (opts->pktsize > opts->maxpktsize)
427 opts->pktsize = opts->maxpktsize;
429 if (pa->parent[0] == 0)
430 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
432 cbq_compute_idletime(pf, pa);
437 * compute ns_per_byte, maxidle, minidle, and offtime
440 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
442 struct cbq_opts *opts;
443 double maxidle_s, maxidle, minidle;
444 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
445 double z, g, f, gton, gtom;
446 u_int minburst, maxburst;
448 opts = &pa->pq_u.cbq_opts;
449 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
450 minburst = opts->minburst;
451 maxburst = opts->maxburst;
453 if (pa->bandwidth == 0)
454 f = 0.0001; /* small enough? */
456 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
458 nsPerByte = ifnsPerByte / f;
459 ptime = (double)opts->pktsize * ifnsPerByte;
460 cptime = ptime * (1.0 - f) / f;
462 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
464 * this causes integer overflow in kernel!
465 * (bandwidth < 6Kbps when max_pkt_size=1500)
467 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
468 warnx("queue bandwidth must be larger than %s",
469 rate2str(ifnsPerByte * (double)opts->maxpktsize /
470 (double)INT_MAX * (double)pa->ifbandwidth));
471 fprintf(stderr, "cbq: queue %s is too slow!\n",
473 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
476 if (maxburst == 0) { /* use default */
477 if (cptime > 10.0 * 1000000)
482 if (minburst == 0) /* use default */
484 if (minburst > maxburst)
487 z = (double)(1 << RM_FILTER_GAIN);
489 gton = pow(g, (double)maxburst);
490 gtom = pow(g, (double)(minburst-1));
491 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
492 maxidle_s = (1.0 - g);
493 if (maxidle > maxidle_s)
494 maxidle = ptime * maxidle;
496 maxidle = ptime * maxidle_s;
497 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
498 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
500 /* scale parameters */
501 maxidle = ((maxidle * 8.0) / nsPerByte) *
502 pow(2.0, (double)RM_FILTER_GAIN);
503 offtime = (offtime * 8.0) / nsPerByte *
504 pow(2.0, (double)RM_FILTER_GAIN);
505 minidle = ((minidle * 8.0) / nsPerByte) *
506 pow(2.0, (double)RM_FILTER_GAIN);
508 maxidle = maxidle / 1000.0;
509 offtime = offtime / 1000.0;
510 minidle = minidle / 1000.0;
512 opts->minburst = minburst;
513 opts->maxburst = maxburst;
514 opts->ns_per_byte = (u_int)nsPerByte;
515 opts->maxidle = (u_int)fabs(maxidle);
516 opts->minidle = (int)minidle;
517 opts->offtime = (u_int)fabs(offtime);
523 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
525 struct pf_altq *altq;
526 int root_class, default_class;
530 * check if cbq has one root queue and one default queue
533 root_class = default_class = 0;
534 TAILQ_FOREACH(altq, &altqs, entries) {
535 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
537 if (altq->qname[0] == 0) /* this is for interface */
539 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
541 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
544 if (root_class != 1) {
545 warnx("should have one root queue on %s", pa->ifname);
548 if (default_class != 1) {
549 warnx("should have one default queue on %s", pa->ifname);
556 print_cbq_opts(const struct pf_altq *a)
558 const struct cbq_opts *opts;
560 opts = &a->pq_u.cbq_opts;
563 if (opts->flags & CBQCLF_RED)
565 if (opts->flags & CBQCLF_ECN)
567 if (opts->flags & CBQCLF_RIO)
569 if (opts->flags & CBQCLF_CLEARDSCP)
570 printf(" cleardscp");
571 if (opts->flags & CBQCLF_FLOWVALVE)
572 printf(" flowvalve");
573 if (opts->flags & CBQCLF_BORROW)
575 if (opts->flags & CBQCLF_WRR)
577 if (opts->flags & CBQCLF_EFFICIENT)
578 printf(" efficient");
579 if (opts->flags & CBQCLF_ROOTCLASS)
581 if (opts->flags & CBQCLF_DEFCLASS)
591 * PRIQ support functions
594 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
596 struct pf_altq *altq;
598 if (pa->priority >= PRIQ_MAXPRI) {
599 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
602 /* the priority should be unique for the interface */
603 TAILQ_FOREACH(altq, &altqs, entries) {
604 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
605 altq->qname[0] != 0 && altq->priority == pa->priority) {
606 warnx("%s and %s have the same priority",
607 altq->qname, pa->qname);
616 check_commit_priq(int dev, int opts, struct pf_altq *pa)
618 struct pf_altq *altq;
623 * check if priq has one default class for this interface
626 TAILQ_FOREACH(altq, &altqs, entries) {
627 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
629 if (altq->qname[0] == 0) /* this is for interface */
631 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
634 if (default_class != 1) {
635 warnx("should have one default queue on %s", pa->ifname);
642 print_priq_opts(const struct pf_altq *a)
644 const struct priq_opts *opts;
646 opts = &a->pq_u.priq_opts;
650 if (opts->flags & PRCF_RED)
652 if (opts->flags & PRCF_ECN)
654 if (opts->flags & PRCF_RIO)
656 if (opts->flags & PRCF_CLEARDSCP)
657 printf(" cleardscp");
658 if (opts->flags & PRCF_DEFAULTCLASS)
668 * HFSC support functions
671 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
673 struct pf_altq *altq, *parent;
674 struct hfsc_opts *opts;
675 struct service_curve sc;
677 opts = &pa->pq_u.hfsc_opts;
679 if (pa->parent[0] == 0) {
681 opts->lssc_m1 = pa->ifbandwidth;
682 opts->lssc_m2 = pa->ifbandwidth;
690 /* if link_share is not specified, use bandwidth */
691 if (opts->lssc_m2 == 0)
692 opts->lssc_m2 = pa->bandwidth;
694 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
695 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
696 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
697 warnx("m2 is zero for %s", pa->qname);
701 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
702 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
703 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
704 warnx("m1 must be zero for convex curve: %s", pa->qname);
710 * for the real-time service curve, the sum of the service curves
711 * should not exceed 80% of the interface bandwidth. 20% is reserved
712 * not to over-commit the actual interface bandwidth.
713 * for the linkshare service curve, the sum of the child service
714 * curve should not exceed the parent service curve.
715 * for the upper-limit service curve, the assigned bandwidth should
716 * be smaller than the interface bandwidth, and the upper-limit should
717 * be larger than the real-time service curve when both are defined.
719 parent = qname_to_pfaltq(pa->parent, pa->ifname);
721 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
723 TAILQ_FOREACH(altq, &altqs, entries) {
724 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
726 if (altq->qname[0] == 0) /* this is for interface */
729 /* if the class has a real-time service curve, add it. */
730 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
731 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
732 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
733 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
734 gsc_add_sc(&rtsc, &sc);
737 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
740 /* if the class has a linkshare service curve, add it. */
741 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
742 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
743 sc.d = altq->pq_u.hfsc_opts.lssc_d;
744 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
745 gsc_add_sc(&lssc, &sc);
749 /* check the real-time service curve. reserve 20% of interface bw */
750 if (opts->rtsc_m2 != 0) {
751 /* add this queue to the sum */
752 sc.m1 = opts->rtsc_m1;
754 sc.m2 = opts->rtsc_m2;
755 gsc_add_sc(&rtsc, &sc);
756 /* compare the sum with 80% of the interface */
759 sc.m2 = pa->ifbandwidth / 100 * 80;
760 if (!is_gsc_under_sc(&rtsc, &sc)) {
761 warnx("real-time sc exceeds 80%% of the interface "
762 "bandwidth (%s)", rate2str((double)sc.m2));
767 /* check the linkshare service curve. */
768 if (opts->lssc_m2 != 0) {
769 /* add this queue to the child sum */
770 sc.m1 = opts->lssc_m1;
772 sc.m2 = opts->lssc_m2;
773 gsc_add_sc(&lssc, &sc);
774 /* compare the sum of the children with parent's sc */
775 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
776 sc.d = parent->pq_u.hfsc_opts.lssc_d;
777 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
778 if (!is_gsc_under_sc(&lssc, &sc)) {
779 warnx("linkshare sc exceeds parent's sc");
784 /* check the upper-limit service curve. */
785 if (opts->ulsc_m2 != 0) {
786 if (opts->ulsc_m1 > pa->ifbandwidth ||
787 opts->ulsc_m2 > pa->ifbandwidth) {
788 warnx("upper-limit larger than interface bandwidth");
791 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
792 warnx("upper-limit sc smaller than real-time sc");
809 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
811 struct pf_altq *altq, *def = NULL;
815 /* check if hfsc has one default queue for this interface */
817 TAILQ_FOREACH(altq, &altqs, entries) {
818 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
820 if (altq->qname[0] == 0) /* this is for interface */
822 if (altq->parent[0] == 0) /* dummy root */
824 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
829 if (default_class != 1) {
830 warnx("should have one default queue on %s", pa->ifname);
833 /* make sure the default queue is a leaf */
834 TAILQ_FOREACH(altq, &altqs, entries) {
835 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
837 if (altq->qname[0] == 0) /* this is for interface */
839 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
840 warnx("default queue is not a leaf");
848 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
850 const struct hfsc_opts *opts;
851 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
853 opts = &a->pq_u.hfsc_opts;
855 rtsc = lssc = ulsc = NULL;
857 rtsc = &qopts->data.hfsc_opts.realtime;
858 lssc = &qopts->data.hfsc_opts.linkshare;
859 ulsc = &qopts->data.hfsc_opts.upperlimit;
862 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
863 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
864 opts->lssc_d != 0))) {
866 if (opts->flags & HFCF_RED)
868 if (opts->flags & HFCF_ECN)
870 if (opts->flags & HFCF_RIO)
872 if (opts->flags & HFCF_CLEARDSCP)
873 printf(" cleardscp");
874 if (opts->flags & HFCF_DEFAULTCLASS)
876 if (opts->rtsc_m2 != 0)
877 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
878 opts->rtsc_m2, rtsc);
879 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
881 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
882 opts->lssc_m2, lssc);
883 if (opts->ulsc_m2 != 0)
884 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
885 opts->ulsc_m2, ulsc);
894 * admission control using generalized service curve
897 #define INFINITY HUGE_VAL /* positive infinity defined in <math.h> */
900 /* add a new service curve to a generalized service curve */
902 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
907 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
908 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
912 * check whether all points of a generalized service curve have
913 * their y-coordinates no larger than a given two-piece linear
917 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
919 struct segment *s, *last, *end;
922 if (is_sc_null(sc)) {
925 LIST_FOREACH(s, gsc, _next) {
932 * gsc has a dummy entry at the end with x = INFINITY.
933 * loop through up to this dummy entry.
935 end = gsc_getentry(gsc, INFINITY);
939 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
940 if (s->y > sc_x2y(sc, s->x))
944 /* last now holds the real last segment */
947 if (last->m > sc->m2)
949 if (last->x < sc->d && last->m > sc->m1) {
950 y = last->y + (sc->d - last->x) * last->m;
951 if (y > sc_x2y(sc, sc->d))
958 gsc_destroy(struct gen_sc *gsc)
962 while ((s = LIST_FIRST(gsc)) != NULL) {
963 LIST_REMOVE(s, _next);
969 * return a segment entry starting at x.
970 * if gsc has no entry starting at x, a new entry is created at x.
972 static struct segment *
973 gsc_getentry(struct gen_sc *gsc, double x)
975 struct segment *new, *prev, *s;
978 LIST_FOREACH(s, gsc, _next) {
980 return (s); /* matching entry found */
987 /* we have to create a new entry */
988 if ((new = calloc(1, sizeof(struct segment))) == NULL)
992 if (x == INFINITY || s == NULL)
994 else if (s->x == INFINITY)
999 /* insert the new entry at the head of the list */
1002 LIST_INSERT_HEAD(gsc, new, _next);
1005 * the start point intersects with the segment pointed by
1006 * prev. divide prev into 2 segments
1008 if (x == INFINITY) {
1015 prev->d = x - prev->x;
1016 new->y = prev->d * prev->m + prev->y;
1019 LIST_INSERT_AFTER(prev, new, _next);
1024 /* add a segment to a generalized service curve */
1026 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1028 struct segment *start, *end, *s;
1035 start = gsc_getentry(gsc, x);
1036 end = gsc_getentry(gsc, x2);
1037 if (start == NULL || end == NULL)
1040 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1042 s->y += y + (s->x - x) * m;
1045 end = gsc_getentry(gsc, INFINITY);
1046 for (; s != end; s = LIST_NEXT(s, _next)) {
1053 /* get y-projection of a service curve */
1055 sc_x2y(struct service_curve *sc, double x)
1059 if (x <= (double)sc->d)
1060 /* y belongs to the 1st segment */
1061 y = x * (double)sc->m1;
1063 /* y belongs to the 2nd segment */
1064 y = (double)sc->d * (double)sc->m1
1065 + (x - (double)sc->d) * (double)sc->m2;
1073 #define RATESTR_MAX 16
1076 rate2str(double rate)
1079 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1082 static const char unit[] = " KMG";
1084 buf = r2sbuf[idx++];
1085 if (idx == R2S_BUFS)
1088 for (i = 0; rate >= 1000 && i <= 3; i++)
1091 if ((int)(rate * 100) % 100)
1092 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1094 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1102 * FreeBSD does not have SIOCGIFDATA.
1103 * To emulate this, DIOCGIFSPEED ioctl added to pf.
1106 getifspeed(int pfdev, char *ifname)
1108 struct pf_ifspeed io;
1110 bzero(&io, sizeof io);
1111 if (strlcpy(io.ifname, ifname, IFNAMSIZ) >=
1113 errx(1, "getifspeed: strlcpy");
1114 if (ioctl(pfdev, DIOCGIFSPEED, &io) == -1)
1115 err(1, "DIOCGIFSPEED");
1116 return ((u_int32_t)io.baudrate);
1120 getifspeed(char *ifname)
1124 struct if_data ifrdat;
1126 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1128 bzero(&ifr, sizeof(ifr));
1129 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1130 sizeof(ifr.ifr_name))
1131 errx(1, "getifspeed: strlcpy");
1132 ifr.ifr_data = (caddr_t)&ifrdat;
1133 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1134 err(1, "SIOCGIFDATA");
1135 if (shutdown(s, SHUT_RDWR) == -1)
1139 return ((u_int32_t)ifrdat.ifi_baudrate);
1144 getifmtu(char *ifname)
1149 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1151 bzero(&ifr, sizeof(ifr));
1152 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1153 sizeof(ifr.ifr_name))
1154 errx(1, "getifmtu: strlcpy");
1155 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1159 err(1, "SIOCGIFMTU");
1161 if (shutdown(s, SHUT_RDWR) == -1)
1165 if (ifr.ifr_mtu > 0)
1166 return (ifr.ifr_mtu);
1168 warnx("could not get mtu for %s, assuming 1500", ifname);
1174 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1179 switch (pa->scheduler) {
1181 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1184 pa->pq_u.priq_opts = opts->data.priq_opts;
1187 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1188 if (opts->data.hfsc_opts.linkshare.used) {
1189 pa->pq_u.hfsc_opts.lssc_m1 =
1190 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1192 pa->pq_u.hfsc_opts.lssc_m2 =
1193 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1195 pa->pq_u.hfsc_opts.lssc_d =
1196 opts->data.hfsc_opts.linkshare.d;
1198 if (opts->data.hfsc_opts.realtime.used) {
1199 pa->pq_u.hfsc_opts.rtsc_m1 =
1200 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1202 pa->pq_u.hfsc_opts.rtsc_m2 =
1203 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1205 pa->pq_u.hfsc_opts.rtsc_d =
1206 opts->data.hfsc_opts.realtime.d;
1208 if (opts->data.hfsc_opts.upperlimit.used) {
1209 pa->pq_u.hfsc_opts.ulsc_m1 =
1210 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1212 pa->pq_u.hfsc_opts.ulsc_m2 =
1213 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1215 pa->pq_u.hfsc_opts.ulsc_d =
1216 opts->data.hfsc_opts.upperlimit.d;
1220 warnx("eval_queue_opts: unknown scheduler type %u",
1230 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1232 if (bw->bw_absolute > 0)
1233 return (bw->bw_absolute);
1235 if (bw->bw_percent > 0)
1236 return (ref_bw / 100 * bw->bw_percent);
1242 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1243 const struct node_hfsc_sc *sc)
1245 printf(" %s", scname);
1249 if (sc != NULL && sc->m1.bw_percent > 0)
1250 printf("%u%%", sc->m1.bw_percent);
1252 printf("%s", rate2str((double)m1));
1256 if (sc != NULL && sc->m2.bw_percent > 0)
1257 printf(" %u%%", sc->m2.bw_percent);
1259 printf(" %s", rate2str((double)m2));