2 /* $OpenBSD: pfctl_altq.c,v 1.92 2007/05/27 05:15:17 claudio 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>
26 #include <sys/param.h>
31 #include <netinet/in.h>
32 #include <net/pfvar.h>
43 #include <altq/altq.h>
44 #include <altq/altq_cbq.h>
45 #include <altq/altq_priq.h>
46 #include <altq/altq_hfsc.h>
48 #include "pfctl_parser.h"
51 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
53 TAILQ_HEAD(altqs
, pf_altq
) altqs
= TAILQ_HEAD_INITIALIZER(altqs
);
54 LIST_HEAD(gen_sc
, segment
) rtsc
, lssc
;
56 struct pf_altq
*qname_to_pfaltq(const char *, const char *);
57 u_int32_t
qname_to_qid(const char *);
59 static int eval_pfqueue_cbq(struct pfctl
*, struct pf_altq
*);
60 static int cbq_compute_idletime(struct pfctl
*, struct pf_altq
*);
61 static int check_commit_cbq(int, int, struct pf_altq
*);
62 static int print_cbq_opts(const struct pf_altq
*);
64 static int eval_pfqueue_priq(struct pfctl
*, struct pf_altq
*);
65 static int check_commit_priq(int, int, struct pf_altq
*);
66 static int print_priq_opts(const struct pf_altq
*);
68 static int eval_pfqueue_hfsc(struct pfctl
*, struct pf_altq
*);
69 static int check_commit_hfsc(int, int, struct pf_altq
*);
70 static int print_hfsc_opts(const struct pf_altq
*,
71 const struct node_queue_opt
*);
73 static void gsc_add_sc(struct gen_sc
*, struct service_curve
*);
74 static int is_gsc_under_sc(struct gen_sc
*,
75 struct service_curve
*);
76 static void gsc_destroy(struct gen_sc
*);
77 static struct segment
*gsc_getentry(struct gen_sc
*, double);
78 static int gsc_add_seg(struct gen_sc
*, double, double, double,
80 static double sc_x2y(struct service_curve
*, double);
82 u_int32_t
getifspeed(char *);
83 u_long
getifmtu(char *);
84 int eval_queue_opts(struct pf_altq
*, struct node_queue_opt
*,
86 u_int32_t
eval_bwspec(struct node_queue_bw
*, u_int32_t
);
87 void print_hfsc_sc(const char *, u_int
, u_int
, u_int
,
88 const struct node_hfsc_sc
*);
91 pfaltq_store(struct pf_altq
*a
)
95 if ((altq
= malloc(sizeof(*altq
))) == NULL
)
97 memcpy(altq
, a
, sizeof(struct pf_altq
));
98 TAILQ_INSERT_TAIL(&altqs
, altq
, entries
);
102 pfaltq_lookup(const char *ifname
)
104 struct pf_altq
*altq
;
106 TAILQ_FOREACH(altq
, &altqs
, entries
) {
107 if (strncmp(ifname
, altq
->ifname
, IFNAMSIZ
) == 0 &&
115 qname_to_pfaltq(const char *qname
, const char *ifname
)
117 struct pf_altq
*altq
;
119 TAILQ_FOREACH(altq
, &altqs
, entries
) {
120 if (strncmp(ifname
, altq
->ifname
, IFNAMSIZ
) == 0 &&
121 strncmp(qname
, altq
->qname
, PF_QNAME_SIZE
) == 0)
128 qname_to_qid(const char *qname
)
130 struct pf_altq
*altq
;
133 * We guarantee that same named queues on different interfaces
134 * have the same qid, so we do NOT need to limit matching on
138 TAILQ_FOREACH(altq
, &altqs
, entries
) {
139 if (strncmp(qname
, altq
->qname
, PF_QNAME_SIZE
) == 0)
146 print_altq(const struct pf_altq
*a
, unsigned level
, struct node_queue_bw
*bw
,
147 struct node_queue_opt
*qopts
)
149 if (a
->qname
[0] != 0) {
150 print_queue(a
, level
, bw
, 1, qopts
);
154 printf("altq on %s ", a
->ifname
);
156 switch (a
->scheduler
) {
158 if (!print_cbq_opts(a
))
162 if (!print_priq_opts(a
))
166 if (!print_hfsc_opts(a
, qopts
))
171 if (bw
!= NULL
&& bw
->bw_percent
> 0) {
172 if (bw
->bw_percent
< 100)
173 printf("bandwidth %u%% ", bw
->bw_percent
);
175 printf("bandwidth %s ", rate2str((double)a
->ifbandwidth
));
177 if (a
->qlimit
!= DEFAULT_QLIMIT
)
178 printf("qlimit %u ", a
->qlimit
);
179 printf("tbrsize %u ", a
->tbrsize
);
183 print_queue(const struct pf_altq
*a
, unsigned level
, struct node_queue_bw
*bw
,
184 int print_interface
, struct node_queue_opt
*qopts
)
189 for (i
= 0; i
< level
; ++i
)
191 printf("%s ", a
->qname
);
193 printf("on %s ", a
->ifname
);
194 if (a
->scheduler
== ALTQT_CBQ
|| a
->scheduler
== ALTQT_HFSC
) {
195 if (bw
!= NULL
&& bw
->bw_percent
> 0) {
196 if (bw
->bw_percent
< 100)
197 printf("bandwidth %u%% ", bw
->bw_percent
);
199 printf("bandwidth %s ", rate2str((double)a
->bandwidth
));
201 if (a
->priority
!= DEFAULT_PRIORITY
)
202 printf("priority %u ", a
->priority
);
203 if (a
->qlimit
!= DEFAULT_QLIMIT
)
204 printf("qlimit %u ", a
->qlimit
);
205 switch (a
->scheduler
) {
213 print_hfsc_opts(a
, qopts
);
219 * eval_pfaltq computes the discipline parameters.
222 eval_pfaltq(struct pfctl
*pf
, struct pf_altq
*pa
, struct node_queue_bw
*bw
,
223 struct node_queue_opt
*opts
)
225 u_int rate
, size
, errors
= 0;
227 if (bw
->bw_absolute
> 0)
228 pa
->ifbandwidth
= bw
->bw_absolute
;
230 if ((rate
= getifspeed(pa
->ifname
)) == 0) {
231 fprintf(stderr
, "interface %s does not know its bandwidth, "
232 "please specify an absolute bandwidth\n",
235 } else if ((pa
->ifbandwidth
= eval_bwspec(bw
, rate
)) == 0)
236 pa
->ifbandwidth
= rate
;
238 errors
+= eval_queue_opts(pa
, opts
, pa
->ifbandwidth
);
240 /* if tbrsize is not specified, use heuristics */
241 if (pa
->tbrsize
== 0) {
242 rate
= pa
->ifbandwidth
;
243 if (rate
<= 1 * 1000 * 1000)
245 else if (rate
<= 10 * 1000 * 1000)
247 else if (rate
<= 200 * 1000 * 1000)
251 size
= size
* getifmtu(pa
->ifname
);
260 * check_commit_altq does consistency check for each interface
263 check_commit_altq(int dev
, int opts
)
265 struct pf_altq
*altq
;
268 /* call the discipline check for each interface. */
269 TAILQ_FOREACH(altq
, &altqs
, entries
) {
270 if (altq
->qname
[0] == 0) {
271 switch (altq
->scheduler
) {
273 error
= check_commit_cbq(dev
, opts
, altq
);
276 error
= check_commit_priq(dev
, opts
, altq
);
279 error
= check_commit_hfsc(dev
, opts
, altq
);
290 * eval_pfqueue computes the queue parameters.
293 eval_pfqueue(struct pfctl
*pf
, struct pf_altq
*pa
, struct node_queue_bw
*bw
,
294 struct node_queue_opt
*opts
)
296 /* should be merged with expand_queue */
297 struct pf_altq
*if_pa
, *parent
, *altq
;
301 /* find the corresponding interface and copy fields used by queues */
302 if ((if_pa
= pfaltq_lookup(pa
->ifname
)) == NULL
) {
303 fprintf(stderr
, "altq not defined on %s\n", pa
->ifname
);
306 pa
->scheduler
= if_pa
->scheduler
;
307 pa
->ifbandwidth
= if_pa
->ifbandwidth
;
309 if (qname_to_pfaltq(pa
->qname
, pa
->ifname
) != NULL
) {
310 fprintf(stderr
, "queue %s already exists on interface %s\n",
311 pa
->qname
, pa
->ifname
);
314 pa
->qid
= qname_to_qid(pa
->qname
);
317 if (pa
->parent
[0] != 0) {
318 parent
= qname_to_pfaltq(pa
->parent
, pa
->ifname
);
319 if (parent
== NULL
) {
320 fprintf(stderr
, "parent %s not found for %s\n",
321 pa
->parent
, pa
->qname
);
324 pa
->parent_qid
= parent
->qid
;
327 pa
->qlimit
= DEFAULT_QLIMIT
;
329 if (pa
->scheduler
== ALTQT_CBQ
|| pa
->scheduler
== ALTQT_HFSC
) {
330 pa
->bandwidth
= eval_bwspec(bw
,
331 parent
== NULL
? 0 : parent
->bandwidth
);
333 if (pa
->bandwidth
> pa
->ifbandwidth
) {
334 fprintf(stderr
, "bandwidth for %s higher than "
335 "interface\n", pa
->qname
);
338 /* check the sum of the child bandwidth is under parent's */
339 if (parent
!= NULL
) {
340 if (pa
->bandwidth
> parent
->bandwidth
) {
341 warnx("bandwidth for %s higher than parent",
346 TAILQ_FOREACH(altq
, &altqs
, entries
) {
347 if (strncmp(altq
->ifname
, pa
->ifname
,
349 altq
->qname
[0] != 0 &&
350 strncmp(altq
->parent
, pa
->parent
,
352 bwsum
+= altq
->bandwidth
;
354 bwsum
+= pa
->bandwidth
;
355 if (bwsum
> parent
->bandwidth
) {
356 warnx("the sum of the child bandwidth higher"
357 " than parent \"%s\"", parent
->qname
);
362 if (eval_queue_opts(pa
, opts
, parent
== NULL
? 0 : parent
->bandwidth
))
365 switch (pa
->scheduler
) {
367 error
= eval_pfqueue_cbq(pf
, pa
);
370 error
= eval_pfqueue_priq(pf
, pa
);
373 error
= eval_pfqueue_hfsc(pf
, pa
);
382 * CBQ support functions
384 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
385 #define RM_NS_PER_SEC (1000000000)
388 eval_pfqueue_cbq(struct pfctl
*pf
, struct pf_altq
*pa
)
390 struct cbq_opts
*opts
;
393 if (pa
->priority
>= CBQ_MAXPRI
) {
394 warnx("priority out of range: max %d", CBQ_MAXPRI
- 1);
398 ifmtu
= getifmtu(pa
->ifname
);
399 opts
= &pa
->pq_u
.cbq_opts
;
401 if (opts
->pktsize
== 0) { /* use default */
402 opts
->pktsize
= ifmtu
;
403 if (opts
->pktsize
> MCLBYTES
) /* do what TCP does */
404 opts
->pktsize
&= ~MCLBYTES
;
405 } else if (opts
->pktsize
> ifmtu
)
406 opts
->pktsize
= ifmtu
;
407 if (opts
->maxpktsize
== 0) /* use default */
408 opts
->maxpktsize
= ifmtu
;
409 else if (opts
->maxpktsize
> ifmtu
)
410 opts
->pktsize
= ifmtu
;
412 if (opts
->pktsize
> opts
->maxpktsize
)
413 opts
->pktsize
= opts
->maxpktsize
;
415 if (pa
->parent
[0] == 0)
416 opts
->flags
|= (CBQCLF_ROOTCLASS
| CBQCLF_WRR
);
418 cbq_compute_idletime(pf
, pa
);
423 * compute ns_per_byte, maxidle, minidle, and offtime
426 cbq_compute_idletime(struct pfctl
*pf
, struct pf_altq
*pa
)
428 struct cbq_opts
*opts
;
429 double maxidle_s
, maxidle
, minidle
;
430 double offtime
, nsPerByte
, ifnsPerByte
, ptime
, cptime
;
431 double z
, g
, f
, gton
, gtom
;
432 u_int minburst
, maxburst
;
434 opts
= &pa
->pq_u
.cbq_opts
;
435 ifnsPerByte
= (1.0 / (double)pa
->ifbandwidth
) * RM_NS_PER_SEC
* 8;
436 minburst
= opts
->minburst
;
437 maxburst
= opts
->maxburst
;
439 if (pa
->bandwidth
== 0)
440 f
= 0.0001; /* small enough? */
442 f
= ((double) pa
->bandwidth
/ (double) pa
->ifbandwidth
);
444 nsPerByte
= ifnsPerByte
/ f
;
445 ptime
= (double)opts
->pktsize
* ifnsPerByte
;
446 cptime
= ptime
* (1.0 - f
) / f
;
448 if (nsPerByte
* (double)opts
->maxpktsize
> (double)INT_MAX
) {
450 * this causes integer overflow in kernel!
451 * (bandwidth < 6Kbps when max_pkt_size=1500)
453 if (pa
->bandwidth
!= 0 && (pf
->opts
& PF_OPT_QUIET
) == 0)
454 warnx("queue bandwidth must be larger than %s",
455 rate2str(ifnsPerByte
* (double)opts
->maxpktsize
/
456 (double)INT_MAX
* (double)pa
->ifbandwidth
));
457 fprintf(stderr
, "cbq: queue %s is too slow!\n",
459 nsPerByte
= (double)(INT_MAX
/ opts
->maxpktsize
);
462 if (maxburst
== 0) { /* use default */
463 if (cptime
> 10.0 * 1000000)
468 if (minburst
== 0) /* use default */
470 if (minburst
> maxburst
)
473 z
= (double)(1 << RM_FILTER_GAIN
);
475 gton
= pow(g
, (double)maxburst
);
476 gtom
= pow(g
, (double)(minburst
-1));
477 maxidle
= ((1.0 / f
- 1.0) * ((1.0 - gton
) / gton
));
478 maxidle_s
= (1.0 - g
);
479 if (maxidle
> maxidle_s
)
480 maxidle
= ptime
* maxidle
;
482 maxidle
= ptime
* maxidle_s
;
483 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");
560 if (opts
->flags
& CBQCLF_BORROW
)
563 if (opts
->flags
& CBQCLF_WRR
)
565 if (opts
->flags
& CBQCLF_EFFICIENT
)
566 printf(" efficient");
567 if (opts
->flags
& CBQCLF_ROOTCLASS
)
569 if (opts
->flags
& CBQCLF_DEFCLASS
)
579 * PRIQ support functions
582 eval_pfqueue_priq(struct pfctl
*pf
, struct pf_altq
*pa
)
584 struct pf_altq
*altq
;
586 if (pa
->priority
>= PRIQ_MAXPRI
) {
587 warnx("priority out of range: max %d", PRIQ_MAXPRI
- 1);
590 /* the priority should be unique for the interface */
591 TAILQ_FOREACH(altq
, &altqs
, entries
) {
592 if (strncmp(altq
->ifname
, pa
->ifname
, IFNAMSIZ
) == 0 &&
593 altq
->qname
[0] != 0 && altq
->priority
== pa
->priority
) {
594 warnx("%s and %s have the same priority",
595 altq
->qname
, pa
->qname
);
604 check_commit_priq(int dev
, int opts
, struct pf_altq
*pa
)
606 struct pf_altq
*altq
;
611 * check if priq has one default class for this interface
614 TAILQ_FOREACH(altq
, &altqs
, entries
) {
615 if (strncmp(altq
->ifname
, pa
->ifname
, IFNAMSIZ
) != 0)
617 if (altq
->qname
[0] == 0) /* this is for interface */
619 if (altq
->pq_u
.priq_opts
.flags
& PRCF_DEFAULTCLASS
)
622 if (default_class
!= 1) {
623 warnx("should have one default queue on %s", pa
->ifname
);
630 print_priq_opts(const struct pf_altq
*a
)
632 const struct priq_opts
*opts
;
634 opts
= &a
->pq_u
.priq_opts
;
638 if (opts
->flags
& PRCF_RED
)
640 if (opts
->flags
& PRCF_ECN
)
642 if (opts
->flags
& PRCF_RIO
)
644 if (opts
->flags
& PRCF_CLEARDSCP
)
645 printf(" cleardscp");
646 if (opts
->flags
& PRCF_DEFAULTCLASS
)
656 * HFSC support functions
659 eval_pfqueue_hfsc(struct pfctl
*pf
, struct pf_altq
*pa
)
661 struct pf_altq
*altq
, *parent
;
662 struct hfsc_opts
*opts
;
663 struct service_curve sc
;
665 opts
= &pa
->pq_u
.hfsc_opts
;
667 if (pa
->parent
[0] == 0) {
669 opts
->lssc_m1
= pa
->ifbandwidth
;
670 opts
->lssc_m2
= pa
->ifbandwidth
;
678 /* if link_share is not specified, use bandwidth */
679 if (opts
->lssc_m2
== 0)
680 opts
->lssc_m2
= pa
->bandwidth
;
682 if ((opts
->rtsc_m1
> 0 && opts
->rtsc_m2
== 0) ||
683 (opts
->lssc_m1
> 0 && opts
->lssc_m2
== 0) ||
684 (opts
->ulsc_m1
> 0 && opts
->ulsc_m2
== 0)) {
685 warnx("m2 is zero for %s", pa
->qname
);
689 if ((opts
->rtsc_m1
< opts
->rtsc_m2
&& opts
->rtsc_m1
!= 0) ||
690 (opts
->lssc_m1
< opts
->lssc_m2
&& opts
->lssc_m1
!= 0) ||
691 (opts
->ulsc_m1
< opts
->ulsc_m2
&& opts
->ulsc_m1
!= 0)) {
692 warnx("m1 must be zero for convex curve: %s", pa
->qname
);
698 * for the real-time service curve, the sum of the service curves
699 * should not exceed 80% of the interface bandwidth. 20% is reserved
700 * not to over-commit the actual interface bandwidth.
701 * for the linkshare service curve, the sum of the child service
702 * curve should not exceed the parent service curve.
703 * for the upper-limit service curve, the assigned bandwidth should
704 * be smaller than the interface bandwidth, and the upper-limit should
705 * be larger than the real-time service curve when both are defined.
707 parent
= qname_to_pfaltq(pa
->parent
, pa
->ifname
);
709 errx(1, "parent %s not found for %s", pa
->parent
, pa
->qname
);
711 TAILQ_FOREACH(altq
, &altqs
, entries
) {
712 if (strncmp(altq
->ifname
, pa
->ifname
, IFNAMSIZ
) != 0)
714 if (altq
->qname
[0] == 0) /* this is for interface */
717 /* if the class has a real-time service curve, add it. */
718 if (opts
->rtsc_m2
!= 0 && altq
->pq_u
.hfsc_opts
.rtsc_m2
!= 0) {
719 sc
.m1
= altq
->pq_u
.hfsc_opts
.rtsc_m1
;
720 sc
.d
= altq
->pq_u
.hfsc_opts
.rtsc_d
;
721 sc
.m2
= altq
->pq_u
.hfsc_opts
.rtsc_m2
;
722 gsc_add_sc(&rtsc
, &sc
);
725 if (strncmp(altq
->parent
, pa
->parent
, PF_QNAME_SIZE
) != 0)
728 /* if the class has a linkshare service curve, add it. */
729 if (opts
->lssc_m2
!= 0 && altq
->pq_u
.hfsc_opts
.lssc_m2
!= 0) {
730 sc
.m1
= altq
->pq_u
.hfsc_opts
.lssc_m1
;
731 sc
.d
= altq
->pq_u
.hfsc_opts
.lssc_d
;
732 sc
.m2
= altq
->pq_u
.hfsc_opts
.lssc_m2
;
733 gsc_add_sc(&lssc
, &sc
);
737 /* check the real-time service curve. reserve 20% of interface bw */
738 if (opts
->rtsc_m2
!= 0) {
739 /* add this queue to the sum */
740 sc
.m1
= opts
->rtsc_m1
;
742 sc
.m2
= opts
->rtsc_m2
;
743 gsc_add_sc(&rtsc
, &sc
);
744 /* compare the sum with 80% of the interface */
747 sc
.m2
= pa
->ifbandwidth
/ 100 * 80;
748 if (!is_gsc_under_sc(&rtsc
, &sc
)) {
749 warnx("real-time sc exceeds 80%% of the interface "
750 "bandwidth (%s)", rate2str((double)sc
.m2
));
755 /* check the linkshare service curve. */
756 if (opts
->lssc_m2
!= 0) {
757 /* add this queue to the child sum */
758 sc
.m1
= opts
->lssc_m1
;
760 sc
.m2
= opts
->lssc_m2
;
761 gsc_add_sc(&lssc
, &sc
);
762 /* compare the sum of the children with parent's sc */
763 sc
.m1
= parent
->pq_u
.hfsc_opts
.lssc_m1
;
764 sc
.d
= parent
->pq_u
.hfsc_opts
.lssc_d
;
765 sc
.m2
= parent
->pq_u
.hfsc_opts
.lssc_m2
;
766 if (!is_gsc_under_sc(&lssc
, &sc
)) {
767 warnx("linkshare sc exceeds parent's sc");
772 /* check the upper-limit service curve. */
773 if (opts
->ulsc_m2
!= 0) {
774 if (opts
->ulsc_m1
> pa
->ifbandwidth
||
775 opts
->ulsc_m2
> pa
->ifbandwidth
) {
776 warnx("upper-limit larger than interface bandwidth");
779 if (opts
->rtsc_m2
!= 0 && opts
->rtsc_m2
> opts
->ulsc_m2
) {
780 warnx("upper-limit sc smaller than real-time sc");
797 check_commit_hfsc(int dev
, int opts
, struct pf_altq
*pa
)
799 struct pf_altq
*altq
, *def
= NULL
;
803 /* check if hfsc has one default queue for this interface */
805 TAILQ_FOREACH(altq
, &altqs
, entries
) {
806 if (strncmp(altq
->ifname
, pa
->ifname
, IFNAMSIZ
) != 0)
808 if (altq
->qname
[0] == 0) /* this is for interface */
810 if (altq
->parent
[0] == 0) /* dummy root */
812 if (altq
->pq_u
.hfsc_opts
.flags
& HFCF_DEFAULTCLASS
) {
817 if (default_class
!= 1) {
818 warnx("should have one default queue on %s", pa
->ifname
);
821 /* make sure the default queue is a leaf */
822 TAILQ_FOREACH(altq
, &altqs
, entries
) {
823 if (strncmp(altq
->ifname
, pa
->ifname
, IFNAMSIZ
) != 0)
825 if (altq
->qname
[0] == 0) /* this is for interface */
827 if (strncmp(altq
->parent
, def
->qname
, PF_QNAME_SIZE
) == 0) {
828 warnx("default queue is not a leaf");
836 print_hfsc_opts(const struct pf_altq
*a
, const struct node_queue_opt
*qopts
)
838 const struct hfsc_opts
*opts
;
839 const struct node_hfsc_sc
*rtsc
, *lssc
, *ulsc
;
841 opts
= &a
->pq_u
.hfsc_opts
;
843 rtsc
= lssc
= ulsc
= NULL
;
845 rtsc
= &qopts
->data
.hfsc_opts
.realtime
;
846 lssc
= &qopts
->data
.hfsc_opts
.linkshare
;
847 ulsc
= &qopts
->data
.hfsc_opts
.upperlimit
;
850 if (opts
->flags
|| opts
->rtsc_m2
!= 0 || opts
->ulsc_m2
!= 0 ||
851 (opts
->lssc_m2
!= 0 && (opts
->lssc_m2
!= a
->bandwidth
||
852 opts
->lssc_d
!= 0))) {
854 if (opts
->flags
& HFCF_RED
)
856 if (opts
->flags
& HFCF_ECN
)
858 if (opts
->flags
& HFCF_RIO
)
860 if (opts
->flags
& HFCF_CLEARDSCP
)
861 printf(" cleardscp");
862 if (opts
->flags
& HFCF_DEFAULTCLASS
)
864 if (opts
->rtsc_m2
!= 0)
865 print_hfsc_sc("realtime", opts
->rtsc_m1
, opts
->rtsc_d
,
866 opts
->rtsc_m2
, rtsc
);
867 if (opts
->lssc_m2
!= 0 && (opts
->lssc_m2
!= a
->bandwidth
||
869 print_hfsc_sc("linkshare", opts
->lssc_m1
, opts
->lssc_d
,
870 opts
->lssc_m2
, lssc
);
871 if (opts
->ulsc_m2
!= 0)
872 print_hfsc_sc("upperlimit", opts
->ulsc_m1
, opts
->ulsc_d
,
873 opts
->ulsc_m2
, ulsc
);
882 * admission control using generalized service curve
885 #define INFINITY HUGE_VAL /* positive infinity defined in <math.h> */
886 #endif /* !__NetBSD__ */
888 /* add a new service curve to a generalized service curve */
890 gsc_add_sc(struct gen_sc
*gsc
, struct service_curve
*sc
)
895 gsc_add_seg(gsc
, 0.0, 0.0, (double)sc
->d
, (double)sc
->m1
);
896 gsc_add_seg(gsc
, (double)sc
->d
, 0.0, INFINITY
, (double)sc
->m2
);
900 * check whether all points of a generalized service curve have
901 * their y-coordinates no larger than a given two-piece linear
905 is_gsc_under_sc(struct gen_sc
*gsc
, struct service_curve
*sc
)
907 struct segment
*s
, *last
, *end
;
910 if (is_sc_null(sc
)) {
913 LIST_FOREACH(s
, gsc
, _next
) {
920 * gsc has a dummy entry at the end with x = INFINITY.
921 * loop through up to this dummy entry.
923 end
= gsc_getentry(gsc
, INFINITY
);
927 for (s
= LIST_FIRST(gsc
); s
!= end
; s
= LIST_NEXT(s
, _next
)) {
928 if (s
->y
> sc_x2y(sc
, s
->x
))
932 /* last now holds the real last segment */
935 if (last
->m
> sc
->m2
)
937 if (last
->x
< sc
->d
&& last
->m
> sc
->m1
) {
938 y
= last
->y
+ (sc
->d
- last
->x
) * last
->m
;
939 if (y
> sc_x2y(sc
, sc
->d
))
946 gsc_destroy(struct gen_sc
*gsc
)
950 while ((s
= LIST_FIRST(gsc
)) != NULL
) {
951 LIST_REMOVE(s
, _next
);
957 * return a segment entry starting at x.
958 * if gsc has no entry starting at x, a new entry is created at x.
960 static struct segment
*
961 gsc_getentry(struct gen_sc
*gsc
, double x
)
963 struct segment
*new, *prev
, *s
;
966 LIST_FOREACH(s
, gsc
, _next
) {
968 return (s
); /* matching entry found */
975 /* we have to create a new entry */
976 if ((new = calloc(1, sizeof(struct segment
))) == NULL
)
980 if (x
== INFINITY
|| s
== NULL
)
982 else if (s
->x
== INFINITY
)
987 /* insert the new entry at the head of the list */
990 LIST_INSERT_HEAD(gsc
, new, _next
);
993 * the start point intersects with the segment pointed by
994 * prev. divide prev into 2 segments
1003 prev
->d
= x
- prev
->x
;
1004 new->y
= prev
->d
* prev
->m
+ prev
->y
;
1007 LIST_INSERT_AFTER(prev
, new, _next
);
1012 /* add a segment to a generalized service curve */
1014 gsc_add_seg(struct gen_sc
*gsc
, double x
, double y
, double d
, double m
)
1016 struct segment
*start
, *end
, *s
;
1023 start
= gsc_getentry(gsc
, x
);
1024 end
= gsc_getentry(gsc
, x2
);
1025 if (start
== NULL
|| end
== NULL
)
1028 for (s
= start
; s
!= end
; s
= LIST_NEXT(s
, _next
)) {
1030 s
->y
+= y
+ (s
->x
- x
) * m
;
1033 end
= gsc_getentry(gsc
, INFINITY
);
1034 for (; s
!= end
; s
= LIST_NEXT(s
, _next
)) {
1041 /* get y-projection of a service curve */
1043 sc_x2y(struct service_curve
*sc
, double x
)
1047 if (x
<= (double)sc
->d
)
1048 /* y belongs to the 1st segment */
1049 y
= x
* (double)sc
->m1
;
1051 /* y belongs to the 2nd segment */
1052 y
= (double)sc
->d
* (double)sc
->m1
1053 + (x
- (double)sc
->d
) * (double)sc
->m2
;
1061 #define RATESTR_MAX 16
1064 rate2str(double rate
)
1067 static char r2sbuf
[R2S_BUFS
][RATESTR_MAX
]; /* ring bufer */
1070 static const char unit
[] = " KMG";
1072 buf
= r2sbuf
[idx
++];
1073 if (idx
== R2S_BUFS
)
1076 for (i
= 0; rate
>= 1000 && i
<= 3; i
++)
1079 if ((int)(rate
* 100) % 100)
1080 snprintf(buf
, RATESTR_MAX
, "%.2f%cb", rate
, unit
[i
]);
1082 snprintf(buf
, RATESTR_MAX
, "%d%cb", (int)rate
, unit
[i
]);
1088 getifspeed(char *ifname
)
1092 struct ifdatareq ifdr
;
1093 struct if_data
*ifrdat
;
1095 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1096 err(1, "getifspeed: socket");
1097 memset(&ifdr
, 0, sizeof(ifdr
));
1098 if (strlcpy(ifdr
.ifdr_name
, ifname
, sizeof(ifdr
.ifdr_name
)) >=
1099 sizeof(ifdr
.ifdr_name
))
1100 errx(1, "getifspeed: strlcpy");
1101 if (ioctl(s
, SIOCGIFDATA
, &ifdr
) == -1)
1102 err(1, "getifspeed: SIOCGIFDATA");
1103 ifrdat
= &ifdr
.ifdr_data
;
1105 err(1, "getifspeed: close");
1106 return ((u_int32_t
)ifrdat
->ifi_baudrate
);
1110 struct if_data ifrdat
;
1112 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1114 bzero(&ifr
, sizeof(ifr
));
1115 if (strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
)) >=
1116 sizeof(ifr
.ifr_name
))
1117 errx(1, "getifspeed: strlcpy");
1118 ifr
.ifr_data
= (caddr_t
)&ifrdat
;
1119 if (ioctl(s
, SIOCGIFDATA
, (caddr_t
)&ifr
) == -1)
1120 err(1, "SIOCGIFDATA");
1123 return ((u_int32_t
)ifrdat
.ifi_baudrate
);
1124 #endif /* !__NetBSD__ */
1128 getifmtu(char *ifname
)
1133 if ((s
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1135 bzero(&ifr
, sizeof(ifr
));
1136 if (strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
)) >=
1137 sizeof(ifr
.ifr_name
))
1138 errx(1, "getifmtu: strlcpy");
1139 if (ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
) == -1)
1140 err(1, "SIOCGIFMTU");
1143 if (ifr
.ifr_mtu
> 0)
1144 return (ifr
.ifr_mtu
);
1146 warnx("could not get mtu for %s, assuming 1500", ifname
);
1152 eval_queue_opts(struct pf_altq
*pa
, struct node_queue_opt
*opts
,
1157 switch (pa
->scheduler
) {
1159 pa
->pq_u
.cbq_opts
= opts
->data
.cbq_opts
;
1162 pa
->pq_u
.priq_opts
= opts
->data
.priq_opts
;
1165 pa
->pq_u
.hfsc_opts
.flags
= opts
->data
.hfsc_opts
.flags
;
1166 if (opts
->data
.hfsc_opts
.linkshare
.used
) {
1167 pa
->pq_u
.hfsc_opts
.lssc_m1
=
1168 eval_bwspec(&opts
->data
.hfsc_opts
.linkshare
.m1
,
1170 pa
->pq_u
.hfsc_opts
.lssc_m2
=
1171 eval_bwspec(&opts
->data
.hfsc_opts
.linkshare
.m2
,
1173 pa
->pq_u
.hfsc_opts
.lssc_d
=
1174 opts
->data
.hfsc_opts
.linkshare
.d
;
1176 if (opts
->data
.hfsc_opts
.realtime
.used
) {
1177 pa
->pq_u
.hfsc_opts
.rtsc_m1
=
1178 eval_bwspec(&opts
->data
.hfsc_opts
.realtime
.m1
,
1180 pa
->pq_u
.hfsc_opts
.rtsc_m2
=
1181 eval_bwspec(&opts
->data
.hfsc_opts
.realtime
.m2
,
1183 pa
->pq_u
.hfsc_opts
.rtsc_d
=
1184 opts
->data
.hfsc_opts
.realtime
.d
;
1186 if (opts
->data
.hfsc_opts
.upperlimit
.used
) {
1187 pa
->pq_u
.hfsc_opts
.ulsc_m1
=
1188 eval_bwspec(&opts
->data
.hfsc_opts
.upperlimit
.m1
,
1190 pa
->pq_u
.hfsc_opts
.ulsc_m2
=
1191 eval_bwspec(&opts
->data
.hfsc_opts
.upperlimit
.m2
,
1193 pa
->pq_u
.hfsc_opts
.ulsc_d
=
1194 opts
->data
.hfsc_opts
.upperlimit
.d
;
1198 warnx("eval_queue_opts: unknown scheduler type %u",
1208 eval_bwspec(struct node_queue_bw
*bw
, u_int32_t ref_bw
)
1210 if (bw
->bw_absolute
> 0)
1211 return (bw
->bw_absolute
);
1213 if (bw
->bw_percent
> 0)
1214 return (ref_bw
/ 100 * bw
->bw_percent
);
1220 print_hfsc_sc(const char *scname
, u_int m1
, u_int d
, u_int m2
,
1221 const struct node_hfsc_sc
*sc
)
1223 printf(" %s", scname
);
1227 if (sc
!= NULL
&& sc
->m1
.bw_percent
> 0)
1228 printf("%u%%", sc
->m1
.bw_percent
);
1230 printf("%s", rate2str((double)m1
));
1234 if (sc
!= NULL
&& sc
->m2
.bw_percent
> 0)
1235 printf(" %u%%", sc
->m2
.bw_percent
);
1237 printf(" %s", rate2str((double)m2
));