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/limits.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 /* XXX there are several sysctl leftover here */
30 #include <sys/sysctl.h>
49 #include <netinet/in.h>
50 #include <netinet/ip_fw.h>
51 #include <netinet/ip_dummynet.h>
52 #include <arpa/inet.h> /* inet_ntoa */
55 static struct _s_x dummynet_params[] = {
57 { "noerror", TOK_NOERROR },
58 { "buckets", TOK_BUCKETS },
59 { "dst-ip", TOK_DSTIP },
60 { "src-ip", TOK_SRCIP },
61 { "dst-port", TOK_DSTPORT },
62 { "src-port", TOK_SRCPORT },
63 { "proto", TOK_PROTO },
64 { "weight", TOK_WEIGHT },
66 { "maxlen", TOK_LMAX },
68 { "mask", TOK_MASK }, /* alias for both */
69 { "sched_mask", TOK_SCHED_MASK },
70 { "flow_mask", TOK_FLOW_MASK },
71 { "droptail", TOK_DROPTAIL },
76 { "codel", TOK_CODEL}, /* Codel AQM */
77 { "fq_codel", TOK_FQ_CODEL}, /* FQ-Codel */
78 { "pie", TOK_PIE}, /* PIE AQM */
79 { "fq_pie", TOK_FQ_PIE}, /* FQ-PIE */
82 { "bandwidth", TOK_BW },
83 { "delay", TOK_DELAY },
86 { "queue", TOK_QUEUE },
87 { "flowset", TOK_FLOWSET },
88 { "sched", TOK_SCHED },
90 { "priority", TOK_PRI },
92 { "flow-id", TOK_FLOWID},
93 { "dst-ipv6", TOK_DSTIP6},
94 { "dst-ip6", TOK_DSTIP6},
95 { "src-ipv6", TOK_SRCIP6},
96 { "src-ip6", TOK_SRCIP6},
97 { "profile", TOK_PROFILE},
98 { "burst", TOK_BURST},
99 { "dummynet-params", TOK_NULL },
100 { NULL, 0 } /* terminator */
104 /* AQM/extra sched parameters tokens*/
105 static struct _s_x aqm_params[] = {
106 { "target", TOK_TARGET},
107 { "interval", TOK_INTERVAL},
108 { "limit", TOK_LIMIT},
109 { "flows", TOK_FLOWS},
110 { "quantum", TOK_QUANTUM},
112 { "noecn", TOK_NO_ECN},
113 { "tupdate", TOK_TUPDATE},
114 { "max_burst", TOK_MAX_BURST},
115 { "max_ecnth", TOK_MAX_ECNTH},
116 { "alpha", TOK_ALPHA},
118 { "capdrop", TOK_CAPDROP},
119 { "nocapdrop", TOK_NO_CAPDROP},
120 { "onoff", TOK_ONOFF},
123 { "derand", TOK_DERAND},
124 { "noderand", TOK_NO_DERAND},
125 { NULL, 0 } /* terminator */
129 #define O_NEXT(p, len) ((void *)((char *)p + len))
132 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
140 /* make room in the buffer and move the pointer forward */
142 o_next(struct dn_id **o, int len, int type)
144 struct dn_id *ret = *o;
145 oid_fill(ret, len, type, 0);
146 *o = O_NEXT(*o, len);
154 CODEL_ECN_ENABLED = 1
157 /* PIE flags, from PIE kernel module */
160 PIE_CAPDROP_ENABLED = 2,
161 PIE_ON_OFF_MODE_ENABLED = 4,
162 PIE_DEPRATEEST_ENABLED = 8,
163 PIE_DERAND_ENABLED = 16
166 #define PIE_FIX_POINT_BITS 13
167 #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
169 /* integer to time */
171 us_to_time(int t, char *strt)
176 sprintf(strt,"%d", t);
178 sprintf(strt,"%dus", t);
179 else if (t < 1000000)
180 sprintf(strt,"%gms", (float) t / 1000);
182 sprintf(strt,"%gfs", (float) t / 1000000);
186 * returns -1 if s is not a valid time, otherwise, return time in us
189 time_to_us(const char *s)
193 char strt[16]="", stru[16]="";
197 for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
213 if (!strcasecmp(stru, "us"))
215 if (!strcasecmp(stru, "ms"))
216 return (strtod(strt, NULL) * 1000);
217 if (!strcasecmp(stru, "s"))
218 return (strtod(strt, NULL)*1000000);
224 /* Get AQM or scheduler extra parameters */
226 get_extra_parms(uint32_t nr, char *out, int subtype)
228 struct dn_extra_parms *ep;
230 char strt1[15], strt2[15], strt3[15];
233 /* prepare the request */
234 l = sizeof(struct dn_extra_parms);
235 ep = safe_calloc(1, l);
236 memset(ep, 0, sizeof(*ep));
239 oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
241 ep->oid.subtype = subtype;
244 ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
247 errx(EX_DATAERR, "Error getting extra parameters\n");
252 if( !strcasecmp(ep->name, "codel")) {
253 us_to_time(ep->par[0], strt1);
254 us_to_time(ep->par[1], strt2);
255 l = sprintf(out, " AQM CoDel target %s interval %s",
257 if (ep->par[2] & CODEL_ECN_ENABLED)
258 l = sprintf(out + l, " ECN");
260 l += sprintf(out + l, " NoECN");
261 } else if( !strcasecmp(ep->name, "pie")) {
262 us_to_time(ep->par[0], strt1);
263 us_to_time(ep->par[1], strt2);
264 us_to_time(ep->par[2], strt3);
265 l = sprintf(out, " AQM type PIE target %s tupdate %s alpha "
266 "%g beta %g max_burst %s max_ecnth %.3g",
269 ep->par[4] / (float) PIE_SCALE,
270 ep->par[5] / (float) PIE_SCALE,
272 ep->par[3] / (float) PIE_SCALE
275 if (ep->par[6] & PIE_ECN_ENABLED)
276 l += sprintf(out + l, " ECN");
278 l += sprintf(out + l, " NoECN");
279 if (ep->par[6] & PIE_CAPDROP_ENABLED)
280 l += sprintf(out + l, " CapDrop");
282 l += sprintf(out + l, " NoCapDrop");
283 if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
284 l += sprintf(out + l, " OnOff");
285 if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
286 l += sprintf(out + l, " DRE");
288 l += sprintf(out + l, " TS");
289 if (ep->par[6] & PIE_DERAND_ENABLED)
290 l += sprintf(out + l, " Derand");
292 l += sprintf(out + l, " NoDerand");
297 if (!strcasecmp(ep->name,"FQ_CODEL")) {
298 us_to_time(ep->par[0], strt1);
299 us_to_time(ep->par[1], strt2);
300 l = sprintf(out," FQ_CODEL target %s interval %s"
301 " quantum %jd limit %jd flows %jd",
303 (intmax_t) ep->par[3],
304 (intmax_t) ep->par[4],
305 (intmax_t) ep->par[5]
307 if (ep->par[2] & CODEL_ECN_ENABLED)
308 l += sprintf(out + l, " ECN");
310 l += sprintf(out + l, " NoECN");
311 l += sprintf(out + l, "\n");
312 } else if (!strcasecmp(ep->name,"FQ_PIE")) {
313 us_to_time(ep->par[0], strt1);
314 us_to_time(ep->par[1], strt2);
315 us_to_time(ep->par[2], strt3);
316 l = sprintf(out, " FQ_PIE target %s tupdate %s alpha "
317 "%g beta %g max_burst %s max_ecnth %.3g"
318 " quantum %jd limit %jd flows %jd",
321 ep->par[4] / (float) PIE_SCALE,
322 ep->par[5] / (float) PIE_SCALE,
324 ep->par[3] / (float) PIE_SCALE,
325 (intmax_t) ep->par[7],
326 (intmax_t) ep->par[8],
327 (intmax_t) ep->par[9]
330 if (ep->par[6] & PIE_ECN_ENABLED)
331 l += sprintf(out + l, " ECN");
333 l += sprintf(out + l, " NoECN");
334 if (ep->par[6] & PIE_CAPDROP_ENABLED)
335 l += sprintf(out + l, " CapDrop");
337 l += sprintf(out + l, " NoCapDrop");
338 if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
339 l += sprintf(out + l, " OnOff");
340 if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
341 l += sprintf(out + l, " DRE");
343 l += sprintf(out + l, " TS");
344 if (ep->par[6] & PIE_DERAND_ENABLED)
345 l += sprintf(out + l, " Derand");
347 l += sprintf(out + l, " NoDerand");
348 l += sprintf(out + l, "\n");
360 sort_q(void *arg, const void *pa, const void *pb)
362 int rev = (co.do_sort < 0);
363 int field = rev ? -co.do_sort : co.do_sort;
365 const struct dn_flow_queue *a = pa;
366 const struct dn_flow_queue *b = pb;
370 res = a->len - b->len;
373 res = a->len_bytes - b->len_bytes;
376 case 3: /* tot pkts */
377 res = a->tot_pkts - b->tot_pkts;
380 case 4: /* tot bytes */
381 res = a->tot_bytes - b->tot_bytes;
388 return (int)(rev ? res : -res);
392 /* print a mask and header for the subsequent list of flows */
394 print_mask(struct ipfw_flow_id *id)
396 if (!IS_IP6_FLOW_ID(id)) {
398 "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
399 id->extra ? "queue," : "",
401 id->src_ip, id->src_port,
402 id->dst_ip, id->dst_port);
405 printf("\n mask: %sproto: 0x%02x, flow_id: 0x%08x, ",
406 id->extra ? "queue," : "",
407 id->proto, id->flow_id6);
408 inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
409 printf("%s/0x%04x -> ", buf, id->src_port);
410 inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
411 printf("%s/0x%04x\n", buf, id->dst_port);
416 print_header(struct ipfw_flow_id *id)
418 if (!IS_IP6_FLOW_ID(id))
419 printf("BKT Prot ___Source IP/port____ "
420 "____Dest. IP/port____ "
421 "Tot_pkt/bytes Pkt/Byte Drp\n");
423 printf("BKT ___Prot___ _flow-id_ "
424 "______________Source IPv6/port_______________ "
425 "_______________Dest. IPv6/port_______________ "
426 "Tot_pkt/bytes Pkt/Byte Drp\n");
430 list_flow(struct buf_pr *bp, struct dn_flow *ni)
433 struct protoent *pe = NULL;
435 struct ipfw_flow_id *id = &ni->fid;
437 pe = getprotobynumber(id->proto);
438 /* XXX: Should check for IPv4 flows */
439 bprintf(bp, "%3u%c", (ni->oid.id) & 0xff,
440 id->extra ? '*' : ' ');
441 if (!IS_IP6_FLOW_ID(id)) {
443 bprintf(bp, "%-4s ", pe->p_name);
445 bprintf(bp, "%4u ", id->proto);
446 ina.s_addr = htonl(id->src_ip);
447 bprintf(bp, "%15s/%-5d ",
448 inet_ntoa(ina), id->src_port);
449 ina.s_addr = htonl(id->dst_ip);
450 bprintf(bp, "%15s/%-5d ",
451 inet_ntoa(ina), id->dst_port);
453 /* Print IPv6 flows */
455 bprintf(bp, "%9s ", pe->p_name);
457 bprintf(bp, "%9u ", id->proto);
458 bprintf(bp, "%7d %39s/%-5d ", id->flow_id6,
459 inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
461 bprintf(bp, " %39s/%-5d ",
462 inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
465 pr_u64(bp, &ni->tot_pkts, 4);
466 pr_u64(bp, &ni->tot_bytes, 8);
467 bprintf(bp, "%2u %4u %3u",
468 ni->length, ni->len_bytes, ni->drops);
472 print_flowset_parms(struct dn_fs *fs, char *prefix)
477 char red[200]; /* Display RED parameters */
480 if (fs->flags & DN_QSIZE_BYTES) {
482 sprintf(qs, "%d KB", l / 1024);
484 sprintf(qs, "%d B", l);
486 sprintf(qs, "%3d sl.", l);
488 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
492 if (fs->flags & DN_IS_RED) { /* RED parameters */
494 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
495 (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
496 1.0 * fs->w_q / (double)(1 << SCALE_RED),
499 1.0 * fs->max_p / (double)(1 << SCALE_RED));
500 if (fs->flags & DN_IS_ECN)
501 strlcat(red, " (ecn)", sizeof(red));
503 /* get AQM parameters */
504 } else if (fs->flags & DN_IS_AQM) {
505 get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
508 sprintf(red, "droptail");
511 printf("%s %s%s %d queues (%d buckets) %s\n",
512 prefix, qs, plr, fs->oid.id, fs->buckets, red);
515 printf("q%05d %s%s %d flows (%d buckets) sched %d "
516 "weight %d lmax %d pri %d %s\n",
517 fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
518 fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
519 if (fs->flags & DN_HAVE_MASK)
520 print_mask(&fs->flow_mask);
525 print_extra_delay_parms(struct dn_profile *p)
528 if (p->samples_no <= 0)
531 loss = p->loss_level;
532 loss /= p->samples_no;
533 printf("\t profile: name \"%s\" loss %f samples %d\n",
534 p->name, loss, p->samples_no);
546 * generic list routine. We expect objects in a specific order, i.e.
547 * PIPES AND SCHEDULERS:
548 * link; scheduler; internal flowset if any; instances
549 * we can tell a pipe from the number.
553 * link i (int queue); scheduler i; si(i) { flowsets() : queues }
556 list_pipes(struct dn_id *oid, struct dn_id *end)
558 char buf[160]; /* pending buffer */
559 int toPrint = 1; /* print header */
564 for (; oid != end; oid = O_NEXT(oid, oid->len)) {
565 if (oid->len < sizeof(*oid))
566 errx(1, "invalid oid len %d\n", oid->len);
571 printf("unrecognized object %d size %d\n", oid->type, oid->len);
573 case DN_TEXT: /* list of attached flowsets */
580 l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
583 printf(" Children flowsets: ");
584 for (i = 0; i < l; i++)
585 printf("%u ", d->p[i]);
591 printf("answer for cmd %d, len %d\n", oid->type, oid->id);
594 struct dn_sch *s = (struct dn_sch *)oid;
596 printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
598 s->name, s->flags, s->buckets, s->oid.id);
601 get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
604 if (s->flags & DN_HAVE_MASK)
605 print_mask(&s->sched_mask);
611 print_header(&((struct dn_flow *)oid)->fid);
614 list_flow(&bp, (struct dn_flow *)oid);
615 printf("%s\n", bp.buf);
620 struct dn_link *p = (struct dn_link *)oid;
621 double b = p->bandwidth;
625 /* This starts a new object so flush buffer */
629 sprintf(bwbuf, "unlimited ");
630 else if (b >= 1000000000)
631 sprintf(bwbuf, "%7.3f Gbit/s", b/1000000000);
632 else if (b >= 1000000)
633 sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
635 sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
637 sprintf(bwbuf, "%7.3f bit/s ", b);
639 if (humanize_number(burst, sizeof(burst), p->burst,
640 "", HN_AUTOSCALE, 0) < 0 || g_co.verbose)
641 sprintf(burst, "%d", (int)p->burst);
642 sprintf(buf, "%05d: %s %4d ms burst %s",
643 p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
648 print_flowset_parms((struct dn_fs *)oid, buf);
652 print_extra_delay_parms((struct dn_profile *)oid);
654 flush_buf(buf); // XXX does it really go here ?
661 * Delete pipe, queue or scheduler i
664 ipfw_delete_pipe(int do_pipe, int i)
668 uintptr_t a[1]; /* add more if we want a list */
670 oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
671 cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
672 ( (do_pipe == 2) ? DN_FS : DN_SCH);
674 i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
677 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
683 * Code to parse delay profiles.
685 * Some link types introduce extra delays in the transmission
686 * of a packet, e.g. because of MAC level framing, contention on
687 * the use of the channel, MAC level retransmissions and so on.
688 * From our point of view, the channel is effectively unavailable
689 * for this extra time, which is constant or variable depending
690 * on the link type. Additionally, packets may be dropped after this
691 * time (e.g. on a wireless link after too many retransmissions).
692 * We can model the additional delay with an empirical curve
693 * that represents its distribution.
695 * cumulative probability
705 * +-------*------------------->
708 * The empirical curve may have both vertical and horizontal lines.
709 * Vertical lines represent constant delay for a range of
710 * probabilities; horizontal lines correspond to a discontinuty
711 * in the delay distribution: the link will use the largest delay
712 * for a given probability.
714 * To pass the curve to dummynet, we must store the parameters
715 * in a file as described below, and issue the command
717 * ipfw pipe <n> config ... bw XXX profile <filename> ...
719 * The file format is the following, with whitespace acting as
720 * a separator and '#' indicating the beginning a comment:
723 * the number of samples used in the internal
724 * representation (2..1024; default 100);
727 * The probability above which packets are lost.
728 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
731 * Optional a name (listed by "ipfw pipe show")
732 * to identify the distribution;
734 * "delay prob" | "prob delay"
735 * One of these two lines is mandatory and defines
736 * the format of the following lines with data points.
739 * 2 or more lines representing points in the curve,
740 * with either delay or probability first, according
741 * to the chosen format.
742 * The unit for delay is milliseconds.
744 * Data points does not need to be ordered or equal to the number
745 * specified in the "samples" line. ipfw will sort and interpolate
746 * the curve as needed.
748 * Example of a profile file:
754 0 200 # minimum overhead is 200ms
761 * Internally, we will convert the curve to a fixed number of
762 * samples, and when it is time to transmit a packet we will
763 * model the extra delay as extra bits in the packet.
767 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
768 #define ED_TOK_SAMPLES "samples"
769 #define ED_TOK_LOSS "loss-level"
770 #define ED_TOK_NAME "name"
771 #define ED_TOK_DELAY "delay"
772 #define ED_TOK_PROB "prob"
773 #define ED_TOK_BW "bw"
774 #define ED_SEPARATORS " \t\n"
775 #define ED_MIN_SAMPLES_NO 2
778 * returns 1 if s is a non-negative number, with at least one '.'
781 is_valid_number(const char *s)
783 int i, dots_found = 0;
786 for (i = 0; i<len; ++i)
787 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
793 * Take as input a string describing a bandwidth value
794 * and return the numeric bandwidth value.
795 * set clocking interface or bandwidth value
798 read_bandwidth(char *arg, uint32_t *bandwidth, char *if_name, int namelen)
800 if (*bandwidth != (uint32_t)-1)
801 warnx("duplicate token, override bandwidth value!");
803 if (arg[0] >= 'a' && arg[0] <= 'z') {
805 errx(1, "no if support");
807 if (namelen >= IFNAMSIZ)
808 warn("interface name truncated");
811 strlcpy(if_name, arg, namelen);
813 } else { /* read bandwidth value */
817 bw = strtoul(arg, &end, 0);
818 if (*end == 'K' || *end == 'k') {
821 } else if (*end == 'M' || *end == 'm') {
824 } else if (*end == 'G' || *end == 'g') {
829 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
830 _substrcmp2(end, "by", "bytes") == 0)
834 errx(EX_DATAERR, "bandwidth too large");
836 *bandwidth = (uint32_t)bw;
848 compare_points(const void *vp1, const void *vp2)
850 const struct point *p1 = vp1;
851 const struct point *p2 = vp2;
854 res = p1->prob - p2->prob;
856 res = p1->delay - p2->delay;
865 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
868 load_extra_delays(const char *filename, struct dn_profile *p,
869 struct dn_link *link)
871 char line[ED_MAX_LINE_LEN];
878 char profile_name[ED_MAX_NAME_LEN];
879 int delay_first = -1;
881 struct point points[ED_MAX_SAMPLES_NO];
884 /* XXX link never NULL? */
885 p->link_nr = link->link_nr;
887 profile_name[0] = '\0';
888 f = fopen(filename, "r");
890 err(EX_UNAVAILABLE, "fopen: %s", filename);
892 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
893 char *s, *cur = line, *name = NULL, *arg = NULL;
899 s = strsep(&cur, ED_SEPARATORS);
900 if (s == NULL || *s == '#')
905 errx(ED_EFMT("too many arguments"));
911 if (name == NULL) /* empty line */
914 errx(ED_EFMT("missing arg for %s"), name);
916 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
918 errx(ED_EFMT("duplicate ``samples'' line"));
920 errx(ED_EFMT("invalid number of samples"));
922 if (samples>ED_MAX_SAMPLES_NO)
923 errx(ED_EFMT("too many samples, maximum is %d"),
926 } else if (!strcasecmp(name, ED_TOK_BW)) {
928 read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
929 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
931 errx(ED_EFMT("duplicated token: %s"), name);
932 if (!is_valid_number(arg))
933 errx(ED_EFMT("invalid %s"), arg);
936 errx(ED_EFMT("%s greater than 1.0"), name);
938 } else if (!strcasecmp(name, ED_TOK_NAME)) {
939 if (profile_name[0] != '\0')
940 errx(ED_EFMT("duplicated token: %s"), name);
941 strlcpy(profile_name, arg, sizeof(profile_name));
943 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
945 errx(ED_EFMT("duplicated token: %s"), name);
948 } else if (!strcasecmp(name, ED_TOK_PROB)) {
950 errx(ED_EFMT("duplicated token: %s"), name);
953 } else if (do_points) {
954 if (!is_valid_number(name) || !is_valid_number(arg))
955 errx(ED_EFMT("invalid point found"));
957 points[points_no].delay = atof(name);
958 points[points_no].prob = atof(arg);
960 points[points_no].delay = atof(arg);
961 points[points_no].prob = atof(name);
963 if (points[points_no].prob > 1.0)
964 errx(ED_EFMT("probability greater than 1.0"));
967 errx(ED_EFMT("unrecognised command '%s'"), name);
974 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
979 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
983 /* make sure that there are enough points. */
984 if (points_no < ED_MIN_SAMPLES_NO)
985 errx(ED_EFMT("too few samples, need at least %d"),
988 qsort(points, points_no, sizeof(struct point), compare_points);
991 for (i = 0; i<points_no-1; ++i) {
992 double y1 = points[i].prob * samples;
993 double x1 = points[i].delay;
994 double y2 = points[i+1].prob * samples;
995 double x2 = points[i+1].delay;
1001 for (; ix<stop; ++ix)
1002 p->samples[ix] = x1;
1004 double m = (y2-y1)/(x2-x1);
1005 double c = y1 - m*x1;
1006 for (; ix<stop ; ++ix)
1007 p->samples[ix] = (ix - c)/m;
1010 p->samples_no = samples;
1011 p->loss_level = loss * samples;
1012 strlcpy(p->name, profile_name, sizeof(p->name));
1017 /* Parse AQM/extra scheduler parameters */
1019 process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
1024 /* use kernel defaults */
1025 for (i=0; i<DN_MAX_EXTRA_PARM; i++)
1032 * 0- target, 1- interval, 2- flags,
1034 * 3- quantum, 4- limit, 5- flows
1036 if (type==TOK_CODEL)
1039 ep->par[2] = CODEL_ECN_ENABLED;
1042 int tok = match_token(aqm_params, *av);
1046 if (*ac <= 0 || time_to_us(av[0]) < 0)
1047 errx(EX_DATAERR, "target needs time\n");
1049 ep->par[0] = time_to_us(av[0]);
1054 if (*ac <= 0 || time_to_us(av[0]) < 0)
1055 errx(EX_DATAERR, "interval needs time\n");
1057 ep->par[1] = time_to_us(av[0]);
1062 ep->par[2] = CODEL_ECN_ENABLED;
1065 ep->par[2] &= ~CODEL_ECN_ENABLED;
1067 /* Config fq_codel parameters */
1069 if (type != TOK_FQ_CODEL)
1070 errx(EX_DATAERR, "quantum is not for codel\n");
1071 if (*ac <= 0 || !is_valid_number(av[0]))
1072 errx(EX_DATAERR, "quantum needs number\n");
1074 ep->par[3]= atoi(av[0]);
1079 if (type != TOK_FQ_CODEL)
1080 errx(EX_DATAERR, "limit is not for codel, use queue instead\n");
1081 if (*ac <= 0 || !is_valid_number(av[0]))
1082 errx(EX_DATAERR, "limit needs number\n");
1084 ep->par[4] = atoi(av[0]);
1089 if (type != TOK_FQ_CODEL)
1090 errx(EX_DATAERR, "flows is not for codel\n");
1091 if (*ac <= 0 || !is_valid_number(av[0]))
1092 errx(EX_DATAERR, "flows needs number\n");
1094 ep->par[5] = atoi(av[0]);
1099 printf("%s is Invalid parameter\n", av[-1]);
1106 * 0- target , 1- tupdate, 2- max_burst,
1107 * 3- max_ecnth, 4- alpha,
1110 * 7- quantum, 8- limit, 9- flows
1113 if ( type == TOK_PIE)
1114 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
1115 | PIE_DERAND_ENABLED;
1117 /* for FQ-PIE, use TS mode */
1118 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DERAND_ENABLED
1122 int tok = match_token(aqm_params, *av);
1126 if (*ac <= 0 || time_to_us(av[0]) < 0)
1127 errx(EX_DATAERR, "target needs time\n");
1129 ep->par[0] = time_to_us(av[0]);
1134 if (*ac <= 0 || time_to_us(av[0]) < 0)
1135 errx(EX_DATAERR, "tupdate needs time\n");
1137 ep->par[1] = time_to_us(av[0]);
1142 if (*ac <= 0 || time_to_us(av[0]) < 0)
1143 errx(EX_DATAERR, "max_burst needs time\n");
1145 ep->par[2] = time_to_us(av[0]);
1150 if (*ac <= 0 || !is_valid_number(av[0]))
1151 errx(EX_DATAERR, "max_ecnth needs number\n");
1153 ep->par[3] = atof(av[0]) * PIE_SCALE;
1158 if (*ac <= 0 || !is_valid_number(av[0]))
1159 errx(EX_DATAERR, "alpha needs number\n");
1161 ep->par[4] = atof(av[0]) * PIE_SCALE;
1166 if (*ac <= 0 || !is_valid_number(av[0]))
1167 errx(EX_DATAERR, "beta needs number\n");
1169 ep->par[5] = atof(av[0]) * PIE_SCALE;
1174 ep->par[6] |= PIE_ECN_ENABLED;
1177 ep->par[6] &= ~PIE_ECN_ENABLED;
1181 ep->par[6] |= PIE_CAPDROP_ENABLED;
1183 case TOK_NO_CAPDROP:
1184 ep->par[6] &= ~PIE_CAPDROP_ENABLED;
1188 ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
1192 ep->par[6] |= PIE_DEPRATEEST_ENABLED;
1196 ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
1200 ep->par[6] |= PIE_DERAND_ENABLED;
1203 ep->par[6] &= ~PIE_DERAND_ENABLED;
1206 /* Config fq_pie parameters */
1208 if (type != TOK_FQ_PIE)
1209 errx(EX_DATAERR, "quantum is not for pie\n");
1210 if (*ac <= 0 || !is_valid_number(av[0]))
1211 errx(EX_DATAERR, "quantum needs number\n");
1213 ep->par[7]= atoi(av[0]);
1218 if (type != TOK_FQ_PIE)
1219 errx(EX_DATAERR, "limit is not for pie, use queue instead\n");
1220 if (*ac <= 0 || !is_valid_number(av[0]))
1221 errx(EX_DATAERR, "limit needs number\n");
1223 ep->par[8] = atoi(av[0]);
1228 if (type != TOK_FQ_PIE)
1229 errx(EX_DATAERR, "flows is not for pie\n");
1230 if (*ac <= 0 || !is_valid_number(av[0]))
1231 errx(EX_DATAERR, "flows needs number\n");
1233 ep->par[9] = atoi(av[0]);
1239 printf("%s is invalid parameter\n", av[-1]);
1252 * configuration of pipes, schedulers, flowsets.
1253 * When we configure a new scheduler, an empty pipe is created, so:
1255 * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
1256 * sched N+Delta type fifo sched_mask ...
1257 * pipe N+Delta <parameters>
1258 * flowset N+Delta pipe N+Delta (no parameters)
1259 * sched N type wf2q+ sched_mask ...
1260 * pipe N <parameters>
1262 * do_pipe = 2 -> flowset N config
1263 * flowset N parameters
1265 * do_pipe = 3 -> sched N config
1266 * sched N parameters (default no pipe)
1267 * optional Pipe N config ...
1271 ipfw_config_pipe(int ac, char **av)
1276 struct dn_id *buf, *base;
1277 struct dn_sch *sch = NULL;
1278 struct dn_link *p = NULL;
1279 struct dn_fs *fs = NULL;
1280 struct dn_profile *pf = NULL;
1281 struct ipfw_flow_id *mask = NULL;
1283 struct dn_extra_parms *aqm_extra = NULL;
1284 struct dn_extra_parms *sch_extra = NULL;
1289 uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
1292 * allocate space for 1 header,
1293 * 1 scheduler, 1 link, 1 flowset, 1 profile
1295 lmax = sizeof(struct dn_id); /* command header */
1296 lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
1297 sizeof(struct dn_fs) + sizeof(struct dn_profile);
1301 lmax_extra = sizeof(struct dn_extra_parms);
1302 /* two lmax_extra because one for AQM params and another
1305 lmax += lmax_extra*2;
1310 if (ac && isdigit(**av)) {
1311 i = atoi(*av); av++; ac--;
1315 errx(EX_USAGE, "need a pipe/flowset/sched number");
1316 base = buf = safe_calloc(1, lmax);
1317 /* all commands start with a 'CONFIGURE' and a version */
1318 o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
1319 base->id = DN_API_VERSION;
1321 switch (g_co.do_pipe) {
1322 case 1: /* "pipe N config ..." */
1323 /* Allocate space for the WF2Q+ scheduler, its link
1324 * and the FIFO flowset. Set the number, but leave
1325 * the scheduler subtype and other parameters to 0
1326 * so the kernel will use appropriate defaults.
1327 * XXX todo: add a flag to record if a parameter
1328 * is actually configured.
1329 * If we do a 'pipe config' mask -> sched_mask.
1330 * The FIFO scheduler and link are derived from the
1331 * WF2Q+ one in the kernel.
1334 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1335 sch_extra ->oid.subtype = 0; /* don't configure scheduler */
1337 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1338 p = o_next(&buf, sizeof(*p), DN_LINK);
1340 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1341 aqm_extra ->oid.subtype = 0; /* don't configure AQM */
1343 fs = o_next(&buf, sizeof(*fs), DN_FS);
1346 sch->oid.subtype = 0; /* defaults to WF2Q+ */
1347 mask = &sch->sched_mask;
1348 flags = &sch->flags;
1349 buckets = &sch->buckets;
1350 *flags |= DN_PIPE_CMD;
1354 /* This flowset is only for the FIFO scheduler */
1355 fs->fs_nr = i + 2*DN_MAX_ID;
1356 fs->sched_nr = i + DN_MAX_ID;
1359 case 2: /* "queue N config ... " */
1361 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1362 aqm_extra ->oid.subtype = 0;
1364 fs = o_next(&buf, sizeof(*fs), DN_FS);
1366 mask = &fs->flow_mask;
1368 buckets = &fs->buckets;
1371 case 3: /* "sched N config ..." */
1373 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1374 sch_extra ->oid.subtype = 0;
1376 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1378 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1379 aqm_extra ->oid.subtype = 0;
1381 fs = o_next(&buf, sizeof(*fs), DN_FS);
1383 mask = &sch->sched_mask;
1384 flags = &sch->flags;
1385 buckets = &sch->buckets;
1386 /* fs is used only with !MULTIQUEUE schedulers */
1387 fs->fs_nr = i + DN_MAX_ID;
1391 /* set to -1 those fields for which we want to reuse existing
1392 * values from the kernel.
1393 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
1394 * XXX todo: support reuse of the mask.
1398 for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
1402 int tok = match_token(dummynet_params, *av);
1407 NEED(fs, "noerror is only for pipes");
1408 fs->flags |= DN_NOERROR;
1412 NEED(fs, "plr is only for pipes");
1413 NEED1("plr needs argument 0..1\n");
1414 d = strtod(av[0], NULL);
1419 fs->plr = (int)(d*0x7fffffff);
1424 NEED(fs, "queue is only for pipes or flowsets");
1425 NEED1("queue needs queue size\n");
1427 fs->qsize = strtoul(av[0], &end, 0);
1428 if (*end == 'K' || *end == 'k') {
1429 fs->flags |= DN_QSIZE_BYTES;
1431 } else if (*end == 'B' ||
1432 _substrcmp2(end, "by", "bytes") == 0) {
1433 fs->flags |= DN_QSIZE_BYTES;
1439 NEED(fs, "buckets is only for pipes or flowsets");
1440 NEED1("buckets needs argument\n");
1441 *buckets = strtoul(av[0], NULL, 0);
1446 case TOK_SCHED_MASK:
1448 NEED(mask, "tok_mask");
1449 NEED1("mask needs mask specifier\n");
1451 * per-flow queue, mask is dst_ip, dst_port,
1452 * src_ip, src_port, proto measured in bits
1455 bzero(mask, sizeof(*mask));
1459 uint32_t *p32 = NULL;
1460 uint16_t *p16 = NULL;
1461 uint32_t *p20 = NULL;
1462 struct in6_addr *pa6 = NULL;
1465 tok = match_token(dummynet_params, *av);
1470 * special case, all bits significant
1471 * except 'extra' (the queue number)
1475 mask->dst_port = ~0;
1476 mask->src_port = ~0;
1478 n2mask(&mask->dst_ip6, 128);
1479 n2mask(&mask->src_ip6, 128);
1480 mask->flow_id6 = ~0;
1481 *flags |= DN_HAVE_MASK;
1486 *flags |= DN_HAVE_MASK;
1490 mask->addr_type = 4;
1491 p32 = &mask->dst_ip;
1495 mask->addr_type = 4;
1496 p32 = &mask->src_ip;
1500 mask->addr_type = 6;
1501 pa6 = &mask->dst_ip6;
1505 mask->addr_type = 6;
1506 pa6 = &mask->src_ip6;
1510 mask->addr_type = 6;
1511 p20 = &mask->flow_id6;
1515 p16 = &mask->dst_port;
1519 p16 = &mask->src_port;
1526 ac++; av--; /* backtrack */
1530 errx(EX_USAGE, "mask: value missing");
1531 if (*av[0] == '/') {
1532 a = strtoul(av[0]+1, &end, 0);
1534 a = (a == 32) ? ~0 : (1 << a) - 1;
1536 a = strtoul(av[0], &end, 0);
1539 else if (p16 != NULL) {
1542 "port mask must be 16 bit");
1544 } else if (p20 != NULL) {
1547 "flow_id mask must be 20 bit");
1549 } else if (pa6 != NULL) {
1552 "in6addr invalid mask len");
1558 "proto mask must be 8 bit");
1559 mask->proto = (uint8_t)a;
1562 *flags |= DN_HAVE_MASK;
1564 } /* end while, config masks */
1570 NEED(fs, "codel/pie is only for flowsets");
1572 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1573 fs->flags |= DN_IS_AQM;
1575 strlcpy(aqm_extra->name, av[-1],
1576 sizeof(aqm_extra->name));
1577 aqm_extra->oid.subtype = DN_AQM_PARAMS;
1579 process_extra_parms(&ac, av, aqm_extra, tok);
1584 if (!strcmp(av[-1],"type"))
1585 errx(EX_DATAERR, "use type before fq_codel/fq_pie");
1587 NEED(sch, "fq_codel/fq_pie is only for schd");
1588 strlcpy(sch_extra->name, av[-1],
1589 sizeof(sch_extra->name));
1590 sch_extra->oid.subtype = DN_SCH_PARAMS;
1591 process_extra_parms(&ac, av, sch_extra, tok);
1596 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1597 fs->flags |= DN_IS_RED;
1598 if (tok == TOK_GRED)
1599 fs->flags |= DN_IS_GENTLE_RED;
1601 * the format for parameters is w_q/min_th/max_th/max_p
1603 if ((end = strsep(&av[0], "/"))) {
1604 double w_q = strtod(end, NULL);
1605 if (w_q > 1 || w_q <= 0)
1606 errx(EX_DATAERR, "0 < w_q <= 1");
1607 fs->w_q = (int) (w_q * (1 << SCALE_RED));
1609 if ((end = strsep(&av[0], "/"))) {
1610 fs->min_th = strtoul(end, &end, 0);
1611 if (*end == 'K' || *end == 'k')
1614 if ((end = strsep(&av[0], "/"))) {
1615 fs->max_th = strtoul(end, &end, 0);
1616 if (*end == 'K' || *end == 'k')
1619 if ((end = strsep(&av[0], "/"))) {
1620 double max_p = strtod(end, NULL);
1621 if (max_p > 1 || max_p < 0)
1622 errx(EX_DATAERR, "0 <= max_p <= 1");
1623 fs->max_p = (int)(max_p * (1 << SCALE_RED));
1629 fs->flags |= DN_IS_ECN;
1633 NEED(fs, "droptail is only for flowsets");
1634 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1638 NEED(p, "bw is only for links");
1639 NEED1("bw needs bandwidth or interface\n");
1640 read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1645 NEED(p, "delay is only for links");
1646 NEED1("delay needs argument 0..10000ms\n");
1647 p->delay = strtoul(av[0], NULL, 0);
1653 NEED(sch, "type is only for schedulers");
1654 NEED1("type needs a string");
1656 if (l == 0 || l > 15)
1657 errx(1, "type %s too long\n", av[0]);
1658 strlcpy(sch->name, av[0], sizeof(sch->name));
1659 sch->oid.subtype = 0; /* use string */
1661 /* if fq_codel is selected, consider all tokens after it
1664 if (!strcasecmp(av[0],"fq_codel") || !strcasecmp(av[0],"fq_pie")){
1665 strlcpy(sch_extra->name, av[0],
1666 sizeof(sch_extra->name));
1667 sch_extra->oid.subtype = DN_SCH_PARAMS;
1668 process_extra_parms(&ac, av, sch_extra, tok);
1679 NEED(fs, "weight is only for flowsets");
1680 NEED1("weight needs argument\n");
1681 fs->par[0] = strtol(av[0], &end, 0);
1686 NEED(fs, "lmax is only for flowsets");
1687 NEED1("lmax needs argument\n");
1688 fs->par[1] = strtol(av[0], &end, 0);
1693 NEED(fs, "priority is only for flowsets");
1694 NEED1("priority needs argument\n");
1695 fs->par[2] = strtol(av[0], &end, 0);
1701 NEED(fs, "pipe/sched");
1702 NEED1("pipe/link/sched needs number\n");
1703 fs->sched_nr = strtoul(av[0], &end, 0);
1708 NEED((!pf), "profile already set");
1711 NEED1("extra delay needs the file name\n");
1712 pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1713 load_extra_delays(av[0], pf, p); //XXX can't fail?
1720 NEED1("burst needs argument\n");
1722 if (expand_number(av[0], &p->burst) < 0)
1723 if (errno != ERANGE)
1725 "burst: invalid argument");
1726 if (errno || p->burst > (1ULL << 48) - 1)
1728 "burst: out of range (0..2^48-1)");
1733 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1737 /* check validity of parameters */
1739 if (p->delay > 10000)
1740 errx(EX_DATAERR, "delay must be < 10000");
1741 if (p->bandwidth == (uint32_t)-1)
1745 /* XXX accept a 0 scheduler to keep the default */
1746 if (fs->flags & DN_QSIZE_BYTES) {
1750 len = sizeof(limit);
1751 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1752 &limit, &len, NULL, 0) == -1)
1754 if (fs->qsize > limit)
1755 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1760 len = sizeof(limit);
1761 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1762 &limit, &len, NULL, 0) == -1)
1764 if (fs->qsize > limit)
1765 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1769 if ((fs->flags & DN_IS_ECN) && !((fs->flags & DN_IS_RED)||
1770 (fs->flags & DN_IS_AQM)))
1771 errx(EX_USAGE, "ECN can be used with red/gred/"
1772 "codel/fq_codel only!");
1774 if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1775 errx(EX_USAGE, "enable red/gred for ECN");
1779 if (fs->flags & DN_IS_RED) {
1781 int lookup_depth, avg_pkt_size;
1783 if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
1784 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1785 fs->min_th, fs->max_th);
1786 else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1787 errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1788 fs->min_th, fs->max_th);
1790 if (fs->max_th == 0)
1791 errx(EX_DATAERR, "max_th must be > 0");
1794 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1795 &lookup_depth, &len, NULL, 0) == -1)
1797 if (lookup_depth == 0)
1798 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1799 " must be greater than zero");
1802 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1803 &avg_pkt_size, &len, NULL, 0) == -1)
1806 if (avg_pkt_size == 0)
1808 "net.inet.ip.dummynet.red_avg_pkt_size must"
1809 " be greater than zero");
1811 #if 0 /* the following computation is now done in the kernel */
1813 * Ticks needed for sending a medium-sized packet.
1814 * Unfortunately, when we are configuring a WF2Q+ queue, we
1815 * do not have bandwidth information, because that is stored
1816 * in the parent pipe, and also we have multiple queues
1817 * competing for it. So we set s=0, which is not very
1818 * correct. But on the other hand, why do we want RED with
1821 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1824 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1826 * max idle time (in ticks) before avg queue size becomes 0.
1827 * NOTA: (3/w_q) is approx the value x so that
1828 * (1-w_q)^x < 10^-3.
1830 w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1831 idle = s * 3. / w_q;
1832 fs->lookup_step = (int)idle / lookup_depth;
1833 if (!fs->lookup_step)
1834 fs->lookup_step = 1;
1836 for (t = fs->lookup_step; t > 1; --t)
1838 fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1839 #endif /* code moved in the kernel */
1843 i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1846 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1850 dummynet_flush(void)
1853 oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1854 do_cmd(IP_DUMMYNET3, &oid, oid.len);
1857 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1858 * Returns the number of ranges, and possibly stores them
1859 * in the array v of size len.
1862 parse_range(int ac, char *av[], uint32_t *v, int len)
1868 if (v == NULL || len < 2) {
1873 for (s = *av; s != NULL; av++, ac--) {
1874 v[0] = strtoul(s, &endptr, 10);
1875 v[1] = (*endptr != '-') ? v[0] :
1876 strtoul(endptr+1, &endptr, 10);
1877 if (*endptr == '\0') { /* prepare for next round */
1878 s = (ac > 0) ? *(av+1) : NULL;
1880 if (*endptr != ',') {
1881 warn("invalid number: %s", s);
1885 /* continue processing from here */
1891 v[0] >= DN_MAX_ID-1 ||
1892 v[1] >= DN_MAX_ID-1) {
1893 continue; /* invalid entry */
1896 /* translate if 'pipe list' */
1897 if (g_co.do_pipe == 1) {
1901 v = (n*2 < len) ? v + 2 : base;
1906 /* main entry point for dummynet list functions. co.do_pipe indicates
1907 * which function we want to support.
1908 * av may contain filtering arguments, either individual entries
1909 * or ranges, or lists (space or commas are valid separators).
1910 * Format for a range can be n1-n2 or n3 n4 n5 ...
1911 * In a range n1 must be <= n2, otherwise the range is ignored.
1912 * A number 'n4' is translate in a range 'n4-n4'
1913 * All number must be > 0 and < DN_MAX_ID-1
1916 dummynet_list(int ac, char *av[], int show_counters)
1918 struct dn_id *oid, *x = NULL;
1920 int n; /* # of ranges */
1922 u_int max_size; /* largest obj passed up */
1924 (void)show_counters; // XXX unused, but we should use it.
1926 av++; /* skip 'list' | 'show' word */
1928 n = parse_range(ac, av, NULL, 0); /* Count # of ranges. */
1930 /* Allocate space to store ranges */
1931 l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1932 oid = safe_calloc(1, l);
1933 oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1935 if (n > 0) /* store ranges in idx */
1936 parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1938 * Compute the size of the largest object returned. If the
1939 * response leaves at least this much spare space in the
1940 * buffer, then surely the response is complete; otherwise
1941 * there might be a risk of truncation and we will need to
1942 * retry with a larger buffer.
1943 * XXX don't bother with smaller structs.
1945 max_size = sizeof(struct dn_fs);
1946 if (max_size < sizeof(struct dn_sch))
1947 max_size = sizeof(struct dn_sch);
1948 if (max_size < sizeof(struct dn_flow))
1949 max_size = sizeof(struct dn_flow);
1951 switch (g_co.do_pipe) {
1953 oid->subtype = DN_LINK; /* list pipe */
1956 oid->subtype = DN_FS; /* list queue */
1959 oid->subtype = DN_SCH; /* list sched */
1964 * Ask the kernel an estimate of the required space (result
1965 * in oid.id), unless we are requesting a subset of objects,
1966 * in which case the kernel does not give an exact answer.
1967 * In any case, space might grow in the meantime due to the
1968 * creation of new queues, so we must be prepared to retry.
1973 ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1974 if (ret != 0 || oid->id <= sizeof(*oid))
1976 buflen = oid->id + max_size;
1977 oid->len = sizeof(*oid); /* restore */
1979 /* Try a few times, until the buffer fits */
1980 for (i = 0; i < 20; i++) {
1982 x = safe_realloc(x, l);
1983 bcopy(oid, x, oid->len);
1984 ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1985 if (ret != 0 || x->id <= sizeof(*oid))
1986 goto done; /* no response */
1987 if (l + max_size <= buflen)
1989 buflen *= 2; /* double for next attempt */
1991 list_pipes(x, O_NEXT(x, l));