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>
43 #include <netinet/in.h>
44 #include <netinet/ip_fw.h>
45 #include <netinet/ip_dummynet.h>
46 #include <arpa/inet.h> /* inet_ntoa */
48 static struct _s_x dummynet_params[] = {
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 },
60 { "droptail", TOK_DROPTAIL },
64 { "bandwidth", TOK_BW },
65 { "delay", TOK_DELAY },
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 { "profile", TOK_PIPE_PROFILE},
74 { "burst", TOK_BURST},
75 { "dummynet-params", TOK_NULL },
76 { NULL, 0 } /* terminator */
80 sort_q(void *arg, const void *pa, const void *pb)
82 int rev = (co.do_sort < 0);
83 int field = rev ? -co.do_sort : co.do_sort;
85 const struct dn_flow_queue *a = pa;
86 const struct dn_flow_queue *b = pb;
90 res = a->len - b->len;
93 res = a->len_bytes - b->len_bytes;
96 case 3: /* tot pkts */
97 res = a->tot_pkts - b->tot_pkts;
100 case 4: /* tot bytes */
101 res = a->tot_bytes - b->tot_bytes;
108 return (int)(rev ? res : -res);
112 list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
115 int index_printed, indexes = 0;
119 if (fs->rq_elements == 0)
123 qsort_r(q, fs->rq_elements, sizeof *q, NULL, sort_q);
125 /* Print IPv4 flows */
127 for (l = 0; l < fs->rq_elements; l++) {
130 /* XXX: Should check for IPv4 flows */
131 if (IS_IP6_FLOW_ID(&(q[l].id)))
134 if (!index_printed) {
136 if (indexes > 0) /* currently a no-op */
140 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
142 fs->flow_mask.src_ip, fs->flow_mask.src_port,
143 fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
145 printf("BKT Prot ___Source IP/port____ "
146 "____Dest. IP/port____ "
147 "Tot_pkt/bytes Pkt/Byte Drp\n");
150 printf("%3d ", q[l].hash_slot);
151 pe = getprotobynumber(q[l].id.proto);
153 printf("%-4s ", pe->p_name);
155 printf("%4u ", q[l].id.proto);
156 ina.s_addr = htonl(q[l].id.src_ip);
158 inet_ntoa(ina), q[l].id.src_port);
159 ina.s_addr = htonl(q[l].id.dst_ip);
161 inet_ntoa(ina), q[l].id.dst_port);
162 printf("%4llu %8llu %2u %4u %3u\n",
163 align_uint64(&q[l].tot_pkts),
164 align_uint64(&q[l].tot_bytes),
165 q[l].len, q[l].len_bytes, q[l].drops);
167 printf(" S %20llu F %20llu\n",
168 align_uint64(&q[l].S), align_uint64(&q[l].F));
171 /* Print IPv6 flows */
173 for (l = 0; l < fs->rq_elements; l++) {
174 if (!IS_IP6_FLOW_ID(&(q[l].id)))
177 if (!index_printed) {
182 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ",
183 fs->flow_mask.proto, fs->flow_mask.flow_id6);
184 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
186 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
187 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
188 buff, sizeof(buff) );
189 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
191 printf("BKT ___Prot___ _flow-id_ "
192 "______________Source IPv6/port_______________ "
193 "_______________Dest. IPv6/port_______________ "
194 "Tot_pkt/bytes Pkt/Byte Drp\n");
196 printf("%3d ", q[l].hash_slot);
197 pe = getprotobynumber(q[l].id.proto);
199 printf("%9s ", pe->p_name);
201 printf("%9u ", q[l].id.proto);
202 printf("%7d %39s/%-5d ", q[l].id.flow_id6,
203 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
205 printf(" %39s/%-5d ",
206 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
208 printf(" %4llu %8llu %2u %4u %3u\n",
209 align_uint64(&q[l].tot_pkts),
210 align_uint64(&q[l].tot_bytes),
211 q[l].len, q[l].len_bytes, q[l].drops);
213 printf(" S %20llu F %20llu\n",
214 align_uint64(&q[l].S),
215 align_uint64(&q[l].F));
220 print_flowset_parms(struct dn_flow_set *fs, char *prefix)
225 char red[90]; /* Display RED parameters */
228 if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
230 sprintf(qs, "%d KB", l / 1024);
232 sprintf(qs, "%d B", l);
234 sprintf(qs, "%3d sl.", l);
236 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
239 if (fs->flags_fs & DN_IS_RED) /* RED parameters */
241 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
242 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
243 1.0 * fs->w_q / (double)(1 << SCALE_RED),
244 SCALE_VAL(fs->min_th),
245 SCALE_VAL(fs->max_th),
246 1.0 * fs->max_p / (double)(1 << SCALE_RED));
248 sprintf(red, "droptail");
250 printf("%s %s%s %d queues (%d buckets) %s\n",
251 prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
255 print_extra_delay_parms(struct dn_pipe *p)
258 if (p->samples_no <= 0)
261 loss = p->loss_level;
262 loss /= p->samples_no;
263 printf("\t profile: name \"%s\" loss %f samples %d\n",
264 p->name, loss, p->samples_no);
268 ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
272 struct dn_pipe *p = (struct dn_pipe *) data;
273 struct dn_flow_set *fs;
274 struct dn_flow_queue *q;
278 rulenum = strtoul(*av++, NULL, 10);
281 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
282 double b = p->bandwidth;
287 if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
288 break; /* done with pipes, now queues */
291 * compute length, as pipe have variable size
293 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
294 next = (char *)p + l;
297 if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
301 * Print rate (or clocking interface)
303 if (p->if_name[0] != '\0')
304 sprintf(buf, "%s", p->if_name);
306 sprintf(buf, "unlimited");
307 else if (b >= 1000000)
308 sprintf(buf, "%7.3f Mbit/s", b/1000000);
310 sprintf(buf, "%7.3f Kbit/s", b/1000);
312 sprintf(buf, "%7.3f bit/s ", b);
314 sprintf(prefix, "%05d: %s %4d ms ",
315 p->pipe_nr, buf, p->delay);
317 print_flowset_parms(&(p->fs), prefix);
319 if (humanize_number(burst, sizeof(burst), p->burst,
320 "Byte", HN_AUTOSCALE, 0) < 0 || co.verbose)
321 printf("\t burst: %ju Byte\n", p->burst);
323 printf("\t burst: %s\n", burst);
325 print_extra_delay_parms(p);
327 q = (struct dn_flow_queue *)(p+1);
328 list_queues(&(p->fs), q);
330 for (fs = next; nbytes >= sizeof *fs; fs = next) {
333 if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
335 l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
336 next = (char *)fs + l;
339 if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
340 (rulenum != fs->parent_nr && co.do_pipe == 1))) {
344 q = (struct dn_flow_queue *)(fs+1);
345 sprintf(prefix, "q%05d: weight %d pipe %d ",
346 fs->fs_nr, fs->weight, fs->parent_nr);
347 print_flowset_parms(fs, prefix);
353 * Delete pipe or queue i
356 ipfw_delete_pipe(int pipe_or_queue, int i)
360 memset(&p, 0, sizeof p);
361 if (pipe_or_queue == 1)
362 p.pipe_nr = i; /* pipe */
364 p.fs.fs_nr = i; /* queue */
365 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
368 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
374 * Code to parse delay profiles.
376 * Some link types introduce extra delays in the transmission
377 * of a packet, e.g. because of MAC level framing, contention on
378 * the use of the channel, MAC level retransmissions and so on.
379 * From our point of view, the channel is effectively unavailable
380 * for this extra time, which is constant or variable depending
381 * on the link type. Additionally, packets may be dropped after this
382 * time (e.g. on a wireless link after too many retransmissions).
383 * We can model the additional delay with an empirical curve
384 * that represents its distribution.
386 * cumulative probability
396 * +-------*------------------->
399 * The empirical curve may have both vertical and horizontal lines.
400 * Vertical lines represent constant delay for a range of
401 * probabilities; horizontal lines correspond to a discontinuty
402 * in the delay distribution: the pipe will use the largest delay
403 * for a given probability.
405 * To pass the curve to dummynet, we must store the parameters
406 * in a file as described below, and issue the command
408 * ipfw pipe <n> config ... bw XXX profile <filename> ...
410 * The file format is the following, with whitespace acting as
411 * a separator and '#' indicating the beginning a comment:
414 * the number of samples used in the internal
415 * representation (2..1024; default 100);
418 * The probability above which packets are lost.
419 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
422 * Optional a name (listed by "ipfw pipe show")
423 * to identify the distribution;
425 * "delay prob" | "prob delay"
426 * One of these two lines is mandatory and defines
427 * the format of the following lines with data points.
430 * 2 or more lines representing points in the curve,
431 * with either delay or probability first, according
432 * to the chosen format.
433 * The unit for delay is milliseconds.
435 * Data points does not need to be ordered or equal to the number
436 * specified in the "samples" line. ipfw will sort and interpolate
437 * the curve as needed.
439 * Example of a profile file:
445 0 200 # minimum overhead is 200ms
452 * Internally, we will convert the curve to a fixed number of
453 * samples, and when it is time to transmit a packet we will
454 * model the extra delay as extra bits in the packet.
458 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
459 #define ED_TOK_SAMPLES "samples"
460 #define ED_TOK_LOSS "loss-level"
461 #define ED_TOK_NAME "name"
462 #define ED_TOK_DELAY "delay"
463 #define ED_TOK_PROB "prob"
464 #define ED_TOK_BW "bw"
465 #define ED_SEPARATORS " \t\n"
466 #define ED_MIN_SAMPLES_NO 2
469 * returns 1 if s is a non-negative number, with at least one '.'
472 is_valid_number(const char *s)
474 int i, dots_found = 0;
477 for (i = 0; i<len; ++i)
478 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
484 * Take as input a string describing a bandwidth value
485 * and return the numeric bandwidth value.
486 * set clocking interface or bandwidth value
489 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
491 if (*bandwidth != -1)
492 warn("duplicate token, override bandwidth value!");
494 if (arg[0] >= 'a' && arg[0] <= 'z') {
495 if (namelen >= IFNAMSIZ)
496 warn("interface name truncated");
499 strncpy(if_name, arg, namelen);
500 if_name[namelen] = '\0';
502 } else { /* read bandwidth value */
506 bw = strtoul(arg, &end, 0);
507 if (*end == 'K' || *end == 'k') {
510 } else if (*end == 'M') {
515 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
516 _substrcmp2(end, "by", "bytes") == 0)
520 errx(EX_DATAERR, "bandwidth too large");
533 compare_points(const void *vp1, const void *vp2)
535 const struct point *p1 = vp1;
536 const struct point *p2 = vp2;
539 res = p1->prob - p2->prob;
541 res = p1->delay - p2->delay;
550 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
553 load_extra_delays(const char *filename, struct dn_pipe *p)
555 char line[ED_MAX_LINE_LEN];
562 char profile_name[ED_MAX_NAME_LEN];
563 int delay_first = -1;
565 struct point points[ED_MAX_SAMPLES_NO];
568 profile_name[0] = '\0';
569 f = fopen(filename, "r");
571 err(EX_UNAVAILABLE, "fopen: %s", filename);
573 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
574 char *s, *cur = line, *name = NULL, *arg = NULL;
580 s = strsep(&cur, ED_SEPARATORS);
581 if (s == NULL || *s == '#')
586 errx(ED_EFMT("too many arguments"));
592 if (name == NULL) /* empty line */
595 errx(ED_EFMT("missing arg for %s"), name);
597 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
599 errx(ED_EFMT("duplicate ``samples'' line"));
601 errx(ED_EFMT("invalid number of samples"));
603 if (samples>ED_MAX_SAMPLES_NO)
604 errx(ED_EFMT("too many samples, maximum is %d"),
607 } else if (!strcasecmp(name, ED_TOK_BW)) {
608 read_bandwidth(arg, &p->bandwidth, p->if_name, sizeof(p->if_name));
609 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
611 errx(ED_EFMT("duplicated token: %s"), name);
612 if (!is_valid_number(arg))
613 errx(ED_EFMT("invalid %s"), arg);
616 errx(ED_EFMT("%s greater than 1.0"), name);
618 } else if (!strcasecmp(name, ED_TOK_NAME)) {
619 if (profile_name[0] != '\0')
620 errx(ED_EFMT("duplicated token: %s"), name);
621 strncpy(profile_name, arg, sizeof(profile_name) - 1);
622 profile_name[sizeof(profile_name)-1] = '\0';
624 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
626 errx(ED_EFMT("duplicated token: %s"), name);
629 } else if (!strcasecmp(name, ED_TOK_PROB)) {
631 errx(ED_EFMT("duplicated token: %s"), name);
634 } else if (do_points) {
635 if (!is_valid_number(name) || !is_valid_number(arg))
636 errx(ED_EFMT("invalid point found"));
638 points[points_no].delay = atof(name);
639 points[points_no].prob = atof(arg);
641 points[points_no].delay = atof(arg);
642 points[points_no].prob = atof(name);
644 if (points[points_no].prob > 1.0)
645 errx(ED_EFMT("probability greater than 1.0"));
648 errx(ED_EFMT("unrecognised command '%s'"), name);
655 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
660 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
664 /* make sure that there are enough points. */
665 if (points_no < ED_MIN_SAMPLES_NO)
666 errx(ED_EFMT("too few samples, need at least %d"),
669 qsort(points, points_no, sizeof(struct point), compare_points);
672 for (i = 0; i<points_no-1; ++i) {
673 double y1 = points[i].prob * samples;
674 double x1 = points[i].delay;
675 double y2 = points[i+1].prob * samples;
676 double x2 = points[i+1].delay;
682 for (; index<stop; ++index)
683 p->samples[index] = x1;
685 double m = (y2-y1)/(x2-x1);
686 double c = y1 - m*x1;
687 for (; index<stop ; ++index)
688 p->samples[index] = (index - c)/m;
691 p->samples_no = samples;
692 p->loss_level = loss * samples;
693 strncpy(p->name, profile_name, sizeof(p->name));
697 ipfw_config_pipe(int ac, char **av)
699 int samples[ED_MAX_SAMPLES_NO];
705 memset(&p, 0, sizeof p);
710 if (ac && isdigit(**av)) {
711 i = atoi(*av); av++; ac--;
719 int tok = match_token(dummynet_params, *av);
724 p.fs.flags_fs |= DN_NOERROR;
728 NEED1("plr needs argument 0..1\n");
729 d = strtod(av[0], NULL);
734 p.fs.plr = (int)(d*0x7fffffff);
739 NEED1("queue needs queue size\n");
741 p.fs.qsize = strtoul(av[0], &end, 0);
742 if (*end == 'K' || *end == 'k') {
743 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
745 } else if (*end == 'B' ||
746 _substrcmp2(end, "by", "bytes") == 0) {
747 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
753 NEED1("buckets needs argument\n");
754 p.fs.rq_size = strtoul(av[0], NULL, 0);
759 NEED1("mask needs mask specifier\n");
761 * per-flow queue, mask is dst_ip, dst_port,
762 * src_ip, src_port, proto measured in bits
766 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
770 uint32_t *p32 = NULL;
771 uint16_t *p16 = NULL;
772 uint32_t *p20 = NULL;
773 struct in6_addr *pa6 = NULL;
776 tok = match_token(dummynet_params, *av);
781 * special case, all bits significant
783 p.fs.flow_mask.dst_ip = ~0;
784 p.fs.flow_mask.src_ip = ~0;
785 p.fs.flow_mask.dst_port = ~0;
786 p.fs.flow_mask.src_port = ~0;
787 p.fs.flow_mask.proto = ~0;
788 n2mask(&(p.fs.flow_mask.dst_ip6), 128);
789 n2mask(&(p.fs.flow_mask.src_ip6), 128);
790 p.fs.flow_mask.flow_id6 = ~0;
791 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
795 p32 = &p.fs.flow_mask.dst_ip;
799 p32 = &p.fs.flow_mask.src_ip;
803 pa6 = &(p.fs.flow_mask.dst_ip6);
807 pa6 = &(p.fs.flow_mask.src_ip6);
811 p20 = &p.fs.flow_mask.flow_id6;
815 p16 = &p.fs.flow_mask.dst_port;
819 p16 = &p.fs.flow_mask.src_port;
826 ac++; av--; /* backtrack */
830 errx(EX_USAGE, "mask: value missing");
832 a = strtoul(av[0]+1, &end, 0);
834 a = (a == 32) ? ~0 : (1 << a) - 1;
836 a = strtoul(av[0], &end, 0);
839 else if (p16 != NULL) {
842 "port mask must be 16 bit");
844 } else if (p20 != NULL) {
847 "flow_id mask must be 20 bit");
849 } else if (pa6 != NULL) {
852 "in6addr invalid mask len");
858 "proto mask must be 8 bit");
859 p.fs.flow_mask.proto = (uint8_t)a;
862 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
864 } /* end while, config masks */
870 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
871 p.fs.flags_fs |= DN_IS_RED;
873 p.fs.flags_fs |= DN_IS_GENTLE_RED;
875 * the format for parameters is w_q/min_th/max_th/max_p
877 if ((end = strsep(&av[0], "/"))) {
878 double w_q = strtod(end, NULL);
879 if (w_q > 1 || w_q <= 0)
880 errx(EX_DATAERR, "0 < w_q <= 1");
881 p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
883 if ((end = strsep(&av[0], "/"))) {
884 p.fs.min_th = strtoul(end, &end, 0);
885 if (*end == 'K' || *end == 'k')
888 if ((end = strsep(&av[0], "/"))) {
889 p.fs.max_th = strtoul(end, &end, 0);
890 if (*end == 'K' || *end == 'k')
893 if ((end = strsep(&av[0], "/"))) {
894 double max_p = strtod(end, NULL);
895 if (max_p > 1 || max_p <= 0)
896 errx(EX_DATAERR, "0 < max_p <= 1");
897 p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
903 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
907 NEED1("bw needs bandwidth or interface\n");
909 errx(EX_DATAERR, "bandwidth only valid for pipes");
910 read_bandwidth(av[0], &p.bandwidth, p.if_name, sizeof(p.if_name));
916 errx(EX_DATAERR, "delay only valid for pipes");
917 NEED1("delay needs argument 0..10000ms\n");
918 p.delay = strtoul(av[0], NULL, 0);
924 errx(EX_DATAERR,"weight only valid for queues");
925 NEED1("weight needs argument 0..100\n");
926 p.fs.weight = strtoul(av[0], &end, 0);
932 errx(EX_DATAERR,"pipe only valid for queues");
933 NEED1("pipe needs pipe_number\n");
934 p.fs.parent_nr = strtoul(av[0], &end, 0);
938 case TOK_PIPE_PROFILE:
940 errx(EX_DATAERR, "extra delay only valid for pipes");
941 NEED1("extra delay needs the file name\n");
942 p.samples = &samples[0];
943 load_extra_delays(av[0], &p);
949 errx(EX_DATAERR, "burst only valid for pipes");
950 NEED1("burst needs argument\n");
952 if (expand_number(av[0], (int64_t *)&p.burst) < 0)
955 "burst: invalid argument");
956 if (errno || p.burst > (1ULL << 48) - 1)
958 "burst: out of range (0..2^48-1)");
963 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
966 if (co.do_pipe == 1) {
968 errx(EX_DATAERR, "pipe_nr must be > 0");
970 errx(EX_DATAERR, "delay must be < 10000");
971 } else { /* co.do_pipe == 2, queue */
972 if (p.fs.parent_nr == 0)
973 errx(EX_DATAERR, "pipe must be > 0");
974 if (p.fs.weight >100)
975 errx(EX_DATAERR, "weight must be <= 100");
978 /* check for bandwidth value */
979 if (p.bandwidth == -1) {
981 if (p.samples_no > 0)
982 errx(EX_DATAERR, "profile requires a bandwidth limit");
985 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
990 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
991 &limit, &len, NULL, 0) == -1)
993 if (p.fs.qsize > limit)
994 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1000 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1001 &limit, &len, NULL, 0) == -1)
1003 if (p.fs.qsize > limit)
1004 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1006 if (p.fs.flags_fs & DN_IS_RED) {
1008 int lookup_depth, avg_pkt_size;
1009 double s, idle, weight, w_q;
1010 struct clockinfo ck;
1013 if (p.fs.min_th >= p.fs.max_th)
1014 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1015 p.fs.min_th, p.fs.max_th);
1016 if (p.fs.max_th == 0)
1017 errx(EX_DATAERR, "max_th must be > 0");
1020 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1021 &lookup_depth, &len, NULL, 0) == -1)
1022 errx(1, "sysctlbyname(\"%s\")",
1023 "net.inet.ip.dummynet.red_lookup_depth");
1024 if (lookup_depth == 0)
1025 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1026 " must be greater than zero");
1029 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1030 &avg_pkt_size, &len, NULL, 0) == -1)
1032 errx(1, "sysctlbyname(\"%s\")",
1033 "net.inet.ip.dummynet.red_avg_pkt_size");
1034 if (avg_pkt_size == 0)
1036 "net.inet.ip.dummynet.red_avg_pkt_size must"
1037 " be greater than zero");
1039 len = sizeof(struct clockinfo);
1040 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
1041 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
1044 * Ticks needed for sending a medium-sized packet.
1045 * Unfortunately, when we are configuring a WF2Q+ queue, we
1046 * do not have bandwidth information, because that is stored
1047 * in the parent pipe, and also we have multiple queues
1048 * competing for it. So we set s=0, which is not very
1049 * correct. But on the other hand, why do we want RED with
1052 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1055 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1058 * max idle time (in ticks) before avg queue size becomes 0.
1059 * NOTA: (3/w_q) is approx the value x so that
1060 * (1-w_q)^x < 10^-3.
1062 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
1063 idle = s * 3. / w_q;
1064 p.fs.lookup_step = (int)idle / lookup_depth;
1065 if (!p.fs.lookup_step)
1066 p.fs.lookup_step = 1;
1068 for (t = p.fs.lookup_step; t > 1; --t)
1070 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
1072 if (p.samples_no <= 0) {
1073 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
1075 struct dn_pipe_max pm;
1076 int len = sizeof(pm);
1078 memcpy(&pm.pipe, &p, sizeof(pm.pipe));
1079 memcpy(&pm.samples, samples, sizeof(pm.samples));
1081 i = do_cmd(IP_DUMMYNET_CONFIGURE, &pm, len);
1085 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");