-
Notifications
You must be signed in to change notification settings - Fork 7
/
policy.c
561 lines (453 loc) · 13.2 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* 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 "event.h"
static bool
below_assoc_threshold(struct sta_info *si_cur, struct sta_info *si_new)
{
int n_assoc_cur = si_cur->node->n_assoc;
int n_assoc_new = si_new->node->n_assoc;
bool ref_5g = si_cur->node->freq > 4000;
bool node_5g = si_new->node->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;
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;
return is_better;
}
static bool
below_load_threshold(struct sta_info *si)
{
return si->node->n_assoc >= config.load_kick_min_clients &&
si->node->load > config.load_kick_threshold;
}
static bool
has_better_load(struct sta_info *si_cur, struct sta_info *si_new)
{
return !below_load_threshold(si_cur) && below_load_threshold(si_new);
}
static bool
below_max_assoc(struct sta_info *si)
{
struct usteer_node *node = si->node;
return !node->max_assoc || node->n_assoc < node->max_assoc;
}
static bool
over_min_signal(struct sta_info *si)
{
if (config.min_snr && si->signal < usteer_snr_to_signal(si->node, config.min_snr))
return false;
if (config.roam_trigger_snr && si->signal < usteer_snr_to_signal(si->node, config.roam_trigger_snr))
return false;
return true;
}
static uint32_t
is_better_candidate(struct sta_info *si_cur, struct sta_info *si_new)
{
uint32_t reasons = 0;
if (!below_max_assoc(si_new))
return 0;
if (!over_min_signal(si_new))
return 0;
if (below_assoc_threshold(si_cur, si_new) &&
!below_assoc_threshold(si_new, si_cur))
reasons |= (1 << UEV_SELECT_REASON_NUM_ASSOC);
if (better_signal_strength(si_cur, si_new))
reasons |= (1 << UEV_SELECT_REASON_SIGNAL);
if (has_better_load(si_cur, si_new) &&
!has_better_load(si_cur, si_new))
reasons |= (1 << UEV_SELECT_REASON_LOAD);
return reasons;
}
static struct sta_info *
find_better_candidate(struct sta_info *si_ref, struct uevent *ev, uint32_t required_criteria, uint64_t max_age)
{
struct sta_info *si;
struct sta *sta = si_ref->sta;
uint32_t reasons;
list_for_each_entry(si, &sta->nodes, list) {
if (si == si_ref)
continue;
if (current_time - si->seen > config.seen_policy_timeout)
continue;
if (strcmp(si->node->ssid, si_ref->node->ssid) != 0)
continue;
if (max_age && max_age < current_time - si->seen)
continue;
reasons = is_better_candidate(si_ref, si);
if (!reasons)
continue;
if (!(reasons & required_criteria))
continue;
if (ev) {
ev->si_other = si;
ev->select_reasons = reasons;
}
return si;
}
return NULL;
}
int
usteer_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;
}
/* Handle events coming in from hostapd. The function will evaluate if hostapd should
* respond to the request */
bool
usteer_check_request(struct sta_info *si, enum usteer_event_type type)
{
struct uevent ev = {
.si_cur = si,
};
int min_signal;
bool ret = true;
/* auth requests are always accepted */
if (type == EVENT_TYPE_AUTH)
goto out;
if (type == EVENT_TYPE_ASSOC) {
/* Check if assoc request has lower signal than min_signal.
* If this is the case, block assoc even when assoc steering is enabled.
*
* Otherwise, the client potentially ends up in a assoc - kick loop.
*/
if (config.min_snr && si->signal < usteer_snr_to_signal(si->node, config.min_snr)) {
ev.reason = UEV_REASON_LOW_SIGNAL;
ev.threshold.cur = si->signal;
ev.threshold.ref = usteer_snr_to_signal(si->node, config.min_snr);
ret = false;
goto out;
} else if (!config.assoc_steering) {
goto out;
}
}
/* Reject and request that has a too low signal quality */
min_signal = usteer_snr_to_signal(si->node, config.min_connect_snr);
if (si->signal < min_signal) {
ev.reason = UEV_REASON_LOW_SIGNAL;
ev.threshold.cur = si->signal;
ev.threshold.ref = min_signal;
ret = false;
goto out;
}
/* Reject if the station is younger than the Initial connect delay before responding to probe requests
* this allows other APs to see packets as well
*/
if (type == EVENT_TYPE_PROBE && current_time - si->created < config.initial_connect_delay) {
ev.reason = UEV_REASON_CONNECT_DELAY;
ev.threshold.cur = current_time - si->created;
ev.threshold.ref = config.initial_connect_delay;
ret = false;
goto out;
}
/* Check if any of the other APs are better suited for accepting this station */
if (!find_better_candidate(si, &ev, UEV_SELECT_REASON_ALL, 0))
goto out;
/* Reject the request if a better AP was found */
ev.reason = UEV_REASON_BETTER_CANDIDATE;
ev.node_cur = si->node;
ret = false;
out:
switch (type) {
case EVENT_TYPE_PROBE:
ev.type = UEV_PROBE_REQ_ACCEPT;
break;
case EVENT_TYPE_ASSOC:
ev.type = UEV_ASSOC_REQ_ACCEPT;
break;
case EVENT_TYPE_AUTH:
ev.type = UEV_AUTH_REQ_ACCEPT;
break;
default:
break;
}
/* Turn event type into REJECT if we want to reject */
if (!ret)
ev.type++;
if (!ret && si->stats[type].blocked_cur >= config.max_retry_band) {
ev.reason = UEV_REASON_RETRY_EXCEEDED;
ev.threshold.cur = si->stats[type].blocked_cur;
ev.threshold.ref = config.max_retry_band;
}
/* send the event */
usteer_event(&ev);
return ret;
}
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,
struct uevent *ev)
{
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;
usteer_event(ev);
}
static void
usteer_roam_sm_start_scan(struct sta_info *si, struct uevent *ev)
{
/* Start scanning in case we are not timeout-constrained or timeout has expired */
if (!config.roam_scan_timeout ||
current_time > si->roam_scan_timeout_start + config.roam_scan_timeout) {
usteer_roam_set_state(si, ROAM_TRIGGER_SCAN, ev);
return;
}
/* We are currently in scan timeout / cooldown.
* Check if we are in ROAM_TRIGGER_IDLE state. Enter this state if not.
*/
if (si->roam_state == ROAM_TRIGGER_IDLE)
return;
/* Enter idle state */
usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, ev);
}
static bool
usteer_roam_sm_found_better_node(struct sta_info *si, struct uevent *ev, enum roam_trigger_state next_state)
{
uint64_t max_age = 2 * config.roam_scan_interval;
if (max_age > current_time - si->roam_scan_start)
max_age = current_time - si->roam_scan_start;
if (find_better_candidate(si, ev, (1 << UEV_SELECT_REASON_SIGNAL), max_age)) {
usteer_roam_set_state(si, next_state, ev);
return true;
}
return false;
}
static bool
usteer_roam_trigger_sm(struct sta_info *si)
{
struct uevent ev = {
.si_cur = si,
};
uint64_t min_signal;
min_signal = usteer_snr_to_signal(si->node, config.roam_trigger_snr);
switch (si->roam_state) {
case ROAM_TRIGGER_SCAN:
if (!si->roam_tries) {
si->roam_scan_start = current_time;
}
/* Check if we've found a better node regardless of the scan-interval */
if (usteer_roam_sm_found_better_node(si, &ev, ROAM_TRIGGER_SCAN_DONE))
break;
/* Only scan every scan-interval */
if (current_time - si->roam_event < config.roam_scan_interval)
break;
/* Check if no node was found within roam_scan_tries tries */
if (config.roam_scan_tries && si->roam_tries >= config.roam_scan_tries) {
if (!config.roam_scan_timeout) {
/* Prepare to kick client */
usteer_roam_set_state(si, ROAM_TRIGGER_WAIT_KICK, &ev);
} else {
/* Kick in scan timeout */
si->roam_scan_timeout_start = current_time;
usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, &ev);
}
break;
}
/* Send beacon-request to client */
usteer_ubus_trigger_client_scan(si);
usteer_roam_sm_start_scan(si, &ev);
break;
case ROAM_TRIGGER_IDLE:
usteer_roam_sm_start_scan(si, &ev);
break;
case ROAM_TRIGGER_SCAN_DONE:
if (usteer_roam_sm_found_better_node(si, &ev, ROAM_TRIGGER_WAIT_KICK))
break;
/* Kick back to SCAN state if candidate expired */
usteer_roam_sm_start_scan(si, &ev);
break;
case ROAM_TRIGGER_WAIT_KICK:
if (si->signal > min_signal)
break;
usteer_roam_set_state(si, ROAM_TRIGGER_NOTIFY_KICK, &ev);
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, &ev);
break;
case ROAM_TRIGGER_KICK:
usteer_ubus_kick_client(si);
usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, &ev);
return true;
}
return false;
}
static void
usteer_local_node_roam_check(struct usteer_local_node *ln, struct uevent *ev)
{
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 = usteer_snr_to_signal(&ln->node, min_signal);
list_for_each_entry(si, &ln->node.sta_info, node_list) {
if (si->connected != STA_CONNECTED || si->signal >= min_signal ||
current_time - si->roam_kick < config.roam_trigger_interval) {
usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, ev);
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)
{
unsigned int min_count = DIV_ROUND_UP(config.min_snr_kick_delay, config.local_sta_update);
struct uevent ev = {
.node_local = &ln->node,
};
struct sta_info *si;
int min_signal;
if (!config.min_snr)
return;
min_signal = usteer_snr_to_signal(&ln->node, config.min_snr);
ev.threshold.ref = min_signal;
list_for_each_entry(si, &ln->node.sta_info, node_list) {
if (si->connected != STA_CONNECTED)
continue;
if (si->signal >= min_signal) {
si->below_min_snr = 0;
continue;
} else {
si->below_min_snr++;
}
if (si->below_min_snr <= min_count)
continue;
si->kick_count++;
ev.type = UEV_SIGNAL_KICK;
ev.threshold.cur = si->signal;
ev.count = si->kick_count;
usteer_event(&ev);
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;
struct uevent ev = {
.node_local = &ln->node,
};
unsigned int min_count = DIV_ROUND_UP(config.load_kick_delay, config.local_sta_update);
usteer_local_node_roam_check(ln, &ev);
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) {
if (!ln->load_thr_count)
return;
ln->load_thr_count = 0;
ev.type = UEV_LOAD_KICK_RESET;
ev.threshold.cur = node->load;
ev.threshold.ref = config.load_kick_threshold;
goto out;
}
if (++ln->load_thr_count <= min_count) {
if (ln->load_thr_count > 1)
return;
ev.type = UEV_LOAD_KICK_TRIGGER;
ev.threshold.cur = node->load;
ev.threshold.ref = config.load_kick_threshold;
goto out;
}
ln->load_thr_count = 0;
if (node->n_assoc < config.load_kick_min_clients) {
ev.type = UEV_LOAD_KICK_MIN_CLIENTS;
ev.threshold.cur = node->n_assoc;
ev.threshold.ref = config.load_kick_min_clients;
goto out;
}
list_for_each_entry(si, &ln->node.sta_info, node_list) {
struct sta_info *tmp;
if (si->connected != STA_CONNECTED)
continue;
if (is_more_kickable(kick1, si))
kick1 = si;
tmp = find_better_candidate(si, NULL, (1 << UEV_SELECT_REASON_LOAD), 0);
if (!tmp)
continue;
if (is_more_kickable(kick2, si)) {
kick2 = si;
candidate = tmp;
}
}
if (!kick1) {
ev.type = UEV_LOAD_KICK_NO_CLIENT;
goto out;
}
if (kick2)
kick1 = kick2;
kick1->kick_count++;
ev.type = UEV_LOAD_KICK_CLIENT;
ev.si_cur = kick1;
ev.si_other = candidate;
ev.count = kick1->kick_count;
usteer_ubus_kick_client(kick1);
out:
usteer_event(&ev);
}