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