2 * Codel/FQ_Codel and PIE/FQ_PIE Code:
3 * Copyright (C) 2016 Centre for Advanced Internet Architectures,
4 * Swinburne University of Technology, Melbourne, Australia.
5 * Portions of this code were made possible in part by a gift from
6 * The Comcast Innovation Fund.
7 * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
9 * Copyright (c) 2002-2003,2010 Luigi Rizzo
11 * Redistribution and use in source forms, with and without modification,
12 * are permitted provided that this entire comment appears intact.
14 * Redistribution in binary form may occur without any restrictions.
15 * Obviously, it would be nice if you gave credit where credit is due
16 * but requiring it would be too onerous.
18 * This software is provided ``AS IS'' without any warranties of any kind.
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 /* XXX there are several sysctl leftover here */
29 #include <sys/sysctl.h>
48 #include <netinet/in.h>
49 #include <netinet/ip_fw.h>
50 #include <netinet/ip_dummynet.h>
51 #include <arpa/inet.h> /* inet_ntoa */
54 static struct _s_x dummynet_params[] = {
56 { "noerror", TOK_NOERROR },
57 { "buckets", TOK_BUCKETS },
58 { "dst-ip", TOK_DSTIP },
59 { "src-ip", TOK_SRCIP },
60 { "dst-port", TOK_DSTPORT },
61 { "src-port", TOK_SRCPORT },
62 { "proto", TOK_PROTO },
63 { "weight", TOK_WEIGHT },
65 { "maxlen", TOK_LMAX },
67 { "mask", TOK_MASK }, /* alias for both */
68 { "sched_mask", TOK_SCHED_MASK },
69 { "flow_mask", TOK_FLOW_MASK },
70 { "droptail", TOK_DROPTAIL },
75 { "codel", TOK_CODEL}, /* Codel AQM */
76 { "fq_codel", TOK_FQ_CODEL}, /* FQ-Codel */
77 { "pie", TOK_PIE}, /* PIE AQM */
78 { "fq_pie", TOK_FQ_PIE}, /* FQ-PIE */
81 { "bandwidth", TOK_BW },
82 { "delay", TOK_DELAY },
85 { "queue", TOK_QUEUE },
86 { "flowset", TOK_FLOWSET },
87 { "sched", TOK_SCHED },
89 { "priority", TOK_PRI },
91 { "flow-id", TOK_FLOWID},
92 { "dst-ipv6", TOK_DSTIP6},
93 { "dst-ip6", TOK_DSTIP6},
94 { "src-ipv6", TOK_SRCIP6},
95 { "src-ip6", TOK_SRCIP6},
96 { "profile", TOK_PROFILE},
97 { "burst", TOK_BURST},
98 { "dummynet-params", TOK_NULL },
99 { NULL, 0 } /* terminator */
103 /* AQM/extra sched parameters tokens*/
104 static struct _s_x aqm_params[] = {
105 { "target", TOK_TARGET},
106 { "interval", TOK_INTERVAL},
107 { "limit", TOK_LIMIT},
108 { "flows", TOK_FLOWS},
109 { "quantum", TOK_QUANTUM},
111 { "noecn", TOK_NO_ECN},
112 { "tupdate", TOK_TUPDATE},
113 { "max_burst", TOK_MAX_BURST},
114 { "max_ecnth", TOK_MAX_ECNTH},
115 { "alpha", TOK_ALPHA},
117 { "capdrop", TOK_CAPDROP},
118 { "nocapdrop", TOK_NO_CAPDROP},
119 { "onoff", TOK_ONOFF},
122 { "derand", TOK_DERAND},
123 { "noderand", TOK_NO_DERAND},
124 { NULL, 0 } /* terminator */
128 #define O_NEXT(p, len) ((void *)((char *)p + len))
131 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
139 /* make room in the buffer and move the pointer forward */
141 o_next(struct dn_id **o, int len, int type)
143 struct dn_id *ret = *o;
144 oid_fill(ret, len, type, 0);
145 *o = O_NEXT(*o, len);
153 CODEL_ECN_ENABLED = 1
156 /* PIE flags, from PIE kernel module */
159 PIE_CAPDROP_ENABLED = 2,
160 PIE_ON_OFF_MODE_ENABLED = 4,
161 PIE_DEPRATEEST_ENABLED = 8,
162 PIE_DERAND_ENABLED = 16
165 #define PIE_FIX_POINT_BITS 13
166 #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
168 /* integer to time */
170 us_to_time(int t,char *strt)
175 sprintf(strt,"%d", t);
177 sprintf(strt,"%dus", t);
178 else if (t < 1000000)
179 sprintf(strt,"%gms", (float) t / 1000);
181 sprintf(strt,"%gfs", (float) t / 1000000);
185 * returns -1 if s is not a valid time, otherwise, return time in us
188 time_to_us(const char *s)
192 char strt[16]="", stru[16]="";
196 for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
212 if (!strcasecmp(stru, "us"))
214 if (!strcasecmp(stru, "ms"))
215 return (strtod(strt, NULL) * 1000);
216 if (!strcasecmp(stru, "s"))
217 return (strtod(strt, NULL)*1000000);
223 /* Get AQM or scheduler extra parameters */
225 get_extra_parms(uint32_t nr, char *out, int subtype)
227 struct dn_extra_parms *ep;
229 char strt1[15], strt2[15], strt3[15];
232 /* prepare the request */
233 l = sizeof(struct dn_extra_parms);
234 ep = safe_calloc(1, l);
235 memset(ep, 0, sizeof(*ep));
238 oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
240 ep->oid.subtype = subtype;
243 ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
246 errx(EX_DATAERR, "Error getting extra parameters\n");
251 if( !strcasecmp(ep->name, "codel")) {
252 us_to_time(ep->par[0], strt1);
253 us_to_time(ep->par[1], strt2);
254 l = sprintf(out, " AQM CoDel target %s interval %s",
256 if (ep->par[2] & CODEL_ECN_ENABLED)
257 l = sprintf(out + l, " ECN");
259 l += sprintf(out + l, " NoECN");
260 } else if( !strcasecmp(ep->name, "pie")) {
261 us_to_time(ep->par[0], strt1);
262 us_to_time(ep->par[1], strt2);
263 us_to_time(ep->par[2], strt3);
264 l = sprintf(out, " AQM type PIE target %s tupdate %s alpha "
265 "%g beta %g max_burst %s max_ecnth %.3g",
268 ep->par[4] / (float) PIE_SCALE,
269 ep->par[5] / (float) PIE_SCALE,
271 ep->par[3] / (float) PIE_SCALE
274 if (ep->par[6] & PIE_ECN_ENABLED)
275 l += sprintf(out + l, " ECN");
277 l += sprintf(out + l, " NoECN");
278 if (ep->par[6] & PIE_CAPDROP_ENABLED)
279 l += sprintf(out + l, " CapDrop");
281 l += sprintf(out + l, " NoCapDrop");
282 if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
283 l += sprintf(out + l, " OnOff");
284 if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
285 l += sprintf(out + l, " DRE");
287 l += sprintf(out + l, " TS");
288 if (ep->par[6] & PIE_DERAND_ENABLED)
289 l += sprintf(out + l, " Derand");
291 l += sprintf(out + l, " NoDerand");
296 if (!strcasecmp(ep->name,"FQ_CODEL")) {
297 us_to_time(ep->par[0], strt1);
298 us_to_time(ep->par[1], strt2);
299 l = sprintf(out," FQ_CODEL target %s interval %s"
300 " quantum %jd limit %jd flows %jd",
302 (intmax_t) ep->par[3],
303 (intmax_t) ep->par[4],
304 (intmax_t) ep->par[5]
306 if (ep->par[2] & CODEL_ECN_ENABLED)
307 l += sprintf(out + l, " ECN");
309 l += sprintf(out + l, " NoECN");
310 l += sprintf(out + l, "\n");
311 } else if (!strcasecmp(ep->name,"FQ_PIE")) {
312 us_to_time(ep->par[0], strt1);
313 us_to_time(ep->par[1], strt2);
314 us_to_time(ep->par[2], strt3);
315 l = sprintf(out, " FQ_PIE target %s tupdate %s alpha "
316 "%g beta %g max_burst %s max_ecnth %.3g"
317 " quantum %jd limit %jd flows %jd",
320 ep->par[4] / (float) PIE_SCALE,
321 ep->par[5] / (float) PIE_SCALE,
323 ep->par[3] / (float) PIE_SCALE,
324 (intmax_t) ep->par[7],
325 (intmax_t) ep->par[8],
326 (intmax_t) ep->par[9]
329 if (ep->par[6] & PIE_ECN_ENABLED)
330 l += sprintf(out + l, " ECN");
332 l += sprintf(out + l, " NoECN");
333 if (ep->par[6] & PIE_CAPDROP_ENABLED)
334 l += sprintf(out + l, " CapDrop");
336 l += sprintf(out + l, " NoCapDrop");
337 if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
338 l += sprintf(out + l, " OnOff");
339 if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
340 l += sprintf(out + l, " DRE");
342 l += sprintf(out + l, " TS");
343 if (ep->par[6] & PIE_DERAND_ENABLED)
344 l += sprintf(out + l, " Derand");
346 l += sprintf(out + l, " NoDerand");
347 l += sprintf(out + l, "\n");
359 sort_q(void *arg, const void *pa, const void *pb)
361 int rev = (co.do_sort < 0);
362 int field = rev ? -co.do_sort : co.do_sort;
364 const struct dn_flow_queue *a = pa;
365 const struct dn_flow_queue *b = pb;
369 res = a->len - b->len;
372 res = a->len_bytes - b->len_bytes;
375 case 3: /* tot pkts */
376 res = a->tot_pkts - b->tot_pkts;
379 case 4: /* tot bytes */
380 res = a->tot_bytes - b->tot_bytes;
387 return (int)(rev ? res : -res);
391 /* print a mask and header for the subsequent list of flows */
393 print_mask(struct ipfw_flow_id *id)
395 if (!IS_IP6_FLOW_ID(id)) {
397 "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
398 id->extra ? "queue," : "",
400 id->src_ip, id->src_port,
401 id->dst_ip, id->dst_port);
404 printf("\n mask: %sproto: 0x%02x, flow_id: 0x%08x, ",
405 id->extra ? "queue," : "",
406 id->proto, id->flow_id6);
407 inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
408 printf("%s/0x%04x -> ", buf, id->src_port);
409 inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
410 printf("%s/0x%04x\n", buf, id->dst_port);
415 print_header(struct ipfw_flow_id *id)
417 if (!IS_IP6_FLOW_ID(id))
418 printf("BKT Prot ___Source IP/port____ "
419 "____Dest. IP/port____ "
420 "Tot_pkt/bytes Pkt/Byte Drp\n");
422 printf("BKT ___Prot___ _flow-id_ "
423 "______________Source IPv6/port_______________ "
424 "_______________Dest. IPv6/port_______________ "
425 "Tot_pkt/bytes Pkt/Byte Drp\n");
429 list_flow(struct buf_pr *bp, struct dn_flow *ni)
432 struct protoent *pe = NULL;
434 struct ipfw_flow_id *id = &ni->fid;
436 pe = getprotobynumber(id->proto);
437 /* XXX: Should check for IPv4 flows */
438 bprintf(bp, "%3u%c", (ni->oid.id) & 0xff,
439 id->extra ? '*' : ' ');
440 if (!IS_IP6_FLOW_ID(id)) {
442 bprintf(bp, "%-4s ", pe->p_name);
444 bprintf(bp, "%4u ", id->proto);
445 ina.s_addr = htonl(id->src_ip);
446 bprintf(bp, "%15s/%-5d ",
447 inet_ntoa(ina), id->src_port);
448 ina.s_addr = htonl(id->dst_ip);
449 bprintf(bp, "%15s/%-5d ",
450 inet_ntoa(ina), id->dst_port);
452 /* Print IPv6 flows */
454 bprintf(bp, "%9s ", pe->p_name);
456 bprintf(bp, "%9u ", id->proto);
457 bprintf(bp, "%7d %39s/%-5d ", id->flow_id6,
458 inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
460 bprintf(bp, " %39s/%-5d ",
461 inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
464 pr_u64(bp, &ni->tot_pkts, 4);
465 pr_u64(bp, &ni->tot_bytes, 8);
466 bprintf(bp, "%2u %4u %3u",
467 ni->length, ni->len_bytes, ni->drops);
471 print_flowset_parms(struct dn_fs *fs, char *prefix)
476 char red[200]; /* Display RED parameters */
479 if (fs->flags & DN_QSIZE_BYTES) {
481 sprintf(qs, "%d KB", l / 1024);
483 sprintf(qs, "%d B", l);
485 sprintf(qs, "%3d sl.", l);
487 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
491 if (fs->flags & DN_IS_RED) { /* RED parameters */
493 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
494 (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
495 1.0 * fs->w_q / (double)(1 << SCALE_RED),
498 1.0 * fs->max_p / (double)(1 << SCALE_RED));
499 if (fs->flags & DN_IS_ECN)
500 strncat(red, " (ecn)", 6);
502 /* get AQM parameters */
503 } else if (fs->flags & DN_IS_AQM) {
504 get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
507 sprintf(red, "droptail");
510 printf("%s %s%s %d queues (%d buckets) %s\n",
511 prefix, qs, plr, fs->oid.id, fs->buckets, red);
514 printf("q%05d %s%s %d flows (%d buckets) sched %d "
515 "weight %d lmax %d pri %d %s\n",
516 fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
517 fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
518 if (fs->flags & DN_HAVE_MASK)
519 print_mask(&fs->flow_mask);
524 print_extra_delay_parms(struct dn_profile *p)
527 if (p->samples_no <= 0)
530 loss = p->loss_level;
531 loss /= p->samples_no;
532 printf("\t profile: name \"%s\" loss %f samples %d\n",
533 p->name, loss, p->samples_no);
545 * generic list routine. We expect objects in a specific order, i.e.
546 * PIPES AND SCHEDULERS:
547 * link; scheduler; internal flowset if any; instances
548 * we can tell a pipe from the number.
552 * link i (int queue); scheduler i; si(i) { flowsets() : queues }
555 list_pipes(struct dn_id *oid, struct dn_id *end)
557 char buf[160]; /* pending buffer */
558 int toPrint = 1; /* print header */
563 for (; oid != end; oid = O_NEXT(oid, oid->len)) {
564 if (oid->len < sizeof(*oid))
565 errx(1, "invalid oid len %d\n", oid->len);
570 printf("unrecognized object %d size %d\n", oid->type, oid->len);
572 case DN_TEXT: /* list of attached flowsets */
579 l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
582 printf(" Children flowsets: ");
583 for (i = 0; i < l; i++)
584 printf("%u ", d->p[i]);
590 printf("answer for cmd %d, len %d\n", oid->type, oid->id);
593 struct dn_sch *s = (struct dn_sch *)oid;
595 printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
597 s->name, s->flags, s->buckets, s->oid.id);
600 get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
603 if (s->flags & DN_HAVE_MASK)
604 print_mask(&s->sched_mask);
610 print_header(&((struct dn_flow *)oid)->fid);
613 list_flow(&bp, (struct dn_flow *)oid);
614 printf("%s\n", bp.buf);
619 struct dn_link *p = (struct dn_link *)oid;
620 double b = p->bandwidth;
624 /* This starts a new object so flush buffer */
628 sprintf(bwbuf, "unlimited ");
629 else if (b >= 1000000000)
630 sprintf(bwbuf, "%7.3f Gbit/s", b/1000000000);
631 else if (b >= 1000000)
632 sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
634 sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
636 sprintf(bwbuf, "%7.3f bit/s ", b);
638 if (humanize_number(burst, sizeof(burst), p->burst,
639 "", HN_AUTOSCALE, 0) < 0 || co.verbose)
640 sprintf(burst, "%d", (int)p->burst);
641 sprintf(buf, "%05d: %s %4d ms burst %s",
642 p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
647 print_flowset_parms((struct dn_fs *)oid, buf);
651 print_extra_delay_parms((struct dn_profile *)oid);
653 flush_buf(buf); // XXX does it really go here ?
660 * Delete pipe, queue or scheduler i
663 ipfw_delete_pipe(int do_pipe, int i)
667 uintptr_t a[1]; /* add more if we want a list */
669 oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
670 cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
671 ( (do_pipe == 2) ? DN_FS : DN_SCH);
673 i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
676 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
682 * Code to parse delay profiles.
684 * Some link types introduce extra delays in the transmission
685 * of a packet, e.g. because of MAC level framing, contention on
686 * the use of the channel, MAC level retransmissions and so on.
687 * From our point of view, the channel is effectively unavailable
688 * for this extra time, which is constant or variable depending
689 * on the link type. Additionally, packets may be dropped after this
690 * time (e.g. on a wireless link after too many retransmissions).
691 * We can model the additional delay with an empirical curve
692 * that represents its distribution.
694 * cumulative probability
704 * +-------*------------------->
707 * The empirical curve may have both vertical and horizontal lines.
708 * Vertical lines represent constant delay for a range of
709 * probabilities; horizontal lines correspond to a discontinuty
710 * in the delay distribution: the link will use the largest delay
711 * for a given probability.
713 * To pass the curve to dummynet, we must store the parameters
714 * in a file as described below, and issue the command
716 * ipfw pipe <n> config ... bw XXX profile <filename> ...
718 * The file format is the following, with whitespace acting as
719 * a separator and '#' indicating the beginning a comment:
722 * the number of samples used in the internal
723 * representation (2..1024; default 100);
726 * The probability above which packets are lost.
727 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
730 * Optional a name (listed by "ipfw pipe show")
731 * to identify the distribution;
733 * "delay prob" | "prob delay"
734 * One of these two lines is mandatory and defines
735 * the format of the following lines with data points.
738 * 2 or more lines representing points in the curve,
739 * with either delay or probability first, according
740 * to the chosen format.
741 * The unit for delay is milliseconds.
743 * Data points does not need to be ordered or equal to the number
744 * specified in the "samples" line. ipfw will sort and interpolate
745 * the curve as needed.
747 * Example of a profile file:
753 0 200 # minimum overhead is 200ms
760 * Internally, we will convert the curve to a fixed number of
761 * samples, and when it is time to transmit a packet we will
762 * model the extra delay as extra bits in the packet.
766 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
767 #define ED_TOK_SAMPLES "samples"
768 #define ED_TOK_LOSS "loss-level"
769 #define ED_TOK_NAME "name"
770 #define ED_TOK_DELAY "delay"
771 #define ED_TOK_PROB "prob"
772 #define ED_TOK_BW "bw"
773 #define ED_SEPARATORS " \t\n"
774 #define ED_MIN_SAMPLES_NO 2
777 * returns 1 if s is a non-negative number, with at least one '.'
780 is_valid_number(const char *s)
782 int i, dots_found = 0;
785 for (i = 0; i<len; ++i)
786 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
792 * Take as input a string describing a bandwidth value
793 * and return the numeric bandwidth value.
794 * set clocking interface or bandwidth value
797 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen)
799 if (*bandwidth != -1)
800 warnx("duplicate token, override bandwidth value!");
802 if (arg[0] >= 'a' && arg[0] <= 'z') {
804 errx(1, "no if support");
806 if (namelen >= IFNAMSIZ)
807 warn("interface name truncated");
810 strlcpy(if_name, arg, namelen);
812 } else { /* read bandwidth value */
816 bw = strtoul(arg, &end, 0);
817 if (*end == 'K' || *end == 'k') {
820 } else if (*end == 'M' || *end == 'm') {
823 } else if (*end == 'G' || *end == 'g') {
828 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
829 _substrcmp2(end, "by", "bytes") == 0)
833 errx(EX_DATAERR, "bandwidth too large");
847 compare_points(const void *vp1, const void *vp2)
849 const struct point *p1 = vp1;
850 const struct point *p2 = vp2;
853 res = p1->prob - p2->prob;
855 res = p1->delay - p2->delay;
864 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
867 load_extra_delays(const char *filename, struct dn_profile *p,
868 struct dn_link *link)
870 char line[ED_MAX_LINE_LEN];
877 char profile_name[ED_MAX_NAME_LEN];
878 int delay_first = -1;
880 struct point points[ED_MAX_SAMPLES_NO];
883 /* XXX link never NULL? */
884 p->link_nr = link->link_nr;
886 profile_name[0] = '\0';
887 f = fopen(filename, "r");
889 err(EX_UNAVAILABLE, "fopen: %s", filename);
891 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
892 char *s, *cur = line, *name = NULL, *arg = NULL;
898 s = strsep(&cur, ED_SEPARATORS);
899 if (s == NULL || *s == '#')
904 errx(ED_EFMT("too many arguments"));
910 if (name == NULL) /* empty line */
913 errx(ED_EFMT("missing arg for %s"), name);
915 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
917 errx(ED_EFMT("duplicate ``samples'' line"));
919 errx(ED_EFMT("invalid number of samples"));
921 if (samples>ED_MAX_SAMPLES_NO)
922 errx(ED_EFMT("too many samples, maximum is %d"),
925 } else if (!strcasecmp(name, ED_TOK_BW)) {
927 read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
928 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
930 errx(ED_EFMT("duplicated token: %s"), name);
931 if (!is_valid_number(arg))
932 errx(ED_EFMT("invalid %s"), arg);
935 errx(ED_EFMT("%s greater than 1.0"), name);
937 } else if (!strcasecmp(name, ED_TOK_NAME)) {
938 if (profile_name[0] != '\0')
939 errx(ED_EFMT("duplicated token: %s"), name);
940 strlcpy(profile_name, arg, sizeof(profile_name));
942 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
944 errx(ED_EFMT("duplicated token: %s"), name);
947 } else if (!strcasecmp(name, ED_TOK_PROB)) {
949 errx(ED_EFMT("duplicated token: %s"), name);
952 } else if (do_points) {
953 if (!is_valid_number(name) || !is_valid_number(arg))
954 errx(ED_EFMT("invalid point found"));
956 points[points_no].delay = atof(name);
957 points[points_no].prob = atof(arg);
959 points[points_no].delay = atof(arg);
960 points[points_no].prob = atof(name);
962 if (points[points_no].prob > 1.0)
963 errx(ED_EFMT("probability greater than 1.0"));
966 errx(ED_EFMT("unrecognised command '%s'"), name);
973 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
978 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
982 /* make sure that there are enough points. */
983 if (points_no < ED_MIN_SAMPLES_NO)
984 errx(ED_EFMT("too few samples, need at least %d"),
987 qsort(points, points_no, sizeof(struct point), compare_points);
990 for (i = 0; i<points_no-1; ++i) {
991 double y1 = points[i].prob * samples;
992 double x1 = points[i].delay;
993 double y2 = points[i+1].prob * samples;
994 double x2 = points[i+1].delay;
1000 for (; ix<stop; ++ix)
1001 p->samples[ix] = x1;
1003 double m = (y2-y1)/(x2-x1);
1004 double c = y1 - m*x1;
1005 for (; ix<stop ; ++ix)
1006 p->samples[ix] = (ix - c)/m;
1009 p->samples_no = samples;
1010 p->loss_level = loss * samples;
1011 strlcpy(p->name, profile_name, sizeof(p->name));
1016 /* Parse AQM/extra scheduler parameters */
1018 process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
1023 /* use kernel defaults */
1024 for (i=0; i<DN_MAX_EXTRA_PARM; i++)
1031 * 0- target, 1- interval, 2- flags,
1033 * 3- quantum, 4- limit, 5- flows
1035 if (type==TOK_CODEL)
1038 ep->par[2] = CODEL_ECN_ENABLED;
1041 int tok = match_token(aqm_params, *av);
1045 if (*ac <= 0 || time_to_us(av[0]) < 0)
1046 errx(EX_DATAERR, "target needs time\n");
1048 ep->par[0] = time_to_us(av[0]);
1053 if (*ac <= 0 || time_to_us(av[0]) < 0)
1054 errx(EX_DATAERR, "interval needs time\n");
1056 ep->par[1] = time_to_us(av[0]);
1061 ep->par[2] = CODEL_ECN_ENABLED;
1064 ep->par[2] &= ~CODEL_ECN_ENABLED;
1066 /* Config fq_codel parameters */
1068 if (type != TOK_FQ_CODEL)
1069 errx(EX_DATAERR, "quantum is not for codel\n");
1070 if (*ac <= 0 || !is_valid_number(av[0]))
1071 errx(EX_DATAERR, "quantum needs number\n");
1073 ep->par[3]= atoi(av[0]);
1078 if (type != TOK_FQ_CODEL)
1079 errx(EX_DATAERR, "limit is not for codel, use queue instead\n");
1080 if (*ac <= 0 || !is_valid_number(av[0]))
1081 errx(EX_DATAERR, "limit needs number\n");
1083 ep->par[4] = atoi(av[0]);
1088 if (type != TOK_FQ_CODEL)
1089 errx(EX_DATAERR, "flows is not for codel\n");
1090 if (*ac <= 0 || !is_valid_number(av[0]))
1091 errx(EX_DATAERR, "flows needs number\n");
1093 ep->par[5] = atoi(av[0]);
1098 printf("%s is Invalid parameter\n", av[-1]);
1105 * 0- target , 1- tupdate, 2- max_burst,
1106 * 3- max_ecnth, 4- alpha,
1109 * 7- quantum, 8- limit, 9- flows
1112 if ( type == TOK_PIE)
1113 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
1114 | PIE_DERAND_ENABLED;
1116 /* for FQ-PIE, use TS mode */
1117 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DERAND_ENABLED
1121 int tok = match_token(aqm_params, *av);
1125 if (*ac <= 0 || time_to_us(av[0]) < 0)
1126 errx(EX_DATAERR, "target needs time\n");
1128 ep->par[0] = time_to_us(av[0]);
1133 if (*ac <= 0 || time_to_us(av[0]) < 0)
1134 errx(EX_DATAERR, "tupdate needs time\n");
1136 ep->par[1] = time_to_us(av[0]);
1141 if (*ac <= 0 || time_to_us(av[0]) < 0)
1142 errx(EX_DATAERR, "max_burst needs time\n");
1144 ep->par[2] = time_to_us(av[0]);
1149 if (*ac <= 0 || !is_valid_number(av[0]))
1150 errx(EX_DATAERR, "max_ecnth needs number\n");
1152 ep->par[3] = atof(av[0]) * PIE_SCALE;
1157 if (*ac <= 0 || !is_valid_number(av[0]))
1158 errx(EX_DATAERR, "alpha needs number\n");
1160 ep->par[4] = atof(av[0]) * PIE_SCALE;
1165 if (*ac <= 0 || !is_valid_number(av[0]))
1166 errx(EX_DATAERR, "beta needs number\n");
1168 ep->par[5] = atof(av[0]) * PIE_SCALE;
1173 ep->par[6] |= PIE_ECN_ENABLED;
1176 ep->par[6] &= ~PIE_ECN_ENABLED;
1180 ep->par[6] |= PIE_CAPDROP_ENABLED;
1182 case TOK_NO_CAPDROP:
1183 ep->par[6] &= ~PIE_CAPDROP_ENABLED;
1187 ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
1191 ep->par[6] |= PIE_DEPRATEEST_ENABLED;
1195 ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
1199 ep->par[6] |= PIE_DERAND_ENABLED;
1202 ep->par[6] &= ~PIE_DERAND_ENABLED;
1205 /* Config fq_pie parameters */
1207 if (type != TOK_FQ_PIE)
1208 errx(EX_DATAERR, "quantum is not for pie\n");
1209 if (*ac <= 0 || !is_valid_number(av[0]))
1210 errx(EX_DATAERR, "quantum needs number\n");
1212 ep->par[7]= atoi(av[0]);
1217 if (type != TOK_FQ_PIE)
1218 errx(EX_DATAERR, "limit is not for pie, use queue instead\n");
1219 if (*ac <= 0 || !is_valid_number(av[0]))
1220 errx(EX_DATAERR, "limit needs number\n");
1222 ep->par[8] = atoi(av[0]);
1227 if (type != TOK_FQ_PIE)
1228 errx(EX_DATAERR, "flows is not for pie\n");
1229 if (*ac <= 0 || !is_valid_number(av[0]))
1230 errx(EX_DATAERR, "flows needs number\n");
1232 ep->par[9] = atoi(av[0]);
1238 printf("%s is invalid parameter\n", av[-1]);
1251 * configuration of pipes, schedulers, flowsets.
1252 * When we configure a new scheduler, an empty pipe is created, so:
1254 * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
1255 * sched N+Delta type fifo sched_mask ...
1256 * pipe N+Delta <parameters>
1257 * flowset N+Delta pipe N+Delta (no parameters)
1258 * sched N type wf2q+ sched_mask ...
1259 * pipe N <parameters>
1261 * do_pipe = 2 -> flowset N config
1262 * flowset N parameters
1264 * do_pipe = 3 -> sched N config
1265 * sched N parameters (default no pipe)
1266 * optional Pipe N config ...
1270 ipfw_config_pipe(int ac, char **av)
1275 struct dn_id *buf, *base;
1276 struct dn_sch *sch = NULL;
1277 struct dn_link *p = NULL;
1278 struct dn_fs *fs = NULL;
1279 struct dn_profile *pf = NULL;
1280 struct ipfw_flow_id *mask = NULL;
1282 struct dn_extra_parms *aqm_extra;
1283 struct dn_extra_parms *sch_extra;
1288 uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
1291 * allocate space for 1 header,
1292 * 1 scheduler, 1 link, 1 flowset, 1 profile
1294 lmax = sizeof(struct dn_id); /* command header */
1295 lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
1296 sizeof(struct dn_fs) + sizeof(struct dn_profile);
1300 lmax_extra = sizeof(struct dn_extra_parms);
1301 /* two lmax_extra because one for AQM params and another
1304 lmax += lmax_extra*2;
1309 if (ac && isdigit(**av)) {
1310 i = atoi(*av); av++; ac--;
1314 errx(EX_USAGE, "need a pipe/flowset/sched number");
1315 base = buf = safe_calloc(1, lmax);
1316 /* all commands start with a 'CONFIGURE' and a version */
1317 o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
1318 base->id = DN_API_VERSION;
1320 switch (co.do_pipe) {
1321 case 1: /* "pipe N config ..." */
1322 /* Allocate space for the WF2Q+ scheduler, its link
1323 * and the FIFO flowset. Set the number, but leave
1324 * the scheduler subtype and other parameters to 0
1325 * so the kernel will use appropriate defaults.
1326 * XXX todo: add a flag to record if a parameter
1327 * is actually configured.
1328 * If we do a 'pipe config' mask -> sched_mask.
1329 * The FIFO scheduler and link are derived from the
1330 * WF2Q+ one in the kernel.
1333 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1334 sch_extra ->oid.subtype = 0; /* don't configure scheduler */
1336 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1337 p = o_next(&buf, sizeof(*p), DN_LINK);
1339 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1340 aqm_extra ->oid.subtype = 0; /* don't configure AQM */
1342 fs = o_next(&buf, sizeof(*fs), DN_FS);
1345 sch->oid.subtype = 0; /* defaults to WF2Q+ */
1346 mask = &sch->sched_mask;
1347 flags = &sch->flags;
1348 buckets = &sch->buckets;
1349 *flags |= DN_PIPE_CMD;
1353 /* This flowset is only for the FIFO scheduler */
1354 fs->fs_nr = i + 2*DN_MAX_ID;
1355 fs->sched_nr = i + DN_MAX_ID;
1358 case 2: /* "queue N config ... " */
1360 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1361 aqm_extra ->oid.subtype = 0;
1363 fs = o_next(&buf, sizeof(*fs), DN_FS);
1365 mask = &fs->flow_mask;
1367 buckets = &fs->buckets;
1370 case 3: /* "sched N config ..." */
1372 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1373 sch_extra ->oid.subtype = 0;
1375 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1377 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1378 aqm_extra ->oid.subtype = 0;
1380 fs = o_next(&buf, sizeof(*fs), DN_FS);
1382 mask = &sch->sched_mask;
1383 flags = &sch->flags;
1384 buckets = &sch->buckets;
1385 /* fs is used only with !MULTIQUEUE schedulers */
1386 fs->fs_nr = i + DN_MAX_ID;
1390 /* set to -1 those fields for which we want to reuse existing
1391 * values from the kernel.
1392 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
1393 * XXX todo: support reuse of the mask.
1397 for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
1401 int tok = match_token(dummynet_params, *av);
1406 NEED(fs, "noerror is only for pipes");
1407 fs->flags |= DN_NOERROR;
1411 NEED(fs, "plr is only for pipes");
1412 NEED1("plr needs argument 0..1\n");
1413 d = strtod(av[0], NULL);
1418 fs->plr = (int)(d*0x7fffffff);
1423 NEED(fs, "queue is only for pipes or flowsets");
1424 NEED1("queue needs queue size\n");
1426 fs->qsize = strtoul(av[0], &end, 0);
1427 if (*end == 'K' || *end == 'k') {
1428 fs->flags |= DN_QSIZE_BYTES;
1430 } else if (*end == 'B' ||
1431 _substrcmp2(end, "by", "bytes") == 0) {
1432 fs->flags |= DN_QSIZE_BYTES;
1438 NEED(fs, "buckets is only for pipes or flowsets");
1439 NEED1("buckets needs argument\n");
1440 *buckets = strtoul(av[0], NULL, 0);
1445 case TOK_SCHED_MASK:
1447 NEED(mask, "tok_mask");
1448 NEED1("mask needs mask specifier\n");
1450 * per-flow queue, mask is dst_ip, dst_port,
1451 * src_ip, src_port, proto measured in bits
1454 bzero(mask, sizeof(*mask));
1458 uint32_t *p32 = NULL;
1459 uint16_t *p16 = NULL;
1460 uint32_t *p20 = NULL;
1461 struct in6_addr *pa6 = NULL;
1464 tok = match_token(dummynet_params, *av);
1469 * special case, all bits significant
1470 * except 'extra' (the queue number)
1474 mask->dst_port = ~0;
1475 mask->src_port = ~0;
1477 n2mask(&mask->dst_ip6, 128);
1478 n2mask(&mask->src_ip6, 128);
1479 mask->flow_id6 = ~0;
1480 *flags |= DN_HAVE_MASK;
1485 *flags |= DN_HAVE_MASK;
1489 mask->addr_type = 4;
1490 p32 = &mask->dst_ip;
1494 mask->addr_type = 4;
1495 p32 = &mask->src_ip;
1499 mask->addr_type = 6;
1500 pa6 = &mask->dst_ip6;
1504 mask->addr_type = 6;
1505 pa6 = &mask->src_ip6;
1509 mask->addr_type = 6;
1510 p20 = &mask->flow_id6;
1514 p16 = &mask->dst_port;
1518 p16 = &mask->src_port;
1525 ac++; av--; /* backtrack */
1529 errx(EX_USAGE, "mask: value missing");
1530 if (*av[0] == '/') {
1531 a = strtoul(av[0]+1, &end, 0);
1533 a = (a == 32) ? ~0 : (1 << a) - 1;
1535 a = strtoul(av[0], &end, 0);
1538 else if (p16 != NULL) {
1541 "port mask must be 16 bit");
1543 } else if (p20 != NULL) {
1546 "flow_id mask must be 20 bit");
1548 } else if (pa6 != NULL) {
1551 "in6addr invalid mask len");
1557 "proto mask must be 8 bit");
1558 mask->proto = (uint8_t)a;
1561 *flags |= DN_HAVE_MASK;
1563 } /* end while, config masks */
1569 NEED(fs, "codel/pie is only for flowsets");
1571 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1572 fs->flags |= DN_IS_AQM;
1574 strlcpy(aqm_extra->name, av[-1],
1575 sizeof(aqm_extra->name));
1576 aqm_extra->oid.subtype = DN_AQM_PARAMS;
1578 process_extra_parms(&ac, av, aqm_extra, tok);
1583 if (!strcmp(av[-1],"type"))
1584 errx(EX_DATAERR, "use type before fq_codel/fq_pie");
1586 NEED(sch, "fq_codel/fq_pie is only for schd");
1587 strlcpy(sch_extra->name, av[-1],
1588 sizeof(sch_extra->name));
1589 sch_extra->oid.subtype = DN_SCH_PARAMS;
1590 process_extra_parms(&ac, av, sch_extra, tok);
1595 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1596 fs->flags |= DN_IS_RED;
1597 if (tok == TOK_GRED)
1598 fs->flags |= DN_IS_GENTLE_RED;
1600 * the format for parameters is w_q/min_th/max_th/max_p
1602 if ((end = strsep(&av[0], "/"))) {
1603 double w_q = strtod(end, NULL);
1604 if (w_q > 1 || w_q <= 0)
1605 errx(EX_DATAERR, "0 < w_q <= 1");
1606 fs->w_q = (int) (w_q * (1 << SCALE_RED));
1608 if ((end = strsep(&av[0], "/"))) {
1609 fs->min_th = strtoul(end, &end, 0);
1610 if (*end == 'K' || *end == 'k')
1613 if ((end = strsep(&av[0], "/"))) {
1614 fs->max_th = strtoul(end, &end, 0);
1615 if (*end == 'K' || *end == 'k')
1618 if ((end = strsep(&av[0], "/"))) {
1619 double max_p = strtod(end, NULL);
1620 if (max_p > 1 || max_p < 0)
1621 errx(EX_DATAERR, "0 <= max_p <= 1");
1622 fs->max_p = (int)(max_p * (1 << SCALE_RED));
1628 fs->flags |= DN_IS_ECN;
1632 NEED(fs, "droptail is only for flowsets");
1633 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1637 NEED(p, "bw is only for links");
1638 NEED1("bw needs bandwidth or interface\n");
1639 read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1644 NEED(p, "delay is only for links");
1645 NEED1("delay needs argument 0..10000ms\n");
1646 p->delay = strtoul(av[0], NULL, 0);
1652 NEED(sch, "type is only for schedulers");
1653 NEED1("type needs a string");
1655 if (l == 0 || l > 15)
1656 errx(1, "type %s too long\n", av[0]);
1657 strlcpy(sch->name, av[0], sizeof(sch->name));
1658 sch->oid.subtype = 0; /* use string */
1660 /* if fq_codel is selected, consider all tokens after it
1663 if (!strcasecmp(av[0],"fq_codel") || !strcasecmp(av[0],"fq_pie")){
1664 strlcpy(sch_extra->name, av[0],
1665 sizeof(sch_extra->name));
1666 sch_extra->oid.subtype = DN_SCH_PARAMS;
1667 process_extra_parms(&ac, av, sch_extra, tok);
1678 NEED(fs, "weight is only for flowsets");
1679 NEED1("weight needs argument\n");
1680 fs->par[0] = strtol(av[0], &end, 0);
1685 NEED(fs, "lmax is only for flowsets");
1686 NEED1("lmax needs argument\n");
1687 fs->par[1] = strtol(av[0], &end, 0);
1692 NEED(fs, "priority is only for flowsets");
1693 NEED1("priority needs argument\n");
1694 fs->par[2] = strtol(av[0], &end, 0);
1700 NEED(fs, "pipe/sched");
1701 NEED1("pipe/link/sched needs number\n");
1702 fs->sched_nr = strtoul(av[0], &end, 0);
1707 NEED((!pf), "profile already set");
1710 NEED1("extra delay needs the file name\n");
1711 pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1712 load_extra_delays(av[0], pf, p); //XXX can't fail?
1719 NEED1("burst needs argument\n");
1721 if (expand_number(av[0], &p->burst) < 0)
1722 if (errno != ERANGE)
1724 "burst: invalid argument");
1725 if (errno || p->burst > (1ULL << 48) - 1)
1727 "burst: out of range (0..2^48-1)");
1732 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1736 /* check validity of parameters */
1738 if (p->delay > 10000)
1739 errx(EX_DATAERR, "delay must be < 10000");
1740 if (p->bandwidth == -1)
1744 /* XXX accept a 0 scheduler to keep the default */
1745 if (fs->flags & DN_QSIZE_BYTES) {
1749 len = sizeof(limit);
1750 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1751 &limit, &len, NULL, 0) == -1)
1753 if (fs->qsize > limit)
1754 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1759 len = sizeof(limit);
1760 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1761 &limit, &len, NULL, 0) == -1)
1763 if (fs->qsize > limit)
1764 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1768 if ((fs->flags & DN_IS_ECN) && !((fs->flags & DN_IS_RED)||
1769 (fs->flags & DN_IS_AQM)))
1770 errx(EX_USAGE, "ECN can be used with red/gred/"
1771 "codel/fq_codel only!");
1773 if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1774 errx(EX_USAGE, "enable red/gred for ECN");
1778 if (fs->flags & DN_IS_RED) {
1780 int lookup_depth, avg_pkt_size;
1782 if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
1783 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1784 fs->min_th, fs->max_th);
1785 else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1786 errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1787 fs->min_th, fs->max_th);
1789 if (fs->max_th == 0)
1790 errx(EX_DATAERR, "max_th must be > 0");
1793 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1794 &lookup_depth, &len, NULL, 0) == -1)
1796 if (lookup_depth == 0)
1797 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1798 " must be greater than zero");
1801 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1802 &avg_pkt_size, &len, NULL, 0) == -1)
1805 if (avg_pkt_size == 0)
1807 "net.inet.ip.dummynet.red_avg_pkt_size must"
1808 " be greater than zero");
1810 #if 0 /* the following computation is now done in the kernel */
1812 * Ticks needed for sending a medium-sized packet.
1813 * Unfortunately, when we are configuring a WF2Q+ queue, we
1814 * do not have bandwidth information, because that is stored
1815 * in the parent pipe, and also we have multiple queues
1816 * competing for it. So we set s=0, which is not very
1817 * correct. But on the other hand, why do we want RED with
1820 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1823 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1825 * max idle time (in ticks) before avg queue size becomes 0.
1826 * NOTA: (3/w_q) is approx the value x so that
1827 * (1-w_q)^x < 10^-3.
1829 w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1830 idle = s * 3. / w_q;
1831 fs->lookup_step = (int)idle / lookup_depth;
1832 if (!fs->lookup_step)
1833 fs->lookup_step = 1;
1835 for (t = fs->lookup_step; t > 1; --t)
1837 fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1838 #endif /* code moved in the kernel */
1842 i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1845 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1849 dummynet_flush(void)
1852 oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1853 do_cmd(IP_DUMMYNET3, &oid, oid.len);
1856 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1857 * Returns the number of ranges, and possibly stores them
1858 * in the array v of size len.
1861 parse_range(int ac, char *av[], uint32_t *v, int len)
1867 if (v == NULL || len < 2) {
1872 for (s = *av; s != NULL; av++, ac--) {
1873 v[0] = strtoul(s, &endptr, 10);
1874 v[1] = (*endptr != '-') ? v[0] :
1875 strtoul(endptr+1, &endptr, 10);
1876 if (*endptr == '\0') { /* prepare for next round */
1877 s = (ac > 0) ? *(av+1) : NULL;
1879 if (*endptr != ',') {
1880 warn("invalid number: %s", s);
1884 /* continue processing from here */
1890 v[0] >= DN_MAX_ID-1 ||
1891 v[1] >= DN_MAX_ID-1) {
1892 continue; /* invalid entry */
1895 /* translate if 'pipe list' */
1896 if (co.do_pipe == 1) {
1900 v = (n*2 < len) ? v + 2 : base;
1905 /* main entry point for dummynet list functions. co.do_pipe indicates
1906 * which function we want to support.
1907 * av may contain filtering arguments, either individual entries
1908 * or ranges, or lists (space or commas are valid separators).
1909 * Format for a range can be n1-n2 or n3 n4 n5 ...
1910 * In a range n1 must be <= n2, otherwise the range is ignored.
1911 * A number 'n4' is translate in a range 'n4-n4'
1912 * All number must be > 0 and < DN_MAX_ID-1
1915 dummynet_list(int ac, char *av[], int show_counters)
1917 struct dn_id *oid, *x = NULL;
1919 int n; /* # of ranges */
1921 u_int max_size; /* largest obj passed up */
1923 (void)show_counters; // XXX unused, but we should use it.
1925 av++; /* skip 'list' | 'show' word */
1927 n = parse_range(ac, av, NULL, 0); /* Count # of ranges. */
1929 /* Allocate space to store ranges */
1930 l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1931 oid = safe_calloc(1, l);
1932 oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1934 if (n > 0) /* store ranges in idx */
1935 parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1937 * Compute the size of the largest object returned. If the
1938 * response leaves at least this much spare space in the
1939 * buffer, then surely the response is complete; otherwise
1940 * there might be a risk of truncation and we will need to
1941 * retry with a larger buffer.
1942 * XXX don't bother with smaller structs.
1944 max_size = sizeof(struct dn_fs);
1945 if (max_size < sizeof(struct dn_sch))
1946 max_size = sizeof(struct dn_sch);
1947 if (max_size < sizeof(struct dn_flow))
1948 max_size = sizeof(struct dn_flow);
1950 switch (co.do_pipe) {
1952 oid->subtype = DN_LINK; /* list pipe */
1955 oid->subtype = DN_FS; /* list queue */
1958 oid->subtype = DN_SCH; /* list sched */
1963 * Ask the kernel an estimate of the required space (result
1964 * in oid.id), unless we are requesting a subset of objects,
1965 * in which case the kernel does not give an exact answer.
1966 * In any case, space might grow in the meantime due to the
1967 * creation of new queues, so we must be prepared to retry.
1972 ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1973 if (ret != 0 || oid->id <= sizeof(*oid))
1975 buflen = oid->id + max_size;
1976 oid->len = sizeof(*oid); /* restore */
1978 /* Try a few times, until the buffer fits */
1979 for (i = 0; i < 20; i++) {
1981 x = safe_realloc(x, l);
1982 bcopy(oid, x, oid->len);
1983 ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1984 if (ret != 0 || x->id <= sizeof(*oid))
1985 goto done; /* no response */
1986 if (l + max_size <= buflen)
1988 buflen *= 2; /* double for next attempt */
1990 list_pipes(x, O_NEXT(x, l));