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
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>
44 #include <netinet/in.h>
45 #include <netinet/ip_fw.h>
46 #include <netinet/ip_dummynet.h>
47 #include <arpa/inet.h> /* inet_ntoa */
49 static struct _s_x dummynet_params[] = {
51 { "noerror", TOK_NOERROR },
52 { "buckets", TOK_BUCKETS },
53 { "dst-ip", TOK_DSTIP },
54 { "src-ip", TOK_SRCIP },
55 { "dst-port", TOK_DSTPORT },
56 { "src-port", TOK_SRCPORT },
57 { "proto", TOK_PROTO },
58 { "weight", TOK_WEIGHT },
61 { "droptail", TOK_DROPTAIL },
65 { "bandwidth", TOK_BW },
66 { "delay", TOK_DELAY },
68 { "queue", TOK_QUEUE },
69 { "flow-id", TOK_FLOWID},
70 { "dst-ipv6", TOK_DSTIP6},
71 { "dst-ip6", TOK_DSTIP6},
72 { "src-ipv6", TOK_SRCIP6},
73 { "src-ip6", TOK_SRCIP6},
74 { "profile", TOK_PIPE_PROFILE},
75 { "burst", TOK_BURST},
76 { "dummynet-params", TOK_NULL },
77 { NULL, 0 } /* terminator */
81 sort_q(const void *pa, const void *pb)
83 int rev = (co.do_sort < 0);
84 int field = rev ? -co.do_sort : co.do_sort;
86 const struct dn_flow_queue *a = pa;
87 const struct dn_flow_queue *b = pb;
91 res = a->len - b->len;
94 res = a->len_bytes - b->len_bytes;
97 case 3: /* tot pkts */
98 res = a->tot_pkts - b->tot_pkts;
101 case 4: /* tot bytes */
102 res = a->tot_bytes - b->tot_bytes;
109 return (int)(rev ? res : -res);
113 list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
116 int index_printed, indexes = 0;
120 if (fs->rq_elements == 0)
124 heapsort(q, fs->rq_elements, sizeof *q, sort_q);
126 /* Print IPv4 flows */
128 for (l = 0; l < fs->rq_elements; l++) {
131 /* XXX: Should check for IPv4 flows */
132 if (IS_IP6_FLOW_ID(&(q[l].id)))
135 if (!index_printed) {
137 if (indexes > 0) /* currently a no-op */
141 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
143 fs->flow_mask.src_ip, fs->flow_mask.src_port,
144 fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
146 printf("BKT Prot ___Source IP/port____ "
147 "____Dest. IP/port____ "
148 "Tot_pkt/bytes Pkt/Byte Drp\n");
151 printf("%3d ", q[l].hash_slot);
152 pe = getprotobynumber(q[l].id.proto);
154 printf("%-4s ", pe->p_name);
156 printf("%4u ", q[l].id.proto);
157 ina.s_addr = htonl(q[l].id.src_ip);
159 inet_ntoa(ina), q[l].id.src_port);
160 ina.s_addr = htonl(q[l].id.dst_ip);
162 inet_ntoa(ina), q[l].id.dst_port);
163 printf("%4llu %8llu %2u %4u %3u\n",
164 align_uint64(&q[l].tot_pkts),
165 align_uint64(&q[l].tot_bytes),
166 q[l].len, q[l].len_bytes, q[l].drops);
168 printf(" S %20llu F %20llu\n",
169 align_uint64(&q[l].S), align_uint64(&q[l].F));
172 /* Print IPv6 flows */
174 for (l = 0; l < fs->rq_elements; l++) {
175 if (!IS_IP6_FLOW_ID(&(q[l].id)))
178 if (!index_printed) {
183 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ",
184 fs->flow_mask.proto, fs->flow_mask.flow_id6);
185 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
187 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
188 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
189 buff, sizeof(buff) );
190 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
192 printf("BKT ___Prot___ _flow-id_ "
193 "______________Source IPv6/port_______________ "
194 "_______________Dest. IPv6/port_______________ "
195 "Tot_pkt/bytes Pkt/Byte Drp\n");
197 printf("%3d ", q[l].hash_slot);
198 pe = getprotobynumber(q[l].id.proto);
200 printf("%9s ", pe->p_name);
202 printf("%9u ", q[l].id.proto);
203 printf("%7d %39s/%-5d ", q[l].id.flow_id6,
204 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
206 printf(" %39s/%-5d ",
207 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
209 printf(" %4llu %8llu %2u %4u %3u\n",
210 align_uint64(&q[l].tot_pkts),
211 align_uint64(&q[l].tot_bytes),
212 q[l].len, q[l].len_bytes, q[l].drops);
214 printf(" S %20llu F %20llu\n",
215 align_uint64(&q[l].S),
216 align_uint64(&q[l].F));
221 print_flowset_parms(struct dn_flow_set *fs, char *prefix)
226 char red[90]; /* Display RED parameters */
229 if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
231 sprintf(qs, "%d KB", l / 1024);
233 sprintf(qs, "%d B", l);
235 sprintf(qs, "%3d sl.", l);
237 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
240 if (fs->flags_fs & DN_IS_RED) /* RED parameters */
242 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
243 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
244 1.0 * fs->w_q / (double)(1 << SCALE_RED),
245 SCALE_VAL(fs->min_th),
246 SCALE_VAL(fs->max_th),
247 1.0 * fs->max_p / (double)(1 << SCALE_RED));
249 sprintf(red, "droptail");
251 printf("%s %s%s %d queues (%d buckets) %s\n",
252 prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
256 print_extra_delay_parms(struct dn_pipe *p)
259 if (p->samples_no <= 0)
262 loss = p->loss_level;
263 loss /= p->samples_no;
264 printf("\t profile: name \"%s\" loss %f samples %d\n",
265 p->name, loss, p->samples_no);
269 ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[])
273 struct dn_pipe *p = (struct dn_pipe *) data;
274 struct dn_flow_set *fs;
275 struct dn_flow_queue *q;
279 rulenum = strtoul(*av++, NULL, 10);
282 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
283 double b = p->bandwidth;
288 if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE)
289 break; /* done with pipes, now queues */
292 * compute length, as pipe have variable size
294 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
295 next = (char *)p + l;
298 if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2)
302 * Print rate (or clocking interface)
304 if (p->if_name[0] != '\0')
305 sprintf(buf, "%s", p->if_name);
307 sprintf(buf, "unlimited");
308 else if (b >= 1000000)
309 sprintf(buf, "%7.3f Mbit/s", b/1000000);
311 sprintf(buf, "%7.3f Kbit/s", b/1000);
313 sprintf(buf, "%7.3f bit/s ", b);
315 sprintf(prefix, "%05d: %s %4d ms ",
316 p->pipe_nr, buf, p->delay);
318 print_flowset_parms(&(p->fs), prefix);
320 if (humanize_number(burst, sizeof(burst), p->burst,
321 "Byte", HN_AUTOSCALE, 0) < 0 || co.verbose)
322 printf("\t burst: %ju Byte\n", p->burst);
324 printf("\t burst: %s\n", burst);
326 print_extra_delay_parms(p);
328 q = (struct dn_flow_queue *)(p+1);
329 list_queues(&(p->fs), q);
331 for (fs = next; nbytes >= sizeof *fs; fs = next) {
334 if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE)
336 l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
337 next = (char *)fs + l;
340 if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) ||
341 (rulenum != fs->parent_nr && co.do_pipe == 1))) {
345 q = (struct dn_flow_queue *)(fs+1);
346 sprintf(prefix, "q%05d: weight %d pipe %d ",
347 fs->fs_nr, fs->weight, fs->parent_nr);
348 print_flowset_parms(fs, prefix);
354 * Delete pipe or queue i
357 ipfw_delete_pipe(int pipe_or_queue, int i)
361 memset(&p, 0, sizeof p);
362 if (pipe_or_queue == 1)
363 p.pipe_nr = i; /* pipe */
365 p.fs.fs_nr = i; /* queue */
366 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
369 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
375 * Code to parse delay profiles.
377 * Some link types introduce extra delays in the transmission
378 * of a packet, e.g. because of MAC level framing, contention on
379 * the use of the channel, MAC level retransmissions and so on.
380 * From our point of view, the channel is effectively unavailable
381 * for this extra time, which is constant or variable depending
382 * on the link type. Additionally, packets may be dropped after this
383 * time (e.g. on a wireless link after too many retransmissions).
384 * We can model the additional delay with an empirical curve
385 * that represents its distribution.
387 * cumulative probability
397 * +-------*------------------->
400 * The empirical curve may have both vertical and horizontal lines.
401 * Vertical lines represent constant delay for a range of
402 * probabilities; horizontal lines correspond to a discontinuty
403 * in the delay distribution: the pipe will use the largest delay
404 * for a given probability.
406 * To pass the curve to dummynet, we must store the parameters
407 * in a file as described below, and issue the command
409 * ipfw pipe <n> config ... bw XXX profile <filename> ...
411 * The file format is the following, with whitespace acting as
412 * a separator and '#' indicating the beginning a comment:
415 * the number of samples used in the internal
416 * representation (2..1024; default 100);
419 * The probability above which packets are lost.
420 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
423 * Optional a name (listed by "ipfw pipe show")
424 * to identify the distribution;
426 * "delay prob" | "prob delay"
427 * One of these two lines is mandatory and defines
428 * the format of the following lines with data points.
431 * 2 or more lines representing points in the curve,
432 * with either delay or probability first, according
433 * to the chosen format.
434 * The unit for delay is milliseconds.
436 * Data points does not need to be ordered or equal to the number
437 * specified in the "samples" line. ipfw will sort and interpolate
438 * the curve as needed.
440 * Example of a profile file:
446 0 200 # minimum overhead is 200ms
453 * Internally, we will convert the curve to a fixed number of
454 * samples, and when it is time to transmit a packet we will
455 * model the extra delay as extra bits in the packet.
459 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
460 #define ED_TOK_SAMPLES "samples"
461 #define ED_TOK_LOSS "loss-level"
462 #define ED_TOK_NAME "name"
463 #define ED_TOK_DELAY "delay"
464 #define ED_TOK_PROB "prob"
465 #define ED_TOK_BW "bw"
466 #define ED_SEPARATORS " \t\n"
467 #define ED_MIN_SAMPLES_NO 2
470 * returns 1 if s is a non-negative number, with at least one '.'
473 is_valid_number(const char *s)
475 int i, dots_found = 0;
478 for (i = 0; i<len; ++i)
479 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
485 * Take as input a string describing a bandwidth value
486 * and return the numeric bandwidth value.
487 * set clocking interface or bandwidth value
490 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
492 if (*bandwidth != -1)
493 warn("duplicate token, override bandwidth value!");
495 if (arg[0] >= 'a' && arg[0] <= 'z') {
496 if (namelen >= IFNAMSIZ)
497 warn("interface name truncated");
500 strncpy(if_name, arg, namelen);
501 if_name[namelen] = '\0';
503 } else { /* read bandwidth value */
507 bw = strtoul(arg, &end, 0);
508 if (*end == 'K' || *end == 'k') {
511 } else if (*end == 'M') {
516 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
517 _substrcmp2(end, "by", "bytes") == 0)
521 errx(EX_DATAERR, "bandwidth too large");
534 compare_points(const void *vp1, const void *vp2)
536 const struct point *p1 = vp1;
537 const struct point *p2 = vp2;
540 res = p1->prob - p2->prob;
542 res = p1->delay - p2->delay;
551 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
554 load_extra_delays(const char *filename, struct dn_pipe *p)
556 char line[ED_MAX_LINE_LEN];
563 char profile_name[ED_MAX_NAME_LEN];
564 int delay_first = -1;
566 struct point points[ED_MAX_SAMPLES_NO];
569 profile_name[0] = '\0';
570 f = fopen(filename, "r");
572 err(EX_UNAVAILABLE, "fopen: %s", filename);
574 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
575 char *s, *cur = line, *name = NULL, *arg = NULL;
581 s = strsep(&cur, ED_SEPARATORS);
582 if (s == NULL || *s == '#')
587 errx(ED_EFMT("too many arguments"));
593 if (name == NULL) /* empty line */
596 errx(ED_EFMT("missing arg for %s"), name);
598 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
600 errx(ED_EFMT("duplicate ``samples'' line"));
602 errx(ED_EFMT("invalid number of samples"));
604 if (samples>ED_MAX_SAMPLES_NO)
605 errx(ED_EFMT("too many samples, maximum is %d"),
608 } else if (!strcasecmp(name, ED_TOK_BW)) {
609 read_bandwidth(arg, &p->bandwidth, p->if_name, sizeof(p->if_name));
610 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
612 errx(ED_EFMT("duplicated token: %s"), name);
613 if (!is_valid_number(arg))
614 errx(ED_EFMT("invalid %s"), arg);
617 errx(ED_EFMT("%s greater than 1.0"), name);
619 } else if (!strcasecmp(name, ED_TOK_NAME)) {
620 if (profile_name[0] != '\0')
621 errx(ED_EFMT("duplicated token: %s"), name);
622 strncpy(profile_name, arg, sizeof(profile_name) - 1);
623 profile_name[sizeof(profile_name)-1] = '\0';
625 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
627 errx(ED_EFMT("duplicated token: %s"), name);
630 } else if (!strcasecmp(name, ED_TOK_PROB)) {
632 errx(ED_EFMT("duplicated token: %s"), name);
635 } else if (do_points) {
636 if (!is_valid_number(name) || !is_valid_number(arg))
637 errx(ED_EFMT("invalid point found"));
639 points[points_no].delay = atof(name);
640 points[points_no].prob = atof(arg);
642 points[points_no].delay = atof(arg);
643 points[points_no].prob = atof(name);
645 if (points[points_no].prob > 1.0)
646 errx(ED_EFMT("probability greater than 1.0"));
649 errx(ED_EFMT("unrecognised command '%s'"), name);
654 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
659 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
663 /* make sure that there are enough points. */
664 if (points_no < ED_MIN_SAMPLES_NO)
665 errx(ED_EFMT("too few samples, need at least %d"),
668 qsort(points, points_no, sizeof(struct point), compare_points);
671 for (i = 0; i<points_no-1; ++i) {
672 double y1 = points[i].prob * samples;
673 double x1 = points[i].delay;
674 double y2 = points[i+1].prob * samples;
675 double x2 = points[i+1].delay;
681 for (; index<stop; ++index)
682 p->samples[index] = x1;
684 double m = (y2-y1)/(x2-x1);
685 double c = y1 - m*x1;
686 for (; index<stop ; ++index)
687 p->samples[index] = (index - c)/m;
690 p->samples_no = samples;
691 p->loss_level = loss * samples;
692 strncpy(p->name, profile_name, sizeof(p->name));
696 ipfw_config_pipe(int ac, char **av)
698 int samples[ED_MAX_SAMPLES_NO];
704 memset(&p, 0, sizeof p);
709 if (ac && isdigit(**av)) {
710 i = atoi(*av); av++; ac--;
718 int tok = match_token(dummynet_params, *av);
723 p.fs.flags_fs |= DN_NOERROR;
727 NEED1("plr needs argument 0..1\n");
728 d = strtod(av[0], NULL);
733 p.fs.plr = (int)(d*0x7fffffff);
738 NEED1("queue needs queue size\n");
740 p.fs.qsize = strtoul(av[0], &end, 0);
741 if (*end == 'K' || *end == 'k') {
742 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
744 } else if (*end == 'B' ||
745 _substrcmp2(end, "by", "bytes") == 0) {
746 p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
752 NEED1("buckets needs argument\n");
753 p.fs.rq_size = strtoul(av[0], NULL, 0);
758 NEED1("mask needs mask specifier\n");
760 * per-flow queue, mask is dst_ip, dst_port,
761 * src_ip, src_port, proto measured in bits
765 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
769 uint32_t *p32 = NULL;
770 uint16_t *p16 = NULL;
771 uint32_t *p20 = NULL;
772 struct in6_addr *pa6 = NULL;
775 tok = match_token(dummynet_params, *av);
780 * special case, all bits significant
782 p.fs.flow_mask.dst_ip = ~0;
783 p.fs.flow_mask.src_ip = ~0;
784 p.fs.flow_mask.dst_port = ~0;
785 p.fs.flow_mask.src_port = ~0;
786 p.fs.flow_mask.proto = ~0;
787 n2mask(&(p.fs.flow_mask.dst_ip6), 128);
788 n2mask(&(p.fs.flow_mask.src_ip6), 128);
789 p.fs.flow_mask.flow_id6 = ~0;
790 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
794 p32 = &p.fs.flow_mask.dst_ip;
798 p32 = &p.fs.flow_mask.src_ip;
802 pa6 = &(p.fs.flow_mask.dst_ip6);
806 pa6 = &(p.fs.flow_mask.src_ip6);
810 p20 = &p.fs.flow_mask.flow_id6;
814 p16 = &p.fs.flow_mask.dst_port;
818 p16 = &p.fs.flow_mask.src_port;
825 ac++; av--; /* backtrack */
829 errx(EX_USAGE, "mask: value missing");
831 a = strtoul(av[0]+1, &end, 0);
833 a = (a == 32) ? ~0 : (1 << a) - 1;
835 a = strtoul(av[0], &end, 0);
838 else if (p16 != NULL) {
841 "port mask must be 16 bit");
843 } else if (p20 != NULL) {
846 "flow_id mask must be 20 bit");
848 } else if (pa6 != NULL) {
851 "in6addr invalid mask len");
857 "proto mask must be 8 bit");
858 p.fs.flow_mask.proto = (uint8_t)a;
861 p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
863 } /* end while, config masks */
869 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
870 p.fs.flags_fs |= DN_IS_RED;
872 p.fs.flags_fs |= DN_IS_GENTLE_RED;
874 * the format for parameters is w_q/min_th/max_th/max_p
876 if ((end = strsep(&av[0], "/"))) {
877 double w_q = strtod(end, NULL);
878 if (w_q > 1 || w_q <= 0)
879 errx(EX_DATAERR, "0 < w_q <= 1");
880 p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
882 if ((end = strsep(&av[0], "/"))) {
883 p.fs.min_th = strtoul(end, &end, 0);
884 if (*end == 'K' || *end == 'k')
887 if ((end = strsep(&av[0], "/"))) {
888 p.fs.max_th = strtoul(end, &end, 0);
889 if (*end == 'K' || *end == 'k')
892 if ((end = strsep(&av[0], "/"))) {
893 double max_p = strtod(end, NULL);
894 if (max_p > 1 || max_p <= 0)
895 errx(EX_DATAERR, "0 < max_p <= 1");
896 p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
902 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
906 NEED1("bw needs bandwidth or interface\n");
908 errx(EX_DATAERR, "bandwidth only valid for pipes");
909 read_bandwidth(av[0], &p.bandwidth, p.if_name, sizeof(p.if_name));
915 errx(EX_DATAERR, "delay only valid for pipes");
916 NEED1("delay needs argument 0..10000ms\n");
917 p.delay = strtoul(av[0], NULL, 0);
923 errx(EX_DATAERR,"weight only valid for queues");
924 NEED1("weight needs argument 0..100\n");
925 p.fs.weight = strtoul(av[0], &end, 0);
931 errx(EX_DATAERR,"pipe only valid for queues");
932 NEED1("pipe needs pipe_number\n");
933 p.fs.parent_nr = strtoul(av[0], &end, 0);
937 case TOK_PIPE_PROFILE:
939 errx(EX_DATAERR, "extra delay only valid for pipes");
940 NEED1("extra delay needs the file name\n");
941 p.samples = &samples[0];
942 load_extra_delays(av[0], &p);
948 errx(EX_DATAERR, "burst only valid for pipes");
949 NEED1("burst needs argument\n");
951 if (expand_number(av[0], &p.burst) < 0)
954 "burst: invalid argument");
955 if (errno || p.burst > (1ULL << 48) - 1)
957 "burst: out of range (0..2^48-1)");
962 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
965 if (co.do_pipe == 1) {
967 errx(EX_DATAERR, "pipe_nr must be > 0");
969 errx(EX_DATAERR, "delay must be < 10000");
970 } else { /* co.do_pipe == 2, queue */
971 if (p.fs.parent_nr == 0)
972 errx(EX_DATAERR, "pipe must be > 0");
973 if (p.fs.weight >100)
974 errx(EX_DATAERR, "weight must be <= 100");
977 /* check for bandwidth value */
978 if (p.bandwidth == -1) {
980 if (p.samples_no > 0)
981 errx(EX_DATAERR, "profile requires a bandwidth limit");
984 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
989 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
990 &limit, &len, NULL, 0) == -1)
992 if (p.fs.qsize > limit)
993 errx(EX_DATAERR, "queue size must be < %ldB", limit);
999 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1000 &limit, &len, NULL, 0) == -1)
1002 if (p.fs.qsize > limit)
1003 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1005 if (p.fs.flags_fs & DN_IS_RED) {
1007 int lookup_depth, avg_pkt_size;
1008 double s, idle, weight, w_q;
1009 struct clockinfo ck;
1012 if (p.fs.min_th >= p.fs.max_th)
1013 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1014 p.fs.min_th, p.fs.max_th);
1015 if (p.fs.max_th == 0)
1016 errx(EX_DATAERR, "max_th must be > 0");
1019 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1020 &lookup_depth, &len, NULL, 0) == -1)
1021 errx(1, "sysctlbyname(\"%s\")",
1022 "net.inet.ip.dummynet.red_lookup_depth");
1023 if (lookup_depth == 0)
1024 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1025 " must be greater than zero");
1028 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1029 &avg_pkt_size, &len, NULL, 0) == -1)
1031 errx(1, "sysctlbyname(\"%s\")",
1032 "net.inet.ip.dummynet.red_avg_pkt_size");
1033 if (avg_pkt_size == 0)
1035 "net.inet.ip.dummynet.red_avg_pkt_size must"
1036 " be greater than zero");
1038 len = sizeof(struct clockinfo);
1039 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
1040 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
1043 * Ticks needed for sending a medium-sized packet.
1044 * Unfortunately, when we are configuring a WF2Q+ queue, we
1045 * do not have bandwidth information, because that is stored
1046 * in the parent pipe, and also we have multiple queues
1047 * competing for it. So we set s=0, which is not very
1048 * correct. But on the other hand, why do we want RED with
1051 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1054 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1057 * max idle time (in ticks) before avg queue size becomes 0.
1058 * NOTA: (3/w_q) is approx the value x so that
1059 * (1-w_q)^x < 10^-3.
1061 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
1062 idle = s * 3. / w_q;
1063 p.fs.lookup_step = (int)idle / lookup_depth;
1064 if (!p.fs.lookup_step)
1065 p.fs.lookup_step = 1;
1067 for (t = p.fs.lookup_step; t > 1; --t)
1069 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
1071 if (p.samples_no <= 0) {
1072 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
1074 struct dn_pipe_max pm;
1075 int len = sizeof(pm);
1077 memcpy(&pm.pipe, &p, sizeof(pm.pipe));
1078 memcpy(&pm.samples, samples, sizeof(pm.samples));
1080 i = do_cmd(IP_DUMMYNET_CONFIGURE, &pm, len);
1084 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");