dummynet.c revision 187983
1/*
2 * Copyright (c) 2002-2003 Luigi Rizzo
3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4 * Copyright (c) 1994 Ugen J.S.Antsilevich
5 *
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
8 *
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
11 *
12 * Redistribution in binary form may occur without any restrictions.
13 * Obviously, it would be nice if you gave credit where credit is due
14 * but requiring it would be too onerous.
15 *
16 * This software is provided ``AS IS'' without any warranties of any kind.
17 *
18 * NEW command line interface for IP firewall facility
19 *
20 * $FreeBSD: head/sbin/ipfw/dummynet.c 187983 2009-02-01 16:00:49Z luigi $
21 *
22 * dummynet support
23 */
24
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <sys/queue.h>
28/* XXX there are several sysctl leftover here */
29#include <sys/sysctl.h>
30
31#include "ipfw2.h"
32
33#include <ctype.h>
34#include <err.h>
35#include <netdb.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sysexits.h>
40
41#include <net/if.h>
42#include <net/if.h>
43#include <netinet/in.h>
44#include <netinet/ip_fw.h>
45#include <netinet/ip_dummynet.h>
46#include <arpa/inet.h>	/* inet_ntoa */
47
48static struct _s_x dummynet_params[] = {
49	{ "plr",		TOK_PLR },
50	{ "noerror",		TOK_NOERROR },
51	{ "buckets",		TOK_BUCKETS },
52	{ "dst-ip",		TOK_DSTIP },
53	{ "src-ip",		TOK_SRCIP },
54	{ "dst-port",		TOK_DSTPORT },
55	{ "src-port",		TOK_SRCPORT },
56	{ "proto",		TOK_PROTO },
57	{ "weight",		TOK_WEIGHT },
58	{ "all",		TOK_ALL },
59	{ "mask",		TOK_MASK },
60	{ "droptail",		TOK_DROPTAIL },
61	{ "red",		TOK_RED },
62	{ "gred",		TOK_GRED },
63	{ "bw",			TOK_BW },
64	{ "bandwidth",		TOK_BW },
65	{ "delay",		TOK_DELAY },
66	{ "pipe",		TOK_PIPE },
67	{ "queue",		TOK_QUEUE },
68	{ "flow-id",		TOK_FLOWID},
69	{ "dst-ipv6",		TOK_DSTIP6},
70	{ "dst-ip6",		TOK_DSTIP6},
71	{ "src-ipv6",		TOK_SRCIP6},
72	{ "src-ip6",		TOK_SRCIP6},
73	{ "dummynet-params",	TOK_NULL },
74	{ NULL, 0 }	/* terminator */
75};
76
77static int
78sort_q(const void *pa, const void *pb)
79{
80	int rev = (co.do_sort < 0);
81	int field = rev ? -co.do_sort : co.do_sort;
82	long long res = 0;
83	const struct dn_flow_queue *a = pa;
84	const struct dn_flow_queue *b = pb;
85
86	switch (field) {
87	case 1: /* pkts */
88		res = a->len - b->len;
89		break;
90	case 2: /* bytes */
91		res = a->len_bytes - b->len_bytes;
92		break;
93
94	case 3: /* tot pkts */
95		res = a->tot_pkts - b->tot_pkts;
96		break;
97
98	case 4: /* tot bytes */
99		res = a->tot_bytes - b->tot_bytes;
100		break;
101	}
102	if (res < 0)
103		res = -1;
104	if (res > 0)
105		res = 1;
106	return (int)(rev ? res : -res);
107}
108
109static void
110list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
111{
112	int l;
113	int index_printed, indexes = 0;
114	char buff[255];
115	struct protoent *pe;
116
117	if (fs->rq_elements == 0)
118		return;
119
120	if (co.do_sort != 0)
121		heapsort(q, fs->rq_elements, sizeof *q, sort_q);
122
123	/* Print IPv4 flows */
124	index_printed = 0;
125	for (l = 0; l < fs->rq_elements; l++) {
126		struct in_addr ina;
127
128		/* XXX: Should check for IPv4 flows */
129		if (IS_IP6_FLOW_ID(&(q[l].id)))
130			continue;
131
132		if (!index_printed) {
133			index_printed = 1;
134			if (indexes > 0)	/* currently a no-op */
135				printf("\n");
136			indexes++;
137			printf("    "
138			    "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
139			    fs->flow_mask.proto,
140			    fs->flow_mask.src_ip, fs->flow_mask.src_port,
141			    fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
142
143			printf("BKT Prot ___Source IP/port____ "
144			    "____Dest. IP/port____ "
145			    "Tot_pkt/bytes Pkt/Byte Drp\n");
146		}
147
148		printf("%3d ", q[l].hash_slot);
149		pe = getprotobynumber(q[l].id.proto);
150		if (pe)
151			printf("%-4s ", pe->p_name);
152		else
153			printf("%4u ", q[l].id.proto);
154		ina.s_addr = htonl(q[l].id.src_ip);
155		printf("%15s/%-5d ",
156		    inet_ntoa(ina), q[l].id.src_port);
157		ina.s_addr = htonl(q[l].id.dst_ip);
158		printf("%15s/%-5d ",
159		    inet_ntoa(ina), q[l].id.dst_port);
160		printf("%4llu %8llu %2u %4u %3u\n",
161		    align_uint64(&q[l].tot_pkts),
162		    align_uint64(&q[l].tot_bytes),
163		    q[l].len, q[l].len_bytes, q[l].drops);
164		if (co.verbose)
165			printf("   S %20llu  F %20llu\n",
166			    align_uint64(&q[l].S), align_uint64(&q[l].F));
167	}
168
169	/* Print IPv6 flows */
170	index_printed = 0;
171	for (l = 0; l < fs->rq_elements; l++) {
172		if (!IS_IP6_FLOW_ID(&(q[l].id)))
173			continue;
174
175		if (!index_printed) {
176			index_printed = 1;
177			if (indexes > 0)
178				printf("\n");
179			indexes++;
180			printf("\n        mask: proto: 0x%02x, flow_id: 0x%08x,  ",
181			    fs->flow_mask.proto, fs->flow_mask.flow_id6);
182			inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
183			    buff, sizeof(buff));
184			printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
185			inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
186			    buff, sizeof(buff) );
187			printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
188
189			printf("BKT ___Prot___ _flow-id_ "
190			    "______________Source IPv6/port_______________ "
191			    "_______________Dest. IPv6/port_______________ "
192			    "Tot_pkt/bytes Pkt/Byte Drp\n");
193		}
194		printf("%3d ", q[l].hash_slot);
195		pe = getprotobynumber(q[l].id.proto);
196		if (pe != NULL)
197			printf("%9s ", pe->p_name);
198		else
199			printf("%9u ", q[l].id.proto);
200		printf("%7d  %39s/%-5d ", q[l].id.flow_id6,
201		    inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
202		    q[l].id.src_port);
203		printf(" %39s/%-5d ",
204		    inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
205		    q[l].id.dst_port);
206		printf(" %4llu %8llu %2u %4u %3u\n",
207		    align_uint64(&q[l].tot_pkts),
208		    align_uint64(&q[l].tot_bytes),
209		    q[l].len, q[l].len_bytes, q[l].drops);
210		if (co.verbose)
211			printf("   S %20llu  F %20llu\n",
212			    align_uint64(&q[l].S),
213			    align_uint64(&q[l].F));
214	}
215}
216
217static void
218print_flowset_parms(struct dn_flow_set *fs, char *prefix)
219{
220	int l;
221	char qs[30];
222	char plr[30];
223	char red[90];	/* Display RED parameters */
224
225	l = fs->qsize;
226	if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
227		if (l >= 8192)
228			sprintf(qs, "%d KB", l / 1024);
229		else
230			sprintf(qs, "%d B", l);
231	} else
232		sprintf(qs, "%3d sl.", l);
233	if (fs->plr)
234		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
235	else
236		plr[0] = '\0';
237	if (fs->flags_fs & DN_IS_RED)	/* RED parameters */
238		sprintf(red,
239		    "\n\t  %cRED w_q %f min_th %d max_th %d max_p %f",
240		    (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
241		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
242		    SCALE_VAL(fs->min_th),
243		    SCALE_VAL(fs->max_th),
244		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
245	else
246		sprintf(red, "droptail");
247
248	printf("%s %s%s %d queues (%d buckets) %s\n",
249	    prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
250}
251
252void
253ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
254{
255	int rulenum;
256	void *next = data;
257	struct dn_pipe *p = (struct dn_pipe *) data;
258	struct dn_flow_set *fs;
259	struct dn_flow_queue *q;
260	int l;
261
262	if (ac > 0)
263		rulenum = strtoul(*av++, NULL, 10);
264	else
265		rulenum = 0;
266	for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
267		double b = p->bandwidth;
268		char buf[30];
269		char prefix[80];
270
271		if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
272			break;	/* done with pipes, now queues */
273
274		/*
275		 * compute length, as pipe have variable size
276		 */
277		l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
278		next = (char *)p + l;
279		nbytes -= l;
280
281		if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
282			continue;
283
284		/*
285		 * Print rate (or clocking interface)
286		 */
287		if (p->if_name[0] != '\0')
288			sprintf(buf, "%s", p->if_name);
289		else if (b == 0)
290			sprintf(buf, "unlimited");
291		else if (b >= 1000000)
292			sprintf(buf, "%7.3f Mbit/s", b/1000000);
293		else if (b >= 1000)
294			sprintf(buf, "%7.3f Kbit/s", b/1000);
295		else
296			sprintf(buf, "%7.3f bit/s ", b);
297
298		sprintf(prefix, "%05d: %s %4d ms ",
299		    p->pipe_nr, buf, p->delay);
300		print_flowset_parms(&(p->fs), prefix);
301		if (co.verbose)
302			printf("   V %20llu\n", align_uint64(&p->V) >> MY_M);
303
304		q = (struct dn_flow_queue *)(p+1);
305		list_queues(&(p->fs), q);
306	}
307	for (fs = next; nbytes >= sizeof *fs; fs = next) {
308		char prefix[80];
309
310		if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
311			break;
312		l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
313		next = (char *)fs + l;
314		nbytes -= l;
315
316		if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
317		    (rulenum != fs->parent_nr && co.do_pipe == 1))) {
318			continue;
319		}
320
321		q = (struct dn_flow_queue *)(fs+1);
322		sprintf(prefix, "q%05d: weight %d pipe %d ",
323		    fs->fs_nr, fs->weight, fs->parent_nr);
324		print_flowset_parms(fs, prefix);
325		list_queues(fs, q);
326	}
327}
328
329/*
330 * Delete pipe or queue i
331 */
332int
333ipfw_delete_pipe(int pipe_or_queue, int i)
334{
335	struct dn_pipe p;
336
337	memset(&p, 0, sizeof p);
338	if (pipe_or_queue == 1)
339		p.pipe_nr = i;		/* pipe */
340	else
341		p.fs.fs_nr = i;		/* queue */
342	i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
343	if (i) {
344		i = 1;
345		warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
346	}
347	return i;
348}
349
350void
351ipfw_config_pipe(int ac, char **av)
352{
353	struct dn_pipe p;
354	int i;
355	char *end;
356	void *par = NULL;
357
358	memset(&p, 0, sizeof p);
359
360	av++; ac--;
361	/* Pipe number */
362	if (ac && isdigit(**av)) {
363		i = atoi(*av); av++; ac--;
364		if (co.do_pipe == 1)
365			p.pipe_nr = i;
366		else
367			p.fs.fs_nr = i;
368	}
369	while (ac > 0) {
370		double d;
371		int tok = match_token(dummynet_params, *av);
372		ac--; av++;
373
374		switch(tok) {
375		case TOK_NOERROR:
376			p.fs.flags_fs |= DN_NOERROR;
377			break;
378
379		case TOK_PLR:
380			NEED1("plr needs argument 0..1\n");
381			d = strtod(av[0], NULL);
382			if (d > 1)
383				d = 1;
384			else if (d < 0)
385				d = 0;
386			p.fs.plr = (int)(d*0x7fffffff);
387			ac--; av++;
388			break;
389
390		case TOK_QUEUE:
391			NEED1("queue needs queue size\n");
392			end = NULL;
393			p.fs.qsize = strtoul(av[0], &end, 0);
394			if (*end == 'K' || *end == 'k') {
395				p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
396				p.fs.qsize *= 1024;
397			} else if (*end == 'B' ||
398			    _substrcmp2(end, "by", "bytes") == 0) {
399				p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
400			}
401			ac--; av++;
402			break;
403
404		case TOK_BUCKETS:
405			NEED1("buckets needs argument\n");
406			p.fs.rq_size = strtoul(av[0], NULL, 0);
407			ac--; av++;
408			break;
409
410		case TOK_MASK:
411			NEED1("mask needs mask specifier\n");
412			/*
413			 * per-flow queue, mask is dst_ip, dst_port,
414			 * src_ip, src_port, proto measured in bits
415			 */
416			par = NULL;
417
418			bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
419			end = NULL;
420
421			while (ac >= 1) {
422			    uint32_t *p32 = NULL;
423			    uint16_t *p16 = NULL;
424			    uint32_t *p20 = NULL;
425			    struct in6_addr *pa6 = NULL;
426			    uint32_t a;
427
428			    tok = match_token(dummynet_params, *av);
429			    ac--; av++;
430			    switch(tok) {
431			    case TOK_ALL:
432				    /*
433				     * special case, all bits significant
434				     */
435				    p.fs.flow_mask.dst_ip = ~0;
436				    p.fs.flow_mask.src_ip = ~0;
437				    p.fs.flow_mask.dst_port = ~0;
438				    p.fs.flow_mask.src_port = ~0;
439				    p.fs.flow_mask.proto = ~0;
440				    n2mask(&(p.fs.flow_mask.dst_ip6), 128);
441				    n2mask(&(p.fs.flow_mask.src_ip6), 128);
442				    p.fs.flow_mask.flow_id6 = ~0;
443				    p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
444				    goto end_mask;
445
446			    case TOK_DSTIP:
447				    p32 = &p.fs.flow_mask.dst_ip;
448				    break;
449
450			    case TOK_SRCIP:
451				    p32 = &p.fs.flow_mask.src_ip;
452				    break;
453
454			    case TOK_DSTIP6:
455				    pa6 = &(p.fs.flow_mask.dst_ip6);
456				    break;
457
458			    case TOK_SRCIP6:
459				    pa6 = &(p.fs.flow_mask.src_ip6);
460				    break;
461
462			    case TOK_FLOWID:
463				    p20 = &p.fs.flow_mask.flow_id6;
464				    break;
465
466			    case TOK_DSTPORT:
467				    p16 = &p.fs.flow_mask.dst_port;
468				    break;
469
470			    case TOK_SRCPORT:
471				    p16 = &p.fs.flow_mask.src_port;
472				    break;
473
474			    case TOK_PROTO:
475				    break;
476
477			    default:
478				    ac++; av--; /* backtrack */
479				    goto end_mask;
480			    }
481			    if (ac < 1)
482				    errx(EX_USAGE, "mask: value missing");
483			    if (*av[0] == '/') {
484				    a = strtoul(av[0]+1, &end, 0);
485				    if (pa6 == NULL)
486					    a = (a == 32) ? ~0 : (1 << a) - 1;
487			    } else
488				    a = strtoul(av[0], &end, 0);
489			    if (p32 != NULL)
490				    *p32 = a;
491			    else if (p16 != NULL) {
492				    if (a > 0xFFFF)
493					    errx(EX_DATAERR,
494						"port mask must be 16 bit");
495				    *p16 = (uint16_t)a;
496			    } else if (p20 != NULL) {
497				    if (a > 0xfffff)
498					errx(EX_DATAERR,
499					    "flow_id mask must be 20 bit");
500				    *p20 = (uint32_t)a;
501			    } else if (pa6 != NULL) {
502				    if (a > 128)
503					errx(EX_DATAERR,
504					    "in6addr invalid mask len");
505				    else
506					n2mask(pa6, a);
507			    } else {
508				    if (a > 0xFF)
509					    errx(EX_DATAERR,
510						"proto mask must be 8 bit");
511				    p.fs.flow_mask.proto = (uint8_t)a;
512			    }
513			    if (a != 0)
514				    p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
515			    ac--; av++;
516			} /* end while, config masks */
517end_mask:
518			break;
519
520		case TOK_RED:
521		case TOK_GRED:
522			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
523			p.fs.flags_fs |= DN_IS_RED;
524			if (tok == TOK_GRED)
525				p.fs.flags_fs |= DN_IS_GENTLE_RED;
526			/*
527			 * the format for parameters is w_q/min_th/max_th/max_p
528			 */
529			if ((end = strsep(&av[0], "/"))) {
530			    double w_q = strtod(end, NULL);
531			    if (w_q > 1 || w_q <= 0)
532				errx(EX_DATAERR, "0 < w_q <= 1");
533			    p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
534			}
535			if ((end = strsep(&av[0], "/"))) {
536			    p.fs.min_th = strtoul(end, &end, 0);
537			    if (*end == 'K' || *end == 'k')
538				p.fs.min_th *= 1024;
539			}
540			if ((end = strsep(&av[0], "/"))) {
541			    p.fs.max_th = strtoul(end, &end, 0);
542			    if (*end == 'K' || *end == 'k')
543				p.fs.max_th *= 1024;
544			}
545			if ((end = strsep(&av[0], "/"))) {
546			    double max_p = strtod(end, NULL);
547			    if (max_p > 1 || max_p <= 0)
548				errx(EX_DATAERR, "0 < max_p <= 1");
549			    p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
550			}
551			ac--; av++;
552			break;
553
554		case TOK_DROPTAIL:
555			p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
556			break;
557
558		case TOK_BW:
559			NEED1("bw needs bandwidth or interface\n");
560			if (co.do_pipe != 1)
561			    errx(EX_DATAERR, "bandwidth only valid for pipes");
562			/*
563			 * set clocking interface or bandwidth value
564			 */
565			if (av[0][0] >= 'a' && av[0][0] <= 'z') {
566			    int l = sizeof(p.if_name)-1;
567			    /* interface name */
568			    strncpy(p.if_name, av[0], l);
569			    p.if_name[l] = '\0';
570			    p.bandwidth = 0;
571			} else {
572			    p.if_name[0] = '\0';
573			    p.bandwidth = strtoul(av[0], &end, 0);
574			    if (*end == 'K' || *end == 'k') {
575				end++;
576				p.bandwidth *= 1000;
577			    } else if (*end == 'M') {
578				end++;
579				p.bandwidth *= 1000000;
580			    }
581			    if ((*end == 'B' &&
582				  _substrcmp2(end, "Bi", "Bit/s") != 0) ||
583			        _substrcmp2(end, "by", "bytes") == 0)
584				p.bandwidth *= 8;
585			    if (p.bandwidth < 0)
586				errx(EX_DATAERR, "bandwidth too large");
587			}
588			ac--; av++;
589			break;
590
591		case TOK_DELAY:
592			if (co.do_pipe != 1)
593				errx(EX_DATAERR, "delay only valid for pipes");
594			NEED1("delay needs argument 0..10000ms\n");
595			p.delay = strtoul(av[0], NULL, 0);
596			ac--; av++;
597			break;
598
599		case TOK_WEIGHT:
600			if (co.do_pipe == 1)
601				errx(EX_DATAERR,"weight only valid for queues");
602			NEED1("weight needs argument 0..100\n");
603			p.fs.weight = strtoul(av[0], &end, 0);
604			ac--; av++;
605			break;
606
607		case TOK_PIPE:
608			if (co.do_pipe == 1)
609				errx(EX_DATAERR,"pipe only valid for queues");
610			NEED1("pipe needs pipe_number\n");
611			p.fs.parent_nr = strtoul(av[0], &end, 0);
612			ac--; av++;
613			break;
614
615		default:
616			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
617		}
618	}
619	if (co.do_pipe == 1) {
620		if (p.pipe_nr == 0)
621			errx(EX_DATAERR, "pipe_nr must be > 0");
622		if (p.delay > 10000)
623			errx(EX_DATAERR, "delay must be < 10000");
624	} else { /* co.do_pipe == 2, queue */
625		if (p.fs.parent_nr == 0)
626			errx(EX_DATAERR, "pipe must be > 0");
627		if (p.fs.weight >100)
628			errx(EX_DATAERR, "weight must be <= 100");
629	}
630	if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
631		size_t len;
632		long limit;
633
634		len = sizeof(limit);
635		if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
636			&limit, &len, NULL, 0) == -1)
637			limit = 1024*1024;
638		if (p.fs.qsize > limit)
639			errx(EX_DATAERR, "queue size must be < %ldB", limit);
640	} else {
641		size_t len;
642		long limit;
643
644		len = sizeof(limit);
645		if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
646			&limit, &len, NULL, 0) == -1)
647			limit = 100;
648		if (p.fs.qsize > limit)
649			errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
650	}
651	if (p.fs.flags_fs & DN_IS_RED) {
652		size_t len;
653		int lookup_depth, avg_pkt_size;
654		double s, idle, weight, w_q;
655		struct clockinfo ck;
656		int t;
657
658		if (p.fs.min_th >= p.fs.max_th)
659		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
660			p.fs.min_th, p.fs.max_th);
661		if (p.fs.max_th == 0)
662		    errx(EX_DATAERR, "max_th must be > 0");
663
664		len = sizeof(int);
665		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
666			&lookup_depth, &len, NULL, 0) == -1)
667		    errx(1, "sysctlbyname(\"%s\")",
668			"net.inet.ip.dummynet.red_lookup_depth");
669		if (lookup_depth == 0)
670		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
671			" must be greater than zero");
672
673		len = sizeof(int);
674		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
675			&avg_pkt_size, &len, NULL, 0) == -1)
676
677		    errx(1, "sysctlbyname(\"%s\")",
678			"net.inet.ip.dummynet.red_avg_pkt_size");
679		if (avg_pkt_size == 0)
680			errx(EX_DATAERR,
681			    "net.inet.ip.dummynet.red_avg_pkt_size must"
682			    " be greater than zero");
683
684		len = sizeof(struct clockinfo);
685		if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
686			errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
687
688		/*
689		 * Ticks needed for sending a medium-sized packet.
690		 * Unfortunately, when we are configuring a WF2Q+ queue, we
691		 * do not have bandwidth information, because that is stored
692		 * in the parent pipe, and also we have multiple queues
693		 * competing for it. So we set s=0, which is not very
694		 * correct. But on the other hand, why do we want RED with
695		 * WF2Q+ ?
696		 */
697		if (p.bandwidth==0) /* this is a WF2Q+ queue */
698			s = 0;
699		else
700			s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
701
702		/*
703		 * max idle time (in ticks) before avg queue size becomes 0.
704		 * NOTA:  (3/w_q) is approx the value x so that
705		 * (1-w_q)^x < 10^-3.
706		 */
707		w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
708		idle = s * 3. / w_q;
709		p.fs.lookup_step = (int)idle / lookup_depth;
710		if (!p.fs.lookup_step)
711			p.fs.lookup_step = 1;
712		weight = 1 - w_q;
713		for (t = p.fs.lookup_step; t > 1; --t)
714			weight *= 1 - w_q;
715		p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
716	}
717	i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
718	if (i)
719		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
720}
721