xref: /freebsd/sbin/ipfw/dummynet.c (revision 908e960ea6343acd9515d89d5d5696f9d8bf090c)
1 /*
2  * Copyright (c) 2002-2003 Luigi Rizzo
3  * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4  * Copyright (c) 1994 Ugen J.S.Antsilevich
5  *
6  * Idea and grammar partially left from:
7  * Copyright (c) 1993 Daniel Boulet
8  *
9  * Redistribution and use in source forms, with and without modification,
10  * are permitted provided that this entire comment appears intact.
11  *
12  * Redistribution in binary form may occur without any restrictions.
13  * Obviously, it would be nice if you gave credit where credit is due
14  * but requiring it would be too onerous.
15  *
16  * This software is provided ``AS IS'' without any warranties of any kind.
17  *
18  * NEW command line interface for IP firewall facility
19  *
20  * $FreeBSD$
21  *
22  * dummynet support
23  */
24 
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/queue.h>
28 /* XXX there are several sysctl leftover here */
29 #include <sys/sysctl.h>
30 
31 #include "ipfw2.h"
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <netdb.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/ip_fw.h>
44 #include <netinet/ip_dummynet.h>
45 #include <arpa/inet.h>	/* inet_ntoa */
46 
47 static struct _s_x dummynet_params[] = {
48 	{ "plr",		TOK_PLR },
49 	{ "noerror",		TOK_NOERROR },
50 	{ "buckets",		TOK_BUCKETS },
51 	{ "dst-ip",		TOK_DSTIP },
52 	{ "src-ip",		TOK_SRCIP },
53 	{ "dst-port",		TOK_DSTPORT },
54 	{ "src-port",		TOK_SRCPORT },
55 	{ "proto",		TOK_PROTO },
56 	{ "weight",		TOK_WEIGHT },
57 	{ "all",		TOK_ALL },
58 	{ "mask",		TOK_MASK },
59 	{ "droptail",		TOK_DROPTAIL },
60 	{ "red",		TOK_RED },
61 	{ "gred",		TOK_GRED },
62 	{ "bw",			TOK_BW },
63 	{ "bandwidth",		TOK_BW },
64 	{ "delay",		TOK_DELAY },
65 	{ "pipe",		TOK_PIPE },
66 	{ "queue",		TOK_QUEUE },
67 	{ "flow-id",		TOK_FLOWID},
68 	{ "dst-ipv6",		TOK_DSTIP6},
69 	{ "dst-ip6",		TOK_DSTIP6},
70 	{ "src-ipv6",		TOK_SRCIP6},
71 	{ "src-ip6",		TOK_SRCIP6},
72 	{ "profile",		TOK_PIPE_PROFILE},
73 	{ "dummynet-params",	TOK_NULL },
74 	{ NULL, 0 }	/* terminator */
75 };
76 
77 static int
78 sort_q(const void *pa, const void *pb)
79 {
80 	int rev = (co.do_sort < 0);
81 	int field = rev ? -co.do_sort : co.do_sort;
82 	long long res = 0;
83 	const struct dn_flow_queue *a = pa;
84 	const struct dn_flow_queue *b = pb;
85 
86 	switch (field) {
87 	case 1: /* pkts */
88 		res = a->len - b->len;
89 		break;
90 	case 2: /* bytes */
91 		res = a->len_bytes - b->len_bytes;
92 		break;
93 
94 	case 3: /* tot pkts */
95 		res = a->tot_pkts - b->tot_pkts;
96 		break;
97 
98 	case 4: /* tot bytes */
99 		res = a->tot_bytes - b->tot_bytes;
100 		break;
101 	}
102 	if (res < 0)
103 		res = -1;
104 	if (res > 0)
105 		res = 1;
106 	return (int)(rev ? res : -res);
107 }
108 
109 static void
110 list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
111 {
112 	int l;
113 	int index_printed, indexes = 0;
114 	char buff[255];
115 	struct protoent *pe;
116 
117 	if (fs->rq_elements == 0)
118 		return;
119 
120 	if (co.do_sort != 0)
121 		heapsort(q, fs->rq_elements, sizeof *q, sort_q);
122 
123 	/* Print IPv4 flows */
124 	index_printed = 0;
125 	for (l = 0; l < fs->rq_elements; l++) {
126 		struct in_addr ina;
127 
128 		/* XXX: Should check for IPv4 flows */
129 		if (IS_IP6_FLOW_ID(&(q[l].id)))
130 			continue;
131 
132 		if (!index_printed) {
133 			index_printed = 1;
134 			if (indexes > 0)	/* currently a no-op */
135 				printf("\n");
136 			indexes++;
137 			printf("    "
138 			    "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
139 			    fs->flow_mask.proto,
140 			    fs->flow_mask.src_ip, fs->flow_mask.src_port,
141 			    fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
142 
143 			printf("BKT Prot ___Source IP/port____ "
144 			    "____Dest. IP/port____ "
145 			    "Tot_pkt/bytes Pkt/Byte Drp\n");
146 		}
147 
148 		printf("%3d ", q[l].hash_slot);
149 		pe = getprotobynumber(q[l].id.proto);
150 		if (pe)
151 			printf("%-4s ", pe->p_name);
152 		else
153 			printf("%4u ", q[l].id.proto);
154 		ina.s_addr = htonl(q[l].id.src_ip);
155 		printf("%15s/%-5d ",
156 		    inet_ntoa(ina), q[l].id.src_port);
157 		ina.s_addr = htonl(q[l].id.dst_ip);
158 		printf("%15s/%-5d ",
159 		    inet_ntoa(ina), q[l].id.dst_port);
160 		printf("%4llu %8llu %2u %4u %3u\n",
161 		    align_uint64(&q[l].tot_pkts),
162 		    align_uint64(&q[l].tot_bytes),
163 		    q[l].len, q[l].len_bytes, q[l].drops);
164 		if (co.verbose)
165 			printf("   S %20llu  F %20llu\n",
166 			    align_uint64(&q[l].S), align_uint64(&q[l].F));
167 	}
168 
169 	/* Print IPv6 flows */
170 	index_printed = 0;
171 	for (l = 0; l < fs->rq_elements; l++) {
172 		if (!IS_IP6_FLOW_ID(&(q[l].id)))
173 			continue;
174 
175 		if (!index_printed) {
176 			index_printed = 1;
177 			if (indexes > 0)
178 				printf("\n");
179 			indexes++;
180 			printf("\n        mask: proto: 0x%02x, flow_id: 0x%08x,  ",
181 			    fs->flow_mask.proto, fs->flow_mask.flow_id6);
182 			inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
183 			    buff, sizeof(buff));
184 			printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
185 			inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
186 			    buff, sizeof(buff) );
187 			printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
188 
189 			printf("BKT ___Prot___ _flow-id_ "
190 			    "______________Source IPv6/port_______________ "
191 			    "_______________Dest. IPv6/port_______________ "
192 			    "Tot_pkt/bytes Pkt/Byte Drp\n");
193 		}
194 		printf("%3d ", q[l].hash_slot);
195 		pe = getprotobynumber(q[l].id.proto);
196 		if (pe != NULL)
197 			printf("%9s ", pe->p_name);
198 		else
199 			printf("%9u ", q[l].id.proto);
200 		printf("%7d  %39s/%-5d ", q[l].id.flow_id6,
201 		    inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
202 		    q[l].id.src_port);
203 		printf(" %39s/%-5d ",
204 		    inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
205 		    q[l].id.dst_port);
206 		printf(" %4llu %8llu %2u %4u %3u\n",
207 		    align_uint64(&q[l].tot_pkts),
208 		    align_uint64(&q[l].tot_bytes),
209 		    q[l].len, q[l].len_bytes, q[l].drops);
210 		if (co.verbose)
211 			printf("   S %20llu  F %20llu\n",
212 			    align_uint64(&q[l].S),
213 			    align_uint64(&q[l].F));
214 	}
215 }
216 
217 static void
218 print_flowset_parms(struct dn_flow_set *fs, char *prefix)
219 {
220 	int l;
221 	char qs[30];
222 	char plr[30];
223 	char red[90];	/* Display RED parameters */
224 
225 	l = fs->qsize;
226 	if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
227 		if (l >= 8192)
228 			sprintf(qs, "%d KB", l / 1024);
229 		else
230 			sprintf(qs, "%d B", l);
231 	} else
232 		sprintf(qs, "%3d sl.", l);
233 	if (fs->plr)
234 		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
235 	else
236 		plr[0] = '\0';
237 	if (fs->flags_fs & DN_IS_RED)	/* RED parameters */
238 		sprintf(red,
239 		    "\n\t  %cRED w_q %f min_th %d max_th %d max_p %f",
240 		    (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
241 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
242 		    SCALE_VAL(fs->min_th),
243 		    SCALE_VAL(fs->max_th),
244 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
245 	else
246 		sprintf(red, "droptail");
247 
248 	printf("%s %s%s %d queues (%d buckets) %s\n",
249 	    prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
250 }
251 
252 static void
253 print_extra_delay_parms(struct dn_pipe *p, char *prefix)
254 {
255 	double loss;
256 	if (p->samples_no <= 0)
257 		return;
258 
259 	loss = p->loss_level;
260 	loss /= p->samples_no;
261 	printf("%s profile: name \"%s\" loss %f samples %d\n",
262 		prefix, p->name, loss, p->samples_no);
263 }
264 
265 void
266 ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
267 {
268 	int rulenum;
269 	void *next = data;
270 	struct dn_pipe *p = (struct dn_pipe *) data;
271 	struct dn_flow_set *fs;
272 	struct dn_flow_queue *q;
273 	int l;
274 
275 	if (ac > 0)
276 		rulenum = strtoul(*av++, NULL, 10);
277 	else
278 		rulenum = 0;
279 	for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
280 		double b = p->bandwidth;
281 		char buf[30];
282 		char prefix[80];
283 
284 		if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
285 			break;	/* done with pipes, now queues */
286 
287 		/*
288 		 * compute length, as pipe have variable size
289 		 */
290 		l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
291 		next = (char *)p + l;
292 		nbytes -= l;
293 
294 		if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
295 			continue;
296 
297 		/*
298 		 * Print rate (or clocking interface)
299 		 */
300 		if (p->if_name[0] != '\0')
301 			sprintf(buf, "%s", p->if_name);
302 		else if (b == 0)
303 			sprintf(buf, "unlimited");
304 		else if (b >= 1000000)
305 			sprintf(buf, "%7.3f Mbit/s", b/1000000);
306 		else if (b >= 1000)
307 			sprintf(buf, "%7.3f Kbit/s", b/1000);
308 		else
309 			sprintf(buf, "%7.3f bit/s ", b);
310 
311 		sprintf(prefix, "%05d: %s %4d ms ",
312 		    p->pipe_nr, buf, p->delay);
313 
314 		print_extra_delay_parms(p, prefix);
315 
316 		print_flowset_parms(&(p->fs), prefix);
317 
318 		q = (struct dn_flow_queue *)(p+1);
319 		list_queues(&(p->fs), q);
320 	}
321 	for (fs = next; nbytes >= sizeof *fs; fs = next) {
322 		char prefix[80];
323 
324 		if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
325 			break;
326 		l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
327 		next = (char *)fs + l;
328 		nbytes -= l;
329 
330 		if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
331 		    (rulenum != fs->parent_nr && co.do_pipe == 1))) {
332 			continue;
333 		}
334 
335 		q = (struct dn_flow_queue *)(fs+1);
336 		sprintf(prefix, "q%05d: weight %d pipe %d ",
337 		    fs->fs_nr, fs->weight, fs->parent_nr);
338 		print_flowset_parms(fs, prefix);
339 		list_queues(fs, q);
340 	}
341 }
342 
343 /*
344  * Delete pipe or queue i
345  */
346 int
347 ipfw_delete_pipe(int pipe_or_queue, int i)
348 {
349 	struct dn_pipe p;
350 
351 	memset(&p, 0, sizeof p);
352 	if (pipe_or_queue == 1)
353 		p.pipe_nr = i;		/* pipe */
354 	else
355 		p.fs.fs_nr = i;		/* queue */
356 	i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
357 	if (i) {
358 		i = 1;
359 		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
360 	}
361 	return i;
362 }
363 
364 /*
365  * Code to parse delay profiles.
366  *
367  * Some link types introduce extra delays in the transmission
368  * of a packet, e.g. because of MAC level framing, contention on
369  * the use of the channel, MAC level retransmissions and so on.
370  * From our point of view, the channel is effectively unavailable
371  * for this extra time, which is constant or variable depending
372  * on the link type. Additionally, packets may be dropped after this
373  * time (e.g. on a wireless link after too many retransmissions).
374  * We can model the additional delay with an empirical curve
375  * that represents its distribution.
376  *
377  *	cumulative probability
378  *	1.0 ^
379  *	    |
380  *	L   +-- loss-level          x
381  *	    |                 ******
382  *	    |                *
383  *	    |           *****
384  *	    |          *
385  *	    |        **
386  *	    |       *
387  *	    +-------*------------------->
388  *			delay
389  *
390  * The empirical curve may have both vertical and horizontal lines.
391  * Vertical lines represent constant delay for a range of
392  * probabilities; horizontal lines correspond to a discontinuty
393  * in the delay distribution: the pipe will use the largest delay
394  * for a given probability.
395  *
396  * To pass the curve to dummynet, we must store the parameters
397  * in a file as described below, and issue the command
398  *
399  *      ipfw pipe <n> config ... bw XXX profile <filename> ...
400  *
401  * The file format is the following, with whitespace acting as
402  * a separator and '#' indicating the beginning a comment:
403  *
404  *	samples N
405  *		the number of samples used in the internal
406  *		representation (2..1024; default 100);
407  *
408  *	loss-level L
409  *		The probability above which packets are lost.
410  *               (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
411  *
412  *	name identifier
413  *		Optional a name (listed by "ipfw pipe show")
414  *		to identify the distribution;
415  *
416  *	"delay prob" | "prob delay"
417  *		One of these two lines is mandatory and defines
418  *		the format of the following lines with data points.
419  *
420  *	XXX YYY
421  *		2 or more lines representing points in the curve,
422  *		with either delay or probability first, according
423  *		to the chosen format.
424  *		The unit for delay is milliseconds.
425  *
426  * Data points does not need to be ordered or equal to the number
427  * specified in the "samples" line. ipfw will sort and interpolate
428  * the curve as needed.
429  *
430  * Example of a profile file:
431 
432         name    bla_bla_bla
433         samples 100
434         loss-level    0.86
435         prob    delay
436         0       200	# minimum overhead is 200ms
437         0.5     200
438         0.5     300
439         0.8     1000
440         0.9     1300
441         1       1300
442 
443  * Internally, we will convert the curve to a fixed number of
444  * samples, and when it is time to transmit a packet we will
445  * model the extra delay as extra bits in the packet.
446  *
447  */
448 
449 #define ED_MAX_LINE_LEN	256+ED_MAX_NAME_LEN
450 #define ED_TOK_SAMPLES	"samples"
451 #define ED_TOK_LOSS	"loss-level"
452 #define ED_TOK_NAME	"name"
453 #define ED_TOK_DELAY	"delay"
454 #define ED_TOK_PROB	"prob"
455 #define ED_SEPARATORS	" \t\n"
456 #define ED_MIN_SAMPLES_NO	2
457 
458 /*
459  * returns 1 if s is a non-negative number, with at least one '.'
460  */
461 static int
462 is_valid_number(const char *s)
463 {
464 	int i, dots_found = 0;
465 	int len = strlen(s);
466 
467 	for (i = 0; i<len; ++i)
468 		if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
469 			return 0;
470 	return 1;
471 }
472 
473 struct point {
474 	double prob;
475 	double delay;
476 };
477 
478 int
479 compare_points(const void *vp1, const void *vp2)
480 {
481 	const struct point *p1 = vp1;
482 	const struct point *p2 = vp2;
483 	double res = 0;
484 
485 	res = p1->prob - p2->prob;
486 	if (res == 0)
487 		res = p1->delay - p2->delay;
488 	if (res < 0)
489 		return -1;
490 	else if (res > 0)
491 		return 1;
492 	else
493 		return 0;
494 }
495 
496 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
497 
498 static void
499 load_extra_delays(const char *filename, struct dn_pipe *p)
500 {
501 	char    line[ED_MAX_LINE_LEN];
502 	FILE    *f;
503 	int     lineno = 0;
504 	int     i;
505 
506 	int     samples = -1;
507 	double  loss = -1.0;
508 	char    profile_name[ED_MAX_NAME_LEN];
509 	int     delay_first = -1;
510 	int     do_points = 0;
511 	struct point    points[ED_MAX_SAMPLES_NO];
512 	int     points_no = 0;
513 
514 	profile_name[0] = '\0';
515 	f = fopen(filename, "r");
516 	if (f == NULL)
517 		err(EX_UNAVAILABLE, "fopen: %s", filename);
518 
519 	while (fgets(line, ED_MAX_LINE_LEN, f)) {         /* read commands */
520 		char *s, *cur = line, *name = NULL, *arg = NULL;
521 
522 		++lineno;
523 
524 		/* parse the line */
525 		while (cur) {
526 			s = strsep(&cur, ED_SEPARATORS);
527 			if (s == NULL || *s == '#')
528 				break;
529 			if (*s == '\0')
530 				continue;
531 			if (arg)
532 				errx(ED_EFMT("too many arguments"));
533 			if (name == NULL)
534 				name = s;
535 			else
536 				arg = s;
537 		}
538 		if (name == NULL)	/* empty line */
539 			continue;
540 		if (arg == NULL)
541 			errx(ED_EFMT("missing arg for %s"), name);
542 
543 		if (!strcasecmp(name, ED_TOK_SAMPLES)) {
544 		    if (samples > 0)
545 			errx(ED_EFMT("duplicate ``samples'' line"));
546 		    if (atoi(arg) <=0)
547 			errx(ED_EFMT("invalid number of samples"));
548 		    samples = atoi(arg);
549 		    if (samples>ED_MAX_SAMPLES_NO)
550 			    errx(ED_EFMT("too many samples, maximum is %d"),
551 				ED_MAX_SAMPLES_NO);
552 		    do_points = 0;
553 		} else if (!strcasecmp(name, ED_TOK_LOSS)) {
554 		    if (loss != -1.0)
555 			errx(ED_EFMT("duplicated token: %s"), name);
556 		    if (!is_valid_number(arg))
557 			errx(ED_EFMT("invalid %s"), arg);
558 		    loss = atof(arg);
559 		    if (loss > 1)
560 			errx(ED_EFMT("%s greater than 1.0"), name);
561 		    do_points = 0;
562 		} else if (!strcasecmp(name, ED_TOK_NAME)) {
563 		    if (profile_name[0] != '\0')
564 			errx(ED_EFMT("duplicated token: %s"), name);
565 		    strncpy(profile_name, arg, sizeof(profile_name) - 1);
566 		    profile_name[sizeof(profile_name)-1] = '\0';
567 		    do_points = 0;
568 		} else if (!strcasecmp(name, ED_TOK_DELAY)) {
569 		    if (do_points)
570 			errx(ED_EFMT("duplicated token: %s"), name);
571 		    delay_first = 1;
572 		    do_points = 1;
573 		} else if (!strcasecmp(name, ED_TOK_PROB)) {
574 		    if (do_points)
575 			errx(ED_EFMT("duplicated token: %s"), name);
576 		    delay_first = 0;
577 		    do_points = 1;
578 		} else if (do_points) {
579 		    if (!is_valid_number(name) || !is_valid_number(arg))
580 			errx(ED_EFMT("invalid point found"));
581 		    if (delay_first) {
582 			points[points_no].delay = atof(name);
583 			points[points_no].prob = atof(arg);
584 		    } else {
585 			points[points_no].delay = atof(arg);
586 			points[points_no].prob = atof(name);
587 		    }
588 		    if (points[points_no].prob > 1.0)
589 			errx(ED_EFMT("probability greater than 1.0"));
590 		    ++points_no;
591 		} else {
592 		    errx(ED_EFMT("unrecognised command '%s'"), name);
593 		}
594 	}
595 
596 	if (samples == -1) {
597 	    warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
598 	    samples = 100;
599 	}
600 
601 	if (loss == -1.0) {
602 	    warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
603 	    loss = 1;
604 	}
605 
606 	/* make sure that there are enough points. */
607 	if (points_no < ED_MIN_SAMPLES_NO)
608 	    errx(ED_EFMT("too few samples, need at least %d"),
609 		ED_MIN_SAMPLES_NO);
610 
611 	qsort(points, points_no, sizeof(struct point), compare_points);
612 
613 	/* interpolation */
614 	for (i = 0; i<points_no-1; ++i) {
615 	    double y1 = points[i].prob * samples;
616 	    double x1 = points[i].delay;
617 	    double y2 = points[i+1].prob * samples;
618 	    double x2 = points[i+1].delay;
619 
620 	    int index = y1;
621 	    int stop = y2;
622 
623 	    if (x1 == x2) {
624 		for (; index<stop; ++index)
625 		    p->samples[index] = x1;
626 	    } else {
627 		double m = (y2-y1)/(x2-x1);
628 		double c = y1 - m*x1;
629 		for (; index<stop ; ++index)
630 		    p->samples[index] = (index - c)/m;
631 	    }
632 	}
633 	p->samples_no = samples;
634 	p->loss_level = loss * samples;
635 	strncpy(p->name, profile_name, sizeof(p->name));
636 }
637 
638 void
639 ipfw_config_pipe(int ac, char **av)
640 {
641 	int samples[ED_MAX_SAMPLES_NO];
642 	struct dn_pipe p;
643 	int i;
644 	char *end;
645 	void *par = NULL;
646 
647 	memset(&p, 0, sizeof p);
648 
649 	av++; ac--;
650 	/* Pipe number */
651 	if (ac && isdigit(**av)) {
652 		i = atoi(*av); av++; ac--;
653 		if (co.do_pipe == 1)
654 			p.pipe_nr = i;
655 		else
656 			p.fs.fs_nr = i;
657 	}
658 	while (ac > 0) {
659 		double d;
660 		int tok = match_token(dummynet_params, *av);
661 		ac--; av++;
662 
663 		switch(tok) {
664 		case TOK_NOERROR:
665 			p.fs.flags_fs |= DN_NOERROR;
666 			break;
667 
668 		case TOK_PLR:
669 			NEED1("plr needs argument 0..1\n");
670 			d = strtod(av[0], NULL);
671 			if (d > 1)
672 				d = 1;
673 			else if (d < 0)
674 				d = 0;
675 			p.fs.plr = (int)(d*0x7fffffff);
676 			ac--; av++;
677 			break;
678 
679 		case TOK_QUEUE:
680 			NEED1("queue needs queue size\n");
681 			end = NULL;
682 			p.fs.qsize = strtoul(av[0], &end, 0);
683 			if (*end == 'K' || *end == 'k') {
684 				p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
685 				p.fs.qsize *= 1024;
686 			} else if (*end == 'B' ||
687 			    _substrcmp2(end, "by", "bytes") == 0) {
688 				p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
689 			}
690 			ac--; av++;
691 			break;
692 
693 		case TOK_BUCKETS:
694 			NEED1("buckets needs argument\n");
695 			p.fs.rq_size = strtoul(av[0], NULL, 0);
696 			ac--; av++;
697 			break;
698 
699 		case TOK_MASK:
700 			NEED1("mask needs mask specifier\n");
701 			/*
702 			 * per-flow queue, mask is dst_ip, dst_port,
703 			 * src_ip, src_port, proto measured in bits
704 			 */
705 			par = NULL;
706 
707 			bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
708 			end = NULL;
709 
710 			while (ac >= 1) {
711 			    uint32_t *p32 = NULL;
712 			    uint16_t *p16 = NULL;
713 			    uint32_t *p20 = NULL;
714 			    struct in6_addr *pa6 = NULL;
715 			    uint32_t a;
716 
717 			    tok = match_token(dummynet_params, *av);
718 			    ac--; av++;
719 			    switch(tok) {
720 			    case TOK_ALL:
721 				    /*
722 				     * special case, all bits significant
723 				     */
724 				    p.fs.flow_mask.dst_ip = ~0;
725 				    p.fs.flow_mask.src_ip = ~0;
726 				    p.fs.flow_mask.dst_port = ~0;
727 				    p.fs.flow_mask.src_port = ~0;
728 				    p.fs.flow_mask.proto = ~0;
729 				    n2mask(&(p.fs.flow_mask.dst_ip6), 128);
730 				    n2mask(&(p.fs.flow_mask.src_ip6), 128);
731 				    p.fs.flow_mask.flow_id6 = ~0;
732 				    p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
733 				    goto end_mask;
734 
735 			    case TOK_DSTIP:
736 				    p32 = &p.fs.flow_mask.dst_ip;
737 				    break;
738 
739 			    case TOK_SRCIP:
740 				    p32 = &p.fs.flow_mask.src_ip;
741 				    break;
742 
743 			    case TOK_DSTIP6:
744 				    pa6 = &(p.fs.flow_mask.dst_ip6);
745 				    break;
746 
747 			    case TOK_SRCIP6:
748 				    pa6 = &(p.fs.flow_mask.src_ip6);
749 				    break;
750 
751 			    case TOK_FLOWID:
752 				    p20 = &p.fs.flow_mask.flow_id6;
753 				    break;
754 
755 			    case TOK_DSTPORT:
756 				    p16 = &p.fs.flow_mask.dst_port;
757 				    break;
758 
759 			    case TOK_SRCPORT:
760 				    p16 = &p.fs.flow_mask.src_port;
761 				    break;
762 
763 			    case TOK_PROTO:
764 				    break;
765 
766 			    default:
767 				    ac++; av--; /* backtrack */
768 				    goto end_mask;
769 			    }
770 			    if (ac < 1)
771 				    errx(EX_USAGE, "mask: value missing");
772 			    if (*av[0] == '/') {
773 				    a = strtoul(av[0]+1, &end, 0);
774 				    if (pa6 == NULL)
775 					    a = (a == 32) ? ~0 : (1 << a) - 1;
776 			    } else
777 				    a = strtoul(av[0], &end, 0);
778 			    if (p32 != NULL)
779 				    *p32 = a;
780 			    else if (p16 != NULL) {
781 				    if (a > 0xFFFF)
782 					    errx(EX_DATAERR,
783 						"port mask must be 16 bit");
784 				    *p16 = (uint16_t)a;
785 			    } else if (p20 != NULL) {
786 				    if (a > 0xfffff)
787 					errx(EX_DATAERR,
788 					    "flow_id mask must be 20 bit");
789 				    *p20 = (uint32_t)a;
790 			    } else if (pa6 != NULL) {
791 				    if (a > 128)
792 					errx(EX_DATAERR,
793 					    "in6addr invalid mask len");
794 				    else
795 					n2mask(pa6, a);
796 			    } else {
797 				    if (a > 0xFF)
798 					    errx(EX_DATAERR,
799 						"proto mask must be 8 bit");
800 				    p.fs.flow_mask.proto = (uint8_t)a;
801 			    }
802 			    if (a != 0)
803 				    p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
804 			    ac--; av++;
805 			} /* end while, config masks */
806 end_mask:
807 			break;
808 
809 		case TOK_RED:
810 		case TOK_GRED:
811 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
812 			p.fs.flags_fs |= DN_IS_RED;
813 			if (tok == TOK_GRED)
814 				p.fs.flags_fs |= DN_IS_GENTLE_RED;
815 			/*
816 			 * the format for parameters is w_q/min_th/max_th/max_p
817 			 */
818 			if ((end = strsep(&av[0], "/"))) {
819 			    double w_q = strtod(end, NULL);
820 			    if (w_q > 1 || w_q <= 0)
821 				errx(EX_DATAERR, "0 < w_q <= 1");
822 			    p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
823 			}
824 			if ((end = strsep(&av[0], "/"))) {
825 			    p.fs.min_th = strtoul(end, &end, 0);
826 			    if (*end == 'K' || *end == 'k')
827 				p.fs.min_th *= 1024;
828 			}
829 			if ((end = strsep(&av[0], "/"))) {
830 			    p.fs.max_th = strtoul(end, &end, 0);
831 			    if (*end == 'K' || *end == 'k')
832 				p.fs.max_th *= 1024;
833 			}
834 			if ((end = strsep(&av[0], "/"))) {
835 			    double max_p = strtod(end, NULL);
836 			    if (max_p > 1 || max_p <= 0)
837 				errx(EX_DATAERR, "0 < max_p <= 1");
838 			    p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
839 			}
840 			ac--; av++;
841 			break;
842 
843 		case TOK_DROPTAIL:
844 			p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
845 			break;
846 
847 		case TOK_BW:
848 			NEED1("bw needs bandwidth or interface\n");
849 			if (co.do_pipe != 1)
850 			    errx(EX_DATAERR, "bandwidth only valid for pipes");
851 			/*
852 			 * set clocking interface or bandwidth value
853 			 */
854 			if (av[0][0] >= 'a' && av[0][0] <= 'z') {
855 			    int l = sizeof(p.if_name)-1;
856 			    /* interface name */
857 			    strncpy(p.if_name, av[0], l);
858 			    p.if_name[l] = '\0';
859 			    p.bandwidth = 0;
860 			} else {
861 			    p.if_name[0] = '\0';
862 			    p.bandwidth = strtoul(av[0], &end, 0);
863 			    if (*end == 'K' || *end == 'k') {
864 				end++;
865 				p.bandwidth *= 1000;
866 			    } else if (*end == 'M') {
867 				end++;
868 				p.bandwidth *= 1000000;
869 			    }
870 			    if ((*end == 'B' &&
871 				  _substrcmp2(end, "Bi", "Bit/s") != 0) ||
872 			        _substrcmp2(end, "by", "bytes") == 0)
873 				p.bandwidth *= 8;
874 			    if (p.bandwidth < 0)
875 				errx(EX_DATAERR, "bandwidth too large");
876 			}
877 			ac--; av++;
878 			break;
879 
880 		case TOK_DELAY:
881 			if (co.do_pipe != 1)
882 				errx(EX_DATAERR, "delay only valid for pipes");
883 			NEED1("delay needs argument 0..10000ms\n");
884 			p.delay = strtoul(av[0], NULL, 0);
885 			ac--; av++;
886 			break;
887 
888 		case TOK_WEIGHT:
889 			if (co.do_pipe == 1)
890 				errx(EX_DATAERR,"weight only valid for queues");
891 			NEED1("weight needs argument 0..100\n");
892 			p.fs.weight = strtoul(av[0], &end, 0);
893 			ac--; av++;
894 			break;
895 
896 		case TOK_PIPE:
897 			if (co.do_pipe == 1)
898 				errx(EX_DATAERR,"pipe only valid for queues");
899 			NEED1("pipe needs pipe_number\n");
900 			p.fs.parent_nr = strtoul(av[0], &end, 0);
901 			ac--; av++;
902 			break;
903 
904 		case TOK_PIPE_PROFILE:
905 			if (co.do_pipe != 1)
906 			    errx(EX_DATAERR, "extra delay only valid for pipes");
907 			NEED1("extra delay needs the file name\n");
908 			p.samples = &samples[0];
909 			load_extra_delays(av[0], &p);
910 			--ac; ++av;
911 			break;
912 
913 		default:
914 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
915 		}
916 	}
917 	if (co.do_pipe == 1) {
918 		if (p.pipe_nr == 0)
919 			errx(EX_DATAERR, "pipe_nr must be > 0");
920 		if (p.delay > 10000)
921 			errx(EX_DATAERR, "delay must be < 10000");
922 		if (p.samples_no > 0 && p.bandwidth == 0)
923 			errx(EX_DATAERR,
924 				"profile requires a bandwidth limit");
925 	} else { /* co.do_pipe == 2, queue */
926 		if (p.fs.parent_nr == 0)
927 			errx(EX_DATAERR, "pipe must be > 0");
928 		if (p.fs.weight >100)
929 			errx(EX_DATAERR, "weight must be <= 100");
930 	}
931 	if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
932 		size_t len;
933 		long limit;
934 
935 		len = sizeof(limit);
936 		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
937 			&limit, &len, NULL, 0) == -1)
938 			limit = 1024*1024;
939 		if (p.fs.qsize > limit)
940 			errx(EX_DATAERR, "queue size must be < %ldB", limit);
941 	} else {
942 		size_t len;
943 		long limit;
944 
945 		len = sizeof(limit);
946 		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
947 			&limit, &len, NULL, 0) == -1)
948 			limit = 100;
949 		if (p.fs.qsize > limit)
950 			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
951 	}
952 	if (p.fs.flags_fs & DN_IS_RED) {
953 		size_t len;
954 		int lookup_depth, avg_pkt_size;
955 		double s, idle, weight, w_q;
956 		struct clockinfo ck;
957 		int t;
958 
959 		if (p.fs.min_th >= p.fs.max_th)
960 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
961 			p.fs.min_th, p.fs.max_th);
962 		if (p.fs.max_th == 0)
963 		    errx(EX_DATAERR, "max_th must be > 0");
964 
965 		len = sizeof(int);
966 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
967 			&lookup_depth, &len, NULL, 0) == -1)
968 		    errx(1, "sysctlbyname(\"%s\")",
969 			"net.inet.ip.dummynet.red_lookup_depth");
970 		if (lookup_depth == 0)
971 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
972 			" must be greater than zero");
973 
974 		len = sizeof(int);
975 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
976 			&avg_pkt_size, &len, NULL, 0) == -1)
977 
978 		    errx(1, "sysctlbyname(\"%s\")",
979 			"net.inet.ip.dummynet.red_avg_pkt_size");
980 		if (avg_pkt_size == 0)
981 			errx(EX_DATAERR,
982 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
983 			    " be greater than zero");
984 
985 		len = sizeof(struct clockinfo);
986 		if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
987 			errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
988 
989 		/*
990 		 * Ticks needed for sending a medium-sized packet.
991 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
992 		 * do not have bandwidth information, because that is stored
993 		 * in the parent pipe, and also we have multiple queues
994 		 * competing for it. So we set s=0, which is not very
995 		 * correct. But on the other hand, why do we want RED with
996 		 * WF2Q+ ?
997 		 */
998 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
999 			s = 0;
1000 		else
1001 			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1002 
1003 		/*
1004 		 * max idle time (in ticks) before avg queue size becomes 0.
1005 		 * NOTA:  (3/w_q) is approx the value x so that
1006 		 * (1-w_q)^x < 10^-3.
1007 		 */
1008 		w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
1009 		idle = s * 3. / w_q;
1010 		p.fs.lookup_step = (int)idle / lookup_depth;
1011 		if (!p.fs.lookup_step)
1012 			p.fs.lookup_step = 1;
1013 		weight = 1 - w_q;
1014 		for (t = p.fs.lookup_step; t > 1; --t)
1015 			weight *= 1 - w_q;
1016 		p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
1017 	}
1018 	if (p.samples_no <= 0) {
1019 		i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
1020 	} else {
1021 		struct dn_pipe_max pm;
1022 		int len = sizeof(pm);
1023 
1024 		memcpy(&pm.pipe, &p, sizeof(pm.pipe));
1025 		memcpy(&pm.samples, samples, sizeof(pm.samples));
1026 
1027 		i = do_cmd(IP_DUMMYNET_CONFIGURE, &pm, len);
1028 	}
1029 
1030 	if (i)
1031 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1032 }
1033