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 <ralsaadi@swin.edu.au>
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 * dummynet support
21 */
22
23 #define NEW_AQM
24 #include <sys/limits.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 /* XXX there are several sysctl leftover here */
28 #include <sys/sysctl.h>
29
30 #include "ipfw2.h"
31
32 #ifdef NEW_AQM
33 #include <stdint.h>
34 #endif
35
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <libutil.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sysexits.h>
45
46 #include <net/if.h>
47 #include <netinet/in.h>
48 #include <netinet/ip_fw.h>
49 #include <netinet/ip_dummynet.h>
50 #include <arpa/inet.h> /* inet_ntoa */
51
52
53 static struct _s_x dummynet_params[] = {
54 { "plr", TOK_PLR },
55 { "noerror", TOK_NOERROR },
56 { "buckets", TOK_BUCKETS },
57 { "dst-ip", TOK_DSTIP },
58 { "src-ip", TOK_SRCIP },
59 { "dst-port", TOK_DSTPORT },
60 { "src-port", TOK_SRCPORT },
61 { "proto", TOK_PROTO },
62 { "weight", TOK_WEIGHT },
63 { "lmax", TOK_LMAX },
64 { "maxlen", TOK_LMAX },
65 { "all", TOK_ALL },
66 { "mask", TOK_MASK }, /* alias for both */
67 { "sched_mask", TOK_SCHED_MASK },
68 { "flow_mask", TOK_FLOW_MASK },
69 { "droptail", TOK_DROPTAIL },
70 { "ecn", TOK_ECN },
71 { "red", TOK_RED },
72 { "gred", TOK_GRED },
73 #ifdef NEW_AQM
74 { "codel", TOK_CODEL}, /* Codel AQM */
75 { "fq_codel", TOK_FQ_CODEL}, /* FQ-Codel */
76 { "pie", TOK_PIE}, /* PIE AQM */
77 { "fq_pie", TOK_FQ_PIE}, /* FQ-PIE */
78 #endif
79 { "bw", TOK_BW },
80 { "bandwidth", TOK_BW },
81 { "delay", TOK_DELAY },
82 { "link", TOK_LINK },
83 { "pipe", TOK_PIPE },
84 { "queue", TOK_QUEUE },
85 { "flowset", TOK_FLOWSET },
86 { "sched", TOK_SCHED },
87 { "pri", TOK_PRI },
88 { "priority", TOK_PRI },
89 { "type", TOK_TYPE },
90 { "flow-id", TOK_FLOWID},
91 { "dst-ipv6", TOK_DSTIP6},
92 { "dst-ip6", TOK_DSTIP6},
93 { "src-ipv6", TOK_SRCIP6},
94 { "src-ip6", TOK_SRCIP6},
95 { "profile", TOK_PROFILE},
96 { "burst", TOK_BURST},
97 { "dummynet-params", TOK_NULL },
98 { NULL, 0 } /* terminator */
99 };
100
101 #ifdef NEW_AQM
102 /* AQM/extra sched parameters tokens*/
103 static struct _s_x aqm_params[] = {
104 { "target", TOK_TARGET},
105 { "interval", TOK_INTERVAL},
106 { "limit", TOK_LIMIT},
107 { "flows", TOK_FLOWS},
108 { "quantum", TOK_QUANTUM},
109 { "ecn", TOK_ECN},
110 { "noecn", TOK_NO_ECN},
111 { "tupdate", TOK_TUPDATE},
112 { "max_burst", TOK_MAX_BURST},
113 { "max_ecnth", TOK_MAX_ECNTH},
114 { "alpha", TOK_ALPHA},
115 { "beta", TOK_BETA},
116 { "capdrop", TOK_CAPDROP},
117 { "nocapdrop", TOK_NO_CAPDROP},
118 { "onoff", TOK_ONOFF},
119 { "dre", TOK_DRE},
120 { "ts", TOK_TS},
121 { "derand", TOK_DERAND},
122 { "noderand", TOK_NO_DERAND},
123 { NULL, 0 } /* terminator */
124 };
125 #endif
126
127 #define O_NEXT(p, len) ((void *)((char *)p + len))
128
129 static void
oid_fill(struct dn_id * oid,int len,int type,uintptr_t id)130 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id)
131 {
132 oid->len = len;
133 oid->type = type;
134 oid->subtype = 0;
135 oid->id = id;
136 }
137
138 /* make room in the buffer and move the pointer forward */
139 static void *
o_next(struct dn_id ** o,int len,int type)140 o_next(struct dn_id **o, int len, int type)
141 {
142 struct dn_id *ret = *o;
143 oid_fill(ret, len, type, 0);
144 *o = O_NEXT(*o, len);
145 return ret;
146 }
147
148 #ifdef NEW_AQM
149
150 /* Codel flags */
151 enum {
152 CODEL_ECN_ENABLED = 1
153 };
154
155 /* PIE flags, from PIE kernel module */
156 enum {
157 PIE_ECN_ENABLED = 1,
158 PIE_CAPDROP_ENABLED = 2,
159 PIE_ON_OFF_MODE_ENABLED = 4,
160 PIE_DEPRATEEST_ENABLED = 8,
161 PIE_DERAND_ENABLED = 16
162 };
163
164 #define PIE_FIX_POINT_BITS 13
165 #define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)
166
167 /* integer to time */
168 static void
us_to_time(int t,char * strt)169 us_to_time(int t, char *strt)
170 {
171 if (t < 0)
172 strt[0]='\0';
173 else if ( t==0 )
174 sprintf(strt,"%d", t);
175 else if (t< 1000)
176 sprintf(strt,"%dus", t);
177 else if (t < 1000000)
178 sprintf(strt,"%gms", (float) t / 1000);
179 else
180 sprintf(strt,"%gfs", (float) t / 1000000);
181 }
182
183 /*
184 * returns -1 if s is not a valid time, otherwise, return time in us
185 */
186 static long
time_to_us(const char * s)187 time_to_us(const char *s)
188 {
189 int i, dots = 0;
190 int len = strlen(s);
191 char strt[16]="", stru[16]="";
192
193 if (len>15)
194 return -1;
195 for (i = 0; i<len && (isdigit(s[i]) || s[i]=='.') ; i++)
196 if (s[i]=='.') {
197 if (dots)
198 return -1;
199 else
200 dots++;
201 }
202
203 if (!i)
204 return -1;
205 strncpy(strt, s, i);
206 if (i<len)
207 strcpy(stru, s+i);
208 else
209 strcpy(stru, "ms");
210
211 if (!strcasecmp(stru, "us"))
212 return atol(strt);
213 if (!strcasecmp(stru, "ms"))
214 return (strtod(strt, NULL) * 1000);
215 if (!strcasecmp(stru, "s"))
216 return (strtod(strt, NULL)*1000000);
217
218 return -1;
219 }
220
221
222 /* Get AQM or scheduler extra parameters */
223 static void
get_extra_parms(uint32_t nr,char * out,int subtype)224 get_extra_parms(uint32_t nr, char *out, int subtype)
225 {
226 struct dn_extra_parms *ep;
227 int ret;
228 char strt1[15], strt2[15], strt3[15];
229 u_int l;
230
231 /* prepare the request */
232 l = sizeof(struct dn_extra_parms);
233 ep = safe_calloc(1, l);
234 memset(ep, 0, sizeof(*ep));
235 *out = '\0';
236
237 oid_fill(&ep->oid, l, DN_CMD_GET, DN_API_VERSION);
238 ep->oid.len = l;
239 ep->oid.subtype = subtype;
240 ep->nr = nr;
241
242 ret = do_cmd(-IP_DUMMYNET3, ep, (uintptr_t)&l);
243 if (ret) {
244 free(ep);
245 errx(EX_DATAERR, "Error getting extra parameters\n");
246 }
247
248 switch (subtype) {
249 case DN_AQM_PARAMS:
250 if( !strcasecmp(ep->name, "codel")) {
251 us_to_time(ep->par[0], strt1);
252 us_to_time(ep->par[1], strt2);
253 l = sprintf(out, " AQM CoDel target %s interval %s",
254 strt1, strt2);
255 if (ep->par[2] & CODEL_ECN_ENABLED)
256 l = sprintf(out + l, " ECN");
257 else
258 l += sprintf(out + l, " NoECN");
259 } else if( !strcasecmp(ep->name, "pie")) {
260 us_to_time(ep->par[0], strt1);
261 us_to_time(ep->par[1], strt2);
262 us_to_time(ep->par[2], strt3);
263 l = sprintf(out, " AQM type PIE target %s tupdate %s alpha "
264 "%g beta %g max_burst %s max_ecnth %.3g",
265 strt1,
266 strt2,
267 ep->par[4] / (float) PIE_SCALE,
268 ep->par[5] / (float) PIE_SCALE,
269 strt3,
270 ep->par[3] / (float) PIE_SCALE
271 );
272
273 if (ep->par[6] & PIE_ECN_ENABLED)
274 l += sprintf(out + l, " ECN");
275 else
276 l += sprintf(out + l, " NoECN");
277 if (ep->par[6] & PIE_CAPDROP_ENABLED)
278 l += sprintf(out + l, " CapDrop");
279 else
280 l += sprintf(out + l, " NoCapDrop");
281 if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
282 l += sprintf(out + l, " OnOff");
283 if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
284 l += sprintf(out + l, " DRE");
285 else
286 l += sprintf(out + l, " TS");
287 if (ep->par[6] & PIE_DERAND_ENABLED)
288 l += sprintf(out + l, " Derand");
289 else
290 l += sprintf(out + l, " NoDerand");
291 }
292 break;
293
294 case DN_SCH_PARAMS:
295 if (!strcasecmp(ep->name,"FQ_CODEL")) {
296 us_to_time(ep->par[0], strt1);
297 us_to_time(ep->par[1], strt2);
298 l = sprintf(out," FQ_CODEL target %s interval %s"
299 " quantum %jd limit %jd flows %jd",
300 strt1, strt2,
301 (intmax_t) ep->par[3],
302 (intmax_t) ep->par[4],
303 (intmax_t) ep->par[5]
304 );
305 if (ep->par[2] & CODEL_ECN_ENABLED)
306 l += sprintf(out + l, " ECN");
307 else
308 l += sprintf(out + l, " NoECN");
309 l += sprintf(out + l, "\n");
310 } else if (!strcasecmp(ep->name,"FQ_PIE")) {
311 us_to_time(ep->par[0], strt1);
312 us_to_time(ep->par[1], strt2);
313 us_to_time(ep->par[2], strt3);
314 l = sprintf(out, " FQ_PIE target %s tupdate %s alpha "
315 "%g beta %g max_burst %s max_ecnth %.3g"
316 " quantum %jd limit %jd flows %jd",
317 strt1,
318 strt2,
319 ep->par[4] / (float) PIE_SCALE,
320 ep->par[5] / (float) PIE_SCALE,
321 strt3,
322 ep->par[3] / (float) PIE_SCALE,
323 (intmax_t) ep->par[7],
324 (intmax_t) ep->par[8],
325 (intmax_t) ep->par[9]
326 );
327
328 if (ep->par[6] & PIE_ECN_ENABLED)
329 l += sprintf(out + l, " ECN");
330 else
331 l += sprintf(out + l, " NoECN");
332 if (ep->par[6] & PIE_CAPDROP_ENABLED)
333 l += sprintf(out + l, " CapDrop");
334 else
335 l += sprintf(out + l, " NoCapDrop");
336 if (ep->par[6] & PIE_ON_OFF_MODE_ENABLED)
337 l += sprintf(out + l, " OnOff");
338 if (ep->par[6] & PIE_DEPRATEEST_ENABLED)
339 l += sprintf(out + l, " DRE");
340 else
341 l += sprintf(out + l, " TS");
342 if (ep->par[6] & PIE_DERAND_ENABLED)
343 l += sprintf(out + l, " Derand");
344 else
345 l += sprintf(out + l, " NoDerand");
346 l += sprintf(out + l, "\n");
347 }
348 break;
349 }
350
351 free(ep);
352 }
353 #endif
354
355
356 #if 0
357 static int
358 sort_q(void *arg, const void *pa, const void *pb)
359 {
360 int rev = (co.do_sort < 0);
361 int field = rev ? -co.do_sort : co.do_sort;
362 long long res = 0;
363 const struct dn_flow_queue *a = pa;
364 const struct dn_flow_queue *b = pb;
365
366 switch (field) {
367 case 1: /* pkts */
368 res = a->len - b->len;
369 break;
370 case 2: /* bytes */
371 res = a->len_bytes - b->len_bytes;
372 break;
373
374 case 3: /* tot pkts */
375 res = a->tot_pkts - b->tot_pkts;
376 break;
377
378 case 4: /* tot bytes */
379 res = a->tot_bytes - b->tot_bytes;
380 break;
381 }
382 if (res < 0)
383 res = -1;
384 if (res > 0)
385 res = 1;
386 return (int)(rev ? res : -res);
387 }
388 #endif
389
390 /* print a mask and header for the subsequent list of flows */
391 static void
print_mask(struct ipfw_flow_id * id)392 print_mask(struct ipfw_flow_id *id)
393 {
394 if (!IS_IP6_FLOW_ID(id)) {
395 printf(" "
396 "mask: %s 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
397 id->extra ? "queue," : "",
398 id->proto,
399 id->src_ip, id->src_port,
400 id->dst_ip, id->dst_port);
401 } else {
402 char buf[255];
403 printf("\n mask: %sproto: 0x%02x, flow_id: 0x%08x, ",
404 id->extra ? "queue," : "",
405 id->proto, id->flow_id6);
406 inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf));
407 printf("%s/0x%04x -> ", buf, id->src_port);
408 inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf));
409 printf("%s/0x%04x\n", buf, id->dst_port);
410 }
411 }
412
413 static void
print_header(struct ipfw_flow_id * id)414 print_header(struct ipfw_flow_id *id)
415 {
416 if (!IS_IP6_FLOW_ID(id))
417 printf("BKT Prot ___Source IP/port____ "
418 "____Dest. IP/port____ "
419 "Tot_pkt/bytes Pkt/Byte Drp\n");
420 else
421 printf("BKT ___Prot___ _flow-id_ "
422 "______________Source IPv6/port_______________ "
423 "_______________Dest. IPv6/port_______________ "
424 "Tot_pkt/bytes Pkt/Byte Drp\n");
425 }
426
427 static void
list_flow(struct buf_pr * bp,struct dn_flow * ni)428 list_flow(struct buf_pr *bp, struct dn_flow *ni)
429 {
430 char buff[255];
431 struct protoent *pe = NULL;
432 struct in_addr ina;
433 struct ipfw_flow_id *id = &ni->fid;
434
435 pe = getprotobynumber(id->proto);
436 /* XXX: Should check for IPv4 flows */
437 bprintf(bp, "%3u%c", (ni->oid.id) & 0xff,
438 id->extra ? '*' : ' ');
439 if (!IS_IP6_FLOW_ID(id)) {
440 if (pe)
441 bprintf(bp, "%-4s ", pe->p_name);
442 else
443 bprintf(bp, "%4u ", id->proto);
444 ina.s_addr = htonl(id->src_ip);
445 bprintf(bp, "%15s/%-5d ",
446 inet_ntoa(ina), id->src_port);
447 ina.s_addr = htonl(id->dst_ip);
448 bprintf(bp, "%15s/%-5d ",
449 inet_ntoa(ina), id->dst_port);
450 } else {
451 /* Print IPv6 flows */
452 if (pe != NULL)
453 bprintf(bp, "%9s ", pe->p_name);
454 else
455 bprintf(bp, "%9u ", id->proto);
456 bprintf(bp, "%7d %39s/%-5d ", id->flow_id6,
457 inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)),
458 id->src_port);
459 bprintf(bp, " %39s/%-5d ",
460 inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)),
461 id->dst_port);
462 }
463 pr_u64(bp, &ni->tot_pkts, 4);
464 pr_u64(bp, &ni->tot_bytes, 8);
465 bprintf(bp, "%2u %4u %3u",
466 ni->length, ni->len_bytes, ni->drops);
467 }
468
469 static void
print_flowset_parms(struct dn_fs * fs,char * prefix)470 print_flowset_parms(struct dn_fs *fs, char *prefix)
471 {
472 int l;
473 char qs[30];
474 char plr[30];
475 char red[200]; /* Display RED parameters */
476
477 l = fs->qsize;
478 if (fs->flags & DN_QSIZE_BYTES) {
479 if (l >= 8192)
480 sprintf(qs, "%d KB", l / 1024);
481 else
482 sprintf(qs, "%d B", l);
483 } else
484 sprintf(qs, "%3d sl.", l);
485 if (fs->plr)
486 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
487 else
488 plr[0] = '\0';
489
490 if (fs->flags & DN_IS_RED) { /* RED parameters */
491 sprintf(red,
492 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f",
493 (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ',
494 1.0 * fs->w_q / (double)(1 << SCALE_RED),
495 fs->min_th,
496 fs->max_th,
497 1.0 * fs->max_p / (double)(1 << SCALE_RED));
498 if (fs->flags & DN_IS_ECN)
499 strlcat(red, " (ecn)", sizeof(red));
500 #ifdef NEW_AQM
501 /* get AQM parameters */
502 } else if (fs->flags & DN_IS_AQM) {
503 get_extra_parms(fs->fs_nr, red, DN_AQM_PARAMS);
504 #endif
505 } else
506 sprintf(red, "droptail");
507
508 if (prefix[0]) {
509 printf("%s %s%s %d queues (%d buckets) %s\n",
510 prefix, qs, plr, fs->oid.id, fs->buckets, red);
511 prefix[0] = '\0';
512 } else {
513 printf("q%05d %s%s %d flows (%d buckets) sched %d "
514 "weight %d lmax %d pri %d %s\n",
515 fs->fs_nr, qs, plr, fs->oid.id, fs->buckets,
516 fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red);
517 if (fs->flags & DN_HAVE_MASK)
518 print_mask(&fs->flow_mask);
519 }
520 }
521
522 static void
print_extra_delay_parms(struct dn_profile * p)523 print_extra_delay_parms(struct dn_profile *p)
524 {
525 double loss;
526 if (p->samples_no <= 0)
527 return;
528
529 loss = p->loss_level;
530 loss /= p->samples_no;
531 printf("\t profile: name \"%s\" loss %f samples %d\n",
532 p->name, loss, p->samples_no);
533 }
534
535 static void
flush_buf(char * buf)536 flush_buf(char *buf)
537 {
538 if (buf[0])
539 printf("%s\n", buf);
540 buf[0] = '\0';
541 }
542
543 /*
544 * generic list routine. We expect objects in a specific order, i.e.
545 * PIPES AND SCHEDULERS:
546 * link; scheduler; internal flowset if any; instances
547 * we can tell a pipe from the number.
548 *
549 * FLOWSETS:
550 * flowset; queues;
551 * link i (int queue); scheduler i; si(i) { flowsets() : queues }
552 */
553 static void
list_pipes(struct dn_id * oid,struct dn_id * end)554 list_pipes(struct dn_id *oid, struct dn_id *end)
555 {
556 char buf[160]; /* pending buffer */
557 int toPrint = 1; /* print header */
558 struct buf_pr bp;
559
560 buf[0] = '\0';
561 bp_alloc(&bp, 4096);
562 for (; oid != end; oid = O_NEXT(oid, oid->len)) {
563 if (oid->len < sizeof(*oid))
564 errx(1, "invalid oid len %d\n", oid->len);
565
566 switch (oid->type) {
567 default:
568 flush_buf(buf);
569 printf("unrecognized object %d size %d\n", oid->type, oid->len);
570 break;
571 case DN_TEXT: /* list of attached flowsets */
572 {
573 int i, l;
574 struct {
575 struct dn_id id;
576 uint32_t p[0];
577 } *d = (void *)oid;
578 l = (oid->len - sizeof(*oid))/sizeof(d->p[0]);
579 if (l == 0)
580 break;
581 printf(" Children flowsets: ");
582 for (i = 0; i < l; i++)
583 printf("%u ", d->p[i]);
584 printf("\n");
585 break;
586 }
587 case DN_CMD_GET:
588 if (g_co.verbose)
589 printf("answer for cmd %d, len %d\n", oid->type, oid->id);
590 break;
591 case DN_SCH: {
592 struct dn_sch *s = (struct dn_sch *)oid;
593 flush_buf(buf);
594 printf(" sched %d type %s flags 0x%x %d buckets %d active\n",
595 s->sched_nr,
596 s->name, s->flags, s->buckets, s->oid.id);
597 #ifdef NEW_AQM
598 char parms[200];
599 get_extra_parms(s->sched_nr, parms, DN_SCH_PARAMS);
600 printf("%s",parms);
601 #endif
602 if (s->flags & DN_HAVE_MASK)
603 print_mask(&s->sched_mask);
604 }
605 break;
606
607 case DN_FLOW:
608 if (toPrint != 0) {
609 print_header(&((struct dn_flow *)oid)->fid);
610 toPrint = 0;
611 }
612 list_flow(&bp, (struct dn_flow *)oid);
613 printf("%s\n", bp.buf);
614 bp_flush(&bp);
615 break;
616
617 case DN_LINK: {
618 struct dn_link *p = (struct dn_link *)oid;
619 double b = p->bandwidth;
620 char bwbuf[30];
621 char burst[5 + 7];
622
623 /* This starts a new object so flush buffer */
624 flush_buf(buf);
625 /* data rate */
626 if (b == 0)
627 sprintf(bwbuf, "unlimited ");
628 else if (b >= 1000000000)
629 sprintf(bwbuf, "%7.3f Gbit/s", b/1000000000);
630 else if (b >= 1000000)
631 sprintf(bwbuf, "%7.3f Mbit/s", b/1000000);
632 else if (b >= 1000)
633 sprintf(bwbuf, "%7.3f Kbit/s", b/1000);
634 else
635 sprintf(bwbuf, "%7.3f bit/s ", b);
636
637 if (humanize_number(burst, sizeof(burst), p->burst,
638 "", HN_AUTOSCALE, 0) < 0 || g_co.verbose)
639 sprintf(burst, "%d", (int)p->burst);
640 sprintf(buf, "%05d: %s %4d ms burst %s",
641 p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst);
642 }
643 break;
644
645 case DN_FS:
646 print_flowset_parms((struct dn_fs *)oid, buf);
647 break;
648 case DN_PROFILE:
649 flush_buf(buf);
650 print_extra_delay_parms((struct dn_profile *)oid);
651 }
652 flush_buf(buf); // XXX does it really go here ?
653 }
654
655 bp_free(&bp);
656 }
657
658 /*
659 * Delete pipe, queue or scheduler i
660 */
661 int
ipfw_delete_pipe(int do_pipe,int i)662 ipfw_delete_pipe(int do_pipe, int i)
663 {
664 struct {
665 struct dn_id oid;
666 uintptr_t a[1]; /* add more if we want a list */
667 } cmd;
668 oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION);
669 cmd.oid.subtype = (do_pipe == 1) ? DN_LINK :
670 ( (do_pipe == 2) ? DN_FS : DN_SCH);
671 cmd.a[0] = i;
672 i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len);
673 if (i) {
674 i = 1;
675 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i);
676 }
677 return i;
678 }
679
680 /*
681 * Code to parse delay profiles.
682 *
683 * Some link types introduce extra delays in the transmission
684 * of a packet, e.g. because of MAC level framing, contention on
685 * the use of the channel, MAC level retransmissions and so on.
686 * From our point of view, the channel is effectively unavailable
687 * for this extra time, which is constant or variable depending
688 * on the link type. Additionally, packets may be dropped after this
689 * time (e.g. on a wireless link after too many retransmissions).
690 * We can model the additional delay with an empirical curve
691 * that represents its distribution.
692 *
693 * cumulative probability
694 * 1.0 ^
695 * |
696 * L +-- loss-level x
697 * | ******
698 * | *
699 * | *****
700 * | *
701 * | **
702 * | *
703 * +-------*------------------->
704 * delay
705 *
706 * The empirical curve may have both vertical and horizontal lines.
707 * Vertical lines represent constant delay for a range of
708 * probabilities; horizontal lines correspond to a discontinuty
709 * in the delay distribution: the link will use the largest delay
710 * for a given probability.
711 *
712 * To pass the curve to dummynet, we must store the parameters
713 * in a file as described below, and issue the command
714 *
715 * ipfw pipe <n> config ... bw XXX profile <filename> ...
716 *
717 * The file format is the following, with whitespace acting as
718 * a separator and '#' indicating the beginning a comment:
719 *
720 * samples N
721 * the number of samples used in the internal
722 * representation (2..1024; default 100);
723 *
724 * loss-level L
725 * The probability above which packets are lost.
726 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss);
727 *
728 * name identifier
729 * Optional a name (listed by "ipfw pipe show")
730 * to identify the distribution;
731 *
732 * "delay prob" | "prob delay"
733 * One of these two lines is mandatory and defines
734 * the format of the following lines with data points.
735 *
736 * XXX YYY
737 * 2 or more lines representing points in the curve,
738 * with either delay or probability first, according
739 * to the chosen format.
740 * The unit for delay is milliseconds.
741 *
742 * Data points does not need to be ordered or equal to the number
743 * specified in the "samples" line. ipfw will sort and interpolate
744 * the curve as needed.
745 *
746 * Example of a profile file:
747
748 name bla_bla_bla
749 samples 100
750 loss-level 0.86
751 prob delay
752 0 200 # minimum overhead is 200ms
753 0.5 200
754 0.5 300
755 0.8 1000
756 0.9 1300
757 1 1300
758
759 * Internally, we will convert the curve to a fixed number of
760 * samples, and when it is time to transmit a packet we will
761 * model the extra delay as extra bits in the packet.
762 *
763 */
764
765 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN
766 #define ED_TOK_SAMPLES "samples"
767 #define ED_TOK_LOSS "loss-level"
768 #define ED_TOK_NAME "name"
769 #define ED_TOK_DELAY "delay"
770 #define ED_TOK_PROB "prob"
771 #define ED_TOK_BW "bw"
772 #define ED_SEPARATORS " \t\n"
773 #define ED_MIN_SAMPLES_NO 2
774
775 /*
776 * returns 1 if s is a non-negative number, with at least one '.'
777 */
778 static int
is_valid_number(const char * s)779 is_valid_number(const char *s)
780 {
781 int i, dots_found = 0;
782 int len = strlen(s);
783
784 for (i = 0; i<len; ++i)
785 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1))
786 return 0;
787 return 1;
788 }
789
790 /*
791 * Take as input a string describing a bandwidth value
792 * and return the numeric bandwidth value.
793 * set clocking interface or bandwidth value
794 */
795 static void
read_bandwidth(char * arg,uint32_t * bandwidth,char * if_name,int namelen)796 read_bandwidth(char *arg, uint32_t *bandwidth, char *if_name, int namelen)
797 {
798 if (*bandwidth != (uint32_t)-1)
799 warnx("duplicate token, override bandwidth value!");
800
801 if (arg[0] >= 'a' && arg[0] <= 'z') {
802 if (!if_name) {
803 errx(1, "no if support");
804 }
805 if (namelen >= IFNAMSIZ)
806 warn("interface name truncated");
807 namelen--;
808 /* interface name */
809 strlcpy(if_name, arg, namelen);
810 *bandwidth = 0;
811 } else { /* read bandwidth value */
812 uint64_t bw;
813 char *end = NULL;
814
815 bw = strtoul(arg, &end, 0);
816 if (*end == 'K' || *end == 'k') {
817 end++;
818 bw *= 1000;
819 } else if (*end == 'M' || *end == 'm') {
820 end++;
821 bw *= 1000000;
822 } else if (*end == 'G' || *end == 'g') {
823 end++;
824 bw *= 1000000000;
825 }
826 if ((*end == 'B' &&
827 _substrcmp2(end, "Bi", "Bit/s") != 0) ||
828 _substrcmp2(end, "by", "bytes") == 0)
829 bw *= 8;
830
831 if (bw > UINT_MAX)
832 errx(EX_DATAERR, "bandwidth too large");
833
834 *bandwidth = (uint32_t)bw;
835 if (if_name)
836 if_name[0] = '\0';
837 }
838 }
839
840 struct point {
841 double prob;
842 double delay;
843 };
844
845 static int
compare_points(const void * vp1,const void * vp2)846 compare_points(const void *vp1, const void *vp2)
847 {
848 const struct point *p1 = vp1;
849 const struct point *p2 = vp2;
850 double res = 0;
851
852 res = p1->prob - p2->prob;
853 if (res == 0)
854 res = p1->delay - p2->delay;
855 if (res < 0)
856 return -1;
857 else if (res > 0)
858 return 1;
859 else
860 return 0;
861 }
862
863 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno
864
865 static void
load_extra_delays(const char * filename,struct dn_profile * p,struct dn_link * link)866 load_extra_delays(const char *filename, struct dn_profile *p,
867 struct dn_link *link)
868 {
869 char line[ED_MAX_LINE_LEN];
870 FILE *f;
871 int lineno = 0;
872 int i;
873
874 int samples = -1;
875 double loss = -1.0;
876 char profile_name[ED_MAX_NAME_LEN];
877 int delay_first = -1;
878 int do_points = 0;
879 struct point points[ED_MAX_SAMPLES_NO];
880 int points_no = 0;
881
882 /* XXX link never NULL? */
883 p->link_nr = link->link_nr;
884
885 profile_name[0] = '\0';
886 f = fopen(filename, "r");
887 if (f == NULL)
888 err(EX_UNAVAILABLE, "fopen: %s", filename);
889
890 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */
891 char *s, *cur = line, *name = NULL, *arg = NULL;
892
893 ++lineno;
894
895 /* parse the line */
896 while (cur) {
897 s = strsep(&cur, ED_SEPARATORS);
898 if (s == NULL || *s == '#')
899 break;
900 if (*s == '\0')
901 continue;
902 if (arg)
903 errx(ED_EFMT("too many arguments"));
904 if (name == NULL)
905 name = s;
906 else
907 arg = s;
908 }
909 if (name == NULL) /* empty line */
910 continue;
911 if (arg == NULL)
912 errx(ED_EFMT("missing arg for %s"), name);
913
914 if (!strcasecmp(name, ED_TOK_SAMPLES)) {
915 if (samples > 0)
916 errx(ED_EFMT("duplicate ``samples'' line"));
917 if (atoi(arg) <=0)
918 errx(ED_EFMT("invalid number of samples"));
919 samples = atoi(arg);
920 if (samples>ED_MAX_SAMPLES_NO)
921 errx(ED_EFMT("too many samples, maximum is %d"),
922 ED_MAX_SAMPLES_NO);
923 do_points = 0;
924 } else if (!strcasecmp(name, ED_TOK_BW)) {
925 char buf[IFNAMSIZ];
926 read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf));
927 } else if (!strcasecmp(name, ED_TOK_LOSS)) {
928 if (loss != -1.0)
929 errx(ED_EFMT("duplicated token: %s"), name);
930 if (!is_valid_number(arg))
931 errx(ED_EFMT("invalid %s"), arg);
932 loss = atof(arg);
933 if (loss > 1)
934 errx(ED_EFMT("%s greater than 1.0"), name);
935 do_points = 0;
936 } else if (!strcasecmp(name, ED_TOK_NAME)) {
937 if (profile_name[0] != '\0')
938 errx(ED_EFMT("duplicated token: %s"), name);
939 strlcpy(profile_name, arg, sizeof(profile_name));
940 do_points = 0;
941 } else if (!strcasecmp(name, ED_TOK_DELAY)) {
942 if (do_points)
943 errx(ED_EFMT("duplicated token: %s"), name);
944 delay_first = 1;
945 do_points = 1;
946 } else if (!strcasecmp(name, ED_TOK_PROB)) {
947 if (do_points)
948 errx(ED_EFMT("duplicated token: %s"), name);
949 delay_first = 0;
950 do_points = 1;
951 } else if (do_points) {
952 if (!is_valid_number(name) || !is_valid_number(arg))
953 errx(ED_EFMT("invalid point found"));
954 if (delay_first) {
955 points[points_no].delay = atof(name);
956 points[points_no].prob = atof(arg);
957 } else {
958 points[points_no].delay = atof(arg);
959 points[points_no].prob = atof(name);
960 }
961 if (points[points_no].prob > 1.0)
962 errx(ED_EFMT("probability greater than 1.0"));
963 ++points_no;
964 } else {
965 errx(ED_EFMT("unrecognised command '%s'"), name);
966 }
967 }
968
969 fclose (f);
970
971 if (samples == -1) {
972 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES);
973 samples = 100;
974 }
975
976 if (loss == -1.0) {
977 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS);
978 loss = 1;
979 }
980
981 /* make sure that there are enough points. */
982 if (points_no < ED_MIN_SAMPLES_NO)
983 errx(ED_EFMT("too few samples, need at least %d"),
984 ED_MIN_SAMPLES_NO);
985
986 qsort(points, points_no, sizeof(struct point), compare_points);
987
988 /* interpolation */
989 for (i = 0; i<points_no-1; ++i) {
990 double y1 = points[i].prob * samples;
991 double x1 = points[i].delay;
992 double y2 = points[i+1].prob * samples;
993 double x2 = points[i+1].delay;
994
995 int ix = y1;
996 int stop = y2;
997
998 if (x1 == x2) {
999 for (; ix<stop; ++ix)
1000 p->samples[ix] = x1;
1001 } else {
1002 double m = (y2-y1)/(x2-x1);
1003 double c = y1 - m*x1;
1004 for (; ix<stop ; ++ix)
1005 p->samples[ix] = (ix - c)/m;
1006 }
1007 }
1008 p->samples_no = samples;
1009 p->loss_level = loss * samples;
1010 strlcpy(p->name, profile_name, sizeof(p->name));
1011 }
1012
1013 #ifdef NEW_AQM
1014
1015 /* Parse AQM/extra scheduler parameters */
1016 static int
process_extra_parms(int * ac,char ** av,struct dn_extra_parms * ep,uint16_t type)1017 process_extra_parms(int *ac, char **av, struct dn_extra_parms *ep,
1018 uint16_t type)
1019 {
1020 int i;
1021
1022 /* use kernel defaults */
1023 for (i=0; i<DN_MAX_EXTRA_PARM; i++)
1024 ep->par[i] = -1;
1025
1026 switch(type) {
1027 case TOK_CODEL:
1028 case TOK_FQ_CODEL:
1029 /* Codel
1030 * 0- target, 1- interval, 2- flags,
1031 * FQ_CODEL
1032 * 3- quantum, 4- limit, 5- flows
1033 */
1034 if (type==TOK_CODEL)
1035 ep->par[2] = 0;
1036 else
1037 ep->par[2] = CODEL_ECN_ENABLED;
1038
1039 while (*ac > 0) {
1040 int tok = match_token(aqm_params, *av);
1041 (*ac)--; av++;
1042 switch(tok) {
1043 case TOK_TARGET:
1044 if (*ac <= 0 || time_to_us(av[0]) < 0)
1045 errx(EX_DATAERR, "target needs time\n");
1046
1047 ep->par[0] = time_to_us(av[0]);
1048 (*ac)--; av++;
1049 break;
1050
1051 case TOK_INTERVAL:
1052 if (*ac <= 0 || time_to_us(av[0]) < 0)
1053 errx(EX_DATAERR, "interval needs time\n");
1054
1055 ep->par[1] = time_to_us(av[0]);
1056 (*ac)--; av++;
1057 break;
1058
1059 case TOK_ECN:
1060 ep->par[2] = CODEL_ECN_ENABLED;
1061 break;
1062 case TOK_NO_ECN:
1063 ep->par[2] &= ~CODEL_ECN_ENABLED;
1064 break;
1065 /* Config fq_codel parameters */
1066 case TOK_QUANTUM:
1067 if (type != TOK_FQ_CODEL)
1068 errx(EX_DATAERR, "quantum is not for codel\n");
1069 if (*ac <= 0 || !is_valid_number(av[0]))
1070 errx(EX_DATAERR, "quantum needs number\n");
1071
1072 ep->par[3]= atoi(av[0]);
1073 (*ac)--; av++;
1074 break;
1075
1076 case TOK_LIMIT:
1077 if (type != TOK_FQ_CODEL)
1078 errx(EX_DATAERR, "limit is not for codel, use queue instead\n");
1079 if (*ac <= 0 || !is_valid_number(av[0]))
1080 errx(EX_DATAERR, "limit needs number\n");
1081
1082 ep->par[4] = atoi(av[0]);
1083 (*ac)--; av++;
1084 break;
1085
1086 case TOK_FLOWS:
1087 if (type != TOK_FQ_CODEL)
1088 errx(EX_DATAERR, "flows is not for codel\n");
1089 if (*ac <= 0 || !is_valid_number(av[0]))
1090 errx(EX_DATAERR, "flows needs number\n");
1091
1092 ep->par[5] = atoi(av[0]);
1093 (*ac)--; av++;
1094 break;
1095
1096 default:
1097 printf("%s is Invalid parameter\n", av[-1]);
1098 }
1099 }
1100 break;
1101 case TOK_PIE:
1102 case TOK_FQ_PIE:
1103 /* PIE
1104 * 0- target , 1- tupdate, 2- max_burst,
1105 * 3- max_ecnth, 4- alpha,
1106 * 5- beta, 6- flags
1107 * FQ_CODEL
1108 * 7- quantum, 8- limit, 9- flows
1109 */
1110
1111 if ( type == TOK_PIE)
1112 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED
1113 | PIE_DERAND_ENABLED;
1114 else
1115 /* for FQ-PIE, use TS mode */
1116 ep->par[6] = PIE_CAPDROP_ENABLED | PIE_DERAND_ENABLED
1117 | PIE_ECN_ENABLED;
1118
1119 while (*ac > 0) {
1120 int tok = match_token(aqm_params, *av);
1121 (*ac)--; av++;
1122 switch(tok) {
1123 case TOK_TARGET:
1124 if (*ac <= 0 || time_to_us(av[0]) < 0)
1125 errx(EX_DATAERR, "target needs time\n");
1126
1127 ep->par[0] = time_to_us(av[0]);
1128 (*ac)--; av++;
1129 break;
1130
1131 case TOK_TUPDATE:
1132 if (*ac <= 0 || time_to_us(av[0]) < 0)
1133 errx(EX_DATAERR, "tupdate needs time\n");
1134
1135 ep->par[1] = time_to_us(av[0]);
1136 (*ac)--; av++;
1137 break;
1138
1139 case TOK_MAX_BURST:
1140 if (*ac <= 0 || time_to_us(av[0]) < 0)
1141 errx(EX_DATAERR, "max_burst needs time\n");
1142
1143 ep->par[2] = time_to_us(av[0]);
1144 (*ac)--; av++;
1145 break;
1146
1147 case TOK_MAX_ECNTH:
1148 if (*ac <= 0 || !is_valid_number(av[0]))
1149 errx(EX_DATAERR, "max_ecnth needs number\n");
1150
1151 ep->par[3] = atof(av[0]) * PIE_SCALE;
1152 (*ac)--; av++;
1153 break;
1154
1155 case TOK_ALPHA:
1156 if (*ac <= 0 || !is_valid_number(av[0]))
1157 errx(EX_DATAERR, "alpha needs number\n");
1158
1159 ep->par[4] = atof(av[0]) * PIE_SCALE;
1160 (*ac)--; av++;
1161 break;
1162
1163 case TOK_BETA:
1164 if (*ac <= 0 || !is_valid_number(av[0]))
1165 errx(EX_DATAERR, "beta needs number\n");
1166
1167 ep->par[5] = atof(av[0]) * PIE_SCALE;
1168 (*ac)--; av++;
1169 break;
1170
1171 case TOK_ECN:
1172 ep->par[6] |= PIE_ECN_ENABLED;
1173 break;
1174 case TOK_NO_ECN:
1175 ep->par[6] &= ~PIE_ECN_ENABLED;
1176 break;
1177
1178 case TOK_CAPDROP:
1179 ep->par[6] |= PIE_CAPDROP_ENABLED;
1180 break;
1181 case TOK_NO_CAPDROP:
1182 ep->par[6] &= ~PIE_CAPDROP_ENABLED;
1183 break;
1184
1185 case TOK_ONOFF:
1186 ep->par[6] |= PIE_ON_OFF_MODE_ENABLED;
1187 break;
1188
1189 case TOK_DRE:
1190 ep->par[6] |= PIE_DEPRATEEST_ENABLED;
1191 break;
1192
1193 case TOK_TS:
1194 ep->par[6] &= ~PIE_DEPRATEEST_ENABLED;
1195 break;
1196
1197 case TOK_DERAND:
1198 ep->par[6] |= PIE_DERAND_ENABLED;
1199 break;
1200 case TOK_NO_DERAND:
1201 ep->par[6] &= ~PIE_DERAND_ENABLED;
1202 break;
1203
1204 /* Config fq_pie parameters */
1205 case TOK_QUANTUM:
1206 if (type != TOK_FQ_PIE)
1207 errx(EX_DATAERR, "quantum is not for pie\n");
1208 if (*ac <= 0 || !is_valid_number(av[0]))
1209 errx(EX_DATAERR, "quantum needs number\n");
1210
1211 ep->par[7]= atoi(av[0]);
1212 (*ac)--; av++;
1213 break;
1214
1215 case TOK_LIMIT:
1216 if (type != TOK_FQ_PIE)
1217 errx(EX_DATAERR, "limit is not for pie, use queue instead\n");
1218 if (*ac <= 0 || !is_valid_number(av[0]))
1219 errx(EX_DATAERR, "limit needs number\n");
1220
1221 ep->par[8] = atoi(av[0]);
1222 (*ac)--; av++;
1223 break;
1224
1225 case TOK_FLOWS:
1226 if (type != TOK_FQ_PIE)
1227 errx(EX_DATAERR, "flows is not for pie\n");
1228 if (*ac <= 0 || !is_valid_number(av[0]))
1229 errx(EX_DATAERR, "flows needs number\n");
1230
1231 ep->par[9] = atoi(av[0]);
1232 (*ac)--; av++;
1233 break;
1234
1235
1236 default:
1237 printf("%s is invalid parameter\n", av[-1]);
1238 }
1239 }
1240 break;
1241 }
1242
1243 return 0;
1244 }
1245
1246 #endif
1247
1248
1249 /*
1250 * configuration of pipes, schedulers, flowsets.
1251 * When we configure a new scheduler, an empty pipe is created, so:
1252 *
1253 * do_pipe = 1 -> "pipe N config ..." only for backward compatibility
1254 * sched N+Delta type fifo sched_mask ...
1255 * pipe N+Delta <parameters>
1256 * flowset N+Delta pipe N+Delta (no parameters)
1257 * sched N type wf2q+ sched_mask ...
1258 * pipe N <parameters>
1259 *
1260 * do_pipe = 2 -> flowset N config
1261 * flowset N parameters
1262 *
1263 * do_pipe = 3 -> sched N config
1264 * sched N parameters (default no pipe)
1265 * optional Pipe N config ...
1266 * pipe ==>
1267 */
1268 void
ipfw_config_pipe(int ac,char ** av)1269 ipfw_config_pipe(int ac, char **av)
1270 {
1271 int i;
1272 u_int j;
1273 char *end;
1274 struct dn_id *buf, *base;
1275 struct dn_sch *sch = NULL;
1276 struct dn_link *p = NULL;
1277 struct dn_fs *fs = NULL;
1278 struct dn_profile *pf = NULL;
1279 struct ipfw_flow_id *mask = NULL;
1280 #ifdef NEW_AQM
1281 struct dn_extra_parms *aqm_extra = NULL;
1282 struct dn_extra_parms *sch_extra = NULL;
1283 int lmax_extra;
1284 #endif
1285
1286 int lmax;
1287 uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo;
1288
1289 /*
1290 * allocate space for 1 header,
1291 * 1 scheduler, 1 link, 1 flowset, 1 profile
1292 */
1293 lmax = sizeof(struct dn_id); /* command header */
1294 lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) +
1295 sizeof(struct dn_fs) + sizeof(struct dn_profile);
1296
1297 #ifdef NEW_AQM
1298 /* Extra Params */
1299 lmax_extra = sizeof(struct dn_extra_parms);
1300 /* two lmax_extra because one for AQM params and another
1301 * sch params
1302 */
1303 lmax += lmax_extra*2;
1304 #endif
1305
1306 av++; ac--;
1307 /* Pipe number */
1308 if (ac && isdigit(**av)) {
1309 i = atoi(*av); av++; ac--;
1310 } else
1311 i = -1;
1312 if (i <= 0)
1313 errx(EX_USAGE, "need a pipe/flowset/sched number");
1314 base = buf = safe_calloc(1, lmax);
1315 /* all commands start with a 'CONFIGURE' and a version */
1316 o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG);
1317 base->id = DN_API_VERSION;
1318
1319 switch (g_co.do_pipe) {
1320 case 1: /* "pipe N config ..." */
1321 /* Allocate space for the WF2Q+ scheduler, its link
1322 * and the FIFO flowset. Set the number, but leave
1323 * the scheduler subtype and other parameters to 0
1324 * so the kernel will use appropriate defaults.
1325 * XXX todo: add a flag to record if a parameter
1326 * is actually configured.
1327 * If we do a 'pipe config' mask -> sched_mask.
1328 * The FIFO scheduler and link are derived from the
1329 * WF2Q+ one in the kernel.
1330 */
1331 #ifdef NEW_AQM
1332 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1333 sch_extra ->oid.subtype = 0; /* don't configure scheduler */
1334 #endif
1335 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1336 p = o_next(&buf, sizeof(*p), DN_LINK);
1337 #ifdef NEW_AQM
1338 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1339 aqm_extra ->oid.subtype = 0; /* don't configure AQM */
1340 #endif
1341 fs = o_next(&buf, sizeof(*fs), DN_FS);
1342
1343 sch->sched_nr = i;
1344 sch->oid.subtype = 0; /* defaults to WF2Q+ */
1345 mask = &sch->sched_mask;
1346 flags = &sch->flags;
1347 buckets = &sch->buckets;
1348 *flags |= DN_PIPE_CMD;
1349
1350 p->link_nr = i;
1351
1352 /* This flowset is only for the FIFO scheduler */
1353 fs->fs_nr = i + 2*DN_MAX_ID;
1354 fs->sched_nr = i + DN_MAX_ID;
1355 break;
1356
1357 case 2: /* "queue N config ... " */
1358 #ifdef NEW_AQM
1359 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1360 aqm_extra ->oid.subtype = 0;
1361 #endif
1362 fs = o_next(&buf, sizeof(*fs), DN_FS);
1363 fs->fs_nr = i;
1364 mask = &fs->flow_mask;
1365 flags = &fs->flags;
1366 buckets = &fs->buckets;
1367 break;
1368
1369 case 3: /* "sched N config ..." */
1370 #ifdef NEW_AQM
1371 sch_extra = o_next(&buf, lmax_extra, DN_TEXT);
1372 sch_extra ->oid.subtype = 0;
1373 #endif
1374 sch = o_next(&buf, sizeof(*sch), DN_SCH);
1375 #ifdef NEW_AQM
1376 aqm_extra = o_next(&buf, lmax_extra, DN_TEXT);
1377 aqm_extra ->oid.subtype = 0;
1378 #endif
1379 fs = o_next(&buf, sizeof(*fs), DN_FS);
1380 sch->sched_nr = i;
1381 mask = &sch->sched_mask;
1382 flags = &sch->flags;
1383 buckets = &sch->buckets;
1384 /* fs is used only with !MULTIQUEUE schedulers */
1385 fs->fs_nr = i + DN_MAX_ID;
1386 fs->sched_nr = i;
1387 break;
1388 }
1389 /* set to -1 those fields for which we want to reuse existing
1390 * values from the kernel.
1391 * Also, *_nr and subtype = 0 mean reuse the value from the kernel.
1392 * XXX todo: support reuse of the mask.
1393 */
1394 if (p)
1395 p->bandwidth = -1;
1396 for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++)
1397 fs->par[j] = -1;
1398 while (ac > 0) {
1399 double d;
1400 int tok = match_token(dummynet_params, *av);
1401 ac--; av++;
1402
1403 switch(tok) {
1404 case TOK_NOERROR:
1405 NEED(fs, "noerror is only for pipes");
1406 fs->flags |= DN_NOERROR;
1407 break;
1408
1409 case TOK_PLR:
1410 NEED(fs, "plr is only for pipes");
1411 NEED1("plr needs argument 0..1\n");
1412 d = strtod(av[0], NULL);
1413 if (d > 1)
1414 d = 1;
1415 else if (d < 0)
1416 d = 0;
1417 fs->plr = (int)(d*0x7fffffff);
1418 ac--; av++;
1419 break;
1420
1421 case TOK_QUEUE:
1422 NEED(fs, "queue is only for pipes or flowsets");
1423 NEED1("queue needs queue size\n");
1424 end = NULL;
1425 fs->qsize = strtoul(av[0], &end, 0);
1426 if (*end == 'K' || *end == 'k') {
1427 fs->flags |= DN_QSIZE_BYTES;
1428 fs->qsize *= 1024;
1429 } else if (*end == 'B' ||
1430 _substrcmp2(end, "by", "bytes") == 0) {
1431 fs->flags |= DN_QSIZE_BYTES;
1432 }
1433 ac--; av++;
1434 break;
1435
1436 case TOK_BUCKETS:
1437 NEED(fs, "buckets is only for pipes or flowsets");
1438 NEED1("buckets needs argument\n");
1439 *buckets = strtoul(av[0], NULL, 0);
1440 ac--; av++;
1441 break;
1442
1443 case TOK_FLOW_MASK:
1444 case TOK_SCHED_MASK:
1445 case TOK_MASK:
1446 NEED(mask, "tok_mask");
1447 NEED1("mask needs mask specifier\n");
1448 /*
1449 * per-flow queue, mask is dst_ip, dst_port,
1450 * src_ip, src_port, proto measured in bits
1451 */
1452
1453 bzero(mask, sizeof(*mask));
1454 end = NULL;
1455
1456 while (ac >= 1) {
1457 uint32_t *p32 = NULL;
1458 uint16_t *p16 = NULL;
1459 uint32_t *p20 = NULL;
1460 struct in6_addr *pa6 = NULL;
1461 uint32_t a;
1462
1463 tok = match_token(dummynet_params, *av);
1464 ac--; av++;
1465 switch(tok) {
1466 case TOK_ALL:
1467 /*
1468 * special case, all bits significant
1469 * except 'extra' (the queue number)
1470 */
1471 mask->dst_ip = ~0;
1472 mask->src_ip = ~0;
1473 mask->dst_port = ~0;
1474 mask->src_port = ~0;
1475 mask->proto = ~0;
1476 n2mask(&mask->dst_ip6, 128);
1477 n2mask(&mask->src_ip6, 128);
1478 mask->flow_id6 = ~0;
1479 *flags |= DN_HAVE_MASK;
1480 goto end_mask;
1481
1482 case TOK_QUEUE:
1483 mask->extra = ~0;
1484 *flags |= DN_HAVE_MASK;
1485 goto end_mask;
1486
1487 case TOK_DSTIP:
1488 mask->addr_type = 4;
1489 p32 = &mask->dst_ip;
1490 break;
1491
1492 case TOK_SRCIP:
1493 mask->addr_type = 4;
1494 p32 = &mask->src_ip;
1495 break;
1496
1497 case TOK_DSTIP6:
1498 mask->addr_type = 6;
1499 pa6 = &mask->dst_ip6;
1500 break;
1501
1502 case TOK_SRCIP6:
1503 mask->addr_type = 6;
1504 pa6 = &mask->src_ip6;
1505 break;
1506
1507 case TOK_FLOWID:
1508 mask->addr_type = 6;
1509 p20 = &mask->flow_id6;
1510 break;
1511
1512 case TOK_DSTPORT:
1513 p16 = &mask->dst_port;
1514 break;
1515
1516 case TOK_SRCPORT:
1517 p16 = &mask->src_port;
1518 break;
1519
1520 case TOK_PROTO:
1521 break;
1522
1523 default:
1524 ac++; av--; /* backtrack */
1525 goto end_mask;
1526 }
1527 if (ac < 1)
1528 errx(EX_USAGE, "mask: value missing");
1529 if (*av[0] == '/') {
1530 a = strtoul(av[0]+1, &end, 0);
1531 if (pa6 == NULL)
1532 a = (a == 32) ? ~0 : (1 << a) - 1;
1533 } else
1534 a = strtoul(av[0], &end, 0);
1535 if (p32 != NULL)
1536 *p32 = a;
1537 else if (p16 != NULL) {
1538 if (a > 0xFFFF)
1539 errx(EX_DATAERR,
1540 "port mask must be 16 bit");
1541 *p16 = (uint16_t)a;
1542 } else if (p20 != NULL) {
1543 if (a > 0xfffff)
1544 errx(EX_DATAERR,
1545 "flow_id mask must be 20 bit");
1546 *p20 = (uint32_t)a;
1547 } else if (pa6 != NULL) {
1548 if (a > 128)
1549 errx(EX_DATAERR,
1550 "in6addr invalid mask len");
1551 else
1552 n2mask(pa6, a);
1553 } else {
1554 if (a > 0xFF)
1555 errx(EX_DATAERR,
1556 "proto mask must be 8 bit");
1557 mask->proto = (uint8_t)a;
1558 }
1559 if (a != 0)
1560 *flags |= DN_HAVE_MASK;
1561 ac--; av++;
1562 } /* end while, config masks */
1563 end_mask:
1564 break;
1565 #ifdef NEW_AQM
1566 case TOK_CODEL:
1567 case TOK_PIE:
1568 NEED(fs, "codel/pie is only for flowsets");
1569
1570 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1571 fs->flags |= DN_IS_AQM;
1572
1573 strlcpy(aqm_extra->name, av[-1],
1574 sizeof(aqm_extra->name));
1575 aqm_extra->oid.subtype = DN_AQM_PARAMS;
1576
1577 process_extra_parms(&ac, av, aqm_extra, tok);
1578 break;
1579
1580 case TOK_FQ_CODEL:
1581 case TOK_FQ_PIE:
1582 if (!strcmp(av[-1],"type"))
1583 errx(EX_DATAERR, "use type before fq_codel/fq_pie");
1584
1585 NEED(sch, "fq_codel/fq_pie is only for schd");
1586 strlcpy(sch_extra->name, av[-1],
1587 sizeof(sch_extra->name));
1588 sch_extra->oid.subtype = DN_SCH_PARAMS;
1589 process_extra_parms(&ac, av, sch_extra, tok);
1590 break;
1591 #endif
1592 case TOK_RED:
1593 case TOK_GRED:
1594 NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
1595 fs->flags |= DN_IS_RED;
1596 if (tok == TOK_GRED)
1597 fs->flags |= DN_IS_GENTLE_RED;
1598 /*
1599 * the format for parameters is w_q/min_th/max_th/max_p
1600 */
1601 if ((end = strsep(&av[0], "/"))) {
1602 double w_q = strtod(end, NULL);
1603 if (w_q > 1 || w_q <= 0)
1604 errx(EX_DATAERR, "0 < w_q <= 1");
1605 fs->w_q = (int) (w_q * (1 << SCALE_RED));
1606 }
1607 if ((end = strsep(&av[0], "/"))) {
1608 fs->min_th = strtoul(end, &end, 0);
1609 if (*end == 'K' || *end == 'k')
1610 fs->min_th *= 1024;
1611 }
1612 if ((end = strsep(&av[0], "/"))) {
1613 fs->max_th = strtoul(end, &end, 0);
1614 if (*end == 'K' || *end == 'k')
1615 fs->max_th *= 1024;
1616 }
1617 if ((end = strsep(&av[0], "/"))) {
1618 double max_p = strtod(end, NULL);
1619 if (max_p > 1 || max_p < 0)
1620 errx(EX_DATAERR, "0 <= max_p <= 1");
1621 fs->max_p = (int)(max_p * (1 << SCALE_RED));
1622 }
1623 ac--; av++;
1624 break;
1625
1626 case TOK_ECN:
1627 fs->flags |= DN_IS_ECN;
1628 break;
1629
1630 case TOK_DROPTAIL:
1631 NEED(fs, "droptail is only for flowsets");
1632 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
1633 break;
1634
1635 case TOK_BW:
1636 NEED(p, "bw is only for links");
1637 NEED1("bw needs bandwidth or interface\n");
1638 read_bandwidth(av[0], &p->bandwidth, NULL, 0);
1639 ac--; av++;
1640 break;
1641
1642 case TOK_DELAY:
1643 NEED(p, "delay is only for links");
1644 NEED1("delay needs argument 0..10000ms\n");
1645 p->delay = strtoul(av[0], NULL, 0);
1646 ac--; av++;
1647 break;
1648
1649 case TOK_TYPE: {
1650 int l;
1651 NEED(sch, "type is only for schedulers");
1652 NEED1("type needs a string");
1653 l = strlen(av[0]);
1654 if (l == 0 || l > 15)
1655 errx(1, "type %s too long\n", av[0]);
1656 strlcpy(sch->name, av[0], sizeof(sch->name));
1657 sch->oid.subtype = 0; /* use string */
1658 #ifdef NEW_AQM
1659 /* if fq_codel is selected, consider all tokens after it
1660 * as parameters
1661 */
1662 if (!strcasecmp(av[0],"fq_codel") || !strcasecmp(av[0],"fq_pie")){
1663 strlcpy(sch_extra->name, av[0],
1664 sizeof(sch_extra->name));
1665 sch_extra->oid.subtype = DN_SCH_PARAMS;
1666 process_extra_parms(&ac, av, sch_extra, tok);
1667 } else {
1668 ac--;av++;
1669 }
1670 #else
1671 ac--;av++;
1672 #endif
1673 break;
1674 }
1675
1676 case TOK_WEIGHT:
1677 NEED(fs, "weight is only for flowsets");
1678 NEED1("weight needs argument\n");
1679 fs->par[0] = strtol(av[0], &end, 0);
1680 ac--; av++;
1681 break;
1682
1683 case TOK_LMAX:
1684 NEED(fs, "lmax is only for flowsets");
1685 NEED1("lmax needs argument\n");
1686 fs->par[1] = strtol(av[0], &end, 0);
1687 ac--; av++;
1688 break;
1689
1690 case TOK_PRI:
1691 NEED(fs, "priority is only for flowsets");
1692 NEED1("priority needs argument\n");
1693 fs->par[2] = strtol(av[0], &end, 0);
1694 ac--; av++;
1695 break;
1696
1697 case TOK_SCHED:
1698 case TOK_PIPE:
1699 NEED(fs, "pipe/sched");
1700 NEED1("pipe/link/sched needs number\n");
1701 fs->sched_nr = strtoul(av[0], &end, 0);
1702 ac--; av++;
1703 break;
1704
1705 case TOK_PROFILE:
1706 NEED((!pf), "profile already set");
1707 NEED(p, "profile");
1708 {
1709 NEED1("extra delay needs the file name\n");
1710 pf = o_next(&buf, sizeof(*pf), DN_PROFILE);
1711 load_extra_delays(av[0], pf, p); //XXX can't fail?
1712 --ac; ++av;
1713 }
1714 break;
1715
1716 case TOK_BURST:
1717 NEED(p, "burst");
1718 NEED1("burst needs argument\n");
1719 errno = 0;
1720 if (expand_number(av[0], &p->burst) < 0)
1721 if (errno != ERANGE)
1722 errx(EX_DATAERR,
1723 "burst: invalid argument");
1724 if (errno || p->burst > (1ULL << 48) - 1)
1725 errx(EX_DATAERR,
1726 "burst: out of range (0..2^48-1)");
1727 ac--; av++;
1728 break;
1729
1730 default:
1731 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
1732 }
1733 }
1734
1735 /* check validity of parameters */
1736 if (p) {
1737 if (p->delay > 10000)
1738 errx(EX_DATAERR, "delay must be < 10000");
1739 if (p->bandwidth == (uint32_t)-1)
1740 p->bandwidth = 0;
1741 }
1742 if (fs) {
1743 /* XXX accept a 0 scheduler to keep the default */
1744 if (fs->flags & DN_QSIZE_BYTES) {
1745 size_t len;
1746 long limit;
1747
1748 len = sizeof(limit);
1749 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit",
1750 &limit, &len, NULL, 0) == -1)
1751 limit = 1024*1024;
1752 if (fs->qsize > limit)
1753 errx(EX_DATAERR, "queue size must be < %ldB", limit);
1754 } else {
1755 size_t len;
1756 long limit;
1757
1758 len = sizeof(limit);
1759 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit",
1760 &limit, &len, NULL, 0) == -1)
1761 limit = 100;
1762 if (fs->qsize > limit)
1763 errx(EX_DATAERR, "2 <= queue size <= %ld", limit);
1764 }
1765
1766 #ifdef NEW_AQM
1767 if ((fs->flags & DN_IS_ECN) && !((fs->flags & DN_IS_RED)||
1768 (fs->flags & DN_IS_AQM)))
1769 errx(EX_USAGE, "ECN can be used with red/gred/"
1770 "codel/fq_codel only!");
1771 #else
1772 if ((fs->flags & DN_IS_ECN) && !(fs->flags & DN_IS_RED))
1773 errx(EX_USAGE, "enable red/gred for ECN");
1774
1775 #endif
1776
1777 if (fs->flags & DN_IS_RED) {
1778 size_t len;
1779 int lookup_depth, avg_pkt_size;
1780
1781 if (!(fs->flags & DN_IS_ECN) && (fs->min_th >= fs->max_th))
1782 errx(EX_DATAERR, "min_th %d must be < than max_th %d",
1783 fs->min_th, fs->max_th);
1784 else if ((fs->flags & DN_IS_ECN) && (fs->min_th > fs->max_th))
1785 errx(EX_DATAERR, "min_th %d must be =< than max_th %d",
1786 fs->min_th, fs->max_th);
1787
1788 if (fs->max_th == 0)
1789 errx(EX_DATAERR, "max_th must be > 0");
1790
1791 len = sizeof(int);
1792 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
1793 &lookup_depth, &len, NULL, 0) == -1)
1794 lookup_depth = 256;
1795 if (lookup_depth == 0)
1796 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
1797 " must be greater than zero");
1798
1799 len = sizeof(int);
1800 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
1801 &avg_pkt_size, &len, NULL, 0) == -1)
1802 avg_pkt_size = 512;
1803
1804 if (avg_pkt_size == 0)
1805 errx(EX_DATAERR,
1806 "net.inet.ip.dummynet.red_avg_pkt_size must"
1807 " be greater than zero");
1808
1809 #if 0 /* the following computation is now done in the kernel */
1810 /*
1811 * Ticks needed for sending a medium-sized packet.
1812 * Unfortunately, when we are configuring a WF2Q+ queue, we
1813 * do not have bandwidth information, because that is stored
1814 * in the parent pipe, and also we have multiple queues
1815 * competing for it. So we set s=0, which is not very
1816 * correct. But on the other hand, why do we want RED with
1817 * WF2Q+ ?
1818 */
1819 if (p.bandwidth==0) /* this is a WF2Q+ queue */
1820 s = 0;
1821 else
1822 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth;
1823 /*
1824 * max idle time (in ticks) before avg queue size becomes 0.
1825 * NOTA: (3/w_q) is approx the value x so that
1826 * (1-w_q)^x < 10^-3.
1827 */
1828 w_q = ((double)fs->w_q) / (1 << SCALE_RED);
1829 idle = s * 3. / w_q;
1830 fs->lookup_step = (int)idle / lookup_depth;
1831 if (!fs->lookup_step)
1832 fs->lookup_step = 1;
1833 weight = 1 - w_q;
1834 for (t = fs->lookup_step; t > 1; --t)
1835 weight *= 1 - w_q;
1836 fs->lookup_weight = (int)(weight * (1 << SCALE_RED));
1837 #endif /* code moved in the kernel */
1838 }
1839 }
1840
1841 i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base);
1842
1843 if (i)
1844 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
1845 }
1846
1847 void
dummynet_flush(void)1848 dummynet_flush(void)
1849 {
1850 struct dn_id oid;
1851 oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION);
1852 do_cmd(IP_DUMMYNET3, &oid, oid.len);
1853 }
1854
1855 /* Parse input for 'ipfw [pipe|sched|queue] show [range list]'
1856 * Returns the number of ranges, and possibly stores them
1857 * in the array v of size len.
1858 */
1859 static int
parse_range(int ac,char * av[],uint32_t * v,int len)1860 parse_range(int ac, char *av[], uint32_t *v, int len)
1861 {
1862 int n = 0;
1863 char *endptr, *s;
1864 uint32_t base[2];
1865
1866 if (v == NULL || len < 2) {
1867 v = base;
1868 len = 2;
1869 }
1870
1871 for (s = *av; s != NULL; av++, ac--) {
1872 v[0] = strtoul(s, &endptr, 10);
1873 v[1] = (*endptr != '-') ? v[0] :
1874 strtoul(endptr+1, &endptr, 10);
1875 if (*endptr == '\0') { /* prepare for next round */
1876 s = (ac > 0) ? *(av+1) : NULL;
1877 } else {
1878 if (*endptr != ',') {
1879 warn("invalid number: %s", s);
1880 s = ++endptr;
1881 continue;
1882 }
1883 /* continue processing from here */
1884 s = ++endptr;
1885 ac++;
1886 av--;
1887 }
1888 if (v[1] < v[0] ||
1889 v[0] >= DN_MAX_ID-1 ||
1890 v[1] >= DN_MAX_ID-1) {
1891 continue; /* invalid entry */
1892 }
1893 n++;
1894 /* translate if 'pipe list' */
1895 if (g_co.do_pipe == 1) {
1896 v[0] += DN_MAX_ID;
1897 v[1] += DN_MAX_ID;
1898 }
1899 v = (n*2 < len) ? v + 2 : base;
1900 }
1901 return n;
1902 }
1903
1904 /* main entry point for dummynet list functions. co.do_pipe indicates
1905 * which function we want to support.
1906 * av may contain filtering arguments, either individual entries
1907 * or ranges, or lists (space or commas are valid separators).
1908 * Format for a range can be n1-n2 or n3 n4 n5 ...
1909 * In a range n1 must be <= n2, otherwise the range is ignored.
1910 * A number 'n4' is translate in a range 'n4-n4'
1911 * All number must be > 0 and < DN_MAX_ID-1
1912 */
1913 void
dummynet_list(int ac,char * av[],int show_counters)1914 dummynet_list(int ac, char *av[], int show_counters)
1915 {
1916 struct dn_id *oid, *x = NULL;
1917 int ret, i;
1918 int n; /* # of ranges */
1919 u_int buflen, l;
1920 u_int max_size; /* largest obj passed up */
1921
1922 (void)show_counters; // XXX unused, but we should use it.
1923 ac--;
1924 av++; /* skip 'list' | 'show' word */
1925
1926 n = parse_range(ac, av, NULL, 0); /* Count # of ranges. */
1927
1928 /* Allocate space to store ranges */
1929 l = sizeof(*oid) + sizeof(uint32_t) * n * 2;
1930 oid = safe_calloc(1, l);
1931 oid_fill(oid, l, DN_CMD_GET, DN_API_VERSION);
1932
1933 if (n > 0) /* store ranges in idx */
1934 parse_range(ac, av, (uint32_t *)(oid + 1), n*2);
1935 /*
1936 * Compute the size of the largest object returned. If the
1937 * response leaves at least this much spare space in the
1938 * buffer, then surely the response is complete; otherwise
1939 * there might be a risk of truncation and we will need to
1940 * retry with a larger buffer.
1941 * XXX don't bother with smaller structs.
1942 */
1943 max_size = sizeof(struct dn_fs);
1944 if (max_size < sizeof(struct dn_sch))
1945 max_size = sizeof(struct dn_sch);
1946 if (max_size < sizeof(struct dn_flow))
1947 max_size = sizeof(struct dn_flow);
1948
1949 switch (g_co.do_pipe) {
1950 case 1:
1951 oid->subtype = DN_LINK; /* list pipe */
1952 break;
1953 case 2:
1954 oid->subtype = DN_FS; /* list queue */
1955 break;
1956 case 3:
1957 oid->subtype = DN_SCH; /* list sched */
1958 break;
1959 }
1960
1961 /*
1962 * Ask the kernel an estimate of the required space (result
1963 * in oid.id), unless we are requesting a subset of objects,
1964 * in which case the kernel does not give an exact answer.
1965 * In any case, space might grow in the meantime due to the
1966 * creation of new queues, so we must be prepared to retry.
1967 */
1968 if (n > 0) {
1969 buflen = 4*1024;
1970 } else {
1971 ret = do_cmd(-IP_DUMMYNET3, oid, (uintptr_t)&l);
1972 if (ret != 0 || oid->id <= sizeof(*oid))
1973 goto done;
1974 buflen = oid->id + max_size;
1975 oid->len = sizeof(*oid); /* restore */
1976 }
1977 /* Try a few times, until the buffer fits */
1978 for (i = 0; i < 20; i++) {
1979 l = buflen;
1980 x = safe_realloc(x, l);
1981 bcopy(oid, x, oid->len);
1982 ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l);
1983 if (ret != 0 || x->id <= sizeof(*oid))
1984 goto done; /* no response */
1985 if (l + max_size <= buflen)
1986 break; /* ok */
1987 buflen *= 2; /* double for next attempt */
1988 }
1989 list_pipes(x, O_NEXT(x, l));
1990 done:
1991 if (x)
1992 free(x);
1993 free(oid);
1994 }
1995