Fix buildworld.
[dragonfly.git] / usr.sbin / pfctl / pfctl_altq.c
1 /*      $OpenBSD: pfctl_altq.c,v 1.94 2008/07/25 17:43:44 martynas Exp $        */
2
3 /*
4  * Copyright (c) 2002
5  *      Sony Computer Science Laboratories Inc.
6  * Copyright (c) 2002, 2003 Henning Brauer <henning@openbsd.org>
7  *
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.
11  *
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.
19  */
20
21 #define _KERNEL_STRUCTURES
22 #include <sys/param.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #include <sys/sysctl.h>
26
27 #include <net/if.h>
28 #include <net/if_mib.h>
29 #include <netinet/in.h>
30 #include <net/pf/pfvar.h>
31
32 #include <err.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <math.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_priq.h>
44 #include <net/altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
46
47 #include "pfctl_parser.h"
48 #include "pfctl.h"
49
50 #define is_sc_null(sc)  (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
51
52 TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
53 LIST_HEAD(gen_sc, segment) rtsc, lssc;
54
55 struct pf_altq  *qname_to_pfaltq(const char *, const char *);
56 u_int32_t        qname_to_qid(const char *);
57
58 static int      eval_pfqueue_cbq(struct pfctl *, struct pf_altq *);
59 static int      cbq_compute_idletime(struct pfctl *, struct pf_altq *);
60 static int      check_commit_cbq(int, int, struct pf_altq *);
61 static int      print_cbq_opts(const struct pf_altq *);
62
63 static int      eval_pfqueue_priq(struct pfctl *, struct pf_altq *);
64 static int      check_commit_priq(int, int, struct pf_altq *);
65 static int      print_priq_opts(const struct pf_altq *);
66
67 static int      eval_pfqueue_hfsc(struct pfctl *, struct pf_altq *);
68 static int      check_commit_hfsc(int, int, struct pf_altq *);
69 static int      print_hfsc_opts(const struct pf_altq *,
70                     const struct node_queue_opt *);
71
72 static int      eval_pfqueue_fairq(struct pfctl *, struct pf_altq *);
73 static int      print_fairq_opts(const struct pf_altq *,
74                     const struct node_queue_opt *);
75 static int      check_commit_fairq(int, int, struct pf_altq *);
76
77 static void              gsc_add_sc(struct gen_sc *, struct service_curve *);
78 static int               is_gsc_under_sc(struct gen_sc *,
79                              struct service_curve *);
80 static void              gsc_destroy(struct gen_sc *);
81 static struct segment   *gsc_getentry(struct gen_sc *, double);
82 static int               gsc_add_seg(struct gen_sc *, double, double, double,
83                              double);
84 static double            sc_x2y(struct service_curve *, double);
85
86 u_int32_t        getifspeed(const char *);
87 u_long           getifmtu(char *);
88 int              eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
89                      u_int32_t);
90 u_int32_t        eval_bwspec(struct node_queue_bw *, u_int32_t);
91 void             print_hfsc_sc(const char *, u_int, u_int, u_int,
92                      const struct node_hfsc_sc *);
93 void             print_fairq_sc(const char *, u_int, u_int, u_int,
94                      const struct node_fairq_sc *);
95
96 void
97 pfaltq_store(struct pf_altq *a)
98 {
99         struct pf_altq  *altq;
100
101         if ((altq = malloc(sizeof(*altq))) == NULL)
102                 err(1, "malloc");
103         memcpy(altq, a, sizeof(struct pf_altq));
104         TAILQ_INSERT_TAIL(&altqs, altq, entries);
105 }
106
107 struct pf_altq *
108 pfaltq_lookup(const char *ifname)
109 {
110         struct pf_altq  *altq;
111
112         TAILQ_FOREACH(altq, &altqs, entries) {
113                 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
114                     altq->qname[0] == 0)
115                         return (altq);
116         }
117         return (NULL);
118 }
119
120 struct pf_altq *
121 qname_to_pfaltq(const char *qname, const char *ifname)
122 {
123         struct pf_altq  *altq;
124
125         TAILQ_FOREACH(altq, &altqs, entries) {
126                 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
127                     strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
128                         return (altq);
129         }
130         return (NULL);
131 }
132
133 u_int32_t
134 qname_to_qid(const char *qname)
135 {
136         struct pf_altq  *altq;
137
138         /*
139          * We guarantee that same named queues on different interfaces
140          * have the same qid, so we do NOT need to limit matching on
141          * one interface!
142          */
143
144         TAILQ_FOREACH(altq, &altqs, entries) {
145                 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
146                         return (altq->qid);
147         }
148         return (0);
149 }
150
151 void
152 print_altq(const struct pf_altq *a, unsigned int level,
153     struct node_queue_bw *bw, struct node_queue_opt *qopts)
154 {
155         if (a->qname[0] != 0) {
156                 print_queue(a, level, bw, 1, qopts);
157                 return;
158         }
159
160         printf("altq on %s ", a->ifname);
161
162         switch (a->scheduler) {
163         case ALTQT_CBQ:
164                 if (!print_cbq_opts(a))
165                         printf("cbq ");
166                 break;
167         case ALTQT_PRIQ:
168                 if (!print_priq_opts(a))
169                         printf("priq ");
170                 break;
171         case ALTQT_HFSC:
172                 if (!print_hfsc_opts(a, qopts))
173                         printf("hfsc ");
174                 break;
175         case ALTQT_FAIRQ:
176                 if (!print_fairq_opts(a, qopts))
177                         printf("hfsc ");
178                 break;
179         }
180
181         if (bw != NULL && bw->bw_percent > 0) {
182                 if (bw->bw_percent < 100)
183                         printf("bandwidth %u%% ", bw->bw_percent);
184         } else
185                 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
186
187         if (a->qlimit != DEFAULT_QLIMIT)
188                 printf("qlimit %u ", a->qlimit);
189         printf("tbrsize %u ", a->tbrsize);
190 }
191
192 void
193 print_queue(const struct pf_altq *a, unsigned int level,
194     struct node_queue_bw *bw, int print_interface,
195     struct node_queue_opt *qopts)
196 {
197         unsigned int    i;
198
199         printf("queue ");
200         for (i = 0; i < level; ++i)
201                 printf(" ");
202         printf("%s ", a->qname);
203         if (print_interface)
204                 printf("on %s ", a->ifname);
205         if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC ||
206             a->scheduler == ALTQT_FAIRQ) {
207                 if (bw != NULL && bw->bw_percent > 0) {
208                         if (bw->bw_percent < 100)
209                                 printf("bandwidth %u%% ", bw->bw_percent);
210                 } else
211                         printf("bandwidth %s ", rate2str((double)a->bandwidth));
212         }
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) {
218         case ALTQT_CBQ:
219                 print_cbq_opts(a);
220                 break;
221         case ALTQT_PRIQ:
222                 print_priq_opts(a);
223                 break;
224         case ALTQT_HFSC:
225                 print_hfsc_opts(a, qopts);
226                 break;
227         case ALTQT_FAIRQ:
228                 print_fairq_opts(a, qopts);
229                 break;
230         }
231 }
232
233 /*
234  * eval_pfaltq computes the discipline parameters.
235  */
236 int
237 eval_pfaltq(struct pfctl *pf __unused, struct pf_altq *pa, struct node_queue_bw *bw,
238     struct node_queue_opt *opts)
239 {
240         u_int   rate, size, errors = 0;
241
242         if (bw->bw_absolute > 0)
243                 pa->ifbandwidth = bw->bw_absolute;
244         else
245                 if ((rate = getifspeed(pa->ifname)) == 0) {
246                         fprintf(stderr, "interface %s does not know its bandwidth, "
247                             "please specify an absolute bandwidth\n",
248                             pa->ifname);
249                         errors++;
250                 } else if ((pa->ifbandwidth = eval_bwspec(bw, rate)) == 0)
251                         pa->ifbandwidth = rate;
252
253         errors += eval_queue_opts(pa, opts, pa->ifbandwidth);
254
255         /* if tbrsize is not specified, use heuristics */
256         if (pa->tbrsize == 0) {
257                 rate = pa->ifbandwidth;
258                 if (rate <= 1 * 1000 * 1000)
259                         size = 1;
260                 else if (rate <= 10 * 1000 * 1000)
261                         size = 4;
262                 else if (rate <= 200 * 1000 * 1000)
263                         size = 8;
264                 else
265                         size = 24;
266                 size = size * getifmtu(pa->ifname);
267                 if (size > 0xffff)
268                         size = 0xffff;
269                 pa->tbrsize = size;
270         }
271         return (errors);
272 }
273
274 /*
275  * check_commit_altq does consistency check for each interface
276  */
277 int
278 check_commit_altq(int dev, int opts)
279 {
280         struct pf_altq  *altq;
281         int              error = 0;
282
283         /* call the discipline check for each interface. */
284         TAILQ_FOREACH(altq, &altqs, entries) {
285                 if (altq->qname[0] == 0) {
286                         switch (altq->scheduler) {
287                         case ALTQT_CBQ:
288                                 error = check_commit_cbq(dev, opts, altq);
289                                 break;
290                         case ALTQT_PRIQ:
291                                 error = check_commit_priq(dev, opts, altq);
292                                 break;
293                         case ALTQT_HFSC:
294                                 error = check_commit_hfsc(dev, opts, altq);
295                                 break;
296                         case ALTQT_FAIRQ:
297                                 error = check_commit_fairq(dev, opts, altq);
298                                 break;
299                         default:
300                                 break;
301                         }
302                 }
303         }
304         return (error);
305 }
306
307 /*
308  * eval_pfqueue computes the queue parameters.
309  */
310 int
311 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
312     struct node_queue_opt *opts)
313 {
314         /* should be merged with expand_queue */
315         struct pf_altq  *if_pa, *parent, *altq;
316         u_int32_t        bwsum;
317         int              error = 0;
318
319         /* find the corresponding interface and copy fields used by queues */
320         if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
321                 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
322                 return (1);
323         }
324         pa->scheduler = if_pa->scheduler;
325         pa->ifbandwidth = if_pa->ifbandwidth;
326
327         if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
328                 fprintf(stderr, "queue %s already exists on interface %s\n",
329                     pa->qname, pa->ifname);
330                 return (1);
331         }
332         pa->qid = qname_to_qid(pa->qname);
333
334         parent = NULL;
335         if (pa->parent[0] != 0) {
336                 parent = qname_to_pfaltq(pa->parent, pa->ifname);
337                 if (parent == NULL) {
338                         fprintf(stderr, "parent %s not found for %s\n",
339                             pa->parent, pa->qname);
340                         return (1);
341                 }
342                 pa->parent_qid = parent->qid;
343         }
344         if (pa->qlimit == 0)
345                 pa->qlimit = DEFAULT_QLIMIT;
346
347         if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC ||
348                 pa->scheduler == ALTQT_FAIRQ) {
349                 pa->bandwidth = eval_bwspec(bw,
350                     parent == NULL ? 0 : parent->bandwidth);
351
352                 if (pa->bandwidth > pa->ifbandwidth) {
353                         fprintf(stderr, "bandwidth for %s higher than "
354                             "interface\n", pa->qname);
355                         return (1);
356                 }
357                 /* check the sum of the child bandwidth is under parent's */
358                 if (parent != NULL) {
359                         if (pa->bandwidth > parent->bandwidth) {
360                                 warnx("bandwidth for %s higher than parent",
361                                     pa->qname);
362                                 return (1);
363                         }
364                         bwsum = 0;
365                         TAILQ_FOREACH(altq, &altqs, entries) {
366                                 if (strncmp(altq->ifname, pa->ifname,
367                                     IFNAMSIZ) == 0 &&
368                                     altq->qname[0] != 0 &&
369                                     strncmp(altq->parent, pa->parent,
370                                     PF_QNAME_SIZE) == 0)
371                                         bwsum += altq->bandwidth;
372                         }
373                         bwsum += pa->bandwidth;
374                         if (bwsum > parent->bandwidth) {
375                                 warnx("the sum of the child bandwidth higher"
376                                     " than parent \"%s\"", parent->qname);
377                         }
378                 }
379         }
380
381         if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
382                 return (1);
383
384         switch (pa->scheduler) {
385         case ALTQT_CBQ:
386                 error = eval_pfqueue_cbq(pf, pa);
387                 break;
388         case ALTQT_PRIQ:
389                 error = eval_pfqueue_priq(pf, pa);
390                 break;
391         case ALTQT_HFSC:
392                 error = eval_pfqueue_hfsc(pf, pa);
393                 break;
394         case ALTQT_FAIRQ:
395                 error = eval_pfqueue_fairq(pf, pa);
396                 break;
397         default:
398                 break;
399         }
400         return (error);
401 }
402
403 /*
404  * CBQ support functions
405  */
406 #define RM_FILTER_GAIN  5       /* log2 of gain, e.g., 5 => 31/32 */
407 #define RM_NS_PER_SEC   (1000000000)
408
409 static int
410 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
411 {
412         struct cbq_opts *opts;
413         u_int            ifmtu;
414
415         if (pa->priority >= CBQ_MAXPRI) {
416                 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
417                 return (-1);
418         }
419
420         ifmtu = getifmtu(pa->ifname);
421         opts = &pa->pq_u.cbq_opts;
422
423         if (opts->pktsize == 0) {       /* use default */
424                 opts->pktsize = ifmtu;
425                 if (opts->pktsize > MCLBYTES)   /* do what TCP does */
426                         opts->pktsize &= ~MCLBYTES;
427         } else if (opts->pktsize > ifmtu)
428                 opts->pktsize = ifmtu;
429         if (opts->maxpktsize == 0)      /* use default */
430                 opts->maxpktsize = ifmtu;
431         else if (opts->maxpktsize > ifmtu)
432                 opts->pktsize = ifmtu;
433
434         if (opts->pktsize > opts->maxpktsize)
435                 opts->pktsize = opts->maxpktsize;
436
437         if (pa->parent[0] == 0)
438                 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
439
440         cbq_compute_idletime(pf, pa);
441         return (0);
442 }
443
444 /*
445  * compute ns_per_byte, maxidle, minidle, and offtime
446  */
447 static int
448 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
449 {
450         struct cbq_opts *opts;
451         double           maxidle_s, maxidle, minidle;
452         double           offtime, nsPerByte, ifnsPerByte, ptime, cptime;
453         double           z, g, f, gton, gtom;
454         u_int            minburst, maxburst;
455
456         opts = &pa->pq_u.cbq_opts;
457         ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
458         minburst = opts->minburst;
459         maxburst = opts->maxburst;
460
461         if (pa->bandwidth == 0)
462                 f = 0.0001;     /* small enough? */
463         else
464                 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
465
466         nsPerByte = ifnsPerByte / f;
467         ptime = (double)opts->pktsize * ifnsPerByte;
468         cptime = ptime * (1.0 - f) / f;
469
470         if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
471                 /*
472                  * this causes integer overflow in kernel!
473                  * (bandwidth < 6Kbps when max_pkt_size=1500)
474                  */
475                 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0) {
476                         warnx("queue bandwidth must be larger than %s",
477                             rate2str(ifnsPerByte * (double)opts->maxpktsize /
478                             (double)INT_MAX * (double)pa->ifbandwidth));
479                         fprintf(stderr, "cbq: queue %s is too slow!\n",
480                             pa->qname);
481                 }
482                 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
483         }
484
485         if (maxburst == 0) {  /* use default */
486                 if (cptime > 10.0 * 1000000)
487                         maxburst = 4;
488                 else
489                         maxburst = 16;
490         }
491         if (minburst == 0)  /* use default */
492                 minburst = 2;
493         if (minburst > maxburst)
494                 minburst = maxburst;
495
496         z = (double)(1 << RM_FILTER_GAIN);
497         g = (1.0 - 1.0 / z);
498         gton = pow(g, (double)maxburst);
499         gtom = pow(g, (double)(minburst-1));
500         maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
501         maxidle_s = (1.0 - g);
502         if (maxidle > maxidle_s)
503                 maxidle = ptime * maxidle;
504         else
505                 maxidle = ptime * maxidle_s;
506         offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
507         minidle = -((double)opts->maxpktsize * (double)nsPerByte);
508
509         /* scale parameters */
510         maxidle = ((maxidle * 8.0) / nsPerByte) *
511             pow(2.0, (double)RM_FILTER_GAIN);
512         offtime = (offtime * 8.0) / nsPerByte *
513             pow(2.0, (double)RM_FILTER_GAIN);
514         minidle = ((minidle * 8.0) / nsPerByte) *
515             pow(2.0, (double)RM_FILTER_GAIN);
516
517         maxidle = maxidle / 1000.0;
518         offtime = offtime / 1000.0;
519         minidle = minidle / 1000.0;
520
521         opts->minburst = minburst;
522         opts->maxburst = maxburst;
523         opts->ns_per_byte = (u_int)nsPerByte;
524         opts->maxidle = (u_int)fabs(maxidle);
525         opts->minidle = (int)minidle;
526         opts->offtime = (u_int)fabs(offtime);
527
528         return (0);
529 }
530
531 static int
532 check_commit_cbq(int dev __unused, int opts __unused, struct pf_altq *pa)
533 {
534         struct pf_altq  *altq;
535         int              root_class, default_class;
536         int              error = 0;
537
538         /*
539          * check if cbq has one root queue and one default queue
540          * for this interface
541          */
542         root_class = default_class = 0;
543         TAILQ_FOREACH(altq, &altqs, entries) {
544                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
545                         continue;
546                 if (altq->qname[0] == 0)  /* this is for interface */
547                         continue;
548                 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
549                         root_class++;
550                 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
551                         default_class++;
552         }
553         if (root_class != 1) {
554                 warnx("should have one root queue on %s", pa->ifname);
555                 error++;
556         }
557         if (default_class != 1) {
558                 warnx("should have one default queue on %s", pa->ifname);
559                 error++;
560         }
561         return (error);
562 }
563
564 static int
565 print_cbq_opts(const struct pf_altq *a)
566 {
567         const struct cbq_opts   *opts;
568
569         opts = &a->pq_u.cbq_opts;
570         if (opts->flags) {
571                 printf("cbq(");
572                 if (opts->flags & CBQCLF_RED)
573                         printf(" red");
574                 if (opts->flags & CBQCLF_ECN)
575                         printf(" ecn");
576                 if (opts->flags & CBQCLF_RIO)
577                         printf(" rio");
578                 if (opts->flags & CBQCLF_CLEARDSCP)
579                         printf(" cleardscp");
580                 if (opts->flags & CBQCLF_BORROW)
581                         printf(" borrow");
582                 if (opts->flags & CBQCLF_WRR)
583                         printf(" wrr");
584                 if (opts->flags & CBQCLF_EFFICIENT)
585                         printf(" efficient");
586                 if (opts->flags & CBQCLF_ROOTCLASS)
587                         printf(" root");
588                 if (opts->flags & CBQCLF_DEFCLASS)
589                         printf(" default");
590                 printf(" ) ");
591
592                 return (1);
593         } else
594                 return (0);
595 }
596
597 /*
598  * PRIQ support functions
599  */
600 static int
601 eval_pfqueue_priq(struct pfctl *pf __unused, struct pf_altq *pa)
602 {
603         struct pf_altq  *altq;
604
605         if (pa->priority >= PRIQ_MAXPRI) {
606                 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
607                 return (-1);
608         }
609         /* the priority should be unique for the interface */
610         TAILQ_FOREACH(altq, &altqs, entries) {
611                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
612                     altq->qname[0] != 0 && altq->priority == pa->priority) {
613                         warnx("%s and %s have the same priority",
614                             altq->qname, pa->qname);
615                         return (-1);
616                 }
617         }
618
619         return (0);
620 }
621
622 static int
623 check_commit_priq(int dev __unused, int opts __unused, struct pf_altq *pa)
624 {
625         struct pf_altq  *altq;
626         int              default_class;
627         int              error = 0;
628
629         /*
630          * check if priq has one default class for this interface
631          */
632         default_class = 0;
633         TAILQ_FOREACH(altq, &altqs, entries) {
634                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
635                         continue;
636                 if (altq->qname[0] == 0)  /* this is for interface */
637                         continue;
638                 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
639                         default_class++;
640         }
641         if (default_class != 1) {
642                 warnx("should have one default queue on %s", pa->ifname);
643                 error++;
644         }
645         return (error);
646 }
647
648 static int
649 print_priq_opts(const struct pf_altq *a)
650 {
651         const struct priq_opts  *opts;
652
653         opts = &a->pq_u.priq_opts;
654
655         if (opts->flags) {
656                 printf("priq(");
657                 if (opts->flags & PRCF_RED)
658                         printf(" red");
659                 if (opts->flags & PRCF_ECN)
660                         printf(" ecn");
661                 if (opts->flags & PRCF_RIO)
662                         printf(" rio");
663                 if (opts->flags & PRCF_CLEARDSCP)
664                         printf(" cleardscp");
665                 if (opts->flags & PRCF_DEFAULTCLASS)
666                         printf(" default");
667                 printf(" ) ");
668
669                 return (1);
670         } else
671                 return (0);
672 }
673
674 /*
675  * HFSC support functions
676  */
677 static int
678 eval_pfqueue_hfsc(struct pfctl *pf __unused, struct pf_altq *pa)
679 {
680         struct pf_altq          *altq, *parent;
681         struct hfsc_opts        *opts;
682         struct service_curve     sc;
683
684         opts = &pa->pq_u.hfsc_opts;
685
686         if (pa->parent[0] == 0) {
687                 /* root queue */
688                 opts->lssc_m1 = pa->ifbandwidth;
689                 opts->lssc_m2 = pa->ifbandwidth;
690                 opts->lssc_d = 0;
691                 return (0);
692         }
693
694         LIST_INIT(&rtsc);
695         LIST_INIT(&lssc);
696
697         /* if link_share is not specified, use bandwidth */
698         if (opts->lssc_m2 == 0)
699                 opts->lssc_m2 = pa->bandwidth;
700
701         if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
702             (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
703             (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
704                 warnx("m2 is zero for %s", pa->qname);
705                 return (-1);
706         }
707
708         if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
709             (opts->lssc_m1 < opts->lssc_m2 && opts->lssc_m1 != 0) ||
710             (opts->ulsc_m1 < opts->ulsc_m2 && opts->ulsc_m1 != 0)) {
711                 warnx("m1 must be zero for convex curve: %s", pa->qname);
712                 return (-1);
713         }
714
715         /*
716          * admission control:
717          * for the real-time service curve, the sum of the service curves
718          * should not exceed 80% of the interface bandwidth.  20% is reserved
719          * not to over-commit the actual interface bandwidth.
720          * for the linkshare service curve, the sum of the child service
721          * curve should not exceed the parent service curve.
722          * for the upper-limit service curve, the assigned bandwidth should
723          * be smaller than the interface bandwidth, and the upper-limit should
724          * be larger than the real-time service curve when both are defined.
725          */
726         parent = qname_to_pfaltq(pa->parent, pa->ifname);
727         if (parent == NULL)
728                 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
729
730         TAILQ_FOREACH(altq, &altqs, entries) {
731                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
732                         continue;
733                 if (altq->qname[0] == 0)  /* this is for interface */
734                         continue;
735
736                 /* if the class has a real-time service curve, add it. */
737                 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
738                         sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
739                         sc.d = altq->pq_u.hfsc_opts.rtsc_d;
740                         sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
741                         gsc_add_sc(&rtsc, &sc);
742                 }
743
744                 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
745                         continue;
746
747                 /* if the class has a linkshare service curve, add it. */
748                 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
749                         sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
750                         sc.d = altq->pq_u.hfsc_opts.lssc_d;
751                         sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
752                         gsc_add_sc(&lssc, &sc);
753                 }
754         }
755
756         /* check the real-time service curve.  reserve 20% of interface bw */
757         if (opts->rtsc_m2 != 0) {
758                 /* add this queue to the sum */
759                 sc.m1 = opts->rtsc_m1;
760                 sc.d = opts->rtsc_d;
761                 sc.m2 = opts->rtsc_m2;
762                 gsc_add_sc(&rtsc, &sc);
763                 /* compare the sum with 80% of the interface */
764                 sc.m1 = 0;
765                 sc.d = 0;
766                 sc.m2 = pa->ifbandwidth / 100 * 80;
767                 if (!is_gsc_under_sc(&rtsc, &sc)) {
768                         warnx("real-time sc exceeds 80%% of the interface "
769                             "bandwidth (%s)", rate2str((double)sc.m2));
770                         goto err_ret;
771                 }
772         }
773
774         /* check the linkshare service curve. */
775         if (opts->lssc_m2 != 0) {
776                 /* add this queue to the child sum */
777                 sc.m1 = opts->lssc_m1;
778                 sc.d = opts->lssc_d;
779                 sc.m2 = opts->lssc_m2;
780                 gsc_add_sc(&lssc, &sc);
781                 /* compare the sum of the children with parent's sc */
782                 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
783                 sc.d = parent->pq_u.hfsc_opts.lssc_d;
784                 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
785                 if (!is_gsc_under_sc(&lssc, &sc)) {
786                         warnx("linkshare sc exceeds parent's sc");
787                         goto err_ret;
788                 }
789         }
790
791         /* check the upper-limit service curve. */
792         if (opts->ulsc_m2 != 0) {
793                 if (opts->ulsc_m1 > pa->ifbandwidth ||
794                     opts->ulsc_m2 > pa->ifbandwidth) {
795                         warnx("upper-limit larger than interface bandwidth");
796                         goto err_ret;
797                 }
798                 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
799                         warnx("upper-limit sc smaller than real-time sc");
800                         goto err_ret;
801                 }
802         }
803
804         gsc_destroy(&rtsc);
805         gsc_destroy(&lssc);
806
807         return (0);
808
809 err_ret:
810         gsc_destroy(&rtsc);
811         gsc_destroy(&lssc);
812         return (-1);
813 }
814
815 /*
816  * FAIRQ support functions
817  */
818 static int
819 eval_pfqueue_fairq(struct pfctl *pf __unused, struct pf_altq *pa)
820 {
821         struct pf_altq          *altq, *parent;
822         struct fairq_opts       *opts;
823         struct service_curve     sc;
824
825         opts = &pa->pq_u.fairq_opts;
826
827         if (pa->parent[0] == 0) {
828                 /* root queue */
829                 opts->lssc_m1 = pa->ifbandwidth;
830                 opts->lssc_m2 = pa->ifbandwidth;
831                 opts->lssc_d = 0;
832                 return (0);
833         }
834
835         LIST_INIT(&lssc);
836
837         /* if link_share is not specified, use bandwidth */
838         if (opts->lssc_m2 == 0)
839                 opts->lssc_m2 = pa->bandwidth;
840
841         /*
842          * admission control:
843          * for the real-time service curve, the sum of the service curves
844          * should not exceed 80% of the interface bandwidth.  20% is reserved
845          * not to over-commit the actual interface bandwidth.
846          * for the link-sharing service curve, the sum of the child service
847          * curve should not exceed the parent service curve.
848          * for the upper-limit service curve, the assigned bandwidth should
849          * be smaller than the interface bandwidth, and the upper-limit should
850          * be larger than the real-time service curve when both are defined.
851          */
852         parent = qname_to_pfaltq(pa->parent, pa->ifname);
853         if (parent == NULL)
854                 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
855
856         TAILQ_FOREACH(altq, &altqs, entries) {
857                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
858                         continue;
859                 if (altq->qname[0] == 0)  /* this is for interface */
860                         continue;
861
862                 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
863                         continue;
864
865                 /* if the class has a link-sharing service curve, add it. */
866                 if (opts->lssc_m2 != 0 && altq->pq_u.fairq_opts.lssc_m2 != 0) {
867                         sc.m1 = altq->pq_u.fairq_opts.lssc_m1;
868                         sc.d = altq->pq_u.fairq_opts.lssc_d;
869                         sc.m2 = altq->pq_u.fairq_opts.lssc_m2;
870                         gsc_add_sc(&lssc, &sc);
871                 }
872         }
873
874         /* check the link-sharing service curve. */
875         if (opts->lssc_m2 != 0) {
876                 sc.m1 = parent->pq_u.fairq_opts.lssc_m1;
877                 sc.d = parent->pq_u.fairq_opts.lssc_d;
878                 sc.m2 = parent->pq_u.fairq_opts.lssc_m2;
879                 if (!is_gsc_under_sc(&lssc, &sc)) {
880                         warnx("link-sharing sc exceeds parent's sc");
881                         goto err_ret;
882                 }
883         }
884
885         gsc_destroy(&lssc);
886
887         return (0);
888
889 err_ret:
890         gsc_destroy(&lssc);
891         return (-1);
892 }
893
894 static int
895 check_commit_hfsc(int dev __unused, int opts __unused, struct pf_altq *pa)
896 {
897         struct pf_altq  *altq, *def = NULL;
898         int              default_class;
899         int              error = 0;
900
901         /* check if hfsc has one default queue for this interface */
902         default_class = 0;
903         TAILQ_FOREACH(altq, &altqs, entries) {
904                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
905                         continue;
906                 if (altq->qname[0] == 0)  /* this is for interface */
907                         continue;
908                 if (altq->parent[0] == 0)  /* dummy root */
909                         continue;
910                 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
911                         default_class++;
912                         def = altq;
913                 }
914         }
915         if (default_class != 1) {
916                 warnx("should have one default queue on %s", pa->ifname);
917                 return (1);
918         }
919         /* make sure the default queue is a leaf */
920         TAILQ_FOREACH(altq, &altqs, entries) {
921                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
922                         continue;
923                 if (altq->qname[0] == 0)  /* this is for interface */
924                         continue;
925                 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
926                         warnx("default queue is not a leaf");
927                         error++;
928                 }
929         }
930         return (error);
931 }
932
933 static int
934 check_commit_fairq(int dev __unused, int opts __unused, struct pf_altq *pa)
935 {
936         struct pf_altq  *altq, *def = NULL;
937         int              default_class;
938         int              error = 0;
939
940         /* check if fairq has one default queue for this interface */
941         default_class = 0;
942         TAILQ_FOREACH(altq, &altqs, entries) {
943                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
944                         continue;
945                 if (altq->qname[0] == 0)  /* this is for interface */
946                         continue;
947                 if (altq->pq_u.fairq_opts.flags & FARF_DEFAULTCLASS) {
948                         default_class++;
949                         def = altq;
950                 }
951         }
952         if (default_class != 1) {
953                 warnx("should have one default queue on %s", pa->ifname);
954                 return (1);
955         }
956         /* make sure the default queue is a leaf */
957         TAILQ_FOREACH(altq, &altqs, entries) {
958                 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
959                         continue;
960                 if (altq->qname[0] == 0)  /* this is for interface */
961                         continue;
962                 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
963                         warnx("default queue is not a leaf");
964                         error++;
965                 }
966         }
967         return (error);
968 }
969
970 static int
971 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
972 {
973         const struct hfsc_opts          *opts;
974         const struct node_hfsc_sc       *loc_rtsc, *loc_lssc, *ulsc;
975
976         opts = &a->pq_u.hfsc_opts;
977         if (qopts == NULL)
978                 loc_rtsc = loc_lssc = ulsc = NULL;
979         else {
980                 loc_rtsc = &qopts->data.hfsc_opts.realtime;
981                 loc_lssc = &qopts->data.hfsc_opts.linkshare;
982                 ulsc = &qopts->data.hfsc_opts.upperlimit;
983         }
984
985         if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
986             (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
987             opts->lssc_d != 0))) {
988                 printf("hfsc(");
989                 if (opts->flags & HFCF_RED)
990                         printf(" red");
991                 if (opts->flags & HFCF_ECN)
992                         printf(" ecn");
993                 if (opts->flags & HFCF_RIO)
994                         printf(" rio");
995                 if (opts->flags & HFCF_CLEARDSCP)
996                         printf(" cleardscp");
997                 if (opts->flags & HFCF_DEFAULTCLASS)
998                         printf(" default");
999                 if (opts->rtsc_m2 != 0)
1000                         print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
1001                             opts->rtsc_m2, loc_rtsc);
1002                 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1003                     opts->lssc_d != 0))
1004                         print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1005                             opts->lssc_m2, loc_lssc);
1006                 if (opts->ulsc_m2 != 0)
1007                         print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
1008                             opts->ulsc_m2, ulsc);
1009                 printf(" ) ");
1010
1011                 return (1);
1012         } else
1013                 return (0);
1014 }
1015
1016 static int
1017 print_fairq_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
1018 {
1019         const struct fairq_opts         *opts;
1020         const struct node_fairq_sc      *loc_lssc;
1021
1022         opts = &a->pq_u.fairq_opts;
1023         if (qopts == NULL)
1024                 loc_lssc = NULL;
1025         else
1026                 loc_lssc = &qopts->data.fairq_opts.linkshare;
1027
1028         if (opts->flags ||
1029             (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1030             opts->lssc_d != 0))) {
1031                 printf("fairq(");
1032                 if (opts->flags & FARF_RED)
1033                         printf(" red");
1034                 if (opts->flags & FARF_ECN)
1035                         printf(" ecn");
1036                 if (opts->flags & FARF_RIO)
1037                         printf(" rio");
1038                 if (opts->flags & FARF_CLEARDSCP)
1039                         printf(" cleardscp");
1040                 if (opts->flags & FARF_DEFAULTCLASS)
1041                         printf(" default");
1042                 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
1043                     opts->lssc_d != 0))
1044                         print_fairq_sc("linkshare", opts->lssc_m1, opts->lssc_d,
1045                             opts->lssc_m2, loc_lssc);
1046                 printf(" ) ");
1047
1048                 return (1);
1049         } else
1050                 return (0);
1051 }
1052
1053 /*
1054  * admission control using generalized service curve
1055  */
1056
1057 /* add a new service curve to a generalized service curve */
1058 static void
1059 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
1060 {
1061         if (is_sc_null(sc))
1062                 return;
1063         if (sc->d != 0)
1064                 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
1065         gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
1066 }
1067
1068 /*
1069  * check whether all points of a generalized service curve have
1070  * their y-coordinates no larger than a given two-piece linear
1071  * service curve.
1072  */
1073 static int
1074 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
1075 {
1076         struct segment  *s, *last, *end;
1077         double           y;
1078
1079         if (is_sc_null(sc)) {
1080                 if (LIST_EMPTY(gsc))
1081                         return (1);
1082                 LIST_FOREACH(s, gsc, _next) {
1083                         if (s->m != 0)
1084                                 return (0);
1085                 }
1086                 return (1);
1087         }
1088         /*
1089          * gsc has a dummy entry at the end with x = INFINITY.
1090          * loop through up to this dummy entry.
1091          */
1092         end = gsc_getentry(gsc, INFINITY);
1093         if (end == NULL)
1094                 return (1);
1095         last = NULL;
1096         for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
1097                 if (s->y > sc_x2y(sc, s->x))
1098                         return (0);
1099                 last = s;
1100         }
1101         /* last now holds the real last segment */
1102         if (last == NULL)
1103                 return (1);
1104         if (last->m > sc->m2)
1105                 return (0);
1106         if (last->x < sc->d && last->m > sc->m1) {
1107                 y = last->y + (sc->d - last->x) * last->m;
1108                 if (y > sc_x2y(sc, sc->d))
1109                         return (0);
1110         }
1111         return (1);
1112 }
1113
1114 static void
1115 gsc_destroy(struct gen_sc *gsc)
1116 {
1117         struct segment  *s;
1118
1119         while ((s = LIST_FIRST(gsc)) != NULL) {
1120                 LIST_REMOVE(s, _next);
1121                 free(s);
1122         }
1123 }
1124
1125 /*
1126  * return a segment entry starting at x.
1127  * if gsc has no entry starting at x, a new entry is created at x.
1128  */
1129 static struct segment *
1130 gsc_getentry(struct gen_sc *gsc, double x)
1131 {
1132         struct segment  *new, *prev, *s;
1133
1134         prev = NULL;
1135         LIST_FOREACH(s, gsc, _next) {
1136                 if (s->x == x)
1137                         return (s);     /* matching entry found */
1138                 else if (s->x < x)
1139                         prev = s;
1140                 else
1141                         break;
1142         }
1143
1144         /* we have to create a new entry */
1145         if ((new = calloc(1, sizeof(struct segment))) == NULL)
1146                 return (NULL);
1147
1148         new->x = x;
1149         if (x == INFINITY || s == NULL)
1150                 new->d = 0;
1151         else if (s->x == INFINITY)
1152                 new->d = INFINITY;
1153         else
1154                 new->d = s->x - x;
1155         if (prev == NULL) {
1156                 /* insert the new entry at the head of the list */
1157                 new->y = 0;
1158                 new->m = 0;
1159                 LIST_INSERT_HEAD(gsc, new, _next);
1160         } else {
1161                 /*
1162                  * the start point intersects with the segment pointed by
1163                  * prev.  divide prev into 2 segments
1164                  */
1165                 if (x == INFINITY) {
1166                         prev->d = INFINITY;
1167                         if (prev->m == 0)
1168                                 new->y = prev->y;
1169                         else
1170                                 new->y = INFINITY;
1171                 } else {
1172                         prev->d = x - prev->x;
1173                         new->y = prev->d * prev->m + prev->y;
1174                 }
1175                 new->m = prev->m;
1176                 LIST_INSERT_AFTER(prev, new, _next);
1177         }
1178         return (new);
1179 }
1180
1181 /* add a segment to a generalized service curve */
1182 static int
1183 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1184 {
1185         struct segment  *start, *end, *s;
1186         double           x2;
1187
1188         if (d == INFINITY)
1189                 x2 = INFINITY;
1190         else
1191                 x2 = x + d;
1192         start = gsc_getentry(gsc, x);
1193         end = gsc_getentry(gsc, x2);
1194         if (start == NULL || end == NULL)
1195                 return (-1);
1196
1197         for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1198                 s->m += m;
1199                 s->y += y + (s->x - x) * m;
1200         }
1201
1202         end = gsc_getentry(gsc, INFINITY);
1203         for (; s != end; s = LIST_NEXT(s, _next)) {
1204                 s->y += m * d;
1205         }
1206
1207         return (0);
1208 }
1209
1210 /* get y-projection of a service curve */
1211 static double
1212 sc_x2y(struct service_curve *sc, double x)
1213 {
1214         double  y;
1215
1216         if (x <= (double)sc->d)
1217                 /* y belongs to the 1st segment */
1218                 y = x * (double)sc->m1;
1219         else
1220                 /* y belongs to the 2nd segment */
1221                 y = (double)sc->d * (double)sc->m1
1222                         + (x - (double)sc->d) * (double)sc->m2;
1223         return (y);
1224 }
1225
1226 /*
1227  * misc utilities
1228  */
1229 #define R2S_BUFS        8
1230 #define RATESTR_MAX     16
1231
1232 char *
1233 rate2str(double rate)
1234 {
1235         char            *buf;
1236         static char      r2sbuf[R2S_BUFS][RATESTR_MAX];  /* ring bufer */
1237         static int       idx = 0;
1238         int              i;
1239         static const char unit[] = " KMG";
1240
1241         buf = r2sbuf[idx++];
1242         if (idx == R2S_BUFS)
1243                 idx = 0;
1244
1245         for (i = 0; rate >= 1000 && i <= 3; i++)
1246                 rate /= 1000;
1247
1248         if ((int)(rate * 100) % 100)
1249                 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1250         else
1251                 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1252
1253         return (buf);
1254 }
1255
1256 u_int32_t
1257 getifspeed(const char *ifname)
1258 {
1259         size_t datalen;
1260         int idx;
1261         struct ifmibdata data;
1262         int name[] = {
1263                 CTL_NET,
1264                 PF_LINK,
1265                 NETLINK_GENERIC,
1266                 IFMIB_IFDATA,
1267                 0,
1268                 IFDATA_GENERAL
1269         };
1270
1271         if ((idx = (int)if_nametoindex(ifname)) == 0)
1272                 err(1, "getifspeed: if_nametoindex");
1273         name[4] = idx;
1274
1275         datalen = sizeof(data);
1276         if (sysctl(name, 6, &data, &datalen, NULL, 0))
1277                 err(1, "getifspeed: sysctl");
1278
1279         return(data.ifmd_data.ifi_baudrate);
1280 }
1281
1282 u_long
1283 getifmtu(char *ifname)
1284 {
1285         int             s;
1286         struct ifreq    ifr;
1287
1288         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1289                 err(1, "socket");
1290         bzero(&ifr, sizeof(ifr));
1291         if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1292             sizeof(ifr.ifr_name))
1293                 errx(1, "getifmtu: strlcpy");
1294         if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1295                 err(1, "SIOCGIFMTU");
1296         if (close(s))
1297                 err(1, "close");
1298         if (ifr.ifr_mtu > 0)
1299                 return (ifr.ifr_mtu);
1300         else {
1301                 warnx("could not get mtu for %s, assuming 1500", ifname);
1302                 return (1500);
1303         }
1304 }
1305
1306 int
1307 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1308     u_int32_t ref_bw)
1309 {
1310         int     errors = 0;
1311
1312         switch (pa->scheduler) {
1313         case ALTQT_CBQ:
1314                 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1315                 break;
1316         case ALTQT_PRIQ:
1317                 pa->pq_u.priq_opts = opts->data.priq_opts;
1318                 break;
1319         case ALTQT_HFSC:
1320                 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1321                 if (opts->data.hfsc_opts.linkshare.used) {
1322                         pa->pq_u.hfsc_opts.lssc_m1 =
1323                             eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1324                             ref_bw);
1325                         pa->pq_u.hfsc_opts.lssc_m2 =
1326                             eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1327                             ref_bw);
1328                         pa->pq_u.hfsc_opts.lssc_d =
1329                             opts->data.hfsc_opts.linkshare.d;
1330                 }
1331                 if (opts->data.hfsc_opts.realtime.used) {
1332                         pa->pq_u.hfsc_opts.rtsc_m1 =
1333                             eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1334                             ref_bw);
1335                         pa->pq_u.hfsc_opts.rtsc_m2 =
1336                             eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1337                             ref_bw);
1338                         pa->pq_u.hfsc_opts.rtsc_d =
1339                             opts->data.hfsc_opts.realtime.d;
1340                 }
1341                 if (opts->data.hfsc_opts.upperlimit.used) {
1342                         pa->pq_u.hfsc_opts.ulsc_m1 =
1343                             eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1344                             ref_bw);
1345                         pa->pq_u.hfsc_opts.ulsc_m2 =
1346                             eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1347                             ref_bw);
1348                         pa->pq_u.hfsc_opts.ulsc_d =
1349                             opts->data.hfsc_opts.upperlimit.d;
1350                 }
1351                 break;
1352         case ALTQT_FAIRQ:
1353                 pa->pq_u.fairq_opts.flags = opts->data.fairq_opts.flags;
1354                 pa->pq_u.fairq_opts.nbuckets = opts->data.fairq_opts.nbuckets;
1355                 pa->pq_u.fairq_opts.hogs_m1 =
1356                         eval_bwspec(&opts->data.fairq_opts.hogs_bw, ref_bw);
1357
1358                 if (opts->data.fairq_opts.linkshare.used) {
1359                         pa->pq_u.fairq_opts.lssc_m1 =
1360                             eval_bwspec(&opts->data.fairq_opts.linkshare.m1,
1361                             ref_bw);
1362                         pa->pq_u.fairq_opts.lssc_m2 =
1363                             eval_bwspec(&opts->data.fairq_opts.linkshare.m2,
1364                             ref_bw);
1365                         pa->pq_u.fairq_opts.lssc_d =
1366                             opts->data.fairq_opts.linkshare.d;
1367                 }
1368                 break;
1369         default:
1370                 warnx("eval_queue_opts: unknown scheduler type %u",
1371                     opts->qtype);
1372                 errors++;
1373                 break;
1374         }
1375
1376         return (errors);
1377 }
1378
1379 u_int32_t
1380 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1381 {
1382         if (bw->bw_absolute > 0)
1383                 return (bw->bw_absolute);
1384
1385         if (bw->bw_percent > 0)
1386                 return (ref_bw / 100 * bw->bw_percent);
1387
1388         return (0);
1389 }
1390
1391 void
1392 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1393     const struct node_hfsc_sc *sc)
1394 {
1395         printf(" %s", scname);
1396
1397         if (d != 0) {
1398                 printf("(");
1399                 if (sc != NULL && sc->m1.bw_percent > 0)
1400                         printf("%u%%", sc->m1.bw_percent);
1401                 else
1402                         printf("%s", rate2str((double)m1));
1403                 printf(" %u", d);
1404         }
1405
1406         if (sc != NULL && sc->m2.bw_percent > 0)
1407                 printf(" %u%%", sc->m2.bw_percent);
1408         else
1409                 printf(" %s", rate2str((double)m2));
1410
1411         if (d != 0)
1412                 printf(")");
1413 }
1414
1415 void
1416 print_fairq_sc(const char *scname, u_int m1, u_int d, u_int m2,
1417     const struct node_fairq_sc *sc)
1418 {
1419         printf(" %s", scname);
1420
1421         if (d != 0) {
1422                 printf("(");
1423                 if (sc != NULL && sc->m1.bw_percent > 0)
1424                         printf("%u%%", sc->m1.bw_percent);
1425                 else
1426                         printf("%s", rate2str((double)m1));
1427                 printf(" %u", d);
1428         }
1429
1430         if (sc != NULL && sc->m2.bw_percent > 0)
1431                 printf(" %u%%", sc->m2.bw_percent);
1432         else
1433                 printf(" %s", rate2str((double)m2));
1434
1435         if (d != 0)
1436                 printf(")");
1437 }
1438