forked from blocktrron/usteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.c
533 lines (434 loc) · 13.5 KB
/
policy.c
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2020 embedd.ch
* Copyright (C) 2020 Felix Fietkau <[email protected]>
* Copyright (C) 2020 John Crispin <[email protected]>
*/
#include "usteer.h"
#include "node.h"
#include "hearing_map.h"
static bool
below_assoc_threshold(struct usteer_node *node_cur, struct usteer_node *node_new, struct sta_info *si)
{
int n_assoc_cur = node_cur->n_assoc;
int n_assoc_new = node_new->n_assoc;
bool ref_5g = node_cur->freq > 4000;
bool node_5g = node_new->freq > 4000;
if (ref_5g && !node_5g)
n_assoc_new += config.band_steering_threshold;
else if (!ref_5g && node_5g)
n_assoc_cur += config.band_steering_threshold;
n_assoc_new += config.load_balancing_threshold;
if (n_assoc_new > n_assoc_cur) {
MSG_T_STA("band_steering_threshold,load_balancing_threshold",
si->sta->addr, "exeeded (bs=%u, lb=%u)\n",
config.band_steering_threshold,
config.load_balancing_threshold);
}
return n_assoc_new <= n_assoc_cur;
}
static bool
better_signal_strength(struct sta_info *si_cur, struct sta_info *si_new)
{
const bool is_better = si_new->signal - si_cur->signal
> (int) config.signal_diff_threshold;
if (!config.signal_diff_threshold)
return false;
if (is_better) {
MSG_T_STA("signal_diff_threshold", si_cur->sta->addr,
"exceeded (config=%i) (real=%i)\n",
config.signal_diff_threshold,
si_new->signal - si_cur->signal);
}
return is_better;
}
static bool
better_signal_strength_hearing_map(struct beacon_report *br_cur, struct beacon_report *br_new)
{
uint16_t rcpi_threshold = (config.signal_diff_threshold / 100) * 255;
const bool is_better = br_new->rcpi - br_cur->rcpi
> rcpi_threshold;
if (!config.signal_diff_threshold)
return false;
if (is_better) {
MSG_T_STA("rcpi_diff_threshold", br_cur->address->sta->addr,
"exceeded (config=%i) (real=%i)\n",
rcpi_threshold,
br_new->rcpi - br_cur->rcpi);
}
return is_better;
}
static bool
below_load_threshold(struct usteer_node *node)
{
return node->n_assoc >= config.load_kick_min_clients &&
node->load > config.load_kick_threshold;
}
static bool
has_better_load(struct usteer_node *node_cur, struct usteer_node *node_new)
{
return !below_load_threshold(node_cur) && below_load_threshold(node_new);
}
static bool
below_max_assoc(struct usteer_node *node)
{
return !node->max_assoc || node->n_assoc < node->max_assoc;
}
static bool
is_better_candidate(struct sta_info *si_cur, struct sta_info *si_new)
{
if (!below_max_assoc(si_new->node))
return false;
return below_assoc_threshold(si_cur->node, si_new->node, si_cur) ||
better_signal_strength(si_cur, si_new) ||
has_better_load(si_cur->node, si_new->node);
}
static bool
is_better_candidate_hearing_map(struct beacon_report *br_cur, struct beacon_report *br_new)
{
struct usteer_node *node_new = get_usteer_node_from_bssid(br_new->bssid);
struct usteer_node *node_cur = get_usteer_node_from_bssid(br_cur->bssid);
if (!below_max_assoc(node_new))
return false;
return below_assoc_threshold(node_cur, node_new, br_cur->address) ||
better_signal_strength_hearing_map(br_cur, br_new) ||
has_better_load(node_cur, node_new);
}
static struct sta_info *
find_better_candidate(struct sta_info *si_ref)
{
struct sta_info *si;
struct sta *sta = si_ref->sta;
struct beacon_report *br;
struct beacon_report *br_cur = NULL;
list_for_each_entry(br, &si_ref->beacon_reports, sta_list) {
struct usteer_node *node = get_usteer_node_from_bssid(br->bssid);
if (!node)
continue;
if (node == si_ref->node) {
br_cur = br;
break;
}
}
list_for_each_entry(br, &si_ref->beacon_reports, sta_list) {
if(!br_cur)
break;
struct usteer_node *node = get_usteer_node_from_bssid(br->bssid);
if (!node)
continue;
if (node == si_ref->node)
continue;
if (strcmp(node->ssid, si_ref->node->ssid) != 0)
continue;
bool create;
if (is_better_candidate_hearing_map(br_cur, br) &&
!is_better_candidate_hearing_map(br, br_cur))
return usteer_sta_info_get(br->address->sta, node, &create);
}
list_for_each_entry(si, &sta->nodes, list) {
if (si == si_ref)
continue;
if (current_time - si->seen > config.seen_policy_timeout) {
MSG_T_STA("seen_policy_timeout", si->sta->addr,
"timeout exceeded (%u)\n", config.seen_policy_timeout);
continue;
}
if (strcmp(si->node->ssid, si_ref->node->ssid) != 0)
continue;
if (is_better_candidate(si_ref, si) &&
!is_better_candidate(si, si_ref))
return si;
}
return NULL;
}
static int
snr_to_signal(struct usteer_node *node, int snr)
{
int noise = -95;
if (snr < 0)
return snr;
if (node->noise)
noise = node->noise;
return noise + snr;
}
bool
usteer_check_request(struct sta_info *si, enum usteer_event_type type)
{
struct sta_info *si_new;
int min_signal;
if (type == EVENT_TYPE_ASSOC)
return true;
if (si->stats[type].blocked_cur >= config.max_retry_band) {
MSG_T_STA("max_retry_band", si->sta->addr,
"max retry (%u) exceeded\n", config.max_retry_band);
return true;
}
min_signal = snr_to_signal(si->node, config.min_connect_snr);
if (si->signal < min_signal) {
if (type != EVENT_TYPE_PROBE || config.debug_level >= MSG_DEBUG)
MSG(VERBOSE, "Ignoring %s request from "MAC_ADDR_FMT" due to low signal (%d < %d)\n",
event_types[type], MAC_ADDR_DATA(si->sta->addr),
si->signal, min_signal);
MSG_T_STA("min_connect_snr", si->sta->addr,
"snr to low (config=%i) (real=%i)\n",
min_signal, si->signal);
return false;
}
if (current_time - si->created < config.initial_connect_delay) {
if (type != EVENT_TYPE_PROBE || config.debug_level >= MSG_DEBUG)
MSG(VERBOSE, "Ignoring %s request from "MAC_ADDR_FMT" during initial connect delay\n",
event_types[type], MAC_ADDR_DATA(si->sta->addr));
MSG_T_STA("initial_connect_delay", si->sta->addr,
"is below delay (%u)\n", config.initial_connect_delay);
return false;
}
si_new = find_better_candidate(si);
if (!si_new)
return true;
if (type != EVENT_TYPE_PROBE || config.debug_level >= MSG_DEBUG)
MSG(VERBOSE, "Ignoring %s request from "MAC_ADDR_FMT", "
"node (local/remote): %s/%s, "
"signal=%d/%d, n_assoc=%d/%d\n", event_types[type],
MAC_ADDR_DATA(si->sta->addr),
usteer_node_name(si->node), usteer_node_name(si_new->node),
si->signal, si_new->signal,
si->node->n_assoc, si_new->node->n_assoc);
return false;
}
uint64_t
usteer_get_client_active_bits(struct sta_info *si)
{
struct sta_active_bytes active_bytes = si->active_bytes;
uint64_t rx_delta;
uint64_t tx_delta;
rx_delta = active_bytes.data[1][0] - active_bytes.data[0][0];
tx_delta = active_bytes.data[1][1] - active_bytes.data[0][1];
return ((rx_delta + tx_delta) / config.kick_client_active_sec) * 8;
}
static bool
is_active_client(struct sta_info *si)
{
uint64_t client_active_ratio = usteer_get_client_active_bits(si);
if (client_active_ratio >= config.kick_client_active_bits) {
MSG_T("load_kick_active",
"client "MAC_ADDR_FMT" is still active (config=%llu) (real=%llu)",
MAC_ADDR_DATA(si->sta->addr), config.kick_client_active_bits, client_active_ratio);
return true;
}
MSG_T("load_kick_active",
"client "MAC_ADDR_FMT" is inactive (config=%llu) (real=%llu)",
MAC_ADDR_DATA(si->sta->addr), config.kick_client_active_bits, client_active_ratio);
return false;
}
static bool
is_more_kickable(struct sta_info *si_cur, struct sta_info *si_new)
{
if (!si_cur)
return true;
if (si_new->kick_count > si_cur->kick_count)
return false;
return si_cur->signal > si_new->signal;
}
static void
usteer_roam_set_state(struct sta_info *si, enum roam_trigger_state state)
{
static const char * const state_names[] = {
#define _S(n) [ROAM_TRIGGER_##n] = #n,
__roam_trigger_states
#undef _S
};
si->roam_event = current_time;
if (si->roam_state == state) {
if (si->roam_state == ROAM_TRIGGER_IDLE) {
si->roam_tries = 0;
return;
}
si->roam_tries++;
} else {
si->roam_tries = 0;
}
si->roam_state = state;
MSG(VERBOSE, "Roam trigger SM for client "MAC_ADDR_FMT": state=%s, tries=%d, signal=%d\n",
MAC_ADDR_DATA(si->sta->addr), state_names[state], si->roam_tries, si->signal);
}
static bool
usteer_roam_trigger_sm(struct sta_info *si)
{
struct sta_info *si_new;
int min_signal;
min_signal = snr_to_signal(si->node, config.roam_trigger_snr);
switch (si->roam_state) {
case ROAM_TRIGGER_SCAN:
if (current_time - si->roam_event < config.roam_scan_interval)
break;
if (find_better_candidate(si) ||
si->roam_scan_done > si->roam_event) {
usteer_roam_set_state(si, ROAM_TRIGGER_SCAN_DONE);
break;
}
if (config.roam_scan_tries &&
si->roam_tries >= config.roam_scan_tries) {
usteer_roam_set_state(si, ROAM_TRIGGER_WAIT_KICK);
break;
}
usteer_ubus_trigger_client_scan(si);
usteer_roam_set_state(si, ROAM_TRIGGER_SCAN);
break;
case ROAM_TRIGGER_IDLE:
if (find_better_candidate(si)) {
usteer_roam_set_state(si, ROAM_TRIGGER_SCAN_DONE);
break;
}
usteer_roam_set_state(si, ROAM_TRIGGER_SCAN);
break;
case ROAM_TRIGGER_SCAN_DONE:
/* Check for stale scan results, kick back to SCAN state if necessary */
if (current_time - si->roam_scan_done > 2 * config.roam_scan_interval) {
usteer_roam_set_state(si, ROAM_TRIGGER_SCAN);
break;
}
si_new = find_better_candidate(si);
if (!si_new)
break;
usteer_roam_set_state(si, ROAM_TRIGGER_WAIT_KICK);
break;
case ROAM_TRIGGER_WAIT_KICK:
if (si->signal > min_signal)
break;
usteer_roam_set_state(si, ROAM_TRIGGER_NOTIFY_KICK);
usteer_ubus_notify_client_disassoc(si);
break;
case ROAM_TRIGGER_NOTIFY_KICK:
if (current_time - si->roam_event < config.roam_kick_delay * 100)
break;
usteer_roam_set_state(si, ROAM_TRIGGER_KICK);
break;
case ROAM_TRIGGER_KICK:
usteer_ubus_kick_client(si);
usteer_roam_set_state(si, ROAM_TRIGGER_IDLE);
return true;
}
return false;
}
static void
usteer_local_node_roam_check(struct usteer_local_node *ln)
{
struct sta_info *si;
int min_signal;
if (config.roam_scan_snr)
min_signal = config.roam_scan_snr;
else if (config.roam_trigger_snr)
min_signal = config.roam_trigger_snr;
else
return;
usteer_update_time();
min_signal = snr_to_signal(&ln->node, min_signal);
list_for_each_entry(si, &ln->node.sta_info, node_list) {
if (!si->connected || si->signal >= min_signal || is_active_client(si) ||
current_time - si->roam_kick < config.roam_trigger_interval) {
usteer_roam_set_state(si, ROAM_TRIGGER_IDLE);
continue;
}
/*
* If the state machine kicked a client, other clients should wait
* until the next turn
*/
if (usteer_roam_trigger_sm(si))
return;
}
}
static void
usteer_local_node_snr_kick(struct usteer_local_node *ln)
{
struct sta_info *si;
int min_signal;
if (!config.min_snr)
return;
min_signal = snr_to_signal(&ln->node, config.min_snr);
list_for_each_entry(si, &ln->node.sta_info, node_list) {
if (!si->connected)
continue;
if (is_active_client(si))
continue;
if (si->signal >= min_signal)
continue;
si->kick_count++;
MSG(VERBOSE, "Kicking client "MAC_ADDR_FMT" due to low SNR, signal=%d\n",
MAC_ADDR_DATA(si->sta->addr), si->signal);
usteer_ubus_kick_client(si);
return;
}
}
void
usteer_local_node_kick(struct usteer_local_node *ln)
{
struct usteer_node *node = &ln->node;
struct sta_info *kick1 = NULL, *kick2 = NULL;
struct sta_info *candidate = NULL;
struct sta_info *si;
usteer_local_node_roam_check(ln);
usteer_local_node_snr_kick(ln);
if (!config.load_kick_enabled || !config.load_kick_threshold ||
!config.load_kick_delay)
return;
if (node->load < config.load_kick_threshold) {
MSG_T("load_kick_threshold",
"is below load for this node (config=%i) (real=%i)\n",
config.load_kick_threshold, node->load);
ln->load_thr_count = 0;
return;
}
if (++ln->load_thr_count <=
DIV_ROUND_UP(config.load_kick_delay, config.local_sta_update)) {
MSG_T("load_kick_delay", "delay kicking (config=%i)\n",
config.load_kick_delay);
return;
}
MSG(VERBOSE, "AP load threshold exceeded on %s (%d), try to kick a client\n",
usteer_node_name(node), node->load);
ln->load_thr_count = 0;
if (node->n_assoc < config.load_kick_min_clients) {
MSG_T("load_kick_min_clients",
"min limit reached, stop kicking clients on this node "
"(n_assoc=%i) (config=%i)\n",
node->n_assoc, config.load_kick_min_clients);
return;
}
list_for_each_entry(si, &ln->node.sta_info, node_list) {
struct sta_info *tmp;
if (!si->connected)
continue;
if (is_active_client(si))
continue;
if (is_more_kickable(kick1, si))
kick1 = si;
tmp = find_better_candidate(si);
if (!tmp)
continue;
if (is_more_kickable(kick2, si)) {
kick2 = si;
candidate = tmp;
}
}
if (!kick1)
return;
if (kick2)
kick1 = kick2;
MSG(VERBOSE, "Kicking client "MAC_ADDR_FMT", signal=%d, better_candidate=%s\n",
MAC_ADDR_DATA(kick1->sta->addr), kick1->signal,
candidate ? usteer_node_name(candidate->node) : "(none)");
kick1->kick_count++;
usteer_ubus_kick_client(kick1);
}