pfctl_altq.c revision 1.3 1 /* $NetBSD: pfctl_altq.c,v 1.3 2004/06/23 04:38:43 itojun Exp $ */
2 /* $OpenBSD: pfctl_altq.c,v 1.83 2004/03/14 21:51:44 dhartmei Exp $ */
3
4 /*
5 * Copyright (c) 2002
6 * Sony Computer Science Laboratories Inc.
7 * Copyright (c) 2002, 2003 Henning Brauer <henning (at) openbsd.org>
8 *
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.
12 *
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.
20 */
21
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/socket.h>
25 #ifdef __NetBSD__
26 #include <sys/param.h>
27 #include <sys/mbuf.h>
28 #endif
29
30 #include <net/if.h>
31 #include <netinet/in.h>
32 #include <net/pfvar.h>
33
34 #include <err.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <math.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <altq/altq.h>
44 #include <altq/altq_cbq.h>
45 #include <altq/altq_priq.h>
46 #include <altq/altq_hfsc.h>
47
48 #include "pfctl_parser.h"
49 #include "pfctl.h"
50
51 #define is_sc_null(sc) (((sc) == NULL) || ((sc)->m1 == 0 && (sc)->m2 == 0))
52
53 TAILQ_HEAD(altqs, pf_altq) altqs = TAILQ_HEAD_INITIALIZER(altqs);
54 LIST_HEAD(gen_sc, segment) rtsc, lssc;
55
56 struct pf_altq *qname_to_pfaltq(const char *, const char *);
57 u_int32_t qname_to_qid(const char *);
58
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 *);
63
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 *);
67
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 *);
72
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,
79 double);
80 static double sc_x2y(struct service_curve *, double);
81
82 u_int32_t getifspeed(char *);
83 u_long getifmtu(char *);
84 int eval_queue_opts(struct pf_altq *, struct node_queue_opt *,
85 u_int32_t);
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 *);
89
90 void
91 pfaltq_store(struct pf_altq *a)
92 {
93 struct pf_altq *altq;
94
95 if ((altq = malloc(sizeof(*altq))) == NULL)
96 err(1, "malloc");
97 memcpy(altq, a, sizeof(struct pf_altq));
98 TAILQ_INSERT_TAIL(&altqs, altq, entries);
99 }
100
101 void
102 pfaltq_free(struct pf_altq *a)
103 {
104 struct pf_altq *altq;
105
106 TAILQ_FOREACH(altq, &altqs, entries) {
107 if (strncmp(a->ifname, altq->ifname, IFNAMSIZ) == 0 &&
108 strncmp(a->qname, altq->qname, PF_QNAME_SIZE) == 0) {
109 TAILQ_REMOVE(&altqs, altq, entries);
110 free(altq);
111 return;
112 }
113 }
114 }
115
116 struct pf_altq *
117 pfaltq_lookup(const char *ifname)
118 {
119 struct pf_altq *altq;
120
121 TAILQ_FOREACH(altq, &altqs, entries) {
122 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
123 altq->qname[0] == 0)
124 return (altq);
125 }
126 return (NULL);
127 }
128
129 struct pf_altq *
130 qname_to_pfaltq(const char *qname, const char *ifname)
131 {
132 struct pf_altq *altq;
133
134 TAILQ_FOREACH(altq, &altqs, entries) {
135 if (strncmp(ifname, altq->ifname, IFNAMSIZ) == 0 &&
136 strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
137 return (altq);
138 }
139 return (NULL);
140 }
141
142 u_int32_t
143 qname_to_qid(const char *qname)
144 {
145 struct pf_altq *altq;
146
147 /*
148 * We guarantee that same named queues on different interfaces
149 * have the same qid, so we do NOT need to limit matching on
150 * one interface!
151 */
152
153 TAILQ_FOREACH(altq, &altqs, entries) {
154 if (strncmp(qname, altq->qname, PF_QNAME_SIZE) == 0)
155 return (altq->qid);
156 }
157 return (0);
158 }
159
160 void
161 print_altq(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
162 struct node_queue_opt *qopts)
163 {
164 if (a->qname[0] != 0) {
165 print_queue(a, level, bw, 0, qopts);
166 return;
167 }
168
169 printf("altq on %s ", a->ifname);
170
171 switch (a->scheduler) {
172 case ALTQT_CBQ:
173 if (!print_cbq_opts(a))
174 printf("cbq ");
175 break;
176 case ALTQT_PRIQ:
177 if (!print_priq_opts(a))
178 printf("priq ");
179 break;
180 case ALTQT_HFSC:
181 if (!print_hfsc_opts(a, qopts))
182 printf("hfsc ");
183 break;
184 }
185
186 if (bw != NULL && bw->bw_percent > 0) {
187 if (bw->bw_percent < 100)
188 printf("bandwidth %u%% ", bw->bw_percent);
189 } else
190 printf("bandwidth %s ", rate2str((double)a->ifbandwidth));
191
192 if (a->qlimit != DEFAULT_QLIMIT)
193 printf("qlimit %u ", a->qlimit);
194 printf("tbrsize %u ", a->tbrsize);
195 }
196
197 void
198 print_queue(const struct pf_altq *a, unsigned level, struct node_queue_bw *bw,
199 int print_interface, struct node_queue_opt *qopts)
200 {
201 unsigned i;
202
203 printf("queue ");
204 for (i = 0; i < level; ++i)
205 printf(" ");
206 printf("%s ", a->qname);
207 if (print_interface)
208 printf("on %s ", a->ifname);
209 if (a->scheduler == ALTQT_CBQ || a->scheduler == ALTQT_HFSC) {
210 if (bw != NULL && bw->bw_percent > 0) {
211 if (bw->bw_percent < 100)
212 printf("bandwidth %u%% ", bw->bw_percent);
213 } else
214 printf("bandwidth %s ", rate2str((double)a->bandwidth));
215 }
216 if (a->priority != DEFAULT_PRIORITY)
217 printf("priority %u ", a->priority);
218 if (a->qlimit != DEFAULT_QLIMIT)
219 printf("qlimit %u ", a->qlimit);
220 switch (a->scheduler) {
221 case ALTQT_CBQ:
222 print_cbq_opts(a);
223 break;
224 case ALTQT_PRIQ:
225 print_priq_opts(a);
226 break;
227 case ALTQT_HFSC:
228 print_hfsc_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, 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, "cannot determine interface bandwidth "
247 "for %s, 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 default:
297 break;
298 }
299 }
300 }
301 return (error);
302 }
303
304 /*
305 * eval_pfqueue computes the queue parameters.
306 */
307 int
308 eval_pfqueue(struct pfctl *pf, struct pf_altq *pa, struct node_queue_bw *bw,
309 struct node_queue_opt *opts)
310 {
311 /* should be merged with expand_queue */
312 struct pf_altq *if_pa, *parent;
313 int error = 0;
314
315 /* find the corresponding interface and copy fields used by queues */
316 if ((if_pa = pfaltq_lookup(pa->ifname)) == NULL) {
317 fprintf(stderr, "altq not defined on %s\n", pa->ifname);
318 return (1);
319 }
320 pa->scheduler = if_pa->scheduler;
321 pa->ifbandwidth = if_pa->ifbandwidth;
322
323 if (qname_to_pfaltq(pa->qname, pa->ifname) != NULL) {
324 fprintf(stderr, "queue %s already exists on interface %s\n",
325 pa->qname, pa->ifname);
326 return (1);
327 }
328 pa->qid = qname_to_qid(pa->qname);
329
330 parent = NULL;
331 if (pa->parent[0] != 0) {
332 parent = qname_to_pfaltq(pa->parent, pa->ifname);
333 if (parent == NULL) {
334 fprintf(stderr, "parent %s not found for %s\n",
335 pa->parent, pa->qname);
336 return (1);
337 }
338 pa->parent_qid = parent->qid;
339 }
340 if (pa->qlimit == 0)
341 pa->qlimit = DEFAULT_QLIMIT;
342
343 if (pa->scheduler == ALTQT_CBQ || pa->scheduler == ALTQT_HFSC) {
344 if ((pa->bandwidth = eval_bwspec(bw,
345 parent == NULL ? 0 : parent->bandwidth)) == 0) {
346 fprintf(stderr, "bandwidth for %s invalid (%d / %d)\n",
347 pa->qname, bw->bw_absolute, bw->bw_percent);
348 return (1);
349 }
350
351 if (pa->bandwidth > pa->ifbandwidth) {
352 fprintf(stderr, "bandwidth for %s higher than "
353 "interface\n", pa->qname);
354 return (1);
355 }
356 if (parent != NULL && pa->bandwidth > parent->bandwidth) {
357 fprintf(stderr, "bandwidth for %s higher than parent\n",
358 pa->qname);
359 return (1);
360 }
361 }
362
363 if (eval_queue_opts(pa, opts, parent == NULL? 0 : parent->bandwidth))
364 return (1);
365
366 switch (pa->scheduler) {
367 case ALTQT_CBQ:
368 error = eval_pfqueue_cbq(pf, pa);
369 break;
370 case ALTQT_PRIQ:
371 error = eval_pfqueue_priq(pf, pa);
372 break;
373 case ALTQT_HFSC:
374 error = eval_pfqueue_hfsc(pf, pa);
375 break;
376 default:
377 break;
378 }
379 return (error);
380 }
381
382 /*
383 * CBQ support functions
384 */
385 #define RM_FILTER_GAIN 5 /* log2 of gain, e.g., 5 => 31/32 */
386 #define RM_NS_PER_SEC (1000000000)
387
388 static int
389 eval_pfqueue_cbq(struct pfctl *pf, struct pf_altq *pa)
390 {
391 struct cbq_opts *opts;
392 u_int ifmtu;
393
394 if (pa->priority >= CBQ_MAXPRI) {
395 warnx("priority out of range: max %d", CBQ_MAXPRI - 1);
396 return (-1);
397 }
398
399 ifmtu = getifmtu(pa->ifname);
400 opts = &pa->pq_u.cbq_opts;
401
402 if (opts->pktsize == 0) { /* use default */
403 opts->pktsize = ifmtu;
404 if (opts->pktsize > MCLBYTES) /* do what TCP does */
405 opts->pktsize &= ~MCLBYTES;
406 } else if (opts->pktsize > ifmtu)
407 opts->pktsize = ifmtu;
408 if (opts->maxpktsize == 0) /* use default */
409 opts->maxpktsize = ifmtu;
410 else if (opts->maxpktsize > ifmtu)
411 opts->pktsize = ifmtu;
412
413 if (opts->pktsize > opts->maxpktsize)
414 opts->pktsize = opts->maxpktsize;
415
416 if (pa->parent[0] == 0)
417 opts->flags |= (CBQCLF_ROOTCLASS | CBQCLF_WRR);
418
419 cbq_compute_idletime(pf, pa);
420 return (0);
421 }
422
423 /*
424 * compute ns_per_byte, maxidle, minidle, and offtime
425 */
426 static int
427 cbq_compute_idletime(struct pfctl *pf, struct pf_altq *pa)
428 {
429 struct cbq_opts *opts;
430 double maxidle_s, maxidle, minidle;
431 double offtime, nsPerByte, ifnsPerByte, ptime, cptime;
432 double z, g, f, gton, gtom;
433 u_int minburst, maxburst;
434
435 opts = &pa->pq_u.cbq_opts;
436 ifnsPerByte = (1.0 / (double)pa->ifbandwidth) * RM_NS_PER_SEC * 8;
437 minburst = opts->minburst;
438 maxburst = opts->maxburst;
439
440 if (pa->bandwidth == 0)
441 f = 0.0001; /* small enough? */
442 else
443 f = ((double) pa->bandwidth / (double) pa->ifbandwidth);
444
445 nsPerByte = ifnsPerByte / f;
446 ptime = (double)opts->pktsize * ifnsPerByte;
447 cptime = ptime * (1.0 - f) / f;
448
449 if (nsPerByte * (double)opts->maxpktsize > (double)INT_MAX) {
450 /*
451 * this causes integer overflow in kernel!
452 * (bandwidth < 6Kbps when max_pkt_size=1500)
453 */
454 if (pa->bandwidth != 0 && (pf->opts & PF_OPT_QUIET) == 0)
455 warnx("queue bandwidth must be larger than %s",
456 rate2str(ifnsPerByte * (double)opts->maxpktsize /
457 (double)INT_MAX * (double)pa->ifbandwidth));
458 fprintf(stderr, "cbq: queue %s is too slow!\n",
459 pa->qname);
460 nsPerByte = (double)(INT_MAX / opts->maxpktsize);
461 }
462
463 if (maxburst == 0) { /* use default */
464 if (cptime > 10.0 * 1000000)
465 maxburst = 4;
466 else
467 maxburst = 16;
468 }
469 if (minburst == 0) /* use default */
470 minburst = 2;
471 if (minburst > maxburst)
472 minburst = maxburst;
473
474 z = (double)(1 << RM_FILTER_GAIN);
475 g = (1.0 - 1.0 / z);
476 gton = pow(g, (double)maxburst);
477 gtom = pow(g, (double)(minburst-1));
478 maxidle = ((1.0 / f - 1.0) * ((1.0 - gton) / gton));
479 maxidle_s = (1.0 - g);
480 if (maxidle > maxidle_s)
481 maxidle = ptime * maxidle;
482 else
483 maxidle = ptime * maxidle_s;
484 if (minburst)
485 offtime = cptime * (1.0 + 1.0/(1.0 - g) * (1.0 - gtom) / gtom);
486 else
487 offtime = cptime;
488 minidle = -((double)opts->maxpktsize * (double)nsPerByte);
489
490 /* scale parameters */
491 maxidle = ((maxidle * 8.0) / nsPerByte) *
492 pow(2.0, (double)RM_FILTER_GAIN);
493 offtime = (offtime * 8.0) / nsPerByte *
494 pow(2.0, (double)RM_FILTER_GAIN);
495 minidle = ((minidle * 8.0) / nsPerByte) *
496 pow(2.0, (double)RM_FILTER_GAIN);
497
498 maxidle = maxidle / 1000.0;
499 offtime = offtime / 1000.0;
500 minidle = minidle / 1000.0;
501
502 opts->minburst = minburst;
503 opts->maxburst = maxburst;
504 opts->ns_per_byte = (u_int)nsPerByte;
505 opts->maxidle = (u_int)fabs(maxidle);
506 opts->minidle = (int)minidle;
507 opts->offtime = (u_int)fabs(offtime);
508
509 return (0);
510 }
511
512 static int
513 check_commit_cbq(int dev, int opts, struct pf_altq *pa)
514 {
515 struct pf_altq *altq;
516 int root_class, default_class;
517 int error = 0;
518
519 /*
520 * check if cbq has one root queue and one default queue
521 * for this interface
522 */
523 root_class = default_class = 0;
524 TAILQ_FOREACH(altq, &altqs, entries) {
525 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
526 continue;
527 if (altq->qname[0] == 0) /* this is for interface */
528 continue;
529 if (altq->pq_u.cbq_opts.flags & CBQCLF_ROOTCLASS)
530 root_class++;
531 if (altq->pq_u.cbq_opts.flags & CBQCLF_DEFCLASS)
532 default_class++;
533 }
534 if (root_class != 1) {
535 warnx("should have one root queue on %s", pa->ifname);
536 error++;
537 }
538 if (default_class != 1) {
539 warnx("should have one default queue on %s", pa->ifname);
540 error++;
541 }
542 return (error);
543 }
544
545 static int
546 print_cbq_opts(const struct pf_altq *a)
547 {
548 const struct cbq_opts *opts;
549
550 opts = &a->pq_u.cbq_opts;
551 if (opts->flags) {
552 printf("cbq(");
553 if (opts->flags & CBQCLF_RED)
554 printf(" red");
555 if (opts->flags & CBQCLF_ECN)
556 printf(" ecn");
557 if (opts->flags & CBQCLF_RIO)
558 printf(" rio");
559 if (opts->flags & CBQCLF_CLEARDSCP)
560 printf(" cleardscp");
561 if (opts->flags & CBQCLF_FLOWVALVE)
562 printf(" flowvalve");
563 #ifdef CBQCLF_BORROW
564 if (opts->flags & CBQCLF_BORROW)
565 printf(" borrow");
566 #endif
567 if (opts->flags & CBQCLF_WRR)
568 printf(" wrr");
569 if (opts->flags & CBQCLF_EFFICIENT)
570 printf(" efficient");
571 if (opts->flags & CBQCLF_ROOTCLASS)
572 printf(" root");
573 if (opts->flags & CBQCLF_DEFCLASS)
574 printf(" default");
575 printf(" ) ");
576
577 return (1);
578 } else
579 return (0);
580 }
581
582 /*
583 * PRIQ support functions
584 */
585 static int
586 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
587 {
588 struct pf_altq *altq;
589
590 if (pa->priority >= PRIQ_MAXPRI) {
591 warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
592 return (-1);
593 }
594 /* the priority should be unique for the interface */
595 TAILQ_FOREACH(altq, &altqs, entries) {
596 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
597 altq->qname[0] != 0 && altq->priority == pa->priority) {
598 warnx("%s and %s have the same priority",
599 altq->qname, pa->qname);
600 return (-1);
601 }
602 }
603
604 return (0);
605 }
606
607 static int
608 check_commit_priq(int dev, int opts, struct pf_altq *pa)
609 {
610 struct pf_altq *altq;
611 int default_class;
612 int error = 0;
613
614 /*
615 * check if priq has one default class for this interface
616 */
617 default_class = 0;
618 TAILQ_FOREACH(altq, &altqs, entries) {
619 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
620 continue;
621 if (altq->qname[0] == 0) /* this is for interface */
622 continue;
623 if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
624 default_class++;
625 }
626 if (default_class != 1) {
627 warnx("should have one default queue on %s", pa->ifname);
628 error++;
629 }
630 return (error);
631 }
632
633 static int
634 print_priq_opts(const struct pf_altq *a)
635 {
636 const struct priq_opts *opts;
637
638 opts = &a->pq_u.priq_opts;
639
640 if (opts->flags) {
641 printf("priq(");
642 if (opts->flags & PRCF_RED)
643 printf(" red");
644 if (opts->flags & PRCF_ECN)
645 printf(" ecn");
646 if (opts->flags & PRCF_RIO)
647 printf(" rio");
648 if (opts->flags & PRCF_CLEARDSCP)
649 printf(" cleardscp");
650 if (opts->flags & PRCF_DEFAULTCLASS)
651 printf(" default");
652 printf(" ) ");
653
654 return (1);
655 } else
656 return (0);
657 }
658
659 /*
660 * HFSC support functions
661 */
662 static int
663 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
664 {
665 struct pf_altq *altq, *parent;
666 struct hfsc_opts *opts;
667 struct service_curve sc;
668
669 opts = &pa->pq_u.hfsc_opts;
670
671 if (pa->parent[0] == 0) {
672 /* root queue */
673 opts->lssc_m1 = pa->ifbandwidth;
674 opts->lssc_m2 = pa->ifbandwidth;
675 opts->lssc_d = 0;
676 return (0);
677 }
678
679 LIST_INIT(&rtsc);
680 LIST_INIT(&lssc);
681
682 /* if link_share is not specified, use bandwidth */
683 if (opts->lssc_m2 == 0)
684 opts->lssc_m2 = pa->bandwidth;
685
686 if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
687 (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
688 (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
689 warnx("m2 is zero for %s", pa->qname);
690 return (-1);
691 }
692
693 if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
694 (opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
695 (opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0)) {
696 warnx("m1 must be zero for convex curve: %s", pa->qname);
697 return (-1);
698 }
699
700 /*
701 * admission control:
702 * for the real-time service curve, the sum of the service curves
703 * should not exceed 80% of the interface bandwidth. 20% is reserved
704 * not to over-commit the actual interface bandwidth.
705 * for the link-sharing service curve, the sum of the child service
706 * curve should not exceed the parent service curve.
707 * for the upper-limit service curve, the assigned bandwidth should
708 * be smaller than the interface bandwidth, and the upper-limit should
709 * be larger than the real-time service curve when both are defined.
710 */
711 parent = qname_to_pfaltq(pa->parent, pa->ifname);
712 if (parent == NULL)
713 errx(1, "parent %s not found for %s", pa->parent, pa->qname);
714
715 TAILQ_FOREACH(altq, &altqs, entries) {
716 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
717 continue;
718 if (altq->qname[0] == 0) /* this is for interface */
719 continue;
720
721 /* if the class has a real-time service curve, add it. */
722 if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
723 sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
724 sc.d = altq->pq_u.hfsc_opts.rtsc_d;
725 sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
726 gsc_add_sc(&rtsc, &sc);
727 }
728
729 if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
730 continue;
731
732 /* if the class has a link-sharing service curve, add it. */
733 if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
734 sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
735 sc.d = altq->pq_u.hfsc_opts.lssc_d;
736 sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
737 gsc_add_sc(&lssc, &sc);
738 }
739 }
740
741 /* check the real-time service curve. reserve 20% of interface bw */
742 if (opts->rtsc_m2 != 0) {
743 sc.m1 = 0;
744 sc.d = 0;
745 sc.m2 = pa->ifbandwidth / 100 * 80;
746 if (!is_gsc_under_sc(&rtsc, &sc)) {
747 warnx("real-time sc exceeds the interface bandwidth");
748 goto err_ret;
749 }
750 }
751
752 /* check the link-sharing service curve. */
753 if (opts->lssc_m2 != 0) {
754 sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
755 sc.d = parent->pq_u.hfsc_opts.lssc_d;
756 sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
757 if (!is_gsc_under_sc(&lssc, &sc)) {
758 warnx("link-sharing sc exceeds parent's sc");
759 goto err_ret;
760 }
761 }
762
763 /* check the upper-limit service curve. */
764 if (opts->ulsc_m2 != 0) {
765 if (opts->ulsc_m1 > pa->ifbandwidth ||
766 opts->ulsc_m2 > pa->ifbandwidth) {
767 warnx("upper-limit larger than interface bandwidth");
768 goto err_ret;
769 }
770 if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
771 warnx("upper-limit sc smaller than real-time sc");
772 goto err_ret;
773 }
774 }
775
776 gsc_destroy(&rtsc);
777 gsc_destroy(&lssc);
778
779 return (0);
780
781 err_ret:
782 gsc_destroy(&rtsc);
783 gsc_destroy(&lssc);
784 return (-1);
785 }
786
787 static int
788 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
789 {
790 struct pf_altq *altq, *def = NULL;
791 int default_class;
792 int error = 0;
793
794 /* check if hfsc has one default queue for this interface */
795 default_class = 0;
796 TAILQ_FOREACH(altq, &altqs, entries) {
797 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
798 continue;
799 if (altq->qname[0] == 0) /* this is for interface */
800 continue;
801 if (altq->parent[0] == 0) /* dummy root */
802 continue;
803 if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
804 default_class++;
805 def = altq;
806 }
807 }
808 if (default_class != 1) {
809 warnx("should have one default queue on %s", pa->ifname);
810 return (1);
811 }
812 /* make sure the default queue is a leaf */
813 TAILQ_FOREACH(altq, &altqs, entries) {
814 if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
815 continue;
816 if (altq->qname[0] == 0) /* this is for interface */
817 continue;
818 if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
819 warnx("default queue is not a leaf");
820 error++;
821 }
822 }
823 return (error);
824 }
825
826 static int
827 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
828 {
829 const struct hfsc_opts *opts;
830 const struct node_hfsc_sc *rtsc, *lssc, *ulsc;
831
832 opts = &a->pq_u.hfsc_opts;
833 if (qopts == NULL)
834 rtsc = lssc = ulsc = NULL;
835 else {
836 rtsc = &qopts->data.hfsc_opts.realtime;
837 lssc = &qopts->data.hfsc_opts.linkshare;
838 ulsc = &qopts->data.hfsc_opts.upperlimit;
839 }
840
841 if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
842 (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
843 opts->lssc_d != 0))) {
844 printf("hfsc(");
845 if (opts->flags & HFCF_RED)
846 printf(" red");
847 if (opts->flags & HFCF_ECN)
848 printf(" ecn");
849 if (opts->flags & HFCF_RIO)
850 printf(" rio");
851 if (opts->flags & HFCF_CLEARDSCP)
852 printf(" cleardscp");
853 if (opts->flags & HFCF_DEFAULTCLASS)
854 printf(" default");
855 if (opts->rtsc_m2 != 0)
856 print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
857 opts->rtsc_m2, rtsc);
858 if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
859 opts->lssc_d != 0))
860 print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
861 opts->lssc_m2, lssc);
862 if (opts->ulsc_m2 != 0)
863 print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
864 opts->ulsc_m2, ulsc);
865 printf(" ) ");
866
867 return (1);
868 } else
869 return (0);
870 }
871
872 /*
873 * admission control using generalized service curve
874 */
875 #ifdef __OpenBSD__
876 #define INFINITY HUGE_VAL /* positive infinity defined in <math.h> */
877 #endif
878
879 /* add a new service curve to a generalized service curve */
880 static void
881 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
882 {
883 if (is_sc_null(sc))
884 return;
885 if (sc->d != 0)
886 gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
887 gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
888 }
889
890 /*
891 * check whether all points of a generalized service curve have
892 * their y-coordinates no larger than a given two-piece linear
893 * service curve.
894 */
895 static int
896 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
897 {
898 struct segment *s, *last, *end;
899 double y;
900
901 if (is_sc_null(sc)) {
902 if (LIST_EMPTY(gsc))
903 return (1);
904 LIST_FOREACH(s, gsc, _next) {
905 if (s->m != 0)
906 return (0);
907 }
908 return (1);
909 }
910 /*
911 * gsc has a dummy entry at the end with x = INFINITY.
912 * loop through up to this dummy entry.
913 */
914 end = gsc_getentry(gsc, INFINITY);
915 if (end == NULL)
916 return (1);
917 last = NULL;
918 for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
919 if (s->y > sc_x2y(sc, s->x))
920 return (0);
921 last = s;
922 }
923 /* last now holds the real last segment */
924 if (last == NULL)
925 return (1);
926 if (last->m > sc->m2)
927 return (0);
928 if (last->x < sc->d && last->m > sc->m1) {
929 y = last->y + (sc->d - last->x) * last->m;
930 if (y > sc_x2y(sc, sc->d))
931 return (0);
932 }
933 return (1);
934 }
935
936 static void
937 gsc_destroy(struct gen_sc *gsc)
938 {
939 struct segment *s;
940
941 while ((s = LIST_FIRST(gsc)) != NULL) {
942 LIST_REMOVE(s, _next);
943 free(s);
944 }
945 }
946
947 /*
948 * return a segment entry starting at x.
949 * if gsc has no entry starting at x, a new entry is created at x.
950 */
951 static struct segment *
952 gsc_getentry(struct gen_sc *gsc, double x)
953 {
954 struct segment *new, *prev, *s;
955
956 prev = NULL;
957 LIST_FOREACH(s, gsc, _next) {
958 if (s->x == x)
959 return (s); /* matching entry found */
960 else if (s->x < x)
961 prev = s;
962 else
963 break;
964 }
965
966 /* we have to create a new entry */
967 if ((new = calloc(1, sizeof(struct segment))) == NULL)
968 return (NULL);
969
970 new->x = x;
971 if (x == INFINITY || s == NULL)
972 new->d = 0;
973 else if (s->x == INFINITY)
974 new->d = INFINITY;
975 else
976 new->d = s->x - x;
977 if (prev == NULL) {
978 /* insert the new entry at the head of the list */
979 new->y = 0;
980 new->m = 0;
981 LIST_INSERT_HEAD(gsc, new, _next);
982 } else {
983 /*
984 * the start point intersects with the segment pointed by
985 * prev. divide prev into 2 segments
986 */
987 if (x == INFINITY) {
988 prev->d = INFINITY;
989 if (prev->m == 0)
990 new->y = prev->y;
991 else
992 new->y = INFINITY;
993 } else {
994 prev->d = x - prev->x;
995 new->y = prev->d * prev->m + prev->y;
996 }
997 new->m = prev->m;
998 LIST_INSERT_AFTER(prev, new, _next);
999 }
1000 return (new);
1001 }
1002
1003 /* add a segment to a generalized service curve */
1004 static int
1005 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
1006 {
1007 struct segment *start, *end, *s;
1008 double x2;
1009
1010 if (d == INFINITY)
1011 x2 = INFINITY;
1012 else
1013 x2 = x + d;
1014 start = gsc_getentry(gsc, x);
1015 end = gsc_getentry(gsc, x2);
1016 if (start == NULL || end == NULL)
1017 return (-1);
1018
1019 for (s = start; s != end; s = LIST_NEXT(s, _next)) {
1020 s->m += m;
1021 s->y += y + (s->x - x) * m;
1022 }
1023
1024 end = gsc_getentry(gsc, INFINITY);
1025 for (; s != end; s = LIST_NEXT(s, _next)) {
1026 s->y += m * d;
1027 }
1028
1029 return (0);
1030 }
1031
1032 /* get y-projection of a service curve */
1033 static double
1034 sc_x2y(struct service_curve *sc, double x)
1035 {
1036 double y;
1037
1038 if (x <= (double)sc->d)
1039 /* y belongs to the 1st segment */
1040 y = x * (double)sc->m1;
1041 else
1042 /* y belongs to the 2nd segment */
1043 y = (double)sc->d * (double)sc->m1
1044 + (x - (double)sc->d) * (double)sc->m2;
1045 return (y);
1046 }
1047
1048 /*
1049 * misc utilities
1050 */
1051 #define R2S_BUFS 8
1052 #define RATESTR_MAX 16
1053
1054 char *
1055 rate2str(double rate)
1056 {
1057 char *buf;
1058 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
1059 static int idx = 0;
1060 int i;
1061 static const char unit[] = " KMG";
1062
1063 buf = r2sbuf[idx++];
1064 if (idx == R2S_BUFS)
1065 idx = 0;
1066
1067 for (i = 0; rate >= 1000 && i <= 3; i++)
1068 rate /= 1000;
1069
1070 if ((int)(rate * 100) % 100)
1071 snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
1072 else
1073 snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
1074
1075 return (buf);
1076 }
1077
1078 u_int32_t
1079 getifspeed(char *ifname)
1080 {
1081 int s;
1082 struct ifreq ifr;
1083 struct if_data ifrdat;
1084
1085 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1086 err(1, "socket");
1087 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1088 sizeof(ifr.ifr_name))
1089 errx(1, "getifspeed: strlcpy");
1090 ifr.ifr_data = (caddr_t)&ifrdat;
1091 if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
1092 err(1, "SIOCGIFDATA");
1093 if (shutdown(s, SHUT_RDWR) == -1)
1094 err(1, "shutdown");
1095 if (close(s))
1096 err(1, "close");
1097 return ((u_int32_t)ifrdat.ifi_baudrate);
1098 }
1099
1100 u_long
1101 getifmtu(char *ifname)
1102 {
1103 int s;
1104 struct ifreq ifr;
1105
1106 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1107 err(1, "socket");
1108 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
1109 sizeof(ifr.ifr_name))
1110 errx(1, "getifmtu: strlcpy");
1111 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
1112 err(1, "SIOCGIFMTU");
1113 if (shutdown(s, SHUT_RDWR) == -1)
1114 err(1, "shutdown");
1115 if (close(s))
1116 err(1, "close");
1117 if (ifr.ifr_mtu > 0)
1118 return (ifr.ifr_mtu);
1119 else {
1120 warnx("could not get mtu for %s, assuming 1500", ifname);
1121 return (1500);
1122 }
1123 }
1124
1125 int
1126 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
1127 u_int32_t ref_bw)
1128 {
1129 int errors = 0;
1130
1131 switch (pa->scheduler) {
1132 case ALTQT_CBQ:
1133 pa->pq_u.cbq_opts = opts->data.cbq_opts;
1134 break;
1135 case ALTQT_PRIQ:
1136 pa->pq_u.priq_opts = opts->data.priq_opts;
1137 break;
1138 case ALTQT_HFSC:
1139 pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
1140 if (opts->data.hfsc_opts.linkshare.used) {
1141 pa->pq_u.hfsc_opts.lssc_m1 =
1142 eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
1143 ref_bw);
1144 pa->pq_u.hfsc_opts.lssc_m2 =
1145 eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
1146 ref_bw);
1147 pa->pq_u.hfsc_opts.lssc_d =
1148 opts->data.hfsc_opts.linkshare.d;
1149 }
1150 if (opts->data.hfsc_opts.realtime.used) {
1151 pa->pq_u.hfsc_opts.rtsc_m1 =
1152 eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
1153 ref_bw);
1154 pa->pq_u.hfsc_opts.rtsc_m2 =
1155 eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
1156 ref_bw);
1157 pa->pq_u.hfsc_opts.rtsc_d =
1158 opts->data.hfsc_opts.realtime.d;
1159 }
1160 if (opts->data.hfsc_opts.upperlimit.used) {
1161 pa->pq_u.hfsc_opts.ulsc_m1 =
1162 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
1163 ref_bw);
1164 pa->pq_u.hfsc_opts.ulsc_m2 =
1165 eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
1166 ref_bw);
1167 pa->pq_u.hfsc_opts.ulsc_d =
1168 opts->data.hfsc_opts.upperlimit.d;
1169 }
1170 break;
1171 default:
1172 warnx("eval_queue_opts: unknown scheduler type %u",
1173 opts->qtype);
1174 errors++;
1175 break;
1176 }
1177
1178 return (errors);
1179 }
1180
1181 u_int32_t
1182 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
1183 {
1184 if (bw->bw_absolute > 0)
1185 return (bw->bw_absolute);
1186
1187 if (bw->bw_percent > 0)
1188 return (ref_bw / 100 * bw->bw_percent);
1189
1190 return (0);
1191 }
1192
1193 void
1194 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
1195 const struct node_hfsc_sc *sc)
1196 {
1197 printf(" %s", scname);
1198
1199 if (d != 0) {
1200 printf("(");
1201 if (sc != NULL && sc->m1.bw_percent > 0)
1202 printf("%u%%", sc->m1.bw_percent);
1203 else
1204 printf("%s", rate2str((double)m1));
1205 printf(" %u", d);
1206 }
1207
1208 if (sc != NULL && sc->m2.bw_percent > 0)
1209 printf(" %u%%", sc->m2.bw_percent);
1210 else
1211 printf(" %s", rate2str((double)m2));
1212
1213 if (d != 0)
1214 printf(")");
1215 }
1216