1 /*-
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 <[email protected]>
8 *
9 * Copyright (c) 2002-2003,2010 Luigi Rizzo
10 *
11 * Redistribution and use in source forms, with and without modification,
12 * are permitted provided that this entire comment appears intact.
13 *
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.
17 *
18 * This software is provided ``AS IS'' without any warranties of any kind.
19 *
20 * $FreeBSD$
21 *
22 * dummynet support
23 */
24
25 #define NEW_AQM
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>
31
32 #include "ipfw2.h"
33
34 #ifdef NEW_AQM
35 #include <stdint.h>
36 #endif
37
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <libutil.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47
48 #include <net/if.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 */
53
54
55 static struct _s_x dummynet_params[] = {
56 { "plr", TOK_PLR },
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 },
65 { "lmax", TOK_LMAX },
66 { "maxlen", TOK_LMAX },
67 { "all", TOK_ALL },
68 { "mask", TOK_MASK }, /* alias for both */
69 { "sched_mask", TOK_SCHED_MASK },
70 { "flow_mask", TOK_FLOW_MASK },
71 { "droptail", TOK_DROPTAIL },
72 { "ecn", TOK_ECN },
73 { "red", TOK_RED },
74 { "gred", TOK_GRED },
75 #ifdef NEW_AQM
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 */
80 #endif
81 { "bw", TOK_BW },
82 { "bandwidth", TOK_BW },
83 { "delay", TOK_DELAY },
84 { "link", TOK_LINK },
85 { "pipe", TOK_PIPE },
86 { "queue", TOK_QUEUE },
87 { "flowset", TOK_FLOWSET },
88 { "sched", TOK_SCHED },
89 { "pri", TOK_PRI },
90 { "priority", TOK_PRI },
91 { "type", TOK_TYPE },
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 */
101 };
102
103 #ifdef NEW_AQM
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},
111 { "ecn", TOK_ECN},
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},
117 { "beta", TOK_BETA},
118 { "capdrop", TOK_CAPDROP},
119 { "nocapdrop", TOK_NO_CAPDROP},
120 { "onoff", TOK_ONOFF},
121 { "dre", TOK_DRE},
122 { "ts", TOK_TS},
123 { "derand", TOK_DERAND},
124 { "noderand", TOK_NO_DERAND},
125 { NULL, 0 } /* terminator */
126 };
127 #endif
128
129 #define O_NEXT(p, len) ((void *)((char *)p + len))
130
131 static void
oid_fill(struct dn_id * oid,int len,int type,uintptr_t id)132 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
133 {
134 oid->len = len;
135 oid->type = type;
136 oid->subtype = 0;
137 oid->id = id;
138 }
139
140 /* make room in the buffer and move the pointer forward */
141 static void *
o_next(struct dn_id ** o,int len,int type)142 o_next(struct dn_id **o, int len, int type)
143 {
144 struct dn_id *ret = *o;
145 oid_fill(ret, len, type, 0);
146 *o = O_NEXT(*o, len);
147 return ret;
148 }
149
150 #ifdef NEW_AQM
151
152 /* Codel flags */
153 enum {
154 CODEL_ECN_ENABLED = 1
155 };
156
157 /* PIE flags, from PIE kernel module */
158 enum {
159 PIE_ECN_ENABLED = 1,
160 PIE_CAPDROP_ENABLED = 2,
161 PIE_ON_OFF_MODE_ENABLED = 4,
162 PIE_DEPRATEEST_ENABLED = 8,
163 PIE_DERAND_ENABLED = 16
164 };
165
166 #define PIE_FIX_POINT_BITS 13
167 #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
168
169 /* integer to time */
170 static void
us_to_time(int t,char * strt)171 us_to_time(int t, char *strt)
172 {
173 if (t < 0)
174 strt[0]='\0';
175 else if ( t==0 )
176 sprintf(strt,"%d", t);
177 else if (t< 1000)
178 sprintf(strt,"%dus", t);
179 else if (t < 1000000)
180 sprintf(strt,"%gms", (float) t / 1000);
181 else
182 sprintf(strt,"%gfs", (float) t / 1000000);
183 }
184
185 /*
186 * returns -1 if s is not a valid time, otherwise, return time in us
187 */
188 static long
time_to_us(const char * s)189 time_to_us(const char *s)
190 {
191 int i, dots = 0;
192 int len = strlen(s);
193 char strt[16]="", stru[16]="";
194
195 if (len>15)
196 return -1;
197 for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
198 if (s[i]=='.') {
199 if (dots)
200 return -1;
201 else
202 dots++;
203 }
204
205 if (!i)
206 return -1;
207 strncpy(strt, s, i);
208 if (i<len)
209 strcpy(stru, s+i);
210 else
211 strcpy(stru, "ms");
212
213 if (!strcasecmp(stru, "us"))
214 return atol(strt);
215 if (!strcasecmp(stru, "ms"))
216 return (strtod(strt, NULL) * 1000);
217 if (!strcasecmp(stru, "s"))
218 return (strtod(strt, NULL)*1000000);
219
220 return -1;
221 }
222
223
224 /* Get AQM or scheduler extra parameters */
225 static void
get_extra_parms(uint32_t nr,char * out,int subtype)226 get_extra_parms(uint32_t nr, char *out, int subtype)
227 {
228 struct dn_extra_parms *ep;
229 int ret;
230 char strt1[15], strt2[15], strt3[15];
231 u_int l;
232
233 /* prepare the request */
234 l = sizeof(struct dn_extra_parms);
235 ep = safe_calloc(1, l);
236 memset(ep, 0, sizeof(*ep));
237 *out = '\0';
238
239 oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
240 ep->oid.len = l;
241 ep->oid.subtype = subtype;
242 ep->nr = nr;
243
244 ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
245 if (ret) {
246 free(ep);
247 errx(EX_DATAERR, "Error getting extra parameters\n");
248 }
249
250 switch (subtype) {
251 case DN_AQM_PARAMS:
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",
256 strt1, strt2);
257 if (ep->par[2] & CODEL_ECN_ENABLED)
258 l = sprintf(out + l, " ECN");
259 else
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",
267 strt1,
268 strt2,
269 ep->par[4] / (float) PIE_SCALE,
270 ep->par[5] / (float) PIE_SCALE,
271 strt3,
272 ep->par[3] / (float) PIE_SCALE
273 );
274
275 if (ep->par[6] & PIE_ECN_ENABLED)
276 l += sprintf(out + l, " ECN");
277 else
278 l += sprintf(out + l, " NoECN");
279 if (ep->par[6] & PIE_CAPDROP_ENABLED)
280 l += sprintf(out + l, " CapDrop");
281 else
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");
287 else
288 l += sprintf(out + l, " TS");
289 if (ep->par[6] & PIE_DERAND_ENABLED)
290 l += sprintf(out + l, " Derand");
291 else
292 l += sprintf(out + l, " NoDerand");
293 }
294 break;
295
296 case DN_SCH_PARAMS:
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",
302 strt1, strt2,
303 (intmax_t) ep->par[3],
304 (intmax_t) ep->par[4],
305 (intmax_t) ep->par[5]
306 );
307 if (ep->par[2] & CODEL_ECN_ENABLED)
308 l += sprintf(out + l, " ECN");
309 else
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",
319 strt1,
320 strt2,
321 ep->par[4] / (float) PIE_SCALE,
322 ep->par[5] / (float) PIE_SCALE,
323 strt3,
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]
328 );
329
330 if (ep->par[6] & PIE_ECN_ENABLED)
331 l += sprintf(out + l, " ECN");
332 else
333 l += sprintf(out + l, " NoECN");
334 if (ep->par[6] & PIE_CAPDROP_ENABLED)
335 l += sprintf(out + l, " CapDrop");
336 else
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");
342 else
343 l += sprintf(out + l, " TS");
344 if (ep->par[6] & PIE_DERAND_ENABLED)
345 l += sprintf(out + l, " Derand");
346 else
347 l += sprintf(out + l, " NoDerand");
348 l += sprintf(out + l, "\n");
349 }
350 break;
351 }
352
353 free(ep);
354 }
355 #endif
356
357
358 #if 0
359 static int
360 sort_q(void *arg, const void *pa, const void *pb)
361 {
362 int rev = (co.do_sort < 0);
363 int field = rev ? -co.do_sort : co.do_sort;
364 long long res = 0;
365 const struct dn_flow_queue *a = pa;
366 const struct dn_flow_queue *b = pb;
367
368 switch (field) {
369 case 1: /* pkts */
370 res = a->len - b->len;
371 break;
372 case 2: /* bytes */
373 res = a->len_bytes - b->len_bytes;
374 break;
375
376 case 3: /* tot pkts */
377 res = a->tot_pkts - b->tot_pkts;
378 break;
379
380 case 4: /* tot bytes */
381 res = a->tot_bytes - b->tot_bytes;
382 break;
383 }
384 if (res < 0)
385 res = -1;
386 if (res > 0)
387 res = 1;
388 return (int)(rev ? res : -res);
389 }
390 #endif
391
392 /* print a mask and header for the subsequent list of flows */
393 static void
print_mask(struct ipfw_flow_id * id)394 print_mask(struct ipfw_flow_id *id)
395 {
396 if (!IS_IP6_FLOW_ID(id)) {
397 printf(" "
398 "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
399 id->extra ? "queue," : "",
400 id->proto,
401 id->src_ip, id->src_port,
402 id->dst_ip, id->dst_port);
403 } else {
404 char buf[255];
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);
412 }
413 }
414
415 static void
print_header(struct ipfw_flow_id * id)416 print_header(struct ipfw_flow_id *id)
417 {
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");
422 else
423 printf("BKT ___Prot___ _flow-id_ "
424 "______________Source IPv6/port_______________ "
425 "_______________Dest. IPv6/port_______________ "
426 "Tot_pkt/bytes Pkt/Byte Drp\n");
427 }
428
429 static void
list_flow(struct buf_pr * bp,struct dn_flow * ni)430 list_flow(struct buf_pr *bp, struct dn_flow *ni)
431 {
432 char buff[255];
433 struct protoent *pe = NULL;
434 struct in_addr ina;
435 struct ipfw_flow_id *id = &ni->fid;
436
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)) {
442 if (pe)
443 bprintf(bp, "%-4s ", pe->p_name);
444 else
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);
452 } else {
453 /* Print IPv6 flows */
454 if (pe != NULL)
455 bprintf(bp, "%9s ", pe->p_name);
456 else
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)),
460 id->src_port);
461 bprintf(bp, " %39s/%-5d ",
462 inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
463 id->dst_port);
464 }
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);
469 }
470
471 static void
print_flowset_parms(struct dn_fs * fs,char * prefix)472 print_flowset_parms(struct dn_fs *fs, char *prefix)
473 {
474 int l;
475 char qs[30];
476 char plr[30];
477 char red[200]; /* Display RED parameters */
478
479 l = fs->qsize;
480 if (fs->flags & DN_QSIZE_BYTES) {
481 if (l >= 8192)
482 sprintf(qs, "%d KB", l / 1024);
483 else
484 sprintf(qs, "%d B", l);
485 } else
486 sprintf(qs, "%3d sl.", l);
487 if (fs->plr)
488 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
489 else
490 plr[0] = '\0';
491
492 if (fs->flags & DN_IS_RED) { /* RED parameters */
493 sprintf(red,
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),
497 fs->min_th,
498 fs->max_th,
499 1.0 * fs->max_p / (double)(1 << SCALE_RED));
500 if (fs->flags & DN_IS_ECN)
501 strlcat(red, " (ecn)", sizeof(red));
502 #ifdef NEW_AQM
503 /* get AQM parameters */
504 } else if (fs->flags & DN_IS_AQM) {
505 get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
506 #endif
507 } else
508 sprintf(red, "droptail");
509
510 if (prefix[0]) {
511 printf("%s %s%s %d queues (%d buckets) %s\n",
512 prefix, qs, plr, fs->oid.id, fs->buckets, red);
513 prefix[0] = '\0';
514 } else {
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);
521 }
522 }
523
524 static void
print_extra_delay_parms(struct dn_profile * p)525 print_extra_delay_parms(struct dn_profile *p)
526 {
527 double loss;
528 if (p->samples_no <= 0)
529 return;
530
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);
535 }
536
537 static void
flush_buf(char * buf)538 flush_buf(char *buf)
539 {
540 if (buf[0])
541 printf("%s\n", buf);
542 buf[0] = '\0';
543 }
544
545 /*
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.
550 *
551 * FLOWSETS:
552 * flowset; queues;
553 * link i (int queue); scheduler i; si(i) { flowsets() : queues }
554 */
555 static void
list_pipes(struct dn_id * oid,struct dn_id * end)556 list_pipes(struct dn_id *oid, struct dn_id *end)
557 {
558 char buf[160]; /* pending buffer */
559 int toPrint = 1; /* print header */
560 struct buf_pr bp;
561
562 buf[0] = '\0';
563 bp_alloc(&bp, 4096);
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);
567
568 switch (oid->type) {
569 default:
570 flush_buf(buf);
571 printf("unrecognized object %d size %d\n", oid->type, oid->len);
572 break;
573 case DN_TEXT: /* list of attached flowsets */
574 {
575 int i, l;
576 struct {
577 struct dn_id id;
578 uint32_t p[0];
579 } *d = (void *)oid;
580 l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
581 if (l == 0)
582 break;
583 printf(" Children flowsets: ");
584 for (i = 0; i < l; i++)
585 printf("%u ", d->p[i]);
586 printf("\n");
587 break;
588 }
589 case DN_CMD_GET:
590 if (g_co.verbose)
591 printf("answer for cmd %d, len %d\n", oid->type, oid->id);
592 break;
593 case DN_SCH: {
594 struct dn_sch *s = (struct dn_sch *)oid;
595 flush_buf(buf);
596 printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
597 s->sched_nr,
598 s->name, s->flags, s->buckets, s->oid.id);
599 #ifdef NEW_AQM
600 char parms[200];
601 get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
602 printf("%s",parms);
603 #endif
604 if (s->flags & DN_HAVE_MASK)
605 print_mask(&s->sched_mask);
606 }
607 break;
608
609 case DN_FLOW:
610 if (toPrint != 0) {
611 print_header(&((struct dn_flow *)oid)->fid);
612 toPrint = 0;
613 }
614 list_flow(&bp, (struct dn_flow *)oid);
615 printf("%s\n", bp.buf);
616 bp_flush(&bp);
617 break;
618
619 case DN_LINK: {
620 struct dn_link *p = (struct dn_link *)oid;
621 double b = p->bandwidth;
622 char bwbuf[30];
623 char burst[5 + 7];
624
625 /* This starts a new object so flush buffer */
626 flush_buf(buf);
627 /* data rate */
628 if (b == 0)
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);
634 else if (b >= 1000)
635 sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
636 else
637 sprintf(bwbuf, "%7.3f bit/s ", b);
638
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);
644 }
645 break;
646
647 case DN_FS:
648 print_flowset_parms((struct dn_fs *)oid, buf);
649 break;
650 case DN_PROFILE:
651 flush_buf(buf);
652 print_extra_delay_parms((struct dn_profile *)oid);
653 }
654 flush_buf(buf); // XXX does it really go here ?
655 }
656
657 bp_free(&bp);
658 }
659
660 /*
661 * Delete pipe, queue or scheduler i
662 */
663 int
ipfw_delete_pipe(int do_pipe,int i)664 ipfw_delete_pipe(int do_pipe, int i)
665 {
666 struct {
667 struct dn_id oid;
668 uintptr_t a[1]; /* add more if we want a list */
669 } cmd;
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);
673 cmd.a[0] = i;
674 i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
675 if (i) {
676 i = 1;
677 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
678 }
679 return i;
680 }
681
682 /*
683 * Code to parse delay profiles.
684 *
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.
694 *
695 * cumulative probability
696 * 1.0 ^
697 * |
698 * L +-- loss-level x
699 * | ******
700 * | *
701 * | *****
702 * | *
703 * | **
704 * | *
705 * +-------*------------------->
706 * delay
707 *
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.
713 *
714 * To pass the curve to dummynet, we must store the parameters
715 * in a file as described below, and issue the command
716 *
717 * ipfw pipe <n> config ... bw XXX profile <filename> ...
718 *
719 * The file format is the following, with whitespace acting as
720 * a separator and '#' indicating the beginning a comment:
721 *
722 * samples N
723 * the number of samples used in the internal
724 * representation (2..1024; default 100);
725 *
726 * loss-level L
727 * The probability above which packets are lost.
728 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
729 *
730 * name identifier
731 * Optional a name (listed by "ipfw pipe show")
732 * to identify the distribution;
733 *
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.
737 *
738 * XXX YYY
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.
743 *
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.
747 *
748 * Example of a profile file:
749
750 name bla_bla_bla
751 samples 100
752 loss-level 0.86
753 prob delay
754 0 200 # minimum overhead is 200ms
755 0.5 200
756 0.5 300
757 0.8 1000
758 0.9 1300
759 1 1300
760
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.
764 *
765 */
766
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
776
777 /*
778 * returns 1 if s is a non-negative number, with at least one '.'
779 */
780 static int
is_valid_number(const char * s)781 is_valid_number(const char *s)
782 {
783 int i, dots_found = 0;
784 int len = strlen(s);
785
786 for (i = 0; i<len; ++i)
787 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
788 return 0;
789 return 1;
790 }
791
792 /*
793 * Take as input a string describing a bandwidth value
794 * and return the numeric bandwidth value.
795 * set clocking interface or bandwidth value
796 */
797 static void
read_bandwidth(char * arg,uint32_t * bandwidth,char * if_name,int namelen)798 read_bandwidth(char *arg, uint32_t *bandwidth, char *if_name, int namelen)
799 {
800 if (*bandwidth != (uint32_t)-1)
801 warnx("duplicate token, override bandwidth value!");
802
803 if (arg[0] >= 'a' && arg[0] <= 'z') {
804 if (!if_name) {
805 errx(1, "no if support");
806 }
807 if (namelen >= IFNAMSIZ)
808 warn("interface name truncated");
809 namelen--;
810 /* interface name */
811 strlcpy(if_name, arg, namelen);
812 *bandwidth = 0;
813 } else { /* read bandwidth value */
814 uint64_t bw;
815 char *end = NULL;
816
817 bw = strtoul(arg, &end, 0);
818 if (*end == 'K' || *end == 'k') {
819 end++;
820 bw *= 1000;
821 } else if (*end == 'M' || *end == 'm') {
822 end++;
823 bw *= 1000000;
824 } else if (*end == 'G' || *end == 'g') {
825 end++;
826 bw *= 1000000000;
827 }
828 if ((*end == 'B' &&
829 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
830 _substrcmp2(end, "by", "bytes") == 0)
831 bw *= 8;
832
833 if (bw > UINT_MAX)
834 errx(EX_DATAERR, "bandwidth too large");
835
836 *bandwidth = (uint32_t)bw;
837 if (if_name)
838 if_name[0] = '\0';
839 }
840 }
841
842 struct point {
843 double prob;
844 double delay;
845 };
846
847 static int
compare_points(const void * vp1,const void * vp2)848 compare_points(const void *vp1, const void *vp2)
849 {
850 const struct point *p1 = vp1;
851 const struct point *p2 = vp2;
852 double res = 0;
853
854 res = p1->prob - p2->prob;
855 if (res == 0)
856 res = p1->delay - p2->delay;
857 if (res < 0)
858 return -1;
859 else if (res > 0)
860 return 1;
861 else
862 return 0;
863 }
864
865 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
866
867 static void
load_extra_delays(const char * filename,struct dn_profile * p,struct dn_link * link)868 load_extra_delays(const char *filename, struct dn_profile *p,
869 struct dn_link *link)
870 {
871 char line[ED_MAX_LINE_LEN];
872 FILE *f;
873 int lineno = 0;
874 int i;
875
876 int samples = -1;
877 double loss = -1.0;
878 char profile_name[ED_MAX_NAME_LEN];
879 int delay_first = -1;
880 int do_points = 0;
881 struct point points[ED_MAX_SAMPLES_NO];
882 int points_no = 0;
883
884 /* XXX link never NULL? */
885 p->link_nr = link->link_nr;
886
887 profile_name[0] = '\0';
888 f = fopen(filename, "r");
889 if (f == NULL)
890 err(EX_UNAVAILABLE, "fopen: %s", filename);
891
892 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
893 char *s, *cur = line, *name = NULL, *arg = NULL;
894
895 ++lineno;
896
897 /* parse the line */
898 while (cur) {
899 s = strsep(&cur, ED_SEPARATORS);
900 if (s == NULL || *s == '#')
901 break;
902 if (*s == '\0')
903 continue;
904 if (arg)
905 errx(ED_EFMT("too many arguments"));
906 if (name == NULL)
907 name = s;
908 else
909 arg = s;
910 }
911 if (name == NULL) /* empty line */
912 continue;
913 if (arg == NULL)
914 errx(ED_EFMT("missing arg for %s"), name);
915
916 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
917 if (samples > 0)
918 errx(ED_EFMT("duplicate ``samples'' line"));
919 if (atoi(arg) <=0)
920 errx(ED_EFMT("invalid number of samples"));
921 samples = atoi(arg);
922 if (samples>ED_MAX_SAMPLES_NO)
923 errx(ED_EFMT("too many samples, maximum is %d"),
924 ED_MAX_SAMPLES_NO);
925 do_points = 0;
926 } else if (!strcasecmp(name, ED_TOK_BW)) {
927 char buf[IFNAMSIZ];
928 read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
929 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
930 if (loss != -1.0)
931 errx(ED_EFMT("duplicated token: %s"), name);
932 if (!is_valid_number(arg))
933 errx(ED_EFMT("invalid %s"), arg);
934 loss = atof(arg);
935 if (loss > 1)
936 errx(ED_EFMT("%s greater than 1.0"), name);
937 do_points = 0;
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));
942 do_points = 0;
943 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
944 if (do_points)
945 errx(ED_EFMT("duplicated token: %s"), name);
946 delay_first = 1;
947 do_points = 1;
948 } else if (!strcasecmp(name, ED_TOK_PROB)) {
949 if (do_points)
950 errx(ED_EFMT("duplicated token: %s"), name);
951 delay_first = 0;
952 do_points = 1;
953 } else if (do_points) {
954 if (!is_valid_number(name) || !is_valid_number(arg))
955 errx(ED_EFMT("invalid point found"));
956 if (delay_first) {
957 points[points_no].delay = atof(name);
958 points[points_no].prob = atof(arg);
959 } else {
960 points[points_no].delay = atof(arg);
961 points[points_no].prob = atof(name);
962 }
963 if (points[points_no].prob > 1.0)
964 errx(ED_EFMT("probability greater than 1.0"));
965 ++points_no;
966 } else {
967 errx(ED_EFMT("unrecognised command '%s'"), name);
968 }
969 }
970
971 fclose (f);
972
973 if (samples == -1) {
974 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
975 samples = 100;
976 }
977
978 if (loss == -1.0) {
979 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
980 loss = 1;
981 }
982
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"),
986 ED_MIN_SAMPLES_NO);
987
988 qsort(points, points_no, sizeof(struct point), compare_points);
989
990 /* interpolation */
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;
996
997 int ix = y1;
998 int stop = y2;
999
1000 if (x1 == x2) {
1001 for (; ix<stop; ++ix)
1002 p->samples[ix] = x1;
1003 } else {
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;
1008 }
1009 }
1010 p->samples_no = samples;
1011 p->loss_level = loss * samples;
1012 strlcpy(p->name, profile_name, sizeof(p->name));
1013 }
1014
1015 #ifdef NEW_AQM
1016
1017 /* Parse AQM/extra scheduler parameters */
1018 static int
process_extra_parms(int * ac,char ** av,struct dn_extra_parms * ep,uint16_t type)1019 process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
1020 uint16_t type)
1021 {
1022 int i;
1023
1024 /* use kernel defaults */
1025 for (i=0; i<DN_MAX_EXTRA_PARM; i++)
1026 ep->par[i] = -1;
1027
1028 switch(type) {
1029 case TOK_CODEL:
1030 case TOK_FQ_CODEL:
1031 /* Codel
1032 * 0- target, 1- interval, 2- flags,
1033 * FQ_CODEL
1034 * 3- quantum, 4- limit, 5- flows
1035 */
1036 if (type==TOK_CODEL)
1037 ep->par[2] = 0;
1038 else
1039 ep->par[2] = CODEL_ECN_ENABLED;
1040
1041 while (*ac > 0) {
1042 int tok = match_token(aqm_params, *av);
1043 (*ac)--; av++;
1044 switch(tok) {
1045 case TOK_TARGET:
1046 if (*ac <= 0 || time_to_us(av[0]) < 0)
1047 errx(EX_DATAERR, "target needs time\n");
1048
1049 ep->par[0] = time_to_us(av[0]);
1050 (*ac)--; av++;
1051 break;
1052
1053 case TOK_INTERVAL:
1054 if (*ac <= 0 || time_to_us(av[0]) < 0)
1055 errx(EX_DATAERR, "interval needs time\n");
1056
1057 ep->par[1] = time_to_us(av[0]);
1058 (*ac)--; av++;
1059 break;
1060
1061 case TOK_ECN:
1062 ep->par[2] = CODEL_ECN_ENABLED;
1063 break;
1064 case TOK_NO_ECN:
1065 ep->par[2] &= ~CODEL_ECN_ENABLED;
1066 break;
1067 /* Config fq_codel parameters */
1068 case TOK_QUANTUM:
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");
1073
1074 ep->par[3]= atoi(av[0]);
1075 (*ac)--; av++;
1076 break;
1077
1078 case TOK_LIMIT:
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");
1083
1084 ep->par[4] = atoi(av[0]);
1085 (*ac)--; av++;
1086 break;
1087
1088 case TOK_FLOWS:
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");
1093
1094 ep->par[5] = atoi(av[0]);
1095 (*ac)--; av++;
1096 break;
1097
1098 default:
1099 printf("%s is Invalid parameter\n", av[-1]);
1100 }
1101 }
1102 break;
1103 case TOK_PIE:
1104 case TOK_FQ_PIE:
1105 /* PIE
1106 * 0- target , 1- tupdate, 2- max_burst,
1107 * 3- max_ecnth, 4- alpha,
1108 * 5- beta, 6- flags
1109 * FQ_CODEL
1110 * 7- quantum, 8- limit, 9- flows
1111 */
1112
1113 if ( type == TOK_PIE)
1114 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
1115 | PIE_DERAND_ENABLED;
1116 else
1117 /* for FQ-PIE, use TS mode */
1118 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DERAND_ENABLED
1119 | PIE_ECN_ENABLED;
1120
1121 while (*ac > 0) {
1122 int tok = match_token(aqm_params, *av);
1123 (*ac)--; av++;
1124 switch(tok) {
1125 case TOK_TARGET:
1126 if (*ac <= 0 || time_to_us(av[0]) < 0)
1127 errx(EX_DATAERR, "target needs time\n");
1128
1129 ep->par[0] = time_to_us(av[0]);
1130 (*ac)--; av++;
1131 break;
1132
1133 case TOK_TUPDATE:
1134 if (*ac <= 0 || time_to_us(av[0]) < 0)
1135 errx(EX_DATAERR, "tupdate needs time\n");
1136
1137 ep->par[1] = time_to_us(av[0]);
1138 (*ac)--; av++;
1139 break;
1140
1141 case TOK_MAX_BURST:
1142 if (*ac <= 0 || time_to_us(av[0]) < 0)
1143 errx(EX_DATAERR, "max_burst needs time\n");
1144
1145 ep->par[2] = time_to_us(av[0]);
1146 (*ac)--; av++;
1147 break;
1148
1149 case TOK_MAX_ECNTH:
1150 if (*ac <= 0 || !is_valid_number(av[0]))
1151 errx(EX_DATAERR, "max_ecnth needs number\n");
1152
1153 ep->par[3] = atof(av[0]) * PIE_SCALE;
1154 (*ac)--; av++;
1155 break;
1156
1157 case TOK_ALPHA:
1158 if (*ac <= 0 || !is_valid_number(av[0]))
1159 errx(EX_DATAERR, "alpha needs number\n");
1160
1161 ep->par[4] = atof(av[0]) * PIE_SCALE;
1162 (*ac)--; av++;
1163 break;
1164
1165 case TOK_BETA:
1166 if (*ac <= 0 || !is_valid_number(av[0]))
1167 errx(EX_DATAERR, "beta needs number\n");
1168
1169 ep->par[5] = atof(av[0]) * PIE_SCALE;
1170 (*ac)--; av++;
1171 break;
1172
1173 case TOK_ECN:
1174 ep->par[6] |= PIE_ECN_ENABLED;
1175 break;
1176 case TOK_NO_ECN:
1177 ep->par[6] &= ~PIE_ECN_ENABLED;
1178 break;
1179
1180 case TOK_CAPDROP:
1181 ep->par[6] |= PIE_CAPDROP_ENABLED;
1182 break;
1183 case TOK_NO_CAPDROP:
1184 ep->par[6] &= ~PIE_CAPDROP_ENABLED;
1185 break;
1186
1187 case TOK_ONOFF:
1188 ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
1189 break;
1190
1191 case TOK_DRE:
1192 ep->par[6] |= PIE_DEPRATEEST_ENABLED;
1193 break;
1194
1195 case TOK_TS:
1196 ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
1197 break;
1198
1199 case TOK_DERAND:
1200 ep->par[6] |= PIE_DERAND_ENABLED;
1201 break;
1202 case TOK_NO_DERAND:
1203 ep->par[6] &= ~PIE_DERAND_ENABLED;
1204 break;
1205
1206 /* Config fq_pie parameters */
1207 case TOK_QUANTUM:
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");
1212
1213 ep->par[7]= atoi(av[0]);
1214 (*ac)--; av++;
1215 break;
1216
1217 case TOK_LIMIT:
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");
1222
1223 ep->par[8] = atoi(av[0]);
1224 (*ac)--; av++;
1225 break;
1226
1227 case TOK_FLOWS:
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");
1232
1233 ep->par[9] = atoi(av[0]);
1234 (*ac)--; av++;
1235 break;
1236
1237
1238 default:
1239 printf("%s is invalid parameter\n", av[-1]);
1240 }
1241 }
1242 break;
1243 }
1244
1245 return 0;
1246 }
1247
1248 #endif
1249
1250
1251 /*
1252 * configuration of pipes, schedulers, flowsets.
1253 * When we configure a new scheduler, an empty pipe is created, so:
1254 *
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>
1261 *
1262 * do_pipe = 2 -> flowset N config
1263 * flowset N parameters
1264 *
1265 * do_pipe = 3 -> sched N config
1266 * sched N parameters (default no pipe)
1267 * optional Pipe N config ...
1268 * pipe ==>
1269 */
1270 void
ipfw_config_pipe(int ac,char ** av)1271 ipfw_config_pipe(int ac, char **av)
1272 {
1273 int i;
1274 u_int j;
1275 char *end;
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;
1282 #ifdef NEW_AQM
1283 struct dn_extra_parms *aqm_extra = NULL;
1284 struct dn_extra_parms *sch_extra = NULL;
1285 int lmax_extra;
1286 #endif
1287
1288 int lmax;
1289 uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
1290
1291 /*
1292 * allocate space for 1 header,
1293 * 1 scheduler, 1 link, 1 flowset, 1 profile
1294 */
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);
1298
1299 #ifdef NEW_AQM
1300 /* Extra Params */
1301 lmax_extra = sizeof(struct dn_extra_parms);
1302 /* two lmax_extra because one for AQM params and another
1303 * sch params
1304 */
1305 lmax += lmax_extra*2;
1306 #endif
1307
1308 av++; ac--;
1309 /* Pipe number */
1310 if (ac && isdigit(**av)) {
1311 i = atoi(*av); av++; ac--;
1312 } else
1313 i = -1;
1314 if (i <= 0)
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;
1320
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.
1332 */
1333 #ifdef NEW_AQM
1334 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1335 sch_extra ->oid.subtype = 0; /* don't configure scheduler */
1336 #endif
1337 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1338 p = o_next(&buf, sizeof(*p), DN_LINK);
1339 #ifdef NEW_AQM
1340 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1341 aqm_extra ->oid.subtype = 0; /* don't configure AQM */
1342 #endif
1343 fs = o_next(&buf, sizeof(*fs), DN_FS);
1344
1345 sch->sched_nr = i;
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;
1351
1352 p->link_nr = i;
1353
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;
1357 break;
1358
1359 case 2: /* "queue N config ... " */
1360 #ifdef NEW_AQM
1361 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1362 aqm_extra ->oid.subtype = 0;
1363 #endif
1364 fs = o_next(&buf, sizeof(*fs), DN_FS);
1365 fs->fs_nr = i;
1366 mask = &fs->flow_mask;
1367 flags = &fs->flags;
1368 buckets = &fs->buckets;
1369 break;
1370
1371 case 3: /* "sched N config ..." */
1372 #ifdef NEW_AQM
1373 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1374 sch_extra ->oid.subtype = 0;
1375 #endif
1376 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1377 #ifdef NEW_AQM
1378 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1379 aqm_extra ->oid.subtype = 0;
1380 #endif
1381 fs = o_next(&buf, sizeof(*fs), DN_FS);
1382 sch->sched_nr = i;
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;
1388 fs->sched_nr = i;
1389 break;
1390 }
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.
1395 */
1396 if (p)
1397 p->bandwidth = -1;
1398 for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
1399 fs->par[j] = -1;
1400 while (ac > 0) {
1401 double d;
1402 int tok = match_token(dummynet_params, *av);
1403 ac--; av++;
1404
1405 switch(tok) {
1406 case TOK_NOERROR:
1407 NEED(fs, "noerror is only for pipes");
1408 fs->flags |= DN_NOERROR;
1409 break;
1410
1411 case TOK_PLR:
1412 NEED(fs, "plr is only for pipes");
1413 NEED1("plr needs argument 0..1\n");
1414 d = strtod(av[0], NULL);
1415 if (d > 1)
1416 d = 1;
1417 else if (d < 0)
1418 d = 0;
1419 fs->plr = (int)(d*0x7fffffff);
1420 ac--; av++;
1421 break;
1422
1423 case TOK_QUEUE:
1424 NEED(fs, "queue is only for pipes or flowsets");
1425 NEED1("queue needs queue size\n");
1426 end = NULL;
1427 fs->qsize = strtoul(av[0], &end, 0);
1428 if (*end == 'K' || *end == 'k') {
1429 fs->flags |= DN_QSIZE_BYTES;
1430 fs->qsize *= 1024;
1431 } else if (*end == 'B' ||
1432 _substrcmp2(end, "by", "bytes") == 0) {
1433 fs->flags |= DN_QSIZE_BYTES;
1434 }
1435 ac--; av++;
1436 break;
1437
1438 case TOK_BUCKETS:
1439 NEED(fs, "buckets is only for pipes or flowsets");
1440 NEED1("buckets needs argument\n");
1441 *buckets = strtoul(av[0], NULL, 0);
1442 ac--; av++;
1443 break;
1444
1445 case TOK_FLOW_MASK:
1446 case TOK_SCHED_MASK:
1447 case TOK_MASK:
1448 NEED(mask, "tok_mask");
1449 NEED1("mask needs mask specifier\n");
1450 /*
1451 * per-flow queue, mask is dst_ip, dst_port,
1452 * src_ip, src_port, proto measured in bits
1453 */
1454
1455 bzero(mask, sizeof(*mask));
1456 end = NULL;
1457
1458 while (ac >= 1) {
1459 uint32_t *p32 = NULL;
1460 uint16_t *p16 = NULL;
1461 uint32_t *p20 = NULL;
1462 struct in6_addr *pa6 = NULL;
1463 uint32_t a;
1464
1465 tok = match_token(dummynet_params, *av);
1466 ac--; av++;
1467 switch(tok) {
1468 case TOK_ALL:
1469 /*
1470 * special case, all bits significant
1471 * except 'extra' (the queue number)
1472 */
1473 mask->dst_ip = ~0;
1474 mask->src_ip = ~0;
1475 mask->dst_port = ~0;
1476 mask->src_port = ~0;
1477 mask->proto = ~0;
1478 n2mask(&mask->dst_ip6, 128);
1479 n2mask(&mask->src_ip6, 128);
1480 mask->flow_id6 = ~0;
1481 *flags |= DN_HAVE_MASK;
1482 goto end_mask;
1483
1484 case TOK_QUEUE:
1485 mask->extra = ~0;
1486 *flags |= DN_HAVE_MASK;
1487 goto end_mask;
1488
1489 case TOK_DSTIP:
1490 mask->addr_type = 4;
1491 p32 = &mask->dst_ip;
1492 break;
1493
1494 case TOK_SRCIP:
1495 mask->addr_type = 4;
1496 p32 = &mask->src_ip;
1497 break;
1498
1499 case TOK_DSTIP6:
1500 mask->addr_type = 6;
1501 pa6 = &mask->dst_ip6;
1502 break;
1503
1504 case TOK_SRCIP6:
1505 mask->addr_type = 6;
1506 pa6 = &mask->src_ip6;
1507 break;
1508
1509 case TOK_FLOWID:
1510 mask->addr_type = 6;
1511 p20 = &mask->flow_id6;
1512 break;
1513
1514 case TOK_DSTPORT:
1515 p16 = &mask->dst_port;
1516 break;
1517
1518 case TOK_SRCPORT:
1519 p16 = &mask->src_port;
1520 break;
1521
1522 case TOK_PROTO:
1523 break;
1524
1525 default:
1526 ac++; av--; /* backtrack */
1527 goto end_mask;
1528 }
1529 if (ac < 1)
1530 errx(EX_USAGE, "mask: value missing");
1531 if (*av[0] == '/') {
1532 a = strtoul(av[0]+1, &end, 0);
1533 if (pa6 == NULL)
1534 a = (a == 32) ? ~0 : (1 << a) - 1;
1535 } else
1536 a = strtoul(av[0], &end, 0);
1537 if (p32 != NULL)
1538 *p32 = a;
1539 else if (p16 != NULL) {
1540 if (a > 0xFFFF)
1541 errx(EX_DATAERR,
1542 "port mask must be 16 bit");
1543 *p16 = (uint16_t)a;
1544 } else if (p20 != NULL) {
1545 if (a > 0xfffff)
1546 errx(EX_DATAERR,
1547 "flow_id mask must be 20 bit");
1548 *p20 = (uint32_t)a;
1549 } else if (pa6 != NULL) {
1550 if (a > 128)
1551 errx(EX_DATAERR,
1552 "in6addr invalid mask len");
1553 else
1554 n2mask(pa6, a);
1555 } else {
1556 if (a > 0xFF)
1557 errx(EX_DATAERR,
1558 "proto mask must be 8 bit");
1559 mask->proto = (uint8_t)a;
1560 }
1561 if (a != 0)
1562 *flags |= DN_HAVE_MASK;
1563 ac--; av++;
1564 } /* end while, config masks */
1565 end_mask:
1566 break;
1567 #ifdef NEW_AQM
1568 case TOK_CODEL:
1569 case TOK_PIE:
1570 NEED(fs, "codel/pie is only for flowsets");
1571
1572 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1573 fs->flags |= DN_IS_AQM;
1574
1575 strlcpy(aqm_extra->name, av[-1],
1576 sizeof(aqm_extra->name));
1577 aqm_extra->oid.subtype = DN_AQM_PARAMS;
1578
1579 process_extra_parms(&ac, av, aqm_extra, tok);
1580 break;
1581
1582 case TOK_FQ_CODEL:
1583 case TOK_FQ_PIE:
1584 if (!strcmp(av[-1],"type"))
1585 errx(EX_DATAERR, "use type before fq_codel/fq_pie");
1586
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);
1592 break;
1593 #endif
1594 case TOK_RED:
1595 case TOK_GRED:
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;
1600 /*
1601 * the format for parameters is w_q/min_th/max_th/max_p
1602 */
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));
1608 }
1609 if ((end = strsep(&av[0], "/"))) {
1610 fs->min_th = strtoul(end, &end, 0);
1611 if (*end == 'K' || *end == 'k')
1612 fs->min_th *= 1024;
1613 }
1614 if ((end = strsep(&av[0], "/"))) {
1615 fs->max_th = strtoul(end, &end, 0);
1616 if (*end == 'K' || *end == 'k')
1617 fs->max_th *= 1024;
1618 }
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));
1624 }
1625 ac--; av++;
1626 break;
1627
1628 case TOK_ECN:
1629 fs->flags |= DN_IS_ECN;
1630 break;
1631
1632 case TOK_DROPTAIL:
1633 NEED(fs, "droptail is only for flowsets");
1634 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1635 break;
1636
1637 case TOK_BW:
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);
1641 ac--; av++;
1642 break;
1643
1644 case TOK_DELAY:
1645 NEED(p, "delay is only for links");
1646 NEED1("delay needs argument 0..10000ms\n");
1647 p->delay = strtoul(av[0], NULL, 0);
1648 ac--; av++;
1649 break;
1650
1651 case TOK_TYPE: {
1652 int l;
1653 NEED(sch, "type is only for schedulers");
1654 NEED1("type needs a string");
1655 l = strlen(av[0]);
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 */
1660 #ifdef NEW_AQM
1661 /* if fq_codel is selected, consider all tokens after it
1662 * as parameters
1663 */
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);
1669 } else {
1670 ac--;av++;
1671 }
1672 #else
1673 ac--;av++;
1674 #endif
1675 break;
1676 }
1677
1678 case TOK_WEIGHT:
1679 NEED(fs, "weight is only for flowsets");
1680 NEED1("weight needs argument\n");
1681 fs->par[0] = strtol(av[0], &end, 0);
1682 ac--; av++;
1683 break;
1684
1685 case TOK_LMAX:
1686 NEED(fs, "lmax is only for flowsets");
1687 NEED1("lmax needs argument\n");
1688 fs->par[1] = strtol(av[0], &end, 0);
1689 ac--; av++;
1690 break;
1691
1692 case TOK_PRI:
1693 NEED(fs, "priority is only for flowsets");
1694 NEED1("priority needs argument\n");
1695 fs->par[2] = strtol(av[0], &end, 0);
1696 ac--; av++;
1697 break;
1698
1699 case TOK_SCHED:
1700 case TOK_PIPE:
1701 NEED(fs, "pipe/sched");
1702 NEED1("pipe/link/sched needs number\n");
1703 fs->sched_nr = strtoul(av[0], &end, 0);
1704 ac--; av++;
1705 break;
1706
1707 case TOK_PROFILE:
1708 NEED((!pf), "profile already set");
1709 NEED(p, "profile");
1710 {
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?
1714 --ac; ++av;
1715 }
1716 break;
1717
1718 case TOK_BURST:
1719 NEED(p, "burst");
1720 NEED1("burst needs argument\n");
1721 errno = 0;
1722 if (expand_number(av[0], &p->burst) < 0)
1723 if (errno != ERANGE)
1724 errx(EX_DATAERR,
1725 "burst: invalid argument");
1726 if (errno || p->burst > (1ULL << 48) - 1)
1727 errx(EX_DATAERR,
1728 "burst: out of range (0..2^48-1)");
1729 ac--; av++;
1730 break;
1731
1732 default:
1733 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1734 }
1735 }
1736
1737 /* check validity of parameters */
1738 if (p) {
1739 if (p->delay > 10000)
1740 errx(EX_DATAERR, "delay must be < 10000");
1741 if (p->bandwidth == (uint32_t)-1)
1742 p->bandwidth = 0;
1743 }
1744 if (fs) {
1745 /* XXX accept a 0 scheduler to keep the default */
1746 if (fs->flags & DN_QSIZE_BYTES) {
1747 size_t len;
1748 long limit;
1749
1750 len = sizeof(limit);
1751 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1752 &limit, &len, NULL, 0) == -1)
1753 limit = 1024*1024;
1754 if (fs->qsize > limit)
1755 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1756 } else {
1757 size_t len;
1758 long limit;
1759
1760 len = sizeof(limit);
1761 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1762 &limit, &len, NULL, 0) == -1)
1763 limit = 100;
1764 if (fs->qsize > limit)
1765 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1766 }
1767
1768 #ifdef NEW_AQM
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!");
1773 #else
1774 if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1775 errx(EX_USAGE, "enable red/gred for ECN");
1776
1777 #endif
1778
1779 if (fs->flags & DN_IS_RED) {
1780 size_t len;
1781 int lookup_depth, avg_pkt_size;
1782
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);
1789
1790 if (fs->max_th == 0)
1791 errx(EX_DATAERR, "max_th must be > 0");
1792
1793 len = sizeof(int);
1794 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1795 &lookup_depth, &len, NULL, 0) == -1)
1796 lookup_depth = 256;
1797 if (lookup_depth == 0)
1798 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1799 " must be greater than zero");
1800
1801 len = sizeof(int);
1802 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1803 &avg_pkt_size, &len, NULL, 0) == -1)
1804 avg_pkt_size = 512;
1805
1806 if (avg_pkt_size == 0)
1807 errx(EX_DATAERR,
1808 "net.inet.ip.dummynet.red_avg_pkt_size must"
1809 " be greater than zero");
1810
1811 #if 0 /* the following computation is now done in the kernel */
1812 /*
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
1819 * WF2Q+ ?
1820 */
1821 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1822 s = 0;
1823 else
1824 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1825 /*
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.
1829 */
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;
1835 weight = 1 - w_q;
1836 for (t = fs->lookup_step; t > 1; --t)
1837 weight *= 1 - w_q;
1838 fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1839 #endif /* code moved in the kernel */
1840 }
1841 }
1842
1843 i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1844
1845 if (i)
1846 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1847 }
1848
1849 void
dummynet_flush(void)1850 dummynet_flush(void)
1851 {
1852 struct dn_id oid;
1853 oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1854 do_cmd(IP_DUMMYNET3, &oid, oid.len);
1855 }
1856
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.
1860 */
1861 static int
parse_range(int ac,char * av[],uint32_t * v,int len)1862 parse_range(int ac, char *av[], uint32_t *v, int len)
1863 {
1864 int n = 0;
1865 char *endptr, *s;
1866 uint32_t base[2];
1867
1868 if (v == NULL || len < 2) {
1869 v = base;
1870 len = 2;
1871 }
1872
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;
1879 } else {
1880 if (*endptr != ',') {
1881 warn("invalid number: %s", s);
1882 s = ++endptr;
1883 continue;
1884 }
1885 /* continue processing from here */
1886 s = ++endptr;
1887 ac++;
1888 av--;
1889 }
1890 if (v[1] < v[0] ||
1891 v[0] >= DN_MAX_ID-1 ||
1892 v[1] >= DN_MAX_ID-1) {
1893 continue; /* invalid entry */
1894 }
1895 n++;
1896 /* translate if 'pipe list' */
1897 if (g_co.do_pipe == 1) {
1898 v[0] += DN_MAX_ID;
1899 v[1] += DN_MAX_ID;
1900 }
1901 v = (n*2 < len) ? v + 2 : base;
1902 }
1903 return n;
1904 }
1905
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
1914 */
1915 void
dummynet_list(int ac,char * av[],int show_counters)1916 dummynet_list(int ac, char *av[], int show_counters)
1917 {
1918 struct dn_id *oid, *x = NULL;
1919 int ret, i;
1920 int n; /* # of ranges */
1921 u_int buflen, l;
1922 u_int max_size; /* largest obj passed up */
1923
1924 (void)show_counters; // XXX unused, but we should use it.
1925 ac--;
1926 av++; /* skip 'list' | 'show' word */
1927
1928 n = parse_range(ac, av, NULL, 0); /* Count # of ranges. */
1929
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);
1934
1935 if (n > 0) /* store ranges in idx */
1936 parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1937 /*
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.
1944 */
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);
1950
1951 switch (g_co.do_pipe) {
1952 case 1:
1953 oid->subtype = DN_LINK; /* list pipe */
1954 break;
1955 case 2:
1956 oid->subtype = DN_FS; /* list queue */
1957 break;
1958 case 3:
1959 oid->subtype = DN_SCH; /* list sched */
1960 break;
1961 }
1962
1963 /*
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.
1969 */
1970 if (n > 0) {
1971 buflen = 4*1024;
1972 } else {
1973 ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1974 if (ret != 0 || oid->id <= sizeof(*oid))
1975 goto done;
1976 buflen = oid->id + max_size;
1977 oid->len = sizeof(*oid); /* restore */
1978 }
1979 /* Try a few times, until the buffer fits */
1980 for (i = 0; i < 20; i++) {
1981 l = buflen;
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)
1988 break; /* ok */
1989 buflen *= 2; /* double for next attempt */
1990 }
1991 list_pipes(x, O_NEXT(x, l));
1992 done:
1993 if (x)
1994 free(x);
1995 free(oid);
1996 }
1997