dummynet.c revision 187769
1/* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * 9 * Redistribution and use in source forms, with and without modification, 10 * are permitted provided that this entire comment appears intact. 11 * 12 * Redistribution in binary form may occur without any restrictions. 13 * Obviously, it would be nice if you gave credit where credit is due 14 * but requiring it would be too onerous. 15 * 16 * This software is provided ``AS IS'' without any warranties of any kind. 17 * 18 * NEW command line interface for IP firewall facility 19 * 20 * $FreeBSD: head/sbin/ipfw/dummynet.c 187769 2009-01-27 11:06:59Z luigi $ 21 * 22 * dummynet support 23 */ 24 25#include <sys/types.h> 26#include <sys/socket.h> 27#include <sys/sockio.h> 28#include <sys/sysctl.h> 29 30#include "ipfw2.h" 31 32#include <ctype.h> 33#include <err.h> 34#include <netdb.h> 35#include <stdio.h> 36#include <stdlib.h> 37#include <string.h> 38#include <sysexits.h> 39 40#include <net/if.h> 41#include <netinet/in.h> 42#include <netinet/in_systm.h> 43#include <netinet/ip.h> 44#include <netinet/ip_fw.h> 45#include <netinet/ip_dummynet.h> 46#include <arpa/inet.h> 47 48static struct _s_x dummynet_params[] = { 49 { "plr", TOK_PLR }, 50 { "noerror", TOK_NOERROR }, 51 { "buckets", TOK_BUCKETS }, 52 { "dst-ip", TOK_DSTIP }, 53 { "src-ip", TOK_SRCIP }, 54 { "dst-port", TOK_DSTPORT }, 55 { "src-port", TOK_SRCPORT }, 56 { "proto", TOK_PROTO }, 57 { "weight", TOK_WEIGHT }, 58 { "all", TOK_ALL }, 59 { "mask", TOK_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 { "pipe", TOK_PIPE }, 67 { "queue", TOK_QUEUE }, 68 { "flow-id", TOK_FLOWID}, 69 { "dst-ipv6", TOK_DSTIP6}, 70 { "dst-ip6", TOK_DSTIP6}, 71 { "src-ipv6", TOK_SRCIP6}, 72 { "src-ip6", TOK_SRCIP6}, 73 { "dummynet-params", TOK_NULL }, 74 { NULL, 0 } /* terminator */ 75}; 76 77static int 78sort_q(const void *pa, const void *pb) 79{ 80 int rev = (co.do_sort < 0); 81 int field = rev ? -co.do_sort : co.do_sort; 82 long long res = 0; 83 const struct dn_flow_queue *a = pa; 84 const struct dn_flow_queue *b = pb; 85 86 switch (field) { 87 case 1: /* pkts */ 88 res = a->len - b->len; 89 break; 90 case 2: /* bytes */ 91 res = a->len_bytes - b->len_bytes; 92 break; 93 94 case 3: /* tot pkts */ 95 res = a->tot_pkts - b->tot_pkts; 96 break; 97 98 case 4: /* tot bytes */ 99 res = a->tot_bytes - b->tot_bytes; 100 break; 101 } 102 if (res < 0) 103 res = -1; 104 if (res > 0) 105 res = 1; 106 return (int)(rev ? res : -res); 107} 108 109static void 110list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q) 111{ 112 int l; 113 int index_printed, indexes = 0; 114 char buff[255]; 115 struct protoent *pe; 116 117 if (fs->rq_elements == 0) 118 return; 119 120 if (co.do_sort != 0) 121 heapsort(q, fs->rq_elements, sizeof *q, sort_q); 122 123 /* Print IPv4 flows */ 124 index_printed = 0; 125 for (l = 0; l < fs->rq_elements; l++) { 126 struct in_addr ina; 127 128 /* XXX: Should check for IPv4 flows */ 129 if (IS_IP6_FLOW_ID(&(q[l].id))) 130 continue; 131 132 if (!index_printed) { 133 index_printed = 1; 134 if (indexes > 0) /* currently a no-op */ 135 printf("\n"); 136 indexes++; 137 printf(" " 138 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n", 139 fs->flow_mask.proto, 140 fs->flow_mask.src_ip, fs->flow_mask.src_port, 141 fs->flow_mask.dst_ip, fs->flow_mask.dst_port); 142 143 printf("BKT Prot ___Source IP/port____ " 144 "____Dest. IP/port____ " 145 "Tot_pkt/bytes Pkt/Byte Drp\n"); 146 } 147 148 printf("%3d ", q[l].hash_slot); 149 pe = getprotobynumber(q[l].id.proto); 150 if (pe) 151 printf("%-4s ", pe->p_name); 152 else 153 printf("%4u ", q[l].id.proto); 154 ina.s_addr = htonl(q[l].id.src_ip); 155 printf("%15s/%-5d ", 156 inet_ntoa(ina), q[l].id.src_port); 157 ina.s_addr = htonl(q[l].id.dst_ip); 158 printf("%15s/%-5d ", 159 inet_ntoa(ina), q[l].id.dst_port); 160 printf("%4qu %8qu %2u %4u %3u\n", 161 q[l].tot_pkts, q[l].tot_bytes, 162 q[l].len, q[l].len_bytes, q[l].drops); 163 if (co.verbose) 164 printf(" S %20qd F %20qd\n", 165 q[l].S, q[l].F); 166 } 167 168 /* Print IPv6 flows */ 169 index_printed = 0; 170 for (l = 0; l < fs->rq_elements; l++) { 171 if (!IS_IP6_FLOW_ID(&(q[l].id))) 172 continue; 173 174 if (!index_printed) { 175 index_printed = 1; 176 if (indexes > 0) 177 printf("\n"); 178 indexes++; 179 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ", 180 fs->flow_mask.proto, fs->flow_mask.flow_id6); 181 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6), 182 buff, sizeof(buff)); 183 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port); 184 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6), 185 buff, sizeof(buff) ); 186 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port); 187 188 printf("BKT ___Prot___ _flow-id_ " 189 "______________Source IPv6/port_______________ " 190 "_______________Dest. IPv6/port_______________ " 191 "Tot_pkt/bytes Pkt/Byte Drp\n"); 192 } 193 printf("%3d ", q[l].hash_slot); 194 pe = getprotobynumber(q[l].id.proto); 195 if (pe != NULL) 196 printf("%9s ", pe->p_name); 197 else 198 printf("%9u ", q[l].id.proto); 199 printf("%7d %39s/%-5d ", q[l].id.flow_id6, 200 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)), 201 q[l].id.src_port); 202 printf(" %39s/%-5d ", 203 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)), 204 q[l].id.dst_port); 205 printf(" %4qu %8qu %2u %4u %3u\n", 206 q[l].tot_pkts, q[l].tot_bytes, 207 q[l].len, q[l].len_bytes, q[l].drops); 208 if (co.verbose) 209 printf(" S %20qd F %20qd\n", q[l].S, q[l].F); 210 } 211} 212 213static void 214print_flowset_parms(struct dn_flow_set *fs, char *prefix) 215{ 216 int l; 217 char qs[30]; 218 char plr[30]; 219 char red[90]; /* Display RED parameters */ 220 221 l = fs->qsize; 222 if (fs->flags_fs & DN_QSIZE_IS_BYTES) { 223 if (l >= 8192) 224 sprintf(qs, "%d KB", l / 1024); 225 else 226 sprintf(qs, "%d B", l); 227 } else 228 sprintf(qs, "%3d sl.", l); 229 if (fs->plr) 230 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff)); 231 else 232 plr[0] = '\0'; 233 if (fs->flags_fs & DN_IS_RED) /* RED parameters */ 234 sprintf(red, 235 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f", 236 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ', 237 1.0 * fs->w_q / (double)(1 << SCALE_RED), 238 SCALE_VAL(fs->min_th), 239 SCALE_VAL(fs->max_th), 240 1.0 * fs->max_p / (double)(1 << SCALE_RED)); 241 else 242 sprintf(red, "droptail"); 243 244 printf("%s %s%s %d queues (%d buckets) %s\n", 245 prefix, qs, plr, fs->rq_elements, fs->rq_size, red); 246} 247 248void 249ipfw_list_pipes(void *data, uint nbytes, int ac, char *av[]) 250{ 251 int rulenum; 252 void *next = data; 253 struct dn_pipe *p = (struct dn_pipe *) data; 254 struct dn_flow_set *fs; 255 struct dn_flow_queue *q; 256 int l; 257 258 if (ac > 0) 259 rulenum = strtoul(*av++, NULL, 10); 260 else 261 rulenum = 0; 262 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) { 263 double b = p->bandwidth; 264 char buf[30]; 265 char prefix[80]; 266 267 if (SLIST_NEXT(p, next) != (struct dn_pipe *)DN_IS_PIPE) 268 break; /* done with pipes, now queues */ 269 270 /* 271 * compute length, as pipe have variable size 272 */ 273 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q); 274 next = (char *)p + l; 275 nbytes -= l; 276 277 if ((rulenum != 0 && rulenum != p->pipe_nr) || co.do_pipe == 2) 278 continue; 279 280 /* 281 * Print rate (or clocking interface) 282 */ 283 if (p->if_name[0] != '\0') 284 sprintf(buf, "%s", p->if_name); 285 else if (b == 0) 286 sprintf(buf, "unlimited"); 287 else if (b >= 1000000) 288 sprintf(buf, "%7.3f Mbit/s", b/1000000); 289 else if (b >= 1000) 290 sprintf(buf, "%7.3f Kbit/s", b/1000); 291 else 292 sprintf(buf, "%7.3f bit/s ", b); 293 294 sprintf(prefix, "%05d: %s %4d ms ", 295 p->pipe_nr, buf, p->delay); 296 print_flowset_parms(&(p->fs), prefix); 297 if (co.verbose) 298 printf(" V %20qd\n", p->V >> MY_M); 299 300 q = (struct dn_flow_queue *)(p+1); 301 list_queues(&(p->fs), q); 302 } 303 for (fs = next; nbytes >= sizeof *fs; fs = next) { 304 char prefix[80]; 305 306 if (SLIST_NEXT(fs, next) != (struct dn_flow_set *)DN_IS_QUEUE) 307 break; 308 l = sizeof(*fs) + fs->rq_elements * sizeof(*q); 309 next = (char *)fs + l; 310 nbytes -= l; 311 312 if (rulenum != 0 && ((rulenum != fs->fs_nr && co.do_pipe == 2) || 313 (rulenum != fs->parent_nr && co.do_pipe == 1))) { 314 continue; 315 } 316 317 q = (struct dn_flow_queue *)(fs+1); 318 sprintf(prefix, "q%05d: weight %d pipe %d ", 319 fs->fs_nr, fs->weight, fs->parent_nr); 320 print_flowset_parms(fs, prefix); 321 list_queues(fs, q); 322 } 323} 324 325/* 326 * Delete pipe or queue i 327 */ 328int 329ipfw_delete_pipe(int pipe_or_queue, int n) 330{ 331 struct dn_pipe p; 332 int i; 333 334 memset(&p, 0, sizeof p); 335 if (pipe_or_queue == 1) 336 p.pipe_nr = i; /* pipe */ 337 else 338 p.fs.fs_nr = i; /* queue */ 339 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p); 340 if (i) { 341 i = 1; 342 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", i); 343 } 344 return i; 345} 346 347void 348ipfw_config_pipe(int ac, char **av) 349{ 350 struct dn_pipe p; 351 int i; 352 char *end; 353 void *par = NULL; 354 355 memset(&p, 0, sizeof p); 356 357 av++; ac--; 358 /* Pipe number */ 359 if (ac && isdigit(**av)) { 360 i = atoi(*av); av++; ac--; 361 if (co.do_pipe == 1) 362 p.pipe_nr = i; 363 else 364 p.fs.fs_nr = i; 365 } 366 while (ac > 0) { 367 double d; 368 int tok = match_token(dummynet_params, *av); 369 ac--; av++; 370 371 switch(tok) { 372 case TOK_NOERROR: 373 p.fs.flags_fs |= DN_NOERROR; 374 break; 375 376 case TOK_PLR: 377 NEED1("plr needs argument 0..1\n"); 378 d = strtod(av[0], NULL); 379 if (d > 1) 380 d = 1; 381 else if (d < 0) 382 d = 0; 383 p.fs.plr = (int)(d*0x7fffffff); 384 ac--; av++; 385 break; 386 387 case TOK_QUEUE: 388 NEED1("queue needs queue size\n"); 389 end = NULL; 390 p.fs.qsize = strtoul(av[0], &end, 0); 391 if (*end == 'K' || *end == 'k') { 392 p.fs.flags_fs |= DN_QSIZE_IS_BYTES; 393 p.fs.qsize *= 1024; 394 } else if (*end == 'B' || 395 _substrcmp2(end, "by", "bytes") == 0) { 396 p.fs.flags_fs |= DN_QSIZE_IS_BYTES; 397 } 398 ac--; av++; 399 break; 400 401 case TOK_BUCKETS: 402 NEED1("buckets needs argument\n"); 403 p.fs.rq_size = strtoul(av[0], NULL, 0); 404 ac--; av++; 405 break; 406 407 case TOK_MASK: 408 NEED1("mask needs mask specifier\n"); 409 /* 410 * per-flow queue, mask is dst_ip, dst_port, 411 * src_ip, src_port, proto measured in bits 412 */ 413 par = NULL; 414 415 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask)); 416 end = NULL; 417 418 while (ac >= 1) { 419 uint32_t *p32 = NULL; 420 uint16_t *p16 = NULL; 421 uint32_t *p20 = NULL; 422 struct in6_addr *pa6 = NULL; 423 uint32_t a; 424 425 tok = match_token(dummynet_params, *av); 426 ac--; av++; 427 switch(tok) { 428 case TOK_ALL: 429 /* 430 * special case, all bits significant 431 */ 432 p.fs.flow_mask.dst_ip = ~0; 433 p.fs.flow_mask.src_ip = ~0; 434 p.fs.flow_mask.dst_port = ~0; 435 p.fs.flow_mask.src_port = ~0; 436 p.fs.flow_mask.proto = ~0; 437 n2mask(&(p.fs.flow_mask.dst_ip6), 128); 438 n2mask(&(p.fs.flow_mask.src_ip6), 128); 439 p.fs.flow_mask.flow_id6 = ~0; 440 p.fs.flags_fs |= DN_HAVE_FLOW_MASK; 441 goto end_mask; 442 443 case TOK_DSTIP: 444 p32 = &p.fs.flow_mask.dst_ip; 445 break; 446 447 case TOK_SRCIP: 448 p32 = &p.fs.flow_mask.src_ip; 449 break; 450 451 case TOK_DSTIP6: 452 pa6 = &(p.fs.flow_mask.dst_ip6); 453 break; 454 455 case TOK_SRCIP6: 456 pa6 = &(p.fs.flow_mask.src_ip6); 457 break; 458 459 case TOK_FLOWID: 460 p20 = &p.fs.flow_mask.flow_id6; 461 break; 462 463 case TOK_DSTPORT: 464 p16 = &p.fs.flow_mask.dst_port; 465 break; 466 467 case TOK_SRCPORT: 468 p16 = &p.fs.flow_mask.src_port; 469 break; 470 471 case TOK_PROTO: 472 break; 473 474 default: 475 ac++; av--; /* backtrack */ 476 goto end_mask; 477 } 478 if (ac < 1) 479 errx(EX_USAGE, "mask: value missing"); 480 if (*av[0] == '/') { 481 a = strtoul(av[0]+1, &end, 0); 482 if (pa6 == NULL) 483 a = (a == 32) ? ~0 : (1 << a) - 1; 484 } else 485 a = strtoul(av[0], &end, 0); 486 if (p32 != NULL) 487 *p32 = a; 488 else if (p16 != NULL) { 489 if (a > 0xFFFF) 490 errx(EX_DATAERR, 491 "port mask must be 16 bit"); 492 *p16 = (uint16_t)a; 493 } else if (p20 != NULL) { 494 if (a > 0xfffff) 495 errx(EX_DATAERR, 496 "flow_id mask must be 20 bit"); 497 *p20 = (uint32_t)a; 498 } else if (pa6 != NULL) { 499 if (a > 128) 500 errx(EX_DATAERR, 501 "in6addr invalid mask len"); 502 else 503 n2mask(pa6, a); 504 } else { 505 if (a > 0xFF) 506 errx(EX_DATAERR, 507 "proto mask must be 8 bit"); 508 p.fs.flow_mask.proto = (uint8_t)a; 509 } 510 if (a != 0) 511 p.fs.flags_fs |= DN_HAVE_FLOW_MASK; 512 ac--; av++; 513 } /* end while, config masks */ 514end_mask: 515 break; 516 517 case TOK_RED: 518 case TOK_GRED: 519 NEED1("red/gred needs w_q/min_th/max_th/max_p\n"); 520 p.fs.flags_fs |= DN_IS_RED; 521 if (tok == TOK_GRED) 522 p.fs.flags_fs |= DN_IS_GENTLE_RED; 523 /* 524 * the format for parameters is w_q/min_th/max_th/max_p 525 */ 526 if ((end = strsep(&av[0], "/"))) { 527 double w_q = strtod(end, NULL); 528 if (w_q > 1 || w_q <= 0) 529 errx(EX_DATAERR, "0 < w_q <= 1"); 530 p.fs.w_q = (int) (w_q * (1 << SCALE_RED)); 531 } 532 if ((end = strsep(&av[0], "/"))) { 533 p.fs.min_th = strtoul(end, &end, 0); 534 if (*end == 'K' || *end == 'k') 535 p.fs.min_th *= 1024; 536 } 537 if ((end = strsep(&av[0], "/"))) { 538 p.fs.max_th = strtoul(end, &end, 0); 539 if (*end == 'K' || *end == 'k') 540 p.fs.max_th *= 1024; 541 } 542 if ((end = strsep(&av[0], "/"))) { 543 double max_p = strtod(end, NULL); 544 if (max_p > 1 || max_p <= 0) 545 errx(EX_DATAERR, "0 < max_p <= 1"); 546 p.fs.max_p = (int)(max_p * (1 << SCALE_RED)); 547 } 548 ac--; av++; 549 break; 550 551 case TOK_DROPTAIL: 552 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED); 553 break; 554 555 case TOK_BW: 556 NEED1("bw needs bandwidth or interface\n"); 557 if (co.do_pipe != 1) 558 errx(EX_DATAERR, "bandwidth only valid for pipes"); 559 /* 560 * set clocking interface or bandwidth value 561 */ 562 if (av[0][0] >= 'a' && av[0][0] <= 'z') { 563 int l = sizeof(p.if_name)-1; 564 /* interface name */ 565 strncpy(p.if_name, av[0], l); 566 p.if_name[l] = '\0'; 567 p.bandwidth = 0; 568 } else { 569 p.if_name[0] = '\0'; 570 p.bandwidth = strtoul(av[0], &end, 0); 571 if (*end == 'K' || *end == 'k') { 572 end++; 573 p.bandwidth *= 1000; 574 } else if (*end == 'M') { 575 end++; 576 p.bandwidth *= 1000000; 577 } 578 if ((*end == 'B' && 579 _substrcmp2(end, "Bi", "Bit/s") != 0) || 580 _substrcmp2(end, "by", "bytes") == 0) 581 p.bandwidth *= 8; 582 if (p.bandwidth < 0) 583 errx(EX_DATAERR, "bandwidth too large"); 584 } 585 ac--; av++; 586 break; 587 588 case TOK_DELAY: 589 if (co.do_pipe != 1) 590 errx(EX_DATAERR, "delay only valid for pipes"); 591 NEED1("delay needs argument 0..10000ms\n"); 592 p.delay = strtoul(av[0], NULL, 0); 593 ac--; av++; 594 break; 595 596 case TOK_WEIGHT: 597 if (co.do_pipe == 1) 598 errx(EX_DATAERR,"weight only valid for queues"); 599 NEED1("weight needs argument 0..100\n"); 600 p.fs.weight = strtoul(av[0], &end, 0); 601 ac--; av++; 602 break; 603 604 case TOK_PIPE: 605 if (co.do_pipe == 1) 606 errx(EX_DATAERR,"pipe only valid for queues"); 607 NEED1("pipe needs pipe_number\n"); 608 p.fs.parent_nr = strtoul(av[0], &end, 0); 609 ac--; av++; 610 break; 611 612 default: 613 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]); 614 } 615 } 616 if (co.do_pipe == 1) { 617 if (p.pipe_nr == 0) 618 errx(EX_DATAERR, "pipe_nr must be > 0"); 619 if (p.delay > 10000) 620 errx(EX_DATAERR, "delay must be < 10000"); 621 } else { /* co.do_pipe == 2, queue */ 622 if (p.fs.parent_nr == 0) 623 errx(EX_DATAERR, "pipe must be > 0"); 624 if (p.fs.weight >100) 625 errx(EX_DATAERR, "weight must be <= 100"); 626 } 627 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) { 628 size_t len; 629 long limit; 630 631 len = sizeof(limit); 632 if (sysctlbyname("net.inet.ip.dummynet.pipe_byte_limit", 633 &limit, &len, NULL, 0) == -1) 634 limit = 1024*1024; 635 if (p.fs.qsize > limit) 636 errx(EX_DATAERR, "queue size must be < %ldB", limit); 637 } else { 638 size_t len; 639 long limit; 640 641 len = sizeof(limit); 642 if (sysctlbyname("net.inet.ip.dummynet.pipe_slot_limit", 643 &limit, &len, NULL, 0) == -1) 644 limit = 100; 645 if (p.fs.qsize > limit) 646 errx(EX_DATAERR, "2 <= queue size <= %ld", limit); 647 } 648 if (p.fs.flags_fs & DN_IS_RED) { 649 size_t len; 650 int lookup_depth, avg_pkt_size; 651 double s, idle, weight, w_q; 652 struct clockinfo ck; 653 int t; 654 655 if (p.fs.min_th >= p.fs.max_th) 656 errx(EX_DATAERR, "min_th %d must be < than max_th %d", 657 p.fs.min_th, p.fs.max_th); 658 if (p.fs.max_th == 0) 659 errx(EX_DATAERR, "max_th must be > 0"); 660 661 len = sizeof(int); 662 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth", 663 &lookup_depth, &len, NULL, 0) == -1) 664 errx(1, "sysctlbyname(\"%s\")", 665 "net.inet.ip.dummynet.red_lookup_depth"); 666 if (lookup_depth == 0) 667 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth" 668 " must be greater than zero"); 669 670 len = sizeof(int); 671 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size", 672 &avg_pkt_size, &len, NULL, 0) == -1) 673 674 errx(1, "sysctlbyname(\"%s\")", 675 "net.inet.ip.dummynet.red_avg_pkt_size"); 676 if (avg_pkt_size == 0) 677 errx(EX_DATAERR, 678 "net.inet.ip.dummynet.red_avg_pkt_size must" 679 " be greater than zero"); 680 681 len = sizeof(struct clockinfo); 682 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1) 683 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate"); 684 685 /* 686 * Ticks needed for sending a medium-sized packet. 687 * Unfortunately, when we are configuring a WF2Q+ queue, we 688 * do not have bandwidth information, because that is stored 689 * in the parent pipe, and also we have multiple queues 690 * competing for it. So we set s=0, which is not very 691 * correct. But on the other hand, why do we want RED with 692 * WF2Q+ ? 693 */ 694 if (p.bandwidth==0) /* this is a WF2Q+ queue */ 695 s = 0; 696 else 697 s = (double)ck.hz * avg_pkt_size * 8 / p.bandwidth; 698 699 /* 700 * max idle time (in ticks) before avg queue size becomes 0. 701 * NOTA: (3/w_q) is approx the value x so that 702 * (1-w_q)^x < 10^-3. 703 */ 704 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED); 705 idle = s * 3. / w_q; 706 p.fs.lookup_step = (int)idle / lookup_depth; 707 if (!p.fs.lookup_step) 708 p.fs.lookup_step = 1; 709 weight = 1 - w_q; 710 for (t = p.fs.lookup_step; t > 1; --t) 711 weight *= 1 - w_q; 712 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED)); 713 } 714 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p); 715 if (i) 716 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE"); 717} 718