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