~ubuntu-branches/ubuntu/raring/iproute/raring-proposed

« back to all changes in this revision

Viewing changes to tc/q_sfq.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-05-22 14:10:07 UTC
  • mfrom: (1.1.15) (23.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120522141007-uuwicj2cx2x71vfb
Tags: 20120319-1ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Mark iproute as Multi-Arch: foreign.
  - Add cross-building support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include <netinet/in.h>
20
20
#include <arpa/inet.h>
21
21
#include <string.h>
 
22
#include <math.h>
22
23
 
23
24
#include "utils.h"
24
25
#include "tc_util.h"
 
26
#include "tc_red.h"
25
27
 
26
28
static void explain(void)
27
29
{
28
30
        fprintf(stderr, "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n");
29
 
        fprintf(stderr, "               [ divisor NUMBER ]\n");
 
31
        fprintf(stderr, "               [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n");
 
32
        fprintf(stderr, "               [ headdrop ]\n");
 
33
        fprintf(stderr, "               [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n");
 
34
        fprintf(stderr, "               [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n");
 
35
        fprintf(stderr, "               [ ecn ] [ harddrop ]\n");
30
36
}
31
37
 
32
38
static int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
33
39
{
34
 
        int ok=0;
35
 
        struct tc_sfq_qopt opt;
 
40
        int ok = 0, red = 0;
 
41
        struct tc_sfq_qopt_v1 opt;
 
42
        unsigned int burst = 0;
 
43
        int wlog;
 
44
        unsigned int avpkt = 1000;
 
45
        double probability = 0.02;
36
46
 
37
47
        memset(&opt, 0, sizeof(opt));
38
48
 
39
49
        while (argc > 0) {
40
50
                if (strcmp(*argv, "quantum") == 0) {
41
51
                        NEXT_ARG();
42
 
                        if (get_size(&opt.quantum, *argv)) {
 
52
                        if (get_size(&opt.v0.quantum, *argv)) {
43
53
                                fprintf(stderr, "Illegal \"limit\"\n");
44
54
                                return -1;
45
55
                        }
46
56
                        ok++;
47
57
                } else if (strcmp(*argv, "perturb") == 0) {
48
58
                        NEXT_ARG();
49
 
                        if (get_integer(&opt.perturb_period, *argv, 0)) {
 
59
                        if (get_integer(&opt.v0.perturb_period, *argv, 0)) {
50
60
                                fprintf(stderr, "Illegal \"perturb\"\n");
51
61
                                return -1;
52
62
                        }
53
63
                        ok++;
54
64
                } else if (strcmp(*argv, "limit") == 0) {
55
65
                        NEXT_ARG();
56
 
                        if (get_u32(&opt.limit, *argv, 0)) {
 
66
                        if (get_u32(&opt.v0.limit, *argv, 0)) {
57
67
                                fprintf(stderr, "Illegal \"limit\"\n");
58
68
                                return -1;
59
69
                        }
60
 
                        if (opt.limit < 2) {
 
70
                        if (opt.v0.limit < 2) {
61
71
                                fprintf(stderr, "Illegal \"limit\", must be > 1\n");
62
72
                                return -1;
63
73
                        }
64
74
                        ok++;
65
75
                } else if (strcmp(*argv, "divisor") == 0) {
66
76
                        NEXT_ARG();
67
 
                        if (get_u32(&opt.divisor, *argv, 0)) {
 
77
                        if (get_u32(&opt.v0.divisor, *argv, 0)) {
68
78
                                fprintf(stderr, "Illegal \"divisor\"\n");
69
79
                                return -1;
70
80
                        }
71
81
                        ok++;
 
82
                } else if (strcmp(*argv, "flows") == 0) {
 
83
                        NEXT_ARG();
 
84
                        if (get_u32(&opt.v0.flows, *argv, 0)) {
 
85
                                fprintf(stderr, "Illegal \"flows\"\n");
 
86
                                return -1;
 
87
                        }
 
88
                        ok++;
 
89
                } else if (strcmp(*argv, "depth") == 0) {
 
90
                        NEXT_ARG();
 
91
                        if (get_u32(&opt.depth, *argv, 0)) {
 
92
                                fprintf(stderr, "Illegal \"flows\"\n");
 
93
                                return -1;
 
94
                        }
 
95
                        ok++;
 
96
                } else if (strcmp(*argv, "headdrop") == 0) {
 
97
                        opt.headdrop = 1;
 
98
                        ok++;
 
99
                } else if (strcmp(*argv, "redflowlimit") == 0) {
 
100
                        NEXT_ARG();
 
101
                        if (get_u32(&opt.limit, *argv, 0)) {
 
102
                                fprintf(stderr, "Illegal \"redflowlimit\"\n");
 
103
                                return -1;
 
104
                        }
 
105
                        red++;
 
106
                } else if (strcmp(*argv, "min") == 0) {
 
107
                        NEXT_ARG();
 
108
                        if (get_u32(&opt.qth_min, *argv, 0)) {
 
109
                                fprintf(stderr, "Illegal \"min\"\n");
 
110
                                return -1;
 
111
                        }
 
112
                        red++;
 
113
                } else if (strcmp(*argv, "max") == 0) {
 
114
                        NEXT_ARG();
 
115
                        if (get_u32(&opt.qth_max, *argv, 0)) {
 
116
                                fprintf(stderr, "Illegal \"max\"\n");
 
117
                                return -1;
 
118
                        }
 
119
                        red++;
 
120
                } else if (strcmp(*argv, "burst") == 0) {
 
121
                        NEXT_ARG();
 
122
                        if (get_unsigned(&burst, *argv, 0)) {
 
123
                                fprintf(stderr, "Illegal \"burst\"\n");
 
124
                                return -1;
 
125
                        }
 
126
                        red++;
 
127
                } else if (strcmp(*argv, "avpkt") == 0) {
 
128
                        NEXT_ARG();
 
129
                        if (get_size(&avpkt, *argv)) {
 
130
                                fprintf(stderr, "Illegal \"avpkt\"\n");
 
131
                                return -1;
 
132
                        }
 
133
                        red++;
 
134
                } else if (strcmp(*argv, "probability") == 0) {
 
135
                        NEXT_ARG();
 
136
                        if (sscanf(*argv, "%lg", &probability) != 1) {
 
137
                                fprintf(stderr, "Illegal \"probability\"\n");
 
138
                                return -1;
 
139
                        }
 
140
                        red++;
 
141
                } else if (strcmp(*argv, "ecn") == 0) {
 
142
                        opt.flags |= TC_RED_ECN;
 
143
                        red++;
 
144
                } else if (strcmp(*argv, "harddrop") == 0) {
 
145
                        opt.flags |= TC_RED_HARDDROP;
 
146
                        red++;
72
147
                } else if (strcmp(*argv, "help") == 0) {
73
148
                        explain();
74
149
                        return -1;
79
154
                }
80
155
                argc--; argv++;
81
156
        }
82
 
 
83
 
        if (ok)
 
157
        if (red) {
 
158
                if (!opt.limit) {
 
159
                        fprintf(stderr, "Required parameter (redflowlimit) is missing\n");
 
160
                        return -1;
 
161
                }
 
162
                /* Compute default min/max thresholds based on 
 
163
                   Sally Floyd's recommendations:
 
164
                   http://www.icir.org/floyd/REDparameters.txt
 
165
                */
 
166
                if (!opt.qth_max) 
 
167
                        opt.qth_max = opt.limit / 4;
 
168
                if (!opt.qth_min)
 
169
                        opt.qth_min = opt.qth_max / 3;
 
170
                if (!burst)
 
171
                        burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
 
172
 
 
173
                if (opt.qth_max > opt.limit) {
 
174
                        fprintf(stderr, "\"max\" is larger than \"limit\"\n");
 
175
                        return -1;
 
176
                }
 
177
 
 
178
                if (opt.qth_min >= opt.qth_max) {
 
179
                        fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
 
180
                        return -1;
 
181
                }
 
182
 
 
183
                wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt);
 
184
                if (wlog < 0) {
 
185
                        fprintf(stderr, "SFQ: failed to calculate EWMA constant.\n");
 
186
                        return -1;
 
187
                }
 
188
                if (wlog >= 10)
 
189
                        fprintf(stderr, "SFQ: WARNING. Burst %u seems to be too large.\n", burst);
 
190
                opt.Wlog = wlog;
 
191
 
 
192
                wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability);
 
193
                if (wlog < 0) {
 
194
                        fprintf(stderr, "SFQ: failed to calculate probability.\n");
 
195
                        return -1;
 
196
                }
 
197
                opt.Plog = wlog;
 
198
                opt.max_P = probability * pow(2, 32);
 
199
        }
 
200
 
 
201
        if (ok || red)
84
202
                addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
85
203
        return 0;
86
204
}
88
206
static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
89
207
{
90
208
        struct tc_sfq_qopt *qopt;
 
209
        struct tc_sfq_qopt_v1 *qopt_ext = NULL;
91
210
        SPRINT_BUF(b1);
92
 
 
 
211
        SPRINT_BUF(b2);
 
212
        SPRINT_BUF(b3);
93
213
        if (opt == NULL)
94
214
                return 0;
95
215
 
96
216
        if (RTA_PAYLOAD(opt)  < sizeof(*qopt))
97
217
                return -1;
 
218
        if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext))
 
219
                qopt_ext = RTA_DATA(opt);
98
220
        qopt = RTA_DATA(opt);
99
221
        fprintf(f, "limit %up ", qopt->limit);
100
222
        fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1));
 
223
        if (qopt_ext && qopt_ext->depth)
 
224
                fprintf(f, "depth %u ", qopt_ext->depth);
 
225
        if (qopt_ext && qopt_ext->headdrop)
 
226
                fprintf(f, "headdrop ");
 
227
 
101
228
        if (show_details) {
102
229
                fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor);
103
230
        }
