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