1 /* 2 * Copyright (c) 2002-2003,2010 Luigi Rizzo 3 * 4 * Redistribution and use in source forms, with and without modification, 5 * are permitted provided that this entire comment appears intact. 6 * 7 * Redistribution in binary form may occur without any restrictions. 8 * Obviously, it would be nice if you gave credit where credit is due 9 * but requiring it would be too onerous. 10 * 11 * This software is provided ``AS IS'' without any warranties of any kind. 12 * 13 * NEW command line interface for IP firewall facility 14 * 15 * $FreeBSD$ 16 * 17 * dummynet support 18 */ 19 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 /* XXX there are several sysctl leftover here */ 23 #include <sys/sysctl.h> 24 25 #include "ipfw2.h" 26 27 #include <ctype.h> 28 #include <err.h> 29 #include <errno.h> 30 #include <libutil.h> 31 #include <netdb.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <sysexits.h> 36 37 #include <net/if.h> 38 #include <netinet/in.h> 39 #include <netinet/ip_fw.h> 40 #include <netinet/ip_dummynet.h> 41 #include <arpa/inet.h> /* inet_ntoa */ 42 43 44 static struct _s_x dummynet_params[] = { 45 { "plr", TOK_PLR }, 46 { "noerror", TOK_NOERROR }, 47 { "buckets", TOK_BUCKETS }, 48 { "dst-ip", TOK_DSTIP }, 49 { "src-ip", TOK_SRCIP }, 50 { "dst-port", TOK_DSTPORT }, 51 { "src-port", TOK_SRCPORT }, 52 { "proto", TOK_PROTO }, 53 { "weight", TOK_WEIGHT }, 54 { "lmax", TOK_LMAX }, 55 { "maxlen", TOK_LMAX }, 56 { "all", TOK_ALL }, 57 { "mask", TOK_MASK }, /* alias for both */ 58 { "sched_mask", TOK_SCHED_MASK }, 59 { "flow_mask", TOK_FLOW_MASK }, 60 { "droptail", TOK_DROPTAIL }, 61 { "red", TOK_RED }, 62 { "gred", TOK_GRED }, 63 { "bw", TOK_BW }, 64 { "bandwidth", TOK_BW }, 65 { "delay", TOK_DELAY }, 66 { "link", TOK_LINK }, 67 { "pipe", TOK_PIPE }, 68 { "queue", TOK_QUEUE }, 69 { "flowset", TOK_FLOWSET }, 70 { "sched", TOK_SCHED }, 71 { "pri", TOK_PRI }, 72 { "priority", TOK_PRI }, 73 { "type", TOK_TYPE }, 74 { "flow-id", TOK_FLOWID}, 75 { "dst-ipv6", TOK_DSTIP6}, 76 { "dst-ip6", TOK_DSTIP6}, 77 { "src-ipv6", TOK_SRCIP6}, 78 { "src-ip6", TOK_SRCIP6}, 79 { "profile", TOK_PROFILE}, 80 { "burst", TOK_BURST}, 81 { "dummynet-params", TOK_NULL }, 82 { NULL, 0 } /* terminator */ 83 }; 84 85 #define O_NEXT(p, len) ((void *)((char *)p + len)) 86 87 static void 88 oid_fill(struct dn_id *oid, int len, int type, uintptr_t id) 89 { 90 oid->len = len; 91 oid->type = type; 92 oid->subtype = 0; 93 oid->id = id; 94 } 95 96 /* make room in the buffer and move the pointer forward */ 97 static void * 98 o_next(struct dn_id **o, int len, int type) 99 { 100 struct dn_id *ret = *o; 101 oid_fill(ret, len, type, 0); 102 *o = O_NEXT(*o, len); 103 return ret; 104 } 105 106 #if 0 107 static int 108 sort_q(void *arg, const void *pa, const void *pb) 109 { 110 int rev = (co.do_sort < 0); 111 int field = rev ? -co.do_sort : co.do_sort; 112 long long res = 0; 113 const struct dn_flow_queue *a = pa; 114 const struct dn_flow_queue *b = pb; 115 116 switch (field) { 117 case 1: /* pkts */ 118 res = a->len - b->len; 119 break; 120 case 2: /* bytes */ 121 res = a->len_bytes - b->len_bytes; 122 break; 123 124 case 3: /* tot pkts */ 125 res = a->tot_pkts - b->tot_pkts; 126 break; 127 128 case 4: /* tot bytes */ 129 res = a->tot_bytes - b->tot_bytes; 130 break; 131 } 132 if (res < 0) 133 res = -1; 134 if (res > 0) 135 res = 1; 136 return (int)(rev ? res : -res); 137 } 138 #endif 139 140 /* print a mask and header for the subsequent list of flows */ 141 static void 142 print_mask(struct ipfw_flow_id *id) 143 { 144 if (!IS_IP6_FLOW_ID(id)) { 145 printf(" " 146 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n", 147 id->proto, 148 id->src_ip, id->src_port, 149 id->dst_ip, id->dst_port); 150 151 printf("BKT Prot ___Source IP/port____ " 152 "____Dest. IP/port____ " 153 "Tot_pkt/bytes Pkt/Byte Drp\n"); 154 } else { 155 char buf[255]; 156 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ", 157 id->proto, id->flow_id6); 158 inet_ntop(AF_INET6, &(id->src_ip6), buf, sizeof(buf)); 159 printf("%s/0x%04x -> ", buf, id->src_port); 160 inet_ntop(AF_INET6, &(id->dst_ip6), buf, sizeof(buf)); 161 printf("%s/0x%04x\n", buf, id->dst_port); 162 163 printf("BKT ___Prot___ _flow-id_ " 164 "______________Source IPv6/port_______________ " 165 "_______________Dest. IPv6/port_______________ " 166 "Tot_pkt/bytes Pkt/Byte Drp\n"); 167 } 168 } 169 170 static void 171 list_flow(struct dn_flow *ni) 172 { 173 char buff[255]; 174 struct protoent *pe; 175 struct in_addr ina; 176 struct ipfw_flow_id *id = &ni->fid; 177 178 pe = getprotobynumber(id->proto); 179 /* XXX: Should check for IPv4 flows */ 180 printf("%3u ", (ni->oid.id) & 0xff); 181 if (!IS_IP6_FLOW_ID(id)) { 182 if (pe) 183 printf("%-4s ", pe->p_name); 184 else 185 printf("%4u ", id->proto); 186 ina.s_addr = htonl(id->src_ip); 187 printf("%15s/%-5d ", 188 inet_ntoa(ina), id->src_port); 189 ina.s_addr = htonl(id->dst_ip); 190 printf("%15s/%-5d ", 191 inet_ntoa(ina), id->dst_port); 192 } else { 193 /* Print IPv6 flows */ 194 if (pe != NULL) 195 printf("%9s ", pe->p_name); 196 else 197 printf("%9u ", id->proto); 198 printf("%7d %39s/%-5d ", id->flow_id6, 199 inet_ntop(AF_INET6, &(id->src_ip6), buff, sizeof(buff)), 200 id->src_port); 201 printf(" %39s/%-5d ", 202 inet_ntop(AF_INET6, &(id->dst_ip6), buff, sizeof(buff)), 203 id->dst_port); 204 } 205 printf("%4llu %8llu %2u %4u %3u\n", 206 align_uint64(&ni->tot_pkts), 207 align_uint64(&ni->tot_bytes), 208 ni->length, ni->len_bytes, ni->drops); 209 } 210 211 static void 212 print_flowset_parms(struct dn_fs *fs, char *prefix) 213 { 214 int l; 215 char qs[30]; 216 char plr[30]; 217 char red[90]; /* Display RED parameters */ 218 219 l = fs->qsize; 220 if (fs->flags & DN_QSIZE_BYTES) { 221 if (l >= 8192) 222 sprintf(qs, "%d KB", l / 1024); 223 else 224 sprintf(qs, "%d B", l); 225 } else 226 sprintf(qs, "%3d sl.", l); 227 if (fs->plr) 228 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff)); 229 else 230 plr[0] = '\0'; 231 232 if (fs->flags & DN_IS_RED) /* RED parameters */ 233 sprintf(red, 234 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f", 235 (fs->flags & DN_IS_GENTLE_RED) ? 'G' : ' ', 236 1.0 * fs->w_q / (double)(1 << SCALE_RED), 237 fs->min_th, 238 fs->max_th, 239 1.0 * fs->max_p / (double)(1 << SCALE_RED)); 240 else 241 sprintf(red, "droptail"); 242 243 if (prefix[0]) { 244 printf("%s %s%s %d queues (%d buckets) %s\n", 245 prefix, qs, plr, fs->oid.id, fs->buckets, red); 246 prefix[0] = '\0'; 247 } else { 248 printf("q%05d %s%s %d flows (%d buckets) sched %d " 249 "weight %d lmax %d pri %d %s\n", 250 fs->fs_nr, qs, plr, fs->oid.id, fs->buckets, 251 fs->sched_nr, fs->par[0], fs->par[1], fs->par[2], red); 252 if (fs->flags & DN_HAVE_MASK) 253 print_mask(&fs->flow_mask); 254 } 255 } 256 257 static void 258 print_extra_delay_parms(struct dn_profile *p) 259 { 260 double loss; 261 if (p->samples_no <= 0) 262 return; 263 264 loss = p->loss_level; 265 loss /= p->samples_no; 266 printf("\t profile: name \"%s\" loss %f samples %d\n", 267 p->name, loss, p->samples_no); 268 } 269 270 static void 271 flush_buf(char *buf) 272 { 273 if (buf[0]) 274 printf("%s\n", buf); 275 buf[0] = '\0'; 276 } 277 278 /* 279 * generic list routine. We expect objects in a specific order, i.e. 280 * PIPES AND SCHEDULERS: 281 * link; scheduler; internal flowset if any; instances 282 * we can tell a pipe from the number. 283 * 284 * FLOWSETS: 285 * flowset; queues; 286 * link i (int queue); scheduler i; si(i) { flowsets() : queues } 287 */ 288 static void 289 list_pipes(struct dn_id *oid, struct dn_id *end) 290 { 291 char buf[160]; /* pending buffer */ 292 buf[0] = '\0'; 293 294 for (; oid != end; oid = O_NEXT(oid, oid->len)) { 295 if (oid->len < sizeof(*oid)) 296 errx(1, "invalid oid len %d\n", oid->len); 297 298 switch (oid->type) { 299 default: 300 flush_buf(buf); 301 printf("unrecognized object %d size %d\n", oid->type, oid->len); 302 break; 303 case DN_TEXT: /* list of attached flowsets */ 304 { 305 int i, l; 306 struct { 307 struct dn_id id; 308 uint32_t p[0]; 309 } *d = (void *)oid; 310 l = (oid->len - sizeof(*oid))/sizeof(d->p[0]); 311 if (l == 0) 312 break; 313 printf(" Children flowsets: "); 314 for (i = 0; i < l; i++) 315 printf("%u ", d->p[i]); 316 printf("\n"); 317 break; 318 } 319 case DN_CMD_GET: 320 if (co.verbose) 321 printf("answer for cmd %d, len %d\n", oid->type, oid->id); 322 break; 323 case DN_SCH: { 324 struct dn_sch *s = (struct dn_sch *)oid; 325 flush_buf(buf); 326 printf(" sched %d type %s flags 0x%x %d buckets %d active\n", 327 s->sched_nr, 328 s->name, s->flags, s->buckets, s->oid.id); 329 if (s->flags & DN_HAVE_MASK) 330 print_mask(&s->sched_mask); 331 } 332 break; 333 334 case DN_FLOW: 335 list_flow((struct dn_flow *)oid); 336 break; 337 338 case DN_LINK: { 339 struct dn_link *p = (struct dn_link *)oid; 340 double b = p->bandwidth; 341 char bwbuf[30]; 342 char burst[5 + 7]; 343 344 /* This starts a new object so flush buffer */ 345 flush_buf(buf); 346 /* data rate */ 347 if (b == 0) 348 sprintf(bwbuf, "unlimited "); 349 else if (b >= 1000000) 350 sprintf(bwbuf, "%7.3f Mbit/s", b/1000000); 351 else if (b >= 1000) 352 sprintf(bwbuf, "%7.3f Kbit/s", b/1000); 353 else 354 sprintf(bwbuf, "%7.3f bit/s ", b); 355 356 if (humanize_number(burst, sizeof(burst), p->burst, 357 "", HN_AUTOSCALE, 0) < 0 || co.verbose) 358 sprintf(burst, "%d", (int)p->burst); 359 sprintf(buf, "%05d: %s %4d ms burst %s", 360 p->link_nr % DN_MAX_ID, bwbuf, p->delay, burst); 361 } 362 break; 363 364 case DN_FS: 365 print_flowset_parms((struct dn_fs *)oid, buf); 366 break; 367 case DN_PROFILE: 368 flush_buf(buf); 369 print_extra_delay_parms((struct dn_profile *)oid); 370 } 371 flush_buf(buf); // XXX does it really go here ? 372 } 373 } 374 375 /* 376 * Delete pipe, queue or scheduler i 377 */ 378 int 379 ipfw_delete_pipe(int do_pipe, int i) 380 { 381 struct { 382 struct dn_id oid; 383 uintptr_t a[1]; /* add more if we want a list */ 384 } cmd; 385 oid_fill((void *)&cmd, sizeof(cmd), DN_CMD_DELETE, DN_API_VERSION); 386 cmd.oid.subtype = (do_pipe == 1) ? DN_LINK : 387 ( (do_pipe == 2) ? DN_FS : DN_SCH); 388 cmd.a[0] = i; 389 i = do_cmd(IP_DUMMYNET3, &cmd, cmd.oid.len); 390 if (i) { 391 i = 1; 392 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i); 393 } 394 return i; 395 } 396 397 /* 398 * Code to parse delay profiles. 399 * 400 * Some link types introduce extra delays in the transmission 401 * of a packet, e.g. because of MAC level framing, contention on 402 * the use of the channel, MAC level retransmissions and so on. 403 * From our point of view, the channel is effectively unavailable 404 * for this extra time, which is constant or variable depending 405 * on the link type. Additionally, packets may be dropped after this 406 * time (e.g. on a wireless link after too many retransmissions). 407 * We can model the additional delay with an empirical curve 408 * that represents its distribution. 409 * 410 * cumulative probability 411 * 1.0 ^ 412 * | 413 * L +-- loss-level x 414 * | ****** 415 * | * 416 * | ***** 417 * | * 418 * | ** 419 * | * 420 * +-------*-------------------> 421 * delay 422 * 423 * The empirical curve may have both vertical and horizontal lines. 424 * Vertical lines represent constant delay for a range of 425 * probabilities; horizontal lines correspond to a discontinuty 426 * in the delay distribution: the link will use the largest delay 427 * for a given probability. 428 * 429 * To pass the curve to dummynet, we must store the parameters 430 * in a file as described below, and issue the command 431 * 432 * ipfw pipe <n> config ... bw XXX profile <filename> ... 433 * 434 * The file format is the following, with whitespace acting as 435 * a separator and '#' indicating the beginning a comment: 436 * 437 * samples N 438 * the number of samples used in the internal 439 * representation (2..1024; default 100); 440 * 441 * loss-level L 442 * The probability above which packets are lost. 443 * (0.0 <= L <= 1.0, default 1.0 i.e. no loss); 444 * 445 * name identifier 446 * Optional a name (listed by "ipfw pipe show") 447 * to identify the distribution; 448 * 449 * "delay prob" | "prob delay" 450 * One of these two lines is mandatory and defines 451 * the format of the following lines with data points. 452 * 453 * XXX YYY 454 * 2 or more lines representing points in the curve, 455 * with either delay or probability first, according 456 * to the chosen format. 457 * The unit for delay is milliseconds. 458 * 459 * Data points does not need to be ordered or equal to the number 460 * specified in the "samples" line. ipfw will sort and interpolate 461 * the curve as needed. 462 * 463 * Example of a profile file: 464 465 name bla_bla_bla 466 samples 100 467 loss-level 0.86 468 prob delay 469 0 200 # minimum overhead is 200ms 470 0.5 200 471 0.5 300 472 0.8 1000 473 0.9 1300 474 1 1300 475 476 * Internally, we will convert the curve to a fixed number of 477 * samples, and when it is time to transmit a packet we will 478 * model the extra delay as extra bits in the packet. 479 * 480 */ 481 482 #define ED_MAX_LINE_LEN 256+ED_MAX_NAME_LEN 483 #define ED_TOK_SAMPLES "samples" 484 #define ED_TOK_LOSS "loss-level" 485 #define ED_TOK_NAME "name" 486 #define ED_TOK_DELAY "delay" 487 #define ED_TOK_PROB "prob" 488 #define ED_TOK_BW "bw" 489 #define ED_SEPARATORS " \t\n" 490 #define ED_MIN_SAMPLES_NO 2 491 492 /* 493 * returns 1 if s is a non-negative number, with at least one '.' 494 */ 495 static int 496 is_valid_number(const char *s) 497 { 498 int i, dots_found = 0; 499 int len = strlen(s); 500 501 for (i = 0; i<len; ++i) 502 if (!isdigit(s[i]) && (s[i] !='.' || ++dots_found > 1)) 503 return 0; 504 return 1; 505 } 506 507 /* 508 * Take as input a string describing a bandwidth value 509 * and return the numeric bandwidth value. 510 * set clocking interface or bandwidth value 511 */ 512 static void 513 read_bandwidth(char *arg, int *bandwidth, char *if_name, int namelen) 514 { 515 if (*bandwidth != -1) 516 warnx("duplicate token, override bandwidth value!"); 517 518 if (arg[0] >= 'a' && arg[0] <= 'z') { 519 if (!if_name) { 520 errx(1, "no if support"); 521 } 522 if (namelen >= IFNAMSIZ) 523 warn("interface name truncated"); 524 namelen--; 525 /* interface name */ 526 strncpy(if_name, arg, namelen); 527 if_name[namelen] = '\0'; 528 *bandwidth = 0; 529 } else { /* read bandwidth value */ 530 int bw; 531 char *end = NULL; 532 533 bw = strtoul(arg, &end, 0); 534 if (*end == 'K' || *end == 'k') { 535 end++; 536 bw *= 1000; 537 } else if (*end == 'M') { 538 end++; 539 bw *= 1000000; 540 } 541 if ((*end == 'B' && 542 _substrcmp2(end, "Bi", "Bit/s") != 0) || 543 _substrcmp2(end, "by", "bytes") == 0) 544 bw *= 8; 545 546 if (bw < 0) 547 errx(EX_DATAERR, "bandwidth too large"); 548 549 *bandwidth = bw; 550 if (if_name) 551 if_name[0] = '\0'; 552 } 553 } 554 555 struct point { 556 double prob; 557 double delay; 558 }; 559 560 static int 561 compare_points(const void *vp1, const void *vp2) 562 { 563 const struct point *p1 = vp1; 564 const struct point *p2 = vp2; 565 double res = 0; 566 567 res = p1->prob - p2->prob; 568 if (res == 0) 569 res = p1->delay - p2->delay; 570 if (res < 0) 571 return -1; 572 else if (res > 0) 573 return 1; 574 else 575 return 0; 576 } 577 578 #define ED_EFMT(s) EX_DATAERR,"error in %s at line %d: "#s,filename,lineno 579 580 static void 581 load_extra_delays(const char *filename, struct dn_profile *p, 582 struct dn_link *link) 583 { 584 char line[ED_MAX_LINE_LEN]; 585 FILE *f; 586 int lineno = 0; 587 int i; 588 589 int samples = -1; 590 double loss = -1.0; 591 char profile_name[ED_MAX_NAME_LEN]; 592 int delay_first = -1; 593 int do_points = 0; 594 struct point points[ED_MAX_SAMPLES_NO]; 595 int points_no = 0; 596 597 /* XXX link never NULL? */ 598 p->link_nr = link->link_nr; 599 600 profile_name[0] = '\0'; 601 f = fopen(filename, "r"); 602 if (f == NULL) 603 err(EX_UNAVAILABLE, "fopen: %s", filename); 604 605 while (fgets(line, ED_MAX_LINE_LEN, f)) { /* read commands */ 606 char *s, *cur = line, *name = NULL, *arg = NULL; 607 608 ++lineno; 609 610 /* parse the line */ 611 while (cur) { 612 s = strsep(&cur, ED_SEPARATORS); 613 if (s == NULL || *s == '#') 614 break; 615 if (*s == '\0') 616 continue; 617 if (arg) 618 errx(ED_EFMT("too many arguments")); 619 if (name == NULL) 620 name = s; 621 else 622 arg = s; 623 } 624 if (name == NULL) /* empty line */ 625 continue; 626 if (arg == NULL) 627 errx(ED_EFMT("missing arg for %s"), name); 628 629 if (!strcasecmp(name, ED_TOK_SAMPLES)) { 630 if (samples > 0) 631 errx(ED_EFMT("duplicate ``samples'' line")); 632 if (atoi(arg) <=0) 633 errx(ED_EFMT("invalid number of samples")); 634 samples = atoi(arg); 635 if (samples>ED_MAX_SAMPLES_NO) 636 errx(ED_EFMT("too many samples, maximum is %d"), 637 ED_MAX_SAMPLES_NO); 638 do_points = 0; 639 } else if (!strcasecmp(name, ED_TOK_BW)) { 640 char buf[IFNAMSIZ]; 641 read_bandwidth(arg, &link->bandwidth, buf, sizeof(buf)); 642 } else if (!strcasecmp(name, ED_TOK_LOSS)) { 643 if (loss != -1.0) 644 errx(ED_EFMT("duplicated token: %s"), name); 645 if (!is_valid_number(arg)) 646 errx(ED_EFMT("invalid %s"), arg); 647 loss = atof(arg); 648 if (loss > 1) 649 errx(ED_EFMT("%s greater than 1.0"), name); 650 do_points = 0; 651 } else if (!strcasecmp(name, ED_TOK_NAME)) { 652 if (profile_name[0] != '\0') 653 errx(ED_EFMT("duplicated token: %s"), name); 654 strncpy(profile_name, arg, sizeof(profile_name) - 1); 655 profile_name[sizeof(profile_name)-1] = '\0'; 656 do_points = 0; 657 } else if (!strcasecmp(name, ED_TOK_DELAY)) { 658 if (do_points) 659 errx(ED_EFMT("duplicated token: %s"), name); 660 delay_first = 1; 661 do_points = 1; 662 } else if (!strcasecmp(name, ED_TOK_PROB)) { 663 if (do_points) 664 errx(ED_EFMT("duplicated token: %s"), name); 665 delay_first = 0; 666 do_points = 1; 667 } else if (do_points) { 668 if (!is_valid_number(name) || !is_valid_number(arg)) 669 errx(ED_EFMT("invalid point found")); 670 if (delay_first) { 671 points[points_no].delay = atof(name); 672 points[points_no].prob = atof(arg); 673 } else { 674 points[points_no].delay = atof(arg); 675 points[points_no].prob = atof(name); 676 } 677 if (points[points_no].prob > 1.0) 678 errx(ED_EFMT("probability greater than 1.0")); 679 ++points_no; 680 } else { 681 errx(ED_EFMT("unrecognised command '%s'"), name); 682 } 683 } 684 685 fclose (f); 686 687 if (samples == -1) { 688 warnx("'%s' not found, assuming 100", ED_TOK_SAMPLES); 689 samples = 100; 690 } 691 692 if (loss == -1.0) { 693 warnx("'%s' not found, assuming no loss", ED_TOK_LOSS); 694 loss = 1; 695 } 696 697 /* make sure that there are enough points. */ 698 if (points_no < ED_MIN_SAMPLES_NO) 699 errx(ED_EFMT("too few samples, need at least %d"), 700 ED_MIN_SAMPLES_NO); 701 702 qsort(points, points_no, sizeof(struct point), compare_points); 703 704 /* interpolation */ 705 for (i = 0; i<points_no-1; ++i) { 706 double y1 = points[i].prob * samples; 707 double x1 = points[i].delay; 708 double y2 = points[i+1].prob * samples; 709 double x2 = points[i+1].delay; 710 711 int ix = y1; 712 int stop = y2; 713 714 if (x1 == x2) { 715 for (; ix<stop; ++ix) 716 p->samples[ix] = x1; 717 } else { 718 double m = (y2-y1)/(x2-x1); 719 double c = y1 - m*x1; 720 for (; ix<stop ; ++ix) 721 p->samples[ix] = (ix - c)/m; 722 } 723 } 724 p->samples_no = samples; 725 p->loss_level = loss * samples; 726 strncpy(p->name, profile_name, sizeof(p->name)); 727 } 728 729 /* 730 * configuration of pipes, schedulers, flowsets. 731 * When we configure a new scheduler, an empty pipe is created, so: 732 * 733 * do_pipe = 1 -> "pipe N config ..." only for backward compatibility 734 * sched N+Delta type fifo sched_mask ... 735 * pipe N+Delta <parameters> 736 * flowset N+Delta pipe N+Delta (no parameters) 737 * sched N type wf2q+ sched_mask ... 738 * pipe N <parameters> 739 * 740 * do_pipe = 2 -> flowset N config 741 * flowset N parameters 742 * 743 * do_pipe = 3 -> sched N config 744 * sched N parameters (default no pipe) 745 * optional Pipe N config ... 746 * pipe ==> 747 */ 748 void 749 ipfw_config_pipe(int ac, char **av) 750 { 751 int i, j; 752 char *end; 753 void *par = NULL; 754 struct dn_id *buf, *base; 755 struct dn_sch *sch = NULL; 756 struct dn_link *p = NULL; 757 struct dn_fs *fs = NULL; 758 struct dn_profile *pf = NULL; 759 struct ipfw_flow_id *mask = NULL; 760 int lmax; 761 uint32_t _foo = 0, *flags = &_foo , *buckets = &_foo; 762 763 /* 764 * allocate space for 1 header, 765 * 1 scheduler, 1 link, 1 flowset, 1 profile 766 */ 767 lmax = sizeof(struct dn_id); /* command header */ 768 lmax += sizeof(struct dn_sch) + sizeof(struct dn_link) + 769 sizeof(struct dn_fs) + sizeof(struct dn_profile); 770 771 av++; ac--; 772 /* Pipe number */ 773 if (ac && isdigit(**av)) { 774 i = atoi(*av); av++; ac--; 775 } else 776 i = -1; 777 if (i <= 0) 778 errx(EX_USAGE, "need a pipe/flowset/sched number"); 779 base = buf = safe_calloc(1, lmax); 780 /* all commands start with a 'CONFIGURE' and a version */ 781 o_next(&buf, sizeof(struct dn_id), DN_CMD_CONFIG); 782 base->id = DN_API_VERSION; 783 784 switch (co.do_pipe) { 785 case 1: /* "pipe N config ..." */ 786 /* Allocate space for the WF2Q+ scheduler, its link 787 * and the FIFO flowset. Set the number, but leave 788 * the scheduler subtype and other parameters to 0 789 * so the kernel will use appropriate defaults. 790 * XXX todo: add a flag to record if a parameter 791 * is actually configured. 792 * If we do a 'pipe config' mask -> sched_mask. 793 * The FIFO scheduler and link are derived from the 794 * WF2Q+ one in the kernel. 795 */ 796 sch = o_next(&buf, sizeof(*sch), DN_SCH); 797 p = o_next(&buf, sizeof(*p), DN_LINK); 798 fs = o_next(&buf, sizeof(*fs), DN_FS); 799 800 sch->sched_nr = i; 801 sch->oid.subtype = 0; /* defaults to WF2Q+ */ 802 mask = &sch->sched_mask; 803 flags = &sch->flags; 804 buckets = &sch->buckets; 805 *flags |= DN_PIPE_CMD; 806 807 p->link_nr = i; 808 809 /* This flowset is only for the FIFO scheduler */ 810 fs->fs_nr = i + 2*DN_MAX_ID; 811 fs->sched_nr = i + DN_MAX_ID; 812 break; 813 814 case 2: /* "queue N config ... " */ 815 fs = o_next(&buf, sizeof(*fs), DN_FS); 816 fs->fs_nr = i; 817 mask = &fs->flow_mask; 818 flags = &fs->flags; 819 buckets = &fs->buckets; 820 break; 821 822 case 3: /* "sched N config ..." */ 823 sch = o_next(&buf, sizeof(*sch), DN_SCH); 824 fs = o_next(&buf, sizeof(*fs), DN_FS); 825 sch->sched_nr = i; 826 mask = &sch->sched_mask; 827 flags = &sch->flags; 828 buckets = &sch->buckets; 829 /* fs is used only with !MULTIQUEUE schedulers */ 830 fs->fs_nr = i + DN_MAX_ID; 831 fs->sched_nr = i; 832 break; 833 } 834 /* set to -1 those fields for which we want to reuse existing 835 * values from the kernel. 836 * Also, *_nr and subtype = 0 mean reuse the value from the kernel. 837 * XXX todo: support reuse of the mask. 838 */ 839 if (p) 840 p->bandwidth = -1; 841 for (j = 0; j < sizeof(fs->par)/sizeof(fs->par[0]); j++) 842 fs->par[j] = -1; 843 while (ac > 0) { 844 double d; 845 int tok = match_token(dummynet_params, *av); 846 ac--; av++; 847 848 switch(tok) { 849 case TOK_NOERROR: 850 NEED(fs, "noerror is only for pipes"); 851 fs->flags |= DN_NOERROR; 852 break; 853 854 case TOK_PLR: 855 NEED(fs, "plr is only for pipes"); 856 NEED1("plr needs argument 0..1\n"); 857 d = strtod(av[0], NULL); 858 if (d > 1) 859 d = 1; 860 else if (d < 0) 861 d = 0; 862 fs->plr = (int)(d*0x7fffffff); 863 ac--; av++; 864 break; 865 866 case TOK_QUEUE: 867 NEED(fs, "queue is only for pipes or flowsets"); 868 NEED1("queue needs queue size\n"); 869 end = NULL; 870 fs->qsize = strtoul(av[0], &end, 0); 871 if (*end == 'K' || *end == 'k') { 872 fs->flags |= DN_QSIZE_BYTES; 873 fs->qsize *= 1024; 874 } else if (*end == 'B' || 875 _substrcmp2(end, "by", "bytes") == 0) { 876 fs->flags |= DN_QSIZE_BYTES; 877 } 878 ac--; av++; 879 break; 880 881 case TOK_BUCKETS: 882 NEED(fs, "buckets is only for pipes or flowsets"); 883 NEED1("buckets needs argument\n"); 884 *buckets = strtoul(av[0], NULL, 0); 885 ac--; av++; 886 break; 887 888 case TOK_FLOW_MASK: 889 case TOK_SCHED_MASK: 890 case TOK_MASK: 891 NEED(mask, "tok_mask"); 892 NEED1("mask needs mask specifier\n"); 893 /* 894 * per-flow queue, mask is dst_ip, dst_port, 895 * src_ip, src_port, proto measured in bits 896 */ 897 par = NULL; 898 899 bzero(mask, sizeof(*mask)); 900 end = NULL; 901 902 while (ac >= 1) { 903 uint32_t *p32 = NULL; 904 uint16_t *p16 = NULL; 905 uint32_t *p20 = NULL; 906 struct in6_addr *pa6 = NULL; 907 uint32_t a; 908 909 tok = match_token(dummynet_params, *av); 910 ac--; av++; 911 switch(tok) { 912 case TOK_ALL: 913 /* 914 * special case, all bits significant 915 */ 916 mask->dst_ip = ~0; 917 mask->src_ip = ~0; 918 mask->dst_port = ~0; 919 mask->src_port = ~0; 920 mask->proto = ~0; 921 n2mask(&mask->dst_ip6, 128); 922 n2mask(&mask->src_ip6, 128); 923 mask->flow_id6 = ~0; 924 *flags |= DN_HAVE_MASK; 925 goto end_mask; 926 927 case TOK_DSTIP: 928 mask->addr_type = 4; 929 p32 = &mask->dst_ip; 930 break; 931 932 case TOK_SRCIP: 933 mask->addr_type = 4; 934 p32 = &mask->src_ip; 935 break; 936 937 case TOK_DSTIP6: 938 mask->addr_type = 6; 939 pa6 = &mask->dst_ip6; 940 break; 941 942 case TOK_SRCIP6: 943 mask->addr_type = 6; 944 pa6 = &mask->src_ip6; 945 break; 946 947 case TOK_FLOWID: 948 mask->addr_type = 6; 949 p20 = &mask->flow_id6; 950 break; 951 952 case TOK_DSTPORT: 953 p16 = &mask->dst_port; 954 break; 955 956 case TOK_SRCPORT: 957 p16 = &mask->src_port; 958 break; 959 960 case TOK_PROTO: 961 break; 962 963 default: 964 ac++; av--; /* backtrack */ 965 goto end_mask; 966 } 967 if (ac < 1) 968 errx(EX_USAGE, "mask: value missing"); 969 if (*av[0] == '/') { 970 a = strtoul(av[0]+1, &end, 0); 971 if (pa6 == NULL) 972 a = (a == 32) ? ~0 : (1 << a) - 1; 973 } else 974 a = strtoul(av[0], &end, 0); 975 if (p32 != NULL) 976 *p32 = a; 977 else if (p16 != NULL) { 978 if (a > 0xFFFF) 979 errx(EX_DATAERR, 980 "port mask must be 16 bit"); 981 *p16 = (uint16_t)a; 982 } else if (p20 != NULL) { 983 if (a > 0xfffff) 984 errx(EX_DATAERR, 985 "flow_id mask must be 20 bit"); 986 *p20 = (uint32_t)a; 987 } else if (pa6 != NULL) { 988 if (a > 128) 989 errx(EX_DATAERR, 990 "in6addr invalid mask len"); 991 else 992 n2mask(pa6, a); 993 } else { 994 if (a > 0xFF) 995 errx(EX_DATAERR, 996 "proto mask must be 8 bit"); 997 fs->flow_mask.proto = (uint8_t)a; 998 } 999 if (a != 0) 1000 *flags |= DN_HAVE_MASK; 1001 ac--; av++; 1002 } /* end while, config masks */ 1003 end_mask: 1004 break; 1005 1006 case TOK_RED: 1007 case TOK_GRED: 1008 NEED1("red/gred needs w_q/min_th/max_th/max_p\n"); 1009 fs->flags |= DN_IS_RED; 1010 if (tok == TOK_GRED) 1011 fs->flags |= DN_IS_GENTLE_RED; 1012 /* 1013 * the format for parameters is w_q/min_th/max_th/max_p 1014 */ 1015 if ((end = strsep(&av[0], "/"))) { 1016 double w_q = strtod(end, NULL); 1017 if (w_q > 1 || w_q <= 0) 1018 errx(EX_DATAERR, "0 < w_q <= 1"); 1019 fs->w_q = (int) (w_q * (1 << SCALE_RED)); 1020 } 1021 if ((end = strsep(&av[0], "/"))) { 1022 fs->min_th = strtoul(end, &end, 0); 1023 if (*end == 'K' || *end == 'k') 1024 fs->min_th *= 1024; 1025 } 1026 if ((end = strsep(&av[0], "/"))) { 1027 fs->max_th = strtoul(end, &end, 0); 1028 if (*end == 'K' || *end == 'k') 1029 fs->max_th *= 1024; 1030 } 1031 if ((end = strsep(&av[0], "/"))) { 1032 double max_p = strtod(end, NULL); 1033 if (max_p > 1 || max_p <= 0) 1034 errx(EX_DATAERR, "0 < max_p <= 1"); 1035 fs->max_p = (int)(max_p * (1 << SCALE_RED)); 1036 } 1037 ac--; av++; 1038 break; 1039 1040 case TOK_DROPTAIL: 1041 NEED(fs, "droptail is only for flowsets"); 1042 fs->flags &= ~(DN_IS_RED|DN_IS_GENTLE_RED); 1043 break; 1044 1045 case TOK_BW: 1046 NEED(p, "bw is only for links"); 1047 NEED1("bw needs bandwidth or interface\n"); 1048 read_bandwidth(av[0], &p->bandwidth, NULL, 0); 1049 ac--; av++; 1050 break; 1051 1052 case TOK_DELAY: 1053 NEED(p, "delay is only for links"); 1054 NEED1("delay needs argument 0..10000ms\n"); 1055 p->delay = strtoul(av[0], NULL, 0); 1056 ac--; av++; 1057 break; 1058 1059 case TOK_TYPE: { 1060 int l; 1061 NEED(sch, "type is only for schedulers"); 1062 NEED1("type needs a string"); 1063 l = strlen(av[0]); 1064 if (l == 0 || l > 15) 1065 errx(1, "type %s too long\n", av[0]); 1066 strcpy(sch->name, av[0]); 1067 sch->oid.subtype = 0; /* use string */ 1068 ac--; av++; 1069 break; 1070 } 1071 1072 case TOK_WEIGHT: 1073 NEED(fs, "weight is only for flowsets"); 1074 NEED1("weight needs argument\n"); 1075 fs->par[0] = strtol(av[0], &end, 0); 1076 ac--; av++; 1077 break; 1078 1079 case TOK_LMAX: 1080 NEED(fs, "lmax is only for flowsets"); 1081 NEED1("lmax needs argument\n"); 1082 fs->par[1] = strtol(av[0], &end, 0); 1083 ac--; av++; 1084 break; 1085 1086 case TOK_PRI: 1087 NEED(fs, "priority is only for flowsets"); 1088 NEED1("priority needs argument\n"); 1089 fs->par[2] = strtol(av[0], &end, 0); 1090 ac--; av++; 1091 break; 1092 1093 case TOK_SCHED: 1094 case TOK_PIPE: 1095 NEED(fs, "pipe/sched"); 1096 NEED1("pipe/link/sched needs number\n"); 1097 fs->sched_nr = strtoul(av[0], &end, 0); 1098 ac--; av++; 1099 break; 1100 1101 case TOK_PROFILE: 1102 NEED((!pf), "profile already set"); 1103 NEED(p, "profile"); 1104 { 1105 NEED1("extra delay needs the file name\n"); 1106 pf = o_next(&buf, sizeof(*pf), DN_PROFILE); 1107 load_extra_delays(av[0], pf, p); //XXX can't fail? 1108 --ac; ++av; 1109 } 1110 break; 1111 1112 case TOK_BURST: 1113 NEED(p, "burst"); 1114 NEED1("burst needs argument\n"); 1115 errno = 0; 1116 if (expand_number(av[0], (int64_t *)&p->burst) < 0) 1117 if (errno != ERANGE) 1118 errx(EX_DATAERR, 1119 "burst: invalid argument"); 1120 if (errno || p->burst > (1ULL << 48) - 1) 1121 errx(EX_DATAERR, 1122 "burst: out of range (0..2^48-1)"); 1123 ac--; av++; 1124 break; 1125 1126 default: 1127 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]); 1128 } 1129 } 1130 1131 /* check validity of parameters */ 1132 if (p) { 1133 if (p->delay > 10000) 1134 errx(EX_DATAERR, "delay must be < 10000"); 1135 if (p->bandwidth == -1) 1136 p->bandwidth = 0; 1137 } 1138 if (fs) { 1139 /* XXX accept a 0 scheduler to keep the default */ 1140 if (fs->flags & DN_QSIZE_BYTES) { 1141 size_t len; 1142 long limit; 1143 1144 len = sizeof(limit); 1145 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit", 1146 &limit, &len, NULL, 0) == -1) 1147 limit = 1024*1024; 1148 if (fs->qsize > limit) 1149 errx(EX_DATAERR, "queue size must be < %ldB", limit); 1150 } else { 1151 size_t len; 1152 long limit; 1153 1154 len = sizeof(limit); 1155 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit", 1156 &limit, &len, NULL, 0) == -1) 1157 limit = 100; 1158 if (fs->qsize > limit) 1159 errx(EX_DATAERR, "2 <= queue size <= %ld", limit); 1160 } 1161 1162 if (fs->flags & DN_IS_RED) { 1163 size_t len; 1164 int lookup_depth, avg_pkt_size; 1165 double w_q; 1166 1167 if (fs->min_th >= fs->max_th) 1168 errx(EX_DATAERR, "min_th %d must be < than max_th %d", 1169 fs->min_th, fs->max_th); 1170 if (fs->max_th == 0) 1171 errx(EX_DATAERR, "max_th must be > 0"); 1172 1173 len = sizeof(int); 1174 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth", 1175 &lookup_depth, &len, NULL, 0) == -1) 1176 lookup_depth = 256; 1177 if (lookup_depth == 0) 1178 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth" 1179 " must be greater than zero"); 1180 1181 len = sizeof(int); 1182 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size", 1183 &avg_pkt_size, &len, NULL, 0) == -1) 1184 avg_pkt_size = 512; 1185 1186 if (avg_pkt_size == 0) 1187 errx(EX_DATAERR, 1188 "net.inet.ip.dummynet.red_avg_pkt_size must" 1189 " be greater than zero"); 1190 1191 /* 1192 * Ticks needed for sending a medium-sized packet. 1193 * Unfortunately, when we are configuring a WF2Q+ queue, we 1194 * do not have bandwidth information, because that is stored 1195 * in the parent pipe, and also we have multiple queues 1196 * competing for it. So we set s=0, which is not very 1197 * correct. But on the other hand, why do we want RED with 1198 * WF2Q+ ? 1199 */ 1200 #if 0 1201 if (p.bandwidth==0) /* this is a WF2Q+ queue */ 1202 s = 0; 1203 else 1204 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth; 1205 #endif 1206 /* 1207 * max idle time (in ticks) before avg queue size becomes 0. 1208 * NOTA: (3/w_q) is approx the value x so that 1209 * (1-w_q)^x < 10^-3. 1210 */ 1211 w_q = ((double)fs->w_q) / (1 << SCALE_RED); 1212 #if 0 // go in kernel 1213 idle = s * 3. / w_q; 1214 fs->lookup_step = (int)idle / lookup_depth; 1215 if (!fs->lookup_step) 1216 fs->lookup_step = 1; 1217 weight = 1 - w_q; 1218 for (t = fs->lookup_step; t > 1; --t) 1219 weight *= 1 - w_q; 1220 fs->lookup_weight = (int)(weight * (1 << SCALE_RED)); 1221 #endif 1222 } 1223 } 1224 1225 i = do_cmd(IP_DUMMYNET3, base, (char *)buf - (char *)base); 1226 1227 if (i) 1228 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE"); 1229 } 1230 1231 void 1232 dummynet_flush(void) 1233 { 1234 struct dn_id oid; 1235 oid_fill(&oid, sizeof(oid), DN_CMD_FLUSH, DN_API_VERSION); 1236 do_cmd(IP_DUMMYNET3, &oid, oid.len); 1237 } 1238 1239 /* main entry point for dummynet list functions. co.do_pipe indicates 1240 * which function we want to support. 1241 * XXX todo- accept filtering arguments. 1242 */ 1243 void 1244 dummynet_list(int ac, char *av[], int show_counters) 1245 { 1246 struct dn_id oid, *x; 1247 int ret, l = sizeof(oid); 1248 1249 oid_fill(&oid, l, DN_CMD_GET, DN_API_VERSION); 1250 switch (co.do_pipe) { 1251 case 1: 1252 oid.subtype = DN_LINK; /* list pipe */ 1253 break; 1254 case 2: 1255 oid.subtype = DN_FS; /* list queue */ 1256 break; 1257 case 3: 1258 oid.subtype = DN_SCH; /* list sched */ 1259 break; 1260 } 1261 ret = do_cmd(-IP_DUMMYNET3, &oid, (uintptr_t)&l); 1262 // printf("%s returns %d need %d\n", __FUNCTION__, ret, oid.id); 1263 if (ret != 0 || oid.id <= sizeof(oid)) 1264 return; 1265 l = oid.id; 1266 x = safe_calloc(1, l); 1267 *x = oid; 1268 ret = do_cmd(-IP_DUMMYNET3, x, (uintptr_t)&l); 1269 // printf("%s returns %d need %d\n", __FUNCTION__, ret, oid.id); 1270 // XXX filter on ac, av 1271 list_pipes(x, O_NEXT(x, l)); 1272 free(x); 1273 } 1274