104
231
        fprintf(f, "divisor %u ", qopt->divisor);
105
232
        if (qopt->perturb_period)
106
233
                fprintf(f, "perturb %dsec ", qopt->perturb_period);
 
234
        if (qopt_ext && qopt_ext->qth_min) {
 
235
                fprintf(f, "\n ewma %u ", qopt_ext->Wlog);
 
236
                fprintf(f, "min %s max %s probability %g ",
 
237
                        sprint_size(qopt_ext->qth_min, b2),
 
238
                        sprint_size(qopt_ext->qth_max, b3),
 
239
                        qopt_ext->max_P / pow(2, 32));
 
240
                if (qopt_ext->flags & TC_RED_ECN)
 
241
                        fprintf(f, "ecn ");
 
242
                if (show_stats) {
 
243
                        fprintf(f, "\n prob_mark %u prob_mark_head %u prob_drop %u",
 
244
                                qopt_ext->stats.prob_mark,
 
245
                                qopt_ext->stats.prob_mark_head,
 
246
                                qopt_ext->stats.prob_drop);
 
247
                        fprintf(f, "\n forced_mark %u forced_mark_head %u forced_drop %u",
 
248
                                qopt_ext->stats.forced_mark,
 
249
                                qopt_ext->stats.forced_mark_head,
 
250
                                qopt_ext->stats.forced_drop);
 
251
                }
 
252
        }
107
253
        return 0;
108
254
}
109
255