blob: a234d2e017080274b272cda29d5941639d36f232 [file] [log] [blame]
Stephen Hemmingera4eca972011-01-13 09:23:17 -08001/*
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 Dumazet650252d2012-01-20 16:19:44 +010021#include <math.h>
Stephen Hemmingera4eca972011-01-13 09:23:17 -080022
23#include "utils.h"
24#include "tc_util.h"
25
26#include "tc_red.h"
27
28static 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
34static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv,
35 struct nlmsghdr *n)
36{
Phil Sutterd17b1362016-07-18 16:48:42 +020037 struct tc_red_qopt opt = {};
Stephen Hemminger32a121c2016-03-21 11:48:36 -070038 unsigned int burst = 0;
39 unsigned int avpkt = 1000;
Stephen Hemmingera4eca972011-01-13 09:23:17 -080040 double probability = 0.02;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070041 unsigned int rate = 0;
Stephen Hemmingera4eca972011-01-13 09:23:17 -080042 int ecn_ok = 0;
43 int wlog;
44 __u8 sbuf[256];
Eric Dumazet650252d2012-01-20 16:19:44 +010045 __u32 max_P;
Stephen Hemmingera4eca972011-01-13 09:23:17 -080046 struct rtattr *tail;
47
Stephen Hemmingera4eca972011-01-13 09:23:17 -080048 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 Hemminger3d0b7432014-12-20 15:47:17 -0800109 /* Compute default min/max thresholds based on
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800110 Sally Floyd's recommendations:
111 http://www.icir.org/floyd/REDparameters.txt
112 */
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800113 if (!opt.qth_max)
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800114 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 Jarosch9f1ba572011-10-03 05:23:18 +0000125 if (opt.qth_min >= opt.qth_max) {
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800126 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 Dumazetab15aea2011-12-01 15:25:59 +0100136 fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst);
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800137 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 Dumazet650252d2012-01-20 16:19:44 +0100159 max_P = probability * pow(2, 32);
160 addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P));
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800161 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
162 return 0;
163}
164
165static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
166{
Eric Dumazet650252d2012-01-20 16:19:44 +0100167 struct rtattr *tb[TCA_CHOKE_MAX+1];
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800168 const struct tc_red_qopt *qopt;
Eric Dumazet650252d2012-01-20 16:19:44 +0100169 __u32 max_P = 0;
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800170
171 if (opt == NULL)
172 return 0;
173
Eric Dumazet650252d2012-01-20 16:19:44 +0100174 parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt);
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800175
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 Dumazet650252d2012-01-20 16:19:44 +0100181 if (tb[TCA_CHOKE_MAX_P] &&
182 RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32))
Stephen Hemmingerff247462012-04-10 08:47:55 -0700183 max_P = rta_getattr_u32(tb[TCA_CHOKE_MAX_P]);
Stephen Hemmingera4eca972011-01-13 09:23:17 -0800184
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 Dumazet650252d2012-01-20 16:19:44 +0100192 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 Hemmingera4eca972011-01-13 09:23:17 -0800198 }
199 return 0;
200}
201
202static 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
220struct 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};