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 #define PFIOC_USE_LATEST
26 #include <sys/types.h>
27 #include <sys/bitset.h>
28 #include <sys/ioctl.h>
29 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <net/pfvar.h>
46 #include <net/altq/altq.h>
47 #include <net/altq/altq_cbq.h>
48 #include <net/altq/altq_codel.h>
49 #include <net/altq/altq_priq.h>
50 #include <net/altq/altq_hfsc.h>
51 #include <net/altq/altq_fairq.h>
53 #include "pfctl_parser.h"
56 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
58 static STAILQ_HEAD(interfaces, pfctl_altq) interfaces = STAILQ_HEAD_INITIALIZER(interfaces);
59 static struct hsearch_data queue_map;
60 static struct hsearch_data if_map;
61 static struct hsearch_data qid_map;
63 static struct pfctl_altq *pfaltq_lookup(char *ifname);
64 static struct pfctl_altq *qname_to_pfaltq(const char *, const char *);
65 static u_int32_t qname_to_qid(char *);
67 static int eval_pfqueue_cbq(struct pfctl *, struct pf_altq *,
69 static int cbq_compute_idletime(struct pfctl *, struct pf_altq *);
70 static int check_commit_cbq(int, int, struct pfctl_altq *);
71 static int print_cbq_opts(const struct pf_altq *);
73 static int print_codel_opts(const struct pf_altq *,
74 const struct node_queue_opt *);
76 static int eval_pfqueue_priq(struct pfctl *, struct pf_altq *,
78 static int check_commit_priq(int, int, struct pfctl_altq *);
79 static int print_priq_opts(const struct pf_altq *);
81 static int eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *,
82 struct pfctl_altq *, struct pfctl_altq *);
83 static int check_commit_hfsc(int, int, struct pfctl_altq *);
84 static int print_hfsc_opts(const struct pf_altq *,
85 const struct node_queue_opt *);
87 static int eval_pfqueue_fairq(struct pfctl *, struct pf_altq *,
88 struct pfctl_altq *, struct pfctl_altq *);
89 static int print_fairq_opts(const struct pf_altq *,
90 const struct node_queue_opt *);
91 static int check_commit_fairq(int, int, struct pfctl_altq *);
93 static void gsc_add_sc(struct gen_sc *, struct service_curve *);
94 static int is_gsc_under_sc(struct gen_sc *,
95 struct service_curve *);
96 static struct segment *gsc_getentry(struct gen_sc *, double);
97 static int gsc_add_seg(struct gen_sc *, double, double, double,
99 static double sc_x2y(struct service_curve *, double);
101 u_int32_t getifspeed(char *);
102 u_long getifmtu(char *);
103 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
105 u_int64_t eval_bwspec(struct node_queue_bw *, u_int64_t);
106 void print_hfsc_sc(const char *, u_int, u_int, u_int,
107 const struct node_hfsc_sc *);
108 void print_fairq_sc(const char *, u_int, u_int, u_int,
109 const struct node_fairq_sc *);
111 static __attribute__((constructor)) void
112 pfctl_altq_init(void)
115 * As hdestroy() will never be called on these tables, it will be
116 * safe to use references into the stored data as keys.
118 if (hcreate_r(0, &queue_map) == 0)
119 err(1, "Failed to create altq queue map");
120 if (hcreate_r(0, &if_map) == 0)
121 err(1, "Failed to create altq interface map");
122 if (hcreate_r(0, &qid_map) == 0)
123 err(1, "Failed to create altq queue id map");
127 pfaltq_store(struct pf_altq *a)
129 struct pfctl_altq *altq;
134 if ((altq = malloc(sizeof(*altq))) == NULL)
135 err(1, "queue malloc");
136 memcpy(&altq->pa, a, sizeof(struct pf_altq));
137 memset(&altq->meta, 0, sizeof(altq->meta));
139 if (a->qname[0] == 0) {
140 item.key = altq->pa.ifname;
142 if (hsearch_r(item, ENTER, &ret_item, &if_map) == 0)
143 err(1, "interface map insert");
144 STAILQ_INSERT_TAIL(&interfaces, altq, meta.link);
146 key_size = sizeof(a->ifname) + sizeof(a->qname);
147 if ((item.key = malloc(key_size)) == NULL)
148 err(1, "queue map key malloc");
149 snprintf(item.key, key_size, "%s:%s", a->ifname, a->qname);
151 if (hsearch_r(item, ENTER, &ret_item, &queue_map) == 0)
152 err(1, "queue map insert");
154 item.key = altq->pa.qname;
155 item.data = &altq->pa.qid;
156 if (hsearch_r(item, ENTER, &ret_item, &qid_map) == 0)
157 err(1, "qid map insert");
161 static struct pfctl_altq *
162 pfaltq_lookup(char *ifname)
168 if (hsearch_r(item, FIND, &ret_item, &if_map) == 0)
171 return (ret_item->data);
174 static struct pfctl_altq *
175 qname_to_pfaltq(const char *qname, const char *ifname)
179 char key[IFNAMSIZ + PF_QNAME_SIZE];
182 snprintf(item.key, sizeof(key), "%s:%s", ifname, qname);
183 if (hsearch_r(item, FIND, &ret_item, &queue_map) == 0)
186 return (ret_item->data);
190 qname_to_qid(char *qname)
197 * We guarantee that same named queues on different interfaces
201 if (hsearch_r(item, FIND, &ret_item, &qid_map) == 0)
204 qid = *(uint32_t *)ret_item->data;
209 print_altq(const struct pf_altq *a, unsigned int level,
210 struct node_queue_bw *bw, struct node_queue_opt *qopts)
212 if (a->qname[0] != 0) {
213 print_queue(a, level, bw, 1, qopts);
218 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
222 printf("altq on %s ", a->ifname);
224 switch (a->scheduler) {
226 if (!print_cbq_opts(a))
230 if (!print_priq_opts(a))
234 if (!print_hfsc_opts(a, qopts))
238 if (!print_fairq_opts(a, qopts))
242 if (!print_codel_opts(a, qopts))
247 if (bw != NULL && bw->bw_percent > 0) {
248 if (bw->bw_percent < 100)
249 printf("bandwidth %u%% ", bw->bw_percent);
251 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
253 if (a->qlimit != DEFAULT_QLIMIT)
254 printf("qlimit %u ", a->qlimit);
255 printf("tbrsize %u ", a->tbrsize);
259 print_queue(const struct pf_altq *a, unsigned int level,
260 struct node_queue_bw *bw, int print_interface,
261 struct node_queue_opt *qopts)
266 if (a->local_flags & PFALTQ_FLAG_IF_REMOVED)
270 for (i = 0; i < level; ++i)
272 printf("%s ", a->qname);
274 printf("on %s ", a->ifname);
275 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC ||
276 a->scheduler == ALTQT_FAIRQ) {
277 if (bw != NULL && bw->bw_percent > 0) {
278 if (bw->bw_percent < 100)
279 printf("bandwidth %u%% ", bw->bw_percent);
281 printf("bandwidth %s ", rate2str((double)a->bandwidth));
283 if (a->priority != DEFAULT_PRIORITY)
284 printf("priority %u ", a->priority);
285 if (a->qlimit != DEFAULT_QLIMIT)
286 printf("qlimit %u ", a->qlimit);
287 switch (a->scheduler) {
295 print_hfsc_opts(a, qopts);
298 print_fairq_opts(a, qopts);
304 * eval_pfaltq computes the discipline parameters.
307 eval_pfaltq(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
308 struct node_queue_opt *opts)
311 u_int size, errors = 0;
313 if (bw->bw_absolute > 0)
314 pa->ifbandwidth = bw->bw_absolute;
316 if ((rate = getifspeed(pa->ifname)) == 0) {
317 fprintf(stderr, "interface %s does not know its bandwidth, "
318 "please specify an absolute bandwidth\n",
321 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
322 pa->ifbandwidth = rate;
325 * Limit bandwidth to UINT_MAX for schedulers that aren't 64-bit ready.
327 if ((pa->scheduler != ALTQT_HFSC) && (pa->ifbandwidth > UINT_MAX)) {
328 pa->ifbandwidth = UINT_MAX;
329 warnx("interface %s bandwidth limited to %" PRIu64 " bps "
330 "because selected scheduler is 32-bit limited\n", pa->ifname,
333 errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
335 /* if tbrsize is not specified, use heuristics */
336 if (pa->tbrsize == 0) {
337 rate = pa->ifbandwidth;
338 if (rate <= 1 * 1000 * 1000)
340 else if (rate <= 10 * 1000 * 1000)
342 else if (rate <= 200 * 1000 * 1000)
344 else if (rate <= 2500 * 1000 * 1000ULL)
348 size = size * getifmtu(pa->ifname);
355 * check_commit_altq does consistency check for each interface
358 check_commit_altq(int dev, int opts)
360 struct pfctl_altq *if_ppa;
363 /* call the discipline check for each interface. */
364 STAILQ_FOREACH(if_ppa, &interfaces, meta.link) {
365 switch (if_ppa->pa.scheduler) {
367 error = check_commit_cbq(dev, opts, if_ppa);
370 error = check_commit_priq(dev, opts, if_ppa);
373 error = check_commit_hfsc(dev, opts, if_ppa);
376 error = check_commit_fairq(dev, opts, if_ppa);
386 * eval_pfqueue computes the queue parameters.
389 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
390 struct node_queue_opt *opts)
392 /* should be merged with expand_queue */
393 struct pfctl_altq *if_ppa, *parent;
396 /* find the corresponding interface and copy fields used by queues */
397 if ((if_ppa = pfaltq_lookup(pa->ifname)) == NULL) {
398 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
401 pa->scheduler = if_ppa->pa.scheduler;
402 pa->ifbandwidth = if_ppa->pa.ifbandwidth;
404 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
405 fprintf(stderr, "queue %s already exists on interface %s\n",
406 pa->qname, pa->ifname);
409 pa->qid = qname_to_qid(pa->qname);
412 if (pa->parent[0] != 0) {
413 parent = qname_to_pfaltq(pa->parent, pa->ifname);
414 if (parent == NULL) {
415 fprintf(stderr, "parent %s not found for %s\n",
416 pa->parent, pa->qname);
419 pa->parent_qid = parent->pa.qid;
422 pa->qlimit = DEFAULT_QLIMIT;
424 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC ||
425 pa->scheduler == ALTQT_FAIRQ) {
426 pa->bandwidth = eval_bwspec(bw,
427 parent == NULL ? pa->ifbandwidth : parent->pa.bandwidth);
429 if (pa->bandwidth > pa->ifbandwidth) {
430 fprintf(stderr, "bandwidth for %s higher than "
431 "interface\n", pa->qname);
435 * If not HFSC, then check that the sum of the child
436 * bandwidths is less than the parent's bandwidth. For
437 * HFSC, the equivalent concept is to check that the sum of
438 * the child linkshare service curves are under the parent's
439 * linkshare service curve, and that check is performed by
440 * eval_pfqueue_hfsc().
442 if ((parent != NULL) && (pa->scheduler != ALTQT_HFSC)) {
443 if (pa->bandwidth > parent->pa.bandwidth) {
444 warnx("bandwidth for %s higher than parent",
448 parent->meta.bwsum += pa->bandwidth;
449 if (parent->meta.bwsum > parent->pa.bandwidth) {
450 warnx("the sum of the child bandwidth (%" PRIu64
451 ") higher than parent \"%s\" (%" PRIu64 ")",
452 parent->meta.bwsum, parent->pa.qname,
453 parent->pa.bandwidth);
458 if (eval_queue_opts(pa, opts,
459 parent == NULL ? pa->ifbandwidth : parent->pa.bandwidth))
463 parent->meta.children++;
465 switch (pa->scheduler) {
467 error = eval_pfqueue_cbq(pf, pa, if_ppa);
470 error = eval_pfqueue_priq(pf, pa, if_ppa);
473 error = eval_pfqueue_hfsc(pf, pa, if_ppa, parent);
476 error = eval_pfqueue_fairq(pf, pa, if_ppa, parent);
485 * CBQ support functions
487 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
488 #define RM_NS_PER_SEC (1000000000)
491 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa, struct pfctl_altq *if_ppa)
493 struct cbq_opts *opts;
496 if (pa->priority >= CBQ_MAXPRI) {
497 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
501 ifmtu = getifmtu(pa->ifname);
502 opts = &pa->pq_u.cbq_opts;
504 if (opts->pktsize == 0) { /* use default */
505 opts->pktsize = ifmtu;
506 if (opts->pktsize > MCLBYTES) /* do what TCP does */
507 opts->pktsize &= ~MCLBYTES;
508 } else if (opts->pktsize > ifmtu)
509 opts->pktsize = ifmtu;
510 if (opts->maxpktsize == 0) /* use default */
511 opts->maxpktsize = ifmtu;
512 else if (opts->maxpktsize > ifmtu)
513 opts->pktsize = ifmtu;
515 if (opts->pktsize > opts->maxpktsize)
516 opts->pktsize = opts->maxpktsize;
518 if (pa->parent[0] == 0)
519 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
521 if (pa->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
522 if_ppa->meta.root_classes++;
523 if (pa->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
524 if_ppa->meta.default_classes++;
526 cbq_compute_idletime(pf, pa);
531 * compute ns_per_byte, maxidle, minidle, and offtime
534 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
536 struct cbq_opts *opts;
537 double maxidle_s, maxidle, minidle;
538 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
539 double z, g, f, gton, gtom;
540 u_int minburst, maxburst;
542 opts = &pa->pq_u.cbq_opts;
543 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
544 minburst = opts->minburst;
545 maxburst = opts->maxburst;
547 if (pa->bandwidth == 0)
548 f = 0.0001; /* small enough? */
550 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
552 nsPerByte = ifnsPerByte / f;
553 ptime = (double)opts->pktsize * ifnsPerByte;
554 cptime = ptime * (1.0 - f) / f;
556 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
558 * this causes integer overflow in kernel!
559 * (bandwidth < 6Kbps when max_pkt_size=1500)
561 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0) {
562 warnx("queue bandwidth must be larger than %s",
563 rate2str(ifnsPerByte * (double)opts->maxpktsize /
564 (double)INT_MAX * (double)pa->ifbandwidth));
565 fprintf(stderr, "cbq: queue %s is too slow!\n",
568 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
571 if (maxburst == 0) { /* use default */
572 if (cptime > 10.0 * 1000000)
577 if (minburst == 0) /* use default */
579 if (minburst > maxburst)
582 z = (double)(1 << RM_FILTER_GAIN);
584 gton = pow(g, (double)maxburst);
585 gtom = pow(g, (double)(minburst-1));
586 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
587 maxidle_s = (1.0 - g);
588 if (maxidle > maxidle_s)
589 maxidle = ptime * maxidle;
591 maxidle = ptime * maxidle_s;
592 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
593 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
595 /* scale parameters */
596 maxidle = ((maxidle * 8.0) / nsPerByte) *
597 pow(2.0, (double)RM_FILTER_GAIN);
598 offtime = (offtime * 8.0) / nsPerByte *
599 pow(2.0, (double)RM_FILTER_GAIN);
600 minidle = ((minidle * 8.0) / nsPerByte) *
601 pow(2.0, (double)RM_FILTER_GAIN);
603 maxidle = maxidle / 1000.0;
604 offtime = offtime / 1000.0;
605 minidle = minidle / 1000.0;
607 opts->minburst = minburst;
608 opts->maxburst = maxburst;
609 opts->ns_per_byte = (u_int)nsPerByte;
610 opts->maxidle = (u_int)fabs(maxidle);
611 opts->minidle = (int)minidle;
612 opts->offtime = (u_int)fabs(offtime);
618 check_commit_cbq(int dev, int opts, struct pfctl_altq *if_ppa)
623 * check if cbq has one root queue and one default queue
626 if (if_ppa->meta.root_classes != 1) {
627 warnx("should have one root queue on %s", if_ppa->pa.ifname);
630 if (if_ppa->meta.default_classes != 1) {
631 warnx("should have one default queue on %s", if_ppa->pa.ifname);
638 print_cbq_opts(const struct pf_altq *a)
640 const struct cbq_opts *opts;
642 opts = &a->pq_u.cbq_opts;
645 if (opts->flags & CBQCLF_RED)
647 if (opts->flags & CBQCLF_ECN)
649 if (opts->flags & CBQCLF_RIO)
651 if (opts->flags & CBQCLF_CODEL)
653 if (opts->flags & CBQCLF_CLEARDSCP)
654 printf(" cleardscp");
655 if (opts->flags & CBQCLF_FLOWVALVE)
656 printf(" flowvalve");
657 if (opts->flags & CBQCLF_BORROW)
659 if (opts->flags & CBQCLF_WRR)
661 if (opts->flags & CBQCLF_EFFICIENT)
662 printf(" efficient");
663 if (opts->flags & CBQCLF_ROOTCLASS)
665 if (opts->flags & CBQCLF_DEFCLASS)
675 * PRIQ support functions
678 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa, struct pfctl_altq *if_ppa)
681 if (pa->priority >= PRIQ_MAXPRI) {
682 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
685 if (BIT_ISSET(QPRI_BITSET_SIZE, pa->priority, &if_ppa->meta.qpris)) {
686 warnx("%s does not have a unique priority on interface %s",
687 pa->qname, pa->ifname);
690 BIT_SET(QPRI_BITSET_SIZE, pa->priority, &if_ppa->meta.qpris);
692 if (pa->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
693 if_ppa->meta.default_classes++;
698 check_commit_priq(int dev, int opts, struct pfctl_altq *if_ppa)
702 * check if priq has one default class for this interface
704 if (if_ppa->meta.default_classes != 1) {
705 warnx("should have one default queue on %s", if_ppa->pa.ifname);
712 print_priq_opts(const struct pf_altq *a)
714 const struct priq_opts *opts;
716 opts = &a->pq_u.priq_opts;
720 if (opts->flags & PRCF_RED)
722 if (opts->flags & PRCF_ECN)
724 if (opts->flags & PRCF_RIO)
726 if (opts->flags & PRCF_CODEL)
728 if (opts->flags & PRCF_CLEARDSCP)
729 printf(" cleardscp");
730 if (opts->flags & PRCF_DEFAULTCLASS)
740 * HFSC support functions
743 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa, struct pfctl_altq *if_ppa,
744 struct pfctl_altq *parent)
746 struct hfsc_opts_v1 *opts;
747 struct service_curve sc;
749 opts = &pa->pq_u.hfsc_opts;
751 if (parent == NULL) {
753 opts->lssc_m1 = pa->ifbandwidth;
754 opts->lssc_m2 = pa->ifbandwidth;
759 /* First child initializes the parent's service curve accumulators. */
760 if (parent->meta.children == 1) {
761 LIST_INIT(&parent->meta.rtsc);
762 LIST_INIT(&parent->meta.lssc);
765 if (parent->pa.pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
766 warnx("adding %s would make default queue %s not a leaf",
767 pa->qname, pa->parent);
771 if (pa->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS)
772 if_ppa->meta.default_classes++;
774 /* if link_share is not specified, use bandwidth */
775 if (opts->lssc_m2 == 0)
776 opts->lssc_m2 = pa->bandwidth;
778 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
779 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
780 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
781 warnx("m2 is zero for %s", pa->qname);
785 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
786 (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
787 (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
788 warnx("m1 must be zero for convex curve: %s", pa->qname);
794 * for the real-time service curve, the sum of the service curves
795 * should not exceed 80% of the interface bandwidth. 20% is reserved
796 * not to over-commit the actual interface bandwidth.
797 * for the linkshare service curve, the sum of the child service
798 * curve should not exceed the parent service curve.
799 * for the upper-limit service curve, the assigned bandwidth should
800 * be smaller than the interface bandwidth, and the upper-limit should
801 * be larger than the real-time service curve when both are defined.
804 /* check the real-time service curve. reserve 20% of interface bw */
805 if (opts->rtsc_m2 != 0) {
806 /* add this queue to the sum */
807 sc.m1 = opts->rtsc_m1;
809 sc.m2 = opts->rtsc_m2;
810 gsc_add_sc(&parent->meta.rtsc, &sc);
811 /* compare the sum with 80% of the interface */
814 sc.m2 = pa->ifbandwidth / 100 * 80;
815 if (!is_gsc_under_sc(&parent->meta.rtsc, &sc)) {
816 warnx("real-time sc exceeds 80%% of the interface "
817 "bandwidth (%s)", rate2str((double)sc.m2));
822 /* check the linkshare service curve. */
823 if (opts->lssc_m2 != 0) {
824 /* add this queue to the child sum */
825 sc.m1 = opts->lssc_m1;
827 sc.m2 = opts->lssc_m2;
828 gsc_add_sc(&parent->meta.lssc, &sc);
829 /* compare the sum of the children with parent's sc */
830 sc.m1 = parent->pa.pq_u.hfsc_opts.lssc_m1;
831 sc.d = parent->pa.pq_u.hfsc_opts.lssc_d;
832 sc.m2 = parent->pa.pq_u.hfsc_opts.lssc_m2;
833 if (!is_gsc_under_sc(&parent->meta.lssc, &sc)) {
834 warnx("linkshare sc exceeds parent's sc");
839 /* check the upper-limit service curve. */
840 if (opts->ulsc_m2 != 0) {
841 if (opts->ulsc_m1 > pa->ifbandwidth ||
842 opts->ulsc_m2 > pa->ifbandwidth) {
843 warnx("upper-limit larger than interface bandwidth");
846 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
847 warnx("upper-limit sc smaller than real-time sc");
856 * FAIRQ support functions
859 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa,
860 struct pfctl_altq *if_ppa, struct pfctl_altq *parent)
862 struct fairq_opts *opts;
863 struct service_curve sc;
865 opts = &pa->pq_u.fairq_opts;
867 if (parent == NULL) {
869 opts->lssc_m1 = pa->ifbandwidth;
870 opts->lssc_m2 = pa->ifbandwidth;
875 /* First child initializes the parent's service curve accumulator. */
876 if (parent->meta.children == 1)
877 LIST_INIT(&parent->meta.lssc);
879 if (parent->pa.pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
880 warnx("adding %s would make default queue %s not a leaf",
881 pa->qname, pa->parent);
885 if (pa->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS)
886 if_ppa->meta.default_classes++;
888 /* if link_share is not specified, use bandwidth */
889 if (opts->lssc_m2 == 0)
890 opts->lssc_m2 = pa->bandwidth;
894 * for the real-time service curve, the sum of the service curves
895 * should not exceed 80% of the interface bandwidth. 20% is reserved
896 * not to over-commit the actual interface bandwidth.
897 * for the link-sharing service curve, the sum of the child service
898 * curve should not exceed the parent service curve.
899 * for the upper-limit service curve, the assigned bandwidth should
900 * be smaller than the interface bandwidth, and the upper-limit should
901 * be larger than the real-time service curve when both are defined.
904 /* check the linkshare service curve. */
905 if (opts->lssc_m2 != 0) {
906 /* add this queue to the child sum */
907 sc.m1 = opts->lssc_m1;
909 sc.m2 = opts->lssc_m2;
910 gsc_add_sc(&parent->meta.lssc, &sc);
911 /* compare the sum of the children with parent's sc */
912 sc.m1 = parent->pa.pq_u.fairq_opts.lssc_m1;
913 sc.d = parent->pa.pq_u.fairq_opts.lssc_d;
914 sc.m2 = parent->pa.pq_u.fairq_opts.lssc_m2;
915 if (!is_gsc_under_sc(&parent->meta.lssc, &sc)) {
916 warnx("link-sharing sc exceeds parent's sc");
925 check_commit_hfsc(int dev, int opts, struct pfctl_altq *if_ppa)
928 /* check if hfsc has one default queue for this interface */
929 if (if_ppa->meta.default_classes != 1) {
930 warnx("should have one default queue on %s", if_ppa->pa.ifname);
937 check_commit_fairq(int dev __unused, int opts __unused, struct pfctl_altq *if_ppa)
940 /* check if fairq has one default queue for this interface */
941 if (if_ppa->meta.default_classes != 1) {
942 warnx("should have one default queue on %s", if_ppa->pa.ifname);
949 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
951 const struct hfsc_opts_v1 *opts;
952 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
954 opts = &a->pq_u.hfsc_opts;
956 rtsc = lssc = ulsc = NULL;
958 rtsc = &qopts->data.hfsc_opts.realtime;
959 lssc = &qopts->data.hfsc_opts.linkshare;
960 ulsc = &qopts->data.hfsc_opts.upperlimit;
963 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
964 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
965 opts->lssc_d != 0))) {
967 if (opts->flags & HFCF_RED)
969 if (opts->flags & HFCF_ECN)
971 if (opts->flags & HFCF_RIO)
973 if (opts->flags & HFCF_CODEL)
975 if (opts->flags & HFCF_CLEARDSCP)
976 printf(" cleardscp");
977 if (opts->flags & HFCF_DEFAULTCLASS)
979 if (opts->rtsc_m2 != 0)
980 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
981 opts->rtsc_m2, rtsc);
982 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
984 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
985 opts->lssc_m2, lssc);
986 if (opts->ulsc_m2 != 0)
987 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
988 opts->ulsc_m2, ulsc);
997 print_codel_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
999 const struct codel_opts *opts;
1001 opts = &a->pq_u.codel_opts;
1002 if (opts->target || opts->interval || opts->ecn) {
1005 printf(" target %d", opts->target);
1007 printf(" interval %d", opts->interval);
1019 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1021 const struct fairq_opts *opts;
1022 const struct node_fairq_sc *loc_lssc;
1024 opts = &a->pq_u.fairq_opts;
1028 loc_lssc = &qopts->data.fairq_opts.linkshare;
1031 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1032 opts->lssc_d != 0))) {
1034 if (opts->flags & FARF_RED)
1036 if (opts->flags & FARF_ECN)
1038 if (opts->flags & FARF_RIO)
1040 if (opts->flags & FARF_CODEL)
1042 if (opts->flags & FARF_CLEARDSCP)
1043 printf(" cleardscp");
1044 if (opts->flags & FARF_DEFAULTCLASS)
1046 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1048 print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1049 opts->lssc_m2, loc_lssc);
1058 * admission control using generalized service curve
1061 /* add a new service curve to a generalized service curve */
1063 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1068 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1069 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1073 * check whether all points of a generalized service curve have
1074 * their y-coordinates no larger than a given two-piece linear
1078 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1080 struct segment *s, *last, *end;
1083 if (is_sc_null(sc)) {
1084 if (LIST_EMPTY(gsc))
1086 LIST_FOREACH(s, gsc, _next) {
1093 * gsc has a dummy entry at the end with x = INFINITY.
1094 * loop through up to this dummy entry.
1096 end = gsc_getentry(gsc, INFINITY);
1100 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1101 if (s->y > sc_x2y(sc, s->x))
1105 /* last now holds the real last segment */
1108 if (last->m > sc->m2)
1110 if (last->x < sc->d && last->m > sc->m1) {
1111 y = last->y + (sc->d - last->x) * last->m;
1112 if (y > sc_x2y(sc, sc->d))
1119 * return a segment entry starting at x.
1120 * if gsc has no entry starting at x, a new entry is created at x.
1122 static struct segment *
1123 gsc_getentry(struct gen_sc *gsc, double x)
1125 struct segment *new, *prev, *s;
1128 LIST_FOREACH(s, gsc, _next) {
1130 return (s); /* matching entry found */
1137 /* we have to create a new entry */
1138 if ((new = calloc(1, sizeof(struct segment))) == NULL)
1142 if (x == INFINITY || s == NULL)
1144 else if (s->x == INFINITY)
1149 /* insert the new entry at the head of the list */
1152 LIST_INSERT_HEAD(gsc, new, _next);
1155 * the start point intersects with the segment pointed by
1156 * prev. divide prev into 2 segments
1158 if (x == INFINITY) {
1165 prev->d = x - prev->x;
1166 new->y = prev->d * prev->m + prev->y;
1169 LIST_INSERT_AFTER(prev, new, _next);
1174 /* add a segment to a generalized service curve */
1176 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1178 struct segment *start, *end, *s;
1185 start = gsc_getentry(gsc, x);
1186 end = gsc_getentry(gsc, x2);
1187 if (start == NULL || end == NULL)
1190 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1192 s->y += y + (s->x - x) * m;
1195 end = gsc_getentry(gsc, INFINITY);
1196 for (; s != end; s = LIST_NEXT(s, _next)) {
1203 /* get y-projection of a service curve */
1205 sc_x2y(struct service_curve *sc, double x)
1209 if (x <= (double)sc->d)
1210 /* y belongs to the 1st segment */
1211 y = x * (double)sc->m1;
1213 /* y belongs to the 2nd segment */
1214 y = (double)sc->d * (double)sc->m1
1215 + (x - (double)sc->d) * (double)sc->m2;
1223 #define RATESTR_MAX 16
1226 rate2str(double rate)
1229 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1232 static const char unit[] = " KMG";
1234 buf = r2sbuf[idx++];
1235 if (idx == R2S_BUFS)
1238 for (i = 0; rate >= 1000 && i <= 3; i++)
1241 if ((int)(rate * 100) % 100)
1242 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1244 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1250 getifspeed(char *ifname)
1254 struct if_data ifrdat;
1256 s = get_query_socket();
1257 bzero(&ifr, sizeof(ifr));
1258 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1259 sizeof(ifr.ifr_name))
1260 errx(1, "getifspeed: strlcpy");
1261 ifr.ifr_data = (caddr_t)&ifrdat;
1262 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1263 err(1, "SIOCGIFDATA");
1264 return ((u_int32_t)ifrdat.ifi_baudrate);
1268 getifmtu(char *ifname)
1273 s = get_query_socket();
1274 bzero(&ifr, sizeof(ifr));
1275 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1276 sizeof(ifr.ifr_name))
1277 errx(1, "getifmtu: strlcpy");
1278 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1282 err(1, "SIOCGIFMTU");
1284 if (ifr.ifr_mtu > 0)
1285 return (ifr.ifr_mtu);
1287 warnx("could not get mtu for %s, assuming 1500", ifname);
1293 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1298 switch (pa->scheduler) {
1300 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1303 pa->pq_u.priq_opts = opts->data.priq_opts;
1306 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1307 if (opts->data.hfsc_opts.linkshare.used) {
1308 pa->pq_u.hfsc_opts.lssc_m1 =
1309 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1311 pa->pq_u.hfsc_opts.lssc_m2 =
1312 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1314 pa->pq_u.hfsc_opts.lssc_d =
1315 opts->data.hfsc_opts.linkshare.d;
1317 if (opts->data.hfsc_opts.realtime.used) {
1318 pa->pq_u.hfsc_opts.rtsc_m1 =
1319 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1321 pa->pq_u.hfsc_opts.rtsc_m2 =
1322 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1324 pa->pq_u.hfsc_opts.rtsc_d =
1325 opts->data.hfsc_opts.realtime.d;
1327 if (opts->data.hfsc_opts.upperlimit.used) {
1328 pa->pq_u.hfsc_opts.ulsc_m1 =
1329 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1331 pa->pq_u.hfsc_opts.ulsc_m2 =
1332 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1334 pa->pq_u.hfsc_opts.ulsc_d =
1335 opts->data.hfsc_opts.upperlimit.d;
1339 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1340 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1341 pa->pq_u.fairq_opts.hogs_m1 =
1342 eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1344 if (opts->data.fairq_opts.linkshare.used) {
1345 pa->pq_u.fairq_opts.lssc_m1 =
1346 eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1348 pa->pq_u.fairq_opts.lssc_m2 =
1349 eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1351 pa->pq_u.fairq_opts.lssc_d =
1352 opts->data.fairq_opts.linkshare.d;
1356 pa->pq_u.codel_opts.target = opts->data.codel_opts.target;
1357 pa->pq_u.codel_opts.interval = opts->data.codel_opts.interval;
1358 pa->pq_u.codel_opts.ecn = opts->data.codel_opts.ecn;
1361 warnx("eval_queue_opts: unknown scheduler type %u",
1371 * If absolute bandwidth if set, return the lesser of that value and the
1372 * reference bandwidth. Limiting to the reference bandwidth allows simple
1373 * limiting of configured bandwidth parameters for schedulers that are
1374 * 32-bit limited, as the root/interface bandwidth (top-level reference
1375 * bandwidth) will be properly limited in that case.
1377 * Otherwise, if the absolute bandwidth is not set, return given percentage
1378 * of reference bandwidth.
1381 eval_bwspec(struct node_queue_bw *bw, u_int64_t ref_bw)
1383 if (bw->bw_absolute > 0)
1384 return (MIN(bw->bw_absolute, ref_bw));
1386 if (bw->bw_percent > 0)
1387 return (ref_bw / 100 * bw->bw_percent);
1393 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1394 const struct node_hfsc_sc *sc)
1396 printf(" %s", scname);
1400 if (sc != NULL && sc->m1.bw_percent > 0)
1401 printf("%u%%", sc->m1.bw_percent);
1403 printf("%s", rate2str((double)m1));
1407 if (sc != NULL && sc->m2.bw_percent > 0)
1408 printf(" %u%%", sc->m2.bw_percent);
1410 printf(" %s", rate2str((double)m2));
1417 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1418 const struct node_fairq_sc *sc)
1420 printf(" %s", scname);
1424 if (sc != NULL && sc->m1.bw_percent > 0)
1425 printf("%u%%", sc->m1.bw_percent);
1427 printf("%s", rate2str((double)m1));
1431 if (sc != NULL && sc->m2.bw_percent > 0)
1432 printf(" %u%%", sc->m2.bw_percent);
1434 printf(" %s", rate2str((double)m2));