xref: /freebsd/sbin/ipfw/dummynet.c (revision ee5cf11617a9b7f034d95c639bd4d27d1f09e848)
1 /*
2  * Codel/FQ_Codel and PIE/FQ_PIE Code:
3  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
4  *  Swinburne University of Technology, Melbourne, Australia.
5  * Portions of this code were made possible in part by a gift from
6  *  The Comcast Innovation Fund.
7  * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
8  *
9  * Copyright (c) 2002-2003,2010 Luigi Rizzo
10  *
11  * Redistribution and use in source forms, with and without modification,
12  * are permitted provided that this entire comment appears intact.
13  *
14  * Redistribution in binary form may occur without any restrictions.
15  * Obviously, it would be nice if you gave credit where credit is due
16  * but requiring it would be too onerous.
17  *
18  * This software is provided ``AS IS'' without any warranties of any kind.
19  *
20  * $FreeBSD$
21  *
22  * dummynet support
23  */
24 
25 #define NEW_AQM
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 /* XXX there are several sysctl leftover here */
29 #include <sys/sysctl.h>
30 
31 #include "ipfw2.h"
32 
33 #ifdef NEW_AQM
34 #include <stdint.h>
35 #endif
36 
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <libutil.h>
41 #include <netdb.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sysexits.h>
46 
47 #include <net/if.h>
48 #include <netinet/in.h>
49 #include <netinet/ip_fw.h>
50 #include <netinet/ip_dummynet.h>
51 #include <arpa/inet.h>	/* inet_ntoa */
52 
53 
54 static struct _s_x dummynet_params[] = {
55 	{ "plr",		TOK_PLR },
56 	{ "noerror",		TOK_NOERROR },
57 	{ "buckets",		TOK_BUCKETS },
58 	{ "dst-ip",		TOK_DSTIP },
59 	{ "src-ip",		TOK_SRCIP },
60 	{ "dst-port",		TOK_DSTPORT },
61 	{ "src-port",		TOK_SRCPORT },
62 	{ "proto",		TOK_PROTO },
63 	{ "weight",		TOK_WEIGHT },
64 	{ "lmax",		TOK_LMAX },
65 	{ "maxlen",		TOK_LMAX },
66 	{ "all",		TOK_ALL },
67 	{ "mask",		TOK_MASK }, /* alias for both */
68 	{ "sched_mask",		TOK_SCHED_MASK },
69 	{ "flow_mask",		TOK_FLOW_MASK },
70 	{ "droptail",		TOK_DROPTAIL },
71 	{ "ecn",		TOK_ECN },
72 	{ "red",		TOK_RED },
73 	{ "gred",		TOK_GRED },
74 #ifdef NEW_AQM
75 	{ "codel",		TOK_CODEL}, /* Codel AQM */
76 	{ "fq_codel",	TOK_FQ_CODEL}, /* FQ-Codel  */
77 	{ "pie",		TOK_PIE}, /* PIE AQM */
78 	{ "fq_pie",		TOK_FQ_PIE}, /* FQ-PIE */
79 #endif
80 	{ "bw",			TOK_BW },
81 	{ "bandwidth",		TOK_BW },
82 	{ "delay",		TOK_DELAY },
83 	{ "link",		TOK_LINK },
84 	{ "pipe",		TOK_PIPE },
85 	{ "queue",		TOK_QUEUE },
86 	{ "flowset",		TOK_FLOWSET },
87 	{ "sched",		TOK_SCHED },
88 	{ "pri",		TOK_PRI },
89 	{ "priority",		TOK_PRI },
90 	{ "type",		TOK_TYPE },
91 	{ "flow-id",		TOK_FLOWID},
92 	{ "dst-ipv6",		TOK_DSTIP6},
93 	{ "dst-ip6",		TOK_DSTIP6},
94 	{ "src-ipv6",		TOK_SRCIP6},
95 	{ "src-ip6",		TOK_SRCIP6},
96 	{ "profile",		TOK_PROFILE},
97 	{ "burst",		TOK_BURST},
98 	{ "dummynet-params",	TOK_NULL },
99 	{ NULL, 0 }	/* terminator */
100 };
101 
102 #ifdef NEW_AQM
103 /* AQM/extra sched parameters  tokens*/
104 static struct _s_x aqm_params[] = {
105 	{ "target",		TOK_TARGET},
106 	{ "interval",		TOK_INTERVAL},
107 	{ "limit",		TOK_LIMIT},
108 	{ "flows",		TOK_FLOWS},
109 	{ "quantum",		TOK_QUANTUM},
110 	{ "ecn",		TOK_ECN},
111 	{ "noecn",		TOK_NO_ECN},
112 	{ "tupdate",		TOK_TUPDATE},
113 	{ "max_burst",		TOK_MAX_BURST},
114 	{ "max_ecnth",	TOK_MAX_ECNTH},
115 	{ "alpha",		TOK_ALPHA},
116 	{ "beta",		TOK_BETA},
117 	{ "capdrop",	TOK_CAPDROP},
118 	{ "nocapdrop",	TOK_NO_CAPDROP},
119 	{ "onoff",	TOK_ONOFF},
120 	{ "dre",	TOK_DRE},
121 	{ "ts",	TOK_TS},
122 	{ "derand",	TOK_DERAND},
123 	{ "noderand",	TOK_NO_DERAND},
124 	{ NULL, 0 }	/* terminator */
125 };
126 #endif
127 
128 #define O_NEXT(p, len) ((void *)((char *)p + len))
129 
130 static void
131 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
132 {
133 	oid->len = len;
134 	oid->type = type;
135 	oid->subtype = 0;
136 	oid->id = id;
137 }
138 
139 /* make room in the buffer and move the pointer forward */
140 static void *
141 o_next(struct dn_id **o, int len, int type)
142 {
143 	struct dn_id *ret = *o;
144 	oid_fill(ret, len, type, 0);
145 	*o = O_NEXT(*o, len);
146 	return ret;
147 }
148 
149 #ifdef NEW_AQM
150 
151 /* Codel flags */
152 enum {
153 	CODEL_ECN_ENABLED = 1
154 };
155 
156 /* PIE flags, from PIE kernel module */
157 enum {
158 	PIE_ECN_ENABLED = 1,
159 	PIE_CAPDROP_ENABLED = 2,
160 	PIE_ON_OFF_MODE_ENABLED = 4,
161 	PIE_DEPRATEEST_ENABLED = 8,
162 	PIE_DERAND_ENABLED = 16
163 };
164 
165 #define PIE_FIX_POINT_BITS 13
166 #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
167 
168 /* integer to time */
169 void
170 us_to_time(int t,char *strt)
171 {
172 	if (t < 0)
173 		strt[0]='\0';
174 	else if ( t==0 )
175 		sprintf(strt,"%d", t);
176 	else if (t< 1000)
177 		sprintf(strt,"%dus", t);
178 	else if (t < 1000000)
179 		sprintf(strt,"%gms", (float) t / 1000);
180 	else
181 		sprintf(strt,"%gfs", (float) t / 1000000);
182 }
183 
184 /*
185  * returns -1 if s is not a valid time, otherwise, return time in us
186  */
187 static long
188 time_to_us(const char *s)
189 {
190 	int i, dots = 0;
191 	int len = strlen(s);
192 	char strt[16]="", stru[16]="";
193 
194 	if (len>15)
195 		return -1;
196 	for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
197 		if (s[i]=='.') {
198 			if (dots)
199 				return -1;
200 			else
201 				dots++;
202 		}
203 
204 	if (!i)
205 		return -1;
206 	strncpy(strt, s, i);
207 	if (i<len)
208 		strcpy(stru, s+i);
209 	else
210 		strcpy(stru, "ms");
211 
212 	if (!strcasecmp(stru, "us"))
213 		return atol(strt);
214 	if (!strcasecmp(stru, "ms"))
215 		return (strtod(strt, NULL) * 1000);
216 	if (!strcasecmp(stru, "s"))
217 		return (strtod(strt, NULL)*1000000);
218 
219 	return -1;
220 }
221 
222 
223 /* Get AQM or scheduler extra parameters  */
224 void
225 get_extra_parms(uint32_t nr, char *out, int subtype)
226 {
227 	struct dn_extra_parms *ep;
228 	int ret;
229 	char strt1[15], strt2[15], strt3[15];
230 	u_int l;
231 
232 	/* prepare the request */
233 	l = sizeof(struct dn_extra_parms);
234 	ep = safe_calloc(1, l);
235 	memset(ep, 0, sizeof(*ep));
236 	*out = '\0';
237 
238 	oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
239 	ep->oid.len = l;
240 	ep->oid.subtype = subtype;
241 	ep->nr = nr;
242 
243 	ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
244 	if (ret) {
245 		free(ep);
246 		errx(EX_DATAERR, "Error getting extra parameters\n");
247 	}
248 
249 	switch (subtype) {
250 	case DN_AQM_PARAMS:
251 		if( !strcasecmp(ep->name, "codel")) {
252 			us_to_time(ep->par[0], strt1);
253 			us_to_time(ep->par[1], strt2);
254 			l = sprintf(out, " AQM CoDel target %s interval %s",
255 				strt1, strt2);
256 			if (ep->par[2] & CODEL_ECN_ENABLED)
257 				l = sprintf(out + l, " ECN");
258 			else
259 				l += sprintf(out + l, " NoECN");
260 		} else if( !strcasecmp(ep->name, "pie")) {
261 			us_to_time(ep->par[0], strt1);
262 			us_to_time(ep->par[1], strt2);
263 			us_to_time(ep->par[2], strt3);
264 			l = sprintf(out, " AQM type PIE target %s tupdate %s alpha "
265 					"%g beta %g max_burst %s max_ecnth %.3g",
266 					strt1,
267 					strt2,
268 					ep->par[4] / (float) PIE_SCALE,
269 					ep->par[5] / (float) PIE_SCALE,
270 					strt3,
271 					ep->par[3] / (float) PIE_SCALE
272 				);
273 
274 			if (ep->par[6] & PIE_ECN_ENABLED)
275 				l += sprintf(out + l, " ECN");
276 			else
277 				l += sprintf(out + l, " NoECN");
278 			if (ep->par[6] & PIE_CAPDROP_ENABLED)
279 				l += sprintf(out + l, " CapDrop");
280 			else
281 				l += sprintf(out + l, " NoCapDrop");
282 			if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
283 				l += sprintf(out + l, " OnOff");
284 			if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
285 				l += sprintf(out + l, " DRE");
286 			else
287 				l += sprintf(out + l, " TS");
288 			if (ep->par[6] & PIE_DERAND_ENABLED)
289 				l += sprintf(out + l, " Derand");
290 			else
291 				l += sprintf(out + l, " NoDerand");
292 		}
293 		break;
294 
295 	case	DN_SCH_PARAMS:
296 		if (!strcasecmp(ep->name,"FQ_CODEL")) {
297 			us_to_time(ep->par[0], strt1);
298 			us_to_time(ep->par[1], strt2);
299 			l = sprintf(out," FQ_CODEL target %s interval %s"
300 				" quantum %jd limit %jd flows %jd",
301 				strt1, strt2,
302 				(intmax_t) ep->par[3],
303 				(intmax_t) ep->par[4],
304 				(intmax_t) ep->par[5]
305 				);
306 			if (ep->par[2] & CODEL_ECN_ENABLED)
307 				l += sprintf(out + l, " ECN");
308 			else
309 				l += sprintf(out + l, " NoECN");
310 			l += sprintf(out + l, "\n");
311 		} else 	if (!strcasecmp(ep->name,"FQ_PIE")) {
312 			us_to_time(ep->par[0], strt1);
313 			us_to_time(ep->par[1], strt2);
314 			us_to_time(ep->par[2], strt3);
315 			l = sprintf(out, "  FQ_PIE target %s tupdate %s alpha "
316 				"%g beta %g max_burst %s max_ecnth %.3g"
317 				" quantum %jd limit %jd flows %jd",
318 				strt1,
319 				strt2,
320 				ep->par[4] / (float) PIE_SCALE,
321 				ep->par[5] / (float) PIE_SCALE,
322 				strt3,
323 				ep->par[3] / (float) PIE_SCALE,
324 				(intmax_t) ep->par[7],
325 				(intmax_t) ep->par[8],
326 				(intmax_t) ep->par[9]
327 			);
328 
329 			if (ep->par[6] & PIE_ECN_ENABLED)
330 				l += sprintf(out + l, " ECN");
331 			else
332 				l += sprintf(out + l, " NoECN");
333 			if (ep->par[6] & PIE_CAPDROP_ENABLED)
334 				l += sprintf(out + l, " CapDrop");
335 			else
336 				l += sprintf(out + l, " NoCapDrop");
337 			if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
338 				l += sprintf(out + l, " OnOff");
339 			if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
340 				l += sprintf(out + l, " DRE");
341 			else
342 				l += sprintf(out + l, " TS");
343 			if (ep->par[6] & PIE_DERAND_ENABLED)
344 				l += sprintf(out + l, " Derand");
345 			else
346 				l += sprintf(out + l, " NoDerand");
347 			l += sprintf(out + l, "\n");
348 		}
349 		break;
350 	}
351 
352 	free(ep);
353 }
354 #endif
355 
356 
357 #if 0
358 static int
359 sort_q(void *arg, const void *pa, const void *pb)
360 {
361 	int rev = (co.do_sort < 0);
362 	int field = rev ? -co.do_sort : co.do_sort;
363 	long long res = 0;
364 	const struct dn_flow_queue *a = pa;
365 	const struct dn_flow_queue *b = pb;
366 
367 	switch (field) {
368 	case 1: /* pkts */
369 		res = a->len - b->len;
370 		break;
371 	case 2: /* bytes */
372 		res = a->len_bytes - b->len_bytes;
373 		break;
374 
375 	case 3: /* tot pkts */
376 		res = a->tot_pkts - b->tot_pkts;
377 		break;
378 
379 	case 4: /* tot bytes */
380 		res = a->tot_bytes - b->tot_bytes;
381 		break;
382 	}
383 	if (res < 0)
384 		res = -1;
385 	if (res > 0)
386 		res = 1;
387 	return (int)(rev ? res : -res);
388 }
389 #endif
390 
391 /* print a mask and header for the subsequent list of flows */
392 static void
393 print_mask(struct ipfw_flow_id *id)
394 {
395 	if (!IS_IP6_FLOW_ID(id)) {
396 		printf("    "
397 		    "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
398 		    id->extra ? "queue," : "",
399 		    id->proto,
400 		    id->src_ip, id->src_port,
401 		    id->dst_ip, id->dst_port);
402 	} else {
403 		char buf[255];
404 		printf("\n        mask: %sproto: 0x%02x, flow_id: 0x%08x,  ",
405 		    id->extra ? "queue," : "",
406 		    id->proto, id->flow_id6);
407 		inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
408 		printf("%s/0x%04x -> ", buf, id->src_port);
409 		inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
410 		printf("%s/0x%04x\n", buf, id->dst_port);
411 	}
412 }
413 
414 static void
415 print_header(struct ipfw_flow_id *id)
416 {
417 	if (!IS_IP6_FLOW_ID(id))
418 		printf("BKT Prot ___Source IP/port____ "
419 		    "____Dest. IP/port____ "
420 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
421 	else
422 		printf("BKT ___Prot___ _flow-id_ "
423 		    "______________Source IPv6/port_______________ "
424 		    "_______________Dest. IPv6/port_______________ "
425 		    "Tot_pkt/bytes Pkt/Byte Drp\n");
426 }
427 
428 static void
429 list_flow(struct buf_pr *bp, struct dn_flow *ni)
430 {
431 	char buff[255];
432 	struct protoent *pe = NULL;
433 	struct in_addr ina;
434 	struct ipfw_flow_id *id = &ni->fid;
435 
436 	pe = getprotobynumber(id->proto);
437 		/* XXX: Should check for IPv4 flows */
438 	bprintf(bp, "%3u%c", (ni->oid.id) & 0xff,
439 		id->extra ? '*' : ' ');
440 	if (!IS_IP6_FLOW_ID(id)) {
441 		if (pe)
442 			bprintf(bp, "%-4s ", pe->p_name);
443 		else
444 			bprintf(bp, "%4u ", id->proto);
445 		ina.s_addr = htonl(id->src_ip);
446 		bprintf(bp, "%15s/%-5d ",
447 		    inet_ntoa(ina), id->src_port);
448 		ina.s_addr = htonl(id->dst_ip);
449 		bprintf(bp, "%15s/%-5d ",
450 		    inet_ntoa(ina), id->dst_port);
451 	} else {
452 		/* Print IPv6 flows */
453 		if (pe != NULL)
454 			bprintf(bp, "%9s ", pe->p_name);
455 		else
456 			bprintf(bp, "%9u ", id->proto);
457 		bprintf(bp, "%7d  %39s/%-5d ", id->flow_id6,
458 		    inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
459 		    id->src_port);
460 		bprintf(bp, " %39s/%-5d ",
461 		    inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
462 		    id->dst_port);
463 	}
464 	pr_u64(bp, &ni->tot_pkts, 4);
465 	pr_u64(bp, &ni->tot_bytes, 8);
466 	bprintf(bp, "%2u %4u %3u",
467 	    ni->length, ni->len_bytes, ni->drops);
468 }
469 
470 static void
471 print_flowset_parms(struct dn_fs *fs, char *prefix)
472 {
473 	int l;
474 	char qs[30];
475 	char plr[30];
476 	char red[200];	/* Display RED parameters */
477 
478 	l = fs->qsize;
479 	if (fs->flags & DN_QSIZE_BYTES) {
480 		if (l >= 8192)
481 			sprintf(qs, "%d KB", l / 1024);
482 		else
483 			sprintf(qs, "%d B", l);
484 	} else
485 		sprintf(qs, "%3d sl.", l);
486 	if (fs->plr)
487 		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
488 	else
489 		plr[0] = '\0';
490 
491 	if (fs->flags & DN_IS_RED) {	/* RED parameters */
492 		sprintf(red,
493 		    "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
494 		    (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
495 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
496 		    fs->min_th,
497 		    fs->max_th,
498 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
499 		if (fs->flags & DN_IS_ECN)
500 			strncat(red, " (ecn)", 6);
501 #ifdef NEW_AQM
502 	/* get AQM parameters */
503 	} else if (fs->flags & DN_IS_AQM) {
504 			get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
505 #endif
506 	} else
507 		sprintf(red, "droptail");
508 
509 	if (prefix[0]) {
510 	    printf("%s %s%s %d queues (%d buckets) %s\n",
511 		prefix, qs, plr, fs->oid.id, fs->buckets, red);
512 	    prefix[0] = '\0';
513 	} else {
514 	    printf("q%05d %s%s %d flows (%d buckets) sched %d "
515 			"weight %d lmax %d pri %d %s\n",
516 		fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
517 		fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
518 	    if (fs->flags & DN_HAVE_MASK)
519 		print_mask(&fs->flow_mask);
520 	}
521 }
522 
523 static void
524 print_extra_delay_parms(struct dn_profile *p)
525 {
526 	double loss;
527 	if (p->samples_no <= 0)
528 		return;
529 
530 	loss = p->loss_level;
531 	loss /= p->samples_no;
532 	printf("\t profile: name \"%s\" loss %f samples %d\n",
533 		p->name, loss, p->samples_no);
534 }
535 
536 static void
537 flush_buf(char *buf)
538 {
539 	if (buf[0])
540 		printf("%s\n", buf);
541 	buf[0] = '\0';
542 }
543 
544 /*
545  * generic list routine. We expect objects in a specific order, i.e.
546  * PIPES AND SCHEDULERS:
547  *	link; scheduler; internal flowset if any; instances
548  * we can tell a pipe from the number.
549  *
550  * FLOWSETS:
551  *	flowset; queues;
552  * link i (int queue); scheduler i; si(i) { flowsets() : queues }
553  */
554 static void
555 list_pipes(struct dn_id *oid, struct dn_id *end)
556 {
557     char buf[160];	/* pending buffer */
558     int toPrint = 1;	/* print header */
559     struct buf_pr bp;
560 
561     buf[0] = '\0';
562     bp_alloc(&bp, 4096);
563     for (; oid != end; oid = O_NEXT(oid, oid->len)) {
564 	if (oid->len < sizeof(*oid))
565 		errx(1, "invalid oid len %d\n", oid->len);
566 
567 	switch (oid->type) {
568 	default:
569 	    flush_buf(buf);
570 	    printf("unrecognized object %d size %d\n", oid->type, oid->len);
571 	    break;
572 	case DN_TEXT: /* list of attached flowsets */
573 	    {
574 		int i, l;
575 		struct {
576 			struct dn_id id;
577 			uint32_t p[0];
578 		} *d = (void *)oid;
579 		l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
580 		if (l == 0)
581 		    break;
582 		printf("   Children flowsets: ");
583 		for (i = 0; i < l; i++)
584 			printf("%u ", d->p[i]);
585 		printf("\n");
586 		break;
587 	    }
588 	case DN_CMD_GET:
589 	    if (co.verbose)
590 		printf("answer for cmd %d, len %d\n", oid->type, oid->id);
591 	    break;
592 	case DN_SCH: {
593 	    struct dn_sch *s = (struct dn_sch *)oid;
594 	    flush_buf(buf);
595 	    printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
596 			s->sched_nr,
597 			s->name, s->flags, s->buckets, s->oid.id);
598 #ifdef NEW_AQM
599 		char parms[200];
600 		get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
601 		printf("%s",parms);
602 #endif
603 	    if (s->flags & DN_HAVE_MASK)
604 		print_mask(&s->sched_mask);
605 	    }
606 	    break;
607 
608 	case DN_FLOW:
609 	    if (toPrint != 0) {
610 		    print_header(&((struct dn_flow *)oid)->fid);
611 		    toPrint = 0;
612 	    }
613 	    list_flow(&bp, (struct dn_flow *)oid);
614 	    printf("%s\n", bp.buf);
615 	    break;
616 
617 	case DN_LINK: {
618 	    struct dn_link *p = (struct dn_link *)oid;
619 	    double b = p->bandwidth;
620 	    char bwbuf[30];
621 	    char burst[5 + 7];
622 
623 	    /* This starts a new object so flush buffer */
624 	    flush_buf(buf);
625 	    /* data rate */
626 	    if (b == 0)
627 		sprintf(bwbuf, "unlimited     ");
628 	    else if (b >= 1000000)
629 		sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
630 	    else if (b >= 1000)
631 		sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
632 	    else
633 		sprintf(bwbuf, "%7.3f bit/s ", b);
634 
635 	    if (humanize_number(burst, sizeof(burst), p->burst,
636 		    "", HN_AUTOSCALE, 0) < 0 || co.verbose)
637 		sprintf(burst, "%d", (int)p->burst);
638 	    sprintf(buf, "%05d: %s %4d ms burst %s",
639 		p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
640 	    }
641 	    break;
642 
643 	case DN_FS:
644 	    print_flowset_parms((struct dn_fs *)oid, buf);
645 	    break;
646 	case DN_PROFILE:
647 	    flush_buf(buf);
648 	    print_extra_delay_parms((struct dn_profile *)oid);
649 	}
650 	flush_buf(buf); // XXX does it really go here ?
651     }
652 
653     bp_free(&bp);
654 }
655 
656 /*
657  * Delete pipe, queue or scheduler i
658  */
659 int
660 ipfw_delete_pipe(int do_pipe, int i)
661 {
662 	struct {
663 		struct dn_id oid;
664 		uintptr_t a[1];	/* add more if we want a list */
665 	} cmd;
666 	oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
667 	cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
668 		( (do_pipe == 2) ? DN_FS : DN_SCH);
669 	cmd.a[0] = i;
670 	i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
671 	if (i) {
672 		i = 1;
673 		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
674 	}
675 	return i;
676 }
677 
678 /*
679  * Code to parse delay profiles.
680  *
681  * Some link types introduce extra delays in the transmission
682  * of a packet, e.g. because of MAC level framing, contention on
683  * the use of the channel, MAC level retransmissions and so on.
684  * From our point of view, the channel is effectively unavailable
685  * for this extra time, which is constant or variable depending
686  * on the link type. Additionally, packets may be dropped after this
687  * time (e.g. on a wireless link after too many retransmissions).
688  * We can model the additional delay with an empirical curve
689  * that represents its distribution.
690  *
691  *      cumulative probability
692  *      1.0 ^
693  *          |
694  *      L   +-- loss-level          x
695  *          |                 ******
696  *          |                *
697  *          |           *****
698  *          |          *
699  *          |        **
700  *          |       *
701  *          +-------*------------------->
702  *                      delay
703  *
704  * The empirical curve may have both vertical and horizontal lines.
705  * Vertical lines represent constant delay for a range of
706  * probabilities; horizontal lines correspond to a discontinuty
707  * in the delay distribution: the link will use the largest delay
708  * for a given probability.
709  *
710  * To pass the curve to dummynet, we must store the parameters
711  * in a file as described below, and issue the command
712  *
713  *      ipfw pipe <n> config ... bw XXX profile <filename> ...
714  *
715  * The file format is the following, with whitespace acting as
716  * a separator and '#' indicating the beginning a comment:
717  *
718  *	samples N
719  *		the number of samples used in the internal
720  *		representation (2..1024; default 100);
721  *
722  *	loss-level L
723  *		The probability above which packets are lost.
724  *	       (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
725  *
726  *	name identifier
727  *		Optional a name (listed by "ipfw pipe show")
728  *		to identify the distribution;
729  *
730  *	"delay prob" | "prob delay"
731  *		One of these two lines is mandatory and defines
732  *		the format of the following lines with data points.
733  *
734  *	XXX YYY
735  *		2 or more lines representing points in the curve,
736  *		with either delay or probability first, according
737  *		to the chosen format.
738  *		The unit for delay is milliseconds.
739  *
740  * Data points does not need to be ordered or equal to the number
741  * specified in the "samples" line. ipfw will sort and interpolate
742  * the curve as needed.
743  *
744  * Example of a profile file:
745 
746 	name    bla_bla_bla
747 	samples 100
748 	loss-level    0.86
749 	prob    delay
750 	0       200	# minimum overhead is 200ms
751 	0.5     200
752 	0.5     300
753 	0.8     1000
754 	0.9     1300
755 	1       1300
756 
757  * Internally, we will convert the curve to a fixed number of
758  * samples, and when it is time to transmit a packet we will
759  * model the extra delay as extra bits in the packet.
760  *
761  */
762 
763 #define ED_MAX_LINE_LEN	256+ED_MAX_NAME_LEN
764 #define ED_TOK_SAMPLES	"samples"
765 #define ED_TOK_LOSS	"loss-level"
766 #define ED_TOK_NAME	"name"
767 #define ED_TOK_DELAY	"delay"
768 #define ED_TOK_PROB	"prob"
769 #define ED_TOK_BW	"bw"
770 #define ED_SEPARATORS	" \t\n"
771 #define ED_MIN_SAMPLES_NO	2
772 
773 /*
774  * returns 1 if s is a non-negative number, with at least one '.'
775  */
776 static int
777 is_valid_number(const char *s)
778 {
779 	int i, dots_found = 0;
780 	int len = strlen(s);
781 
782 	for (i = 0; i<len; ++i)
783 		if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
784 			return 0;
785 	return 1;
786 }
787 
788 /*
789  * Take as input a string describing a bandwidth value
790  * and return the numeric bandwidth value.
791  * set clocking interface or bandwidth value
792  */
793 static void
794 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
795 {
796 	if (*bandwidth != -1)
797 		warnx("duplicate token, override bandwidth value!");
798 
799 	if (arg[0] >= 'a' && arg[0] <= 'z') {
800 		if (!if_name) {
801 			errx(1, "no if support");
802 		}
803 		if (namelen >= IFNAMSIZ)
804 			warn("interface name truncated");
805 		namelen--;
806 		/* interface name */
807 		strncpy(if_name, arg, namelen);
808 		if_name[namelen] = '\0';
809 		*bandwidth = 0;
810 	} else {	/* read bandwidth value */
811 		int bw;
812 		char *end = NULL;
813 
814 		bw = strtoul(arg, &end, 0);
815 		if (*end == 'K' || *end == 'k') {
816 			end++;
817 			bw *= 1000;
818 		} else if (*end == 'M' || *end == 'm') {
819 			end++;
820 			bw *= 1000000;
821 		}
822 		if ((*end == 'B' &&
823 			_substrcmp2(end, "Bi", "Bit/s") != 0) ||
824 		    _substrcmp2(end, "by", "bytes") == 0)
825 			bw *= 8;
826 
827 		if (bw < 0)
828 			errx(EX_DATAERR, "bandwidth too large");
829 
830 		*bandwidth = bw;
831 		if (if_name)
832 			if_name[0] = '\0';
833 	}
834 }
835 
836 struct point {
837 	double prob;
838 	double delay;
839 };
840 
841 static int
842 compare_points(const void *vp1, const void *vp2)
843 {
844 	const struct point *p1 = vp1;
845 	const struct point *p2 = vp2;
846 	double res = 0;
847 
848 	res = p1->prob - p2->prob;
849 	if (res == 0)
850 		res = p1->delay - p2->delay;
851 	if (res < 0)
852 		return -1;
853 	else if (res > 0)
854 		return 1;
855 	else
856 		return 0;
857 }
858 
859 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
860 
861 static void
862 load_extra_delays(const char *filename, struct dn_profile *p,
863 	struct dn_link *link)
864 {
865 	char    line[ED_MAX_LINE_LEN];
866 	FILE    *f;
867 	int     lineno = 0;
868 	int     i;
869 
870 	int     samples = -1;
871 	double  loss = -1.0;
872 	char    profile_name[ED_MAX_NAME_LEN];
873 	int     delay_first = -1;
874 	int     do_points = 0;
875 	struct point    points[ED_MAX_SAMPLES_NO];
876 	int     points_no = 0;
877 
878 	/* XXX link never NULL? */
879 	p->link_nr = link->link_nr;
880 
881 	profile_name[0] = '\0';
882 	f = fopen(filename, "r");
883 	if (f == NULL)
884 		err(EX_UNAVAILABLE, "fopen: %s", filename);
885 
886 	while (fgets(line, ED_MAX_LINE_LEN, f)) {	 /* read commands */
887 		char *s, *cur = line, *name = NULL, *arg = NULL;
888 
889 		++lineno;
890 
891 		/* parse the line */
892 		while (cur) {
893 			s = strsep(&cur, ED_SEPARATORS);
894 			if (s == NULL || *s == '#')
895 				break;
896 			if (*s == '\0')
897 				continue;
898 			if (arg)
899 				errx(ED_EFMT("too many arguments"));
900 			if (name == NULL)
901 				name = s;
902 			else
903 				arg = s;
904 		}
905 		if (name == NULL)	/* empty line */
906 			continue;
907 		if (arg == NULL)
908 			errx(ED_EFMT("missing arg for %s"), name);
909 
910 		if (!strcasecmp(name, ED_TOK_SAMPLES)) {
911 		    if (samples > 0)
912 			errx(ED_EFMT("duplicate ``samples'' line"));
913 		    if (atoi(arg) <=0)
914 			errx(ED_EFMT("invalid number of samples"));
915 		    samples = atoi(arg);
916 		    if (samples>ED_MAX_SAMPLES_NO)
917 			    errx(ED_EFMT("too many samples, maximum is %d"),
918 				ED_MAX_SAMPLES_NO);
919 		    do_points = 0;
920 		} else if (!strcasecmp(name, ED_TOK_BW)) {
921 		    char buf[IFNAMSIZ];
922 		    read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
923 		} else if (!strcasecmp(name, ED_TOK_LOSS)) {
924 		    if (loss != -1.0)
925 			errx(ED_EFMT("duplicated token: %s"), name);
926 		    if (!is_valid_number(arg))
927 			errx(ED_EFMT("invalid %s"), arg);
928 		    loss = atof(arg);
929 		    if (loss > 1)
930 			errx(ED_EFMT("%s greater than 1.0"), name);
931 		    do_points = 0;
932 		} else if (!strcasecmp(name, ED_TOK_NAME)) {
933 		    if (profile_name[0] != '\0')
934 			errx(ED_EFMT("duplicated token: %s"), name);
935 		    strncpy(profile_name, arg, sizeof(profile_name) - 1);
936 		    profile_name[sizeof(profile_name)-1] = '\0';
937 		    do_points = 0;
938 		} else if (!strcasecmp(name, ED_TOK_DELAY)) {
939 		    if (do_points)
940 			errx(ED_EFMT("duplicated token: %s"), name);
941 		    delay_first = 1;
942 		    do_points = 1;
943 		} else if (!strcasecmp(name, ED_TOK_PROB)) {
944 		    if (do_points)
945 			errx(ED_EFMT("duplicated token: %s"), name);
946 		    delay_first = 0;
947 		    do_points = 1;
948 		} else if (do_points) {
949 		    if (!is_valid_number(name) || !is_valid_number(arg))
950 			errx(ED_EFMT("invalid point found"));
951 		    if (delay_first) {
952 			points[points_no].delay = atof(name);
953 			points[points_no].prob = atof(arg);
954 		    } else {
955 			points[points_no].delay = atof(arg);
956 			points[points_no].prob = atof(name);
957 		    }
958 		    if (points[points_no].prob > 1.0)
959 			errx(ED_EFMT("probability greater than 1.0"));
960 		    ++points_no;
961 		} else {
962 		    errx(ED_EFMT("unrecognised command '%s'"), name);
963 		}
964 	}
965 
966 	fclose (f);
967 
968 	if (samples == -1) {
969 	    warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
970 	    samples = 100;
971 	}
972 
973 	if (loss == -1.0) {
974 	    warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
975 	    loss = 1;
976 	}
977 
978 	/* make sure that there are enough points. */
979 	if (points_no < ED_MIN_SAMPLES_NO)
980 	    errx(ED_EFMT("too few samples, need at least %d"),
981 		ED_MIN_SAMPLES_NO);
982 
983 	qsort(points, points_no, sizeof(struct point), compare_points);
984 
985 	/* interpolation */
986 	for (i = 0; i<points_no-1; ++i) {
987 	    double y1 = points[i].prob * samples;
988 	    double x1 = points[i].delay;
989 	    double y2 = points[i+1].prob * samples;
990 	    double x2 = points[i+1].delay;
991 
992 	    int ix = y1;
993 	    int stop = y2;
994 
995 	    if (x1 == x2) {
996 		for (; ix<stop; ++ix)
997 		    p->samples[ix] = x1;
998 	    } else {
999 		double m = (y2-y1)/(x2-x1);
1000 		double c = y1 - m*x1;
1001 		for (; ix<stop ; ++ix)
1002 		    p->samples[ix] = (ix - c)/m;
1003 	    }
1004 	}
1005 	p->samples_no = samples;
1006 	p->loss_level = loss * samples;
1007 	strncpy(p->name, profile_name, sizeof(p->name));
1008 }
1009 
1010 #ifdef NEW_AQM
1011 
1012 /* Parse AQM/extra scheduler parameters */
1013 static int
1014 process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
1015 	uint16_t type)
1016 {
1017 	int i;
1018 
1019 	/* use kernel defaults */
1020 	for (i=0; i<DN_MAX_EXTRA_PARM; i++)
1021 		ep->par[i] = -1;
1022 
1023 	switch(type) {
1024 	case TOK_CODEL:
1025 	case TOK_FQ_CODEL:
1026 	/* Codel
1027 	 * 0- target, 1- interval, 2- flags,
1028 	 * FQ_CODEL
1029 	 * 3- quantum, 4- limit, 5- flows
1030 	 */
1031 		if (type==TOK_CODEL)
1032 			ep->par[2] = 0;
1033 		else
1034 			ep->par[2] = CODEL_ECN_ENABLED;
1035 
1036 		while (*ac > 0) {
1037 			int tok = match_token(aqm_params, *av);
1038 			(*ac)--; av++;
1039 			switch(tok) {
1040 			case TOK_TARGET:
1041 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1042 					errx(EX_DATAERR, "target needs time\n");
1043 
1044 				ep->par[0] = time_to_us(av[0]);
1045 				(*ac)--; av++;
1046 				break;
1047 
1048 			case TOK_INTERVAL:
1049 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1050 					errx(EX_DATAERR, "interval needs time\n");
1051 
1052 				ep->par[1] = time_to_us(av[0]);
1053 				(*ac)--; av++;
1054 				break;
1055 
1056 			case TOK_ECN:
1057 				ep->par[2] = CODEL_ECN_ENABLED;
1058 				break;
1059 			case TOK_NO_ECN:
1060 				ep->par[2] &= ~CODEL_ECN_ENABLED;
1061 				break;
1062 			/* Config fq_codel parameters */
1063 			case TOK_QUANTUM:
1064 				if (type != TOK_FQ_CODEL)
1065 					errx(EX_DATAERR, "quantum is not for codel\n");
1066 				if (*ac <= 0 || !is_valid_number(av[0]))
1067 					errx(EX_DATAERR, "quantum needs number\n");
1068 
1069 				ep->par[3]= atoi(av[0]);
1070 				(*ac)--; av++;
1071 				break;
1072 
1073 			case TOK_LIMIT:
1074 				if (type != TOK_FQ_CODEL)
1075 					errx(EX_DATAERR, "limit is not for codel, use queue instead\n");
1076 				if (*ac <= 0 || !is_valid_number(av[0]))
1077 					errx(EX_DATAERR, "limit needs number\n");
1078 
1079 				ep->par[4] = atoi(av[0]);
1080 				(*ac)--; av++;
1081 				break;
1082 
1083 			case TOK_FLOWS:
1084 				if (type != TOK_FQ_CODEL)
1085 					errx(EX_DATAERR, "flows is not for codel\n");
1086 				if (*ac <= 0 || !is_valid_number(av[0]))
1087 					errx(EX_DATAERR, "flows needs number\n");
1088 
1089 				ep->par[5] = atoi(av[0]);
1090 				(*ac)--; av++;
1091 				break;
1092 
1093 			default:
1094 				printf("%s is Invalid parameter\n", av[-1]);
1095 			}
1096 		}
1097 		break;
1098 	case TOK_PIE:
1099 	case TOK_FQ_PIE:
1100 		/* PIE
1101 		 * 0- target , 1- tupdate, 2- max_burst,
1102 		 * 3- max_ecnth, 4- alpha,
1103 		 * 5- beta, 6- flags
1104 		 * FQ_CODEL
1105 		 * 7- quantum, 8- limit, 9- flows
1106 		 */
1107 
1108 		if ( type == TOK_PIE)
1109 			ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
1110 				| PIE_DERAND_ENABLED;
1111 		else
1112 			/* for FQ-PIE, use TS mode */
1113 			ep->par[6] = PIE_CAPDROP_ENABLED |  PIE_DERAND_ENABLED
1114 				| PIE_ECN_ENABLED;
1115 
1116 		while (*ac > 0) {
1117 			int tok = match_token(aqm_params, *av);
1118 			(*ac)--; av++;
1119 			switch(tok) {
1120 			case TOK_TARGET:
1121 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1122 					errx(EX_DATAERR, "target needs time\n");
1123 
1124 				ep->par[0] = time_to_us(av[0]);
1125 				(*ac)--; av++;
1126 				break;
1127 
1128 			case TOK_TUPDATE:
1129 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1130 					errx(EX_DATAERR, "tupdate needs time\n");
1131 
1132 				ep->par[1] = time_to_us(av[0]);
1133 				(*ac)--; av++;
1134 				break;
1135 
1136 			case TOK_MAX_BURST:
1137 				if (*ac <= 0 || time_to_us(av[0]) < 0)
1138 					errx(EX_DATAERR, "max_burst needs time\n");
1139 
1140 				ep->par[2] = time_to_us(av[0]);
1141 				(*ac)--; av++;
1142 				break;
1143 
1144 			case TOK_MAX_ECNTH:
1145 				if (*ac <= 0 || !is_valid_number(av[0]))
1146 					errx(EX_DATAERR, "max_ecnth needs number\n");
1147 
1148 				ep->par[3] = atof(av[0]) * PIE_SCALE;
1149 				(*ac)--; av++;
1150 				break;
1151 
1152 			case TOK_ALPHA:
1153 				if (*ac <= 0 || !is_valid_number(av[0]))
1154 					errx(EX_DATAERR, "alpha needs number\n");
1155 
1156 				ep->par[4] = atof(av[0]) * PIE_SCALE;
1157 				(*ac)--; av++;
1158 				break;
1159 
1160 			case TOK_BETA:
1161 				if (*ac <= 0 || !is_valid_number(av[0]))
1162 					errx(EX_DATAERR, "beta needs number\n");
1163 
1164 				ep->par[5] = atof(av[0]) * PIE_SCALE;
1165 				(*ac)--; av++;
1166 				break;
1167 
1168 			case TOK_ECN:
1169 				ep->par[6] |= PIE_ECN_ENABLED;
1170 				break;
1171 			case TOK_NO_ECN:
1172 				ep->par[6] &= ~PIE_ECN_ENABLED;
1173 				break;
1174 
1175 			case TOK_CAPDROP:
1176 				ep->par[6] |= PIE_CAPDROP_ENABLED;
1177 				break;
1178 			case TOK_NO_CAPDROP:
1179 				ep->par[6] &= ~PIE_CAPDROP_ENABLED;
1180 				break;
1181 
1182 			case TOK_ONOFF:
1183 				ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
1184 				break;
1185 
1186 			case TOK_DRE:
1187 				ep->par[6] |= PIE_DEPRATEEST_ENABLED;
1188 				break;
1189 
1190 			case TOK_TS:
1191 				ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
1192 				break;
1193 
1194 			case TOK_DERAND:
1195 				ep->par[6] |= PIE_DERAND_ENABLED;
1196 				break;
1197 			case TOK_NO_DERAND:
1198 				ep->par[6] &= ~PIE_DERAND_ENABLED;
1199 				break;
1200 
1201 			/* Config fq_pie parameters */
1202 			case TOK_QUANTUM:
1203 				if (type != TOK_FQ_PIE)
1204 					errx(EX_DATAERR, "quantum is not for pie\n");
1205 				if (*ac <= 0 || !is_valid_number(av[0]))
1206 					errx(EX_DATAERR, "quantum needs number\n");
1207 
1208 				ep->par[7]= atoi(av[0]);
1209 				(*ac)--; av++;
1210 				break;
1211 
1212 			case TOK_LIMIT:
1213 				if (type != TOK_FQ_PIE)
1214 					errx(EX_DATAERR, "limit is not for pie, use queue instead\n");
1215 				if (*ac <= 0 || !is_valid_number(av[0]))
1216 					errx(EX_DATAERR, "limit needs number\n");
1217 
1218 				ep->par[8] = atoi(av[0]);
1219 				(*ac)--; av++;
1220 				break;
1221 
1222 			case TOK_FLOWS:
1223 				if (type != TOK_FQ_PIE)
1224 					errx(EX_DATAERR, "flows is not for pie\n");
1225 				if (*ac <= 0 || !is_valid_number(av[0]))
1226 					errx(EX_DATAERR, "flows needs number\n");
1227 
1228 				ep->par[9] = atoi(av[0]);
1229 				(*ac)--; av++;
1230 				break;
1231 
1232 
1233 			default:
1234 				printf("%s is invalid parameter\n", av[-1]);
1235 			}
1236 		}
1237 		break;
1238 	}
1239 
1240 	return 0;
1241 }
1242 
1243 #endif
1244 
1245 
1246 /*
1247  * configuration of pipes, schedulers, flowsets.
1248  * When we configure a new scheduler, an empty pipe is created, so:
1249  *
1250  * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
1251  *	sched N+Delta type fifo sched_mask ...
1252  *	pipe N+Delta <parameters>
1253  *	flowset N+Delta pipe N+Delta (no parameters)
1254  *	sched N type wf2q+ sched_mask ...
1255  *	pipe N <parameters>
1256  *
1257  * do_pipe = 2 -> flowset N config
1258  *	flowset N parameters
1259  *
1260  * do_pipe = 3 -> sched N config
1261  *	sched N parameters (default no pipe)
1262  *	optional Pipe N config ...
1263  * pipe ==>
1264  */
1265 void
1266 ipfw_config_pipe(int ac, char **av)
1267 {
1268 	int i;
1269 	u_int j;
1270 	char *end;
1271 	struct dn_id *buf, *base;
1272 	struct dn_sch *sch = NULL;
1273 	struct dn_link *p = NULL;
1274 	struct dn_fs *fs = NULL;
1275 	struct dn_profile *pf = NULL;
1276 	struct ipfw_flow_id *mask = NULL;
1277 #ifdef NEW_AQM
1278 	struct dn_extra_parms *aqm_extra;
1279 	struct dn_extra_parms *sch_extra;
1280 	int lmax_extra;
1281 #endif
1282 
1283 	int lmax;
1284 	uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
1285 
1286 	/*
1287 	 * allocate space for 1 header,
1288 	 * 1 scheduler, 1 link, 1 flowset, 1 profile
1289 	 */
1290 	lmax = sizeof(struct dn_id);	/* command header */
1291 	lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
1292 		sizeof(struct dn_fs) + sizeof(struct dn_profile);
1293 
1294 #ifdef NEW_AQM
1295 	/* Extra Params */
1296 	lmax_extra = sizeof(struct dn_extra_parms);
1297 	/* two lmax_extra because one for AQM params and another
1298 	 * sch params
1299 	 */
1300 	lmax += lmax_extra*2;
1301 #endif
1302 
1303 	av++; ac--;
1304 	/* Pipe number */
1305 	if (ac && isdigit(**av)) {
1306 		i = atoi(*av); av++; ac--;
1307 	} else
1308 		i = -1;
1309 	if (i <= 0)
1310 		errx(EX_USAGE, "need a pipe/flowset/sched number");
1311 	base = buf = safe_calloc(1, lmax);
1312 	/* all commands start with a 'CONFIGURE' and a version */
1313 	o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
1314 	base->id = DN_API_VERSION;
1315 
1316 	switch (co.do_pipe) {
1317 	case 1: /* "pipe N config ..." */
1318 		/* Allocate space for the WF2Q+ scheduler, its link
1319 		 * and the FIFO flowset. Set the number, but leave
1320 		 * the scheduler subtype and other parameters to 0
1321 		 * so the kernel will use appropriate defaults.
1322 		 * XXX todo: add a flag to record if a parameter
1323 		 * is actually configured.
1324 		 * If we do a 'pipe config' mask -> sched_mask.
1325 		 * The FIFO scheduler and link are derived from the
1326 		 * WF2Q+ one in the kernel.
1327 		 */
1328 #ifdef NEW_AQM
1329 		sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1330 		sch_extra ->oid.subtype = 0; /* don't configure scheduler */
1331 #endif
1332 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
1333 		p = o_next(&buf, sizeof(*p), DN_LINK);
1334 #ifdef NEW_AQM
1335 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1336 		aqm_extra ->oid.subtype = 0; /* don't configure AQM */
1337 #endif
1338 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1339 
1340 		sch->sched_nr = i;
1341 		sch->oid.subtype = 0;	/* defaults to WF2Q+ */
1342 		mask = &sch->sched_mask;
1343 		flags = &sch->flags;
1344 		buckets = &sch->buckets;
1345 		*flags |= DN_PIPE_CMD;
1346 
1347 		p->link_nr = i;
1348 
1349 		/* This flowset is only for the FIFO scheduler */
1350 		fs->fs_nr = i + 2*DN_MAX_ID;
1351 		fs->sched_nr = i + DN_MAX_ID;
1352 		break;
1353 
1354 	case 2: /* "queue N config ... " */
1355 #ifdef NEW_AQM
1356 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1357 		aqm_extra ->oid.subtype = 0;
1358 #endif
1359 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1360 		fs->fs_nr = i;
1361 		mask = &fs->flow_mask;
1362 		flags = &fs->flags;
1363 		buckets = &fs->buckets;
1364 		break;
1365 
1366 	case 3: /* "sched N config ..." */
1367 #ifdef NEW_AQM
1368 		sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1369 		sch_extra ->oid.subtype = 0;
1370 #endif
1371 		sch = o_next(&buf, sizeof(*sch), DN_SCH);
1372 #ifdef NEW_AQM
1373 		aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1374 		aqm_extra ->oid.subtype = 0;
1375 #endif
1376 		fs = o_next(&buf, sizeof(*fs), DN_FS);
1377 		sch->sched_nr = i;
1378 		mask = &sch->sched_mask;
1379 		flags = &sch->flags;
1380 		buckets = &sch->buckets;
1381 		/* fs is used only with !MULTIQUEUE schedulers */
1382 		fs->fs_nr = i + DN_MAX_ID;
1383 		fs->sched_nr = i;
1384 		break;
1385 	}
1386 	/* set to -1 those fields for which we want to reuse existing
1387 	 * values from the kernel.
1388 	 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
1389 	 * XXX todo: support reuse of the mask.
1390 	 */
1391 	if (p)
1392 		p->bandwidth = -1;
1393 	for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
1394 		fs->par[j] = -1;
1395 	while (ac > 0) {
1396 		double d;
1397 		int tok = match_token(dummynet_params, *av);
1398 		ac--; av++;
1399 
1400 		switch(tok) {
1401 		case TOK_NOERROR:
1402 			NEED(fs, "noerror is only for pipes");
1403 			fs->flags |= DN_NOERROR;
1404 			break;
1405 
1406 		case TOK_PLR:
1407 			NEED(fs, "plr is only for pipes");
1408 			NEED1("plr needs argument 0..1\n");
1409 			d = strtod(av[0], NULL);
1410 			if (d > 1)
1411 				d = 1;
1412 			else if (d < 0)
1413 				d = 0;
1414 			fs->plr = (int)(d*0x7fffffff);
1415 			ac--; av++;
1416 			break;
1417 
1418 		case TOK_QUEUE:
1419 			NEED(fs, "queue is only for pipes or flowsets");
1420 			NEED1("queue needs queue size\n");
1421 			end = NULL;
1422 			fs->qsize = strtoul(av[0], &end, 0);
1423 			if (*end == 'K' || *end == 'k') {
1424 				fs->flags |= DN_QSIZE_BYTES;
1425 				fs->qsize *= 1024;
1426 			} else if (*end == 'B' ||
1427 			    _substrcmp2(end, "by", "bytes") == 0) {
1428 				fs->flags |= DN_QSIZE_BYTES;
1429 			}
1430 			ac--; av++;
1431 			break;
1432 
1433 		case TOK_BUCKETS:
1434 			NEED(fs, "buckets is only for pipes or flowsets");
1435 			NEED1("buckets needs argument\n");
1436 			*buckets = strtoul(av[0], NULL, 0);
1437 			ac--; av++;
1438 			break;
1439 
1440 		case TOK_FLOW_MASK:
1441 		case TOK_SCHED_MASK:
1442 		case TOK_MASK:
1443 			NEED(mask, "tok_mask");
1444 			NEED1("mask needs mask specifier\n");
1445 			/*
1446 			 * per-flow queue, mask is dst_ip, dst_port,
1447 			 * src_ip, src_port, proto measured in bits
1448 			 */
1449 
1450 			bzero(mask, sizeof(*mask));
1451 			end = NULL;
1452 
1453 			while (ac >= 1) {
1454 			    uint32_t *p32 = NULL;
1455 			    uint16_t *p16 = NULL;
1456 			    uint32_t *p20 = NULL;
1457 			    struct in6_addr *pa6 = NULL;
1458 			    uint32_t a;
1459 
1460 			    tok = match_token(dummynet_params, *av);
1461 			    ac--; av++;
1462 			    switch(tok) {
1463 			    case TOK_ALL:
1464 				    /*
1465 				     * special case, all bits significant
1466 				     * except 'extra' (the queue number)
1467 				     */
1468 				    mask->dst_ip = ~0;
1469 				    mask->src_ip = ~0;
1470 				    mask->dst_port = ~0;
1471 				    mask->src_port = ~0;
1472 				    mask->proto = ~0;
1473 				    n2mask(&mask->dst_ip6, 128);
1474 				    n2mask(&mask->src_ip6, 128);
1475 				    mask->flow_id6 = ~0;
1476 				    *flags |= DN_HAVE_MASK;
1477 				    goto end_mask;
1478 
1479 			    case TOK_QUEUE:
1480 				    mask->extra = ~0;
1481 				    *flags |= DN_HAVE_MASK;
1482 				    goto end_mask;
1483 
1484 			    case TOK_DSTIP:
1485 				    mask->addr_type = 4;
1486 				    p32 = &mask->dst_ip;
1487 				    break;
1488 
1489 			    case TOK_SRCIP:
1490 				    mask->addr_type = 4;
1491 				    p32 = &mask->src_ip;
1492 				    break;
1493 
1494 			    case TOK_DSTIP6:
1495 				    mask->addr_type = 6;
1496 				    pa6 = &mask->dst_ip6;
1497 				    break;
1498 
1499 			    case TOK_SRCIP6:
1500 				    mask->addr_type = 6;
1501 				    pa6 = &mask->src_ip6;
1502 				    break;
1503 
1504 			    case TOK_FLOWID:
1505 				    mask->addr_type = 6;
1506 				    p20 = &mask->flow_id6;
1507 				    break;
1508 
1509 			    case TOK_DSTPORT:
1510 				    p16 = &mask->dst_port;
1511 				    break;
1512 
1513 			    case TOK_SRCPORT:
1514 				    p16 = &mask->src_port;
1515 				    break;
1516 
1517 			    case TOK_PROTO:
1518 				    break;
1519 
1520 			    default:
1521 				    ac++; av--; /* backtrack */
1522 				    goto end_mask;
1523 			    }
1524 			    if (ac < 1)
1525 				    errx(EX_USAGE, "mask: value missing");
1526 			    if (*av[0] == '/') {
1527 				    a = strtoul(av[0]+1, &end, 0);
1528 				    if (pa6 == NULL)
1529 					    a = (a == 32) ? ~0 : (1 << a) - 1;
1530 			    } else
1531 				    a = strtoul(av[0], &end, 0);
1532 			    if (p32 != NULL)
1533 				    *p32 = a;
1534 			    else if (p16 != NULL) {
1535 				    if (a > 0xFFFF)
1536 					    errx(EX_DATAERR,
1537 						"port mask must be 16 bit");
1538 				    *p16 = (uint16_t)a;
1539 			    } else if (p20 != NULL) {
1540 				    if (a > 0xfffff)
1541 					errx(EX_DATAERR,
1542 					    "flow_id mask must be 20 bit");
1543 				    *p20 = (uint32_t)a;
1544 			    } else if (pa6 != NULL) {
1545 				    if (a > 128)
1546 					errx(EX_DATAERR,
1547 					    "in6addr invalid mask len");
1548 				    else
1549 					n2mask(pa6, a);
1550 			    } else {
1551 				    if (a > 0xFF)
1552 					    errx(EX_DATAERR,
1553 						"proto mask must be 8 bit");
1554 				    mask->proto = (uint8_t)a;
1555 			    }
1556 			    if (a != 0)
1557 				    *flags |= DN_HAVE_MASK;
1558 			    ac--; av++;
1559 			} /* end while, config masks */
1560 end_mask:
1561 			break;
1562 #ifdef NEW_AQM
1563 		case TOK_CODEL:
1564 		case TOK_PIE:
1565 			NEED(fs, "codel/pie is only for flowsets");
1566 
1567 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1568 			fs->flags |= DN_IS_AQM;
1569 
1570 			strcpy(aqm_extra->name,av[-1]);
1571 			aqm_extra->oid.subtype = DN_AQM_PARAMS;
1572 
1573 			process_extra_parms(&ac, av, aqm_extra, tok);
1574 			break;
1575 
1576 		case TOK_FQ_CODEL:
1577 		case TOK_FQ_PIE:
1578 			if (!strcmp(av[-1],"type"))
1579 				errx(EX_DATAERR, "use type before fq_codel/fq_pie");
1580 
1581 			NEED(sch, "fq_codel/fq_pie is only for schd");
1582 			strcpy(sch_extra->name,av[-1]);
1583 			sch_extra->oid.subtype = DN_SCH_PARAMS;
1584 			process_extra_parms(&ac, av, sch_extra, tok);
1585 			break;
1586 #endif
1587 		case TOK_RED:
1588 		case TOK_GRED:
1589 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1590 			fs->flags |= DN_IS_RED;
1591 			if (tok == TOK_GRED)
1592 				fs->flags |= DN_IS_GENTLE_RED;
1593 			/*
1594 			 * the format for parameters is w_q/min_th/max_th/max_p
1595 			 */
1596 			if ((end = strsep(&av[0], "/"))) {
1597 			    double w_q = strtod(end, NULL);
1598 			    if (w_q > 1 || w_q <= 0)
1599 				errx(EX_DATAERR, "0 < w_q <= 1");
1600 			    fs->w_q = (int) (w_q * (1 << SCALE_RED));
1601 			}
1602 			if ((end = strsep(&av[0], "/"))) {
1603 			    fs->min_th = strtoul(end, &end, 0);
1604 			    if (*end == 'K' || *end == 'k')
1605 				fs->min_th *= 1024;
1606 			}
1607 			if ((end = strsep(&av[0], "/"))) {
1608 			    fs->max_th = strtoul(end, &end, 0);
1609 			    if (*end == 'K' || *end == 'k')
1610 				fs->max_th *= 1024;
1611 			}
1612 			if ((end = strsep(&av[0], "/"))) {
1613 			    double max_p = strtod(end, NULL);
1614 			    if (max_p > 1 || max_p < 0)
1615 				errx(EX_DATAERR, "0 <= max_p <= 1");
1616 			    fs->max_p = (int)(max_p * (1 << SCALE_RED));
1617 			}
1618 			ac--; av++;
1619 			break;
1620 
1621 		case TOK_ECN:
1622 			fs->flags |= DN_IS_ECN;
1623 			break;
1624 
1625 		case TOK_DROPTAIL:
1626 			NEED(fs, "droptail is only for flowsets");
1627 			fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1628 			break;
1629 
1630 		case TOK_BW:
1631 			NEED(p, "bw is only for links");
1632 			NEED1("bw needs bandwidth or interface\n");
1633 			read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1634 			ac--; av++;
1635 			break;
1636 
1637 		case TOK_DELAY:
1638 			NEED(p, "delay is only for links");
1639 			NEED1("delay needs argument 0..10000ms\n");
1640 			p->delay = strtoul(av[0], NULL, 0);
1641 			ac--; av++;
1642 			break;
1643 
1644 		case TOK_TYPE: {
1645 			int l;
1646 			NEED(sch, "type is only for schedulers");
1647 			NEED1("type needs a string");
1648 			l = strlen(av[0]);
1649 			if (l == 0 || l > 15)
1650 				errx(1, "type %s too long\n", av[0]);
1651 			strcpy(sch->name, av[0]);
1652 			sch->oid.subtype = 0; /* use string */
1653 #ifdef NEW_AQM
1654 			/* if fq_codel is selected, consider all tokens after it
1655 			 * as parameters
1656 			 */
1657 			if (!strcasecmp(av[0],"fq_codel") || !strcasecmp(av[0],"fq_pie")){
1658 				strcpy(sch_extra->name,av[0]);
1659 				sch_extra->oid.subtype = DN_SCH_PARAMS;
1660 				process_extra_parms(&ac, av, sch_extra, tok);
1661 			} else {
1662 				ac--;av++;
1663 			}
1664 #else
1665 			ac--;av++;
1666 #endif
1667 			break;
1668 		    }
1669 
1670 		case TOK_WEIGHT:
1671 			NEED(fs, "weight is only for flowsets");
1672 			NEED1("weight needs argument\n");
1673 			fs->par[0] = strtol(av[0], &end, 0);
1674 			ac--; av++;
1675 			break;
1676 
1677 		case TOK_LMAX:
1678 			NEED(fs, "lmax is only for flowsets");
1679 			NEED1("lmax needs argument\n");
1680 			fs->par[1] = strtol(av[0], &end, 0);
1681 			ac--; av++;
1682 			break;
1683 
1684 		case TOK_PRI:
1685 			NEED(fs, "priority is only for flowsets");
1686 			NEED1("priority needs argument\n");
1687 			fs->par[2] = strtol(av[0], &end, 0);
1688 			ac--; av++;
1689 			break;
1690 
1691 		case TOK_SCHED:
1692 		case TOK_PIPE:
1693 			NEED(fs, "pipe/sched");
1694 			NEED1("pipe/link/sched needs number\n");
1695 			fs->sched_nr = strtoul(av[0], &end, 0);
1696 			ac--; av++;
1697 			break;
1698 
1699 		case TOK_PROFILE:
1700 			NEED((!pf), "profile already set");
1701 			NEED(p, "profile");
1702 		    {
1703 			NEED1("extra delay needs the file name\n");
1704 			pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1705 			load_extra_delays(av[0], pf, p); //XXX can't fail?
1706 			--ac; ++av;
1707 		    }
1708 			break;
1709 
1710 		case TOK_BURST:
1711 			NEED(p, "burst");
1712 			NEED1("burst needs argument\n");
1713 			errno = 0;
1714 			if (expand_number(av[0], &p->burst) < 0)
1715 				if (errno != ERANGE)
1716 					errx(EX_DATAERR,
1717 					    "burst: invalid argument");
1718 			if (errno || p->burst > (1ULL << 48) - 1)
1719 				errx(EX_DATAERR,
1720 				    "burst: out of range (0..2^48-1)");
1721 			ac--; av++;
1722 			break;
1723 
1724 		default:
1725 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1726 		}
1727 	}
1728 
1729 	/* check validity of parameters */
1730 	if (p) {
1731 		if (p->delay > 10000)
1732 			errx(EX_DATAERR, "delay must be < 10000");
1733 		if (p->bandwidth == -1)
1734 			p->bandwidth = 0;
1735 	}
1736 	if (fs) {
1737 		/* XXX accept a 0 scheduler to keep the default */
1738 	    if (fs->flags & DN_QSIZE_BYTES) {
1739 		size_t len;
1740 		long limit;
1741 
1742 		len = sizeof(limit);
1743 		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1744 			&limit, &len, NULL, 0) == -1)
1745 			limit = 1024*1024;
1746 		if (fs->qsize > limit)
1747 			errx(EX_DATAERR, "queue size must be < %ldB", limit);
1748 	    } else {
1749 		size_t len;
1750 		long limit;
1751 
1752 		len = sizeof(limit);
1753 		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1754 			&limit, &len, NULL, 0) == -1)
1755 			limit = 100;
1756 		if (fs->qsize > limit)
1757 			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1758 	    }
1759 
1760 #ifdef NEW_AQM
1761 		if ((fs->flags & DN_IS_ECN) && !((fs->flags & DN_IS_RED)||
1762 			(fs->flags & DN_IS_AQM)))
1763 			errx(EX_USAGE, "ECN can be used with red/gred/"
1764 				"codel/fq_codel only!");
1765 #else
1766 	    if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1767 		errx(EX_USAGE, "enable red/gred for ECN");
1768 
1769 #endif
1770 
1771 	    if (fs->flags & DN_IS_RED) {
1772 		size_t len;
1773 		int lookup_depth, avg_pkt_size;
1774 
1775 		if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
1776 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1777 			fs->min_th, fs->max_th);
1778 		else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1779 		    errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1780 			fs->min_th, fs->max_th);
1781 
1782 		if (fs->max_th == 0)
1783 		    errx(EX_DATAERR, "max_th must be > 0");
1784 
1785 		len = sizeof(int);
1786 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1787 			&lookup_depth, &len, NULL, 0) == -1)
1788 			lookup_depth = 256;
1789 		if (lookup_depth == 0)
1790 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1791 			" must be greater than zero");
1792 
1793 		len = sizeof(int);
1794 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1795 			&avg_pkt_size, &len, NULL, 0) == -1)
1796 			avg_pkt_size = 512;
1797 
1798 		if (avg_pkt_size == 0)
1799 			errx(EX_DATAERR,
1800 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
1801 			    " be greater than zero");
1802 
1803 #if 0 /* the following computation is now done in the kernel */
1804 		/*
1805 		 * Ticks needed for sending a medium-sized packet.
1806 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
1807 		 * do not have bandwidth information, because that is stored
1808 		 * in the parent pipe, and also we have multiple queues
1809 		 * competing for it. So we set s=0, which is not very
1810 		 * correct. But on the other hand, why do we want RED with
1811 		 * WF2Q+ ?
1812 		 */
1813 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
1814 			s = 0;
1815 		else
1816 			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1817 		/*
1818 		 * max idle time (in ticks) before avg queue size becomes 0.
1819 		 * NOTA:  (3/w_q) is approx the value x so that
1820 		 * (1-w_q)^x < 10^-3.
1821 		 */
1822 		w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1823 		idle = s * 3. / w_q;
1824 		fs->lookup_step = (int)idle / lookup_depth;
1825 		if (!fs->lookup_step)
1826 			fs->lookup_step = 1;
1827 		weight = 1 - w_q;
1828 		for (t = fs->lookup_step; t > 1; --t)
1829 			weight *= 1 - w_q;
1830 		fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1831 #endif /* code moved in the kernel */
1832 	    }
1833 	}
1834 
1835 	i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1836 
1837 	if (i)
1838 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1839 }
1840 
1841 void
1842 dummynet_flush(void)
1843 {
1844 	struct dn_id oid;
1845 	oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1846 	do_cmd(IP_DUMMYNET3, &oid, oid.len);
1847 }
1848 
1849 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1850  * Returns the number of ranges, and possibly stores them
1851  * in the array v of size len.
1852  */
1853 static int
1854 parse_range(int ac, char *av[], uint32_t *v, int len)
1855 {
1856 	int n = 0;
1857 	char *endptr, *s;
1858 	uint32_t base[2];
1859 
1860 	if (v == NULL || len < 2) {
1861 		v = base;
1862 		len = 2;
1863 	}
1864 
1865 	for (s = *av; s != NULL; av++, ac--) {
1866 		v[0] = strtoul(s, &endptr, 10);
1867 		v[1] = (*endptr != '-') ? v[0] :
1868 			 strtoul(endptr+1, &endptr, 10);
1869 		if (*endptr == '\0') { /* prepare for next round */
1870 			s = (ac > 0) ? *(av+1) : NULL;
1871 		} else {
1872 			if (*endptr != ',') {
1873 				warn("invalid number: %s", s);
1874 				s = ++endptr;
1875 				continue;
1876 			}
1877 			/* continue processing from here */
1878 			s = ++endptr;
1879 			ac++;
1880 			av--;
1881 		}
1882 		if (v[1] < v[0] ||
1883 			v[1] >= DN_MAX_ID-1 ||
1884 			v[1] >= DN_MAX_ID-1) {
1885 			continue; /* invalid entry */
1886 		}
1887 		n++;
1888 		/* translate if 'pipe list' */
1889 		if (co.do_pipe == 1) {
1890 			v[0] += DN_MAX_ID;
1891 			v[1] += DN_MAX_ID;
1892 		}
1893 		v = (n*2 < len) ? v + 2 : base;
1894 	}
1895 	return n;
1896 }
1897 
1898 /* main entry point for dummynet list functions. co.do_pipe indicates
1899  * which function we want to support.
1900  * av may contain filtering arguments, either individual entries
1901  * or ranges, or lists (space or commas are valid separators).
1902  * Format for a range can be n1-n2 or n3 n4 n5 ...
1903  * In a range n1 must be <= n2, otherwise the range is ignored.
1904  * A number 'n4' is translate in a range 'n4-n4'
1905  * All number must be > 0 and < DN_MAX_ID-1
1906  */
1907 void
1908 dummynet_list(int ac, char *av[], int show_counters)
1909 {
1910 	struct dn_id *oid, *x = NULL;
1911 	int ret, i;
1912 	int n; 		/* # of ranges */
1913 	u_int buflen, l;
1914 	u_int max_size;	/* largest obj passed up */
1915 
1916 	(void)show_counters;	// XXX unused, but we should use it.
1917 	ac--;
1918 	av++; 		/* skip 'list' | 'show' word */
1919 
1920 	n = parse_range(ac, av, NULL, 0);	/* Count # of ranges. */
1921 
1922 	/* Allocate space to store ranges */
1923 	l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1924 	oid = safe_calloc(1, l);
1925 	oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1926 
1927 	if (n > 0)	/* store ranges in idx */
1928 		parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1929 	/*
1930 	 * Compute the size of the largest object returned. If the
1931 	 * response leaves at least this much spare space in the
1932 	 * buffer, then surely the response is complete; otherwise
1933 	 * there might be a risk of truncation and we will need to
1934 	 * retry with a larger buffer.
1935 	 * XXX don't bother with smaller structs.
1936 	 */
1937 	max_size = sizeof(struct dn_fs);
1938 	if (max_size < sizeof(struct dn_sch))
1939 		max_size = sizeof(struct dn_sch);
1940 	if (max_size < sizeof(struct dn_flow))
1941 		max_size = sizeof(struct dn_flow);
1942 
1943 	switch (co.do_pipe) {
1944 	case 1:
1945 		oid->subtype = DN_LINK;	/* list pipe */
1946 		break;
1947 	case 2:
1948 		oid->subtype = DN_FS;	/* list queue */
1949 		break;
1950 	case 3:
1951 		oid->subtype = DN_SCH;	/* list sched */
1952 		break;
1953 	}
1954 
1955 	/*
1956 	 * Ask the kernel an estimate of the required space (result
1957 	 * in oid.id), unless we are requesting a subset of objects,
1958 	 * in which case the kernel does not give an exact answer.
1959 	 * In any case, space might grow in the meantime due to the
1960 	 * creation of new queues, so we must be prepared to retry.
1961 	 */
1962 	if (n > 0) {
1963 		buflen = 4*1024;
1964 	} else {
1965 		ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1966 		if (ret != 0 || oid->id <= sizeof(*oid))
1967 			goto done;
1968 		buflen = oid->id + max_size;
1969 		oid->len = sizeof(*oid); /* restore */
1970 	}
1971 	/* Try a few times, until the buffer fits */
1972 	for (i = 0; i < 20; i++) {
1973 		l = buflen;
1974 		x = safe_realloc(x, l);
1975 		bcopy(oid, x, oid->len);
1976 		ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1977 		if (ret != 0 || x->id <= sizeof(*oid))
1978 			goto done; /* no response */
1979 		if (l + max_size <= buflen)
1980 			break; /* ok */
1981 		buflen *= 2;	 /* double for next attempt */
1982 	}
1983 	list_pipes(x, O_NEXT(x, l));
1984 done:
1985 	if (x)
1986 		free(x);
1987 	free(oid);
1988 }
1989