-
Notifications
You must be signed in to change notification settings - Fork 82
/
probetxrate.hh
318 lines (250 loc) · 6.56 KB
/
probetxrate.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#ifndef CLICK_PROBETXRATE_HH
#define CLICK_PROBETXRATE_HH
#include <click/element.hh>
#include <click/deque.hh>
#include <click/etheraddress.hh>
#include <click/bighashmap.hh>
#include <click/glue.hh>
#include <clicknet/wifi.h>
#include <elements/wifi/bitrate.hh>
CLICK_DECLS
class AvailableRates;
/*
=c
ProbeTXRate([I<KEYWORDS>])
=s Wifi
Madwifi wireless bit-rate selection algorithm
=d
Probematically determine the txrate for a give ethernet dst
=over 8
=item RATE_WINDOW
How long to remember tx packets
=item STEPUP
a value from 0 to 100 of what the percentage must be before
the rate is increased
=item STEPDOWN
a value from 0 to 100. when the percentage of successful packets
falls below this value, the card will try the next lowest rate
=item BEFORE_SWITCH
how many packets must be received before you calculate the rate
for a give host. i.e. if you set this to 4, each rate will try
at least 4 packets before switching up or down.
This element should be used in conjunction with SetTXRate
and a wifi-enabled kernel module. (like hostap or airo).
=back 8
=a SetTXRate, WifiTXFeedback
*/
class ProbeTXRate : public Element { public:
ProbeTXRate() CLICK_COLD;
~ProbeTXRate() CLICK_COLD;
const char *class_name() const override { return "ProbeTXRate"; }
const char *port_count() const override { return "2/0-2"; }
const char *processing() const override { return "ah/a"; }
const char *flow_code() const override { return "#/#"; }
int configure(Vector<String> &, ErrorHandler *) CLICK_COLD;
bool can_live_reconfigure() const { return true; }
void push (int, Packet *);
Packet *pull(int);
void process_feedback(Packet *);
void assign_rate(Packet *);
void add_handlers() CLICK_COLD;
bool _debug;
unsigned _offset;
unsigned _packet_size_threshold;
String print_rates();
struct tx_result {
Timestamp _when;
int _rate;
bool _success;
int _time;
int _tries;
tx_result(const Timestamp &t, int rate,
bool success, int time, int tries) :
_when(t),
_rate(rate),
_success(success),
_time(time),
_tries(tries)
{ }
tx_result() {}
};
struct DstInfo {
public:
EtherAddress _eth;
Deque<tx_result> _results;
Vector<int> _rates;
Vector<int> _packets;
Vector<int> _total_time;
Vector<int> _total_success;
Vector<int> _total_fail;
Vector<int> _perfect_time;
Vector<int> _total_tries;
unsigned _count;
DstInfo() {
}
DstInfo(EtherAddress eth, Vector<int> rates) {
_eth = eth;
_rates = rates;
_packets = Vector<int>(_rates.size(), 0);
_total_time = Vector<int>(_rates.size(), 0);
_total_success = Vector<int>(_rates.size(), 0);
_total_fail = Vector<int>(_rates.size(), 0);
_total_tries = Vector<int>(_rates.size(), 0);
_perfect_time = Vector<int>(_rates.size(), 0);
_count = 0;
for (int x = 0; x < _rates.size(); x++) {
_perfect_time[x] = calc_usecs_wifi_packet(1500, _rates[x], 0);
}
}
int rate_index(int rate) {
int ndx = 0;
for (int x = 0; x < _rates.size(); x++) {
if (rate == _rates[x]) {
ndx = x;
break;
}
}
return (ndx == _rates.size()) ? -1 : ndx;
}
void check () {
for (int x = 0; x < _rates.size(); x++) {
int sum_success = 0;
int sum_fail = 0;
int sum_time = 0;
for (int y = 0; y < _results.size(); y++) {
if (_results[y]._rate != _rates[x]) {
continue;
}
if (_results[y]._success) {
sum_success++;
} else {
sum_fail++;
}
sum_time += _results[y]._time;
}
if (sum_success != _total_success[x]) {
click_chatter("rate %d mismatch success %d %d\n",
_rates[x],
sum_success, _total_success[x]);
}
if (sum_fail != _total_fail[x]) {
click_chatter("rate %d mismatch fail %d %d\n",
_rates[x],
sum_fail, _total_fail[x]);
}
if (sum_time != _total_time[x]) {
click_chatter("rate %d mismatch time %d %d\n",
_rates[x],
sum_time, _total_time[x]);
}
}
}
void add_result(const Timestamp &now, int rate, int tries, int success,
int time) {
int ndx = rate_index(rate);
if (!rate || ndx < 0 || ndx > _rates.size()){
return;
}
_total_time[ndx] += time;
_total_tries[ndx] += tries;
_packets[ndx]++;
if (success) {
_total_success[ndx]++;
} else {
_total_fail[ndx]++;
}
_results.push_back(tx_result(now, rate,
success, time, tries));
}
void trim(const Timestamp &ts) {
while (_results.size() && _results[0]._when < ts) {
tx_result t = _results[0];
_results.pop_front();
int ndx = rate_index(t._rate);
if (ndx < 0 || ndx >= _rates.size()) {
click_chatter("%s: ???",
__func__);
continue;
}
if (t._success) {
_total_success[ndx]--;
} else {
_total_fail[ndx]--;
}
_packets[ndx]--;
_total_time[ndx] -= t._time;
_total_tries[ndx] -= t._tries;
}
}
int average(int ndx) {
if (!_total_success[ndx]) {
return 0;
}
return _total_time[ndx] / _total_success[ndx];
}
int best_rate_ndx() {
int best_ndx = -1;
int best_time = 0;
bool found = false;
if (!_rates.size()) {
return -1;
}
for (int x = 0; x < _rates.size(); x++) {
if (_total_success[x]) {
int time = _total_time[x] / _total_success[x];
if (!found || time < best_time) {
best_ndx = x;
best_time = time;
found = true;
}
}
}
if (found) {
return best_ndx;
}
for (int x = _rates.size()-1; x > 0; x--) {
if (_total_fail[x] < 3) {
return x;
}
}
return 0;
}
Vector<int> pick_rate() {
int best_ndx = best_rate_ndx();
if (_rates.size() == 0) {
click_chatter("no rates to pick from for %s\n",
_eth.unparse().c_str());
return _rates;
}
if (best_ndx < 0) {
/* no rate has had a successful packet yet */
return _rates;
}
int best_time = average(best_ndx);
Vector<int> possible_rates;
for (int x = 0; x < _rates.size(); x++) {
if (best_time < _perfect_time[x]) {
/* couldn't possibly be better */
continue;
}
if (_total_fail[x] > 3 && !_total_success[x]) {
continue;
}
possible_rates.push_back(_rates[x]);
}
return possible_rates;
}
};
typedef HashMap<EtherAddress, DstInfo> NeighborTable;
typedef NeighborTable::const_iterator NIter;
NeighborTable _neighbors;
EtherAddress _bcast;
int _rate_window_ms;
Timestamp _rate_window;
AvailableRates *_rtable;
bool _active;
unsigned _original_retries;
unsigned _min_sample;
};
CLICK_ENDDECLS
#endif