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 { "burst", TOK_BURST},
74 { "dummynet-params", TOK_NULL },
75 { NULL, 0 } /* terminator */
79 * XXX to be updated to the new version,
80 * without the global struct command_opts variable
83 sort_q(void * to_be_done, const void *pa, const void *pb)
85 int rev = (co.do_sort < 0);
86 int field = rev ? -co.do_sort : co.do_sort;
88 const struct dn_flow_queue *a = pa;
89 const struct dn_flow_queue *b = pb;
93 res = a->len - b->len;
96 res = a->len_bytes - b->len_bytes;
99 case 3: /* tot pkts */
100 res = a->tot_pkts - b->tot_pkts;
103 case 4: /* tot bytes */
104 res = a->tot_bytes - b->tot_bytes;
111 return (int)(rev ? res : -res);
115 list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
118 int index_printed, indexes = 0;
122 if (fs->rq_elements == 0)
126 qsort_r(q, fs->rq_elements, sizeof *q, NULL, sort_q);
128 /* Print IPv4 flows */
130 for (l = 0; l < fs->rq_elements; l++) {
133 /* XXX: Should check for IPv4 flows */
134 if (IS_IP6_FLOW_ID(&(q[l].id)))
137 if (!index_printed) {
139 if (indexes > 0) /* currently a no-op */
143 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
145 fs->flow_mask.src_ip, fs->flow_mask.src_port,
146 fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
148 printf("BKT Prot ___Source IP/port____ "
149 "____Dest. IP/port____ "
150 "Tot_pkt/bytes Pkt/Byte Drp\n");
153 printf("%3d ", q[l].hash_slot);
154 pe = getprotobynumber(q[l].id.proto);
156 printf("%-4s ", pe->p_name);
158 printf("%4u ", q[l].id.proto);
159 ina.s_addr = htonl(q[l].id.src_ip);
161 inet_ntoa(ina), q[l].id.src_port);
162 ina.s_addr = htonl(q[l].id.dst_ip);
164 inet_ntoa(ina), q[l].id.dst_port);
165 printf("%4llu %8llu %2u %4u %3u\n",
166 align_uint64(&q[l].tot_pkts),
167 align_uint64(&q[l].tot_bytes),
168 q[l].len, q[l].len_bytes, q[l].drops);
170 printf(" S %20llu F %20llu\n",
171 align_uint64(&q[l].S), align_uint64(&q[l].F));
174 /* Print IPv6 flows */
176 for (l = 0; l < fs->rq_elements; l++) {
177 if (!IS_IP6_FLOW_ID(&(q[l].id)))
180 if (!index_printed) {
185 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ",
186 fs->flow_mask.proto, fs->flow_mask.flow_id6);
187 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
189 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
190 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
191 buff, sizeof(buff) );
192 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
194 printf("BKT ___Prot___ _flow-id_ "
195 "______________Source IPv6/port_______________ "
196 "_______________Dest. IPv6/port_______________ "
197 "Tot_pkt/bytes Pkt/Byte Drp\n");
199 printf("%3d ", q[l].hash_slot);
200 pe = getprotobynumber(q[l].id.proto);
202 printf("%9s ", pe->p_name);
204 printf("%9u ", q[l].id.proto);
205 printf("%7d %39s/%-5d ", q[l].id.flow_id6,
206 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
208 printf(" %39s/%-5d ",
209 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
211 printf(" %4llu %8llu %2u %4u %3u\n",
212 align_uint64(&q[l].tot_pkts),
213 align_uint64(&q[l].tot_bytes),
214 q[l].len, q[l].len_bytes, q[l].drops);
216 printf(" S %20llu F %20llu\n",
217 align_uint64(&q[l].S),
218 align_uint64(&q[l].F));
223 print_flowset_parms(struct dn_flow_set *fs, char *prefix)
228 char red[90]; /* Display RED parameters */
231 if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
233 sprintf(qs, "%d KB", l / 1024);
235 sprintf(qs, "%d B", l);
237 sprintf(qs, "%3d sl.", l);
239 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
242 if (fs->flags_fs & DN_IS_RED) /* RED parameters */
244 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
245 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
246 1.0 * fs->w_q / (double)(1 << SCALE_RED),
247 SCALE_VAL(fs->min_th),
248 SCALE_VAL(fs->max_th),
249 1.0 * fs->max_p / (double)(1 << SCALE_RED));
251 sprintf(red, "droptail");
253 printf("%s %s%s %d queues (%d buckets) %s\n",
254 prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
258 print_extra_delay_parms(struct dn_pipe *p)
261 if (p->samples_no <= 0)
264 loss = p->loss_level;
265 loss /= p->samples_no;
266 printf("\t profile: name \"%s\" loss %f samples %d\n",
267 p->name, loss, p->samples_no);
271 ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
275 struct dn_pipe *p = (struct dn_pipe *) data;
276 struct dn_flow_set *fs;
277 struct dn_flow_queue *q;
281 rulenum = strtoul(*av++, NULL, 10);
284 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
285 double b = p->bandwidth;
290 if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
291 break; /* done with pipes, now queues */
294 * compute length, as pipe have variable size
296 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
297 next = (char *)p + l;
300 if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
304 * Print rate (or clocking interface)
306 if (p->if_name[0] != '\0')
307 sprintf(buf, "%s", p->if_name);
309 sprintf(buf, "unlimited");
310 else if (b >= 1000000)
311 sprintf(buf, "%7.3f Mbit/s", b/1000000);
313 sprintf(buf, "%7.3f Kbit/s", b/1000);
315 sprintf(buf, "%7.3f bit/s ", b);
317 sprintf(prefix, "%05d: %s %4d ms ",
318 p->pipe_nr, buf, p->delay);
320 print_flowset_parms(&(p->fs), prefix);
322 if (humanize_number(burst, sizeof(burst), p->burst,
323 "Byte", HN_AUTOSCALE, 0) < 0 || co.verbose)
324 printf("\t burst: %ju Byte\n", p->burst);
326 printf("\t burst: %s\n", burst);
328 print_extra_delay_parms(p);
330 q = (struct dn_flow_queue *)(p+1);
331 list_queues(&(p->fs), q);
333 for (fs = next; nbytes >= sizeof *fs; fs = next) {
336 if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
338 l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
339 next = (char *)fs + l;
342 if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
343 (rulenum != fs->parent_nr && co.do_pipe == 1))) {
347 q = (struct dn_flow_queue *)(fs+1);
348 sprintf(prefix, "q%05d: weight %d pipe %d ",
349 fs->fs_nr, fs->weight, fs->parent_nr);
350 print_flowset_parms(fs, prefix);
356 * Delete pipe or queue i
359 ipfw_delete_pipe(int pipe_or_queue, int i)
363 memset(&p, 0, sizeof p);
364 if (pipe_or_queue == 1)
365 p.pipe_nr = i; /* pipe */
367 p.fs.fs_nr = i; /* queue */
368 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
371 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
377 * Code to parse delay profiles.
379 * Some link types introduce extra delays in the transmission
380 * of a packet, e.g. because of MAC level framing, contention on
381 * the use of the channel, MAC level retransmissions and so on.
382 * From our point of view, the channel is effectively unavailable
383 * for this extra time, which is constant or variable depending
384 * on the link type. Additionally, packets may be dropped after this
385 * time (e.g. on a wireless link after too many retransmissions).
386 * We can model the additional delay with an empirical curve
387 * that represents its distribution.
389 * cumulative probability
399 * +-------*------------------->
402 * The empirical curve may have both vertical and horizontal lines.
403 * Vertical lines represent constant delay for a range of
404 * probabilities; horizontal lines correspond to a discontinuty
405 * in the delay distribution: the pipe will use the largest delay
406 * for a given probability.
408 * To pass the curve to dummynet, we must store the parameters
409 * in a file as described below, and issue the command
411 * ipfw pipe <n> config ... bw XXX profile <filename> ...
413 * The file format is the following, with whitespace acting as
414 * a separator and '#' indicating the beginning a comment:
417 * the number of samples used in the internal
418 * representation (2..1024; default 100);
421 * The probability above which packets are lost.
422 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
425 * Optional a name (listed by "ipfw pipe show")
426 * to identify the distribution;
428 * "delay prob" | "prob delay"
429 * One of these two lines is mandatory and defines
430 * the format of the following lines with data points.
433 * 2 or more lines representing points in the curve,
434 * with either delay or probability first, according
435 * to the chosen format.
436 * The unit for delay is milliseconds.
438 * Data points does not need to be ordered or equal to the number
439 * specified in the "samples" line. ipfw will sort and interpolate
440 * the curve as needed.
442 * Example of a profile file:
448 0 200 # minimum overhead is 200ms
455 * Internally, we will convert the curve to a fixed number of
456 * samples, and when it is time to transmit a packet we will
457 * model the extra delay as extra bits in the packet.
461 /* XXX move to an array definition ? */
462 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
463 #define ED_TOK_SAMPLES "samples"
464 #define ED_TOK_LOSS "loss-level"
465 #define ED_TOK_NAME "name"
466 #define ED_TOK_DELAY "delay"
467 #define ED_TOK_PROB "prob"
468 #define ED_TOK_BW "bw"
469 #define ED_SEPARATORS " \t\n"
470 #define ED_MIN_SAMPLES_NO 2
473 * returns 1 if s is a non-negative number, with at least one '.'
476 is_valid_number(const char *s)
478 int i, dots_found = 0;
481 for (i = 0; i<len; ++i)
482 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
488 * Take as input a string describing a bandwidth value
489 * and return the numeric bandwidth value.
490 * set clocking interface or bandwidth value
493 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
495 if (*bandwidth != -1)
496 warn("duplicate token, override bandwidth value!");
498 if (arg[0] >= 'a' && arg[0] <= 'z') {
499 if (namelen >= IFNAMSIZ)
500 warn("interface name truncated");
503 strncpy(if_name, arg, namelen);
504 if_name[namelen] = '\0';
506 } else { /* read bandwidth value */
510 bw = strtoul(arg, &end, 0);
511 if (*end == 'K' || *end == 'k') {
514 } else if (*end == 'M') {
519 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
520 _substrcmp2(end, "by", "bytes") == 0)
524 errx(EX_DATAERR, "bandwidth too large");
537 compare_points(const void *vp1, const void *vp2)
539 const struct point *p1 = vp1;
540 const struct point *p2 = vp2;
543 res = p1->prob - p2->prob;
545 res = p1->delay - p2->delay;
554 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
557 load_extra_delays(const char *filename, struct dn_pipe *p)
559 char line[ED_MAX_LINE_LEN];
566 char profile_name[ED_MAX_NAME_LEN];
567 int delay_first = -1;
569 struct point points[ED_MAX_SAMPLES_NO];
572 profile_name[0] = '\0';
573 f = fopen(filename, "r");
575 err(EX_UNAVAILABLE, "fopen: %s", filename);
577 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
578 char *s, *cur = line, *name = NULL, *arg = NULL;
584 s = strsep(&cur, ED_SEPARATORS);
585 if (s == NULL || *s == '#')
590 errx(ED_EFMT("too many arguments"));
596 if (name == NULL) /* empty line */
599 errx(ED_EFMT("missing arg for %s"), name);
601 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
603 errx(ED_EFMT("duplicate ``samples'' line"));
605 errx(ED_EFMT("invalid number of samples"));
607 if (samples>ED_MAX_SAMPLES_NO)
608 errx(ED_EFMT("too many samples, maximum is %d"),
611 } else if (!strcasecmp(name, ED_TOK_BW)) {
612 read_bandwidth(arg, &p->bandwidth, p->if_name, sizeof(p->if_name));
613 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
615 errx(ED_EFMT("duplicated token: %s"), name);
616 if (!is_valid_number(arg))
617 errx(ED_EFMT("invalid %s"), arg);
620 errx(ED_EFMT("%s greater than 1.0"), name);
622 } else if (!strcasecmp(name, ED_TOK_NAME)) {
623 if (profile_name[0] != '\0')
624 errx(ED_EFMT("duplicated token: %s"), name);
625 strncpy(profile_name, arg, sizeof(profile_name) - 1);
626 profile_name[sizeof(profile_name)-1] = '\0';
628 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
630 errx(ED_EFMT("duplicated token: %s"), name);
633 } else if (!strcasecmp(name, ED_TOK_PROB)) {
635 errx(ED_EFMT("duplicated token: %s"), name);
638 } else if (do_points) {
639 if (!is_valid_number(name) || !is_valid_number(arg))
640 errx(ED_EFMT("invalid point found"));
642 points[points_no].delay = atof(name);
643 points[points_no].prob = atof(arg);
645 points[points_no].delay = atof(arg);
646 points[points_no].prob = atof(name);
648 if (points[points_no].prob > 1.0)
649 errx(ED_EFMT("probability greater than 1.0"));
652 errx(ED_EFMT("unrecognised command '%s'"), name);
659 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
664 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
668 /* make sure that there are enough points. */
669 if (points_no < ED_MIN_SAMPLES_NO)
670 errx(ED_EFMT("too few samples, need at least %d"),
673 qsort(points, points_no, sizeof(struct point), compare_points);
676 for (i = 0; i<points_no-1; ++i) {
677 double y1 = points[i].prob * samples;
678 double x1 = points[i].delay;
679 double y2 = points[i+1].prob * samples;
680 double x2 = points[i+1].delay;
686 for (; index<stop; ++index)
687 p->samples[index] = x1;
689 double m = (y2-y1)/(x2-x1);
690 double c = y1 - m*x1;
691 for (; index<stop ; ++index)
692 p->samples[index] = (index - c)/m;
695 p->samples_no = samples;
696 p->loss_level = loss * samples;
697 strncpy(p->name, profile_name, sizeof(p->name));
701 ipfw_config_pipe(int ac, char **av)
703 int samples[ED_MAX_SAMPLES_NO];
709 memset(&p, 0, sizeof p);
714 if (ac && isdigit(**av)) {
715 i = atoi(*av); av++; ac--;
723 int tok = match_token(dummynet_params, *av);
728 p.fs.flags_fs |= DN_NOERROR;
732 NEED1("plr needs argument 0..1\n");
733 d = strtod(av[0], NULL);
738 p.fs.plr = (int)(d*0x7fffffff);
743 NEED1("queue needs queue size\n");
745 p.fs.qsize = strtoul(av[0], &end, 0);
746 if (*end == 'K' || *end == 'k') {
747 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
749 } else if (*end == 'B' ||
750 _substrcmp2(end, "by", "bytes") == 0) {
751 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
757 NEED1("buckets needs argument\n");
758 p.fs.rq_size = strtoul(av[0], NULL, 0);
763 NEED1("mask needs mask specifier\n");
765 * per-flow queue, mask is dst_ip, dst_port,
766 * src_ip, src_port, proto measured in bits
770 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
774 uint32_t *p32 = NULL;
775 uint16_t *p16 = NULL;
776 uint32_t *p20 = NULL;
777 struct in6_addr *pa6 = NULL;
780 tok = match_token(dummynet_params, *av);
785 * special case, all bits significant
787 p.fs.flow_mask.dst_ip = ~0;
788 p.fs.flow_mask.src_ip = ~0;
789 p.fs.flow_mask.dst_port = ~0;
790 p.fs.flow_mask.src_port = ~0;
791 p.fs.flow_mask.proto = ~0;
792 n2mask(&(p.fs.flow_mask.dst_ip6), 128);
793 n2mask(&(p.fs.flow_mask.src_ip6), 128);
794 p.fs.flow_mask.flow_id6 = ~0;
795 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
799 p32 = &p.fs.flow_mask.dst_ip;
803 p32 = &p.fs.flow_mask.src_ip;
807 pa6 = &(p.fs.flow_mask.dst_ip6);
811 pa6 = &(p.fs.flow_mask.src_ip6);
815 p20 = &p.fs.flow_mask.flow_id6;
819 p16 = &p.fs.flow_mask.dst_port;
823 p16 = &p.fs.flow_mask.src_port;
830 ac++; av--; /* backtrack */
834 errx(EX_USAGE, "mask: value missing");
836 a = strtoul(av[0]+1, &end, 0);
838 a = (a == 32) ? ~0 : (1 << a) - 1;
840 a = strtoul(av[0], &end, 0);
843 else if (p16 != NULL) {
846 "port mask must be 16 bit");
848 } else if (p20 != NULL) {
851 "flow_id mask must be 20 bit");
853 } else if (pa6 != NULL) {
856 "in6addr invalid mask len");
862 "proto mask must be 8 bit");
863 p.fs.flow_mask.proto = (uint8_t)a;
866 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
868 } /* end while, config masks */
874 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
875 p.fs.flags_fs |= DN_IS_RED;
877 p.fs.flags_fs |= DN_IS_GENTLE_RED;
879 * the format for parameters is w_q/min_th/max_th/max_p
881 if ((end = strsep(&av[0], "/"))) {
882 double w_q = strtod(end, NULL);
883 if (w_q > 1 || w_q <= 0)
884 errx(EX_DATAERR, "0 < w_q <= 1");
885 p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
887 if ((end = strsep(&av[0], "/"))) {
888 p.fs.min_th = strtoul(end, &end, 0);
889 if (*end == 'K' || *end == 'k')
892 if ((end = strsep(&av[0], "/"))) {
893 p.fs.max_th = strtoul(end, &end, 0);
894 if (*end == 'K' || *end == 'k')
897 if ((end = strsep(&av[0], "/"))) {
898 double max_p = strtod(end, NULL);
899 if (max_p > 1 || max_p <= 0)
900 errx(EX_DATAERR, "0 < max_p <= 1");
901 p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
907 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
911 NEED1("bw needs bandwidth or interface\n");
913 errx(EX_DATAERR, "bandwidth only valid for pipes");
914 read_bandwidth(av[0], &p.bandwidth, p.if_name, sizeof(p.if_name));
920 errx(EX_DATAERR, "delay only valid for pipes");
921 NEED1("delay needs argument 0..10000ms\n");
922 p.delay = strtoul(av[0], NULL, 0);
928 errx(EX_DATAERR,"weight only valid for queues");
929 NEED1("weight needs argument 0..100\n");
930 p.fs.weight = strtoul(av[0], &end, 0);
936 errx(EX_DATAERR,"pipe only valid for queues");
937 NEED1("pipe needs pipe_number\n");
938 p.fs.parent_nr = strtoul(av[0], &end, 0);
942 case TOK_PIPE_PROFILE:
944 errx(EX_DATAERR, "extra delay only valid for pipes");
945 NEED1("extra delay needs the file name\n");
946 p.samples = &samples[0];
947 load_extra_delays(av[0], &p);
953 errx(EX_DATAERR, "burst only valid for pipes");
954 NEED1("burst needs argument\n");
956 if (expand_number(av[0], (int64_t *)&p.burst) < 0)
959 "burst: invalid argument");
960 if (errno || p.burst > (1ULL << 48) - 1)
962 "burst: out of range (0..2^48-1)");
967 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
970 if (co.do_pipe == 1) {
972 errx(EX_DATAERR, "pipe_nr must be > 0");
974 errx(EX_DATAERR, "delay must be < 10000");
975 } else { /* co.do_pipe == 2, queue */
976 if (p.fs.parent_nr == 0)
977 errx(EX_DATAERR, "pipe must be > 0");
978 if (p.fs.weight >100)
979 errx(EX_DATAERR, "weight must be <= 100");
982 /* check for bandwidth value */
983 if (p.bandwidth == -1) {
985 if (p.samples_no > 0)
986 errx(EX_DATAERR, "profile requires a bandwidth limit");
989 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
994 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
995 &limit, &len, NULL, 0) == -1)
997 if (p.fs.qsize > limit)
998 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1003 len = sizeof(limit);
1004 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1005 &limit, &len, NULL, 0) == -1)
1007 if (p.fs.qsize > limit)
1008 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1010 if (p.fs.flags_fs & DN_IS_RED) {
1012 int lookup_depth, avg_pkt_size;
1013 double s, idle, weight, w_q;
1014 struct clockinfo ck;
1017 if (p.fs.min_th >= p.fs.max_th)
1018 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1019 p.fs.min_th, p.fs.max_th);
1020 if (p.fs.max_th == 0)
1021 errx(EX_DATAERR, "max_th must be > 0");
1024 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1025 &lookup_depth, &len, NULL, 0) == -1)
1026 errx(1, "sysctlbyname(\"%s\")",
1027 "net.inet.ip.dummynet.red_lookup_depth");
1028 if (lookup_depth == 0)
1029 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1030 " must be greater than zero");
1033 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1034 &avg_pkt_size, &len, NULL, 0) == -1)
1036 errx(1, "sysctlbyname(\"%s\")",
1037 "net.inet.ip.dummynet.red_avg_pkt_size");
1038 if (avg_pkt_size == 0)
1040 "net.inet.ip.dummynet.red_avg_pkt_size must"
1041 " be greater than zero");
1043 len = sizeof(struct clockinfo);
1044 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
1045 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
1048 * Ticks needed for sending a medium-sized packet.
1049 * Unfortunately, when we are configuring a WF2Q+ queue, we
1050 * do not have bandwidth information, because that is stored
1051 * in the parent pipe, and also we have multiple queues
1052 * competing for it. So we set s=0, which is not very
1053 * correct. But on the other hand, why do we want RED with
1056 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1059 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1062 * max idle time (in ticks) before avg queue size becomes 0.
1063 * NOTA: (3/w_q) is approx the value x so that
1064 * (1-w_q)^x < 10^-3.
1066 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
1067 idle = s * 3. / w_q;
1068 p.fs.lookup_step = (int)idle / lookup_depth;
1069 if (!p.fs.lookup_step)
1070 p.fs.lookup_step = 1;
1072 for (t = p.fs.lookup_step; t > 1; --t)
1074 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
1076 if (p.samples_no <= 0) {
1077 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
1079 struct dn_pipe_max pm;
1080 int len = sizeof(pm);
1082 memcpy(&pm.pipe, &p, sizeof(pm.pipe));
1083 memcpy(&pm.samples, samples, sizeof(pm.samples));
1085 i = do_cmd(IP_DUMMYNET_CONFIGURE, &pm, len);
1089 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");