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