policy: make policy helpers more generic
[project/usteer.git] / policy.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 *
15 * Copyright (C) 2020 embedd.ch
16 * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
17 * Copyright (C) 2020 John Crispin <john@phrozen.org>
18 */
19
20 #include "usteer.h"
21 #include "node.h"
22 #include "event.h"
23
24 static bool
25 below_assoc_threshold(struct usteer_node *node_cur, struct usteer_node *node_new)
26 {
27 int n_assoc_cur = node_cur->n_assoc;
28 int n_assoc_new = node_new->n_assoc;
29 bool ref_5g = node_cur->freq > 4000;
30 bool node_5g = node_new->freq > 4000;
31
32 if (ref_5g && !node_5g)
33 n_assoc_new += config.band_steering_threshold;
34 else if (!ref_5g && node_5g)
35 n_assoc_cur += config.band_steering_threshold;
36
37 n_assoc_new += config.load_balancing_threshold;
38
39 return n_assoc_new <= n_assoc_cur;
40 }
41
42 static bool
43 better_signal_strength(int signal_cur, int signal_new)
44 {
45 const bool is_better = signal_new - signal_cur
46 > (int) config.signal_diff_threshold;
47
48 if (!config.signal_diff_threshold)
49 return false;
50
51 return is_better;
52 }
53
54 static bool
55 below_load_threshold(struct usteer_node *node)
56 {
57 return node->n_assoc >= config.load_kick_min_clients &&
58 node->load > config.load_kick_threshold;
59 }
60
61 static bool
62 has_better_load(struct usteer_node *node_cur, struct usteer_node *node_new)
63 {
64 return !below_load_threshold(node_cur) && below_load_threshold(node_new);
65 }
66
67 static bool
68 below_max_assoc(struct usteer_node *node)
69 {
70 return !node->max_assoc || node->n_assoc < node->max_assoc;
71 }
72
73 static bool
74 over_min_signal(struct usteer_node *node, int signal)
75 {
76 if (config.min_snr && signal < usteer_snr_to_signal(node, config.min_snr))
77 return false;
78
79 if (config.roam_trigger_snr && signal < usteer_snr_to_signal(node, config.roam_trigger_snr))
80 return false;
81
82 return true;
83 }
84
85 static uint32_t
86 is_better_candidate(struct sta_info *si_cur, struct sta_info *si_new)
87 {
88 struct usteer_node *current_node = si_cur->node;
89 struct usteer_node *new_node = si_new->node;
90 int current_signal = si_cur->signal;
91 int new_signal = si_new->signal;
92 uint32_t reasons = 0;
93
94 if (!below_max_assoc(new_node))
95 return 0;
96
97 if (!over_min_signal(new_node, new_signal))
98 return 0;
99
100 if (below_assoc_threshold(current_node, new_node) &&
101 !below_assoc_threshold(new_node, current_node))
102 reasons |= (1 << UEV_SELECT_REASON_NUM_ASSOC);
103
104 if (better_signal_strength(current_signal, new_signal))
105 reasons |= (1 << UEV_SELECT_REASON_SIGNAL);
106
107 if (has_better_load(current_node, new_node) &&
108 !has_better_load(current_node, new_node))
109 reasons |= (1 << UEV_SELECT_REASON_LOAD);
110
111 return reasons;
112 }
113
114 static struct sta_info *
115 find_better_candidate(struct sta_info *si_ref, struct uevent *ev, uint32_t required_criteria, uint64_t max_age)
116 {
117 struct sta_info *si;
118 struct sta *sta = si_ref->sta;
119 uint32_t reasons;
120
121 list_for_each_entry(si, &sta->nodes, list) {
122 if (si == si_ref)
123 continue;
124
125 if (current_time - si->seen > config.seen_policy_timeout)
126 continue;
127
128 if (strcmp(si->node->ssid, si_ref->node->ssid) != 0)
129 continue;
130
131 if (max_age && max_age < current_time - si->seen)
132 continue;
133
134 reasons = is_better_candidate(si_ref, si);
135 if (!reasons)
136 continue;
137
138 if (!(reasons & required_criteria))
139 continue;
140
141 if (ev) {
142 ev->si_other = si;
143 ev->select_reasons = reasons;
144 }
145
146 return si;
147 }
148
149 return NULL;
150 }
151
152 int
153 usteer_snr_to_signal(struct usteer_node *node, int snr)
154 {
155 int noise = -95;
156
157 if (snr < 0)
158 return snr;
159
160 if (node->noise)
161 noise = node->noise;
162
163 return noise + snr;
164 }
165
166 bool
167 usteer_check_request(struct sta_info *si, enum usteer_event_type type)
168 {
169 struct uevent ev = {
170 .si_cur = si,
171 };
172 int min_signal;
173 bool ret = true;
174
175 if (type == EVENT_TYPE_AUTH)
176 goto out;
177
178 if (type == EVENT_TYPE_ASSOC) {
179 /* Check if assoc request has lower signal than min_signal.
180 * If this is the case, block assoc even when assoc steering is enabled.
181 *
182 * Otherwise, the client potentially ends up in a assoc - kick loop.
183 */
184 if (config.min_snr && si->signal < usteer_snr_to_signal(si->node, config.min_snr)) {
185 ev.reason = UEV_REASON_LOW_SIGNAL;
186 ev.threshold.cur = si->signal;
187 ev.threshold.ref = usteer_snr_to_signal(si->node, config.min_snr);
188 ret = false;
189 goto out;
190 } else if (!config.assoc_steering) {
191 goto out;
192 }
193 }
194
195 min_signal = usteer_snr_to_signal(si->node, config.min_connect_snr);
196 if (si->signal < min_signal) {
197 ev.reason = UEV_REASON_LOW_SIGNAL;
198 ev.threshold.cur = si->signal;
199 ev.threshold.ref = min_signal;
200 ret = false;
201 goto out;
202 }
203
204 if (current_time - si->created < config.initial_connect_delay) {
205 ev.reason = UEV_REASON_CONNECT_DELAY;
206 ev.threshold.cur = current_time - si->created;
207 ev.threshold.ref = config.initial_connect_delay;
208 ret = false;
209 goto out;
210 }
211
212 if (!find_better_candidate(si, &ev, UEV_SELECT_REASON_ALL, 0))
213 goto out;
214
215 ev.reason = UEV_REASON_BETTER_CANDIDATE;
216 ev.node_cur = si->node;
217 ret = false;
218
219 out:
220 switch (type) {
221 case EVENT_TYPE_PROBE:
222 ev.type = UEV_PROBE_REQ_ACCEPT;
223 break;
224 case EVENT_TYPE_ASSOC:
225 ev.type = UEV_ASSOC_REQ_ACCEPT;
226 break;
227 case EVENT_TYPE_AUTH:
228 ev.type = UEV_AUTH_REQ_ACCEPT;
229 break;
230 default:
231 break;
232 }
233
234 if (!ret)
235 ev.type++;
236
237 if (!ret && si->stats[type].blocked_cur >= config.max_retry_band) {
238 ev.reason = UEV_REASON_RETRY_EXCEEDED;
239 ev.threshold.cur = si->stats[type].blocked_cur;
240 ev.threshold.ref = config.max_retry_band;
241 }
242 usteer_event(&ev);
243
244 return ret;
245 }
246
247 static bool
248 is_more_kickable(struct sta_info *si_cur, struct sta_info *si_new)
249 {
250 if (!si_cur)
251 return true;
252
253 if (si_new->kick_count > si_cur->kick_count)
254 return false;
255
256 return si_cur->signal > si_new->signal;
257 }
258
259 static void
260 usteer_roam_set_state(struct sta_info *si, enum roam_trigger_state state,
261 struct uevent *ev)
262 {
263 si->roam_event = current_time;
264
265 if (si->roam_state == state) {
266 if (si->roam_state == ROAM_TRIGGER_IDLE) {
267 si->roam_tries = 0;
268 return;
269 }
270
271 si->roam_tries++;
272 } else {
273 si->roam_tries = 0;
274 }
275
276 si->roam_state = state;
277 usteer_event(ev);
278 }
279
280 static void
281 usteer_roam_sm_start_scan(struct sta_info *si, struct uevent *ev)
282 {
283 /* Start scanning in case we are not timeout-constrained or timeout has expired */
284 if (!config.roam_scan_timeout ||
285 current_time > si->roam_scan_timeout_start + config.roam_scan_timeout) {
286 usteer_roam_set_state(si, ROAM_TRIGGER_SCAN, ev);
287 return;
288 }
289
290 /* We are currently in scan timeout / cooldown.
291 * Check if we are in ROAM_TRIGGER_IDLE state. Enter this state if not.
292 */
293 if (si->roam_state == ROAM_TRIGGER_IDLE)
294 return;
295
296 /* Enter idle state */
297 usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, ev);
298 }
299
300 static bool
301 usteer_roam_sm_found_better_node(struct sta_info *si, struct uevent *ev, enum roam_trigger_state next_state)
302 {
303 uint64_t max_age = 2 * config.roam_scan_interval;
304
305 if (max_age > current_time - si->roam_scan_start)
306 max_age = current_time - si->roam_scan_start;
307
308 if (find_better_candidate(si, ev, (1 << UEV_SELECT_REASON_SIGNAL), max_age)) {
309 usteer_roam_set_state(si, next_state, ev);
310 return true;
311 }
312
313 return false;
314 }
315
316 static bool
317 usteer_roam_trigger_sm(struct sta_info *si)
318 {
319 struct uevent ev = {
320 .si_cur = si,
321 };
322 uint64_t min_signal;
323
324 min_signal = usteer_snr_to_signal(si->node, config.roam_trigger_snr);
325
326 switch (si->roam_state) {
327 case ROAM_TRIGGER_SCAN:
328 if (!si->roam_tries) {
329 si->roam_scan_start = current_time;
330 }
331
332 /* Check if we've found a better node regardless of the scan-interval */
333 if (usteer_roam_sm_found_better_node(si, &ev, ROAM_TRIGGER_SCAN_DONE))
334 break;
335
336 /* Only scan every scan-interval */
337 if (current_time - si->roam_event < config.roam_scan_interval)
338 break;
339
340 /* Check if no node was found within roam_scan_tries tries */
341 if (config.roam_scan_tries && si->roam_tries >= config.roam_scan_tries) {
342 if (!config.roam_scan_timeout) {
343 /* Prepare to kick client */
344 usteer_roam_set_state(si, ROAM_TRIGGER_WAIT_KICK, &ev);
345 } else {
346 /* Kick in scan timeout */
347 si->roam_scan_timeout_start = current_time;
348 usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, &ev);
349 }
350 break;
351 }
352
353 /* Send beacon-request to client */
354 usteer_ubus_trigger_client_scan(si);
355 usteer_roam_sm_start_scan(si, &ev);
356 break;
357
358 case ROAM_TRIGGER_IDLE:
359 usteer_roam_sm_start_scan(si, &ev);
360 break;
361
362 case ROAM_TRIGGER_SCAN_DONE:
363 if (usteer_roam_sm_found_better_node(si, &ev, ROAM_TRIGGER_WAIT_KICK))
364 break;
365
366 /* Kick back to SCAN state if candidate expired */
367 usteer_roam_sm_start_scan(si, &ev);
368 break;
369
370 case ROAM_TRIGGER_WAIT_KICK:
371 if (si->signal > min_signal)
372 break;
373
374 usteer_roam_set_state(si, ROAM_TRIGGER_NOTIFY_KICK, &ev);
375 usteer_ubus_notify_client_disassoc(si);
376 break;
377 case ROAM_TRIGGER_NOTIFY_KICK:
378 if (current_time - si->roam_event < config.roam_kick_delay * 100)
379 break;
380
381 usteer_roam_set_state(si, ROAM_TRIGGER_KICK, &ev);
382 break;
383 case ROAM_TRIGGER_KICK:
384 usteer_ubus_kick_client(si);
385 usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, &ev);
386 return true;
387 }
388
389 return false;
390 }
391
392 static void
393 usteer_local_node_roam_check(struct usteer_local_node *ln, struct uevent *ev)
394 {
395 struct sta_info *si;
396 int min_signal;
397
398 if (config.roam_scan_snr)
399 min_signal = config.roam_scan_snr;
400 else if (config.roam_trigger_snr)
401 min_signal = config.roam_trigger_snr;
402 else
403 return;
404
405 usteer_update_time();
406 min_signal = usteer_snr_to_signal(&ln->node, min_signal);
407
408 list_for_each_entry(si, &ln->node.sta_info, node_list) {
409 if (si->connected != STA_CONNECTED || si->signal >= min_signal ||
410 current_time - si->roam_kick < config.roam_trigger_interval) {
411 usteer_roam_set_state(si, ROAM_TRIGGER_IDLE, ev);
412 continue;
413 }
414
415 /*
416 * If the state machine kicked a client, other clients should wait
417 * until the next turn
418 */
419 if (usteer_roam_trigger_sm(si))
420 return;
421 }
422 }
423
424 static void
425 usteer_local_node_snr_kick(struct usteer_local_node *ln)
426 {
427 unsigned int min_count = DIV_ROUND_UP(config.min_snr_kick_delay, config.local_sta_update);
428 struct uevent ev = {
429 .node_local = &ln->node,
430 };
431 struct sta_info *si;
432 int min_signal;
433
434 if (!config.min_snr)
435 return;
436
437 min_signal = usteer_snr_to_signal(&ln->node, config.min_snr);
438 ev.threshold.ref = min_signal;
439
440 list_for_each_entry(si, &ln->node.sta_info, node_list) {
441 if (si->connected != STA_CONNECTED)
442 continue;
443
444 if (si->signal >= min_signal) {
445 si->below_min_snr = 0;
446 continue;
447 } else {
448 si->below_min_snr++;
449 }
450
451 if (si->below_min_snr <= min_count)
452 continue;
453
454 si->kick_count++;
455
456 ev.type = UEV_SIGNAL_KICK;
457 ev.threshold.cur = si->signal;
458 ev.count = si->kick_count;
459 usteer_event(&ev);
460
461 usteer_ubus_kick_client(si);
462 return;
463 }
464 }
465
466 void
467 usteer_local_node_kick(struct usteer_local_node *ln)
468 {
469 struct usteer_node *node = &ln->node;
470 struct sta_info *kick1 = NULL, *kick2 = NULL;
471 struct sta_info *candidate = NULL;
472 struct sta_info *si;
473 struct uevent ev = {
474 .node_local = &ln->node,
475 };
476 unsigned int min_count = DIV_ROUND_UP(config.load_kick_delay, config.local_sta_update);
477
478 usteer_local_node_roam_check(ln, &ev);
479 usteer_local_node_snr_kick(ln);
480
481 if (!config.load_kick_enabled || !config.load_kick_threshold ||
482 !config.load_kick_delay)
483 return;
484
485 if (node->load < config.load_kick_threshold) {
486 if (!ln->load_thr_count)
487 return;
488
489 ln->load_thr_count = 0;
490 ev.type = UEV_LOAD_KICK_RESET;
491 ev.threshold.cur = node->load;
492 ev.threshold.ref = config.load_kick_threshold;
493 goto out;
494 }
495
496 if (++ln->load_thr_count <= min_count) {
497 if (ln->load_thr_count > 1)
498 return;
499
500 ev.type = UEV_LOAD_KICK_TRIGGER;
501 ev.threshold.cur = node->load;
502 ev.threshold.ref = config.load_kick_threshold;
503 goto out;
504 }
505
506 ln->load_thr_count = 0;
507 if (node->n_assoc < config.load_kick_min_clients) {
508 ev.type = UEV_LOAD_KICK_MIN_CLIENTS;
509 ev.threshold.cur = node->n_assoc;
510 ev.threshold.ref = config.load_kick_min_clients;
511 goto out;
512 }
513
514 list_for_each_entry(si, &ln->node.sta_info, node_list) {
515 struct sta_info *tmp;
516
517 if (si->connected != STA_CONNECTED)
518 continue;
519
520 if (is_more_kickable(kick1, si))
521 kick1 = si;
522
523 tmp = find_better_candidate(si, NULL, (1 << UEV_SELECT_REASON_LOAD), 0);
524 if (!tmp)
525 continue;
526
527 if (is_more_kickable(kick2, si)) {
528 kick2 = si;
529 candidate = tmp;
530 }
531 }
532
533 if (!kick1) {
534 ev.type = UEV_LOAD_KICK_NO_CLIENT;
535 goto out;
536 }
537
538 if (kick2)
539 kick1 = kick2;
540
541 kick1->kick_count++;
542
543 ev.type = UEV_LOAD_KICK_CLIENT;
544 ev.si_cur = kick1;
545 ev.si_other = candidate;
546 ev.count = kick1->kick_count;
547
548 usteer_ubus_kick_client(kick1);
549
550 out:
551 usteer_event(&ev);
552 }