Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * q_choke.c CHOKE. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Stephen Hemminger <shemminger@vyatta.com> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> |
| 20 | #include <string.h> |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 21 | #include <math.h> |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 22 | |
| 23 | #include "utils.h" |
| 24 | #include "tc_util.h" |
| 25 | |
| 26 | #include "tc_red.h" |
| 27 | |
| 28 | static void explain(void) |
| 29 | { |
| 30 | fprintf(stderr, "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n"); |
| 31 | fprintf(stderr, " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n"); |
| 32 | } |
| 33 | |
| 34 | static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv, |
| 35 | struct nlmsghdr *n) |
| 36 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 37 | struct tc_red_qopt opt = {}; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 38 | unsigned int burst = 0; |
| 39 | unsigned int avpkt = 1000; |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 40 | double probability = 0.02; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 41 | unsigned int rate = 0; |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 42 | int ecn_ok = 0; |
| 43 | int wlog; |
| 44 | __u8 sbuf[256]; |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 45 | __u32 max_P; |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 46 | struct rtattr *tail; |
| 47 | |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 48 | while (argc > 0) { |
| 49 | if (strcmp(*argv, "limit") == 0) { |
| 50 | NEXT_ARG(); |
| 51 | if (get_unsigned(&opt.limit, *argv, 0)) { |
| 52 | fprintf(stderr, "Illegal \"limit\"\n"); |
| 53 | return -1; |
| 54 | } |
| 55 | } else if (strcmp(*argv, "bandwidth") == 0) { |
| 56 | NEXT_ARG(); |
| 57 | if (get_rate(&rate, *argv)) { |
| 58 | fprintf(stderr, "Illegal \"bandwidth\"\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | } else if (strcmp(*argv, "ecn") == 0) { |
| 62 | ecn_ok = 1; |
| 63 | } else if (strcmp(*argv, "min") == 0) { |
| 64 | NEXT_ARG(); |
| 65 | if (get_unsigned(&opt.qth_min, *argv, 0)) { |
| 66 | fprintf(stderr, "Illegal \"min\"\n"); |
| 67 | return -1; |
| 68 | } |
| 69 | } else if (strcmp(*argv, "max") == 0) { |
| 70 | NEXT_ARG(); |
| 71 | if (get_unsigned(&opt.qth_max, *argv, 0)) { |
| 72 | fprintf(stderr, "Illegal \"max\"\n"); |
| 73 | return -1; |
| 74 | } |
| 75 | } else if (strcmp(*argv, "burst") == 0) { |
| 76 | NEXT_ARG(); |
| 77 | if (get_unsigned(&burst, *argv, 0)) { |
| 78 | fprintf(stderr, "Illegal \"burst\"\n"); |
| 79 | return -1; |
| 80 | } |
| 81 | } else if (strcmp(*argv, "avpkt") == 0) { |
| 82 | NEXT_ARG(); |
| 83 | if (get_size(&avpkt, *argv)) { |
| 84 | fprintf(stderr, "Illegal \"avpkt\"\n"); |
| 85 | return -1; |
| 86 | } |
| 87 | } else if (strcmp(*argv, "probability") == 0) { |
| 88 | NEXT_ARG(); |
| 89 | if (sscanf(*argv, "%lg", &probability) != 1) { |
| 90 | fprintf(stderr, "Illegal \"probability\"\n"); |
| 91 | return -1; |
| 92 | } |
| 93 | } else if (strcmp(*argv, "help") == 0) { |
| 94 | explain(); |
| 95 | return -1; |
| 96 | } else { |
| 97 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 98 | explain(); |
| 99 | return -1; |
| 100 | } |
| 101 | argc--; argv++; |
| 102 | } |
| 103 | |
| 104 | if (!rate || !opt.limit) { |
| 105 | fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 109 | /* Compute default min/max thresholds based on |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 110 | Sally Floyd's recommendations: |
| 111 | http://www.icir.org/floyd/REDparameters.txt |
| 112 | */ |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 113 | if (!opt.qth_max) |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 114 | opt.qth_max = opt.limit / 4; |
| 115 | if (!opt.qth_min) |
| 116 | opt.qth_min = opt.qth_max / 3; |
| 117 | if (!burst) |
| 118 | burst = (2 * opt.qth_min + opt.qth_max) / 3; |
| 119 | |
| 120 | if (opt.qth_max > opt.limit) { |
| 121 | fprintf(stderr, "\"max\" is larger than \"limit\"\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
Thomas Jarosch | 9f1ba57 | 2011-10-03 05:23:18 +0000 | [diff] [blame] | 125 | if (opt.qth_min >= opt.qth_max) { |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 126 | fprintf(stderr, "\"min\" is not smaller than \"max\"\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt); |
| 131 | if (wlog < 0) { |
| 132 | fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n"); |
| 133 | return -1; |
| 134 | } |
| 135 | if (wlog >= 10) |
Eric Dumazet | ab15aea | 2011-12-01 15:25:59 +0100 | [diff] [blame] | 136 | fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst); |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 137 | opt.Wlog = wlog; |
| 138 | |
| 139 | wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability); |
| 140 | if (wlog < 0) { |
| 141 | fprintf(stderr, "CHOKE: failed to calculate probability.\n"); |
| 142 | return -1; |
| 143 | } |
| 144 | opt.Plog = wlog; |
| 145 | |
| 146 | wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf); |
| 147 | if (wlog < 0) { |
| 148 | fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | opt.Scell_log = wlog; |
| 152 | if (ecn_ok) |
| 153 | opt.flags |= TC_RED_ECN; |
| 154 | |
| 155 | tail = NLMSG_TAIL(n); |
| 156 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
| 157 | addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt)); |
| 158 | addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256); |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 159 | max_P = probability * pow(2, 32); |
| 160 | addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P)); |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 161 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 166 | { |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 167 | struct rtattr *tb[TCA_CHOKE_MAX+1]; |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 168 | const struct tc_red_qopt *qopt; |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 169 | __u32 max_P = 0; |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 170 | |
| 171 | if (opt == NULL) |
| 172 | return 0; |
| 173 | |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 174 | parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt); |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 175 | |
| 176 | if (tb[TCA_CHOKE_PARMS] == NULL) |
| 177 | return -1; |
| 178 | qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]); |
| 179 | if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS]) < sizeof(*qopt)) |
| 180 | return -1; |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 181 | if (tb[TCA_CHOKE_MAX_P] && |
| 182 | RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32)) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 183 | max_P = rta_getattr_u32(tb[TCA_CHOKE_MAX_P]); |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 184 | |
| 185 | fprintf(f, "limit %up min %up max %up ", |
| 186 | qopt->limit, qopt->qth_min, qopt->qth_max); |
| 187 | |
| 188 | if (qopt->flags & TC_RED_ECN) |
| 189 | fprintf(f, "ecn "); |
| 190 | |
| 191 | if (show_details) { |
Eric Dumazet | 650252d | 2012-01-20 16:19:44 +0100 | [diff] [blame] | 192 | fprintf(f, "ewma %u ", qopt->Wlog); |
| 193 | if (max_P) |
| 194 | fprintf(f, "probability %g ", max_P / pow(2, 32)); |
| 195 | else |
| 196 | fprintf(f, "Plog %u ", qopt->Plog); |
| 197 | fprintf(f, "Scell_log %u", qopt->Scell_log); |
Stephen Hemminger | a4eca97 | 2011-01-13 09:23:17 -0800 | [diff] [blame] | 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int choke_print_xstats(struct qdisc_util *qu, FILE *f, |
| 203 | struct rtattr *xstats) |
| 204 | { |
| 205 | struct tc_choke_xstats *st; |
| 206 | |
| 207 | if (xstats == NULL) |
| 208 | return 0; |
| 209 | |
| 210 | if (RTA_PAYLOAD(xstats) < sizeof(*st)) |
| 211 | return -1; |
| 212 | |
| 213 | st = RTA_DATA(xstats); |
| 214 | fprintf(f, " marked %u early %u pdrop %u other %u matched %u", |
| 215 | st->marked, st->early, st->pdrop, st->other, st->matched); |
| 216 | return 0; |
| 217 | |
| 218 | } |
| 219 | |
| 220 | struct qdisc_util choke_qdisc_util = { |
| 221 | .id = "choke", |
| 222 | .parse_qopt = choke_parse_opt, |
| 223 | .print_qopt = choke_print_opt, |
| 224 | .print_xstats = choke_print_xstats, |
| 225 | }; |