Home | History | Annotate | Line # | Download | only in pfctl
pfctl_altq.c revision 1.2
      1 /*	$NetBSD: pfctl_altq.c,v 1.2 2004/06/22 15:16:30 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 		if (opts->flags & CBQCLF_BORROW)
    564 			printf(" borrow");
    565 		if (opts->flags & CBQCLF_WRR)
    566 			printf(" wrr");
    567 		if (opts->flags & CBQCLF_EFFICIENT)
    568 			printf(" efficient");
    569 		if (opts->flags & CBQCLF_ROOTCLASS)
    570 			printf(" root");
    571 		if (opts->flags & CBQCLF_DEFCLASS)
    572 			printf(" default");
    573 		printf(" ) ");
    574 
    575 		return (1);
    576 	} else
    577 		return (0);
    578 }
    579 
    580 /*
    581  * PRIQ support functions
    582  */
    583 static int
    584 eval_pfqueue_priq(struct pfctl *pf, struct pf_altq *pa)
    585 {
    586 	struct pf_altq	*altq;
    587 
    588 	if (pa->priority >= PRIQ_MAXPRI) {
    589 		warnx("priority out of range: max %d", PRIQ_MAXPRI - 1);
    590 		return (-1);
    591 	}
    592 	/* the priority should be unique for the interface */
    593 	TAILQ_FOREACH(altq, &altqs, entries) {
    594 		if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) == 0 &&
    595 		    altq->qname[0] != 0 && altq->priority == pa->priority) {
    596 			warnx("%s and %s have the same priority",
    597 			    altq->qname, pa->qname);
    598 			return (-1);
    599 		}
    600 	}
    601 
    602 	return (0);
    603 }
    604 
    605 static int
    606 check_commit_priq(int dev, int opts, struct pf_altq *pa)
    607 {
    608 	struct pf_altq	*altq;
    609 	int		 default_class;
    610 	int		 error = 0;
    611 
    612 	/*
    613 	 * check if priq has one default class for this interface
    614 	 */
    615 	default_class = 0;
    616 	TAILQ_FOREACH(altq, &altqs, entries) {
    617 		if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
    618 			continue;
    619 		if (altq->qname[0] == 0)  /* this is for interface */
    620 			continue;
    621 		if (altq->pq_u.priq_opts.flags & PRCF_DEFAULTCLASS)
    622 			default_class++;
    623 	}
    624 	if (default_class != 1) {
    625 		warnx("should have one default queue on %s", pa->ifname);
    626 		error++;
    627 	}
    628 	return (error);
    629 }
    630 
    631 static int
    632 print_priq_opts(const struct pf_altq *a)
    633 {
    634 	const struct priq_opts	*opts;
    635 
    636 	opts = &a->pq_u.priq_opts;
    637 
    638 	if (opts->flags) {
    639 		printf("priq(");
    640 		if (opts->flags & PRCF_RED)
    641 			printf(" red");
    642 		if (opts->flags & PRCF_ECN)
    643 			printf(" ecn");
    644 		if (opts->flags & PRCF_RIO)
    645 			printf(" rio");
    646 		if (opts->flags & PRCF_CLEARDSCP)
    647 			printf(" cleardscp");
    648 		if (opts->flags & PRCF_DEFAULTCLASS)
    649 			printf(" default");
    650 		printf(" ) ");
    651 
    652 		return (1);
    653 	} else
    654 		return (0);
    655 }
    656 
    657 /*
    658  * HFSC support functions
    659  */
    660 static int
    661 eval_pfqueue_hfsc(struct pfctl *pf, struct pf_altq *pa)
    662 {
    663 	struct pf_altq		*altq, *parent;
    664 	struct hfsc_opts	*opts;
    665 	struct service_curve	 sc;
    666 
    667 	opts = &pa->pq_u.hfsc_opts;
    668 
    669 	if (pa->parent[0] == 0) {
    670 		/* root queue */
    671 		opts->lssc_m1 = pa->ifbandwidth;
    672 		opts->lssc_m2 = pa->ifbandwidth;
    673 		opts->lssc_d = 0;
    674 		return (0);
    675 	}
    676 
    677 	LIST_INIT(&rtsc);
    678 	LIST_INIT(&lssc);
    679 
    680 	/* if link_share is not specified, use bandwidth */
    681 	if (opts->lssc_m2 == 0)
    682 		opts->lssc_m2 = pa->bandwidth;
    683 
    684 	if ((opts->rtsc_m1 > 0 && opts->rtsc_m2 == 0) ||
    685 	    (opts->lssc_m1 > 0 && opts->lssc_m2 == 0) ||
    686 	    (opts->ulsc_m1 > 0 && opts->ulsc_m2 == 0)) {
    687 		warnx("m2 is zero for %s", pa->qname);
    688 		return (-1);
    689 	}
    690 
    691 	if ((opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
    692 	    (opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0) ||
    693 	    (opts->rtsc_m1 < opts->rtsc_m2 && opts->rtsc_m1 != 0)) {
    694 		warnx("m1 must be zero for convex curve: %s", pa->qname);
    695 		return (-1);
    696 	}
    697 
    698 	/*
    699 	 * admission control:
    700 	 * for the real-time service curve, the sum of the service curves
    701 	 * should not exceed 80% of the interface bandwidth.  20% is reserved
    702 	 * not to over-commit the actual interface bandwidth.
    703 	 * for the link-sharing service curve, the sum of the child service
    704 	 * curve should not exceed the parent service curve.
    705 	 * for the upper-limit service curve, the assigned bandwidth should
    706 	 * be smaller than the interface bandwidth, and the upper-limit should
    707 	 * be larger than the real-time service curve when both are defined.
    708 	 */
    709 	parent = qname_to_pfaltq(pa->parent, pa->ifname);
    710 	if (parent == NULL)
    711 		errx(1, "parent %s not found for %s", pa->parent, pa->qname);
    712 
    713 	TAILQ_FOREACH(altq, &altqs, entries) {
    714 		if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
    715 			continue;
    716 		if (altq->qname[0] == 0)  /* this is for interface */
    717 			continue;
    718 
    719 		/* if the class has a real-time service curve, add it. */
    720 		if (opts->rtsc_m2 != 0 && altq->pq_u.hfsc_opts.rtsc_m2 != 0) {
    721 			sc.m1 = altq->pq_u.hfsc_opts.rtsc_m1;
    722 			sc.d = altq->pq_u.hfsc_opts.rtsc_d;
    723 			sc.m2 = altq->pq_u.hfsc_opts.rtsc_m2;
    724 			gsc_add_sc(&rtsc, &sc);
    725 		}
    726 
    727 		if (strncmp(altq->parent, pa->parent, PF_QNAME_SIZE) != 0)
    728 			continue;
    729 
    730 		/* if the class has a link-sharing service curve, add it. */
    731 		if (opts->lssc_m2 != 0 && altq->pq_u.hfsc_opts.lssc_m2 != 0) {
    732 			sc.m1 = altq->pq_u.hfsc_opts.lssc_m1;
    733 			sc.d = altq->pq_u.hfsc_opts.lssc_d;
    734 			sc.m2 = altq->pq_u.hfsc_opts.lssc_m2;
    735 			gsc_add_sc(&lssc, &sc);
    736 		}
    737 	}
    738 
    739 	/* check the real-time service curve.  reserve 20% of interface bw */
    740 	if (opts->rtsc_m2 != 0) {
    741 		sc.m1 = 0;
    742 		sc.d = 0;
    743 		sc.m2 = pa->ifbandwidth / 100 * 80;
    744 		if (!is_gsc_under_sc(&rtsc, &sc)) {
    745 			warnx("real-time sc exceeds the interface bandwidth");
    746 			goto err_ret;
    747 		}
    748 	}
    749 
    750 	/* check the link-sharing service curve. */
    751 	if (opts->lssc_m2 != 0) {
    752 		sc.m1 = parent->pq_u.hfsc_opts.lssc_m1;
    753 		sc.d = parent->pq_u.hfsc_opts.lssc_d;
    754 		sc.m2 = parent->pq_u.hfsc_opts.lssc_m2;
    755 		if (!is_gsc_under_sc(&lssc, &sc)) {
    756 			warnx("link-sharing sc exceeds parent's sc");
    757 			goto err_ret;
    758 		}
    759 	}
    760 
    761 	/* check the upper-limit service curve. */
    762 	if (opts->ulsc_m2 != 0) {
    763 		if (opts->ulsc_m1 > pa->ifbandwidth ||
    764 		    opts->ulsc_m2 > pa->ifbandwidth) {
    765 			warnx("upper-limit larger than interface bandwidth");
    766 			goto err_ret;
    767 		}
    768 		if (opts->rtsc_m2 != 0 && opts->rtsc_m2 > opts->ulsc_m2) {
    769 			warnx("upper-limit sc smaller than real-time sc");
    770 			goto err_ret;
    771 		}
    772 	}
    773 
    774 	gsc_destroy(&rtsc);
    775 	gsc_destroy(&lssc);
    776 
    777 	return (0);
    778 
    779 err_ret:
    780 	gsc_destroy(&rtsc);
    781 	gsc_destroy(&lssc);
    782 	return (-1);
    783 }
    784 
    785 static int
    786 check_commit_hfsc(int dev, int opts, struct pf_altq *pa)
    787 {
    788 	struct pf_altq	*altq, *def = NULL;
    789 	int		 default_class;
    790 	int		 error = 0;
    791 
    792 	/* check if hfsc has one default queue for this interface */
    793 	default_class = 0;
    794 	TAILQ_FOREACH(altq, &altqs, entries) {
    795 		if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
    796 			continue;
    797 		if (altq->qname[0] == 0)  /* this is for interface */
    798 			continue;
    799 		if (altq->parent[0] == 0)  /* dummy root */
    800 			continue;
    801 		if (altq->pq_u.hfsc_opts.flags & HFCF_DEFAULTCLASS) {
    802 			default_class++;
    803 			def = altq;
    804 		}
    805 	}
    806 	if (default_class != 1) {
    807 		warnx("should have one default queue on %s", pa->ifname);
    808 		return (1);
    809 	}
    810 	/* make sure the default queue is a leaf */
    811 	TAILQ_FOREACH(altq, &altqs, entries) {
    812 		if (strncmp(altq->ifname, pa->ifname, IFNAMSIZ) != 0)
    813 			continue;
    814 		if (altq->qname[0] == 0)  /* this is for interface */
    815 			continue;
    816 		if (strncmp(altq->parent, def->qname, PF_QNAME_SIZE) == 0) {
    817 			warnx("default queue is not a leaf");
    818 			error++;
    819 		}
    820 	}
    821 	return (error);
    822 }
    823 
    824 static int
    825 print_hfsc_opts(const struct pf_altq *a, const struct node_queue_opt *qopts)
    826 {
    827 	const struct hfsc_opts		*opts;
    828 	const struct node_hfsc_sc	*rtsc, *lssc, *ulsc;
    829 
    830 	opts = &a->pq_u.hfsc_opts;
    831 	if (qopts == NULL)
    832 		rtsc = lssc = ulsc = NULL;
    833 	else {
    834 		rtsc = &qopts->data.hfsc_opts.realtime;
    835 		lssc = &qopts->data.hfsc_opts.linkshare;
    836 		ulsc = &qopts->data.hfsc_opts.upperlimit;
    837 	}
    838 
    839 	if (opts->flags || opts->rtsc_m2 != 0 || opts->ulsc_m2 != 0 ||
    840 	    (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
    841 	    opts->lssc_d != 0))) {
    842 		printf("hfsc(");
    843 		if (opts->flags & HFCF_RED)
    844 			printf(" red");
    845 		if (opts->flags & HFCF_ECN)
    846 			printf(" ecn");
    847 		if (opts->flags & HFCF_RIO)
    848 			printf(" rio");
    849 		if (opts->flags & HFCF_CLEARDSCP)
    850 			printf(" cleardscp");
    851 		if (opts->flags & HFCF_DEFAULTCLASS)
    852 			printf(" default");
    853 		if (opts->rtsc_m2 != 0)
    854 			print_hfsc_sc("realtime", opts->rtsc_m1, opts->rtsc_d,
    855 			    opts->rtsc_m2, rtsc);
    856 		if (opts->lssc_m2 != 0 && (opts->lssc_m2 != a->bandwidth ||
    857 		    opts->lssc_d != 0))
    858 			print_hfsc_sc("linkshare", opts->lssc_m1, opts->lssc_d,
    859 			    opts->lssc_m2, lssc);
    860 		if (opts->ulsc_m2 != 0)
    861 			print_hfsc_sc("upperlimit", opts->ulsc_m1, opts->ulsc_d,
    862 			    opts->ulsc_m2, ulsc);
    863 		printf(" ) ");
    864 
    865 		return (1);
    866 	} else
    867 		return (0);
    868 }
    869 
    870 /*
    871  * admission control using generalized service curve
    872  */
    873 #ifdef __OpenBSD__
    874 #define	INFINITY	HUGE_VAL  /* positive infinity defined in <math.h> */
    875 #endif
    876 
    877 /* add a new service curve to a generalized service curve */
    878 static void
    879 gsc_add_sc(struct gen_sc *gsc, struct service_curve *sc)
    880 {
    881 	if (is_sc_null(sc))
    882 		return;
    883 	if (sc->d != 0)
    884 		gsc_add_seg(gsc, 0.0, 0.0, (double)sc->d, (double)sc->m1);
    885 	gsc_add_seg(gsc, (double)sc->d, 0.0, INFINITY, (double)sc->m2);
    886 }
    887 
    888 /*
    889  * check whether all points of a generalized service curve have
    890  * their y-coordinates no larger than a given two-piece linear
    891  * service curve.
    892  */
    893 static int
    894 is_gsc_under_sc(struct gen_sc *gsc, struct service_curve *sc)
    895 {
    896 	struct segment	*s, *last, *end;
    897 	double		 y;
    898 
    899 	if (is_sc_null(sc)) {
    900 		if (LIST_EMPTY(gsc))
    901 			return (1);
    902 		LIST_FOREACH(s, gsc, _next) {
    903 			if (s->m != 0)
    904 				return (0);
    905 		}
    906 		return (1);
    907 	}
    908 	/*
    909 	 * gsc has a dummy entry at the end with x = INFINITY.
    910 	 * loop through up to this dummy entry.
    911 	 */
    912 	end = gsc_getentry(gsc, INFINITY);
    913 	if (end == NULL)
    914 		return (1);
    915 	last = NULL;
    916 	for (s = LIST_FIRST(gsc); s != end; s = LIST_NEXT(s, _next)) {
    917 		if (s->y > sc_x2y(sc, s->x))
    918 			return (0);
    919 		last = s;
    920 	}
    921 	/* last now holds the real last segment */
    922 	if (last == NULL)
    923 		return (1);
    924 	if (last->m > sc->m2)
    925 		return (0);
    926 	if (last->x < sc->d && last->m > sc->m1) {
    927 		y = last->y + (sc->d - last->x) * last->m;
    928 		if (y > sc_x2y(sc, sc->d))
    929 			return (0);
    930 	}
    931 	return (1);
    932 }
    933 
    934 static void
    935 gsc_destroy(struct gen_sc *gsc)
    936 {
    937 	struct segment	*s;
    938 
    939 	while ((s = LIST_FIRST(gsc)) != NULL) {
    940 		LIST_REMOVE(s, _next);
    941 		free(s);
    942 	}
    943 }
    944 
    945 /*
    946  * return a segment entry starting at x.
    947  * if gsc has no entry starting at x, a new entry is created at x.
    948  */
    949 static struct segment *
    950 gsc_getentry(struct gen_sc *gsc, double x)
    951 {
    952 	struct segment	*new, *prev, *s;
    953 
    954 	prev = NULL;
    955 	LIST_FOREACH(s, gsc, _next) {
    956 		if (s->x == x)
    957 			return (s);	/* matching entry found */
    958 		else if (s->x < x)
    959 			prev = s;
    960 		else
    961 			break;
    962 	}
    963 
    964 	/* we have to create a new entry */
    965 	if ((new = calloc(1, sizeof(struct segment))) == NULL)
    966 		return (NULL);
    967 
    968 	new->x = x;
    969 	if (x == INFINITY || s == NULL)
    970 		new->d = 0;
    971 	else if (s->x == INFINITY)
    972 		new->d = INFINITY;
    973 	else
    974 		new->d = s->x - x;
    975 	if (prev == NULL) {
    976 		/* insert the new entry at the head of the list */
    977 		new->y = 0;
    978 		new->m = 0;
    979 		LIST_INSERT_HEAD(gsc, new, _next);
    980 	} else {
    981 		/*
    982 		 * the start point intersects with the segment pointed by
    983 		 * prev.  divide prev into 2 segments
    984 		 */
    985 		if (x == INFINITY) {
    986 			prev->d = INFINITY;
    987 			if (prev->m == 0)
    988 				new->y = prev->y;
    989 			else
    990 				new->y = INFINITY;
    991 		} else {
    992 			prev->d = x - prev->x;
    993 			new->y = prev->d * prev->m + prev->y;
    994 		}
    995 		new->m = prev->m;
    996 		LIST_INSERT_AFTER(prev, new, _next);
    997 	}
    998 	return (new);
    999 }
   1000 
   1001 /* add a segment to a generalized service curve */
   1002 static int
   1003 gsc_add_seg(struct gen_sc *gsc, double x, double y, double d, double m)
   1004 {
   1005 	struct segment	*start, *end, *s;
   1006 	double		 x2;
   1007 
   1008 	if (d == INFINITY)
   1009 		x2 = INFINITY;
   1010 	else
   1011 		x2 = x + d;
   1012 	start = gsc_getentry(gsc, x);
   1013 	end = gsc_getentry(gsc, x2);
   1014 	if (start == NULL || end == NULL)
   1015 		return (-1);
   1016 
   1017 	for (s = start; s != end; s = LIST_NEXT(s, _next)) {
   1018 		s->m += m;
   1019 		s->y += y + (s->x - x) * m;
   1020 	}
   1021 
   1022 	end = gsc_getentry(gsc, INFINITY);
   1023 	for (; s != end; s = LIST_NEXT(s, _next)) {
   1024 		s->y += m * d;
   1025 	}
   1026 
   1027 	return (0);
   1028 }
   1029 
   1030 /* get y-projection of a service curve */
   1031 static double
   1032 sc_x2y(struct service_curve *sc, double x)
   1033 {
   1034 	double	y;
   1035 
   1036 	if (x <= (double)sc->d)
   1037 		/* y belongs to the 1st segment */
   1038 		y = x * (double)sc->m1;
   1039 	else
   1040 		/* y belongs to the 2nd segment */
   1041 		y = (double)sc->d * (double)sc->m1
   1042 			+ (x - (double)sc->d) * (double)sc->m2;
   1043 	return (y);
   1044 }
   1045 
   1046 /*
   1047  * misc utilities
   1048  */
   1049 #define	R2S_BUFS	8
   1050 #define	RATESTR_MAX	16
   1051 
   1052 char *
   1053 rate2str(double rate)
   1054 {
   1055 	char		*buf;
   1056 	static char	 r2sbuf[R2S_BUFS][RATESTR_MAX];  /* ring bufer */
   1057 	static int	 idx = 0;
   1058 	int		 i;
   1059 	static const char unit[] = " KMG";
   1060 
   1061 	buf = r2sbuf[idx++];
   1062 	if (idx == R2S_BUFS)
   1063 		idx = 0;
   1064 
   1065 	for (i = 0; rate >= 1000 && i <= 3; i++)
   1066 		rate /= 1000;
   1067 
   1068 	if ((int)(rate * 100) % 100)
   1069 		snprintf(buf, RATESTR_MAX, "%.2f%cb", rate, unit[i]);
   1070 	else
   1071 		snprintf(buf, RATESTR_MAX, "%d%cb", (int)rate, unit[i]);
   1072 
   1073 	return (buf);
   1074 }
   1075 
   1076 u_int32_t
   1077 getifspeed(char *ifname)
   1078 {
   1079 	int		s;
   1080 	struct ifreq	ifr;
   1081 	struct if_data	ifrdat;
   1082 
   1083 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
   1084 		err(1, "socket");
   1085 	if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
   1086 	    sizeof(ifr.ifr_name))
   1087 		errx(1, "getifspeed: strlcpy");
   1088 	ifr.ifr_data = (caddr_t)&ifrdat;
   1089 	if (ioctl(s, SIOCGIFDATA, (caddr_t)&ifr) == -1)
   1090 		err(1, "SIOCGIFDATA");
   1091 	if (shutdown(s, SHUT_RDWR) == -1)
   1092 		err(1, "shutdown");
   1093 	if (close(s))
   1094 		err(1, "close");
   1095 	return ((u_int32_t)ifrdat.ifi_baudrate);
   1096 }
   1097 
   1098 u_long
   1099 getifmtu(char *ifname)
   1100 {
   1101 	int		s;
   1102 	struct ifreq	ifr;
   1103 
   1104 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
   1105 		err(1, "socket");
   1106 	if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
   1107 	    sizeof(ifr.ifr_name))
   1108 		errx(1, "getifmtu: strlcpy");
   1109 	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) == -1)
   1110 		err(1, "SIOCGIFMTU");
   1111 	if (shutdown(s, SHUT_RDWR) == -1)
   1112 		err(1, "shutdown");
   1113 	if (close(s))
   1114 		err(1, "close");
   1115 	if (ifr.ifr_mtu > 0)
   1116 		return (ifr.ifr_mtu);
   1117 	else {
   1118 		warnx("could not get mtu for %s, assuming 1500", ifname);
   1119 		return (1500);
   1120 	}
   1121 }
   1122 
   1123 int
   1124 eval_queue_opts(struct pf_altq *pa, struct node_queue_opt *opts,
   1125     u_int32_t ref_bw)
   1126 {
   1127 	int	errors = 0;
   1128 
   1129 	switch (pa->scheduler) {
   1130 	case ALTQT_CBQ:
   1131 		pa->pq_u.cbq_opts = opts->data.cbq_opts;
   1132 		break;
   1133 	case ALTQT_PRIQ:
   1134 		pa->pq_u.priq_opts = opts->data.priq_opts;
   1135 		break;
   1136 	case ALTQT_HFSC:
   1137 		pa->pq_u.hfsc_opts.flags = opts->data.hfsc_opts.flags;
   1138 		if (opts->data.hfsc_opts.linkshare.used) {
   1139 			pa->pq_u.hfsc_opts.lssc_m1 =
   1140 			    eval_bwspec(&opts->data.hfsc_opts.linkshare.m1,
   1141 			    ref_bw);
   1142 			pa->pq_u.hfsc_opts.lssc_m2 =
   1143 			    eval_bwspec(&opts->data.hfsc_opts.linkshare.m2,
   1144 			    ref_bw);
   1145 			pa->pq_u.hfsc_opts.lssc_d =
   1146 			    opts->data.hfsc_opts.linkshare.d;
   1147 		}
   1148 		if (opts->data.hfsc_opts.realtime.used) {
   1149 			pa->pq_u.hfsc_opts.rtsc_m1 =
   1150 			    eval_bwspec(&opts->data.hfsc_opts.realtime.m1,
   1151 			    ref_bw);
   1152 			pa->pq_u.hfsc_opts.rtsc_m2 =
   1153 			    eval_bwspec(&opts->data.hfsc_opts.realtime.m2,
   1154 			    ref_bw);
   1155 			pa->pq_u.hfsc_opts.rtsc_d =
   1156 			    opts->data.hfsc_opts.realtime.d;
   1157 		}
   1158 		if (opts->data.hfsc_opts.upperlimit.used) {
   1159 			pa->pq_u.hfsc_opts.ulsc_m1 =
   1160 			    eval_bwspec(&opts->data.hfsc_opts.upperlimit.m1,
   1161 			    ref_bw);
   1162 			pa->pq_u.hfsc_opts.ulsc_m2 =
   1163 			    eval_bwspec(&opts->data.hfsc_opts.upperlimit.m2,
   1164 			    ref_bw);
   1165 			pa->pq_u.hfsc_opts.ulsc_d =
   1166 			    opts->data.hfsc_opts.upperlimit.d;
   1167 		}
   1168 		break;
   1169 	default:
   1170 		warnx("eval_queue_opts: unknown scheduler type %u",
   1171 		    opts->qtype);
   1172 		errors++;
   1173 		break;
   1174 	}
   1175 
   1176 	return (errors);
   1177 }
   1178 
   1179 u_int32_t
   1180 eval_bwspec(struct node_queue_bw *bw, u_int32_t ref_bw)
   1181 {
   1182 	if (bw->bw_absolute > 0)
   1183 		return (bw->bw_absolute);
   1184 
   1185 	if (bw->bw_percent > 0)
   1186 		return (ref_bw / 100 * bw->bw_percent);
   1187 
   1188 	return (0);
   1189 }
   1190 
   1191 void
   1192 print_hfsc_sc(const char *scname, u_int m1, u_int d, u_int m2,
   1193     const struct node_hfsc_sc *sc)
   1194 {
   1195 	printf(" %s", scname);
   1196 
   1197 	if (d != 0) {
   1198 		printf("(");
   1199 		if (sc != NULL && sc->m1.bw_percent > 0)
   1200 			printf("%u%%", sc->m1.bw_percent);
   1201 		else
   1202 			printf("%s", rate2str((double)m1));
   1203 		printf(" %u", d);
   1204 	}
   1205 
   1206 	if (sc != NULL && sc->m2.bw_percent > 0)
   1207 		printf(" %u%%", sc->m2.bw_percent);
   1208 	else
   1209 		printf(" %s", rate2str((double)m2));
   1210 
   1211 	if (d != 0)
   1212 		printf(")");
   1213 }
   1214