xref: /freebsd/sbin/ipfw/dummynet.c (revision d8b878873e7aa8df1972cc6a642804b17eb61087)
1 /*
2  * Copyright (c) 2002-2003,2010 Luigi Rizzo
3  *
4  * Redistribution and use in source forms, with and without modification,
5  * are permitted provided that this entire comment appears intact.
6  *
7  * Redistribution in binary form may occur without any restrictions.
8  * Obviously, it would be nice if you gave credit where credit is due
9  * but requiring it would be too onerous.
10  *
11  * This software is provided ``AS IS'' without any warranties of any kind.
12  *
13  * $FreeBSD$
14  *
15  * dummynet support
16  */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 /* XXX there are several sysctl leftover here */
21 #include <sys/sysctl.h>
22 
23 #include "ipfw2.h"
24 
25 #include <ctype.h>
26 #include <err.h>
27 #include <errno.h>
28 #include <libutil.h>
29 #include <netdb.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sysexits.h>
34 
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <netinet/ip_fw.h>
38 #include <netinet/ip_dummynet.h>
39 #include <arpa/inet.h>	/* inet_ntoa */
40 
41 
42 static struct _s_x dummynet_params[] = {
43 	{ "plr",		TOK_PLR },
44 	{ "noerror",		TOK_NOERROR },
45 	{ "buckets",		TOK_BUCKETS },
46 	{ "dst-ip",		TOK_DSTIP },
47 	{ "src-ip",		TOK_SRCIP },
48 	{ "dst-port",		TOK_DSTPORT },
49 	{ "src-port",		TOK_SRCPORT },
50 	{ "proto",		TOK_PROTO },
51 	{ "weight",		TOK_WEIGHT },
52 	{ "lmax",		TOK_LMAX },
53 	{ "maxlen",		TOK_LMAX },
54 	{ "all",		TOK_ALL },
55 	{ "mask",		TOK_MASK }, /* alias for both */
56 	{ "sched_mask",		TOK_SCHED_MASK },
57 	{ "flow_mask",		TOK_FLOW_MASK },
58 	{ "droptail",		TOK_DROPTAIL },
59 	{ "red",		TOK_RED },
60 	{ "gred",		TOK_GRED },
61 	{ "bw",			TOK_BW },
62 	{ "bandwidth",		TOK_BW },
63 	{ "delay",		TOK_DELAY },
64 	{ "link",		TOK_LINK },
65 	{ "pipe",		TOK_PIPE },
66 	{ "queue",		TOK_QUEUE },
67 	{ "flowset",		TOK_FLOWSET },
68 	{ "sched",		TOK_SCHED },
69 	{ "pri",		TOK_PRI },
70 	{ "priority",		TOK_PRI },
71 	{ "type",		TOK_TYPE },
72 	{ "flow-id",		TOK_FLOWID},
73 	{ "dst-ipv6",		TOK_DSTIP6},
74 	{ "dst-ip6",		TOK_DSTIP6},
75 	{ "src-ipv6",		TOK_SRCIP6},
76 	{ "src-ip6",		TOK_SRCIP6},
77 	{ "profile",		TOK_PROFILE},
78 	{ "burst",		TOK_BURST},
79 	{ "dummynet-params",	TOK_NULL },
80 	{ NULL, 0 }	/* terminator */
81 };
82 
83 #define O_NEXT(p, len) ((void *)((char *)p + len))
84 
85 static void
86 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
87 {
88 	oid->len = len;
89 	oid->type = type;
90 	oid->subtype = 0;
91 	oid->id = id;
92 }
93 
94 /* make room in the buffer and move the pointer forward */
95 static void *
96 o_next(struct dn_id **o, int len, int type)
97 {
98 	struct dn_id *ret = *o;
99 	oid_fill(ret, len, type, 0);
100 	*o = O_NEXT(*o, len);
101 	return ret;
102 }
103 
104 #if 0
105 static int
106 sort_q(void *arg, const void *pa, const void *pb)
107 {
108 	int rev = (co.do_sort < 0);
109 	int field = rev ? -co.do_sort : co.do_sort;
110 	long long res = 0;
111 	const struct dn_flow_queue *a = pa;
112 	const struct dn_flow_queue *b = pb;
113 
114 	switch (field) {
115 	case 1: /* pkts */
116 		res = a->len - b->len;
117 		break;
118 	case 2: /* bytes */
119 		res = a->len_bytes - b->len_bytes;
120 		break;
121 
122 	case 3: /* tot pkts */
123 		res = a->tot_pkts - b->tot_pkts;
124 		break;
125 
126 	case 4: /* tot bytes */
127 		res = a->tot_bytes - b->tot_bytes;
128 		break;
129 	}
130 	if (res < 0)
131 		res = -1;
132 	if (res > 0)
133 		res = 1;
134 	return (int)(rev ? res : -res);
135 }
136 #endif
137 
138 /* print a mask and header for the subsequent list of flows */
139 static void
140 print_mask(struct ipfw_flow_id *id)
141 {
142 	if (!IS_IP6_FLOW_ID(id)) {
143 		printf("    "
144 		    "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
145 		    id->extra ? "queue," : "",
146 		    id->proto,
147 		    id->src_ip, id->src_port,
148 		    id->dst_ip, id->dst_port);
149 
150 		printf("BKT Prot ___Source IP/port____ "
151 		    "____Dest. IP/port____ "
152 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
153 	} else {
154 		char buf[255];
155 		printf("\n        mask: %sproto: 0x%02x, flow_id: 0x%08x,  ",
156 		    id->extra ? "queue," : "",
157 		    id->proto, id->flow_id6);
158 		inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
159 		printf("%s/0x%04x -> ", buf, id->src_port);
160 		inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
161 		printf("%s/0x%04x\n", buf, id->dst_port);
162 
163 		printf("BKT ___Prot___ _flow-id_ "
164 		    "______________Source IPv6/port_______________ "
165 		    "_______________Dest. IPv6/port_______________ "
166 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
167 	}
168 }
169 
170 static void
171 list_flow(struct dn_flow *ni)
172 {
173 	char buff[255];
174 	struct protoent *pe;
175 	struct in_addr ina;
176 	struct ipfw_flow_id *id = &ni->fid;
177 
178 	pe = getprotobynumber(id->proto);
179 		/* XXX: Should check for IPv4 flows */
180 	printf("%3u%c", (ni->oid.id) & 0xff,
181 		id->extra ? '*' : ' ');
182 	if (!IS_IP6_FLOW_ID(id)) {
183 		if (pe)
184 			printf("%-4s ", pe->p_name);
185 		else
186 			printf("%4u ", id->proto);
187 		ina.s_addr = htonl(id->src_ip);
188 		printf("%15s/%-5d ",
189 		    inet_ntoa(ina), id->src_port);
190 		ina.s_addr = htonl(id->dst_ip);
191 		printf("%15s/%-5d ",
192 		    inet_ntoa(ina), id->dst_port);
193 	} else {
194 		/* Print IPv6 flows */
195 		if (pe != NULL)
196 			printf("%9s ", pe->p_name);
197 		else
198 			printf("%9u ", id->proto);
199 		printf("%7d  %39s/%-5d ", id->flow_id6,
200 		    inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
201 		    id->src_port);
202 		printf(" %39s/%-5d ",
203 		    inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
204 		    id->dst_port);
205 	}
206 	pr_u64(&ni->tot_pkts, 4);
207 	pr_u64(&ni->tot_bytes, 8);
208 	printf("%2u %4u %3u\n",
209 	    ni->length, ni->len_bytes, ni->drops);
210 }
211 
212 static void
213 print_flowset_parms(struct dn_fs *fs, char *prefix)
214 {
215 	int l;
216 	char qs[30];
217 	char plr[30];
218 	char red[90];	/* Display RED parameters */
219 
220 	l = fs->qsize;
221 	if (fs->flags & DN_QSIZE_BYTES) {
222 		if (l >= 8192)
223 			sprintf(qs, "%d KB", l / 1024);
224 		else
225 			sprintf(qs, "%d B", l);
226 	} else
227 		sprintf(qs, "%3d sl.", l);
228 	if (fs->plr)
229 		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
230 	else
231 		plr[0] = '\0';
232 
233 	if (fs->flags & DN_IS_RED)	/* RED parameters */
234 		sprintf(red,
235 		    "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
236 		    (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
237 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
238 		    fs->min_th,
239 		    fs->max_th,
240 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
241 	else
242 		sprintf(red, "droptail");
243 
244 	if (prefix[0]) {
245 	    printf("%s %s%s %d queues (%d buckets) %s\n",
246 		prefix, qs, plr, fs->oid.id, fs->buckets, red);
247 	    prefix[0] = '\0';
248 	} else {
249 	    printf("q%05d %s%s %d flows (%d buckets) sched %d "
250 			"weight %d lmax %d pri %d %s\n",
251 		fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
252 		fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
253 	    if (fs->flags & DN_HAVE_MASK)
254 		print_mask(&fs->flow_mask);
255 	}
256 }
257 
258 static void
259 print_extra_delay_parms(struct dn_profile *p)
260 {
261 	double loss;
262 	if (p->samples_no <= 0)
263 		return;
264 
265 	loss = p->loss_level;
266 	loss /= p->samples_no;
267 	printf("\t profile: name \"%s\" loss %f samples %d\n",
268 		p->name, loss, p->samples_no);
269 }
270 
271 static void
272 flush_buf(char *buf)
273 {
274 	if (buf[0])
275 		printf("%s\n", buf);
276 	buf[0] = '\0';
277 }
278 
279 /*
280  * generic list routine. We expect objects in a specific order, i.e.
281  * PIPES AND SCHEDULERS:
282  *	link; scheduler; internal flowset if any; instances
283  * we can tell a pipe from the number.
284  *
285  * FLOWSETS:
286  *	flowset; queues;
287  * link i (int queue); scheduler i; si(i) { flowsets() : queues }
288  */
289 static void
290 list_pipes(struct dn_id *oid, struct dn_id *end)
291 {
292     char buf[160];	/* pending buffer */
293 
294     buf[0] = '\0';
295     for (; oid != end; oid = O_NEXT(oid, oid->len)) {
296 	if (oid->len < sizeof(*oid))
297 		errx(1, "invalid oid len %d\n", oid->len);
298 
299 	switch (oid->type) {
300 	default:
301 	    flush_buf(buf);
302 	    printf("unrecognized object %d size %d\n", oid->type, oid->len);
303 	    break;
304 	case DN_TEXT: /* list of attached flowsets */
305 	    {
306 		int i, l;
307 		struct {
308 			struct dn_id id;
309 			uint32_t p[0];
310 		} *d = (void *)oid;
311 		l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
312 		if (l == 0)
313 		    break;
314 		printf("   Children flowsets: ");
315 		for (i = 0; i < l; i++)
316 			printf("%u ", d->p[i]);
317 		printf("\n");
318 		break;
319 	    }
320 	case DN_CMD_GET:
321 	    if (co.verbose)
322 		printf("answer for cmd %d, len %d\n", oid->type, oid->id);
323 	    break;
324 	case DN_SCH: {
325 	    struct dn_sch *s = (struct dn_sch *)oid;
326 	    flush_buf(buf);
327 	    printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
328 			s->sched_nr,
329 			s->name, s->flags, s->buckets, s->oid.id);
330 	    if (s->flags & DN_HAVE_MASK)
331 		print_mask(&s->sched_mask);
332 	    }
333 	    break;
334 
335 	case DN_FLOW:
336 	    list_flow((struct dn_flow *)oid);
337 	    break;
338 
339 	case DN_LINK: {
340 	    struct dn_link *p = (struct dn_link *)oid;
341 	    double b = p->bandwidth;
342 	    char bwbuf[30];
343 	    char burst[5 + 7];
344 
345 	    /* This starts a new object so flush buffer */
346 	    flush_buf(buf);
347 	    /* data rate */
348 	    if (b == 0)
349 		sprintf(bwbuf, "unlimited     ");
350 	    else if (b >= 1000000)
351 		sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
352 	    else if (b >= 1000)
353 		sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
354 	    else
355 		sprintf(bwbuf, "%7.3f bit/s ", b);
356 
357 	    if (humanize_number(burst, sizeof(burst), p->burst,
358 		    "", HN_AUTOSCALE, 0) < 0 || co.verbose)
359 		sprintf(burst, "%d", (int)p->burst);
360 	    sprintf(buf, "%05d: %s %4d ms burst %s",
361 		p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
362 	    }
363 	    break;
364 
365 	case DN_FS:
366 	    print_flowset_parms((struct dn_fs *)oid, buf);
367 	    break;
368 	case DN_PROFILE:
369 	    flush_buf(buf);
370 	    print_extra_delay_parms((struct dn_profile *)oid);
371 	}
372 	flush_buf(buf); // XXX does it really go here ?
373     }
374 }
375 
376 /*
377  * Delete pipe, queue or scheduler i
378  */
379 int
380 ipfw_delete_pipe(int do_pipe, int i)
381 {
382 	struct {
383 		struct dn_id oid;
384 		uintptr_t a[1];	/* add more if we want a list */
385 	} cmd;
386 	oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
387 	cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
388 		( (do_pipe == 2) ? DN_FS : DN_SCH);
389 	cmd.a[0] = i;
390 	i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
391 	if (i) {
392 		i = 1;
393 		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
394 	}
395 	return i;
396 }
397 
398 /*
399  * Code to parse delay profiles.
400  *
401  * Some link types introduce extra delays in the transmission
402  * of a packet, e.g. because of MAC level framing, contention on
403  * the use of the channel, MAC level retransmissions and so on.
404  * From our point of view, the channel is effectively unavailable
405  * for this extra time, which is constant or variable depending
406  * on the link type. Additionally, packets may be dropped after this
407  * time (e.g. on a wireless link after too many retransmissions).
408  * We can model the additional delay with an empirical curve
409  * that represents its distribution.
410  *
411  *	cumulative probability
412  *	1.0 ^
413  *	    |
414  *	L   +-- loss-level          x
415  *	    |                 ******
416  *	    |                *
417  *	    |           *****
418  *	    |          *
419  *	    |        **
420  *	    |       *
421  *	    +-------*------------------->
422  *			delay
423  *
424  * The empirical curve may have both vertical and horizontal lines.
425  * Vertical lines represent constant delay for a range of
426  * probabilities; horizontal lines correspond to a discontinuty
427  * in the delay distribution: the link will use the largest delay
428  * for a given probability.
429  *
430  * To pass the curve to dummynet, we must store the parameters
431  * in a file as described below, and issue the command
432  *
433  *      ipfw pipe <n> config ... bw XXX profile <filename> ...
434  *
435  * The file format is the following, with whitespace acting as
436  * a separator and '#' indicating the beginning a comment:
437  *
438  *	samples N
439  *		the number of samples used in the internal
440  *		representation (2..1024; default 100);
441  *
442  *	loss-level L
443  *		The probability above which packets are lost.
444  *               (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
445  *
446  *	name identifier
447  *		Optional a name (listed by "ipfw pipe show")
448  *		to identify the distribution;
449  *
450  *	"delay prob" | "prob delay"
451  *		One of these two lines is mandatory and defines
452  *		the format of the following lines with data points.
453  *
454  *	XXX YYY
455  *		2 or more lines representing points in the curve,
456  *		with either delay or probability first, according
457  *		to the chosen format.
458  *		The unit for delay is milliseconds.
459  *
460  * Data points does not need to be ordered or equal to the number
461  * specified in the "samples" line. ipfw will sort and interpolate
462  * the curve as needed.
463  *
464  * Example of a profile file:
465 
466         name    bla_bla_bla
467         samples 100
468         loss-level    0.86
469         prob    delay
470         0       200	# minimum overhead is 200ms
471         0.5     200
472         0.5     300
473         0.8     1000
474         0.9     1300
475         1       1300
476 
477  * Internally, we will convert the curve to a fixed number of
478  * samples, and when it is time to transmit a packet we will
479  * model the extra delay as extra bits in the packet.
480  *
481  */
482 
483 #define ED_MAX_LINE_LEN	256+ED_MAX_NAME_LEN
484 #define ED_TOK_SAMPLES	"samples"
485 #define ED_TOK_LOSS	"loss-level"
486 #define ED_TOK_NAME	"name"
487 #define ED_TOK_DELAY	"delay"
488 #define ED_TOK_PROB	"prob"
489 #define ED_TOK_BW	"bw"
490 #define ED_SEPARATORS	" \t\n"
491 #define ED_MIN_SAMPLES_NO	2
492 
493 /*
494  * returns 1 if s is a non-negative number, with at least one '.'
495  */
496 static int
497 is_valid_number(const char *s)
498 {
499 	int i, dots_found = 0;
500 	int len = strlen(s);
501 
502 	for (i = 0; i<len; ++i)
503 		if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
504 			return 0;
505 	return 1;
506 }
507 
508 /*
509  * Take as input a string describing a bandwidth value
510  * and return the numeric bandwidth value.
511  * set clocking interface or bandwidth value
512  */
513 static void
514 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
515 {
516 	if (*bandwidth != -1)
517 		warnx("duplicate token, override bandwidth value!");
518 
519 	if (arg[0] >= 'a' && arg[0] <= 'z') {
520 		if (!if_name) {
521 			errx(1, "no if support");
522 		}
523 		if (namelen >= IFNAMSIZ)
524 			warn("interface name truncated");
525 		namelen--;
526 		/* interface name */
527 		strncpy(if_name, arg, namelen);
528 		if_name[namelen] = '\0';
529 		*bandwidth = 0;
530 	} else {	/* read bandwidth value */
531 		int bw;
532 		char *end = NULL;
533 
534 		bw = strtoul(arg, &end, 0);
535 		if (*end == 'K' || *end == 'k') {
536 			end++;
537 			bw *= 1000;
538 		} else if (*end == 'M' || *end == 'm') {
539 			end++;
540 			bw *= 1000000;
541 		}
542 		if ((*end == 'B' &&
543 			_substrcmp2(end, "Bi", "Bit/s") != 0) ||
544 		    _substrcmp2(end, "by", "bytes") == 0)
545 			bw *= 8;
546 
547 		if (bw < 0)
548 			errx(EX_DATAERR, "bandwidth too large");
549 
550 		*bandwidth = bw;
551 		if (if_name)
552 			if_name[0] = '\0';
553 	}
554 }
555 
556 struct point {
557 	double prob;
558 	double delay;
559 };
560 
561 static int
562 compare_points(const void *vp1, const void *vp2)
563 {
564 	const struct point *p1 = vp1;
565 	const struct point *p2 = vp2;
566 	double res = 0;
567 
568 	res = p1->prob - p2->prob;
569 	if (res == 0)
570 		res = p1->delay - p2->delay;
571 	if (res < 0)
572 		return -1;
573 	else if (res > 0)
574 		return 1;
575 	else
576 		return 0;
577 }
578 
579 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
580 
581 static void
582 load_extra_delays(const char *filename, struct dn_profile *p,
583 	struct dn_link *link)
584 {
585 	char    line[ED_MAX_LINE_LEN];
586 	FILE    *f;
587 	int     lineno = 0;
588 	int     i;
589 
590 	int     samples = -1;
591 	double  loss = -1.0;
592 	char    profile_name[ED_MAX_NAME_LEN];
593 	int     delay_first = -1;
594 	int     do_points = 0;
595 	struct point    points[ED_MAX_SAMPLES_NO];
596 	int     points_no = 0;
597 
598 	/* XXX link never NULL? */
599 	p->link_nr = link->link_nr;
600 
601 	profile_name[0] = '\0';
602 	f = fopen(filename, "r");
603 	if (f == NULL)
604 		err(EX_UNAVAILABLE, "fopen: %s", filename);
605 
606 	while (fgets(line, ED_MAX_LINE_LEN, f)) {         /* read commands */
607 		char *s, *cur = line, *name = NULL, *arg = NULL;
608 
609 		++lineno;
610 
611 		/* parse the line */
612 		while (cur) {
613 			s = strsep(&cur, ED_SEPARATORS);
614 			if (s == NULL || *s == '#')
615 				break;
616 			if (*s == '\0')
617 				continue;
618 			if (arg)
619 				errx(ED_EFMT("too many arguments"));
620 			if (name == NULL)
621 				name = s;
622 			else
623 				arg = s;
624 		}
625 		if (name == NULL)	/* empty line */
626 			continue;
627 		if (arg == NULL)
628 			errx(ED_EFMT("missing arg for %s"), name);
629 
630 		if (!strcasecmp(name, ED_TOK_SAMPLES)) {
631 		    if (samples > 0)
632 			errx(ED_EFMT("duplicate ``samples'' line"));
633 		    if (atoi(arg) <=0)
634 			errx(ED_EFMT("invalid number of samples"));
635 		    samples = atoi(arg);
636 		    if (samples>ED_MAX_SAMPLES_NO)
637 			    errx(ED_EFMT("too many samples, maximum is %d"),
638 				ED_MAX_SAMPLES_NO);
639 		    do_points = 0;
640 		} else if (!strcasecmp(name, ED_TOK_BW)) {
641 		    char buf[IFNAMSIZ];
642 		    read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
643 		} else if (!strcasecmp(name, ED_TOK_LOSS)) {
644 		    if (loss != -1.0)
645 			errx(ED_EFMT("duplicated token: %s"), name);
646 		    if (!is_valid_number(arg))
647 			errx(ED_EFMT("invalid %s"), arg);
648 		    loss = atof(arg);
649 		    if (loss > 1)
650 			errx(ED_EFMT("%s greater than 1.0"), name);
651 		    do_points = 0;
652 		} else if (!strcasecmp(name, ED_TOK_NAME)) {
653 		    if (profile_name[0] != '\0')
654 			errx(ED_EFMT("duplicated token: %s"), name);
655 		    strncpy(profile_name, arg, sizeof(profile_name) - 1);
656 		    profile_name[sizeof(profile_name)-1] = '\0';
657 		    do_points = 0;
658 		} else if (!strcasecmp(name, ED_TOK_DELAY)) {
659 		    if (do_points)
660 			errx(ED_EFMT("duplicated token: %s"), name);
661 		    delay_first = 1;
662 		    do_points = 1;
663 		} else if (!strcasecmp(name, ED_TOK_PROB)) {
664 		    if (do_points)
665 			errx(ED_EFMT("duplicated token: %s"), name);
666 		    delay_first = 0;
667 		    do_points = 1;
668 		} else if (do_points) {
669 		    if (!is_valid_number(name) || !is_valid_number(arg))
670 			errx(ED_EFMT("invalid point found"));
671 		    if (delay_first) {
672 			points[points_no].delay = atof(name);
673 			points[points_no].prob = atof(arg);
674 		    } else {
675 			points[points_no].delay = atof(arg);
676 			points[points_no].prob = atof(name);
677 		    }
678 		    if (points[points_no].prob > 1.0)
679 			errx(ED_EFMT("probability greater than 1.0"));
680 		    ++points_no;
681 		} else {
682 		    errx(ED_EFMT("unrecognised command '%s'"), name);
683 		}
684 	}
685 
686 	fclose (f);
687 
688 	if (samples == -1) {
689 	    warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
690 	    samples = 100;
691 	}
692 
693 	if (loss == -1.0) {
694 	    warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
695 	    loss = 1;
696 	}
697 
698 	/* make sure that there are enough points. */
699 	if (points_no < ED_MIN_SAMPLES_NO)
700 	    errx(ED_EFMT("too few samples, need at least %d"),
701 		ED_MIN_SAMPLES_NO);
702 
703 	qsort(points, points_no, sizeof(struct point), compare_points);
704 
705 	/* interpolation */
706 	for (i = 0; i<points_no-1; ++i) {
707 	    double y1 = points[i].prob * samples;
708 	    double x1 = points[i].delay;
709 	    double y2 = points[i+1].prob * samples;
710 	    double x2 = points[i+1].delay;
711 
712 	    int ix = y1;
713 	    int stop = y2;
714 
715 	    if (x1 == x2) {
716 		for (; ix<stop; ++ix)
717 		    p->samples[ix] = x1;
718 	    } else {
719 		double m = (y2-y1)/(x2-x1);
720 		double c = y1 - m*x1;
721 		for (; ix<stop ; ++ix)
722 		    p->samples[ix] = (ix - c)/m;
723 	    }
724 	}
725 	p->samples_no = samples;
726 	p->loss_level = loss * samples;
727 	strncpy(p->name, profile_name, sizeof(p->name));
728 }
729 
730 /*
731  * configuration of pipes, schedulers, flowsets.
732  * When we configure a new scheduler, an empty pipe is created, so:
733  *
734  * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
735  *	sched N+Delta type fifo sched_mask ...
736  *	pipe N+Delta <parameters>
737  *	flowset N+Delta pipe N+Delta (no parameters)
738  *	sched N type wf2q+ sched_mask ...
739  *	pipe N <parameters>
740  *
741  * do_pipe = 2 -> flowset N config
742  *	flowset N parameters
743  *
744  * do_pipe = 3 -> sched N config
745  *	sched N parameters (default no pipe)
746  *	optional Pipe N config ...
747  * pipe ==>
748  */
749 void
750 ipfw_config_pipe(int ac, char **av)
751 {
752 	int i, j;
753 	char *end;
754 	void *par = NULL;
755 	struct dn_id *buf, *base;
756 	struct dn_sch *sch = NULL;
757 	struct dn_link *p = NULL;
758 	struct dn_fs *fs = NULL;
759 	struct dn_profile *pf = NULL;
760 	struct ipfw_flow_id *mask = NULL;
761 	int lmax;
762 	uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
763 
764 	/*
765 	 * allocate space for 1 header,
766 	 * 1 scheduler, 1 link, 1 flowset, 1 profile
767 	 */
768 	lmax = sizeof(struct dn_id);	/* command header */
769 	lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
770 		sizeof(struct dn_fs) + sizeof(struct dn_profile);
771 
772 	av++; ac--;
773 	/* Pipe number */
774 	if (ac && isdigit(**av)) {
775 		i = atoi(*av); av++; ac--;
776 	} else
777 		i = -1;
778 	if (i <= 0)
779 		errx(EX_USAGE, "need a pipe/flowset/sched number");
780 	base = buf = safe_calloc(1, lmax);
781 	/* all commands start with a 'CONFIGURE' and a version */
782 	o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
783 	base->id = DN_API_VERSION;
784 
785 	switch (co.do_pipe) {
786 	case 1: /* "pipe N config ..." */
787 		/* Allocate space for the WF2Q+ scheduler, its link
788 		 * and the FIFO flowset. Set the number, but leave
789 		 * the scheduler subtype and other parameters to 0
790 		 * so the kernel will use appropriate defaults.
791 		 * XXX todo: add a flag to record if a parameter
792 		 * is actually configured.
793 		 * If we do a 'pipe config' mask -> sched_mask.
794 		 * The FIFO scheduler and link are derived from the
795 		 * WF2Q+ one in the kernel.
796 		 */
797 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
798 		p = o_next(&buf, sizeof(*p), DN_LINK);
799 		fs = o_next(&buf, sizeof(*fs), DN_FS);
800 
801 		sch->sched_nr = i;
802 		sch->oid.subtype = 0;	/* defaults to WF2Q+ */
803 		mask = &sch->sched_mask;
804 		flags = &sch->flags;
805 		buckets = &sch->buckets;
806 		*flags |= DN_PIPE_CMD;
807 
808 		p->link_nr = i;
809 
810 		/* This flowset is only for the FIFO scheduler */
811 		fs->fs_nr = i + 2*DN_MAX_ID;
812 		fs->sched_nr = i + DN_MAX_ID;
813 		break;
814 
815 	case 2: /* "queue N config ... " */
816 		fs = o_next(&buf, sizeof(*fs), DN_FS);
817 		fs->fs_nr = i;
818 		mask = &fs->flow_mask;
819 		flags = &fs->flags;
820 		buckets = &fs->buckets;
821 		break;
822 
823 	case 3: /* "sched N config ..." */
824 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
825 		fs = o_next(&buf, sizeof(*fs), DN_FS);
826 		sch->sched_nr = i;
827 		mask = &sch->sched_mask;
828 		flags = &sch->flags;
829 		buckets = &sch->buckets;
830 		/* fs is used only with !MULTIQUEUE schedulers */
831 		fs->fs_nr = i + DN_MAX_ID;
832 		fs->sched_nr = i;
833 		break;
834 	}
835 	/* set to -1 those fields for which we want to reuse existing
836 	 * values from the kernel.
837 	 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
838 	 * XXX todo: support reuse of the mask.
839 	 */
840 	if (p)
841 		p->bandwidth = -1;
842 	for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
843 		fs->par[j] = -1;
844 	while (ac > 0) {
845 		double d;
846 		int tok = match_token(dummynet_params, *av);
847 		ac--; av++;
848 
849 		switch(tok) {
850 		case TOK_NOERROR:
851 			NEED(fs, "noerror is only for pipes");
852 			fs->flags |= DN_NOERROR;
853 			break;
854 
855 		case TOK_PLR:
856 			NEED(fs, "plr is only for pipes");
857 			NEED1("plr needs argument 0..1\n");
858 			d = strtod(av[0], NULL);
859 			if (d > 1)
860 				d = 1;
861 			else if (d < 0)
862 				d = 0;
863 			fs->plr = (int)(d*0x7fffffff);
864 			ac--; av++;
865 			break;
866 
867 		case TOK_QUEUE:
868 			NEED(fs, "queue is only for pipes or flowsets");
869 			NEED1("queue needs queue size\n");
870 			end = NULL;
871 			fs->qsize = strtoul(av[0], &end, 0);
872 			if (*end == 'K' || *end == 'k') {
873 				fs->flags |= DN_QSIZE_BYTES;
874 				fs->qsize *= 1024;
875 			} else if (*end == 'B' ||
876 			    _substrcmp2(end, "by", "bytes") == 0) {
877 				fs->flags |= DN_QSIZE_BYTES;
878 			}
879 			ac--; av++;
880 			break;
881 
882 		case TOK_BUCKETS:
883 			NEED(fs, "buckets is only for pipes or flowsets");
884 			NEED1("buckets needs argument\n");
885 			*buckets = strtoul(av[0], NULL, 0);
886 			ac--; av++;
887 			break;
888 
889 		case TOK_FLOW_MASK:
890 		case TOK_SCHED_MASK:
891 		case TOK_MASK:
892 			NEED(mask, "tok_mask");
893 			NEED1("mask needs mask specifier\n");
894 			/*
895 			 * per-flow queue, mask is dst_ip, dst_port,
896 			 * src_ip, src_port, proto measured in bits
897 			 */
898 			par = NULL;
899 
900 			bzero(mask, sizeof(*mask));
901 			end = NULL;
902 
903 			while (ac >= 1) {
904 			    uint32_t *p32 = NULL;
905 			    uint16_t *p16 = NULL;
906 			    uint32_t *p20 = NULL;
907 			    struct in6_addr *pa6 = NULL;
908 			    uint32_t a;
909 
910 			    tok = match_token(dummynet_params, *av);
911 			    ac--; av++;
912 			    switch(tok) {
913 			    case TOK_ALL:
914 				    /*
915 				     * special case, all bits significant
916 				     * except 'extra' (the queue number)
917 				     */
918 				    mask->dst_ip = ~0;
919 				    mask->src_ip = ~0;
920 				    mask->dst_port = ~0;
921 				    mask->src_port = ~0;
922 				    mask->proto = ~0;
923 				    n2mask(&mask->dst_ip6, 128);
924 				    n2mask(&mask->src_ip6, 128);
925 				    mask->flow_id6 = ~0;
926 				    *flags |= DN_HAVE_MASK;
927 				    goto end_mask;
928 
929 			    case TOK_QUEUE:
930 				    mask->extra = ~0;
931 				    *flags |= DN_HAVE_MASK;
932 				    goto end_mask;
933 
934 			    case TOK_DSTIP:
935 				    mask->addr_type = 4;
936 				    p32 = &mask->dst_ip;
937 				    break;
938 
939 			    case TOK_SRCIP:
940 				    mask->addr_type = 4;
941 				    p32 = &mask->src_ip;
942 				    break;
943 
944 			    case TOK_DSTIP6:
945 				    mask->addr_type = 6;
946 				    pa6 = &mask->dst_ip6;
947 				    break;
948 
949 			    case TOK_SRCIP6:
950 				    mask->addr_type = 6;
951 				    pa6 = &mask->src_ip6;
952 				    break;
953 
954 			    case TOK_FLOWID:
955 				    mask->addr_type = 6;
956 				    p20 = &mask->flow_id6;
957 				    break;
958 
959 			    case TOK_DSTPORT:
960 				    p16 = &mask->dst_port;
961 				    break;
962 
963 			    case TOK_SRCPORT:
964 				    p16 = &mask->src_port;
965 				    break;
966 
967 			    case TOK_PROTO:
968 				    break;
969 
970 			    default:
971 				    ac++; av--; /* backtrack */
972 				    goto end_mask;
973 			    }
974 			    if (ac < 1)
975 				    errx(EX_USAGE, "mask: value missing");
976 			    if (*av[0] == '/') {
977 				    a = strtoul(av[0]+1, &end, 0);
978 				    if (pa6 == NULL)
979 					    a = (a == 32) ? ~0 : (1 << a) - 1;
980 			    } else
981 				    a = strtoul(av[0], &end, 0);
982 			    if (p32 != NULL)
983 				    *p32 = a;
984 			    else if (p16 != NULL) {
985 				    if (a > 0xFFFF)
986 					    errx(EX_DATAERR,
987 						"port mask must be 16 bit");
988 				    *p16 = (uint16_t)a;
989 			    } else if (p20 != NULL) {
990 				    if (a > 0xfffff)
991 					errx(EX_DATAERR,
992 					    "flow_id mask must be 20 bit");
993 				    *p20 = (uint32_t)a;
994 			    } else if (pa6 != NULL) {
995 				    if (a > 128)
996 					errx(EX_DATAERR,
997 					    "in6addr invalid mask len");
998 				    else
999 					n2mask(pa6, a);
1000 			    } else {
1001 				    if (a > 0xFF)
1002 					    errx(EX_DATAERR,
1003 						"proto mask must be 8 bit");
1004 				    mask->proto = (uint8_t)a;
1005 			    }
1006 			    if (a != 0)
1007 				    *flags |= DN_HAVE_MASK;
1008 			    ac--; av++;
1009 			} /* end while, config masks */
1010 end_mask:
1011 			break;
1012 
1013 		case TOK_RED:
1014 		case TOK_GRED:
1015 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1016 			fs->flags |= DN_IS_RED;
1017 			if (tok == TOK_GRED)
1018 				fs->flags |= DN_IS_GENTLE_RED;
1019 			/*
1020 			 * the format for parameters is w_q/min_th/max_th/max_p
1021 			 */
1022 			if ((end = strsep(&av[0], "/"))) {
1023 			    double w_q = strtod(end, NULL);
1024 			    if (w_q > 1 || w_q <= 0)
1025 				errx(EX_DATAERR, "0 < w_q <= 1");
1026 			    fs->w_q = (int) (w_q * (1 << SCALE_RED));
1027 			}
1028 			if ((end = strsep(&av[0], "/"))) {
1029 			    fs->min_th = strtoul(end, &end, 0);
1030 			    if (*end == 'K' || *end == 'k')
1031 				fs->min_th *= 1024;
1032 			}
1033 			if ((end = strsep(&av[0], "/"))) {
1034 			    fs->max_th = strtoul(end, &end, 0);
1035 			    if (*end == 'K' || *end == 'k')
1036 				fs->max_th *= 1024;
1037 			}
1038 			if ((end = strsep(&av[0], "/"))) {
1039 			    double max_p = strtod(end, NULL);
1040 			    if (max_p > 1 || max_p <= 0)
1041 				errx(EX_DATAERR, "0 < max_p <= 1");
1042 			    fs->max_p = (int)(max_p * (1 << SCALE_RED));
1043 			}
1044 			ac--; av++;
1045 			break;
1046 
1047 		case TOK_DROPTAIL:
1048 			NEED(fs, "droptail is only for flowsets");
1049 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1050 			break;
1051 
1052 		case TOK_BW:
1053 			NEED(p, "bw is only for links");
1054 			NEED1("bw needs bandwidth or interface\n");
1055 			read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1056 			ac--; av++;
1057 			break;
1058 
1059 		case TOK_DELAY:
1060 			NEED(p, "delay is only for links");
1061 			NEED1("delay needs argument 0..10000ms\n");
1062 			p->delay = strtoul(av[0], NULL, 0);
1063 			ac--; av++;
1064 			break;
1065 
1066 		case TOK_TYPE: {
1067 			int l;
1068 			NEED(sch, "type is only for schedulers");
1069 			NEED1("type needs a string");
1070 			l = strlen(av[0]);
1071 			if (l == 0 || l > 15)
1072 				errx(1, "type %s too long\n", av[0]);
1073 			strcpy(sch->name, av[0]);
1074 			sch->oid.subtype = 0; /* use string */
1075 			ac--; av++;
1076 			break;
1077 		    }
1078 
1079 		case TOK_WEIGHT:
1080 			NEED(fs, "weight is only for flowsets");
1081 			NEED1("weight needs argument\n");
1082 			fs->par[0] = strtol(av[0], &end, 0);
1083 			ac--; av++;
1084 			break;
1085 
1086 		case TOK_LMAX:
1087 			NEED(fs, "lmax is only for flowsets");
1088 			NEED1("lmax needs argument\n");
1089 			fs->par[1] = strtol(av[0], &end, 0);
1090 			ac--; av++;
1091 			break;
1092 
1093 		case TOK_PRI:
1094 			NEED(fs, "priority is only for flowsets");
1095 			NEED1("priority needs argument\n");
1096 			fs->par[2] = strtol(av[0], &end, 0);
1097 			ac--; av++;
1098 			break;
1099 
1100 		case TOK_SCHED:
1101 		case TOK_PIPE:
1102 			NEED(fs, "pipe/sched");
1103 			NEED1("pipe/link/sched needs number\n");
1104 			fs->sched_nr = strtoul(av[0], &end, 0);
1105 			ac--; av++;
1106 			break;
1107 
1108 		case TOK_PROFILE:
1109 			NEED((!pf), "profile already set");
1110 			NEED(p, "profile");
1111 		    {
1112 			NEED1("extra delay needs the file name\n");
1113 			pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1114 			load_extra_delays(av[0], pf, p); //XXX can't fail?
1115 			--ac; ++av;
1116 		    }
1117 			break;
1118 
1119 		case TOK_BURST:
1120 			NEED(p, "burst");
1121 			NEED1("burst needs argument\n");
1122 			errno = 0;
1123 			if (expand_number(av[0], (int64_t *)&p->burst) < 0)
1124 				if (errno != ERANGE)
1125 					errx(EX_DATAERR,
1126 					    "burst: invalid argument");
1127 			if (errno || p->burst > (1ULL << 48) - 1)
1128 				errx(EX_DATAERR,
1129 				    "burst: out of range (0..2^48-1)");
1130 			ac--; av++;
1131 			break;
1132 
1133 		default:
1134 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1135 		}
1136 	}
1137 
1138 	/* check validity of parameters */
1139 	if (p) {
1140 		if (p->delay > 10000)
1141 			errx(EX_DATAERR, "delay must be < 10000");
1142 		if (p->bandwidth == -1)
1143 			p->bandwidth = 0;
1144 	}
1145 	if (fs) {
1146 		/* XXX accept a 0 scheduler to keep the default */
1147 	    if (fs->flags & DN_QSIZE_BYTES) {
1148 		size_t len;
1149 		long limit;
1150 
1151 		len = sizeof(limit);
1152 		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1153 			&limit, &len, NULL, 0) == -1)
1154 			limit = 1024*1024;
1155 		if (fs->qsize > limit)
1156 			errx(EX_DATAERR, "queue size must be < %ldB", limit);
1157 	    } else {
1158 		size_t len;
1159 		long limit;
1160 
1161 		len = sizeof(limit);
1162 		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1163 			&limit, &len, NULL, 0) == -1)
1164 			limit = 100;
1165 		if (fs->qsize > limit)
1166 			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1167 	    }
1168 
1169 	    if (fs->flags & DN_IS_RED) {
1170 		size_t len;
1171 		int lookup_depth, avg_pkt_size;
1172 		double w_q;
1173 
1174 		if (fs->min_th >= fs->max_th)
1175 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1176 			fs->min_th, fs->max_th);
1177 		if (fs->max_th == 0)
1178 		    errx(EX_DATAERR, "max_th must be > 0");
1179 
1180 		len = sizeof(int);
1181 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1182 			&lookup_depth, &len, NULL, 0) == -1)
1183 			lookup_depth = 256;
1184 		if (lookup_depth == 0)
1185 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1186 			" must be greater than zero");
1187 
1188 		len = sizeof(int);
1189 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1190 			&avg_pkt_size, &len, NULL, 0) == -1)
1191 			avg_pkt_size = 512;
1192 
1193 		if (avg_pkt_size == 0)
1194 			errx(EX_DATAERR,
1195 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
1196 			    " be greater than zero");
1197 
1198 		/*
1199 		 * Ticks needed for sending a medium-sized packet.
1200 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
1201 		 * do not have bandwidth information, because that is stored
1202 		 * in the parent pipe, and also we have multiple queues
1203 		 * competing for it. So we set s=0, which is not very
1204 		 * correct. But on the other hand, why do we want RED with
1205 		 * WF2Q+ ?
1206 		 */
1207 #if 0
1208 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
1209 			s = 0;
1210 		else
1211 			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1212 #endif
1213 		/*
1214 		 * max idle time (in ticks) before avg queue size becomes 0.
1215 		 * NOTA:  (3/w_q) is approx the value x so that
1216 		 * (1-w_q)^x < 10^-3.
1217 		 */
1218 		w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1219 #if 0 // go in kernel
1220 		idle = s * 3. / w_q;
1221 		fs->lookup_step = (int)idle / lookup_depth;
1222 		if (!fs->lookup_step)
1223 			fs->lookup_step = 1;
1224 		weight = 1 - w_q;
1225 		for (t = fs->lookup_step; t > 1; --t)
1226 			weight *= 1 - w_q;
1227 		fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1228 #endif
1229 	    }
1230 	}
1231 
1232 	i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1233 
1234 	if (i)
1235 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1236 }
1237 
1238 void
1239 dummynet_flush(void)
1240 {
1241 	struct dn_id oid;
1242 	oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1243 	do_cmd(IP_DUMMYNET3, &oid, oid.len);
1244 }
1245 
1246 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1247  * Returns the number of ranges, and possibly stores them
1248  * in the array v of size len.
1249  */
1250 static int
1251 parse_range(int ac, char *av[], uint32_t *v, int len)
1252 {
1253 	int n = 0;
1254 	char *endptr, *s;
1255 	uint32_t base[2];
1256 
1257 	if (v == NULL || len < 2) {
1258 		v = base;
1259 		len = 2;
1260 	}
1261 
1262 	for (s = *av; s != NULL; av++, ac--) {
1263 		v[0] = strtoul(s, &endptr, 10);
1264 		v[1] = (*endptr != '-') ? v[0] :
1265 			 strtoul(endptr+1, &endptr, 10);
1266 		if (*endptr == '\0') { /* prepare for next round */
1267 			s = (ac > 0) ? *(av+1) : NULL;
1268 		} else {
1269 			if (*endptr != ',') {
1270 				warn("invalid number: %s", s);
1271 				s = ++endptr;
1272 				continue;
1273 			}
1274 			/* continue processing from here */
1275 			s = ++endptr;
1276 			ac++;
1277 			av--;
1278 		}
1279 		if (v[1] < v[0] ||
1280 			v[1] < 0 || v[1] >= DN_MAX_ID-1 ||
1281 			v[0] < 0 || v[1] >= DN_MAX_ID-1) {
1282 			continue; /* invalid entry */
1283 		}
1284 		n++;
1285 		/* translate if 'pipe list' */
1286 		if (co.do_pipe == 1) {
1287 			v[0] += DN_MAX_ID;
1288 			v[1] += DN_MAX_ID;
1289 		}
1290 		v = (n*2 < len) ? v + 2 : base;
1291 	}
1292 	return n;
1293 }
1294 
1295 /* main entry point for dummynet list functions. co.do_pipe indicates
1296  * which function we want to support.
1297  * av may contain filtering arguments, either individual entries
1298  * or ranges, or lists (space or commas are valid separators).
1299  * Format for a range can be n1-n2 or n3 n4 n5 ...
1300  * In a range n1 must be <= n2, otherwise the range is ignored.
1301  * A number 'n4' is translate in a range 'n4-n4'
1302  * All number must be > 0 and < DN_MAX_ID-1
1303  */
1304 void
1305 dummynet_list(int ac, char *av[], int show_counters)
1306 {
1307 	struct dn_id *oid, *x = NULL;
1308 	int ret, i, l;
1309 	int n; 		/* # of ranges */
1310 	int buflen;
1311 	int max_size;	/* largest obj passed up */
1312 
1313 	ac--;
1314 	av++; 		/* skip 'list' | 'show' word */
1315 
1316 	n = parse_range(ac, av, NULL, 0);	/* Count # of ranges. */
1317 
1318 	/* Allocate space to store ranges */
1319 	l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1320 	oid = safe_calloc(1, l);
1321 	oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1322 
1323 	if (n > 0)	/* store ranges in idx */
1324 		parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1325 	/*
1326 	 * Compute the size of the largest object returned. If the
1327 	 * response leaves at least this much spare space in the
1328 	 * buffer, then surely the response is complete; otherwise
1329 	 * there might be a risk of truncation and we will need to
1330 	 * retry with a larger buffer.
1331 	 * XXX don't bother with smaller structs.
1332 	 */
1333 	max_size = sizeof(struct dn_fs);
1334 	if (max_size < sizeof(struct dn_sch))
1335 		max_size = sizeof(struct dn_sch);
1336 	if (max_size < sizeof(struct dn_flow))
1337 		max_size = sizeof(struct dn_flow);
1338 
1339 	switch (co.do_pipe) {
1340 	case 1:
1341 		oid->subtype = DN_LINK;	/* list pipe */
1342 		break;
1343 	case 2:
1344 		oid->subtype = DN_FS;	/* list queue */
1345 		break;
1346 	case 3:
1347 		oid->subtype = DN_SCH;	/* list sched */
1348 		break;
1349 	}
1350 
1351 	/*
1352 	 * Ask the kernel an estimate of the required space (result
1353 	 * in oid.id), unless we are requesting a subset of objects,
1354 	 * in which case the kernel does not give an exact answer.
1355 	 * In any case, space might grow in the meantime due to the
1356 	 * creation of new queues, so we must be prepared to retry.
1357 	 */
1358 	if (n > 0) {
1359 		buflen = 4*1024;
1360 	} else {
1361 		ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1362 		if (ret != 0 || oid->id <= sizeof(*oid))
1363 			goto done;
1364 		buflen = oid->id + max_size;
1365 		oid->len = sizeof(*oid); /* restore */
1366 	}
1367 	/* Try a few times, until the buffer fits */
1368 	for (i = 0; i < 20; i++) {
1369 		l = buflen;
1370 		x = safe_realloc(x, l);
1371 		bcopy(oid, x, oid->len);
1372 		ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1373 		if (ret != 0 || x->id <= sizeof(*oid))
1374 			goto done; /* no response */
1375 		if (l + max_size <= buflen)
1376 			break; /* ok */
1377 		buflen *= 2;	 /* double for next attempt */
1378 	}
1379 	list_pipes(x, O_NEXT(x, l));
1380 done:
1381 	if (x)
1382 		free(x);
1383 	free(oid);
1384 }
1385