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
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
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.
16 * This software is provided ``AS IS'' without any warranties of any kind.
18 * NEW command line interface for IP firewall facility
20 * $FreeBSD: head/sbin/ipfw/dummynet.c 187769 2009-01-27 11:06:59Z luigi $
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>
42 #include <netinet/in.h>
43 #include <netinet/ip_fw.h>
44 #include <netinet/ip_dummynet.h>
45 #include <arpa/inet.h> /* inet_ntoa */
47 static struct _s_x dummynet_params[] = {
49 { "noerror", TOK_NOERROR },
50 { "buckets", TOK_BUCKETS },
51 { "dst-ip", TOK_DSTIP },
52 { "src-ip", TOK_SRCIP },
53 { "dst-port", TOK_DSTPORT },
54 { "src-port", TOK_SRCPORT },
55 { "proto", TOK_PROTO },
56 { "weight", TOK_WEIGHT },
59 { "droptail", TOK_DROPTAIL },
63 { "bandwidth", TOK_BW },
64 { "delay", TOK_DELAY },
66 { "queue", TOK_QUEUE },
67 { "flow-id", TOK_FLOWID},
68 { "dst-ipv6", TOK_DSTIP6},
69 { "dst-ip6", TOK_DSTIP6},
70 { "src-ipv6", TOK_SRCIP6},
71 { "src-ip6", TOK_SRCIP6},
72 { "profile", TOK_PIPE_PROFILE},
73 { "dummynet-params", TOK_NULL },
74 { NULL, 0 } /* terminator */
78 sort_q(const void *pa, const void *pb)
80 int rev = (co.do_sort < 0);
81 int field = rev ? -co.do_sort : co.do_sort;
83 const struct dn_flow_queue *a = pa;
84 const struct dn_flow_queue *b = pb;
88 res = a->len - b->len;
91 res = a->len_bytes - b->len_bytes;
94 case 3: /* tot pkts */
95 res = a->tot_pkts - b->tot_pkts;
98 case 4: /* tot bytes */
99 res = a->tot_bytes - b->tot_bytes;
106 return (int)(rev ? res : -res);
110 list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
113 int index_printed, indexes = 0;
117 if (fs->rq_elements == 0)
121 heapsort(q, fs->rq_elements, sizeof *q, sort_q);
123 /* Print IPv4 flows */
125 for (l = 0; l < fs->rq_elements; l++) {
128 /* XXX: Should check for IPv4 flows */
129 if (IS_IP6_FLOW_ID(&(q[l].id)))
132 if (!index_printed) {
134 if (indexes > 0) /* currently a no-op */
138 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
140 fs->flow_mask.src_ip, fs->flow_mask.src_port,
141 fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
143 printf("BKT Prot ___Source IP/port____ "
144 "____Dest. IP/port____ "
145 "Tot_pkt/bytes Pkt/Byte Drp\n");
148 printf("%3d ", q[l].hash_slot);
149 pe = getprotobynumber(q[l].id.proto);
151 printf("%-4s ", pe->p_name);
153 printf("%4u ", q[l].id.proto);
154 ina.s_addr = htonl(q[l].id.src_ip);
156 inet_ntoa(ina), q[l].id.src_port);
157 ina.s_addr = htonl(q[l].id.dst_ip);
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);
165 printf(" S %20llu F %20llu\n",
166 align_uint64(&q[l].S), align_uint64(&q[l].F));
169 /* Print IPv6 flows */
171 for (l = 0; l < fs->rq_elements; l++) {
172 if (!IS_IP6_FLOW_ID(&(q[l].id)))
175 if (!index_printed) {
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),
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);
189 printf("BKT ___Prot___ _flow-id_ "
190 "______________Source IPv6/port_______________ "
191 "_______________Dest. IPv6/port_______________ "
192 "Tot_pkt/bytes Pkt/Byte Drp\n");
194 printf("%3d ", q[l].hash_slot);
195 pe = getprotobynumber(q[l].id.proto);
197 printf("%9s ", pe->p_name);
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)),
203 printf(" %39s/%-5d ",
204 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
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);
211 printf(" S %20llu F %20llu\n",
212 align_uint64(&q[l].S),
213 align_uint64(&q[l].F));
218 print_flowset_parms(struct dn_flow_set *fs, char *prefix)
223 char red[90]; /* Display RED parameters */
226 if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
228 sprintf(qs, "%d KB", l / 1024);
230 sprintf(qs, "%d B", l);
232 sprintf(qs, "%3d sl.", l);
234 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
237 if (fs->flags_fs & DN_IS_RED) /* RED parameters */
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));
246 sprintf(red, "droptail");
248 printf("%s %s%s %d queues (%d buckets) %s\n",
249 prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
253 print_extra_delay_parms(struct dn_pipe *p, char *prefix)
256 if (p->samples_no <= 0)
259 loss = p->loss_level;
260 loss /= p->samples_no;
261 printf("%s profile: name \"%s\" loss %f samples %d\n",
262 prefix, p->name, loss, p->samples_no);
266 ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
270 struct dn_pipe *p = (struct dn_pipe *) data;
271 struct dn_flow_set *fs;
272 struct dn_flow_queue *q;
276 rulenum = strtoul(*av++, NULL, 10);
279 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
280 double b = p->bandwidth;
284 if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
285 break; /* done with pipes, now queues */
288 * compute length, as pipe have variable size
290 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
291 next = (char *)p + l;
294 if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
298 * Print rate (or clocking interface)
300 if (p->if_name[0] != '\0')
301 sprintf(buf, "%s", p->if_name);
303 sprintf(buf, "unlimited");
304 else if (b >= 1000000)
305 sprintf(buf, "%7.3f Mbit/s", b/1000000);
307 sprintf(buf, "%7.3f Kbit/s", b/1000);
309 sprintf(buf, "%7.3f bit/s ", b);
311 sprintf(prefix, "%05d: %s %4d ms ",
312 p->pipe_nr, buf, p->delay);
314 print_extra_delay_parms(p, prefix);
316 print_flowset_parms(&(p->fs), prefix);
318 q = (struct dn_flow_queue *)(p+1);
319 list_queues(&(p->fs), q);
321 for (fs = next; nbytes >= sizeof *fs; fs = next) {
324 if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
326 l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
327 next = (char *)fs + l;
330 if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
331 (rulenum != fs->parent_nr && co.do_pipe == 1))) {
335 q = (struct dn_flow_queue *)(fs+1);
336 sprintf(prefix, "q%05d: weight %d pipe %d ",
337 fs->fs_nr, fs->weight, fs->parent_nr);
338 print_flowset_parms(fs, prefix);
344 * Delete pipe or queue i
347 ipfw_delete_pipe(int pipe_or_queue, int i)
351 memset(&p, 0, sizeof p);
352 if (pipe_or_queue == 1)
353 p.pipe_nr = i; /* pipe */
355 p.fs.fs_nr = i; /* queue */
356 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
359 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
365 * Code to parse delay profiles.
367 * Some link types introduce extra delays in the transmission
368 * of a packet, e.g. because of MAC level framing, contention on
369 * the use of the channel, MAC level retransmissions and so on.
370 * From our point of view, the channel is effectively unavailable
371 * for this extra time, which is constant or variable depending
372 * on the link type. Additionally, packets may be dropped after this
373 * time (e.g. on a wireless link after too many retransmissions).
374 * We can model the additional delay with an empirical curve
375 * that represents its distribution.
377 * cumulative probability
387 * +-------*------------------->
390 * The empirical curve may have both vertical and horizontal lines.
391 * Vertical lines represent constant delay for a range of
392 * probabilities; horizontal lines correspond to a discontinuty
393 * in the delay distribution: the pipe will use the largest delay
394 * for a given probability.
396 * To pass the curve to dummynet, we must store the parameters
397 * in a file as described below, and issue the command
399 * ipfw pipe <n> config ... bw XXX profile <filename> ...
401 * The file format is the following, with whitespace acting as
402 * a separator and '#' indicating the beginning a comment:
405 * the number of samples used in the internal
406 * representation (2..1024; default 100);
409 * The probability above which packets are lost.
410 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
413 * Optional a name (listed by "ipfw pipe show")
414 * to identify the distribution;
416 * "delay prob" | "prob delay"
417 * One of these two lines is mandatory and defines
418 * the format of the following lines with data points.
421 * 2 or more lines representing points in the curve,
422 * with either delay or probability first, according
423 * to the chosen format.
424 * The unit for delay is milliseconds.
426 * Data points does not need to be ordered or equal to the number
427 * specified in the "samples" line. ipfw will sort and interpolate
428 * the curve as needed.
430 * Example of a profile file:
436 0 200 # minimum overhead is 200ms
443 * Internally, we will convert the curve to a fixed number of
444 * samples, and when it is time to transmit a packet we will
445 * model the extra delay as extra bits in the packet.
449 /* XXX move to an array definition ? */
450 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
451 #define ED_TOK_SAMPLES "samples"
452 #define ED_TOK_LOSS "loss-level"
453 #define ED_TOK_NAME "name"
454 #define ED_TOK_DELAY "delay"
455 #define ED_TOK_PROB "prob"
456 #define ED_TOK_BW "bw"
457 #define ED_SEPARATORS " \t\n"
458 #define ED_MIN_SAMPLES_NO 2
461 * returns 1 if s is a non-negative number, with at least one '.'
464 is_valid_number(const char *s)
466 int i, dots_found = 0;
469 for (i = 0; i<len; ++i)
470 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
476 * Take as input a string describing a bandwidth value
477 * and return the numeric bandwidth value.
478 * set clocking interface or bandwidth value
481 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
483 if (*bandwidth != -1)
484 warn("duplicate token, override bandwidth value!");
486 if (arg[0] >= 'a' && arg[0] <= 'z') {
487 if (namelen >= IFNAMSIZ)
488 warn("interface name truncated");
491 strncpy(if_name, arg, namelen);
492 if_name[namelen] = '\0';
494 } else { /* read bandwidth value */
498 bw = strtoul(arg, &end, 0);
499 if (*end == 'K' || *end == 'k') {
502 } else if (*end == 'M') {
507 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
508 _substrcmp2(end, "by", "bytes") == 0)
512 errx(EX_DATAERR, "bandwidth too large");
525 compare_points(const void *vp1, const void *vp2)
527 const struct point *p1 = vp1;
528 const struct point *p2 = vp2;
531 res = p1->prob - p2->prob;
533 res = p1->delay - p2->delay;
542 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
545 load_extra_delays(const char *filename, struct dn_pipe *p)
547 char line[ED_MAX_LINE_LEN];
554 char profile_name[ED_MAX_NAME_LEN];
555 int delay_first = -1;
557 struct point points[ED_MAX_SAMPLES_NO];
560 profile_name[0] = '\0';
561 f = fopen(filename, "r");
563 err(EX_UNAVAILABLE, "fopen: %s", filename);
565 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
566 char *s, *cur = line, *name = NULL, *arg = NULL;
572 s = strsep(&cur, ED_SEPARATORS);
573 if (s == NULL || *s == '#')
578 errx(ED_EFMT("too many arguments"));
584 if (name == NULL) /* empty line */
587 errx(ED_EFMT("missing arg for %s"), name);
589 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
591 errx(ED_EFMT("duplicate ``samples'' line"));
593 errx(ED_EFMT("invalid number of samples"));
595 if (samples>ED_MAX_SAMPLES_NO)
596 errx(ED_EFMT("too many samples, maximum is %d"),
599 } else if (!strcasecmp(name, ED_TOK_BW)) {
600 read_bandwidth(arg, &p->bandwidth, p->if_name, sizeof(p->if_name));
601 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
603 errx(ED_EFMT("duplicated token: %s"), name);
604 if (!is_valid_number(arg))
605 errx(ED_EFMT("invalid %s"), arg);
608 errx(ED_EFMT("%s greater than 1.0"), name);
610 } else if (!strcasecmp(name, ED_TOK_NAME)) {
611 if (profile_name[0] != '\0')
612 errx(ED_EFMT("duplicated token: %s"), name);
613 strncpy(profile_name, arg, sizeof(profile_name) - 1);
614 profile_name[sizeof(profile_name)-1] = '\0';
616 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
618 errx(ED_EFMT("duplicated token: %s"), name);
621 } else if (!strcasecmp(name, ED_TOK_PROB)) {
623 errx(ED_EFMT("duplicated token: %s"), name);
626 } else if (do_points) {
627 if (!is_valid_number(name) || !is_valid_number(arg))
628 errx(ED_EFMT("invalid point found"));
630 points[points_no].delay = atof(name);
631 points[points_no].prob = atof(arg);
633 points[points_no].delay = atof(arg);
634 points[points_no].prob = atof(name);
636 if (points[points_no].prob > 1.0)
637 errx(ED_EFMT("probability greater than 1.0"));
640 errx(ED_EFMT("unrecognised command '%s'"), name);
645 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
650 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
654 /* make sure that there are enough points. */
655 if (points_no < ED_MIN_SAMPLES_NO)
656 errx(ED_EFMT("too few samples, need at least %d"),
659 qsort(points, points_no, sizeof(struct point), compare_points);
662 for (i = 0; i<points_no-1; ++i) {
663 double y1 = points[i].prob * samples;
664 double x1 = points[i].delay;
665 double y2 = points[i+1].prob * samples;
666 double x2 = points[i+1].delay;
672 for (; index<stop; ++index)
673 p->samples[index] = x1;
675 double m = (y2-y1)/(x2-x1);
676 double c = y1 - m*x1;
677 for (; index<stop ; ++index)
678 p->samples[index] = (index - c)/m;
681 p->samples_no = samples;
682 p->loss_level = loss * samples;
683 strncpy(p->name, profile_name, sizeof(p->name));
687 ipfw_config_pipe(int ac, char **av)
689 int samples[ED_MAX_SAMPLES_NO];
695 memset(&p, 0, sizeof p);
700 if (ac && isdigit(**av)) {
701 i = atoi(*av); av++; ac--;
709 int tok = match_token(dummynet_params, *av);
714 p.fs.flags_fs |= DN_NOERROR;
718 NEED1("plr needs argument 0..1\n");
719 d = strtod(av[0], NULL);
724 p.fs.plr = (int)(d*0x7fffffff);
729 NEED1("queue needs queue size\n");
731 p.fs.qsize = strtoul(av[0], &end, 0);
732 if (*end == 'K' || *end == 'k') {
733 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
735 } else if (*end == 'B' ||
736 _substrcmp2(end, "by", "bytes") == 0) {
737 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
743 NEED1("buckets needs argument\n");
744 p.fs.rq_size = strtoul(av[0], NULL, 0);
749 NEED1("mask needs mask specifier\n");
751 * per-flow queue, mask is dst_ip, dst_port,
752 * src_ip, src_port, proto measured in bits
756 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
760 uint32_t *p32 = NULL;
761 uint16_t *p16 = NULL;
762 uint32_t *p20 = NULL;
763 struct in6_addr *pa6 = NULL;
766 tok = match_token(dummynet_params, *av);
771 * special case, all bits significant
773 p.fs.flow_mask.dst_ip = ~0;
774 p.fs.flow_mask.src_ip = ~0;
775 p.fs.flow_mask.dst_port = ~0;
776 p.fs.flow_mask.src_port = ~0;
777 p.fs.flow_mask.proto = ~0;
778 n2mask(&(p.fs.flow_mask.dst_ip6), 128);
779 n2mask(&(p.fs.flow_mask.src_ip6), 128);
780 p.fs.flow_mask.flow_id6 = ~0;
781 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
785 p32 = &p.fs.flow_mask.dst_ip;
789 p32 = &p.fs.flow_mask.src_ip;
793 pa6 = &(p.fs.flow_mask.dst_ip6);
797 pa6 = &(p.fs.flow_mask.src_ip6);
801 p20 = &p.fs.flow_mask.flow_id6;
805 p16 = &p.fs.flow_mask.dst_port;
809 p16 = &p.fs.flow_mask.src_port;
816 ac++; av--; /* backtrack */
820 errx(EX_USAGE, "mask: value missing");
822 a = strtoul(av[0]+1, &end, 0);
824 a = (a == 32) ? ~0 : (1 << a) - 1;
826 a = strtoul(av[0], &end, 0);
829 else if (p16 != NULL) {
832 "port mask must be 16 bit");
834 } else if (p20 != NULL) {
837 "flow_id mask must be 20 bit");
839 } else if (pa6 != NULL) {
842 "in6addr invalid mask len");
848 "proto mask must be 8 bit");
849 p.fs.flow_mask.proto = (uint8_t)a;
852 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
854 } /* end while, config masks */
860 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
861 p.fs.flags_fs |= DN_IS_RED;
863 p.fs.flags_fs |= DN_IS_GENTLE_RED;
865 * the format for parameters is w_q/min_th/max_th/max_p
867 if ((end = strsep(&av[0], "/"))) {
868 double w_q = strtod(end, NULL);
869 if (w_q > 1 || w_q <= 0)
870 errx(EX_DATAERR, "0 < w_q <= 1");
871 p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
873 if ((end = strsep(&av[0], "/"))) {
874 p.fs.min_th = strtoul(end, &end, 0);
875 if (*end == 'K' || *end == 'k')
878 if ((end = strsep(&av[0], "/"))) {
879 p.fs.max_th = strtoul(end, &end, 0);
880 if (*end == 'K' || *end == 'k')
883 if ((end = strsep(&av[0], "/"))) {
884 double max_p = strtod(end, NULL);
885 if (max_p > 1 || max_p <= 0)
886 errx(EX_DATAERR, "0 < max_p <= 1");
887 p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
893 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
897 NEED1("bw needs bandwidth or interface\n");
899 errx(EX_DATAERR, "bandwidth only valid for pipes");
900 read_bandwidth(av[0], &p.bandwidth, p.if_name, sizeof(p.if_name));
906 errx(EX_DATAERR, "delay only valid for pipes");
907 NEED1("delay needs argument 0..10000ms\n");
908 p.delay = strtoul(av[0], NULL, 0);
914 errx(EX_DATAERR,"weight only valid for queues");
915 NEED1("weight needs argument 0..100\n");
916 p.fs.weight = strtoul(av[0], &end, 0);
922 errx(EX_DATAERR,"pipe only valid for queues");
923 NEED1("pipe needs pipe_number\n");
924 p.fs.parent_nr = strtoul(av[0], &end, 0);
928 case TOK_PIPE_PROFILE:
930 errx(EX_DATAERR, "extra delay only valid for pipes");
931 NEED1("extra delay needs the file name\n");
932 p.samples = &samples[0];
933 load_extra_delays(av[0], &p);
938 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
941 if (co.do_pipe == 1) {
943 errx(EX_DATAERR, "pipe_nr must be > 0");
945 errx(EX_DATAERR, "delay must be < 10000");
946 } else { /* co.do_pipe == 2, queue */
947 if (p.fs.parent_nr == 0)
948 errx(EX_DATAERR, "pipe must be > 0");
949 if (p.fs.weight >100)
950 errx(EX_DATAERR, "weight must be <= 100");
953 /* check for bandwidth value */
954 if (p.bandwidth == -1) {
956 if (p.samples_no > 0)
957 errx(EX_DATAERR, "profile requires a bandwidth limit");
960 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
965 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
966 &limit, &len, NULL, 0) == -1)
968 if (p.fs.qsize > limit)
969 errx(EX_DATAERR, "queue size must be < %ldB", limit);
975 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
976 &limit, &len, NULL, 0) == -1)
978 if (p.fs.qsize > limit)
979 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
981 if (p.fs.flags_fs & DN_IS_RED) {
983 int lookup_depth, avg_pkt_size;
984 double s, idle, weight, w_q;
988 if (p.fs.min_th >= p.fs.max_th)
989 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
990 p.fs.min_th, p.fs.max_th);
991 if (p.fs.max_th == 0)
992 errx(EX_DATAERR, "max_th must be > 0");
995 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
996 &lookup_depth, &len, NULL, 0) == -1)
997 errx(1, "sysctlbyname(\"%s\")",
998 "net.inet.ip.dummynet.red_lookup_depth");
999 if (lookup_depth == 0)
1000 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1001 " must be greater than zero");
1004 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1005 &avg_pkt_size, &len, NULL, 0) == -1)
1007 errx(1, "sysctlbyname(\"%s\")",
1008 "net.inet.ip.dummynet.red_avg_pkt_size");
1009 if (avg_pkt_size == 0)
1011 "net.inet.ip.dummynet.red_avg_pkt_size must"
1012 " be greater than zero");
1014 len = sizeof(struct clockinfo);
1015 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
1016 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
1019 * Ticks needed for sending a medium-sized packet.
1020 * Unfortunately, when we are configuring a WF2Q+ queue, we
1021 * do not have bandwidth information, because that is stored
1022 * in the parent pipe, and also we have multiple queues
1023 * competing for it. So we set s=0, which is not very
1024 * correct. But on the other hand, why do we want RED with
1027 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1030 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1033 * max idle time (in ticks) before avg queue size becomes 0.
1034 * NOTA: (3/w_q) is approx the value x so that
1035 * (1-w_q)^x < 10^-3.
1037 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
1038 idle = s * 3. / w_q;
1039 p.fs.lookup_step = (int)idle / lookup_depth;
1040 if (!p.fs.lookup_step)
1041 p.fs.lookup_step = 1;
1043 for (t = p.fs.lookup_step; t > 1; --t)
1045 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
1047 if (p.samples_no <= 0) {
1048 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
1050 struct dn_pipe_max pm;
1051 int len = sizeof(pm);
1053 memcpy(&pm.pipe, &p, sizeof(pm.pipe));
1054 memcpy(&pm.samples, samples, sizeof(pm.samples));
1056 i = do_cmd(IP_DUMMYNET_CONFIGURE, &pm, len);
1060 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");