2 * Copyright (c) 2002-2003,2010 Luigi Rizzo
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
7 * Redistribution in binary form may occur without any restrictions.
8 * Obviously, it would be nice if you gave credit where credit is due
9 * but requiring it would be too onerous.
11 * This software is provided ``AS IS'' without any warranties of any kind.
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 /* XXX there are several sysctl leftover here */
21 #include <sys/sysctl.h>
36 #include <netinet/in.h>
37 #include <netinet/ip_fw.h>
38 #include <netinet/ip_dummynet.h>
39 #include <arpa/inet.h> /* inet_ntoa */
42 static struct _s_x dummynet_params[] = {
44 { "noerror", TOK_NOERROR },
45 { "buckets", TOK_BUCKETS },
46 { "dst-ip", TOK_DSTIP },
47 { "src-ip", TOK_SRCIP },
48 { "dst-port", TOK_DSTPORT },
49 { "src-port", TOK_SRCPORT },
50 { "proto", TOK_PROTO },
51 { "weight", TOK_WEIGHT },
53 { "maxlen", TOK_LMAX },
55 { "mask", TOK_MASK }, /* alias for both */
56 { "sched_mask", TOK_SCHED_MASK },
57 { "flow_mask", TOK_FLOW_MASK },
58 { "droptail", TOK_DROPTAIL },
62 { "bandwidth", TOK_BW },
63 { "delay", TOK_DELAY },
66 { "queue", TOK_QUEUE },
67 { "flowset", TOK_FLOWSET },
68 { "sched", TOK_SCHED },
70 { "priority", TOK_PRI },
72 { "flow-id", TOK_FLOWID},
73 { "dst-ipv6", TOK_DSTIP6},
74 { "dst-ip6", TOK_DSTIP6},
75 { "src-ipv6", TOK_SRCIP6},
76 { "src-ip6", TOK_SRCIP6},
77 { "profile", TOK_PROFILE},
78 { "burst", TOK_BURST},
79 { "dummynet-params", TOK_NULL },
80 { NULL, 0 } /* terminator */
83 #define O_NEXT(p, len) ((void *)((char *)p + len))
86 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
94 /* make room in the buffer and move the pointer forward */
96 o_next(struct dn_id **o, int len, int type)
98 struct dn_id *ret = *o;
99 oid_fill(ret, len, type, 0);
100 *o = O_NEXT(*o, len);
106 sort_q(void *arg, const void *pa, const void *pb)
108 int rev = (co.do_sort < 0);
109 int field = rev ? -co.do_sort : co.do_sort;
111 const struct dn_flow_queue *a = pa;
112 const struct dn_flow_queue *b = pb;
116 res = a->len - b->len;
119 res = a->len_bytes - b->len_bytes;
122 case 3: /* tot pkts */
123 res = a->tot_pkts - b->tot_pkts;
126 case 4: /* tot bytes */
127 res = a->tot_bytes - b->tot_bytes;
134 return (int)(rev ? res : -res);
138 /* print a mask and header for the subsequent list of flows */
140 print_mask(struct ipfw_flow_id *id)
142 if (!IS_IP6_FLOW_ID(id)) {
144 "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
145 id->extra ? "queue," : "",
147 id->src_ip, id->src_port,
148 id->dst_ip, id->dst_port);
151 printf("\n mask: %sproto: 0x%02x, flow_id: 0x%08x, ",
152 id->extra ? "queue," : "",
153 id->proto, id->flow_id6);
154 inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
155 printf("%s/0x%04x -> ", buf, id->src_port);
156 inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
157 printf("%s/0x%04x\n", buf, id->dst_port);
162 print_header(struct ipfw_flow_id *id)
164 if (!IS_IP6_FLOW_ID(id))
165 printf("BKT Prot ___Source IP/port____ "
166 "____Dest. IP/port____ "
167 "Tot_pkt/bytes Pkt/Byte Drp\n");
169 printf("BKT ___Prot___ _flow-id_ "
170 "______________Source IPv6/port_______________ "
171 "_______________Dest. IPv6/port_______________ "
172 "Tot_pkt/bytes Pkt/Byte Drp\n");
176 list_flow(struct dn_flow *ni, int *print)
179 struct protoent *pe = NULL;
181 struct ipfw_flow_id *id = &ni->fid;
184 print_header(&ni->fid);
187 pe = getprotobynumber(id->proto);
188 /* XXX: Should check for IPv4 flows */
189 printf("%3u%c", (ni->oid.id) & 0xff,
190 id->extra ? '*' : ' ');
191 if (!IS_IP6_FLOW_ID(id)) {
193 printf("%-4s ", pe->p_name);
195 printf("%4u ", id->proto);
196 ina.s_addr = htonl(id->src_ip);
198 inet_ntoa(ina), id->src_port);
199 ina.s_addr = htonl(id->dst_ip);
201 inet_ntoa(ina), id->dst_port);
203 /* Print IPv6 flows */
205 printf("%9s ", pe->p_name);
207 printf("%9u ", id->proto);
208 printf("%7d %39s/%-5d ", id->flow_id6,
209 inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
211 printf(" %39s/%-5d ",
212 inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
215 pr_u64(&ni->tot_pkts, 4);
216 pr_u64(&ni->tot_bytes, 8);
217 printf("%2u %4u %3u\n",
218 ni->length, ni->len_bytes, ni->drops);
222 print_flowset_parms(struct dn_fs *fs, char *prefix)
227 char red[90]; /* Display RED parameters */
230 if (fs->flags & DN_QSIZE_BYTES) {
232 sprintf(qs, "%d KB", l / 1024);
234 sprintf(qs, "%d B", l);
236 sprintf(qs, "%3d sl.", l);
238 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
242 if (fs->flags & DN_IS_RED) /* RED parameters */
244 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
245 (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
246 1.0 * fs->w_q / (double)(1 << SCALE_RED),
249 1.0 * fs->max_p / (double)(1 << SCALE_RED));
251 sprintf(red, "droptail");
254 printf("%s %s%s %d queues (%d buckets) %s\n",
255 prefix, qs, plr, fs->oid.id, fs->buckets, red);
258 printf("q%05d %s%s %d flows (%d buckets) sched %d "
259 "weight %d lmax %d pri %d %s\n",
260 fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
261 fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
262 if (fs->flags & DN_HAVE_MASK)
263 print_mask(&fs->flow_mask);
268 print_extra_delay_parms(struct dn_profile *p)
271 if (p->samples_no <= 0)
274 loss = p->loss_level;
275 loss /= p->samples_no;
276 printf("\t profile: name \"%s\" loss %f samples %d\n",
277 p->name, loss, p->samples_no);
289 * generic list routine. We expect objects in a specific order, i.e.
290 * PIPES AND SCHEDULERS:
291 * link; scheduler; internal flowset if any; instances
292 * we can tell a pipe from the number.
296 * link i (int queue); scheduler i; si(i) { flowsets() : queues }
299 list_pipes(struct dn_id *oid, struct dn_id *end)
301 char buf[160]; /* pending buffer */
302 int toPrint = 1; /* print header */
305 for (; oid != end; oid = O_NEXT(oid, oid->len)) {
306 if (oid->len < sizeof(*oid))
307 errx(1, "invalid oid len %d\n", oid->len);
312 printf("unrecognized object %d size %d\n", oid->type, oid->len);
314 case DN_TEXT: /* list of attached flowsets */
321 l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
324 printf(" Children flowsets: ");
325 for (i = 0; i < l; i++)
326 printf("%u ", d->p[i]);
332 printf("answer for cmd %d, len %d\n", oid->type, oid->id);
335 struct dn_sch *s = (struct dn_sch *)oid;
337 printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
339 s->name, s->flags, s->buckets, s->oid.id);
340 if (s->flags & DN_HAVE_MASK)
341 print_mask(&s->sched_mask);
346 list_flow((struct dn_flow *)oid, &toPrint);
350 struct dn_link *p = (struct dn_link *)oid;
351 double b = p->bandwidth;
355 /* This starts a new object so flush buffer */
359 sprintf(bwbuf, "unlimited ");
360 else if (b >= 1000000)
361 sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
363 sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
365 sprintf(bwbuf, "%7.3f bit/s ", b);
367 if (humanize_number(burst, sizeof(burst), p->burst,
368 "", HN_AUTOSCALE, 0) < 0 || co.verbose)
369 sprintf(burst, "%d", (int)p->burst);
370 sprintf(buf, "%05d: %s %4d ms burst %s",
371 p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
376 print_flowset_parms((struct dn_fs *)oid, buf);
380 print_extra_delay_parms((struct dn_profile *)oid);
382 flush_buf(buf); // XXX does it really go here ?
387 * Delete pipe, queue or scheduler i
390 ipfw_delete_pipe(int do_pipe, int i)
394 uintptr_t a[1]; /* add more if we want a list */
396 oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
397 cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
398 ( (do_pipe == 2) ? DN_FS : DN_SCH);
400 i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
403 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
409 * Code to parse delay profiles.
411 * Some link types introduce extra delays in the transmission
412 * of a packet, e.g. because of MAC level framing, contention on
413 * the use of the channel, MAC level retransmissions and so on.
414 * From our point of view, the channel is effectively unavailable
415 * for this extra time, which is constant or variable depending
416 * on the link type. Additionally, packets may be dropped after this
417 * time (e.g. on a wireless link after too many retransmissions).
418 * We can model the additional delay with an empirical curve
419 * that represents its distribution.
421 * cumulative probability
431 * +-------*------------------->
434 * The empirical curve may have both vertical and horizontal lines.
435 * Vertical lines represent constant delay for a range of
436 * probabilities; horizontal lines correspond to a discontinuty
437 * in the delay distribution: the link will use the largest delay
438 * for a given probability.
440 * To pass the curve to dummynet, we must store the parameters
441 * in a file as described below, and issue the command
443 * ipfw pipe <n> config ... bw XXX profile <filename> ...
445 * The file format is the following, with whitespace acting as
446 * a separator and '#' indicating the beginning a comment:
449 * the number of samples used in the internal
450 * representation (2..1024; default 100);
453 * The probability above which packets are lost.
454 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
457 * Optional a name (listed by "ipfw pipe show")
458 * to identify the distribution;
460 * "delay prob" | "prob delay"
461 * One of these two lines is mandatory and defines
462 * the format of the following lines with data points.
465 * 2 or more lines representing points in the curve,
466 * with either delay or probability first, according
467 * to the chosen format.
468 * The unit for delay is milliseconds.
470 * Data points does not need to be ordered or equal to the number
471 * specified in the "samples" line. ipfw will sort and interpolate
472 * the curve as needed.
474 * Example of a profile file:
480 0 200 # minimum overhead is 200ms
487 * Internally, we will convert the curve to a fixed number of
488 * samples, and when it is time to transmit a packet we will
489 * model the extra delay as extra bits in the packet.
493 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
494 #define ED_TOK_SAMPLES "samples"
495 #define ED_TOK_LOSS "loss-level"
496 #define ED_TOK_NAME "name"
497 #define ED_TOK_DELAY "delay"
498 #define ED_TOK_PROB "prob"
499 #define ED_TOK_BW "bw"
500 #define ED_SEPARATORS " \t\n"
501 #define ED_MIN_SAMPLES_NO 2
504 * returns 1 if s is a non-negative number, with at least one '.'
507 is_valid_number(const char *s)
509 int i, dots_found = 0;
512 for (i = 0; i<len; ++i)
513 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
519 * Take as input a string describing a bandwidth value
520 * and return the numeric bandwidth value.
521 * set clocking interface or bandwidth value
524 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
526 if (*bandwidth != -1)
527 warnx("duplicate token, override bandwidth value!");
529 if (arg[0] >= 'a' && arg[0] <= 'z') {
531 errx(1, "no if support");
533 if (namelen >= IFNAMSIZ)
534 warn("interface name truncated");
537 strncpy(if_name, arg, namelen);
538 if_name[namelen] = '\0';
540 } else { /* read bandwidth value */
544 bw = strtoul(arg, &end, 0);
545 if (*end == 'K' || *end == 'k') {
548 } else if (*end == 'M' || *end == 'm') {
553 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
554 _substrcmp2(end, "by", "bytes") == 0)
558 errx(EX_DATAERR, "bandwidth too large");
572 compare_points(const void *vp1, const void *vp2)
574 const struct point *p1 = vp1;
575 const struct point *p2 = vp2;
578 res = p1->prob - p2->prob;
580 res = p1->delay - p2->delay;
589 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
592 load_extra_delays(const char *filename, struct dn_profile *p,
593 struct dn_link *link)
595 char line[ED_MAX_LINE_LEN];
602 char profile_name[ED_MAX_NAME_LEN];
603 int delay_first = -1;
605 struct point points[ED_MAX_SAMPLES_NO];
608 /* XXX link never NULL? */
609 p->link_nr = link->link_nr;
611 profile_name[0] = '\0';
612 f = fopen(filename, "r");
614 err(EX_UNAVAILABLE, "fopen: %s", filename);
616 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
617 char *s, *cur = line, *name = NULL, *arg = NULL;
623 s = strsep(&cur, ED_SEPARATORS);
624 if (s == NULL || *s == '#')
629 errx(ED_EFMT("too many arguments"));
635 if (name == NULL) /* empty line */
638 errx(ED_EFMT("missing arg for %s"), name);
640 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
642 errx(ED_EFMT("duplicate ``samples'' line"));
644 errx(ED_EFMT("invalid number of samples"));
646 if (samples>ED_MAX_SAMPLES_NO)
647 errx(ED_EFMT("too many samples, maximum is %d"),
650 } else if (!strcasecmp(name, ED_TOK_BW)) {
652 read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
653 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
655 errx(ED_EFMT("duplicated token: %s"), name);
656 if (!is_valid_number(arg))
657 errx(ED_EFMT("invalid %s"), arg);
660 errx(ED_EFMT("%s greater than 1.0"), name);
662 } else if (!strcasecmp(name, ED_TOK_NAME)) {
663 if (profile_name[0] != '\0')
664 errx(ED_EFMT("duplicated token: %s"), name);
665 strncpy(profile_name, arg, sizeof(profile_name) - 1);
666 profile_name[sizeof(profile_name)-1] = '\0';
668 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
670 errx(ED_EFMT("duplicated token: %s"), name);
673 } else if (!strcasecmp(name, ED_TOK_PROB)) {
675 errx(ED_EFMT("duplicated token: %s"), name);
678 } else if (do_points) {
679 if (!is_valid_number(name) || !is_valid_number(arg))
680 errx(ED_EFMT("invalid point found"));
682 points[points_no].delay = atof(name);
683 points[points_no].prob = atof(arg);
685 points[points_no].delay = atof(arg);
686 points[points_no].prob = atof(name);
688 if (points[points_no].prob > 1.0)
689 errx(ED_EFMT("probability greater than 1.0"));
692 errx(ED_EFMT("unrecognised command '%s'"), name);
699 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
704 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
708 /* make sure that there are enough points. */
709 if (points_no < ED_MIN_SAMPLES_NO)
710 errx(ED_EFMT("too few samples, need at least %d"),
713 qsort(points, points_no, sizeof(struct point), compare_points);
716 for (i = 0; i<points_no-1; ++i) {
717 double y1 = points[i].prob * samples;
718 double x1 = points[i].delay;
719 double y2 = points[i+1].prob * samples;
720 double x2 = points[i+1].delay;
726 for (; ix<stop; ++ix)
729 double m = (y2-y1)/(x2-x1);
730 double c = y1 - m*x1;
731 for (; ix<stop ; ++ix)
732 p->samples[ix] = (ix - c)/m;
735 p->samples_no = samples;
736 p->loss_level = loss * samples;
737 strncpy(p->name, profile_name, sizeof(p->name));
741 * configuration of pipes, schedulers, flowsets.
742 * When we configure a new scheduler, an empty pipe is created, so:
744 * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
745 * sched N+Delta type fifo sched_mask ...
746 * pipe N+Delta <parameters>
747 * flowset N+Delta pipe N+Delta (no parameters)
748 * sched N type wf2q+ sched_mask ...
749 * pipe N <parameters>
751 * do_pipe = 2 -> flowset N config
752 * flowset N parameters
754 * do_pipe = 3 -> sched N config
755 * sched N parameters (default no pipe)
756 * optional Pipe N config ...
760 ipfw_config_pipe(int ac, char **av)
765 struct dn_id *buf, *base;
766 struct dn_sch *sch = NULL;
767 struct dn_link *p = NULL;
768 struct dn_fs *fs = NULL;
769 struct dn_profile *pf = NULL;
770 struct ipfw_flow_id *mask = NULL;
772 uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
775 * allocate space for 1 header,
776 * 1 scheduler, 1 link, 1 flowset, 1 profile
778 lmax = sizeof(struct dn_id); /* command header */
779 lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
780 sizeof(struct dn_fs) + sizeof(struct dn_profile);
784 if (ac && isdigit(**av)) {
785 i = atoi(*av); av++; ac--;
789 errx(EX_USAGE, "need a pipe/flowset/sched number");
790 base = buf = safe_calloc(1, lmax);
791 /* all commands start with a 'CONFIGURE' and a version */
792 o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
793 base->id = DN_API_VERSION;
795 switch (co.do_pipe) {
796 case 1: /* "pipe N config ..." */
797 /* Allocate space for the WF2Q+ scheduler, its link
798 * and the FIFO flowset. Set the number, but leave
799 * the scheduler subtype and other parameters to 0
800 * so the kernel will use appropriate defaults.
801 * XXX todo: add a flag to record if a parameter
802 * is actually configured.
803 * If we do a 'pipe config' mask -> sched_mask.
804 * The FIFO scheduler and link are derived from the
805 * WF2Q+ one in the kernel.
807 sch = o_next(&buf, sizeof(*sch), DN_SCH);
808 p = o_next(&buf, sizeof(*p), DN_LINK);
809 fs = o_next(&buf, sizeof(*fs), DN_FS);
812 sch->oid.subtype = 0; /* defaults to WF2Q+ */
813 mask = &sch->sched_mask;
815 buckets = &sch->buckets;
816 *flags |= DN_PIPE_CMD;
820 /* This flowset is only for the FIFO scheduler */
821 fs->fs_nr = i + 2*DN_MAX_ID;
822 fs->sched_nr = i + DN_MAX_ID;
825 case 2: /* "queue N config ... " */
826 fs = o_next(&buf, sizeof(*fs), DN_FS);
828 mask = &fs->flow_mask;
830 buckets = &fs->buckets;
833 case 3: /* "sched N config ..." */
834 sch = o_next(&buf, sizeof(*sch), DN_SCH);
835 fs = o_next(&buf, sizeof(*fs), DN_FS);
837 mask = &sch->sched_mask;
839 buckets = &sch->buckets;
840 /* fs is used only with !MULTIQUEUE schedulers */
841 fs->fs_nr = i + DN_MAX_ID;
845 /* set to -1 those fields for which we want to reuse existing
846 * values from the kernel.
847 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
848 * XXX todo: support reuse of the mask.
852 for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
856 int tok = match_token(dummynet_params, *av);
861 NEED(fs, "noerror is only for pipes");
862 fs->flags |= DN_NOERROR;
866 NEED(fs, "plr is only for pipes");
867 NEED1("plr needs argument 0..1\n");
868 d = strtod(av[0], NULL);
873 fs->plr = (int)(d*0x7fffffff);
878 NEED(fs, "queue is only for pipes or flowsets");
879 NEED1("queue needs queue size\n");
881 fs->qsize = strtoul(av[0], &end, 0);
882 if (*end == 'K' || *end == 'k') {
883 fs->flags |= DN_QSIZE_BYTES;
885 } else if (*end == 'B' ||
886 _substrcmp2(end, "by", "bytes") == 0) {
887 fs->flags |= DN_QSIZE_BYTES;
893 NEED(fs, "buckets is only for pipes or flowsets");
894 NEED1("buckets needs argument\n");
895 *buckets = strtoul(av[0], NULL, 0);
902 NEED(mask, "tok_mask");
903 NEED1("mask needs mask specifier\n");
905 * per-flow queue, mask is dst_ip, dst_port,
906 * src_ip, src_port, proto measured in bits
910 bzero(mask, sizeof(*mask));
914 uint32_t *p32 = NULL;
915 uint16_t *p16 = NULL;
916 uint32_t *p20 = NULL;
917 struct in6_addr *pa6 = NULL;
920 tok = match_token(dummynet_params, *av);
925 * special case, all bits significant
926 * except 'extra' (the queue number)
933 n2mask(&mask->dst_ip6, 128);
934 n2mask(&mask->src_ip6, 128);
936 *flags |= DN_HAVE_MASK;
941 *flags |= DN_HAVE_MASK;
956 pa6 = &mask->dst_ip6;
961 pa6 = &mask->src_ip6;
966 p20 = &mask->flow_id6;
970 p16 = &mask->dst_port;
974 p16 = &mask->src_port;
981 ac++; av--; /* backtrack */
985 errx(EX_USAGE, "mask: value missing");
987 a = strtoul(av[0]+1, &end, 0);
989 a = (a == 32) ? ~0 : (1 << a) - 1;
991 a = strtoul(av[0], &end, 0);
994 else if (p16 != NULL) {
997 "port mask must be 16 bit");
999 } else if (p20 != NULL) {
1002 "flow_id mask must be 20 bit");
1004 } else if (pa6 != NULL) {
1007 "in6addr invalid mask len");
1013 "proto mask must be 8 bit");
1014 mask->proto = (uint8_t)a;
1017 *flags |= DN_HAVE_MASK;
1019 } /* end while, config masks */
1025 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1026 fs->flags |= DN_IS_RED;
1027 if (tok == TOK_GRED)
1028 fs->flags |= DN_IS_GENTLE_RED;
1030 * the format for parameters is w_q/min_th/max_th/max_p
1032 if ((end = strsep(&av[0], "/"))) {
1033 double w_q = strtod(end, NULL);
1034 if (w_q > 1 || w_q <= 0)
1035 errx(EX_DATAERR, "0 < w_q <= 1");
1036 fs->w_q = (int) (w_q * (1 << SCALE_RED));
1038 if ((end = strsep(&av[0], "/"))) {
1039 fs->min_th = strtoul(end, &end, 0);
1040 if (*end == 'K' || *end == 'k')
1043 if ((end = strsep(&av[0], "/"))) {
1044 fs->max_th = strtoul(end, &end, 0);
1045 if (*end == 'K' || *end == 'k')
1048 if ((end = strsep(&av[0], "/"))) {
1049 double max_p = strtod(end, NULL);
1050 if (max_p > 1 || max_p <= 0)
1051 errx(EX_DATAERR, "0 < max_p <= 1");
1052 fs->max_p = (int)(max_p * (1 << SCALE_RED));
1058 NEED(fs, "droptail is only for flowsets");
1059 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1063 NEED(p, "bw is only for links");
1064 NEED1("bw needs bandwidth or interface\n");
1065 read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1070 NEED(p, "delay is only for links");
1071 NEED1("delay needs argument 0..10000ms\n");
1072 p->delay = strtoul(av[0], NULL, 0);
1078 NEED(sch, "type is only for schedulers");
1079 NEED1("type needs a string");
1081 if (l == 0 || l > 15)
1082 errx(1, "type %s too long\n", av[0]);
1083 strcpy(sch->name, av[0]);
1084 sch->oid.subtype = 0; /* use string */
1090 NEED(fs, "weight is only for flowsets");
1091 NEED1("weight needs argument\n");
1092 fs->par[0] = strtol(av[0], &end, 0);
1097 NEED(fs, "lmax is only for flowsets");
1098 NEED1("lmax needs argument\n");
1099 fs->par[1] = strtol(av[0], &end, 0);
1104 NEED(fs, "priority is only for flowsets");
1105 NEED1("priority needs argument\n");
1106 fs->par[2] = strtol(av[0], &end, 0);
1112 NEED(fs, "pipe/sched");
1113 NEED1("pipe/link/sched needs number\n");
1114 fs->sched_nr = strtoul(av[0], &end, 0);
1119 NEED((!pf), "profile already set");
1122 NEED1("extra delay needs the file name\n");
1123 pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1124 load_extra_delays(av[0], pf, p); //XXX can't fail?
1131 NEED1("burst needs argument\n");
1133 if (expand_number(av[0], &p->burst) < 0)
1134 if (errno != ERANGE)
1136 "burst: invalid argument");
1137 if (errno || p->burst > (1ULL << 48) - 1)
1139 "burst: out of range (0..2^48-1)");
1144 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1148 /* check validity of parameters */
1150 if (p->delay > 10000)
1151 errx(EX_DATAERR, "delay must be < 10000");
1152 if (p->bandwidth == -1)
1156 /* XXX accept a 0 scheduler to keep the default */
1157 if (fs->flags & DN_QSIZE_BYTES) {
1161 len = sizeof(limit);
1162 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1163 &limit, &len, NULL, 0) == -1)
1165 if (fs->qsize > limit)
1166 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1171 len = sizeof(limit);
1172 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1173 &limit, &len, NULL, 0) == -1)
1175 if (fs->qsize > limit)
1176 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1179 if (fs->flags & DN_IS_RED) {
1181 int lookup_depth, avg_pkt_size;
1184 if (fs->min_th >= fs->max_th)
1185 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1186 fs->min_th, fs->max_th);
1187 if (fs->max_th == 0)
1188 errx(EX_DATAERR, "max_th must be > 0");
1191 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1192 &lookup_depth, &len, NULL, 0) == -1)
1194 if (lookup_depth == 0)
1195 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1196 " must be greater than zero");
1199 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1200 &avg_pkt_size, &len, NULL, 0) == -1)
1203 if (avg_pkt_size == 0)
1205 "net.inet.ip.dummynet.red_avg_pkt_size must"
1206 " be greater than zero");
1209 * Ticks needed for sending a medium-sized packet.
1210 * Unfortunately, when we are configuring a WF2Q+ queue, we
1211 * do not have bandwidth information, because that is stored
1212 * in the parent pipe, and also we have multiple queues
1213 * competing for it. So we set s=0, which is not very
1214 * correct. But on the other hand, why do we want RED with
1218 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1221 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1224 * max idle time (in ticks) before avg queue size becomes 0.
1225 * NOTA: (3/w_q) is approx the value x so that
1226 * (1-w_q)^x < 10^-3.
1228 w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1229 #if 0 // go in kernel
1230 idle = s * 3. / w_q;
1231 fs->lookup_step = (int)idle / lookup_depth;
1232 if (!fs->lookup_step)
1233 fs->lookup_step = 1;
1235 for (t = fs->lookup_step; t > 1; --t)
1237 fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1242 i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1245 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1249 dummynet_flush(void)
1252 oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1253 do_cmd(IP_DUMMYNET3, &oid, oid.len);
1256 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1257 * Returns the number of ranges, and possibly stores them
1258 * in the array v of size len.
1261 parse_range(int ac, char *av[], uint32_t *v, int len)
1267 if (v == NULL || len < 2) {
1272 for (s = *av; s != NULL; av++, ac--) {
1273 v[0] = strtoul(s, &endptr, 10);
1274 v[1] = (*endptr != '-') ? v[0] :
1275 strtoul(endptr+1, &endptr, 10);
1276 if (*endptr == '\0') { /* prepare for next round */
1277 s = (ac > 0) ? *(av+1) : NULL;
1279 if (*endptr != ',') {
1280 warn("invalid number: %s", s);
1284 /* continue processing from here */
1290 v[1] < 0 || v[1] >= DN_MAX_ID-1 ||
1291 v[0] < 0 || v[1] >= DN_MAX_ID-1) {
1292 continue; /* invalid entry */
1295 /* translate if 'pipe list' */
1296 if (co.do_pipe == 1) {
1300 v = (n*2 < len) ? v + 2 : base;
1305 /* main entry point for dummynet list functions. co.do_pipe indicates
1306 * which function we want to support.
1307 * av may contain filtering arguments, either individual entries
1308 * or ranges, or lists (space or commas are valid separators).
1309 * Format for a range can be n1-n2 or n3 n4 n5 ...
1310 * In a range n1 must be <= n2, otherwise the range is ignored.
1311 * A number 'n4' is translate in a range 'n4-n4'
1312 * All number must be > 0 and < DN_MAX_ID-1
1315 dummynet_list(int ac, char *av[], int show_counters)
1317 struct dn_id *oid, *x = NULL;
1319 int n; /* # of ranges */
1321 int max_size; /* largest obj passed up */
1324 av++; /* skip 'list' | 'show' word */
1326 n = parse_range(ac, av, NULL, 0); /* Count # of ranges. */
1328 /* Allocate space to store ranges */
1329 l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1330 oid = safe_calloc(1, l);
1331 oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1333 if (n > 0) /* store ranges in idx */
1334 parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1336 * Compute the size of the largest object returned. If the
1337 * response leaves at least this much spare space in the
1338 * buffer, then surely the response is complete; otherwise
1339 * there might be a risk of truncation and we will need to
1340 * retry with a larger buffer.
1341 * XXX don't bother with smaller structs.
1343 max_size = sizeof(struct dn_fs);
1344 if (max_size < sizeof(struct dn_sch))
1345 max_size = sizeof(struct dn_sch);
1346 if (max_size < sizeof(struct dn_flow))
1347 max_size = sizeof(struct dn_flow);
1349 switch (co.do_pipe) {
1351 oid->subtype = DN_LINK; /* list pipe */
1354 oid->subtype = DN_FS; /* list queue */
1357 oid->subtype = DN_SCH; /* list sched */
1362 * Ask the kernel an estimate of the required space (result
1363 * in oid.id), unless we are requesting a subset of objects,
1364 * in which case the kernel does not give an exact answer.
1365 * In any case, space might grow in the meantime due to the
1366 * creation of new queues, so we must be prepared to retry.
1371 ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1372 if (ret != 0 || oid->id <= sizeof(*oid))
1374 buflen = oid->id + max_size;
1375 oid->len = sizeof(*oid); /* restore */
1377 /* Try a few times, until the buffer fits */
1378 for (i = 0; i < 20; i++) {
1380 x = safe_realloc(x, l);
1381 bcopy(oid, x, oid->len);
1382 ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1383 if (ret != 0 || x->id <= sizeof(*oid))
1384 goto done; /* no response */
1385 if (l + max_size <= buflen)
1387 buflen *= 2; /* double for next attempt */
1389 list_pipes(x, O_NEXT(x, l));